Skip to content

Commit 23b925e

Browse files
committed
Add cywise_truncate_string helper and use it in RemoteAction to limit large JSON output
1 parent 70dcda1 commit 23b925e

3 files changed

Lines changed: 67 additions & 4 deletions

File tree

app/AgentSquad/Actions/RemoteAction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ public function body(): string
161161
return new FailedAnswer("Remote action call failed for action {$this->name()}", $chainOfThought);
162162
}
163163

164-
$chainOfThought[] = new ThoughtActionObservation("Call the remote action {$this->name()} endpoint.", "call[{$action->url}]", "The endpoint has been called and the call succeeded: " . json_encode($data));
164+
$chainOfThought[] = new ThoughtActionObservation("Call the remote action {$this->name()} endpoint.", "call[{$action->url}]", "The endpoint has been called and the call succeeded: " . cywise_truncate_string(json_encode($data)));
165165

166166
// Build the response
167167
if (empty($action->response_template)) {
168168
$transformation = $data;
169-
$chainOfThought[] = new ThoughtActionObservation("Return data from action {$this->name()} as-is.", "transform[" . json_encode($data) . "]", "The data have not been transformed: " . json_encode($transformation));
169+
$chainOfThought[] = new ThoughtActionObservation("Return data from action {$this->name()} as-is.", "transform[" . cywise_truncate_string(json_encode($data)) . "]", "The data have not been transformed: " . json_encode($transformation));
170170
} else {
171171
$transformation = $this->buildResponse($action->response_template, $payload['params'], $data);
172-
$chainOfThought[] = new ThoughtActionObservation("Return data from action {$this->name()} after transformation.", "transform[" . json_encode($data) . "]", "The data have been transformed: {$transformation}");
172+
$chainOfThought[] = new ThoughtActionObservation("Return data from action {$this->name()} after transformation.", "transform[" . cywise_truncate_string(json_encode($data)) . "]", "The data have been transformed: {$transformation}");
173173
}
174174
return new SuccessfulAnswer(
175175
is_string($transformation) ? $transformation : json_encode($transformation),

app/Helpers/Helpers.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,14 @@ function app_config_override(): array
358358
}
359359
}
360360
}
361-
361+
if (!function_exists('cywise_truncate_string')) {
362+
function cywise_truncate_string(string $str, int $size = 500): string
363+
{
364+
if (mb_strlen($str) <= $size) {
365+
return $str;
366+
}
367+
$start = mb_substr($str, 0, 50);
368+
$end = mb_substr($str, -50);
369+
return $start . '[...]' . $end;
370+
}
371+
}

tests/Unit/WithoutDb/HelpersTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,56 @@
167167
"[END 2x REPEATED LINE]",
168168
]);
169169
});
170+
171+
test('truncate string shorter than limit', function () {
172+
$str = 'Short string';
173+
expect(cywise_truncate_string($str, 50))->toEqual($str);
174+
});
175+
176+
test('truncate string equal to limit', function () {
177+
$str = str_repeat('a', 100);
178+
expect(cywise_truncate_string($str, 100))->toEqual($str);
179+
});
180+
181+
test('truncate string longer than limit', function () {
182+
183+
$start = str_repeat('a', 50);
184+
$middle = str_repeat('b', 10);
185+
$end = str_repeat('c', 50);
186+
$str = $start . $middle . $end;
187+
188+
// Total length is 110, limit is 100
189+
$expected = $start . '[...]' . $end;
190+
expect(cywise_truncate_string($str, 100))->toEqual($expected);
191+
});
192+
193+
test('truncate string with default limit (500)', function () {
194+
$str = str_repeat('a', 501);
195+
$expected = str_repeat('a', 50) . '[...]' . str_repeat('a', 50);
196+
expect(cywise_truncate_string($str))->toEqual($expected);
197+
});
198+
199+
test('truncate string with multibyte characters', function () {
200+
201+
$start = str_repeat('é', 50);
202+
$middle = '🚀';
203+
$end = str_repeat('à', 50);
204+
$str = $start . $middle . $end;
205+
206+
// Total mb_length is 101, limit is 100
207+
$expected = $start . '[...]' . $end;
208+
expect(cywise_truncate_string($str, 100))->toEqual($expected);
209+
});
210+
211+
test('truncate string with very short string but limit smaller than 100', function () {
212+
213+
$str = "This is a string that is longer than ten characters."; // 52 chars
214+
$limit = 10;
215+
216+
// Since length (52) > limit (10), it will take first 50 and last 50.
217+
$start = mb_substr($str, 0, 50);
218+
$end = mb_substr($str, -50);
219+
$expected = $start . '[...]' . $end;
220+
221+
expect(cywise_truncate_string($str, $limit))->toEqual($expected);
222+
});

0 commit comments

Comments
 (0)