illuma-agents 1.0.17 → 1.0.19

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.
Files changed (104) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +3 -1
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +18 -9
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/llm/bedrock/index.cjs +5 -3
  6. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  7. package/dist/cjs/llm/openrouter/index.cjs +10 -1
  8. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  9. package/dist/cjs/llm/vertexai/index.cjs +7 -8
  10. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  11. package/dist/cjs/main.cjs +17 -0
  12. package/dist/cjs/main.cjs.map +1 -1
  13. package/dist/cjs/messages/cache.cjs +11 -6
  14. package/dist/cjs/messages/cache.cjs.map +1 -1
  15. package/dist/cjs/messages/core.cjs +2 -2
  16. package/dist/cjs/messages/core.cjs.map +1 -1
  17. package/dist/cjs/messages/format.cjs +2 -1
  18. package/dist/cjs/messages/format.cjs.map +1 -1
  19. package/dist/cjs/messages/tools.cjs +2 -2
  20. package/dist/cjs/messages/tools.cjs.map +1 -1
  21. package/dist/cjs/stream.cjs +29 -16
  22. package/dist/cjs/stream.cjs.map +1 -1
  23. package/dist/cjs/tools/BrowserTools.cjs +473 -0
  24. package/dist/cjs/tools/BrowserTools.cjs.map +1 -0
  25. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +209 -47
  26. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  27. package/dist/cjs/tools/ToolNode.cjs +1 -1
  28. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  29. package/dist/cjs/tools/search/search.cjs.map +1 -1
  30. package/dist/cjs/tools/search/tool.cjs +3 -1
  31. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  32. package/dist/cjs/utils/contextAnalytics.cjs +7 -5
  33. package/dist/cjs/utils/contextAnalytics.cjs.map +1 -1
  34. package/dist/cjs/utils/run.cjs.map +1 -1
  35. package/dist/cjs/utils/toonFormat.cjs +42 -12
  36. package/dist/cjs/utils/toonFormat.cjs.map +1 -1
  37. package/dist/esm/agents/AgentContext.mjs +3 -1
  38. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  39. package/dist/esm/graphs/Graph.mjs +18 -9
  40. package/dist/esm/graphs/Graph.mjs.map +1 -1
  41. package/dist/esm/llm/bedrock/index.mjs +5 -3
  42. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  43. package/dist/esm/llm/openrouter/index.mjs +10 -1
  44. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  45. package/dist/esm/llm/vertexai/index.mjs +7 -8
  46. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  47. package/dist/esm/main.mjs +2 -1
  48. package/dist/esm/main.mjs.map +1 -1
  49. package/dist/esm/messages/cache.mjs +11 -6
  50. package/dist/esm/messages/cache.mjs.map +1 -1
  51. package/dist/esm/messages/core.mjs +2 -2
  52. package/dist/esm/messages/core.mjs.map +1 -1
  53. package/dist/esm/messages/format.mjs +2 -1
  54. package/dist/esm/messages/format.mjs.map +1 -1
  55. package/dist/esm/messages/tools.mjs +2 -2
  56. package/dist/esm/messages/tools.mjs.map +1 -1
  57. package/dist/esm/stream.mjs +29 -16
  58. package/dist/esm/stream.mjs.map +1 -1
  59. package/dist/esm/tools/BrowserTools.mjs +458 -0
  60. package/dist/esm/tools/BrowserTools.mjs.map +1 -0
  61. package/dist/esm/tools/ProgrammaticToolCalling.mjs +208 -48
  62. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  63. package/dist/esm/tools/ToolNode.mjs +1 -1
  64. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  65. package/dist/esm/tools/search/search.mjs.map +1 -1
  66. package/dist/esm/tools/search/tool.mjs +3 -1
  67. package/dist/esm/tools/search/tool.mjs.map +1 -1
  68. package/dist/esm/utils/contextAnalytics.mjs +7 -5
  69. package/dist/esm/utils/contextAnalytics.mjs.map +1 -1
  70. package/dist/esm/utils/run.mjs.map +1 -1
  71. package/dist/esm/utils/toonFormat.mjs +42 -12
  72. package/dist/esm/utils/toonFormat.mjs.map +1 -1
  73. package/dist/types/index.d.ts +1 -0
  74. package/dist/types/tools/BrowserTools.d.ts +244 -0
  75. package/dist/types/tools/ProgrammaticToolCalling.d.ts +19 -0
  76. package/dist/types/types/tools.d.ts +3 -1
  77. package/package.json +6 -2
  78. package/src/agents/AgentContext.ts +28 -20
  79. package/src/graphs/Graph.ts +76 -37
  80. package/src/index.ts +1 -0
  81. package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +495 -473
  82. package/src/llm/bedrock/index.ts +47 -35
  83. package/src/llm/openrouter/index.ts +11 -1
  84. package/src/llm/vertexai/index.ts +9 -10
  85. package/src/messages/cache.ts +104 -55
  86. package/src/messages/core.ts +5 -3
  87. package/src/messages/format.ts +6 -2
  88. package/src/messages/tools.ts +2 -2
  89. package/src/scripts/simple.ts +1 -1
  90. package/src/specs/emergency-prune.test.ts +407 -355
  91. package/src/stream.ts +28 -20
  92. package/src/tools/BrowserTools.test.ts +528 -0
  93. package/src/tools/BrowserTools.ts +582 -0
  94. package/src/tools/ProgrammaticToolCalling.ts +246 -52
  95. package/src/tools/ToolNode.ts +4 -4
  96. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +155 -0
  97. package/src/tools/search/jina-reranker.test.ts +32 -28
  98. package/src/tools/search/search.ts +3 -1
  99. package/src/tools/search/tool.ts +16 -7
  100. package/src/types/tools.ts +3 -1
  101. package/src/utils/contextAnalytics.ts +103 -95
  102. package/src/utils/llmConfig.ts +8 -1
  103. package/src/utils/run.ts +5 -4
  104. 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,GAA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,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;AAErH,QAAA,MAAM,GAAG,YAAY,GAAG,CAA0B,uBAAA,EAAA,cAAc,4DAA4D;QAC5H,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;;;;"}
