@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,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 思考级别(设计 §3.6「思考」):用户面 off / minimal / low / medium / high / xhigh。
|
|
3
|
+
*
|
|
4
|
+
* - `getSupportedLevels(model)`:非推理模型只有 off;`thinkingLevelMap[level] === null` 的级别
|
|
5
|
+
* 不支持;`xhigh` 只有在映射表里显式给出时才支持。
|
|
6
|
+
* - `clampThinkingLevel`:不支持的级别先向上找、再向下找最近的可用级别。
|
|
7
|
+
* - 预算型缺省:minimal 1024 / low 2048 / medium 8192 / high 16384(xhigh 按 high);
|
|
8
|
+
* 与回答共享 max_tokens 时至少给回答留 1024。
|
|
9
|
+
*/
|
|
10
|
+
export const THINKING_LEVELS = [
|
|
11
|
+
"off",
|
|
12
|
+
"minimal",
|
|
13
|
+
"low",
|
|
14
|
+
"medium",
|
|
15
|
+
"high",
|
|
16
|
+
"xhigh",
|
|
17
|
+
];
|
|
18
|
+
export const DEFAULT_THINKING_BUDGETS = {
|
|
19
|
+
minimal: 1024,
|
|
20
|
+
low: 2048,
|
|
21
|
+
medium: 8192,
|
|
22
|
+
high: 16384,
|
|
23
|
+
xhigh: 16384,
|
|
24
|
+
};
|
|
25
|
+
/** 与回答共享 max_tokens 时留给回答的最少 token。 */
|
|
26
|
+
export const MIN_ANSWER_TOKENS = 1024;
|
|
27
|
+
export function isThinkingLevel(value) {
|
|
28
|
+
return typeof value === "string" && THINKING_LEVELS.includes(value);
|
|
29
|
+
}
|
|
30
|
+
export function getSupportedLevels(model) {
|
|
31
|
+
if (!model.reasoning)
|
|
32
|
+
return ["off"];
|
|
33
|
+
return THINKING_LEVELS.filter((level) => {
|
|
34
|
+
const mapped = model.thinkingLevelMap?.[level];
|
|
35
|
+
if (mapped === null)
|
|
36
|
+
return false;
|
|
37
|
+
if (level === "xhigh")
|
|
38
|
+
return mapped !== undefined;
|
|
39
|
+
return true;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
export function clampThinkingLevel(model, level) {
|
|
43
|
+
const supported = getSupportedLevels(model);
|
|
44
|
+
if (supported.includes(level))
|
|
45
|
+
return level;
|
|
46
|
+
const index = THINKING_LEVELS.indexOf(level);
|
|
47
|
+
for (let i = index + 1; i < THINKING_LEVELS.length; i++) {
|
|
48
|
+
const candidate = THINKING_LEVELS[i];
|
|
49
|
+
if (candidate !== undefined && supported.includes(candidate))
|
|
50
|
+
return candidate;
|
|
51
|
+
}
|
|
52
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
53
|
+
const candidate = THINKING_LEVELS[i];
|
|
54
|
+
if (candidate !== undefined && supported.includes(candidate))
|
|
55
|
+
return candidate;
|
|
56
|
+
}
|
|
57
|
+
return supported[0] ?? "off";
|
|
58
|
+
}
|
|
59
|
+
/** 级别在映射表里的供应商原生值;未映射返回 undefined(调用方按协议缺省处理)。 */
|
|
60
|
+
export function mappedThinkingValue(model, level) {
|
|
61
|
+
const mapped = model.thinkingLevelMap?.[level];
|
|
62
|
+
return mapped === null ? undefined : mapped;
|
|
63
|
+
}
|
|
64
|
+
/** 预算:映射表给数字就用数字,否则按缺省表。 */
|
|
65
|
+
export function thinkingBudget(model, level) {
|
|
66
|
+
const mapped = model.thinkingLevelMap?.[level];
|
|
67
|
+
return typeof mapped === "number" ? mapped : DEFAULT_THINKING_BUDGETS[level];
|
|
68
|
+
}
|
|
69
|
+
/** 预算不超过 `ceiling - MIN_ANSWER_TOKENS`(不足时为 0)。 */
|
|
70
|
+
export function clampBudgetToAnswerRoom(budget, ceiling) {
|
|
71
|
+
return Math.min(budget, Math.max(0, ceiling - MIN_ANSWER_TOKENS));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 预算型思考(Anthropic 老模型):budget 计入 max_tokens。`requested` 是调用方要的回答上限
|
|
75
|
+
* (缺省 = 模型上限);返回值 maxTokens ≤ model.maxTokens,budget < maxTokens;budget < 1024 时调用方应关闭思考(Anthropic 下限)。
|
|
76
|
+
*/
|
|
77
|
+
export function budgetedMaxTokens(modelMaxTokens, requested, budget) {
|
|
78
|
+
const maxTokens = requested === undefined ? modelMaxTokens : Math.min(requested + budget, modelMaxTokens);
|
|
79
|
+
let adjusted = budget;
|
|
80
|
+
if (maxTokens <= adjusted)
|
|
81
|
+
adjusted = clampBudgetToAnswerRoom(adjusted, maxTokens);
|
|
82
|
+
return { maxTokens, budget: Math.max(0, Math.min(adjusted, maxTokens - 1)) };
|
|
83
|
+
}
|
|
84
|
+
const DISCRETE_LEVELS = ["minimal", "low", "medium", "high"];
|
|
85
|
+
/**
|
|
86
|
+
* 用户面级别 → 离散级别:映射表给字串就用字串(大小写不敏感),否则用级别本身;`xhigh` 落到
|
|
87
|
+
* `high`。映射值不是四个离散级别之一(例如写成了数字预算)返回 undefined,调用方改走预算。
|
|
88
|
+
*/
|
|
89
|
+
export function discreteThinkingLevel(model, level) {
|
|
90
|
+
const mapped = model.thinkingLevelMap?.[level];
|
|
91
|
+
if (typeof mapped === "number")
|
|
92
|
+
return undefined;
|
|
93
|
+
const value = typeof mapped === "string" ? mapped.toLowerCase() : level;
|
|
94
|
+
if (value === "xhigh")
|
|
95
|
+
return "high";
|
|
96
|
+
return DISCRETE_LEVELS.includes(value) ? value : undefined;
|
|
97
|
+
}
|
|
98
|
+
/** 解析请求级别:缺省 off;钳位到模型支持的级别。 */
|
|
99
|
+
export function resolveThinkingLevel(model, level) {
|
|
100
|
+
return clampThinkingLevel(model, level ?? "off");
|
|
101
|
+
}
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模型接入层契约(设计 §3.1–§3.2、§3.5、§3.6)。[B0] 契约文件,实现归 B1 / B8。
|
|
3
|
+
*
|
|
4
|
+
* 补全与偏差(相对设计正文):
|
|
5
|
+
* - 内容块(TextBlock / ThinkingBlock / ImageBlock / ToolCallBlock)、`Usage`、`StopReason`、
|
|
6
|
+
* LLM 消息(`Message`)定义在这里而不是 agent/types.ts:依赖方向是 agent → ai,协议层
|
|
7
|
+
* 必须能拿到这些类型。agent/types.ts 原样再导出它们。
|
|
8
|
+
* - `JsonSchema`(工具参数子集)也定义在这里,因为 `ToolDecl` 要发给供应商;
|
|
9
|
+
* tools/types.ts 与 agent/schema.ts 再导出。
|
|
10
|
+
* - `TranscriptContext` = 循环交给协议的原始转录(含 `system` 节补丁消息);协议实现先调
|
|
11
|
+
* `normalizeContext()`(ai/context.ts,B1)得到 `NormalizedContext` 再拼请求体。
|
|
12
|
+
* - `GoogleCompat` / `OpenAIResponsesCompat` 只列出已知字段,B8 按需追加(走契约变更流程)。
|
|
13
|
+
* - `ProviderRegistryApi` / `ApiKeyResolution` 是 Runtime(cli/runtime.ts)需要的最小接口,
|
|
14
|
+
* B1 的 `ProviderRegistry` 类实现它。
|
|
15
|
+
* - [W3-C0] 第三波 §1.3 的五个缓存兼容开关单列为 `PromptCacheCompat`(各协议共用),只并入
|
|
16
|
+
* `ProviderCompat`(全部可选),不改各协议 compat 接口——`detectCompat` 的返回形状不变,
|
|
17
|
+
* 缺省值由 C1a 在协议层按端点推断。
|
|
18
|
+
*/
|
|
19
|
+
export type KnownApi = "anthropic-messages" | "openai-completions" | "openai-responses" | "google-generative-ai";
|
|
20
|
+
export type Api = KnownApi | (string & {});
|
|
21
|
+
export type ModelThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh";
|
|
22
|
+
/** 除 off 以外的级别。 */
|
|
23
|
+
export type ThinkingLevel = Exclude<ModelThinkingLevel, "off">;
|
|
24
|
+
export type CacheRetention = "none" | "short" | "long";
|
|
25
|
+
/**
|
|
26
|
+
* [W3-C0] 一次请求的用途(第三波 §1.2):会话层按用途分开记录请求链——`turn` 是循环里的
|
|
27
|
+
* 真实回合,`summary` 是压缩 / 分支摘要,`warm` 是缓存保温重放,`probe` 是 `models check` /
|
|
28
|
+
* `cache-probe` 一类的探测。缺省视为 `turn`。协议层只透传,不改变请求体。
|
|
29
|
+
*/
|
|
30
|
+
export type RequestPurpose = "turn" | "summary" | "warm" | "probe";
|
|
31
|
+
/** `provider/model-id` 拆开后的引用。 */
|
|
32
|
+
export interface ModelRef {
|
|
33
|
+
provider: string;
|
|
34
|
+
id: string;
|
|
35
|
+
/** 所选渠道(多渠道供应商;单渠道 / 隐式渠道不填)。 */
|
|
36
|
+
channel?: string;
|
|
37
|
+
}
|
|
38
|
+
export type JsonSchemaType = "object" | "string" | "number" | "integer" | "boolean" | "array";
|
|
39
|
+
export type JsonPrimitive = string | number | boolean | null;
|
|
40
|
+
/**
|
|
41
|
+
* 支持的关键字:type / properties / required / items / enum / description /
|
|
42
|
+
* additionalProperties(只接受布尔)/ default / title。其它关键字不在子集内,
|
|
43
|
+
* `checkSchemaSubset()`(agent/schema.ts)会报出来。
|
|
44
|
+
*/
|
|
45
|
+
export interface JsonSchema {
|
|
46
|
+
type?: JsonSchemaType;
|
|
47
|
+
title?: string;
|
|
48
|
+
description?: string;
|
|
49
|
+
properties?: Record<string, JsonSchema>;
|
|
50
|
+
required?: readonly string[];
|
|
51
|
+
additionalProperties?: boolean;
|
|
52
|
+
items?: JsonSchema;
|
|
53
|
+
enum?: readonly JsonPrimitive[];
|
|
54
|
+
default?: unknown;
|
|
55
|
+
}
|
|
56
|
+
export interface TextBlock {
|
|
57
|
+
type: "text";
|
|
58
|
+
text: string;
|
|
59
|
+
/** 供应商给的文本签名(原样回放)。 */
|
|
60
|
+
textSignature?: string;
|
|
61
|
+
}
|
|
62
|
+
export interface ThinkingBlock {
|
|
63
|
+
type: "thinking";
|
|
64
|
+
thinking: string;
|
|
65
|
+
/** Anthropic signature 等;跨模型回放时由 transform.ts 降级为文本。 */
|
|
66
|
+
thinkingSignature?: string;
|
|
67
|
+
redacted?: boolean;
|
|
68
|
+
}
|
|
69
|
+
export interface ImageBlock {
|
|
70
|
+
type: "image";
|
|
71
|
+
/** base64,不含 data: 前缀。 */
|
|
72
|
+
data: string;
|
|
73
|
+
mimeType: string;
|
|
74
|
+
}
|
|
75
|
+
export interface ToolCallBlock {
|
|
76
|
+
type: "toolCall";
|
|
77
|
+
id: string;
|
|
78
|
+
name: string;
|
|
79
|
+
/** `toolcall_end` 时保证是合法对象(§3.1 流契约)。 */
|
|
80
|
+
arguments: Record<string, unknown>;
|
|
81
|
+
/** Google thoughtSignature 等。 */
|
|
82
|
+
thoughtSignature?: string;
|
|
83
|
+
}
|
|
84
|
+
/** 用户消息与工具结果可携带的块。 */
|
|
85
|
+
export type ContentBlock = TextBlock | ImageBlock;
|
|
86
|
+
/** 助手消息的块。 */
|
|
87
|
+
export type AssistantContentBlock = TextBlock | ThinkingBlock | ToolCallBlock;
|
|
88
|
+
export interface UsageCost {
|
|
89
|
+
input: number;
|
|
90
|
+
output: number;
|
|
91
|
+
cacheRead: number;
|
|
92
|
+
cacheWrite: number;
|
|
93
|
+
total: number;
|
|
94
|
+
}
|
|
95
|
+
/** `input` 不含缓存部分(§3.6)。 */
|
|
96
|
+
export interface Usage {
|
|
97
|
+
input: number;
|
|
98
|
+
output: number;
|
|
99
|
+
cacheRead: number;
|
|
100
|
+
cacheWrite: number;
|
|
101
|
+
/** 1 小时缓存写入(按 2× 计价),已包含在 cacheWrite 中。 */
|
|
102
|
+
cacheWrite1h?: number;
|
|
103
|
+
reasoning?: number;
|
|
104
|
+
totalTokens: number;
|
|
105
|
+
/** 由 ai/cost.ts 写回;模型无 cost 时缺省。 */
|
|
106
|
+
cost?: UsageCost;
|
|
107
|
+
/**
|
|
108
|
+
* [W3-C0] 原始响应里**出现过**任何缓存字段(即使值为 0)为 true;字段缺失为 false;
|
|
109
|
+
* 协议尚未解析该标记时缺省(第三波 §1.6,「值为 0」与「不报」由此区分)。
|
|
110
|
+
*/
|
|
111
|
+
cacheReported?: boolean;
|
|
112
|
+
}
|
|
113
|
+
export type StopReason = "stop" | "length" | "toolUse" | "aborted" | "error";
|
|
114
|
+
/** 工具声明(发给供应商的形状)。 */
|
|
115
|
+
export interface ToolDecl {
|
|
116
|
+
name: string;
|
|
117
|
+
description: string;
|
|
118
|
+
parameters: JsonSchema;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 系统提示与工具表作为 `system` 消息落盘:首条给全量 `sections`,之后是补丁
|
|
122
|
+
* (节名级替换,`null` 删除;`toolsAdded` / `toolsRemoved` 为工具表差异)。
|
|
123
|
+
*/
|
|
124
|
+
export interface SystemMessage {
|
|
125
|
+
role: "system";
|
|
126
|
+
sections: Record<string, string | null>;
|
|
127
|
+
toolsAdded?: ToolDecl[];
|
|
128
|
+
toolsRemoved?: string[];
|
|
129
|
+
timestamp: number;
|
|
130
|
+
}
|
|
131
|
+
export type KnownMessageOrigin = "steer" | "followUp" | "host";
|
|
132
|
+
export type MessageOrigin = KnownMessageOrigin | (string & {});
|
|
133
|
+
export interface UserMessage {
|
|
134
|
+
role: "user";
|
|
135
|
+
content: string | ContentBlock[];
|
|
136
|
+
/** 无 = 普通用户输入;宿主 `sendUser(text, origin)` 的 origin 原样记录。 */
|
|
137
|
+
origin?: MessageOrigin;
|
|
138
|
+
timestamp: number;
|
|
139
|
+
}
|
|
140
|
+
export interface AssistantMessage {
|
|
141
|
+
role: "assistant";
|
|
142
|
+
content: AssistantContentBlock[];
|
|
143
|
+
api: Api;
|
|
144
|
+
provider: string;
|
|
145
|
+
model: string;
|
|
146
|
+
responseId?: string;
|
|
147
|
+
usage: Usage;
|
|
148
|
+
stopReason: StopReason;
|
|
149
|
+
errorMessage?: string;
|
|
150
|
+
/** 用户面级别(钳位后)。 */
|
|
151
|
+
thinkingLevel?: ModelThinkingLevel;
|
|
152
|
+
/** 实际发给供应商的值(effort 字串或预算)。 */
|
|
153
|
+
providerThinkingLevel?: string;
|
|
154
|
+
/** 供应商原始 finish / stop reason。 */
|
|
155
|
+
rawStopReason?: string;
|
|
156
|
+
timestamp: number;
|
|
157
|
+
}
|
|
158
|
+
export interface ToolResultMessage {
|
|
159
|
+
role: "toolResult";
|
|
160
|
+
toolCallId: string;
|
|
161
|
+
toolName: string;
|
|
162
|
+
content: string | ContentBlock[];
|
|
163
|
+
isError: boolean;
|
|
164
|
+
/** 落盘,不进上下文。 */
|
|
165
|
+
details?: unknown;
|
|
166
|
+
timestamp: number;
|
|
167
|
+
}
|
|
168
|
+
export type Message = SystemMessage | UserMessage | AssistantMessage | ToolResultMessage;
|
|
169
|
+
/** 循环交给协议的转录(未折叠的 system 补丁也在其中)。 */
|
|
170
|
+
export interface TranscriptContext {
|
|
171
|
+
readonly messages: readonly Message[];
|
|
172
|
+
}
|
|
173
|
+
/** normalizeContext() 的输出:system 已折叠为一段文本,工具表已重装,模态已过滤。 */
|
|
174
|
+
export interface NormalizedContext {
|
|
175
|
+
systemPrompt: string;
|
|
176
|
+
/** 节名 → 文本(按装配顺序),供 Anthropic 打缓存断点。 */
|
|
177
|
+
systemSections: readonly {
|
|
178
|
+
name: string;
|
|
179
|
+
text: string;
|
|
180
|
+
}[];
|
|
181
|
+
tools: ToolDecl[];
|
|
182
|
+
messages: (UserMessage | AssistantMessage | ToolResultMessage)[];
|
|
183
|
+
}
|
|
184
|
+
export type OpenAIThinkingFormat = "openai" | "openrouter" | "deepseek" | "zai" | "qwen" | "none";
|
|
185
|
+
export interface OpenAICompletionsCompat {
|
|
186
|
+
maxTokensField: "max_tokens" | "max_completion_tokens";
|
|
187
|
+
supportsDeveloperRole: boolean;
|
|
188
|
+
supportsUsageInStreaming: boolean;
|
|
189
|
+
supportsFinishReason: boolean;
|
|
190
|
+
supportsReasoningEffort: boolean;
|
|
191
|
+
thinkingFormat: OpenAIThinkingFormat;
|
|
192
|
+
/** 预算型思考的字段名(qwen: thinking_budget);无则不发预算。 */
|
|
193
|
+
thinkingTokenBudgetField?: string;
|
|
194
|
+
requiresReasoningContentOnAssistantMessages: boolean;
|
|
195
|
+
requiresToolResultName: boolean;
|
|
196
|
+
requiresAssistantAfterToolResult: boolean;
|
|
197
|
+
supportsMidConvoSystemMessages: boolean;
|
|
198
|
+
/** `anthropic`:OpenRouter 上的 anthropic/* 模型在消息块上打 cache_control。 */
|
|
199
|
+
cacheControlFormat: "none" | "anthropic";
|
|
200
|
+
supportsStrictTools: boolean;
|
|
201
|
+
supportsStore: boolean;
|
|
202
|
+
}
|
|
203
|
+
export interface AnthropicMessagesCompat {
|
|
204
|
+
supportsCacheControlOnTools: boolean;
|
|
205
|
+
supportsTemperatureWithThinking: boolean;
|
|
206
|
+
/** true:新模型用 `effort` 参数;false:老模型用 `budget_tokens`。 */
|
|
207
|
+
adaptiveThinking: boolean;
|
|
208
|
+
maxCacheBreakpoints: number;
|
|
209
|
+
}
|
|
210
|
+
/** B8 补全。 */
|
|
211
|
+
export interface GoogleCompat {
|
|
212
|
+
supportsThoughtSignature: boolean;
|
|
213
|
+
supportsFunctionResponseParts: boolean;
|
|
214
|
+
}
|
|
215
|
+
/** B8 补全。 */
|
|
216
|
+
export interface OpenAIResponsesCompat {
|
|
217
|
+
supportsReasoningSummary: boolean;
|
|
218
|
+
supportsStore: boolean;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* [W3-C0] 供应商不报缓存时的处理(第三波 §1.6):`auto` 按响应自动判定三态;`silent` /
|
|
222
|
+
* `reported` 强制(`ama models cache-probe` 给出建议写法)。
|
|
223
|
+
*/
|
|
224
|
+
export type CacheReportingSetting = "auto" | "silent" | "reported";
|
|
225
|
+
/**
|
|
226
|
+
* [W3-C0] 缓存相关的兼容开关(第三波 §1.3),各协议共用;这里是解析后的完整形状,
|
|
227
|
+
* `ProviderCompat` 里全部可选。缺省值由协议层按端点推断(官方端点开、自定义供应商关)。
|
|
228
|
+
*/
|
|
229
|
+
export interface PromptCacheCompat {
|
|
230
|
+
/** 发 `prompt_cache_key`(OpenAI 系);官方端点缺省 true,其余缺省 false。 */
|
|
231
|
+
sendPromptCacheKey: boolean;
|
|
232
|
+
/** 发 `x-session-affinity` / `x-client-request-id`(OpenRouter 为 `x-session-id`)亲和头。 */
|
|
233
|
+
sendSessionAffinityHeaders: boolean;
|
|
234
|
+
/** `cacheRetention: "long"` 可用(Anthropic 1h、OpenAI 24h);不支持时降为 short。 */
|
|
235
|
+
supportsLongCacheRetention: boolean;
|
|
236
|
+
/** Responses 的 `prompt_cache_options`(30m 显式缓存)。 */
|
|
237
|
+
supportsExplicitPromptCacheMode: boolean;
|
|
238
|
+
cacheReporting: CacheReportingSetting;
|
|
239
|
+
}
|
|
240
|
+
export type ProviderCompat = Partial<OpenAICompletionsCompat & AnthropicMessagesCompat & GoogleCompat & OpenAIResponsesCompat & PromptCacheCompat>;
|
|
241
|
+
export type AuthHeader = "authorization-bearer" | "x-api-key" | "x-goog-api-key" | {
|
|
242
|
+
header: string;
|
|
243
|
+
prefix?: string;
|
|
244
|
+
};
|
|
245
|
+
export interface CostTier {
|
|
246
|
+
/** 当 input + cacheRead + cacheWrite 超过该值时使用本档价格。 */
|
|
247
|
+
inputTokensAbove: number;
|
|
248
|
+
input: number;
|
|
249
|
+
output: number;
|
|
250
|
+
cacheRead: number;
|
|
251
|
+
cacheWrite: number;
|
|
252
|
+
}
|
|
253
|
+
/** 单位:美元 / 百万 token。 */
|
|
254
|
+
export interface ModelCost {
|
|
255
|
+
input: number;
|
|
256
|
+
output: number;
|
|
257
|
+
cacheRead: number;
|
|
258
|
+
cacheWrite: number;
|
|
259
|
+
tiers?: CostTier[];
|
|
260
|
+
}
|
|
261
|
+
/** 模型目录 `promptCache`:只写有公开依据的值,留空 = 不承诺(不保温、归因用启发值)。 */
|
|
262
|
+
export interface ModelPromptCache {
|
|
263
|
+
/** `short` 档 TTL(秒)。 */
|
|
264
|
+
short?: number;
|
|
265
|
+
/** `long` 档 TTL(秒)。 */
|
|
266
|
+
long?: number;
|
|
267
|
+
/** [W3-C0] 最小可缓存长度(token);未命中检测的噪声下限取 max(1024, minTokens)。 */
|
|
268
|
+
minTokens?: number;
|
|
269
|
+
}
|
|
270
|
+
export interface Model {
|
|
271
|
+
id: string;
|
|
272
|
+
name: string;
|
|
273
|
+
provider: string;
|
|
274
|
+
api: Api;
|
|
275
|
+
/** 覆盖供应商 baseUrl。 */
|
|
276
|
+
baseUrl?: string;
|
|
277
|
+
input: ("text" | "image")[];
|
|
278
|
+
reasoning: boolean;
|
|
279
|
+
/** null = 该级别不支持。 */
|
|
280
|
+
thinkingLevelMap?: Partial<Record<ModelThinkingLevel, string | number | null>>;
|
|
281
|
+
/** 缺省 → 关自动压缩并警告。 */
|
|
282
|
+
contextWindow?: number;
|
|
283
|
+
maxTokens: number;
|
|
284
|
+
cost?: ModelCost;
|
|
285
|
+
/** 各档缓存的 TTL(秒)与最小可缓存长度(第三波 §1.4)。 */
|
|
286
|
+
promptCache?: ModelPromptCache;
|
|
287
|
+
headers?: Record<string, string>;
|
|
288
|
+
samplingParams?: Record<string, unknown>;
|
|
289
|
+
compat?: ProviderCompat;
|
|
290
|
+
/**
|
|
291
|
+
* [B1 追加] 鉴权头形状;registry 从 `ProviderData.authHeader` 填入。缺省按协议
|
|
292
|
+
* (anthropic-messages → x-api-key,其余 → Authorization: Bearer)。
|
|
293
|
+
*/
|
|
294
|
+
authHeader?: AuthHeader;
|
|
295
|
+
/**
|
|
296
|
+
* [B1 追加] false = 无 key 也能调用(本地服务);registry 从 `ProviderData.requiresApiKey`
|
|
297
|
+
* 填入。缺省视为 true:流函数缺 key 时同步抛 `AmaError{code:"no_api_key"}`。
|
|
298
|
+
*/
|
|
299
|
+
requiresApiKey?: boolean;
|
|
300
|
+
/** 所选渠道名(registry 物化时填入;隐式 `default` 渠道不填)。 */
|
|
301
|
+
channel?: string;
|
|
302
|
+
/** 该模型挂载的全部渠道,首个为首选(多渠道供应商才有)。 */
|
|
303
|
+
channels?: string[];
|
|
304
|
+
}
|
|
305
|
+
/** 物化后的渠道(docs/providers.md「渠道」);key 不在这里,经 `resolveApiKey(provider, channel)` 取。 */
|
|
306
|
+
export interface ProviderChannel {
|
|
307
|
+
name: string;
|
|
308
|
+
api: Api;
|
|
309
|
+
baseUrl: string;
|
|
310
|
+
authHeader?: AuthHeader;
|
|
311
|
+
headers?: Record<string, string>;
|
|
312
|
+
compat?: ProviderCompat;
|
|
313
|
+
}
|
|
314
|
+
export interface ProviderData {
|
|
315
|
+
id: string;
|
|
316
|
+
name: string;
|
|
317
|
+
api: Api;
|
|
318
|
+
baseUrl: string;
|
|
319
|
+
/** API Key 环境变量候选,顺序即优先级;首项是各家标准名。 */
|
|
320
|
+
envKeys: string[];
|
|
321
|
+
authHeader?: AuthHeader;
|
|
322
|
+
headers?: Record<string, string>;
|
|
323
|
+
compat?: ProviderCompat;
|
|
324
|
+
models: Model[];
|
|
325
|
+
/** 本地服务 false:无 key 也能用。 */
|
|
326
|
+
requiresApiKey: boolean;
|
|
327
|
+
builtin: boolean;
|
|
328
|
+
/** 显式配置的渠道(没有 `channels` 的供应商不填,按单渠道处理)。 */
|
|
329
|
+
channels?: ProviderChannel[];
|
|
330
|
+
/** 首选渠道名(有 `channels` 时)。 */
|
|
331
|
+
defaultChannel?: string;
|
|
332
|
+
}
|
|
333
|
+
/** `catalog/*.json` 文件形状(§3.4)。 */
|
|
334
|
+
export interface ModelCatalogFile {
|
|
335
|
+
version: 1;
|
|
336
|
+
provider: string;
|
|
337
|
+
models: Omit<Model, "provider" | "api">[];
|
|
338
|
+
}
|
|
339
|
+
export interface StreamOptions {
|
|
340
|
+
signal: AbortSignal;
|
|
341
|
+
apiKey?: string;
|
|
342
|
+
/** 值为 null 表示删除该头。 */
|
|
343
|
+
headers?: Record<string, string | null>;
|
|
344
|
+
timeoutMs?: number;
|
|
345
|
+
maxTokens?: number;
|
|
346
|
+
temperature?: number;
|
|
347
|
+
thinkingLevel?: ModelThinkingLevel;
|
|
348
|
+
cacheRetention?: CacheRetention;
|
|
349
|
+
sessionId?: string;
|
|
350
|
+
/** [W3-C0] 请求用途(第三波 §1.2);缺省 `turn`。协议层只透传给观测方,不进请求体。 */
|
|
351
|
+
purpose?: RequestPurpose;
|
|
352
|
+
/**
|
|
353
|
+
* [W3-C0] `"none"`:本次请求带工具表但禁止调用(压缩摘要走会话前缀续写,第三波 §1.8)。
|
|
354
|
+
* 映射:Anthropic `tool_choice:{type:"none"}`、Completions / Responses `tool_choice:"none"`、
|
|
355
|
+
* Google `toolConfig.functionCallingConfig.mode:"NONE"`。缺省不发。
|
|
356
|
+
*/
|
|
357
|
+
toolChoice?: "none";
|
|
358
|
+
/** 观测 / 替换请求体:返回非 undefined 即替换。 */
|
|
359
|
+
onPayload?(payload: unknown): unknown;
|
|
360
|
+
onResponse?(status: number, headers: Headers): void;
|
|
361
|
+
}
|
|
362
|
+
export type AssistantEvent = {
|
|
363
|
+
type: "start";
|
|
364
|
+
partial: AssistantMessage;
|
|
365
|
+
} | {
|
|
366
|
+
type: "text_start" | "text_end";
|
|
367
|
+
contentIndex: number;
|
|
368
|
+
partial: AssistantMessage;
|
|
369
|
+
} | {
|
|
370
|
+
type: "text_delta";
|
|
371
|
+
contentIndex: number;
|
|
372
|
+
delta: string;
|
|
373
|
+
partial: AssistantMessage;
|
|
374
|
+
} | {
|
|
375
|
+
type: "thinking_start" | "thinking_end";
|
|
376
|
+
contentIndex: number;
|
|
377
|
+
partial: AssistantMessage;
|
|
378
|
+
} | {
|
|
379
|
+
type: "thinking_delta";
|
|
380
|
+
contentIndex: number;
|
|
381
|
+
delta: string;
|
|
382
|
+
partial: AssistantMessage;
|
|
383
|
+
} | {
|
|
384
|
+
type: "toolcall_start";
|
|
385
|
+
contentIndex: number;
|
|
386
|
+
id: string;
|
|
387
|
+
name: string;
|
|
388
|
+
partial: AssistantMessage;
|
|
389
|
+
} | {
|
|
390
|
+
type: "toolcall_delta";
|
|
391
|
+
contentIndex: number;
|
|
392
|
+
delta: string;
|
|
393
|
+
partial: AssistantMessage;
|
|
394
|
+
} | {
|
|
395
|
+
type: "toolcall_end";
|
|
396
|
+
contentIndex: number;
|
|
397
|
+
toolCall: ToolCallBlock;
|
|
398
|
+
partial: AssistantMessage;
|
|
399
|
+
} | {
|
|
400
|
+
type: "done";
|
|
401
|
+
reason: "stop" | "length" | "toolUse";
|
|
402
|
+
message: AssistantMessage;
|
|
403
|
+
} | {
|
|
404
|
+
type: "error";
|
|
405
|
+
reason: "aborted" | "error";
|
|
406
|
+
message: AssistantMessage;
|
|
407
|
+
};
|
|
408
|
+
export type AssistantEventType = AssistantEvent["type"];
|
|
409
|
+
/**
|
|
410
|
+
* 流契约:成功后先 `start`;块事件配对;恰好一个终止事件(done / error);
|
|
411
|
+
* 流函数不抛错(缺 key 例外:同步抛 AmaError{code:"no_api_key"})。
|
|
412
|
+
* `result()` 在终止事件后 resolve 为最终消息(error 时也 resolve,不 reject)。
|
|
413
|
+
*/
|
|
414
|
+
export interface AssistantEventStream extends AsyncIterable<AssistantEvent> {
|
|
415
|
+
result(): Promise<AssistantMessage>;
|
|
416
|
+
}
|
|
417
|
+
export interface ApiImplementation<C = unknown> {
|
|
418
|
+
readonly id: Api;
|
|
419
|
+
stream(model: Model, context: TranscriptContext, options: StreamOptions): AssistantEventStream;
|
|
420
|
+
/** 推断缺省 compat。 */
|
|
421
|
+
detectCompat?(model: Model, provider: ProviderData): C;
|
|
422
|
+
}
|
|
423
|
+
export type KeySource = "cli" | "auth-file" | "config" | "env" | "none";
|
|
424
|
+
/** 密钥只存在于 `apiKey` 字段,不进日志、会话、事件。 */
|
|
425
|
+
export interface ApiKeyResolution {
|
|
426
|
+
apiKey: string | undefined;
|
|
427
|
+
source: KeySource;
|
|
428
|
+
/** source 为 env 时的变量名;auth-file 时的文件路径。 */
|
|
429
|
+
origin?: string;
|
|
430
|
+
}
|
|
431
|
+
export type ModelLookup = {
|
|
432
|
+
ok: true;
|
|
433
|
+
model: Model;
|
|
434
|
+
provider: ProviderData;
|
|
435
|
+
} | {
|
|
436
|
+
ok: false;
|
|
437
|
+
reason: "not_found" | "ambiguous" | "provider_not_found";
|
|
438
|
+
candidates: string[];
|
|
439
|
+
};
|
|
440
|
+
export interface ProviderRegistryApi {
|
|
441
|
+
list(): readonly ProviderData[];
|
|
442
|
+
get(providerId: string): ProviderData | undefined;
|
|
443
|
+
/** 解析 `provider/model-id` 或无斜杠的模型 id(§3.4)。 */
|
|
444
|
+
findModel(ref: string): ModelLookup;
|
|
445
|
+
/** `channel`:渠道自己配了 key 时用渠道的,否则用供应商的。 */
|
|
446
|
+
resolveApiKey(providerId: string, channel?: string): Promise<ApiKeyResolution>;
|
|
447
|
+
getApi(api: Api): ApiImplementation | undefined;
|
|
448
|
+
}
|
package/dist/ai/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 模型接入层契约(设计 §3.1–§3.2、§3.5、§3.6)。[B0] 契约文件,实现归 B1 / B8。
|
|
3
|
+
*
|
|
4
|
+
* 补全与偏差(相对设计正文):
|
|
5
|
+
* - 内容块(TextBlock / ThinkingBlock / ImageBlock / ToolCallBlock)、`Usage`、`StopReason`、
|
|
6
|
+
* LLM 消息(`Message`)定义在这里而不是 agent/types.ts:依赖方向是 agent → ai,协议层
|
|
7
|
+
* 必须能拿到这些类型。agent/types.ts 原样再导出它们。
|
|
8
|
+
* - `JsonSchema`(工具参数子集)也定义在这里,因为 `ToolDecl` 要发给供应商;
|
|
9
|
+
* tools/types.ts 与 agent/schema.ts 再导出。
|
|
10
|
+
* - `TranscriptContext` = 循环交给协议的原始转录(含 `system` 节补丁消息);协议实现先调
|
|
11
|
+
* `normalizeContext()`(ai/context.ts,B1)得到 `NormalizedContext` 再拼请求体。
|
|
12
|
+
* - `GoogleCompat` / `OpenAIResponsesCompat` 只列出已知字段,B8 按需追加(走契约变更流程)。
|
|
13
|
+
* - `ProviderRegistryApi` / `ApiKeyResolution` 是 Runtime(cli/runtime.ts)需要的最小接口,
|
|
14
|
+
* B1 的 `ProviderRegistry` 类实现它。
|
|
15
|
+
* - [W3-C0] 第三波 §1.3 的五个缓存兼容开关单列为 `PromptCacheCompat`(各协议共用),只并入
|
|
16
|
+
* `ProviderCompat`(全部可选),不改各协议 compat 接口——`detectCompat` 的返回形状不变,
|
|
17
|
+
* 缺省值由 C1a 在协议层按端点推断。
|
|
18
|
+
*/
|
|
19
|
+
export {};
|