@cjhyy/code-shell-core 0.5.0-rc.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/LICENSE +21 -0
- package/README.md +162 -0
- package/dist/agent/coordinator.d.ts +49 -0
- package/dist/agent/coordinator.js +77 -0
- package/dist/arena/arena.d.ts +43 -0
- package/dist/arena/arena.js +334 -0
- package/dist/arena/context/context-tools.d.ts +16 -0
- package/dist/arena/context/context-tools.js +231 -0
- package/dist/arena/detect-mode.d.ts +20 -0
- package/dist/arena/detect-mode.js +78 -0
- package/dist/arena/digest-builder.d.ts +25 -0
- package/dist/arena/digest-builder.js +120 -0
- package/dist/arena/index.d.ts +29 -0
- package/dist/arena/index.js +29 -0
- package/dist/arena/iterate/convergence.d.ts +25 -0
- package/dist/arena/iterate/convergence.js +103 -0
- package/dist/arena/iterate/formats/index.d.ts +22 -0
- package/dist/arena/iterate/formats/index.js +283 -0
- package/dist/arena/iterate/index.d.ts +11 -0
- package/dist/arena/iterate/index.js +9 -0
- package/dist/arena/iterate/iterative-arena.d.ts +23 -0
- package/dist/arena/iterate/iterative-arena.js +237 -0
- package/dist/arena/iterate/parse.d.ts +42 -0
- package/dist/arena/iterate/parse.js +123 -0
- package/dist/arena/iterate/phases/argue.d.ts +22 -0
- package/dist/arena/iterate/phases/argue.js +159 -0
- package/dist/arena/iterate/phases/revise.d.ts +16 -0
- package/dist/arena/iterate/phases/revise.js +62 -0
- package/dist/arena/iterate/phases/tournament.d.ts +34 -0
- package/dist/arena/iterate/phases/tournament.js +113 -0
- package/dist/arena/iterate/tools/web-tools.d.ts +13 -0
- package/dist/arena/iterate/tools/web-tools.js +54 -0
- package/dist/arena/iterate/types.d.ts +152 -0
- package/dist/arena/iterate/types.js +8 -0
- package/dist/arena/ledger.d.ts +47 -0
- package/dist/arena/ledger.js +151 -0
- package/dist/arena/lenses/architecture.d.ts +5 -0
- package/dist/arena/lenses/architecture.js +22 -0
- package/dist/arena/lenses/engineering.d.ts +5 -0
- package/dist/arena/lenses/engineering.js +22 -0
- package/dist/arena/lenses/general.d.ts +5 -0
- package/dist/arena/lenses/general.js +20 -0
- package/dist/arena/lenses/index.d.ts +16 -0
- package/dist/arena/lenses/index.js +47 -0
- package/dist/arena/lenses/product.d.ts +5 -0
- package/dist/arena/lenses/product.js +22 -0
- package/dist/arena/model-presets.d.ts +23 -0
- package/dist/arena/model-presets.js +44 -0
- package/dist/arena/phases/adjudication.d.ts +24 -0
- package/dist/arena/phases/adjudication.js +144 -0
- package/dist/arena/phases/build-consensus.d.ts +29 -0
- package/dist/arena/phases/build-consensus.js +86 -0
- package/dist/arena/phases/claim-registry.d.ts +26 -0
- package/dist/arena/phases/claim-registry.js +60 -0
- package/dist/arena/phases/cross-review.d.ts +45 -0
- package/dist/arena/phases/cross-review.js +224 -0
- package/dist/arena/phases/debate-rounds.d.ts +27 -0
- package/dist/arena/phases/debate-rounds.js +162 -0
- package/dist/arena/phases/participant-research.d.ts +38 -0
- package/dist/arena/phases/participant-research.js +317 -0
- package/dist/arena/phases/planning-detail-expansion.d.ts +38 -0
- package/dist/arena/phases/planning-detail-expansion.js +121 -0
- package/dist/arena/planner.d.ts +27 -0
- package/dist/arena/planner.js +312 -0
- package/dist/arena/providers/docs.d.ts +6 -0
- package/dist/arena/providers/docs.js +108 -0
- package/dist/arena/providers/git.d.ts +8 -0
- package/dist/arena/providers/git.js +174 -0
- package/dist/arena/providers/index.d.ts +32 -0
- package/dist/arena/providers/index.js +132 -0
- package/dist/arena/providers/none.d.ts +7 -0
- package/dist/arena/providers/none.js +11 -0
- package/dist/arena/providers/repo.d.ts +6 -0
- package/dist/arena/providers/repo.js +255 -0
- package/dist/arena/render/session.d.ts +17 -0
- package/dist/arena/render/session.js +190 -0
- package/dist/arena/render/terminal.d.ts +34 -0
- package/dist/arena/render/terminal.js +286 -0
- package/dist/arena/strategies/discussion.d.ts +25 -0
- package/dist/arena/strategies/discussion.js +143 -0
- package/dist/arena/strategies/index.d.ts +15 -0
- package/dist/arena/strategies/index.js +28 -0
- package/dist/arena/strategies/language-wrapper.d.ts +17 -0
- package/dist/arena/strategies/language-wrapper.js +102 -0
- package/dist/arena/strategies/lens-wrapper.d.ts +16 -0
- package/dist/arena/strategies/lens-wrapper.js +236 -0
- package/dist/arena/strategies/planning.d.ts +30 -0
- package/dist/arena/strategies/planning.js +225 -0
- package/dist/arena/strategies/review.d.ts +26 -0
- package/dist/arena/strategies/review.js +168 -0
- package/dist/arena/strategies/utils.d.ts +39 -0
- package/dist/arena/strategies/utils.js +647 -0
- package/dist/arena/tools/selector.d.ts +17 -0
- package/dist/arena/tools/selector.js +61 -0
- package/dist/arena/transitions.d.ts +48 -0
- package/dist/arena/transitions.js +92 -0
- package/dist/arena/types.d.ts +508 -0
- package/dist/arena/types.js +27 -0
- package/dist/cli/agent-server-stdio.d.ts +32 -0
- package/dist/cli/agent-server-stdio.js +116 -0
- package/dist/colorizer.d.ts +23 -0
- package/dist/colorizer.js +12 -0
- package/dist/context/compaction.d.ts +116 -0
- package/dist/context/compaction.js +534 -0
- package/dist/context/manager.d.ts +145 -0
- package/dist/context/manager.js +423 -0
- package/dist/context/token-counter.d.ts +27 -0
- package/dist/context/token-counter.js +91 -0
- package/dist/context/tool-result-storage.d.ts +96 -0
- package/dist/context/tool-result-storage.js +296 -0
- package/dist/cost-tracker.d.ts +64 -0
- package/dist/cost-tracker.js +314 -0
- package/dist/cron/scheduler.d.ts +34 -0
- package/dist/cron/scheduler.js +115 -0
- package/dist/data/deepseek-models.json +46 -0
- package/dist/data/gemini-models.json +67 -0
- package/dist/data/openai-models.json +175 -0
- package/dist/data/openrouter-models.d.ts +35 -0
- package/dist/data/openrouter-models.js +36 -0
- package/dist/data/openrouter-models.json +4626 -0
- package/dist/data/openrouter-sync.d.ts +16 -0
- package/dist/data/openrouter-sync.js +53 -0
- package/dist/data/static-catalogs.d.ts +25 -0
- package/dist/data/static-catalogs.js +26 -0
- package/dist/data/zai-models.json +56 -0
- package/dist/engine/cost-store.d.ts +22 -0
- package/dist/engine/cost-store.js +15 -0
- package/dist/engine/engine.d.ts +295 -0
- package/dist/engine/engine.js +1467 -0
- package/dist/engine/model-facade.d.ts +24 -0
- package/dist/engine/model-facade.js +194 -0
- package/dist/engine/parse-task.d.ts +59 -0
- package/dist/engine/parse-task.js +135 -0
- package/dist/engine/patch-orphaned-tools.d.ts +52 -0
- package/dist/engine/patch-orphaned-tools.js +95 -0
- package/dist/engine/query.d.ts +50 -0
- package/dist/engine/query.js +107 -0
- package/dist/engine/runtime.d.ts +50 -0
- package/dist/engine/runtime.js +59 -0
- package/dist/engine/streaming-tool-queue.d.ts +34 -0
- package/dist/engine/streaming-tool-queue.js +61 -0
- package/dist/engine/token-budget.d.ts +22 -0
- package/dist/engine/token-budget.js +40 -0
- package/dist/engine/tool-summary.d.ts +13 -0
- package/dist/engine/tool-summary.js +35 -0
- package/dist/engine/turn-loop.d.ts +122 -0
- package/dist/engine/turn-loop.js +602 -0
- package/dist/engine/turn-state.d.ts +20 -0
- package/dist/engine/turn-state.js +17 -0
- package/dist/exceptions.d.ts +61 -0
- package/dist/exceptions.js +116 -0
- package/dist/git/utils.d.ts +35 -0
- package/dist/git/utils.js +98 -0
- package/dist/git/worktree.d.ts +36 -0
- package/dist/git/worktree.js +139 -0
- package/dist/hooks/events.d.ts +114 -0
- package/dist/hooks/events.js +4 -0
- package/dist/hooks/inject.d.ts +19 -0
- package/dist/hooks/inject.js +29 -0
- package/dist/hooks/registry.d.ts +15 -0
- package/dist/hooks/registry.js +84 -0
- package/dist/hooks/shell-runner.d.ts +45 -0
- package/dist/hooks/shell-runner.js +173 -0
- package/dist/index.d.ts +117 -0
- package/dist/index.js +145 -0
- package/dist/llm/api-key-sanitize.d.ts +22 -0
- package/dist/llm/api-key-sanitize.js +81 -0
- package/dist/llm/capabilities/index.d.ts +20 -0
- package/dist/llm/capabilities/index.js +39 -0
- package/dist/llm/capabilities/rules.d.ts +26 -0
- package/dist/llm/capabilities/rules.js +215 -0
- package/dist/llm/capabilities/types.d.ts +114 -0
- package/dist/llm/capabilities/types.js +24 -0
- package/dist/llm/client-base.d.ts +29 -0
- package/dist/llm/client-base.js +137 -0
- package/dist/llm/client-factory.d.ts +10 -0
- package/dist/llm/client-factory.js +29 -0
- package/dist/llm/model-cache.d.ts +21 -0
- package/dist/llm/model-cache.js +46 -0
- package/dist/llm/model-fetcher.d.ts +29 -0
- package/dist/llm/model-fetcher.js +282 -0
- package/dist/llm/model-pool.d.ts +95 -0
- package/dist/llm/model-pool.js +246 -0
- package/dist/llm/provider-catalog.d.ts +32 -0
- package/dist/llm/provider-catalog.js +50 -0
- package/dist/llm/provider-kinds.d.ts +21 -0
- package/dist/llm/provider-kinds.js +132 -0
- package/dist/llm/providers/anthropic.d.ts +19 -0
- package/dist/llm/providers/anthropic.js +267 -0
- package/dist/llm/providers/openai.d.ts +64 -0
- package/dist/llm/providers/openai.js +606 -0
- package/dist/llm/retry.d.ts +12 -0
- package/dist/llm/retry.js +17 -0
- package/dist/llm/stream-watchdog.d.ts +42 -0
- package/dist/llm/stream-watchdog.js +70 -0
- package/dist/llm/token-counter.d.ts +17 -0
- package/dist/llm/token-counter.js +36 -0
- package/dist/llm/types.d.ts +37 -0
- package/dist/llm/types.js +4 -0
- package/dist/logging/logger.d.ts +109 -0
- package/dist/logging/logger.js +390 -0
- package/dist/logging/sanitize-messages.d.ts +42 -0
- package/dist/logging/sanitize-messages.js +150 -0
- package/dist/logging/session-recorder.d.ts +90 -0
- package/dist/logging/session-recorder.js +304 -0
- package/dist/lsp/client.d.ts +41 -0
- package/dist/lsp/client.js +166 -0
- package/dist/lsp/manager.d.ts +40 -0
- package/dist/lsp/manager.js +124 -0
- package/dist/lsp/servers.d.ts +16 -0
- package/dist/lsp/servers.js +60 -0
- package/dist/migrate-models.d.ts +45 -0
- package/dist/migrate-models.js +179 -0
- package/dist/onboarding.d.ts +189 -0
- package/dist/onboarding.js +625 -0
- package/dist/plugins/gitOps.d.ts +32 -0
- package/dist/plugins/gitOps.js +62 -0
- package/dist/plugins/installedPlugins.d.ts +15 -0
- package/dist/plugins/installedPlugins.js +56 -0
- package/dist/plugins/knownMarketplaces.d.ts +10 -0
- package/dist/plugins/knownMarketplaces.js +46 -0
- package/dist/plugins/loadPluginHooks.d.ts +52 -0
- package/dist/plugins/loadPluginHooks.js +162 -0
- package/dist/plugins/marketplaceManager.d.ts +49 -0
- package/dist/plugins/marketplaceManager.js +142 -0
- package/dist/plugins/parseMarketplaceInput.d.ts +16 -0
- package/dist/plugins/parseMarketplaceInput.js +73 -0
- package/dist/plugins/pluginCommandHook.d.ts +54 -0
- package/dist/plugins/pluginCommandHook.js +180 -0
- package/dist/plugins/pluginCommandsLoader.d.ts +25 -0
- package/dist/plugins/pluginCommandsLoader.js +114 -0
- package/dist/plugins/pluginInstaller.d.ts +36 -0
- package/dist/plugins/pluginInstaller.js +228 -0
- package/dist/plugins/schemas.d.ts +9 -0
- package/dist/plugins/schemas.js +136 -0
- package/dist/plugins/types.d.ts +75 -0
- package/dist/plugins/types.js +7 -0
- package/dist/plugins/varRewrite.d.ts +42 -0
- package/dist/plugins/varRewrite.js +129 -0
- package/dist/preset/index.d.ts +60 -0
- package/dist/preset/index.js +159 -0
- package/dist/product/define.d.ts +60 -0
- package/dist/product/define.js +123 -0
- package/dist/product/index.d.ts +5 -0
- package/dist/product/index.js +4 -0
- package/dist/product/types.d.ts +111 -0
- package/dist/product/types.js +21 -0
- package/dist/prompt/composer.d.ts +49 -0
- package/dist/prompt/composer.js +168 -0
- package/dist/prompt/instruction-scanner.d.ts +54 -0
- package/dist/prompt/instruction-scanner.js +187 -0
- package/dist/prompt/section-cache.d.ts +15 -0
- package/dist/prompt/section-cache.js +31 -0
- package/dist/prompt/section-loader.d.ts +26 -0
- package/dist/prompt/section-loader.js +53 -0
- package/dist/prompt/sections/base.md +35 -0
- package/dist/prompt/sections/coding.md +31 -0
- package/dist/prompt/sections/orchestration.md +16 -0
- package/dist/prompt/sections/tone.md +9 -0
- package/dist/protocol/chat-session-manager.d.ts +33 -0
- package/dist/protocol/chat-session-manager.js +65 -0
- package/dist/protocol/chat-session.d.ts +49 -0
- package/dist/protocol/chat-session.js +86 -0
- package/dist/protocol/client.d.ts +104 -0
- package/dist/protocol/client.js +260 -0
- package/dist/protocol/factories.d.ts +93 -0
- package/dist/protocol/factories.js +64 -0
- package/dist/protocol/helpers.d.ts +42 -0
- package/dist/protocol/helpers.js +55 -0
- package/dist/protocol/index.d.ts +4 -0
- package/dist/protocol/index.js +4 -0
- package/dist/protocol/server.d.ts +80 -0
- package/dist/protocol/server.js +855 -0
- package/dist/protocol/transport.d.ts +37 -0
- package/dist/protocol/transport.js +85 -0
- package/dist/protocol/types.d.ts +221 -0
- package/dist/protocol/types.js +61 -0
- package/dist/remote/bridge.d.ts +37 -0
- package/dist/remote/bridge.js +103 -0
- package/dist/run/ArtifactTracker.d.ts +38 -0
- package/dist/run/ArtifactTracker.js +132 -0
- package/dist/run/CheckpointWriter.d.ts +53 -0
- package/dist/run/CheckpointWriter.js +135 -0
- package/dist/run/EngineRunner.d.ts +72 -0
- package/dist/run/EngineRunner.js +119 -0
- package/dist/run/Evaluator.d.ts +56 -0
- package/dist/run/Evaluator.js +62 -0
- package/dist/run/FileRunStore.d.ts +39 -0
- package/dist/run/FileRunStore.js +176 -0
- package/dist/run/Heartbeat.d.ts +54 -0
- package/dist/run/Heartbeat.js +123 -0
- package/dist/run/RunApprovalBackend.d.ts +53 -0
- package/dist/run/RunApprovalBackend.js +85 -0
- package/dist/run/RunLock.d.ts +40 -0
- package/dist/run/RunLock.js +98 -0
- package/dist/run/RunManager.d.ts +82 -0
- package/dist/run/RunManager.js +587 -0
- package/dist/run/RunQueue.d.ts +27 -0
- package/dist/run/RunQueue.js +73 -0
- package/dist/run/RunStore.d.ts +25 -0
- package/dist/run/RunStore.js +9 -0
- package/dist/run/factory.d.ts +63 -0
- package/dist/run/factory.js +51 -0
- package/dist/run/index.d.ts +17 -0
- package/dist/run/index.js +23 -0
- package/dist/run/types.d.ts +136 -0
- package/dist/run/types.js +28 -0
- package/dist/runtime/safe-spawn.d.ts +118 -0
- package/dist/runtime/safe-spawn.js +268 -0
- package/dist/services/analytics.d.ts +29 -0
- package/dist/services/analytics.js +84 -0
- package/dist/services/auto-dream.d.ts +49 -0
- package/dist/services/auto-dream.js +124 -0
- package/dist/services/diagnostics.d.ts +41 -0
- package/dist/services/diagnostics.js +99 -0
- package/dist/services/extract-memories.d.ts +31 -0
- package/dist/services/extract-memories.js +71 -0
- package/dist/services/index.d.ts +11 -0
- package/dist/services/index.js +17 -0
- package/dist/services/memory-orchestrator.d.ts +60 -0
- package/dist/services/memory-orchestrator.js +133 -0
- package/dist/services/notifier.d.ts +25 -0
- package/dist/services/notifier.js +69 -0
- package/dist/services/oauth.d.ts +27 -0
- package/dist/services/oauth.js +153 -0
- package/dist/services/session-memory.d.ts +37 -0
- package/dist/services/session-memory.js +81 -0
- package/dist/session/file-history.d.ts +41 -0
- package/dist/session/file-history.js +115 -0
- package/dist/session/memory.d.ts +104 -0
- package/dist/session/memory.js +310 -0
- package/dist/session/session-manager.d.ts +41 -0
- package/dist/session/session-manager.js +261 -0
- package/dist/session/transcript.d.ts +39 -0
- package/dist/session/transcript.js +186 -0
- package/dist/settings/manager.d.ts +36 -0
- package/dist/settings/manager.js +187 -0
- package/dist/settings/schema.d.ts +1219 -0
- package/dist/settings/schema.js +246 -0
- package/dist/skills/frontmatter.d.ts +13 -0
- package/dist/skills/frontmatter.js +70 -0
- package/dist/skills/index.d.ts +7 -0
- package/dist/skills/index.js +6 -0
- package/dist/skills/scanner.d.ts +31 -0
- package/dist/skills/scanner.js +171 -0
- package/dist/state.d.ts +160 -0
- package/dist/state.js +267 -0
- package/dist/tool-system/builtin/agent-notifications.d.ts +84 -0
- package/dist/tool-system/builtin/agent-notifications.js +197 -0
- package/dist/tool-system/builtin/agent-registry.d.ts +67 -0
- package/dist/tool-system/builtin/agent-registry.js +126 -0
- package/dist/tool-system/builtin/agent-transcript-translator.d.ts +22 -0
- package/dist/tool-system/builtin/agent-transcript-translator.js +160 -0
- package/dist/tool-system/builtin/agent.d.ts +16 -0
- package/dist/tool-system/builtin/agent.js +364 -0
- package/dist/tool-system/builtin/apply-patch/applier.d.ts +26 -0
- package/dist/tool-system/builtin/apply-patch/applier.js +250 -0
- package/dist/tool-system/builtin/apply-patch/index.d.ts +20 -0
- package/dist/tool-system/builtin/apply-patch/index.js +101 -0
- package/dist/tool-system/builtin/apply-patch/parser.d.ts +17 -0
- package/dist/tool-system/builtin/apply-patch/parser.js +212 -0
- package/dist/tool-system/builtin/apply-patch/seek-sequence.d.ts +18 -0
- package/dist/tool-system/builtin/apply-patch/seek-sequence.js +123 -0
- package/dist/tool-system/builtin/apply-patch/types.d.ts +49 -0
- package/dist/tool-system/builtin/apply-patch/types.js +13 -0
- package/dist/tool-system/builtin/arena.d.ts +31 -0
- package/dist/tool-system/builtin/arena.js +416 -0
- package/dist/tool-system/builtin/ask-user.d.ts +20 -0
- package/dist/tool-system/builtin/ask-user.js +99 -0
- package/dist/tool-system/builtin/bash.d.ts +21 -0
- package/dist/tool-system/builtin/bash.js +149 -0
- package/dist/tool-system/builtin/brief.d.ts +6 -0
- package/dist/tool-system/builtin/brief.js +41 -0
- package/dist/tool-system/builtin/config.d.ts +6 -0
- package/dist/tool-system/builtin/config.js +67 -0
- package/dist/tool-system/builtin/cron.d.ts +10 -0
- package/dist/tool-system/builtin/cron.js +61 -0
- package/dist/tool-system/builtin/edit.d.ts +6 -0
- package/dist/tool-system/builtin/edit.js +100 -0
- package/dist/tool-system/builtin/file-cache.d.ts +11 -0
- package/dist/tool-system/builtin/file-cache.js +39 -0
- package/dist/tool-system/builtin/glob.d.ts +7 -0
- package/dist/tool-system/builtin/glob.js +76 -0
- package/dist/tool-system/builtin/grep.d.ts +7 -0
- package/dist/tool-system/builtin/grep.js +150 -0
- package/dist/tool-system/builtin/index.d.ts +20 -0
- package/dist/tool-system/builtin/index.js +449 -0
- package/dist/tool-system/builtin/lsp.d.ts +6 -0
- package/dist/tool-system/builtin/lsp.js +141 -0
- package/dist/tool-system/builtin/mcp-tools.d.ts +10 -0
- package/dist/tool-system/builtin/mcp-tools.js +102 -0
- package/dist/tool-system/builtin/memory.d.ts +26 -0
- package/dist/tool-system/builtin/memory.js +226 -0
- package/dist/tool-system/builtin/notebook-edit.d.ts +6 -0
- package/dist/tool-system/builtin/notebook-edit.js +124 -0
- package/dist/tool-system/builtin/plan.d.ts +15 -0
- package/dist/tool-system/builtin/plan.js +55 -0
- package/dist/tool-system/builtin/powershell.d.ts +11 -0
- package/dist/tool-system/builtin/powershell.js +64 -0
- package/dist/tool-system/builtin/read.d.ts +6 -0
- package/dist/tool-system/builtin/read.js +70 -0
- package/dist/tool-system/builtin/remote-trigger.d.ts +6 -0
- package/dist/tool-system/builtin/remote-trigger.js +54 -0
- package/dist/tool-system/builtin/repl.d.ts +11 -0
- package/dist/tool-system/builtin/repl.js +82 -0
- package/dist/tool-system/builtin/send-message.d.ts +6 -0
- package/dist/tool-system/builtin/send-message.js +47 -0
- package/dist/tool-system/builtin/skill-prompt.d.ts +20 -0
- package/dist/tool-system/builtin/skill-prompt.js +52 -0
- package/dist/tool-system/builtin/skill.d.ts +9 -0
- package/dist/tool-system/builtin/skill.js +55 -0
- package/dist/tool-system/builtin/sleep.d.ts +6 -0
- package/dist/tool-system/builtin/sleep.js +33 -0
- package/dist/tool-system/builtin/task.d.ts +48 -0
- package/dist/tool-system/builtin/task.js +139 -0
- package/dist/tool-system/builtin/tool-search.d.ts +11 -0
- package/dist/tool-system/builtin/tool-search.js +90 -0
- package/dist/tool-system/builtin/web-fetch.d.ts +10 -0
- package/dist/tool-system/builtin/web-fetch.js +328 -0
- package/dist/tool-system/builtin/web-search.d.ts +25 -0
- package/dist/tool-system/builtin/web-search.js +174 -0
- package/dist/tool-system/builtin/worktree.d.ts +10 -0
- package/dist/tool-system/builtin/worktree.js +95 -0
- package/dist/tool-system/builtin/write.d.ts +6 -0
- package/dist/tool-system/builtin/write.js +36 -0
- package/dist/tool-system/context.d.ts +165 -0
- package/dist/tool-system/context.js +18 -0
- package/dist/tool-system/executor.d.ts +49 -0
- package/dist/tool-system/executor.js +495 -0
- package/dist/tool-system/investigation-guard.d.ts +46 -0
- package/dist/tool-system/investigation-guard.js +155 -0
- package/dist/tool-system/mcp-manager.d.ts +67 -0
- package/dist/tool-system/mcp-manager.js +249 -0
- package/dist/tool-system/permission.d.ts +111 -0
- package/dist/tool-system/permission.js +774 -0
- package/dist/tool-system/registry.d.ts +32 -0
- package/dist/tool-system/registry.js +131 -0
- package/dist/tool-system/sandbox/bwrap.d.ts +13 -0
- package/dist/tool-system/sandbox/bwrap.js +63 -0
- package/dist/tool-system/sandbox/index.d.ts +78 -0
- package/dist/tool-system/sandbox/index.js +222 -0
- package/dist/tool-system/sandbox/off.d.ts +2 -0
- package/dist/tool-system/sandbox/off.js +8 -0
- package/dist/tool-system/sandbox/seatbelt.d.ts +18 -0
- package/dist/tool-system/sandbox/seatbelt.js +109 -0
- package/dist/tool-system/task-guard.d.ts +29 -0
- package/dist/tool-system/task-guard.js +70 -0
- package/dist/tool-system/validation.d.ts +5 -0
- package/dist/tool-system/validation.js +39 -0
- package/dist/types.d.ts +376 -0
- package/dist/types.js +4 -0
- package/dist/updater.d.ts +65 -0
- package/dist/updater.js +405 -0
- package/dist/utils/debug.d.ts +41 -0
- package/dist/utils/debug.js +110 -0
- package/dist/utils/earlyInput.d.ts +43 -0
- package/dist/utils/earlyInput.js +166 -0
- package/dist/utils/env.d.ts +24 -0
- package/dist/utils/env.js +104 -0
- package/dist/utils/envUtils.d.ts +51 -0
- package/dist/utils/envUtils.js +135 -0
- package/dist/utils/execFileNoThrow.d.ts +39 -0
- package/dist/utils/execFileNoThrow.js +74 -0
- package/dist/utils/format.d.ts +44 -0
- package/dist/utils/format.js +236 -0
- package/dist/utils/intl.d.ts +22 -0
- package/dist/utils/intl.js +83 -0
- package/dist/utils/lockfile.d.ts +15 -0
- package/dist/utils/lockfile.js +30 -0
- package/dist/utils/memoize.d.ts +19 -0
- package/dist/utils/memoize.js +14 -0
- package/dist/utils/semver.d.ts +6 -0
- package/dist/utils/semver.js +13 -0
- package/dist/utils/sliceAnsi.d.ts +12 -0
- package/dist/utils/sliceAnsi.js +62 -0
- package/dist/utils/systemTheme.d.ts +40 -0
- package/dist/utils/systemTheme.js +108 -0
- package/dist/utils/task-sanitizer.d.ts +6 -0
- package/dist/utils/task-sanitizer.js +24 -0
- package/dist/utils/theme.d.ts +93 -0
- package/dist/utils/theme.js +544 -0
- package/dist/utils/toolDisplay.d.ts +31 -0
- package/dist/utils/toolDisplay.js +188 -0
- package/package.json +72 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level query API — async generator wrapping TurnLoop.
|
|
3
|
+
*
|
|
4
|
+
* Callers consume events via `for await (const event of query(params))`.
|
|
5
|
+
* This is the public entry point for running agentic queries; TurnLoop
|
|
6
|
+
* is the internal state machine.
|
|
7
|
+
*
|
|
8
|
+
* Adapted from restored-src/src/query.ts, simplified for Code Shell.
|
|
9
|
+
*/
|
|
10
|
+
import { TurnLoop } from "./turn-loop.js";
|
|
11
|
+
import { logger } from "../logging/logger.js";
|
|
12
|
+
// ─── Query generator ────────────────────────────────────────────────
|
|
13
|
+
/**
|
|
14
|
+
* Run an agentic query and yield stream events as they occur.
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* const events: StreamEvent[] = [];
|
|
18
|
+
* for await (const event of query(params)) {
|
|
19
|
+
* events.push(event);
|
|
20
|
+
* }
|
|
21
|
+
* // last event contains the final result
|
|
22
|
+
*/
|
|
23
|
+
export async function* query(params) {
|
|
24
|
+
const { messages, systemPrompt, tools, maxTurns = 30, maxToolCallsPerTurn = 20, signal, deps, } = params;
|
|
25
|
+
// Collect events in a queue that the stream callback pushes to
|
|
26
|
+
const eventQueue = [];
|
|
27
|
+
let resolveWait = null;
|
|
28
|
+
const onStream = (event) => {
|
|
29
|
+
eventQueue.push(event);
|
|
30
|
+
if (resolveWait) {
|
|
31
|
+
resolveWait();
|
|
32
|
+
resolveWait = null;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const config = {
|
|
36
|
+
maxTurns,
|
|
37
|
+
maxToolCallsPerTurn,
|
|
38
|
+
onStream,
|
|
39
|
+
signal,
|
|
40
|
+
};
|
|
41
|
+
// Local overhead store — query() is a standalone entry point without a real
|
|
42
|
+
// session id, so per-call in-memory state is enough.
|
|
43
|
+
let localOverhead = 0;
|
|
44
|
+
const loopDeps = {
|
|
45
|
+
model: deps.model,
|
|
46
|
+
toolExecutor: deps.toolExecutor,
|
|
47
|
+
contextManager: deps.contextManager,
|
|
48
|
+
hooks: deps.hooks,
|
|
49
|
+
transcript: deps.transcript,
|
|
50
|
+
systemPrompt,
|
|
51
|
+
tools,
|
|
52
|
+
sessionId: "query",
|
|
53
|
+
ctxOverheadStore: {
|
|
54
|
+
get: () => localOverhead,
|
|
55
|
+
set: (_s, n) => {
|
|
56
|
+
localOverhead = n;
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
const loop = new TurnLoop(loopDeps, config);
|
|
61
|
+
// Start the turn loop in the background
|
|
62
|
+
let result;
|
|
63
|
+
let loopError;
|
|
64
|
+
let loopDone = false;
|
|
65
|
+
const loopPromise = loop
|
|
66
|
+
.run(messages)
|
|
67
|
+
.then((r) => {
|
|
68
|
+
result = r;
|
|
69
|
+
loopDone = true;
|
|
70
|
+
// Wake up the yield loop
|
|
71
|
+
if (resolveWait) {
|
|
72
|
+
resolveWait();
|
|
73
|
+
resolveWait = null;
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
.catch((err) => {
|
|
77
|
+
loopError = err;
|
|
78
|
+
loopDone = true;
|
|
79
|
+
if (resolveWait) {
|
|
80
|
+
resolveWait();
|
|
81
|
+
resolveWait = null;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
// Yield events as they arrive
|
|
85
|
+
while (!loopDone || eventQueue.length > 0) {
|
|
86
|
+
if (eventQueue.length > 0) {
|
|
87
|
+
yield eventQueue.shift();
|
|
88
|
+
}
|
|
89
|
+
else if (!loopDone) {
|
|
90
|
+
// Wait for next event or loop completion
|
|
91
|
+
await new Promise((resolve) => {
|
|
92
|
+
resolveWait = resolve;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Ensure the loop promise is settled
|
|
97
|
+
await loopPromise;
|
|
98
|
+
if (loopError) {
|
|
99
|
+
logger.error("query.error", { error: loopError.message });
|
|
100
|
+
throw loopError;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
text: result.text,
|
|
104
|
+
reason: result.reason,
|
|
105
|
+
turnCount: loop.currentTurn,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ModelPool } from "../llm/model-pool.js";
|
|
2
|
+
import type { ToolRegistry } from "../tool-system/registry.js";
|
|
3
|
+
import type { SettingsManager } from "../settings/manager.js";
|
|
4
|
+
import type { MCPManager } from "../tool-system/mcp-manager.js";
|
|
5
|
+
import type { CostTracker } from "../cost-tracker.js";
|
|
6
|
+
import { type SandboxBackend, type SandboxConfig } from "../tool-system/sandbox/index.js";
|
|
7
|
+
export interface EngineRuntimeOptions {
|
|
8
|
+
modelPool: ModelPool;
|
|
9
|
+
toolRegistry: ToolRegistry;
|
|
10
|
+
settings: SettingsManager;
|
|
11
|
+
mcpPool: MCPManager;
|
|
12
|
+
costTracker: CostTracker;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Shared read-only resources used by all Engine instances in a worker.
|
|
16
|
+
* Mutable per-session state stays on Engine itself.
|
|
17
|
+
*/
|
|
18
|
+
export declare class EngineRuntime {
|
|
19
|
+
readonly modelPool: ModelPool;
|
|
20
|
+
readonly toolRegistry: ToolRegistry;
|
|
21
|
+
readonly settings: SettingsManager;
|
|
22
|
+
readonly mcpPool: MCPManager;
|
|
23
|
+
readonly costTracker: CostTracker;
|
|
24
|
+
private sandboxCache;
|
|
25
|
+
constructor(opts: EngineRuntimeOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Lazily resolve and cache a sandbox backend for `(mode, cwd)`.
|
|
28
|
+
*
|
|
29
|
+
* Explicit modes (`seatbelt`, `bwrap`) propagate the underlying
|
|
30
|
+
* `resolveSandboxBackend` throw — there is no silent downgrade. Only
|
|
31
|
+
* `auto` may degrade to `off` (handled inside the resolver itself).
|
|
32
|
+
* The Engine must not catch the throw at turn time; doing so would
|
|
33
|
+
* violate standard §S4 fail-closed rule.
|
|
34
|
+
*/
|
|
35
|
+
resolveSandbox(config: SandboxConfig, cwd: string): Promise<SandboxBackend>;
|
|
36
|
+
/**
|
|
37
|
+
* Tear down runtime-owned resources. Safe to call multiple times; the
|
|
38
|
+
* MCPManager's `disconnectAll` clears its own connection map and the
|
|
39
|
+
* sandbox cache holds resolved-backend promises that don't need
|
|
40
|
+
* explicit cleanup (they're plain objects). Closes Gate 2 bullet
|
|
41
|
+
* "runtime close shuts down MCP connections, timers, and background
|
|
42
|
+
* work."
|
|
43
|
+
*
|
|
44
|
+
* Hosts that own a long-lived Runtime (the stdio worker, future
|
|
45
|
+
* Electron broker, codeshell-serve) should `await runtime.close()`
|
|
46
|
+
* before exiting the process — otherwise MCP child processes may be
|
|
47
|
+
* left running and bound ports may stay held until the OS cleans up.
|
|
48
|
+
*/
|
|
49
|
+
close(): Promise<void>;
|
|
50
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { resolveSandboxBackend, } from "../tool-system/sandbox/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Shared read-only resources used by all Engine instances in a worker.
|
|
4
|
+
* Mutable per-session state stays on Engine itself.
|
|
5
|
+
*/
|
|
6
|
+
export class EngineRuntime {
|
|
7
|
+
modelPool;
|
|
8
|
+
toolRegistry;
|
|
9
|
+
settings;
|
|
10
|
+
mcpPool;
|
|
11
|
+
costTracker;
|
|
12
|
+
// A2: cached sandbox backends, keyed by (mode + cwd). The capability
|
|
13
|
+
// probe (seatbelt availability, bwrap binary) and the per-turn
|
|
14
|
+
// downgrade-to-off warning fire at most once per (mode, cwd) pair
|
|
15
|
+
// per Runtime — not once per turn.
|
|
16
|
+
sandboxCache = new Map();
|
|
17
|
+
constructor(opts) {
|
|
18
|
+
this.modelPool = opts.modelPool;
|
|
19
|
+
this.toolRegistry = opts.toolRegistry;
|
|
20
|
+
this.settings = opts.settings;
|
|
21
|
+
this.mcpPool = opts.mcpPool;
|
|
22
|
+
this.costTracker = opts.costTracker;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Lazily resolve and cache a sandbox backend for `(mode, cwd)`.
|
|
26
|
+
*
|
|
27
|
+
* Explicit modes (`seatbelt`, `bwrap`) propagate the underlying
|
|
28
|
+
* `resolveSandboxBackend` throw — there is no silent downgrade. Only
|
|
29
|
+
* `auto` may degrade to `off` (handled inside the resolver itself).
|
|
30
|
+
* The Engine must not catch the throw at turn time; doing so would
|
|
31
|
+
* violate standard §S4 fail-closed rule.
|
|
32
|
+
*/
|
|
33
|
+
resolveSandbox(config, cwd) {
|
|
34
|
+
const key = `${config.mode}:${cwd}`;
|
|
35
|
+
let cached = this.sandboxCache.get(key);
|
|
36
|
+
if (!cached) {
|
|
37
|
+
cached = resolveSandboxBackend(config, cwd);
|
|
38
|
+
this.sandboxCache.set(key, cached);
|
|
39
|
+
}
|
|
40
|
+
return cached;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Tear down runtime-owned resources. Safe to call multiple times; the
|
|
44
|
+
* MCPManager's `disconnectAll` clears its own connection map and the
|
|
45
|
+
* sandbox cache holds resolved-backend promises that don't need
|
|
46
|
+
* explicit cleanup (they're plain objects). Closes Gate 2 bullet
|
|
47
|
+
* "runtime close shuts down MCP connections, timers, and background
|
|
48
|
+
* work."
|
|
49
|
+
*
|
|
50
|
+
* Hosts that own a long-lived Runtime (the stdio worker, future
|
|
51
|
+
* Electron broker, codeshell-serve) should `await runtime.close()`
|
|
52
|
+
* before exiting the process — otherwise MCP child processes may be
|
|
53
|
+
* left running and bound ports may stay held until the OS cleans up.
|
|
54
|
+
*/
|
|
55
|
+
async close() {
|
|
56
|
+
await this.mcpPool.disconnectAll();
|
|
57
|
+
this.sandboxCache.clear();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming tool queue — enqueues tools for execution during streaming,
|
|
3
|
+
* starts concurrency-safe tools immediately, queues unsafe tools for
|
|
4
|
+
* sequential execution.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* const queue = new StreamingToolQueue(executor);
|
|
8
|
+
* // During streaming, as tool_use blocks arrive:
|
|
9
|
+
* queue.enqueue(toolCall);
|
|
10
|
+
* // After streaming completes:
|
|
11
|
+
* const results = await queue.drain();
|
|
12
|
+
*/
|
|
13
|
+
import type { ToolCall, ToolResult } from "../types.js";
|
|
14
|
+
import type { ToolExecutor } from "../tool-system/executor.js";
|
|
15
|
+
export declare class StreamingToolQueue {
|
|
16
|
+
private readonly executor;
|
|
17
|
+
private readonly pending;
|
|
18
|
+
private readonly unsafeQueue;
|
|
19
|
+
private readonly callOrder;
|
|
20
|
+
private draining;
|
|
21
|
+
constructor(executor: ToolExecutor);
|
|
22
|
+
/**
|
|
23
|
+
* Enqueue a tool for execution. Concurrency-safe tools start immediately;
|
|
24
|
+
* unsafe tools are queued for sequential execution during drain().
|
|
25
|
+
*/
|
|
26
|
+
enqueue(call: ToolCall): void;
|
|
27
|
+
/**
|
|
28
|
+
* Execute all remaining unsafe tools sequentially, then await all results.
|
|
29
|
+
* Returns results in original enqueue order.
|
|
30
|
+
*/
|
|
31
|
+
drain(): Promise<ToolResult[]>;
|
|
32
|
+
/** Number of tools currently enqueued or executing. */
|
|
33
|
+
get size(): number;
|
|
34
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming tool queue — enqueues tools for execution during streaming,
|
|
3
|
+
* starts concurrency-safe tools immediately, queues unsafe tools for
|
|
4
|
+
* sequential execution.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* const queue = new StreamingToolQueue(executor);
|
|
8
|
+
* // During streaming, as tool_use blocks arrive:
|
|
9
|
+
* queue.enqueue(toolCall);
|
|
10
|
+
* // After streaming completes:
|
|
11
|
+
* const results = await queue.drain();
|
|
12
|
+
*/
|
|
13
|
+
export class StreamingToolQueue {
|
|
14
|
+
executor;
|
|
15
|
+
pending = new Map();
|
|
16
|
+
unsafeQueue = [];
|
|
17
|
+
callOrder = [];
|
|
18
|
+
draining = false;
|
|
19
|
+
constructor(executor) {
|
|
20
|
+
this.executor = executor;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Enqueue a tool for execution. Concurrency-safe tools start immediately;
|
|
24
|
+
* unsafe tools are queued for sequential execution during drain().
|
|
25
|
+
*/
|
|
26
|
+
enqueue(call) {
|
|
27
|
+
this.callOrder.push(call.id);
|
|
28
|
+
if (this.executor.isConcurrencySafe(call.toolName)) {
|
|
29
|
+
// Start immediately
|
|
30
|
+
this.pending.set(call.id, this.executor.executeSingle(call));
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
this.unsafeQueue.push(call);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Execute all remaining unsafe tools sequentially, then await all results.
|
|
38
|
+
* Returns results in original enqueue order.
|
|
39
|
+
*/
|
|
40
|
+
async drain() {
|
|
41
|
+
if (this.draining)
|
|
42
|
+
throw new Error("StreamingToolQueue already draining");
|
|
43
|
+
this.draining = true;
|
|
44
|
+
// Execute unsafe tools sequentially
|
|
45
|
+
for (const call of this.unsafeQueue) {
|
|
46
|
+
this.pending.set(call.id, this.executor.executeSingle(call));
|
|
47
|
+
await this.pending.get(call.id);
|
|
48
|
+
}
|
|
49
|
+
// Await all results
|
|
50
|
+
const resultMap = new Map();
|
|
51
|
+
for (const [id, promise] of this.pending) {
|
|
52
|
+
resultMap.set(id, await promise);
|
|
53
|
+
}
|
|
54
|
+
// Return in original order
|
|
55
|
+
return this.callOrder.map((id) => resultMap.get(id));
|
|
56
|
+
}
|
|
57
|
+
/** Number of tools currently enqueued or executing. */
|
|
58
|
+
get size() {
|
|
59
|
+
return this.callOrder.length;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token budget — decides whether the agent loop should continue, nudge, or stop.
|
|
3
|
+
*
|
|
4
|
+
* - "continue": keep going normally
|
|
5
|
+
* - "nudge": inject a "please wrap up" message, but continue
|
|
6
|
+
* - "stop": terminate the loop gracefully
|
|
7
|
+
*/
|
|
8
|
+
export interface BudgetTracker {
|
|
9
|
+
continuationCount: number;
|
|
10
|
+
lastDeltaTokens: number;
|
|
11
|
+
prevTurnTokens: number;
|
|
12
|
+
}
|
|
13
|
+
export declare function createBudgetTracker(): BudgetTracker;
|
|
14
|
+
export type BudgetDecision = "continue" | "nudge" | "stop";
|
|
15
|
+
/**
|
|
16
|
+
* Check if the turn loop should continue based on token output budget.
|
|
17
|
+
*
|
|
18
|
+
* @param turnOutputTokens - tokens output so far this turn
|
|
19
|
+
* @param budget - max tokens allowed for this turn (Infinity = no limit)
|
|
20
|
+
* @param tracker - mutable tracker state (updated in place)
|
|
21
|
+
*/
|
|
22
|
+
export declare function checkTokenBudget(turnOutputTokens: number, budget: number, tracker: BudgetTracker): BudgetDecision;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token budget — decides whether the agent loop should continue, nudge, or stop.
|
|
3
|
+
*
|
|
4
|
+
* - "continue": keep going normally
|
|
5
|
+
* - "nudge": inject a "please wrap up" message, but continue
|
|
6
|
+
* - "stop": terminate the loop gracefully
|
|
7
|
+
*/
|
|
8
|
+
export function createBudgetTracker() {
|
|
9
|
+
return { continuationCount: 0, lastDeltaTokens: Infinity, prevTurnTokens: 0 };
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Check if the turn loop should continue based on token output budget.
|
|
13
|
+
*
|
|
14
|
+
* @param turnOutputTokens - tokens output so far this turn
|
|
15
|
+
* @param budget - max tokens allowed for this turn (Infinity = no limit)
|
|
16
|
+
* @param tracker - mutable tracker state (updated in place)
|
|
17
|
+
*/
|
|
18
|
+
export function checkTokenBudget(turnOutputTokens, budget, tracker) {
|
|
19
|
+
if (!isFinite(budget) || budget <= 0)
|
|
20
|
+
return "continue";
|
|
21
|
+
const pct = turnOutputTokens / budget;
|
|
22
|
+
const delta = turnOutputTokens - tracker.prevTurnTokens;
|
|
23
|
+
// Diminishing returns: 3+ continuations and last two deltas both < 500
|
|
24
|
+
const isDiminishing = tracker.continuationCount >= 3 &&
|
|
25
|
+
delta < 500 &&
|
|
26
|
+
tracker.lastDeltaTokens < 500;
|
|
27
|
+
// Update tracker
|
|
28
|
+
tracker.lastDeltaTokens = delta;
|
|
29
|
+
tracker.prevTurnTokens = turnOutputTokens;
|
|
30
|
+
// Stop: at/above threshold with prior nudge, or diminishing returns
|
|
31
|
+
if (tracker.continuationCount > 0 && (isDiminishing || pct >= 0.9)) {
|
|
32
|
+
return "stop";
|
|
33
|
+
}
|
|
34
|
+
// Nudge: approaching budget threshold
|
|
35
|
+
if (pct >= 0.9) {
|
|
36
|
+
tracker.continuationCount++;
|
|
37
|
+
return "nudge";
|
|
38
|
+
}
|
|
39
|
+
return "continue";
|
|
40
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool use summary — generates a 1-line progress label after tool batch execution.
|
|
3
|
+
*
|
|
4
|
+
* Runs async (fire-and-forget) so it doesn't block the next model call.
|
|
5
|
+
* Returns a short commit-subject-style string like "Read 3 files, fixed NPE in auth.ts"
|
|
6
|
+
*/
|
|
7
|
+
import type { ToolCall, ToolResult } from "../types.js";
|
|
8
|
+
export type SummarizeFn = (systemPrompt: string, userMessage: string) => Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Generate a 1-line summary of completed tool executions.
|
|
11
|
+
* Returns null on failure (non-critical).
|
|
12
|
+
*/
|
|
13
|
+
export declare function generateToolUseSummary(toolCalls: ToolCall[], results: ToolResult[], summarize: SummarizeFn): Promise<string | null>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool use summary — generates a 1-line progress label after tool batch execution.
|
|
3
|
+
*
|
|
4
|
+
* Runs async (fire-and-forget) so it doesn't block the next model call.
|
|
5
|
+
* Returns a short commit-subject-style string like "Read 3 files, fixed NPE in auth.ts"
|
|
6
|
+
*/
|
|
7
|
+
import { logger } from "../logging/logger.js";
|
|
8
|
+
const SUMMARY_SYSTEM_PROMPT = "Generate a very brief (under 40 chars) summary of what these tools did. " +
|
|
9
|
+
"Use past tense, commit-subject style. Examples: " +
|
|
10
|
+
'"Read 3 config files", "Fixed import in auth.ts", "Searched for API endpoints". ' +
|
|
11
|
+
"Respond with ONLY the summary text, nothing else.";
|
|
12
|
+
/**
|
|
13
|
+
* Generate a 1-line summary of completed tool executions.
|
|
14
|
+
* Returns null on failure (non-critical).
|
|
15
|
+
*/
|
|
16
|
+
export async function generateToolUseSummary(toolCalls, results, summarize) {
|
|
17
|
+
if (toolCalls.length === 0)
|
|
18
|
+
return null;
|
|
19
|
+
try {
|
|
20
|
+
const parts = [];
|
|
21
|
+
for (let i = 0; i < toolCalls.length; i++) {
|
|
22
|
+
const tc = toolCalls[i];
|
|
23
|
+
const result = results[i];
|
|
24
|
+
const input = JSON.stringify(tc.args).slice(0, 200);
|
|
25
|
+
const output = (result?.result ?? result?.error ?? "").slice(0, 300);
|
|
26
|
+
parts.push(`Tool: ${tc.toolName}(${input}) → ${output}`);
|
|
27
|
+
}
|
|
28
|
+
const summary = await summarize(SUMMARY_SYSTEM_PROMPT, parts.join("\n"));
|
|
29
|
+
return summary?.trim() || null;
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
logger.warn("tool_summary.failed", { error: err.message });
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn loop — state machine implementation.
|
|
3
|
+
*
|
|
4
|
+
* Following Claude Code's po_() pattern:
|
|
5
|
+
* pre_check → model_call → post_check → tool_exec → context_mgmt → hook_notify → next turn
|
|
6
|
+
*/
|
|
7
|
+
import type { Message, StreamCallback, TerminalReason } from "../types.js";
|
|
8
|
+
import { ModelFacade } from "./model-facade.js";
|
|
9
|
+
import { ToolExecutor } from "../tool-system/executor.js";
|
|
10
|
+
import { ContextManager } from "../context/manager.js";
|
|
11
|
+
import { HookRegistry } from "../hooks/registry.js";
|
|
12
|
+
import { Transcript } from "../session/transcript.js";
|
|
13
|
+
export interface TurnLoopConfig {
|
|
14
|
+
maxTurns: number;
|
|
15
|
+
maxToolCallsPerTurn: number;
|
|
16
|
+
tokenBudget?: number;
|
|
17
|
+
onStream?: StreamCallback;
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
/**
|
|
20
|
+
* Fired after each turn boundary is recorded. Lets the engine flush an
|
|
21
|
+
* up-to-date snapshot to state.json mid-run, so a long run doesn't leave
|
|
22
|
+
* the on-disk turnCount/status frozen at the last completion.
|
|
23
|
+
*/
|
|
24
|
+
onTurnBoundary?: (turnCount: number) => void;
|
|
25
|
+
}
|
|
26
|
+
export interface CtxOverheadStore {
|
|
27
|
+
/** Tokens for system prompt + tool defs, derived from provider's promptTokens. */
|
|
28
|
+
get(sid: string): number;
|
|
29
|
+
set(sid: string, tokens: number): void;
|
|
30
|
+
}
|
|
31
|
+
export interface TurnLoopDeps {
|
|
32
|
+
model: ModelFacade;
|
|
33
|
+
toolExecutor: ToolExecutor;
|
|
34
|
+
contextManager: ContextManager;
|
|
35
|
+
hooks: HookRegistry;
|
|
36
|
+
transcript: Transcript;
|
|
37
|
+
systemPrompt: string;
|
|
38
|
+
tools: import("../types.js").ToolDefinition[];
|
|
39
|
+
/** Per-sid overhead cache so the ctx bar doesn't drop between turns. */
|
|
40
|
+
ctxOverheadStore: CtxOverheadStore;
|
|
41
|
+
/** Current session id, used to key the overhead store. */
|
|
42
|
+
sessionId: string;
|
|
43
|
+
/**
|
|
44
|
+
* Carried into every hook emit's `ctx.data.isSubAgent` so handlers can
|
|
45
|
+
* skip noisy injections for spawned children. Set by Engine from
|
|
46
|
+
* EngineConfig.isSubAgent (engine.ts:119).
|
|
47
|
+
*/
|
|
48
|
+
isSubAgent?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Reads/clears the most recent compaction event emitted by
|
|
51
|
+
* ContextManager since the last call. Returns `null` if no compaction
|
|
52
|
+
* fired since the previous check. The Engine wires this up so the
|
|
53
|
+
* turn loop can `post_compact` emit + inject hook messages without
|
|
54
|
+
* the ContextManager itself depending on HookRegistry.
|
|
55
|
+
*/
|
|
56
|
+
consumePendingCompactInfo?: () => {
|
|
57
|
+
strategy: string;
|
|
58
|
+
before: number;
|
|
59
|
+
after: number;
|
|
60
|
+
} | null;
|
|
61
|
+
}
|
|
62
|
+
export interface TurnLoopResult {
|
|
63
|
+
text: string;
|
|
64
|
+
reason: TerminalReason;
|
|
65
|
+
messages: Message[];
|
|
66
|
+
}
|
|
67
|
+
export declare class TurnLoop {
|
|
68
|
+
private readonly deps;
|
|
69
|
+
private readonly config;
|
|
70
|
+
private turnCount;
|
|
71
|
+
/** Tool IDs already emitted as tool_use_start during streaming (to avoid duplicates). */
|
|
72
|
+
private streamedToolIds;
|
|
73
|
+
/**
|
|
74
|
+
* Turn-scoped child logger set at the top of each loop iteration. Class
|
|
75
|
+
* methods invoked inside the iteration (callModelWithFallback,
|
|
76
|
+
* executeToolCall, ...) read it instead of using the root `logger`, so
|
|
77
|
+
* every line they write is tagged with the current turn/turnId.
|
|
78
|
+
*/
|
|
79
|
+
private currentTurnLog;
|
|
80
|
+
/** Last emitted ctx token estimate; used to skip no-op usage_update events. */
|
|
81
|
+
private lastCtxEmit;
|
|
82
|
+
constructor(deps: TurnLoopDeps, config: TurnLoopConfig);
|
|
83
|
+
/**
|
|
84
|
+
* Emit a lifecycle hook with isSubAgent + sessionId auto-merged into data.
|
|
85
|
+
* Returns the aggregated HookResult so callers can consume `messages` /
|
|
86
|
+
* `decision` / `stop`. Use this instead of `deps.hooks.emit` directly so
|
|
87
|
+
* every emit carries the same context envelope.
|
|
88
|
+
*/
|
|
89
|
+
private emitHook;
|
|
90
|
+
/**
|
|
91
|
+
* Emit a usage_update so the UI ctx bar reflects current message-array
|
|
92
|
+
* size. Called at every point where messages mutate: after a tool_result
|
|
93
|
+
* is appended, after context management (which may shrink), and after an
|
|
94
|
+
* LLM response (which we also feed through with the provider's authoritative
|
|
95
|
+
* promptTokens to override our estimate).
|
|
96
|
+
*/
|
|
97
|
+
private emitCtxFromMessages;
|
|
98
|
+
private emitCtxFromUsage;
|
|
99
|
+
/**
|
|
100
|
+
* Run the multi-turn agent loop until completion.
|
|
101
|
+
*/
|
|
102
|
+
run(initialMessages: Message[]): Promise<TurnLoopResult>;
|
|
103
|
+
/**
|
|
104
|
+
* Call model with streaming fallback.
|
|
105
|
+
* If streaming fails, emit tombstone and retry non-streaming.
|
|
106
|
+
*/
|
|
107
|
+
private callModelWithFallback;
|
|
108
|
+
/**
|
|
109
|
+
* Execute tools with overlap: start concurrent-safe (read-only) tools
|
|
110
|
+
* immediately in parallel while sequential (write) tools run one-by-one.
|
|
111
|
+
* Both groups run simultaneously — we don't wait for safe tools to finish
|
|
112
|
+
* before starting unsafe ones.
|
|
113
|
+
*/
|
|
114
|
+
private executeToolsOverlapped;
|
|
115
|
+
get currentTurn(): number;
|
|
116
|
+
/**
|
|
117
|
+
* Generate synthetic error tool_results for any dangling tool_use blocks
|
|
118
|
+
* that never received results (e.g. because the API call failed).
|
|
119
|
+
* Prevents model confusion on the next turn.
|
|
120
|
+
*/
|
|
121
|
+
private patchOrphanedToolUses;
|
|
122
|
+
}
|