@mcp-abap-adt/llm-agent-server 10.0.0
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/README.md +13 -0
- package/bin/llm-agent +2 -0
- package/bin/llm-agent-check +2 -0
- package/dist/agent.d.ts +49 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +97 -0
- package/dist/agent.js.map +1 -0
- package/dist/agents/anthropic-agent.d.ts +33 -0
- package/dist/agents/anthropic-agent.d.ts.map +1 -0
- package/dist/agents/anthropic-agent.js +138 -0
- package/dist/agents/anthropic-agent.js.map +1 -0
- package/dist/agents/base.d.ts +90 -0
- package/dist/agents/base.d.ts.map +1 -0
- package/dist/agents/base.js +107 -0
- package/dist/agents/base.js.map +1 -0
- package/dist/agents/deepseek-agent.d.ts +31 -0
- package/dist/agents/deepseek-agent.d.ts.map +1 -0
- package/dist/agents/deepseek-agent.js +121 -0
- package/dist/agents/deepseek-agent.js.map +1 -0
- package/dist/agents/index.d.ts +10 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +10 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/openai-agent.d.ts +32 -0
- package/dist/agents/openai-agent.d.ts.map +1 -0
- package/dist/agents/openai-agent.js +122 -0
- package/dist/agents/openai-agent.js.map +1 -0
- package/dist/agents/prompt-based-agent.d.ts +29 -0
- package/dist/agents/prompt-based-agent.d.ts.map +1 -0
- package/dist/agents/prompt-based-agent.js +67 -0
- package/dist/agents/prompt-based-agent.js.map +1 -0
- package/dist/agents/sap-core-ai-agent.d.ts +41 -0
- package/dist/agents/sap-core-ai-agent.d.ts.map +1 -0
- package/dist/agents/sap-core-ai-agent.js +151 -0
- package/dist/agents/sap-core-ai-agent.js.map +1 -0
- package/dist/cli.d.ts +53 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +338 -0
- package/dist/cli.js.map +1 -0
- package/dist/generated/version.d.ts +2 -0
- package/dist/generated/version.d.ts.map +1 -0
- package/dist/generated/version.js +3 -0
- package/dist/generated/version.js.map +1 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -0
- package/dist/llm-providers/anthropic.d.ts +25 -0
- package/dist/llm-providers/anthropic.d.ts.map +1 -0
- package/dist/llm-providers/anthropic.js +202 -0
- package/dist/llm-providers/anthropic.js.map +1 -0
- package/dist/llm-providers/base.d.ts +33 -0
- package/dist/llm-providers/base.d.ts.map +1 -0
- package/dist/llm-providers/base.js +18 -0
- package/dist/llm-providers/base.js.map +1 -0
- package/dist/llm-providers/deepseek.d.ts +24 -0
- package/dist/llm-providers/deepseek.d.ts.map +1 -0
- package/dist/llm-providers/deepseek.js +60 -0
- package/dist/llm-providers/deepseek.js.map +1 -0
- package/dist/llm-providers/index.d.ts +13 -0
- package/dist/llm-providers/index.d.ts.map +1 -0
- package/dist/llm-providers/index.js +15 -0
- package/dist/llm-providers/index.js.map +1 -0
- package/dist/llm-providers/openai.d.ts +34 -0
- package/dist/llm-providers/openai.d.ts.map +1 -0
- package/dist/llm-providers/openai.js +193 -0
- package/dist/llm-providers/openai.js.map +1 -0
- package/dist/llm-providers/sap-core-ai.d.ts +90 -0
- package/dist/llm-providers/sap-core-ai.d.ts.map +1 -0
- package/dist/llm-providers/sap-core-ai.js +394 -0
- package/dist/llm-providers/sap-core-ai.js.map +1 -0
- package/dist/mcp/client.d.ts +133 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +314 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/smart-agent/adapters/cline-client-adapter.d.ts +18 -0
- package/dist/smart-agent/adapters/cline-client-adapter.d.ts.map +1 -0
- package/dist/smart-agent/adapters/cline-client-adapter.js +21 -0
- package/dist/smart-agent/adapters/cline-client-adapter.js.map +1 -0
- package/dist/smart-agent/adapters/index.d.ts +3 -0
- package/dist/smart-agent/adapters/index.d.ts.map +1 -0
- package/dist/smart-agent/adapters/index.js +3 -0
- package/dist/smart-agent/adapters/index.js.map +1 -0
- package/dist/smart-agent/adapters/llm-adapter.d.ts +24 -0
- package/dist/smart-agent/adapters/llm-adapter.d.ts.map +1 -0
- package/dist/smart-agent/adapters/llm-adapter.js +345 -0
- package/dist/smart-agent/adapters/llm-adapter.js.map +1 -0
- package/dist/smart-agent/adapters/mcp-client-adapter.d.ts +16 -0
- package/dist/smart-agent/adapters/mcp-client-adapter.d.ts.map +1 -0
- package/dist/smart-agent/adapters/mcp-client-adapter.js +95 -0
- package/dist/smart-agent/adapters/mcp-client-adapter.js.map +1 -0
- package/dist/smart-agent/adapters/non-streaming-llm.d.ts +18 -0
- package/dist/smart-agent/adapters/non-streaming-llm.d.ts.map +1 -0
- package/dist/smart-agent/adapters/non-streaming-llm.js +44 -0
- package/dist/smart-agent/adapters/non-streaming-llm.js.map +1 -0
- package/dist/smart-agent/agent.d.ts +229 -0
- package/dist/smart-agent/agent.d.ts.map +1 -0
- package/dist/smart-agent/agent.js +1538 -0
- package/dist/smart-agent/agent.js.map +1 -0
- package/dist/smart-agent/api-adapters/anthropic-adapter.d.ts +10 -0
- package/dist/smart-agent/api-adapters/anthropic-adapter.d.ts.map +1 -0
- package/dist/smart-agent/api-adapters/anthropic-adapter.js +356 -0
- package/dist/smart-agent/api-adapters/anthropic-adapter.js.map +1 -0
- package/dist/smart-agent/api-adapters/index.d.ts +3 -0
- package/dist/smart-agent/api-adapters/index.d.ts.map +1 -0
- package/dist/smart-agent/api-adapters/index.js +3 -0
- package/dist/smart-agent/api-adapters/index.js.map +1 -0
- package/dist/smart-agent/api-adapters/openai-adapter.d.ts +10 -0
- package/dist/smart-agent/api-adapters/openai-adapter.d.ts.map +1 -0
- package/dist/smart-agent/api-adapters/openai-adapter.js +277 -0
- package/dist/smart-agent/api-adapters/openai-adapter.js.map +1 -0
- package/dist/smart-agent/builder.d.ts +281 -0
- package/dist/smart-agent/builder.d.ts.map +1 -0
- package/dist/smart-agent/builder.js +942 -0
- package/dist/smart-agent/builder.js.map +1 -0
- package/dist/smart-agent/cache/index.d.ts +4 -0
- package/dist/smart-agent/cache/index.d.ts.map +1 -0
- package/dist/smart-agent/cache/index.js +3 -0
- package/dist/smart-agent/cache/index.js.map +1 -0
- package/dist/smart-agent/cache/noop-tool-cache.d.ts +7 -0
- package/dist/smart-agent/cache/noop-tool-cache.d.ts.map +1 -0
- package/dist/smart-agent/cache/noop-tool-cache.js +8 -0
- package/dist/smart-agent/cache/noop-tool-cache.js.map +1 -0
- package/dist/smart-agent/cache/tool-cache.d.ts +14 -0
- package/dist/smart-agent/cache/tool-cache.d.ts.map +1 -0
- package/dist/smart-agent/cache/tool-cache.js +32 -0
- package/dist/smart-agent/cache/tool-cache.js.map +1 -0
- package/dist/smart-agent/cache/types.d.ts +7 -0
- package/dist/smart-agent/cache/types.d.ts.map +1 -0
- package/dist/smart-agent/cache/types.js +2 -0
- package/dist/smart-agent/cache/types.js.map +1 -0
- package/dist/smart-agent/check-models-cli.d.ts +14 -0
- package/dist/smart-agent/check-models-cli.d.ts.map +1 -0
- package/dist/smart-agent/check-models-cli.js +237 -0
- package/dist/smart-agent/check-models-cli.js.map +1 -0
- package/dist/smart-agent/classifier/index.d.ts +3 -0
- package/dist/smart-agent/classifier/index.d.ts.map +1 -0
- package/dist/smart-agent/classifier/index.js +2 -0
- package/dist/smart-agent/classifier/index.js.map +1 -0
- package/dist/smart-agent/classifier/llm-classifier.d.ts +20 -0
- package/dist/smart-agent/classifier/llm-classifier.d.ts.map +1 -0
- package/dist/smart-agent/classifier/llm-classifier.js +128 -0
- package/dist/smart-agent/classifier/llm-classifier.js.map +1 -0
- package/dist/smart-agent/cli.d.ts +65 -0
- package/dist/smart-agent/cli.d.ts.map +1 -0
- package/dist/smart-agent/cli.js +199 -0
- package/dist/smart-agent/cli.js.map +1 -0
- package/dist/smart-agent/config/config-watcher.d.ts +54 -0
- package/dist/smart-agent/config/config-watcher.d.ts.map +1 -0
- package/dist/smart-agent/config/config-watcher.js +107 -0
- package/dist/smart-agent/config/config-watcher.js.map +1 -0
- package/dist/smart-agent/config/index.d.ts +2 -0
- package/dist/smart-agent/config/index.d.ts.map +1 -0
- package/dist/smart-agent/config/index.js +2 -0
- package/dist/smart-agent/config/index.js.map +1 -0
- package/dist/smart-agent/config.d.ts +35 -0
- package/dist/smart-agent/config.d.ts.map +1 -0
- package/dist/smart-agent/config.js +360 -0
- package/dist/smart-agent/config.js.map +1 -0
- package/dist/smart-agent/context/context-assembler.d.ts +49 -0
- package/dist/smart-agent/context/context-assembler.d.ts.map +1 -0
- package/dist/smart-agent/context/context-assembler.js +184 -0
- package/dist/smart-agent/context/context-assembler.js.map +1 -0
- package/dist/smart-agent/context/index.d.ts +3 -0
- package/dist/smart-agent/context/index.d.ts.map +1 -0
- package/dist/smart-agent/context/index.js +2 -0
- package/dist/smart-agent/context/index.js.map +1 -0
- package/dist/smart-agent/health/health-checker.d.ts +21 -0
- package/dist/smart-agent/health/health-checker.d.ts.map +1 -0
- package/dist/smart-agent/health/health-checker.js +52 -0
- package/dist/smart-agent/health/health-checker.js.map +1 -0
- package/dist/smart-agent/health/index.d.ts +3 -0
- package/dist/smart-agent/health/index.d.ts.map +1 -0
- package/dist/smart-agent/health/index.js +2 -0
- package/dist/smart-agent/health/index.js.map +1 -0
- package/dist/smart-agent/health/types.d.ts +25 -0
- package/dist/smart-agent/health/types.d.ts.map +1 -0
- package/dist/smart-agent/health/types.js +2 -0
- package/dist/smart-agent/health/types.js.map +1 -0
- package/dist/smart-agent/history/history-memory.d.ts +12 -0
- package/dist/smart-agent/history/history-memory.d.ts.map +1 -0
- package/dist/smart-agent/history/history-memory.js +26 -0
- package/dist/smart-agent/history/history-memory.js.map +1 -0
- package/dist/smart-agent/history/history-summarizer.d.ts +10 -0
- package/dist/smart-agent/history/history-summarizer.d.ts.map +1 -0
- package/dist/smart-agent/history/history-summarizer.js +27 -0
- package/dist/smart-agent/history/history-summarizer.js.map +1 -0
- package/dist/smart-agent/history/index.d.ts +3 -0
- package/dist/smart-agent/history/index.d.ts.map +1 -0
- package/dist/smart-agent/history/index.js +3 -0
- package/dist/smart-agent/history/index.js.map +1 -0
- package/dist/smart-agent/interfaces/api-adapter.d.ts +36 -0
- package/dist/smart-agent/interfaces/api-adapter.d.ts.map +1 -0
- package/dist/smart-agent/interfaces/api-adapter.js +9 -0
- package/dist/smart-agent/interfaces/api-adapter.js.map +1 -0
- package/dist/smart-agent/interfaces/client-adapter.d.ts +23 -0
- package/dist/smart-agent/interfaces/client-adapter.d.ts.map +1 -0
- package/dist/smart-agent/interfaces/client-adapter.js +10 -0
- package/dist/smart-agent/interfaces/client-adapter.js.map +1 -0
- package/dist/smart-agent/interfaces/mcp-connection-strategy.d.ts +27 -0
- package/dist/smart-agent/interfaces/mcp-connection-strategy.d.ts.map +1 -0
- package/dist/smart-agent/interfaces/mcp-connection-strategy.js +2 -0
- package/dist/smart-agent/interfaces/mcp-connection-strategy.js.map +1 -0
- package/dist/smart-agent/interfaces/model-resolver.d.ts +9 -0
- package/dist/smart-agent/interfaces/model-resolver.d.ts.map +1 -0
- package/dist/smart-agent/interfaces/model-resolver.js +2 -0
- package/dist/smart-agent/interfaces/model-resolver.js.map +1 -0
- package/dist/smart-agent/interfaces/pipeline.d.ts +125 -0
- package/dist/smart-agent/interfaces/pipeline.d.ts.map +1 -0
- package/dist/smart-agent/interfaces/pipeline.js +13 -0
- package/dist/smart-agent/interfaces/pipeline.js.map +1 -0
- package/dist/smart-agent/logger/console-logger.d.ts +7 -0
- package/dist/smart-agent/logger/console-logger.d.ts.map +1 -0
- package/dist/smart-agent/logger/console-logger.js +12 -0
- package/dist/smart-agent/logger/console-logger.js.map +1 -0
- package/dist/smart-agent/logger/default-request-logger.d.ts +17 -0
- package/dist/smart-agent/logger/default-request-logger.d.ts.map +1 -0
- package/dist/smart-agent/logger/default-request-logger.js +86 -0
- package/dist/smart-agent/logger/default-request-logger.js.map +1 -0
- package/dist/smart-agent/logger/index.d.ts +3 -0
- package/dist/smart-agent/logger/index.d.ts.map +1 -0
- package/dist/smart-agent/logger/index.js +2 -0
- package/dist/smart-agent/logger/index.js.map +1 -0
- package/dist/smart-agent/logger/noop-request-logger.d.ts +11 -0
- package/dist/smart-agent/logger/noop-request-logger.d.ts.map +1 -0
- package/dist/smart-agent/logger/noop-request-logger.js +20 -0
- package/dist/smart-agent/logger/noop-request-logger.js.map +1 -0
- package/dist/smart-agent/logger/session-logger.d.ts +10 -0
- package/dist/smart-agent/logger/session-logger.d.ts.map +1 -0
- package/dist/smart-agent/logger/session-logger.js +40 -0
- package/dist/smart-agent/logger/session-logger.js.map +1 -0
- package/dist/smart-agent/logger/types.d.ts +63 -0
- package/dist/smart-agent/logger/types.d.ts.map +1 -0
- package/dist/smart-agent/logger/types.js +2 -0
- package/dist/smart-agent/logger/types.js.map +1 -0
- package/dist/smart-agent/mcp-client-factory.d.ts +3 -0
- package/dist/smart-agent/mcp-client-factory.d.ts.map +1 -0
- package/dist/smart-agent/mcp-client-factory.js +21 -0
- package/dist/smart-agent/mcp-client-factory.js.map +1 -0
- package/dist/smart-agent/metrics/in-memory-metrics.d.ts +58 -0
- package/dist/smart-agent/metrics/in-memory-metrics.d.ts.map +1 -0
- package/dist/smart-agent/metrics/in-memory-metrics.js +78 -0
- package/dist/smart-agent/metrics/in-memory-metrics.js.map +1 -0
- package/dist/smart-agent/metrics/index.d.ts +4 -0
- package/dist/smart-agent/metrics/index.d.ts.map +1 -0
- package/dist/smart-agent/metrics/index.js +3 -0
- package/dist/smart-agent/metrics/index.js.map +1 -0
- package/dist/smart-agent/metrics/noop-metrics.d.ts +17 -0
- package/dist/smart-agent/metrics/noop-metrics.d.ts.map +1 -0
- package/dist/smart-agent/metrics/noop-metrics.js +22 -0
- package/dist/smart-agent/metrics/noop-metrics.js.map +1 -0
- package/dist/smart-agent/metrics/types.d.ts +36 -0
- package/dist/smart-agent/metrics/types.d.ts.map +1 -0
- package/dist/smart-agent/metrics/types.js +9 -0
- package/dist/smart-agent/metrics/types.js.map +1 -0
- package/dist/smart-agent/otel/index.d.ts +2 -0
- package/dist/smart-agent/otel/index.d.ts.map +1 -0
- package/dist/smart-agent/otel/index.js +2 -0
- package/dist/smart-agent/otel/index.js.map +1 -0
- package/dist/smart-agent/otel/otel-tracer-adapter.d.ts +15 -0
- package/dist/smart-agent/otel/otel-tracer-adapter.d.ts.map +1 -0
- package/dist/smart-agent/otel/otel-tracer-adapter.js +62 -0
- package/dist/smart-agent/otel/otel-tracer-adapter.js.map +1 -0
- package/dist/smart-agent/pipeline/condition-evaluator.d.ts +35 -0
- package/dist/smart-agent/pipeline/condition-evaluator.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/condition-evaluator.js +143 -0
- package/dist/smart-agent/pipeline/condition-evaluator.js.map +1 -0
- package/dist/smart-agent/pipeline/context.d.ts +121 -0
- package/dist/smart-agent/pipeline/context.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/context.js +19 -0
- package/dist/smart-agent/pipeline/context.js.map +1 -0
- package/dist/smart-agent/pipeline/default-pipeline.d.ts +69 -0
- package/dist/smart-agent/pipeline/default-pipeline.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/default-pipeline.js +278 -0
- package/dist/smart-agent/pipeline/default-pipeline.js.map +1 -0
- package/dist/smart-agent/pipeline/executor.d.ts +54 -0
- package/dist/smart-agent/pipeline/executor.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/executor.js +133 -0
- package/dist/smart-agent/pipeline/executor.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/assemble.d.ts +16 -0
- package/dist/smart-agent/pipeline/handlers/assemble.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/assemble.js +72 -0
- package/dist/smart-agent/pipeline/handlers/assemble.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/build-tool-query.d.ts +30 -0
- package/dist/smart-agent/pipeline/handlers/build-tool-query.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/build-tool-query.js +80 -0
- package/dist/smart-agent/pipeline/handlers/build-tool-query.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/classify.d.ts +21 -0
- package/dist/smart-agent/pipeline/handlers/classify.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/classify.js +60 -0
- package/dist/smart-agent/pipeline/handlers/classify.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/expand.d.ts +16 -0
- package/dist/smart-agent/pipeline/handlers/expand.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/expand.js +24 -0
- package/dist/smart-agent/pipeline/handlers/expand.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/history-upsert.d.ts +29 -0
- package/dist/smart-agent/pipeline/handlers/history-upsert.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/history-upsert.js +75 -0
- package/dist/smart-agent/pipeline/handlers/history-upsert.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/index.d.ts +31 -0
- package/dist/smart-agent/pipeline/handlers/index.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/index.js +44 -0
- package/dist/smart-agent/pipeline/handlers/index.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/rag-query.d.ts +27 -0
- package/dist/smart-agent/pipeline/handlers/rag-query.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/rag-query.js +117 -0
- package/dist/smart-agent/pipeline/handlers/rag-query.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/rerank.d.ts +16 -0
- package/dist/smart-agent/pipeline/handlers/rerank.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/rerank.js +27 -0
- package/dist/smart-agent/pipeline/handlers/rerank.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/skill-select.d.ts +18 -0
- package/dist/smart-agent/pipeline/handlers/skill-select.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/skill-select.js +88 -0
- package/dist/smart-agent/pipeline/handlers/skill-select.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/summarize.d.ts +17 -0
- package/dist/smart-agent/pipeline/handlers/summarize.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/summarize.js +56 -0
- package/dist/smart-agent/pipeline/handlers/summarize.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/tool-loop.d.ts +32 -0
- package/dist/smart-agent/pipeline/handlers/tool-loop.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/tool-loop.js +734 -0
- package/dist/smart-agent/pipeline/handlers/tool-loop.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/tool-select.d.ts +21 -0
- package/dist/smart-agent/pipeline/handlers/tool-select.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/tool-select.js +109 -0
- package/dist/smart-agent/pipeline/handlers/tool-select.js.map +1 -0
- package/dist/smart-agent/pipeline/handlers/translate.d.ts +17 -0
- package/dist/smart-agent/pipeline/handlers/translate.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/handlers/translate.js +42 -0
- package/dist/smart-agent/pipeline/handlers/translate.js.map +1 -0
- package/dist/smart-agent/pipeline/index.d.ts +14 -0
- package/dist/smart-agent/pipeline/index.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/index.js +16 -0
- package/dist/smart-agent/pipeline/index.js.map +1 -0
- package/dist/smart-agent/pipeline/stage-handler.d.ts +36 -0
- package/dist/smart-agent/pipeline/stage-handler.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/stage-handler.js +24 -0
- package/dist/smart-agent/pipeline/stage-handler.js.map +1 -0
- package/dist/smart-agent/pipeline/types.d.ts +116 -0
- package/dist/smart-agent/pipeline/types.d.ts.map +1 -0
- package/dist/smart-agent/pipeline/types.js +54 -0
- package/dist/smart-agent/pipeline/types.js.map +1 -0
- package/dist/smart-agent/pipeline.d.ts +82 -0
- package/dist/smart-agent/pipeline.d.ts.map +1 -0
- package/dist/smart-agent/pipeline.js +8 -0
- package/dist/smart-agent/pipeline.js.map +1 -0
- package/dist/smart-agent/plugins/index.d.ts +4 -0
- package/dist/smart-agent/plugins/index.d.ts.map +1 -0
- package/dist/smart-agent/plugins/index.js +3 -0
- package/dist/smart-agent/plugins/index.js.map +1 -0
- package/dist/smart-agent/plugins/loader.d.ts +67 -0
- package/dist/smart-agent/plugins/loader.d.ts.map +1 -0
- package/dist/smart-agent/plugins/loader.js +114 -0
- package/dist/smart-agent/plugins/loader.js.map +1 -0
- package/dist/smart-agent/plugins/types.d.ts +164 -0
- package/dist/smart-agent/plugins/types.d.ts.map +1 -0
- package/dist/smart-agent/plugins/types.js +136 -0
- package/dist/smart-agent/plugins/types.js.map +1 -0
- package/dist/smart-agent/policy/fallback-llm-call-strategy.d.ts +16 -0
- package/dist/smart-agent/policy/fallback-llm-call-strategy.d.ts.map +1 -0
- package/dist/smart-agent/policy/fallback-llm-call-strategy.js +66 -0
- package/dist/smart-agent/policy/fallback-llm-call-strategy.js.map +1 -0
- package/dist/smart-agent/policy/heuristic-injection-detector.d.ts +5 -0
- package/dist/smart-agent/policy/heuristic-injection-detector.d.ts.map +1 -0
- package/dist/smart-agent/policy/heuristic-injection-detector.js +36 -0
- package/dist/smart-agent/policy/heuristic-injection-detector.js.map +1 -0
- package/dist/smart-agent/policy/index.d.ts +6 -0
- package/dist/smart-agent/policy/index.d.ts.map +1 -0
- package/dist/smart-agent/policy/index.js +5 -0
- package/dist/smart-agent/policy/index.js.map +1 -0
- package/dist/smart-agent/policy/mixed-tool-call-handler.d.ts +16 -0
- package/dist/smart-agent/policy/mixed-tool-call-handler.d.ts.map +1 -0
- package/dist/smart-agent/policy/mixed-tool-call-handler.js +49 -0
- package/dist/smart-agent/policy/mixed-tool-call-handler.js.map +1 -0
- package/dist/smart-agent/policy/non-streaming-llm-call-strategy.d.ts +6 -0
- package/dist/smart-agent/policy/non-streaming-llm-call-strategy.d.ts.map +1 -0
- package/dist/smart-agent/policy/non-streaming-llm-call-strategy.js +21 -0
- package/dist/smart-agent/policy/non-streaming-llm-call-strategy.js.map +1 -0
- package/dist/smart-agent/policy/pending-tool-results-registry.d.ts +25 -0
- package/dist/smart-agent/policy/pending-tool-results-registry.d.ts.map +1 -0
- package/dist/smart-agent/policy/pending-tool-results-registry.js +50 -0
- package/dist/smart-agent/policy/pending-tool-results-registry.js.map +1 -0
- package/dist/smart-agent/policy/streaming-llm-call-strategy.d.ts +6 -0
- package/dist/smart-agent/policy/streaming-llm-call-strategy.d.ts.map +1 -0
- package/dist/smart-agent/policy/streaming-llm-call-strategy.js +7 -0
- package/dist/smart-agent/policy/streaming-llm-call-strategy.js.map +1 -0
- package/dist/smart-agent/policy/tool-availability-registry.d.ts +21 -0
- package/dist/smart-agent/policy/tool-availability-registry.d.ts.map +1 -0
- package/dist/smart-agent/policy/tool-availability-registry.js +77 -0
- package/dist/smart-agent/policy/tool-availability-registry.js.map +1 -0
- package/dist/smart-agent/policy/tool-policy-guard.d.ts +8 -0
- package/dist/smart-agent/policy/tool-policy-guard.d.ts.map +1 -0
- package/dist/smart-agent/policy/tool-policy-guard.js +40 -0
- package/dist/smart-agent/policy/tool-policy-guard.js.map +1 -0
- package/dist/smart-agent/policy/types.d.ts +51 -0
- package/dist/smart-agent/policy/types.d.ts.map +1 -0
- package/dist/smart-agent/policy/types.js +17 -0
- package/dist/smart-agent/policy/types.js.map +1 -0
- package/dist/smart-agent/providers.d.ts +98 -0
- package/dist/smart-agent/providers.d.ts.map +1 -0
- package/dist/smart-agent/providers.js +245 -0
- package/dist/smart-agent/providers.js.map +1 -0
- package/dist/smart-agent/reranker/index.d.ts +4 -0
- package/dist/smart-agent/reranker/index.d.ts.map +1 -0
- package/dist/smart-agent/reranker/index.js +3 -0
- package/dist/smart-agent/reranker/index.js.map +1 -0
- package/dist/smart-agent/reranker/llm-reranker.d.ts +10 -0
- package/dist/smart-agent/reranker/llm-reranker.d.ts.map +1 -0
- package/dist/smart-agent/reranker/llm-reranker.js +55 -0
- package/dist/smart-agent/reranker/llm-reranker.js.map +1 -0
- package/dist/smart-agent/reranker/noop-reranker.d.ts +6 -0
- package/dist/smart-agent/reranker/noop-reranker.d.ts.map +1 -0
- package/dist/smart-agent/reranker/noop-reranker.js +6 -0
- package/dist/smart-agent/reranker/noop-reranker.js.map +1 -0
- package/dist/smart-agent/reranker/types.d.ts +5 -0
- package/dist/smart-agent/reranker/types.d.ts.map +1 -0
- package/dist/smart-agent/reranker/types.js +2 -0
- package/dist/smart-agent/reranker/types.js.map +1 -0
- package/dist/smart-agent/resilience/circuit-breaker-embedder.d.ts +16 -0
- package/dist/smart-agent/resilience/circuit-breaker-embedder.d.ts.map +1 -0
- package/dist/smart-agent/resilience/circuit-breaker-embedder.js +47 -0
- package/dist/smart-agent/resilience/circuit-breaker-embedder.js.map +1 -0
- package/dist/smart-agent/resilience/circuit-breaker-llm.d.ts +19 -0
- package/dist/smart-agent/resilience/circuit-breaker-llm.d.ts.map +1 -0
- package/dist/smart-agent/resilience/circuit-breaker-llm.js +72 -0
- package/dist/smart-agent/resilience/circuit-breaker-llm.js.map +1 -0
- package/dist/smart-agent/resilience/circuit-breaker.d.ts +36 -0
- package/dist/smart-agent/resilience/circuit-breaker.d.ts.map +1 -0
- package/dist/smart-agent/resilience/circuit-breaker.js +60 -0
- package/dist/smart-agent/resilience/circuit-breaker.js.map +1 -0
- package/dist/smart-agent/resilience/fallback-rag.d.ts +21 -0
- package/dist/smart-agent/resilience/fallback-rag.d.ts.map +1 -0
- package/dist/smart-agent/resilience/fallback-rag.js +80 -0
- package/dist/smart-agent/resilience/fallback-rag.js.map +1 -0
- package/dist/smart-agent/resilience/index.d.ts +5 -0
- package/dist/smart-agent/resilience/index.d.ts.map +1 -0
- package/dist/smart-agent/resilience/index.js +5 -0
- package/dist/smart-agent/resilience/index.js.map +1 -0
- package/dist/smart-agent/resilience/rate-limiter-llm.d.ts +17 -0
- package/dist/smart-agent/resilience/rate-limiter-llm.d.ts.map +1 -0
- package/dist/smart-agent/resilience/rate-limiter-llm.js +30 -0
- package/dist/smart-agent/resilience/rate-limiter-llm.js.map +1 -0
- package/dist/smart-agent/resilience/retry-llm.d.ts +32 -0
- package/dist/smart-agent/resilience/retry-llm.d.ts.map +1 -0
- package/dist/smart-agent/resilience/retry-llm.js +103 -0
- package/dist/smart-agent/resilience/retry-llm.js.map +1 -0
- package/dist/smart-agent/resilience/token-bucket-rate-limiter.d.ts +21 -0
- package/dist/smart-agent/resilience/token-bucket-rate-limiter.d.ts.map +1 -0
- package/dist/smart-agent/resilience/token-bucket-rate-limiter.js +32 -0
- package/dist/smart-agent/resilience/token-bucket-rate-limiter.js.map +1 -0
- package/dist/smart-agent/server.d.ts +19 -0
- package/dist/smart-agent/server.d.ts.map +1 -0
- package/dist/smart-agent/server.js +349 -0
- package/dist/smart-agent/server.js.map +1 -0
- package/dist/smart-agent/session/index.d.ts +4 -0
- package/dist/smart-agent/session/index.d.ts.map +1 -0
- package/dist/smart-agent/session/index.js +3 -0
- package/dist/smart-agent/session/index.js.map +1 -0
- package/dist/smart-agent/session/noop-session-manager.d.ts +8 -0
- package/dist/smart-agent/session/noop-session-manager.d.ts.map +1 -0
- package/dist/smart-agent/session/noop-session-manager.js +11 -0
- package/dist/smart-agent/session/noop-session-manager.js.map +1 -0
- package/dist/smart-agent/session/session-manager.d.ts +13 -0
- package/dist/smart-agent/session/session-manager.d.ts.map +1 -0
- package/dist/smart-agent/session/session-manager.js +20 -0
- package/dist/smart-agent/session/session-manager.js.map +1 -0
- package/dist/smart-agent/session/types.d.ts +7 -0
- package/dist/smart-agent/session/types.d.ts.map +1 -0
- package/dist/smart-agent/session/types.js +2 -0
- package/dist/smart-agent/session/types.js.map +1 -0
- package/dist/smart-agent/skills/claude-skill-manager.d.ts +24 -0
- package/dist/smart-agent/skills/claude-skill-manager.d.ts.map +1 -0
- package/dist/smart-agent/skills/claude-skill-manager.js +81 -0
- package/dist/smart-agent/skills/claude-skill-manager.js.map +1 -0
- package/dist/smart-agent/skills/codex-skill-manager.d.ts +20 -0
- package/dist/smart-agent/skills/codex-skill-manager.d.ts.map +1 -0
- package/dist/smart-agent/skills/codex-skill-manager.js +75 -0
- package/dist/smart-agent/skills/codex-skill-manager.js.map +1 -0
- package/dist/smart-agent/skills/filesystem-skill-manager.d.ts +17 -0
- package/dist/smart-agent/skills/filesystem-skill-manager.d.ts.map +1 -0
- package/dist/smart-agent/skills/filesystem-skill-manager.js +48 -0
- package/dist/smart-agent/skills/filesystem-skill-manager.js.map +1 -0
- package/dist/smart-agent/skills/filesystem-skill.d.ts +25 -0
- package/dist/smart-agent/skills/filesystem-skill.d.ts.map +1 -0
- package/dist/smart-agent/skills/filesystem-skill.js +110 -0
- package/dist/smart-agent/skills/filesystem-skill.js.map +1 -0
- package/dist/smart-agent/skills/index.d.ts +9 -0
- package/dist/smart-agent/skills/index.d.ts.map +1 -0
- package/dist/smart-agent/skills/index.js +9 -0
- package/dist/smart-agent/skills/index.js.map +1 -0
- package/dist/smart-agent/skills/skill-utils.d.ts +15 -0
- package/dist/smart-agent/skills/skill-utils.d.ts.map +1 -0
- package/dist/smart-agent/skills/skill-utils.js +46 -0
- package/dist/smart-agent/skills/skill-utils.js.map +1 -0
- package/dist/smart-agent/smart-server.d.ts +150 -0
- package/dist/smart-agent/smart-server.d.ts.map +1 -0
- package/dist/smart-agent/smart-server.js +903 -0
- package/dist/smart-agent/smart-server.js.map +1 -0
- package/dist/smart-agent/strategies/index.d.ts +4 -0
- package/dist/smart-agent/strategies/index.d.ts.map +1 -0
- package/dist/smart-agent/strategies/index.js +4 -0
- package/dist/smart-agent/strategies/index.js.map +1 -0
- package/dist/smart-agent/strategies/lazy-connection-strategy.d.ts +15 -0
- package/dist/smart-agent/strategies/lazy-connection-strategy.d.ts.map +1 -0
- package/dist/smart-agent/strategies/lazy-connection-strategy.js +82 -0
- package/dist/smart-agent/strategies/lazy-connection-strategy.js.map +1 -0
- package/dist/smart-agent/strategies/noop-connection-strategy.d.ts +6 -0
- package/dist/smart-agent/strategies/noop-connection-strategy.d.ts.map +1 -0
- package/dist/smart-agent/strategies/noop-connection-strategy.js +6 -0
- package/dist/smart-agent/strategies/noop-connection-strategy.js.map +1 -0
- package/dist/smart-agent/strategies/periodic-connection-strategy.d.ts +13 -0
- package/dist/smart-agent/strategies/periodic-connection-strategy.d.ts.map +1 -0
- package/dist/smart-agent/strategies/periodic-connection-strategy.js +40 -0
- package/dist/smart-agent/strategies/periodic-connection-strategy.js.map +1 -0
- package/dist/smart-agent/testing/index.d.ts +121 -0
- package/dist/smart-agent/testing/index.d.ts.map +1 -0
- package/dist/smart-agent/testing/index.js +354 -0
- package/dist/smart-agent/testing/index.js.map +1 -0
- package/dist/smart-agent/tracer/index.d.ts +3 -0
- package/dist/smart-agent/tracer/index.d.ts.map +1 -0
- package/dist/smart-agent/tracer/index.js +2 -0
- package/dist/smart-agent/tracer/index.js.map +1 -0
- package/dist/smart-agent/tracer/noop-tracer.d.ts +5 -0
- package/dist/smart-agent/tracer/noop-tracer.d.ts.map +1 -0
- package/dist/smart-agent/tracer/noop-tracer.js +16 -0
- package/dist/smart-agent/tracer/noop-tracer.js.map +1 -0
- package/dist/smart-agent/tracer/types.d.ts +17 -0
- package/dist/smart-agent/tracer/types.d.ts.map +1 -0
- package/dist/smart-agent/tracer/types.js +2 -0
- package/dist/smart-agent/tracer/types.js.map +1 -0
- package/dist/smart-agent/utils/external-tools-normalizer.d.ts +15 -0
- package/dist/smart-agent/utils/external-tools-normalizer.d.ts.map +1 -0
- package/dist/smart-agent/utils/external-tools-normalizer.js +146 -0
- package/dist/smart-agent/utils/external-tools-normalizer.js.map +1 -0
- package/dist/smart-agent/utils/lazy.d.ts +51 -0
- package/dist/smart-agent/utils/lazy.d.ts.map +1 -0
- package/dist/smart-agent/utils/lazy.js +127 -0
- package/dist/smart-agent/utils/lazy.js.map +1 -0
- package/dist/smart-agent/utils/parse-frontmatter.d.ts +15 -0
- package/dist/smart-agent/utils/parse-frontmatter.d.ts.map +1 -0
- package/dist/smart-agent/utils/parse-frontmatter.js +22 -0
- package/dist/smart-agent/utils/parse-frontmatter.js.map +1 -0
- package/dist/smart-agent/utils/tool-call-deltas.d.ts +4 -0
- package/dist/smart-agent/utils/tool-call-deltas.d.ts.map +1 -0
- package/dist/smart-agent/utils/tool-call-deltas.js +25 -0
- package/dist/smart-agent/utils/tool-call-deltas.js.map +1 -0
- package/dist/smart-agent/validator/index.d.ts +3 -0
- package/dist/smart-agent/validator/index.d.ts.map +1 -0
- package/dist/smart-agent/validator/index.js +2 -0
- package/dist/smart-agent/validator/index.js.map +1 -0
- package/dist/smart-agent/validator/noop-validator.d.ts +6 -0
- package/dist/smart-agent/validator/noop-validator.d.ts.map +1 -0
- package/dist/smart-agent/validator/noop-validator.js +6 -0
- package/dist/smart-agent/validator/noop-validator.js.map +1 -0
- package/dist/smart-agent/validator/types.d.ts +17 -0
- package/dist/smart-agent/validator/types.d.ts.map +1 -0
- package/dist/smart-agent/validator/types.js +8 -0
- package/dist/smart-agent/validator/types.js.map +1 -0
- package/dist/smoke-adapters.d.ts +17 -0
- package/dist/smoke-adapters.d.ts.map +1 -0
- package/dist/smoke-adapters.js +178 -0
- package/dist/smoke-adapters.js.map +1 -0
- package/package.json +89 -0
- package/tools/claude-via-agent.ps1 +83 -0
- package/tools/claude-via-agent.sh +96 -0
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ToolLoopHandler — streaming LLM call + MCP tool execution loop.
|
|
3
|
+
*
|
|
4
|
+
* This is the "terminal" pipeline stage. It takes over the streaming yield
|
|
5
|
+
* mechanism to push SSE chunks back to the consumer.
|
|
6
|
+
*
|
|
7
|
+
* Reads: `ctx.assembledMessages`, `ctx.activeTools`, `ctx.toolClientMap`,
|
|
8
|
+
* `ctx.externalTools`, `ctx.mainLlm`
|
|
9
|
+
* Writes: yields chunks via `ctx.yield()`, updates `ctx.timing`
|
|
10
|
+
*
|
|
11
|
+
* ## Config
|
|
12
|
+
*
|
|
13
|
+
* | Field | Type | Default | Description |
|
|
14
|
+
* |--------------------|--------|-------------|---------------------------------|
|
|
15
|
+
* | `maxIterations` | number | from ctx | Max tool-loop iterations |
|
|
16
|
+
* | `maxToolCalls` | number | from ctx | Max total tool calls per request|
|
|
17
|
+
* | `heartbeatIntervalMs` | number | 5000 | SSE heartbeat interval (ms) |
|
|
18
|
+
*
|
|
19
|
+
* ## Includes
|
|
20
|
+
*
|
|
21
|
+
* - Output validation (re-prompts on invalid LLM output)
|
|
22
|
+
* - Tool call classification (internal / external / hallucinated / blocked)
|
|
23
|
+
* - Concurrent tool execution with heartbeat
|
|
24
|
+
* - Tool availability tracking (temporary blacklist)
|
|
25
|
+
*/
|
|
26
|
+
import { OrchestratorError } from '../../agent.js';
|
|
27
|
+
import { fireInternalToolsAsync } from '../../policy/mixed-tool-call-handler.js';
|
|
28
|
+
import { isToolContextUnavailableError } from '../../policy/tool-availability-registry.js';
|
|
29
|
+
import { getStreamToolCallName, toToolCallDelta, } from '../../utils/tool-call-deltas.js';
|
|
30
|
+
function summarizeIterationMessages(messages) {
|
|
31
|
+
const totalChars = messages.reduce((sum, msg) => {
|
|
32
|
+
const content = typeof msg.content === 'string'
|
|
33
|
+
? msg.content
|
|
34
|
+
: JSON.stringify(msg.content ?? '');
|
|
35
|
+
return sum + content.length;
|
|
36
|
+
}, 0);
|
|
37
|
+
return {
|
|
38
|
+
messageCount: messages.length,
|
|
39
|
+
totalChars,
|
|
40
|
+
roles: messages.map((msg, index) => ({
|
|
41
|
+
index,
|
|
42
|
+
role: msg.role,
|
|
43
|
+
contentLength: typeof msg.content === 'string'
|
|
44
|
+
? msg.content.length
|
|
45
|
+
: JSON.stringify(msg.content ?? '').length,
|
|
46
|
+
hasToolCalls: 'tool_calls' in msg && Array.isArray(msg.tool_calls),
|
|
47
|
+
toolCallCount: 'tool_calls' in msg && Array.isArray(msg.tool_calls)
|
|
48
|
+
? msg.tool_calls.length
|
|
49
|
+
: 0,
|
|
50
|
+
toolCallId: 'tool_call_id' in msg ? (msg.tool_call_id ?? null) : null,
|
|
51
|
+
})),
|
|
52
|
+
tail: messages.slice(-6).map((msg, index) => {
|
|
53
|
+
const content = typeof msg.content === 'string'
|
|
54
|
+
? msg.content
|
|
55
|
+
: JSON.stringify(msg.content ?? '');
|
|
56
|
+
return {
|
|
57
|
+
index: messages.length - Math.min(messages.length, 6) + index,
|
|
58
|
+
role: msg.role,
|
|
59
|
+
preview: content.length > 240
|
|
60
|
+
? `${content.slice(0, 240)}...[truncated]`
|
|
61
|
+
: content,
|
|
62
|
+
hasToolCalls: 'tool_calls' in msg && Array.isArray(msg.tool_calls),
|
|
63
|
+
toolCallNames: 'tool_calls' in msg && Array.isArray(msg.tool_calls)
|
|
64
|
+
? msg.tool_calls.map((tc) => tc.function?.name || '')
|
|
65
|
+
: [],
|
|
66
|
+
toolCallId: 'tool_call_id' in msg ? (msg.tool_call_id ?? null) : null,
|
|
67
|
+
};
|
|
68
|
+
}),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export class ToolLoopHandler {
|
|
72
|
+
async execute(ctx, config, parentSpan) {
|
|
73
|
+
const maxIterations = config.maxIterations ?? ctx.config.maxIterations;
|
|
74
|
+
const maxToolCalls = config.maxToolCalls ?? ctx.config.maxToolCalls;
|
|
75
|
+
const heartbeatMs = config.heartbeatIntervalMs ??
|
|
76
|
+
ctx.config.heartbeatIntervalMs ??
|
|
77
|
+
5000;
|
|
78
|
+
const mode = ctx.config.mode || 'smart';
|
|
79
|
+
const externalTools = mode === 'hard' ? [] : ctx.externalTools;
|
|
80
|
+
const externalToolNames = new Set(externalTools.map((t) => t.name));
|
|
81
|
+
let toolCallCount = 0;
|
|
82
|
+
let messages = ctx.assembledMessages;
|
|
83
|
+
const usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
|
84
|
+
const timingLog = [];
|
|
85
|
+
const loopStart = Date.now();
|
|
86
|
+
let currentTools = ctx.activeTools;
|
|
87
|
+
// Inject tool priority instruction when external tools are present
|
|
88
|
+
if (externalTools.length > 0) {
|
|
89
|
+
const systemIdx = messages.findIndex((m) => m.role === 'system');
|
|
90
|
+
if (systemIdx >= 0) {
|
|
91
|
+
const sys = messages[systemIdx];
|
|
92
|
+
messages = [...messages];
|
|
93
|
+
messages[systemIdx] = {
|
|
94
|
+
...sys,
|
|
95
|
+
content: `${sys.content}\n\nIMPORTANT: You have internal tools and client-provided tools (marked [client-provided] in their description). Always prefer internal tools when they can accomplish the task. Use client-provided tools only when no internal tool can do the job.`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// Inject pending internal tool results from previous mixed-call request
|
|
100
|
+
if (ctx.pendingToolResults.has(ctx.sessionId)) {
|
|
101
|
+
const pending = await ctx.pendingToolResults.consume(ctx.sessionId);
|
|
102
|
+
if (pending) {
|
|
103
|
+
messages = [
|
|
104
|
+
...messages,
|
|
105
|
+
pending.assistantMessage,
|
|
106
|
+
...pending.results.map((r) => ({
|
|
107
|
+
role: 'tool',
|
|
108
|
+
content: r.text,
|
|
109
|
+
tool_call_id: r.toolCallId,
|
|
110
|
+
})),
|
|
111
|
+
];
|
|
112
|
+
ctx.options?.sessionLogger?.logStep('pending_tool_results_injected', {
|
|
113
|
+
toolNames: pending.results.map((r) => r.toolName),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (let iteration = 0;; iteration++) {
|
|
118
|
+
if (ctx.options?.signal?.aborted) {
|
|
119
|
+
ctx.yield({
|
|
120
|
+
ok: false,
|
|
121
|
+
error: new OrchestratorError('Aborted', 'ABORTED'),
|
|
122
|
+
});
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
if (iteration >= maxIterations) {
|
|
126
|
+
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
127
|
+
ctx.timing.push(...timingLog);
|
|
128
|
+
ctx.yield({
|
|
129
|
+
ok: true,
|
|
130
|
+
value: {
|
|
131
|
+
content: '',
|
|
132
|
+
finishReason: 'length',
|
|
133
|
+
usage: {
|
|
134
|
+
...usage,
|
|
135
|
+
models: ctx.requestLogger.getSummary().byModel,
|
|
136
|
+
components: ctx.requestLogger.getSummary().byComponent,
|
|
137
|
+
},
|
|
138
|
+
timing: timingLog,
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
// Refresh MCP tools on each iteration (when enabled)
|
|
144
|
+
const prevSelectedTools = [...currentTools];
|
|
145
|
+
if (iteration > 0 && ctx.config.refreshToolsPerIteration !== false) {
|
|
146
|
+
const refreshSpan = ctx.tracer.startSpan('smart_agent.refresh_tools', {
|
|
147
|
+
parent: parentSpan,
|
|
148
|
+
attributes: { 'llm.iteration': iteration + 1 },
|
|
149
|
+
});
|
|
150
|
+
const prevNames = [...ctx.toolClientMap.keys()];
|
|
151
|
+
ctx.toolClientMap.clear();
|
|
152
|
+
ctx.mcpTools.length = 0;
|
|
153
|
+
const settled = await Promise.allSettled(ctx.mcpClients.map(async (client) => ({
|
|
154
|
+
client,
|
|
155
|
+
result: await client.listTools(ctx.options),
|
|
156
|
+
})));
|
|
157
|
+
for (const entry of settled) {
|
|
158
|
+
if (entry.status === 'fulfilled' && entry.value.result.ok) {
|
|
159
|
+
for (const t of entry.value.result.value) {
|
|
160
|
+
if (!ctx.toolClientMap.has(t.name)) {
|
|
161
|
+
ctx.toolClientMap.set(t.name, entry.value.client);
|
|
162
|
+
ctx.mcpTools.push(t);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
currentTools = [...ctx.mcpTools, ...externalTools];
|
|
168
|
+
ctx.options?.sessionLogger?.logStep('tools_refreshed', {
|
|
169
|
+
iteration: iteration + 1,
|
|
170
|
+
previous: prevNames,
|
|
171
|
+
current: currentTools.map((t) => t.name),
|
|
172
|
+
});
|
|
173
|
+
refreshSpan.end();
|
|
174
|
+
}
|
|
175
|
+
// Per-iteration RAG tool re-selection (when enabled)
|
|
176
|
+
if (iteration > 0 &&
|
|
177
|
+
ctx.config.toolReselectPerIteration &&
|
|
178
|
+
ctx.ragStores?.tools) {
|
|
179
|
+
const reselectSpan = ctx.tracer.startSpan('smart_agent.tool_reselect', {
|
|
180
|
+
parent: parentSpan,
|
|
181
|
+
attributes: { 'llm.iteration': iteration + 1 },
|
|
182
|
+
});
|
|
183
|
+
try {
|
|
184
|
+
// Extract last tool calls
|
|
185
|
+
const lastAssistant = [...messages]
|
|
186
|
+
.reverse()
|
|
187
|
+
.find((m) => m.role === 'assistant');
|
|
188
|
+
const toolCallNames = [];
|
|
189
|
+
if (lastAssistant && 'tool_calls' in lastAssistant) {
|
|
190
|
+
const tcs = lastAssistant.tool_calls;
|
|
191
|
+
if (Array.isArray(tcs)) {
|
|
192
|
+
for (const tc of tcs) {
|
|
193
|
+
const name = tc?.function?.name || '';
|
|
194
|
+
if (name)
|
|
195
|
+
toolCallNames.push(name);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// Skip for read-only tools — they rarely need different tools on retry
|
|
200
|
+
const readOnlyPrefixes = [
|
|
201
|
+
'Search',
|
|
202
|
+
'Read',
|
|
203
|
+
'Get',
|
|
204
|
+
'List',
|
|
205
|
+
'Describe',
|
|
206
|
+
];
|
|
207
|
+
const allReadOnly = toolCallNames.length > 0 &&
|
|
208
|
+
toolCallNames.every((n) => readOnlyPrefixes.some((p) => n.startsWith(p)));
|
|
209
|
+
if (allReadOnly) {
|
|
210
|
+
// Read-only tools don't need re-selection — restore previous selection
|
|
211
|
+
// to avoid sending all MCP tools after refresh
|
|
212
|
+
currentTools = prevSelectedTools;
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
// Build context-aware query from error/result context
|
|
216
|
+
const lastToolMsg = [...messages]
|
|
217
|
+
.reverse()
|
|
218
|
+
.find((m) => m.role === 'tool');
|
|
219
|
+
const toolResult = typeof lastToolMsg?.content === 'string'
|
|
220
|
+
? lastToolMsg.content.slice(0, 200)
|
|
221
|
+
: '';
|
|
222
|
+
const isError = toolResult.toLowerCase().includes('error') ||
|
|
223
|
+
toolResult.toLowerCase().includes('already exist') ||
|
|
224
|
+
toolResult.toLowerCase().includes('failed');
|
|
225
|
+
let reSelectQuery;
|
|
226
|
+
if (toolCallNames.length > 0 && isError) {
|
|
227
|
+
const updateHints = toolCallNames
|
|
228
|
+
.filter((n) => n.startsWith('Create'))
|
|
229
|
+
.map((n) => n.replace(/^Create/, 'Update'))
|
|
230
|
+
.join(', ');
|
|
231
|
+
const hints = updateHints ? ` Need ${updateHints}.` : '';
|
|
232
|
+
reSelectQuery = `${toolCallNames.join(', ')} failed: ${toolResult.slice(0, 150)}.${hints} ${ctx.inputText.slice(0, 200)}`;
|
|
233
|
+
}
|
|
234
|
+
else if (toolCallNames.length > 0) {
|
|
235
|
+
reSelectQuery = `After ${toolCallNames.join(', ')}: ${toolResult}\n${ctx.inputText.slice(0, 200)}`;
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
reSelectQuery = ctx.inputText;
|
|
239
|
+
}
|
|
240
|
+
// Query tools RAG
|
|
241
|
+
const { QueryEmbedding, TextOnlyEmbedding } = await import('@mcp-abap-adt/llm-agent');
|
|
242
|
+
const embedding = ctx.embedder
|
|
243
|
+
? new QueryEmbedding(reSelectQuery, ctx.embedder, ctx.options)
|
|
244
|
+
: new TextOnlyEmbedding(reSelectQuery);
|
|
245
|
+
const ragK = ctx.config.ragQueryK ?? 20;
|
|
246
|
+
const ragStart = Date.now();
|
|
247
|
+
const ragResult = await ctx.ragStores.tools.query(embedding, ragK, ctx.options);
|
|
248
|
+
ctx.requestLogger.logRagQuery({
|
|
249
|
+
store: 'tools',
|
|
250
|
+
query: reSelectQuery.slice(0, 200),
|
|
251
|
+
resultCount: ragResult.ok ? ragResult.value.length : 0,
|
|
252
|
+
durationMs: Date.now() - ragStart,
|
|
253
|
+
});
|
|
254
|
+
if (ragResult.ok && ragResult.value.length > 0) {
|
|
255
|
+
const newToolNames = new Set(ragResult.value
|
|
256
|
+
.map((r) => r.metadata?.id || '')
|
|
257
|
+
.filter((id) => id.startsWith('tool:'))
|
|
258
|
+
.map((id) => id.slice(5).replace(/:.*$/, '')));
|
|
259
|
+
if (newToolNames.size > 0) {
|
|
260
|
+
const newMcpTools = ctx.mcpTools.filter((t) => newToolNames.has(t.name));
|
|
261
|
+
currentTools = [
|
|
262
|
+
...newMcpTools,
|
|
263
|
+
...externalTools,
|
|
264
|
+
];
|
|
265
|
+
ctx.options?.sessionLogger?.logStep('tools_reselected', {
|
|
266
|
+
iteration: iteration + 1,
|
|
267
|
+
query: reSelectQuery.slice(0, 100),
|
|
268
|
+
previousTools: toolCallNames,
|
|
269
|
+
newTools: [...newToolNames],
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
finally {
|
|
276
|
+
reselectSpan.end();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
// Filter tools per iteration
|
|
280
|
+
const filteredForIteration = ctx.toolAvailabilityRegistry.filterTools(ctx.sessionId, currentTools);
|
|
281
|
+
currentTools = filteredForIteration.allowed;
|
|
282
|
+
if (filteredForIteration.blocked.length > 0) {
|
|
283
|
+
ctx.options?.sessionLogger?.logStep('active_tools_filtered_in_iteration', { iteration: iteration + 1, blocked: filteredForIteration.blocked });
|
|
284
|
+
}
|
|
285
|
+
let iterPromptTokens = 0;
|
|
286
|
+
let iterCompletionTokens = 0;
|
|
287
|
+
let iterTotalTokens = 0;
|
|
288
|
+
ctx.options?.sessionLogger?.logStep(`llm_request_iter_${iteration + 1}`, {
|
|
289
|
+
messages,
|
|
290
|
+
tools: currentTools,
|
|
291
|
+
});
|
|
292
|
+
const iterationMessageSummary = summarizeIterationMessages(messages);
|
|
293
|
+
const looksLikeFinalPass = messages.some((msg) => msg.role === 'tool' ||
|
|
294
|
+
('tool_calls' in msg &&
|
|
295
|
+
Array.isArray(msg.tool_calls) &&
|
|
296
|
+
msg.tool_calls.length > 0));
|
|
297
|
+
ctx.options?.sessionLogger?.logStep('llm_request_iter_context_summary', {
|
|
298
|
+
iteration: iteration + 1,
|
|
299
|
+
toolCount: currentTools.length,
|
|
300
|
+
looksLikeFinalPass,
|
|
301
|
+
summary: iterationMessageSummary,
|
|
302
|
+
});
|
|
303
|
+
ctx.logger?.log({
|
|
304
|
+
type: 'warning',
|
|
305
|
+
traceId: ctx.options?.trace?.traceId ?? 'tool-loop',
|
|
306
|
+
message: `tool-loop iteration ${iteration + 1} context summary ${JSON.stringify({
|
|
307
|
+
iteration: iteration + 1,
|
|
308
|
+
toolCount: currentTools.length,
|
|
309
|
+
looksLikeFinalPass,
|
|
310
|
+
summary: iterationMessageSummary,
|
|
311
|
+
})}`,
|
|
312
|
+
});
|
|
313
|
+
const llmSpan = ctx.tracer.startSpan('smart_agent.llm_call', {
|
|
314
|
+
parent: parentSpan,
|
|
315
|
+
attributes: { 'llm.iteration': iteration + 1 },
|
|
316
|
+
});
|
|
317
|
+
ctx.metrics.llmCallCount.add();
|
|
318
|
+
const llmCallStart = Date.now();
|
|
319
|
+
let content = '';
|
|
320
|
+
let finishReason;
|
|
321
|
+
const toolCallsMap = new Map();
|
|
322
|
+
// Track which streaming indices belong to external tools so that
|
|
323
|
+
// argument-only continuation deltas (no name field) are forwarded too.
|
|
324
|
+
const externalToolIndices = new Set();
|
|
325
|
+
// Delegate to ILlmCallStrategy — handles streaming, non-streaming, or fallback
|
|
326
|
+
const llmStream = ctx.llmCallStrategy.call(ctx.mainLlm, messages, currentTools, ctx.options);
|
|
327
|
+
for await (const chunkResult of llmStream) {
|
|
328
|
+
if (!chunkResult.ok) {
|
|
329
|
+
llmSpan.setStatus('error', chunkResult.error.message);
|
|
330
|
+
llmSpan.end();
|
|
331
|
+
ctx.yield({
|
|
332
|
+
ok: false,
|
|
333
|
+
error: new OrchestratorError(chunkResult.error.message, 'LLM_ERROR'),
|
|
334
|
+
});
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
const chunk = chunkResult.value;
|
|
338
|
+
// Mid-stream retry or fallback reset: discard accumulated state
|
|
339
|
+
if (chunk.reset) {
|
|
340
|
+
content = '';
|
|
341
|
+
toolCallsMap.clear();
|
|
342
|
+
externalToolIndices.clear();
|
|
343
|
+
finishReason = undefined;
|
|
344
|
+
iterPromptTokens = 0;
|
|
345
|
+
iterCompletionTokens = 0;
|
|
346
|
+
iterTotalTokens = 0;
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (chunk.content) {
|
|
350
|
+
content += chunk.content;
|
|
351
|
+
ctx.yield({ ok: true, value: { content: chunk.content } });
|
|
352
|
+
}
|
|
353
|
+
if (chunk.toolCalls) {
|
|
354
|
+
// Register newly seen external tool indices
|
|
355
|
+
for (const tc of chunk.toolCalls) {
|
|
356
|
+
const name = getStreamToolCallName(tc);
|
|
357
|
+
if (name && externalToolNames.has(name)) {
|
|
358
|
+
const delta = toToolCallDelta(tc, 0);
|
|
359
|
+
externalToolIndices.add(delta.index);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const externalDeltas = chunk.toolCalls.filter((tc) => {
|
|
363
|
+
const delta = toToolCallDelta(tc, 0);
|
|
364
|
+
return externalToolIndices.has(delta.index);
|
|
365
|
+
});
|
|
366
|
+
if (externalDeltas.length > 0) {
|
|
367
|
+
ctx.yield({
|
|
368
|
+
ok: true,
|
|
369
|
+
value: { content: '', toolCalls: externalDeltas },
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
for (const [fallbackIndex, rawToolCall,] of chunk.toolCalls.entries()) {
|
|
373
|
+
const tc = toToolCallDelta(rawToolCall, fallbackIndex);
|
|
374
|
+
if (!toolCallsMap.has(tc.index)) {
|
|
375
|
+
toolCallsMap.set(tc.index, {
|
|
376
|
+
id: tc.id || '',
|
|
377
|
+
name: tc.name || '',
|
|
378
|
+
arguments: tc.arguments || '',
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
const ex = toolCallsMap.get(tc.index);
|
|
383
|
+
if (ex) {
|
|
384
|
+
if (tc.id)
|
|
385
|
+
ex.id = tc.id;
|
|
386
|
+
if (tc.name)
|
|
387
|
+
ex.name = tc.name;
|
|
388
|
+
if (tc.arguments)
|
|
389
|
+
ex.arguments += tc.arguments;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (chunk.finishReason)
|
|
395
|
+
finishReason = chunk.finishReason;
|
|
396
|
+
if (chunk.usage) {
|
|
397
|
+
usage.promptTokens += chunk.usage.promptTokens;
|
|
398
|
+
usage.completionTokens += chunk.usage.completionTokens;
|
|
399
|
+
usage.totalTokens += chunk.usage.totalTokens;
|
|
400
|
+
iterPromptTokens += chunk.usage.promptTokens;
|
|
401
|
+
iterCompletionTokens += chunk.usage.completionTokens;
|
|
402
|
+
iterTotalTokens += chunk.usage.totalTokens;
|
|
403
|
+
ctx.sessionManager.addTokens(chunk.usage.totalTokens);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
llmSpan.setStatus('ok');
|
|
407
|
+
llmSpan.end();
|
|
408
|
+
const llmCallDuration = Date.now() - llmCallStart;
|
|
409
|
+
ctx.metrics.llmCallLatency.record(llmCallDuration);
|
|
410
|
+
timingLog.push({
|
|
411
|
+
phase: `llm_call_${iteration + 1}`,
|
|
412
|
+
duration: llmCallDuration,
|
|
413
|
+
});
|
|
414
|
+
ctx.requestLogger.logLlmCall({
|
|
415
|
+
component: 'tool-loop',
|
|
416
|
+
model: ctx.mainLlm.model ?? 'unknown',
|
|
417
|
+
promptTokens: iterPromptTokens,
|
|
418
|
+
completionTokens: iterCompletionTokens,
|
|
419
|
+
totalTokens: iterTotalTokens,
|
|
420
|
+
durationMs: llmCallDuration,
|
|
421
|
+
});
|
|
422
|
+
const toolCalls = Array.from(toolCallsMap.values()).map((tc) => {
|
|
423
|
+
let args = {};
|
|
424
|
+
try {
|
|
425
|
+
args = JSON.parse(tc.arguments);
|
|
426
|
+
}
|
|
427
|
+
catch {
|
|
428
|
+
args = {};
|
|
429
|
+
}
|
|
430
|
+
return { id: tc.id, name: tc.name, arguments: args };
|
|
431
|
+
});
|
|
432
|
+
ctx.options?.sessionLogger?.logStep(`llm_response_iter_${iteration + 1}`, { content, toolCalls, finishReason });
|
|
433
|
+
// -- No tool calls: validate and finish --------------------------------
|
|
434
|
+
if (finishReason !== 'tool_calls' || toolCalls.length === 0) {
|
|
435
|
+
const valResult = await ctx.outputValidator.validate(content, { messages, tools: currentTools }, ctx.options);
|
|
436
|
+
if (valResult.ok && !valResult.value.valid) {
|
|
437
|
+
const correction = valResult.value.correctedContent ?? valResult.value.reason;
|
|
438
|
+
messages = [
|
|
439
|
+
...messages,
|
|
440
|
+
{ role: 'assistant', content },
|
|
441
|
+
{
|
|
442
|
+
role: 'user',
|
|
443
|
+
content: `Your previous response was rejected by validation: ${correction}. Please try again.`,
|
|
444
|
+
},
|
|
445
|
+
];
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
const summary = ctx.requestLogger.getSummary();
|
|
449
|
+
ctx.options?.sessionLogger?.logStep('final_response', {
|
|
450
|
+
content,
|
|
451
|
+
usage,
|
|
452
|
+
byComponent: summary.byComponent,
|
|
453
|
+
byModel: summary.byModel,
|
|
454
|
+
byCategory: summary.byCategory,
|
|
455
|
+
});
|
|
456
|
+
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
457
|
+
ctx.timing.push(...timingLog);
|
|
458
|
+
ctx.yield({
|
|
459
|
+
ok: true,
|
|
460
|
+
value: {
|
|
461
|
+
content: '',
|
|
462
|
+
finishReason: finishReason || 'stop',
|
|
463
|
+
usage: {
|
|
464
|
+
...usage,
|
|
465
|
+
models: ctx.requestLogger.getSummary().byModel,
|
|
466
|
+
components: ctx.requestLogger.getSummary().byComponent,
|
|
467
|
+
categories: ctx.requestLogger.getSummary().byCategory,
|
|
468
|
+
},
|
|
469
|
+
timing: timingLog,
|
|
470
|
+
},
|
|
471
|
+
});
|
|
472
|
+
return true;
|
|
473
|
+
}
|
|
474
|
+
// -- Classify tool calls -----------------------------------------------
|
|
475
|
+
const internalCalls = toolCalls.filter((tc) => ctx.toolClientMap.has(tc.name));
|
|
476
|
+
const validExternalCalls = toolCalls.filter((tc) => externalToolNames.has(tc.name));
|
|
477
|
+
const blockedToolNames = ctx.toolAvailabilityRegistry.getBlockedToolNames(ctx.sessionId);
|
|
478
|
+
const blockedCalls = toolCalls.filter((tc) => blockedToolNames.has(tc.name));
|
|
479
|
+
const hallucinations = toolCalls.filter((tc) => !blockedToolNames.has(tc.name) &&
|
|
480
|
+
!ctx.toolClientMap.has(tc.name) &&
|
|
481
|
+
!externalToolNames.has(tc.name));
|
|
482
|
+
// -- Handle blocked tools ----------------------------------------------
|
|
483
|
+
if (blockedCalls.length > 0) {
|
|
484
|
+
messages = [
|
|
485
|
+
...messages,
|
|
486
|
+
{
|
|
487
|
+
role: 'assistant',
|
|
488
|
+
content: content || null,
|
|
489
|
+
tool_calls: blockedCalls.map((tc) => ({
|
|
490
|
+
id: tc.id,
|
|
491
|
+
type: 'function',
|
|
492
|
+
function: {
|
|
493
|
+
name: tc.name,
|
|
494
|
+
arguments: JSON.stringify(tc.arguments),
|
|
495
|
+
},
|
|
496
|
+
})),
|
|
497
|
+
},
|
|
498
|
+
];
|
|
499
|
+
for (const blocked of blockedCalls) {
|
|
500
|
+
messages = [
|
|
501
|
+
...messages,
|
|
502
|
+
{
|
|
503
|
+
role: 'tool',
|
|
504
|
+
content: `Error: Tool "${blocked.name}" is temporarily unavailable in this session.`,
|
|
505
|
+
tool_call_id: blocked.id,
|
|
506
|
+
},
|
|
507
|
+
];
|
|
508
|
+
}
|
|
509
|
+
ctx.options?.sessionLogger?.logStep('blocked_tool_calls_intercepted', {
|
|
510
|
+
toolNames: blockedCalls.map((tc) => tc.name),
|
|
511
|
+
});
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
// -- Handle hallucinated tools -----------------------------------------
|
|
515
|
+
if (hallucinations.length > 0) {
|
|
516
|
+
messages = [
|
|
517
|
+
...messages,
|
|
518
|
+
{
|
|
519
|
+
role: 'assistant',
|
|
520
|
+
content: content || null,
|
|
521
|
+
tool_calls: toolCalls.map((tc) => ({
|
|
522
|
+
id: tc.id,
|
|
523
|
+
type: 'function',
|
|
524
|
+
function: {
|
|
525
|
+
name: tc.name,
|
|
526
|
+
arguments: JSON.stringify(tc.arguments),
|
|
527
|
+
},
|
|
528
|
+
})),
|
|
529
|
+
},
|
|
530
|
+
];
|
|
531
|
+
for (const h of hallucinations) {
|
|
532
|
+
messages = [
|
|
533
|
+
...messages,
|
|
534
|
+
{
|
|
535
|
+
role: 'tool',
|
|
536
|
+
content: `Error: Tool "${h.name}" not found.`,
|
|
537
|
+
tool_call_id: h.id,
|
|
538
|
+
},
|
|
539
|
+
];
|
|
540
|
+
}
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
// -- Handle external tool calls (delegate to consumer) -----------------
|
|
544
|
+
if (validExternalCalls.length > 0) {
|
|
545
|
+
if (internalCalls.length > 0) {
|
|
546
|
+
fireInternalToolsAsync(content, internalCalls, ctx.pendingToolResults, ctx.sessionId, {
|
|
547
|
+
toolClientMap: ctx.toolClientMap,
|
|
548
|
+
toolCache: ctx.toolCache,
|
|
549
|
+
metrics: ctx.metrics,
|
|
550
|
+
options: ctx.options,
|
|
551
|
+
});
|
|
552
|
+
ctx.options?.sessionLogger?.logStep('mixed_tool_calls', {
|
|
553
|
+
internal: internalCalls.map((tc) => tc.name),
|
|
554
|
+
external: validExternalCalls.map((tc) => tc.name),
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
558
|
+
ctx.timing.push(...timingLog);
|
|
559
|
+
ctx.yield({
|
|
560
|
+
ok: true,
|
|
561
|
+
value: {
|
|
562
|
+
content: '',
|
|
563
|
+
finishReason: 'tool_calls',
|
|
564
|
+
usage: {
|
|
565
|
+
...usage,
|
|
566
|
+
models: ctx.requestLogger.getSummary().byModel,
|
|
567
|
+
components: ctx.requestLogger.getSummary().byComponent,
|
|
568
|
+
},
|
|
569
|
+
timing: timingLog,
|
|
570
|
+
},
|
|
571
|
+
});
|
|
572
|
+
return true;
|
|
573
|
+
}
|
|
574
|
+
// -- Execute internal MCP tool calls -----------------------------------
|
|
575
|
+
if (content || internalCalls.length > 0) {
|
|
576
|
+
messages = [
|
|
577
|
+
...messages,
|
|
578
|
+
{
|
|
579
|
+
role: 'assistant',
|
|
580
|
+
content: content || null,
|
|
581
|
+
tool_calls: internalCalls.map((tc) => ({
|
|
582
|
+
id: tc.id,
|
|
583
|
+
type: 'function',
|
|
584
|
+
function: {
|
|
585
|
+
name: tc.name,
|
|
586
|
+
arguments: JSON.stringify(tc.arguments),
|
|
587
|
+
},
|
|
588
|
+
})),
|
|
589
|
+
},
|
|
590
|
+
];
|
|
591
|
+
}
|
|
592
|
+
// Check tool call budget
|
|
593
|
+
const remaining = maxToolCalls !== undefined
|
|
594
|
+
? maxToolCalls - toolCallCount
|
|
595
|
+
: internalCalls.length;
|
|
596
|
+
if (remaining <= 0) {
|
|
597
|
+
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
598
|
+
ctx.timing.push(...timingLog);
|
|
599
|
+
ctx.yield({
|
|
600
|
+
ok: true,
|
|
601
|
+
value: {
|
|
602
|
+
content: '',
|
|
603
|
+
finishReason: 'length',
|
|
604
|
+
usage: {
|
|
605
|
+
...usage,
|
|
606
|
+
models: ctx.requestLogger.getSummary().byModel,
|
|
607
|
+
components: ctx.requestLogger.getSummary().byComponent,
|
|
608
|
+
},
|
|
609
|
+
timing: timingLog,
|
|
610
|
+
},
|
|
611
|
+
});
|
|
612
|
+
return true;
|
|
613
|
+
}
|
|
614
|
+
const batch = internalCalls.slice(0, remaining);
|
|
615
|
+
// Yield progress messages
|
|
616
|
+
for (const tc of batch) {
|
|
617
|
+
ctx.yield({
|
|
618
|
+
ok: true,
|
|
619
|
+
value: { content: `\n\n[SmartAgent: Executing ${tc.name}...]\n` },
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
const toolExecPromises = batch.map(async (tc) => {
|
|
623
|
+
const toolStart = Date.now();
|
|
624
|
+
ctx.options?.sessionLogger?.logStep(`mcp_call_${tc.name}`, {
|
|
625
|
+
arguments: tc.arguments,
|
|
626
|
+
});
|
|
627
|
+
const client = ctx.toolClientMap.get(tc.name);
|
|
628
|
+
if (!client)
|
|
629
|
+
return { tc, text: '', res: null, duration: 0, cached: false };
|
|
630
|
+
const toolSpan = ctx.tracer.startSpan('smart_agent.tool_call', {
|
|
631
|
+
parent: parentSpan,
|
|
632
|
+
attributes: { 'tool.name': tc.name },
|
|
633
|
+
});
|
|
634
|
+
const cachedValue = ctx.toolCache.get(tc.name, tc.arguments);
|
|
635
|
+
const wasCached = !!cachedValue;
|
|
636
|
+
const res = cachedValue
|
|
637
|
+
? (() => {
|
|
638
|
+
ctx.metrics.toolCacheHitCount.add();
|
|
639
|
+
toolSpan.setAttribute('cache', 'hit');
|
|
640
|
+
return { ok: true, value: cachedValue };
|
|
641
|
+
})()
|
|
642
|
+
: await (async () => {
|
|
643
|
+
const r = await client.callTool(tc.name, tc.arguments, ctx.options);
|
|
644
|
+
if (r.ok)
|
|
645
|
+
ctx.toolCache.set(tc.name, tc.arguments, r.value);
|
|
646
|
+
return r;
|
|
647
|
+
})();
|
|
648
|
+
const text = !res.ok
|
|
649
|
+
? res.error.message
|
|
650
|
+
: typeof res.value.content === 'string'
|
|
651
|
+
? res.value.content
|
|
652
|
+
: JSON.stringify(res.value.content);
|
|
653
|
+
toolSpan.setStatus(res.ok ? 'ok' : 'error', res.ok ? undefined : text);
|
|
654
|
+
toolSpan.end();
|
|
655
|
+
return {
|
|
656
|
+
tc,
|
|
657
|
+
text,
|
|
658
|
+
res,
|
|
659
|
+
duration: Date.now() - toolStart,
|
|
660
|
+
cached: wasCached,
|
|
661
|
+
};
|
|
662
|
+
});
|
|
663
|
+
// Race: tool execution vs periodic heartbeat
|
|
664
|
+
const allDone = Promise.all(toolExecPromises);
|
|
665
|
+
const pendingTools = new Set(batch.map((tc) => tc.name));
|
|
666
|
+
const toolStartTime = Date.now();
|
|
667
|
+
let results = [];
|
|
668
|
+
let settled = false;
|
|
669
|
+
for (const [i, p] of toolExecPromises.entries()) {
|
|
670
|
+
p.then(() => pendingTools.delete(batch[i].name));
|
|
671
|
+
}
|
|
672
|
+
while (!settled) {
|
|
673
|
+
const winner = await Promise.race([
|
|
674
|
+
allDone.then((r) => ({ tag: 'done', results: r })),
|
|
675
|
+
new Promise((resolve) => setTimeout(() => resolve({ tag: 'tick' }), heartbeatMs)),
|
|
676
|
+
]);
|
|
677
|
+
if (winner.tag === 'done') {
|
|
678
|
+
results = winner.results;
|
|
679
|
+
settled = true;
|
|
680
|
+
}
|
|
681
|
+
else {
|
|
682
|
+
for (const tool of pendingTools) {
|
|
683
|
+
ctx.yield({
|
|
684
|
+
ok: true,
|
|
685
|
+
value: {
|
|
686
|
+
content: '',
|
|
687
|
+
heartbeat: { tool, elapsed: Date.now() - toolStartTime },
|
|
688
|
+
},
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
// Collect timing
|
|
694
|
+
for (const r of results) {
|
|
695
|
+
timingLog.push({ phase: `tool_${r.tc.name}`, duration: r.duration });
|
|
696
|
+
}
|
|
697
|
+
// Process results
|
|
698
|
+
const toolMessages = [];
|
|
699
|
+
for (const r of results) {
|
|
700
|
+
const { tc, text, res } = r;
|
|
701
|
+
if (!res)
|
|
702
|
+
continue;
|
|
703
|
+
if (!res.ok &&
|
|
704
|
+
isToolContextUnavailableError(text) &&
|
|
705
|
+
!externalToolNames.has(tc.name)) {
|
|
706
|
+
const entry = ctx.toolAvailabilityRegistry.block(ctx.sessionId, tc.name, text);
|
|
707
|
+
currentTools = currentTools.filter((t) => t.name !== tc.name);
|
|
708
|
+
ctx.options?.sessionLogger?.logStep(`tool_blacklisted_${tc.name}`, {
|
|
709
|
+
reason: text,
|
|
710
|
+
blockedUntil: entry.blockedUntil,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
ctx.options?.sessionLogger?.logStep(`mcp_result_${tc.name}`, {
|
|
714
|
+
result: text,
|
|
715
|
+
});
|
|
716
|
+
toolCallCount++;
|
|
717
|
+
ctx.metrics.toolCallCount.add();
|
|
718
|
+
toolMessages.push({
|
|
719
|
+
role: 'tool',
|
|
720
|
+
content: text,
|
|
721
|
+
tool_call_id: tc.id,
|
|
722
|
+
});
|
|
723
|
+
ctx.requestLogger.logToolCall({
|
|
724
|
+
toolName: tc.name,
|
|
725
|
+
success: !!res?.ok,
|
|
726
|
+
durationMs: r.duration,
|
|
727
|
+
cached: r.cached,
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
messages = [...messages, ...toolMessages];
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
//# sourceMappingURL=tool-loop.js.map
|