@strands-agents/sdk 1.13.0 → 1.15.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/dist/src/agent/agent-as-tool.d.ts.map +1 -1
- package/dist/src/agent/agent-as-tool.js +8 -2
- package/dist/src/agent/agent-as-tool.js.map +1 -1
- package/dist/src/agent/agent-delegation.d.ts +9 -30
- package/dist/src/agent/agent-delegation.d.ts.map +1 -1
- package/dist/src/agent/agent-delegation.js +26 -98
- package/dist/src/agent/agent-delegation.js.map +1 -1
- package/dist/src/agent/agent.d.ts +49 -15
- package/dist/src/agent/agent.d.ts.map +1 -1
- package/dist/src/agent/agent.js +193 -60
- package/dist/src/agent/agent.js.map +1 -1
- package/dist/src/agent/continuation.d.ts +77 -0
- package/dist/src/agent/continuation.d.ts.map +1 -0
- package/dist/src/agent/continuation.js +167 -0
- package/dist/src/agent/continuation.js.map +1 -0
- package/dist/src/agent/tool-caller.d.ts.map +1 -1
- package/dist/src/agent/tool-caller.js +1 -0
- package/dist/src/agent/tool-caller.js.map +1 -1
- package/dist/src/background-tasks/errors.d.ts +8 -0
- package/dist/src/background-tasks/errors.d.ts.map +1 -0
- package/dist/src/background-tasks/errors.js +11 -0
- package/dist/src/background-tasks/errors.js.map +1 -0
- package/dist/src/background-tasks/in-process/engine.d.ts +96 -0
- package/dist/src/background-tasks/in-process/engine.d.ts.map +1 -0
- package/dist/src/background-tasks/in-process/engine.js +322 -0
- package/dist/src/background-tasks/in-process/engine.js.map +1 -0
- package/dist/src/background-tasks/in-process/types.d.ts +76 -0
- package/dist/src/background-tasks/in-process/types.d.ts.map +1 -0
- package/dist/src/background-tasks/in-process/types.js +2 -0
- package/dist/src/background-tasks/in-process/types.js.map +1 -0
- package/dist/src/context-manager/context-manager.d.ts +30 -0
- package/dist/src/context-manager/context-manager.d.ts.map +1 -0
- package/dist/src/context-manager/context-manager.js +89 -0
- package/dist/src/context-manager/context-manager.js.map +1 -0
- package/dist/src/context-manager/methods/summarize.d.ts +41 -0
- package/dist/src/context-manager/methods/summarize.d.ts.map +1 -0
- package/dist/src/context-manager/methods/summarize.js +108 -0
- package/dist/src/context-manager/methods/summarize.js.map +1 -0
- package/dist/src/context-manager/methods/truncate.d.ts +36 -0
- package/dist/src/context-manager/methods/truncate.d.ts.map +1 -0
- package/dist/src/context-manager/methods/truncate.js +86 -0
- package/dist/src/context-manager/methods/truncate.js.map +1 -0
- package/dist/src/context-manager/modes/agentic/agentic-context.d.ts +1 -2
- package/dist/src/context-manager/modes/agentic/agentic-context.d.ts.map +1 -1
- package/dist/src/context-manager/modes/agentic/agentic-context.js +6 -3
- package/dist/src/context-manager/modes/agentic/agentic-context.js.map +1 -1
- package/dist/src/context-manager/strategies/offload/base.d.ts +140 -0
- package/dist/src/context-manager/strategies/offload/base.d.ts.map +1 -0
- package/dist/src/context-manager/strategies/offload/base.js +376 -0
- package/dist/src/context-manager/strategies/offload/base.js.map +1 -0
- package/dist/src/context-manager/strategies/offload/drop.d.ts +19 -0
- package/dist/src/context-manager/strategies/offload/drop.d.ts.map +1 -0
- package/dist/src/context-manager/strategies/offload/drop.js +31 -0
- package/dist/src/context-manager/strategies/offload/drop.js.map +1 -0
- package/dist/src/context-manager/strategies/offload/index.d.ts +43 -0
- package/dist/src/context-manager/strategies/offload/index.d.ts.map +1 -0
- package/dist/src/context-manager/strategies/offload/index.js +37 -0
- package/dist/src/context-manager/strategies/offload/index.js.map +1 -0
- package/dist/src/context-manager/strategies/offload/summarize.d.ts +22 -0
- package/dist/src/context-manager/strategies/offload/summarize.d.ts.map +1 -0
- package/dist/src/context-manager/strategies/offload/summarize.js +104 -0
- package/dist/src/context-manager/strategies/offload/summarize.js.map +1 -0
- package/dist/src/context-manager/strategies/offload/truncate.d.ts +21 -0
- package/dist/src/context-manager/strategies/offload/truncate.d.ts.map +1 -0
- package/dist/src/context-manager/strategies/offload/truncate.js +66 -0
- package/dist/src/context-manager/strategies/offload/truncate.js.map +1 -0
- package/dist/src/context-manager/types.d.ts +50 -0
- package/dist/src/context-manager/types.d.ts.map +1 -0
- package/dist/src/context-manager/types.js +5 -0
- package/dist/src/context-manager/types.js.map +1 -0
- package/dist/src/hooks/events.d.ts +6 -5
- package/dist/src/hooks/events.d.ts.map +1 -1
- package/dist/src/hooks/events.js +5 -4
- package/dist/src/hooks/events.js.map +1 -1
- package/dist/src/hooks/types.d.ts +1 -0
- package/dist/src/hooks/types.d.ts.map +1 -1
- package/dist/src/hooks/types.js +1 -0
- package/dist/src/hooks/types.js.map +1 -1
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/injection/message-injection.d.ts +12 -9
- package/dist/src/injection/message-injection.d.ts.map +1 -1
- package/dist/src/injection/message-injection.js +17 -16
- package/dist/src/injection/message-injection.js.map +1 -1
- package/dist/src/mcp/client.d.ts.map +1 -1
- package/dist/src/mcp/client.js +9 -0
- package/dist/src/mcp/client.js.map +1 -1
- package/dist/src/middleware/stages.d.ts +15 -2
- package/dist/src/middleware/stages.d.ts.map +1 -1
- package/dist/src/middleware/stages.js.map +1 -1
- package/dist/src/mime.d.ts +1 -0
- package/dist/src/mime.d.ts.map +1 -1
- package/dist/src/mime.js.map +1 -1
- package/dist/src/models/anthropic.d.ts +26 -1
- package/dist/src/models/anthropic.d.ts.map +1 -1
- package/dist/src/models/anthropic.js +178 -54
- package/dist/src/models/anthropic.js.map +1 -1
- package/dist/src/models/bedrock.d.ts +36 -17
- package/dist/src/models/bedrock.d.ts.map +1 -1
- package/dist/src/models/bedrock.js +166 -34
- package/dist/src/models/bedrock.js.map +1 -1
- package/dist/src/models/google/adapters.d.ts.map +1 -1
- package/dist/src/models/google/adapters.js +18 -4
- package/dist/src/models/google/adapters.js.map +1 -1
- package/dist/src/models/google/model.d.ts.map +1 -1
- package/dist/src/models/google/model.js +9 -7
- package/dist/src/models/google/model.js.map +1 -1
- package/dist/src/models/google/types.d.ts +2 -0
- package/dist/src/models/google/types.d.ts.map +1 -1
- package/dist/src/models/model.d.ts +72 -5
- package/dist/src/models/model.d.ts.map +1 -1
- package/dist/src/models/model.js +15 -0
- package/dist/src/models/model.js.map +1 -1
- package/dist/src/models/openai/cache.d.ts +31 -0
- package/dist/src/models/openai/cache.d.ts.map +1 -0
- package/dist/src/models/openai/cache.js +56 -0
- package/dist/src/models/openai/cache.js.map +1 -0
- package/dist/src/models/openai/chat-adapter.d.ts.map +1 -1
- package/dist/src/models/openai/chat-adapter.js +2 -0
- package/dist/src/models/openai/chat-adapter.js.map +1 -1
- package/dist/src/models/openai/model.d.ts.map +1 -1
- package/dist/src/models/openai/model.js +14 -3
- package/dist/src/models/openai/model.js.map +1 -1
- package/dist/src/models/openai/responses-adapter.d.ts.map +1 -1
- package/dist/src/models/openai/responses-adapter.js +3 -1
- package/dist/src/models/openai/responses-adapter.js.map +1 -1
- package/dist/src/models/openai/types.d.ts +5 -1
- package/dist/src/models/openai/types.d.ts.map +1 -1
- package/dist/src/models/routing/fallback-strategy.d.ts +13 -0
- package/dist/src/models/routing/fallback-strategy.d.ts.map +1 -0
- package/dist/src/models/routing/fallback-strategy.js +33 -0
- package/dist/src/models/routing/fallback-strategy.js.map +1 -0
- package/dist/src/models/routing/index.d.ts +11 -0
- package/dist/src/models/routing/index.d.ts.map +1 -0
- package/dist/src/models/routing/index.js +9 -0
- package/dist/src/models/routing/index.js.map +1 -0
- package/dist/src/models/routing/router.d.ts +161 -0
- package/dist/src/models/routing/router.d.ts.map +1 -0
- package/dist/src/models/routing/router.js +474 -0
- package/dist/src/models/routing/router.js.map +1 -0
- package/dist/src/models/routing/strategy.d.ts +53 -0
- package/dist/src/models/routing/strategy.d.ts.map +1 -0
- package/dist/src/models/routing/strategy.js +2 -0
- package/dist/src/models/routing/strategy.js.map +1 -0
- package/dist/src/models/streaming.d.ts +12 -0
- package/dist/src/models/streaming.d.ts.map +1 -1
- package/dist/src/models/streaming.js +17 -0
- package/dist/src/models/streaming.js.map +1 -1
- package/dist/src/multiagent/plugins.d.ts +3 -1
- package/dist/src/multiagent/plugins.d.ts.map +1 -1
- package/dist/src/multiagent/plugins.js +6 -1
- package/dist/src/multiagent/plugins.js.map +1 -1
- package/dist/src/plugins/registry.d.ts +3 -1
- package/dist/src/plugins/registry.d.ts.map +1 -1
- package/dist/src/plugins/registry.js +6 -1
- package/dist/src/plugins/registry.js.map +1 -1
- package/dist/src/retry/default-model-retry-strategy.d.ts +3 -8
- package/dist/src/retry/default-model-retry-strategy.d.ts.map +1 -1
- package/dist/src/retry/default-model-retry-strategy.js +3 -8
- package/dist/src/retry/default-model-retry-strategy.js.map +1 -1
- package/dist/src/retry/model-retry-strategy.d.ts +6 -9
- package/dist/src/retry/model-retry-strategy.d.ts.map +1 -1
- package/dist/src/retry/model-retry-strategy.js +8 -12
- package/dist/src/retry/model-retry-strategy.js.map +1 -1
- package/dist/src/session/session-manager.d.ts +4 -0
- package/dist/src/session/session-manager.d.ts.map +1 -1
- package/dist/src/session/session-manager.js +6 -0
- package/dist/src/session/session-manager.js.map +1 -1
- package/dist/src/storage/in-memory-storage.d.ts +8 -1
- package/dist/src/storage/in-memory-storage.d.ts.map +1 -1
- package/dist/src/storage/in-memory-storage.js +10 -0
- package/dist/src/storage/in-memory-storage.js.map +1 -1
- package/dist/src/storage/local-file-storage.d.ts +8 -1
- package/dist/src/storage/local-file-storage.d.ts.map +1 -1
- package/dist/src/storage/local-file-storage.js +10 -0
- package/dist/src/storage/local-file-storage.js.map +1 -1
- package/dist/src/storage/s3-storage.d.ts +8 -1
- package/dist/src/storage/s3-storage.d.ts.map +1 -1
- package/dist/src/storage/s3-storage.js +10 -0
- package/dist/src/storage/s3-storage.js.map +1 -1
- package/dist/src/storage/search/index.d.ts +13 -0
- package/dist/src/storage/search/index.d.ts.map +1 -0
- package/dist/src/storage/search/index.js +11 -0
- package/dist/src/storage/search/index.js.map +1 -0
- package/dist/src/storage/search/keyword.d.ts +23 -0
- package/dist/src/storage/search/keyword.d.ts.map +1 -0
- package/dist/src/storage/search/keyword.js +53 -0
- package/dist/src/storage/search/keyword.js.map +1 -0
- package/dist/src/storage/search/types.d.ts +24 -0
- package/dist/src/storage/search/types.d.ts.map +1 -0
- package/dist/src/storage/search/types.js +2 -0
- package/dist/src/storage/search/types.js.map +1 -0
- package/dist/src/storage/storage.d.ts +46 -1
- package/dist/src/storage/storage.d.ts.map +1 -1
- package/dist/src/storage/storage.js +22 -0
- package/dist/src/storage/storage.js.map +1 -1
- package/dist/src/telemetry/meter.d.ts +8 -6
- package/dist/src/telemetry/meter.d.ts.map +1 -1
- package/dist/src/telemetry/meter.js +8 -7
- package/dist/src/telemetry/meter.js.map +1 -1
- package/dist/src/telemetry/tracer.d.ts.map +1 -1
- package/dist/src/telemetry/tracer.js +9 -2
- package/dist/src/telemetry/tracer.js.map +1 -1
- package/dist/src/tools/executors/executor.d.ts +2 -0
- package/dist/src/tools/executors/executor.d.ts.map +1 -1
- package/dist/src/tools/executors/executor.js +6 -4
- package/dist/src/tools/executors/executor.js.map +1 -1
- package/dist/src/tools/executors/sequential.d.ts +1 -1
- package/dist/src/tools/executors/sequential.js +2 -2
- package/dist/src/tools/executors/sequential.js.map +1 -1
- package/dist/src/tools/mcp-tool.d.ts +2 -1
- package/dist/src/tools/mcp-tool.d.ts.map +1 -1
- package/dist/src/tools/mcp-tool.js +2 -1
- package/dist/src/tools/mcp-tool.js.map +1 -1
- package/dist/src/tools/tool.d.ts +2 -0
- package/dist/src/tools/tool.d.ts.map +1 -1
- package/dist/src/tools/tool.js.map +1 -1
- package/dist/src/tools/types.d.ts +8 -0
- package/dist/src/tools/types.d.ts.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/types/agent.d.ts +14 -4
- package/dist/src/types/agent.d.ts.map +1 -1
- package/dist/src/types/agent.js +3 -2
- package/dist/src/types/agent.js.map +1 -1
- package/dist/src/types/media.d.ts +65 -3
- package/dist/src/types/media.d.ts.map +1 -1
- package/dist/src/types/media.js +68 -1
- package/dist/src/types/media.js.map +1 -1
- package/dist/src/types/messages.d.ts +6 -4
- package/dist/src/types/messages.d.ts.map +1 -1
- package/dist/src/types/messages.js +4 -1
- package/dist/src/types/messages.js.map +1 -1
- package/dist/src/vended-memory-stores/bedrock-knowledge-base/store.d.ts +11 -0
- package/dist/src/vended-memory-stores/bedrock-knowledge-base/store.d.ts.map +1 -1
- package/dist/src/vended-memory-stores/bedrock-knowledge-base/store.js +53 -3
- package/dist/src/vended-memory-stores/bedrock-knowledge-base/store.js.map +1 -1
- package/dist/src/vended-memory-stores/file-memory-store/index.d.ts +3 -0
- package/dist/src/vended-memory-stores/file-memory-store/index.d.ts.map +1 -0
- package/dist/src/vended-memory-stores/file-memory-store/index.js +2 -0
- package/dist/src/vended-memory-stores/file-memory-store/index.js.map +1 -0
- package/dist/src/vended-memory-stores/file-memory-store/store.d.ts +104 -0
- package/dist/src/vended-memory-stores/file-memory-store/store.d.ts.map +1 -0
- package/dist/src/vended-memory-stores/file-memory-store/store.js +237 -0
- package/dist/src/vended-memory-stores/file-memory-store/store.js.map +1 -0
- package/dist/src/vended-memory-stores/test-memory-store/store.d.ts.map +1 -1
- package/dist/src/vended-memory-stores/test-memory-store/store.js +2 -25
- package/dist/src/vended-memory-stores/test-memory-store/store.js.map +1 -1
- package/dist/src/vended-plugins/context-offloader/plugin.d.ts.map +1 -1
- package/dist/src/vended-plugins/context-offloader/plugin.js +2 -8
- package/dist/src/vended-plugins/context-offloader/plugin.js.map +1 -1
- package/dist/src/vended-tools/http-request/http-request.d.ts +1 -1
- package/dist/src/vended-tools/http-request/http-request.js +2 -2
- package/dist/src/vended-tools/http-request/http-request.js.map +1 -1
- package/dist/src/vended-tools/sleep/make-sleep.d.ts +4 -6
- package/dist/src/vended-tools/sleep/make-sleep.d.ts.map +1 -1
- package/dist/src/vended-tools/sleep/make-sleep.js +5 -7
- package/dist/src/vended-tools/sleep/make-sleep.js.map +1 -1
- package/package.json +16 -4
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model routing primitives.
|
|
3
|
+
*
|
|
4
|
+
* A {@link ModelRouter} asks its {@link RoutingStrategy} before the first model call and after
|
|
5
|
+
* unclaimed failures until routing stops. Returning `undefined` from the opening selection uses the
|
|
6
|
+
* first candidate's concrete default model; returning it after a failure preserves the pending error.
|
|
7
|
+
*
|
|
8
|
+
* A failure round uses each candidate at most once. A successful call starts a new round with that
|
|
9
|
+
* candidate already counted as used. Nested routers are opaque candidates: they make one selection
|
|
10
|
+
* with an empty attempt history and do not perform internal failover for the outer router.
|
|
11
|
+
*
|
|
12
|
+
* `agent.model` remains the first candidate's concrete model. Opening selection runs in model input
|
|
13
|
+
* middleware after continuation input is incorporated, so the opening before-model hook reports that
|
|
14
|
+
* default while {@link AfterModelCallEvent} reports the effective routed model. Token estimation also
|
|
15
|
+
* uses the default model, so prefer candidates with comparable context windows and tokenizers. If a
|
|
16
|
+
* candidate fails after emitting part of a streamed response, consumers see that partial output
|
|
17
|
+
* followed by the replacement model's full response.
|
|
18
|
+
*
|
|
19
|
+
* The API is provisional and may change before it is finalized.
|
|
20
|
+
*/
|
|
21
|
+
import { CancelledError, normalizeError } from '../../errors.js';
|
|
22
|
+
import { AfterInvocationEvent, AfterModelCallEvent, BeforeInvocationEvent } from '../../hooks/events.js';
|
|
23
|
+
import { HookOrder } from '../../hooks/types.js';
|
|
24
|
+
import { logger } from '../../logging/logger.js';
|
|
25
|
+
import { InvokeModelStage } from '../../middleware/stages.js';
|
|
26
|
+
import { Model } from '../model.js';
|
|
27
|
+
import { cloneSystemPrompt } from '../../types/messages.js';
|
|
28
|
+
import { deepCopy } from '../../types/json.js';
|
|
29
|
+
import { FallbackStrategy } from './fallback-strategy.js';
|
|
30
|
+
/**
|
|
31
|
+
* A model or opaque model group with an optional name and description.
|
|
32
|
+
*
|
|
33
|
+
* Base instances are frozen automatically. Subclasses must freeze themselves after initializing additional fields.
|
|
34
|
+
*/
|
|
35
|
+
export class RoutingCandidate {
|
|
36
|
+
/** Concrete model or opaque nested router. */
|
|
37
|
+
model;
|
|
38
|
+
/** Optional strategy-facing name. */
|
|
39
|
+
name;
|
|
40
|
+
/** Optional strategy-facing description. */
|
|
41
|
+
description;
|
|
42
|
+
/**
|
|
43
|
+
* Create an immutable routing candidate.
|
|
44
|
+
*
|
|
45
|
+
* @param options - Candidate model, name, and description
|
|
46
|
+
*/
|
|
47
|
+
constructor(options) {
|
|
48
|
+
this.model = options.model;
|
|
49
|
+
if (options.name !== undefined)
|
|
50
|
+
this.name = options.name;
|
|
51
|
+
if (options.description !== undefined)
|
|
52
|
+
this.description = options.description;
|
|
53
|
+
if (new.target === RoutingCandidate)
|
|
54
|
+
Object.freeze(this);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Per-invocation candidate, attempt history, and switch accounting. */
|
|
58
|
+
class RoutingState {
|
|
59
|
+
candidate;
|
|
60
|
+
model;
|
|
61
|
+
attempts = [];
|
|
62
|
+
switchedTo;
|
|
63
|
+
switches = 0;
|
|
64
|
+
constructor(candidate, model) {
|
|
65
|
+
this.candidate = candidate;
|
|
66
|
+
this.model = model;
|
|
67
|
+
// The opening candidate counts as used for this round.
|
|
68
|
+
this.switchedTo = new Set([candidate]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const ROUTING_KEY_PREFIX = 'strands:modelRouting';
|
|
72
|
+
/** Lazily minted per-object identity numbers (the `id()` equivalent for state keys), weakly held. */
|
|
73
|
+
const OBJECT_IDS = new WeakMap();
|
|
74
|
+
let nextObjectId = 1;
|
|
75
|
+
/**
|
|
76
|
+
* Routes each agent invocation among an immutable set of candidate models.
|
|
77
|
+
*
|
|
78
|
+
* The default {@link FallbackStrategy} prefers the candidate with the fewest recorded failures and
|
|
79
|
+
* breaks ties by declaration order. `maxSwitches` bounds successful candidate changes per invocation.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const router = new ModelRouter([
|
|
84
|
+
* new RoutingCandidate({ model: primary, name: 'primary' }),
|
|
85
|
+
* new RoutingCandidate({ model: fallback, name: 'fallback' }),
|
|
86
|
+
* ])
|
|
87
|
+
* const agent = new Agent({ model: router })
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export class ModelRouter {
|
|
91
|
+
name = 'strands:model-router';
|
|
92
|
+
_candidates;
|
|
93
|
+
_strategy;
|
|
94
|
+
_maxSwitches;
|
|
95
|
+
_attachedAgents = new WeakSet();
|
|
96
|
+
_initializedAgents = new WeakSet();
|
|
97
|
+
/**
|
|
98
|
+
* Create a model router.
|
|
99
|
+
*
|
|
100
|
+
* @param models - Candidate models, nested routers, or candidate wrappers
|
|
101
|
+
* @param options - Routing strategy and switch cap
|
|
102
|
+
* @throws TypeError if models or the strategy are invalid
|
|
103
|
+
* @throws Error if candidates are empty, duplicated, named alike, stateful, or `maxSwitches` is not a non-negative integer
|
|
104
|
+
*/
|
|
105
|
+
constructor(models, options = {}) {
|
|
106
|
+
if (options.strategy !== undefined && !isRoutingStrategy(options.strategy)) {
|
|
107
|
+
throw new TypeError('strategy must implement RoutingStrategy: a select(context) method');
|
|
108
|
+
}
|
|
109
|
+
if (options.maxSwitches !== undefined && (!Number.isInteger(options.maxSwitches) || options.maxSwitches < 0)) {
|
|
110
|
+
throw new Error('maxSwitches must be a non-negative integer');
|
|
111
|
+
}
|
|
112
|
+
const candidates = normalize(models);
|
|
113
|
+
if (candidates.length === 0)
|
|
114
|
+
throw new Error('ModelRouter requires at least one candidate model');
|
|
115
|
+
rejectStateful(candidates);
|
|
116
|
+
rejectDuplicates(candidates);
|
|
117
|
+
this._candidates = Object.freeze(candidates);
|
|
118
|
+
this._strategy = options.strategy ?? new FallbackStrategy();
|
|
119
|
+
if (options.maxSwitches !== undefined)
|
|
120
|
+
this._maxSwitches = options.maxSwitches;
|
|
121
|
+
}
|
|
122
|
+
/** Normalized candidates in declaration order. */
|
|
123
|
+
get candidates() {
|
|
124
|
+
return this._candidates;
|
|
125
|
+
}
|
|
126
|
+
/** First declared candidate resolved without consulting a strategy. */
|
|
127
|
+
get defaultModel() {
|
|
128
|
+
const model = this._candidates[0].model;
|
|
129
|
+
return model instanceof ModelRouter ? model.defaultModel : model;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Attach this router as an agent's configured model.
|
|
133
|
+
*
|
|
134
|
+
* @internal
|
|
135
|
+
* @param agent - Agent configured with this router
|
|
136
|
+
*/
|
|
137
|
+
attachToAgent(agent) {
|
|
138
|
+
this._attachedAgents.add(agent);
|
|
139
|
+
this.initAgent(agent);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Register routing middleware and lifecycle hooks.
|
|
143
|
+
*
|
|
144
|
+
* @param agent - Agent using this router as its model
|
|
145
|
+
* @throws Error if attached as an ordinary plugin rather than as the model
|
|
146
|
+
*/
|
|
147
|
+
initAgent(agent) {
|
|
148
|
+
if (!this._attachedAgents.has(agent)) {
|
|
149
|
+
throw new Error('ModelRouter must be passed through Agent({ model }), not plugins');
|
|
150
|
+
}
|
|
151
|
+
if (this._initializedAgents.has(agent))
|
|
152
|
+
return;
|
|
153
|
+
this._initializedAgents.add(agent);
|
|
154
|
+
agent.addMiddleware(InvokeModelStage.Input, (context) => this._selectionMiddleware(context));
|
|
155
|
+
agent.addHook(AfterModelCallEvent, (event) => this._onModelResult(event), { order: HookOrder.MODEL_ROUTING });
|
|
156
|
+
agent.addHook(AfterInvocationEvent, (event) => this._clearState(event.agent, event.invocationState), {
|
|
157
|
+
order: HookOrder.SDK_LAST,
|
|
158
|
+
});
|
|
159
|
+
agent.addHook(BeforeInvocationEvent, (event) => this._clearState(event.agent, event.invocationState), {
|
|
160
|
+
order: HookOrder.SDK_FIRST,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Return the current routed model after hooks may have changed the selection.
|
|
165
|
+
*
|
|
166
|
+
* @internal
|
|
167
|
+
* @param agent - Agent making the model call
|
|
168
|
+
* @param invocationState - Live invocation state
|
|
169
|
+
* @returns Current routed model, or `undefined` before selection
|
|
170
|
+
*/
|
|
171
|
+
getRoutedModel(agent, invocationState) {
|
|
172
|
+
return this._getState(agent, invocationState)?.model;
|
|
173
|
+
}
|
|
174
|
+
/** Apply the per-invocation selection to the model call, opening one on the first call. */
|
|
175
|
+
async _selectionMiddleware(context) {
|
|
176
|
+
let state = this._getState(context.agent, context.invocationState);
|
|
177
|
+
if (state === undefined) {
|
|
178
|
+
const routingContext = this._routingContext([...context.messages], context.systemPrompt, [...context.toolSpecs], context.invocationState);
|
|
179
|
+
state = await this._openAndCache(context.agent, context.invocationState, routingContext);
|
|
180
|
+
}
|
|
181
|
+
return { ...context, model: state.model };
|
|
182
|
+
}
|
|
183
|
+
/** Make the opening choice and store its state in the invocation state under this pair's key. */
|
|
184
|
+
async _openAndCache(agent, invocationState, context) {
|
|
185
|
+
const [candidate, model] = await this._open(context);
|
|
186
|
+
const state = new RoutingState(candidate, model);
|
|
187
|
+
invocationState[this._stateKey(agent)] = state;
|
|
188
|
+
return state;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Choose and resolve the candidate to start on.
|
|
192
|
+
*
|
|
193
|
+
* A decline serves the default model. A strategy that throws, and a candidate that will not resolve
|
|
194
|
+
* to a model, both propagate.
|
|
195
|
+
*/
|
|
196
|
+
async _open(context) {
|
|
197
|
+
const candidate = await this._ask(context);
|
|
198
|
+
if (candidate === undefined) {
|
|
199
|
+
logger.info(`strategy=<${this._strategyName}> | strategy declined the opening choice, using the default model`);
|
|
200
|
+
return [this._candidates[0], this.defaultModel];
|
|
201
|
+
}
|
|
202
|
+
logger.info(`strategy=<${this._strategyName}>, candidate=<${candidateLabel(candidate)}> | candidate selected`);
|
|
203
|
+
return [candidate, await this._resolve(candidate, context)];
|
|
204
|
+
}
|
|
205
|
+
/** Ask the strategy for a candidate and validate the answer. */
|
|
206
|
+
async _ask(context) {
|
|
207
|
+
return this._validated(await this._strategy.select(context), context);
|
|
208
|
+
}
|
|
209
|
+
/** Return the candidate, throwing if the strategy broke its contract. */
|
|
210
|
+
_validated(candidate, context) {
|
|
211
|
+
if (candidate === undefined)
|
|
212
|
+
return undefined;
|
|
213
|
+
if (!(candidate instanceof RoutingCandidate)) {
|
|
214
|
+
throw new TypeError(`strategy.select must return a RoutingCandidate or undefined; got ${typeName(candidate)}`);
|
|
215
|
+
}
|
|
216
|
+
if (!context.candidates.some((configured) => configured === candidate)) {
|
|
217
|
+
throw new Error('strategy.select must return a candidate from context.candidates');
|
|
218
|
+
}
|
|
219
|
+
return candidate;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Resolve a candidate to a concrete model, recursing into a nested router's selection.
|
|
223
|
+
*
|
|
224
|
+
* A nested router is asked with its own candidates and no attempts, so it contributes one candidate
|
|
225
|
+
* and never advances internally.
|
|
226
|
+
*/
|
|
227
|
+
async _resolve(candidate, context) {
|
|
228
|
+
if (!(candidate.model instanceof ModelRouter))
|
|
229
|
+
return candidate.model;
|
|
230
|
+
return candidate.model._selectModel(candidate.model._routingContext(context.messages, context.systemPrompt, context.toolSpecs, context.invocationState, []));
|
|
231
|
+
}
|
|
232
|
+
/** Resolve this router's chosen candidate, or its default model if the strategy declines. */
|
|
233
|
+
async _selectModel(context) {
|
|
234
|
+
const candidate = await this._ask(context);
|
|
235
|
+
if (candidate === undefined)
|
|
236
|
+
return this.defaultModel;
|
|
237
|
+
return this._resolve(candidate, context);
|
|
238
|
+
}
|
|
239
|
+
/** Strategy class name, for logs that explain a routing decision. */
|
|
240
|
+
get _strategyName() {
|
|
241
|
+
return this._strategy.constructor.name;
|
|
242
|
+
}
|
|
243
|
+
/** Record the outcome and, after an unclaimed failure, apply the strategy's next choice. */
|
|
244
|
+
async _onModelResult(event) {
|
|
245
|
+
const state = this._getState(event.agent, event.invocationState);
|
|
246
|
+
if (state === undefined)
|
|
247
|
+
return;
|
|
248
|
+
if (event.error instanceof CancelledError)
|
|
249
|
+
return;
|
|
250
|
+
if (event.stopData !== undefined) {
|
|
251
|
+
state.attempts.push(makeAttempt(state.candidate));
|
|
252
|
+
// The candidate that succeeded opens the next round, counting as used like any opening choice.
|
|
253
|
+
state.switchedTo = new Set([state.candidate]);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (event.retry || event.error === undefined)
|
|
257
|
+
return;
|
|
258
|
+
state.attempts.push(makeAttempt(state.candidate, event.error));
|
|
259
|
+
if (this._maxSwitches !== undefined && state.switches >= this._maxSwitches) {
|
|
260
|
+
logger.warn(`maxSwitches=<${this._maxSwitches}> | switch cap reached, leaving the error to surface`);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (await this._advance(event, state))
|
|
264
|
+
event.retry = true;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Switch to the strategy's next candidate, returning whether the call should be retried.
|
|
268
|
+
*
|
|
269
|
+
* A usable new candidate is switched to. A strategy that throws, declines, or names a candidate the
|
|
270
|
+
* round already used leaves the model's error to surface. A candidate that will not resolve is
|
|
271
|
+
* unusable rather than unlucky, so it takes its slot in the round and the strategy is asked once
|
|
272
|
+
* more. Each pass either switches or consumes a slot, so the round stays bounded by the candidate
|
|
273
|
+
* count.
|
|
274
|
+
*/
|
|
275
|
+
async _advance(event, state) {
|
|
276
|
+
while (true) {
|
|
277
|
+
const context = this._routingContext(event.agent.messages, event.agent.systemPrompt, event.agent.toolRegistry.list().map((tool) => tool.toolSpec), event.invocationState, state.attempts);
|
|
278
|
+
let candidate;
|
|
279
|
+
try {
|
|
280
|
+
// Validated inside the try: a model error is already pending, so a broken answer must not
|
|
281
|
+
// replace it. The opening choice has nothing pending and lets the error surface.
|
|
282
|
+
candidate = this._validated(await this._strategy.select(context), context);
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
logger.warn(`strategy=<${this._strategyName}>, error=<${String(error)}> | routing failed, leaving the error to surface`);
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
if (candidate === undefined)
|
|
289
|
+
return false;
|
|
290
|
+
// A round uses each candidate at most once, so a strategy that cycles cannot keep resetting
|
|
291
|
+
// the retry budget.
|
|
292
|
+
if (state.switchedTo.has(candidate)) {
|
|
293
|
+
logger.info(`candidate=<${candidateLabel(candidate)}> | already used this round, leaving the error to surface`);
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
let model;
|
|
297
|
+
try {
|
|
298
|
+
model = await this._resolve(candidate, context);
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
const resolutionError = normalizeError(error);
|
|
302
|
+
logger.warn(`candidate=<${candidateLabel(candidate)}>, error=<${resolutionError}> | candidate could not be resolved, asking again`);
|
|
303
|
+
// Recorded so a strategy reading attempts stops offering it, and slot-burned so termination
|
|
304
|
+
// does not depend on the strategy doing that.
|
|
305
|
+
state.attempts.push(makeAttempt(candidate, resolutionError));
|
|
306
|
+
state.switchedTo.add(candidate);
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
logger.info(`from_candidate=<${candidateLabel(state.candidate)}>, to_candidate=<${candidateLabel(candidate)}>, error=<${event.error?.constructor.name}> | model call failed, switching candidate`);
|
|
310
|
+
state.candidate = candidate;
|
|
311
|
+
state.model = model;
|
|
312
|
+
state.switchedTo.add(candidate);
|
|
313
|
+
state.switches += 1;
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Build a {@link RoutingContext} over this router's candidates.
|
|
319
|
+
*
|
|
320
|
+
* Every ask goes through here, so the request a strategy sees is always a copy: the agent's messages
|
|
321
|
+
* and the registry's own tool specs are never handed over directly. Mirrors the copy the agent loop
|
|
322
|
+
* makes for each model call.
|
|
323
|
+
*/
|
|
324
|
+
_routingContext(messages, systemPrompt, toolSpecs, invocationState, attempts = []) {
|
|
325
|
+
const context = {
|
|
326
|
+
messages: messages.map((message) => message.clone()),
|
|
327
|
+
...(systemPrompt !== undefined && { systemPrompt: cloneSystemPrompt(systemPrompt) }),
|
|
328
|
+
toolSpecs: deepCopy(toolSpecs),
|
|
329
|
+
candidates: this._candidates,
|
|
330
|
+
invocationState,
|
|
331
|
+
attempts: Object.freeze([...attempts]),
|
|
332
|
+
};
|
|
333
|
+
return Object.freeze(context);
|
|
334
|
+
}
|
|
335
|
+
/** Drop this agent's routing state so it never spans invocations. */
|
|
336
|
+
_clearState(agent, invocationState) {
|
|
337
|
+
const key = this._stateKey(agent);
|
|
338
|
+
if (this._getState(agent, invocationState) !== undefined)
|
|
339
|
+
delete invocationState[key];
|
|
340
|
+
}
|
|
341
|
+
/** Return the routing state stored under this pair's key, ignoring any foreign value. */
|
|
342
|
+
_getState(agent, invocationState) {
|
|
343
|
+
const value = invocationState[this._stateKey(agent)];
|
|
344
|
+
return value instanceof RoutingState ? value : undefined;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Scope routing state to one agent/router pair.
|
|
348
|
+
*
|
|
349
|
+
* One invocation state can serve several agents, and one router several agents, so neither identity
|
|
350
|
+
* alone is a sufficient key. `agentId` cannot serve either: it is caller-supplied and defaults, so
|
|
351
|
+
* two agents sharing a router could collide. Object identity is unique among live agents, and the
|
|
352
|
+
* state is cleared at both ends of an invocation, so a recycled entry cannot match a live one.
|
|
353
|
+
*
|
|
354
|
+
* The key carries no per-invocation component, so isolation between concurrent invocations relies on
|
|
355
|
+
* the agent allowing only one at a time, which is its default.
|
|
356
|
+
*/
|
|
357
|
+
_stateKey(agent) {
|
|
358
|
+
return `${ROUTING_KEY_PREFIX}:${objectId(agent).toString(16)}:${objectId(this).toString(16)}`;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/** Freeze a routing attempt, omitting `exception` for a success. */
|
|
362
|
+
function makeAttempt(candidate, exception) {
|
|
363
|
+
return Object.freeze({ candidate, ...(exception !== undefined && { exception }) });
|
|
364
|
+
}
|
|
365
|
+
/** Return a stable identity number for the object, minting one on first use. */
|
|
366
|
+
function objectId(value) {
|
|
367
|
+
let identity = OBJECT_IDS.get(value);
|
|
368
|
+
if (identity === undefined) {
|
|
369
|
+
identity = nextObjectId;
|
|
370
|
+
nextObjectId += 1;
|
|
371
|
+
OBJECT_IDS.set(value, identity);
|
|
372
|
+
}
|
|
373
|
+
return identity;
|
|
374
|
+
}
|
|
375
|
+
/** Coerce the input array into {@link RoutingCandidate} objects, validating candidate types. */
|
|
376
|
+
function normalize(models) {
|
|
377
|
+
if (!Array.isArray(models))
|
|
378
|
+
throw new TypeError('models must be a sequence of candidates');
|
|
379
|
+
return models.map(asCandidate);
|
|
380
|
+
}
|
|
381
|
+
/** Wrap a candidate input in a {@link RoutingCandidate}, validating its model type. */
|
|
382
|
+
function asCandidate(item) {
|
|
383
|
+
const candidate = item instanceof RoutingCandidate ? item : new RoutingCandidate({ model: item });
|
|
384
|
+
if (!(candidate.model instanceof Model) && !(candidate.model instanceof ModelRouter)) {
|
|
385
|
+
throw new TypeError(`candidate must be a Model or ModelRouter; got ${typeName(candidate.model)}`);
|
|
386
|
+
}
|
|
387
|
+
return candidate;
|
|
388
|
+
}
|
|
389
|
+
/** Reject any stateful candidate model. */
|
|
390
|
+
function rejectStateful(candidates) {
|
|
391
|
+
for (const candidate of candidates) {
|
|
392
|
+
if (candidate.model instanceof Model && candidate.model.stateful) {
|
|
393
|
+
throw new Error(`candidate=<${candidateLabel(candidate)}> is stateful; routing among stateful models is not supported`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Reject repeated candidates, repeated models, or colliding names.
|
|
399
|
+
*
|
|
400
|
+
* Strategies track health per candidate, so one model behind two candidates would get two failure
|
|
401
|
+
* budgets and never be demoted. The model check reaches through a nested router; names stay per level,
|
|
402
|
+
* since a strategy only chooses among the candidates it is shown.
|
|
403
|
+
*/
|
|
404
|
+
function rejectDuplicates(candidates) {
|
|
405
|
+
const seenCandidates = new Set();
|
|
406
|
+
const seenModels = new Set();
|
|
407
|
+
const seenNames = new Set();
|
|
408
|
+
for (const candidate of candidates) {
|
|
409
|
+
if (seenCandidates.has(candidate))
|
|
410
|
+
throw new Error('duplicate RoutingCandidate instance');
|
|
411
|
+
seenCandidates.add(candidate);
|
|
412
|
+
for (const model of reachableModels(candidate)) {
|
|
413
|
+
if (seenModels.has(model)) {
|
|
414
|
+
throw new Error(`candidate=<${candidateLabel(candidate)}> repeats a model already routed to; construct a separate instance so each candidate has its own health`);
|
|
415
|
+
}
|
|
416
|
+
seenModels.add(model);
|
|
417
|
+
}
|
|
418
|
+
if (candidate.name === undefined)
|
|
419
|
+
continue;
|
|
420
|
+
if (seenNames.has(candidate.name))
|
|
421
|
+
throw new Error(`duplicate candidate name=<${candidate.name}>`);
|
|
422
|
+
seenNames.add(candidate.name);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/** Yield every concrete model this candidate can run, descending into a nested router. */
|
|
426
|
+
function* reachableModels(candidate) {
|
|
427
|
+
if (candidate.model instanceof ModelRouter) {
|
|
428
|
+
for (const nested of candidate.model.candidates)
|
|
429
|
+
yield* reachableModels(nested);
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
yield candidate.model;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/** Return the candidate's name, or its provider and model id when it has none. */
|
|
436
|
+
function candidateLabel(candidate) {
|
|
437
|
+
if (candidate.name !== undefined && candidate.name.length > 0)
|
|
438
|
+
return candidate.name;
|
|
439
|
+
return candidate.model instanceof Model ? modelLabel(candidate.model) : candidate.model.constructor.name;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Label a model by provider and model id.
|
|
443
|
+
*
|
|
444
|
+
* Labels are built eagerly, as log arguments and by the construction guards, so one must never fail a
|
|
445
|
+
* routed call or mask a construction error.
|
|
446
|
+
*/
|
|
447
|
+
function modelLabel(model) {
|
|
448
|
+
const provider = model.constructor.name;
|
|
449
|
+
try {
|
|
450
|
+
const modelId = model.getConfig()?.modelId;
|
|
451
|
+
return modelId ? `${provider}/${modelId}` : provider;
|
|
452
|
+
}
|
|
453
|
+
catch {
|
|
454
|
+
return provider;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
/** Check whether the value structurally implements {@link RoutingStrategy}. */
|
|
458
|
+
function isRoutingStrategy(strategy) {
|
|
459
|
+
return isObject(strategy) && 'select' in strategy && typeof strategy.select === 'function';
|
|
460
|
+
}
|
|
461
|
+
function isObject(value) {
|
|
462
|
+
return typeof value === 'object' && value !== null;
|
|
463
|
+
}
|
|
464
|
+
/** Describe a value's type for error messages. */
|
|
465
|
+
function typeName(value) {
|
|
466
|
+
if (value === null)
|
|
467
|
+
return 'null';
|
|
468
|
+
if (value === undefined)
|
|
469
|
+
return 'undefined';
|
|
470
|
+
if (typeof value === 'object')
|
|
471
|
+
return value.constructor?.name ?? 'object';
|
|
472
|
+
return typeof value;
|
|
473
|
+
}
|
|
474
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../../../src/models/routing/router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChE,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAA;AACxG,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACnC,OAAO,EAAE,iBAAiB,EAAmC,MAAM,yBAAyB,CAAA;AAC5F,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAK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AAI,QAAQ,IAAI,OAAO,QAAQ,CAAC,MAAM,KAAK,UAAU,CAAA;AAC5F,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAA;AACpD,CAAC;AAED,kDAAkD;AAClD,SAAS,QAAQ,CAAC,KAAc;IAC9B,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,MAAM,CAAA;IACjC,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,WAAW,CAAA;IAC3C,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC,WAAW,EAAE,IAAI,IAAI,QAAQ,CAAA;IACzE,OAAO,OAAO,KAAK,CAAA;AACrB,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Model } from '../model.js';
|
|
2
|
+
import type { ModelRouter, RoutingCandidate } from './router.js';
|
|
3
|
+
import type { InvocationState } from '../../types/agent.js';
|
|
4
|
+
import type { Message, SystemPrompt } from '../../types/messages.js';
|
|
5
|
+
import type { ToolSpec } from '../../tools/types.js';
|
|
6
|
+
/** A candidate used during an invocation and the outcome of that attempt. */
|
|
7
|
+
export interface RoutingAttempt {
|
|
8
|
+
/** The configured candidate instance. */
|
|
9
|
+
readonly candidate: RoutingCandidate;
|
|
10
|
+
/** The model or candidate-resolution error, absent when the call succeeded. */
|
|
11
|
+
readonly exception?: Error;
|
|
12
|
+
}
|
|
13
|
+
/** Read-only inputs supplied to a routing strategy. */
|
|
14
|
+
export interface RoutingContext {
|
|
15
|
+
/** Fresh copy of the messages for this strategy ask. */
|
|
16
|
+
readonly messages: Message[];
|
|
17
|
+
/** Fresh copy of the system prompt for this strategy ask. */
|
|
18
|
+
readonly systemPrompt?: SystemPrompt;
|
|
19
|
+
/** Fresh copy of tool specifications for this strategy ask. */
|
|
20
|
+
readonly toolSpecs: ToolSpec[];
|
|
21
|
+
/** Stable configured candidate instances. */
|
|
22
|
+
readonly candidates: readonly RoutingCandidate[];
|
|
23
|
+
/** Live invocation state, exposed as read-only. */
|
|
24
|
+
readonly invocationState: Readonly<InvocationState>;
|
|
25
|
+
/** Chronological attempts made during this invocation. */
|
|
26
|
+
readonly attempts: readonly RoutingAttempt[];
|
|
27
|
+
}
|
|
28
|
+
/** Chooses a configured routing candidate. */
|
|
29
|
+
export interface RoutingStrategy {
|
|
30
|
+
/**
|
|
31
|
+
* Select a candidate from `context.candidates`, or decline with `undefined`.
|
|
32
|
+
*
|
|
33
|
+
* The router asks before the first model call with an empty attempt history, then after unclaimed
|
|
34
|
+
* failures until routing stops. The returned candidate must be the same instance as one in
|
|
35
|
+
* `context.candidates`.
|
|
36
|
+
*
|
|
37
|
+
* Declining the opening selection uses the router's default model. Declining after a failure ends
|
|
38
|
+
* routing and preserves the pending model error. During opening selection, strategy errors, invalid
|
|
39
|
+
* candidates, and candidate-resolution errors propagate. After a model failure, strategy errors and
|
|
40
|
+
* invalid candidates end routing without replacing the pending error.
|
|
41
|
+
*
|
|
42
|
+
* Each failure round may use a candidate once. Returning a candidate already used in the current round
|
|
43
|
+
* ends routing. If a nested candidate cannot resolve after a failure, it consumes its round slot and
|
|
44
|
+
* the strategy is asked again.
|
|
45
|
+
*
|
|
46
|
+
* @param context - Current request and chronological routing history
|
|
47
|
+
* @returns A configured candidate, or `undefined` to decline
|
|
48
|
+
*/
|
|
49
|
+
select(context: RoutingContext): Promise<RoutingCandidate | undefined>;
|
|
50
|
+
}
|
|
51
|
+
/** What each {@link ModelRouter} candidate entry accepts. */
|
|
52
|
+
export type CandidateInput = Model | ModelRouter | RoutingCandidate;
|
|
53
|
+
//# sourceMappingURL=strategy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.d.ts","sourceRoot":"","sources":["../../../../src/models/routing/strategy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,KA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appings":""}
|
|
@@ -427,4 +427,16 @@ export declare function accumulateUsage(target: Usage, source: Usage): void;
|
|
|
427
427
|
* @returns A Usage object with zeroed counters
|
|
428
428
|
*/
|
|
429
429
|
export declare function createEmptyUsage(): Usage;
|
|
430
|
+
/**
|
|
431
|
+
* Returns the total prompt the model processed, including cached tokens.
|
|
432
|
+
*
|
|
433
|
+
* Providers either fold cache tokens into `inputTokens` or report them separately; the totals tell us
|
|
434
|
+
* which. `inputTokens + outputTokens === totalTokens` means cache is already inside `inputTokens`;
|
|
435
|
+
* otherwise the cache counters are added on top. Uncached usage returns `inputTokens` either way.
|
|
436
|
+
*
|
|
437
|
+
* @param usage - Token usage from a model invocation
|
|
438
|
+
* @returns The total prompt token count
|
|
439
|
+
* @internal
|
|
440
|
+
*/
|
|
441
|
+
export declare function totalPromptTokens(usage: Usage): number;
|
|
430
442
|
//# sourceMappingURL=streaming.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../../../src/models/streaming.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,KAAK,EAAE,QAAQ,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAA;AAE/E;;;;;;GAMG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GACxB,0BAA0B,GAC1B,+BAA+B,GAC/B,+BAA+B,GAC/B,8BAA8B,GAC9B,yBAAyB,GACzB,sBAAsB,GACtB,uBAAuB,CAAA;AAa3B;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,KAAK,IAAI,gBAAgB,CAErF;AAED;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;OAEG;IACH,IAAI,EAAE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K,EAAE,KAAK,GAAG,MAAM,CAKtD"}
|
|
@@ -191,4 +191,21 @@ export function createEmptyUsage() {
|
|
|
191
191
|
totalTokens: 0,
|
|
192
192
|
};
|
|
193
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns the total prompt the model processed, including cached tokens.
|
|
196
|
+
*
|
|
197
|
+
* Providers either fold cache tokens into `inputTokens` or report them separately; the totals tell us
|
|
198
|
+
* which. `inputTokens + outputTokens === totalTokens` means cache is already inside `inputTokens`;
|
|
199
|
+
* otherwise the cache counters are added on top. Uncached usage returns `inputTokens` either way.
|
|
200
|
+
*
|
|
201
|
+
* @param usage - Token usage from a model invocation
|
|
202
|
+
* @returns The total prompt token count
|
|
203
|
+
* @internal
|
|
204
|
+
*/
|
|
205
|
+
export function totalPromptTokens(usage) {
|
|
206
|
+
if (usage.inputTokens + usage.outputTokens === usage.totalTokens) {
|
|
207
|
+
return usage.inputTokens;
|
|
208
|
+
}
|
|
209
|
+
return usage.inputTokens + (usage.cacheReadInputTokens ?? 0) + (usage.cacheWriteInputTokens ?? 0);
|
|
210
|
+
}
|
|
194
211
|
//# sourceMappingURL=streaming.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streaming.js","sourceRoot":"","sources":["../../../src/models/streaming.ts"],"names":[],"mappings":"AA2BA,uDAAuD;AACvD,MAAM,qBAAqB,GAAwB,IAAI,GAAG,CAA2B;IACnF,wBAAwB;IACxB,6BAA6B;IAC7B,6BAA6B;IAC7B,4BAA4B;IAC5B,uBAAuB;IACvB,oBAAoB;IACpB,qBAAqB;CACtB,CAAC,CAAA;AAEF;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAuB;IACxD,OAAO,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;AAC9C,CAAC;AAiBD;;GAEG;AACH,MAAM,OAAO,sBAAsB;IACjC;;OAEG;IACM,IAAI,GAAG,wBAAiC,CAAA;IAEjD;;OAE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