@armadra/agent 0.0.0-stage → 0.2.1
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 +22 -0
- package/LICENSE +21 -0
- package/README.md +426 -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 +511 -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 +118 -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 +104 -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 +484 -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 +303 -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 +102 -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/registry.d.ts +83 -0
- package/dist/ai/providers/registry.js +305 -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 +428 -0
- package/dist/ai/types.js +19 -0
- package/dist/bundle/ama-sandbox.cjs +324 -0
- package/dist/bundle/ama.cjs +25770 -0
- package/dist/bundle.d.ts +9 -0
- package/dist/bundle.js +15 -0
- package/dist/cli/args.d.ts +82 -0
- package/dist/cli/args.js +381 -0
- package/dist/cli/bootstrap.d.ts +18 -0
- package/dist/cli/bootstrap.js +398 -0
- package/dist/cli/compose-providers.d.ts +37 -0
- package/dist/cli/compose-providers.js +104 -0
- package/dist/cli/compose-session.d.ts +69 -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 +106 -0
- package/dist/cli/compose.js +206 -0
- package/dist/cli/default-model.d.ts +16 -0
- package/dist/cli/default-model.js +24 -0
- package/dist/cli/deps.d.ts +205 -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 +26 -0
- package/dist/cli/main.js +165 -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 +197 -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 +44 -0
- package/dist/cli/subcommands/config.js +201 -0
- package/dist/cli/subcommands/context.d.ts +21 -0
- package/dist/cli/subcommands/context.js +41 -0
- package/dist/cli/subcommands/doctor.d.ts +11 -0
- package/dist/cli/subcommands/doctor.js +246 -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 +40 -0
- package/dist/cli/subcommands/models-discover.js +204 -0
- package/dist/cli/subcommands/models.d.ts +40 -0
- package/dist/cli/subcommands/models.js +144 -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/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 +413 -0
- package/dist/config/trust.d.ts +42 -0
- package/dist/config/trust.js +122 -0
- package/dist/config/types.d.ts +172 -0
- package/dist/config/types.js +27 -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/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 +202 -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 +487 -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 +216 -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 +28 -0
- package/dist/modes/interactive/startup-ui.js +238 -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 +15 -0
- package/dist/modes/print/print-mode.js +96 -0
- package/dist/modes/rpc/commands.d.ts +40 -0
- package/dist/modes/rpc/commands.js +258 -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 +171 -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 +180 -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 +199 -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/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 +28 -0
- package/dist/tools/read.js +181 -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 +316 -0
- package/docs/rpc.md +253 -0
- package/docs/session-format.md +119 -0
- package/docs/tui.md +168 -0
- package/package.json +101 -4
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具契约(设计 §5.1)。[B0] 契约文件,实现归 B3;也从 `./host` 再导出。
|
|
3
|
+
*
|
|
4
|
+
* 补全与偏差:
|
|
5
|
+
* - `ToolContext.readFiles` 是只读集合,但 read 工具要「成功后加入」——增加 `markRead(path)`。
|
|
6
|
+
* - bash 要注入 `AMA_PROVIDER / AMA_MODEL / AMA_THINKING`(§5.2),增加 `model` 与 `thinkingLevel`。
|
|
7
|
+
* - 截断全文落 `<sessionDir>/outputs/<toolCallId>.txt`(§4.4),增加 `outputDir`
|
|
8
|
+
* (内存会话为 undefined,实现自行落到临时目录)。
|
|
9
|
+
* - todo 工具要写 / 读 `custom` 条目,增加 `session.appendCustom / lastCustom`。
|
|
10
|
+
* - task 工具要开子会话,但 B3 不得 import B2 的实现——增加 `spawnSubagent`,由
|
|
11
|
+
* AgentSession(B2)在构造 ToolContext 时提供;子 Agent(depth ≥ 1)里为 undefined。
|
|
12
|
+
* - `defineTool()` 是恒等函数,只为推断 `I`;放在契约文件里供 SDK 再导出。
|
|
13
|
+
* - `ToolRegistryApi` 是 Runtime 需要的最小接口,B3 的 `ToolRegistry` 实现它。
|
|
14
|
+
* - (W3-C0)`SubagentResult.cache` 可选:子会话的命中率与重计费 token。
|
|
15
|
+
*/
|
|
16
|
+
import type { ContentBlock, JsonSchema, ModelRef, ModelThinkingLevel, Usage } from "../ai/types.js";
|
|
17
|
+
export type { JsonSchema } from "../ai/types.js";
|
|
18
|
+
/** 权限管线的粗分类(§7)。 */
|
|
19
|
+
export type ToolPermission = "read" | "write" | "execute";
|
|
20
|
+
export type ToolExecutionMode = "sequential" | "parallel";
|
|
21
|
+
export interface ToolAnnotations {
|
|
22
|
+
readOnly?: boolean;
|
|
23
|
+
destructive?: boolean;
|
|
24
|
+
openWorld?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface ToolDefinition<I = unknown> {
|
|
27
|
+
/** `^[a-z][a-z0-9_]{1,63}$`;宿主工具建议前缀(canvas_*)。 */
|
|
28
|
+
readonly name: string;
|
|
29
|
+
/** TUI 标题。 */
|
|
30
|
+
readonly label?: string;
|
|
31
|
+
readonly description: string;
|
|
32
|
+
readonly parameters: JsonSchema;
|
|
33
|
+
readonly permission: ToolPermission;
|
|
34
|
+
/** 缺省:read → parallel;write / execute → sequential。 */
|
|
35
|
+
readonly executionMode?: ToolExecutionMode;
|
|
36
|
+
readonly annotations?: ToolAnnotations;
|
|
37
|
+
/** 系统提示 tools 节一行。 */
|
|
38
|
+
readonly promptSnippet?: string;
|
|
39
|
+
/** 系统提示 rules 节。 */
|
|
40
|
+
readonly promptGuidelines?: readonly string[];
|
|
41
|
+
execute(input: I, ctx: ToolContext): Promise<ToolResult>;
|
|
42
|
+
/** TUI 可选自定义渲染。 */
|
|
43
|
+
renderCall?(input: I, width: number): string[];
|
|
44
|
+
renderResult?(result: ToolResult, width: number, expanded: boolean): string[];
|
|
45
|
+
}
|
|
46
|
+
export interface ToolResult {
|
|
47
|
+
content: string | ContentBlock[];
|
|
48
|
+
isError?: boolean;
|
|
49
|
+
/** 落盘,不进上下文。 */
|
|
50
|
+
details?: unknown;
|
|
51
|
+
structured?: unknown;
|
|
52
|
+
/** 整批工具结果都为 true 才提前结束 run。 */
|
|
53
|
+
terminate?: boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface SubagentRequest {
|
|
56
|
+
prompt: string;
|
|
57
|
+
description?: string;
|
|
58
|
+
/** 缺省 = 父活动集去掉 task。 */
|
|
59
|
+
tools?: string[];
|
|
60
|
+
/** `provider/model-id`;缺省继承父。 */
|
|
61
|
+
model?: string;
|
|
62
|
+
thinkingLevel?: ModelThinkingLevel;
|
|
63
|
+
/** 缺省 30。 */
|
|
64
|
+
maxTurns?: number;
|
|
65
|
+
/** 父 toolCallId(写入子会话的 custom{ama.task})。 */
|
|
66
|
+
parentToolCallId: string;
|
|
67
|
+
signal: AbortSignal;
|
|
68
|
+
onUpdate?(partial: string): void;
|
|
69
|
+
}
|
|
70
|
+
export interface SubagentResult {
|
|
71
|
+
/** 子会话最后一条助手文本。 */
|
|
72
|
+
text: string;
|
|
73
|
+
sessionFile?: string;
|
|
74
|
+
usage: Usage;
|
|
75
|
+
stopReason: string;
|
|
76
|
+
isError: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* [W3-C0] 子会话自己的缓存统计(第三波 §1.9),供 task 结果 details 与 `/session` 的
|
|
79
|
+
* 「子任务」行;子会话未接缓存控制器时缺省。
|
|
80
|
+
*/
|
|
81
|
+
cache?: {
|
|
82
|
+
hitRate?: number;
|
|
83
|
+
reBilledTokens: number;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export interface ToolContext {
|
|
87
|
+
readonly toolCallId: string;
|
|
88
|
+
readonly cwd: string;
|
|
89
|
+
readonly sessionId: string;
|
|
90
|
+
readonly sessionFile?: string;
|
|
91
|
+
readonly signal: AbortSignal;
|
|
92
|
+
/** task 深度:主会话 0,子 Agent 1。 */
|
|
93
|
+
readonly depth: number;
|
|
94
|
+
readonly model?: ModelRef;
|
|
95
|
+
readonly thinkingLevel?: ModelThinkingLevel;
|
|
96
|
+
/** 截断全文的落盘目录(`<sessionDir>/outputs`);内存会话为 undefined。 */
|
|
97
|
+
readonly outputDir?: string;
|
|
98
|
+
onUpdate(partial: string): void;
|
|
99
|
+
/** 本会话已 read 的绝对路径(write / edit 先读后写检查)。 */
|
|
100
|
+
readonly readFiles: ReadonlySet<string>;
|
|
101
|
+
markRead(absolutePath: string): void;
|
|
102
|
+
/**
|
|
103
|
+
* 嵌套调用其它工具(codemode 脚本、task),受同一管线;按全部未禁用工具查找(不限于活动集),
|
|
104
|
+
* 发带 `parentToolCallId` 的 tool_execution_* 事件。`signal`:与 ctx.signal 合并,用于提前取消
|
|
105
|
+
* 这一次嵌套调用(codemode 脚本结束时取消仍在跑的调用)。
|
|
106
|
+
*/
|
|
107
|
+
readonly tools: {
|
|
108
|
+
executeTool(name: string, input: unknown, options?: {
|
|
109
|
+
signal?: AbortSignal;
|
|
110
|
+
}): Promise<ToolResult>;
|
|
111
|
+
};
|
|
112
|
+
readonly session: {
|
|
113
|
+
/** 追加 custom 条目(不进上下文)。 */
|
|
114
|
+
appendCustom(customType: string, data: unknown): void;
|
|
115
|
+
/** 活动分支上最近一条该类型 custom 条目的 data。 */
|
|
116
|
+
lastCustom(customType: string): unknown;
|
|
117
|
+
};
|
|
118
|
+
/** 仅 depth 0 且 task 可用时存在。 */
|
|
119
|
+
readonly spawnSubagent?: (request: SubagentRequest) => Promise<SubagentResult>;
|
|
120
|
+
log(level: "debug" | "info" | "warn", message: string): void;
|
|
121
|
+
}
|
|
122
|
+
export type ToolSource = "builtin" | "host" | "sdk";
|
|
123
|
+
export interface ToolRegistryApi {
|
|
124
|
+
/** 同名已存在则抛 AmaError{code:"tool_exists"}。 */
|
|
125
|
+
register(tool: ToolDefinition, source: ToolSource): void;
|
|
126
|
+
disable(name: string): void;
|
|
127
|
+
get(name: string): ToolDefinition | undefined;
|
|
128
|
+
/** 已注册且未禁用的名字,按名排序。 */
|
|
129
|
+
list(): readonly string[];
|
|
130
|
+
/** 当前活动集(发给模型的工具),按名排序。 */
|
|
131
|
+
active(): readonly ToolDefinition[];
|
|
132
|
+
setActive(names: readonly string[]): void;
|
|
133
|
+
}
|
|
134
|
+
/** 恒等函数,帮助推断输入类型。 */
|
|
135
|
+
export declare function defineTool<I>(tool: ToolDefinition<I>): ToolDefinition<I>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具契约(设计 §5.1)。[B0] 契约文件,实现归 B3;也从 `./host` 再导出。
|
|
3
|
+
*
|
|
4
|
+
* 补全与偏差:
|
|
5
|
+
* - `ToolContext.readFiles` 是只读集合,但 read 工具要「成功后加入」——增加 `markRead(path)`。
|
|
6
|
+
* - bash 要注入 `AMA_PROVIDER / AMA_MODEL / AMA_THINKING`(§5.2),增加 `model` 与 `thinkingLevel`。
|
|
7
|
+
* - 截断全文落 `<sessionDir>/outputs/<toolCallId>.txt`(§4.4),增加 `outputDir`
|
|
8
|
+
* (内存会话为 undefined,实现自行落到临时目录)。
|
|
9
|
+
* - todo 工具要写 / 读 `custom` 条目,增加 `session.appendCustom / lastCustom`。
|
|
10
|
+
* - task 工具要开子会话,但 B3 不得 import B2 的实现——增加 `spawnSubagent`,由
|
|
11
|
+
* AgentSession(B2)在构造 ToolContext 时提供;子 Agent(depth ≥ 1)里为 undefined。
|
|
12
|
+
* - `defineTool()` 是恒等函数,只为推断 `I`;放在契约文件里供 SDK 再导出。
|
|
13
|
+
* - `ToolRegistryApi` 是 Runtime 需要的最小接口,B3 的 `ToolRegistry` 实现它。
|
|
14
|
+
* - (W3-C0)`SubagentResult.cache` 可选:子会话的命中率与重计费 token。
|
|
15
|
+
*/
|
|
16
|
+
/** 恒等函数,帮助推断输入类型。 */
|
|
17
|
+
export function defineTool(tool) {
|
|
18
|
+
return tool;
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `write` 工具(设计 §5.2)。[B3]
|
|
3
|
+
*
|
|
4
|
+
* 整文件覆盖、自动建父目录;文件已存在且不在 `ctx.readFiles` → 错误「先 read」;若原文件有 BOM
|
|
5
|
+
* 或 CRLF,写回时保留(新内容自带 BOM / CRLF 时不重复加)。`details: { bytes, created }`。
|
|
6
|
+
*/
|
|
7
|
+
import type { ToolContext, ToolDefinition, ToolResult } from "./types.js";
|
|
8
|
+
export interface WriteInput {
|
|
9
|
+
path: string;
|
|
10
|
+
content: string;
|
|
11
|
+
}
|
|
12
|
+
export interface WriteDetails {
|
|
13
|
+
path: string;
|
|
14
|
+
bytes: number;
|
|
15
|
+
created: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** 按原文件的 BOM / 换行风格调整新内容。 */
|
|
18
|
+
export declare function conformToOriginal(original: string, content: string): string;
|
|
19
|
+
export declare function executeWrite(input: WriteInput, ctx: ToolContext): Promise<ToolResult>;
|
|
20
|
+
export declare function createWriteTool(): ToolDefinition<WriteInput>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `write` 工具(设计 §5.2)。[B3]
|
|
3
|
+
*
|
|
4
|
+
* 整文件覆盖、自动建父目录;文件已存在且不在 `ctx.readFiles` → 错误「先 read」;若原文件有 BOM
|
|
5
|
+
* 或 CRLF,写回时保留(新内容自带 BOM / CRLF 时不重复加)。`details: { bytes, created }`。
|
|
6
|
+
*/
|
|
7
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
8
|
+
import { dirname } from "node:path";
|
|
9
|
+
import { displayPath, resolvePath } from "./paths.js";
|
|
10
|
+
import { withFileMutex } from "./file-mutex.js";
|
|
11
|
+
import { BOM, detectLineEnding, normalizeToLF, restoreLineEndings, splitBom, } from "./edit-fuzzy.js";
|
|
12
|
+
async function exists(path) {
|
|
13
|
+
try {
|
|
14
|
+
const info = await stat(path);
|
|
15
|
+
return info.isDirectory() ? "dir" : "file";
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** 按原文件的 BOM / 换行风格调整新内容。 */
|
|
22
|
+
export function conformToOriginal(original, content) {
|
|
23
|
+
const { bom, text } = splitBom(original);
|
|
24
|
+
const incoming = splitBom(content);
|
|
25
|
+
let body = incoming.text;
|
|
26
|
+
if (detectLineEnding(text) === "\r\n" && text.includes("\n")) {
|
|
27
|
+
body = restoreLineEndings(normalizeToLF(body), "\r\n");
|
|
28
|
+
}
|
|
29
|
+
return (bom !== "" || incoming.bom !== "" ? BOM : "") + body;
|
|
30
|
+
}
|
|
31
|
+
export async function executeWrite(input, ctx) {
|
|
32
|
+
const abs = resolvePath(input.path, ctx.cwd);
|
|
33
|
+
const shown = displayPath(abs, ctx.cwd);
|
|
34
|
+
if (typeof input.content !== "string") {
|
|
35
|
+
return { content: "content must be a string", isError: true };
|
|
36
|
+
}
|
|
37
|
+
return withFileMutex(abs, async () => {
|
|
38
|
+
const state = await exists(abs);
|
|
39
|
+
if (state === "dir")
|
|
40
|
+
return { content: `${shown} is a directory`, isError: true };
|
|
41
|
+
let output = input.content;
|
|
42
|
+
if (state === "file") {
|
|
43
|
+
if (!ctx.readFiles.has(abs)) {
|
|
44
|
+
return {
|
|
45
|
+
content: `${shown} already exists. Read it with the read tool before overwriting it.`,
|
|
46
|
+
isError: true,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
output = conformToOriginal(await readFile(abs, "utf8"), input.content);
|
|
50
|
+
}
|
|
51
|
+
await mkdir(dirname(abs), { recursive: true });
|
|
52
|
+
await writeFile(abs, output, "utf8");
|
|
53
|
+
ctx.markRead(abs);
|
|
54
|
+
const details = {
|
|
55
|
+
path: abs,
|
|
56
|
+
bytes: Buffer.byteLength(output, "utf8"),
|
|
57
|
+
created: state === false,
|
|
58
|
+
};
|
|
59
|
+
const verb = details.created ? "Created" : "Overwrote";
|
|
60
|
+
return { content: `${verb} ${shown} (${details.bytes} bytes)`, details };
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
export function createWriteTool() {
|
|
64
|
+
return {
|
|
65
|
+
name: "write",
|
|
66
|
+
label: "Write",
|
|
67
|
+
description: "Write a file, replacing its entire content; parent directories are created. " +
|
|
68
|
+
"An existing file must be read first. Prefer the edit tool for partial changes.",
|
|
69
|
+
parameters: {
|
|
70
|
+
type: "object",
|
|
71
|
+
properties: {
|
|
72
|
+
path: { type: "string", description: "File path, absolute or relative to cwd" },
|
|
73
|
+
content: { type: "string", description: "Full new file content" },
|
|
74
|
+
},
|
|
75
|
+
required: ["path", "content"],
|
|
76
|
+
additionalProperties: false,
|
|
77
|
+
},
|
|
78
|
+
permission: "write",
|
|
79
|
+
executionMode: "sequential",
|
|
80
|
+
annotations: { destructive: true },
|
|
81
|
+
promptSnippet: "write: create or overwrite a whole file (read existing files first)",
|
|
82
|
+
execute: executeWrite,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ANSI 与列宽工具(设计 §12.1、§12.2)。[B4]
|
|
3
|
+
*
|
|
4
|
+
* - 列宽按字素簇(grapheme)计:东亚宽字符与 emoji 占 2 列,组合字符 / 零宽字符占 0 列,
|
|
5
|
+
* 控制字符占 0 列;CSI / OSC / APC / DCS 等转义序列不占列。
|
|
6
|
+
* - 宽度表自写(零依赖):东亚宽 / 全角区段 + Unicode 属性转义(`\p{Emoji_Presentation}` 等)。
|
|
7
|
+
* - 所有截断 / 切片在保留 SGR 样式的同时,于末尾补 `\x1b[0m`,保证「每行末尾重置样式」。
|
|
8
|
+
*/
|
|
9
|
+
export declare const SGR_RESET = "\u001B[0m";
|
|
10
|
+
/** 单个码点的列宽(0 / 1 / 2)。 */
|
|
11
|
+
export declare function codePointWidth(cp: number): 0 | 1 | 2;
|
|
12
|
+
/** 一个字素簇的列宽:首码点决定基础宽度;VS16(U+FE0F)把文本型 emoji 升为 2 列。 */
|
|
13
|
+
export declare function graphemeWidth(grapheme: string): number;
|
|
14
|
+
/** 从 i 起的转义序列长度;不是转义序列返回 0。未终止的序列吃到字符串末尾。 */
|
|
15
|
+
export declare function escapeLengthAt(s: string, i: number): number;
|
|
16
|
+
export declare function stripAnsi(s: string): string;
|
|
17
|
+
/** 可见列宽:跳过 ANSI 转义序列,按字素簇计宽。 */
|
|
18
|
+
export declare function visibleWidth(s: string): number;
|
|
19
|
+
/** 截断到不超过 width 列;超出时以 ellipsis 结尾;含样式时末尾补重置。 */
|
|
20
|
+
export declare function truncateToWidth(s: string, width: number, ellipsis?: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* 取 [start, end) 列区间。开头补上 start 处生效的 SGR,末尾补重置;
|
|
23
|
+
* 被边界切开的宽字符用空格代替,保证结果恰好占 end - start 列(不足时按原内容)。
|
|
24
|
+
*/
|
|
25
|
+
export declare function sliceByColumn(s: string, start: number, end: number): string;
|
|
26
|
+
/** 右侧补空格到 width 列(已超出则原样返回)。 */
|
|
27
|
+
export declare function padToWidth(s: string, width: number): string;
|
|
28
|
+
/** 自动换行:按空白断词,超长词按字素断开;样式跨行延续(行尾重置、下一行开头恢复)。 */
|
|
29
|
+
export declare function wrapTextWithAnsi(text: string, width: number): string[];
|