summaryrefslogtreecommitdiffhomepage
path: root/src/features/chat/logic/tool-summary.ts
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-10-24 18:48:54 +0900
committerAdam Malczewski <[email protected]>2026-10-24 18:48:54 +0900
commit34526ff48985643bed532b102e8943fa69e77a79 (patch)
tree4208ec0f5f1ef2cf1a9cd9710a442ce10a5f0c61 /src/features/chat/logic/tool-summary.ts
parentdaefad66bdf8dee132d701a3b43ce0d227dde241 (diff)
parentb666d77df8cf4da6ae40fb5637268926f02cc44d (diff)
downloaddispatch-web-34526ff48985643bed532b102e8943fa69e77a79.tar.gz
dispatch-web-34526ff48985643bed532b102e8943fa69e77a79.zip
Merge branch 'feature/text-file-embeds' into dev
Diffstat (limited to 'src/features/chat/logic/tool-summary.ts')
-rw-r--r--src/features/chat/logic/tool-summary.ts19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/features/chat/logic/tool-summary.ts b/src/features/chat/logic/tool-summary.ts
index f6d1569..2380adc 100644
--- a/src/features/chat/logic/tool-summary.ts
+++ b/src/features/chat/logic/tool-summary.ts
@@ -74,7 +74,24 @@ function asArgs(input: unknown): Args {
function serialize(input: unknown, pretty = false): string {
if (typeof input === "string") return input;
try {
- return JSON.stringify(input, null, pretty ? 2 : undefined) ?? String(input);
+ // Detect ancestor cycles before the native serializer recurses. Bun's
+ // pretty JSON cycle detection can otherwise consume seconds of UI time.
+ // Track ancestors, not every visited object: shared non-cyclic values are valid.
+ const ancestors: object[] = [];
+ return (
+ JSON.stringify(
+ input,
+ function (this: unknown, _key, value: unknown) {
+ if (value !== null && typeof value === "object") {
+ while (ancestors.length && ancestors.at(-1) !== this) ancestors.pop();
+ if (ancestors.includes(value)) throw new TypeError("Circular tool input");
+ ancestors.push(value);
+ }
+ return value;
+ },
+ pretty ? 2 : undefined,
+ ) ?? String(input)
+ );
} catch {
return "[unserializable input]";
}