@strands-agents/sdk 1.12.0 → 1.14.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.d.ts +27 -7
- package/dist/src/agent/agent.d.ts.map +1 -1
- package/dist/src/agent/agent.js +141 -38
- package/dist/src/agent/agent.js.map +1 -1
- package/dist/src/agent/continuation.d.ts +36 -0
- package/dist/src/agent/continuation.d.ts.map +1 -0
- package/dist/src/agent/continuation.js +126 -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/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/conversation-manager/sliding-window-conversation-manager.d.ts +25 -0
- package/dist/src/conversation-manager/sliding-window-conversation-manager.d.ts.map +1 -1
- package/dist/src/conversation-manager/sliding-window-conversation-manager.js +90 -2
- package/dist/src/conversation-manager/sliding-window-conversation-manager.js.map +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -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 +51 -19
- package/dist/src/models/bedrock.d.ts.map +1 -1
- package/dist/src/models/bedrock.js +239 -30
- package/dist/src/models/bedrock.js.map +1 -1
- package/dist/src/models/model.d.ts +73 -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/model.d.ts.map +1 -1
- package/dist/src/models/openai/model.js +5 -0
- package/dist/src/models/openai/model.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/sandbox/docker.js +1 -1
- package/dist/src/sandbox/docker.js.map +1 -1
- package/dist/src/sandbox/ssh.js +1 -1
- package/dist/src/sandbox/ssh.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/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 +2 -2
- 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 +82 -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 +219 -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/bash/index.d.ts +13 -7
- package/dist/src/vended-tools/bash/index.d.ts.map +1 -1
- package/dist/src/vended-tools/bash/index.js +13 -6
- package/dist/src/vended-tools/bash/index.js.map +1 -1
- package/dist/src/vended-tools/bash/types.d.ts +4 -24
- package/dist/src/vended-tools/bash/types.d.ts.map +1 -1
- package/dist/src/vended-tools/bash/types.js +4 -31
- package/dist/src/vended-tools/bash/types.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/index.d.ts +1 -0
- package/dist/src/vended-tools/index.d.ts.map +1 -1
- package/dist/src/vended-tools/index.js +1 -0
- package/dist/src/vended-tools/index.js.map +1 -1
- package/dist/src/vended-tools/notebook/notebook.d.ts +1 -1
- package/dist/src/vended-tools/notebook/notebook.d.ts.map +1 -1
- package/dist/src/vended-tools/notebook/notebook.js +20 -6
- package/dist/src/vended-tools/notebook/notebook.js.map +1 -1
- package/dist/src/vended-tools/notebook/types.d.ts +15 -1
- package/dist/src/vended-tools/notebook/types.d.ts.map +1 -1
- package/dist/src/vended-tools/shell/index.d.ts +15 -0
- package/dist/src/vended-tools/shell/index.d.ts.map +1 -0
- package/dist/src/vended-tools/shell/index.js +13 -0
- package/dist/src/vended-tools/shell/index.js.map +1 -0
- package/dist/src/vended-tools/{bash → shell}/make-shell.d.ts +7 -8
- package/dist/src/vended-tools/shell/make-shell.d.ts.map +1 -0
- package/dist/src/vended-tools/{bash → shell}/make-shell.js +16 -9
- package/dist/src/vended-tools/shell/make-shell.js.map +1 -0
- package/dist/src/vended-tools/shell/types.d.ts +47 -0
- package/dist/src/vended-tools/shell/types.d.ts.map +1 -0
- package/dist/src/vended-tools/shell/types.js +34 -0
- package/dist/src/vended-tools/shell/types.js.map +1 -0
- 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 +15 -2
- package/dist/src/vended-tools/bash/make-shell.d.ts.map +0 -1
- package/dist/src/vended-tools/bash/make-shell.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-as-tool.d.ts","sour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,CAAQ;gBAET,MAAM,EAAE,eAAe;IA6CnC;;OAEG;IACH,IAAI,KAAK,IAAI,KAAK,CAEjB;IAEM,MAAM,CAAC,WAAW,EAAE,WAAW,GAAG,mBAAmB;CA8D7D"}
|
|
@@ -98,7 +98,7 @@ export class AgentAsTool extends Tool {
|
|
|
98
98
|
return this._agent;
|
|
99
99
|
}
|
|
100
100
|
async *stream(toolContext) {
|
|
101
|
-
const { toolUse } = toolContext;
|
|
101
|
+
const { toolUse, invocationState, cancelSignal } = toolContext;
|
|
102
102
|
const toolUseId = toolUse.toolUseId;
|
|
103
103
|
// Concurrency guard: loadSnapshot + agent.stream() must not overlap.
|
|
104
104
|
if (this._busy) {
|
|
@@ -114,7 +114,10 @@ export class AgentAsTool extends Tool {
|
|
|
114
114
|
// Stream the sub-agent, forwarding the outer invocation's state so
|
|
115
115
|
// mutations in the inner agent's hooks/tools are visible to the outer
|
|
116
116
|
// agent's downstream callbacks and final AgentResult.
|
|
117
|
-
const gen = this._agent.stream(input, {
|
|
117
|
+
const gen = this._agent.stream(input, {
|
|
118
|
+
invocationState,
|
|
119
|
+
cancelSignal,
|
|
120
|
+
});
|
|
118
121
|
let next = await gen.next();
|
|
119
122
|
while (!next.done) {
|
|
120
123
|
const event = next.value;
|
|
@@ -127,6 +130,9 @@ export class AgentAsTool extends Tool {
|
|
|
127
130
|
next = await gen.next();
|
|
128
131
|
}
|
|
129
132
|
const result = next.value;
|
|
133
|
+
if (result.stopReason === 'cancelled') {
|
|
134
|
+
return createErrorResult(`Agent '${this.name}' cancelled`, toolUseId);
|
|
135
|
+
}
|
|
130
136
|
// Build the tool result
|
|
131
137
|
if (result.structuredOutput !== undefined) {
|
|
132
138
|
return new ToolResultBlock({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-as-tool.js","sourceRoot":"","sources":["../../../src/agent/agent-as-tool.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,OAAO,EAAE,SAAS,EAAE,SAAS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A;gBACjD,CAAC;gBAED,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAA;YACzB,CAAC;YACD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAA;YAEzB,IAAI,MAAM,CAAC,UAAU,KAAK,WAAW,EAAE,CAAC;gBACtC,OAAO,iBAAiB,CAAC,UAAU,IAAI,CAAC,IAAI,aAAa,EAAE,SAAS,CAAC,CAAA;YACvE,CAAC;YAED,wBAAwB;YACxB,IAAI,MAAM,CAAC,gBAAgB,KAAK,SAAS,EAAE,CAAC;gBAC1C,OAAO,IAAI,eAAe,CAAC;oBACzB,SAAS;oBACT,MAAM,EAAE,SAAS;oBACjB,OAAO,EAAE,CAAC,IAAI,SAAS,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,gBAA6B,EAAE,CAAC,CAAC;iBACzE,CAAC,CAAA;YACJ,CAAC;YAED,OAAO,IAAI,eAAe,CAAC;gBACzB,SAAS;gBACT,MAAM,EAAE,SAAS;gBACjB,OAAO,EAAE,CAAC,IAAI,SAAS,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,kCAAkC;aAChF,CAAC,CAAA;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;QAC5C,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QACpB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -29,6 +29,7 @@ import { InterruptState } from '../interrupt.js';
|
|
|
29
29
|
import type { TakeSnapshotOptions } from './snapshot.js';
|
|
30
30
|
import type { Snapshot } from '../types/snapshot.js';
|
|
31
31
|
import type { Sandbox } from '../sandbox/base.js';
|
|
32
|
+
import { ContextManager } from '../context-manager/context-manager.js';
|
|
32
33
|
/**
|
|
33
34
|
* Recursive type definition for nested tool arrays.
|
|
34
35
|
* Allows tools to be organized in nested arrays of any depth.
|
|
@@ -47,16 +48,25 @@ export type ToolList = (Tool | McpClient | Agent | ToolList)[];
|
|
|
47
48
|
* - `'sequential'` — runs tool calls one at a time
|
|
48
49
|
*
|
|
49
50
|
* Cancellation works identically in both modes: {@link Agent.cancel} flips
|
|
50
|
-
* {@link Agent.cancelSignal}
|
|
51
|
+
* {@link Agent.cancelSignal}, which SDK-managed tool contexts expose as `context.cancelSignal`.
|
|
51
52
|
* In concurrent mode, prompt batch-wide cancellation requires every in-flight tool
|
|
52
53
|
* to honor the signal.
|
|
53
54
|
*/
|
|
54
55
|
export type ToolExecutorStrategy = 'sequential' | 'concurrent';
|
|
55
56
|
/**
|
|
56
|
-
* Supported
|
|
57
|
+
* Supported string presets for the `contextManager` parameter.
|
|
57
58
|
*/
|
|
58
59
|
export declare const CONTEXT_MANAGER_STRATEGIES: readonly ["auto", "agentic"];
|
|
59
|
-
|
|
60
|
+
type ContextManagerPreset = (typeof CONTEXT_MANAGER_STRATEGIES)[number];
|
|
61
|
+
/**
|
|
62
|
+
* Supported values for the `contextManager` parameter.
|
|
63
|
+
*
|
|
64
|
+
* - `"auto"`: Managed context with proactive compression + offloading.
|
|
65
|
+
* - `"agentic"`: Model-driven context management via injected tools.
|
|
66
|
+
* - `ContextManager` instance: Full control over strategy-driven offloading.
|
|
67
|
+
* - `false`: Explicitly disable all context management (no compression, no offloading).
|
|
68
|
+
*/
|
|
69
|
+
export type ContextManagerStrategy = ContextManagerPreset | ContextManager | false;
|
|
60
70
|
/**
|
|
61
71
|
* Configuration object for creating a new Agent.
|
|
62
72
|
*/
|
|
@@ -115,13 +125,15 @@ export type AgentConfig = {
|
|
|
115
125
|
*/
|
|
116
126
|
conversationManager?: ConversationManager;
|
|
117
127
|
/**
|
|
118
|
-
* Context management strategy.
|
|
128
|
+
* Context management strategy that controls how messages are compressed and offloaded.
|
|
119
129
|
*
|
|
120
130
|
* - `"auto"`: SummarizingConversationManager with proactive compression + ContextOffloader.
|
|
121
131
|
* - `"agentic"`: (Experimental) Lets the model drive context management via injected tools.
|
|
122
132
|
* This mode may change in future versions.
|
|
133
|
+
* - `ContextManager` instance: Strategy-driven offloading with overflow recovery.
|
|
134
|
+
* - `false`: Explicitly disable context management (no compression, no offloading).
|
|
123
135
|
*
|
|
124
|
-
*
|
|
136
|
+
* When a `ContextManager` instance is provided, any co-provided `conversationManager` is ignored.
|
|
125
137
|
* Defaults to undefined (SlidingWindowConversationManager, no offloader).
|
|
126
138
|
*
|
|
127
139
|
* @remarks The offloader uses in-memory storage by default. When an agent-level
|
|
@@ -270,6 +282,10 @@ export declare class Agent implements LocalAgent, InvokableAgent {
|
|
|
270
282
|
* Optional description of what the agent does.
|
|
271
283
|
*/
|
|
272
284
|
readonly description?: string;
|
|
285
|
+
/**
|
|
286
|
+
* The context manager for strategy-driven offloading, if configured.
|
|
287
|
+
*/
|
|
288
|
+
readonly contextManager?: ContextManager | undefined;
|
|
273
289
|
/**
|
|
274
290
|
* The session manager for saving and restoring agent sessions, if configured.
|
|
275
291
|
*/
|
|
@@ -480,7 +496,7 @@ export declare class Agent implements LocalAgent, InvokableAgent {
|
|
|
480
496
|
/**
|
|
481
497
|
* The cancellation signal for the current invocation.
|
|
482
498
|
*
|
|
483
|
-
*
|
|
499
|
+
* SDK-managed tool contexts receive this as `context.cancelSignal` for cancellable operations.
|
|
484
500
|
* Hooks can check `event.agent.cancelSignal.aborted` to detect cancellation.
|
|
485
501
|
*/
|
|
486
502
|
get cancelSignal(): AbortSignal;
|
|
@@ -494,7 +510,7 @@ export declare class Agent implements LocalAgent, InvokableAgent {
|
|
|
494
510
|
* - At the top of each agent loop cycle
|
|
495
511
|
*
|
|
496
512
|
* If a tool is already executing, it will run to completion unless
|
|
497
|
-
* the tool checks
|
|
513
|
+
* the tool checks `context.cancelSignal` internally.
|
|
498
514
|
*
|
|
499
515
|
* Hook callbacks can check `event.agent.cancelSignal.aborted` to detect
|
|
500
516
|
* cancellation and adjust their behavior accordingly.
|
|
@@ -748,6 +764,7 @@ export declare class Agent implements LocalAgent, InvokableAgent {
|
|
|
748
764
|
* These are separate event classes because they represent different granularities
|
|
749
765
|
* (partial deltas vs finished blocks). Both are yielded in the stream and hookable.
|
|
750
766
|
*
|
|
767
|
+
* @param model - Model selected for this call
|
|
751
768
|
* @param messages - Messages to send to the model
|
|
752
769
|
* @param streamOptions - Options for streaming
|
|
753
770
|
* @returns StreamAggregatedResult containing message, stop reason, and optional redaction message
|
|
@@ -808,5 +825,8 @@ export declare class Agent implements LocalAgent, InvokableAgent {
|
|
|
808
825
|
* @returns MessageAddedEvent to be yielded
|
|
809
826
|
*/
|
|
810
827
|
private _appendMessage;
|
|
828
|
+
private _appendContinuationMessages;
|
|
829
|
+
private _hasOpenUserTurn;
|
|
811
830
|
}
|
|
831
|
+
export {};
|
|
812
832
|
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../../src/agent/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,KAAK,gBAAgB,EAErB,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,gBAAgB,EACtB,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAIL,OAAO,EACP,KAAK,WAAW,EAEhB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EAItB,MAAM,sBAAsB,CAAA;AAE7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAmB,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAI7D,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAyC,MAAM,oBAAoB,CAAA;AAGhG,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAA;AAC3D,OAAO,EAAE,UAAU,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ANGED
|
@@ -30,6 +30,7 @@ import { ConcurrentToolExecutor } from '../tools/executors/concurrent.js';
|
|
|
30
30
|
import { SequentialToolExecutor } from '../tools/executors/sequential.js';
|
|
31
31
|
import { AgentAsTool } from './agent-as-tool.js';
|
|
32
32
|
import { ToolCaller } from './tool-caller.js';
|
|
33
|
+
import { continuations } from './continuation.js';
|
|
33
34
|
import { MemoryManager } from '../memory/memory-manager.js';
|
|
34
35
|
import { SessionManager } from '../session/session-manager.js';
|
|
35
36
|
import { Tracer } from '../telemetry/tracer.js';
|
|
@@ -44,8 +45,9 @@ import { isInterruptResponseContent } from '../types/interrupt.js';
|
|
|
44
45
|
import { takeSnapshot as takeSnapshotInternal, loadSnapshot as loadSnapshotInternal } from './snapshot.js';
|
|
45
46
|
import { defaultSandbox } from '../sandbox/default.js';
|
|
46
47
|
import { summarizeContextTool, truncateContextTool, pinContextTool, createTokenUsageMiddleware, } from '../context-manager/modes/agentic/agentic-context.js';
|
|
48
|
+
import { ContextManager } from '../context-manager/context-manager.js';
|
|
47
49
|
/**
|
|
48
|
-
* Supported
|
|
50
|
+
* Supported string presets for the `contextManager` parameter.
|
|
49
51
|
*/
|
|
50
52
|
export const CONTEXT_MANAGER_STRATEGIES = ['auto', 'agentic'];
|
|
51
53
|
/** Benchmark-validated token threshold for offloading tool results. */
|
|
@@ -65,8 +67,16 @@ const CONTEXT_MANAGER_COMPRESSION_THRESHOLD = 0.85;
|
|
|
65
67
|
* When "auto", uses SummarizingConversationManager with proactive compression.
|
|
66
68
|
* When "agentic", uses SummarizingConversationManager without proactive compression
|
|
67
69
|
* (the agent manages its context via tools; the context manager is only a reactive safety net).
|
|
70
|
+
* When a ContextManager instance, uses NullConversationManager — the ContextManager owns
|
|
71
|
+
* overflow recovery via apply().
|
|
68
72
|
*/
|
|
69
73
|
function resolveConversationManager(contextManager, conversationManager) {
|
|
74
|
+
if (contextManager === false) {
|
|
75
|
+
return conversationManager ?? new NullConversationManager();
|
|
76
|
+
}
|
|
77
|
+
if (contextManager instanceof ContextManager) {
|
|
78
|
+
return new NullConversationManager();
|
|
79
|
+
}
|
|
70
80
|
if (contextManager === 'agentic') {
|
|
71
81
|
return (conversationManager ??
|
|
72
82
|
new SummarizingConversationManager({
|
|
@@ -148,6 +158,10 @@ export class Agent {
|
|
|
148
158
|
* Optional description of what the agent does.
|
|
149
159
|
*/
|
|
150
160
|
description;
|
|
161
|
+
/**
|
|
162
|
+
* The context manager for strategy-driven offloading, if configured.
|
|
163
|
+
*/
|
|
164
|
+
contextManager;
|
|
151
165
|
/**
|
|
152
166
|
* The session manager for saving and restoring agent sessions, if configured.
|
|
153
167
|
*/
|
|
@@ -207,6 +221,7 @@ export class Agent {
|
|
|
207
221
|
this.id = config?.id ?? DEFAULT_AGENT_ID;
|
|
208
222
|
if (config?.description !== undefined)
|
|
209
223
|
this.description = config.description;
|
|
224
|
+
this.contextManager = config?.contextManager instanceof ContextManager ? config.contextManager : undefined;
|
|
210
225
|
this.sessionManager = config?.sessionManager;
|
|
211
226
|
this.storage = config?.storage;
|
|
212
227
|
this.memoryManager =
|
|
@@ -244,7 +259,7 @@ export class Agent {
|
|
|
244
259
|
// Initialize middleware registry
|
|
245
260
|
this._middlewareRegistry = new MiddlewareRegistry();
|
|
246
261
|
if (config?.contextManager === 'agentic') {
|
|
247
|
-
this._middlewareRegistry.addInput(InvokeModelStage.Input, createTokenUsageMiddleware(
|
|
262
|
+
this._middlewareRegistry.addInput(InvokeModelStage.Input, createTokenUsageMiddleware());
|
|
248
263
|
}
|
|
249
264
|
// `undefined` (omitted) → install the default; `null`/`[]` → explicit opt-out.
|
|
250
265
|
const retryStrategies = config?.retryStrategy === null
|
|
@@ -268,6 +283,7 @@ export class Agent {
|
|
|
268
283
|
// when a delegate tool is added (construction, plugin getTools, MCP, runtime).
|
|
269
284
|
// The plugin is a no-op when no delegation tools fire.
|
|
270
285
|
const hasAgentDelegation = (config?.plugins ?? []).some((p) => p.name === 'strands:agent-delegation');
|
|
286
|
+
const contextManagerPlugin = config?.contextManager instanceof ContextManager ? config.contextManager : undefined;
|
|
271
287
|
this._pluginRegistry = new PluginRegistry([
|
|
272
288
|
this._conversationManager,
|
|
273
289
|
...retryStrategies,
|
|
@@ -284,6 +300,7 @@ export class Agent {
|
|
|
284
300
|
]
|
|
285
301
|
: []),
|
|
286
302
|
...(this.memoryManager ? [this.memoryManager] : []),
|
|
303
|
+
...(contextManagerPlugin ? [contextManagerPlugin] : []),
|
|
287
304
|
...(config?.sessionManager ? [config.sessionManager] : []),
|
|
288
305
|
new ModelPlugin(this.model),
|
|
289
306
|
]);
|
|
@@ -533,7 +550,7 @@ export class Agent {
|
|
|
533
550
|
/**
|
|
534
551
|
* The cancellation signal for the current invocation.
|
|
535
552
|
*
|
|
536
|
-
*
|
|
553
|
+
* SDK-managed tool contexts receive this as `context.cancelSignal` for cancellable operations.
|
|
537
554
|
* Hooks can check `event.agent.cancelSignal.aborted` to detect cancellation.
|
|
538
555
|
*/
|
|
539
556
|
get cancelSignal() {
|
|
@@ -549,7 +566,7 @@ export class Agent {
|
|
|
549
566
|
* - At the top of each agent loop cycle
|
|
550
567
|
*
|
|
551
568
|
* If a tool is already executing, it will run to completion unless
|
|
552
|
-
* the tool checks
|
|
569
|
+
* the tool checks `context.cancelSignal` internally.
|
|
553
570
|
*
|
|
554
571
|
* Hook callbacks can check `event.agent.cancelSignal.aborted` to detect
|
|
555
572
|
* cancellation and adjust their behavior accordingly.
|
|
@@ -637,6 +654,7 @@ export class Agent {
|
|
|
637
654
|
*/
|
|
638
655
|
async *stream(args, options) {
|
|
639
656
|
this.acquireLock();
|
|
657
|
+
let continuationEvent;
|
|
640
658
|
try {
|
|
641
659
|
await this.initialize();
|
|
642
660
|
// Thread the resolved invocationState so all layers share the same reference.
|
|
@@ -664,6 +682,8 @@ export class Agent {
|
|
|
664
682
|
const message = new Message({ role: 'assistant', content: [new TextBlock(cancelText)] });
|
|
665
683
|
yield this._appendMessage(message, invocationState);
|
|
666
684
|
const afterEvent = new AfterInvocationEvent({ agent: this, invocationState });
|
|
685
|
+
await continuations.abandon(continuationEvent, new Error('Continuation was not incorporated into agent history'));
|
|
686
|
+
continuationEvent = afterEvent;
|
|
667
687
|
await this._invokeCallbacks(afterEvent);
|
|
668
688
|
yield afterEvent;
|
|
669
689
|
return new AgentResult({
|
|
@@ -678,7 +698,7 @@ export class Agent {
|
|
|
678
698
|
let caughtError;
|
|
679
699
|
const afterInvocationEvent = new AfterInvocationEvent({ agent: this, invocationState });
|
|
680
700
|
try {
|
|
681
|
-
result = yield* this._streamWithMiddleware(currentArgs, resolvedOptions, invocationState);
|
|
701
|
+
result = yield* this._streamWithMiddleware(currentArgs, resolvedOptions, invocationState, continuationEvent);
|
|
682
702
|
}
|
|
683
703
|
catch (error) {
|
|
684
704
|
caughtError = error;
|
|
@@ -687,6 +707,8 @@ export class Agent {
|
|
|
687
707
|
// AfterInvocationEvent always fires — even on error or consumer break. Outside middleware.
|
|
688
708
|
// Invoke hooks (so .resume can be set) but don't yield in finally (yields in finally
|
|
689
709
|
// suspend the generator on consumer break instead of completing cleanup).
|
|
710
|
+
await continuations.abandon(continuationEvent, new Error('Continuation was not incorporated into agent history'));
|
|
711
|
+
continuationEvent = afterInvocationEvent;
|
|
690
712
|
await this._invokeCallbacks(afterInvocationEvent);
|
|
691
713
|
}
|
|
692
714
|
// Yield outside finally — in JS, a `yield` inside `finally` suspends the generator
|
|
@@ -697,9 +719,15 @@ export class Agent {
|
|
|
697
719
|
if (caughtError) {
|
|
698
720
|
throw caughtError;
|
|
699
721
|
}
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
722
|
+
const stopReason = result.stopReason;
|
|
723
|
+
const allowsContinuation = stopReason === 'endTurn' || stopReason === 'stopSequence';
|
|
724
|
+
if (!allowsContinuation && stopReason !== 'interrupt') {
|
|
725
|
+
await continuations.abandon(afterInvocationEvent, new Error(`Continuation abandoned after ${stopReason}`));
|
|
726
|
+
}
|
|
727
|
+
const hasContinuation = (await continuations.prepare(afterInvocationEvent, (continuationArgs) => this._normalizeInput(continuationArgs), stopReason)) !== undefined;
|
|
728
|
+
continuationEvent = hasContinuation ? afterInvocationEvent : undefined;
|
|
729
|
+
if (hasContinuation || afterInvocationEvent.resume !== undefined) {
|
|
730
|
+
currentArgs = afterInvocationEvent.resume ?? [];
|
|
703
731
|
continue;
|
|
704
732
|
}
|
|
705
733
|
// Only emit AgentResultEvent on the final iteration (not on resumed ones).
|
|
@@ -712,6 +740,7 @@ export class Agent {
|
|
|
712
740
|
}
|
|
713
741
|
}
|
|
714
742
|
finally {
|
|
743
|
+
await continuations.abandon(continuationEvent, new Error('Agent stream closed before continuation input was incorporated into agent history'));
|
|
715
744
|
this._isInvoking = false;
|
|
716
745
|
}
|
|
717
746
|
}
|
|
@@ -719,7 +748,7 @@ export class Agent {
|
|
|
719
748
|
* Invokes the AgentStreamStage middleware chain.
|
|
720
749
|
* Hooks fire outside this method (in stream()'s resume loop).
|
|
721
750
|
*/
|
|
722
|
-
async *_streamWithMiddleware(args, options, invocationState) {
|
|
751
|
+
async *_streamWithMiddleware(args, options, invocationState, continuationEvent) {
|
|
723
752
|
// Snapshot so a gate that re-reads its response after next() still resolves even if a tool cycle called deactivate().
|
|
724
753
|
const interruptsSnapshot = { ...this._interruptState.interrupts };
|
|
725
754
|
const context = {
|
|
@@ -733,7 +762,8 @@ export class Agent {
|
|
|
733
762
|
const self = this;
|
|
734
763
|
try {
|
|
735
764
|
const { result } = yield* this._middlewareRegistry.invoke(AgentStreamStage, context, async function* (ctx) {
|
|
736
|
-
const
|
|
765
|
+
const streamArgs = continuations.combine(continuationEvent, ctx.args, (continuationArgs) => self._normalizeInput(continuationArgs));
|
|
766
|
+
const result = yield* self._streamCore(streamArgs, ctx.options, streamArgs === ctx.args ? undefined : continuationEvent);
|
|
737
767
|
return { result };
|
|
738
768
|
});
|
|
739
769
|
if (this._interruptState.activated &&
|
|
@@ -770,8 +800,8 @@ export class Agent {
|
|
|
770
800
|
* Single-pass stream through _stream() with event processing.
|
|
771
801
|
* No resume loop, no lifecycle events — those are handled by stream()'s resume loop.
|
|
772
802
|
*/
|
|
773
|
-
async *_streamCore(args, options) {
|
|
774
|
-
const streamGenerator = this._stream(args, options);
|
|
803
|
+
async *_streamCore(args, options, continuationEvent) {
|
|
804
|
+
const streamGenerator = this._stream(args, options, continuationEvent);
|
|
775
805
|
let caughtError;
|
|
776
806
|
let iterationResult;
|
|
777
807
|
try {
|
|
@@ -933,7 +963,7 @@ export class Agent {
|
|
|
933
963
|
* @param options - Optional per-invocation options
|
|
934
964
|
* @returns Async generator that yields AgentStreamEvent objects and returns AgentResult
|
|
935
965
|
*/
|
|
936
|
-
async *_stream(args, options) {
|
|
966
|
+
async *_stream(args, options, continuationEvent) {
|
|
937
967
|
let currentArgs = args;
|
|
938
968
|
let result;
|
|
939
969
|
this._validateLimits(options);
|
|
@@ -1016,8 +1046,13 @@ export class Agent {
|
|
|
1016
1046
|
// Normalize input and append user messages on first invocation only
|
|
1017
1047
|
if (currentArgs !== undefined) {
|
|
1018
1048
|
const messagesToAppend = this._normalizeInput(currentArgs);
|
|
1019
|
-
|
|
1020
|
-
yield this.
|
|
1049
|
+
if (continuationEvent) {
|
|
1050
|
+
yield* await this._appendContinuationMessages(messagesToAppend, continuationEvent, invocationState);
|
|
1051
|
+
}
|
|
1052
|
+
else {
|
|
1053
|
+
for (const message of messagesToAppend) {
|
|
1054
|
+
yield this._appendMessage(message, invocationState);
|
|
1055
|
+
}
|
|
1021
1056
|
}
|
|
1022
1057
|
currentArgs = undefined;
|
|
1023
1058
|
}
|
|
@@ -1203,7 +1238,9 @@ export class Agent {
|
|
|
1203
1238
|
role: 'assistant',
|
|
1204
1239
|
content: [new TextBlock('Cancelled by user')],
|
|
1205
1240
|
});
|
|
1206
|
-
|
|
1241
|
+
if (this._hasOpenUserTurn()) {
|
|
1242
|
+
yield this._appendMessage(cancelMessage, invocationState);
|
|
1243
|
+
}
|
|
1207
1244
|
result = new AgentResult({
|
|
1208
1245
|
stopReason: 'cancelled',
|
|
1209
1246
|
lastMessage: cancelMessage,
|
|
@@ -1233,14 +1270,16 @@ export class Agent {
|
|
|
1233
1270
|
}
|
|
1234
1271
|
finally {
|
|
1235
1272
|
// If cancelled but the catch block was bypassed (generator terminated
|
|
1236
|
-
// via .return() when the consumer breaks out of for-await),
|
|
1237
|
-
//
|
|
1273
|
+
// via .return() when the consumer breaks out of for-await), close an
|
|
1274
|
+
// existing user turn so the agent can be reinvoked.
|
|
1238
1275
|
if (!caughtError && !result && this.isCancelled) {
|
|
1239
1276
|
const cancelMessage = new Message({
|
|
1240
1277
|
role: 'assistant',
|
|
1241
1278
|
content: [new TextBlock('Cancelled by user')],
|
|
1242
1279
|
});
|
|
1243
|
-
|
|
1280
|
+
if (this._hasOpenUserTurn()) {
|
|
1281
|
+
yield this._appendMessage(cancelMessage, invocationState);
|
|
1282
|
+
}
|
|
1244
1283
|
}
|
|
1245
1284
|
this._tracer.endAgentSpan(agentSpan, {
|
|
1246
1285
|
...(caughtError && { error: caughtError }),
|
|
@@ -1432,8 +1471,18 @@ export class Agent {
|
|
|
1432
1471
|
invocationState,
|
|
1433
1472
|
...(projectedInputTokens !== undefined && { projectedInputTokens }),
|
|
1434
1473
|
});
|
|
1435
|
-
|
|
1474
|
+
let modelContinuation;
|
|
1475
|
+
try {
|
|
1476
|
+
yield beforeModelCallEvent;
|
|
1477
|
+
modelContinuation = await continuations.prepare(beforeModelCallEvent, (continuationArgs) => this._normalizeInput(continuationArgs));
|
|
1478
|
+
}
|
|
1479
|
+
finally {
|
|
1480
|
+
if (modelContinuation === undefined) {
|
|
1481
|
+
await continuations.abandon(beforeModelCallEvent, new Error('Agent stream closed before continuation input was incorporated into agent history'));
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1436
1484
|
if (beforeModelCallEvent.cancel) {
|
|
1485
|
+
await continuations.abandon(beforeModelCallEvent, new Error('Continuation abandoned by BeforeModelCallEvent'));
|
|
1437
1486
|
const cancelText = typeof beforeModelCallEvent.cancel === 'string' ? beforeModelCallEvent.cancel : 'model call denied by hook';
|
|
1438
1487
|
const message = new Message({ role: 'assistant', content: [new TextBlock(cancelText)] });
|
|
1439
1488
|
const stopData = { message, stopReason: 'endTurn' };
|
|
@@ -1451,6 +1500,16 @@ export class Agent {
|
|
|
1451
1500
|
}
|
|
1452
1501
|
return { message, stopReason: 'endTurn' };
|
|
1453
1502
|
}
|
|
1503
|
+
yield* await this._appendContinuationMessages(modelContinuation ?? [], beforeModelCallEvent, invocationState);
|
|
1504
|
+
if (modelContinuation !== undefined) {
|
|
1505
|
+
try {
|
|
1506
|
+
projectedInputTokens = await this._estimateInputTokens(streamOptions);
|
|
1507
|
+
}
|
|
1508
|
+
catch (error) {
|
|
1509
|
+
projectedInputTokens = undefined;
|
|
1510
|
+
logger.debug(`error=<${error}> | token estimation failed after continuation input, proceeding without estimate`);
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1454
1513
|
try {
|
|
1455
1514
|
const result = yield* this._invokeModelWithMiddleware(invocationState, toolChoice, projectedInputTokens);
|
|
1456
1515
|
// Accumulate token usage and model latency metrics
|
|
@@ -1524,6 +1583,7 @@ export class Agent {
|
|
|
1524
1583
|
async *_invokeModelWithMiddleware(invocationState, toolChoice, projectedInputTokens) {
|
|
1525
1584
|
const context = {
|
|
1526
1585
|
agent: this,
|
|
1586
|
+
model: this.model,
|
|
1527
1587
|
messages: this.messages.map((msg) => msg.clone()),
|
|
1528
1588
|
...(this.systemPrompt !== undefined && { systemPrompt: cloneSystemPrompt(this.systemPrompt) }),
|
|
1529
1589
|
toolSpecs: deepCopy(this._toolRegistry.list().map((tool) => tool.toolSpec)),
|
|
@@ -1540,7 +1600,7 @@ export class Agent {
|
|
|
1540
1600
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
1541
1601
|
const self = this;
|
|
1542
1602
|
const middlewareResult = yield* this._middlewareRegistry.invoke(InvokeModelStage, context, async function* (ctx) {
|
|
1543
|
-
const modelId =
|
|
1603
|
+
const modelId = ctx.model.modelId;
|
|
1544
1604
|
const modelSpan = self._tracer.startModelInvokeSpan({
|
|
1545
1605
|
messages: ctx.messages,
|
|
1546
1606
|
...(modelId && { modelId }),
|
|
@@ -1551,12 +1611,15 @@ export class Agent {
|
|
|
1551
1611
|
// get/set methods.
|
|
1552
1612
|
tempModelState = new StateStore(modelStateSnapshot);
|
|
1553
1613
|
const streamOptions = {
|
|
1614
|
+
cancelSignal: self._abortSignal,
|
|
1554
1615
|
toolSpecs: ctx.toolSpecs,
|
|
1555
1616
|
modelState: tempModelState,
|
|
1556
1617
|
...(ctx.systemPrompt !== undefined && { systemPrompt: ctx.systemPrompt }),
|
|
1557
1618
|
...(ctx.toolChoice && { toolChoice: ctx.toolChoice }),
|
|
1619
|
+
// Omitted when zero, so an ordinary call's options are unchanged.
|
|
1620
|
+
...(ctx.dynamicTrailingBlocks ? { dynamicTrailingBlocks: ctx.dynamicTrailingBlocks } : {}),
|
|
1558
1621
|
};
|
|
1559
|
-
const gen = self._streamFromModel(ctx.messages, streamOptions, ctx.invocationState);
|
|
1622
|
+
const gen = self._streamFromModel(ctx.model, ctx.messages, streamOptions, ctx.invocationState);
|
|
1560
1623
|
let iterResult = await gen.next();
|
|
1561
1624
|
while (!iterResult.done) {
|
|
1562
1625
|
yield iterResult.value;
|
|
@@ -1597,29 +1660,38 @@ export class Agent {
|
|
|
1597
1660
|
* These are separate event classes because they represent different granularities
|
|
1598
1661
|
* (partial deltas vs finished blocks). Both are yielded in the stream and hookable.
|
|
1599
1662
|
*
|
|
1663
|
+
* @param model - Model selected for this call
|
|
1600
1664
|
* @param messages - Messages to send to the model
|
|
1601
1665
|
* @param streamOptions - Options for streaming
|
|
1602
1666
|
* @returns StreamAggregatedResult containing message, stop reason, and optional redaction message
|
|
1603
1667
|
*/
|
|
1604
|
-
async *_streamFromModel(messages, streamOptions, invocationState) {
|
|
1668
|
+
async *_streamFromModel(model, messages, streamOptions, invocationState) {
|
|
1605
1669
|
messages = normalizeToolUseNames(messages);
|
|
1606
|
-
const streamGenerator =
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1670
|
+
const streamGenerator = model.streamAggregated(messages, streamOptions);
|
|
1671
|
+
try {
|
|
1672
|
+
let result = await streamGenerator.next();
|
|
1673
|
+
while (!result.done) {
|
|
1674
|
+
this._throwIfCancelled();
|
|
1675
|
+
const event = result.value;
|
|
1676
|
+
if (isModelStreamEvent(event)) {
|
|
1677
|
+
// ModelStreamEvent: wrap in ModelStreamUpdateEvent
|
|
1678
|
+
yield new ModelStreamUpdateEvent({ agent: this, event, invocationState });
|
|
1679
|
+
}
|
|
1680
|
+
else {
|
|
1681
|
+
// ContentBlock: wrap in ContentBlockEvent
|
|
1682
|
+
yield new ContentBlockEvent({ agent: this, contentBlock: event, invocationState });
|
|
1683
|
+
}
|
|
1684
|
+
result = await streamGenerator.next();
|
|
1618
1685
|
}
|
|
1619
|
-
result
|
|
1686
|
+
// result.done is true, result.value contains the return value
|
|
1687
|
+
return result.value;
|
|
1688
|
+
}
|
|
1689
|
+
catch (error) {
|
|
1690
|
+
// Provider transports typically surface cancellation as AbortError.
|
|
1691
|
+
// Preserve the Agent cancellation contract when the shared signal fired.
|
|
1692
|
+
this._throwIfCancelled();
|
|
1693
|
+
throw error;
|
|
1620
1694
|
}
|
|
1621
|
-
// result.done is true, result.value contains the return value
|
|
1622
|
-
return result.value;
|
|
1623
1695
|
}
|
|
1624
1696
|
/**
|
|
1625
1697
|
* Emits `BeforeToolsEvent`, handles the pre-launch cancel paths, then
|
|
@@ -1676,6 +1748,7 @@ export class Agent {
|
|
|
1676
1748
|
middlewareRegistry: this._middlewareRegistry,
|
|
1677
1749
|
tracer: this._tracer,
|
|
1678
1750
|
meter: this._meter,
|
|
1751
|
+
cancelSignal: this._abortSignal,
|
|
1679
1752
|
}, {
|
|
1680
1753
|
toolUseBlocks,
|
|
1681
1754
|
toolResultBlocks,
|
|
@@ -1812,6 +1885,36 @@ export class Agent {
|
|
|
1812
1885
|
this.messages.push(message);
|
|
1813
1886
|
return new MessageAddedEvent({ agent: this, message, invocationState });
|
|
1814
1887
|
}
|
|
1888
|
+
async _appendContinuationMessages(messages, continuationEvent, invocationState) {
|
|
1889
|
+
const events = [];
|
|
1890
|
+
for (const message of messages) {
|
|
1891
|
+
const lastMessage = this.messages.at(-1);
|
|
1892
|
+
if (lastMessage?.role !== message.role) {
|
|
1893
|
+
events.push(this._appendMessage(message, invocationState));
|
|
1894
|
+
continue;
|
|
1895
|
+
}
|
|
1896
|
+
const appendedMessage = new Message({
|
|
1897
|
+
role: message.role,
|
|
1898
|
+
content: [...lastMessage.content, ...message.content],
|
|
1899
|
+
trackingId: lastMessage.trackingId,
|
|
1900
|
+
...(lastMessage.metadata !== undefined && { metadata: lastMessage.metadata }),
|
|
1901
|
+
});
|
|
1902
|
+
this.messages[this.messages.length - 1] = appendedMessage;
|
|
1903
|
+
const messageEvent = new MessageAddedEvent({ agent: this, message: appendedMessage, invocationState });
|
|
1904
|
+
if (events.at(-1)?.message === lastMessage) {
|
|
1905
|
+
events[events.length - 1] = messageEvent;
|
|
1906
|
+
}
|
|
1907
|
+
else {
|
|
1908
|
+
events.push(messageEvent);
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
await continuations.markAppended(continuationEvent);
|
|
1912
|
+
return events;
|
|
1913
|
+
}
|
|
1914
|
+
_hasOpenUserTurn() {
|
|
1915
|
+
const lastMessage = this.messages[this.messages.length - 1];
|
|
1916
|
+
return lastMessage?.role === 'user';
|
|
1917
|
+
}
|
|
1815
1918
|
}
|
|
1816
1919
|
const INVALID_TOOL_NAME_PLACEHOLDER = 'INVALID_TOOL_NAME';
|
|
1817
1920
|
/**
|