illuma-agents 1.0.12 β 1.0.14
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 +77 -3
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +11 -0
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +0 -16
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +7 -10
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +16 -7
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +77 -3
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +11 -0
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +0 -16
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +7 -10
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +16 -7
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +49 -0
- package/dist/types/graphs/Graph.d.ts +20 -0
- package/package.json +2 -2
- package/src/agents/AgentContext.ts +109 -3
- package/src/graphs/Graph.ts +21 -0
- package/src/llm/bedrock/index.ts +2 -8
- package/src/messages/cache.ts +7 -11
- package/src/tools/CodeExecutor.ts +16 -7
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { ContentTypes } from '../common/enum.mjs';
|
|
2
2
|
|
|
3
|
-
/** Always-on logger for cache operations */
|
|
4
|
-
const logCache = (message) => {
|
|
5
|
-
console.log(`[Cache] ${message}`);
|
|
6
|
-
};
|
|
7
3
|
/** Debug logger for cache operations - set ILLUMA_DEBUG_CACHE=true to enable */
|
|
8
4
|
const debugCache = (message, data) => {
|
|
9
5
|
if (process.env.ILLUMA_DEBUG_CACHE === 'true') {
|
|
6
|
+
// eslint-disable-next-line no-console
|
|
10
7
|
console.log(`[Cache] ${message}`, data !== undefined ? JSON.stringify(data, null, 2) : '');
|
|
11
8
|
}
|
|
12
9
|
};
|
|
@@ -212,11 +209,11 @@ function addBedrockCacheControl(messages) {
|
|
|
212
209
|
}
|
|
213
210
|
}
|
|
214
211
|
// Log message summary
|
|
215
|
-
|
|
212
|
+
debugCache(`π¨ Messages | total=${updatedMessages.length} | ${Object.entries(messageTypes).map(([k, v]) => `${k}:${v}`).join(' ')} | skippedReasoning=${skippedWithReasoning}`);
|
|
216
213
|
// If no suitable assistant message found, skip conversation caching
|
|
217
214
|
// (System and Tools caching are still handled separately)
|
|
218
215
|
if (lastAssistantIndex === -1) {
|
|
219
|
-
|
|
216
|
+
debugCache('π¨ Messages | No suitable assistant message for cachePoint (first turn or all have reasoning)');
|
|
220
217
|
return updatedMessages;
|
|
221
218
|
}
|
|
222
219
|
// Add cache point to the last assistant message (without reasoning blocks)
|
|
@@ -227,7 +224,7 @@ function addBedrockCacheControl(messages) {
|
|
|
227
224
|
{ type: ContentTypes.TEXT, text: content },
|
|
228
225
|
{ cachePoint: { type: 'default' } },
|
|
229
226
|
];
|
|
230
|
-
|
|
227
|
+
debugCache(`π Message cachePoint at index ${lastAssistantIndex} (string, ${content.length} chars)`);
|
|
231
228
|
debugCache('addBedrockCacheControl: Added cachePoint to assistant message (string content)', {
|
|
232
229
|
index: lastAssistantIndex,
|
|
233
230
|
contentLength: content.length,
|
|
@@ -237,7 +234,7 @@ function addBedrockCacheControl(messages) {
|
|
|
237
234
|
// Double-check: If this message has reasoning blocks, skip adding cache point entirely
|
|
238
235
|
// This handles edge cases where the initial skip check might have missed it
|
|
239
236
|
if (hasReasoningBlock(assistantMessage)) {
|
|
240
|
-
|
|
237
|
+
debugCache(`β οΈ Message cachePoint SKIPPED at index ${lastAssistantIndex} (has reasoning blocks)`);
|
|
241
238
|
debugCache('addBedrockCacheControl: Skipping - assistant message has reasoning blocks (safety check)', {
|
|
242
239
|
index: lastAssistantIndex,
|
|
243
240
|
});
|
|
@@ -255,7 +252,7 @@ function addBedrockCacheControl(messages) {
|
|
|
255
252
|
cachePoint: { type: 'default' },
|
|
256
253
|
});
|
|
257
254
|
inserted = true;
|
|
258
|
-
|
|
255
|
+
debugCache(`π Message cachePoint at index ${lastAssistantIndex} (array, block ${j}, ${text.length} chars)`);
|
|
259
256
|
debugCache('addBedrockCacheControl: Added cachePoint after text block in assistant message', {
|
|
260
257
|
index: lastAssistantIndex,
|
|
261
258
|
textBlockIndex: j,
|
|
@@ -268,7 +265,7 @@ function addBedrockCacheControl(messages) {
|
|
|
268
265
|
// If no text block found, don't append cache point as the message structure is unexpected
|
|
269
266
|
if (!inserted) {
|
|
270
267
|
const contentTypes = assistantMessage.content.map((b) => b.type);
|
|
271
|
-
|
|
268
|
+
debugCache(`β οΈ Message cachePoint SKIPPED at index ${lastAssistantIndex} (no text block, types: ${contentTypes.join(',')})`);
|
|
272
269
|
debugCache('addBedrockCacheControl: No suitable text block found, skipping cache point', {
|
|
273
270
|
index: lastAssistantIndex,
|
|
274
271
|
contentTypes,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.mjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\n/** Always-on logger for cache operations */\nconst logCache = (message: string) => {\n console.log(`[Cache] ${message}`);\n};\n\n/** Debug logger for cache operations - set ILLUMA_DEBUG_CACHE=true to enable */\nconst debugCache = (message: string, data?: unknown) => {\n if (process.env.ILLUMA_DEBUG_CACHE === 'true') {\n console.log(`[Cache] ${message}`, data !== undefined ? JSON.stringify(data, null, 2) : '');\n }\n};\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * @param messages - The array of message objects.\n * @returns - The updated array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n let userMessagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const message = updatedMessages[i];\n const isUserMessage =\n ('getType' in message && message.getType() === 'human') ||\n ('role' in message && message.role === 'user');\n\n if (Array.isArray(message.content)) {\n message.content = message.content.filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof message.content;\n\n for (let j = 0; j < message.content.length; j++) {\n const block = message.content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n {\n type: 'text',\n text: message.content,\n cache_control: { type: 'ephemeral' },\n },\n ];\n userMessagesModified++;\n } else if (Array.isArray(message.content)) {\n for (let j = message.content.length - 1; j >= 0; j--) {\n const contentPart = message.content[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n break;\n }\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const message = updatedMessages[i];\n const content = message.content;\n\n if (Array.isArray(content)) {\n for (let j = 0; j < content.length; j++) {\n const block = content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const message = updatedMessages[i];\n const content = message.content;\n\n if (Array.isArray(content)) {\n message.content = content.filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof content;\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points using \"Stable Prefix Caching\" strategy.\n * \n * STRATEGY: Place cache point after the LAST ASSISTANT message only.\n * This ensures the prefix (everything before the cache point) remains STABLE\n * as the conversation grows, maximizing cache hits.\n * \n * Why this works:\n * - System message has its own cachePoint (added in AgentContext)\n * - Tools have their own cachePoint (added in CustomChatBedrockConverse)\n * - Conversation history grows, but the PREFIX stays the same\n * - Only the NEW user message is uncached (it's always different)\n * \n * Example conversation flow:\n * Request 1: [System+cachePoint][Tools+cachePoint][User1] β No conversation cache yet\n * Request 2: [System][Tools][User1][Assistant1+cachePoint][User2] β Cache User1+Assistant1\n * Request 3: [System][Tools][User1][Assistant1][User2][Assistant2+cachePoint][User3]\n * β Cache reads User1+A1+User2+A2, cache writes new portion\n * \n * Claude's \"Simplified Cache Management\" automatically looks back up to 20 content\n * blocks from the cache checkpoint to find the longest matching prefix.\n * \n * @param messages - The array of message objects (excluding system message).\n * @returns - The updated array with a single cache point after the last assistant message.\n */\nexport function addBedrockCacheControl<\n T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n if (!Array.isArray(messages) || messages.length < 1) {\n debugCache('addBedrockCacheControl: Skipping - no messages', { count: messages?.length });\n return messages;\n }\n\n debugCache('addBedrockCacheControl: Processing messages with stable prefix strategy', { \n count: messages.length \n });\n \n const updatedMessages: T[] = messages.slice();\n \n // First pass: Remove ALL existing cache points to ensure clean state\n // This prevents accumulation of stale cache points\n for (const message of updatedMessages) {\n const content = message.content;\n if (Array.isArray(content)) {\n message.content = content.filter(\n (block) => !isCachePoint(block)\n ) as typeof content;\n\n // Also remove Anthropic-style cache_control\n for (let j = 0; j < message.content.length; j++) {\n const block = message.content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n }\n\n // Helper function to check if a message contains reasoning/thinking blocks\n const hasReasoningBlock = (message: T): boolean => {\n const content = message.content;\n if (!Array.isArray(content)) {\n return false;\n }\n for (const block of content) {\n const type = (block as { type?: string }).type;\n // Check for all reasoning/thinking block types:\n // - reasoning_content: Bedrock Anthropic extended thinking\n // - reasoning: Generic reasoning format \n // - thinking: Anthropic direct API thinking\n // - redacted_thinking: Anthropic redacted thinking blocks\n if (\n type === 'reasoning_content' ||\n type === 'reasoning' ||\n type === 'thinking' ||\n type === 'redacted_thinking'\n ) {\n return true;\n }\n }\n return false;\n };\n\n // Second pass: Find the LAST assistant message WITHOUT reasoning blocks and add a cache point there\n // Messages with reasoning/thinking blocks cannot have cache points after them (Bedrock limitation)\n let lastAssistantIndex = -1;\n let skippedWithReasoning = 0;\n \n // Count message types for logging\n const messageTypes: Record<string, number> = {};\n for (const message of updatedMessages) {\n const msgType = 'getType' in message && typeof message.getType === 'function' \n ? message.getType() \n : 'unknown';\n messageTypes[msgType] = (messageTypes[msgType] || 0) + 1;\n }\n \n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const message = updatedMessages[i];\n const messageType = 'getType' in message && typeof message.getType === 'function' \n ? message.getType() \n : 'unknown';\n \n if (messageType === 'ai') {\n // Skip assistant messages with reasoning blocks - cache points not allowed after them\n if (hasReasoningBlock(message)) {\n skippedWithReasoning++;\n debugCache('addBedrockCacheControl: Skipping assistant message with reasoning block', { index: i });\n continue;\n }\n lastAssistantIndex = i;\n break;\n }\n }\n\n // Log message summary\n logCache(`π¨ Messages | total=${updatedMessages.length} | ${Object.entries(messageTypes).map(([k,v]) => `${k}:${v}`).join(' ')} | skippedReasoning=${skippedWithReasoning}`);\n\n // If no suitable assistant message found, skip conversation caching\n // (System and Tools caching are still handled separately)\n if (lastAssistantIndex === -1) {\n logCache('π¨ Messages | No suitable assistant message for cachePoint (first turn or all have reasoning)');\n return updatedMessages;\n }\n\n // Add cache point to the last assistant message (without reasoning blocks)\n const assistantMessage = updatedMessages[lastAssistantIndex];\n const content = assistantMessage.content;\n\n if (typeof content === 'string' && content !== '') {\n assistantMessage.content = [\n { type: ContentTypes.TEXT, text: content },\n { cachePoint: { type: 'default' } },\n ] as MessageContentComplex[];\n logCache(`π Message cachePoint at index ${lastAssistantIndex} (string, ${content.length} chars)`);\n debugCache('addBedrockCacheControl: Added cachePoint to assistant message (string content)', {\n index: lastAssistantIndex,\n contentLength: content.length,\n });\n } else if (Array.isArray(assistantMessage.content) && assistantMessage.content.length > 0) {\n // Double-check: If this message has reasoning blocks, skip adding cache point entirely\n // This handles edge cases where the initial skip check might have missed it\n if (hasReasoningBlock(assistantMessage)) {\n logCache(`β οΈ Message cachePoint SKIPPED at index ${lastAssistantIndex} (has reasoning blocks)`);\n debugCache('addBedrockCacheControl: Skipping - assistant message has reasoning blocks (safety check)', {\n index: lastAssistantIndex,\n });\n return updatedMessages;\n }\n \n // Find the last text block and insert cache point after it\n let inserted = false;\n for (let j = assistantMessage.content.length - 1; j >= 0; j--) {\n const block = assistantMessage.content[j] as MessageContentComplex;\n const type = (block as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (block as { text?: string }).text;\n if (text && text !== '') {\n assistantMessage.content.splice(j + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n inserted = true;\n logCache(`π Message cachePoint at index ${lastAssistantIndex} (array, block ${j}, ${text.length} chars)`);\n debugCache('addBedrockCacheControl: Added cachePoint after text block in assistant message', {\n index: lastAssistantIndex,\n textBlockIndex: j,\n contentLength: text.length,\n });\n break;\n }\n }\n }\n \n // If no text block found, don't append cache point as the message structure is unexpected\n if (!inserted) {\n const contentTypes = assistantMessage.content.map((b) => (b as { type?: string }).type);\n logCache(`β οΈ Message cachePoint SKIPPED at index ${lastAssistantIndex} (no text block, types: ${contentTypes.join(',')})`);\n debugCache('addBedrockCacheControl: No suitable text block found, skipping cache point', {\n index: lastAssistantIndex,\n contentTypes,\n });\n }\n }\n\n debugCache('addBedrockCacheControl: Complete - stable prefix caching applied', { \n lastAssistantIndex,\n totalMessages: updatedMessages.length,\n });\n\n return updatedMessages;\n}\n"],"names":[],"mappings":";;AASA;AACA,MAAM,QAAQ,GAAG,CAAC,OAAe,KAAI;AACnC,IAAA,OAAO,CAAC,GAAG,CAAC,WAAW,OAAO,CAAA,CAAE,CAAC;AACnC,CAAC;AAED;AACA,MAAM,UAAU,GAAG,CAAC,OAAe,EAAE,IAAc,KAAI;IACrD,IAAI,OAAO,CAAC,GAAG,CAAC,kBAAkB,KAAK,MAAM,EAAE;AAC7C,QAAA,OAAO,CAAC,GAAG,CAAC,CAAA,QAAA,EAAW,OAAO,CAAA,CAAE,EAAE,IAAI,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;;AAE9F,CAAC;AAED;;;;;;;AAOG;AACG,SAAU,eAAe,CAC7B,QAAa,EAAA;AAEb,IAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;AACnD,QAAA,OAAO,QAAQ;;AAGjB,IAAA,MAAM,eAAe,GAAG,CAAC,GAAG,QAAQ,CAAC;IACrC,IAAI,oBA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;oBAC3B,QAAQ,GAAG,IAAI;oBACf,QAAQ,CAAC,CAAkC,+BAAA,EAAA,kBAAkB,CAAkB,eAAA,EAAA,CAAC,CAAK,EAAA,EAAA,IAAI,CAAC,MAAM,CAAS,OAAA,CAAA,CAAC;oBAC1G,UAAU,CAAC,gFAAgF,EAAE;AAC3F,wBAAA,KAAK,EAAE,kBAAkB;AACzB,wBAAA,cAAc,EAAE,CAAC;wBACjB,aAAa,EAAE,IAAI,CAAC,MAAM;AAC3B,qBAAA,CAAC;oBACF;;;;;QAMN,IAAI,CAAC,QAAQ,EAAE;AACb,YAAA,MAAM,YAAY,GAAG,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,KAAM,CAAuB,CAAC,IAAI,CAAC;AACvF,YAAA,QAAQ,CAAC,CAAA,uCAAA,EAA0C,kBAAkB,CAAA,wBAAA,EAA2B,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAG,CAAA,CAAA,CAAC;YAC1H,UAAU,CAAC,4EAA4E,EAAE;AACvF,gBAAA,KAAK,EAAE,kBAAkB;gBACzB,YAAY;AACb,aAAA,CAAC;;;IAIN,UAAU,CAAC,kEAAkE,EAAE;QAC7E,kBAAkB;QAClB,aAAa,EAAE,eAAe,CAAC,MAAM;AACtC,KAAA,CAAC;AAEF,IAAA,OAAO,eAAe;AACxB;;;;"}
|
|
1
|
+
{"version":3,"file":"cache.mjs","sources":["../../../src/messages/cache.ts"],"sourcesContent":["import { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { AnthropicMessage } from '@/types/messages';\nimport type Anthropic from '@anthropic-ai/sdk';\nimport { ContentTypes } from '@/common/enum';\n\ntype MessageWithContent = {\n content?: string | MessageContentComplex[];\n};\n\n/** Debug logger for cache operations - set ILLUMA_DEBUG_CACHE=true to enable */\nconst debugCache = (message: string, data?: unknown) => {\n if (process.env.ILLUMA_DEBUG_CACHE === 'true') {\n // eslint-disable-next-line no-console\n console.log(`[Cache] ${message}`, data !== undefined ? JSON.stringify(data, null, 2) : '');\n }\n};\n\n/**\n * Anthropic API: Adds cache control to the appropriate user messages in the payload.\n * Strips ALL existing cache control (both Anthropic and Bedrock formats) from all messages,\n * then adds fresh cache control to the last 2 user messages in a single backward pass.\n * This ensures we don't accumulate stale cache points across multiple turns.\n * @param messages - The array of message objects.\n * @returns - The updated array of message objects with cache control added.\n */\nexport function addCacheControl<T extends AnthropicMessage | BaseMessage>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages) || messages.length < 2) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n let userMessagesModified = 0;\n\n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const message = updatedMessages[i];\n const isUserMessage =\n ('getType' in message && message.getType() === 'human') ||\n ('role' in message && message.role === 'user');\n\n if (Array.isArray(message.content)) {\n message.content = message.content.filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof message.content;\n\n for (let j = 0; j < message.content.length; j++) {\n const block = message.content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n\n if (userMessagesModified >= 2 || !isUserMessage) {\n continue;\n }\n\n if (typeof message.content === 'string') {\n message.content = [\n {\n type: 'text',\n text: message.content,\n cache_control: { type: 'ephemeral' },\n },\n ];\n userMessagesModified++;\n } else if (Array.isArray(message.content)) {\n for (let j = message.content.length - 1; j >= 0; j--) {\n const contentPart = message.content[j];\n if ('type' in contentPart && contentPart.type === 'text') {\n (contentPart as Anthropic.TextBlockParam).cache_control = {\n type: 'ephemeral',\n };\n userMessagesModified++;\n break;\n }\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Checks if a content block is a cache point\n */\nfunction isCachePoint(block: MessageContentComplex): boolean {\n return 'cachePoint' in block && !('type' in block);\n}\n\n/**\n * Removes all Anthropic cache_control fields from messages\n * Used when switching from Anthropic to Bedrock provider\n */\nexport function stripAnthropicCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const message = updatedMessages[i];\n const content = message.content;\n\n if (Array.isArray(content)) {\n for (let j = 0; j < content.length; j++) {\n const block = content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Removes all Bedrock cachePoint blocks from messages\n * Used when switching from Bedrock to Anthropic provider\n */\nexport function stripBedrockCacheControl<T extends MessageWithContent>(\n messages: T[]\n): T[] {\n if (!Array.isArray(messages)) {\n return messages;\n }\n\n const updatedMessages = [...messages];\n\n for (let i = 0; i < updatedMessages.length; i++) {\n const message = updatedMessages[i];\n const content = message.content;\n\n if (Array.isArray(content)) {\n message.content = content.filter(\n (block) => !isCachePoint(block as MessageContentComplex)\n ) as typeof content;\n }\n }\n\n return updatedMessages;\n}\n\n/**\n * Adds Bedrock Converse API cache points using \"Stable Prefix Caching\" strategy.\n * \n * STRATEGY: Place cache point after the LAST ASSISTANT message only.\n * This ensures the prefix (everything before the cache point) remains STABLE\n * as the conversation grows, maximizing cache hits.\n * \n * Why this works:\n * - System message has its own cachePoint (added in AgentContext)\n * - Tools have their own cachePoint (added in CustomChatBedrockConverse)\n * - Conversation history grows, but the PREFIX stays the same\n * - Only the NEW user message is uncached (it's always different)\n * \n * Example conversation flow:\n * Request 1: [System+cachePoint][Tools+cachePoint][User1] β No conversation cache yet\n * Request 2: [System][Tools][User1][Assistant1+cachePoint][User2] β Cache User1+Assistant1\n * Request 3: [System][Tools][User1][Assistant1][User2][Assistant2+cachePoint][User3]\n * β Cache reads User1+A1+User2+A2, cache writes new portion\n * \n * Claude's \"Simplified Cache Management\" automatically looks back up to 20 content\n * blocks from the cache checkpoint to find the longest matching prefix.\n * \n * @param messages - The array of message objects (excluding system message).\n * @returns - The updated array with a single cache point after the last assistant message.\n */\nexport function addBedrockCacheControl<\n T extends Partial<BaseMessage> & MessageWithContent,\n>(messages: T[]): T[] {\n if (!Array.isArray(messages) || messages.length < 1) {\n debugCache('addBedrockCacheControl: Skipping - no messages', { count: messages?.length });\n return messages;\n }\n\n debugCache('addBedrockCacheControl: Processing messages with stable prefix strategy', { \n count: messages.length \n });\n \n const updatedMessages: T[] = messages.slice();\n \n // First pass: Remove ALL existing cache points to ensure clean state\n // This prevents accumulation of stale cache points\n for (const message of updatedMessages) {\n const content = message.content;\n if (Array.isArray(content)) {\n message.content = content.filter(\n (block) => !isCachePoint(block)\n ) as typeof content;\n\n // Also remove Anthropic-style cache_control\n for (let j = 0; j < message.content.length; j++) {\n const block = message.content[j] as Record<string, unknown>;\n if ('cache_control' in block) {\n delete block.cache_control;\n }\n }\n }\n }\n\n // Helper function to check if a message contains reasoning/thinking blocks\n const hasReasoningBlock = (message: T): boolean => {\n const content = message.content;\n if (!Array.isArray(content)) {\n return false;\n }\n for (const block of content) {\n const type = (block as { type?: string }).type;\n // Check for all reasoning/thinking block types:\n // - reasoning_content: Bedrock Anthropic extended thinking\n // - reasoning: Generic reasoning format \n // - thinking: Anthropic direct API thinking\n // - redacted_thinking: Anthropic redacted thinking blocks\n if (\n type === 'reasoning_content' ||\n type === 'reasoning' ||\n type === 'thinking' ||\n type === 'redacted_thinking'\n ) {\n return true;\n }\n }\n return false;\n };\n\n // Second pass: Find the LAST assistant message WITHOUT reasoning blocks and add a cache point there\n // Messages with reasoning/thinking blocks cannot have cache points after them (Bedrock limitation)\n let lastAssistantIndex = -1;\n let skippedWithReasoning = 0;\n \n // Count message types for logging\n const messageTypes: Record<string, number> = {};\n for (const message of updatedMessages) {\n const msgType = 'getType' in message && typeof message.getType === 'function' \n ? message.getType() \n : 'unknown';\n messageTypes[msgType] = (messageTypes[msgType] || 0) + 1;\n }\n \n for (let i = updatedMessages.length - 1; i >= 0; i--) {\n const message = updatedMessages[i];\n const messageType = 'getType' in message && typeof message.getType === 'function' \n ? message.getType() \n : 'unknown';\n \n if (messageType === 'ai') {\n // Skip assistant messages with reasoning blocks - cache points not allowed after them\n if (hasReasoningBlock(message)) {\n skippedWithReasoning++;\n debugCache('addBedrockCacheControl: Skipping assistant message with reasoning block', { index: i });\n continue;\n }\n lastAssistantIndex = i;\n break;\n }\n }\n\n // Log message summary\n debugCache(`π¨ Messages | total=${updatedMessages.length} | ${Object.entries(messageTypes).map(([k,v]) => `${k}:${v}`).join(' ')} | skippedReasoning=${skippedWithReasoning}`);\n\n // If no suitable assistant message found, skip conversation caching\n // (System and Tools caching are still handled separately)\n if (lastAssistantIndex === -1) {\n debugCache('π¨ Messages | No suitable assistant message for cachePoint (first turn or all have reasoning)');\n return updatedMessages;\n }\n\n // Add cache point to the last assistant message (without reasoning blocks)\n const assistantMessage = updatedMessages[lastAssistantIndex];\n const content = assistantMessage.content;\n\n if (typeof content === 'string' && content !== '') {\n assistantMessage.content = [\n { type: ContentTypes.TEXT, text: content },\n { cachePoint: { type: 'default' } },\n ] as MessageContentComplex[];\n debugCache(`π Message cachePoint at index ${lastAssistantIndex} (string, ${content.length} chars)`);\n debugCache('addBedrockCacheControl: Added cachePoint to assistant message (string content)', {\n index: lastAssistantIndex,\n contentLength: content.length,\n });\n } else if (Array.isArray(assistantMessage.content) && assistantMessage.content.length > 0) {\n // Double-check: If this message has reasoning blocks, skip adding cache point entirely\n // This handles edge cases where the initial skip check might have missed it\n if (hasReasoningBlock(assistantMessage)) {\n debugCache(`β οΈ Message cachePoint SKIPPED at index ${lastAssistantIndex} (has reasoning blocks)`);\n debugCache('addBedrockCacheControl: Skipping - assistant message has reasoning blocks (safety check)', {\n index: lastAssistantIndex,\n });\n return updatedMessages;\n }\n \n // Find the last text block and insert cache point after it\n let inserted = false;\n for (let j = assistantMessage.content.length - 1; j >= 0; j--) {\n const block = assistantMessage.content[j] as MessageContentComplex;\n const type = (block as { type?: string }).type;\n if (type === ContentTypes.TEXT || type === 'text') {\n const text = (block as { text?: string }).text;\n if (text && text !== '') {\n assistantMessage.content.splice(j + 1, 0, {\n cachePoint: { type: 'default' },\n } as MessageContentComplex);\n inserted = true;\n debugCache(`π Message cachePoint at index ${lastAssistantIndex} (array, block ${j}, ${text.length} chars)`);\n debugCache('addBedrockCacheControl: Added cachePoint after text block in assistant message', {\n index: lastAssistantIndex,\n textBlockIndex: j,\n contentLength: text.length,\n });\n break;\n }\n }\n }\n \n // If no text block found, don't append cache point as the message structure is unexpected\n if (!inserted) {\n const contentTypes = assistantMessage.content.map((b) => (b as { type?: string }).type);\n debugCache(`β οΈ Message cachePoint SKIPPED at index ${lastAssistantIndex} (no text block, types: ${contentTypes.join(',')})`);\n debugCache('addBedrockCacheControl: No suitable text block found, skipping cache point', {\n index: lastAssistantIndex,\n contentTypes,\n });\n }\n }\n\n debugCache('addBedrockCacheControl: Complete - stable prefix caching applied', { \n lastAssistantIndex,\n totalMessages: updatedMessages.length,\n });\n\n return updatedMessages;\n}\n"],"names":[],"mappings":";;AASA;AACA,MAAM,UAAU,GAAG,CAAC,OAAe,EAAE,IAAc,KAAI;IACrD,IAAI,OAAO,CAAC,GAAG,CAAC,kBAAkB,KAAK,MAAM,EAAE;;AAE7C,QAAA,OAAO,CAAC,GAAG,CAAC,CAAA,QAAA,EAAW,OAAO,CAAA,CAAE,EAAE,IAAI,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;;AAE9F,CAAC;AAED;;;;;;;AAOG;AACG,SAAU,eAAe,CAC7B,QAAa,EAAA;AAEb,IAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;AACnD,QAAA,OAAO,QAAQ;;AAGjB,IAAA,MAAM,eAAe,GAAG,CAAC,GAAG,QAAQ,CAAC;IACrC,IAAI,oBAAoB,GAAG,CAAC;AAE5B,IAAA,KAAK,IAAI,CAAC,GAAG,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,E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AAA,IAAI,CAAC,MAAM,CAAS,OAAA,CAAA,CAAC;oBAC5G,UAAU,CAAC,gFAAgF,EAAE;AAC3F,wBAAA,KAAK,EAAE,kBAAkB;AACzB,wBAAA,cAAc,EAAE,CAAC;wBACjB,aAAa,EAAE,IAAI,CAAC,MAAM;AAC3B,qBAAA,CAAC;oBACF;;;;;QAMN,IAAI,CAAC,QAAQ,EAAE;AACb,YAAA,MAAM,YAAY,GAAG,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,KAAM,CAAuB,CAAC,IAAI,CAAC;AACvF,YAAA,UAAU,CAAC,CAAA,uCAAA,EAA0C,kBAAkB,CAAA,wBAAA,EAA2B,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAG,CAAA,CAAA,CAAC;YAC5H,UAAU,CAAC,4EAA4E,EAAE;AACvF,gBAAA,KAAK,EAAE,kBAAkB;gBACzB,YAAY;AACb,aAAA,CAAC;;;IAIN,UAAU,CAAC,kEAAkE,EAAE;QAC7E,kBAAkB;QAClB,aAAa,EAAE,eAAe,CAAC,MAAM;AACtC,KAAA,CAAC;AAEF,IAAA,OAAO,eAAe;AACxB;;;;"}
|
|
@@ -68,16 +68,25 @@ function createCodeExecutionTool(params = {}) {
|
|
|
68
68
|
throw new Error('No API key provided for code execution tool.');
|
|
69
69
|
}
|
|
70
70
|
const description = `
|
|
71
|
-
|
|
71
|
+
β STOP! Before using this tool, ask: "Does user need a DOWNLOADABLE FILE?"
|
|
72
|
+
- If NO (dashboard, chart, visualization, UI) β DO NOT USE THIS TOOL. Use :::artifact instead.
|
|
73
|
+
- If YES (.pptx, .docx, .pdf, .xlsx) β Use this tool.
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
Runs code in a stateless execution environment. Each execution is isolated.
|
|
74
76
|
|
|
75
|
-
|
|
77
|
+
π« NEVER USE FOR:
|
|
78
|
+
- Dashboards, charts, visualizations β Use :::artifact with React/Chart.js
|
|
79
|
+
- "Mock data" or "sample data" for display β Hardcode data in artifact
|
|
80
|
+
- UI components, HTML pages, React apps β Use :::artifact
|
|
76
81
|
|
|
77
|
-
|
|
78
|
-
-
|
|
79
|
-
-
|
|
80
|
-
-
|
|
82
|
+
β
ONLY USE FOR:
|
|
83
|
+
- File generation: PowerPoint (.pptx), Word (.docx), PDF (.pdf), Excel (.xlsx)
|
|
84
|
+
- Processing uploaded files (CSV, Excel analysis)
|
|
85
|
+
- Heavy computation requiring Python
|
|
86
|
+
|
|
87
|
+
Rules:
|
|
88
|
+
- No network access available
|
|
89
|
+
- Generated files auto-delivered (no download links needed)
|
|
81
90
|
`.trim();
|
|
82
91
|
return tool(async ({ lang, code, session_id, ...rest }) => {
|
|
83
92
|
const postData = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CodeExecutor.mjs","sources":["../../../src/tools/CodeExecutor.ts"],"sourcesContent":["import { z } from 'zod';\nimport { config } from 'dotenv';\nimport fetch, { RequestInit } from 'node-fetch';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport { getEnvironmentVariable } from '@langchain/core/utils/env';\nimport type * as t from '@/types';\nimport { EnvVar, Constants } from '@/common';\n\nconfig();\n\nexport const imageExtRegex = /\\.(jpg|jpeg|png|gif|webp)$/i;\nexport const getCodeBaseURL = (): string =>\n getEnvironmentVariable(EnvVar.CODE_BASEURL) ??\n Constants.OFFICIAL_CODE_BASEURL;\n\nconst imageMessage = 'Image is already displayed to the user';\nconst otherMessage = 'File is already downloaded by the user';\nconst accessMessage =\n 'Note: Files are READ-ONLY. Save changes to NEW filenames. To access these files in future executions, provide the `session_id` as a parameter (not in your code).';\nconst emptyOutputMessage =\n 'stdout: Empty. Ensure you\\'re writing output explicitly.\\n';\n\nconst CodeExecutionToolSchema = z.object({\n lang: z\n .enum([\n 'py',\n 'js',\n 'ts',\n 'c',\n 'cpp',\n 'java',\n 'php',\n 'rs',\n 'go',\n 'd',\n 'f90',\n 'r',\n ])\n .describe('The programming language or runtime to execute the code in.'),\n code: z.string()\n .describe(`The complete, self-contained code to execute, without any truncation or minimization.\n- The environment is stateless; variables and imports don't persist between executions.\n- When using \\`session_id\\`: Don't hardcode it in \\`code\\`, and write file modifications to NEW filenames (files are READ-ONLY).\n- Input code **IS ALREADY** displayed to the user, so **DO NOT** repeat it in your response unless asked.\n- Output code **IS NOT** displayed to the user, so **DO** write all desired output explicitly.\n- IMPORTANT: You MUST explicitly print/output ALL results you want the user to see.\n- py: This is not a Jupyter notebook environment. Use \\`print()\\` for all outputs.\n- py: Matplotlib: Use \\`plt.savefig()\\` to save plots as files.\n- js: use the \\`console\\` or \\`process\\` methods for all outputs.\n- r: IMPORTANT: No X11 display available. ALL graphics MUST use Cairo library (library(Cairo)).\n- Other languages: use appropriate output functions.`),\n session_id: z\n .string()\n .optional()\n .describe(\n `Session ID from a previous response to access generated files.\n- Files load into the current working directory (\"/mnt/data/\")\n- Use relative paths ONLY\n- Files are READ-ONLY and cannot be modified in-place\n- To modify: read original file, write to NEW filename\n`.trim()\n ),\n args: z\n .array(z.string())\n .optional()\n .describe(\n 'Additional arguments to execute the code with. This should only be used if the input code requires additional arguments to run.'\n ),\n});\n\nconst baseEndpoint = getCodeBaseURL();\nconst EXEC_ENDPOINT = `${baseEndpoint}/exec`;\n\nfunction createCodeExecutionTool(\n params: t.CodeExecutionToolParams = {}\n): DynamicStructuredTool<typeof CodeExecutionToolSchema> {\n const apiKey =\n params[EnvVar.CODE_API_KEY] ??\n params.apiKey ??\n getEnvironmentVariable(EnvVar.CODE_API_KEY) ??\n '';\n if (!apiKey) {\n throw new Error('No API key provided for code execution tool.');\n }\n\n const description = `\nRuns code and returns stdout/stderr output from a stateless execution environment, similar to running scripts in a command-line interface. Each execution is isolated and independent.\n\nβ οΈ DO NOT USE FOR: Creating dashboards, UI components, React components, HTML pages, charts, or visualizations. These should be created as artifacts directly in your response using :::artifact syntax.\n\nβ
USE FOR: Data processing, calculations, file generation (CSV, Excel, PowerPoint, PDF), matplotlib plots, running algorithms, or computational tasks.\n\nUsage:\n- No network access available.\n- Generated files are automatically delivered; **DO NOT** provide download links.\n- NEVER use this tool to execute malicious code.\n`.trim();\n\n return tool<typeof CodeExecutionToolSchema>(\n async ({ lang, code, session_id, ...rest }) => {\n const postData = {\n lang,\n code,\n ...rest,\n ...params,\n };\n\n if (session_id != null && session_id.length > 0) {\n try {\n const filesEndpoint = `${baseEndpoint}/files/${session_id}?detail=full`;\n const fetchOptions: RequestInit = {\n method: 'GET',\n headers: {\n 'User-Agent': 'Illuma/1.0',\n 'X-API-Key': apiKey,\n },\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n\n const response = await fetch(filesEndpoint, fetchOptions);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch files for session: ${response.status}`\n );\n }\n\n const files = await response.json();\n if (Array.isArray(files) && files.length > 0) {\n const fileReferences: t.CodeEnvFile[] = files.map((file) => {\n // Extract the ID from the file name (part after session ID prefix and before extension)\n const nameParts = file.name.split('/');\n const id = nameParts.length > 1 ? nameParts[1].split('.')[0] : '';\n\n return {\n session_id,\n id,\n name: file.metadata['original-filename'],\n };\n });\n\n if (!postData.files) {\n postData.files = fileReferences;\n } else if (Array.isArray(postData.files)) {\n postData.files = [...postData.files, ...fileReferences];\n }\n }\n } catch {\n // eslint-disable-next-line no-console\n console.warn(`Failed to fetch files for session: ${session_id}`);\n }\n }\n\n try {\n const fetchOptions: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': 'Illuma/1.0',\n 'X-API-Key': apiKey,\n },\n body: JSON.stringify(postData),\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n const response = await fetch(EXEC_ENDPOINT, fetchOptions);\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n\n const result: t.ExecuteResult = await response.json();\n let formattedOutput = '';\n if (result.stdout) {\n formattedOutput += `stdout:\\n${result.stdout}\\n`;\n } else {\n formattedOutput += emptyOutputMessage;\n }\n if (result.stderr) formattedOutput += `stderr:\\n${result.stderr}\\n`;\n if (result.files && result.files.length > 0) {\n formattedOutput += 'Generated files:\\n';\n\n const fileCount = result.files.length;\n for (let i = 0; i < fileCount; i++) {\n const file = result.files[i];\n const isImage = imageExtRegex.test(file.name);\n formattedOutput += `- /mnt/data/${file.name} | ${isImage ? imageMessage : otherMessage}`;\n\n if (i < fileCount - 1) {\n formattedOutput += fileCount <= 3 ? ', ' : ',\\n';\n }\n }\n\n formattedOutput += `\\nsession_id: ${result.session_id}\\n\\n${accessMessage}`;\n return [\n formattedOutput.trim(),\n {\n session_id: result.session_id,\n files: result.files,\n },\n ];\n }\n\n return [formattedOutput.trim(), { session_id: result.session_id }];\n } catch (error) {\n throw new Error(\n `Execution error:\\n\\n${(error as Error | undefined)?.message}`\n );\n }\n },\n {\n name: Constants.EXECUTE_CODE,\n description,\n schema: CodeExecutionToolSchema,\n responseFormat: Constants.CONTENT_AND_ARTIFACT,\n }\n );\n}\n\nexport { createCodeExecutionTool };\n"],"names":[],"mappings":";;;;;;;;AASA,MAAM,EAAE;AAED,MAAM,aAAa,GAAG;AACtB,MAAM,cAAc,GAAG,MAC5B,sBAAsB,CAAC,MAAM,CAAC,YAAY,CAAC;IAC3C,SAAS,CAAC;AAEZ,MAAM,YAAY,GAAG,wCAAwC;AAC7D,MAAM,YAAY,GAAG,wCAAwC;AAC7D,MAAM,aAAa,GACjB,mKAAmK;AACrK,MAAM,kBAAkB,GACtB,4DAA4D;AAE9D,MAAM,uBAAuB,GAAG,CAAC,CAAC,MAAM,CAAC;AACvC,IAAA,IAAI,EAAE;AACH,SAAA,IAAI,CAAC;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,GAAG;QACH,KAAK;QACL,MAAM;QACN,KAAK;QACL,IAAI;QACJ,IAA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AI,EAAE,SAAS,CAAC,YAAY;QAC5B,WAAW;AACX,QAAA,MAAM,EAAE,uBAAuB;QAC/B,cAAc,EAAE,SAAS,CAAC,oBAAoB;AAC/C,KAAA,CACF;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"CodeExecutor.mjs","sources":["../../../src/tools/CodeExecutor.ts"],"sourcesContent":["import { z } from 'zod';\nimport { config } from 'dotenv';\nimport fetch, { RequestInit } from 'node-fetch';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\nimport { tool, DynamicStructuredTool } from '@langchain/core/tools';\nimport { getEnvironmentVariable } from '@langchain/core/utils/env';\nimport type * as t from '@/types';\nimport { EnvVar, Constants } from '@/common';\n\nconfig();\n\nexport const imageExtRegex = /\\.(jpg|jpeg|png|gif|webp)$/i;\nexport const getCodeBaseURL = (): string =>\n getEnvironmentVariable(EnvVar.CODE_BASEURL) ??\n Constants.OFFICIAL_CODE_BASEURL;\n\nconst imageMessage = 'Image is already displayed to the user';\nconst otherMessage = 'File is already downloaded by the user';\nconst accessMessage =\n 'Note: Files are READ-ONLY. Save changes to NEW filenames. To access these files in future executions, provide the `session_id` as a parameter (not in your code).';\nconst emptyOutputMessage =\n 'stdout: Empty. Ensure you\\'re writing output explicitly.\\n';\n\nconst CodeExecutionToolSchema = z.object({\n lang: z\n .enum([\n 'py',\n 'js',\n 'ts',\n 'c',\n 'cpp',\n 'java',\n 'php',\n 'rs',\n 'go',\n 'd',\n 'f90',\n 'r',\n ])\n .describe('The programming language or runtime to execute the code in.'),\n code: z.string()\n .describe(`The complete, self-contained code to execute, without any truncation or minimization.\n- The environment is stateless; variables and imports don't persist between executions.\n- When using \\`session_id\\`: Don't hardcode it in \\`code\\`, and write file modifications to NEW filenames (files are READ-ONLY).\n- Input code **IS ALREADY** displayed to the user, so **DO NOT** repeat it in your response unless asked.\n- Output code **IS NOT** displayed to the user, so **DO** write all desired output explicitly.\n- IMPORTANT: You MUST explicitly print/output ALL results you want the user to see.\n- py: This is not a Jupyter notebook environment. Use \\`print()\\` for all outputs.\n- py: Matplotlib: Use \\`plt.savefig()\\` to save plots as files.\n- js: use the \\`console\\` or \\`process\\` methods for all outputs.\n- r: IMPORTANT: No X11 display available. ALL graphics MUST use Cairo library (library(Cairo)).\n- Other languages: use appropriate output functions.`),\n session_id: z\n .string()\n .optional()\n .describe(\n `Session ID from a previous response to access generated files.\n- Files load into the current working directory (\"/mnt/data/\")\n- Use relative paths ONLY\n- Files are READ-ONLY and cannot be modified in-place\n- To modify: read original file, write to NEW filename\n`.trim()\n ),\n args: z\n .array(z.string())\n .optional()\n .describe(\n 'Additional arguments to execute the code with. This should only be used if the input code requires additional arguments to run.'\n ),\n});\n\nconst baseEndpoint = getCodeBaseURL();\nconst EXEC_ENDPOINT = `${baseEndpoint}/exec`;\n\nfunction createCodeExecutionTool(\n params: t.CodeExecutionToolParams = {}\n): DynamicStructuredTool<typeof CodeExecutionToolSchema> {\n const apiKey =\n params[EnvVar.CODE_API_KEY] ??\n params.apiKey ??\n getEnvironmentVariable(EnvVar.CODE_API_KEY) ??\n '';\n if (!apiKey) {\n throw new Error('No API key provided for code execution tool.');\n }\n\n const description = `\nβ STOP! Before using this tool, ask: \"Does user need a DOWNLOADABLE FILE?\"\n- If NO (dashboard, chart, visualization, UI) β DO NOT USE THIS TOOL. Use :::artifact instead.\n- If YES (.pptx, .docx, .pdf, .xlsx) β Use this tool.\n\nRuns code in a stateless execution environment. Each execution is isolated.\n\nπ« NEVER USE FOR:\n- Dashboards, charts, visualizations β Use :::artifact with React/Chart.js\n- \"Mock data\" or \"sample data\" for display β Hardcode data in artifact\n- UI components, HTML pages, React apps β Use :::artifact\n\nβ
ONLY USE FOR:\n- File generation: PowerPoint (.pptx), Word (.docx), PDF (.pdf), Excel (.xlsx)\n- Processing uploaded files (CSV, Excel analysis)\n- Heavy computation requiring Python\n\nRules:\n- No network access available\n- Generated files auto-delivered (no download links needed)\n`.trim();\n\n return tool<typeof CodeExecutionToolSchema>(\n async ({ lang, code, session_id, ...rest }) => {\n const postData = {\n lang,\n code,\n ...rest,\n ...params,\n };\n\n if (session_id != null && session_id.length > 0) {\n try {\n const filesEndpoint = `${baseEndpoint}/files/${session_id}?detail=full`;\n const fetchOptions: RequestInit = {\n method: 'GET',\n headers: {\n 'User-Agent': 'Illuma/1.0',\n 'X-API-Key': apiKey,\n },\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n\n const response = await fetch(filesEndpoint, fetchOptions);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch files for session: ${response.status}`\n );\n }\n\n const files = await response.json();\n if (Array.isArray(files) && files.length > 0) {\n const fileReferences: t.CodeEnvFile[] = files.map((file) => {\n // Extract the ID from the file name (part after session ID prefix and before extension)\n const nameParts = file.name.split('/');\n const id = nameParts.length > 1 ? nameParts[1].split('.')[0] : '';\n\n return {\n session_id,\n id,\n name: file.metadata['original-filename'],\n };\n });\n\n if (!postData.files) {\n postData.files = fileReferences;\n } else if (Array.isArray(postData.files)) {\n postData.files = [...postData.files, ...fileReferences];\n }\n }\n } catch {\n // eslint-disable-next-line no-console\n console.warn(`Failed to fetch files for session: ${session_id}`);\n }\n }\n\n try {\n const fetchOptions: RequestInit = {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': 'Illuma/1.0',\n 'X-API-Key': apiKey,\n },\n body: JSON.stringify(postData),\n };\n\n if (process.env.PROXY != null && process.env.PROXY !== '') {\n fetchOptions.agent = new HttpsProxyAgent(process.env.PROXY);\n }\n const response = await fetch(EXEC_ENDPOINT, fetchOptions);\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n\n const result: t.ExecuteResult = await response.json();\n let formattedOutput = '';\n if (result.stdout) {\n formattedOutput += `stdout:\\n${result.stdout}\\n`;\n } else {\n formattedOutput += emptyOutputMessage;\n }\n if (result.stderr) formattedOutput += `stderr:\\n${result.stderr}\\n`;\n if (result.files && result.files.length > 0) {\n formattedOutput += 'Generated files:\\n';\n\n const fileCount = result.files.length;\n for (let i = 0; i < fileCount; i++) {\n const file = result.files[i];\n const isImage = imageExtRegex.test(file.name);\n formattedOutput += `- /mnt/data/${file.name} | ${isImage ? imageMessage : otherMessage}`;\n\n if (i < fileCount - 1) {\n formattedOutput += fileCount <= 3 ? ', ' : ',\\n';\n }\n }\n\n formattedOutput += `\\nsession_id: ${result.session_id}\\n\\n${accessMessage}`;\n return [\n formattedOutput.trim(),\n {\n session_id: result.session_id,\n files: result.files,\n },\n ];\n }\n\n return [formattedOutput.trim(), { session_id: result.session_id }];\n } catch (error) {\n throw new Error(\n `Execution error:\\n\\n${(error as Error | undefined)?.message}`\n );\n }\n },\n {\n name: Constants.EXECUTE_CODE,\n description,\n schema: CodeExecutionToolSchema,\n responseFormat: Constants.CONTENT_AND_ARTIFACT,\n }\n );\n}\n\nexport { createCodeExecutionTool };\n"],"names":[],"mappings":";;;;;;;;AASA,MAAM,EAAE;AAED,MAAM,aAAa,GAAG;AACtB,MAAM,cAAc,GAAG,MAC5B,sBAAsB,CAAC,MAAM,CAAC,YAAY,CAAC;IAC3C,SAAS,CAAC;AAEZ,MAAM,YAAY,GAAG,wCAAwC;AAC7D,MAAM,YAAY,GAAG,wCAAwC;AAC7D,MAAM,aAAa,GACjB,mKAAmK;AACrK,MAAM,kBAAkB,GACtB,4DAA4D;AAE9D,MAAM,uBAAuB,GAAG,CAAC,CAAC,MAAM,CAAC;AACvC,IAAA,IAAI,EAAE;AACH,SAAA,IAAI,CAAC;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,GAAG;QACH,KAAK;QACL,MAAM;QACN,KAAK;QACL,IAAI;QACJ,IAAI;QACJ,GAAG;QACH,KAAK;QACL,GAAG;KACJ;SACA,QAAQ,CAAC,6DAA6D,CAAC;AAC1E,IAAA,IAAI,EAAE,CAAC,CAAC,MAAM;AACX,SAAA,QAAQ,CAAC,CAAA;;;;;;;;;;qDAUuC,CAA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,12 @@ export declare class AgentContext {
|
|
|
75
75
|
tokenCalculationPromise?: Promise<void>;
|
|
76
76
|
/** Format content blocks as strings (for legacy compatibility) */
|
|
77
77
|
useLegacyContent: boolean;
|
|
78
|
+
/** Detailed per-tool token breakdown for admin tracking */
|
|
79
|
+
private toolsDetail;
|
|
80
|
+
/** Total tool tokens (sum of all toolsDetail) */
|
|
81
|
+
private toolTokensTotal;
|
|
82
|
+
/** Per-prompt token breakdown for detailed admin reporting */
|
|
83
|
+
private promptBreakdown;
|
|
78
84
|
constructor({ agentId, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, instructions, additionalInstructions, dynamicContext, reasoningKey, toolEnd, instructionTokens, useLegacyContent, }: {
|
|
79
85
|
agentId: string;
|
|
80
86
|
provider: Providers;
|
|
@@ -134,8 +140,51 @@ export declare class AgentContext {
|
|
|
134
140
|
/**
|
|
135
141
|
* Calculate tool tokens and add to instruction tokens
|
|
136
142
|
* Note: System message tokens are calculated during systemRunnable creation
|
|
143
|
+
* Also tracks per-tool token breakdown for admin reporting
|
|
137
144
|
*/
|
|
138
145
|
calculateInstructionTokens(tokenCounter: t.TokenCounter): Promise<void>;
|
|
146
|
+
/**
|
|
147
|
+
* Set the per-prompt token breakdown for detailed admin tracking.
|
|
148
|
+
* Called by the client after assembling all prompt components.
|
|
149
|
+
* @param breakdown - Object with token counts per prompt component
|
|
150
|
+
*/
|
|
151
|
+
setPromptBreakdown(breakdown: {
|
|
152
|
+
branding?: number;
|
|
153
|
+
toolRouting?: number;
|
|
154
|
+
agentInstructions?: number;
|
|
155
|
+
mcpInstructions?: number;
|
|
156
|
+
artifacts?: number;
|
|
157
|
+
memory?: number;
|
|
158
|
+
}): void;
|
|
159
|
+
/**
|
|
160
|
+
* Get a detailed breakdown of context tokens for admin reporting.
|
|
161
|
+
* This provides visibility into what's consuming the input token budget.
|
|
162
|
+
* @returns ContextBreakdown object with per-component token counts
|
|
163
|
+
*/
|
|
164
|
+
getContextBreakdown(): {
|
|
165
|
+
instructions: number;
|
|
166
|
+
artifacts: number;
|
|
167
|
+
tools: number;
|
|
168
|
+
toolCount: number;
|
|
169
|
+
toolContext: number;
|
|
170
|
+
total: number;
|
|
171
|
+
toolsDetail: Array<{
|
|
172
|
+
name: string;
|
|
173
|
+
tokens: number;
|
|
174
|
+
}>;
|
|
175
|
+
toolContextDetail: Array<{
|
|
176
|
+
name: string;
|
|
177
|
+
tokens: number;
|
|
178
|
+
}>;
|
|
179
|
+
prompts?: {
|
|
180
|
+
branding: number;
|
|
181
|
+
toolRouting: number;
|
|
182
|
+
agentInstructions: number;
|
|
183
|
+
mcpInstructions: number;
|
|
184
|
+
artifacts: number;
|
|
185
|
+
memory: number;
|
|
186
|
+
};
|
|
187
|
+
};
|
|
139
188
|
/**
|
|
140
189
|
* Gets the tool registry for deferred tools (for tool search).
|
|
141
190
|
* @param onlyDeferred If true, only returns tools with defer_loading=true
|
|
@@ -85,6 +85,26 @@ export declare class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode>
|
|
|
85
85
|
* Returns a map where key is the contentPart index and value is the agentId
|
|
86
86
|
*/
|
|
87
87
|
getContentPartAgentMap(): Map<number, string>;
|
|
88
|
+
/**
|
|
89
|
+
* Get the context breakdown from the primary agent for admin token tracking.
|
|
90
|
+
* Returns detailed token counts for instructions, tools, etc.
|
|
91
|
+
*/
|
|
92
|
+
getContextBreakdown(): {
|
|
93
|
+
instructions: number;
|
|
94
|
+
artifacts: number;
|
|
95
|
+
tools: number;
|
|
96
|
+
toolCount: number;
|
|
97
|
+
toolContext: number;
|
|
98
|
+
total: number;
|
|
99
|
+
toolsDetail: Array<{
|
|
100
|
+
name: string;
|
|
101
|
+
tokens: number;
|
|
102
|
+
}>;
|
|
103
|
+
toolContextDetail: Array<{
|
|
104
|
+
name: string;
|
|
105
|
+
tokens: number;
|
|
106
|
+
}>;
|
|
107
|
+
} | null;
|
|
88
108
|
createSystemRunnable({ provider, clientOptions, instructions, additional_instructions, }: {
|
|
89
109
|
provider?: Providers;
|
|
90
110
|
clientOptions?: t.ClientOptions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "illuma-agents",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"type": "module",
|
|
15
15
|
"description": "Illuma AI Agents Library",
|
|
16
|
-
"author": "
|
|
16
|
+
"author": "Illuma Team",
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"packageManager": "npm@10.5.2",
|
|
19
19
|
"engines": {
|
|
@@ -153,6 +153,26 @@ export class AgentContext {
|
|
|
153
153
|
tokenCalculationPromise?: Promise<void>;
|
|
154
154
|
/** Format content blocks as strings (for legacy compatibility) */
|
|
155
155
|
useLegacyContent: boolean = false;
|
|
156
|
+
/** Detailed per-tool token breakdown for admin tracking */
|
|
157
|
+
private toolsDetail: Array<{ name: string; tokens: number }> = [];
|
|
158
|
+
/** Total tool tokens (sum of all toolsDetail) */
|
|
159
|
+
private toolTokensTotal: number = 0;
|
|
160
|
+
/** Per-prompt token breakdown for detailed admin reporting */
|
|
161
|
+
private promptBreakdown: {
|
|
162
|
+
branding: number;
|
|
163
|
+
toolRouting: number;
|
|
164
|
+
agentInstructions: number;
|
|
165
|
+
mcpInstructions: number;
|
|
166
|
+
artifacts: number;
|
|
167
|
+
memory: number;
|
|
168
|
+
} = {
|
|
169
|
+
branding: 0,
|
|
170
|
+
toolRouting: 0,
|
|
171
|
+
agentInstructions: 0,
|
|
172
|
+
mcpInstructions: 0,
|
|
173
|
+
artifacts: 0,
|
|
174
|
+
memory: 0,
|
|
175
|
+
};
|
|
156
176
|
|
|
157
177
|
constructor({
|
|
158
178
|
agentId,
|
|
@@ -386,8 +406,6 @@ export class AgentContext {
|
|
|
386
406
|
const supportsCaching = modelId.includes('claude') || modelId.includes('anthropic') || modelId.includes('nova');
|
|
387
407
|
|
|
388
408
|
if (bedrockOptions?.promptCache === true && supportsCaching) {
|
|
389
|
-
// Always log system cache structure
|
|
390
|
-
console.log(`[Cache] π System | chars=${instructionsString.length} | tokens=${this.systemMessageTokens} | model=${modelId}`);
|
|
391
409
|
|
|
392
410
|
finalInstructions = {
|
|
393
411
|
content: [
|
|
@@ -423,6 +441,8 @@ export class AgentContext {
|
|
|
423
441
|
reset(): void {
|
|
424
442
|
this.instructionTokens = 0;
|
|
425
443
|
this.systemMessageTokens = 0;
|
|
444
|
+
this.toolsDetail = [];
|
|
445
|
+
this.toolTokensTotal = 0;
|
|
426
446
|
this.cachedSystemRunnable = undefined;
|
|
427
447
|
this.systemRunnableStale = true;
|
|
428
448
|
this.lastToken = undefined;
|
|
@@ -458,11 +478,14 @@ export class AgentContext {
|
|
|
458
478
|
/**
|
|
459
479
|
* Calculate tool tokens and add to instruction tokens
|
|
460
480
|
* Note: System message tokens are calculated during systemRunnable creation
|
|
481
|
+
* Also tracks per-tool token breakdown for admin reporting
|
|
461
482
|
*/
|
|
462
483
|
async calculateInstructionTokens(
|
|
463
484
|
tokenCounter: t.TokenCounter
|
|
464
485
|
): Promise<void> {
|
|
465
486
|
let toolTokens = 0;
|
|
487
|
+
this.toolsDetail = []; // Reset per-tool breakdown
|
|
488
|
+
|
|
466
489
|
if (this.tools && this.tools.length > 0) {
|
|
467
490
|
for (const tool of this.tools) {
|
|
468
491
|
const genericTool = tool as Record<string, unknown>;
|
|
@@ -480,17 +503,100 @@ export class AgentContext {
|
|
|
480
503
|
describedSchema as Parameters<typeof zodToJsonSchema>[0],
|
|
481
504
|
(genericTool.name as string) || ''
|
|
482
505
|
);
|
|
483
|
-
|
|
506
|
+
const toolName = (genericTool.name as string) || 'unknown';
|
|
507
|
+
const tokens = tokenCounter(
|
|
484
508
|
new SystemMessage(JSON.stringify(jsonSchema))
|
|
485
509
|
);
|
|
510
|
+
|
|
511
|
+
// Track per-tool breakdown
|
|
512
|
+
this.toolsDetail.push({ name: toolName, tokens });
|
|
513
|
+
toolTokens += tokens;
|
|
486
514
|
}
|
|
487
515
|
}
|
|
488
516
|
}
|
|
489
517
|
|
|
518
|
+
// Store total tool tokens for breakdown reporting
|
|
519
|
+
this.toolTokensTotal = toolTokens;
|
|
520
|
+
|
|
490
521
|
// Add tool tokens to existing instruction tokens (which may already include system message tokens)
|
|
491
522
|
this.instructionTokens += toolTokens;
|
|
492
523
|
}
|
|
493
524
|
|
|
525
|
+
/**
|
|
526
|
+
* Set the per-prompt token breakdown for detailed admin tracking.
|
|
527
|
+
* Called by the client after assembling all prompt components.
|
|
528
|
+
* @param breakdown - Object with token counts per prompt component
|
|
529
|
+
*/
|
|
530
|
+
setPromptBreakdown(breakdown: {
|
|
531
|
+
branding?: number;
|
|
532
|
+
toolRouting?: number;
|
|
533
|
+
agentInstructions?: number;
|
|
534
|
+
mcpInstructions?: number;
|
|
535
|
+
artifacts?: number;
|
|
536
|
+
memory?: number;
|
|
537
|
+
}): void {
|
|
538
|
+
if (breakdown.branding !== undefined) this.promptBreakdown.branding = breakdown.branding;
|
|
539
|
+
if (breakdown.toolRouting !== undefined) this.promptBreakdown.toolRouting = breakdown.toolRouting;
|
|
540
|
+
if (breakdown.agentInstructions !== undefined) this.promptBreakdown.agentInstructions = breakdown.agentInstructions;
|
|
541
|
+
if (breakdown.mcpInstructions !== undefined) this.promptBreakdown.mcpInstructions = breakdown.mcpInstructions;
|
|
542
|
+
if (breakdown.artifacts !== undefined) this.promptBreakdown.artifacts = breakdown.artifacts;
|
|
543
|
+
if (breakdown.memory !== undefined) this.promptBreakdown.memory = breakdown.memory;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Get a detailed breakdown of context tokens for admin reporting.
|
|
548
|
+
* This provides visibility into what's consuming the input token budget.
|
|
549
|
+
* @returns ContextBreakdown object with per-component token counts
|
|
550
|
+
*/
|
|
551
|
+
getContextBreakdown(): {
|
|
552
|
+
instructions: number;
|
|
553
|
+
artifacts: number;
|
|
554
|
+
tools: number;
|
|
555
|
+
toolCount: number;
|
|
556
|
+
toolContext: number;
|
|
557
|
+
total: number;
|
|
558
|
+
toolsDetail: Array<{ name: string; tokens: number }>;
|
|
559
|
+
toolContextDetail: Array<{ name: string; tokens: number }>;
|
|
560
|
+
prompts?: {
|
|
561
|
+
branding: number;
|
|
562
|
+
toolRouting: number;
|
|
563
|
+
agentInstructions: number;
|
|
564
|
+
mcpInstructions: number;
|
|
565
|
+
artifacts: number;
|
|
566
|
+
memory: number;
|
|
567
|
+
};
|
|
568
|
+
} {
|
|
569
|
+
// Calculate sum of prompt components
|
|
570
|
+
const promptsTotal =
|
|
571
|
+
this.promptBreakdown.branding +
|
|
572
|
+
this.promptBreakdown.toolRouting +
|
|
573
|
+
this.promptBreakdown.agentInstructions +
|
|
574
|
+
this.promptBreakdown.mcpInstructions +
|
|
575
|
+
this.promptBreakdown.artifacts +
|
|
576
|
+
this.promptBreakdown.memory;
|
|
577
|
+
|
|
578
|
+
return {
|
|
579
|
+
// System message tokens (instructions + additional_instructions)
|
|
580
|
+
instructions: this.systemMessageTokens,
|
|
581
|
+
// Artifacts prompt tokens
|
|
582
|
+
artifacts: this.promptBreakdown.artifacts,
|
|
583
|
+
// Total tool definition tokens
|
|
584
|
+
tools: this.toolTokensTotal,
|
|
585
|
+
// Number of tools
|
|
586
|
+
toolCount: this.toolsDetail.length,
|
|
587
|
+
// Tool context/usage instructions (currently embedded in system message)
|
|
588
|
+
toolContext: 0,
|
|
589
|
+
// Total tracked context tokens
|
|
590
|
+
total: this.instructionTokens,
|
|
591
|
+
// Per-tool token breakdown
|
|
592
|
+
toolsDetail: [...this.toolsDetail],
|
|
593
|
+
// Tool context detail (currently not tracked separately)
|
|
594
|
+
toolContextDetail: [],
|
|
595
|
+
// Per-prompt breakdown (only include if any prompts were tracked)
|
|
596
|
+
prompts: promptsTotal > 0 ? { ...this.promptBreakdown } : undefined,
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
494
600
|
/**
|
|
495
601
|
* Gets the tool registry for deferred tools (for tool search).
|
|
496
602
|
* @param onlyDeferred If true, only returns tools with defer_loading=true
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -391,6 +391,27 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
391
391
|
return contentPartAgentMap;
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
+
/**
|
|
395
|
+
* Get the context breakdown from the primary agent for admin token tracking.
|
|
396
|
+
* Returns detailed token counts for instructions, tools, etc.
|
|
397
|
+
*/
|
|
398
|
+
getContextBreakdown(): {
|
|
399
|
+
instructions: number;
|
|
400
|
+
artifacts: number;
|
|
401
|
+
tools: number;
|
|
402
|
+
toolCount: number;
|
|
403
|
+
toolContext: number;
|
|
404
|
+
total: number;
|
|
405
|
+
toolsDetail: Array<{ name: string; tokens: number }>;
|
|
406
|
+
toolContextDetail: Array<{ name: string; tokens: number }>;
|
|
407
|
+
} | null {
|
|
408
|
+
const primaryContext = this.agentContexts.get(this.defaultAgentId);
|
|
409
|
+
if (!primaryContext) {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
return primaryContext.getContextBreakdown();
|
|
413
|
+
}
|
|
414
|
+
|
|
394
415
|
/* Graph */
|
|
395
416
|
|
|
396
417
|
createSystemRunnable({
|
package/src/llm/bedrock/index.ts
CHANGED
|
@@ -92,8 +92,7 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
console.log(`[Cache] π§ Tools | Core: [${coreToolNames.join(', ')}] (${coreTools.length}) | MCP: [${mcpToolNames.join(', ')}] (${mcpTools.length})`);
|
|
95
|
+
|
|
97
96
|
|
|
98
97
|
// Build tools array with strategic cachePoints:
|
|
99
98
|
// [CoreTool1, CoreTool2, cachePoint] + [MCPTool1, MCPTool2, cachePoint]
|
|
@@ -120,7 +119,7 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
|
120
119
|
cachePointCount++;
|
|
121
120
|
}
|
|
122
121
|
|
|
123
|
-
|
|
122
|
+
|
|
124
123
|
|
|
125
124
|
params.toolConfig.tools = toolsWithCache;
|
|
126
125
|
}
|
|
@@ -176,11 +175,6 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
|
|
|
176
175
|
const outputTokens = (usage.outputTokens as number) ?? 0;
|
|
177
176
|
|
|
178
177
|
if (cacheRead > 0 || cacheWrite > 0) {
|
|
179
|
-
// Always log cache results for tracking
|
|
180
|
-
const cacheStatus = cacheRead > 0 && cacheWrite === 0 ? 'β
HIT' :
|
|
181
|
-
cacheWrite > 0 && cacheRead === 0 ? 'π WRITE' :
|
|
182
|
-
cacheRead > 0 && cacheWrite > 0 ? 'π PARTIAL' : 'β MISS';
|
|
183
|
-
console.log(`[Cache] ${cacheStatus} | read=${cacheRead} | write=${cacheWrite} | input=${inputTokens} | output=${outputTokens}`);
|
|
184
178
|
|
|
185
179
|
needsModification = true;
|
|
186
180
|
enhancedUsageMetadata = {
|