@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,33 @@
|
|
|
1
|
+
import { ChatSession } from "./chat-session.js";
|
|
2
|
+
import type { Engine } from "../engine/engine.js";
|
|
3
|
+
import type { EngineRuntime } from "../engine/runtime.js";
|
|
4
|
+
import type { EngineConfig } from "../engine/engine.js";
|
|
5
|
+
export type EngineConfigSlice = Pick<EngineConfig, "permissionMode" | "preset" | "customSystemPrompt" | "appendSystemPrompt" | "maxTurns" | "maxContextTokens" | "cwd">;
|
|
6
|
+
export interface ChatSessionManagerOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Shared runtime, supplied here so callers and engineFactory closures
|
|
9
|
+
* derived from these options can reference one canonical instance. The
|
|
10
|
+
* manager itself does not retain it — engineFactory captures it via closure.
|
|
11
|
+
*/
|
|
12
|
+
runtime: EngineRuntime;
|
|
13
|
+
/** Build an Engine. Tests inject a fake; production passes a runtime-backed Engine factory. */
|
|
14
|
+
engineFactory: (slice: EngineConfigSlice) => Engine;
|
|
15
|
+
maxSessions?: number;
|
|
16
|
+
idleTtlMs?: number;
|
|
17
|
+
}
|
|
18
|
+
export declare class ChatSessionManager {
|
|
19
|
+
private readonly sessions;
|
|
20
|
+
private readonly factory;
|
|
21
|
+
private readonly maxSessions;
|
|
22
|
+
private readonly idleTtlMs;
|
|
23
|
+
private sweeper;
|
|
24
|
+
constructor(opts: ChatSessionManagerOptions);
|
|
25
|
+
getOrCreate(sessionId: string, slice: EngineConfigSlice): ChatSession;
|
|
26
|
+
get(sessionId: string): ChatSession | undefined;
|
|
27
|
+
close(sessionId: string): void;
|
|
28
|
+
closeAll(): void;
|
|
29
|
+
sessionCount(): number;
|
|
30
|
+
sweepIdle(): void;
|
|
31
|
+
startIdleSweeper(intervalMs?: number): void;
|
|
32
|
+
stopIdleSweeper(): void;
|
|
33
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { ChatSession } from "./chat-session.js";
|
|
2
|
+
export class ChatSessionManager {
|
|
3
|
+
sessions = new Map();
|
|
4
|
+
factory;
|
|
5
|
+
maxSessions;
|
|
6
|
+
idleTtlMs;
|
|
7
|
+
sweeper = null;
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
this.factory = opts.engineFactory;
|
|
10
|
+
this.maxSessions = opts.maxSessions ?? 16;
|
|
11
|
+
this.idleTtlMs = opts.idleTtlMs ?? 30 * 60 * 1000;
|
|
12
|
+
}
|
|
13
|
+
getOrCreate(sessionId, slice) {
|
|
14
|
+
const existing = this.sessions.get(sessionId);
|
|
15
|
+
if (existing) {
|
|
16
|
+
existing.lastActivityAt = Date.now();
|
|
17
|
+
return existing;
|
|
18
|
+
}
|
|
19
|
+
if (this.sessions.size >= this.maxSessions) {
|
|
20
|
+
const err = new Error(`Overloaded: maxSessions=${this.maxSessions} reached`);
|
|
21
|
+
err.code = -32001;
|
|
22
|
+
throw err;
|
|
23
|
+
}
|
|
24
|
+
const engine = this.factory(slice);
|
|
25
|
+
const session = new ChatSession({ id: sessionId, engine });
|
|
26
|
+
this.sessions.set(sessionId, session);
|
|
27
|
+
return session;
|
|
28
|
+
}
|
|
29
|
+
get(sessionId) {
|
|
30
|
+
return this.sessions.get(sessionId);
|
|
31
|
+
}
|
|
32
|
+
close(sessionId) {
|
|
33
|
+
const s = this.sessions.get(sessionId);
|
|
34
|
+
if (!s)
|
|
35
|
+
return;
|
|
36
|
+
s.cancel();
|
|
37
|
+
this.sessions.delete(sessionId);
|
|
38
|
+
}
|
|
39
|
+
closeAll() {
|
|
40
|
+
for (const id of [...this.sessions.keys()])
|
|
41
|
+
this.close(id);
|
|
42
|
+
}
|
|
43
|
+
sessionCount() {
|
|
44
|
+
return this.sessions.size;
|
|
45
|
+
}
|
|
46
|
+
sweepIdle() {
|
|
47
|
+
const cutoff = Date.now() - this.idleTtlMs;
|
|
48
|
+
for (const [id, s] of [...this.sessions]) {
|
|
49
|
+
if (s.lastActivityAt < cutoff && !s.isBusy())
|
|
50
|
+
this.close(id);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
startIdleSweeper(intervalMs = 60_000) {
|
|
54
|
+
if (this.sweeper)
|
|
55
|
+
return;
|
|
56
|
+
this.sweeper = setInterval(() => this.sweepIdle(), intervalMs);
|
|
57
|
+
// Allow the process to exit even if sweeper is pending.
|
|
58
|
+
this.sweeper?.unref?.();
|
|
59
|
+
}
|
|
60
|
+
stopIdleSweeper() {
|
|
61
|
+
if (this.sweeper)
|
|
62
|
+
clearInterval(this.sweeper);
|
|
63
|
+
this.sweeper = null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Engine, EngineResult } from "../engine/engine.js";
|
|
2
|
+
import type { StreamEvent } from "../types.js";
|
|
3
|
+
export interface ChatSessionOptions {
|
|
4
|
+
id: string;
|
|
5
|
+
engine: Engine;
|
|
6
|
+
onStream?: (event: StreamEvent) => void;
|
|
7
|
+
}
|
|
8
|
+
export interface TurnOpts {
|
|
9
|
+
/** Working directory override for this turn. If omitted, Engine uses its
|
|
10
|
+
* configured cwd. */
|
|
11
|
+
cwd?: string;
|
|
12
|
+
onStream?: (event: StreamEvent) => void;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* One ChatSession per UI chat tab. Owns a single Engine, an AbortController
|
|
16
|
+
* for the active turn, and a FIFO queue so a fast second send waits for the
|
|
17
|
+
* first turn to finish instead of being silently rejected.
|
|
18
|
+
*/
|
|
19
|
+
export declare class ChatSession {
|
|
20
|
+
readonly id: string;
|
|
21
|
+
readonly engine: Engine;
|
|
22
|
+
/**
|
|
23
|
+
* Per-session approval callbacks indexed by tool-call requestId.
|
|
24
|
+
* `readonly` guards the Map reference (preventing reassignment); the
|
|
25
|
+
* contents are mutated by `.set()` / `.delete()` as approvals come and go.
|
|
26
|
+
* Task 10 will register entries here when the Engine raises an approval
|
|
27
|
+
* request and clean them up on response.
|
|
28
|
+
*/
|
|
29
|
+
readonly pendingApprovals: Map<string, (decision: unknown) => void>;
|
|
30
|
+
lastActivityAt: number;
|
|
31
|
+
private queue;
|
|
32
|
+
private active;
|
|
33
|
+
private controller;
|
|
34
|
+
private readonly defaultOnStream?;
|
|
35
|
+
constructor(opts: ChatSessionOptions);
|
|
36
|
+
enqueueTurn(task: string, opts: TurnOpts): Promise<EngineResult>;
|
|
37
|
+
/**
|
|
38
|
+
* Abort the in-flight turn and drain queued turns.
|
|
39
|
+
*
|
|
40
|
+
* Relies on `engine.run()` honouring the `AbortSignal`. If `engine.run` were
|
|
41
|
+
* to swallow the abort and resolve successfully, the caller of the in-flight
|
|
42
|
+
* `enqueueTurn` would observe success — not a cancellation. The queued turns
|
|
43
|
+
* are always rejected regardless.
|
|
44
|
+
*/
|
|
45
|
+
cancel(): void;
|
|
46
|
+
isBusy(): boolean;
|
|
47
|
+
queueDepth(): number;
|
|
48
|
+
private pump;
|
|
49
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One ChatSession per UI chat tab. Owns a single Engine, an AbortController
|
|
3
|
+
* for the active turn, and a FIFO queue so a fast second send waits for the
|
|
4
|
+
* first turn to finish instead of being silently rejected.
|
|
5
|
+
*/
|
|
6
|
+
export class ChatSession {
|
|
7
|
+
id;
|
|
8
|
+
engine;
|
|
9
|
+
/**
|
|
10
|
+
* Per-session approval callbacks indexed by tool-call requestId.
|
|
11
|
+
* `readonly` guards the Map reference (preventing reassignment); the
|
|
12
|
+
* contents are mutated by `.set()` / `.delete()` as approvals come and go.
|
|
13
|
+
* Task 10 will register entries here when the Engine raises an approval
|
|
14
|
+
* request and clean them up on response.
|
|
15
|
+
*/
|
|
16
|
+
pendingApprovals = new Map();
|
|
17
|
+
lastActivityAt = Date.now();
|
|
18
|
+
queue = [];
|
|
19
|
+
active = null;
|
|
20
|
+
controller = null;
|
|
21
|
+
defaultOnStream;
|
|
22
|
+
constructor(opts) {
|
|
23
|
+
this.id = opts.id;
|
|
24
|
+
this.engine = opts.engine;
|
|
25
|
+
this.defaultOnStream = opts.onStream;
|
|
26
|
+
}
|
|
27
|
+
enqueueTurn(task, opts) {
|
|
28
|
+
this.lastActivityAt = Date.now();
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
this.queue.push({ task, opts, resolve, reject });
|
|
31
|
+
this.pump();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Abort the in-flight turn and drain queued turns.
|
|
36
|
+
*
|
|
37
|
+
* Relies on `engine.run()` honouring the `AbortSignal`. If `engine.run` were
|
|
38
|
+
* to swallow the abort and resolve successfully, the caller of the in-flight
|
|
39
|
+
* `enqueueTurn` would observe success — not a cancellation. The queued turns
|
|
40
|
+
* are always rejected regardless.
|
|
41
|
+
*/
|
|
42
|
+
cancel() {
|
|
43
|
+
this.controller?.abort();
|
|
44
|
+
// Drain queued turns as cancelled
|
|
45
|
+
const drained = this.queue.splice(0);
|
|
46
|
+
for (const t of drained) {
|
|
47
|
+
t.reject(new Error("cancelled: session aborted before turn ran"));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
isBusy() {
|
|
51
|
+
return this.active !== null;
|
|
52
|
+
}
|
|
53
|
+
queueDepth() {
|
|
54
|
+
return this.queue.length;
|
|
55
|
+
}
|
|
56
|
+
async pump() {
|
|
57
|
+
if (this.active)
|
|
58
|
+
return;
|
|
59
|
+
const next = this.queue.shift();
|
|
60
|
+
if (!next)
|
|
61
|
+
return;
|
|
62
|
+
this.active = next;
|
|
63
|
+
this.controller = new AbortController();
|
|
64
|
+
try {
|
|
65
|
+
const onStream = next.opts.onStream ?? this.defaultOnStream;
|
|
66
|
+
const result = await this.engine.run(next.task, {
|
|
67
|
+
cwd: next.opts.cwd,
|
|
68
|
+
sessionId: this.id,
|
|
69
|
+
signal: this.controller.signal,
|
|
70
|
+
onStream,
|
|
71
|
+
});
|
|
72
|
+
this.lastActivityAt = Date.now();
|
|
73
|
+
next.resolve(result);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
next.reject(err);
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
this.active = null;
|
|
80
|
+
this.controller = null;
|
|
81
|
+
// Drain the next turn if one is waiting.
|
|
82
|
+
if (this.queue.length > 0)
|
|
83
|
+
void this.pump();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentClient — the UI-side interface to the agent server.
|
|
3
|
+
*
|
|
4
|
+
* Replaces direct Engine usage in the UI. Communicates with AgentServer
|
|
5
|
+
* over a Transport (in-process or cross-process).
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const client = new AgentClient({ transport });
|
|
9
|
+
* client.onStreamEvent((event) => { ... });
|
|
10
|
+
* client.onApprovalRequest((reqId, request) => { ... });
|
|
11
|
+
* const result = await client.run("fix the bug");
|
|
12
|
+
*/
|
|
13
|
+
import type { Transport } from "./transport.js";
|
|
14
|
+
import { type RunParams, type RunResult, type AgentStreamEventNotification, type ConfigureParams, type QueryParams, type QueryResult } from "./types.js";
|
|
15
|
+
import type { ApprovalRequest, ApprovalResult, PermissionMode, BackgroundAgentCompletedEvent } from "../types.js";
|
|
16
|
+
export interface AgentClientEvents {
|
|
17
|
+
/** Multi-session envelope: carries sessionId + event. */
|
|
18
|
+
stream: (envelope: AgentStreamEventNotification) => void;
|
|
19
|
+
approvalRequest: (requestId: string, request: ApprovalRequest) => void;
|
|
20
|
+
status: (status: string, message?: string) => void;
|
|
21
|
+
}
|
|
22
|
+
export interface AgentRunOptions {
|
|
23
|
+
cwd?: string;
|
|
24
|
+
sessionId?: string;
|
|
25
|
+
permissionMode?: PermissionMode;
|
|
26
|
+
planMode?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export type BackgroundAgentCompletedHandler = (sessionId: string, event: BackgroundAgentCompletedEvent) => void;
|
|
29
|
+
export declare class AgentClient {
|
|
30
|
+
private transport;
|
|
31
|
+
private emitter;
|
|
32
|
+
private pendingRequests;
|
|
33
|
+
/**
|
|
34
|
+
* Map from a user-supplied `BackgroundAgentCompletedHandler` to the
|
|
35
|
+
* internal envelope-filtering wrapper actually registered on the
|
|
36
|
+
* EventEmitter. Lets `offBackgroundAgentCompleted` look up the real
|
|
37
|
+
* listener so removal is symmetric with subscription.
|
|
38
|
+
*/
|
|
39
|
+
private bgAgentHandlers;
|
|
40
|
+
constructor(options: {
|
|
41
|
+
transport: Transport;
|
|
42
|
+
});
|
|
43
|
+
/**
|
|
44
|
+
* Run an agent task. Resolves when the agent completes.
|
|
45
|
+
* Stream events arrive via the onStreamEvent callback.
|
|
46
|
+
*
|
|
47
|
+
* Accepts either the new object form `{ sessionId, task, ... }` (required
|
|
48
|
+
* for multi-session servers) or the legacy string form `(task, sessionId?)`
|
|
49
|
+
* for backward compatibility with createInProcessClient callers.
|
|
50
|
+
*/
|
|
51
|
+
run(taskOrParams: string | RunParams, sessionIdOrOptions?: string | AgentRunOptions): Promise<RunResult>;
|
|
52
|
+
/**
|
|
53
|
+
* Respond to an approval request from the server.
|
|
54
|
+
*
|
|
55
|
+
* Multi-session form: approve(sessionId, requestId, decision)
|
|
56
|
+
* Legacy form: approve(requestId, decision)
|
|
57
|
+
*/
|
|
58
|
+
approve(sessionId: string, requestId: string, decision: ApprovalResult): Promise<void>;
|
|
59
|
+
approve(requestId: string, decision: ApprovalResult): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Cancel a running agent.
|
|
62
|
+
*
|
|
63
|
+
* Multi-session form: cancel(sessionId, reason?)
|
|
64
|
+
* Legacy form: cancel(reason?)
|
|
65
|
+
*/
|
|
66
|
+
cancel(sessionId?: string, reason?: string): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Update runtime configuration on the server.
|
|
69
|
+
*/
|
|
70
|
+
configure(params: ConfigureParams): Promise<Record<string, unknown>>;
|
|
71
|
+
/**
|
|
72
|
+
* Query server state.
|
|
73
|
+
*/
|
|
74
|
+
query(type: QueryParams["type"], arg?: string | Record<string, unknown>, ...extra: unknown[]): Promise<QueryResult>;
|
|
75
|
+
/**
|
|
76
|
+
* Inject context into the session transcript without triggering a LLM turn.
|
|
77
|
+
* Used to make arena results, tool outputs, etc. visible to subsequent LLM calls.
|
|
78
|
+
*/
|
|
79
|
+
inject(sessionId: string, content: string): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Register a handler for stream event notifications.
|
|
82
|
+
* The handler receives the full envelope `{ sessionId, event }` so callers
|
|
83
|
+
* can route events to the correct tab/session.
|
|
84
|
+
*/
|
|
85
|
+
onStreamEvent(handler: (envelope: AgentStreamEventNotification) => void): void;
|
|
86
|
+
offStreamEvent(handler: (envelope: AgentStreamEventNotification) => void): void;
|
|
87
|
+
onApprovalRequest(handler: (requestId: string, request: ApprovalRequest) => void): void;
|
|
88
|
+
offApprovalRequest(handler: (requestId: string, request: ApprovalRequest) => void): void;
|
|
89
|
+
onStatus(handler: (status: string, message?: string) => void): void;
|
|
90
|
+
offStatus(handler: (status: string, message?: string) => void): void;
|
|
91
|
+
/**
|
|
92
|
+
* Typed convenience for the B2.2 `background_agent_completed` stream
|
|
93
|
+
* event. Internally wraps `onStreamEvent`, so catch-all stream
|
|
94
|
+
* consumers keep seeing the same envelope; this just adds a typed
|
|
95
|
+
* entry point so SDK callers don't have to switch on `event.type`
|
|
96
|
+
* themselves.
|
|
97
|
+
*/
|
|
98
|
+
onBackgroundAgentCompleted(handler: BackgroundAgentCompletedHandler): void;
|
|
99
|
+
offBackgroundAgentCompleted(handler: BackgroundAgentCompletedHandler): void;
|
|
100
|
+
private request;
|
|
101
|
+
private handleResponse;
|
|
102
|
+
private handleNotification;
|
|
103
|
+
close(): void;
|
|
104
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentClient — the UI-side interface to the agent server.
|
|
3
|
+
*
|
|
4
|
+
* Replaces direct Engine usage in the UI. Communicates with AgentServer
|
|
5
|
+
* over a Transport (in-process or cross-process).
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const client = new AgentClient({ transport });
|
|
9
|
+
* client.onStreamEvent((event) => { ... });
|
|
10
|
+
* client.onApprovalRequest((reqId, request) => { ... });
|
|
11
|
+
* const result = await client.run("fix the bug");
|
|
12
|
+
*/
|
|
13
|
+
import { Methods, createRequest, isResponse, isNotification, } from "./types.js";
|
|
14
|
+
import { EventEmitter } from "node:events";
|
|
15
|
+
import { logger } from "../logging/logger.js";
|
|
16
|
+
export class AgentClient {
|
|
17
|
+
transport;
|
|
18
|
+
emitter = new EventEmitter();
|
|
19
|
+
pendingRequests = new Map();
|
|
20
|
+
/**
|
|
21
|
+
* Map from a user-supplied `BackgroundAgentCompletedHandler` to the
|
|
22
|
+
* internal envelope-filtering wrapper actually registered on the
|
|
23
|
+
* EventEmitter. Lets `offBackgroundAgentCompleted` look up the real
|
|
24
|
+
* listener so removal is symmetric with subscription.
|
|
25
|
+
*/
|
|
26
|
+
bgAgentHandlers = new Map();
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.transport = options.transport;
|
|
29
|
+
this.transport.onMessage((msg) => {
|
|
30
|
+
if (isResponse(msg)) {
|
|
31
|
+
this.handleResponse(msg);
|
|
32
|
+
}
|
|
33
|
+
else if (isNotification(msg)) {
|
|
34
|
+
this.handleNotification(msg);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
// ─── Requests ───────────────────────────────────────────────────
|
|
39
|
+
/**
|
|
40
|
+
* Run an agent task. Resolves when the agent completes.
|
|
41
|
+
* Stream events arrive via the onStreamEvent callback.
|
|
42
|
+
*
|
|
43
|
+
* Accepts either the new object form `{ sessionId, task, ... }` (required
|
|
44
|
+
* for multi-session servers) or the legacy string form `(task, sessionId?)`
|
|
45
|
+
* for backward compatibility with createInProcessClient callers.
|
|
46
|
+
*/
|
|
47
|
+
async run(taskOrParams, sessionIdOrOptions) {
|
|
48
|
+
let params;
|
|
49
|
+
if (typeof taskOrParams === "string") {
|
|
50
|
+
// Form: run(task, sessionId | options?)
|
|
51
|
+
const options = typeof sessionIdOrOptions === "string"
|
|
52
|
+
? { sessionId: sessionIdOrOptions }
|
|
53
|
+
: sessionIdOrOptions;
|
|
54
|
+
params = {
|
|
55
|
+
task: taskOrParams,
|
|
56
|
+
sessionId: options?.sessionId ?? "",
|
|
57
|
+
...(options ?? {}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// Form: run({ sessionId, task, ... }). The caller-provided fields
|
|
62
|
+
// win; the empty-string default only fills in when sessionId is
|
|
63
|
+
// truly absent (cold start).
|
|
64
|
+
params = { ...taskOrParams, sessionId: taskOrParams.sessionId ?? "" };
|
|
65
|
+
}
|
|
66
|
+
// If the caller provided a session id, stamp it on the logger eagerly
|
|
67
|
+
// so client-side log lines emitted during this run (before the server
|
|
68
|
+
// response arrives) carry the right sid. The server-resolved id from
|
|
69
|
+
// the response below takes precedence — they only differ on the
|
|
70
|
+
// first call of a brand-new session, when sessionId is undefined.
|
|
71
|
+
if (params.sessionId)
|
|
72
|
+
logger.setSid(params.sessionId);
|
|
73
|
+
const result = (await this.request(Methods.Run, params));
|
|
74
|
+
if (result.sessionId)
|
|
75
|
+
logger.setSid(result.sessionId);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
approve(...args) {
|
|
79
|
+
if (args.length === 3) {
|
|
80
|
+
const [sessionId, requestId, decision] = args;
|
|
81
|
+
return this.request(Methods.Approve, {
|
|
82
|
+
sessionId,
|
|
83
|
+
requestId,
|
|
84
|
+
decision,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const [requestId, decision] = args;
|
|
88
|
+
return this.request(Methods.Approve, {
|
|
89
|
+
requestId,
|
|
90
|
+
decision,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Cancel a running agent.
|
|
95
|
+
*
|
|
96
|
+
* Multi-session form: cancel(sessionId, reason?)
|
|
97
|
+
* Legacy form: cancel(reason?)
|
|
98
|
+
*/
|
|
99
|
+
async cancel(sessionId, reason) {
|
|
100
|
+
await this.request(Methods.Cancel, { sessionId, reason });
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Update runtime configuration on the server.
|
|
104
|
+
*/
|
|
105
|
+
async configure(params) {
|
|
106
|
+
return this.request(Methods.Configure, params);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Query server state.
|
|
110
|
+
*/
|
|
111
|
+
async query(type, arg, ...extra) {
|
|
112
|
+
const params = { type };
|
|
113
|
+
// Per-type argument mapping. The second positional param overloads:
|
|
114
|
+
// - "models" / "sessions" / "providers" / "permission_state": (sessionId?)
|
|
115
|
+
// - "config_set": (key, value)
|
|
116
|
+
// - "config_get": (key)
|
|
117
|
+
// - "permission_set": (mode) — server reads `params.value`
|
|
118
|
+
// - "provider_add" / "model_add" / "provider_refresh" /
|
|
119
|
+
// "provider_delete" / "model_delete": ({ ...payload }) — the object
|
|
120
|
+
// is spread into the top-level params so the server can read
|
|
121
|
+
// `params.key` / `params.provider` / `params.model` directly.
|
|
122
|
+
if (type === "config_set" && extra.length >= 1) {
|
|
123
|
+
params.key = arg;
|
|
124
|
+
params.value = extra[0];
|
|
125
|
+
}
|
|
126
|
+
else if (type === "config_get") {
|
|
127
|
+
params.key = arg;
|
|
128
|
+
}
|
|
129
|
+
else if (type === "permission_set") {
|
|
130
|
+
params.value = arg;
|
|
131
|
+
}
|
|
132
|
+
else if (type === "provider_add" ||
|
|
133
|
+
type === "model_add" ||
|
|
134
|
+
type === "provider_refresh" ||
|
|
135
|
+
type === "provider_delete" ||
|
|
136
|
+
type === "model_delete") {
|
|
137
|
+
if (arg && typeof arg === "object")
|
|
138
|
+
Object.assign(params, arg);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
params.sessionId = arg;
|
|
142
|
+
}
|
|
143
|
+
return this.request(Methods.Query, params);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Inject context into the session transcript without triggering a LLM turn.
|
|
147
|
+
* Used to make arena results, tool outputs, etc. visible to subsequent LLM calls.
|
|
148
|
+
*/
|
|
149
|
+
async inject(sessionId, content) {
|
|
150
|
+
await this.request(Methods.Inject, { sessionId, content });
|
|
151
|
+
}
|
|
152
|
+
// ─── Event Handlers ─────────────────────────────────────────────
|
|
153
|
+
/**
|
|
154
|
+
* Register a handler for stream event notifications.
|
|
155
|
+
* The handler receives the full envelope `{ sessionId, event }` so callers
|
|
156
|
+
* can route events to the correct tab/session.
|
|
157
|
+
*/
|
|
158
|
+
onStreamEvent(handler) {
|
|
159
|
+
this.emitter.on("stream", handler);
|
|
160
|
+
}
|
|
161
|
+
offStreamEvent(handler) {
|
|
162
|
+
this.emitter.off("stream", handler);
|
|
163
|
+
}
|
|
164
|
+
onApprovalRequest(handler) {
|
|
165
|
+
this.emitter.on("approvalRequest", handler);
|
|
166
|
+
}
|
|
167
|
+
offApprovalRequest(handler) {
|
|
168
|
+
this.emitter.off("approvalRequest", handler);
|
|
169
|
+
}
|
|
170
|
+
onStatus(handler) {
|
|
171
|
+
this.emitter.on("status", handler);
|
|
172
|
+
}
|
|
173
|
+
offStatus(handler) {
|
|
174
|
+
this.emitter.off("status", handler);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Typed convenience for the B2.2 `background_agent_completed` stream
|
|
178
|
+
* event. Internally wraps `onStreamEvent`, so catch-all stream
|
|
179
|
+
* consumers keep seeing the same envelope; this just adds a typed
|
|
180
|
+
* entry point so SDK callers don't have to switch on `event.type`
|
|
181
|
+
* themselves.
|
|
182
|
+
*/
|
|
183
|
+
onBackgroundAgentCompleted(handler) {
|
|
184
|
+
const wrapper = (envelope) => {
|
|
185
|
+
if (envelope.event.type === "background_agent_completed") {
|
|
186
|
+
handler(envelope.sessionId, envelope.event);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
this.bgAgentHandlers.set(handler, wrapper);
|
|
190
|
+
this.emitter.on("stream", wrapper);
|
|
191
|
+
}
|
|
192
|
+
offBackgroundAgentCompleted(handler) {
|
|
193
|
+
const wrapper = this.bgAgentHandlers.get(handler);
|
|
194
|
+
if (!wrapper)
|
|
195
|
+
return;
|
|
196
|
+
this.bgAgentHandlers.delete(handler);
|
|
197
|
+
this.emitter.off("stream", wrapper);
|
|
198
|
+
}
|
|
199
|
+
// ─── Internals ──────────────────────────────────────────────────
|
|
200
|
+
request(method, params) {
|
|
201
|
+
return new Promise((resolve, reject) => {
|
|
202
|
+
const req = createRequest(method, params);
|
|
203
|
+
this.pendingRequests.set(req.id, { resolve, reject });
|
|
204
|
+
this.transport.send(req);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
handleResponse(res) {
|
|
208
|
+
const pending = this.pendingRequests.get(res.id);
|
|
209
|
+
if (!pending)
|
|
210
|
+
return;
|
|
211
|
+
this.pendingRequests.delete(res.id);
|
|
212
|
+
if (res.error) {
|
|
213
|
+
// Attach both message and code so callers can do toMatchObject({ code }).
|
|
214
|
+
const err = new Error(`[${res.error.code}] ${res.error.message}`);
|
|
215
|
+
err.code = res.error.code;
|
|
216
|
+
pending.reject(err);
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
pending.resolve(res.result);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
handleNotification(notif) {
|
|
223
|
+
const params = (notif.params ?? {});
|
|
224
|
+
switch (notif.method) {
|
|
225
|
+
case Methods.StreamEvent: {
|
|
226
|
+
const event = params.event;
|
|
227
|
+
const sessionId = params.sessionId ?? "";
|
|
228
|
+
if (event) {
|
|
229
|
+
const envelope = { sessionId, event };
|
|
230
|
+
this.emitter.emit("stream", envelope);
|
|
231
|
+
}
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
case Methods.ApprovalRequest: {
|
|
235
|
+
const requestId = params.requestId;
|
|
236
|
+
const request = params.request;
|
|
237
|
+
if (requestId && request) {
|
|
238
|
+
this.emitter.emit("approvalRequest", requestId, request);
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case Methods.Status: {
|
|
243
|
+
const status = params.status;
|
|
244
|
+
const message = params.message;
|
|
245
|
+
this.emitter.emit("status", status, message);
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
close() {
|
|
251
|
+
// Reject all pending requests
|
|
252
|
+
for (const [, pending] of this.pendingRequests) {
|
|
253
|
+
pending.reject(new Error("Client closed"));
|
|
254
|
+
}
|
|
255
|
+
this.pendingRequests.clear();
|
|
256
|
+
this.bgAgentHandlers.clear();
|
|
257
|
+
this.emitter.removeAllListeners();
|
|
258
|
+
this.transport.close();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed factories — the recommended public entry points for business
|
|
3
|
+
* consumers of `@cjhyy/code-shell-core`.
|
|
4
|
+
*
|
|
5
|
+
* Standard §7 declares a stable API shape:
|
|
6
|
+
*
|
|
7
|
+
* const [serverT, clientT] = createInProcessTransport();
|
|
8
|
+
* const server = await createServer({ transport: serverT, cwd, llm });
|
|
9
|
+
* const client = createClient({ transport: clientT });
|
|
10
|
+
* client.onStreamEvent(({ sessionId, event }) => {});
|
|
11
|
+
* await client.run({ sessionId: "main", task: "..." });
|
|
12
|
+
*
|
|
13
|
+
* Direct `new Engine(...)` / `new AgentServer(...)` / `new AgentClient(...)`
|
|
14
|
+
* remain supported for advanced cases, but most embedders should use these
|
|
15
|
+
* helpers. They:
|
|
16
|
+
*
|
|
17
|
+
* - Construct the Engine for you from a flat config.
|
|
18
|
+
* - Wire the AgentServer to the supplied transport.
|
|
19
|
+
* - Return a `close()` lifecycle hook that cleans up in the right order.
|
|
20
|
+
*
|
|
21
|
+
* These factories deliberately accept a `Transport` rather than building
|
|
22
|
+
* one internally — they need to compose with `createInProcessTransport`
|
|
23
|
+
* (for in-process embedders), `StdioTransport` (for the worker), or a
|
|
24
|
+
* future `IpcAdapter`. A factory that hides the transport choice would
|
|
25
|
+
* preclude the multi-host story standard §S8 describes.
|
|
26
|
+
*/
|
|
27
|
+
import type { LLMConfig } from "../types.js";
|
|
28
|
+
import { Engine, type EngineConfig } from "../engine/engine.js";
|
|
29
|
+
import { AgentServer } from "./server.js";
|
|
30
|
+
import { AgentClient } from "./client.js";
|
|
31
|
+
import type { Transport } from "./transport.js";
|
|
32
|
+
export interface CreateServerOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Transport the server reads requests from and writes notifications to.
|
|
35
|
+
* Pair it with the matching client transport from
|
|
36
|
+
* `createInProcessTransport()` (in-process embed) or a `StdioTransport`
|
|
37
|
+
* (worker process).
|
|
38
|
+
*/
|
|
39
|
+
transport: Transport;
|
|
40
|
+
/**
|
|
41
|
+
* LLM provider/model credentials. Forwarded to `EngineConfig.llm`.
|
|
42
|
+
* Required because there is no useful default — every embedder needs
|
|
43
|
+
* to choose a provider.
|
|
44
|
+
*/
|
|
45
|
+
llm: LLMConfig;
|
|
46
|
+
/** Working directory the Engine treats as the project root. */
|
|
47
|
+
cwd?: string;
|
|
48
|
+
/** Permission mode for the Engine. Defaults to "default". */
|
|
49
|
+
permissionMode?: EngineConfig["permissionMode"];
|
|
50
|
+
/**
|
|
51
|
+
* Escape hatch for embedders who need to set EngineConfig fields the
|
|
52
|
+
* stable surface doesn't expose. Anything passed here is shallow-merged
|
|
53
|
+
* over the factory's computed config. Use sparingly — fields outside
|
|
54
|
+
* this options bag are not part of the stable API contract and may
|
|
55
|
+
* move to `internal` in a future version.
|
|
56
|
+
*/
|
|
57
|
+
engineOverrides?: Partial<EngineConfig>;
|
|
58
|
+
}
|
|
59
|
+
export interface ServerHandle {
|
|
60
|
+
/**
|
|
61
|
+
* The constructed AgentServer. Most embedders only need `close()`; this
|
|
62
|
+
* is exposed for advanced cases (custom hook registration, dynamic tool
|
|
63
|
+
* registry updates) that the typed surface doesn't yet cover.
|
|
64
|
+
*/
|
|
65
|
+
readonly server: AgentServer;
|
|
66
|
+
/**
|
|
67
|
+
* The Engine instance the server wraps. Same caveat as `server` —
|
|
68
|
+
* direct access is supported but not recommended.
|
|
69
|
+
*/
|
|
70
|
+
readonly engine: Engine;
|
|
71
|
+
/**
|
|
72
|
+
* Tear down the server (which aborts any active run + emits the final
|
|
73
|
+
* shutdown notification). Safe to call multiple times.
|
|
74
|
+
*/
|
|
75
|
+
close(): void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Build an `Engine` + `AgentServer` from a flat config and wire them to
|
|
79
|
+
* the caller's `Transport`. Most embedders should call this rather than
|
|
80
|
+
* instantiating the engine/server pair manually.
|
|
81
|
+
*/
|
|
82
|
+
export declare function createServer(options: CreateServerOptions): ServerHandle;
|
|
83
|
+
export interface CreateClientOptions {
|
|
84
|
+
/** Transport paired with the server's. */
|
|
85
|
+
transport: Transport;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Thin convenience wrapper around `new AgentClient`. Exists so the
|
|
89
|
+
* recommended public surface is fully createX-style, mirroring
|
|
90
|
+
* `createServer`. Future versions may add typed event subscriptions or
|
|
91
|
+
* stream-event filters here without breaking embedders.
|
|
92
|
+
*/
|
|
93
|
+
export declare function createClient(options: CreateClientOptions): AgentClient;
|