illuma-agents 1.0.16 → 1.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/agents/AgentContext.cjs +3 -1
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +18 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +79 -32
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +5 -3
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +1 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/index.cjs +10 -1
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +7 -8
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +15 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +11 -6
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +16 -8
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +9 -2
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +17 -10
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/stream.cjs +30 -16
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +209 -47
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +73 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +1 -0
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +3 -1
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/utils/contextAnalytics.cjs +66 -0
- package/dist/cjs/utils/contextAnalytics.cjs.map +1 -0
- package/dist/cjs/utils/run.cjs.map +1 -1
- package/dist/cjs/utils/toonFormat.cjs +388 -0
- package/dist/cjs/utils/toonFormat.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +3 -1
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +19 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +81 -34
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +5 -3
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +1 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/index.mjs +10 -1
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +7 -8
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +4 -2
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +11 -6
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +18 -10
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +10 -3
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +19 -12
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/stream.mjs +30 -16
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +208 -48
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +73 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +1 -0
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +3 -1
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/utils/contextAnalytics.mjs +64 -0
- package/dist/esm/utils/contextAnalytics.mjs.map +1 -0
- package/dist/esm/utils/run.mjs.map +1 -1
- package/dist/esm/utils/toonFormat.mjs +381 -0
- package/dist/esm/utils/toonFormat.mjs.map +1 -0
- package/dist/types/common/enum.d.ts +17 -0
- package/dist/types/graphs/Graph.d.ts +8 -0
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +19 -0
- package/dist/types/types/tools.d.ts +3 -1
- package/dist/types/utils/contextAnalytics.d.ts +37 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/toonFormat.d.ts +111 -0
- package/package.json +3 -2
- package/src/agents/AgentContext.ts +28 -20
- package/src/common/enum.ts +18 -0
- package/src/graphs/Graph.ts +152 -62
- package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +495 -473
- package/src/llm/bedrock/index.ts +47 -35
- package/src/llm/openrouter/index.ts +11 -1
- package/src/llm/vertexai/index.ts +9 -10
- package/src/messages/cache.ts +104 -55
- package/src/messages/core.ts +29 -19
- package/src/messages/format.ts +14 -3
- package/src/messages/tools.ts +20 -13
- package/src/scripts/simple.ts +1 -1
- package/src/specs/emergency-prune.test.ts +407 -355
- package/src/stream.ts +28 -20
- package/src/tools/ProgrammaticToolCalling.ts +246 -52
- package/src/tools/ToolNode.ts +78 -5
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +155 -0
- package/src/tools/search/jina-reranker.test.ts +32 -28
- package/src/tools/search/search.ts +3 -1
- package/src/tools/search/tool.ts +16 -7
- package/src/types/tools.ts +3 -1
- package/src/utils/contextAnalytics.ts +103 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/llmConfig.ts +8 -1
- package/src/utils/run.ts +5 -4
- package/src/utils/toonFormat.ts +475 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Context Analytics Utility
|
|
5
|
+
*
|
|
6
|
+
* Provides context analytics data for observability/traces.
|
|
7
|
+
* No console logging - just data structures for event emission.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Build context analytics for traces (no logging)
|
|
11
|
+
*/
|
|
12
|
+
function buildContextAnalytics(messages, options) {
|
|
13
|
+
const { tokenCounter, maxContextTokens, instructionTokens, indexTokenCountMap, } = options;
|
|
14
|
+
// Calculate total tokens
|
|
15
|
+
let totalTokens = 0;
|
|
16
|
+
const breakdown = {};
|
|
17
|
+
for (let i = 0; i < messages.length; i++) {
|
|
18
|
+
const msg = messages[i];
|
|
19
|
+
const type = msg.getType();
|
|
20
|
+
let tokens = 0;
|
|
21
|
+
if (indexTokenCountMap && indexTokenCountMap[i] != null) {
|
|
22
|
+
tokens = indexTokenCountMap[i];
|
|
23
|
+
}
|
|
24
|
+
else if (tokenCounter) {
|
|
25
|
+
try {
|
|
26
|
+
tokens = tokenCounter(msg);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// Estimate from content length
|
|
30
|
+
const content = typeof msg.content === 'string'
|
|
31
|
+
? msg.content
|
|
32
|
+
: JSON.stringify(msg.content);
|
|
33
|
+
tokens = Math.ceil(content.length / 4);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
totalTokens += tokens;
|
|
37
|
+
if (!breakdown[type]) {
|
|
38
|
+
breakdown[type] = { tokens: 0, percent: 0 };
|
|
39
|
+
}
|
|
40
|
+
breakdown[type].tokens += tokens;
|
|
41
|
+
}
|
|
42
|
+
// Calculate percentages
|
|
43
|
+
for (const type of Object.keys(breakdown)) {
|
|
44
|
+
breakdown[type].percent =
|
|
45
|
+
totalTokens > 0
|
|
46
|
+
? Math.round((breakdown[type].tokens / totalTokens) * 1000) / 10
|
|
47
|
+
: 0;
|
|
48
|
+
}
|
|
49
|
+
// Calculate utilization
|
|
50
|
+
let utilizationPercent;
|
|
51
|
+
if (maxContextTokens && maxContextTokens > 0) {
|
|
52
|
+
utilizationPercent =
|
|
53
|
+
Math.round((totalTokens / maxContextTokens) * 1000) / 10;
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
messageCount: messages.length,
|
|
57
|
+
totalTokens,
|
|
58
|
+
maxContextTokens,
|
|
59
|
+
instructionTokens,
|
|
60
|
+
utilizationPercent,
|
|
61
|
+
breakdown,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
exports.buildContextAnalytics = buildContextAnalytics;
|
|
66
|
+
//# sourceMappingURL=contextAnalytics.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contextAnalytics.cjs","sources":["../../../src/utils/contextAnalytics.ts"],"sourcesContent":["/**\n * Context Analytics Utility\n *\n * Provides context analytics data for observability/traces.\n * No console logging - just data structures for event emission.\n */\n\nimport type { BaseMessage } from '@langchain/core/messages';\nimport type { TokenCounter } from '@/types/run';\n\n/**\n * Context analytics data for traces\n */\nexport interface ContextAnalytics {\n /** Total messages in context */\n messageCount: number;\n /** Total tokens in context */\n totalTokens: number;\n /** Maximum allowed context tokens */\n maxContextTokens?: number;\n /** Instruction/system tokens */\n instructionTokens?: number;\n /** Context utilization percentage (0-100) */\n utilizationPercent?: number;\n /** Breakdown by message type */\n breakdown?: Record<string, { tokens: number; percent: number }>;\n}\n\n/**\n * Build context analytics for traces (no logging)\n */\nexport function buildContextAnalytics(\n messages: BaseMessage[],\n options: {\n tokenCounter?: TokenCounter;\n maxContextTokens?: number;\n instructionTokens?: number;\n indexTokenCountMap?: Record<string, number | undefined>;\n }\n): ContextAnalytics {\n const {\n tokenCounter,\n maxContextTokens,\n instructionTokens,\n indexTokenCountMap,\n } = options;\n\n // Calculate total tokens\n let totalTokens = 0;\n const breakdown: Record<string, { tokens: number; percent: number }> = {};\n\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg.getType();\n\n let tokens = 0;\n if (indexTokenCountMap && indexTokenCountMap[i] != null) {\n tokens = indexTokenCountMap[i]!;\n } else if (tokenCounter) {\n try {\n tokens = tokenCounter(msg);\n } catch {\n // Estimate from content length\n const content =\n typeof msg.content === 'string'\n ? msg.content\n : JSON.stringify(msg.content);\n tokens = Math.ceil(content.length / 4);\n }\n }\n\n totalTokens += tokens;\n\n if (!breakdown[type]) {\n breakdown[type] = { tokens: 0, percent: 0 };\n }\n breakdown[type].tokens += tokens;\n }\n\n // Calculate percentages\n for (const type of Object.keys(breakdown)) {\n breakdown[type].percent =\n totalTokens > 0\n ? Math.round((breakdown[type].tokens / totalTokens) * 1000) / 10\n : 0;\n }\n\n // Calculate utilization\n let utilizationPercent: number | undefined;\n if (maxContextTokens && maxContextTokens > 0) {\n utilizationPercent =\n Math.round((totalTokens / maxContextTokens) * 1000) / 10;\n }\n\n return {\n messageCount: messages.length,\n totalTokens,\n maxContextTokens,\n instructionTokens,\n utilizationPercent,\n breakdown,\n };\n}\n"],"names":[],"mappings":";;AAAA;;;;;AAKG;AAuBH;;AAEG;AACa,SAAA,qBAAqB,CACnC,QAAuB,EACvB,OAKC,EAAA;IAED,MAAM,EACJ,YAAY,EACZ,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,GACnB,GAAG,OAAO;;IAGX,IAAI,WAAW,GAAG,CAAC;IACnB,MAAM,SAAS,GAAwD,EAAE;AAEzE,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;AACxC,QAAA,MAAM,GAAG,GAAG,QAAQ,CAAC,CAAC,CAAC;AACvB,QAAA,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,EAAE;QAE1B,IAAI,MAAM,GAAG,CAAC;QACd,IAAI,kBAAkB,IAAI,kBAAkB,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE;AACvD,YAAA,MAAM,GAAG,kBAAkB,CAAC,CAAC,C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cjs","sources":["../../../src/utils/run.ts"],"sourcesContent":["import { CallbackManagerForChainRun } from '@langchain/core/callbacks/manager';\nimport {\n mergeConfigs,\n patchConfig,\n Runnable,\n RunnableConfig,\n} from '@langchain/core/runnables';\nimport { AsyncLocalStorageProviderSingleton } from '@langchain/core/singletons';\n\n/**\n * Delays the execution for a specified number of milliseconds.\n *\n * @param {number} ms - The number of milliseconds to delay.\n * @return {Promise<void>} A promise that resolves after the specified delay.\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface RunnableCallableArgs extends Partial<any> {\n name?: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n func: (...args: any[]) => any;\n tags?: string[];\n trace?: boolean;\n recurse?: boolean;\n}\n\nexport class RunnableCallable<I = unknown, O = unknown> extends Runnable<I, O> {\n lc_namespace: string[] = ['langgraph'];\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n func: (...args: any[]) => any;\n\n tags?: string[];\n\n config?: RunnableConfig;\n\n trace: boolean = true;\n\n recurse: boolean = true;\n\n constructor(fields: RunnableCallableArgs) {\n super();\n this.name = fields.name ?? fields.func.name;\n this.func = fields.func;\n this.config = fields.tags ? { tags: fields.tags } : undefined;\n this.trace = fields.trace ?? this.trace;\n this.recurse = fields.recurse ?? this.recurse;\n }\n\n protected async _tracedInvoke(\n input: I,\n config?: Partial<RunnableConfig>,\n runManager?: CallbackManagerForChainRun\n ): Promise<O> {\n return new Promise<O>((resolve, reject) => {\n // Defensive check: ensure runManager has getChild method before calling\n const childCallbacks
|
|
1
|
+
{"version":3,"file":"run.cjs","sources":["../../../src/utils/run.ts"],"sourcesContent":["import { CallbackManagerForChainRun } from '@langchain/core/callbacks/manager';\nimport {\n mergeConfigs,\n patchConfig,\n Runnable,\n RunnableConfig,\n} from '@langchain/core/runnables';\nimport { AsyncLocalStorageProviderSingleton } from '@langchain/core/singletons';\n\n/**\n * Delays the execution for a specified number of milliseconds.\n *\n * @param {number} ms - The number of milliseconds to delay.\n * @return {Promise<void>} A promise that resolves after the specified delay.\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport interface RunnableCallableArgs extends Partial<any> {\n name?: string;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n func: (...args: any[]) => any;\n tags?: string[];\n trace?: boolean;\n recurse?: boolean;\n}\n\nexport class RunnableCallable<I = unknown, O = unknown> extends Runnable<I, O> {\n lc_namespace: string[] = ['langgraph'];\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n func: (...args: any[]) => any;\n\n tags?: string[];\n\n config?: RunnableConfig;\n\n trace: boolean = true;\n\n recurse: boolean = true;\n\n constructor(fields: RunnableCallableArgs) {\n super();\n this.name = fields.name ?? fields.func.name;\n this.func = fields.func;\n this.config = fields.tags ? { tags: fields.tags } : undefined;\n this.trace = fields.trace ?? this.trace;\n this.recurse = fields.recurse ?? this.recurse;\n }\n\n protected async _tracedInvoke(\n input: I,\n config?: Partial<RunnableConfig>,\n runManager?: CallbackManagerForChainRun\n ): Promise<O> {\n return new Promise<O>((resolve, reject) => {\n // Defensive check: ensure runManager has getChild method before calling\n const childCallbacks =\n typeof runManager?.getChild === 'function'\n ? runManager.getChild()\n : undefined;\n const childConfig = patchConfig(config, {\n callbacks: childCallbacks,\n });\n void AsyncLocalStorageProviderSingleton.runWithConfig(\n childConfig,\n async () => {\n try {\n const output = await this.func(input, childConfig);\n resolve(output);\n } catch (e) {\n reject(e);\n }\n }\n );\n });\n }\n\n async invoke(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n options?: Partial<RunnableConfig> | undefined\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ): Promise<any> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let returnValue: any;\n\n if (this.trace) {\n returnValue = await this._callWithConfig(\n this._tracedInvoke,\n input,\n mergeConfigs(this.config, options)\n );\n } else {\n returnValue = await this.func(input, mergeConfigs(this.config, options));\n }\n\n if (Runnable.isRunnable(returnValue) && this.recurse) {\n return await returnValue.invoke(input, options);\n }\n\n return returnValue;\n }\n}\n"],"names":["Runnable","patchConfig","AsyncLocalStorageProviderSingleton","mergeConfigs"],"mappings":";;;;;AASA;;;;;AAKG;AACG,SAAU,KAAK,CAAC,EAAU,EAAA;AAC9B,IAAA,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,KAAK,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAC1D;AAYM,MAAO,gBAA2C,SAAQA,kBAAc,CAAA;AAC5E,IAAA,YAAY,GAAa,CAAC,WAAW,CAAC;;AAGtC,IAAA,IAAI;AAEJ,IAAA,IAAI;AAEJ,IAAA,MAAM;IAEN,KAAK,GAAY,IAAI;IAErB,OAAO,GAAY,IAAI;AAEvB,IAAA,WAAA,CAAY,MAA4B,EAAA;AACtC,QAAA,KAAK,EAAE;AACP,QAAA,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI;AAC3C,QAAA,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,IAAI,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,GAAG,SAAS;QAC7D,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK;QACvC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO;;AAGrC,IAAA,MAAM,aAAa,CAC3B,KAAQ,EACR,MAAgC,EAChC,UAAuC,EAAA;QAEvC,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,KAAI;;AAExC,YAAA,MAAM,cAAc,GAClB,OAAO,UAAU,EAAE,QAAQ,KAAK;AAC9B,kBAAE,UAAU,CAAC,QAAQ;kBACnB,SAAS;AACf,YAAA,MAAM,WAAW,GAAGC,qBAAW,CAAC,MAAM,EAAE;AACtC,gBAAA,SAAS,EAAE,cAAc;AAC1B,aAAA,CAAC;YACF,KAAKC,6CAAkC,CAAC,aAAa,CACnD,WAAW,EACX,YAAW;AACT,gBAAA,IAAI;oBACF,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC;oBAClD,OAAO,CAAC,MAAM,CAAC;;gBACf,OAAO,CAAC,EAAE;oBACV,MAAM,CAAC,CAAC,CAAC;;AAEb,aAAC,CACF;AACH,SAAC,CAAC;;AAGJ,IAAA,MAAM,MAAM;;AAEV,IAAA,KAAU,EACV;;;;AAIA,QAAA,IAAI,WAAgB;AAEpB,QAAA,IAAI,IAAI,CAAC,KAAK,EAAE;YACd,WAAW,GAAG,MAAM,IAAI,CAAC,eAAe,CACtC,IAAI,CAAC,aAAa,EAClB,KAAK,EACLC,sBAAY,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CACnC;;aACI;AACL,YAAA,WAAW,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAEA,sBAAY,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;;QAG1E,IAAIH,kBAAQ,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE;YACpD,OAAO,MAAM,WAAW,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC;;AAGjD,QAAA,OAAO,WAAW;;AAErB;;;;;"}
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TOON Format Utility
|
|
5
|
+
*
|
|
6
|
+
* Provides JSON to TOON conversion for token-efficient encoding.
|
|
7
|
+
* TOON format can reduce token count by 30-60% for JSON data.
|
|
8
|
+
*
|
|
9
|
+
* Uses the official @toon-format/toon library when available.
|
|
10
|
+
*/
|
|
11
|
+
// Dynamic import holder for the TOON library (ESM)
|
|
12
|
+
let toonEncode = null;
|
|
13
|
+
let toonLoadAttempted = false;
|
|
14
|
+
let toonLoadPromise = null;
|
|
15
|
+
/**
|
|
16
|
+
* Lazily loads the TOON library
|
|
17
|
+
* Tries both ESM dynamic import and CommonJS require for compatibility
|
|
18
|
+
*/
|
|
19
|
+
async function loadToonLibrary() {
|
|
20
|
+
if (toonLoadAttempted)
|
|
21
|
+
return toonEncode !== null;
|
|
22
|
+
toonLoadAttempted = true;
|
|
23
|
+
// Try ESM dynamic import first
|
|
24
|
+
try {
|
|
25
|
+
const mod = await import('@toon-format/toon');
|
|
26
|
+
toonEncode = mod.encode;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// ESM import failed, try CommonJS require
|
|
31
|
+
try {
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
33
|
+
const mod = require('@toon-format/toon');
|
|
34
|
+
toonEncode = mod.encode;
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Library not available - that's OK, we'll return original content
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Ensures the TOON library is loaded. Call this before using jsonToToon in tests.
|
|
45
|
+
*/
|
|
46
|
+
async function ensureToonLoaded() {
|
|
47
|
+
if (!toonLoadPromise) {
|
|
48
|
+
toonLoadPromise = loadToonLibrary();
|
|
49
|
+
}
|
|
50
|
+
return toonLoadPromise;
|
|
51
|
+
}
|
|
52
|
+
// Start loading immediately
|
|
53
|
+
ensureToonLoaded();
|
|
54
|
+
/**
|
|
55
|
+
* Check if a string appears to be in TOON format.
|
|
56
|
+
* TOON format characteristics:
|
|
57
|
+
* - Uses key: value syntax (like YAML)
|
|
58
|
+
* - Uses array notation like [3] or {fields}
|
|
59
|
+
* - Does NOT start with { or [
|
|
60
|
+
* - Often has patterns like "name[N]:" or "name{fields}:"
|
|
61
|
+
*
|
|
62
|
+
* @param str - String to check
|
|
63
|
+
* @returns true if string appears to be TOON format
|
|
64
|
+
*/
|
|
65
|
+
function isToonFormat(str) {
|
|
66
|
+
if (typeof str !== 'string' || str.length === 0)
|
|
67
|
+
return false;
|
|
68
|
+
const trimmed = str.trim();
|
|
69
|
+
// TOON doesn't start with JSON brackets
|
|
70
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('['))
|
|
71
|
+
return false;
|
|
72
|
+
// Check for TOON-specific patterns:
|
|
73
|
+
// 1. Key-value with colon (but not URL-like patterns)
|
|
74
|
+
// 2. Array notation like "items[3]:" or "data[10]:"
|
|
75
|
+
// 3. Object schema notation like "items{id,name}:"
|
|
76
|
+
const toonPatterns = [
|
|
77
|
+
/^\w+:$/m, // Simple key: at start of line
|
|
78
|
+
/^\w+\[\d+\]:/m, // Array notation: items[3]:
|
|
79
|
+
/^\w+\{[^}]+\}:/m, // Schema notation: items{id,name}:
|
|
80
|
+
/^\w+\[\d+\]\{[^}]+\}:/m, // Combined: items[3]{id,name}:
|
|
81
|
+
];
|
|
82
|
+
// Must match at least one TOON pattern
|
|
83
|
+
const hasToonPattern = toonPatterns.some((pattern) => pattern.test(trimmed));
|
|
84
|
+
// Additional check: TOON typically has multiple lines with consistent indentation
|
|
85
|
+
const lines = trimmed.split('\n').filter((l) => l.trim());
|
|
86
|
+
const hasMultipleKeyValueLines = lines.filter((l) => /^\s*\w+.*:/.test(l)).length >= 2;
|
|
87
|
+
return hasToonPattern || hasMultipleKeyValueLines;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Extract the first valid JSON object or array from a string.
|
|
91
|
+
* Handles pure JSON or JSON embedded in text (e.g., tool responses).
|
|
92
|
+
*
|
|
93
|
+
* @param str - The string to extract JSON from
|
|
94
|
+
* @returns Object with found status, parsed JSON, and indices
|
|
95
|
+
*/
|
|
96
|
+
function extractFirstJson(str) {
|
|
97
|
+
if (typeof str !== 'string' || str.length === 0) {
|
|
98
|
+
return { found: false, parsed: null, startIndex: -1, endIndex: -1 };
|
|
99
|
+
}
|
|
100
|
+
// Find the first { or [ character
|
|
101
|
+
const objStart = str.indexOf('{');
|
|
102
|
+
const arrStart = str.indexOf('[');
|
|
103
|
+
if (objStart === -1 && arrStart === -1) {
|
|
104
|
+
return { found: false, parsed: null, startIndex: -1, endIndex: -1 };
|
|
105
|
+
}
|
|
106
|
+
let startIndex;
|
|
107
|
+
if (objStart === -1) {
|
|
108
|
+
startIndex = arrStart;
|
|
109
|
+
}
|
|
110
|
+
else if (arrStart === -1) {
|
|
111
|
+
startIndex = objStart;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
startIndex = Math.min(objStart, arrStart);
|
|
115
|
+
}
|
|
116
|
+
// Find the matching closing bracket using a stack-based approach
|
|
117
|
+
// Properly handle strings to avoid counting brackets inside strings
|
|
118
|
+
let depth = 0;
|
|
119
|
+
let inString = false;
|
|
120
|
+
let escapeNext = false;
|
|
121
|
+
for (let i = startIndex; i < str.length; i++) {
|
|
122
|
+
const char = str[i];
|
|
123
|
+
if (escapeNext) {
|
|
124
|
+
escapeNext = false;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (char === '\\' && inString) {
|
|
128
|
+
escapeNext = true;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (char === '"') {
|
|
132
|
+
inString = !inString;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (inString)
|
|
136
|
+
continue;
|
|
137
|
+
if (char === '{' || char === '[')
|
|
138
|
+
depth++;
|
|
139
|
+
if (char === '}' || char === ']')
|
|
140
|
+
depth--;
|
|
141
|
+
if (depth === 0) {
|
|
142
|
+
// Found matching bracket, try to parse
|
|
143
|
+
const jsonStr = str.substring(startIndex, i + 1);
|
|
144
|
+
try {
|
|
145
|
+
const parsed = JSON.parse(jsonStr);
|
|
146
|
+
return { found: true, parsed, startIndex, endIndex: i };
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
// Not valid JSON at this bracket level, continue searching
|
|
150
|
+
// Reset and look for the next JSON start after this position
|
|
151
|
+
const nextResult = extractFirstJson(str.substring(i + 1));
|
|
152
|
+
if (nextResult.found) {
|
|
153
|
+
return {
|
|
154
|
+
found: true,
|
|
155
|
+
parsed: nextResult.parsed,
|
|
156
|
+
startIndex: i + 1 + nextResult.startIndex,
|
|
157
|
+
endIndex: i + 1 + nextResult.endIndex,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return { found: false, parsed: null, startIndex: -1, endIndex: -1 };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return { found: false, parsed: null, startIndex: -1, endIndex: -1 };
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Convert JSON content to TOON format for token efficiency.
|
|
168
|
+
* Extracts JSON from string if embedded, converts to TOON.
|
|
169
|
+
* Returns original string if:
|
|
170
|
+
* - Already in TOON format
|
|
171
|
+
* - Not JSON
|
|
172
|
+
* - TOON conversion fails or is larger
|
|
173
|
+
*
|
|
174
|
+
* @param str - The string containing JSON to convert
|
|
175
|
+
* @returns Object with conversion status, result, and reduction percentage
|
|
176
|
+
*/
|
|
177
|
+
function jsonToToon(str) {
|
|
178
|
+
if (!toonEncode || typeof str !== 'string') {
|
|
179
|
+
return {
|
|
180
|
+
converted: false,
|
|
181
|
+
result: str,
|
|
182
|
+
reduction: 0,
|
|
183
|
+
error: !toonEncode ? 'TOON library not loaded' : undefined,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
// Check if already in TOON format - skip conversion
|
|
187
|
+
if (isToonFormat(str)) {
|
|
188
|
+
return { converted: false, result: str, reduction: 0, alreadyToon: true };
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
const { found, parsed, startIndex, endIndex } = extractFirstJson(str);
|
|
192
|
+
if (!found || !parsed) {
|
|
193
|
+
return {
|
|
194
|
+
converted: false,
|
|
195
|
+
result: str,
|
|
196
|
+
reduction: 0,
|
|
197
|
+
error: 'No JSON found in content',
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
// Check if this JSON structure would benefit from TOON
|
|
201
|
+
// Text-heavy content (emails, documents) won't compress well
|
|
202
|
+
if (!isToonBeneficial(parsed)) {
|
|
203
|
+
return {
|
|
204
|
+
converted: false,
|
|
205
|
+
result: str,
|
|
206
|
+
reduction: 0,
|
|
207
|
+
error: 'Content is text-heavy (>70% string values), TOON not beneficial',
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
const toonResult = toonEncode(parsed);
|
|
211
|
+
// Preserve text before and after the JSON block
|
|
212
|
+
const textBefore = str.substring(0, startIndex);
|
|
213
|
+
const textAfter = str.substring(endIndex + 1);
|
|
214
|
+
// Build the full result: textBefore + TOON + textAfter
|
|
215
|
+
const fullResult = textBefore + toonResult + textAfter;
|
|
216
|
+
if (fullResult.length < str.length) {
|
|
217
|
+
const reduction = Math.round(((str.length - fullResult.length) / str.length) * 100);
|
|
218
|
+
return { converted: true, result: fullResult, reduction };
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
// TOON output was larger or same size - not beneficial
|
|
222
|
+
return {
|
|
223
|
+
converted: false,
|
|
224
|
+
result: str,
|
|
225
|
+
reduction: 0,
|
|
226
|
+
error: `TOON output not smaller (${fullResult.length} >= ${str.length})`,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
// TOON encoding or extraction failed - log error for debugging
|
|
232
|
+
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
233
|
+
return {
|
|
234
|
+
converted: false,
|
|
235
|
+
result: str,
|
|
236
|
+
reduction: 0,
|
|
237
|
+
error: `TOON conversion error: ${errorMsg}`,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Check if TOON library is loaded and available
|
|
243
|
+
*/
|
|
244
|
+
function isToonAvailable() {
|
|
245
|
+
return toonEncode !== null;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Analyze JSON structure to determine if TOON conversion would be beneficial.
|
|
249
|
+
* Text-heavy content (emails, documents) doesn't compress well with TOON.
|
|
250
|
+
* Structured data (API responses, metadata) compresses much better.
|
|
251
|
+
*
|
|
252
|
+
* @param parsed - Parsed JSON object
|
|
253
|
+
* @returns true if TOON conversion is likely beneficial
|
|
254
|
+
*/
|
|
255
|
+
function isToonBeneficial(parsed) {
|
|
256
|
+
if (!parsed || typeof parsed !== 'object')
|
|
257
|
+
return false;
|
|
258
|
+
const jsonStr = JSON.stringify(parsed);
|
|
259
|
+
const totalLength = jsonStr.length;
|
|
260
|
+
// Count characters in string values (text content)
|
|
261
|
+
let textContentLength = 0;
|
|
262
|
+
function countTextContent(obj) {
|
|
263
|
+
if (typeof obj === 'string') {
|
|
264
|
+
// Count string length (this is text content that TOON can't compress)
|
|
265
|
+
textContentLength += obj.length;
|
|
266
|
+
}
|
|
267
|
+
else if (Array.isArray(obj)) {
|
|
268
|
+
obj.forEach(countTextContent);
|
|
269
|
+
}
|
|
270
|
+
else if (obj && typeof obj === 'object') {
|
|
271
|
+
Object.values(obj).forEach(countTextContent);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
countTextContent(parsed);
|
|
275
|
+
// Calculate ratio of text content to total JSON
|
|
276
|
+
const textRatio = textContentLength / totalLength;
|
|
277
|
+
// If more than 70% is text content, TOON won't help much
|
|
278
|
+
// TOON compresses structure (field names, brackets), not text
|
|
279
|
+
return textRatio < 0.7;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Process tool output: apply TOON conversion if beneficial, then truncate if needed.
|
|
283
|
+
* This is the main entry point for processing any tool output (regular or MCP).
|
|
284
|
+
*
|
|
285
|
+
* Flow:
|
|
286
|
+
* 1. Check if already TOON format → skip conversion
|
|
287
|
+
* 2. Try TOON conversion if content is JSON and large enough
|
|
288
|
+
* 3. Truncate if still exceeds maxLength (with smart break points)
|
|
289
|
+
*
|
|
290
|
+
* @param content - The tool output content
|
|
291
|
+
* @param options - Processing options
|
|
292
|
+
* @returns Processed content and metadata
|
|
293
|
+
*/
|
|
294
|
+
function processToolOutput(content, options = {}) {
|
|
295
|
+
const { maxLength = 100000, enableToon = true, minSizeForToon = 1000, minReductionPercent = 10, // Increased from 5% - only apply TOON when clearly beneficial
|
|
296
|
+
} = options;
|
|
297
|
+
const originalLength = content.length;
|
|
298
|
+
const originalTokens = Math.ceil(originalLength / 4);
|
|
299
|
+
let result = content;
|
|
300
|
+
let toonConverted = false;
|
|
301
|
+
let alreadyToon = false;
|
|
302
|
+
let reduction = 0;
|
|
303
|
+
let truncated = false;
|
|
304
|
+
let toonError;
|
|
305
|
+
// Step 1: Check if already TOON format
|
|
306
|
+
if (isToonFormat(content)) {
|
|
307
|
+
alreadyToon = true;
|
|
308
|
+
}
|
|
309
|
+
// Step 2: Apply TOON conversion if enabled and content is large enough
|
|
310
|
+
else if (enableToon && content.length > minSizeForToon) {
|
|
311
|
+
const toonResult = jsonToToon(content);
|
|
312
|
+
if (toonResult.alreadyToon) {
|
|
313
|
+
alreadyToon = true;
|
|
314
|
+
}
|
|
315
|
+
else if (toonResult.converted &&
|
|
316
|
+
toonResult.reduction >= minReductionPercent) {
|
|
317
|
+
result = toonResult.result;
|
|
318
|
+
toonConverted = true;
|
|
319
|
+
reduction = toonResult.reduction;
|
|
320
|
+
}
|
|
321
|
+
else if (toonResult.error) {
|
|
322
|
+
// Track error for debugging
|
|
323
|
+
toonError = toonResult.error;
|
|
324
|
+
}
|
|
325
|
+
else if (toonResult.converted &&
|
|
326
|
+
toonResult.reduction < minReductionPercent) {
|
|
327
|
+
// TOON converted but reduction was too small to be worth it
|
|
328
|
+
toonError = `TOON reduction ${toonResult.reduction}% below threshold ${minReductionPercent}%`;
|
|
329
|
+
}
|
|
330
|
+
else if (!toonResult.converted) {
|
|
331
|
+
// Conversion failed without explicit error - investigate
|
|
332
|
+
toonError =
|
|
333
|
+
toonResult.error || 'TOON conversion returned false with no error';
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
// Step 3: Truncate if still too long (with smart break points)
|
|
337
|
+
if (result.length > maxLength) {
|
|
338
|
+
let truncatedStr = result.substring(0, maxLength);
|
|
339
|
+
// Try to find a clean break point
|
|
340
|
+
if (toonConverted || alreadyToon) {
|
|
341
|
+
// For TOON format, break at newline for cleaner output
|
|
342
|
+
const lastNewline = truncatedStr.lastIndexOf('\n');
|
|
343
|
+
if (lastNewline > maxLength * 0.7) {
|
|
344
|
+
truncatedStr = truncatedStr.substring(0, lastNewline);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
// For JSON, try to find a clean JSON break point
|
|
349
|
+
const lastCompleteItem = truncatedStr.lastIndexOf('},');
|
|
350
|
+
const lastArrayItem = truncatedStr.lastIndexOf('],');
|
|
351
|
+
const breakPoint = Math.max(lastCompleteItem, lastArrayItem);
|
|
352
|
+
if (breakPoint > maxLength * 0.5) {
|
|
353
|
+
truncatedStr = truncatedStr.substring(0, breakPoint + 1);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
// Build truncation message
|
|
357
|
+
const truncationInfo = toonConverted
|
|
358
|
+
? `Original ${originalLength.toLocaleString()} chars → TOON ${result.length.toLocaleString()} chars (${reduction}% saved). ` +
|
|
359
|
+
`Still exceeds ${maxLength.toLocaleString()} char limit.`
|
|
360
|
+
: `Original output was ${originalLength.toLocaleString()} characters (~${originalTokens.toLocaleString()} tokens).`;
|
|
361
|
+
result =
|
|
362
|
+
truncatedStr +
|
|
363
|
+
`\n\n[OUTPUT_TRUNCATED: ${truncationInfo} Please use more specific queries or smaller date ranges.]`;
|
|
364
|
+
truncated = true;
|
|
365
|
+
}
|
|
366
|
+
const finalLength = result.length;
|
|
367
|
+
const finalTokens = Math.ceil(finalLength / 4);
|
|
368
|
+
return {
|
|
369
|
+
content: result,
|
|
370
|
+
toonConverted,
|
|
371
|
+
truncated,
|
|
372
|
+
reduction,
|
|
373
|
+
alreadyToon,
|
|
374
|
+
originalLength,
|
|
375
|
+
originalTokens,
|
|
376
|
+
finalLength,
|
|
377
|
+
finalTokens,
|
|
378
|
+
toonError,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
exports.ensureToonLoaded = ensureToonLoaded;
|
|
383
|
+
exports.extractFirstJson = extractFirstJson;
|
|
384
|
+
exports.isToonAvailable = isToonAvailable;
|
|
385
|
+
exports.isToonFormat = isToonFormat;
|
|
386
|
+
exports.jsonToToon = jsonToToon;
|
|
387
|
+
exports.processToolOutput = processToolOutput;
|
|
388
|
+
//# sourceMappingURL=toonFormat.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toonFormat.cjs","sources":["../../../src/utils/toonFormat.ts"],"sourcesContent":["/**\n * TOON Format Utility\n *\n * Provides JSON to TOON conversion for token-efficient encoding.\n * TOON format can reduce token count by 30-60% for JSON data.\n *\n * Uses the official @toon-format/toon library when available.\n */\n\n// Dynamic import holder for the TOON library (ESM)\nlet toonEncode: ((data: unknown) => string) | null = null;\nlet toonLoadAttempted = false;\nlet toonLoadPromise: Promise<boolean> | null = null;\n\n/**\n * Lazily loads the TOON library\n * Tries both ESM dynamic import and CommonJS require for compatibility\n */\nasync function loadToonLibrary(): Promise<boolean> {\n if (toonLoadAttempted) return toonEncode !== null;\n toonLoadAttempted = true;\n\n // Try ESM dynamic import first\n try {\n const mod = await import('@toon-format/toon');\n toonEncode = mod.encode;\n return true;\n } catch {\n // ESM import failed, try CommonJS require\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const mod = require('@toon-format/toon');\n toonEncode = mod.encode;\n return true;\n } catch {\n // Library not available - that's OK, we'll return original content\n return false;\n }\n }\n}\n\n/**\n * Ensures the TOON library is loaded. Call this before using jsonToToon in tests.\n */\nexport async function ensureToonLoaded(): Promise<boolean> {\n if (!toonLoadPromise) {\n toonLoadPromise = loadToonLibrary();\n }\n return toonLoadPromise;\n}\n\n// Start loading immediately\nensureToonLoaded();\n\n/**\n * Check if a string appears to be in TOON format.\n * TOON format characteristics:\n * - Uses key: value syntax (like YAML)\n * - Uses array notation like [3] or {fields}\n * - Does NOT start with { or [\n * - Often has patterns like \"name[N]:\" or \"name{fields}:\"\n *\n * @param str - String to check\n * @returns true if string appears to be TOON format\n */\nexport function isToonFormat(str: string): boolean {\n if (typeof str !== 'string' || str.length === 0) return false;\n\n const trimmed = str.trim();\n\n // TOON doesn't start with JSON brackets\n if (trimmed.startsWith('{') || trimmed.startsWith('[')) return false;\n\n // Check for TOON-specific patterns:\n // 1. Key-value with colon (but not URL-like patterns)\n // 2. Array notation like \"items[3]:\" or \"data[10]:\"\n // 3. Object schema notation like \"items{id,name}:\"\n const toonPatterns = [\n /^\\w+:$/m, // Simple key: at start of line\n /^\\w+\\[\\d+\\]:/m, // Array notation: items[3]:\n /^\\w+\\{[^}]+\\}:/m, // Schema notation: items{id,name}:\n /^\\w+\\[\\d+\\]\\{[^}]+\\}:/m, // Combined: items[3]{id,name}:\n ];\n\n // Must match at least one TOON pattern\n const hasToonPattern = toonPatterns.some((pattern) => pattern.test(trimmed));\n\n // Additional check: TOON typically has multiple lines with consistent indentation\n const lines = trimmed.split('\\n').filter((l) => l.trim());\n const hasMultipleKeyValueLines =\n lines.filter((l) => /^\\s*\\w+.*:/.test(l)).length >= 2;\n\n return hasToonPattern || hasMultipleKeyValueLines;\n}\n\n/**\n * Extract the first valid JSON object or array from a string.\n * Handles pure JSON or JSON embedded in text (e.g., tool responses).\n *\n * @param str - The string to extract JSON from\n * @returns Object with found status, parsed JSON, and indices\n */\nexport function extractFirstJson(str: string): {\n found: boolean;\n parsed: unknown;\n startIndex: number;\n endIndex: number;\n} {\n if (typeof str !== 'string' || str.length === 0) {\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\n }\n\n // Find the first { or [ character\n const objStart = str.indexOf('{');\n const arrStart = str.indexOf('[');\n\n if (objStart === -1 && arrStart === -1) {\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\n }\n\n let startIndex: number;\n if (objStart === -1) {\n startIndex = arrStart;\n } else if (arrStart === -1) {\n startIndex = objStart;\n } else {\n startIndex = Math.min(objStart, arrStart);\n }\n\n // Find the matching closing bracket using a stack-based approach\n // Properly handle strings to avoid counting brackets inside strings\n let depth = 0;\n let inString = false;\n let escapeNext = false;\n\n for (let i = startIndex; i < str.length; i++) {\n const char = str[i];\n\n if (escapeNext) {\n escapeNext = false;\n continue;\n }\n\n if (char === '\\\\' && inString) {\n escapeNext = true;\n continue;\n }\n\n if (char === '\"') {\n inString = !inString;\n continue;\n }\n\n if (inString) continue;\n\n if (char === '{' || char === '[') depth++;\n if (char === '}' || char === ']') depth--;\n\n if (depth === 0) {\n // Found matching bracket, try to parse\n const jsonStr = str.substring(startIndex, i + 1);\n try {\n const parsed = JSON.parse(jsonStr);\n return { found: true, parsed, startIndex, endIndex: i };\n } catch {\n // Not valid JSON at this bracket level, continue searching\n // Reset and look for the next JSON start after this position\n const nextResult = extractFirstJson(str.substring(i + 1));\n if (nextResult.found) {\n return {\n found: true,\n parsed: nextResult.parsed,\n startIndex: i + 1 + nextResult.startIndex,\n endIndex: i + 1 + nextResult.endIndex,\n };\n }\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\n }\n }\n }\n\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\n}\n\n/**\n * Convert JSON content to TOON format for token efficiency.\n * Extracts JSON from string if embedded, converts to TOON.\n * Returns original string if:\n * - Already in TOON format\n * - Not JSON\n * - TOON conversion fails or is larger\n *\n * @param str - The string containing JSON to convert\n * @returns Object with conversion status, result, and reduction percentage\n */\nexport function jsonToToon(str: string): {\n converted: boolean;\n result: string;\n reduction: number;\n alreadyToon?: boolean;\n error?: string;\n} {\n if (!toonEncode || typeof str !== 'string') {\n return {\n converted: false,\n result: str,\n reduction: 0,\n error: !toonEncode ? 'TOON library not loaded' : undefined,\n };\n }\n\n // Check if already in TOON format - skip conversion\n if (isToonFormat(str)) {\n return { converted: false, result: str, reduction: 0, alreadyToon: true };\n }\n\n try {\n const { found, parsed, startIndex, endIndex } = extractFirstJson(str);\n\n if (!found || !parsed) {\n return {\n converted: false,\n result: str,\n reduction: 0,\n error: 'No JSON found in content',\n };\n }\n\n // Check if this JSON structure would benefit from TOON\n // Text-heavy content (emails, documents) won't compress well\n if (!isToonBeneficial(parsed)) {\n return {\n converted: false,\n result: str,\n reduction: 0,\n error:\n 'Content is text-heavy (>70% string values), TOON not beneficial',\n };\n }\n\n const toonResult = toonEncode(parsed);\n\n // Preserve text before and after the JSON block\n const textBefore = str.substring(0, startIndex);\n const textAfter = str.substring(endIndex + 1);\n\n // Build the full result: textBefore + TOON + textAfter\n const fullResult = textBefore + toonResult + textAfter;\n\n if (fullResult.length < str.length) {\n const reduction = Math.round(\n ((str.length - fullResult.length) / str.length) * 100\n );\n return { converted: true, result: fullResult, reduction };\n } else {\n // TOON output was larger or same size - not beneficial\n return {\n converted: false,\n result: str,\n reduction: 0,\n error: `TOON output not smaller (${fullResult.length} >= ${str.length})`,\n };\n }\n } catch (err) {\n // TOON encoding or extraction failed - log error for debugging\n const errorMsg = err instanceof Error ? err.message : String(err);\n return {\n converted: false,\n result: str,\n reduction: 0,\n error: `TOON conversion error: ${errorMsg}`,\n };\n }\n}\n\n/**\n * Check if TOON library is loaded and available\n */\nexport function isToonAvailable(): boolean {\n return toonEncode !== null;\n}\n\n/**\n * Result of processing tool output\n */\nexport interface ProcessToolOutputResult {\n /** The processed content string */\n content: string;\n /** Whether TOON conversion was applied */\n toonConverted: boolean;\n /** Whether content was truncated */\n truncated: boolean;\n /** Percentage reduction from TOON (0 if not converted) */\n reduction: number;\n /** Whether input was already in TOON format */\n alreadyToon: boolean;\n /** Original content length */\n originalLength: number;\n /** Estimated original tokens (~4 chars per token) */\n originalTokens: number;\n /** Final content length */\n finalLength: number;\n /** Estimated final tokens */\n finalTokens: number;\n /** Error message if TOON conversion failed (for debugging) */\n toonError?: string;\n}\n\n/**\n * Options for processing tool output\n */\nexport interface ProcessToolOutputOptions {\n /** Maximum output length in characters (default: 100000) */\n maxLength?: number;\n /** Whether to apply TOON conversion (default: true) */\n enableToon?: boolean;\n /** Minimum content size to attempt TOON conversion (default: 1000) */\n minSizeForToon?: number;\n /** Minimum reduction % to accept TOON result (default: 10) */\n minReductionPercent?: number;\n}\n\n/**\n * Analyze JSON structure to determine if TOON conversion would be beneficial.\n * Text-heavy content (emails, documents) doesn't compress well with TOON.\n * Structured data (API responses, metadata) compresses much better.\n *\n * @param parsed - Parsed JSON object\n * @returns true if TOON conversion is likely beneficial\n */\nfunction isToonBeneficial(parsed: unknown): boolean {\n if (!parsed || typeof parsed !== 'object') return false;\n\n const jsonStr = JSON.stringify(parsed);\n const totalLength = jsonStr.length;\n\n // Count characters in string values (text content)\n let textContentLength = 0;\n\n function countTextContent(obj: unknown): void {\n if (typeof obj === 'string') {\n // Count string length (this is text content that TOON can't compress)\n textContentLength += obj.length;\n } else if (Array.isArray(obj)) {\n obj.forEach(countTextContent);\n } else if (obj && typeof obj === 'object') {\n Object.values(obj).forEach(countTextContent);\n }\n }\n\n countTextContent(parsed);\n\n // Calculate ratio of text content to total JSON\n const textRatio = textContentLength / totalLength;\n\n // If more than 70% is text content, TOON won't help much\n // TOON compresses structure (field names, brackets), not text\n return textRatio < 0.7;\n}\n\n/**\n * Process tool output: apply TOON conversion if beneficial, then truncate if needed.\n * This is the main entry point for processing any tool output (regular or MCP).\n *\n * Flow:\n * 1. Check if already TOON format → skip conversion\n * 2. Try TOON conversion if content is JSON and large enough\n * 3. Truncate if still exceeds maxLength (with smart break points)\n *\n * @param content - The tool output content\n * @param options - Processing options\n * @returns Processed content and metadata\n */\nexport function processToolOutput(\n content: string,\n options: ProcessToolOutputOptions = {}\n): ProcessToolOutputResult {\n const {\n maxLength = 100000,\n enableToon = true,\n minSizeForToon = 1000,\n minReductionPercent = 10, // Increased from 5% - only apply TOON when clearly beneficial\n } = options;\n\n const originalLength = content.length;\n const originalTokens = Math.ceil(originalLength / 4);\n\n let result = content;\n let toonConverted = false;\n let alreadyToon = false;\n let reduction = 0;\n let truncated = false;\n let toonError: string | undefined;\n\n // Step 1: Check if already TOON format\n if (isToonFormat(content)) {\n alreadyToon = true;\n }\n // Step 2: Apply TOON conversion if enabled and content is large enough\n else if (enableToon && content.length > minSizeForToon) {\n const toonResult = jsonToToon(content);\n if (toonResult.alreadyToon) {\n alreadyToon = true;\n } else if (\n toonResult.converted &&\n toonResult.reduction >= minReductionPercent\n ) {\n result = toonResult.result;\n toonConverted = true;\n reduction = toonResult.reduction;\n } else if (toonResult.error) {\n // Track error for debugging\n toonError = toonResult.error;\n } else if (\n toonResult.converted &&\n toonResult.reduction < minReductionPercent\n ) {\n // TOON converted but reduction was too small to be worth it\n toonError = `TOON reduction ${toonResult.reduction}% below threshold ${minReductionPercent}%`;\n } else if (!toonResult.converted) {\n // Conversion failed without explicit error - investigate\n toonError =\n toonResult.error || 'TOON conversion returned false with no error';\n }\n }\n\n // Step 3: Truncate if still too long (with smart break points)\n if (result.length > maxLength) {\n let truncatedStr = result.substring(0, maxLength);\n\n // Try to find a clean break point\n if (toonConverted || alreadyToon) {\n // For TOON format, break at newline for cleaner output\n const lastNewline = truncatedStr.lastIndexOf('\\n');\n if (lastNewline > maxLength * 0.7) {\n truncatedStr = truncatedStr.substring(0, lastNewline);\n }\n } else {\n // For JSON, try to find a clean JSON break point\n const lastCompleteItem = truncatedStr.lastIndexOf('},');\n const lastArrayItem = truncatedStr.lastIndexOf('],');\n const breakPoint = Math.max(lastCompleteItem, lastArrayItem);\n if (breakPoint > maxLength * 0.5) {\n truncatedStr = truncatedStr.substring(0, breakPoint + 1);\n }\n }\n\n // Build truncation message\n const truncationInfo = toonConverted\n ? `Original ${originalLength.toLocaleString()} chars → TOON ${result.length.toLocaleString()} chars (${reduction}% saved). ` +\n `Still exceeds ${maxLength.toLocaleString()} char limit.`\n : `Original output was ${originalLength.toLocaleString()} characters (~${originalTokens.toLocaleString()} tokens).`;\n\n result =\n truncatedStr +\n `\\n\\n[OUTPUT_TRUNCATED: ${truncationInfo} Please use more specific queries or smaller date ranges.]`;\n truncated = true;\n }\n\n const finalLength = result.length;\n const finalTokens = Math.ceil(finalLength / 4);\n\n return {\n content: result,\n toonConverted,\n truncated,\n reduction,\n alreadyToon,\n originalLength,\n originalTokens,\n finalLength,\n finalTokens,\n toonError,\n };\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;AAOG;AAEH;AACA,IAAI,UAAU,GAAuC,IAAI;AACzD,IAAI,iBAAiB,GAAG,KAAK;AAC7B,IAAI,eAAe,GAA4B,IAAI;AAEnD;;;AAGG;AACH,eAAe,eAAe,GAAA;AAC5B,IAAA,IAAI,iBAAiB;QAAE,OAAO,UAAU,KAAK,IAAI;IACjD,iBAAiB,GAAG,IAAI;;AAGxB,IAAA,IAAI;AACF,QAAA,MAAM,GAAG,GAAG,MAAM,OAAO,mBAAmB,CAAC;AAC7C,QAAA,UAAU,GAAG,GAAG,CAAC,MAAM;AACvB,QAAA,OAAO,IAAI;;AACX,IAAA,MAAM;;AAEN,QAAA,IAAI;;AAEF,YAAA,MAAM,GAAG,GAAG,OAAO,CAAC,mBAAmB,CAAC;AACxC,YAAA,UAAU,GAAG,GAAG,CAAC,MAAM;AACvB,YAAA,OAAO,IAAI;;AACX,QAAA,MAAM;;AAEN,YAAA,OAAO,KAAK;;;AAGlB;AAEA;;AAEG;AACI,eAAe,gBAAgB,GAAA;IACpC,IAAI,CAAC,eAAe,EAAE;QACpB,eAAe,GAAG,eAAe,EAAE;;AAErC,IAAA,OAAO,eAAe;AACxB;AAEA;AACA,gBAAgB,EAAE;AAElB;;;;;;;;;;AAUG;AACG,SAAU,YAAY,CAAC,GAAW,EAAA;IACtC,IAAI,OAAO,GAA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;QACtG,SAAS,GAAG,IAAI;;AAGlB,IAAA,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM;IACjC,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;IAE9C,OAAO;AACL,QAAA,OAAO,EAAE,MAAM;QACf,aAAa;QACb,SAAS;QACT,SAAS;QACT,WAAW;QACX,cAAc;QACd,cAAc;QACd,WAAW;QACX,WAAW;QACX,SAAS;KACV;AACH;;;;;;;;;"}
|
|
@@ -282,7 +282,9 @@ class AgentContext {
|
|
|
282
282
|
if (this.provider === Providers.BEDROCK) {
|
|
283
283
|
const bedrockOptions = this.clientOptions;
|
|
284
284
|
const modelId = bedrockOptions?.model?.toLowerCase() ?? '';
|
|
285
|
-
const supportsCaching = modelId.includes('claude') ||
|
|
285
|
+
const supportsCaching = modelId.includes('claude') ||
|
|
286
|
+
modelId.includes('anthropic') ||
|
|
287
|
+
modelId.includes('nova');
|
|
286
288
|
if (bedrockOptions?.promptCache === true && supportsCaching) {
|
|
287
289
|
finalInstructions = {
|
|
288
290
|
content: [
|