@armadra/agent 0.3.0 → 0.5.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 +257 -0
- package/README.md +263 -95
- package/THIRD_PARTY_NOTICES.md +34 -0
- package/dist/acp.d.ts +18 -0
- package/dist/acp.js +19 -0
- package/dist/agent/limits.d.ts +38 -0
- package/dist/agent/limits.js +127 -0
- package/dist/agent/loop-guard.d.ts +38 -0
- package/dist/agent/loop-guard.js +95 -0
- package/dist/agent/loop.d.ts +4 -0
- package/dist/agent/loop.js +7 -0
- package/dist/agent/prompt-rules.d.ts +12 -0
- package/dist/agent/prompt-rules.js +18 -0
- package/dist/agent/reminders.d.ts +59 -0
- package/dist/agent/reminders.js +327 -0
- package/dist/agent/retry.d.ts +1 -1
- package/dist/agent/retry.js +2 -1
- package/dist/agent/session-cache.d.ts +5 -0
- package/dist/agent/session-cache.js +18 -2
- package/dist/agent/session-classifier.d.ts +18 -0
- package/dist/agent/session-classifier.js +105 -0
- package/dist/agent/session-compaction.d.ts +35 -6
- package/dist/agent/session-compaction.js +157 -31
- package/dist/agent/session-core.d.ts +40 -1
- package/dist/agent/session-extensions.d.ts +61 -0
- package/dist/agent/session-extensions.js +129 -0
- package/dist/agent/session-images.d.ts +23 -0
- package/dist/agent/session-images.js +97 -0
- package/dist/agent/session-plan.d.ts +21 -0
- package/dist/agent/session-plan.js +524 -0
- package/dist/agent/session-rewind.d.ts +81 -0
- package/dist/agent/session-rewind.js +354 -0
- package/dist/agent/session-run.d.ts +28 -1
- package/dist/agent/session-run.js +157 -6
- package/dist/agent/session-settings.d.ts +49 -0
- package/dist/agent/session-settings.js +95 -0
- package/dist/agent/session-subagent.d.ts +36 -24
- package/dist/agent/session-subagent.js +259 -129
- package/dist/agent/session-sync.d.ts +10 -3
- package/dist/agent/session-sync.js +37 -8
- package/dist/agent/session-telemetry.d.ts +39 -0
- package/dist/agent/session-telemetry.js +231 -0
- package/dist/agent/session-tools.js +63 -3
- package/dist/agent/session.d.ts +33 -29
- package/dist/agent/session.js +91 -126
- package/dist/agent/subagent-registry.d.ts +91 -0
- package/dist/agent/subagent-registry.js +494 -0
- package/dist/agent/system-prompt.d.ts +6 -3
- package/dist/agent/system-prompt.js +7 -2
- package/dist/agent/tool-runner.d.ts +8 -1
- package/dist/agent/tool-runner.js +72 -6
- package/dist/agent/types-w5.d.ts +192 -0
- package/dist/agent/types-w5.js +10 -0
- package/dist/agent/types.d.ts +55 -4
- package/dist/agent/types.js +2 -0
- package/dist/agent/worktree.d.ts +36 -0
- package/dist/agent/worktree.js +86 -0
- package/dist/agents/builtin.d.ts +14 -0
- package/dist/agents/builtin.js +39 -0
- package/dist/agents/catalog.d.ts +43 -0
- package/dist/agents/catalog.js +82 -0
- package/dist/agents/discover.d.ts +35 -0
- package/dist/agents/discover.js +83 -0
- package/dist/agents/external.d.ts +82 -0
- package/dist/agents/external.js +326 -0
- package/dist/agents/parse.d.ts +18 -0
- package/dist/agents/parse.js +116 -0
- package/dist/agents/result.d.ts +32 -0
- package/dist/agents/result.js +67 -0
- package/dist/agents/task-control.d.ts +17 -0
- package/dist/agents/task-control.js +17 -0
- package/dist/agents/task-record.d.ts +78 -0
- package/dist/agents/task-record.js +141 -0
- package/dist/agents/types.d.ts +46 -0
- package/dist/agents/types.js +8 -0
- package/dist/ai/apis/anthropic-compat.d.ts +36 -0
- package/dist/ai/apis/anthropic-compat.js +92 -0
- package/dist/ai/apis/anthropic-messages.js +3 -2
- package/dist/ai/apis/anthropic-request.d.ts +7 -6
- package/dist/ai/apis/anthropic-request.js +10 -28
- package/dist/ai/apis/cache-params.d.ts +11 -2
- package/dist/ai/apis/cache-params.js +25 -6
- package/dist/ai/apis/google-generative-ai.js +3 -2
- package/dist/ai/apis/openai-compat.d.ts +4 -1
- package/dist/ai/apis/openai-compat.js +8 -1
- package/dist/ai/apis/openai-completions.js +3 -2
- package/dist/ai/apis/openai-responses.js +3 -2
- package/dist/ai/http.d.ts +28 -6
- package/dist/ai/http.js +41 -8
- package/dist/ai/image-limits.d.ts +45 -0
- package/dist/ai/image-limits.js +68 -0
- package/dist/ai/providers/builtin.d.ts +18 -6
- package/dist/ai/providers/builtin.js +135 -6
- package/dist/ai/providers/catalog-data.js +17 -13
- package/dist/ai/providers/catalog.d.ts +60 -7
- package/dist/ai/providers/catalog.js +197 -38
- package/dist/ai/providers/channels.d.ts +33 -3
- package/dist/ai/providers/channels.js +108 -7
- package/dist/ai/providers/enrich.d.ts +6 -3
- package/dist/ai/providers/enrich.js +19 -4
- package/dist/ai/providers/models-dev-cache.d.ts +28 -24
- package/dist/ai/providers/models-dev-cache.js +92 -78
- package/dist/ai/providers/models-dev-data.d.ts +7 -0
- package/dist/ai/providers/models-dev-data.js +32 -0
- package/dist/ai/providers/models-dev-snapshot.d.ts +50 -0
- package/dist/ai/providers/models-dev-snapshot.js +137 -0
- package/dist/ai/providers/models-dev.d.ts +51 -10
- package/dist/ai/providers/models-dev.js +132 -39
- package/dist/ai/providers/registry.d.ts +28 -11
- package/dist/ai/providers/registry.js +112 -65
- package/dist/ai/providers/suggest.d.ts +18 -0
- package/dist/ai/providers/suggest.js +72 -0
- package/dist/ai/sse.d.ts +6 -2
- package/dist/ai/sse.js +22 -2
- package/dist/ai/types.d.ts +37 -4
- package/dist/ai/types.js +5 -0
- package/dist/bundle/ama.cjs +41979 -21147
- package/dist/checkpoints/backend.d.ts +61 -0
- package/dist/checkpoints/backend.js +135 -0
- package/dist/checkpoints/blobs.d.ts +60 -0
- package/dist/checkpoints/blobs.js +203 -0
- package/dist/checkpoints/gc.d.ts +47 -0
- package/dist/checkpoints/gc.js +144 -0
- package/dist/checkpoints/git-head.d.ts +15 -0
- package/dist/checkpoints/git-head.js +89 -0
- package/dist/checkpoints/index.d.ts +17 -0
- package/dist/checkpoints/index.js +17 -0
- package/dist/checkpoints/replay.d.ts +48 -0
- package/dist/checkpoints/replay.js +154 -0
- package/dist/checkpoints/restore.d.ts +86 -0
- package/dist/checkpoints/restore.js +263 -0
- package/dist/checkpoints/settings.d.ts +14 -0
- package/dist/checkpoints/settings.js +23 -0
- package/dist/checkpoints/shadow-git.d.ts +108 -0
- package/dist/checkpoints/shadow-git.js +491 -0
- package/dist/checkpoints/shadow-restore.d.ts +25 -0
- package/dist/checkpoints/shadow-restore.js +128 -0
- package/dist/checkpoints/tracker.d.ts +64 -0
- package/dist/checkpoints/tracker.js +192 -0
- package/dist/checkpoints/types.d.ts +99 -0
- package/dist/checkpoints/types.js +12 -0
- package/dist/cli/args.d.ts +26 -7
- package/dist/cli/args.js +75 -82
- package/dist/cli/bootstrap.js +40 -4
- package/dist/cli/codemode-notice.d.ts +21 -0
- package/dist/cli/codemode-notice.js +56 -0
- package/dist/cli/compose-agents.d.ts +27 -0
- package/dist/cli/compose-agents.js +111 -0
- package/dist/cli/compose-extensions.d.ts +29 -0
- package/dist/cli/compose-extensions.js +44 -0
- package/dist/cli/compose-session.d.ts +8 -0
- package/dist/cli/compose-session.js +76 -2
- package/dist/cli/compose-store.d.ts +1 -1
- package/dist/cli/compose-store.js +3 -1
- package/dist/cli/compose.d.ts +17 -4
- package/dist/cli/compose.js +60 -14
- package/dist/cli/default-model.d.ts +38 -1
- package/dist/cli/default-model.js +96 -9
- package/dist/cli/deps.d.ts +42 -0
- package/dist/cli/exit-codes.d.ts +12 -0
- package/dist/cli/exit-codes.js +15 -0
- package/dist/cli/fake-visibility.d.ts +13 -0
- package/dist/cli/fake-visibility.js +26 -0
- package/dist/cli/from-prompt.d.ts +18 -0
- package/dist/cli/from-prompt.js +49 -0
- package/dist/cli/help-text.d.ts +4 -0
- package/dist/cli/help-text.js +102 -0
- package/dist/cli/main.d.ts +9 -2
- package/dist/cli/main.js +77 -3
- package/dist/cli/proxy.d.ts +51 -0
- package/dist/cli/proxy.js +135 -0
- package/dist/cli/startup-screen.d.ts +29 -0
- package/dist/cli/startup-screen.js +51 -2
- package/dist/cli/startup-steps.d.ts +1 -1
- package/dist/cli/startup-steps.js +21 -13
- package/dist/cli/subcommands/config.d.ts +22 -3
- package/dist/cli/subcommands/config.js +119 -21
- package/dist/cli/subcommands/context.js +4 -1
- package/dist/cli/subcommands/doctor.js +33 -1
- package/dist/cli/subcommands/init.js +2 -1
- package/dist/cli/subcommands/models-discover.d.ts +15 -9
- package/dist/cli/subcommands/models-discover.js +58 -52
- package/dist/cli/subcommands/models.d.ts +2 -0
- package/dist/cli/subcommands/models.js +33 -14
- package/dist/cli/subcommands/probe-runner.d.ts +96 -0
- package/dist/cli/subcommands/probe-runner.js +264 -0
- package/dist/cli/subcommands/providers-probe.d.ts +34 -0
- package/dist/cli/subcommands/providers-probe.js +87 -0
- package/dist/cli/subcommands/providers.d.ts +3 -2
- package/dist/cli/subcommands/providers.js +53 -51
- package/dist/cli/subcommands/sessions-export.d.ts +10 -0
- package/dist/cli/subcommands/sessions-export.js +59 -0
- package/dist/cli/subcommands/sessions-search.d.ts +13 -0
- package/dist/cli/subcommands/sessions-search.js +103 -0
- package/dist/cli/subcommands/sessions.d.ts +6 -3
- package/dist/cli/subcommands/sessions.js +58 -3
- package/dist/cli/subcommands/stats.d.ts +17 -0
- package/dist/cli/subcommands/stats.js +198 -0
- package/dist/cli/system-prompt-arg.d.ts +11 -0
- package/dist/cli/system-prompt-arg.js +34 -0
- package/dist/codemode/capability.d.ts +24 -7
- package/dist/codemode/capability.js +38 -10
- package/dist/codemode/host-side.d.ts +14 -0
- package/dist/codemode/host-side.js +24 -1
- package/dist/codemode/modes.d.ts +4 -11
- package/dist/codemode/modes.js +5 -27
- package/dist/codemode/tool.d.ts +18 -14
- package/dist/codemode/tool.js +76 -31
- package/dist/compaction/breaker.d.ts +25 -9
- package/dist/compaction/breaker.js +47 -17
- package/dist/compaction/estimate.d.ts +11 -3
- package/dist/compaction/estimate.js +64 -22
- package/dist/compaction/image-budget.d.ts +45 -0
- package/dist/compaction/image-budget.js +115 -0
- package/dist/compaction/post-compact.d.ts +38 -0
- package/dist/compaction/post-compact.js +152 -0
- package/dist/compaction/protect.d.ts +27 -0
- package/dist/compaction/protect.js +56 -0
- package/dist/compaction/prune-tier.d.ts +58 -12
- package/dist/compaction/prune-tier.js +120 -54
- package/dist/compaction/summarize-tier.d.ts +23 -4
- package/dist/compaction/summarize-tier.js +100 -29
- package/dist/config/checker.d.ts +28 -0
- package/dist/config/checker.js +97 -0
- package/dist/config/init.d.ts +6 -2
- package/dist/config/init.js +12 -5
- package/dist/config/json-schema.d.ts +1 -0
- package/dist/config/json-schema.js +98 -4
- package/dist/config/key-docs.d.ts +22 -0
- package/dist/config/key-docs.js +174 -0
- package/dist/config/merge.d.ts +12 -9
- package/dist/config/merge.js +102 -13
- package/dist/config/profile.d.ts +2 -0
- package/dist/config/profile.js +2 -0
- package/dist/config/schema-w5.d.ts +18 -0
- package/dist/config/schema-w5.js +109 -0
- package/dist/config/schema.d.ts +5 -9
- package/dist/config/schema.js +91 -118
- package/dist/config/types-w5.d.ts +118 -0
- package/dist/config/types-w5.js +20 -0
- package/dist/config/types.d.ts +84 -7
- package/dist/config/types.js +29 -1
- package/dist/drivers/acp/client.d.ts +57 -0
- package/dist/drivers/acp/client.js +160 -0
- package/dist/drivers/acp/driver.d.ts +31 -0
- package/dist/drivers/acp/driver.js +274 -0
- package/dist/drivers/acp/testing/fake-agent-main.d.ts +5 -0
- package/dist/drivers/acp/testing/fake-agent-main.js +7 -0
- package/dist/drivers/acp/testing/fake-agent.d.ts +27 -0
- package/dist/drivers/acp/testing/fake-agent.js +211 -0
- package/dist/drivers/acp/types.d.ts +285 -0
- package/dist/drivers/acp/types.js +36 -0
- package/dist/drivers/agents.d.ts +58 -0
- package/dist/drivers/agents.js +142 -0
- package/dist/drivers/base.d.ts +35 -0
- package/dist/drivers/base.js +59 -0
- package/dist/drivers/catalog.d.ts +46 -0
- package/dist/drivers/catalog.js +167 -0
- package/dist/drivers/env.d.ts +20 -0
- package/dist/drivers/env.js +46 -0
- package/dist/drivers/host-runners.d.ts +18 -0
- package/dist/drivers/host-runners.js +45 -0
- package/dist/drivers/jsonrpc.d.ts +58 -0
- package/dist/drivers/jsonrpc.js +180 -0
- package/dist/drivers/native/claude-normalize.d.ts +46 -0
- package/dist/drivers/native/claude-normalize.js +106 -0
- package/dist/drivers/native/claude-stream.d.ts +29 -0
- package/dist/drivers/native/claude-stream.js +456 -0
- package/dist/drivers/native/codex-app-server.d.ts +26 -0
- package/dist/drivers/native/codex-app-server.js +407 -0
- package/dist/drivers/native/codex-normalize.d.ts +38 -0
- package/dist/drivers/native/codex-normalize.js +124 -0
- package/dist/drivers/native/oneshot.d.ts +28 -0
- package/dist/drivers/native/oneshot.js +312 -0
- package/dist/drivers/permissions.d.ts +40 -0
- package/dist/drivers/permissions.js +92 -0
- package/dist/drivers/pids.d.ts +32 -0
- package/dist/drivers/pids.js +93 -0
- package/dist/drivers/pool.d.ts +28 -0
- package/dist/drivers/pool.js +96 -0
- package/dist/drivers/probe.d.ts +47 -0
- package/dist/drivers/probe.js +172 -0
- package/dist/drivers/process.d.ts +48 -0
- package/dist/drivers/process.js +100 -0
- package/dist/drivers/runner.d.ts +61 -0
- package/dist/drivers/runner.js +388 -0
- package/dist/drivers/store.d.ts +49 -0
- package/dist/drivers/store.js +91 -0
- package/dist/drivers/turn.d.ts +41 -0
- package/dist/drivers/turn.js +99 -0
- package/dist/drivers/types.d.ts +122 -0
- package/dist/drivers/types.js +11 -0
- package/dist/git/info.d.ts +75 -0
- package/dist/git/info.js +191 -0
- package/dist/hooks/protocol.js +6 -1
- package/dist/hooks/types.d.ts +10 -1
- package/dist/hooks/types.js +2 -0
- package/dist/host/api-impl.d.ts +7 -0
- package/dist/host/api-impl.js +15 -0
- package/dist/host/types.d.ts +24 -2
- package/dist/host/types.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/modes/acp/acp-events.d.ts +46 -0
- package/dist/modes/acp/acp-events.js +233 -0
- package/dist/modes/acp/acp-mode.d.ts +17 -0
- package/dist/modes/acp/acp-mode.js +47 -0
- package/dist/modes/acp/acp-server.d.ts +79 -0
- package/dist/modes/acp/acp-server.js +315 -0
- package/dist/modes/commands-core.d.ts +7 -1
- package/dist/modes/commands-core.js +61 -5
- package/dist/modes/image-input.d.ts +12 -4
- package/dist/modes/image-input.js +22 -5
- package/dist/modes/interactive/agent-panels.d.ts +14 -0
- package/dist/modes/interactive/agent-panels.js +103 -0
- package/dist/modes/interactive/agent-ui.d.ts +73 -0
- package/dist/modes/interactive/agent-ui.js +234 -0
- package/dist/modes/interactive/approval-dialog.d.ts +40 -7
- package/dist/modes/interactive/approval-dialog.js +192 -35
- package/dist/modes/interactive/approval-merge.d.ts +41 -0
- package/dist/modes/interactive/approval-merge.js +89 -0
- package/dist/modes/interactive/clipboard-paste.d.ts +18 -0
- package/dist/modes/interactive/clipboard-paste.js +28 -0
- package/dist/modes/interactive/commands.d.ts +20 -3
- package/dist/modes/interactive/commands.js +75 -16
- package/dist/modes/interactive/double-esc.d.ts +20 -0
- package/dist/modes/interactive/double-esc.js +34 -0
- package/dist/modes/interactive/event-notices.d.ts +32 -0
- package/dist/modes/interactive/event-notices.js +79 -0
- package/dist/modes/interactive/external-editor.d.ts +16 -0
- package/dist/modes/interactive/external-editor.js +39 -0
- package/dist/modes/interactive/interactive-mode.d.ts +16 -2
- package/dist/modes/interactive/interactive-mode.js +184 -202
- package/dist/modes/interactive/key-dispatch.d.ts +18 -2
- package/dist/modes/interactive/key-dispatch.js +53 -6
- package/dist/modes/interactive/line/line-mode.d.ts +3 -1
- package/dist/modes/interactive/line/line-mode.js +28 -9
- package/dist/modes/interactive/line/line-render.d.ts +6 -1
- package/dist/modes/interactive/line/line-render.js +66 -8
- package/dist/modes/interactive/message-view.d.ts +49 -10
- package/dist/modes/interactive/message-view.js +262 -46
- package/dist/modes/interactive/panels.d.ts +35 -0
- package/dist/modes/interactive/panels.js +157 -0
- package/dist/modes/interactive/pickers.d.ts +23 -2
- package/dist/modes/interactive/pickers.js +48 -15
- package/dist/modes/interactive/plan-command.d.ts +40 -0
- package/dist/modes/interactive/plan-command.js +129 -0
- package/dist/modes/interactive/plan-dialog.d.ts +98 -0
- package/dist/modes/interactive/plan-dialog.js +290 -0
- package/dist/modes/interactive/plan-flow.d.ts +34 -0
- package/dist/modes/interactive/plan-flow.js +98 -0
- package/dist/modes/interactive/rewind-command.d.ts +31 -0
- package/dist/modes/interactive/rewind-command.js +106 -0
- package/dist/modes/interactive/rewind-flow.d.ts +37 -0
- package/dist/modes/interactive/rewind-flow.js +151 -0
- package/dist/modes/interactive/rewind-list.d.ts +36 -0
- package/dist/modes/interactive/rewind-list.js +123 -0
- package/dist/modes/interactive/rewind-panel.d.ts +77 -0
- package/dist/modes/interactive/rewind-panel.js +309 -0
- package/dist/modes/interactive/rewind-text.d.ts +40 -0
- package/dist/modes/interactive/rewind-text.js +139 -0
- package/dist/modes/interactive/run-indicator.d.ts +51 -0
- package/dist/modes/interactive/run-indicator.js +189 -0
- package/dist/modes/interactive/session-events.d.ts +24 -0
- package/dist/modes/interactive/session-events.js +96 -0
- package/dist/modes/interactive/startup-header.d.ts +40 -0
- package/dist/modes/interactive/startup-header.js +169 -0
- package/dist/modes/interactive/status-area.d.ts +73 -0
- package/dist/modes/interactive/status-area.js +190 -0
- package/dist/modes/interactive/status-bar.d.ts +107 -16
- package/dist/modes/interactive/status-bar.js +306 -81
- package/dist/modes/interactive/status-line.d.ts +33 -0
- package/dist/modes/interactive/status-line.js +113 -0
- package/dist/modes/interactive/subagent-view.d.ts +56 -0
- package/dist/modes/interactive/subagent-view.js +154 -0
- package/dist/modes/interactive/tasks-report.d.ts +27 -0
- package/dist/modes/interactive/tasks-report.js +114 -0
- package/dist/modes/interactive/tool-summary.d.ts +46 -0
- package/dist/modes/interactive/tool-summary.js +218 -0
- package/dist/modes/interactive/tool-view.d.ts +58 -15
- package/dist/modes/interactive/tool-view.js +233 -145
- package/dist/modes/print/print-mode.d.ts +40 -4
- package/dist/modes/print/print-mode.js +161 -8
- package/dist/modes/rpc/commands.d.ts +9 -0
- package/dist/modes/rpc/commands.js +49 -0
- package/dist/modes/rpc/rpc-mode.js +7 -1
- package/dist/modes/session-report.d.ts +11 -0
- package/dist/modes/session-report.js +48 -1
- package/dist/permissions/auto-safe.d.ts +65 -0
- package/dist/permissions/auto-safe.js +547 -0
- package/dist/permissions/classifier.d.ts +68 -0
- package/dist/permissions/classifier.js +186 -0
- package/dist/permissions/dangerous.d.ts +5 -0
- package/dist/permissions/dangerous.js +1 -1
- package/dist/permissions/modes.d.ts +30 -0
- package/dist/permissions/modes.js +78 -0
- package/dist/permissions/pipeline.d.ts +72 -6
- package/dist/permissions/pipeline.js +306 -11
- package/dist/permissions/protected.d.ts +19 -0
- package/dist/permissions/protected.js +74 -0
- package/dist/permissions/readonly-bash.d.ts +30 -0
- package/dist/permissions/readonly-bash.js +118 -0
- package/dist/permissions/rules.js +3 -0
- package/dist/permissions/types.d.ts +92 -3
- package/dist/permissions/types.js +3 -0
- package/dist/plan/compose.d.ts +9 -0
- package/dist/plan/compose.js +19 -0
- package/dist/plan/controller.d.ts +71 -0
- package/dist/plan/controller.js +27 -0
- package/dist/plan/done-markers.d.ts +17 -0
- package/dist/plan/done-markers.js +51 -0
- package/dist/plan/extract.d.ts +29 -0
- package/dist/plan/extract.js +160 -0
- package/dist/plan/prompts.d.ts +36 -0
- package/dist/plan/prompts.js +89 -0
- package/dist/plan/store.d.ts +37 -0
- package/dist/plan/store.js +90 -0
- package/dist/rpc.d.ts +58 -2
- package/dist/rpc.js +7 -0
- package/dist/sandbox/bash.d.ts +74 -0
- package/dist/sandbox/bash.js +126 -0
- package/dist/sandbox/detect.d.ts +48 -0
- package/dist/sandbox/detect.js +149 -0
- package/dist/sandbox/index.d.ts +5 -0
- package/dist/sandbox/index.js +5 -0
- package/dist/sandbox/profile.d.ts +33 -0
- package/dist/sandbox/profile.js +76 -0
- package/dist/sandbox/wrap.d.ts +29 -0
- package/dist/sandbox/wrap.js +117 -0
- package/dist/sdk.d.ts +42 -5
- package/dist/sdk.js +34 -4
- package/dist/session/export.d.ts +32 -0
- package/dist/session/export.js +187 -0
- package/dist/session/redact.d.ts +15 -0
- package/dist/session/redact.js +55 -0
- package/dist/session/reuse.d.ts +33 -0
- package/dist/session/reuse.js +86 -0
- package/dist/session/scan.d.ts +34 -0
- package/dist/session/scan.js +140 -0
- package/dist/session/search.d.ts +52 -0
- package/dist/session/search.js +211 -0
- package/dist/session/stats-aggregate.d.ts +63 -0
- package/dist/session/stats-aggregate.js +163 -0
- package/dist/session/stats-index.d.ts +26 -0
- package/dist/session/stats-index.js +91 -0
- package/dist/session/stats-scan.d.ts +54 -0
- package/dist/session/stats-scan.js +236 -0
- package/dist/session/types.d.ts +3 -1
- package/dist/session/types.js +1 -0
- package/dist/skills/builtin.d.ts +31 -0
- package/dist/skills/builtin.js +109 -0
- package/dist/skills/discover.d.ts +2 -1
- package/dist/skills/index-prompt.js +5 -8
- package/dist/tools/background-jobs.d.ts +71 -0
- package/dist/tools/background-jobs.js +198 -0
- package/dist/tools/bash.d.ts +53 -1
- package/dist/tools/bash.js +158 -7
- package/dist/tools/clipboard-image.d.ts +44 -0
- package/dist/tools/clipboard-image.js +148 -0
- package/dist/tools/edit.d.ts +2 -0
- package/dist/tools/edit.js +18 -3
- package/dist/tools/image-file.d.ts +38 -3
- package/dist/tools/image-file.js +81 -10
- package/dist/tools/image-resize.d.ts +58 -0
- package/dist/tools/image-resize.js +97 -0
- package/dist/tools/presets.d.ts +40 -8
- package/dist/tools/presets.js +65 -19
- package/dist/tools/read.d.ts +5 -2
- package/dist/tools/read.js +13 -10
- package/dist/tools/registry.d.ts +7 -1
- package/dist/tools/registry.js +27 -4
- package/dist/tools/task-ctl.d.ts +22 -0
- package/dist/tools/task-ctl.js +116 -0
- package/dist/tools/task.d.ts +22 -10
- package/dist/tools/task.js +66 -26
- package/dist/tools/todo.d.ts +22 -2
- package/dist/tools/todo.js +58 -14
- package/dist/tools/truncate.d.ts +15 -0
- package/dist/tools/truncate.js +25 -0
- package/dist/tools/types.d.ts +113 -0
- package/dist/tools/types.js +5 -0
- package/dist/tools/write.js +3 -0
- package/dist/tui/component.d.ts +8 -2
- package/dist/tui/component.js +3 -1
- package/dist/tui/components/box.d.ts +6 -1
- package/dist/tui/components/box.js +16 -6
- package/dist/tui/components/card.d.ts +23 -0
- package/dist/tui/components/card.js +37 -0
- package/dist/tui/components/editor-history.d.ts +6 -0
- package/dist/tui/components/editor-history.js +45 -0
- package/dist/tui/components/editor-paste.d.ts +1 -1
- package/dist/tui/components/editor-paste.js +4 -4
- package/dist/tui/components/editor.d.ts +11 -5
- package/dist/tui/components/editor.js +52 -58
- package/dist/tui/components/key-value.d.ts +3 -0
- package/dist/tui/components/key-value.js +16 -6
- package/dist/tui/components/loader.d.ts +37 -7
- package/dist/tui/components/loader.js +84 -21
- package/dist/tui/components/markdown.d.ts +5 -1
- package/dist/tui/components/markdown.js +45 -21
- package/dist/tui/components/meter.d.ts +3 -3
- package/dist/tui/components/meter.js +13 -11
- package/dist/tui/components/select-list.d.ts +23 -1
- package/dist/tui/components/select-list.js +76 -13
- package/dist/tui/glyphs.d.ts +72 -0
- package/dist/tui/glyphs.js +118 -0
- package/dist/tui/keybindings.d.ts +5 -0
- package/dist/tui/keybindings.js +5 -0
- package/dist/tui/theme.d.ts +30 -6
- package/dist/tui/theme.js +103 -17
- package/dist/tui.d.ts +4 -2
- package/dist/tui.js +3 -1
- package/docs/acp.md +78 -0
- package/docs/agents.md +250 -0
- package/docs/codemode.md +28 -10
- package/docs/hooks.md +36 -32
- package/docs/permissions.md +212 -0
- package/docs/plan.md +107 -0
- package/docs/providers.md +261 -62
- package/docs/rewind-plan.md +181 -0
- package/docs/rpc.md +127 -40
- package/docs/sandbox.md +215 -0
- package/docs/session-format.md +43 -22
- package/docs/sessions.md +174 -0
- package/docs/tui-design.md +751 -0
- package/docs/tui.md +260 -64
- package/package.json +15 -2
|
@@ -12,7 +12,13 @@
|
|
|
12
12
|
* (完整门禁);名字是 `codemode` 直接拒绝。脚本结束时 abort 仍在跑的调用。
|
|
13
13
|
* - 超时:父进程计时,到点杀子进程树(POSIX 进程组 / Windows taskkill);外层 abort:先发
|
|
14
14
|
* `abort`,宽限后杀树。
|
|
15
|
+
* - OS 沙箱(docs/sandbox.md):有可用的 sandbox-exec / bwrap / unshare 时,整条命令行经它启动,拒绝
|
|
16
|
+
* 网络与一切写入(`CODEMODE_OS_POLICY`;子进程本来就没有写权限)。Node 22 / 24 的 strict 依赖这一层
|
|
17
|
+
* (`requireOsSandbox`),包装不了就报错,不裸跑;Node ≥ 25 是叠加的纵深防御。
|
|
15
18
|
*/
|
|
19
|
+
import { type OsSandboxStatus } from "../sandbox/detect.js";
|
|
20
|
+
import type { OsSandboxPolicy } from "../sandbox/profile.js";
|
|
21
|
+
import { type WrappedCommand } from "../sandbox/wrap.js";
|
|
16
22
|
import { type SandboxCapability } from "./capability.js";
|
|
17
23
|
import { type ScriptOptions, type StoreSnapshot, type ToolDecl } from "./protocol.js";
|
|
18
24
|
/** 父进程累积的输出上限(字符);超出后丢弃中段(给模型的再按 max_output_tokens 截)。 */
|
|
@@ -23,6 +29,10 @@ export declare const ABORT_GRACE_MS = 500;
|
|
|
23
29
|
export declare function resolveSandboxEntry(moduleUrl?: string): string | undefined;
|
|
24
30
|
/** 子进程参数(不含可执行文件)。 */
|
|
25
31
|
export declare function sandboxArgs(entry: string, capability?: Pick<SandboxCapability, "permissionFlag">): string[];
|
|
32
|
+
/** codemode 子进程的 OS 沙箱策略:拒绝网络,任何位置都不可写。 */
|
|
33
|
+
export declare const CODEMODE_OS_POLICY: OsSandboxPolicy;
|
|
34
|
+
/** 完整命令行:`<node> <sandboxArgs>`,有 OS 沙箱时再经它包装。 */
|
|
35
|
+
export declare function sandboxCommand(nodePath: string, args: readonly string[], os: Pick<OsSandboxStatus, "kind" | "path">): WrappedCommand;
|
|
26
36
|
/** 子进程环境:空;Electron 下以 Node 方式运行同一可执行文件;Windows 保留 SystemRoot。 */
|
|
27
37
|
export declare function sandboxEnv(versions?: NodeJS.ProcessVersions, platform?: NodeJS.Platform, parentEnv?: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
28
38
|
export interface SandboxRunRequest {
|
|
@@ -39,6 +49,10 @@ export interface SandboxRunRequest {
|
|
|
39
49
|
/** 缺省 process.execPath。 */
|
|
40
50
|
nodePath?: string;
|
|
41
51
|
capability?: Pick<SandboxCapability, "permissionFlag">;
|
|
52
|
+
/** 缺省 osSandboxStatus()(进程内缓存的探测结果)。 */
|
|
53
|
+
os?: Pick<OsSandboxStatus, "kind" | "path">;
|
|
54
|
+
/** true:必须经 OS 沙箱启动(Node 22 / 24 的 strict 依赖它),包装不了直接失败。 */
|
|
55
|
+
requireOsSandbox?: boolean;
|
|
42
56
|
}
|
|
43
57
|
export interface SandboxRunResult {
|
|
44
58
|
ok: boolean;
|
|
@@ -12,11 +12,16 @@
|
|
|
12
12
|
* (完整门禁);名字是 `codemode` 直接拒绝。脚本结束时 abort 仍在跑的调用。
|
|
13
13
|
* - 超时:父进程计时,到点杀子进程树(POSIX 进程组 / Windows taskkill);外层 abort:先发
|
|
14
14
|
* `abort`,宽限后杀树。
|
|
15
|
+
* - OS 沙箱(docs/sandbox.md):有可用的 sandbox-exec / bwrap / unshare 时,整条命令行经它启动,拒绝
|
|
16
|
+
* 网络与一切写入(`CODEMODE_OS_POLICY`;子进程本来就没有写权限)。Node 22 / 24 的 strict 依赖这一层
|
|
17
|
+
* (`requireOsSandbox`),包装不了就报错,不裸跑;Node ≥ 25 是叠加的纵深防御。
|
|
15
18
|
*/
|
|
16
19
|
import { spawn } from "node:child_process";
|
|
17
20
|
import { existsSync, realpathSync } from "node:fs";
|
|
18
21
|
import { dirname, join } from "node:path";
|
|
19
22
|
import { fileURLToPath } from "node:url";
|
|
23
|
+
import { osSandboxStatus } from "../sandbox/detect.js";
|
|
24
|
+
import { wrapCommand } from "../sandbox/wrap.js";
|
|
20
25
|
import { killProcessTree, trackProcessGroup, untrackProcessGroup } from "../tools/process-tree.js";
|
|
21
26
|
import { detectSandboxCapability } from "./capability.js";
|
|
22
27
|
import { LineSplitter, SANDBOX_MAIN_FLAG, decodeChildLine, encodeLine, } from "./protocol.js";
|
|
@@ -49,6 +54,15 @@ export function sandboxArgs(entry, capability = detectSandboxCapability()) {
|
|
|
49
54
|
SANDBOX_MAIN_FLAG,
|
|
50
55
|
];
|
|
51
56
|
}
|
|
57
|
+
/** codemode 子进程的 OS 沙箱策略:拒绝网络,任何位置都不可写。 */
|
|
58
|
+
export const CODEMODE_OS_POLICY = Object.freeze({
|
|
59
|
+
network: "deny",
|
|
60
|
+
writable: Object.freeze([]),
|
|
61
|
+
});
|
|
62
|
+
/** 完整命令行:`<node> <sandboxArgs>`,有 OS 沙箱时再经它包装。 */
|
|
63
|
+
export function sandboxCommand(nodePath, args, os) {
|
|
64
|
+
return wrapCommand(os, nodePath, args, CODEMODE_OS_POLICY);
|
|
65
|
+
}
|
|
52
66
|
/** 子进程环境:空;Electron 下以 Node 方式运行同一可执行文件;Windows 保留 SystemRoot。 */
|
|
53
67
|
export function sandboxEnv(versions = process.versions, platform = process.platform, parentEnv = process.env) {
|
|
54
68
|
const env = {};
|
|
@@ -82,8 +96,17 @@ export async function runSandbox(request) {
|
|
|
82
96
|
if (request.signal.aborted) {
|
|
83
97
|
return { ...base, ok: false, error: "Script aborted", aborted: true, elapsedMs: 0 };
|
|
84
98
|
}
|
|
99
|
+
const command = sandboxCommand(request.nodePath ?? process.execPath, sandboxArgs(entry, request.capability), request.os ?? osSandboxStatus());
|
|
100
|
+
if (request.requireOsSandbox === true && !command.networkDenied) {
|
|
101
|
+
return {
|
|
102
|
+
...base,
|
|
103
|
+
ok: false,
|
|
104
|
+
error: "codemode sandbox requires an OS sandbox to isolate network, but none is available",
|
|
105
|
+
elapsedMs: 0,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
85
108
|
const isWindows = process.platform === "win32";
|
|
86
|
-
const child = spawn(
|
|
109
|
+
const child = spawn(command.command, command.args, {
|
|
87
110
|
env: sandboxEnv(),
|
|
88
111
|
stdio: ["pipe", "pipe", "pipe"],
|
|
89
112
|
detached: !isWindows,
|
package/dist/codemode/modes.d.ts
CHANGED
|
@@ -4,20 +4,13 @@
|
|
|
4
4
|
* | 模式 | 模型看到的工具 |
|
|
5
5
|
* | ------ | -------------------------------------------------------------------------- |
|
|
6
6
|
* | `off` | 不注册 codemode(工厂返回 undefined) |
|
|
7
|
-
* | `on` | 预设的工具 + codemode
|
|
7
|
+
* | `on` | 预设的工具 + codemode;codemode 描述一行列出可在脚本里调用的直接工具 |
|
|
8
8
|
* | `only` | 只有 codemode(宿主 / SDK 工具也不直接暴露);其它工具的声明列在它的描述里 |
|
|
9
9
|
*
|
|
10
|
-
* 活动集由 B6 的 `resolvePreset` 决定(`only` → `["codemode"]
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* 活动集由 B6 的 `resolvePreset` 决定(`only` → `["codemode"]`);这里落 `only` 模式的独占活动集
|
|
11
|
+
* (`PresetToolRegistry.setPresetTools(names, { exclusive: true })`)。`on` 模式不再给其它工具的描述
|
|
12
|
+
* 追加提示:直接工具的描述与 off 模式逐字节相同,codemode 描述里一行列出它们(codemode/tool.ts)。
|
|
13
13
|
*/
|
|
14
|
-
import type { CodemodeMode } from "../config/types.js";
|
|
15
14
|
import { type PresetResolution, type PresetToolRegistry } from "../tools/presets.js";
|
|
16
|
-
import type { ToolDefinition } from "../tools/types.js";
|
|
17
|
-
export declare function codemodeHint(name: string): string;
|
|
18
|
-
/** 复制工具(保留 getter 与方法),描述末尾追加一行 codemode 提示。 */
|
|
19
|
-
export declare function withCodemodeHint<I>(tool: ToolDefinition<I>): ToolDefinition<I>;
|
|
20
|
-
/** 注册前的包装:`on` 模式给 codemode 以外的工具加提示,其它模式原样返回。 */
|
|
21
|
-
export declare function decorateForMode(mode: CodemodeMode): <I>(tool: ToolDefinition<I>) => ToolDefinition<I>;
|
|
22
15
|
/** 把预设结果落到注册表:`only` 时活动集独占(宿主 / SDK 工具也只能在脚本里调用)。 */
|
|
23
16
|
export declare function applyCodemodeMode(registry: PresetToolRegistry, resolution: Pick<PresetResolution, "builtin" | "codemode">): void;
|
package/dist/codemode/modes.js
CHANGED
|
@@ -4,36 +4,14 @@
|
|
|
4
4
|
* | 模式 | 模型看到的工具 |
|
|
5
5
|
* | ------ | -------------------------------------------------------------------------- |
|
|
6
6
|
* | `off` | 不注册 codemode(工厂返回 undefined) |
|
|
7
|
-
* | `on` | 预设的工具 + codemode
|
|
7
|
+
* | `on` | 预设的工具 + codemode;codemode 描述一行列出可在脚本里调用的直接工具 |
|
|
8
8
|
* | `only` | 只有 codemode(宿主 / SDK 工具也不直接暴露);其它工具的声明列在它的描述里 |
|
|
9
9
|
*
|
|
10
|
-
* 活动集由 B6 的 `resolvePreset` 决定(`only` → `["codemode"]
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* 活动集由 B6 的 `resolvePreset` 决定(`only` → `["codemode"]`);这里落 `only` 模式的独占活动集
|
|
11
|
+
* (`PresetToolRegistry.setPresetTools(names, { exclusive: true })`)。`on` 模式不再给其它工具的描述
|
|
12
|
+
* 追加提示:直接工具的描述与 off 模式逐字节相同,codemode 描述里一行列出它们(codemode/tool.ts)。
|
|
13
13
|
*/
|
|
14
|
-
import {
|
|
15
|
-
export function codemodeHint(name) {
|
|
16
|
-
return `Also callable inside codemode scripts as tools.${name}(args).`;
|
|
17
|
-
}
|
|
18
|
-
/** 复制工具(保留 getter 与方法),描述末尾追加一行 codemode 提示。 */
|
|
19
|
-
export function withCodemodeHint(tool) {
|
|
20
|
-
const copy = Object.create(Object.getPrototypeOf(tool), {
|
|
21
|
-
...Object.getOwnPropertyDescriptors(tool),
|
|
22
|
-
});
|
|
23
|
-
Object.defineProperty(copy, "description", {
|
|
24
|
-
value: `${tool.description}\n${codemodeHint(tool.name)}`,
|
|
25
|
-
enumerable: true,
|
|
26
|
-
writable: false,
|
|
27
|
-
configurable: true,
|
|
28
|
-
});
|
|
29
|
-
return copy;
|
|
30
|
-
}
|
|
31
|
-
/** 注册前的包装:`on` 模式给 codemode 以外的工具加提示,其它模式原样返回。 */
|
|
32
|
-
export function decorateForMode(mode) {
|
|
33
|
-
if (mode !== "on")
|
|
34
|
-
return (tool) => tool;
|
|
35
|
-
return (tool) => (tool.name === CODEMODE_TOOL ? tool : withCodemodeHint(tool));
|
|
36
|
-
}
|
|
14
|
+
import {} from "../tools/presets.js";
|
|
37
15
|
/** 把预设结果落到注册表:`only` 时活动集独占(宿主 / SDK 工具也只能在脚本里调用)。 */
|
|
38
16
|
export function applyCodemodeMode(registry, resolution) {
|
|
39
17
|
registry.setPresetTools(resolution.builtin, { exclusive: resolution.codemode === "only" });
|
package/dist/codemode/tool.d.ts
CHANGED
|
@@ -12,7 +12,11 @@
|
|
|
12
12
|
* - store 只在脚本成功时提交(store.ts);
|
|
13
13
|
* - 描述(含工具声明)在第一次读取时确定并冻结:同一会话内字节稳定(设计 §9.1),之后注册的宿主
|
|
14
14
|
* 工具仍可在脚本里调用(`ALL_TOOLS` / `describeTool` 是执行时的清单)。
|
|
15
|
+
* - 描述分两种(`buildCodemodeDescription` 的 `layout`):`only` 模式按预算内联全部工具声明;`on`
|
|
16
|
+
* 模式不重复已直接暴露给模型的工具(它们的 schema 已在工具表里),只用一行列出名字,「仅脚本可
|
|
17
|
+
* 调用」的工具也只列名字(`describeTool(name)` 取签名),把 on 模式的前缀增量压在约 400 token。
|
|
15
18
|
*/
|
|
19
|
+
import type { AmaConfig } from "../config/types.js";
|
|
16
20
|
import type { ToolDefinition, ToolResult } from "../tools/types.js";
|
|
17
21
|
import { type SandboxCapability } from "./capability.js";
|
|
18
22
|
import { type DeclarableTool } from "./declarations.js";
|
|
@@ -25,6 +29,8 @@ export interface CallableTool {
|
|
|
25
29
|
tool: DeclarableTool;
|
|
26
30
|
/** 内置工具(非 bash)解析为文本;宿主 / SDK 工具可能返回结构化值。 */
|
|
27
31
|
textResult: boolean;
|
|
32
|
+
/** 模型也能直接调用(在活动集里);`on` 模式的描述只列名字。 */
|
|
33
|
+
direct?: boolean;
|
|
28
34
|
}
|
|
29
35
|
export interface CodemodeToolOptions {
|
|
30
36
|
/** 脚本里可调用的工具(不含 codemode 本身);描述冻结时与每次执行时各调用一次。 */
|
|
@@ -37,6 +43,8 @@ export interface CodemodeToolOptions {
|
|
|
37
43
|
maxResultChars?: number;
|
|
38
44
|
/** `only` 模式:其它工具不直接暴露,系统提示的工具行与规则写明只能在脚本里调用。 */
|
|
39
45
|
exclusive?: boolean;
|
|
46
|
+
/** 描述的写法:缺省 `only`(按预算内联声明);`on` 只列名字(见 `buildCodemodeDescription`)。 */
|
|
47
|
+
layout?: DescriptionLayout;
|
|
40
48
|
/** 子进程入口(测试 / 嵌入方覆盖)。 */
|
|
41
49
|
entry?: string;
|
|
42
50
|
nodePath?: string;
|
|
@@ -68,7 +76,8 @@ export declare const CODEMODE_EXAMPLE: readonly string[];
|
|
|
68
76
|
* (实测模型常见的两种错误)。其它错误原样返回。
|
|
69
77
|
*/
|
|
70
78
|
export declare function scriptErrorHint(error: string, toolNames: readonly string[]): string;
|
|
71
|
-
export
|
|
79
|
+
export type DescriptionLayout = "only" | "on";
|
|
80
|
+
export declare function buildCodemodeDescription(tools: readonly CallableTool[], inlineBudget: number, capability: SandboxCapability, layout?: DescriptionLayout): string;
|
|
72
81
|
/** 结果文本:状态行 + 输出(超限保留首尾)+ 失败原因。 */
|
|
73
82
|
export declare function formatCodemodeResult(run: Pick<SandboxRunResult, "ok" | "error" | "outputs" | "elapsedMs" | "droppedChars">, limitChars: number, saveFull: (text: string) => string | undefined): {
|
|
74
83
|
text: string;
|
|
@@ -79,27 +88,22 @@ export declare const CODEMODE_ONLY_GUIDELINE = "read, edit, write, bash and the
|
|
|
79
88
|
export declare function createCodemodeTool(options: CodemodeToolOptions): ToolDefinition<CodemodeInput>;
|
|
80
89
|
/** 组装根用:从 ToolFactoryContext 的形状取所需(避免 codemode → cli 的依赖)。 */
|
|
81
90
|
export interface CodemodeFactoryContext {
|
|
82
|
-
config:
|
|
83
|
-
codemode?: {
|
|
84
|
-
mode?: "off" | "on" | "only";
|
|
85
|
-
inlineBudget?: number;
|
|
86
|
-
requireStrict?: boolean;
|
|
87
|
-
};
|
|
88
|
-
tools?: {
|
|
89
|
-
preset?: string;
|
|
90
|
-
maxToolResultChars?: number;
|
|
91
|
-
};
|
|
92
|
-
};
|
|
91
|
+
config: Pick<AmaConfig, "tools" | "codemode" | "sandbox">;
|
|
93
92
|
registry: {
|
|
94
93
|
list(): readonly string[];
|
|
95
94
|
get(name: string): ToolDefinition | undefined;
|
|
96
95
|
} & {
|
|
97
96
|
sourceOf?(name: string): string | undefined;
|
|
97
|
+
/** 活动集(coordinator 预设下脚本只能调这些)。 */
|
|
98
|
+
active?(): readonly {
|
|
99
|
+
name: string;
|
|
100
|
+
}[];
|
|
98
101
|
};
|
|
99
102
|
warn(message: string): void;
|
|
100
103
|
}
|
|
101
104
|
/**
|
|
102
|
-
* codemode
|
|
103
|
-
* → 不注册并 warning(预设随之回退到 default)。
|
|
105
|
+
* codemode 工具工厂:生效模式(显式 `codemode.mode`,否则跟随预设,见 tools/presets.ts)为 off →
|
|
106
|
+
* 不注册;`requireStrict` 而运行时不隔离网络 → 不注册并 warning(预设随之回退到 default)。
|
|
107
|
+
* `coordinator` 预设下脚本可调用的工具限于活动集(不含 codemode 本身)。
|
|
104
108
|
*/
|
|
105
109
|
export declare function codemodeToolFactory(overrides?: Partial<Pick<CodemodeToolOptions, "capability" | "entry" | "nodePath">>): (ctx: CodemodeFactoryContext) => ToolDefinition | undefined;
|
package/dist/codemode/tool.js
CHANGED
|
@@ -12,14 +12,17 @@
|
|
|
12
12
|
* - store 只在脚本成功时提交(store.ts);
|
|
13
13
|
* - 描述(含工具声明)在第一次读取时确定并冻结:同一会话内字节稳定(设计 §9.1),之后注册的宿主
|
|
14
14
|
* 工具仍可在脚本里调用(`ALL_TOOLS` / `describeTool` 是执行时的清单)。
|
|
15
|
+
* - 描述分两种(`buildCodemodeDescription` 的 `layout`):`only` 模式按预算内联全部工具声明;`on`
|
|
16
|
+
* 模式不重复已直接暴露给模型的工具(它们的 schema 已在工具表里),只用一行列出名字,「仅脚本可
|
|
17
|
+
* 调用」的工具也只列名字(`describeTool(name)` 取签名),把 on 模式的前缀增量压在约 400 token。
|
|
15
18
|
*/
|
|
16
19
|
import { mkdirSync, writeFileSync } from "node:fs";
|
|
17
20
|
import { tmpdir } from "node:os";
|
|
18
21
|
import { join } from "node:path";
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
22
|
+
import { codemodeCallableFilter, resolveCodemodeMode } from "../tools/presets.js";
|
|
23
|
+
import { codemodeAvailability, detectSandboxCapability, sandboxCapabilityFor, strictNeedsOsSandbox, } from "./capability.js";
|
|
24
|
+
import { BASH_RESULT_DECLARATION, DEFAULT_INLINE_BUDGET, buildDeclarationBlock, toolDeclaration, } from "./declarations.js";
|
|
21
25
|
import { runSandbox } from "./host-side.js";
|
|
22
|
-
import { codemodeHint } from "./modes.js";
|
|
23
26
|
import { parseOptionsLine } from "./protocol.js";
|
|
24
27
|
import { commitStore, readStore } from "./store.js";
|
|
25
28
|
export const CODEMODE_TOOL_NAME = "codemode";
|
|
@@ -54,13 +57,6 @@ export function toScriptValue(name, result) {
|
|
|
54
57
|
throw new Error(textOf(result.content) || `Tool ${name} failed`);
|
|
55
58
|
return result.structured !== undefined ? result.structured : textOf(result.content);
|
|
56
59
|
}
|
|
57
|
-
/** on 模式下其它工具描述末尾的提示不进声明。 */
|
|
58
|
-
function declarable(tool) {
|
|
59
|
-
const suffix = `\n${codemodeHint(tool.name)}`;
|
|
60
|
-
if (!tool.description.endsWith(suffix))
|
|
61
|
-
return tool;
|
|
62
|
-
return { ...tool, description: tool.description.slice(0, -suffix.length) };
|
|
63
|
-
}
|
|
64
60
|
/** 描述里的示例脚本(6 行):并发两个 read、过滤、return。 */
|
|
65
61
|
export const CODEMODE_EXAMPLE = [
|
|
66
62
|
"const [a, b] = await Promise.all([",
|
|
@@ -86,7 +82,39 @@ export function scriptErrorHint(error, toolNames) {
|
|
|
86
82
|
}
|
|
87
83
|
return error;
|
|
88
84
|
}
|
|
89
|
-
|
|
85
|
+
/**
|
|
86
|
+
* `on` 模式的描述:规则压成三行 + 示例;已直接暴露的工具只列名字(参数同直接调用),仅脚本可调用的
|
|
87
|
+
* 工具也只列名字。不内联任何声明,字节只依赖工具名与沙箱能力。
|
|
88
|
+
*/
|
|
89
|
+
function buildOnDescription(tools, capability) {
|
|
90
|
+
const names = (list) => list
|
|
91
|
+
.map(({ tool }) => tool.name)
|
|
92
|
+
.sort()
|
|
93
|
+
.join(", ");
|
|
94
|
+
const direct = tools.filter((t) => t.direct === true);
|
|
95
|
+
const scriptOnly = tools.filter((t) => t.direct !== true);
|
|
96
|
+
const lines = [
|
|
97
|
+
"Run a JavaScript script that calls tools; only its output comes back to you. Use it to batch many tool calls (Promise.all), filter or summarize large results, or loop, in one step.",
|
|
98
|
+
"Input: raw JavaScript (not JSON, no code fence), the body of an async function (top-level await and return work). Only tools.<name>(args) exists: no require, import, process, fetch or timers.",
|
|
99
|
+
"Example:",
|
|
100
|
+
...CODEMODE_EXAMPLE,
|
|
101
|
+
"tools.<name>(args) returns a Promise that rejects when the call fails or is denied (Promise.allSettled keeps the rest); text(v) / console.log(...) append output; return v is text(v); store(key, v) / load(key) keep small JSON values across scripts; describeTool(name) returns a signature. At most 8 calls run at once, each through the normal permissions.",
|
|
102
|
+
];
|
|
103
|
+
if (!capability.strict)
|
|
104
|
+
lines.push(`Sandbox: ${capability.reason}.`);
|
|
105
|
+
if (direct.length > 0) {
|
|
106
|
+
const bash = direct.some(({ tool }) => tool.name === "bash");
|
|
107
|
+
lines.push(`Your direct tools are callable here too, same arguments: ${names(direct)}${bash ? " (tools.bash resolves to BashResult; the others to text)" : ""}.`);
|
|
108
|
+
if (bash)
|
|
109
|
+
lines.push(BASH_RESULT_DECLARATION);
|
|
110
|
+
}
|
|
111
|
+
if (scriptOnly.length > 0)
|
|
112
|
+
lines.push(`Callable only from scripts: ${names(scriptOnly)}.`);
|
|
113
|
+
return lines.join("\n");
|
|
114
|
+
}
|
|
115
|
+
export function buildCodemodeDescription(tools, inlineBudget, capability, layout = "only") {
|
|
116
|
+
if (layout === "on")
|
|
117
|
+
return buildOnDescription(tools, capability);
|
|
90
118
|
const lines = [
|
|
91
119
|
"Run a JavaScript script that calls tools; only the script's output comes back to you. Use it to batch many tool calls (Promise.all), filter or summarize large results, or loop, in one step.",
|
|
92
120
|
"Only tools.<name>(args) is available. There is no require, import, process, fetch or timers; do not call tools directly as functions.",
|
|
@@ -98,7 +126,7 @@ export function buildCodemodeDescription(tools, inlineBudget, capability) {
|
|
|
98
126
|
if (!capability.strict) {
|
|
99
127
|
lines.push(`Sandbox: ${capability.reason}.`);
|
|
100
128
|
}
|
|
101
|
-
const block = buildDeclarationBlock(tools.map(({ tool, textResult }) => ({ tool
|
|
129
|
+
const block = buildDeclarationBlock(tools.map(({ tool, textResult }) => ({ tool, textResult })), inlineBudget);
|
|
102
130
|
if (block.text !== "")
|
|
103
131
|
lines.push(block.text);
|
|
104
132
|
return lines.join("\n");
|
|
@@ -151,7 +179,7 @@ export function createCodemodeTool(options) {
|
|
|
151
179
|
name: CODEMODE_TOOL_NAME,
|
|
152
180
|
label: "Codemode",
|
|
153
181
|
get description() {
|
|
154
|
-
frozenDescription ??= buildCodemodeDescription(options.listTools(), inlineBudget, capability);
|
|
182
|
+
frozenDescription ??= buildCodemodeDescription(options.listTools(), inlineBudget, capability, options.layout);
|
|
155
183
|
return frozenDescription;
|
|
156
184
|
},
|
|
157
185
|
parameters: {
|
|
@@ -161,8 +189,8 @@ export function createCodemodeTool(options) {
|
|
|
161
189
|
},
|
|
162
190
|
required: ["script"],
|
|
163
191
|
},
|
|
164
|
-
// 网络隔离(Node ≥ 25
|
|
165
|
-
// 类(default 模式免审批);不隔离网络时脚本逃出 vm 就能联网,仍按 execute。
|
|
192
|
+
// 网络隔离(Node ≥ 25,或 OS 沙箱)时脚本只能经 tools.* 做事,每次内层调用各自过权限:codemode
|
|
193
|
+
// 本身按 read 类(default 模式免审批);不隔离网络时脚本逃出 vm 就能联网,仍按 execute。
|
|
166
194
|
permission: capability.strict ? "read" : "execute",
|
|
167
195
|
executionMode: "sequential",
|
|
168
196
|
promptSnippet: options.exclusive === true
|
|
@@ -179,8 +207,10 @@ export function createCodemodeTool(options) {
|
|
|
179
207
|
}
|
|
180
208
|
const tools = options.listTools().map(({ tool: t, textResult }) => ({
|
|
181
209
|
name: t.name,
|
|
182
|
-
declaration: toolDeclaration(
|
|
210
|
+
declaration: toolDeclaration(t, textResult ? {} : { textResult: false }),
|
|
183
211
|
}));
|
|
212
|
+
// 只有清单里的工具可调(coordinator 预设的清单只含活动集);脚本拼出别的名字也不放行。
|
|
213
|
+
const callable = new Set(tools.map((t) => t.name));
|
|
184
214
|
let tail = "";
|
|
185
215
|
const run = await runSandbox({
|
|
186
216
|
script: input.script,
|
|
@@ -188,9 +218,15 @@ export function createCodemodeTool(options) {
|
|
|
188
218
|
tools,
|
|
189
219
|
store: readStore(ctx.session),
|
|
190
220
|
signal: ctx.signal,
|
|
221
|
+
os: capability.os,
|
|
222
|
+
requireOsSandbox: strictNeedsOsSandbox(capability),
|
|
191
223
|
...(options.entry !== undefined ? { entry: options.entry } : {}),
|
|
192
224
|
...(options.nodePath !== undefined ? { nodePath: options.nodePath } : {}),
|
|
193
|
-
callTool: async (name, args, signal) =>
|
|
225
|
+
callTool: async (name, args, signal) => {
|
|
226
|
+
if (!callable.has(name))
|
|
227
|
+
throw new Error(`Tool ${name} is not available in codemode`);
|
|
228
|
+
return toScriptValue(name, await ctx.tools.executeTool(name, args, { signal }));
|
|
229
|
+
},
|
|
194
230
|
onOutput: (text) => {
|
|
195
231
|
tail = `${tail}${tail === "" ? "" : "\n"}${text}`.slice(-TAIL_CHARS);
|
|
196
232
|
ctx.onUpdate(tail);
|
|
@@ -240,33 +276,42 @@ export function createCodemodeTool(options) {
|
|
|
240
276
|
return tool;
|
|
241
277
|
}
|
|
242
278
|
/**
|
|
243
|
-
* codemode
|
|
244
|
-
* → 不注册并 warning(预设随之回退到 default)。
|
|
279
|
+
* codemode 工具工厂:生效模式(显式 `codemode.mode`,否则跟随预设,见 tools/presets.ts)为 off →
|
|
280
|
+
* 不注册;`requireStrict` 而运行时不隔离网络 → 不注册并 warning(预设随之回退到 default)。
|
|
281
|
+
* `coordinator` 预设下脚本可调用的工具限于活动集(不含 codemode 本身)。
|
|
245
282
|
*/
|
|
246
283
|
export function codemodeToolFactory(overrides = {}) {
|
|
247
284
|
return (ctx) => {
|
|
248
|
-
const
|
|
249
|
-
const mode =
|
|
285
|
+
const capability = overrides.capability ?? sandboxCapabilityFor(ctx.config);
|
|
286
|
+
const mode = resolveCodemodeMode(ctx.config, capability.strict).mode;
|
|
250
287
|
if (mode === "off")
|
|
251
288
|
return undefined;
|
|
252
|
-
const availability = codemodeAvailability(ctx.config.codemode?.requireStrict,
|
|
289
|
+
const availability = codemodeAvailability(ctx.config.codemode?.requireStrict, capability);
|
|
253
290
|
if (!availability.available) {
|
|
254
291
|
ctx.warn(availability.warning);
|
|
255
292
|
return undefined;
|
|
256
293
|
}
|
|
257
294
|
const registry = ctx.registry;
|
|
295
|
+
const onlyActive = codemodeCallableFilter(ctx.config) === "active";
|
|
296
|
+
const allowed = (name) => !onlyActive || (registry.active?.() ?? []).some((tool) => tool.name === name);
|
|
297
|
+
const direct = () => new Set(mode === "on" ? (registry.active?.() ?? []).map((tool) => tool.name) : []);
|
|
258
298
|
const options = {
|
|
259
299
|
capability: availability.capability,
|
|
260
300
|
exclusive: mode === "only",
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
301
|
+
layout: mode === "on" ? "on" : "only",
|
|
302
|
+
listTools: () => {
|
|
303
|
+
const active = direct();
|
|
304
|
+
return registry
|
|
305
|
+
.list()
|
|
306
|
+
.filter((name) => name !== CODEMODE_TOOL_NAME && allowed(name))
|
|
307
|
+
.map((name) => registry.get(name))
|
|
308
|
+
.filter((t) => t !== undefined)
|
|
309
|
+
.map((t) => ({
|
|
310
|
+
tool: t,
|
|
311
|
+
textResult: (registry.sourceOf?.(t.name) ?? "builtin") === "builtin",
|
|
312
|
+
...(active.has(t.name) ? { direct: true } : {}),
|
|
313
|
+
}));
|
|
314
|
+
},
|
|
270
315
|
};
|
|
271
316
|
const budget = ctx.config.codemode?.inlineBudget;
|
|
272
317
|
if (budget !== undefined)
|
|
@@ -1,31 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 压缩熔断(设计 §9
|
|
2
|
+
* 压缩熔断(设计 §9「熔断」,wave5 §8.2 C5)。[B2][W5-H1]
|
|
3
3
|
*
|
|
4
4
|
* - 无 `contextWindow` 或配置关闭 → 自动压缩关闭(手动 /compact 仍可用)。
|
|
5
|
-
* -
|
|
6
|
-
*
|
|
5
|
+
* - 不再限制「每 run 档二 ≤ 1 次」(单提示长任务需要多次摘要);改为两种跳闸:
|
|
6
|
+
* - 连续 3 次摘要失败;
|
|
7
|
+
* - 快速回填:连续 3 次摘要都在上一次摘要后不到 3 回合就又需要摘要(压缩不动、越压越满)。
|
|
8
|
+
* 跳闸后关闭自动压缩直到 `reset()`(例如用户手动压缩成功)。
|
|
9
|
+
* - 固定前缀(system + 工具表)已超过预算:摘要无济于事,直接告警、不再尝试(`prefix_overflow`,
|
|
10
|
+
* 由调用方每次检查时更新)。
|
|
11
|
+
* - 「nothing to compact」不计失败(调用方不调 `recordSummary`)。
|
|
7
12
|
* - 溢出恢复:压缩后估算仍 > 0.8 × window → 不重试。
|
|
8
13
|
*/
|
|
9
|
-
export declare const
|
|
10
|
-
|
|
14
|
+
export declare const MAX_CONSECUTIVE_FAILURES = 3;
|
|
15
|
+
/** 上一次摘要后不到这么多回合又需要摘要,记一次快速回填。 */
|
|
16
|
+
export declare const RAPID_REFILL_TURNS = 3;
|
|
17
|
+
export declare const MAX_RAPID_REFILLS = 3;
|
|
11
18
|
export declare const RETRY_AFTER_COMPACTION_RATIO = 0.8;
|
|
12
|
-
export type BreakerBlockReason = "disabled" | "no_window" | "tripped" | "
|
|
19
|
+
export type BreakerBlockReason = "disabled" | "no_window" | "tripped" | "rapid_refill" | "prefix_overflow";
|
|
13
20
|
export declare class CompactionBreaker {
|
|
14
|
-
private summariesThisRun;
|
|
15
21
|
private consecutiveFailures;
|
|
22
|
+
private rapidRefills;
|
|
23
|
+
private turn;
|
|
24
|
+
private lastSummaryTurn;
|
|
25
|
+
private prefixOverflow;
|
|
16
26
|
private enabled;
|
|
17
27
|
private contextWindow;
|
|
18
28
|
constructor(enabled: boolean, contextWindow: number | undefined);
|
|
19
29
|
configure(enabled: boolean, contextWindow: number | undefined): void;
|
|
30
|
+
/** 连续失败或快速回填跳闸(溢出恢复也不再尝试)。 */
|
|
20
31
|
get tripped(): boolean;
|
|
21
|
-
/**
|
|
32
|
+
/** 自动压缩整体是否开启。 */
|
|
22
33
|
get autoEnabled(): boolean;
|
|
34
|
+
/** 新 run 开始(会话在 agent_start 时调用);不再重置任何计数,保留以兼容调用点。 */
|
|
23
35
|
startRun(): void;
|
|
36
|
+
/** 每次请求前的阈值检查调用一次:回合计数(快速回填按它判断)。 */
|
|
37
|
+
tick(): void;
|
|
38
|
+
/** 固定前缀是否已超预算(调用方每次准备摘要前更新)。 */
|
|
39
|
+
setPrefixOverflow(overflow: boolean): void;
|
|
24
40
|
/** 自动档二能否执行;不能则给出原因。 */
|
|
25
41
|
blockReason(): BreakerBlockReason | undefined;
|
|
26
42
|
canSummarize(): boolean;
|
|
27
43
|
recordSummary(success: boolean): void;
|
|
28
|
-
/**
|
|
44
|
+
/** 手动压缩成功等场景:清零全部计数。 */
|
|
29
45
|
reset(): void;
|
|
30
46
|
/** 溢出恢复后是否值得重试。 */
|
|
31
47
|
shouldRetryAfterCompaction(tokensAfter: number): boolean;
|
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 压缩熔断(设计 §9
|
|
2
|
+
* 压缩熔断(设计 §9「熔断」,wave5 §8.2 C5)。[B2][W5-H1]
|
|
3
3
|
*
|
|
4
4
|
* - 无 `contextWindow` 或配置关闭 → 自动压缩关闭(手动 /compact 仍可用)。
|
|
5
|
-
* -
|
|
6
|
-
*
|
|
5
|
+
* - 不再限制「每 run 档二 ≤ 1 次」(单提示长任务需要多次摘要);改为两种跳闸:
|
|
6
|
+
* - 连续 3 次摘要失败;
|
|
7
|
+
* - 快速回填:连续 3 次摘要都在上一次摘要后不到 3 回合就又需要摘要(压缩不动、越压越满)。
|
|
8
|
+
* 跳闸后关闭自动压缩直到 `reset()`(例如用户手动压缩成功)。
|
|
9
|
+
* - 固定前缀(system + 工具表)已超过预算:摘要无济于事,直接告警、不再尝试(`prefix_overflow`,
|
|
10
|
+
* 由调用方每次检查时更新)。
|
|
11
|
+
* - 「nothing to compact」不计失败(调用方不调 `recordSummary`)。
|
|
7
12
|
* - 溢出恢复:压缩后估算仍 > 0.8 × window → 不重试。
|
|
8
13
|
*/
|
|
9
|
-
export const
|
|
10
|
-
|
|
14
|
+
export const MAX_CONSECUTIVE_FAILURES = 3;
|
|
15
|
+
/** 上一次摘要后不到这么多回合又需要摘要,记一次快速回填。 */
|
|
16
|
+
export const RAPID_REFILL_TURNS = 3;
|
|
17
|
+
export const MAX_RAPID_REFILLS = 3;
|
|
11
18
|
export const RETRY_AFTER_COMPACTION_RATIO = 0.8;
|
|
12
19
|
export class CompactionBreaker {
|
|
13
|
-
summariesThisRun = 0;
|
|
14
20
|
consecutiveFailures = 0;
|
|
21
|
+
rapidRefills = 0;
|
|
22
|
+
turn = 0;
|
|
23
|
+
lastSummaryTurn;
|
|
24
|
+
prefixOverflow = false;
|
|
15
25
|
enabled;
|
|
16
26
|
contextWindow;
|
|
17
27
|
constructor(enabled, contextWindow) {
|
|
@@ -22,15 +32,23 @@ export class CompactionBreaker {
|
|
|
22
32
|
this.enabled = enabled;
|
|
23
33
|
this.contextWindow = contextWindow;
|
|
24
34
|
}
|
|
35
|
+
/** 连续失败或快速回填跳闸(溢出恢复也不再尝试)。 */
|
|
25
36
|
get tripped() {
|
|
26
|
-
return this.consecutiveFailures >= MAX_CONSECUTIVE_FAILURES;
|
|
37
|
+
return (this.consecutiveFailures >= MAX_CONSECUTIVE_FAILURES || this.rapidRefills >= MAX_RAPID_REFILLS);
|
|
27
38
|
}
|
|
28
|
-
/**
|
|
39
|
+
/** 自动压缩整体是否开启。 */
|
|
29
40
|
get autoEnabled() {
|
|
30
41
|
return this.enabled && this.contextWindow !== undefined && !this.tripped;
|
|
31
42
|
}
|
|
32
|
-
|
|
33
|
-
|
|
43
|
+
/** 新 run 开始(会话在 agent_start 时调用);不再重置任何计数,保留以兼容调用点。 */
|
|
44
|
+
startRun() { }
|
|
45
|
+
/** 每次请求前的阈值检查调用一次:回合计数(快速回填按它判断)。 */
|
|
46
|
+
tick() {
|
|
47
|
+
this.turn++;
|
|
48
|
+
}
|
|
49
|
+
/** 固定前缀是否已超预算(调用方每次准备摘要前更新)。 */
|
|
50
|
+
setPrefixOverflow(overflow) {
|
|
51
|
+
this.prefixOverflow = overflow;
|
|
34
52
|
}
|
|
35
53
|
/** 自动档二能否执行;不能则给出原因。 */
|
|
36
54
|
blockReason() {
|
|
@@ -38,23 +56,35 @@ export class CompactionBreaker {
|
|
|
38
56
|
return "disabled";
|
|
39
57
|
if (this.contextWindow === undefined)
|
|
40
58
|
return "no_window";
|
|
41
|
-
if (this.
|
|
59
|
+
if (this.consecutiveFailures >= MAX_CONSECUTIVE_FAILURES)
|
|
42
60
|
return "tripped";
|
|
43
|
-
if (this.
|
|
44
|
-
return "
|
|
61
|
+
if (this.rapidRefills >= MAX_RAPID_REFILLS)
|
|
62
|
+
return "rapid_refill";
|
|
63
|
+
if (this.prefixOverflow)
|
|
64
|
+
return "prefix_overflow";
|
|
45
65
|
return undefined;
|
|
46
66
|
}
|
|
47
67
|
canSummarize() {
|
|
48
68
|
return this.blockReason() === undefined;
|
|
49
69
|
}
|
|
50
70
|
recordSummary(success) {
|
|
51
|
-
|
|
52
|
-
|
|
71
|
+
if (!success) {
|
|
72
|
+
this.consecutiveFailures++;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
this.consecutiveFailures = 0;
|
|
76
|
+
const last = this.lastSummaryTurn;
|
|
77
|
+
if (last !== undefined && this.turn - last < RAPID_REFILL_TURNS)
|
|
78
|
+
this.rapidRefills++;
|
|
79
|
+
else
|
|
80
|
+
this.rapidRefills = 0;
|
|
81
|
+
this.lastSummaryTurn = this.turn;
|
|
53
82
|
}
|
|
54
|
-
/**
|
|
83
|
+
/** 手动压缩成功等场景:清零全部计数。 */
|
|
55
84
|
reset() {
|
|
56
85
|
this.consecutiveFailures = 0;
|
|
57
|
-
this.
|
|
86
|
+
this.rapidRefills = 0;
|
|
87
|
+
this.lastSummaryTurn = undefined;
|
|
58
88
|
}
|
|
59
89
|
/** 溢出恢复后是否值得重试。 */
|
|
60
90
|
shouldRetryAfterCompaction(tokensAfter) {
|
|
@@ -2,15 +2,23 @@
|
|
|
2
2
|
* 上下文 token 估算(设计 §9「估算」)。[B2]
|
|
3
3
|
*
|
|
4
4
|
* `contextTokens` = 最后一条非 error / aborted 助手消息的 `totalTokens`(缺则
|
|
5
|
-
* `input + output + cacheRead + cacheWrite`,**含 output**)+
|
|
5
|
+
* `input + output + cacheRead + cacheWrite`,**含 output**)+ 其后消息的估算(图片 1 600)。
|
|
6
6
|
* 若该 usage 之后活动分支上出现过 `context_edit` 或 `compaction`,usage 不再可信,按投影全量重估。
|
|
7
|
+
*
|
|
8
|
+
* [W5-H1] 文本估算按脚本区分(wave5 D28 / C10):CJK 统一表意文字(含扩展区与兼容区)、假名、
|
|
9
|
+
* 谚文、CJK 符号与全角形式每字 1 token,其余字符合计 / 4 向上取整。「字符 / 4」对中文低估 3–4 倍,
|
|
10
|
+
* 中文会话会晚触发直至溢出;所有阈值(档一、档二、保留区)都用这个口径。
|
|
7
11
|
*/
|
|
8
|
-
import type { Usage } from "../ai/types.js";
|
|
12
|
+
import type { ContentBlock, Usage } from "../ai/types.js";
|
|
9
13
|
import type { ContextItem } from "../session/projection.js";
|
|
10
14
|
import type { AgentMessage, SessionEntry } from "../session/types.js";
|
|
11
15
|
export declare const IMAGE_TOKENS = 1600;
|
|
16
|
+
/** 文本的 token 估算:CJK / 假名 / 谚文每字 1,其余字符 / 4(向上取整)。 */
|
|
17
|
+
export declare function estimateTextTokens(text: string): number;
|
|
12
18
|
export declare function calculateContextTokens(usage: Usage): number;
|
|
13
|
-
/**
|
|
19
|
+
/** 内容的估算(文本按脚本,图片每张 1 600)。 */
|
|
20
|
+
export declare function estimateContentTokens(content: string | readonly ContentBlock[]): number;
|
|
21
|
+
/** 单条消息的估算(文本按脚本区分,图片每张 1 600)。 */
|
|
14
22
|
export declare function estimateMessageTokens(message: AgentMessage): number;
|
|
15
23
|
export declare function estimateMessagesTokens(messages: readonly AgentMessage[]): number;
|
|
16
24
|
export interface ContextEstimate {
|