@armadra/agent 0.0.0-stage → 0.3.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/CHANGELOG.md +37 -0
- package/LICENSE +21 -0
- package/README.md +470 -2
- package/dist/agent/agent.d.ts +56 -0
- package/dist/agent/agent.js +129 -0
- package/dist/agent/loop.d.ts +51 -0
- package/dist/agent/loop.js +158 -0
- package/dist/agent/queue.d.ts +25 -0
- package/dist/agent/queue.js +55 -0
- package/dist/agent/retry.d.ts +22 -0
- package/dist/agent/retry.js +92 -0
- package/dist/agent/schema.d.ts +27 -0
- package/dist/agent/schema.js +186 -0
- package/dist/agent/session-cache.d.ts +100 -0
- package/dist/agent/session-cache.js +514 -0
- package/dist/agent/session-compaction.d.ts +49 -0
- package/dist/agent/session-compaction.js +251 -0
- package/dist/agent/session-core.d.ts +103 -0
- package/dist/agent/session-core.js +7 -0
- package/dist/agent/session-run.d.ts +55 -0
- package/dist/agent/session-run.js +246 -0
- package/dist/agent/session-state.d.ts +39 -0
- package/dist/agent/session-state.js +119 -0
- package/dist/agent/session-subagent.d.ts +54 -0
- package/dist/agent/session-subagent.js +157 -0
- package/dist/agent/session-sync.d.ts +20 -0
- package/dist/agent/session-sync.js +111 -0
- package/dist/agent/session-tools.d.ts +24 -0
- package/dist/agent/session-tools.js +277 -0
- package/dist/agent/session.d.ts +117 -0
- package/dist/agent/session.js +485 -0
- package/dist/agent/system-prompt.d.ts +49 -0
- package/dist/agent/system-prompt.js +121 -0
- package/dist/agent/tool-runner.d.ts +64 -0
- package/dist/agent/tool-runner.js +309 -0
- package/dist/agent/transform.d.ts +26 -0
- package/dist/agent/transform.js +171 -0
- package/dist/agent/types.d.ts +378 -0
- package/dist/agent/types.js +21 -0
- package/dist/ai/apis/anthropic-messages.d.ts +26 -0
- package/dist/ai/apis/anthropic-messages.js +214 -0
- package/dist/ai/apis/anthropic-request.d.ts +42 -0
- package/dist/ai/apis/anthropic-request.js +282 -0
- package/dist/ai/apis/api.d.ts +28 -0
- package/dist/ai/apis/api.js +102 -0
- package/dist/ai/apis/cache-params.d.ts +48 -0
- package/dist/ai/apis/cache-params.js +163 -0
- package/dist/ai/apis/google-generative-ai.d.ts +32 -0
- package/dist/ai/apis/google-generative-ai.js +249 -0
- package/dist/ai/apis/google-request.d.ts +40 -0
- package/dist/ai/apis/google-request.js +255 -0
- package/dist/ai/apis/openai-compat.d.ts +35 -0
- package/dist/ai/apis/openai-compat.js +142 -0
- package/dist/ai/apis/openai-completions.d.ts +28 -0
- package/dist/ai/apis/openai-completions.js +246 -0
- package/dist/ai/apis/openai-request.d.ts +35 -0
- package/dist/ai/apis/openai-request.js +303 -0
- package/dist/ai/apis/openai-responses-request.d.ts +52 -0
- package/dist/ai/apis/openai-responses-request.js +266 -0
- package/dist/ai/apis/openai-responses.d.ts +28 -0
- package/dist/ai/apis/openai-responses.js +304 -0
- package/dist/ai/apis/shared.d.ts +56 -0
- package/dist/ai/apis/shared.js +179 -0
- package/dist/ai/cache/economics.d.ts +24 -0
- package/dist/ai/cache/economics.js +69 -0
- package/dist/ai/cache/fingerprint.d.ts +17 -0
- package/dist/ai/cache/fingerprint.js +39 -0
- package/dist/ai/cache/miss.d.ts +49 -0
- package/dist/ai/cache/miss.js +96 -0
- package/dist/ai/cache/reporting.d.ts +39 -0
- package/dist/ai/cache/reporting.js +103 -0
- package/dist/ai/cache/types.d.ts +94 -0
- package/dist/ai/cache/types.js +19 -0
- package/dist/ai/cache/warmer.d.ts +94 -0
- package/dist/ai/cache/warmer.js +224 -0
- package/dist/ai/context.d.ts +35 -0
- package/dist/ai/context.js +114 -0
- package/dist/ai/cost.d.ts +21 -0
- package/dist/ai/cost.js +60 -0
- package/dist/ai/event-stream.d.ts +33 -0
- package/dist/ai/event-stream.js +101 -0
- package/dist/ai/fake/fake-provider.d.ts +68 -0
- package/dist/ai/fake/fake-provider.js +278 -0
- package/dist/ai/fake/fake-script.d.ts +69 -0
- package/dist/ai/fake/fake-script.js +142 -0
- package/dist/ai/http.d.ts +54 -0
- package/dist/ai/http.js +212 -0
- package/dist/ai/json-partial.d.ts +17 -0
- package/dist/ai/json-partial.js +243 -0
- package/dist/ai/overflow.d.ts +22 -0
- package/dist/ai/overflow.js +61 -0
- package/dist/ai/providers/auth.d.ts +71 -0
- package/dist/ai/providers/auth.js +202 -0
- package/dist/ai/providers/builtin.d.ts +23 -0
- package/dist/ai/providers/builtin.js +148 -0
- package/dist/ai/providers/catalog-data.d.ts +5 -0
- package/dist/ai/providers/catalog-data.js +19 -0
- package/dist/ai/providers/catalog.d.ts +28 -0
- package/dist/ai/providers/catalog.js +166 -0
- package/dist/ai/providers/channels.d.ts +51 -0
- package/dist/ai/providers/channels.js +96 -0
- package/dist/ai/providers/enrich.d.ts +34 -0
- package/dist/ai/providers/enrich.js +86 -0
- package/dist/ai/providers/models-dev-cache.d.ts +53 -0
- package/dist/ai/providers/models-dev-cache.js +147 -0
- package/dist/ai/providers/models-dev.d.ts +99 -0
- package/dist/ai/providers/models-dev.js +315 -0
- package/dist/ai/providers/registry.d.ts +114 -0
- package/dist/ai/providers/registry.js +458 -0
- package/dist/ai/sse.d.ts +32 -0
- package/dist/ai/sse.js +140 -0
- package/dist/ai/thinking.d.ts +42 -0
- package/dist/ai/thinking.js +101 -0
- package/dist/ai/types.d.ts +448 -0
- package/dist/ai/types.js +19 -0
- package/dist/bundle/ama-sandbox.cjs +324 -0
- package/dist/bundle/ama.cjs +27837 -0
- package/dist/bundle.d.ts +9 -0
- package/dist/bundle.js +15 -0
- package/dist/cli/args.d.ts +85 -0
- package/dist/cli/args.js +408 -0
- package/dist/cli/bootstrap.d.ts +18 -0
- package/dist/cli/bootstrap.js +399 -0
- package/dist/cli/compose-providers.d.ts +37 -0
- package/dist/cli/compose-providers.js +107 -0
- package/dist/cli/compose-session.d.ts +72 -0
- package/dist/cli/compose-session.js +370 -0
- package/dist/cli/compose-store.d.ts +24 -0
- package/dist/cli/compose-store.js +125 -0
- package/dist/cli/compose.d.ts +108 -0
- package/dist/cli/compose.js +221 -0
- package/dist/cli/default-model.d.ts +16 -0
- package/dist/cli/default-model.js +24 -0
- package/dist/cli/deps.d.ts +207 -0
- package/dist/cli/deps.js +12 -0
- package/dist/cli/exit-codes.d.ts +29 -0
- package/dist/cli/exit-codes.js +40 -0
- package/dist/cli/main.d.ts +27 -0
- package/dist/cli/main.js +177 -0
- package/dist/cli/runtime.d.ts +90 -0
- package/dist/cli/runtime.js +8 -0
- package/dist/cli/startup-screen.d.ts +15 -0
- package/dist/cli/startup-screen.js +56 -0
- package/dist/cli/startup-steps.d.ts +35 -0
- package/dist/cli/startup-steps.js +202 -0
- package/dist/cli/subcommands/auth.d.ts +12 -0
- package/dist/cli/subcommands/auth.js +91 -0
- package/dist/cli/subcommands/config.d.ts +53 -0
- package/dist/cli/subcommands/config.js +277 -0
- package/dist/cli/subcommands/context.d.ts +21 -0
- package/dist/cli/subcommands/context.js +42 -0
- package/dist/cli/subcommands/doctor.d.ts +11 -0
- package/dist/cli/subcommands/doctor.js +246 -0
- package/dist/cli/subcommands/init.d.ts +6 -0
- package/dist/cli/subcommands/init.js +22 -0
- package/dist/cli/subcommands/model-meta.d.ts +16 -0
- package/dist/cli/subcommands/model-meta.js +54 -0
- package/dist/cli/subcommands/models-cache-probe.d.ts +34 -0
- package/dist/cli/subcommands/models-cache-probe.js +190 -0
- package/dist/cli/subcommands/models-discover.d.ts +46 -0
- package/dist/cli/subcommands/models-discover.js +232 -0
- package/dist/cli/subcommands/models.d.ts +40 -0
- package/dist/cli/subcommands/models.js +152 -0
- package/dist/cli/subcommands/providers-list.d.ts +8 -0
- package/dist/cli/subcommands/providers-list.js +97 -0
- package/dist/cli/subcommands/providers-plan.d.ts +79 -0
- package/dist/cli/subcommands/providers-plan.js +215 -0
- package/dist/cli/subcommands/providers.d.ts +27 -0
- package/dist/cli/subcommands/providers.js +433 -0
- package/dist/cli/subcommands/sessions.d.ts +11 -0
- package/dist/cli/subcommands/sessions.js +100 -0
- package/dist/codemode/capability.d.ts +37 -0
- package/dist/codemode/capability.js +55 -0
- package/dist/codemode/declarations.d.ts +44 -0
- package/dist/codemode/declarations.js +111 -0
- package/dist/codemode/host-side.d.ts +59 -0
- package/dist/codemode/host-side.js +253 -0
- package/dist/codemode/modes.d.ts +23 -0
- package/dist/codemode/modes.js +40 -0
- package/dist/codemode/protocol.d.ts +91 -0
- package/dist/codemode/protocol.js +114 -0
- package/dist/codemode/sandbox-entry.d.ts +25 -0
- package/dist/codemode/sandbox-entry.js +323 -0
- package/dist/codemode/store.d.ts +26 -0
- package/dist/codemode/store.js +53 -0
- package/dist/codemode/tool.d.ts +105 -0
- package/dist/codemode/tool.js +283 -0
- package/dist/compaction/branch-summary.d.ts +30 -0
- package/dist/compaction/branch-summary.js +79 -0
- package/dist/compaction/breaker.d.ts +32 -0
- package/dist/compaction/breaker.js +65 -0
- package/dist/compaction/cut-point.d.ts +23 -0
- package/dist/compaction/cut-point.js +70 -0
- package/dist/compaction/estimate.d.ts +30 -0
- package/dist/compaction/estimate.js +114 -0
- package/dist/compaction/prune-tier.d.ts +31 -0
- package/dist/compaction/prune-tier.js +83 -0
- package/dist/compaction/serialize.d.ts +20 -0
- package/dist/compaction/serialize.js +115 -0
- package/dist/compaction/summarize-tier.d.ts +111 -0
- package/dist/compaction/summarize-tier.js +305 -0
- package/dist/config/auth-file.d.ts +39 -0
- package/dist/config/auth-file.js +92 -0
- package/dist/config/context-files.d.ts +30 -0
- package/dist/config/context-files.js +77 -0
- package/dist/config/init.d.ts +30 -0
- package/dist/config/init.js +92 -0
- package/dist/config/json-schema.d.ts +14 -0
- package/dist/config/json-schema.js +182 -0
- package/dist/config/load.d.ts +45 -0
- package/dist/config/load.js +97 -0
- package/dist/config/merge.d.ts +73 -0
- package/dist/config/merge.js +248 -0
- package/dist/config/paths.d.ts +38 -0
- package/dist/config/paths.js +108 -0
- package/dist/config/profile.d.ts +27 -0
- package/dist/config/profile.js +65 -0
- package/dist/config/schema.d.ts +34 -0
- package/dist/config/schema.js +476 -0
- package/dist/config/trust.d.ts +42 -0
- package/dist/config/trust.js +122 -0
- package/dist/config/types.d.ts +190 -0
- package/dist/config/types.js +29 -0
- package/dist/config/write.d.ts +10 -0
- package/dist/config/write.js +25 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +30 -0
- package/dist/hooks/config.d.ts +49 -0
- package/dist/hooks/config.js +102 -0
- package/dist/hooks/dispatcher.d.ts +41 -0
- package/dist/hooks/dispatcher.js +104 -0
- package/dist/hooks/matcher.d.ts +22 -0
- package/dist/hooks/matcher.js +108 -0
- package/dist/hooks/protocol.d.ts +33 -0
- package/dist/hooks/protocol.js +240 -0
- package/dist/hooks/runner.d.ts +30 -0
- package/dist/hooks/runner.js +143 -0
- package/dist/hooks/types.d.ts +147 -0
- package/dist/hooks/types.js +20 -0
- package/dist/host/api-impl.d.ts +75 -0
- package/dist/host/api-impl.js +173 -0
- package/dist/host/loader.d.ts +31 -0
- package/dist/host/loader.js +147 -0
- package/dist/host/types.d.ts +169 -0
- package/dist/host/types.js +14 -0
- package/dist/host.d.ts +5 -0
- package/dist/host.js +4 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +28 -0
- package/dist/modes/commands-core.d.ts +44 -0
- package/dist/modes/commands-core.js +171 -0
- package/dist/modes/image-input.d.ts +27 -0
- package/dist/modes/image-input.js +78 -0
- package/dist/modes/interactive/approval-dialog.d.ts +48 -0
- package/dist/modes/interactive/approval-dialog.js +202 -0
- package/dist/modes/interactive/commands.d.ts +41 -0
- package/dist/modes/interactive/commands.js +201 -0
- package/dist/modes/interactive/completion.d.ts +53 -0
- package/dist/modes/interactive/completion.js +157 -0
- package/dist/modes/interactive/interactive-mode.d.ts +46 -0
- package/dist/modes/interactive/interactive-mode.js +491 -0
- package/dist/modes/interactive/key-dispatch.d.ts +29 -0
- package/dist/modes/interactive/key-dispatch.js +107 -0
- package/dist/modes/interactive/line/line-editor.d.ts +50 -0
- package/dist/modes/interactive/line/line-editor.js +232 -0
- package/dist/modes/interactive/line/line-mode.d.ts +20 -0
- package/dist/modes/interactive/line/line-mode.js +218 -0
- package/dist/modes/interactive/line/line-render.d.ts +35 -0
- package/dist/modes/interactive/line/line-render.js +171 -0
- package/dist/modes/interactive/line/paste-state.d.ts +26 -0
- package/dist/modes/interactive/line/paste-state.js +72 -0
- package/dist/modes/interactive/message-view.d.ts +77 -0
- package/dist/modes/interactive/message-view.js +269 -0
- package/dist/modes/interactive/pickers.d.ts +42 -0
- package/dist/modes/interactive/pickers.js +115 -0
- package/dist/modes/interactive/startup-ui.d.ts +33 -0
- package/dist/modes/interactive/startup-ui.js +268 -0
- package/dist/modes/interactive/status-bar.d.ts +48 -0
- package/dist/modes/interactive/status-bar.js +139 -0
- package/dist/modes/interactive/tool-view.d.ts +95 -0
- package/dist/modes/interactive/tool-view.js +344 -0
- package/dist/modes/print/json-event.d.ts +15 -0
- package/dist/modes/print/json-event.js +39 -0
- package/dist/modes/print/print-mode.d.ts +17 -0
- package/dist/modes/print/print-mode.js +107 -0
- package/dist/modes/rpc/commands.d.ts +40 -0
- package/dist/modes/rpc/commands.js +263 -0
- package/dist/modes/rpc/jsonl.d.ts +16 -0
- package/dist/modes/rpc/jsonl.js +55 -0
- package/dist/modes/rpc/rpc-mode.d.ts +22 -0
- package/dist/modes/rpc/rpc-mode.js +153 -0
- package/dist/modes/session-report.d.ts +55 -0
- package/dist/modes/session-report.js +278 -0
- package/dist/modes/shared.d.ts +13 -0
- package/dist/modes/shared.js +52 -0
- package/dist/modes/startup-ui-text.d.ts +16 -0
- package/dist/modes/startup-ui-text.js +103 -0
- package/dist/permissions/broker.d.ts +47 -0
- package/dist/permissions/broker.js +107 -0
- package/dist/permissions/dangerous.d.ts +47 -0
- package/dist/permissions/dangerous.js +370 -0
- package/dist/permissions/pipeline.d.ts +51 -0
- package/dist/permissions/pipeline.js +181 -0
- package/dist/permissions/preview.d.ts +43 -0
- package/dist/permissions/preview.js +463 -0
- package/dist/permissions/rules.d.ts +58 -0
- package/dist/permissions/rules.js +249 -0
- package/dist/permissions/types.d.ts +100 -0
- package/dist/permissions/types.js +18 -0
- package/dist/rpc.d.ts +173 -0
- package/dist/rpc.js +13 -0
- package/dist/sdk.d.ts +98 -0
- package/dist/sdk.js +195 -0
- package/dist/session/manager.d.ts +70 -0
- package/dist/session/manager.js +278 -0
- package/dist/session/migrate.d.ts +19 -0
- package/dist/session/migrate.js +53 -0
- package/dist/session/projection.d.ts +50 -0
- package/dist/session/projection.js +184 -0
- package/dist/session/store.d.ts +47 -0
- package/dist/session/store.js +210 -0
- package/dist/session/tree.d.ts +21 -0
- package/dist/session/tree.js +88 -0
- package/dist/session/types.d.ts +201 -0
- package/dist/session/types.js +18 -0
- package/dist/skills/discover.d.ts +60 -0
- package/dist/skills/discover.js +160 -0
- package/dist/skills/expand.d.ts +35 -0
- package/dist/skills/expand.js +46 -0
- package/dist/skills/frontmatter.d.ts +27 -0
- package/dist/skills/frontmatter.js +157 -0
- package/dist/skills/index-prompt.d.ts +13 -0
- package/dist/skills/index-prompt.js +42 -0
- package/dist/skills/templates.d.ts +52 -0
- package/dist/skills/templates.js +177 -0
- package/dist/tools/bash.d.ts +49 -0
- package/dist/tools/bash.js +222 -0
- package/dist/tools/edit-fuzzy.d.ts +36 -0
- package/dist/tools/edit-fuzzy.js +129 -0
- package/dist/tools/edit.d.ts +38 -0
- package/dist/tools/edit.js +241 -0
- package/dist/tools/file-mutex.d.ts +8 -0
- package/dist/tools/file-mutex.js +29 -0
- package/dist/tools/glob.d.ts +44 -0
- package/dist/tools/glob.js +217 -0
- package/dist/tools/grep.d.ts +39 -0
- package/dist/tools/grep.js +179 -0
- package/dist/tools/ignore.d.ts +40 -0
- package/dist/tools/ignore.js +174 -0
- package/dist/tools/image-file.d.ts +31 -0
- package/dist/tools/image-file.js +114 -0
- package/dist/tools/ls.d.ts +21 -0
- package/dist/tools/ls.js +96 -0
- package/dist/tools/output-accumulator.d.ts +48 -0
- package/dist/tools/output-accumulator.js +106 -0
- package/dist/tools/paths.d.ts +20 -0
- package/dist/tools/paths.js +58 -0
- package/dist/tools/presets.d.ts +71 -0
- package/dist/tools/presets.js +128 -0
- package/dist/tools/process-tree.d.ts +35 -0
- package/dist/tools/process-tree.js +125 -0
- package/dist/tools/read.d.ts +24 -0
- package/dist/tools/read.js +132 -0
- package/dist/tools/registry.d.ts +43 -0
- package/dist/tools/registry.js +106 -0
- package/dist/tools/shell.d.ts +38 -0
- package/dist/tools/shell.js +91 -0
- package/dist/tools/task.d.ts +29 -0
- package/dist/tools/task.js +107 -0
- package/dist/tools/todo.d.ts +28 -0
- package/dist/tools/todo.js +107 -0
- package/dist/tools/truncate.d.ts +44 -0
- package/dist/tools/truncate.js +175 -0
- package/dist/tools/types.d.ts +135 -0
- package/dist/tools/types.js +19 -0
- package/dist/tools/write.d.ts +20 -0
- package/dist/tools/write.js +84 -0
- package/dist/tui/ansi.d.ts +29 -0
- package/dist/tui/ansi.js +420 -0
- package/dist/tui/component.d.ts +38 -0
- package/dist/tui/component.js +13 -0
- package/dist/tui/components/box.d.ts +26 -0
- package/dist/tui/components/box.js +65 -0
- package/dist/tui/components/container.d.ts +14 -0
- package/dist/tui/components/container.js +39 -0
- package/dist/tui/components/editor-buffer.d.ts +67 -0
- package/dist/tui/components/editor-buffer.js +300 -0
- package/dist/tui/components/editor-paste.d.ts +29 -0
- package/dist/tui/components/editor-paste.js +64 -0
- package/dist/tui/components/editor.d.ts +114 -0
- package/dist/tui/components/editor.js +530 -0
- package/dist/tui/components/key-value.d.ts +29 -0
- package/dist/tui/components/key-value.js +48 -0
- package/dist/tui/components/loader.d.ts +35 -0
- package/dist/tui/components/loader.js +71 -0
- package/dist/tui/components/markdown.d.ts +90 -0
- package/dist/tui/components/markdown.js +462 -0
- package/dist/tui/components/meter.d.ts +29 -0
- package/dist/tui/components/meter.js +47 -0
- package/dist/tui/components/overlay.d.ts +24 -0
- package/dist/tui/components/overlay.js +52 -0
- package/dist/tui/components/select-list.d.ts +57 -0
- package/dist/tui/components/select-list.js +179 -0
- package/dist/tui/components/spacer.d.ts +11 -0
- package/dist/tui/components/spacer.js +16 -0
- package/dist/tui/components/text.d.ts +30 -0
- package/dist/tui/components/text.js +65 -0
- package/dist/tui/keybindings.d.ts +66 -0
- package/dist/tui/keybindings.js +120 -0
- package/dist/tui/keys.d.ts +38 -0
- package/dist/tui/keys.js +275 -0
- package/dist/tui/stdin-buffer.d.ts +41 -0
- package/dist/tui/stdin-buffer.js +206 -0
- package/dist/tui/terminal.d.ts +79 -0
- package/dist/tui/terminal.js +153 -0
- package/dist/tui/theme.d.ts +31 -0
- package/dist/tui/theme.js +199 -0
- package/dist/tui/tui.d.ts +103 -0
- package/dist/tui/tui.js +363 -0
- package/dist/tui/vt-screen.d.ts +46 -0
- package/dist/tui/vt-screen.js +251 -0
- package/dist/tui.d.ts +30 -0
- package/dist/tui.js +31 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.js +24 -0
- package/docs/codemode.md +102 -0
- package/docs/hooks.md +196 -0
- package/docs/host-api.md +161 -0
- package/docs/providers.md +531 -0
- package/docs/rpc.md +253 -0
- package/docs/session-format.md +119 -0
- package/docs/tui.md +170 -0
- package/package.json +101 -4
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话侧的工具装配(设计 §5.1、§6.3):tool-runner 的注入项。[B2]
|
|
3
|
+
*
|
|
4
|
+
* 一次工具调用的门禁顺序(§6.3):schema 校验(tool-runner)→ 命令式 Hook PreToolUse(deny / block
|
|
5
|
+
* 一票否决;updatedInput 替换输入;allow / ask 作为 hookDecision 交给管线)→ 权限管线(B3,注入)→
|
|
6
|
+
* ask 时走审批链(宿主 broker → UI broker → 无人值守 deny;超时 deny)→ 执行 → PostToolUse
|
|
7
|
+
* (additionalContext 追加到结果末尾;block 把结果改为错误)。
|
|
8
|
+
*
|
|
9
|
+
* (W3-B9a-2)ask 时请求带 `context{depth, readFiles}` 与执行前预览 `preview`(只读、有上限,
|
|
10
|
+
* 见 permissions/preview.ts);预览出错或为空则缺省,不影响审批。`permission_request` 事件同样带
|
|
11
|
+
* `preview`,RPC 客户端可直接显示。
|
|
12
|
+
*/
|
|
13
|
+
import { randomUUID } from "node:crypto";
|
|
14
|
+
import { DEFAULT_APPROVAL_TIMEOUT_MS } from "../permissions/broker.js";
|
|
15
|
+
import { previewAction } from "../permissions/preview.js";
|
|
16
|
+
import { runSingleToolCall } from "./tool-runner.js";
|
|
17
|
+
/** 外层是这个工具时,嵌套调用的 Hook 输入带 `viaCodemode: true`。 */
|
|
18
|
+
const CODEMODE_TOOL_NAME = "codemode";
|
|
19
|
+
function textOf(content) {
|
|
20
|
+
if (typeof content === "string")
|
|
21
|
+
return content;
|
|
22
|
+
return content.map((block) => (block.type === "text" ? block.text : "")).join("");
|
|
23
|
+
}
|
|
24
|
+
/** 审批链:依次问每个 broker,第一个给出决定的为准;全部弃权 → deny。 */
|
|
25
|
+
export async function requestApproval(core, request, signal) {
|
|
26
|
+
const timeoutMs = core.options.approvalTimeoutMs ?? DEFAULT_APPROVAL_TIMEOUT_MS;
|
|
27
|
+
const event = {
|
|
28
|
+
type: "permission_request",
|
|
29
|
+
requestId: request.requestId,
|
|
30
|
+
toolName: request.toolName,
|
|
31
|
+
input: request.input,
|
|
32
|
+
reason: request.reason,
|
|
33
|
+
timeoutMs,
|
|
34
|
+
};
|
|
35
|
+
if (request.hookReason !== undefined)
|
|
36
|
+
event.hookReason = request.hookReason;
|
|
37
|
+
if (request.preview !== undefined)
|
|
38
|
+
event.preview = request.preview;
|
|
39
|
+
core.emit(event);
|
|
40
|
+
void core.runHook("Notification", {
|
|
41
|
+
notification: { kind: "approval", message: `approval requested for ${request.toolName}` },
|
|
42
|
+
});
|
|
43
|
+
const controller = new AbortController();
|
|
44
|
+
const onAbort = () => controller.abort();
|
|
45
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
46
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
47
|
+
let decision = "deny";
|
|
48
|
+
try {
|
|
49
|
+
for (const broker of core.options.brokers ?? []) {
|
|
50
|
+
if (controller.signal.aborted)
|
|
51
|
+
break;
|
|
52
|
+
const answer = await Promise.race([
|
|
53
|
+
broker.ask(request, controller.signal).catch(() => undefined),
|
|
54
|
+
new Promise((resolve) => controller.signal.addEventListener("abort", () => resolve(undefined), { once: true })),
|
|
55
|
+
]);
|
|
56
|
+
if (answer !== undefined) {
|
|
57
|
+
decision = answer;
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
signal.removeEventListener("abort", onAbort);
|
|
65
|
+
}
|
|
66
|
+
if (signal.aborted)
|
|
67
|
+
decision = "deny"; // run 被中断:即使 broker 已放行也不执行
|
|
68
|
+
core.emit({ type: "permission_resolved", requestId: request.requestId, decision });
|
|
69
|
+
return decision;
|
|
70
|
+
}
|
|
71
|
+
/** 嵌套调用时 Hook 输入的两个字段(设计 §5.5);模型直接发起的调用不带。 */
|
|
72
|
+
function nestedFields(parent) {
|
|
73
|
+
if (parent === undefined)
|
|
74
|
+
return {};
|
|
75
|
+
return parent.viaCodemode
|
|
76
|
+
? { viaCodemode: true, parentToolCallId: parent.toolCallId }
|
|
77
|
+
: { parentToolCallId: parent.toolCallId };
|
|
78
|
+
}
|
|
79
|
+
/** 审批请求的执行前预览;出错或没有可显示的内容时 undefined(预览永不阻塞审批)。 */
|
|
80
|
+
function safePreview(core, request) {
|
|
81
|
+
try {
|
|
82
|
+
const preview = previewAction(request, { cwd: core.cwd });
|
|
83
|
+
return preview.lines.length > 0 ? preview : undefined;
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
core.log("warn", `approval preview failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export async function gateToolCall(core, call, ctx) {
|
|
91
|
+
const tool = ctx.tool;
|
|
92
|
+
if (tool === undefined)
|
|
93
|
+
return { block: true, reason: `Tool ${call.name} not found` };
|
|
94
|
+
let input = call.arguments;
|
|
95
|
+
let replaced = false;
|
|
96
|
+
let hookDecision;
|
|
97
|
+
let hookReason;
|
|
98
|
+
if (core.options.hooks?.has("PreToolUse", call.name) === true) {
|
|
99
|
+
const outcome = await core.runHook("PreToolUse", { toolCallId: call.id, toolName: call.name, toolInput: input, ...nestedFields(ctx.parent) }, ctx.signal);
|
|
100
|
+
if (outcome !== undefined) {
|
|
101
|
+
if (outcome.stop)
|
|
102
|
+
core.requestStop(outcome.reason);
|
|
103
|
+
if (outcome.decision === "deny" || outcome.decision === "block") {
|
|
104
|
+
return { block: true, reason: outcome.reason ?? `Blocked by PreToolUse hook` };
|
|
105
|
+
}
|
|
106
|
+
if (outcome.hasUpdatedInput) {
|
|
107
|
+
input = outcome.updatedInput;
|
|
108
|
+
replaced = true;
|
|
109
|
+
}
|
|
110
|
+
if (outcome.decision === "allow" || outcome.decision === "ask") {
|
|
111
|
+
hookDecision = outcome.decision;
|
|
112
|
+
hookReason = outcome.reason;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const permission = core.options.permission;
|
|
117
|
+
let decision = hookDecision === "ask" ? "ask" : "allow";
|
|
118
|
+
let approvalReason = "hook";
|
|
119
|
+
let denyMessage;
|
|
120
|
+
if (permission !== undefined) {
|
|
121
|
+
const checkInput = {
|
|
122
|
+
toolName: call.name,
|
|
123
|
+
permission: tool.permission,
|
|
124
|
+
input,
|
|
125
|
+
unattended: core.options.unattended === true,
|
|
126
|
+
};
|
|
127
|
+
if (hookDecision !== undefined)
|
|
128
|
+
checkInput.hookDecision = hookDecision;
|
|
129
|
+
if (hookReason !== undefined)
|
|
130
|
+
checkInput.hookReason = hookReason;
|
|
131
|
+
const verdict = permission.check(checkInput);
|
|
132
|
+
decision = verdict.decision;
|
|
133
|
+
approvalReason = verdict.approvalReason ?? "mode";
|
|
134
|
+
denyMessage = verdict.message;
|
|
135
|
+
}
|
|
136
|
+
else if (decision === "ask" && core.options.unattended === true) {
|
|
137
|
+
decision = "deny";
|
|
138
|
+
}
|
|
139
|
+
if (decision === "deny") {
|
|
140
|
+
return { block: true, reason: denyMessage ?? `Permission denied for ${call.name}` };
|
|
141
|
+
}
|
|
142
|
+
if (decision === "ask") {
|
|
143
|
+
const request = {
|
|
144
|
+
requestId: randomUUID(),
|
|
145
|
+
toolName: call.name,
|
|
146
|
+
input,
|
|
147
|
+
reason: approvalReason,
|
|
148
|
+
context: { depth: core.depth, readFiles: core.readFiles },
|
|
149
|
+
};
|
|
150
|
+
if (hookReason !== undefined)
|
|
151
|
+
request.hookReason = hookReason;
|
|
152
|
+
const preview = safePreview(core, request);
|
|
153
|
+
if (preview !== undefined)
|
|
154
|
+
request.preview = preview;
|
|
155
|
+
const answer = await requestApproval(core, request, ctx.signal);
|
|
156
|
+
if (answer === "deny")
|
|
157
|
+
return { block: true, reason: `The user denied ${call.name}` };
|
|
158
|
+
if (answer === "allow_session")
|
|
159
|
+
permission?.rememberForSession(call.name, input);
|
|
160
|
+
}
|
|
161
|
+
return replaced ? { input } : {};
|
|
162
|
+
}
|
|
163
|
+
export async function afterToolCall(core, call, result, parent) {
|
|
164
|
+
if (core.options.hooks?.has("PostToolUse", call.name) !== true)
|
|
165
|
+
return result;
|
|
166
|
+
const outcome = await core.runHook("PostToolUse", {
|
|
167
|
+
toolCallId: call.id,
|
|
168
|
+
toolName: call.name,
|
|
169
|
+
toolInput: call.arguments,
|
|
170
|
+
toolResult: { content: textOf(result.content), isError: result.isError === true },
|
|
171
|
+
...nestedFields(parent),
|
|
172
|
+
});
|
|
173
|
+
if (outcome === undefined)
|
|
174
|
+
return result;
|
|
175
|
+
if (outcome.stop)
|
|
176
|
+
core.requestStop(outcome.reason);
|
|
177
|
+
let next = result;
|
|
178
|
+
const extras = [];
|
|
179
|
+
if (outcome.decision === "block" || outcome.decision === "deny") {
|
|
180
|
+
next = { ...next, isError: true };
|
|
181
|
+
extras.push(outcome.reason ?? "Blocked by PostToolUse hook");
|
|
182
|
+
}
|
|
183
|
+
if (outcome.additionalContext !== undefined && outcome.additionalContext !== "") {
|
|
184
|
+
extras.push(outcome.additionalContext);
|
|
185
|
+
}
|
|
186
|
+
if (extras.length === 0)
|
|
187
|
+
return next;
|
|
188
|
+
const suffix = `\n\n${extras.join("\n\n")}`;
|
|
189
|
+
const content = typeof next.content === "string"
|
|
190
|
+
? next.content + suffix
|
|
191
|
+
: [...next.content, { type: "text", text: suffix.trimStart() }];
|
|
192
|
+
return { ...next, content };
|
|
193
|
+
}
|
|
194
|
+
export function createToolContext(core, runner, call, signal, onUpdate) {
|
|
195
|
+
const model = core.model();
|
|
196
|
+
let nested = 0;
|
|
197
|
+
const ctx = {
|
|
198
|
+
toolCallId: call.id,
|
|
199
|
+
cwd: core.cwd,
|
|
200
|
+
sessionId: core.manager.id,
|
|
201
|
+
signal,
|
|
202
|
+
depth: core.depth,
|
|
203
|
+
model: { provider: model.provider, id: model.id },
|
|
204
|
+
thinkingLevel: core.thinkingLevel(),
|
|
205
|
+
onUpdate,
|
|
206
|
+
readFiles: core.readFiles,
|
|
207
|
+
markRead: (path) => {
|
|
208
|
+
core.readFiles.add(path);
|
|
209
|
+
},
|
|
210
|
+
tools: {
|
|
211
|
+
executeTool: (name, input, options) => {
|
|
212
|
+
nested++;
|
|
213
|
+
const nestedCall = {
|
|
214
|
+
type: "toolCall",
|
|
215
|
+
id: `${call.id}_n${nested}`,
|
|
216
|
+
name,
|
|
217
|
+
arguments: typeof input === "object" && input !== null && !Array.isArray(input)
|
|
218
|
+
? input
|
|
219
|
+
: {},
|
|
220
|
+
};
|
|
221
|
+
const assistant = core.agent.messages.findLast((message) => message.role === "assistant");
|
|
222
|
+
if (assistant === undefined || assistant.role !== "assistant") {
|
|
223
|
+
return Promise.resolve({ content: "no active assistant message", isError: true });
|
|
224
|
+
}
|
|
225
|
+
const nestedSignal = options?.signal === undefined ? signal : AbortSignal.any([signal, options.signal]);
|
|
226
|
+
return runSingleToolCall(nestedCall, assistant, runner, nestedSignal, {
|
|
227
|
+
parent: { toolCallId: call.id, viaCodemode: call.name === CODEMODE_TOOL_NAME },
|
|
228
|
+
emit: async (event) => core.emit(event),
|
|
229
|
+
});
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
session: {
|
|
233
|
+
appendCustom: (customType, data) => {
|
|
234
|
+
core.appendEntry({ type: "custom", customType, data });
|
|
235
|
+
},
|
|
236
|
+
lastCustom: (customType) => {
|
|
237
|
+
const branch = core.manager.branch();
|
|
238
|
+
for (let i = branch.length - 1; i >= 0; i--) {
|
|
239
|
+
const entry = branch[i];
|
|
240
|
+
if (entry?.type === "custom" && entry.customType === customType)
|
|
241
|
+
return entry.data;
|
|
242
|
+
}
|
|
243
|
+
return undefined;
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
log: (level, message) => core.log(level, message),
|
|
247
|
+
};
|
|
248
|
+
const file = core.manager.file();
|
|
249
|
+
const outputDir = core.outputDir();
|
|
250
|
+
const extra = {};
|
|
251
|
+
if (file !== undefined)
|
|
252
|
+
extra.sessionFile = file;
|
|
253
|
+
if (outputDir !== undefined)
|
|
254
|
+
extra.outputDir = outputDir;
|
|
255
|
+
if (core.depth === 0 && core.options.subagents !== false) {
|
|
256
|
+
extra.spawnSubagent = (request) => core.spawnSubagent(request);
|
|
257
|
+
}
|
|
258
|
+
return Object.assign(ctx, extra);
|
|
259
|
+
}
|
|
260
|
+
export function createToolRunnerOptions(core) {
|
|
261
|
+
const runner = {
|
|
262
|
+
getTool: (name) => core.activeTool(name),
|
|
263
|
+
getNestedTool: (name) => core.tool(name),
|
|
264
|
+
beforeToolCall: (call, ctx) => gateToolCall(core, call, ctx),
|
|
265
|
+
afterToolCall: (call, result, ctx) => afterToolCall(core, call, result, ctx?.parent),
|
|
266
|
+
createToolContext: (call, signal, onUpdate) => createToolContext(core, runner, call, signal, onUpdate),
|
|
267
|
+
get outputDir() {
|
|
268
|
+
return core.outputDir();
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
if (core.options.maxToolResultChars !== undefined) {
|
|
272
|
+
runner.maxToolResultChars = core.options.maxToolResultChars;
|
|
273
|
+
}
|
|
274
|
+
if (core.options.abortGraceMs !== undefined)
|
|
275
|
+
runner.abortGraceMs = core.options.abortGraceMs;
|
|
276
|
+
return runner;
|
|
277
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentSessionImpl(设计 §4.2–§4.3、§9、§11.1 第 16 步、§13.1)。[B2]
|
|
3
|
+
*
|
|
4
|
+
* 会话 = SessionManager(JSONL 树,事实来源)+ Agent(上下文消息、队列、run)+ 压缩 / 重试 / Hook 调度。
|
|
5
|
+
* - 每条 message_end 落盘为条目;Agent 的上下文 = 活动分支的投影(压缩 / context_edit 后重建)。
|
|
6
|
+
* - 首次模型请求前把系统提示 + 工具表作为首条 `system` 消息落盘并 `flush()`(此时才创建文件);
|
|
7
|
+
* 之后节或工具表变化落 system 补丁。
|
|
8
|
+
* - `prompt()`:运行中且无 streamingBehavior → `AmaError{code:"busy"}`;否则展开(expandPrompt)→
|
|
9
|
+
* UserPromptSubmit Hook → 阈值压缩检查 → 会话周期(session-run.ts);周期结束后 resolve。
|
|
10
|
+
* - `steer()` / `followUp()`:运行中入队(返回 "queued");空闲时直接以该消息开始一个周期("handled")。
|
|
11
|
+
* - `abort()`:中断供应商流、工具、重试等待与压缩;回到 idle 后 resolve;不清队列。
|
|
12
|
+
*/
|
|
13
|
+
import type { Model, ModelThinkingLevel } from "../ai/types.js";
|
|
14
|
+
import type { HookEvent, HookEventPayload, HookOutcome } from "../hooks/types.js";
|
|
15
|
+
import type { PermissionMode } from "../permissions/types.js";
|
|
16
|
+
import type { SessionManager } from "../session/manager.js";
|
|
17
|
+
import type { AgentMessage, BranchSummaryEntry, SessionEntry, SessionEntryInput } from "../session/types.js";
|
|
18
|
+
import type { SubagentRequest, SubagentResult, ToolDefinition } from "../tools/types.js";
|
|
19
|
+
import { Agent } from "./agent.js";
|
|
20
|
+
import type { StreamFn } from "./loop.js";
|
|
21
|
+
import type { AgentSessionOptions, SessionCore } from "./session-core.js";
|
|
22
|
+
import { SessionCacheController } from "./session-cache.js";
|
|
23
|
+
import type { SystemPromptInput } from "./system-prompt.js";
|
|
24
|
+
import type { AgentSession, CompactionResult, EnqueueOptions, PromptDisposition, PromptOptions, SessionEvent, SessionState, SessionStats } from "./types.js";
|
|
25
|
+
export type { AgentSessionOptions } from "./session-core.js";
|
|
26
|
+
export declare class AgentSessionImpl implements AgentSession, SessionCore {
|
|
27
|
+
readonly options: AgentSessionOptions;
|
|
28
|
+
readonly manager: SessionManager;
|
|
29
|
+
readonly agent: Agent;
|
|
30
|
+
readonly cwd: string;
|
|
31
|
+
readonly depth: number;
|
|
32
|
+
readonly readFiles: Set<string>;
|
|
33
|
+
readonly stream: StreamFn;
|
|
34
|
+
readonly cache: SessionCacheController;
|
|
35
|
+
private currentModel;
|
|
36
|
+
private currentThinking;
|
|
37
|
+
private readonly allTools;
|
|
38
|
+
private activeNames;
|
|
39
|
+
private readonly listeners;
|
|
40
|
+
private readonly compaction;
|
|
41
|
+
private retrySettings;
|
|
42
|
+
private cycle;
|
|
43
|
+
private retrying;
|
|
44
|
+
private stopReason;
|
|
45
|
+
private stopFlag;
|
|
46
|
+
private turns;
|
|
47
|
+
private systemInput;
|
|
48
|
+
private readonly subagentPool;
|
|
49
|
+
private disposed;
|
|
50
|
+
/** 已入队、尚未投递的消息(投递时发 queue_update)。 */
|
|
51
|
+
private readonly queuedMessages;
|
|
52
|
+
constructor(options: AgentSessionOptions);
|
|
53
|
+
model(): Model;
|
|
54
|
+
thinkingLevel(): ModelThinkingLevel;
|
|
55
|
+
outputDir(): string | undefined;
|
|
56
|
+
resolveApiKey(): Promise<string | undefined>;
|
|
57
|
+
emit(event: SessionEvent): void;
|
|
58
|
+
appendEntry(input: SessionEntryInput): SessionEntry;
|
|
59
|
+
reloadMessages(): void;
|
|
60
|
+
activeTool(name: string): ToolDefinition | undefined;
|
|
61
|
+
tool(name: string): ToolDefinition | undefined;
|
|
62
|
+
activeToolNames(): string[];
|
|
63
|
+
runHook(event: HookEvent, payload: HookEventPayload, signal?: AbortSignal): Promise<HookOutcome | undefined>;
|
|
64
|
+
requestStop(reason: string | undefined): void;
|
|
65
|
+
log(level: "debug" | "info" | "warn" | "error", message: string): void;
|
|
66
|
+
private onAgentEvent;
|
|
67
|
+
private emitQueue;
|
|
68
|
+
private syncSystem;
|
|
69
|
+
private assertUsable;
|
|
70
|
+
private startCycle;
|
|
71
|
+
private runDeps;
|
|
72
|
+
prompt(text: string, options?: PromptOptions): Promise<PromptDisposition>;
|
|
73
|
+
private enqueue;
|
|
74
|
+
private enqueueOrRun;
|
|
75
|
+
steer(text: string, options?: EnqueueOptions): Promise<"queued" | "handled">;
|
|
76
|
+
followUp(text: string, options?: EnqueueOptions): Promise<"queued" | "handled">;
|
|
77
|
+
abort(): Promise<void>;
|
|
78
|
+
waitForIdle(): Promise<void>;
|
|
79
|
+
clearQueue(): {
|
|
80
|
+
steering: string[];
|
|
81
|
+
followUp: string[];
|
|
82
|
+
};
|
|
83
|
+
subscribe(listener: (event: SessionEvent) => void): () => void;
|
|
84
|
+
/** 手动压缩;运行中 → busy。 */
|
|
85
|
+
compact(instructions?: string): Promise<CompactionResult>;
|
|
86
|
+
fork(entryId: string): Promise<AgentSessionImpl>;
|
|
87
|
+
/**
|
|
88
|
+
* `/tree`:同文件换叶子;`summarize` 时为离开的分支写 branch_summary(挂在新叶子下)。
|
|
89
|
+
*/
|
|
90
|
+
navigate(targetId: string | null, options?: {
|
|
91
|
+
summarize?: boolean;
|
|
92
|
+
instructions?: string;
|
|
93
|
+
}): Promise<BranchSummaryEntry | undefined>;
|
|
94
|
+
setModel(ref: string): Promise<void>;
|
|
95
|
+
setThinkingLevel(level: ModelThinkingLevel): void;
|
|
96
|
+
setPermissionMode(mode: PermissionMode): void;
|
|
97
|
+
setActiveTools(names: string[]): void;
|
|
98
|
+
/** 宿主 / SDK 在会话创建后追加工具(下次请求前以 system 补丁声明)。 */
|
|
99
|
+
addTool(tool: ToolDefinition, active?: boolean): void;
|
|
100
|
+
/** 更新系统提示的静态部分(SessionStart Hook 的 hookContext、宿主 instructions 等)。 */
|
|
101
|
+
updateSystem(patch: Partial<Omit<SystemPromptInput, "tools" | "cwd">>): void;
|
|
102
|
+
setAutoCompaction(enabled: boolean): void;
|
|
103
|
+
setAutoRetry(enabled: boolean): void;
|
|
104
|
+
getTools(): readonly ToolDefinition[];
|
|
105
|
+
/** 发 session_start(bootstrap / SDK 在会话就绪后调用一次)。 */
|
|
106
|
+
announceStart(reason: "startup" | "resume" | "new" | "fork"): void;
|
|
107
|
+
get state(): SessionState;
|
|
108
|
+
get messages(): readonly AgentMessage[];
|
|
109
|
+
get entries(): readonly SessionEntry[];
|
|
110
|
+
/** run 被 Hook 的 continue:false 结束时的说明。 */
|
|
111
|
+
get stopHookReason(): string | undefined;
|
|
112
|
+
getLastAssistantText(): string | null;
|
|
113
|
+
getStats(): SessionStats;
|
|
114
|
+
dispose(): Promise<void>;
|
|
115
|
+
childBase(): Pick<AgentSessionOptions, "model" | "thinkingLevel" | "activeTools" | "system">;
|
|
116
|
+
spawnSubagent(request: SubagentRequest): Promise<SubagentResult>;
|
|
117
|
+
}
|