@strands-agents/sdk 1.10.0 → 1.11.1
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/README.md +1 -1
- package/dist/src/agent/agent.d.ts +22 -42
- package/dist/src/agent/agent.d.ts.map +1 -1
- package/dist/src/agent/agent.js +72 -425
- package/dist/src/agent/agent.js.map +1 -1
- package/dist/src/context-manager/modes/agentic/agentic-context.d.ts +2 -2
- package/dist/src/experimental/vended-tools/stop/index.d.ts +8 -0
- package/dist/src/experimental/vended-tools/stop/index.d.ts.map +1 -0
- package/dist/src/experimental/vended-tools/stop/index.js +6 -0
- package/dist/src/experimental/vended-tools/stop/index.js.map +1 -0
- package/dist/src/experimental/vended-tools/stop/stop.d.ts +51 -0
- package/dist/src/experimental/vended-tools/stop/stop.d.ts.map +1 -0
- package/dist/src/experimental/vended-tools/stop/stop.js +89 -0
- package/dist/src/experimental/vended-tools/stop/stop.js.map +1 -0
- package/dist/src/experimental/vended-tools/stop/types.d.ts +54 -0
- package/dist/src/experimental/vended-tools/stop/types.d.ts.map +1 -0
- package/dist/src/experimental/vended-tools/stop/types.js +58 -0
- package/dist/src/experimental/vended-tools/stop/types.js.map +1 -0
- package/dist/src/hooks/events.d.ts +5 -4
- package/dist/src/hooks/events.d.ts.map +1 -1
- package/dist/src/hooks/events.js +3 -3
- package/dist/src/hooks/events.js.map +1 -1
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/index.node.d.ts.map +1 -1
- package/dist/src/index.node.js +2 -6
- package/dist/src/index.node.js.map +1 -1
- package/dist/src/middleware/interrupt.d.ts +18 -0
- package/dist/src/middleware/interrupt.d.ts.map +1 -0
- package/dist/src/middleware/interrupt.js +33 -0
- package/dist/src/middleware/interrupt.js.map +1 -0
- package/dist/src/models/openai/errors.d.ts.map +1 -1
- package/dist/src/models/openai/errors.js +1 -0
- package/dist/src/models/openai/errors.js.map +1 -1
- package/dist/src/models/openai/mantle.d.ts +2 -1
- package/dist/src/models/openai/mantle.d.ts.map +1 -1
- package/dist/src/models/openai/mantle.js +24 -3
- package/dist/src/models/openai/mantle.js.map +1 -1
- package/dist/src/models/openai/responses-adapter.d.ts.map +1 -1
- package/dist/src/models/openai/responses-adapter.js +41 -9
- package/dist/src/models/openai/responses-adapter.js.map +1 -1
- package/dist/src/models/vercel.d.ts.map +1 -1
- package/dist/src/models/vercel.js +28 -1
- package/dist/src/models/vercel.js.map +1 -1
- package/dist/src/register-node-defaults.d.ts +3 -0
- package/dist/src/register-node-defaults.d.ts.map +1 -0
- package/dist/src/register-node-defaults.js +10 -0
- package/dist/src/register-node-defaults.js.map +1 -0
- package/dist/src/sandbox/register-node-defaults.d.ts +3 -0
- package/dist/src/sandbox/register-node-defaults.d.ts.map +1 -0
- package/dist/src/sandbox/register-node-defaults.js +8 -0
- package/dist/src/sandbox/register-node-defaults.js.map +1 -0
- package/dist/src/session/session-manager.d.ts +3 -2
- package/dist/src/session/session-manager.d.ts.map +1 -1
- package/dist/src/session/session-manager.js +2 -2
- package/dist/src/session/session-manager.js.map +1 -1
- package/dist/src/tools/executors/concurrent.d.ts +32 -0
- package/dist/src/tools/executors/concurrent.d.ts.map +1 -0
- package/dist/src/tools/executors/concurrent.js +128 -0
- package/dist/src/tools/executors/concurrent.js.map +1 -0
- package/dist/src/tools/executors/executor.d.ts +62 -0
- package/dist/src/tools/executors/executor.d.ts.map +1 -0
- package/dist/src/tools/executors/executor.js +193 -0
- package/dist/src/tools/executors/executor.js.map +1 -0
- package/dist/src/tools/executors/sequential.d.ts +30 -0
- package/dist/src/tools/executors/sequential.d.ts.map +1 -0
- package/dist/src/tools/executors/sequential.js +67 -0
- package/dist/src/tools/executors/sequential.js.map +1 -0
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/vended-memory-stores/test-memory-store/store.d.ts +32 -24
- 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 +84 -91
- package/dist/src/vended-memory-stores/test-memory-store/store.js.map +1 -1
- package/dist/src/vended-plugins/context-offloader/storage.d.ts.map +1 -1
- package/dist/src/vended-plugins/context-offloader/storage.js +4 -0
- package/dist/src/vended-plugins/context-offloader/storage.js.map +1 -1
- package/dist/src/vended-tools/index.d.ts +2 -1
- package/dist/src/vended-tools/index.d.ts.map +1 -1
- package/dist/src/vended-tools/index.js +2 -1
- package/dist/src/vended-tools/index.js.map +1 -1
- package/dist/src/vended-tools/sleep/index.d.ts +9 -0
- package/dist/src/vended-tools/sleep/index.d.ts.map +1 -0
- package/dist/src/vended-tools/sleep/index.js +7 -0
- package/dist/src/vended-tools/sleep/index.js.map +1 -0
- package/dist/src/vended-tools/sleep/make-sleep.d.ts +48 -0
- package/dist/src/vended-tools/sleep/make-sleep.d.ts.map +1 -0
- package/dist/src/vended-tools/sleep/make-sleep.js +81 -0
- package/dist/src/vended-tools/sleep/make-sleep.js.map +1 -0
- package/dist/src/vended-tools/sleep/sleep.d.ts +19 -0
- package/dist/src/vended-tools/sleep/sleep.d.ts.map +1 -0
- package/dist/src/vended-tools/sleep/sleep.js +20 -0
- package/dist/src/vended-tools/sleep/sleep.js.map +1 -0
- package/dist/src/vended-tools/sleep/types.d.ts +31 -0
- package/dist/src/vended-tools/sleep/types.d.ts.map +1 -0
- package/dist/src/vended-tools/sleep/types.js +22 -0
- package/dist/src/vended-tools/sleep/types.js.map +1 -0
- package/package.json +9 -1
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { normalizeError } from '../../errors.js';
|
|
2
|
+
import { AfterToolCallEvent, BeforeToolCallEvent, ToolStreamUpdateEvent } from '../../hooks/events.js';
|
|
3
|
+
import { InterruptError, interruptFromAgent } from '../../interrupt.js';
|
|
4
|
+
import { createMiddlewareInterrupt } from '../../middleware/interrupt.js';
|
|
5
|
+
import { ExecuteToolStage } from '../../middleware/index.js';
|
|
6
|
+
import { deepCopy } from '../../types/json.js';
|
|
7
|
+
import { TextBlock, ToolResultBlock } from '../../types/messages.js';
|
|
8
|
+
/**
|
|
9
|
+
* Shared pipeline for executing tool calls.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export class ToolExecutor {
|
|
14
|
+
// Tool lookup and tool-body failures become ToolResultBlocks so the model can
|
|
15
|
+
// respond to them; lifecycle and middleware failures may still propagate.
|
|
16
|
+
async *executeTool(options, toolUseBlock, invocationState) {
|
|
17
|
+
const registryTool = options.agent.toolRegistry.get(toolUseBlock.name);
|
|
18
|
+
// Callbacks may replace or mutate this value inside BeforeToolCallEvent.
|
|
19
|
+
let toolUse = {
|
|
20
|
+
name: toolUseBlock.name,
|
|
21
|
+
toolUseId: toolUseBlock.toolUseId,
|
|
22
|
+
input: toolUseBlock.input,
|
|
23
|
+
};
|
|
24
|
+
while (true) {
|
|
25
|
+
const beforeToolCallEvent = new BeforeToolCallEvent({
|
|
26
|
+
agent: options.agent,
|
|
27
|
+
toolUse,
|
|
28
|
+
tool: registryTool,
|
|
29
|
+
invocationState,
|
|
30
|
+
});
|
|
31
|
+
yield beforeToolCallEvent;
|
|
32
|
+
toolUse = {
|
|
33
|
+
...beforeToolCallEvent.toolUse,
|
|
34
|
+
toolUseId: toolUseBlock.toolUseId,
|
|
35
|
+
};
|
|
36
|
+
// selectedTool takes precedence over resolving a hook-renamed toolUse;
|
|
37
|
+
// otherwise retain the original registry lookup. Resolve before cancellation
|
|
38
|
+
// so AfterToolCallEvent reports the same effective tool on every path.
|
|
39
|
+
const effectiveTool = beforeToolCallEvent.selectedTool ??
|
|
40
|
+
(toolUse.name !== toolUseBlock.name ? options.agent.toolRegistry.get(toolUse.name) : registryTool);
|
|
41
|
+
if (beforeToolCallEvent.cancel) {
|
|
42
|
+
const cancelMessage = typeof beforeToolCallEvent.cancel === 'string' ? beforeToolCallEvent.cancel : 'Tool cancelled by hook';
|
|
43
|
+
const cancelResult = new ToolResultBlock({
|
|
44
|
+
toolUseId: toolUse.toolUseId,
|
|
45
|
+
status: 'error',
|
|
46
|
+
content: [new TextBlock(cancelMessage)],
|
|
47
|
+
});
|
|
48
|
+
const afterToolCallEvent = new AfterToolCallEvent({
|
|
49
|
+
agent: options.agent,
|
|
50
|
+
toolUse,
|
|
51
|
+
tool: effectiveTool,
|
|
52
|
+
result: cancelResult,
|
|
53
|
+
invocationState,
|
|
54
|
+
});
|
|
55
|
+
yield afterToolCallEvent;
|
|
56
|
+
if (this._shouldRetry(options.agent, afterToolCallEvent)) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
return this._normalizeToolResultId(afterToolCallEvent.result, toolUseBlock.toolUseId);
|
|
60
|
+
}
|
|
61
|
+
const toolResult = this._normalizeToolResultId(yield* this._executeToolWithMiddleware(options, effectiveTool, toolUse, invocationState), toolUseBlock.toolUseId);
|
|
62
|
+
const error = toolResult.error;
|
|
63
|
+
const afterToolCallEvent = new AfterToolCallEvent({
|
|
64
|
+
agent: options.agent,
|
|
65
|
+
toolUse,
|
|
66
|
+
tool: effectiveTool,
|
|
67
|
+
result: toolResult,
|
|
68
|
+
invocationState,
|
|
69
|
+
...(error !== undefined && { error }),
|
|
70
|
+
});
|
|
71
|
+
yield afterToolCallEvent;
|
|
72
|
+
if (this._shouldRetry(options.agent, afterToolCallEvent)) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
// Return the hook-transformed result so downstream events and the model
|
|
76
|
+
// observe the same value.
|
|
77
|
+
return this._normalizeToolResultId(afterToolCallEvent.result, toolUseBlock.toolUseId);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
_shouldRetry(agent, afterToolCallEvent) {
|
|
81
|
+
return afterToolCallEvent.retry === true && !agent.cancelSignal.aborted;
|
|
82
|
+
}
|
|
83
|
+
_normalizeToolResultId(result, toolUseId) {
|
|
84
|
+
if (result.toolUseId === toolUseId) {
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
return new ToolResultBlock({
|
|
88
|
+
toolUseId,
|
|
89
|
+
status: result.status,
|
|
90
|
+
content: result.content,
|
|
91
|
+
...(result.error !== undefined && { error: result.error }),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
// Keys and serialized results use model-issued toolUseIds so resume and
|
|
95
|
+
// provider correlation match the assistant's tool-use blocks.
|
|
96
|
+
_storePendingToolExecution(options, assistantMessage, completedToolResults) {
|
|
97
|
+
const serializedResults = {};
|
|
98
|
+
for (const [toolUseId, result] of completedToolResults) {
|
|
99
|
+
serializedResults[toolUseId] = result.toJSON();
|
|
100
|
+
}
|
|
101
|
+
options.agent._interruptState.setPendingToolExecution({
|
|
102
|
+
assistantMessageData: assistantMessage.toJSON(),
|
|
103
|
+
completedToolResults: serializedResults,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async *_executeToolWithMiddleware(options, tool, toolUse, invocationState) {
|
|
107
|
+
const context = {
|
|
108
|
+
agent: options.agent,
|
|
109
|
+
tool,
|
|
110
|
+
toolUse: deepCopy(toolUse),
|
|
111
|
+
invocationState,
|
|
112
|
+
interrupt: createMiddlewareInterrupt(options.agent._interruptState, `middleware:executeTool:${toolUse.toolUseId}`),
|
|
113
|
+
};
|
|
114
|
+
// async function* does not bind lexical `this`; capture the executor for the terminal callback.
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
116
|
+
const executor = this;
|
|
117
|
+
const middlewareResult = yield* options.middlewareRegistry.invoke(ExecuteToolStage, context, async function* (middlewareContext) {
|
|
118
|
+
const result = yield* executor._executeToolCore(options, middlewareContext.tool, middlewareContext.toolUse, middlewareContext.invocationState);
|
|
119
|
+
return { result };
|
|
120
|
+
});
|
|
121
|
+
return middlewareResult.result;
|
|
122
|
+
}
|
|
123
|
+
async *_executeToolCore(options, effectiveTool, toolUse, invocationState) {
|
|
124
|
+
const toolSpan = options.tracer.startToolCallSpan({ tool: toolUse });
|
|
125
|
+
const toolStartTime = Date.now();
|
|
126
|
+
let toolResult;
|
|
127
|
+
let error;
|
|
128
|
+
try {
|
|
129
|
+
if (!effectiveTool) {
|
|
130
|
+
toolResult = new ToolResultBlock({
|
|
131
|
+
toolUseId: toolUse.toolUseId,
|
|
132
|
+
status: 'error',
|
|
133
|
+
content: [new TextBlock(`Tool '${toolUse.name}' not found in registry`)],
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
const toolContext = {
|
|
138
|
+
toolUse: {
|
|
139
|
+
name: toolUse.name,
|
|
140
|
+
toolUseId: toolUse.toolUseId,
|
|
141
|
+
input: toolUse.input,
|
|
142
|
+
},
|
|
143
|
+
agent: options.agent,
|
|
144
|
+
invocationState,
|
|
145
|
+
interrupt: (params) => interruptFromAgent(options.agent, `tool:${toolUse.toolUseId}:${params.name}`, params, 'tool'),
|
|
146
|
+
};
|
|
147
|
+
// Iterate manually to wrap raw tool events at the agent boundary and
|
|
148
|
+
// re-enter the tool span for every asynchronous step.
|
|
149
|
+
const toolGenerator = options.tracer.withSpanContext(toolSpan, () => effectiveTool.stream(toolContext));
|
|
150
|
+
let toolNext = await options.tracer.withSpanContext(toolSpan, () => toolGenerator.next());
|
|
151
|
+
while (!toolNext.done) {
|
|
152
|
+
yield new ToolStreamUpdateEvent({ agent: options.agent, event: toolNext.value, invocationState });
|
|
153
|
+
toolNext = await options.tracer.withSpanContext(toolSpan, () => toolGenerator.next());
|
|
154
|
+
}
|
|
155
|
+
toolResult =
|
|
156
|
+
toolNext.value ??
|
|
157
|
+
new ToolResultBlock({
|
|
158
|
+
toolUseId: toolUse.toolUseId,
|
|
159
|
+
status: 'error',
|
|
160
|
+
content: [new TextBlock(`Tool '${toolUse.name}' did not return a result`)],
|
|
161
|
+
});
|
|
162
|
+
error = toolNext.value?.error;
|
|
163
|
+
}
|
|
164
|
+
return toolResult;
|
|
165
|
+
}
|
|
166
|
+
catch (caughtError) {
|
|
167
|
+
if (caughtError instanceof InterruptError) {
|
|
168
|
+
throw caughtError;
|
|
169
|
+
}
|
|
170
|
+
error = normalizeError(caughtError);
|
|
171
|
+
toolResult = new ToolResultBlock({
|
|
172
|
+
toolUseId: toolUse.toolUseId,
|
|
173
|
+
status: 'error',
|
|
174
|
+
content: [new TextBlock(error.message)],
|
|
175
|
+
error,
|
|
176
|
+
});
|
|
177
|
+
return toolResult;
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
// Record the raw execution outcome before AfterToolCallEvent can transform it.
|
|
181
|
+
options.tracer.endToolCallSpan(toolSpan, {
|
|
182
|
+
...(toolResult !== undefined && { toolResult }),
|
|
183
|
+
...(error !== undefined && { error }),
|
|
184
|
+
});
|
|
185
|
+
options.meter.endToolCall({
|
|
186
|
+
tool: toolUse,
|
|
187
|
+
duration: Date.now() - toolStartTime,
|
|
188
|
+
success: toolResult?.status === 'success',
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=executor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.js","sourceRoot":"","sources":["../../../../src/tools/executors/executor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAA;AACtG,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AACvE,OAAO,EAAE,yBAAyB,EAAE,MAAM,+BAA+B,CAAA;AACzE,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAA;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AA+CpE;;;;GAIG;AACH,MAAM,OAAgB,YAAY;IAchC,8EAA8E;IAC9E,0EAA0E;IACh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AAA;YAC/B,CAAC;YAED,OAAO,UAAU,CAAA;QACnB,CAAC;QAAC,OAAO,WAAW,EAAE,CAAC;YACrB,IAAI,WAAW,YAAY,cAAc,EAAE,CAAC;gBAC1C,MAAM,WAAW,CAAA;YACnB,CAAC;YAED,KAAK,GAAG,cAAc,CAAC,WAAW,CAAC,CAAA;YACnC,UAAU,GAAG,IAAI,eAAe,CAAC;gBAC/B,SAAS,EAAE,OAAO,CAAC,SAAS;gBAC5B,MAAM,EAAE,OAAO;gBACf,OAAO,EAAE,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;gBACvC,KAAK;aACN,CAAC,CAAA;YACF,OAAO,UAAU,CAAA;QACnB,CAAC;gBAAS,CAAC;YACT,+EAA+E;YAC/E,OAAO,CAAC,MAAM,CAAC,eAAe,CAAC,QAAQ,EAAE;gBACvC,GAAG,CAAC,UAAU,KAAK,SAAS,IAAI,EAAE,UAAU,EAAE,CAAC;gBAC/C,GAAG,CAAC,KAAK,KAAK,SAAS,IAAI,EAAE,KAAK,EAAE,CAAC;aACtC,CAAC,CAAA;YACF,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC;gBACxB,IAAI,EAAE,OAAO;gBACb,QAAQ,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,aAAa;gBACpC,OAAO,EAAE,UAAU,EAAE,MAAM,KAAK,SAAS;aAC1C,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ToolExecutor } from './executor.js';
|
|
2
|
+
import type { AgentStreamEvent } from '../../types/agent.js';
|
|
3
|
+
import type { ToolExecutionInput, ToolExecutorOptions } from './executor.js';
|
|
4
|
+
/**
|
|
5
|
+
* Executes tool calls one at a time.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { Agent, SequentialToolExecutor } from '@strands-agents/sdk'
|
|
10
|
+
*
|
|
11
|
+
* // The string shorthand keeps imports minimal.
|
|
12
|
+
* const agent = new Agent({ toolExecutor: 'sequential' })
|
|
13
|
+
*
|
|
14
|
+
* // Passing an instance is equivalent if you prefer to be explicit.
|
|
15
|
+
* const explicitAgent = new Agent({ toolExecutor: new SequentialToolExecutor() })
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class SequentialToolExecutor extends ToolExecutor {
|
|
19
|
+
/**
|
|
20
|
+
* Executes tool calls in source order, honoring `agent.cancelSignal` between
|
|
21
|
+
* calls to short-circuit tools that have not started.
|
|
22
|
+
*
|
|
23
|
+
* @param options - Agent dependencies used to execute tools
|
|
24
|
+
* @param input - Tool calls and invocation state
|
|
25
|
+
* @returns Stream of tool lifecycle events
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
execute(options: ToolExecutorOptions, input: ToolExecutionInput): AsyncGenerator<AgentStreamEvent, void, undefined>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=sequential.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sequential.d.ts","sourceRoot":"","sources":["../../../../src/tools/executors/sequential.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAE5C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAC5D,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AAE5E;;;;;;;;;;;;;GAaG;AACH,qBAAa,sBAAuB,SAAQ,YAAY;IACtD;;;;;;;;OAQG;IACa,OAAO,CACrB,OAAO,EAAE,mBAAmB,EAC5B,KAAK,EAAE,kBAAkB,GACxB,cAAc,CAAC,gBAAgB,EAAE,IAAI,EAAE,SAAS,CAAC;CAwCrD"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ToolResultEvent } from '../../hooks/events.js';
|
|
2
|
+
import { InterruptError } from '../../interrupt.js';
|
|
3
|
+
import { TextBlock, ToolResultBlock } from '../../types/messages.js';
|
|
4
|
+
import { ToolExecutor } from './executor.js';
|
|
5
|
+
/**
|
|
6
|
+
* Executes tool calls one at a time.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { Agent, SequentialToolExecutor } from '@strands-agents/sdk'
|
|
11
|
+
*
|
|
12
|
+
* // The string shorthand keeps imports minimal.
|
|
13
|
+
* const agent = new Agent({ toolExecutor: 'sequential' })
|
|
14
|
+
*
|
|
15
|
+
* // Passing an instance is equivalent if you prefer to be explicit.
|
|
16
|
+
* const explicitAgent = new Agent({ toolExecutor: new SequentialToolExecutor() })
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export class SequentialToolExecutor extends ToolExecutor {
|
|
20
|
+
/**
|
|
21
|
+
* Executes tool calls in source order, honoring `agent.cancelSignal` between
|
|
22
|
+
* calls to short-circuit tools that have not started.
|
|
23
|
+
*
|
|
24
|
+
* @param options - Agent dependencies used to execute tools
|
|
25
|
+
* @param input - Tool calls and invocation state
|
|
26
|
+
* @returns Stream of tool lifecycle events
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
async *execute(options, input) {
|
|
30
|
+
const { toolUseBlocks, toolResultBlocks, invocationState, assistantMessage, completedToolResults } = input;
|
|
31
|
+
// Keyed by the model-issued toolUseId so interrupt state matches the
|
|
32
|
+
// model's tool-use blocks.
|
|
33
|
+
const resultsByToolUseId = new Map(completedToolResults);
|
|
34
|
+
for (const toolUseBlock of toolUseBlocks) {
|
|
35
|
+
const completedResult = completedToolResults?.get(toolUseBlock.toolUseId);
|
|
36
|
+
if (completedResult) {
|
|
37
|
+
// Resume results belong in the final message without replaying lifecycle events.
|
|
38
|
+
toolResultBlocks.push(completedResult);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (options.agent.cancelSignal.aborted) {
|
|
42
|
+
const cancelBlock = new ToolResultBlock({
|
|
43
|
+
toolUseId: toolUseBlock.toolUseId,
|
|
44
|
+
status: 'error',
|
|
45
|
+
content: [new TextBlock('Tool execution cancelled')],
|
|
46
|
+
});
|
|
47
|
+
toolResultBlocks.push(cancelBlock);
|
|
48
|
+
resultsByToolUseId.set(toolUseBlock.toolUseId, cancelBlock);
|
|
49
|
+
yield new ToolResultEvent({ agent: options.agent, result: cancelBlock, invocationState });
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const toolResultBlock = yield* this.executeTool(options, toolUseBlock, invocationState);
|
|
54
|
+
toolResultBlocks.push(toolResultBlock);
|
|
55
|
+
resultsByToolUseId.set(toolUseBlock.toolUseId, toolResultBlock);
|
|
56
|
+
yield new ToolResultEvent({ agent: options.agent, result: toolResultBlock, invocationState });
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error instanceof InterruptError) {
|
|
60
|
+
this._storePendingToolExecution(options, assistantMessage, resultsByToolUseId);
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=sequential.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sequential.js","sourceRoot":"","sources":["../../../../src/tools/executors/sequential.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAK5C;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,sBAAuB,SAAQ,YAAY;IACtD;;;;;;;;OAQG;IACM,KAAK,CAAC,CAAC,OAAO,CACrB,OAA4B,EAC5B,KAAyB;QAEzB,MAAM,EAAE,aAAa,EAAE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