@tyvm/knowhow 0.0.47 → 0.0.49
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/benchmarks/results/27b0a06/2025-09-27/xai/xai-grok-code-fast-1.json +2909 -0
- package/benchmarks/results/4057aed/2025-08-14/anthropic/anthropic-claude-sonnet-4-20250514.json +1671 -0
- package/jest.config.js +2 -2
- package/package.json +8 -3
- package/src/agents/base/base.ts +39 -25
- package/src/agents/patcher/patcher.ts +26 -5
- package/src/agents/tools/agentCall.ts +4 -2
- package/src/agents/tools/aiClient.ts +3 -11
- package/src/agents/tools/ast/astAppendNode.ts +90 -0
- package/src/agents/tools/ast/astDeleteNode.ts +88 -0
- package/src/agents/tools/ast/astEditNode.ts +95 -0
- package/src/agents/tools/ast/astGetPathForLine.ts +73 -0
- package/src/agents/tools/ast/astListPaths.ts +66 -0
- package/src/agents/tools/ast/index.ts +7 -0
- package/src/agents/tools/callPlugin.ts +8 -2
- package/src/agents/tools/embeddingSearch.ts +2 -1
- package/src/agents/tools/execCommand.ts +239 -94
- package/src/agents/tools/fileSearch.ts +15 -17
- package/src/agents/tools/index.ts +1 -0
- package/src/agents/tools/language/definitions.ts +10 -2
- package/src/agents/tools/language/index.ts +3 -2
- package/src/agents/tools/lintFile.ts +4 -2
- package/src/agents/tools/list.ts +203 -62
- package/src/agents/tools/patch.ts +48 -14
- package/src/agents/tools/readBlocks.ts +34 -0
- package/src/agents/tools/readFile.ts +23 -0
- package/src/agents/tools/stringReplace.ts +33 -9
- package/src/agents/tools/writeFile.ts +55 -0
- package/src/agents/tools/ycmd/server.ts +14 -4
- package/src/chat/CliChatService.ts +6 -1
- package/src/chat/modules/AgentModule.ts +129 -67
- package/src/chat/modules/AskModule.ts +0 -1
- package/src/chat/modules/SetupModule.ts +4 -4
- package/src/chat/modules/SystemModule.ts +28 -5
- package/src/chat/types.ts +2 -0
- package/src/chat-old.ts +2 -2
- package/src/clients/anthropic.ts +22 -1
- package/src/clients/openai.ts +1 -1
- package/src/clients/xai.ts +15 -5
- package/src/config.ts +17 -5
- package/src/dataset/diffs/generate.ts +2 -2
- package/src/dataset/diffs/jsonl.ts +0 -1
- package/src/embeddings.ts +8 -8
- package/src/index.ts +11 -5
- package/src/plugins/GitPlugin.ts +530 -0
- package/src/plugins/LinterPlugin.ts +89 -0
- package/src/plugins/PluginBase.ts +4 -2
- package/src/plugins/asana.ts +4 -2
- package/src/plugins/downloader/plugin.ts +5 -2
- package/src/plugins/embedding.ts +24 -4
- package/src/plugins/figma.ts +7 -3
- package/src/plugins/github.ts +4 -2
- package/src/plugins/jira.ts +4 -2
- package/src/plugins/language.ts +134 -27
- package/src/plugins/linear.ts +4 -2
- package/src/plugins/notion.ts +4 -2
- package/src/plugins/plugins.ts +27 -16
- package/src/plugins/tree-sitter/editor.ts +369 -0
- package/src/plugins/tree-sitter/lang-packs/index.ts +23 -0
- package/src/plugins/tree-sitter/lang-packs/java.ts +59 -0
- package/src/plugins/tree-sitter/lang-packs/javascript.ts +57 -0
- package/src/plugins/tree-sitter/lang-packs/python.ts +45 -0
- package/src/plugins/tree-sitter/lang-packs/types.ts +79 -0
- package/src/plugins/tree-sitter/lang-packs/typescript.ts +49 -0
- package/src/plugins/tree-sitter/parser.ts +444 -0
- package/src/plugins/tree-sitter/simple-paths.ts +467 -0
- package/src/plugins/types.ts +11 -0
- package/src/plugins/url.ts +5 -3
- package/src/plugins/vim.ts +8 -5
- package/src/processors/CustomVariables.ts +60 -70
- package/src/processors/TokenCompressor.ts +15 -14
- package/src/processors/ToolResponseCache.ts +20 -14
- package/src/services/EmbeddingService.ts +18 -9
- package/src/services/EventService.ts +80 -0
- package/src/services/Mcp.ts +5 -0
- package/src/services/S3.ts +4 -3
- package/src/services/Tools.ts +125 -53
- package/src/services/index.ts +16 -11
- package/src/services/types.ts +3 -3
- package/src/types.ts +7 -2
- package/src/worker.ts +14 -1
- package/test-comprehensive.ts +31 -0
- package/tests/clients/AIClient.test.ts +490 -0
- package/tests/manual/agent-events/run-test.ts +203 -0
- package/tests/{integration → manual/file-edits}/figma.test.ts +1 -1
- package/tests/{integration → manual/file-edits}/fileblocks/readwrite.test.ts +7 -3
- package/tests/{integration → manual/file-edits}/patching.test.ts +11 -8
- package/tests/plugins/language/languagePlugin-content-triggers.test.ts +332 -0
- package/tests/plugins/language/languagePlugin-integration.test.ts +456 -0
- package/tests/plugins/language/languagePlugin.test.ts +363 -0
- package/tests/processors/Base64ImageDetector.test.ts +403 -0
- package/tests/processors/CustomVariables.test.ts +485 -0
- package/tests/processors/HarmonyToolProcessor.test.ts +490 -0
- package/tests/processors/TokenCompressor.test.ts +390 -0
- package/tests/processors/ToolResponseCache.test.ts +736 -0
- package/tests/services/Tools.test.ts +1339 -0
- package/tests/test.spec.ts +162 -117
- package/tests/tree-sitter/editor.test.ts +113 -0
- package/tests/tree-sitter/invalid.test.ts +299 -0
- package/tests/tree-sitter/paths/common-edits.test.ts +564 -0
- package/tests/tree-sitter/paths/debug-exact-position.test.ts +44 -0
- package/tests/tree-sitter/paths/debug-line-indexing.test.ts +49 -0
- package/tests/tree-sitter/paths/debug-paths.test.ts +90 -0
- package/tests/tree-sitter/paths/paths.test.ts +170 -0
- package/tests/tree-sitter/paths/simple-paths.test.ts +367 -0
- package/tests/tree-sitter/sample-after.ts +48 -0
- package/tests/tree-sitter/sample-before.ts +25 -0
- package/tests/tree-sitter/test-files/completely-broken.ts +7 -0
- package/tests/tree-sitter/test-files/duplicate-braces.ts +39 -0
- package/tests/tree-sitter/test-files/invalid-nesting.ts +39 -0
- package/tests/tree-sitter/test-files/malformed-signature.ts +39 -0
- package/tests/tree-sitter/test-files/mismatched-parens.ts +39 -0
- package/tests/tree-sitter/test-files/missing-semicolon.ts +39 -0
- package/tests/tree-sitter/test-files/partially-broken.ts +20 -0
- package/tests/tree-sitter/test-files/specific-errors.ts +14 -0
- package/tests/tree-sitter/test-files/unclosed-string.ts +39 -0
- package/tests/tree-sitter/tree-sitter.test.ts +251 -0
- package/ts_build/package.json +8 -3
- package/ts_build/src/agents/base/base.d.ts +7 -2
- package/ts_build/src/agents/base/base.js +27 -21
- package/ts_build/src/agents/base/base.js.map +1 -1
- package/ts_build/src/agents/patcher/patcher.js +26 -5
- package/ts_build/src/agents/patcher/patcher.js.map +1 -1
- package/ts_build/src/agents/tools/agentCall.js +2 -1
- package/ts_build/src/agents/tools/agentCall.js.map +1 -1
- package/ts_build/src/agents/tools/aiClient.d.ts +7 -8
- package/ts_build/src/agents/tools/aiClient.js.map +1 -1
- package/ts_build/src/agents/tools/ast/astAppendNode.d.ts +1 -0
- package/ts_build/src/agents/tools/ast/astAppendNode.js +96 -0
- package/ts_build/src/agents/tools/ast/astAppendNode.js.map +1 -0
- package/ts_build/src/agents/tools/ast/astDeleteNode.d.ts +1 -0
- package/ts_build/src/agents/tools/ast/astDeleteNode.js +94 -0
- package/ts_build/src/agents/tools/ast/astDeleteNode.js.map +1 -0
- package/ts_build/src/agents/tools/ast/astEditNode.d.ts +1 -0
- package/ts_build/src/agents/tools/ast/astEditNode.js +96 -0
- package/ts_build/src/agents/tools/ast/astEditNode.js.map +1 -0
- package/ts_build/src/agents/tools/ast/astGetPathForLine.d.ts +1 -0
- package/ts_build/src/agents/tools/ast/astGetPathForLine.js +78 -0
- package/ts_build/src/agents/tools/ast/astGetPathForLine.js.map +1 -0
- package/ts_build/src/agents/tools/ast/astListPaths.d.ts +1 -0
- package/ts_build/src/agents/tools/ast/astListPaths.js +78 -0
- package/ts_build/src/agents/tools/ast/astListPaths.js.map +1 -0
- package/ts_build/src/agents/tools/ast/index.d.ts +5 -0
- package/ts_build/src/agents/tools/ast/index.js +14 -0
- package/ts_build/src/agents/tools/ast/index.js.map +1 -0
- package/ts_build/src/agents/tools/astAppendNode.d.ts +1 -0
- package/ts_build/src/agents/tools/astAppendNode.js +98 -0
- package/ts_build/src/agents/tools/astAppendNode.js.map +1 -0
- package/ts_build/src/agents/tools/astDeleteNode.d.ts +1 -0
- package/ts_build/src/agents/tools/astDeleteNode.js +95 -0
- package/ts_build/src/agents/tools/astDeleteNode.js.map +1 -0
- package/ts_build/src/agents/tools/astEditNode.d.ts +1 -0
- package/ts_build/src/agents/tools/astEditNode.js +98 -0
- package/ts_build/src/agents/tools/astEditNode.js.map +1 -0
- package/ts_build/src/agents/tools/astGetPathForLine.d.ts +1 -0
- package/ts_build/src/agents/tools/astGetPathForLine.js +89 -0
- package/ts_build/src/agents/tools/astGetPathForLine.js.map +1 -0
- package/ts_build/src/agents/tools/astListPaths.d.ts +1 -0
- package/ts_build/src/agents/tools/astListPaths.js +82 -0
- package/ts_build/src/agents/tools/astListPaths.js.map +1 -0
- package/ts_build/src/agents/tools/callPlugin.js +4 -2
- package/ts_build/src/agents/tools/callPlugin.js.map +1 -1
- package/ts_build/src/agents/tools/embeddingSearch.js +3 -2
- package/ts_build/src/agents/tools/embeddingSearch.js.map +1 -1
- package/ts_build/src/agents/tools/execCommand.d.ts +2 -2
- package/ts_build/src/agents/tools/execCommand.js +201 -67
- package/ts_build/src/agents/tools/execCommand.js.map +1 -1
- package/ts_build/src/agents/tools/fileSearch.d.ts +1 -1
- package/ts_build/src/agents/tools/fileSearch.js +11 -15
- package/ts_build/src/agents/tools/fileSearch.js.map +1 -1
- package/ts_build/src/agents/tools/github/index.d.ts +1 -1
- package/ts_build/src/agents/tools/index.d.ts +1 -0
- package/ts_build/src/agents/tools/index.js +1 -0
- package/ts_build/src/agents/tools/index.js.map +1 -1
- package/ts_build/src/agents/tools/language/definitions.js +11 -2
- package/ts_build/src/agents/tools/language/definitions.js.map +1 -1
- package/ts_build/src/agents/tools/language/index.js +4 -3
- package/ts_build/src/agents/tools/language/index.js.map +1 -1
- package/ts_build/src/agents/tools/lintFile.js +4 -2
- package/ts_build/src/agents/tools/lintFile.js.map +1 -1
- package/ts_build/src/agents/tools/list.js +185 -49
- package/ts_build/src/agents/tools/list.js.map +1 -1
- package/ts_build/src/agents/tools/patch.js +33 -10
- package/ts_build/src/agents/tools/patch.js.map +1 -1
- package/ts_build/src/agents/tools/readBlocks.js +23 -0
- package/ts_build/src/agents/tools/readBlocks.js.map +1 -1
- package/ts_build/src/agents/tools/readFile.js +14 -0
- package/ts_build/src/agents/tools/readFile.js.map +1 -1
- package/ts_build/src/agents/tools/stringReplace.js +19 -2
- package/ts_build/src/agents/tools/stringReplace.js.map +1 -1
- package/ts_build/src/agents/tools/writeFile.js +40 -0
- package/ts_build/src/agents/tools/writeFile.js.map +1 -1
- package/ts_build/src/agents/tools/ycmd/server.js +5 -0
- package/ts_build/src/agents/tools/ycmd/server.js.map +1 -1
- package/ts_build/src/chat/CliChatService.d.ts +1 -0
- package/ts_build/src/chat/CliChatService.js +6 -2
- package/ts_build/src/chat/CliChatService.js.map +1 -1
- package/ts_build/src/chat/modules/AgentModule.d.ts +5 -1
- package/ts_build/src/chat/modules/AgentModule.js +62 -32
- package/ts_build/src/chat/modules/AgentModule.js.map +1 -1
- package/ts_build/src/chat/modules/AskModule.js.map +1 -1
- package/ts_build/src/chat/modules/SetupModule.js +4 -3
- package/ts_build/src/chat/modules/SetupModule.js.map +1 -1
- package/ts_build/src/chat/modules/SystemModule.js +19 -4
- package/ts_build/src/chat/modules/SystemModule.js.map +1 -1
- package/ts_build/src/chat/modules/index.d.ts +5 -0
- package/ts_build/src/chat/modules/index.js +14 -0
- package/ts_build/src/chat/modules/index.js.map +1 -0
- package/ts_build/src/chat/types.d.ts +2 -0
- package/ts_build/src/chat-old.js +3 -3
- package/ts_build/src/chat-old.js.map +1 -1
- package/ts_build/src/clients/anthropic.d.ts +1 -0
- package/ts_build/src/clients/anthropic.js +22 -1
- package/ts_build/src/clients/anthropic.js.map +1 -1
- package/ts_build/src/clients/openai.js +1 -1
- package/ts_build/src/clients/openai.js.map +1 -1
- package/ts_build/src/clients/xai.d.ts +7 -0
- package/ts_build/src/clients/xai.js +13 -4
- package/ts_build/src/clients/xai.js.map +1 -1
- package/ts_build/src/config.js +14 -3
- package/ts_build/src/config.js.map +1 -1
- package/ts_build/src/dataset/diffs/generate.js +2 -2
- package/ts_build/src/dataset/diffs/generate.js.map +1 -1
- package/ts_build/src/dataset/diffs/jsonl.js.map +1 -1
- package/ts_build/src/embeddings.js +9 -13
- package/ts_build/src/embeddings.js.map +1 -1
- package/ts_build/src/index.js +10 -10
- package/ts_build/src/index.js.map +1 -1
- package/ts_build/src/plugins/GitPlugin.d.ts +39 -0
- package/ts_build/src/plugins/GitPlugin.js +439 -0
- package/ts_build/src/plugins/GitPlugin.js.map +1 -0
- package/ts_build/src/plugins/LinterPlugin.d.ts +15 -0
- package/ts_build/src/plugins/LinterPlugin.js +65 -0
- package/ts_build/src/plugins/LinterPlugin.js.map +1 -0
- package/ts_build/src/plugins/PluginBase.d.ts +4 -3
- package/ts_build/src/plugins/PluginBase.js +3 -3
- package/ts_build/src/plugins/PluginBase.js.map +1 -1
- package/ts_build/src/plugins/asana.d.ts +3 -1
- package/ts_build/src/plugins/asana.js +3 -2
- package/ts_build/src/plugins/asana.js.map +1 -1
- package/ts_build/src/plugins/downloader/plugin.d.ts +3 -1
- package/ts_build/src/plugins/downloader/plugin.js +3 -2
- package/ts_build/src/plugins/downloader/plugin.js.map +1 -1
- package/ts_build/src/plugins/embedding.d.ts +5 -1
- package/ts_build/src/plugins/embedding.js +15 -3
- package/ts_build/src/plugins/embedding.js.map +1 -1
- package/ts_build/src/plugins/figma.d.ts +3 -1
- package/ts_build/src/plugins/figma.js +28 -4
- package/ts_build/src/plugins/figma.js.map +1 -1
- package/ts_build/src/plugins/github.d.ts +3 -1
- package/ts_build/src/plugins/github.js +3 -2
- package/ts_build/src/plugins/github.js.map +1 -1
- package/ts_build/src/plugins/jira.d.ts +3 -1
- package/ts_build/src/plugins/jira.js +3 -2
- package/ts_build/src/plugins/jira.js.map +1 -1
- package/ts_build/src/plugins/language.d.ts +7 -4
- package/ts_build/src/plugins/language.js +85 -20
- package/ts_build/src/plugins/language.js.map +1 -1
- package/ts_build/src/plugins/linear.d.ts +3 -1
- package/ts_build/src/plugins/linear.js +3 -2
- package/ts_build/src/plugins/linear.js.map +1 -1
- package/ts_build/src/plugins/notion.d.ts +3 -1
- package/ts_build/src/plugins/notion.js +3 -2
- package/ts_build/src/plugins/notion.js.map +1 -1
- package/ts_build/src/plugins/plugins.d.ts +4 -3
- package/ts_build/src/plugins/plugins.js +24 -14
- package/ts_build/src/plugins/plugins.js.map +1 -1
- package/ts_build/src/plugins/tree-sitter/editor.d.ts +34 -0
- package/ts_build/src/plugins/tree-sitter/editor.js +218 -0
- package/ts_build/src/plugins/tree-sitter/editor.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/human-readable-paths-new.d.ts +29 -0
- package/ts_build/src/plugins/tree-sitter/human-readable-paths-new.js +538 -0
- package/ts_build/src/plugins/tree-sitter/human-readable-paths-new.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/human-readable-paths.d.ts +22 -0
- package/ts_build/src/plugins/tree-sitter/human-readable-paths.js +332 -0
- package/ts_build/src/plugins/tree-sitter/human-readable-paths.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/index.d.ts +8 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/index.js +26 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/index.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/java.d.ts +2 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/java.js +61 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/java.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/javascript.d.ts +2 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/javascript.js +59 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/javascript.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/python.d.ts +2 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/python.js +47 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/python.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/types.d.ts +43 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/types.js +3 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/types.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/typescript.d.ts +2 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/typescript.js +50 -0
- package/ts_build/src/plugins/tree-sitter/lang-packs/typescript.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/parser.d.ts +75 -0
- package/ts_build/src/plugins/tree-sitter/parser.js +306 -0
- package/ts_build/src/plugins/tree-sitter/parser.js.map +1 -0
- package/ts_build/src/plugins/tree-sitter/simple-paths.d.ts +22 -0
- package/ts_build/src/plugins/tree-sitter/simple-paths.js +332 -0
- package/ts_build/src/plugins/tree-sitter/simple-paths.js.map +1 -0
- package/ts_build/src/plugins/types.d.ts +10 -0
- package/ts_build/src/plugins/url.d.ts +3 -2
- package/ts_build/src/plugins/url.js +3 -2
- package/ts_build/src/plugins/url.js.map +1 -1
- package/ts_build/src/plugins/vim.d.ts +4 -2
- package/ts_build/src/plugins/vim.js +6 -8
- package/ts_build/src/plugins/vim.js.map +1 -1
- package/ts_build/src/processors/CustomVariables.js +45 -47
- package/ts_build/src/processors/CustomVariables.js.map +1 -1
- package/ts_build/src/processors/TokenCompressor.js +10 -13
- package/ts_build/src/processors/TokenCompressor.js.map +1 -1
- package/ts_build/src/processors/ToolResponseCache.d.ts +2 -2
- package/ts_build/src/processors/ToolResponseCache.js +18 -10
- package/ts_build/src/processors/ToolResponseCache.js.map +1 -1
- package/ts_build/src/services/EmbeddingService.d.ts +10 -1
- package/ts_build/src/services/EmbeddingService.js +12 -12
- package/ts_build/src/services/EmbeddingService.js.map +1 -1
- package/ts_build/src/services/EventService.d.ts +7 -0
- package/ts_build/src/services/EventService.js +49 -0
- package/ts_build/src/services/EventService.js.map +1 -1
- package/ts_build/src/services/Mcp.js +8 -0
- package/ts_build/src/services/Mcp.js.map +1 -1
- package/ts_build/src/services/S3.js +4 -3
- package/ts_build/src/services/S3.js.map +1 -1
- package/ts_build/src/services/Tools.d.ts +1 -0
- package/ts_build/src/services/Tools.js +97 -35
- package/ts_build/src/services/Tools.js.map +1 -1
- package/ts_build/src/services/index.d.ts +4 -5
- package/ts_build/src/services/index.js +14 -9
- package/ts_build/src/services/index.js.map +1 -1
- package/ts_build/src/services/types.js +3 -3
- package/ts_build/src/services/types.js.map +1 -1
- package/ts_build/src/types.d.ts +7 -1
- package/ts_build/src/types.js +4 -0
- package/ts_build/src/types.js.map +1 -1
- package/ts_build/src/worker.js +12 -1
- package/ts_build/src/worker.js.map +1 -1
- package/ts_build/tests/clients/AIClient.test.d.ts +1 -0
- package/ts_build/tests/clients/AIClient.test.js +377 -0
- package/ts_build/tests/clients/AIClient.test.js.map +1 -0
- package/ts_build/tests/languagePlugin.test.js +217 -11
- package/ts_build/tests/languagePlugin.test.js.map +1 -1
- package/ts_build/tests/manual/agent-events/event-handler-reliability.test.d.ts +1 -0
- package/ts_build/tests/manual/agent-events/event-handler-reliability.test.js +315 -0
- package/ts_build/tests/manual/agent-events/event-handler-reliability.test.js.map +1 -0
- package/ts_build/tests/manual/agent-events/run-test.d.ts +2 -0
- package/ts_build/tests/manual/agent-events/run-test.js +148 -0
- package/ts_build/tests/manual/agent-events/run-test.js.map +1 -0
- package/ts_build/tests/manual/file-edits/figma.test.d.ts +1 -0
- package/ts_build/tests/manual/file-edits/figma.test.js +47 -0
- package/ts_build/tests/manual/file-edits/figma.test.js.map +1 -0
- package/ts_build/tests/manual/file-edits/fileblocks/readwrite.test.d.ts +1 -0
- package/ts_build/tests/manual/file-edits/fileblocks/readwrite.test.js +100 -0
- package/ts_build/tests/manual/file-edits/fileblocks/readwrite.test.js.map +1 -0
- package/ts_build/tests/manual/file-edits/patching.test.d.ts +1 -0
- package/ts_build/tests/manual/file-edits/patching.test.js +119 -0
- package/ts_build/tests/manual/file-edits/patching.test.js.map +1 -0
- package/ts_build/tests/plugins/language/languagePlugin-content-triggers.test.d.ts +1 -0
- package/ts_build/tests/plugins/language/languagePlugin-content-triggers.test.js +277 -0
- package/ts_build/tests/plugins/language/languagePlugin-content-triggers.test.js.map +1 -0
- package/ts_build/tests/plugins/language/languagePlugin-integration.test.d.ts +1 -0
- package/ts_build/tests/plugins/language/languagePlugin-integration.test.js +331 -0
- package/ts_build/tests/plugins/language/languagePlugin-integration.test.js.map +1 -0
- package/ts_build/tests/plugins/language/languagePlugin.test.d.ts +1 -0
- package/ts_build/tests/plugins/language/languagePlugin.test.js +286 -0
- package/ts_build/tests/plugins/language/languagePlugin.test.js.map +1 -0
- package/ts_build/tests/processors/Base64ImageDetector.test.d.ts +1 -0
- package/ts_build/tests/processors/Base64ImageDetector.test.js +351 -0
- package/ts_build/tests/processors/Base64ImageDetector.test.js.map +1 -0
- package/ts_build/tests/processors/CustomVariables.test.d.ts +1 -0
- package/ts_build/tests/processors/CustomVariables.test.js +354 -0
- package/ts_build/tests/processors/CustomVariables.test.js.map +1 -0
- package/ts_build/tests/processors/HarmonyToolProcessor.test.d.ts +1 -0
- package/ts_build/tests/processors/HarmonyToolProcessor.test.js +382 -0
- package/ts_build/tests/processors/HarmonyToolProcessor.test.js.map +1 -0
- package/ts_build/tests/processors/TokenCompressor.test.d.ts +1 -0
- package/ts_build/tests/processors/TokenCompressor.test.js +299 -0
- package/ts_build/tests/processors/TokenCompressor.test.js.map +1 -0
- package/ts_build/tests/processors/ToolResponseCache.test.d.ts +1 -0
- package/ts_build/tests/processors/ToolResponseCache.test.js +550 -0
- package/ts_build/tests/processors/ToolResponseCache.test.js.map +1 -0
- package/ts_build/tests/services/Plugins/plugin-event-integration.test.d.ts +1 -0
- package/ts_build/tests/services/Plugins/plugin-event-integration.test.js +232 -0
- package/ts_build/tests/services/Plugins/plugin-event-integration.test.js.map +1 -0
- package/ts_build/tests/services/Tools.test.d.ts +1 -0
- package/ts_build/tests/services/Tools.test.js +1059 -0
- package/ts_build/tests/services/Tools.test.js.map +1 -0
- package/ts_build/tests/test.spec.js +110 -68
- package/ts_build/tests/test.spec.js.map +1 -1
- package/ts_build/tests/tree-sitter/editor.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/editor.test.js +85 -0
- package/ts_build/tests/tree-sitter/editor.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/invalid.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/invalid.test.js +198 -0
- package/ts_build/tests/tree-sitter/invalid.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/paths/common-edits.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/paths/common-edits.test.js +347 -0
- package/ts_build/tests/tree-sitter/paths/common-edits.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/paths/debug-exact-position.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/paths/debug-exact-position.test.js +35 -0
- package/ts_build/tests/tree-sitter/paths/debug-exact-position.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/paths/debug-line-indexing.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/paths/debug-line-indexing.test.js +38 -0
- package/ts_build/tests/tree-sitter/paths/debug-line-indexing.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/paths/debug-paths.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/paths/debug-paths.test.js +74 -0
- package/ts_build/tests/tree-sitter/paths/debug-paths.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/paths/human-readable-paths.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/paths/human-readable-paths.test.js +302 -0
- package/ts_build/tests/tree-sitter/paths/human-readable-paths.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/paths/paths.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/paths/paths.test.js +116 -0
- package/ts_build/tests/tree-sitter/paths/paths.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/paths/simple-paths.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/paths/simple-paths.test.js +302 -0
- package/ts_build/tests/tree-sitter/paths/simple-paths.test.js.map +1 -0
- package/ts_build/tests/tree-sitter/sample-after.d.ts +11 -0
- package/ts_build/tests/tree-sitter/sample-after.js +44 -0
- package/ts_build/tests/tree-sitter/sample-after.js.map +1 -0
- package/ts_build/tests/tree-sitter/sample-before.d.ts +9 -0
- package/ts_build/tests/tree-sitter/sample-before.js +28 -0
- package/ts_build/tests/tree-sitter/sample-before.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/completely-broken.d.ts +2 -0
- package/ts_build/tests/tree-sitter/test-files/completely-broken.js +17 -0
- package/ts_build/tests/tree-sitter/test-files/completely-broken.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/duplicate-braces.d.ts +8 -0
- package/ts_build/tests/tree-sitter/test-files/duplicate-braces.js +38 -0
- package/ts_build/tests/tree-sitter/test-files/duplicate-braces.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/invalid-nesting.d.ts +8 -0
- package/ts_build/tests/tree-sitter/test-files/invalid-nesting.js +38 -0
- package/ts_build/tests/tree-sitter/test-files/invalid-nesting.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/malformed-signature.d.ts +8 -0
- package/ts_build/tests/tree-sitter/test-files/malformed-signature.js +38 -0
- package/ts_build/tests/tree-sitter/test-files/malformed-signature.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/mismatched-parens.d.ts +10 -0
- package/ts_build/tests/tree-sitter/test-files/mismatched-parens.js +38 -0
- package/ts_build/tests/tree-sitter/test-files/mismatched-parens.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/missing-semicolon.d.ts +8 -0
- package/ts_build/tests/tree-sitter/test-files/missing-semicolon.js +38 -0
- package/ts_build/tests/tree-sitter/test-files/missing-semicolon.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/partially-broken.d.ts +6 -0
- package/ts_build/tests/tree-sitter/test-files/partially-broken.js +20 -0
- package/ts_build/tests/tree-sitter/test-files/partially-broken.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/specific-errors.d.ts +7 -0
- package/ts_build/tests/tree-sitter/test-files/specific-errors.js +14 -0
- package/ts_build/tests/tree-sitter/test-files/specific-errors.js.map +1 -0
- package/ts_build/tests/tree-sitter/test-files/unclosed-string.d.ts +8 -0
- package/ts_build/tests/tree-sitter/test-files/unclosed-string.js +38 -0
- package/ts_build/tests/tree-sitter/test-files/unclosed-string.js.map +1 -0
- package/ts_build/tests/tree-sitter/tree-sitter.test.d.ts +1 -0
- package/ts_build/tests/tree-sitter/tree-sitter.test.js +185 -0
- package/ts_build/tests/tree-sitter/tree-sitter.test.js.map +1 -0
- package/tsconfig.json +2 -1
- package/tests/languagePlugin.test.ts +0 -74
- /package/src/chat/modules/{index.js → index.ts} +0 -0
- /package/tests/{integration → manual/file-edits}/patching/input.txt +0 -0
- /package/tests/{integration → manual/file-edits}/patching/output.txt +0 -0
- /package/tests/{integration → manual/file-edits}/patching/patch.txt +0 -0
- /package/tests/{integration → manual/file-edits}/patching/unseen.txt +0 -0
|
@@ -0,0 +1,1059 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const Tools_1 = require("../../src/services/Tools");
|
|
4
|
+
const types_1 = require("../../src/services/types");
|
|
5
|
+
describe("ToolsService", () => {
|
|
6
|
+
let toolsService;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
toolsService = new Tools_1.ToolsService();
|
|
9
|
+
});
|
|
10
|
+
describe("constructor", () => {
|
|
11
|
+
it("should initialize with empty context when no context is provided", () => {
|
|
12
|
+
const service = new Tools_1.ToolsService();
|
|
13
|
+
const context = service.getContext();
|
|
14
|
+
expect(context).toBeDefined();
|
|
15
|
+
expect(context.Tools).toBe(service);
|
|
16
|
+
expect(context.Agents).toBeUndefined();
|
|
17
|
+
expect(context.Events).toBeUndefined();
|
|
18
|
+
expect(context.Clients).toBeUndefined();
|
|
19
|
+
expect(context.Plugins).toBeUndefined();
|
|
20
|
+
});
|
|
21
|
+
it("should initialize with provided context", () => {
|
|
22
|
+
const mockAgents = {};
|
|
23
|
+
const mockEvents = {};
|
|
24
|
+
const initialContext = {
|
|
25
|
+
Agents: mockAgents,
|
|
26
|
+
Events: mockEvents,
|
|
27
|
+
};
|
|
28
|
+
const service = new Tools_1.ToolsService(initialContext);
|
|
29
|
+
const context = service.getContext();
|
|
30
|
+
expect(context.Tools).toBe(service);
|
|
31
|
+
expect(context.Agents).toBe(mockAgents);
|
|
32
|
+
expect(context.Events).toBe(mockEvents);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe("context management", () => {
|
|
36
|
+
it("should set and get context", () => {
|
|
37
|
+
const mockContext = {
|
|
38
|
+
Agents: {},
|
|
39
|
+
metadata: { test: "value" },
|
|
40
|
+
};
|
|
41
|
+
toolsService.setContext(mockContext);
|
|
42
|
+
const context = toolsService.getContext();
|
|
43
|
+
expect(context.Tools).toBe(toolsService);
|
|
44
|
+
expect(context.Agents).toBe(mockContext.Agents);
|
|
45
|
+
expect(context.metadata).toEqual({ test: "value" });
|
|
46
|
+
});
|
|
47
|
+
it("should add context properties", () => {
|
|
48
|
+
const mockAgents = {};
|
|
49
|
+
toolsService.addContext("Agents", mockAgents);
|
|
50
|
+
const context = toolsService.getContext();
|
|
51
|
+
expect(context.Agents).toBe(mockAgents);
|
|
52
|
+
expect(context.Tools).toBe(toolsService);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
describe("tool management", () => {
|
|
56
|
+
const mockTool = {
|
|
57
|
+
type: "function",
|
|
58
|
+
function: {
|
|
59
|
+
name: "testTool",
|
|
60
|
+
description: "A test tool",
|
|
61
|
+
parameters: {
|
|
62
|
+
type: "object",
|
|
63
|
+
properties: {
|
|
64
|
+
input: { type: "string", description: "Test input" },
|
|
65
|
+
},
|
|
66
|
+
required: ["input"],
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
it("should add a single tool", () => {
|
|
71
|
+
toolsService.addTool(mockTool);
|
|
72
|
+
expect(toolsService.getTools()).toContain(mockTool);
|
|
73
|
+
expect(toolsService.getToolNames()).toContain("testTool");
|
|
74
|
+
});
|
|
75
|
+
it("should add multiple tools", () => {
|
|
76
|
+
const tool2 = {
|
|
77
|
+
type: "function",
|
|
78
|
+
function: {
|
|
79
|
+
name: "testTool2",
|
|
80
|
+
description: "Another test tool",
|
|
81
|
+
parameters: {
|
|
82
|
+
type: "object",
|
|
83
|
+
properties: {},
|
|
84
|
+
required: [],
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
toolsService.addTools([mockTool, tool2]);
|
|
89
|
+
expect(toolsService.getTools()).toHaveLength(2);
|
|
90
|
+
expect(toolsService.getToolNames()).toEqual(["testTool", "testTool2"]);
|
|
91
|
+
});
|
|
92
|
+
it("should prevent duplicate tool names", () => {
|
|
93
|
+
toolsService.addTool(mockTool);
|
|
94
|
+
toolsService.addTools([mockTool]);
|
|
95
|
+
expect(toolsService.getTools()).toHaveLength(1);
|
|
96
|
+
});
|
|
97
|
+
it("should get tool by name", () => {
|
|
98
|
+
toolsService.addTool(mockTool);
|
|
99
|
+
const retrievedTool = toolsService.getTool("testTool");
|
|
100
|
+
expect(retrievedTool).toBe(mockTool);
|
|
101
|
+
const nonExistentTool = toolsService.getTool("nonExistent");
|
|
102
|
+
expect(nonExistentTool).toBeUndefined();
|
|
103
|
+
});
|
|
104
|
+
it("should get tools by names", () => {
|
|
105
|
+
const tool2 = {
|
|
106
|
+
type: "function",
|
|
107
|
+
function: {
|
|
108
|
+
name: "testTool2",
|
|
109
|
+
description: "Another test tool",
|
|
110
|
+
parameters: {
|
|
111
|
+
type: "object",
|
|
112
|
+
properties: {},
|
|
113
|
+
required: [],
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
toolsService.addTools([mockTool, tool2]);
|
|
118
|
+
const selectedTools = toolsService.getToolsByNames(["testTool"]);
|
|
119
|
+
expect(selectedTools).toHaveLength(1);
|
|
120
|
+
expect(selectedTools[0]).toBe(mockTool);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
describe("function management", () => {
|
|
124
|
+
it("should set and get functions", () => {
|
|
125
|
+
const testFunction = jest.fn().mockReturnValue("test result");
|
|
126
|
+
toolsService.setFunction("testFunc", testFunction);
|
|
127
|
+
const retrievedFunction = toolsService.getFunction("testFunc");
|
|
128
|
+
expect(retrievedFunction).toBeDefined();
|
|
129
|
+
expect(typeof retrievedFunction).toBe("function");
|
|
130
|
+
});
|
|
131
|
+
it("should add multiple functions", () => {
|
|
132
|
+
const functions = {
|
|
133
|
+
func1: jest.fn(),
|
|
134
|
+
func2: jest.fn(),
|
|
135
|
+
};
|
|
136
|
+
toolsService.addFunctions(functions);
|
|
137
|
+
expect(toolsService.getFunction("func1")).toBeDefined();
|
|
138
|
+
expect(toolsService.getFunction("func2")).toBeDefined();
|
|
139
|
+
});
|
|
140
|
+
it("should set multiple functions by names and functions arrays", () => {
|
|
141
|
+
const func1 = jest.fn();
|
|
142
|
+
const func2 = jest.fn();
|
|
143
|
+
toolsService.setFunctions(["test1", "test2"], [func1, func2]);
|
|
144
|
+
expect(toolsService.getFunction("test1")).toBeDefined();
|
|
145
|
+
expect(toolsService.getFunction("test2")).toBeDefined();
|
|
146
|
+
});
|
|
147
|
+
it("should define tools and functions together", () => {
|
|
148
|
+
const mockTool = {
|
|
149
|
+
type: "function",
|
|
150
|
+
function: {
|
|
151
|
+
name: "combinedTool",
|
|
152
|
+
parameters: {
|
|
153
|
+
type: "object",
|
|
154
|
+
properties: {},
|
|
155
|
+
required: [],
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
const mockFunction = jest.fn();
|
|
160
|
+
toolsService.defineTools([mockTool], { combinedTool: mockFunction });
|
|
161
|
+
expect(toolsService.getTool("combinedTool")).toBe(mockTool);
|
|
162
|
+
expect(toolsService.getFunction("combinedTool")).toBeDefined();
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
describe("copyToolsFrom", () => {
|
|
166
|
+
it("should copy tools and functions from another ToolsService", () => {
|
|
167
|
+
const sourceService = new Tools_1.ToolsService();
|
|
168
|
+
const mockTool = {
|
|
169
|
+
type: "function",
|
|
170
|
+
function: {
|
|
171
|
+
name: "sourceTool",
|
|
172
|
+
parameters: {
|
|
173
|
+
type: "object",
|
|
174
|
+
properties: {},
|
|
175
|
+
required: [],
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
const mockFunction = jest.fn();
|
|
180
|
+
sourceService.addTool(mockTool);
|
|
181
|
+
sourceService.setFunction("sourceTool", mockFunction);
|
|
182
|
+
toolsService.copyToolsFrom(["sourceTool"], sourceService);
|
|
183
|
+
expect(toolsService.getTool("sourceTool")).toEqual(mockTool);
|
|
184
|
+
expect(toolsService.getFunction("sourceTool")).toBeDefined();
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
describe("callTool", () => {
|
|
188
|
+
const mockTool = {
|
|
189
|
+
type: "function",
|
|
190
|
+
function: {
|
|
191
|
+
name: "testTool",
|
|
192
|
+
description: "A test tool",
|
|
193
|
+
parameters: {
|
|
194
|
+
type: "object",
|
|
195
|
+
properties: {
|
|
196
|
+
input: { type: "string", description: "Test input" },
|
|
197
|
+
optional: { type: "string", description: "Optional input" },
|
|
198
|
+
},
|
|
199
|
+
required: ["input"],
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
const mockToolCall = {
|
|
204
|
+
id: "call_123",
|
|
205
|
+
type: "function",
|
|
206
|
+
function: {
|
|
207
|
+
name: "testTool",
|
|
208
|
+
arguments: JSON.stringify({ input: "test value" }),
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
beforeEach(() => {
|
|
212
|
+
toolsService.addTool(mockTool);
|
|
213
|
+
jest.clearAllMocks();
|
|
214
|
+
});
|
|
215
|
+
it("should successfully call a tool with correct arguments", async () => {
|
|
216
|
+
const mockFunction = jest.fn().mockResolvedValue("success result");
|
|
217
|
+
toolsService.setFunction("testTool", mockFunction);
|
|
218
|
+
const result = await toolsService.callTool(mockToolCall);
|
|
219
|
+
expect(mockFunction).toHaveBeenCalledWith({ input: "test value" });
|
|
220
|
+
expect(result.toolMessages).toHaveLength(1);
|
|
221
|
+
expect(result.toolMessages[0]).toEqual({
|
|
222
|
+
tool_call_id: "call_123",
|
|
223
|
+
role: "tool",
|
|
224
|
+
name: "testTool",
|
|
225
|
+
content: "success result",
|
|
226
|
+
});
|
|
227
|
+
expect(result.functionResp).toBe("success result");
|
|
228
|
+
});
|
|
229
|
+
it("should handle positional arguments", async () => {
|
|
230
|
+
const positionalTool = {
|
|
231
|
+
type: "function",
|
|
232
|
+
function: {
|
|
233
|
+
name: "positionalTool",
|
|
234
|
+
parameters: {
|
|
235
|
+
type: "object",
|
|
236
|
+
positional: true,
|
|
237
|
+
properties: {
|
|
238
|
+
arg1: { type: "string" },
|
|
239
|
+
arg2: { type: "number" },
|
|
240
|
+
},
|
|
241
|
+
required: ["arg1", "arg2"],
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
const positionalCall = {
|
|
246
|
+
id: "call_456",
|
|
247
|
+
type: "function",
|
|
248
|
+
function: {
|
|
249
|
+
name: "positionalTool",
|
|
250
|
+
arguments: JSON.stringify({ arg1: "hello", arg2: 42 }),
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
const mockFunction = jest.fn().mockResolvedValue("positional result");
|
|
254
|
+
toolsService.addTool(positionalTool);
|
|
255
|
+
toolsService.setFunction("positionalTool", mockFunction);
|
|
256
|
+
await toolsService.callTool(positionalCall);
|
|
257
|
+
expect(mockFunction).toHaveBeenCalledWith("hello", 42);
|
|
258
|
+
});
|
|
259
|
+
it("should handle tool not enabled error", async () => {
|
|
260
|
+
const mockFunction = jest.fn();
|
|
261
|
+
toolsService.setFunction("testTool", mockFunction);
|
|
262
|
+
const result = await toolsService.callTool(mockToolCall, ["otherTool"]);
|
|
263
|
+
expect(mockFunction).not.toHaveBeenCalled();
|
|
264
|
+
expect(result.toolMessages[0].name).toBe("error");
|
|
265
|
+
expect(result.toolMessages[0].content).toContain("not enabled");
|
|
266
|
+
expect(result.functionResp).toBeUndefined();
|
|
267
|
+
});
|
|
268
|
+
it("should handle tool definition not found", async () => {
|
|
269
|
+
const unknownToolCall = {
|
|
270
|
+
id: "call_unknown",
|
|
271
|
+
type: "function",
|
|
272
|
+
function: {
|
|
273
|
+
name: "unknownTool",
|
|
274
|
+
arguments: "{}",
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
const result = await toolsService.callTool(unknownToolCall);
|
|
278
|
+
expect(result.toolMessages[0].name).toBe("error");
|
|
279
|
+
expect(result.toolMessages[0].content).toContain("not enabled");
|
|
280
|
+
});
|
|
281
|
+
it("should handle function implementation not found", async () => {
|
|
282
|
+
const result = await toolsService.callTool(mockToolCall);
|
|
283
|
+
expect(result.toolMessages[0].name).toBe("error");
|
|
284
|
+
expect(result.toolMessages[0].content).toContain("not found");
|
|
285
|
+
});
|
|
286
|
+
it("should handle function execution errors", async () => {
|
|
287
|
+
const mockFunction = jest
|
|
288
|
+
.fn()
|
|
289
|
+
.mockRejectedValue(new Error("Function failed"));
|
|
290
|
+
toolsService.setFunction("testTool", mockFunction);
|
|
291
|
+
const result = await toolsService.callTool(mockToolCall);
|
|
292
|
+
expect(result.toolMessages[0].name).toBe("error");
|
|
293
|
+
expect(result.toolMessages[0].content).toContain("ERROR: Function failed");
|
|
294
|
+
expect(result.functionResp).toBeUndefined();
|
|
295
|
+
});
|
|
296
|
+
it("should handle object responses by converting to JSON", async () => {
|
|
297
|
+
const mockFunction = jest
|
|
298
|
+
.fn()
|
|
299
|
+
.mockResolvedValue({ key: "value", nested: { data: 123 } });
|
|
300
|
+
toolsService.setFunction("testTool", mockFunction);
|
|
301
|
+
const result = await toolsService.callTool(mockToolCall);
|
|
302
|
+
expect(result.toolMessages[0].content).toBe(JSON.stringify({ key: "value", nested: { data: 123 } }, null, 2));
|
|
303
|
+
});
|
|
304
|
+
it("should handle multi_tool_use.parallel special case", async () => {
|
|
305
|
+
const parallelTool = {
|
|
306
|
+
type: "function",
|
|
307
|
+
function: {
|
|
308
|
+
name: "multi_tool_use.parallel",
|
|
309
|
+
parameters: {
|
|
310
|
+
type: "object",
|
|
311
|
+
properties: {},
|
|
312
|
+
required: [],
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
const parallelCall = {
|
|
317
|
+
id: "call_parallel",
|
|
318
|
+
type: "function",
|
|
319
|
+
function: {
|
|
320
|
+
name: "multi_tool_use.parallel",
|
|
321
|
+
arguments: JSON.stringify([
|
|
322
|
+
{ recipient_name: "tool1.action", parameters: {} },
|
|
323
|
+
{ recipient_name: "tool2.action", parameters: {} },
|
|
324
|
+
]),
|
|
325
|
+
},
|
|
326
|
+
};
|
|
327
|
+
const mockFunction = jest.fn().mockResolvedValue(["result1", "result2"]);
|
|
328
|
+
toolsService.addTool(parallelTool);
|
|
329
|
+
toolsService.setFunction("multi_tool_use.parallel", mockFunction);
|
|
330
|
+
const result = await toolsService.callTool(parallelCall);
|
|
331
|
+
expect(result.toolMessages).toHaveLength(2);
|
|
332
|
+
expect(result.toolMessages[0].tool_call_id).toBe("call_parallel_0");
|
|
333
|
+
expect(result.toolMessages[1].tool_call_id).toBe("call_parallel_1");
|
|
334
|
+
expect(result.toolMessages[0].name).toBe("action");
|
|
335
|
+
expect(result.toolMessages[1].name).toBe("action");
|
|
336
|
+
});
|
|
337
|
+
it("should handle string arguments that need parsing", async () => {
|
|
338
|
+
const toolCallWithStringArgs = {
|
|
339
|
+
id: "call_string",
|
|
340
|
+
type: "function",
|
|
341
|
+
function: {
|
|
342
|
+
name: "testTool",
|
|
343
|
+
arguments: '{"input": "escaped\\nstring"}',
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
const mockFunction = jest.fn().mockResolvedValue("success");
|
|
347
|
+
toolsService.setFunction("testTool", mockFunction);
|
|
348
|
+
await toolsService.callTool(toolCallWithStringArgs);
|
|
349
|
+
expect(mockFunction).toHaveBeenCalledWith({ input: "escaped\nstring" });
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
describe("Tool Override System", () => {
|
|
353
|
+
const mockTool = {
|
|
354
|
+
type: "function",
|
|
355
|
+
function: {
|
|
356
|
+
name: "overridableTool",
|
|
357
|
+
description: "A tool that can be overridden",
|
|
358
|
+
parameters: {
|
|
359
|
+
type: "object",
|
|
360
|
+
properties: {
|
|
361
|
+
input: { type: "string" },
|
|
362
|
+
},
|
|
363
|
+
required: ["input"],
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
beforeEach(() => {
|
|
368
|
+
toolsService.addTool(mockTool);
|
|
369
|
+
jest.clearAllMocks();
|
|
370
|
+
});
|
|
371
|
+
it("should register override with string pattern", () => {
|
|
372
|
+
const overrideFunction = jest.fn().mockResolvedValue("override result");
|
|
373
|
+
toolsService.registerOverride("overridableTool", overrideFunction);
|
|
374
|
+
expect(() => toolsService.getFunction("overridableTool")).not.toThrow();
|
|
375
|
+
});
|
|
376
|
+
it("should register override with regex pattern", () => {
|
|
377
|
+
const overrideFunction = jest.fn().mockResolvedValue("regex override");
|
|
378
|
+
const pattern = /^overridable/;
|
|
379
|
+
toolsService.registerOverride(pattern, overrideFunction);
|
|
380
|
+
expect(() => toolsService.getFunction("overridableTool")).not.toThrow();
|
|
381
|
+
});
|
|
382
|
+
it("should execute override function instead of original", async () => {
|
|
383
|
+
const originalFunction = jest.fn().mockResolvedValue("original result");
|
|
384
|
+
const overrideFunction = jest.fn().mockResolvedValue("override result");
|
|
385
|
+
toolsService.setFunction("overridableTool", originalFunction);
|
|
386
|
+
toolsService.registerOverride("overridableTool", overrideFunction);
|
|
387
|
+
const toolCall = {
|
|
388
|
+
id: "call_override",
|
|
389
|
+
type: "function",
|
|
390
|
+
function: {
|
|
391
|
+
name: "overridableTool",
|
|
392
|
+
arguments: JSON.stringify({ input: "test" }),
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
const result = await toolsService.callTool(toolCall);
|
|
396
|
+
expect(overrideFunction).toHaveBeenCalledWith([{ input: "test" }], expect.any(Object));
|
|
397
|
+
expect(originalFunction).not.toHaveBeenCalled();
|
|
398
|
+
expect(result.functionResp).toBe("override result");
|
|
399
|
+
});
|
|
400
|
+
it("should handle override priority ordering", async () => {
|
|
401
|
+
const override1 = jest.fn().mockResolvedValue("override1");
|
|
402
|
+
const override2 = jest.fn().mockResolvedValue("override2");
|
|
403
|
+
toolsService.registerOverride("overridableTool", override1, 1);
|
|
404
|
+
toolsService.registerOverride("overridableTool", override2, 2);
|
|
405
|
+
const func = toolsService.getFunction("overridableTool");
|
|
406
|
+
expect(await func({})).toBe("override2");
|
|
407
|
+
});
|
|
408
|
+
it("should handle pattern matching with wildcards", async () => {
|
|
409
|
+
const overrideFunction = jest.fn().mockResolvedValue("wildcard override");
|
|
410
|
+
toolsService.registerOverride("overrid*Tool", overrideFunction);
|
|
411
|
+
const func = toolsService.getFunction("overridableTool");
|
|
412
|
+
expect(await func({})).toBe("wildcard override");
|
|
413
|
+
});
|
|
414
|
+
it("should remove overrides", async () => {
|
|
415
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
416
|
+
const overrideFunction = jest.fn().mockResolvedValue("override");
|
|
417
|
+
toolsService.setFunction("overridableTool", originalFunction);
|
|
418
|
+
toolsService.registerOverride("overridableTool", overrideFunction);
|
|
419
|
+
const overriddenFunc = toolsService.getFunction("overridableTool");
|
|
420
|
+
expect(await overriddenFunc({})).toBe("override");
|
|
421
|
+
toolsService.removeOverride("overridableTool");
|
|
422
|
+
const restoredFunc = toolsService.getFunction("overridableTool");
|
|
423
|
+
expect(await restoredFunc({})).toBe("original");
|
|
424
|
+
});
|
|
425
|
+
it("should preserve original functions when override is registered", async () => {
|
|
426
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
427
|
+
const overrideFunction = jest.fn().mockResolvedValue("override");
|
|
428
|
+
toolsService.setFunction("overridableTool", originalFunction);
|
|
429
|
+
toolsService.registerOverride("overridableTool", overrideFunction);
|
|
430
|
+
const originalStored = toolsService.getOriginalFunction("overridableTool");
|
|
431
|
+
expect(await originalStored({})).toBe("original");
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
describe("Tool Wrapper System", () => {
|
|
435
|
+
const mockTool = {
|
|
436
|
+
type: "function",
|
|
437
|
+
function: {
|
|
438
|
+
name: "wrappableTool",
|
|
439
|
+
description: "A tool that can be wrapped",
|
|
440
|
+
parameters: {
|
|
441
|
+
type: "object",
|
|
442
|
+
properties: {
|
|
443
|
+
input: { type: "string" },
|
|
444
|
+
},
|
|
445
|
+
required: ["input"],
|
|
446
|
+
},
|
|
447
|
+
},
|
|
448
|
+
};
|
|
449
|
+
beforeEach(() => {
|
|
450
|
+
toolsService.addTool(mockTool);
|
|
451
|
+
jest.clearAllMocks();
|
|
452
|
+
});
|
|
453
|
+
it("should register wrapper with string pattern", () => {
|
|
454
|
+
const wrapperFunction = jest
|
|
455
|
+
.fn()
|
|
456
|
+
.mockImplementation((originalFn, args) => {
|
|
457
|
+
return originalFn(args);
|
|
458
|
+
});
|
|
459
|
+
toolsService.registerWrapper("wrappableTool", wrapperFunction);
|
|
460
|
+
expect(() => toolsService.getFunction("wrappableTool")).not.toThrow();
|
|
461
|
+
});
|
|
462
|
+
it("should register wrapper with regex pattern", () => {
|
|
463
|
+
const wrapperFunction = jest
|
|
464
|
+
.fn()
|
|
465
|
+
.mockImplementation((originalFn, args) => {
|
|
466
|
+
return originalFn(args);
|
|
467
|
+
});
|
|
468
|
+
const pattern = /^wrappable/;
|
|
469
|
+
toolsService.registerWrapper(pattern, wrapperFunction);
|
|
470
|
+
expect(() => toolsService.getFunction("wrappableTool")).not.toThrow();
|
|
471
|
+
});
|
|
472
|
+
it("should execute wrapper function with original function", async () => {
|
|
473
|
+
const originalFunction = jest.fn().mockResolvedValue("original result");
|
|
474
|
+
const wrapperFunction = jest
|
|
475
|
+
.fn()
|
|
476
|
+
.mockImplementation(async (originalFn, args) => {
|
|
477
|
+
const result = await originalFn(args);
|
|
478
|
+
return `wrapped: ${result}`;
|
|
479
|
+
});
|
|
480
|
+
toolsService.setFunction("wrappableTool", originalFunction);
|
|
481
|
+
toolsService.registerWrapper("wrappableTool", wrapperFunction);
|
|
482
|
+
const toolCall = {
|
|
483
|
+
id: "call_wrapper",
|
|
484
|
+
type: "function",
|
|
485
|
+
function: {
|
|
486
|
+
name: "wrappableTool",
|
|
487
|
+
arguments: JSON.stringify({ input: "test" }),
|
|
488
|
+
},
|
|
489
|
+
};
|
|
490
|
+
const result = await toolsService.callTool(toolCall);
|
|
491
|
+
expect(wrapperFunction).toHaveBeenCalled();
|
|
492
|
+
expect(originalFunction).toHaveBeenCalledWith({ input: "test" });
|
|
493
|
+
expect(result.functionResp).toBe("wrapped: original result");
|
|
494
|
+
});
|
|
495
|
+
it("should handle wrapper priority ordering", () => {
|
|
496
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
497
|
+
const wrapper1 = jest.fn().mockImplementation((fn, args) => fn(args));
|
|
498
|
+
const wrapper2 = jest.fn().mockImplementation((fn, args) => fn(args));
|
|
499
|
+
toolsService.setFunction("wrappableTool", originalFunction);
|
|
500
|
+
toolsService.registerWrapper("wrappableTool", wrapper1, 1);
|
|
501
|
+
toolsService.registerWrapper("wrappableTool", wrapper2, 2);
|
|
502
|
+
const func = toolsService.getFunction("wrappableTool");
|
|
503
|
+
expect(func).not.toBe(originalFunction);
|
|
504
|
+
expect(func).not.toBe(wrapper1);
|
|
505
|
+
});
|
|
506
|
+
it("should support multiple wrapper chaining", async () => {
|
|
507
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
508
|
+
const wrapper1 = jest
|
|
509
|
+
.fn()
|
|
510
|
+
.mockImplementation(async (originalFn, args) => {
|
|
511
|
+
const result = await originalFn(args);
|
|
512
|
+
return `wrapper1(${result})`;
|
|
513
|
+
});
|
|
514
|
+
const wrapper2 = jest
|
|
515
|
+
.fn()
|
|
516
|
+
.mockImplementation(async (originalFn, args) => {
|
|
517
|
+
const result = await originalFn(args);
|
|
518
|
+
return `wrapper2(${result})`;
|
|
519
|
+
});
|
|
520
|
+
toolsService.setFunction("wrappableTool", originalFunction);
|
|
521
|
+
toolsService.registerWrapper("wrappableTool", wrapper1, 1);
|
|
522
|
+
toolsService.registerWrapper("wrappableTool", wrapper2, 2);
|
|
523
|
+
const toolCall = {
|
|
524
|
+
id: "call_chained",
|
|
525
|
+
type: "function",
|
|
526
|
+
function: {
|
|
527
|
+
name: "wrappableTool",
|
|
528
|
+
arguments: JSON.stringify({ input: "test" }),
|
|
529
|
+
},
|
|
530
|
+
};
|
|
531
|
+
const result = await toolsService.callTool(toolCall);
|
|
532
|
+
expect(result.functionResp).toContain("wrapper1");
|
|
533
|
+
expect(result.functionResp).toContain("wrapper2");
|
|
534
|
+
expect(originalFunction).toHaveBeenCalled();
|
|
535
|
+
});
|
|
536
|
+
it("should remove wrappers", async () => {
|
|
537
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
538
|
+
const wrapperFunction = jest
|
|
539
|
+
.fn()
|
|
540
|
+
.mockImplementation((fn, args) => fn(args));
|
|
541
|
+
toolsService.setFunction("wrappableTool", originalFunction);
|
|
542
|
+
toolsService.registerWrapper("wrappableTool", wrapperFunction);
|
|
543
|
+
expect(toolsService.getFunction("wrappableTool")).not.toBe(originalFunction);
|
|
544
|
+
toolsService.removeWrapper("wrappableTool");
|
|
545
|
+
const restoredFunc = toolsService.getFunction("wrappableTool");
|
|
546
|
+
expect(await restoredFunc({})).toBe("original");
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
describe("Pattern Matching", () => {
|
|
550
|
+
it("should match glob patterns with wildcards", async () => {
|
|
551
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
552
|
+
const overrideFunction = jest.fn().mockResolvedValue("override");
|
|
553
|
+
toolsService.setFunction("testTool", originalFunction);
|
|
554
|
+
toolsService.setFunction("testHelper", originalFunction);
|
|
555
|
+
toolsService.setFunction("otherTool", originalFunction);
|
|
556
|
+
toolsService.registerOverride("test*", overrideFunction);
|
|
557
|
+
const testFunc = toolsService.getFunction("testTool");
|
|
558
|
+
const testHelperFunc = toolsService.getFunction("testHelper");
|
|
559
|
+
const otherFunc = toolsService.getFunction("otherTool");
|
|
560
|
+
expect(await testFunc({})).toBe("override");
|
|
561
|
+
expect(await testHelperFunc({})).toBe("override");
|
|
562
|
+
expect(await otherFunc({})).toBe("original");
|
|
563
|
+
});
|
|
564
|
+
it("should match regex patterns", async () => {
|
|
565
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
566
|
+
const overrideFunction = jest.fn().mockResolvedValue("override");
|
|
567
|
+
toolsService.setFunction("tool123", originalFunction);
|
|
568
|
+
toolsService.setFunction("tool456", originalFunction);
|
|
569
|
+
toolsService.setFunction("toolABC", originalFunction);
|
|
570
|
+
toolsService.registerOverride(/tool\d+$/, overrideFunction);
|
|
571
|
+
const tool123Func = toolsService.getFunction("tool123");
|
|
572
|
+
const tool456Func = toolsService.getFunction("tool456");
|
|
573
|
+
const toolABCFunc = toolsService.getFunction("toolABC");
|
|
574
|
+
expect(await tool123Func({})).toBe("override");
|
|
575
|
+
expect(await tool456Func({})).toBe("override");
|
|
576
|
+
expect(await toolABCFunc({})).toBe("original");
|
|
577
|
+
});
|
|
578
|
+
it("should handle complex glob patterns", async () => {
|
|
579
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
580
|
+
const overrideFunction = jest.fn().mockResolvedValue("override");
|
|
581
|
+
toolsService.setFunction("api_get_user", originalFunction);
|
|
582
|
+
toolsService.setFunction("api_post_user", originalFunction);
|
|
583
|
+
toolsService.setFunction("api_delete_user", originalFunction);
|
|
584
|
+
toolsService.setFunction("util_format", originalFunction);
|
|
585
|
+
toolsService.registerOverride("api_*_user", overrideFunction);
|
|
586
|
+
const apiGetFunc = toolsService.getFunction("api_get_user");
|
|
587
|
+
const apiPostFunc = toolsService.getFunction("api_post_user");
|
|
588
|
+
const apiDeleteFunc = toolsService.getFunction("api_delete_user");
|
|
589
|
+
expect(await apiGetFunc({})).toBe("override");
|
|
590
|
+
expect(await apiPostFunc({})).toBe("override");
|
|
591
|
+
expect(await apiDeleteFunc({})).toBe("override");
|
|
592
|
+
const utilFormatFunc = toolsService.getFunction("util_format");
|
|
593
|
+
expect(await utilFormatFunc({})).toBe("original");
|
|
594
|
+
});
|
|
595
|
+
it("should test pattern matcher factory function directly", () => {
|
|
596
|
+
const stringMatcher = (0, types_1.createPatternMatcher)("test*");
|
|
597
|
+
expect(stringMatcher.matches("testTool")).toBe(true);
|
|
598
|
+
expect(stringMatcher.matches("otherTool")).toBe(false);
|
|
599
|
+
const regexMatcher = (0, types_1.createPatternMatcher)(/^api_/);
|
|
600
|
+
expect(regexMatcher.matches("api_call")).toBe(true);
|
|
601
|
+
expect(regexMatcher.matches("util_call")).toBe(false);
|
|
602
|
+
});
|
|
603
|
+
});
|
|
604
|
+
describe("Error Handling and Edge Cases", () => {
|
|
605
|
+
it("should handle malformed tool call arguments", async () => {
|
|
606
|
+
const mockTool = {
|
|
607
|
+
type: "function",
|
|
608
|
+
function: {
|
|
609
|
+
name: "testTool",
|
|
610
|
+
parameters: { type: "object", properties: {}, required: [] },
|
|
611
|
+
},
|
|
612
|
+
};
|
|
613
|
+
const malformedCall = {
|
|
614
|
+
id: "call_malformed",
|
|
615
|
+
type: "function",
|
|
616
|
+
function: {
|
|
617
|
+
name: "testTool",
|
|
618
|
+
arguments: "invalid json {",
|
|
619
|
+
},
|
|
620
|
+
};
|
|
621
|
+
toolsService.addTool(mockTool);
|
|
622
|
+
toolsService.setFunction("testTool", jest.fn().mockResolvedValue("result"));
|
|
623
|
+
const result = await toolsService.callTool(malformedCall);
|
|
624
|
+
expect(result.toolMessages[0].name).toBe("error");
|
|
625
|
+
expect(result.toolMessages[0].content).toContain("JSON");
|
|
626
|
+
});
|
|
627
|
+
it("should handle tools with no parameters", async () => {
|
|
628
|
+
const noParamTool = {
|
|
629
|
+
type: "function",
|
|
630
|
+
function: {
|
|
631
|
+
name: "noParamTool",
|
|
632
|
+
description: "Tool with no parameters",
|
|
633
|
+
parameters: {
|
|
634
|
+
type: "object",
|
|
635
|
+
properties: {},
|
|
636
|
+
required: [],
|
|
637
|
+
},
|
|
638
|
+
},
|
|
639
|
+
};
|
|
640
|
+
const noParamCall = {
|
|
641
|
+
id: "call_no_param",
|
|
642
|
+
type: "function",
|
|
643
|
+
function: {
|
|
644
|
+
name: "noParamTool",
|
|
645
|
+
arguments: "{}",
|
|
646
|
+
},
|
|
647
|
+
};
|
|
648
|
+
const mockFunction = jest.fn().mockResolvedValue("no param result");
|
|
649
|
+
toolsService.addTool(noParamTool);
|
|
650
|
+
toolsService.setFunction("noParamTool", mockFunction);
|
|
651
|
+
const result = await toolsService.callTool(noParamCall);
|
|
652
|
+
expect(mockFunction).toHaveBeenCalledWith({});
|
|
653
|
+
expect(result.functionResp).toBe("no param result");
|
|
654
|
+
});
|
|
655
|
+
it("should handle tools with complex nested parameter structures", async () => {
|
|
656
|
+
const complexTool = {
|
|
657
|
+
type: "function",
|
|
658
|
+
function: {
|
|
659
|
+
name: "complexTool",
|
|
660
|
+
parameters: {
|
|
661
|
+
type: "object",
|
|
662
|
+
properties: {
|
|
663
|
+
config: {
|
|
664
|
+
type: "object",
|
|
665
|
+
properties: {
|
|
666
|
+
settings: {
|
|
667
|
+
type: "array",
|
|
668
|
+
items: {
|
|
669
|
+
type: "object",
|
|
670
|
+
properties: {
|
|
671
|
+
key: { type: "string" },
|
|
672
|
+
value: { type: "number" },
|
|
673
|
+
},
|
|
674
|
+
},
|
|
675
|
+
},
|
|
676
|
+
},
|
|
677
|
+
},
|
|
678
|
+
},
|
|
679
|
+
required: ["config"],
|
|
680
|
+
},
|
|
681
|
+
},
|
|
682
|
+
};
|
|
683
|
+
const complexCall = {
|
|
684
|
+
id: "call_complex",
|
|
685
|
+
type: "function",
|
|
686
|
+
function: {
|
|
687
|
+
name: "complexTool",
|
|
688
|
+
arguments: JSON.stringify({
|
|
689
|
+
config: {
|
|
690
|
+
settings: [
|
|
691
|
+
{ key: "timeout", value: 5000 },
|
|
692
|
+
{ key: "retries", value: 3 },
|
|
693
|
+
],
|
|
694
|
+
},
|
|
695
|
+
}),
|
|
696
|
+
},
|
|
697
|
+
};
|
|
698
|
+
const mockFunction = jest.fn().mockResolvedValue("complex result");
|
|
699
|
+
toolsService.addTool(complexTool);
|
|
700
|
+
toolsService.setFunction("complexTool", mockFunction);
|
|
701
|
+
const result = await toolsService.callTool(complexCall);
|
|
702
|
+
expect(mockFunction).toHaveBeenCalledWith({
|
|
703
|
+
config: {
|
|
704
|
+
settings: [
|
|
705
|
+
{ key: "timeout", value: 5000 },
|
|
706
|
+
{ key: "retries", value: 3 },
|
|
707
|
+
],
|
|
708
|
+
},
|
|
709
|
+
});
|
|
710
|
+
expect(result.functionResp).toBe("complex result");
|
|
711
|
+
});
|
|
712
|
+
it("should handle concurrent tool calls", async () => {
|
|
713
|
+
const concurrentTool = {
|
|
714
|
+
type: "function",
|
|
715
|
+
function: {
|
|
716
|
+
name: "concurrentTool",
|
|
717
|
+
parameters: {
|
|
718
|
+
type: "object",
|
|
719
|
+
properties: {
|
|
720
|
+
delay: { type: "number" },
|
|
721
|
+
},
|
|
722
|
+
required: ["delay"],
|
|
723
|
+
},
|
|
724
|
+
},
|
|
725
|
+
};
|
|
726
|
+
const mockFunction = jest.fn().mockImplementation(async ({ delay }) => {
|
|
727
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
728
|
+
return `completed after ${delay}ms`;
|
|
729
|
+
});
|
|
730
|
+
toolsService.addTool(concurrentTool);
|
|
731
|
+
toolsService.setFunction("concurrentTool", mockFunction);
|
|
732
|
+
const call1 = {
|
|
733
|
+
id: "call_1",
|
|
734
|
+
type: "function",
|
|
735
|
+
function: {
|
|
736
|
+
name: "concurrentTool",
|
|
737
|
+
arguments: JSON.stringify({ delay: 100 }),
|
|
738
|
+
},
|
|
739
|
+
};
|
|
740
|
+
const call2 = {
|
|
741
|
+
id: "call_2",
|
|
742
|
+
type: "function",
|
|
743
|
+
function: {
|
|
744
|
+
name: "concurrentTool",
|
|
745
|
+
arguments: JSON.stringify({ delay: 50 }),
|
|
746
|
+
},
|
|
747
|
+
};
|
|
748
|
+
const [result1, result2] = await Promise.all([
|
|
749
|
+
toolsService.callTool(call1),
|
|
750
|
+
toolsService.callTool(call2),
|
|
751
|
+
]);
|
|
752
|
+
expect(result1.functionResp).toBe("completed after 100ms");
|
|
753
|
+
expect(result2.functionResp).toBe("completed after 50ms");
|
|
754
|
+
expect(mockFunction).toHaveBeenCalledTimes(2);
|
|
755
|
+
});
|
|
756
|
+
it("should handle undefined and null function responses", async () => {
|
|
757
|
+
const undefinedTool = {
|
|
758
|
+
type: "function",
|
|
759
|
+
function: {
|
|
760
|
+
name: "undefinedTool",
|
|
761
|
+
parameters: { type: "object", properties: {}, required: [] },
|
|
762
|
+
},
|
|
763
|
+
};
|
|
764
|
+
const undefinedCall = {
|
|
765
|
+
id: "call_undefined",
|
|
766
|
+
type: "function",
|
|
767
|
+
function: {
|
|
768
|
+
name: "undefinedTool",
|
|
769
|
+
arguments: "{}",
|
|
770
|
+
},
|
|
771
|
+
};
|
|
772
|
+
toolsService.addTool(undefinedTool);
|
|
773
|
+
toolsService.setFunction("undefinedTool", jest.fn().mockResolvedValue(undefined));
|
|
774
|
+
let result = await toolsService.callTool(undefinedCall);
|
|
775
|
+
expect(result.toolMessages[0].content).toBe("undefined");
|
|
776
|
+
const nullTool = {
|
|
777
|
+
type: "function",
|
|
778
|
+
function: {
|
|
779
|
+
name: "nullTool",
|
|
780
|
+
parameters: { type: "object", properties: {}, required: [] },
|
|
781
|
+
},
|
|
782
|
+
};
|
|
783
|
+
const nullCall = {
|
|
784
|
+
id: "call_null",
|
|
785
|
+
type: "function",
|
|
786
|
+
function: {
|
|
787
|
+
name: "nullTool",
|
|
788
|
+
arguments: "{}",
|
|
789
|
+
},
|
|
790
|
+
};
|
|
791
|
+
toolsService.addTool(nullTool);
|
|
792
|
+
toolsService.setFunction("nullTool", jest.fn().mockResolvedValue(null));
|
|
793
|
+
result = await toolsService.callTool(nullCall);
|
|
794
|
+
expect(result.toolMessages[0].content).toBe("null");
|
|
795
|
+
});
|
|
796
|
+
});
|
|
797
|
+
describe("Integration Tests", () => {
|
|
798
|
+
it("should handle complete workflow with override and wrapper", async () => {
|
|
799
|
+
const baseTool = {
|
|
800
|
+
type: "function",
|
|
801
|
+
function: {
|
|
802
|
+
name: "workflowTool",
|
|
803
|
+
parameters: {
|
|
804
|
+
type: "object",
|
|
805
|
+
properties: {
|
|
806
|
+
input: { type: "string" },
|
|
807
|
+
},
|
|
808
|
+
required: ["input"],
|
|
809
|
+
},
|
|
810
|
+
},
|
|
811
|
+
};
|
|
812
|
+
const originalFunction = jest.fn().mockResolvedValue("original");
|
|
813
|
+
const wrapperFunction = jest
|
|
814
|
+
.fn()
|
|
815
|
+
.mockImplementation(async (originalFn, args) => {
|
|
816
|
+
const result = await originalFn(args);
|
|
817
|
+
return `wrapped: ${result}`;
|
|
818
|
+
});
|
|
819
|
+
const overrideFunction = jest.fn().mockResolvedValue("override");
|
|
820
|
+
toolsService.addTool(baseTool);
|
|
821
|
+
toolsService.setFunction("workflowTool", originalFunction);
|
|
822
|
+
toolsService.registerWrapper("workflowTool", wrapperFunction);
|
|
823
|
+
toolsService.registerOverride("workflowTool", overrideFunction);
|
|
824
|
+
const toolCall = {
|
|
825
|
+
id: "call_workflow",
|
|
826
|
+
type: "function",
|
|
827
|
+
function: {
|
|
828
|
+
name: "workflowTool",
|
|
829
|
+
arguments: JSON.stringify({ input: "test" }),
|
|
830
|
+
},
|
|
831
|
+
};
|
|
832
|
+
const result = await toolsService.callTool(toolCall);
|
|
833
|
+
expect(overrideFunction).toHaveBeenCalled();
|
|
834
|
+
expect(wrapperFunction).not.toHaveBeenCalled();
|
|
835
|
+
expect(originalFunction).not.toHaveBeenCalled();
|
|
836
|
+
expect(result.functionResp).toBe("override");
|
|
837
|
+
});
|
|
838
|
+
it("should maintain tool state across multiple operations", () => {
|
|
839
|
+
const tools = [
|
|
840
|
+
{
|
|
841
|
+
type: "function",
|
|
842
|
+
function: {
|
|
843
|
+
name: "tool1",
|
|
844
|
+
parameters: { type: "object", properties: {}, required: [] },
|
|
845
|
+
},
|
|
846
|
+
},
|
|
847
|
+
{
|
|
848
|
+
type: "function",
|
|
849
|
+
function: {
|
|
850
|
+
name: "tool2",
|
|
851
|
+
parameters: { type: "object", properties: {}, required: [] },
|
|
852
|
+
},
|
|
853
|
+
},
|
|
854
|
+
];
|
|
855
|
+
toolsService.addTools(tools);
|
|
856
|
+
expect(toolsService.getToolsByNames(["tool1", "tool2"])).toHaveLength(2);
|
|
857
|
+
toolsService.setFunction("tool1", jest.fn());
|
|
858
|
+
toolsService.setFunction("tool2", jest.fn());
|
|
859
|
+
expect(toolsService.getFunction("tool1")).toBeDefined();
|
|
860
|
+
expect(toolsService.getFunction("tool2")).toBeDefined();
|
|
861
|
+
const newService = new Tools_1.ToolsService();
|
|
862
|
+
newService.copyToolsFrom(["tool1", "tool2"], toolsService);
|
|
863
|
+
expect(newService.getToolsByNames(["tool1", "tool2"])).toHaveLength(2);
|
|
864
|
+
});
|
|
865
|
+
describe("Function Binding", () => {
|
|
866
|
+
let toolsService;
|
|
867
|
+
let mockContext;
|
|
868
|
+
beforeEach(() => {
|
|
869
|
+
mockContext = {
|
|
870
|
+
metadata: { testKey: "testValue" },
|
|
871
|
+
};
|
|
872
|
+
toolsService = new Tools_1.ToolsService(mockContext);
|
|
873
|
+
});
|
|
874
|
+
describe("this binding in tool functions", () => {
|
|
875
|
+
it("should bind functions so 'this' refers to ToolsService instance", async () => {
|
|
876
|
+
const testTool = {
|
|
877
|
+
type: "function",
|
|
878
|
+
function: {
|
|
879
|
+
name: "testThisBinding",
|
|
880
|
+
description: "Test function that accesses this",
|
|
881
|
+
parameters: {
|
|
882
|
+
type: "object",
|
|
883
|
+
properties: {
|
|
884
|
+
message: { type: "string" },
|
|
885
|
+
},
|
|
886
|
+
},
|
|
887
|
+
},
|
|
888
|
+
};
|
|
889
|
+
function testFunction(args) {
|
|
890
|
+
if (!this || typeof this.getContext !== "function") {
|
|
891
|
+
throw new Error("'this' is not bound to ToolsService instance");
|
|
892
|
+
}
|
|
893
|
+
const context = this.getContext();
|
|
894
|
+
return {
|
|
895
|
+
message: args.message,
|
|
896
|
+
contextMetadata: context.metadata,
|
|
897
|
+
hasThisBinding: true,
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
toolsService.addTool(testTool);
|
|
901
|
+
toolsService.setFunction("testThisBinding", testFunction);
|
|
902
|
+
const toolCall = {
|
|
903
|
+
id: "test-call-1",
|
|
904
|
+
type: "function",
|
|
905
|
+
function: {
|
|
906
|
+
name: "testThisBinding",
|
|
907
|
+
arguments: JSON.stringify({ message: "test" }),
|
|
908
|
+
},
|
|
909
|
+
};
|
|
910
|
+
const result = await toolsService.callTool(toolCall);
|
|
911
|
+
expect(result.functionResp.hasThisBinding).toBe(true);
|
|
912
|
+
expect(result.functionResp.contextMetadata).toEqual({
|
|
913
|
+
testKey: "testValue",
|
|
914
|
+
});
|
|
915
|
+
expect(result.functionResp.message).toBe("test");
|
|
916
|
+
});
|
|
917
|
+
it("should allow tool functions to call other tools via this.callTool", async () => {
|
|
918
|
+
const helperTool = {
|
|
919
|
+
type: "function",
|
|
920
|
+
function: {
|
|
921
|
+
name: "helperTool",
|
|
922
|
+
description: "Helper tool",
|
|
923
|
+
parameters: {
|
|
924
|
+
type: "object",
|
|
925
|
+
properties: {
|
|
926
|
+
value: { type: "string" },
|
|
927
|
+
},
|
|
928
|
+
},
|
|
929
|
+
},
|
|
930
|
+
};
|
|
931
|
+
const mainTool = {
|
|
932
|
+
type: "function",
|
|
933
|
+
function: {
|
|
934
|
+
name: "mainTool",
|
|
935
|
+
description: "Main tool that calls helper",
|
|
936
|
+
parameters: {
|
|
937
|
+
type: "object",
|
|
938
|
+
properties: {
|
|
939
|
+
input: { type: "string" },
|
|
940
|
+
},
|
|
941
|
+
},
|
|
942
|
+
},
|
|
943
|
+
};
|
|
944
|
+
function helperFunction(args) {
|
|
945
|
+
return `Helper processed: ${args.value}`;
|
|
946
|
+
}
|
|
947
|
+
async function mainFunction(args) {
|
|
948
|
+
if (!this || typeof this.callTool !== "function") {
|
|
949
|
+
throw new Error("'this' is not bound to ToolsService instance");
|
|
950
|
+
}
|
|
951
|
+
const helperCall = {
|
|
952
|
+
id: "helper-call",
|
|
953
|
+
type: "function",
|
|
954
|
+
function: {
|
|
955
|
+
name: "helperTool",
|
|
956
|
+
arguments: JSON.stringify({ value: args.input }),
|
|
957
|
+
},
|
|
958
|
+
};
|
|
959
|
+
const helperResult = await this.callTool(helperCall);
|
|
960
|
+
return `Main tool result: ${helperResult.functionResp}`;
|
|
961
|
+
}
|
|
962
|
+
toolsService.addTool(helperTool);
|
|
963
|
+
toolsService.addTool(mainTool);
|
|
964
|
+
toolsService.setFunction("helperTool", helperFunction);
|
|
965
|
+
toolsService.setFunction("mainTool", mainFunction);
|
|
966
|
+
const toolCall = {
|
|
967
|
+
id: "test-call-2",
|
|
968
|
+
type: "function",
|
|
969
|
+
function: {
|
|
970
|
+
name: "mainTool",
|
|
971
|
+
arguments: JSON.stringify({ input: "test data" }),
|
|
972
|
+
},
|
|
973
|
+
};
|
|
974
|
+
const result = await toolsService.callTool(toolCall);
|
|
975
|
+
expect(result.functionResp).toBe("Main tool result: Helper processed: test data");
|
|
976
|
+
});
|
|
977
|
+
it("should maintain binding through overrides", async () => {
|
|
978
|
+
const testTool = {
|
|
979
|
+
type: "function",
|
|
980
|
+
function: {
|
|
981
|
+
name: "bindingTestTool",
|
|
982
|
+
description: "Test tool for binding with overrides",
|
|
983
|
+
parameters: {
|
|
984
|
+
type: "object",
|
|
985
|
+
properties: {
|
|
986
|
+
data: { type: "string" },
|
|
987
|
+
},
|
|
988
|
+
},
|
|
989
|
+
},
|
|
990
|
+
};
|
|
991
|
+
function originalFunction(args) {
|
|
992
|
+
if (!this || typeof this.getContext !== "function") {
|
|
993
|
+
throw new Error("'this' is not bound to ToolsService instance in original");
|
|
994
|
+
}
|
|
995
|
+
return `Original: ${args.data}`;
|
|
996
|
+
}
|
|
997
|
+
const overrideFunction = function (originalArgs, originalTool) {
|
|
998
|
+
if (!this || typeof this.getContext !== "function") {
|
|
999
|
+
throw new Error("'this' is not bound to ToolsService instance in override");
|
|
1000
|
+
}
|
|
1001
|
+
return `Override: ${originalArgs[0].data}`;
|
|
1002
|
+
};
|
|
1003
|
+
toolsService.addTool(testTool);
|
|
1004
|
+
toolsService.setFunction("bindingTestTool", originalFunction);
|
|
1005
|
+
toolsService.registerOverride("bindingTestTool", overrideFunction);
|
|
1006
|
+
const toolCall = {
|
|
1007
|
+
id: "test-call-3",
|
|
1008
|
+
type: "function",
|
|
1009
|
+
function: {
|
|
1010
|
+
name: "bindingTestTool",
|
|
1011
|
+
arguments: JSON.stringify({ data: "test" }),
|
|
1012
|
+
},
|
|
1013
|
+
};
|
|
1014
|
+
const result = await toolsService.callTool(toolCall);
|
|
1015
|
+
expect(result.functionResp).toBe("Override: test");
|
|
1016
|
+
});
|
|
1017
|
+
it("should maintain binding through wrappers", async () => {
|
|
1018
|
+
const testTool = {
|
|
1019
|
+
type: "function",
|
|
1020
|
+
function: {
|
|
1021
|
+
name: "wrapperBindingTest",
|
|
1022
|
+
description: "Test tool for binding with wrappers",
|
|
1023
|
+
parameters: {
|
|
1024
|
+
type: "object",
|
|
1025
|
+
properties: {
|
|
1026
|
+
value: { type: "string" },
|
|
1027
|
+
},
|
|
1028
|
+
},
|
|
1029
|
+
},
|
|
1030
|
+
};
|
|
1031
|
+
function baseFunction(args) {
|
|
1032
|
+
if (!this || typeof this.getContext !== "function") {
|
|
1033
|
+
throw new Error("'this' is not bound to ToolsService instance in base function");
|
|
1034
|
+
}
|
|
1035
|
+
return `Base: ${args.value}`;
|
|
1036
|
+
}
|
|
1037
|
+
const wrapper = (originalFunc, args, tool) => {
|
|
1038
|
+
const result = originalFunc.call(toolsService, args);
|
|
1039
|
+
return `Wrapped: ${result}`;
|
|
1040
|
+
};
|
|
1041
|
+
toolsService.addTool(testTool);
|
|
1042
|
+
toolsService.setFunction("wrapperBindingTest", baseFunction);
|
|
1043
|
+
toolsService.registerWrapper("wrapperBindingTest", wrapper);
|
|
1044
|
+
const toolCall = {
|
|
1045
|
+
id: "test-call-4",
|
|
1046
|
+
type: "function",
|
|
1047
|
+
function: {
|
|
1048
|
+
name: "wrapperBindingTest",
|
|
1049
|
+
arguments: JSON.stringify({ value: "test" }),
|
|
1050
|
+
},
|
|
1051
|
+
};
|
|
1052
|
+
const result = await toolsService.callTool(toolCall);
|
|
1053
|
+
expect(result.functionResp).toBe("Wrapped: Base: test");
|
|
1054
|
+
});
|
|
1055
|
+
});
|
|
1056
|
+
});
|
|
1057
|
+
});
|
|
1058
|
+
});
|
|
1059
|
+
//# sourceMappingURL=Tools.test.js.map
|