illuma-agents 1.0.17 → 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/graphs/Graph.cjs +18 -9
- 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/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 +2 -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 +2 -2
- package/dist/cjs/messages/core.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +2 -1
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/tools.cjs +2 -2
- package/dist/cjs/messages/tools.cjs.map +1 -1
- package/dist/cjs/stream.cjs +29 -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 +1 -1
- package/dist/cjs/tools/ToolNode.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 +7 -5
- package/dist/cjs/utils/contextAnalytics.cjs.map +1 -1
- package/dist/cjs/utils/run.cjs.map +1 -1
- package/dist/cjs/utils/toonFormat.cjs +42 -12
- package/dist/cjs/utils/toonFormat.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +3 -1
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +18 -9
- 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/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 +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 +2 -2
- package/dist/esm/messages/core.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +2 -1
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/tools.mjs +2 -2
- package/dist/esm/messages/tools.mjs.map +1 -1
- package/dist/esm/stream.mjs +29 -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 +1 -1
- package/dist/esm/tools/ToolNode.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 +7 -5
- package/dist/esm/utils/contextAnalytics.mjs.map +1 -1
- package/dist/esm/utils/run.mjs.map +1 -1
- package/dist/esm/utils/toonFormat.mjs +42 -12
- package/dist/esm/utils/toonFormat.mjs.map +1 -1
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +19 -0
- package/dist/types/types/tools.d.ts +3 -1
- package/package.json +2 -2
- package/src/agents/AgentContext.ts +28 -20
- package/src/graphs/Graph.ts +76 -37
- 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 +5 -3
- package/src/messages/format.ts +6 -2
- package/src/messages/tools.ts +2 -2
- 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 +4 -4
- 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 -95
- package/src/utils/llmConfig.ts +8 -1
- package/src/utils/run.ts +5 -4
- package/src/utils/toonFormat.ts +475 -437
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toonFormat.mjs","sources":["../../../src/utils/toonFormat.ts"],"sourcesContent":["/**\r\n * TOON Format Utility\r\n * \r\n * Provides JSON to TOON conversion for token-efficient encoding.\r\n * TOON format can reduce token count by 30-60% for JSON data.\r\n * \r\n * Uses the official @toon-format/toon library when available.\r\n */\r\n\r\n// Dynamic import holder for the TOON library (ESM)\r\nlet toonEncode: ((data: unknown) => string) | null = null;\r\nlet toonLoadAttempted = false;\r\nlet toonLoadPromise: Promise<boolean> | null = null;\r\n\r\n/**\r\n * Lazily loads the TOON library\r\n * Tries both ESM dynamic import and CommonJS require for compatibility\r\n */\r\nasync function loadToonLibrary(): Promise<boolean> {\r\n if (toonLoadAttempted) return toonEncode !== null;\r\n toonLoadAttempted = true;\r\n\r\n // Try ESM dynamic import first\r\n try {\r\n const mod = await import('@toon-format/toon');\r\n toonEncode = mod.encode;\r\n return true;\r\n } catch {\r\n // ESM import failed, try CommonJS require\r\n try {\r\n // eslint-disable-next-line @typescript-eslint/no-require-imports\r\n const mod = require('@toon-format/toon');\r\n toonEncode = mod.encode;\r\n return true;\r\n } catch {\r\n // Library not available - that's OK, we'll return original content\r\n return false;\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Ensures the TOON library is loaded. Call this before using jsonToToon in tests.\r\n */\r\nexport async function ensureToonLoaded(): Promise<boolean> {\r\n if (!toonLoadPromise) {\r\n toonLoadPromise = loadToonLibrary();\r\n }\r\n return toonLoadPromise;\r\n}\r\n\r\n// Start loading immediately\r\nensureToonLoaded();\r\n\r\n/**\r\n * Check if a string appears to be in TOON format.\r\n * TOON format characteristics:\r\n * - Uses key: value syntax (like YAML)\r\n * - Uses array notation like [3] or {fields}\r\n * - Does NOT start with { or [\r\n * - Often has patterns like \"name[N]:\" or \"name{fields}:\"\r\n * \r\n * @param str - String to check\r\n * @returns true if string appears to be TOON format\r\n */\r\nexport function isToonFormat(str: string): boolean {\r\n if (typeof str !== 'string' || str.length === 0) return false;\r\n \r\n const trimmed = str.trim();\r\n \r\n // TOON doesn't start with JSON brackets\r\n if (trimmed.startsWith('{') || trimmed.startsWith('[')) return false;\r\n \r\n // Check for TOON-specific patterns:\r\n // 1. Key-value with colon (but not URL-like patterns)\r\n // 2. Array notation like \"items[3]:\" or \"data[10]:\"\r\n // 3. Object schema notation like \"items{id,name}:\"\r\n const toonPatterns = [\r\n /^\\w+:$/m, // Simple key: at start of line\r\n /^\\w+\\[\\d+\\]:/m, // Array notation: items[3]:\r\n /^\\w+\\{[^}]+\\}:/m, // Schema notation: items{id,name}:\r\n /^\\w+\\[\\d+\\]\\{[^}]+\\}:/m, // Combined: items[3]{id,name}:\r\n ];\r\n \r\n // Must match at least one TOON pattern\r\n const hasToonPattern = toonPatterns.some(pattern => pattern.test(trimmed));\r\n \r\n // Additional check: TOON typically has multiple lines with consistent indentation\r\n const lines = trimmed.split('\\n').filter(l => l.trim());\r\n const hasMultipleKeyValueLines = lines.filter(l => /^\\s*\\w+.*:/.test(l)).length >= 2;\r\n \r\n return hasToonPattern || hasMultipleKeyValueLines;\r\n}\r\n\r\n/**\r\n * Extract the first valid JSON object or array from a string.\r\n * Handles pure JSON or JSON embedded in text (e.g., tool responses).\r\n * \r\n * @param str - The string to extract JSON from\r\n * @returns Object with found status, parsed JSON, and indices\r\n */\r\nexport function extractFirstJson(str: string): {\r\n found: boolean;\r\n parsed: unknown;\r\n startIndex: number;\r\n endIndex: number;\r\n} {\r\n if (typeof str !== 'string' || str.length === 0) {\r\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\r\n }\r\n\r\n // Find the first { or [ character\r\n const objStart = str.indexOf('{');\r\n const arrStart = str.indexOf('[');\r\n\r\n if (objStart === -1 && arrStart === -1) {\r\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\r\n }\r\n\r\n let startIndex: number;\r\n if (objStart === -1) {\r\n startIndex = arrStart;\r\n } else if (arrStart === -1) {\r\n startIndex = objStart;\r\n } else {\r\n startIndex = Math.min(objStart, arrStart);\r\n }\r\n\r\n // Find the matching closing bracket using a stack-based approach\r\n // Properly handle strings to avoid counting brackets inside strings\r\n let depth = 0;\r\n let inString = false;\r\n let escapeNext = false;\r\n\r\n for (let i = startIndex; i < str.length; i++) {\r\n const char = str[i];\r\n\r\n if (escapeNext) {\r\n escapeNext = false;\r\n continue;\r\n }\r\n\r\n if (char === '\\\\' && inString) {\r\n escapeNext = true;\r\n continue;\r\n }\r\n\r\n if (char === '\"') {\r\n inString = !inString;\r\n continue;\r\n }\r\n\r\n if (inString) continue;\r\n\r\n if (char === '{' || char === '[') depth++;\r\n if (char === '}' || char === ']') depth--;\r\n\r\n if (depth === 0) {\r\n // Found matching bracket, try to parse\r\n const jsonStr = str.substring(startIndex, i + 1);\r\n try {\r\n const parsed = JSON.parse(jsonStr);\r\n return { found: true, parsed, startIndex, endIndex: i };\r\n } catch {\r\n // Not valid JSON at this bracket level, continue searching\r\n // Reset and look for the next JSON start after this position\r\n const nextResult = extractFirstJson(str.substring(i + 1));\r\n if (nextResult.found) {\r\n return {\r\n found: true,\r\n parsed: nextResult.parsed,\r\n startIndex: i + 1 + nextResult.startIndex,\r\n endIndex: i + 1 + nextResult.endIndex,\r\n };\r\n }\r\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\r\n }\r\n }\r\n }\r\n\r\n return { found: false, parsed: null, startIndex: -1, endIndex: -1 };\r\n}\r\n\r\n/**\r\n * Convert JSON content to TOON format for token efficiency.\r\n * Extracts JSON from string if embedded, converts to TOON.\r\n * Returns original string if:\r\n * - Already in TOON format\r\n * - Not JSON\r\n * - TOON conversion fails or is larger\r\n * \r\n * @param str - The string containing JSON to convert\r\n * @returns Object with conversion status, result, and reduction percentage\r\n */\r\nexport function jsonToToon(str: string): {\r\n converted: boolean;\r\n result: string;\r\n reduction: number;\r\n alreadyToon?: boolean;\r\n error?: string;\r\n} {\r\n if (!toonEncode || typeof str !== 'string') {\r\n return { converted: false, result: str, reduction: 0, error: !toonEncode ? 'TOON library not loaded' : undefined };\r\n }\r\n\r\n // Check if already in TOON format - skip conversion\r\n if (isToonFormat(str)) {\r\n return { converted: false, result: str, reduction: 0, alreadyToon: true };\r\n }\r\n\r\n try {\r\n const { found, parsed, startIndex, endIndex } = extractFirstJson(str);\r\n\r\n if (!found || !parsed) {\r\n return { converted: false, result: str, reduction: 0, error: 'No JSON found in content' };\r\n }\r\n\r\n // Check if this JSON structure would benefit from TOON\r\n // Text-heavy content (emails, documents) won't compress well\r\n if (!isToonBeneficial(parsed)) {\r\n return { converted: false, result: str, reduction: 0, error: 'Content is text-heavy (>70% string values), TOON not beneficial' };\r\n }\r\n\r\n const toonResult = toonEncode(parsed);\r\n \r\n // Preserve text before and after the JSON block\r\n const textBefore = str.substring(0, startIndex);\r\n const textAfter = str.substring(endIndex + 1);\r\n \r\n // Build the full result: textBefore + TOON + textAfter\r\n const fullResult = textBefore + toonResult + textAfter;\r\n\r\n if (fullResult.length < str.length) {\r\n const reduction = Math.round(((str.length - fullResult.length) / str.length) * 100);\r\n return { converted: true, result: fullResult, reduction };\r\n } else {\r\n // TOON output was larger or same size - not beneficial\r\n return { converted: false, result: str, reduction: 0, error: `TOON output not smaller (${fullResult.length} >= ${str.length})` };\r\n }\r\n } catch (err) {\r\n // TOON encoding or extraction failed - log error for debugging\r\n const errorMsg = err instanceof Error ? err.message : String(err);\r\n return { converted: false, result: str, reduction: 0, error: `TOON conversion error: ${errorMsg}` };\r\n }\r\n}\r\n\r\n/**\r\n * Check if TOON library is loaded and available\r\n */\r\nexport function isToonAvailable(): boolean {\r\n return toonEncode !== null;\r\n}\r\n\r\n/**\r\n * Result of processing tool output\r\n */\r\nexport interface ProcessToolOutputResult {\r\n /** The processed content string */\r\n content: string;\r\n /** Whether TOON conversion was applied */\r\n toonConverted: boolean;\r\n /** Whether content was truncated */\r\n truncated: boolean;\r\n /** Percentage reduction from TOON (0 if not converted) */\r\n reduction: number;\r\n /** Whether input was already in TOON format */\r\n alreadyToon: boolean;\r\n /** Original content length */\r\n originalLength: number;\r\n /** Estimated original tokens (~4 chars per token) */\r\n originalTokens: number;\r\n /** Final content length */\r\n finalLength: number;\r\n /** Estimated final tokens */\r\n finalTokens: number;\r\n /** Error message if TOON conversion failed (for debugging) */\r\n toonError?: string;\r\n}\r\n\r\n/**\r\n * Options for processing tool output\r\n */\r\nexport interface ProcessToolOutputOptions {\r\n /** Maximum output length in characters (default: 100000) */\r\n maxLength?: number;\r\n /** Whether to apply TOON conversion (default: true) */\r\n enableToon?: boolean;\r\n /** Minimum content size to attempt TOON conversion (default: 1000) */\r\n minSizeForToon?: number;\r\n /** Minimum reduction % to accept TOON result (default: 10) */\r\n minReductionPercent?: number;\r\n}\r\n\r\n/**\r\n * Analyze JSON structure to determine if TOON conversion would be beneficial.\r\n * Text-heavy content (emails, documents) doesn't compress well with TOON.\r\n * Structured data (API responses, metadata) compresses much better.\r\n * \r\n * @param parsed - Parsed JSON object\r\n * @returns true if TOON conversion is likely beneficial\r\n */\r\nfunction isToonBeneficial(parsed: unknown): boolean {\r\n if (!parsed || typeof parsed !== 'object') return false;\r\n \r\n const jsonStr = JSON.stringify(parsed);\r\n const totalLength = jsonStr.length;\r\n \r\n // Count characters in string values (text content)\r\n let textContentLength = 0;\r\n \r\n function countTextContent(obj: unknown): void {\r\n if (typeof obj === 'string') {\r\n // Count string length (this is text content that TOON can't compress)\r\n textContentLength += obj.length;\r\n } else if (Array.isArray(obj)) {\r\n obj.forEach(countTextContent);\r\n } else if (obj && typeof obj === 'object') {\r\n Object.values(obj).forEach(countTextContent);\r\n }\r\n }\r\n \r\n countTextContent(parsed);\r\n \r\n // Calculate ratio of text content to total JSON\r\n const textRatio = textContentLength / totalLength;\r\n \r\n // If more than 70% is text content, TOON won't help much\r\n // TOON compresses structure (field names, brackets), not text\r\n return textRatio < 0.7;\r\n}\r\n\r\n/**\r\n * Process tool output: apply TOON conversion if beneficial, then truncate if needed.\r\n * This is the main entry point for processing any tool output (regular or MCP).\r\n * \r\n * Flow:\r\n * 1. Check if already TOON format → skip conversion\r\n * 2. Try TOON conversion if content is JSON and large enough\r\n * 3. Truncate if still exceeds maxLength (with smart break points)\r\n * \r\n * @param content - The tool output content\r\n * @param options - Processing options\r\n * @returns Processed content and metadata\r\n */\r\nexport function processToolOutput(\r\n content: string,\r\n options: ProcessToolOutputOptions = {}\r\n): ProcessToolOutputResult {\r\n const { \r\n maxLength = 100000, \r\n enableToon = true,\r\n minSizeForToon = 1000,\r\n minReductionPercent = 10, // Increased from 5% - only apply TOON when clearly beneficial\r\n } = options;\r\n\r\n const originalLength = content.length;\r\n const originalTokens = Math.ceil(originalLength / 4);\r\n \r\n let result = content;\r\n let toonConverted = false;\r\n let alreadyToon = false;\r\n let reduction = 0;\r\n let truncated = false;\r\n let toonError: string | undefined;\r\n\r\n // Step 1: Check if already TOON format\r\n if (isToonFormat(content)) {\r\n alreadyToon = true;\r\n }\r\n // Step 2: Apply TOON conversion if enabled and content is large enough\r\n else if (enableToon && content.length > minSizeForToon) {\r\n const toonResult = jsonToToon(content);\r\n if (toonResult.alreadyToon) {\r\n alreadyToon = true;\r\n } else if (toonResult.converted && toonResult.reduction >= minReductionPercent) {\r\n result = toonResult.result;\r\n toonConverted = true;\r\n reduction = toonResult.reduction;\r\n } else if (toonResult.error) {\r\n // Track error for debugging\r\n toonError = toonResult.error;\r\n } else if (toonResult.converted && toonResult.reduction < minReductionPercent) {\r\n // TOON converted but reduction was too small to be worth it\r\n toonError = `TOON reduction ${toonResult.reduction}% below threshold ${minReductionPercent}%`;\r\n } else if (!toonResult.converted) {\r\n // Conversion failed without explicit error - investigate\r\n toonError = toonResult.error || 'TOON conversion returned false with no error';\r\n }\r\n }\r\n\r\n // Step 3: Truncate if still too long (with smart break points)\r\n if (result.length > maxLength) {\r\n let truncatedStr = result.substring(0, maxLength);\r\n \r\n // Try to find a clean break point\r\n if (toonConverted || alreadyToon) {\r\n // For TOON format, break at newline for cleaner output\r\n const lastNewline = truncatedStr.lastIndexOf('\\n');\r\n if (lastNewline > maxLength * 0.7) {\r\n truncatedStr = truncatedStr.substring(0, lastNewline);\r\n }\r\n } else {\r\n // For JSON, try to find a clean JSON break point\r\n const lastCompleteItem = truncatedStr.lastIndexOf('},');\r\n const lastArrayItem = truncatedStr.lastIndexOf('],');\r\n const breakPoint = Math.max(lastCompleteItem, lastArrayItem);\r\n if (breakPoint > maxLength * 0.5) {\r\n truncatedStr = truncatedStr.substring(0, breakPoint + 1);\r\n }\r\n }\r\n \r\n // Build truncation message\r\n const truncationInfo = toonConverted\r\n ? `Original ${originalLength.toLocaleString()} chars → TOON ${result.length.toLocaleString()} chars (${reduction}% saved). ` +\r\n `Still exceeds ${maxLength.toLocaleString()} char limit.`\r\n : `Original output was ${originalLength.toLocaleString()} characters (~${originalTokens.toLocaleString()} tokens).`;\r\n \r\n result = truncatedStr + `\\n\\n[OUTPUT_TRUNCATED: ${truncationInfo} Please use more specific queries or smaller date ranges.]`;\r\n truncated = true;\r\n }\r\n\r\n const finalLength = result.length;\r\n const finalTokens = Math.ceil(finalLength / 4);\r\n\r\n return { \r\n content: result, \r\n toonConverted, \r\n truncated, \r\n reduction, \r\n alreadyToon,\r\n originalLength,\r\n originalTokens,\r\n finalLength,\r\n finalTokens,\r\n toonError,\r\n };\r\n}\r\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,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;AAAE,QAAA,OAAO,KAAK;AAE7D,IAAA,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,EAAE;;AAG1B,IAAA,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC,UAAU,CAAC,GA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file":"toonFormat.mjs","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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W,CAAC,IAAI,CAAC;YACpD,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,gBAAgB,EAAE,aAAa,CAAC;AAC5D,YAAA,IAAI,UAAU,GAAG,SAAS,GAAG,GAAG,EAAE;gBAChC,YAAY,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC,EAAE,UAAU,GAAG,CAAC,CAAC;;;;QAK5D,MAAM,cAAc,GAAG;AACrB,cAAE,CAAA,SAAA,EAAY,cAAc,CAAC,cAAc,EAAE,CAAA,cAAA,EAAiB,MAAM,CAAC,MAAM,CAAC,cAAc,EAAE,CAAA,QAAA,EAAW,SAAS,CAAY,UAAA,CAAA;AAC1H,gBAAA,CAAA,cAAA,EAAiB,SAAS,CAAC,cAAc,EAAE,CAAc,YAAA;AAC3D,cAAE,CAAA,oBAAA,EAAuB,cAAc,CAAC,cAAc,EAAE,CAAiB,cAAA,EAAA,cAAc,CAAC,cAAc,EAAE,CAAA,SAAA,CAAW;QAErH,MAAM;YACJ,YAAY;gBACZ,CAA0B,uBAAA,EAAA,cAAc,4DAA4D;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;;;;"}
|
|
@@ -42,6 +42,16 @@ export declare function extractUsedToolNames(code: string, toolNameMap: Map<stri
|
|
|
42
42
|
* @returns Filtered array of tool definitions
|
|
43
43
|
*/
|
|
44
44
|
export declare function filterToolsByUsage(toolDefs: t.LCTool[], code: string, debug?: boolean): t.LCTool[];
|
|
45
|
+
/**
|
|
46
|
+
* Fetches files from a previous session to make them available for the current execution.
|
|
47
|
+
* Files are returned as CodeEnvFile references to be included in the request.
|
|
48
|
+
* @param baseUrl - The base URL for the Code API
|
|
49
|
+
* @param apiKey - The API key for authentication
|
|
50
|
+
* @param sessionId - The session ID to fetch files from
|
|
51
|
+
* @param proxy - Optional HTTP proxy URL
|
|
52
|
+
* @returns Array of CodeEnvFile references, or empty array if fetch fails
|
|
53
|
+
*/
|
|
54
|
+
export declare function fetchSessionFiles(baseUrl: string, apiKey: string, sessionId: string, proxy?: string): Promise<t.CodeEnvFile[]>;
|
|
45
55
|
/**
|
|
46
56
|
* Makes an HTTP request to the Code API.
|
|
47
57
|
* @param endpoint - The API endpoint URL
|
|
@@ -51,9 +61,18 @@ export declare function filterToolsByUsage(toolDefs: t.LCTool[], code: string, d
|
|
|
51
61
|
* @returns The parsed API response
|
|
52
62
|
*/
|
|
53
63
|
export declare function makeRequest(endpoint: string, apiKey: string, body: Record<string, unknown>, proxy?: string): Promise<t.ProgrammaticExecutionResponse>;
|
|
64
|
+
/**
|
|
65
|
+
* Unwraps tool responses that may be formatted as tuples or content blocks.
|
|
66
|
+
* MCP tools return [content, artifacts], we need to extract the raw data.
|
|
67
|
+
* @param result - The raw result from tool.invoke()
|
|
68
|
+
* @param isMCPTool - Whether this is an MCP tool (has mcp property)
|
|
69
|
+
* @returns Unwrapped raw data (string, object, or parsed JSON)
|
|
70
|
+
*/
|
|
71
|
+
export declare function unwrapToolResponse(result: unknown, isMCPTool: boolean): unknown;
|
|
54
72
|
/**
|
|
55
73
|
* Executes tools in parallel when requested by the API.
|
|
56
74
|
* Uses Promise.all for parallel execution, catching individual errors.
|
|
75
|
+
* Unwraps formatted responses (e.g., MCP tool tuples) to raw data.
|
|
57
76
|
* @param toolCalls - Array of tool calls from the API
|
|
58
77
|
* @param toolMap - Map of tool names to executable tools
|
|
59
78
|
* @returns Array of tool results
|
|
@@ -11,7 +11,9 @@ export type CustomToolCall = {
|
|
|
11
11
|
type?: 'tool_call';
|
|
12
12
|
output?: string;
|
|
13
13
|
};
|
|
14
|
-
export type GenericTool = StructuredToolInterface | RunnableToolLike
|
|
14
|
+
export type GenericTool = (StructuredToolInterface | RunnableToolLike) & {
|
|
15
|
+
mcp?: boolean;
|
|
16
|
+
};
|
|
15
17
|
export type ToolMap = Map<string, GenericTool>;
|
|
16
18
|
export type ToolRefs = {
|
|
17
19
|
tools: GenericTool[];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "illuma-agents",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.18",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"image": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/image.ts --provider 'google' --name 'Jo' --location 'New York, NY'",
|
|
42
42
|
"code_exec_files": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_files.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
43
43
|
"code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'google' --name 'Jo' --location 'New York, NY'",
|
|
44
|
-
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider '
|
|
44
|
+
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'openrouter' --name 'Jo' --location 'New York, NY'",
|
|
45
45
|
"caching": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/caching.ts --name 'Jo' --location 'New York, NY'",
|
|
46
46
|
"thinking": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/thinking.ts --name 'Jo' --location 'New York, NY'",
|
|
47
47
|
"memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
@@ -166,13 +166,13 @@ export class AgentContext {
|
|
|
166
166
|
artifacts: number;
|
|
167
167
|
memory: number;
|
|
168
168
|
} = {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
169
|
+
branding: 0,
|
|
170
|
+
toolRouting: 0,
|
|
171
|
+
agentInstructions: 0,
|
|
172
|
+
mcpInstructions: 0,
|
|
173
|
+
artifacts: 0,
|
|
174
|
+
memory: 0,
|
|
175
|
+
};
|
|
176
176
|
|
|
177
177
|
constructor({
|
|
178
178
|
agentId,
|
|
@@ -403,10 +403,12 @@ export class AgentContext {
|
|
|
403
403
|
| t.BedrockAnthropicInput
|
|
404
404
|
| undefined;
|
|
405
405
|
const modelId = bedrockOptions?.model?.toLowerCase() ?? '';
|
|
406
|
-
const supportsCaching =
|
|
406
|
+
const supportsCaching =
|
|
407
|
+
modelId.includes('claude') ||
|
|
408
|
+
modelId.includes('anthropic') ||
|
|
409
|
+
modelId.includes('nova');
|
|
407
410
|
|
|
408
411
|
if (bedrockOptions?.promptCache === true && supportsCaching) {
|
|
409
|
-
|
|
410
412
|
finalInstructions = {
|
|
411
413
|
content: [
|
|
412
414
|
{
|
|
@@ -485,7 +487,7 @@ export class AgentContext {
|
|
|
485
487
|
): Promise<void> {
|
|
486
488
|
let toolTokens = 0;
|
|
487
489
|
this.toolsDetail = []; // Reset per-tool breakdown
|
|
488
|
-
|
|
490
|
+
|
|
489
491
|
if (this.tools && this.tools.length > 0) {
|
|
490
492
|
for (const tool of this.tools) {
|
|
491
493
|
const genericTool = tool as Record<string, unknown>;
|
|
@@ -507,7 +509,7 @@ export class AgentContext {
|
|
|
507
509
|
const tokens = tokenCounter(
|
|
508
510
|
new SystemMessage(JSON.stringify(jsonSchema))
|
|
509
511
|
);
|
|
510
|
-
|
|
512
|
+
|
|
511
513
|
// Track per-tool breakdown
|
|
512
514
|
this.toolsDetail.push({ name: toolName, tokens });
|
|
513
515
|
toolTokens += tokens;
|
|
@@ -517,7 +519,7 @@ export class AgentContext {
|
|
|
517
519
|
|
|
518
520
|
// Store total tool tokens for breakdown reporting
|
|
519
521
|
this.toolTokensTotal = toolTokens;
|
|
520
|
-
|
|
522
|
+
|
|
521
523
|
// Add tool tokens to existing instruction tokens (which may already include system message tokens)
|
|
522
524
|
this.instructionTokens += toolTokens;
|
|
523
525
|
}
|
|
@@ -535,12 +537,18 @@ export class AgentContext {
|
|
|
535
537
|
artifacts?: number;
|
|
536
538
|
memory?: number;
|
|
537
539
|
}): void {
|
|
538
|
-
if (breakdown.branding !== undefined)
|
|
539
|
-
|
|
540
|
-
if (breakdown.
|
|
541
|
-
|
|
542
|
-
if (breakdown.
|
|
543
|
-
|
|
540
|
+
if (breakdown.branding !== undefined)
|
|
541
|
+
this.promptBreakdown.branding = breakdown.branding;
|
|
542
|
+
if (breakdown.toolRouting !== undefined)
|
|
543
|
+
this.promptBreakdown.toolRouting = breakdown.toolRouting;
|
|
544
|
+
if (breakdown.agentInstructions !== undefined)
|
|
545
|
+
this.promptBreakdown.agentInstructions = breakdown.agentInstructions;
|
|
546
|
+
if (breakdown.mcpInstructions !== undefined)
|
|
547
|
+
this.promptBreakdown.mcpInstructions = breakdown.mcpInstructions;
|
|
548
|
+
if (breakdown.artifacts !== undefined)
|
|
549
|
+
this.promptBreakdown.artifacts = breakdown.artifacts;
|
|
550
|
+
if (breakdown.memory !== undefined)
|
|
551
|
+
this.promptBreakdown.memory = breakdown.memory;
|
|
544
552
|
}
|
|
545
553
|
|
|
546
554
|
/**
|
|
@@ -565,9 +573,9 @@ export class AgentContext {
|
|
|
565
573
|
artifacts: number;
|
|
566
574
|
memory: number;
|
|
567
575
|
};
|
|
568
|
-
|
|
576
|
+
} {
|
|
569
577
|
// Calculate sum of prompt components
|
|
570
|
-
const promptsTotal =
|
|
578
|
+
const promptsTotal =
|
|
571
579
|
this.promptBreakdown.branding +
|
|
572
580
|
this.promptBreakdown.toolRouting +
|
|
573
581
|
this.promptBreakdown.agentInstructions +
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -57,7 +57,10 @@ import {
|
|
|
57
57
|
joinKeys,
|
|
58
58
|
sleep,
|
|
59
59
|
} from '@/utils';
|
|
60
|
-
import {
|
|
60
|
+
import {
|
|
61
|
+
buildContextAnalytics,
|
|
62
|
+
type ContextAnalytics,
|
|
63
|
+
} from '@/utils/contextAnalytics';
|
|
61
64
|
import { getChatModelClass, manualToolStreamProviders } from '@/llm/providers';
|
|
62
65
|
import { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';
|
|
63
66
|
import { ChatOpenAI, AzureChatOpenAI } from '@/llm/openai';
|
|
@@ -214,7 +217,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
214
217
|
/**
|
|
215
218
|
* Estimates a human-friendly description of the conversation timeframe based on message count.
|
|
216
219
|
* Uses rough heuristics to provide context about how much history is available.
|
|
217
|
-
*
|
|
220
|
+
*
|
|
218
221
|
* @param messageCount - Number of messages in the remaining context
|
|
219
222
|
* @returns A friendly description like "the last few minutes", "the past hour", etc.
|
|
220
223
|
*/
|
|
@@ -224,7 +227,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
224
227
|
// - Normal chat: ~10-15 messages per hour
|
|
225
228
|
// - Slow/thoughtful chat: ~5-8 messages per hour
|
|
226
229
|
// We use a middle estimate of ~12 messages per hour
|
|
227
|
-
|
|
230
|
+
|
|
228
231
|
if (messageCount <= 5) {
|
|
229
232
|
return 'just the last few exchanges';
|
|
230
233
|
} else if (messageCount <= 15) {
|
|
@@ -712,7 +715,8 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
712
715
|
content: `[SESSION_CONTEXT]\n${agentContext.dynamicContext}`,
|
|
713
716
|
});
|
|
714
717
|
const ackMessage = new AIMessageChunk({
|
|
715
|
-
content:
|
|
718
|
+
content:
|
|
719
|
+
'Understood. I have noted the session context including the current date/time (CST) and will apply it appropriately.',
|
|
716
720
|
});
|
|
717
721
|
messages = [dynamicContextMessage, ackMessage, ...messages];
|
|
718
722
|
}
|
|
@@ -745,7 +749,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
745
749
|
this.config = config;
|
|
746
750
|
|
|
747
751
|
let messagesToUse = messages;
|
|
748
|
-
|
|
752
|
+
|
|
749
753
|
if (
|
|
750
754
|
!agentContext.pruneMessages &&
|
|
751
755
|
agentContext.tokenCounter &&
|
|
@@ -829,7 +833,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
829
833
|
* Handle edge case: when switching from a non-thinking agent to a thinking-enabled agent,
|
|
830
834
|
* convert AI messages with tool calls to HumanMessages to avoid thinking block requirements.
|
|
831
835
|
* This is required by Anthropic/Bedrock when thinking is enabled.
|
|
832
|
-
*
|
|
836
|
+
*
|
|
833
837
|
* IMPORTANT: This MUST happen BEFORE cache control is applied.
|
|
834
838
|
* If we add cachePoint to an AI message first, then convert that AI message to a HumanMessage,
|
|
835
839
|
* the cachePoint is lost. By converting first, we ensure cache control is applied to the
|
|
@@ -873,7 +877,10 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
873
877
|
// Both Claude and Nova models support cachePoint in system and messages
|
|
874
878
|
// (Llama, Titan, and other models do NOT support cachePoint)
|
|
875
879
|
const modelId = bedrockOptions?.model?.toLowerCase() ?? '';
|
|
876
|
-
const supportsCaching =
|
|
880
|
+
const supportsCaching =
|
|
881
|
+
modelId.includes('claude') ||
|
|
882
|
+
modelId.includes('anthropic') ||
|
|
883
|
+
modelId.includes('nova');
|
|
877
884
|
if (bedrockOptions?.promptCache === true && supportsCaching) {
|
|
878
885
|
finalMessages = addBedrockCacheControl<BaseMessage>(finalMessages);
|
|
879
886
|
}
|
|
@@ -909,10 +916,17 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
909
916
|
}
|
|
910
917
|
|
|
911
918
|
// Get model info for analytics
|
|
912
|
-
const bedrockOpts = agentContext.clientOptions as
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
919
|
+
const bedrockOpts = agentContext.clientOptions as
|
|
920
|
+
| t.BedrockAnthropicClientOptions
|
|
921
|
+
| undefined;
|
|
922
|
+
const modelId =
|
|
923
|
+
bedrockOpts?.model ||
|
|
924
|
+
(agentContext.clientOptions as t.AnthropicClientOptions | undefined)
|
|
925
|
+
?.modelName;
|
|
926
|
+
const thinkingConfig =
|
|
927
|
+
bedrockOpts?.additionalModelRequestFields?.['thinking'] ||
|
|
928
|
+
(agentContext.clientOptions as t.AnthropicClientOptions | undefined)
|
|
929
|
+
?.thinking;
|
|
916
930
|
|
|
917
931
|
// Build and emit context analytics for traces
|
|
918
932
|
const contextAnalytics = buildContextAnalytics(finalMessages, {
|
|
@@ -921,10 +935,10 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
921
935
|
instructionTokens: agentContext.instructionTokens,
|
|
922
936
|
indexTokenCountMap: agentContext.indexTokenCountMap,
|
|
923
937
|
});
|
|
924
|
-
|
|
938
|
+
|
|
925
939
|
// Store for retrieval via getContextAnalytics() after run completes
|
|
926
940
|
this.lastContextAnalytics = contextAnalytics;
|
|
927
|
-
|
|
941
|
+
|
|
928
942
|
await safeDispatchCustomEvent(
|
|
929
943
|
GraphEvents.ON_CONTEXT_ANALYTICS,
|
|
930
944
|
{
|
|
@@ -949,8 +963,9 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
949
963
|
);
|
|
950
964
|
} catch (primaryError) {
|
|
951
965
|
// Check if this is a "input too long" error from Bedrock/Anthropic
|
|
952
|
-
const errorMessage =
|
|
953
|
-
|
|
966
|
+
const errorMessage =
|
|
967
|
+
(primaryError as Error).message.toLowerCase() ?? '';
|
|
968
|
+
const isInputTooLongError =
|
|
954
969
|
errorMessage.includes('too long') ||
|
|
955
970
|
errorMessage.includes('input is too long') ||
|
|
956
971
|
errorMessage.includes('context length') ||
|
|
@@ -960,41 +975,50 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
960
975
|
|
|
961
976
|
// Log when we detect the error
|
|
962
977
|
if (isInputTooLongError) {
|
|
963
|
-
console.warn(
|
|
978
|
+
console.warn(
|
|
979
|
+
'[Graph] Detected input too long error:',
|
|
980
|
+
errorMessage.substring(0, 200)
|
|
981
|
+
);
|
|
964
982
|
console.warn('[Graph] Checking emergency pruning conditions:', {
|
|
965
983
|
hasPruneMessages: !!agentContext.pruneMessages,
|
|
966
984
|
hasTokenCounter: !!agentContext.tokenCounter,
|
|
967
985
|
maxContextTokens: agentContext.maxContextTokens,
|
|
968
|
-
indexTokenMapKeys: Object.keys(agentContext.indexTokenCountMap)
|
|
986
|
+
indexTokenMapKeys: Object.keys(agentContext.indexTokenCountMap)
|
|
987
|
+
.length,
|
|
969
988
|
});
|
|
970
989
|
}
|
|
971
990
|
|
|
972
991
|
// If input too long and we have pruning capability OR tokenCounter, retry with progressively more aggressive pruning
|
|
973
992
|
// Note: We can create emergency pruneMessages dynamically if we have tokenCounter and maxContextTokens
|
|
974
|
-
const canPrune =
|
|
993
|
+
const canPrune =
|
|
994
|
+
agentContext.tokenCounter && agentContext.maxContextTokens;
|
|
975
995
|
if (isInputTooLongError && canPrune) {
|
|
976
996
|
// Progressive reduction: 50% -> 25% -> 10% of original context
|
|
977
997
|
const reductionLevels = [0.5, 0.25, 0.1];
|
|
978
|
-
|
|
998
|
+
|
|
979
999
|
for (const reductionFactor of reductionLevels) {
|
|
980
1000
|
if (result) break; // Exit if we got a result
|
|
981
|
-
|
|
982
|
-
const reducedMaxTokens = Math.floor(
|
|
1001
|
+
|
|
1002
|
+
const reducedMaxTokens = Math.floor(
|
|
1003
|
+
agentContext.maxContextTokens! * reductionFactor
|
|
1004
|
+
);
|
|
983
1005
|
console.warn(
|
|
984
1006
|
`[Graph] Input too long. Retrying with ${reductionFactor * 100}% context (${reducedMaxTokens} tokens)...`
|
|
985
1007
|
);
|
|
986
|
-
|
|
1008
|
+
|
|
987
1009
|
// Build fresh indexTokenCountMap if missing/incomplete
|
|
988
1010
|
// This is needed when messages were dynamically added without updating the token map
|
|
989
1011
|
let tokenMapForPruning = agentContext.indexTokenCountMap;
|
|
990
1012
|
if (Object.keys(tokenMapForPruning).length < messages.length) {
|
|
991
|
-
console.warn(
|
|
1013
|
+
console.warn(
|
|
1014
|
+
'[Graph] Building fresh token count map for emergency pruning...'
|
|
1015
|
+
);
|
|
992
1016
|
tokenMapForPruning = {};
|
|
993
1017
|
for (let i = 0; i < messages.length; i++) {
|
|
994
1018
|
tokenMapForPruning[i] = agentContext.tokenCounter!(messages[i]);
|
|
995
1019
|
}
|
|
996
1020
|
}
|
|
997
|
-
|
|
1021
|
+
|
|
998
1022
|
const emergencyPrune = createPruneMessages({
|
|
999
1023
|
startIndex: this.startIndex,
|
|
1000
1024
|
provider: agentContext.provider,
|
|
@@ -1011,15 +1035,18 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
1011
1035
|
|
|
1012
1036
|
// Skip if we can't fit any messages
|
|
1013
1037
|
if (reducedMessages.length === 0) {
|
|
1014
|
-
console.warn(
|
|
1038
|
+
console.warn(
|
|
1039
|
+
`[Graph] Cannot fit any messages at ${reductionFactor * 100}% reduction, trying next level...`
|
|
1040
|
+
);
|
|
1015
1041
|
continue;
|
|
1016
1042
|
}
|
|
1017
1043
|
|
|
1018
1044
|
// Calculate how many messages were pruned and estimate context timeframe
|
|
1019
1045
|
const prunedCount = finalMessages.length - reducedMessages.length;
|
|
1020
1046
|
const remainingCount = reducedMessages.length;
|
|
1021
|
-
const estimatedContextDescription =
|
|
1022
|
-
|
|
1047
|
+
const estimatedContextDescription =
|
|
1048
|
+
this.getContextTimeframeDescription(remainingCount);
|
|
1049
|
+
|
|
1023
1050
|
// Inject a personalized context message to inform the agent about pruning
|
|
1024
1051
|
const pruneNoticeMessage = new HumanMessage({
|
|
1025
1052
|
content: `[CONTEXT NOTICE]
|
|
@@ -1027,11 +1054,11 @@ Our conversation has grown quite long, so I've focused on ${estimatedContextDesc
|
|
|
1027
1054
|
|
|
1028
1055
|
If I seem to be missing something we discussed earlier, just give me a quick reminder and I'll pick right back up! I'm still fully engaged and ready to help with whatever you need.`,
|
|
1029
1056
|
});
|
|
1030
|
-
|
|
1057
|
+
|
|
1031
1058
|
// Insert the notice after the system message (if any) but before conversation
|
|
1032
1059
|
const hasSystemMessage = reducedMessages[0]?.getType() === 'system';
|
|
1033
1060
|
const insertIndex = hasSystemMessage ? 1 : 0;
|
|
1034
|
-
|
|
1061
|
+
|
|
1035
1062
|
// Create new array with the pruning notice
|
|
1036
1063
|
const messagesWithNotice = [
|
|
1037
1064
|
...reducedMessages.slice(0, insertIndex),
|
|
@@ -1059,9 +1086,13 @@ If I seem to be missing something we discussed earlier, just give me a quick rem
|
|
|
1059
1086
|
| t.BedrockAnthropicClientOptions
|
|
1060
1087
|
| undefined;
|
|
1061
1088
|
const modelId = bedrockOptions?.model?.toLowerCase() ?? '';
|
|
1062
|
-
const supportsCaching =
|
|
1089
|
+
const supportsCaching =
|
|
1090
|
+
modelId.includes('claude') ||
|
|
1091
|
+
modelId.includes('anthropic') ||
|
|
1092
|
+
modelId.includes('nova');
|
|
1063
1093
|
if (bedrockOptions?.promptCache === true && supportsCaching) {
|
|
1064
|
-
retryMessages =
|
|
1094
|
+
retryMessages =
|
|
1095
|
+
addBedrockCacheControl<BaseMessage>(retryMessages);
|
|
1065
1096
|
}
|
|
1066
1097
|
}
|
|
1067
1098
|
|
|
@@ -1076,18 +1107,26 @@ If I seem to be missing something we discussed earlier, just give me a quick rem
|
|
|
1076
1107
|
config
|
|
1077
1108
|
);
|
|
1078
1109
|
// Success with reduced context
|
|
1079
|
-
console.info(
|
|
1110
|
+
console.info(
|
|
1111
|
+
`[Graph] ✅ Retry successful at ${reductionFactor * 100}% with ${reducedMessages.length} messages (reduced from ${finalMessages.length})`
|
|
1112
|
+
);
|
|
1080
1113
|
} catch (retryError) {
|
|
1081
|
-
const retryErrorMsg =
|
|
1082
|
-
|
|
1114
|
+
const retryErrorMsg =
|
|
1115
|
+
(retryError as Error).message.toLowerCase() ?? '';
|
|
1116
|
+
const stillTooLong =
|
|
1083
1117
|
retryErrorMsg.includes('too long') ||
|
|
1084
1118
|
retryErrorMsg.includes('context length') ||
|
|
1085
1119
|
retryErrorMsg.includes('validationexception');
|
|
1086
|
-
|
|
1120
|
+
|
|
1087
1121
|
if (stillTooLong && reductionFactor > 0.1) {
|
|
1088
|
-
console.warn(
|
|
1122
|
+
console.warn(
|
|
1123
|
+
`[Graph] Still too long at ${reductionFactor * 100}%, trying more aggressive pruning...`
|
|
1124
|
+
);
|
|
1089
1125
|
} else {
|
|
1090
|
-
console.error(
|
|
1126
|
+
console.error(
|
|
1127
|
+
`[Graph] Retry at ${reductionFactor * 100}% failed:`,
|
|
1128
|
+
(retryError as Error).message
|
|
1129
|
+
);
|
|
1091
1130
|
}
|
|
1092
1131
|
}
|
|
1093
1132
|
}
|