@orion-agents/orion-code 0.3.66 → 0.4.4
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 +87 -0
- package/README.md +78 -361
- package/README.zh-CN.md +71 -262
- package/bin/orion +4 -1
- package/dist/acp/session-updates.d.ts +35 -0
- package/dist/acp/session-updates.js +183 -0
- package/dist/acp/sessions.d.ts +32 -0
- package/dist/acp/sessions.js +328 -0
- package/dist/acp/stdio.d.ts +3 -0
- package/dist/acp/stdio.js +59 -0
- package/dist/cli.js +136 -185
- package/dist/commands/context-tool-command-handlers.d.ts +1 -1
- package/dist/commands/context-tool-command-handlers.js +24 -3
- package/dist/commands/core-command-handlers.js +10 -18
- package/dist/commands/diagnostic-command-handlers.js +58 -48
- package/dist/commands/diagnostic-commands.js +0 -12
- package/dist/commands/init-command.d.ts +3 -0
- package/dist/commands/init-command.js +131 -0
- package/dist/commands/model-command-handlers.d.ts +1 -2
- package/dist/commands/model-command-handlers.js +15 -100
- package/dist/commands/model-commands.js +0 -11
- package/dist/commands/registry.js +1 -19
- package/dist/commands/rsi-command.d.ts +2 -0
- package/dist/commands/rsi-command.js +180 -0
- package/dist/commands/session-command-handlers.js +28 -19
- package/dist/commands/system-commands.js +0 -1
- package/dist/commands/target-command.js +49 -6
- package/dist/commands/tool-commands.js +1 -1
- package/dist/commands/types.d.ts +4 -10
- package/dist/commands/types.js +1 -21
- package/dist/commands/workflow-command-handlers.js +13 -4
- package/dist/commands/workflow-commands.js +2 -2
- package/dist/framework/loop-budget-contract.d.ts +2 -8
- package/dist/framework/loop-budget-contract.js +0 -3
- package/dist/framework/loop-progress.d.ts +18 -0
- package/dist/framework/loop-progress.js +63 -0
- package/dist/framework/prompt.d.ts +0 -2
- package/dist/framework/prompt.js +9 -27
- package/dist/framework/query.d.ts +26 -31
- package/dist/framework/query.js +451 -292
- package/dist/framework/store.d.ts +6 -10
- package/dist/framework/store.js +2 -21
- package/dist/framework/tool-call-orchestrator.d.ts +7 -0
- package/dist/framework/tool-call-orchestrator.js +8 -3
- package/dist/framework/tool-state.d.ts +2 -0
- package/dist/framework/tool.d.ts +0 -23
- package/dist/framework/verification-command.d.ts +15 -0
- package/dist/framework/verification-command.js +32 -0
- package/dist/harness/assembler.d.ts +1 -0
- package/dist/harness/assembler.js +24 -0
- package/dist/harness/capsule.d.ts +1 -1
- package/dist/harness/capsule.js +33 -14
- package/dist/harness/context-harness.d.ts +37 -12
- package/dist/harness/context-harness.js +545 -35
- package/dist/harness/contract.js +1 -1
- package/dist/harness/drift-guard.d.ts +4 -0
- package/dist/harness/drift-guard.js +68 -16
- package/dist/harness/index.d.ts +1 -1
- package/dist/harness/intent.js +21 -0
- package/dist/harness/ledger.d.ts +7 -11
- package/dist/harness/ledger.js +50 -9
- package/dist/harness/progress-controller.d.ts +12 -7
- package/dist/harness/progress-controller.js +96 -29
- package/dist/harness/state.js +10 -1
- package/dist/harness/stop-controller.d.ts +0 -5
- package/dist/harness/stop-controller.js +11 -31
- package/dist/harness/task-checklist.d.ts +16 -0
- package/dist/harness/task-checklist.js +92 -0
- package/dist/harness/types.d.ts +78 -1
- package/dist/harness/verification.d.ts +19 -1
- package/dist/harness/verification.js +116 -29
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -3
- package/dist/print-ui/event-stream.d.ts +43 -0
- package/dist/print-ui/event-stream.js +204 -0
- package/dist/print-ui/launch.d.ts +12 -1
- package/dist/print-ui/launch.js +173 -11
- package/dist/product/environment.d.ts +1 -1
- package/dist/product/environment.js +1 -1
- package/dist/product/identity.d.ts +1 -1
- package/dist/product/identity.js +1 -1
- package/dist/product/paths.d.ts +3 -2
- package/dist/product/paths.js +6 -2
- package/dist/runtime/agent-loop.d.ts +12 -2
- package/dist/runtime/agent-loop.js +172 -29
- package/dist/runtime/agent-runtime-controller.d.ts +14 -4
- package/dist/runtime/agent-runtime-controller.js +366 -192
- package/dist/runtime/agent-runtime-protocol.d.ts +10 -18
- package/dist/runtime/agent-runtime-runner.d.ts +10 -0
- package/dist/runtime/builtin-tool-provider.js +5 -1
- package/dist/runtime/capability-receipt-journal.js +1 -0
- package/dist/runtime/durable-tool-receipt-reader.js +2 -1
- package/dist/runtime/first-party-tool-services.d.ts +1 -0
- package/dist/runtime/first-party-tool-services.js +2 -12
- package/dist/runtime/first-party-tool-universe.d.ts +1 -0
- package/dist/runtime/first-party-tool-universe.js +1 -1
- package/dist/runtime/followup-queue-store.d.ts +16 -0
- package/dist/runtime/followup-queue-store.js +75 -0
- package/dist/runtime/goal-lifecycle-v2.d.ts +16 -4
- package/dist/runtime/goal-lifecycle-v2.js +217 -39
- package/dist/runtime/goal-runtime-coordinator.d.ts +14 -2
- package/dist/runtime/goal-runtime-coordinator.js +147 -36
- package/dist/runtime/goals/presentation.js +3 -1
- package/dist/runtime/goals/types.d.ts +1 -3
- package/dist/runtime/goals/types.js +0 -3
- package/dist/runtime/loop-budget.d.ts +0 -7
- package/dist/runtime/loop-budget.js +8 -52
- package/dist/runtime/mcp/first-party-mcp-adapter.d.ts +2 -0
- package/dist/runtime/mcp/first-party-mcp-adapter.js +23 -19
- package/dist/runtime/model-coordinator.js +2 -5
- package/dist/runtime/orion-runtime-v1.d.ts +18 -2
- package/dist/runtime/orion-runtime-v1.js +183 -46
- package/dist/runtime/orion-session-runner.d.ts +7 -0
- package/dist/runtime/orion-session-runner.js +82 -0
- package/dist/runtime/plan-review.d.ts +1 -0
- package/dist/runtime/plan-review.js +8 -4
- package/dist/runtime/product-bootstrap.d.ts +6 -0
- package/dist/runtime/product-bootstrap.js +41 -51
- package/dist/runtime/product-orion-runtime.js +129 -58
- package/dist/runtime/release-receipts.d.ts +24 -133
- package/dist/runtime/release-receipts.js +129 -693
- package/dist/runtime/rich-text/layout.js +108 -65
- package/dist/runtime/rsi/contracts.d.ts +50 -0
- package/dist/runtime/rsi/contracts.js +17 -0
- package/dist/runtime/rsi/diagnosis.d.ts +46 -0
- package/dist/runtime/rsi/diagnosis.js +98 -0
- package/dist/runtime/rsi/evaluation-suite.d.ts +25 -0
- package/dist/runtime/rsi/evaluation-suite.js +141 -0
- package/dist/runtime/rsi/evaluation-worker.d.ts +19 -0
- package/dist/runtime/rsi/evaluation-worker.js +64 -0
- package/dist/runtime/rsi/evaluation.d.ts +49 -0
- package/dist/runtime/rsi/evaluation.js +345 -0
- package/dist/runtime/rsi/lifecycle.d.ts +54 -0
- package/dist/runtime/rsi/lifecycle.js +279 -0
- package/dist/runtime/rsi/proposal.d.ts +39 -0
- package/dist/runtime/rsi/proposal.js +312 -0
- package/dist/runtime/rsi/receipt.d.ts +28 -0
- package/dist/runtime/rsi/receipt.js +138 -0
- package/dist/runtime/rsi/skill-state.d.ts +7 -0
- package/dist/runtime/rsi/skill-state.js +50 -0
- package/dist/runtime/rsi/trajectory.d.ts +19 -0
- package/dist/runtime/rsi/trajectory.js +314 -0
- package/dist/runtime/rsi/triage.d.ts +274 -0
- package/dist/runtime/rsi/triage.js +58 -0
- package/dist/runtime/session-composer-control.d.ts +5 -24
- package/dist/runtime/session-composer-control.js +27 -189
- package/dist/runtime/step-snapshot.d.ts +4 -1
- package/dist/runtime/step-snapshot.js +11 -1
- package/dist/runtime/subagent-thread-runtime.js +27 -5
- package/dist/runtime/subagents/policy.js +0 -1
- package/dist/runtime/subagents/production.js +1 -5
- package/dist/runtime/subagents/runtime-integration.js +2 -4
- package/dist/runtime/subagents/types.d.ts +0 -5
- package/dist/runtime/subagents/types.js +0 -2
- package/dist/runtime/task-context-recovery.d.ts +55 -0
- package/dist/runtime/task-context-recovery.js +349 -0
- package/dist/runtime/task-context-service.d.ts +20 -10
- package/dist/runtime/task-context-service.js +69 -9
- package/dist/runtime/thread-admission.d.ts +3 -1
- package/dist/runtime/thread-admission.js +21 -7
- package/dist/runtime/thread-event-store.d.ts +2 -2
- package/dist/runtime/thread-event-store.js +7 -2
- package/dist/runtime/thread-projection.js +4 -2
- package/dist/runtime/thread-runtime.d.ts +10 -0
- package/dist/runtime/thread-runtime.js +85 -3
- package/dist/runtime/thread-session-index.d.ts +8 -0
- package/dist/runtime/thread-session-index.js +50 -1
- package/dist/runtime/thread-session-view.d.ts +2 -1
- package/dist/runtime/thread-session-view.js +4 -0
- package/dist/runtime/thread-tool-journal.d.ts +1 -0
- package/dist/runtime/thread-tool-journal.js +6 -2
- package/dist/runtime/thread-ui-adapter.d.ts +1 -0
- package/dist/runtime/thread-ui-adapter.js +89 -22
- package/dist/runtime/tool-gateway.d.ts +9 -0
- package/dist/runtime/tool-gateway.js +24 -0
- package/dist/runtime/tool-receipt-validator.js +4 -0
- package/dist/runtime/tool-transcript-entry.d.ts +16 -0
- package/dist/runtime/tool-transcript-entry.js +73 -0
- package/dist/runtime/turn-commit.d.ts +4 -1
- package/dist/runtime/turn-commit.js +19 -15
- package/dist/runtime/ui-events.d.ts +15 -1
- package/dist/runtime/ui-view-model.d.ts +6 -5
- package/dist/runtime/ui-view-model.js +78 -61
- package/dist/runtime/workspace-runtime-kernel.js +0 -1
- package/dist/runtime/workspace-verification.d.ts +7 -0
- package/dist/runtime/workspace-verification.js +83 -0
- package/dist/services/atomic-write.d.ts +2 -0
- package/dist/services/atomic-write.js +12 -1
- package/dist/services/code-review/format.d.ts +4 -1
- package/dist/services/code-review/format.js +5 -1
- package/dist/services/code-review/runner.d.ts +2 -2
- package/dist/services/code-review/runner.js +86 -64
- package/dist/services/code-review/snapshot.d.ts +9 -9
- package/dist/services/code-review/snapshot.js +182 -166
- package/dist/services/compact/auto-compact.d.ts +21 -6
- package/dist/services/compact/auto-compact.js +148 -22
- package/dist/services/compact/background-candidate.d.ts +33 -0
- package/dist/services/compact/background-candidate.js +184 -0
- package/dist/services/compact/compact.d.ts +4 -1
- package/dist/services/compact/compact.js +30 -3
- package/dist/services/compact/coordinator.d.ts +10 -4
- package/dist/services/compact/coordinator.js +17 -7
- package/dist/services/compact/summary-generator.d.ts +4 -2
- package/dist/services/compact/summary-generator.js +17 -3
- package/dist/services/config.d.ts +5 -5
- package/dist/services/config.js +29 -48
- package/dist/services/doctor.js +85 -13
- package/dist/services/effort.d.ts +5 -5
- package/dist/services/effort.js +5 -11
- package/dist/services/file-lock.d.ts +9 -3
- package/dist/services/file-lock.js +92 -26
- package/dist/services/global-config.d.ts +26 -64
- package/dist/services/global-config.js +233 -193
- package/dist/services/llm.d.ts +42 -4
- package/dist/services/llm.js +177 -45
- package/dist/services/model-client-pool.d.ts +15 -9
- package/dist/services/model-client-pool.js +62 -33
- package/dist/services/model-context.d.ts +2 -1
- package/dist/services/model-context.js +15 -9
- package/dist/services/model-registry.d.ts +4 -15
- package/dist/services/model-registry.js +32 -101
- package/dist/services/provider-diagnostics.d.ts +8 -0
- package/dist/services/provider-diagnostics.js +17 -12
- package/dist/services/provider-resilience/coordinator.d.ts +1 -1
- package/dist/services/provider-resilience/coordinator.js +10 -6
- package/dist/services/provider-resilience/request-gate.d.ts +3 -0
- package/dist/services/provider-resilience/request-gate.js +29 -18
- package/dist/services/provider-resilience/types.d.ts +2 -0
- package/dist/services/session-storage.d.ts +7 -10
- package/dist/services/session-storage.js +57 -30
- package/dist/services/settings-coordinator.d.ts +0 -6
- package/dist/services/settings-coordinator.js +19 -56
- package/dist/services/settings-document-repository.d.ts +3 -10
- package/dist/services/settings-document-repository.js +7 -85
- package/dist/services/usage-state.d.ts +3 -3
- package/dist/services/usage-state.js +11 -42
- package/dist/terminal-ui/answer-format.d.ts +14 -0
- package/dist/terminal-ui/answer-format.js +119 -0
- package/dist/terminal-ui/launch.d.ts +7 -6
- package/dist/terminal-ui/launch.js +160 -70
- package/dist/terminal-ui/output-queue.d.ts +2 -1
- package/dist/terminal-ui/output-queue.js +1 -1
- package/dist/terminal-ui/raw-editor.d.ts +2 -1
- package/dist/terminal-ui/raw-editor.js +56 -15
- package/dist/tools/core/edit-file.js +6 -2
- package/dist/tools/core/write-file.js +9 -2
- package/dist/tools/lsp.d.ts +16 -1
- package/dist/tools/lsp.js +60 -19
- package/dist/tools/sandbox.d.ts +1 -1
- package/dist/tools/sandbox.js +3 -16
- package/dist/tools/todo.d.ts +4 -2
- package/dist/tools/todo.js +91 -30
- package/dist/tui-core/input-parser.d.ts +1 -0
- package/dist/tui-core/input-parser.js +6 -0
- package/dist/tui-core/style.d.ts +1 -8
- package/dist/tui-core/style.js +3 -110
- package/dist/tui-ui/chrome-view-model.d.ts +1 -1
- package/dist/tui-ui/chrome-view-model.js +36 -32
- package/dist/tui-ui/launch.js +14 -14
- package/dist/tui-ui/layout.d.ts +5 -7
- package/dist/tui-ui/layout.js +129 -124
- package/dist/tui-ui/runner.d.ts +11 -12
- package/dist/tui-ui/runner.js +88 -67
- package/dist/tui-ui/state.d.ts +0 -11
- package/dist/tui-ui/state.js +22 -33
- package/dist/tui-ui/terminal-image.js +22 -4
- package/dist/tui-ui/transcript-inspector-layout.js +6 -3
- package/dist/tui-ui/transcript-inspector.d.ts +1 -0
- package/dist/tui-ui/transcript-inspector.js +7 -1
- package/dist/tui-ui/transcript-layout.js +42 -6
- package/dist/utils/terminal-text.d.ts +11 -0
- package/dist/utils/terminal-text.js +114 -0
- package/docs/cli-guide.md +575 -0
- package/docs/orion.example.yaml +43 -0
- package/docs/readme.md +12 -57
- package/npm-shrinkwrap.json +484 -1855
- package/package.json +13 -63
- package/dist/migration/command.d.ts +0 -9
- package/dist/migration/command.js +0 -100
- package/dist/migration/index.d.ts +0 -7
- package/dist/migration/index.js +0 -14
- package/dist/migration/migrate.d.ts +0 -19
- package/dist/migration/migrate.js +0 -406
- package/dist/migration/types.d.ts +0 -55
- package/dist/migration/types.js +0 -6
- package/dist/services/tool-allowlist.d.ts +0 -110
- package/dist/services/tool-allowlist.js +0 -299
- package/dist/services/yaml-config.d.ts +0 -57
- package/dist/services/yaml-config.js +0 -178
- package/dist/web/cli-options.d.ts +0 -17
- package/dist/web/cli-options.js +0 -51
- package/dist/web/code-review-run-service.d.ts +0 -68
- package/dist/web/code-review-run-service.js +0 -228
- package/dist/web/context-text.d.ts +0 -17
- package/dist/web/context-text.js +0 -40
- package/dist/web/errors.d.ts +0 -6
- package/dist/web/errors.js +0 -13
- package/dist/web/event-hub.d.ts +0 -45
- package/dist/web/event-hub.js +0 -370
- package/dist/web/file-read-service.d.ts +0 -101
- package/dist/web/file-read-service.js +0 -593
- package/dist/web/file-write-service.d.ts +0 -16
- package/dist/web/file-write-service.js +0 -103
- package/dist/web/git-compare-service.d.ts +0 -45
- package/dist/web/git-compare-service.js +0 -160
- package/dist/web/git-diff-document.d.ts +0 -102
- package/dist/web/git-diff-document.js +0 -258
- package/dist/web/git-file-source.d.ts +0 -25
- package/dist/web/git-file-source.js +0 -21
- package/dist/web/git-history-service.d.ts +0 -151
- package/dist/web/git-history-service.js +0 -619
- package/dist/web/git-index-transaction.d.ts +0 -45
- package/dist/web/git-index-transaction.js +0 -174
- package/dist/web/git-patch-builder.d.ts +0 -30
- package/dist/web/git-patch-builder.js +0 -138
- package/dist/web/git-read-model-service.d.ts +0 -452
- package/dist/web/git-read-model-service.js +0 -1833
- package/dist/web/git-version-service.d.ts +0 -79
- package/dist/web/git-version-service.js +0 -279
- package/dist/web/host-daemon.d.ts +0 -70
- package/dist/web/host-daemon.js +0 -237
- package/dist/web/index.d.ts +0 -6
- package/dist/web/index.js +0 -28
- package/dist/web/launch.d.ts +0 -12
- package/dist/web/launch.js +0 -89
- package/dist/web/native-directory-picker.d.ts +0 -63
- package/dist/web/native-directory-picker.js +0 -169
- package/dist/web/protocol.d.ts +0 -705
- package/dist/web/protocol.js +0 -639
- package/dist/web/review-service.d.ts +0 -22
- package/dist/web/review-service.js +0 -68
- package/dist/web/server.d.ts +0 -20
- package/dist/web/server.js +0 -1453
- package/dist/web/session-runtime-registry.d.ts +0 -138
- package/dist/web/session-runtime-registry.js +0 -471
- package/dist/web/terminal-manager.d.ts +0 -156
- package/dist/web/terminal-manager.js +0 -779
- package/dist/web/terminal-server.d.ts +0 -8
- package/dist/web/terminal-server.js +0 -304
- package/dist/web/workbench-controller.d.ts +0 -395
- package/dist/web/workbench-controller.js +0 -3077
- package/dist/web/workspace-discovery.d.ts +0 -92
- package/dist/web/workspace-discovery.js +0 -324
- package/dist/web/workspace-mutation-arbiter.d.ts +0 -54
- package/dist/web/workspace-mutation-arbiter.js +0 -267
- package/dist/web/workspace-open-telemetry.d.ts +0 -55
- package/dist/web/workspace-open-telemetry.js +0 -73
- package/dist/web-client/assets/FilesPanel-CPDovK_K.js +0 -1
- package/dist/web-client/assets/GitPanel-DtU_TD61.js +0 -4
- package/dist/web-client/assets/ReviewPanel-vMO7BEA3.js +0 -6
- package/dist/web-client/assets/TerminalPanel-C9vDTAlS.js +0 -23
- package/dist/web-client/assets/TerminalPanel-DLuoa74B.css +0 -1
- package/dist/web-client/assets/index-CABdwh4l.css +0 -1
- package/dist/web-client/assets/index-KvshfEbl.js +0 -20
- package/dist/web-client/assets/useAutoNotice-IE1g2wcb.js +0 -1
- package/dist/web-client/favicon.svg +0 -9
- package/dist/web-client/index.html +0 -17
- package/docs/architecture/agent-mode-permission-contract.md +0 -87
- package/docs/architecture/v0.3.0-web-api.yaml +0 -1340
- package/docs/architecture/v0.3.1-web-api.yaml +0 -2338
- package/docs/architecture/v0.3.2-web-api.yaml +0 -3029
- package/docs/architecture/v0.3.3-web-api.yaml +0 -3205
- package/docs/architecture/v0.3.4-stabilization-contract.md +0 -32
- package/docs/migration/v0.1.9-to-v0.2.0.md +0 -98
- package/docs/migration/v0.2.2-to-v0.3.0-settings.md +0 -114
- package/docs/migration/v0.2.2-to-v0.3.0.md +0 -55
- package/docs/migration/v0.3.0-to-v0.3.1.md +0 -88
- package/docs/migration/v0.3.1-to-v0.3.2.md +0 -94
- package/docs/migration/v0.3.2-to-v0.3.3.md +0 -62
- package/docs/migration/v0.3.3-to-v0.3.4.md +0 -36
- package/docs/orion.example.json +0 -86
- package/docs/plan/v0.2.0-dsh-harness-redesign-plan.md +0 -1234
- package/docs/plan/v0.2.0-release-checklist.md +0 -91
- package/docs/plan/v0.3.0-node-runtime-compatibility-plan.md +0 -57
- package/docs/plan/v0.3.0-settings-integration-plan.md +0 -740
- package/docs/plan/v0.3.0-web-workbench-plan.md +0 -376
- package/docs/plan/v0.3.1-web-workbench-professional-shell-plan.md +0 -848
- package/docs/plan/v0.3.2-web-workbench-layout-and-composer-plan.md +0 -869
- package/docs/plan/v0.3.3-plan.md +0 -1113
- package/docs/plan/v0.3.4-stabilization-plan.md +0 -89
- package/docs/test/v0.3.1-web-workbench-e2e-plan.md +0 -140
- package/docs/test/v0.3.2-web-workbench-e2e-plan.md +0 -100
- package/docs/test/v0.3.3-web-workbench-e2e-plan.md +0 -79
- package/docs/test/v0.3.4-stabilization-e2e-plan.md +0 -54
package/dist/services/llm.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type { ProviderErrorType } from './provider-diagnostics';
|
|
|
11
11
|
import { type ProviderRequestDiagnosticsV2 } from './provider-resilience';
|
|
12
12
|
import { type EffortPreference, type ReasoningCapability, type ResolvedEffort } from './effort';
|
|
13
13
|
import type { ProviderProtocol } from './model-registry';
|
|
14
|
+
import type { ProviderRequestGate } from './provider-resilience/request-gate';
|
|
14
15
|
export { LLMProviderError } from './provider-diagnostics';
|
|
15
16
|
export type { ProviderErrorDiagnostic, ProviderErrorType } from './provider-diagnostics';
|
|
16
17
|
/** LLM 配置 — 用户只需关注 3 项 */
|
|
@@ -29,6 +30,17 @@ export interface LLMConfig {
|
|
|
29
30
|
reasoningCapability?: ReasoningCapability;
|
|
30
31
|
fallbackReasoningCapability?: ReasoningCapability;
|
|
31
32
|
effortPreference?: EffortPreference;
|
|
33
|
+
maxOutputTokens?: number;
|
|
34
|
+
contextWindow?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface LLMModelRoute {
|
|
37
|
+
model: string;
|
|
38
|
+
providerKey: string;
|
|
39
|
+
client: OpenAI;
|
|
40
|
+
contextWindow: number;
|
|
41
|
+
maxOutputTokens: number;
|
|
42
|
+
protocol: ProviderProtocol;
|
|
43
|
+
reasoningCapability?: ReasoningCapability;
|
|
32
44
|
}
|
|
33
45
|
/** 重试配置 */
|
|
34
46
|
export interface RetryConfig {
|
|
@@ -54,7 +66,10 @@ export interface LLMRequestDiagnostics {
|
|
|
54
66
|
fallbackTriggered: boolean;
|
|
55
67
|
fallbackFromModel?: string;
|
|
56
68
|
fallbackToModel?: string;
|
|
69
|
+
fallbackFromProvider?: string;
|
|
70
|
+
fallbackToProvider?: string;
|
|
57
71
|
finalModel: string;
|
|
72
|
+
finalProvider?: string;
|
|
58
73
|
usingFallback: boolean;
|
|
59
74
|
recoveryCount: number;
|
|
60
75
|
unknownBilledAttemptCount: number;
|
|
@@ -125,7 +140,11 @@ export interface LLMUsage {
|
|
|
125
140
|
export interface LLMUsageEvent {
|
|
126
141
|
usage: LLMUsage;
|
|
127
142
|
model: string;
|
|
128
|
-
operation: 'chat' | 'chat_stream';
|
|
143
|
+
operation: 'chat' | 'chat_stream' | 'compact_summary';
|
|
144
|
+
}
|
|
145
|
+
export interface CompactSummaryTransport {
|
|
146
|
+
readonly revision: number;
|
|
147
|
+
readonly llm: Pick<LLMService, 'chat'>;
|
|
129
148
|
}
|
|
130
149
|
export interface LLMResponse {
|
|
131
150
|
/** 回复内容 */
|
|
@@ -138,6 +157,8 @@ export interface LLMResponse {
|
|
|
138
157
|
finishReason?: LLMFinishReason;
|
|
139
158
|
/** Exact non-empty provider finish_reason for diagnostics and compatibility. */
|
|
140
159
|
rawFinishReason?: string;
|
|
160
|
+
/** Explicit provider signal about ending the turn, when supplied. */
|
|
161
|
+
endTurn?: boolean;
|
|
141
162
|
/** 工具调用 */
|
|
142
163
|
toolCalls?: Array<{
|
|
143
164
|
id: string;
|
|
@@ -158,6 +179,11 @@ export interface StreamCallbacks {
|
|
|
158
179
|
onChunk?: StreamCallback;
|
|
159
180
|
/** 思考提示回调(流式开始前) */
|
|
160
181
|
onThinking?: () => void;
|
|
182
|
+
/**
|
|
183
|
+
* 推理文本回调 — 承载 `delta.reasoning_content`(DeepSeek/Qwen/GLM/Kimi 等
|
|
184
|
+
* 推理模型的思考通道惯例)。每收到一段思考文本触发一次。
|
|
185
|
+
*/
|
|
186
|
+
onThinkingText?: StreamCallback;
|
|
161
187
|
}
|
|
162
188
|
export interface StreamOptions {
|
|
163
189
|
abortSignal?: AbortSignal;
|
|
@@ -178,6 +204,10 @@ export declare class ProviderRequestPreflightError extends Error {
|
|
|
178
204
|
}
|
|
179
205
|
export declare class LLMService {
|
|
180
206
|
private client;
|
|
207
|
+
private providerKey;
|
|
208
|
+
private contextWindow?;
|
|
209
|
+
private fallbackRoute?;
|
|
210
|
+
private compactTransportRevision;
|
|
181
211
|
private config;
|
|
182
212
|
private consecutive529Errors;
|
|
183
213
|
private usingFallback;
|
|
@@ -203,7 +233,9 @@ export declare class LLMService {
|
|
|
203
233
|
/**
|
|
204
234
|
* 非流式对话
|
|
205
235
|
*/
|
|
206
|
-
chat(messages: Message[], tools?: Tool[]
|
|
236
|
+
chat(messages: Message[], tools?: Tool[], options?: {
|
|
237
|
+
abortSignal?: AbortSignal;
|
|
238
|
+
}): Promise<LLMResponse>;
|
|
207
239
|
/**
|
|
208
240
|
* 流式对话(带重试)
|
|
209
241
|
*/
|
|
@@ -219,6 +251,11 @@ export declare class LLMService {
|
|
|
219
251
|
setModel(model: string): void;
|
|
220
252
|
/** Replace the active provider transport after a validated profile switch. */
|
|
221
253
|
setProviderClient(client: OpenAI): void;
|
|
254
|
+
/** Apply a resolved registry profile and its fallback for the next logical request. */
|
|
255
|
+
setModelRoutes(primary: LLMModelRoute, fallback: LLMModelRoute | undefined, preference: EffortPreference): void;
|
|
256
|
+
/** Capture the current model and auth transport for one opportunistic summary.
|
|
257
|
+
* This path has its own timing, no model retry or fallback, and no turn budget. */
|
|
258
|
+
captureCompactSummaryTransport(gate: ProviderRequestGate): CompactSummaryTransport;
|
|
222
259
|
setEffortContext(input: {
|
|
223
260
|
preference: EffortPreference;
|
|
224
261
|
protocol: ProviderProtocol;
|
|
@@ -232,12 +269,13 @@ export declare class LLMService {
|
|
|
232
269
|
getModel(): string;
|
|
233
270
|
/** Maximum completion tokens requested from the provider for each call. */
|
|
234
271
|
getMaxTokens(): number;
|
|
272
|
+
getContextWindow(): number | undefined;
|
|
235
273
|
/**
|
|
236
274
|
* 获取当前配置摘要
|
|
237
275
|
*/
|
|
238
276
|
getConfigSummary(): Record<string, string>;
|
|
239
|
-
private
|
|
240
|
-
private
|
|
277
|
+
private createModelRequestSnapshot;
|
|
278
|
+
private requestRoute;
|
|
241
279
|
private annotateEffortUsage;
|
|
242
280
|
private createRequestDiagnostics;
|
|
243
281
|
private applyResilienceDiagnostics;
|
package/dist/services/llm.js
CHANGED
|
@@ -236,8 +236,16 @@ function extractLLMUsage(usage, response, requestId) {
|
|
|
236
236
|
requestId: requestId || (0, crypto_1.randomUUID)(),
|
|
237
237
|
};
|
|
238
238
|
}
|
|
239
|
+
function readEndTurn(response) {
|
|
240
|
+
const choiceEndTurn = response.choices?.[0]?.end_turn;
|
|
241
|
+
if (typeof choiceEndTurn === 'boolean')
|
|
242
|
+
return choiceEndTurn;
|
|
243
|
+
return typeof response.end_turn === 'boolean' ? response.end_turn : undefined;
|
|
244
|
+
}
|
|
239
245
|
class LLMService {
|
|
240
246
|
constructor(config) {
|
|
247
|
+
this.providerKey = 'default';
|
|
248
|
+
this.compactTransportRevision = 0;
|
|
241
249
|
this.consecutive529Errors = 0;
|
|
242
250
|
this.usingFallback = false;
|
|
243
251
|
this.usageObservers = new Set();
|
|
@@ -250,10 +258,11 @@ class LLMService {
|
|
|
250
258
|
dangerouslyAllowBrowser: true,
|
|
251
259
|
});
|
|
252
260
|
// Agent 内部控制参数,不由用户配置
|
|
261
|
+
this.contextWindow = config.contextWindow;
|
|
253
262
|
this.config = {
|
|
254
263
|
model: config.model,
|
|
255
264
|
fallbackModel: config.fallbackModel ?? '',
|
|
256
|
-
maxTokens: DEFAULT_MAX_TOKENS,
|
|
265
|
+
maxTokens: config.maxOutputTokens ?? DEFAULT_MAX_TOKENS,
|
|
257
266
|
temperature: DEFAULT_TEMPERATURE,
|
|
258
267
|
timeout: config.timeout ?? 60000,
|
|
259
268
|
maxRetries: DEFAULT_MAX_RETRIES,
|
|
@@ -322,10 +331,12 @@ class LLMService {
|
|
|
322
331
|
/**
|
|
323
332
|
* 非流式对话
|
|
324
333
|
*/
|
|
325
|
-
async chat(messages, tools) {
|
|
334
|
+
async chat(messages, tools, options) {
|
|
335
|
+
throwIfAborted(options?.abortSignal);
|
|
326
336
|
const requestDiagnostics = this.createRequestDiagnostics();
|
|
327
337
|
this.lastRequestDiagnostics = requestDiagnostics;
|
|
328
|
-
const
|
|
338
|
+
const modelSnapshot = this.createModelRequestSnapshot();
|
|
339
|
+
let effort = modelSnapshot.primary.effort;
|
|
329
340
|
// v0.2.25: Use resilience coordinator when available.
|
|
330
341
|
let response;
|
|
331
342
|
try {
|
|
@@ -333,22 +344,27 @@ class LLMService {
|
|
|
333
344
|
const result = await this.resilience.execute({
|
|
334
345
|
logicalRequestId: `chat-${Date.now()}`,
|
|
335
346
|
operation: 'root_chat',
|
|
336
|
-
providerKey:
|
|
337
|
-
requestedModel:
|
|
338
|
-
fallbackModel:
|
|
339
|
-
|
|
347
|
+
providerKey: modelSnapshot.primary.providerKey,
|
|
348
|
+
requestedModel: modelSnapshot.primary.model,
|
|
349
|
+
fallbackModel: modelSnapshot.fallback?.model,
|
|
350
|
+
fallbackProviderKey: modelSnapshot.fallback?.providerKey,
|
|
351
|
+
abortSignal: options?.abortSignal,
|
|
352
|
+
}, async (attempt, signal, model = modelSnapshot.primary.model, _reporter, providerKey) => {
|
|
353
|
+
throwIfAborted(signal);
|
|
340
354
|
await this.assertProviderRequestAllowed('chat', attempt, messages, model);
|
|
355
|
+
const route = this.requestRoute(modelSnapshot, model, providerKey);
|
|
356
|
+
effort = route.effort;
|
|
341
357
|
const params = {
|
|
342
358
|
model,
|
|
343
359
|
messages: this.toOpenAIMessages(messages),
|
|
344
|
-
max_tokens:
|
|
360
|
+
max_tokens: route.maxOutputTokens,
|
|
345
361
|
temperature: this.config.temperature,
|
|
346
|
-
...(0, effort_1.buildProviderEffortParams)(
|
|
362
|
+
...(0, effort_1.buildProviderEffortParams)(effort),
|
|
347
363
|
};
|
|
348
364
|
if (tools && tools.length > 0)
|
|
349
365
|
params.tools = tools;
|
|
350
366
|
return {
|
|
351
|
-
response: await
|
|
367
|
+
response: await route.client.chat.completions.create(params, signal ? { signal } : undefined),
|
|
352
368
|
};
|
|
353
369
|
});
|
|
354
370
|
response = result.result;
|
|
@@ -361,11 +377,12 @@ class LLMService {
|
|
|
361
377
|
messages: this.toOpenAIMessages(messages),
|
|
362
378
|
max_tokens: this.config.maxTokens,
|
|
363
379
|
temperature: this.config.temperature,
|
|
364
|
-
...(0, effort_1.buildProviderEffortParams)(
|
|
380
|
+
...(0, effort_1.buildProviderEffortParams)(effort),
|
|
365
381
|
};
|
|
366
382
|
if (tools && tools.length > 0)
|
|
367
383
|
params.tools = tools;
|
|
368
|
-
|
|
384
|
+
const signal = options?.abortSignal;
|
|
385
|
+
response = (await this.client.chat.completions.create(params, signal ? { signal } : undefined));
|
|
369
386
|
}
|
|
370
387
|
}
|
|
371
388
|
catch (error) {
|
|
@@ -400,10 +417,10 @@ class LLMService {
|
|
|
400
417
|
const finishReason = normalizeFinishReason(providerFinishReason) ??
|
|
401
418
|
(toolCalls && toolCalls.length > 0 ? 'tool_calls' : undefined);
|
|
402
419
|
const usage = response.usage
|
|
403
|
-
? this.annotateEffortUsage(extractLLMUsage(response.usage, response, response.id),
|
|
420
|
+
? this.annotateEffortUsage(extractLLMUsage(response.usage, response, response.id), effort)
|
|
404
421
|
: undefined;
|
|
405
422
|
requestDiagnostics.finalModel = response.model ?? this.config.model;
|
|
406
|
-
requestDiagnostics.usingFallback = this.usingFallback;
|
|
423
|
+
requestDiagnostics.usingFallback || (requestDiagnostics.usingFallback = this.usingFallback);
|
|
407
424
|
this.lastRequestDiagnostics = {
|
|
408
425
|
...requestDiagnostics,
|
|
409
426
|
retryErrorTypes: [...requestDiagnostics.retryErrorTypes],
|
|
@@ -416,6 +433,7 @@ class LLMService {
|
|
|
416
433
|
model: response.model ?? this.config.model,
|
|
417
434
|
finishReason,
|
|
418
435
|
rawFinishReason: providerFinishReason,
|
|
436
|
+
endTurn: readEndTurn(response),
|
|
419
437
|
toolCalls,
|
|
420
438
|
};
|
|
421
439
|
}
|
|
@@ -425,9 +443,10 @@ class LLMService {
|
|
|
425
443
|
async chatStream(messages, callbacks, tools, options) {
|
|
426
444
|
const onChunk = typeof callbacks === 'function' ? callbacks : callbacks?.onChunk;
|
|
427
445
|
const onThinking = typeof callbacks === 'object' ? callbacks?.onThinking : undefined;
|
|
446
|
+
const onThinkingText = typeof callbacks === 'object' ? callbacks?.onThinkingText : undefined;
|
|
428
447
|
const requestDiagnostics = this.createRequestDiagnostics();
|
|
429
448
|
this.lastRequestDiagnostics = requestDiagnostics;
|
|
430
|
-
const
|
|
449
|
+
const modelSnapshot = this.createModelRequestSnapshot();
|
|
431
450
|
let visibleStreamText = '';
|
|
432
451
|
const emitStreamText = (text, bufferForRecovery, reporter) => {
|
|
433
452
|
if (!text)
|
|
@@ -490,34 +509,37 @@ class LLMService {
|
|
|
490
509
|
const streamResult = await this.resilience.execute({
|
|
491
510
|
logicalRequestId: `stream-${Date.now()}`,
|
|
492
511
|
operation: 'root_chat_stream',
|
|
493
|
-
providerKey:
|
|
494
|
-
requestedModel:
|
|
495
|
-
fallbackModel:
|
|
512
|
+
providerKey: modelSnapshot.primary.providerKey,
|
|
513
|
+
requestedModel: modelSnapshot.primary.model,
|
|
514
|
+
fallbackModel: modelSnapshot.fallback?.model,
|
|
515
|
+
fallbackProviderKey: modelSnapshot.fallback?.providerKey,
|
|
496
516
|
abortSignal: options?.abortSignal,
|
|
497
|
-
}, async (attempt, signal, model =
|
|
517
|
+
}, async (attempt, signal, model = modelSnapshot.primary.model, reporter, providerKey) => {
|
|
498
518
|
throwIfAborted(signal);
|
|
499
519
|
await this.assertProviderRequestAllowed('chat_stream', attempt, messages, model);
|
|
520
|
+
const route = this.requestRoute(modelSnapshot, model, providerKey);
|
|
500
521
|
const params = {
|
|
501
522
|
model,
|
|
502
523
|
messages: this.toOpenAIMessages(messages),
|
|
503
|
-
max_tokens:
|
|
524
|
+
max_tokens: route.maxOutputTokens,
|
|
504
525
|
temperature: this.config.temperature,
|
|
505
526
|
stream: true,
|
|
506
527
|
stream_options: { include_usage: true },
|
|
507
|
-
...(0, effort_1.buildProviderEffortParams)(
|
|
528
|
+
...(0, effort_1.buildProviderEffortParams)(route.effort),
|
|
508
529
|
};
|
|
509
530
|
if (tools && tools.length > 0) {
|
|
510
531
|
params.tools = tools;
|
|
511
532
|
}
|
|
512
533
|
onThinking?.();
|
|
513
534
|
const requestOptions = signal ? { signal } : undefined;
|
|
514
|
-
const stream = (await
|
|
535
|
+
const stream = (await route.client.chat.completions.create(params, requestOptions));
|
|
515
536
|
let content = '';
|
|
516
537
|
let usedModel = model;
|
|
517
538
|
let usage;
|
|
518
539
|
let providerRequestId;
|
|
519
540
|
let providerFinishReason;
|
|
520
541
|
let finishReason;
|
|
542
|
+
let endTurn;
|
|
521
543
|
const bufferForRecovery = visibleStreamText.length > 0;
|
|
522
544
|
const toolCallsMap = new Map();
|
|
523
545
|
for await (const chunk of stream) {
|
|
@@ -537,6 +559,10 @@ class LLMService {
|
|
|
537
559
|
content += text;
|
|
538
560
|
emitStreamText(text, bufferForRecovery, reporter);
|
|
539
561
|
}
|
|
562
|
+
const thinkingDelta = delta?.reasoning_content;
|
|
563
|
+
if (thinkingDelta) {
|
|
564
|
+
onThinkingText?.(thinkingDelta);
|
|
565
|
+
}
|
|
540
566
|
if ((delta?.tool_calls?.length ?? 0) > 0 || (msg?.tool_calls?.length ?? 0) > 0) {
|
|
541
567
|
reporter?.onToolCallDelta();
|
|
542
568
|
}
|
|
@@ -581,13 +607,14 @@ class LLMService {
|
|
|
581
607
|
}
|
|
582
608
|
}
|
|
583
609
|
if (chunk.usage) {
|
|
584
|
-
usage = this.annotateEffortUsage(extractLLMUsage(chunk.usage, chunk, chunk.id ?? providerRequestId),
|
|
610
|
+
usage = this.annotateEffortUsage(extractLLMUsage(chunk.usage, chunk, chunk.id ?? providerRequestId), route.effort);
|
|
585
611
|
}
|
|
586
612
|
if (chunk.id)
|
|
587
613
|
providerRequestId = chunk.id;
|
|
588
614
|
if (chunk.model) {
|
|
589
615
|
usedModel = chunk.model;
|
|
590
616
|
}
|
|
617
|
+
endTurn = readEndTurn(chunk) ?? endTurn;
|
|
591
618
|
const chunkFinishReason = rawFinishReason(choice?.finish_reason);
|
|
592
619
|
if (chunkFinishReason) {
|
|
593
620
|
providerFinishReason = chunkFinishReason;
|
|
@@ -623,6 +650,7 @@ class LLMService {
|
|
|
623
650
|
usage,
|
|
624
651
|
finishReason,
|
|
625
652
|
rawFinishReason: providerFinishReason,
|
|
653
|
+
endTurn,
|
|
626
654
|
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
627
655
|
},
|
|
628
656
|
usage: usage
|
|
@@ -640,7 +668,6 @@ class LLMService {
|
|
|
640
668
|
this.applyResilienceDiagnostics(requestDiagnostics, streamResult.diagnostics);
|
|
641
669
|
this.consecutive529Errors = 0;
|
|
642
670
|
requestDiagnostics.finalModel = response.model;
|
|
643
|
-
requestDiagnostics.usingFallback = this.usingFallback;
|
|
644
671
|
this.lastRequestDiagnostics = {
|
|
645
672
|
...requestDiagnostics,
|
|
646
673
|
retryErrorTypes: [...requestDiagnostics.retryErrorTypes],
|
|
@@ -652,27 +679,31 @@ class LLMService {
|
|
|
652
679
|
const response = await withRetry(async (attempt) => {
|
|
653
680
|
throwIfAborted(options?.abortSignal);
|
|
654
681
|
await this.assertProviderRequestAllowed('chat_stream', attempt, messages);
|
|
682
|
+
const route = this.usingFallback && modelSnapshot.fallback
|
|
683
|
+
? modelSnapshot.fallback
|
|
684
|
+
: this.requestRoute(modelSnapshot, this.config.model);
|
|
655
685
|
const params = {
|
|
656
686
|
model: this.config.model,
|
|
657
687
|
messages: this.toOpenAIMessages(messages),
|
|
658
|
-
max_tokens:
|
|
688
|
+
max_tokens: route.maxOutputTokens,
|
|
659
689
|
temperature: this.config.temperature,
|
|
660
690
|
stream: true,
|
|
661
691
|
stream_options: { include_usage: true },
|
|
662
|
-
...(0, effort_1.buildProviderEffortParams)(
|
|
692
|
+
...(0, effort_1.buildProviderEffortParams)(route.effort),
|
|
663
693
|
};
|
|
664
694
|
if (tools && tools.length > 0) {
|
|
665
695
|
params.tools = tools;
|
|
666
696
|
}
|
|
667
697
|
onThinking?.();
|
|
668
698
|
const requestOptions = options?.abortSignal ? { signal: options.abortSignal } : undefined;
|
|
669
|
-
const stream = (await
|
|
699
|
+
const stream = (await route.client.chat.completions.create(params, requestOptions));
|
|
670
700
|
let content = '';
|
|
671
701
|
let usedModel = this.config.model;
|
|
672
702
|
let usage;
|
|
673
703
|
let providerRequestId;
|
|
674
704
|
let providerFinishReason;
|
|
675
705
|
let finishReason;
|
|
706
|
+
let endTurn;
|
|
676
707
|
const bufferForRecovery = visibleStreamText.length > 0;
|
|
677
708
|
const toolCallsMap = new Map();
|
|
678
709
|
for await (const chunk of stream) {
|
|
@@ -692,6 +723,10 @@ class LLMService {
|
|
|
692
723
|
content += text;
|
|
693
724
|
emitStreamText(text, bufferForRecovery);
|
|
694
725
|
}
|
|
726
|
+
const thinkingDelta = delta?.reasoning_content;
|
|
727
|
+
if (thinkingDelta) {
|
|
728
|
+
onThinkingText?.(thinkingDelta);
|
|
729
|
+
}
|
|
695
730
|
// Handle tool_calls from delta (OpenAI standard streaming format).
|
|
696
731
|
// A single stream chunk may contain multiple tool call deltas.
|
|
697
732
|
for (const tc of delta?.tool_calls ?? []) {
|
|
@@ -736,13 +771,14 @@ class LLMService {
|
|
|
736
771
|
}
|
|
737
772
|
}
|
|
738
773
|
if (chunk.usage) {
|
|
739
|
-
usage = this.annotateEffortUsage(extractLLMUsage(chunk.usage, chunk, chunk.id ?? providerRequestId),
|
|
774
|
+
usage = this.annotateEffortUsage(extractLLMUsage(chunk.usage, chunk, chunk.id ?? providerRequestId), route.effort);
|
|
740
775
|
}
|
|
741
776
|
if (chunk.id)
|
|
742
777
|
providerRequestId = chunk.id;
|
|
743
778
|
if (chunk.model) {
|
|
744
779
|
usedModel = chunk.model;
|
|
745
780
|
}
|
|
781
|
+
endTurn = readEndTurn(chunk) ?? endTurn;
|
|
746
782
|
const chunkFinishReason = rawFinishReason(choice?.finish_reason);
|
|
747
783
|
if (chunkFinishReason) {
|
|
748
784
|
providerFinishReason = chunkFinishReason;
|
|
@@ -783,6 +819,7 @@ class LLMService {
|
|
|
783
819
|
usage,
|
|
784
820
|
finishReason,
|
|
785
821
|
rawFinishReason: providerFinishReason,
|
|
822
|
+
endTurn,
|
|
786
823
|
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
787
824
|
};
|
|
788
825
|
}, retryConfig);
|
|
@@ -813,7 +850,7 @@ class LLMService {
|
|
|
813
850
|
throw error;
|
|
814
851
|
if (error instanceof ProviderRequestPreflightError)
|
|
815
852
|
throw error;
|
|
816
|
-
throw (0, provider_diagnostics_1.toLLMProviderError)(error);
|
|
853
|
+
throw (0, provider_diagnostics_1.toLLMProviderError)(error instanceof provider_resilience_1.ProviderRetryExhaustedError ? (error.originalError ?? error) : error);
|
|
817
854
|
}
|
|
818
855
|
}
|
|
819
856
|
publishUsage(usage, model, operation) {
|
|
@@ -836,6 +873,7 @@ class LLMService {
|
|
|
836
873
|
const response = await this.chatStream(messages, {
|
|
837
874
|
onChunk: callbacks?.onChunk,
|
|
838
875
|
onThinking: iteration === 1 ? callbacks?.onThinking : undefined,
|
|
876
|
+
onThinkingText: callbacks?.onThinkingText,
|
|
839
877
|
}, tools);
|
|
840
878
|
const assistantMsg = {
|
|
841
879
|
role: 'assistant',
|
|
@@ -875,15 +913,83 @@ class LLMService {
|
|
|
875
913
|
* 切换模型
|
|
876
914
|
*/
|
|
877
915
|
setModel(model) {
|
|
916
|
+
if (model !== this.config.model)
|
|
917
|
+
this.compactTransportRevision++;
|
|
878
918
|
this.config.model = model;
|
|
879
919
|
}
|
|
880
920
|
/** Replace the active provider transport after a validated profile switch. */
|
|
881
921
|
setProviderClient(client) {
|
|
922
|
+
if (client !== this.client)
|
|
923
|
+
this.compactTransportRevision++;
|
|
882
924
|
this.client = client;
|
|
883
925
|
this.consecutive529Errors = 0;
|
|
884
926
|
this.usingFallback = false;
|
|
885
927
|
this.lastRequestDiagnostics = this.createRequestDiagnostics();
|
|
886
928
|
}
|
|
929
|
+
/** Apply a resolved registry profile and its fallback for the next logical request. */
|
|
930
|
+
setModelRoutes(primary, fallback, preference) {
|
|
931
|
+
this.setEffortContext({
|
|
932
|
+
preference,
|
|
933
|
+
protocol: primary.protocol,
|
|
934
|
+
capability: primary.reasoningCapability,
|
|
935
|
+
fallbackCapability: fallback?.reasoningCapability,
|
|
936
|
+
});
|
|
937
|
+
if (primary.client !== this.client ||
|
|
938
|
+
primary.model !== this.config.model ||
|
|
939
|
+
primary.contextWindow !== this.contextWindow ||
|
|
940
|
+
primary.maxOutputTokens !== this.config.maxTokens) {
|
|
941
|
+
this.compactTransportRevision++;
|
|
942
|
+
}
|
|
943
|
+
this.client = primary.client;
|
|
944
|
+
this.providerKey = primary.providerKey;
|
|
945
|
+
this.contextWindow = primary.contextWindow;
|
|
946
|
+
this.config.model = primary.model;
|
|
947
|
+
this.config.maxTokens = primary.maxOutputTokens;
|
|
948
|
+
this.config.fallbackModel = fallback?.model ?? '';
|
|
949
|
+
this.fallbackRoute = fallback;
|
|
950
|
+
this.resetToPrimary();
|
|
951
|
+
this.lastRequestDiagnostics = this.createRequestDiagnostics();
|
|
952
|
+
}
|
|
953
|
+
/** Capture the current model and auth transport for one opportunistic summary.
|
|
954
|
+
* This path has its own timing, no model retry or fallback, and no turn budget. */
|
|
955
|
+
captureCompactSummaryTransport(gate) {
|
|
956
|
+
const client = this.client;
|
|
957
|
+
const model = this.config.model;
|
|
958
|
+
const maxTokens = Math.min(this.config.maxTokens, 768);
|
|
959
|
+
const providerKey = this.providerKey;
|
|
960
|
+
const revision = this.compactTransportRevision;
|
|
961
|
+
return {
|
|
962
|
+
revision,
|
|
963
|
+
llm: {
|
|
964
|
+
chat: async (messages, _tools, options) => {
|
|
965
|
+
const signal = options?.abortSignal;
|
|
966
|
+
signal?.throwIfAborted();
|
|
967
|
+
if (revision !== this.compactTransportRevision || model !== this.config.model) {
|
|
968
|
+
throw new Error('Compact summary provider changed.');
|
|
969
|
+
}
|
|
970
|
+
const lease = gate.tryAcquireSpeculative(providerKey);
|
|
971
|
+
if (!lease)
|
|
972
|
+
throw new Error('No idle provider slot for compact summary.');
|
|
973
|
+
try {
|
|
974
|
+
const response = (await client.chat.completions.create({ model, messages: this.toOpenAIMessages(messages), max_tokens: maxTokens }, { signal, timeout: 8000, maxRetries: 0 }));
|
|
975
|
+
signal?.throwIfAborted();
|
|
976
|
+
if (revision !== this.compactTransportRevision || model !== this.config.model) {
|
|
977
|
+
throw new Error('Compact summary provider changed.');
|
|
978
|
+
}
|
|
979
|
+
const usage = response.usage
|
|
980
|
+
? extractLLMUsage(response.usage, response, response.id)
|
|
981
|
+
: undefined;
|
|
982
|
+
if (usage)
|
|
983
|
+
this.publishUsage(usage, response.model ?? model, 'compact_summary');
|
|
984
|
+
return { content: response.choices?.[0]?.message?.content ?? '', model, usage };
|
|
985
|
+
}
|
|
986
|
+
finally {
|
|
987
|
+
lease.release();
|
|
988
|
+
}
|
|
989
|
+
},
|
|
990
|
+
},
|
|
991
|
+
};
|
|
992
|
+
}
|
|
887
993
|
setEffortContext(input) {
|
|
888
994
|
const resolved = (0, effort_1.resolveEffort)({ session: input.preference, capability: input.capability });
|
|
889
995
|
if (input.preference !== 'auto' && !resolved.supported) {
|
|
@@ -927,6 +1033,9 @@ class LLMService {
|
|
|
927
1033
|
getMaxTokens() {
|
|
928
1034
|
return this.config.maxTokens;
|
|
929
1035
|
}
|
|
1036
|
+
getContextWindow() {
|
|
1037
|
+
return this.contextWindow;
|
|
1038
|
+
}
|
|
930
1039
|
/**
|
|
931
1040
|
* 获取当前配置摘要
|
|
932
1041
|
*/
|
|
@@ -941,7 +1050,7 @@ class LLMService {
|
|
|
941
1050
|
};
|
|
942
1051
|
}
|
|
943
1052
|
// ---- Internal ----
|
|
944
|
-
|
|
1053
|
+
createModelRequestSnapshot() {
|
|
945
1054
|
const preference = this.effortContext.preference;
|
|
946
1055
|
const primaryCapability = this.effortContext.capability
|
|
947
1056
|
? {
|
|
@@ -955,25 +1064,40 @@ class LLMService {
|
|
|
955
1064
|
supportedLevels: [...this.effortContext.fallbackCapability.supportedLevels],
|
|
956
1065
|
}
|
|
957
1066
|
: undefined;
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
1067
|
+
const primary = {
|
|
1068
|
+
model: this.config.model,
|
|
1069
|
+
providerKey: this.providerKey,
|
|
1070
|
+
client: this.client,
|
|
1071
|
+
maxOutputTokens: this.config.maxTokens,
|
|
1072
|
+
effort: {
|
|
962
1073
|
protocol: this.effortContext.protocol,
|
|
963
1074
|
capability: primaryCapability,
|
|
964
1075
|
resolved: (0, effort_1.resolveEffort)({ session: preference, capability: primaryCapability }),
|
|
965
1076
|
},
|
|
1077
|
+
};
|
|
1078
|
+
if (!this.config.fallbackModel)
|
|
1079
|
+
return { primary };
|
|
1080
|
+
return {
|
|
1081
|
+
primary,
|
|
966
1082
|
fallback: {
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1083
|
+
model: this.config.fallbackModel,
|
|
1084
|
+
providerKey: this.fallbackRoute?.providerKey ?? this.providerKey,
|
|
1085
|
+
client: this.fallbackRoute?.client ?? this.client,
|
|
1086
|
+
maxOutputTokens: this.fallbackRoute?.maxOutputTokens ?? this.config.maxTokens,
|
|
1087
|
+
effort: {
|
|
1088
|
+
protocol: this.fallbackRoute?.protocol ?? this.effortContext.protocol,
|
|
1089
|
+
capability: fallbackCapability,
|
|
1090
|
+
resolved: (0, effort_1.resolveEffort)({ session: preference, capability: fallbackCapability }),
|
|
1091
|
+
},
|
|
970
1092
|
},
|
|
971
1093
|
};
|
|
972
1094
|
}
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
1095
|
+
requestRoute(snapshot, model, providerKey = snapshot.primary.providerKey) {
|
|
1096
|
+
const { primary, fallback } = snapshot;
|
|
1097
|
+
return fallback?.model === model &&
|
|
1098
|
+
(model !== primary.model || providerKey !== primary.providerKey)
|
|
1099
|
+
? fallback
|
|
1100
|
+
: primary;
|
|
977
1101
|
}
|
|
978
1102
|
annotateEffortUsage(usage, snapshot) {
|
|
979
1103
|
return {
|
|
@@ -1015,15 +1139,23 @@ class LLMService {
|
|
|
1015
1139
|
target.lastRetryErrorType = providerErrorTypeForFailureKind(lastRetriedAttempt.failureKind);
|
|
1016
1140
|
target.lastRetryStatus = lastRetriedAttempt.status;
|
|
1017
1141
|
}
|
|
1142
|
+
const primary = diagnostics.attempts[0];
|
|
1143
|
+
const fallback = diagnostics.fallbackCount > 0
|
|
1144
|
+
? diagnostics.attempts.find(attempt => attempt.model !== primary?.model || attempt.providerKey !== primary?.providerKey)
|
|
1145
|
+
: undefined;
|
|
1018
1146
|
if (diagnostics.fallbackCount > 0) {
|
|
1019
1147
|
target.fallbackTriggered = true;
|
|
1020
1148
|
target.fallbackFromModel = diagnostics.requestedModel;
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
}
|
|
1149
|
+
target.fallbackToModel = fallback?.model;
|
|
1150
|
+
target.fallbackFromProvider = primary?.providerKey;
|
|
1151
|
+
target.fallbackToProvider = fallback?.providerKey;
|
|
1025
1152
|
}
|
|
1026
1153
|
target.finalModel = diagnostics.finalModel ?? target.finalModel;
|
|
1154
|
+
target.finalProvider = diagnostics.attempts.at(-1)?.providerKey;
|
|
1155
|
+
this.usingFallback =
|
|
1156
|
+
Boolean(fallback) ||
|
|
1157
|
+
(diagnostics.fallbackCount > 0 &&
|
|
1158
|
+
Boolean(diagnostics.finalModel && diagnostics.finalModel !== diagnostics.requestedModel));
|
|
1027
1159
|
target.usingFallback = this.usingFallback;
|
|
1028
1160
|
}
|
|
1029
1161
|
/** 转换为 OpenAI SDK 消息格式 */
|
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
* Slice 3 of v0.2.26 plan.
|
|
9
9
|
*/
|
|
10
10
|
import OpenAI from 'openai';
|
|
11
|
-
import type {
|
|
11
|
+
import type { EffortPreference } from './effort';
|
|
12
|
+
import type { LLMService } from './llm';
|
|
13
|
+
import type { ModelRegistry, ProviderConfig, ResolvedModelProfile } from './model-registry';
|
|
12
14
|
export interface PooledClient {
|
|
13
15
|
provider: ProviderConfig;
|
|
14
16
|
client: OpenAI;
|
|
@@ -18,6 +20,9 @@ export declare class ModelClientPool {
|
|
|
18
20
|
private clients;
|
|
19
21
|
/** Get or create an OpenAI client for a provider. */
|
|
20
22
|
getClient(provider: ProviderConfig): OpenAI;
|
|
23
|
+
/** Bind the selected profile and usable fallback as a single model selection. */
|
|
24
|
+
bindModel(llm: LLMService, registry: ModelRegistry, profile: ResolvedModelProfile, preference: EffortPreference): void;
|
|
25
|
+
private route;
|
|
21
26
|
/** Invalidate and recreate a provider's client (e.g. after key rotation). */
|
|
22
27
|
invalidate(providerId: string): void;
|
|
23
28
|
/** Get the provider config for a client. */
|
|
@@ -26,12 +31,13 @@ export declare class ModelClientPool {
|
|
|
26
31
|
clear(): void;
|
|
27
32
|
get size(): number;
|
|
28
33
|
}
|
|
29
|
-
export declare function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
export declare function resolveProviderCredential(value: string): {
|
|
35
|
+
apiKey: string;
|
|
36
|
+
source: 'environment' | 'literal';
|
|
37
|
+
error?: never;
|
|
38
|
+
} | {
|
|
39
|
+
apiKey: '';
|
|
40
|
+
source: 'environment' | 'literal';
|
|
41
|
+
error: string;
|
|
42
|
+
};
|
|
37
43
|
//# sourceMappingURL=model-client-pool.d.ts.map
|