1
+ {"version":3,"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,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,G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I;;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;;;;"}
@@ -8,6 +8,7 @@ export * from './tools/Calculator';
8
8
  export * from './tools/CodeExecutor';
9
9
  export * from './tools/ProgrammaticToolCalling';
10
10
  export * from './tools/ToolSearchRegex';
11
+ export * from './tools/BrowserTools';
11
12
  export * from './tools/handlers';
12
13
  export * from './tools/search';
13
14
  export * from './common';
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Browser Automation Tools for Ranger Browser Extension
3
+ *
4
+ * These tools allow the LLM to interact with the browser through the
5
+ * ranger-browser extension. They generate structured actions that are
6
+ * sent to the extension via SSE streaming for execution.
7
+ *
8
+ * The extension handles:
9
+ * - DOM extraction with element indexing
10
+ * - Click, type, hover, scroll actions
11
+ * - Navigation and page context
12
+ * - Visual element highlighting
13
+ */
14
+ import { z } from 'zod';
15
+ import { DynamicStructuredTool } from '@langchain/core/tools';
16
+ /**
17
+ * Enhanced click schema that supports both index-based and coordinate-based clicking
18
+ */
19
+ declare const BrowserClickSchema: z.ZodObject<{
20
+ index: z.ZodOptional<z.ZodNumber>;
21
+ coordinates: z.ZodOptional<z.ZodObject<{
22
+ x: z.ZodNumber;
23
+ y: z.ZodNumber;
24
+ }, "strip", z.ZodTypeAny, {
25
+ y: number;
26
+ x: number;
27
+ }, {
28
+ y: number;
29
+ x: number;
30
+ }>>;
31
+ visualDescription: z.ZodOptional<z.ZodString>;
32
+ reason: z.ZodOptional<z.ZodString>;
33
+ }, "strip", z.ZodTypeAny, {
34
+ index?: number | undefined;
35
+ coordinates?: {
36
+ y: number;
37
+ x: number;
38
+ } | undefined;
39
+ visualDescription?: string | undefined;
40
+ reason?: string | undefined;
41
+ }, {
42
+ index?: number | undefined;
43
+ coordinates?: {
44
+ y: number;
45
+ x: number;
46
+ } | undefined;
47
+ visualDescription?: string | undefined;
48
+ reason?: string | undefined;
49
+ }>;
50
+ declare const BrowserTypeSchema: z.ZodObject<{
51
+ index: z.ZodNumber;
52
+ text: z.ZodString;
53
+ clear: z.ZodOptional<z.ZodBoolean>;
54
+ pressEnter: z.ZodOptional<z.ZodBoolean>;
55
+ }, "strip", z.ZodTypeAny, {
56
+ text: string;
57
+ index: number;
58
+ clear?: boolean | undefined;
59
+ pressEnter?: boolean | undefined;
60
+ }, {
61
+ text: string;
62
+ index: number;
63
+ clear?: boolean | undefined;
64
+ pressEnter?: boolean | undefined;
65
+ }>;
66
+ declare const BrowserNavigateSchema: z.ZodObject<{
67
+ url: z.ZodString;
68
+ reason: z.ZodOptional<z.ZodString>;
69
+ }, "strip", z.ZodTypeAny, {
70
+ url: string;
71
+ reason?: string | undefined;
72
+ }, {
73
+ url: string;
74
+ reason?: string | undefined;
75
+ }>;
76
+ declare const BrowserScrollSchema: z.ZodObject<{
77
+ direction: z.ZodEnum<["up", "down", "left", "right"]>;
78
+ amount: z.ZodOptional<z.ZodNumber>;
79
+ }, "strip", z.ZodTypeAny, {
80
+ direction: "up" | "down" | "left" | "right";
81
+ amount?: number | undefined;
82
+ }, {
83
+ direction: "up" | "down" | "left" | "right";
84
+ amount?: number | undefined;
85
+ }>;
86
+ declare const BrowserExtractSchema: z.ZodObject<{
87
+ query: z.ZodOptional<z.ZodString>;
88
+ selector: z.ZodOptional<z.ZodString>;
89
+ }, "strip", z.ZodTypeAny, {
90
+ query?: string | undefined;
91
+ selector?: string | undefined;
92
+ }, {
93
+ query?: string | undefined;
94
+ selector?: string | undefined;
95
+ }>;
96
+ declare const BrowserHoverSchema: z.ZodObject<{
97
+ index: z.ZodNumber;
98
+ }, "strip", z.ZodTypeAny, {
99
+ index: number;
100
+ }, {
101
+ index: number;
102
+ }>;
103
+ declare const BrowserWaitSchema: z.ZodObject<{
104
+ duration: z.ZodOptional<z.ZodNumber>;
105
+ reason: z.ZodOptional<z.ZodString>;
106
+ }, "strip", z.ZodTypeAny, {
107
+ duration?: number | undefined;
108
+ reason?: string | undefined;
109
+ }, {
110
+ duration?: number | undefined;
111
+ reason?: string | undefined;
112
+ }>;
113
+ declare const BrowserGoBackSchema: z.ZodObject<{
114
+ reason: z.ZodOptional<z.ZodString>;
115
+ }, "strip", z.ZodTypeAny, {
116
+ reason?: string | undefined;
117
+ }, {
118
+ reason?: string | undefined;
119
+ }>;
120
+ declare const BrowserScreenshotSchema: z.ZodObject<{
121
+ fullPage: z.ZodOptional<z.ZodBoolean>;
122
+ reason: z.ZodOptional<z.ZodString>;
123
+ }, "strip", z.ZodTypeAny, {
124
+ reason?: string | undefined;
125
+ fullPage?: boolean | undefined;
126
+ }, {
127
+ reason?: string | undefined;
128
+ fullPage?: boolean | undefined;
129
+ }>;
130
+ /**
131
+ * Browser click tool - clicks an element by index or coordinates
132
+ * Supports both semantic (index-based) and vision (coordinate-based) clicking
133
+ */
134
+ export declare function createBrowserClickTool(): DynamicStructuredTool<typeof BrowserClickSchema>;
135
+ /**
136
+ * Browser type tool - types text into an input field
137
+ */
138
+ export declare function createBrowserTypeTool(): DynamicStructuredTool<typeof BrowserTypeSchema>;
139
+ /**
140
+ * Browser navigate tool - navigates to a URL
141
+ */
142
+ export declare function createBrowserNavigateTool(): DynamicStructuredTool<typeof BrowserNavigateSchema>;
143
+ /**
144
+ * Browser scroll tool - scrolls the page
145
+ */
146
+ export declare function createBrowserScrollTool(): DynamicStructuredTool<typeof BrowserScrollSchema>;
147
+ /**
148
+ * Browser extract tool - extracts content from the page
149
+ */
150
+ export declare function createBrowserExtractTool(): DynamicStructuredTool<typeof BrowserExtractSchema>;
151
+ /**
152
+ * Browser hover tool - hovers over an element
153
+ */
154
+ export declare function createBrowserHoverTool(): DynamicStructuredTool<typeof BrowserHoverSchema>;
155
+ /**
156
+ * Browser wait tool - waits for a specified duration
157
+ */
158
+ export declare function createBrowserWaitTool(): DynamicStructuredTool<typeof BrowserWaitSchema>;
159
+ /**
160
+ * Browser go back tool - navigates back in history
161
+ */
162
+ export declare function createBrowserGoBackTool(): DynamicStructuredTool<typeof BrowserGoBackSchema>;
163
+ /**
164
+ * Browser screenshot tool - captures a screenshot
165
+ */
166
+ export declare function createBrowserScreenshotTool(): DynamicStructuredTool<typeof BrowserScreenshotSchema>;
167
+ export type BrowserToolsConfig = {
168
+ /** Enable click tool */
169
+ enableClick?: boolean;
170
+ /** Enable type tool */
171
+ enableType?: boolean;
172
+ /** Enable navigate tool */
173
+ enableNavigate?: boolean;
174
+ /** Enable scroll tool */
175
+ enableScroll?: boolean;
176
+ /** Enable extract tool */
177
+ enableExtract?: boolean;
178
+ /** Enable hover tool */
179
+ enableHover?: boolean;
180
+ /** Enable wait tool */
181
+ enableWait?: boolean;
182
+ /** Enable back tool */
183
+ enableBack?: boolean;
184
+ /** Enable screenshot tool */
185
+ enableScreenshot?: boolean;
186
+ };
187
+ /**
188
+ * Create all browser automation tools
189
+ *
190
+ * IMPORTANT: These tools should ONLY be registered when:
191
+ * 1. The request comes from a browser extension that can execute them
192
+ * 2. The client has indicated browser capability (e.g., via header or parameter)
193
+ *
194
+ * DO NOT register these for normal web UI users - they cannot execute browser actions.
195
+ *
196
+ * Detection in Ranger API:
197
+ * - Check for `X-Ranger-Browser-Extension: true` header
198
+ * - Or check for `browserCapable: true` in request body
199
+ * - Or check user agent for extension identifier
200
+ *
201
+ * @example
202
+ * // In Ranger API endpoint:
203
+ * const hasBrowserExtension = req.headers['x-ranger-browser-extension'] === 'true';
204
+ * const tools = hasBrowserExtension
205
+ * ? [...normalTools, ...createBrowserTools()]
206
+ * : normalTools;
207
+ */
208
+ export declare function createBrowserTools(config?: BrowserToolsConfig): DynamicStructuredTool[];
209
+ /**
210
+ * Browser tool name constants
211
+ * Use these instead of magic strings
212
+ */
213
+ export declare const EBrowserTools: {
214
+ readonly CLICK: "browser_click";
215
+ readonly TYPE: "browser_type";
216
+ readonly NAVIGATE: "browser_navigate";
217
+ readonly SCROLL: "browser_scroll";
218
+ readonly EXTRACT: "browser_extract";
219
+ readonly HOVER: "browser_hover";
220
+ readonly WAIT: "browser_wait";
221
+ readonly BACK: "browser_back";
222
+ readonly SCREENSHOT: "browser_screenshot";
223
+ };
224
+ /**
225
+ * Get browser tool names for filtering/identification
226
+ */
227
+ export declare const BROWSER_TOOL_NAMES: readonly ["browser_click", "browser_type", "browser_navigate", "browser_scroll", "browser_extract", "browser_hover", "browser_wait", "browser_back", "browser_screenshot"];
228
+ export type BrowserToolName = typeof BROWSER_TOOL_NAMES[number];
229
+ /**
230
+ * Check if a tool call is a browser action
231
+ */
232
+ export declare function isBrowserToolCall(toolName: string): toolName is BrowserToolName;
233
+ /**
234
+ * Check if request indicates browser extension capability
235
+ * Use this to conditionally register browser tools
236
+ *
237
+ * @example
238
+ * // In Express middleware or endpoint:
239
+ * if (hasBrowserCapability(req.headers)) {
240
+ * tools.push(...createBrowserTools());
241
+ * }
242
+ */
243
+ export declare function hasBrowserCapability(headers: Record<string, string | string[] | undefined>): boolean;
244
+ export {};
@@ -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.17",
3
+ "version": "1.0.19",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -13,6 +13,10 @@
13
13
  },
14
14
  "type": "module",
15
15
  "description": "Illuma AI Agents Library",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/codevakure/agents"
19
+ },
16
20
  "author": "Illuma Team",
17
21
  "license": "MIT",
18
22
  "packageManager": "npm@10.5.2",
@@ -41,7 +45,7 @@
41
45
  "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
46
  "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
47
  "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 'openAI' --name 'Jo' --location 'New York, NY'",
48
+ "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
49
  "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
50
  "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
51
  "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
- branding: 0,
170
- toolRouting: 0,
171
- agentInstructions: 0,
172
- mcpInstructions: 0,
173
- artifacts: 0,
174
- memory: 0,
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 = modelId.includes('claude') || modelId.includes('anthropic') || modelId.includes('nova');
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) 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;
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 +