@strands-agents/sdk 1.14.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-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 +22 -8
- package/dist/src/agent/agent.d.ts.map +1 -1
- package/dist/src/agent/agent.js +76 -37
- package/dist/src/agent/agent.js.map +1 -1
- package/dist/src/agent/continuation.d.ts +41 -0
- package/dist/src/agent/continuation.d.ts.map +1 -1
- package/dist/src/agent/continuation.js +41 -0
- package/dist/src/agent/continuation.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/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 +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/models/bedrock.d.ts +11 -0
- package/dist/src/models/bedrock.d.ts.map +1 -1
- package/dist/src/models/bedrock.js +61 -10
- 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 +4 -0
- package/dist/src/models/model.d.ts.map +1 -1
- 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 +9 -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/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/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/tsconfig.tsbuildinfo +1 -1
- package/dist/src/types/agent.d.ts +12 -2
- 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/vended-memory-stores/file-memory-store/index.d.ts +1 -1
- package/dist/src/vended-memory-stores/file-memory-store/index.d.ts.map +1 -1
- package/dist/src/vended-memory-stores/file-memory-store/store.d.ts +22 -0
- package/dist/src/vended-memory-stores/file-memory-store/store.d.ts.map +1 -1
- package/dist/src/vended-memory-stores/file-memory-store/store.js +30 -12
- package/dist/src/vended-memory-stores/file-memory-store/store.js.map +1 -1
- package/package.json +8 -4
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal continuation handling for agent invocations.
|
|
3
|
+
*
|
|
4
|
+
* Coordinates continuation inputs contributed by hooks, including preparation,
|
|
5
|
+
* interrupt deferral, input combination, and append or abandonment callbacks.
|
|
6
|
+
*/
|
|
1
7
|
import { AfterInvocationEvent, BeforeModelCallEvent } from '../hooks/events.js';
|
|
2
8
|
import { logger } from '../logging/logger.js';
|
|
3
9
|
const stateByEvent = new WeakMap();
|
|
@@ -14,11 +20,25 @@ export const continuations = {
|
|
|
14
20
|
markAppended,
|
|
15
21
|
prepare,
|
|
16
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* Adds an input contribution for an invocation event.
|
|
25
|
+
*
|
|
26
|
+
* @param event - Event that owns the continuation input.
|
|
27
|
+
* @param input - Input and optional settlement callbacks to register.
|
|
28
|
+
*/
|
|
17
29
|
function addInput(event, input) {
|
|
18
30
|
const state = stateByEvent.get(event) ?? { inputs: [] };
|
|
19
31
|
state.inputs.push(input);
|
|
20
32
|
stateByEvent.set(event, state);
|
|
21
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Normalizes the inputs registered for an event and prepares those that form complete message sequences.
|
|
36
|
+
*
|
|
37
|
+
* @param event - Event whose continuation inputs should be prepared.
|
|
38
|
+
* @param normalizeInput - Converts invocation input into messages.
|
|
39
|
+
* @param stopReason - Stop reason that determines whether inputs can continue now or must be deferred.
|
|
40
|
+
* @returns The prepared messages, or `undefined` when no continuation is ready.
|
|
41
|
+
*/
|
|
22
42
|
async function prepare(event, normalizeInput, stopReason) {
|
|
23
43
|
if (stopReason === 'interrupt') {
|
|
24
44
|
const inputs = consumeInputs(event);
|
|
@@ -55,6 +75,14 @@ async function prepare(event, normalizeInput, stopReason) {
|
|
|
55
75
|
stateByEvent.set(event, { inputs: acceptedInputs, messages });
|
|
56
76
|
return messages;
|
|
57
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Prepends prepared continuation messages to invocation input.
|
|
80
|
+
*
|
|
81
|
+
* @param event - Event whose prepared messages should be applied.
|
|
82
|
+
* @param args - Invocation input to combine with the prepared messages.
|
|
83
|
+
* @param normalizeInput - Converts invocation input into messages.
|
|
84
|
+
* @returns The combined input, or the original input when no prepared messages can be applied.
|
|
85
|
+
*/
|
|
58
86
|
function combine(event, args, normalizeInput) {
|
|
59
87
|
const messages = event ? stateByEvent.get(event)?.messages : undefined;
|
|
60
88
|
if (!messages)
|
|
@@ -66,6 +94,12 @@ function combine(event, args, normalizeInput) {
|
|
|
66
94
|
}
|
|
67
95
|
return [...messages, ...publicMessages];
|
|
68
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Marks prepared inputs as incorporated into agent history.
|
|
99
|
+
*
|
|
100
|
+
* @param event - Event whose prepared inputs were appended.
|
|
101
|
+
* @returns A promise that resolves after append callbacks finish.
|
|
102
|
+
*/
|
|
69
103
|
async function markAppended(event) {
|
|
70
104
|
if (!event || !stateByEvent.get(event)?.messages)
|
|
71
105
|
return;
|
|
@@ -78,6 +112,13 @@ async function markAppended(event) {
|
|
|
78
112
|
}
|
|
79
113
|
}
|
|
80
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Abandons the inputs registered for an event.
|
|
117
|
+
*
|
|
118
|
+
* @param event - Event whose inputs should be abandoned.
|
|
119
|
+
* @param reason - Reason the inputs could not be incorporated.
|
|
120
|
+
* @returns A promise that resolves after abandonment callbacks finish.
|
|
121
|
+
*/
|
|
81
122
|
async function abandon(event, reason) {
|
|
82
123
|
if (!event)
|
|
83
124
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"continuation.js","sourceRoot":"","sources":["../../../src/agent/continuation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB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AAA;YACrC,CAAC;iBAAM,IAAI,KAAK,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;gBAC5C,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC;oBAAE,OAAO,KAAK,CAAA;YACzF,CAAC;QACH,CAAC;QAED,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,iBAAiB,CAAC,IAAI,GAAG,CAAC;YAAE,OAAO,KAAK,CAAA;QACvE,iBAAiB,GAAG,cAAc,CAAA;IACpC,CAAC;IACD,OAAO,iBAAiB,CAAC,IAAI,KAAK,CAAC,CAAA;AACrC,CAAC;AAED,KAAK,UAAU,eAAe,CAAC,KAAwB,EAAE,MAAe;IACtE,IAAI,CAAC;QACH,MAAM,KAAK,CAAC,WAAW,EAAE,CAAC,MAAM,CAAC,CAAA;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,IAAI,CAAC,UAAU,KAAK,0CAA0C,CAAC,CAAA;IACxE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../src/background-tasks/errors.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,qBAAa,2BAA4B,SAAQ,KAAK;IACpD;;OAEG;gBACS,MAAM,EAAE,MAAM;CAI3B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Raised when a background task ID is not found. @internal */
|
|
2
|
+
export class BackgroundTaskNotFoundError extends Error {
|
|
3
|
+
/**
|
|
4
|
+
* @param taskId - ID that could not be found.
|
|
5
|
+
*/
|
|
6
|
+
constructor(taskId) {
|
|
7
|
+
super(`Background task '${taskId}' was not found`);
|
|
8
|
+
this.name = 'BackgroundTaskNotFoundError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/background-tasks/errors.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IACpD;;OAEG;IACH,YAAY,MAAc;QACxB,KAAK,CAAC,oBAAoB,MAAM,iBAAiB,CAAC,CAAA;QAClD,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAA;IAC3C,CAAC;CACF"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { InterruptStateData } from '../../interrupt.js';
|
|
2
|
+
import type { InProcessTaskEngineOptions, InProcessTaskRecord } from './types.js';
|
|
3
|
+
/** Runs and tracks bounded in-process tasks. @internal */
|
|
4
|
+
export declare class InProcessTaskEngine {
|
|
5
|
+
private readonly _options;
|
|
6
|
+
private readonly _tasks;
|
|
7
|
+
private readonly _queue;
|
|
8
|
+
private readonly _activeExecutions;
|
|
9
|
+
private readonly _idleWaiters;
|
|
10
|
+
/**
|
|
11
|
+
* Creates an in-process task engine.
|
|
12
|
+
*
|
|
13
|
+
* @param options - Execution limits and callbacks supplied by the task manager.
|
|
14
|
+
* @throws TypeError if the concurrency or timeout configuration is invalid.
|
|
15
|
+
*/
|
|
16
|
+
constructor(options: InProcessTaskEngineOptions);
|
|
17
|
+
/**
|
|
18
|
+
* Submits a task for execution, returning an existing task when its idempotency key matches.
|
|
19
|
+
*
|
|
20
|
+
* @param admission - Tool execution details and optional idempotency key.
|
|
21
|
+
* @returns A snapshot of the admitted or matching task.
|
|
22
|
+
*/
|
|
23
|
+
submit(admission: {
|
|
24
|
+
readonly toolUseId: string;
|
|
25
|
+
readonly toolName: string;
|
|
26
|
+
readonly invocationStateId: string;
|
|
27
|
+
readonly idempotencyKey?: string;
|
|
28
|
+
}): InProcessTaskRecord;
|
|
29
|
+
/**
|
|
30
|
+
* Gets a task by ID.
|
|
31
|
+
*
|
|
32
|
+
* @param taskId - ID of the task to retrieve.
|
|
33
|
+
* @returns A snapshot of the task, or `undefined` when it does not exist.
|
|
34
|
+
*/
|
|
35
|
+
get(taskId: string): InProcessTaskRecord | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Lists the tasks tracked by this engine.
|
|
38
|
+
*
|
|
39
|
+
* @returns Snapshots of the tracked tasks.
|
|
40
|
+
*/
|
|
41
|
+
list(): readonly InProcessTaskRecord[];
|
|
42
|
+
/**
|
|
43
|
+
* Removes a terminal task.
|
|
44
|
+
*
|
|
45
|
+
* @param taskId - ID of the task to remove.
|
|
46
|
+
* @throws Error if the task does not exist or has not reached a terminal state.
|
|
47
|
+
*/
|
|
48
|
+
remove(taskId: string): void;
|
|
49
|
+
/**
|
|
50
|
+
* Cancels a non-terminal task and aborts its active execution.
|
|
51
|
+
*
|
|
52
|
+
* @param taskId - ID of the task to cancel.
|
|
53
|
+
* @param options - Cancellation options passed to the active execution.
|
|
54
|
+
* @returns A snapshot of the cancelled task, or the unchanged task if it was already terminal.
|
|
55
|
+
* @throws Error if the task does not exist.
|
|
56
|
+
*/
|
|
57
|
+
cancel(taskId: string, options: {
|
|
58
|
+
readonly reason: string;
|
|
59
|
+
}): InProcessTaskRecord;
|
|
60
|
+
/**
|
|
61
|
+
* Waits until no tasks are queued or executing.
|
|
62
|
+
*
|
|
63
|
+
* @param options - Optional cancellation signal for the wait.
|
|
64
|
+
* @returns A promise that resolves when the engine is idle.
|
|
65
|
+
* @throws The cancellation signal's reason if the wait is aborted.
|
|
66
|
+
*/
|
|
67
|
+
waitForIdle(options?: {
|
|
68
|
+
readonly cancelSignal?: AbortSignal;
|
|
69
|
+
}): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Applies interrupt responses to a task with `input_required` status.
|
|
72
|
+
*
|
|
73
|
+
* The task remains `input_required` while interrupts are unanswered and is queued for
|
|
74
|
+
* execution once the updated interrupt state is ready.
|
|
75
|
+
*
|
|
76
|
+
* @param taskId - ID of the task requiring input.
|
|
77
|
+
* @param update - Applies interrupt responses to the task state and reports when it is ready.
|
|
78
|
+
* @returns A snapshot of the updated task.
|
|
79
|
+
* @throws BackgroundTaskNotFoundError if the task does not exist.
|
|
80
|
+
* @throws Error if the task does not require input or has no state.
|
|
81
|
+
*/
|
|
82
|
+
resume(taskId: string, update: (state: InterruptStateData) => {
|
|
83
|
+
readonly state: InterruptStateData;
|
|
84
|
+
readonly ready: boolean;
|
|
85
|
+
}): InProcessTaskRecord;
|
|
86
|
+
private _scheduleTask;
|
|
87
|
+
private _startQueuedTasks;
|
|
88
|
+
private _execute;
|
|
89
|
+
private _finishOutcome;
|
|
90
|
+
private _timeoutTask;
|
|
91
|
+
private _updateTask;
|
|
92
|
+
private _requireTask;
|
|
93
|
+
private _notifyTaskUpdated;
|
|
94
|
+
private _wakeIdleWaiters;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../../../src/background-tasks/in-process/engine.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAC5D,OAAO,KAAK,EAAE,0BAA0B,EAAE,mBAAmB,EAAoC,MAAM,YAAY,CAAA;AAWnH,0DAA0D;AAC1D,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA4B;IACrD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyC;IAChE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoB;IAC3C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAqC;IACvE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAwB;IAErD;;;;;OAKG;gBACS,OAAO,EAAE,0BAA0B;IAQ/C;;;;;OAKG;IACH,MAAM,CAAC,SAAS,EAAE;QAChB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;QAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;QACzB,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAA;QAClC,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KACjC,GAAG,mBAAmB;IAmBvB;;;;;OAKG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,mBAAmB,GAAG,SAAS;IAKpD;;;;OAIG;IACH,IAAI,IAAI,SAAS,mBAAmB,EAAE;IAItC;;;;;OAKG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAQ5B;;;;;;;OAOG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE;QAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,mBAAmB;IAkBjF;;;;;;OAMG;IACG,WAAW,CAAC,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,YAAY,CAAC,EAAE,WAAW,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBnF;;;;;;;;;;;OAWG;IACH,MAAM,CACJ,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,KAAK,EAAE,kBAAkB,KAAK;QACrC,QAAQ,CAAC,KAAK,EAAE,kBAAkB,CAAA;QAClC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;KACxB,GACA,mBAAmB;IAmBtB,OAAO,CAAC,aAAa;IAMrB,OAAO,CAAC,iBAAiB;YAsBX,QAAQ;IAmCtB,OAAO,CAAC,cAAc;IAgCtB,OAAO,CAAC,YAAY;IAepB,OAAO,CAAC,WAAW;IAcnB,OAAO,CAAC,YAAY;IAMpB,OAAO,CAAC,kBAAkB;IAO1B,OAAO,CAAC,gBAAgB;CAIzB"}
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { normalizeError } from '../../errors.js';
|
|
2
|
+
import { BackgroundTaskNotFoundError } from '../errors.js';
|
|
3
|
+
// Node.js treats delays above the signed 32-bit limit as 1ms.
|
|
4
|
+
const MAX_TIMER_DELAY_MS = 2 ** 31 - 1;
|
|
5
|
+
const DEFAULT_EXECUTION_FAILURE_MESSAGE = 'Background task execution failed';
|
|
6
|
+
/** Runs and tracks bounded in-process tasks. @internal */
|
|
7
|
+
export class InProcessTaskEngine {
|
|
8
|
+
_options;
|
|
9
|
+
_tasks = new Map();
|
|
10
|
+
_queue = new Set();
|
|
11
|
+
_activeExecutions = new Map();
|
|
12
|
+
_idleWaiters = new Set();
|
|
13
|
+
/**
|
|
14
|
+
* Creates an in-process task engine.
|
|
15
|
+
*
|
|
16
|
+
* @param options - Execution limits and callbacks supplied by the task manager.
|
|
17
|
+
* @throws TypeError if the concurrency or timeout configuration is invalid.
|
|
18
|
+
*/
|
|
19
|
+
constructor(options) {
|
|
20
|
+
if (!Number.isSafeInteger(options.maxConcurrency) || options.maxConcurrency <= 0) {
|
|
21
|
+
throw new TypeError(`maxConcurrency must be a positive finite integer, got ${options.maxConcurrency}`);
|
|
22
|
+
}
|
|
23
|
+
if (options.timeout !== Infinity)
|
|
24
|
+
assertTimerDelay('timeout', options.timeout);
|
|
25
|
+
this._options = options;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Submits a task for execution, returning an existing task when its idempotency key matches.
|
|
29
|
+
*
|
|
30
|
+
* @param admission - Tool execution details and optional idempotency key.
|
|
31
|
+
* @returns A snapshot of the admitted or matching task.
|
|
32
|
+
*/
|
|
33
|
+
submit(admission) {
|
|
34
|
+
if (admission.idempotencyKey !== undefined) {
|
|
35
|
+
const existing = [...this._tasks.values()].find((task) => task.idempotencyKey === admission.idempotencyKey);
|
|
36
|
+
if (existing)
|
|
37
|
+
return globalThis.structuredClone(existing);
|
|
38
|
+
}
|
|
39
|
+
const now = new Date().toISOString();
|
|
40
|
+
const stored = {
|
|
41
|
+
taskId: globalThis.crypto.randomUUID(),
|
|
42
|
+
...admission,
|
|
43
|
+
status: 'queued',
|
|
44
|
+
createdAt: now,
|
|
45
|
+
updatedAt: now,
|
|
46
|
+
};
|
|
47
|
+
this._tasks.set(stored.taskId, stored);
|
|
48
|
+
this._notifyTaskUpdated(stored);
|
|
49
|
+
this._scheduleTask(stored.taskId);
|
|
50
|
+
return globalThis.structuredClone(stored);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets a task by ID.
|
|
54
|
+
*
|
|
55
|
+
* @param taskId - ID of the task to retrieve.
|
|
56
|
+
* @returns A snapshot of the task, or `undefined` when it does not exist.
|
|
57
|
+
*/
|
|
58
|
+
get(taskId) {
|
|
59
|
+
const task = this._tasks.get(taskId);
|
|
60
|
+
return task ? globalThis.structuredClone(task) : undefined;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Lists the tasks tracked by this engine.
|
|
64
|
+
*
|
|
65
|
+
* @returns Snapshots of the tracked tasks.
|
|
66
|
+
*/
|
|
67
|
+
list() {
|
|
68
|
+
return [...this._tasks.values()].map((task) => globalThis.structuredClone(task));
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Removes a terminal task.
|
|
72
|
+
*
|
|
73
|
+
* @param taskId - ID of the task to remove.
|
|
74
|
+
* @throws Error if the task does not exist or has not reached a terminal state.
|
|
75
|
+
*/
|
|
76
|
+
remove(taskId) {
|
|
77
|
+
const task = this._requireTask(taskId);
|
|
78
|
+
if (!isInProcessTaskTerminalStatus(task.status)) {
|
|
79
|
+
throw new Error(`Background task '${taskId}' cannot be removed before reaching a terminal status`);
|
|
80
|
+
}
|
|
81
|
+
this._tasks.delete(taskId);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Cancels a non-terminal task and aborts its active execution.
|
|
85
|
+
*
|
|
86
|
+
* @param taskId - ID of the task to cancel.
|
|
87
|
+
* @param options - Cancellation options passed to the active execution.
|
|
88
|
+
* @returns A snapshot of the cancelled task, or the unchanged task if it was already terminal.
|
|
89
|
+
* @throws Error if the task does not exist.
|
|
90
|
+
*/
|
|
91
|
+
cancel(taskId, options) {
|
|
92
|
+
const current = this._requireTask(taskId);
|
|
93
|
+
if (isInProcessTaskTerminalStatus(current.status))
|
|
94
|
+
return globalThis.structuredClone(current);
|
|
95
|
+
const task = this._updateTask(taskId, (record) => {
|
|
96
|
+
record.status = 'cancelled';
|
|
97
|
+
delete record.state;
|
|
98
|
+
return true;
|
|
99
|
+
});
|
|
100
|
+
this._queue.delete(taskId);
|
|
101
|
+
const activeExecution = this._activeExecutions.get(taskId);
|
|
102
|
+
if (activeExecution?.timeout) {
|
|
103
|
+
clearTimeout(activeExecution.timeout);
|
|
104
|
+
}
|
|
105
|
+
activeExecution?.controller.abort(options.reason);
|
|
106
|
+
this._wakeIdleWaiters();
|
|
107
|
+
return task;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Waits until no tasks are queued or executing.
|
|
111
|
+
*
|
|
112
|
+
* @param options - Optional cancellation signal for the wait.
|
|
113
|
+
* @returns A promise that resolves when the engine is idle.
|
|
114
|
+
* @throws The cancellation signal's reason if the wait is aborted.
|
|
115
|
+
*/
|
|
116
|
+
async waitForIdle(options) {
|
|
117
|
+
const cancelSignal = options?.cancelSignal;
|
|
118
|
+
while (this._queue.size > 0 || this._activeExecutions.size > 0) {
|
|
119
|
+
if (cancelSignal?.aborted)
|
|
120
|
+
throw cancelSignal.reason;
|
|
121
|
+
await new Promise((resolve, reject) => {
|
|
122
|
+
const onAbort = () => {
|
|
123
|
+
this._idleWaiters.delete(onIdle);
|
|
124
|
+
reject(cancelSignal.reason);
|
|
125
|
+
};
|
|
126
|
+
const onIdle = () => {
|
|
127
|
+
cancelSignal?.removeEventListener('abort', onAbort);
|
|
128
|
+
resolve();
|
|
129
|
+
};
|
|
130
|
+
this._idleWaiters.add(onIdle);
|
|
131
|
+
cancelSignal?.addEventListener('abort', onAbort, { once: true });
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Applies interrupt responses to a task with `input_required` status.
|
|
137
|
+
*
|
|
138
|
+
* The task remains `input_required` while interrupts are unanswered and is queued for
|
|
139
|
+
* execution once the updated interrupt state is ready.
|
|
140
|
+
*
|
|
141
|
+
* @param taskId - ID of the task requiring input.
|
|
142
|
+
* @param update - Applies interrupt responses to the task state and reports when it is ready.
|
|
143
|
+
* @returns A snapshot of the updated task.
|
|
144
|
+
* @throws BackgroundTaskNotFoundError if the task does not exist.
|
|
145
|
+
* @throws Error if the task does not require input or has no state.
|
|
146
|
+
*/
|
|
147
|
+
resume(taskId, update) {
|
|
148
|
+
const task = this._updateTask(taskId, (record) => {
|
|
149
|
+
if (record.status !== 'input_required') {
|
|
150
|
+
throw new Error(`Background task '${taskId}' cannot be resumed: status is '${record.status}', not 'input_required'`);
|
|
151
|
+
}
|
|
152
|
+
if (record.state === undefined) {
|
|
153
|
+
throw new Error(`Background task '${taskId}' cannot be resumed: interrupt state is missing`);
|
|
154
|
+
}
|
|
155
|
+
const resumed = update(record.state);
|
|
156
|
+
record.state = resumed.state;
|
|
157
|
+
if (resumed.ready)
|
|
158
|
+
record.status = 'queued';
|
|
159
|
+
return true;
|
|
160
|
+
});
|
|
161
|
+
if (task.status === 'queued')
|
|
162
|
+
this._scheduleTask(taskId);
|
|
163
|
+
return task;
|
|
164
|
+
}
|
|
165
|
+
_scheduleTask(taskId) {
|
|
166
|
+
if (this._activeExecutions.has(taskId))
|
|
167
|
+
return;
|
|
168
|
+
this._queue.add(taskId);
|
|
169
|
+
this._startQueuedTasks();
|
|
170
|
+
}
|
|
171
|
+
_startQueuedTasks() {
|
|
172
|
+
while (this._activeExecutions.size < this._options.maxConcurrency && this._queue.size > 0) {
|
|
173
|
+
const taskId = this._queue.values().next().value;
|
|
174
|
+
this._queue.delete(taskId);
|
|
175
|
+
const activeExecution = {
|
|
176
|
+
controller: new AbortController(),
|
|
177
|
+
};
|
|
178
|
+
this._activeExecutions.set(taskId, activeExecution);
|
|
179
|
+
void this._execute(taskId, activeExecution)
|
|
180
|
+
.finally(() => {
|
|
181
|
+
if (activeExecution.timeout)
|
|
182
|
+
clearTimeout(activeExecution.timeout);
|
|
183
|
+
this._activeExecutions.delete(taskId);
|
|
184
|
+
if (this._tasks.get(taskId)?.status === 'queued') {
|
|
185
|
+
this._queue.add(taskId);
|
|
186
|
+
}
|
|
187
|
+
this._wakeIdleWaiters();
|
|
188
|
+
this._startQueuedTasks();
|
|
189
|
+
})
|
|
190
|
+
.catch(() => undefined);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async _execute(taskId, activeExecution) {
|
|
194
|
+
const working = this._updateTask(taskId, (task) => {
|
|
195
|
+
task.status = 'working';
|
|
196
|
+
return true;
|
|
197
|
+
});
|
|
198
|
+
if (Number.isFinite(this._options.timeout)) {
|
|
199
|
+
activeExecution.timeout = setTimeout(() => {
|
|
200
|
+
delete activeExecution.timeout;
|
|
201
|
+
this._timeoutTask(taskId, activeExecution);
|
|
202
|
+
}, this._options.timeout);
|
|
203
|
+
}
|
|
204
|
+
let outcome;
|
|
205
|
+
try {
|
|
206
|
+
outcome = await this._options.execute({
|
|
207
|
+
taskId,
|
|
208
|
+
toolUseId: working.toolUseId,
|
|
209
|
+
toolName: working.toolName,
|
|
210
|
+
invocationStateId: working.invocationStateId,
|
|
211
|
+
...(working.state !== undefined && { state: working.state }),
|
|
212
|
+
cancelSignal: activeExecution.controller.signal,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
outcome = {
|
|
217
|
+
status: 'failed',
|
|
218
|
+
failure: {
|
|
219
|
+
type: 'executionError',
|
|
220
|
+
message: getExecutionFailureMessage(error),
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
this._finishOutcome(taskId, outcome);
|
|
225
|
+
}
|
|
226
|
+
_finishOutcome(taskId, outcome) {
|
|
227
|
+
if (outcome.status === 'input_required') {
|
|
228
|
+
this._updateTask(taskId, (record) => {
|
|
229
|
+
if (record.status !== 'working')
|
|
230
|
+
return false;
|
|
231
|
+
record.status = 'input_required';
|
|
232
|
+
record.state = outcome.state;
|
|
233
|
+
return true;
|
|
234
|
+
});
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
this._updateTask(taskId, (record) => {
|
|
238
|
+
if (record.status !== 'working')
|
|
239
|
+
return false;
|
|
240
|
+
if (outcome.status === 'failed') {
|
|
241
|
+
record.status = 'failed';
|
|
242
|
+
delete record.state;
|
|
243
|
+
record.failure = {
|
|
244
|
+
type: outcome.failure.type,
|
|
245
|
+
message: outcome.failure.message || DEFAULT_EXECUTION_FAILURE_MESSAGE,
|
|
246
|
+
};
|
|
247
|
+
if (outcome.result !== undefined) {
|
|
248
|
+
record.result = outcome.result;
|
|
249
|
+
}
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
record.status = 'completed';
|
|
253
|
+
delete record.state;
|
|
254
|
+
record.result = outcome.result;
|
|
255
|
+
return true;
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
_timeoutTask(taskId, activeExecution) {
|
|
259
|
+
const reason = `Timed out after ${this._options.timeout}ms`;
|
|
260
|
+
const task = this._updateTask(taskId, (record) => {
|
|
261
|
+
if (record.status !== 'working')
|
|
262
|
+
return false;
|
|
263
|
+
record.status = 'failed';
|
|
264
|
+
delete record.state;
|
|
265
|
+
record.failure = {
|
|
266
|
+
type: 'timeout',
|
|
267
|
+
message: reason,
|
|
268
|
+
};
|
|
269
|
+
return true;
|
|
270
|
+
});
|
|
271
|
+
if (task)
|
|
272
|
+
activeExecution.controller.abort(reason);
|
|
273
|
+
}
|
|
274
|
+
_updateTask(taskId, update) {
|
|
275
|
+
const current = this._requireTask(taskId);
|
|
276
|
+
const next = globalThis.structuredClone(current);
|
|
277
|
+
if (!update(next))
|
|
278
|
+
return undefined;
|
|
279
|
+
next.updatedAt = new Date().toISOString();
|
|
280
|
+
const stored = globalThis.structuredClone(next);
|
|
281
|
+
this._tasks.set(taskId, stored);
|
|
282
|
+
this._notifyTaskUpdated(stored);
|
|
283
|
+
return globalThis.structuredClone(stored);
|
|
284
|
+
}
|
|
285
|
+
_requireTask(taskId) {
|
|
286
|
+
const task = this._tasks.get(taskId);
|
|
287
|
+
if (!task)
|
|
288
|
+
throw new BackgroundTaskNotFoundError(taskId);
|
|
289
|
+
return task;
|
|
290
|
+
}
|
|
291
|
+
_notifyTaskUpdated(task) {
|
|
292
|
+
const taskSnapshot = globalThis.structuredClone(task);
|
|
293
|
+
void Promise.resolve()
|
|
294
|
+
.then(() => this._options.onTaskUpdated(taskSnapshot))
|
|
295
|
+
.catch(() => undefined);
|
|
296
|
+
}
|
|
297
|
+
_wakeIdleWaiters() {
|
|
298
|
+
for (const resolve of this._idleWaiters)
|
|
299
|
+
resolve();
|
|
300
|
+
this._idleWaiters.clear();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
function assertTimerDelay(name, value) {
|
|
304
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
305
|
+
throw new TypeError(`${name} must be a positive finite integer, got ${value}`);
|
|
306
|
+
}
|
|
307
|
+
if (value > MAX_TIMER_DELAY_MS) {
|
|
308
|
+
throw new TypeError(`${name} must be at most ${MAX_TIMER_DELAY_MS}ms, got ${value}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
function getExecutionFailureMessage(error) {
|
|
312
|
+
try {
|
|
313
|
+
return normalizeError(error).message || DEFAULT_EXECUTION_FAILURE_MESSAGE;
|
|
314
|
+
}
|
|
315
|
+
catch {
|
|
316
|
+
return DEFAULT_EXECUTION_FAILURE_MESSAGE;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function isInProcessTaskTerminalStatus(status) {
|
|
320
|
+
return status === 'completed' || status === 'failed' || status === 'cancelled';
|
|
321
|
+
}
|
|
322
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../../../../src/background-tasks/in-process/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,M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,MAAM,KAAK,WAAW,CAAA;AAChF,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { InterruptStateData } from '../../interrupt.js';
|
|
2
|
+
import type { ToolResultBlockData } from '../../types/messages.js';
|
|
3
|
+
/** Background task lifecycle status. @internal */
|
|
4
|
+
export type TaskStatus = 'queued' | 'working' | 'input_required' | 'completed' | 'failed' | 'cancelled';
|
|
5
|
+
/** Background task failure category. @internal */
|
|
6
|
+
export type TaskFailureType = 'toolError' | 'executionError' | 'timeout';
|
|
7
|
+
/** Options and callbacks for bounded in-process task execution. @internal */
|
|
8
|
+
export interface InProcessTaskEngineOptions {
|
|
9
|
+
/** Maximum number of task executions that may run concurrently. */
|
|
10
|
+
readonly maxConcurrency: number;
|
|
11
|
+
/** Per-execution timeout in milliseconds, or `Infinity` to disable timeouts. */
|
|
12
|
+
readonly timeout: number;
|
|
13
|
+
/** Runs one task execution and returns whether it completed, requires input, or failed. */
|
|
14
|
+
readonly execute: (context: InProcessTaskExecutionContext) => Promise<TaskExecutionOutcome>;
|
|
15
|
+
/** Receives committed task updates without blocking or failing the engine. */
|
|
16
|
+
readonly onTaskUpdated: (task: InProcessTaskRecord) => void;
|
|
17
|
+
}
|
|
18
|
+
/** Context supplied to one in-process task execution. @internal */
|
|
19
|
+
export interface InProcessTaskExecutionContext {
|
|
20
|
+
/** Identifier of the logical task. */
|
|
21
|
+
readonly taskId: string;
|
|
22
|
+
/** Identifier of the tool use. */
|
|
23
|
+
readonly toolUseId: string;
|
|
24
|
+
/** Name of the tool to execute. */
|
|
25
|
+
readonly toolName: string;
|
|
26
|
+
/** Identifier of the manager-owned live execution state. */
|
|
27
|
+
readonly invocationStateId: string;
|
|
28
|
+
/** Interrupt state supplied when resuming a task that requires input. */
|
|
29
|
+
readonly state?: InterruptStateData;
|
|
30
|
+
/** Signal aborted when this execution should stop. */
|
|
31
|
+
readonly cancelSignal: AbortSignal;
|
|
32
|
+
}
|
|
33
|
+
/** Outcome returned by one in-process task execution. @internal */
|
|
34
|
+
export type TaskExecutionOutcome = {
|
|
35
|
+
readonly status: 'completed';
|
|
36
|
+
readonly result: ToolResultBlockData;
|
|
37
|
+
} | {
|
|
38
|
+
readonly status: 'input_required';
|
|
39
|
+
readonly state: InterruptStateData;
|
|
40
|
+
} | {
|
|
41
|
+
readonly status: 'failed';
|
|
42
|
+
readonly failure: {
|
|
43
|
+
readonly type: TaskFailureType;
|
|
44
|
+
readonly message: string;
|
|
45
|
+
};
|
|
46
|
+
readonly result?: ToolResultBlockData;
|
|
47
|
+
};
|
|
48
|
+
/** In-process task record. @internal */
|
|
49
|
+
export interface InProcessTaskRecord {
|
|
50
|
+
/** Identifier of the logical task. */
|
|
51
|
+
readonly taskId: string;
|
|
52
|
+
/** Optional key used to deduplicate task admission. */
|
|
53
|
+
readonly idempotencyKey?: string;
|
|
54
|
+
/** Identifier of the tool use. */
|
|
55
|
+
readonly toolUseId: string;
|
|
56
|
+
/** Name of the tool to execute. */
|
|
57
|
+
readonly toolName: string;
|
|
58
|
+
/** Identifier of the manager-owned live execution state. */
|
|
59
|
+
readonly invocationStateId: string;
|
|
60
|
+
/** Current task lifecycle status. */
|
|
61
|
+
status: TaskStatus;
|
|
62
|
+
/** Interrupt state, required while the task needs input and retained while resuming. */
|
|
63
|
+
state?: InterruptStateData;
|
|
64
|
+
/** Execution result, required while completed and optional while failed. */
|
|
65
|
+
result?: ToolResultBlockData;
|
|
66
|
+
/** Failure details recorded when the task fails. */
|
|
67
|
+
failure?: {
|
|
68
|
+
readonly type: TaskFailureType;
|
|
69
|
+
readonly message: string;
|
|
70
|
+
};
|
|
71
|
+
/** ISO-8601 creation timestamp. */
|
|
72
|
+
readonly createdAt: string;
|
|
73
|
+
/** ISO-8601 timestamp of the latest transition. */
|
|
74
|
+
updatedAt: string;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceR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