@temporalio/strands-agents 1.24.0 → 2.0.0-rc.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/LICENSE +6 -6
- package/README.md +112 -15
- package/lib/empty-module.d.ts +1 -1
- package/lib/empty-module.js +6 -1
- package/lib/empty-module.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +21 -11
- package/lib/index.js.map +1 -1
- package/lib/load-polyfills.js +15 -0
- package/lib/load-polyfills.js.map +1 -1
- package/lib/model-activity.d.ts +57 -2
- package/lib/model-activity.js +100 -6
- package/lib/model-activity.js.map +1 -1
- package/lib/plugin.d.ts +33 -12
- package/lib/plugin.js +127 -26
- package/lib/plugin.js.map +1 -1
- package/lib/strands-errors.d.ts +39 -0
- package/lib/strands-errors.js +130 -0
- package/lib/strands-errors.js.map +1 -0
- package/lib/temporal-activity-tool.d.ts +18 -10
- package/lib/temporal-activity-tool.js +50 -29
- package/lib/temporal-activity-tool.js.map +1 -1
- package/lib/temporal-agent.d.ts +38 -4
- package/lib/temporal-agent.js +68 -16
- package/lib/temporal-agent.js.map +1 -1
- package/lib/temporal-mcp-client.d.ts +113 -17
- package/lib/temporal-mcp-client.js +376 -102
- package/lib/temporal-mcp-client.js.map +1 -1
- package/lib/temporal-mcp-tool.d.ts +3 -1
- package/lib/temporal-mcp-tool.js +26 -16
- package/lib/temporal-mcp-tool.js.map +1 -1
- package/lib/temporal-model.d.ts +32 -0
- package/lib/temporal-model.js +108 -24
- package/lib/temporal-model.js.map +1 -1
- package/lib/workflow.js +17 -7
- package/lib/workflow.js.map +1 -1
- package/package.json +46 -22
- package/src/empty-module.ts +5 -0
- package/src/index.ts +3 -1
- package/src/load-polyfills.ts +20 -1
- package/src/model-activity.ts +153 -8
- package/src/plugin.ts +159 -31
- package/src/strands-errors.ts +147 -0
- package/src/temporal-activity-tool.ts +43 -32
- package/src/temporal-agent.ts +77 -19
- package/src/temporal-mcp-client.ts +407 -91
- package/src/temporal-mcp-tool.ts +9 -9
- package/src/temporal-model.ts +111 -18
- package/lib/failure-converter.d.ts +0 -14
- package/lib/failure-converter.js +0 -61
- package/lib/failure-converter.js.map +0 -1
- package/src/failure-converter.ts +0 -77
package/src/temporal-mcp-tool.ts
CHANGED
|
@@ -4,6 +4,7 @@ import * as workflow from '@temporalio/workflow';
|
|
|
4
4
|
import type { ActivityOptions } from '@temporalio/workflow';
|
|
5
5
|
import type { CallToolInput, McpToolInfo } from './temporal-mcp-client';
|
|
6
6
|
import { callToolActivityName } from './temporal-mcp-client';
|
|
7
|
+
import { activityErrorResult } from './temporal-activity-tool';
|
|
7
8
|
import { toJsonSchema } from './json-schema';
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -22,15 +23,18 @@ export class TemporalMCPTool extends Tool {
|
|
|
22
23
|
readonly description: string;
|
|
23
24
|
readonly toolSpec: ToolSpec;
|
|
24
25
|
readonly server: string;
|
|
26
|
+
/** The tool's name on the MCP server, which a `prefix` does not change. */
|
|
27
|
+
readonly serverToolName: string;
|
|
25
28
|
private readonly activityOptions: ActivityOptions;
|
|
26
29
|
|
|
27
|
-
constructor(server: string, info: McpToolInfo, activityOptions: ActivityOptions) {
|
|
30
|
+
constructor(server: string, info: McpToolInfo, activityOptions: ActivityOptions, prefix?: string) {
|
|
28
31
|
super();
|
|
29
32
|
this.server = server;
|
|
30
|
-
this.
|
|
33
|
+
this.serverToolName = info.name;
|
|
34
|
+
this.name = prefix ? `${prefix}_${info.name}` : info.name;
|
|
31
35
|
this.description = info.description || `Tool which performs ${info.name}`;
|
|
32
36
|
this.toolSpec = {
|
|
33
|
-
name:
|
|
37
|
+
name: this.name,
|
|
34
38
|
description: this.description,
|
|
35
39
|
inputSchema: toJsonSchema(info.inputSchema),
|
|
36
40
|
};
|
|
@@ -49,7 +53,7 @@ export class TemporalMCPTool extends Tool {
|
|
|
49
53
|
});
|
|
50
54
|
try {
|
|
51
55
|
const raw = await activities[callToolActivityName(this.server)]!({
|
|
52
|
-
toolName: this.
|
|
56
|
+
toolName: this.serverToolName,
|
|
53
57
|
args: input,
|
|
54
58
|
});
|
|
55
59
|
const result = raw as { content?: unknown[]; isError?: boolean } | null;
|
|
@@ -69,11 +73,7 @@ export class TemporalMCPTool extends Tool {
|
|
|
69
73
|
content,
|
|
70
74
|
});
|
|
71
75
|
} catch (err) {
|
|
72
|
-
return
|
|
73
|
-
toolUseId,
|
|
74
|
-
status: 'error',
|
|
75
|
-
content: [new TextBlock(String(err instanceof Error ? err.message : err))],
|
|
76
|
-
});
|
|
76
|
+
return activityErrorResult(toolUseId, err);
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
}
|
package/src/temporal-model.ts
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
-
import { Model } from '@strands-agents/sdk';
|
|
1
|
+
import { ContextWindowOverflowError, Model } from '@strands-agents/sdk';
|
|
2
2
|
import type { BaseModelConfig, Message, ModelStreamEvent, StreamOptions } from '@strands-agents/sdk';
|
|
3
3
|
import * as workflow from '@temporalio/workflow';
|
|
4
4
|
import type { ActivityOptions } from '@temporalio/workflow';
|
|
5
5
|
import type { Duration } from '@temporalio/common/lib/time';
|
|
6
|
-
import
|
|
6
|
+
import { applicationFailureType, CONTEXT_WINDOW_OVERFLOW_TYPE } from './strands-errors';
|
|
7
|
+
import type {
|
|
8
|
+
GetModelConfigInput,
|
|
9
|
+
InvokeModelInput,
|
|
10
|
+
InvokeModelStreamingInput,
|
|
11
|
+
InvokeStatefulModelInput,
|
|
12
|
+
InvokeStatefulModelResult,
|
|
13
|
+
InvokeStatefulModelStreamingInput,
|
|
14
|
+
ModelConfigInfo,
|
|
15
|
+
} from './model-activity';
|
|
16
|
+
|
|
17
|
+
/** Name of the activity that returns the worker model's {@link ModelConfigInfo}. */
|
|
18
|
+
export const GET_MODEL_CONFIG_ACTIVITY = 'strandsGetModelConfig';
|
|
19
|
+
|
|
20
|
+
/** Names of the activities that run a stateful model with the agent's model state. */
|
|
21
|
+
export const INVOKE_STATEFUL_MODEL_ACTIVITY = 'strandsInvokeStatefulModel';
|
|
22
|
+
export const INVOKE_STATEFUL_MODEL_STREAMING_ACTIVITY = 'strandsInvokeStatefulModelStreaming';
|
|
7
23
|
|
|
8
24
|
/**
|
|
9
25
|
* Options for {@link TemporalModel}.
|
|
@@ -28,12 +44,24 @@ export interface TemporalModelOptions {
|
|
|
28
44
|
* The `modelName` selects which factory the plugin will invoke worker-side;
|
|
29
45
|
* it must match a key in {@link StrandsPlugin}'s `models` map. A name not
|
|
30
46
|
* present in `models` raises an error inside the model activity at first call.
|
|
47
|
+
*
|
|
48
|
+
* `activityOptions.heartbeatTimeout` defaults to 10 seconds. The model activities heartbeat on a
|
|
49
|
+
* timer at half of it, so a cancel reaches a running model call and a lost worker is noticed. Pass
|
|
50
|
+
* `heartbeatTimeout: 0` to turn heartbeats off. The config read runs synchronously and can't
|
|
51
|
+
* heartbeat, so it's scheduled without a heartbeat timeout.
|
|
52
|
+
*
|
|
53
|
+
* `TemporalAgent` reads the worker model's config before its first call. With a plain Strands
|
|
54
|
+
* `Agent`, call `await model.loadConfig()` before building the agent: without it, the model reports
|
|
55
|
+
* itself as stateless, and a stateful worker model fails every call.
|
|
31
56
|
*/
|
|
32
57
|
export class TemporalModel extends Model<BaseModelConfig> {
|
|
33
58
|
private readonly modelName?: string;
|
|
34
59
|
private readonly activityOptions: ActivityOptions;
|
|
35
60
|
private readonly streamingTopic?: string;
|
|
36
61
|
private readonly streamingBatchInterval: Duration;
|
|
62
|
+
private config: ModelConfigInfo = {};
|
|
63
|
+
private isStateful = false;
|
|
64
|
+
private configLoaded = false;
|
|
37
65
|
|
|
38
66
|
constructor(options: TemporalModelOptions = {}) {
|
|
39
67
|
super();
|
|
@@ -47,35 +75,100 @@ export class TemporalModel extends Model<BaseModelConfig> {
|
|
|
47
75
|
// No-op; the real model is configured worker-side via the plugin's factories.
|
|
48
76
|
}
|
|
49
77
|
|
|
78
|
+
/**
|
|
79
|
+
* The worker model's id and context window, once {@link loadConfig} has read them; otherwise
|
|
80
|
+
* empty.
|
|
81
|
+
*/
|
|
50
82
|
override getConfig(): BaseModelConfig {
|
|
51
|
-
return {};
|
|
83
|
+
return { ...this.config };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Whether the worker model keeps the conversation server-side, once {@link loadConfig} has read it. */
|
|
87
|
+
override get stateful(): boolean {
|
|
88
|
+
return this.isStateful;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Read the worker model's id, context window and stateful flag through an activity, once per
|
|
93
|
+
* instance, so that Strands' traces, proactive compression and stateful handling use the real
|
|
94
|
+
* model's values. {@link TemporalAgent} calls this before Strands initializes the agent.
|
|
95
|
+
*
|
|
96
|
+
* Returns `undefined`, without awaiting anything, when the config was already read, so callers
|
|
97
|
+
* can go on synchronously.
|
|
98
|
+
*/
|
|
99
|
+
loadConfig(): Promise<void> | undefined {
|
|
100
|
+
if (this.configLoaded) return undefined;
|
|
101
|
+
// The config read runs synchronously, so it can't heartbeat.
|
|
102
|
+
const activities = workflow.proxyActivities<{
|
|
103
|
+
[GET_MODEL_CONFIG_ACTIVITY](input: GetModelConfigInput): Promise<ModelConfigInfo>;
|
|
104
|
+
}>({
|
|
105
|
+
startToCloseTimeout: '10 minutes',
|
|
106
|
+
...this.activityOptions,
|
|
107
|
+
heartbeatTimeout: undefined,
|
|
108
|
+
});
|
|
109
|
+
return activities[GET_MODEL_CONFIG_ACTIVITY]({ modelName: this.modelName }).then(({ stateful, ...config }) => {
|
|
110
|
+
this.config = config;
|
|
111
|
+
// A result recorded before the flag existed has none: the model ran as stateless then.
|
|
112
|
+
this.isStateful = stateful === true;
|
|
113
|
+
this.configLoaded = true;
|
|
114
|
+
});
|
|
52
115
|
}
|
|
53
116
|
|
|
54
117
|
override async *stream(messages: Message[], options?: StreamOptions): AsyncIterable<ModelStreamEvent> {
|
|
55
118
|
const proxiedActivities = workflow.proxyActivities<{
|
|
56
119
|
invokeModel(input: InvokeModelInput): Promise<ModelStreamEvent[]>;
|
|
57
120
|
invokeModelStreaming(input: InvokeModelStreamingInput): Promise<ModelStreamEvent[]>;
|
|
121
|
+
[INVOKE_STATEFUL_MODEL_ACTIVITY](input: InvokeStatefulModelInput): Promise<InvokeStatefulModelResult>;
|
|
122
|
+
[INVOKE_STATEFUL_MODEL_STREAMING_ACTIVITY](
|
|
123
|
+
input: InvokeStatefulModelStreamingInput
|
|
124
|
+
): Promise<InvokeStatefulModelResult>;
|
|
58
125
|
}>({
|
|
59
126
|
startToCloseTimeout: '10 minutes',
|
|
60
127
|
...this.activityOptions,
|
|
128
|
+
heartbeatTimeout: this.activityOptions.heartbeatTimeout ?? '10 seconds',
|
|
61
129
|
});
|
|
62
130
|
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
131
|
+
const { modelState, ...rest } = options ?? {};
|
|
132
|
+
const input = { modelName: this.modelName, messages, options: this.isStateful ? rest : options };
|
|
133
|
+
const streaming = this.streamingTopic
|
|
134
|
+
? { streamingTopic: this.streamingTopic, streamingBatchInterval: this.streamingBatchInterval }
|
|
135
|
+
: undefined;
|
|
136
|
+
|
|
137
|
+
if (!this.isStateful) {
|
|
138
|
+
let events: ModelStreamEvent[];
|
|
139
|
+
try {
|
|
140
|
+
events = streaming
|
|
141
|
+
? await proxiedActivities.invokeModelStreaming({ ...input, ...streaming })
|
|
142
|
+
: await proxiedActivities.invokeModel(input);
|
|
143
|
+
} catch (err) {
|
|
144
|
+
// Give the agent Strands' own error for a context overflow, so its conversation manager can
|
|
145
|
+
// trim the conversation and call the model again. Keep the activity failure as its cause.
|
|
146
|
+
if (
|
|
147
|
+
err instanceof workflow.ActivityFailure &&
|
|
148
|
+
applicationFailureType(err.cause) === CONTEXT_WINDOW_OVERFLOW_TYPE
|
|
149
|
+
) {
|
|
150
|
+
throw Object.assign(new ContextWindowOverflowError(err.cause?.message ?? ''), { cause: err });
|
|
151
|
+
}
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
yield* events;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// A stateful model's overflow is not rethrown as Strands' error: its conversation is on the
|
|
159
|
+
// server, and the default manager the agent keeps would trim and retry where Strands, which
|
|
160
|
+
// gives a stateful agent no manager, fails the turn.
|
|
76
161
|
|
|
77
|
-
|
|
78
|
-
|
|
162
|
+
// The agent's model state is a live store; send a copy of its values, and copy the updated
|
|
163
|
+
// values back, as the model would have written them natively.
|
|
164
|
+
const statefulInput = { ...input, modelState: modelState?.getAll() ?? {} };
|
|
165
|
+
const result = streaming
|
|
166
|
+
? await proxiedActivities[INVOKE_STATEFUL_MODEL_STREAMING_ACTIVITY]({ ...statefulInput, ...streaming })
|
|
167
|
+
: await proxiedActivities[INVOKE_STATEFUL_MODEL_ACTIVITY](statefulInput);
|
|
168
|
+
if (modelState) {
|
|
169
|
+
modelState.clear();
|
|
170
|
+
for (const [key, value] of Object.entries(result.modelState)) modelState.set(key, value);
|
|
79
171
|
}
|
|
172
|
+
yield* result.events;
|
|
80
173
|
}
|
|
81
174
|
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { DefaultFailureConverter, type FailureConverter, type PayloadConverter, type ProtoFailure, type SerializationContext } from '@temporalio/common';
|
|
2
|
-
export declare const STRANDS_INTERRUPT_TYPE = "StrandsInterrupt";
|
|
3
|
-
/**
|
|
4
|
-
* Failure converter that preserves Strands `InterruptError` payloads across the
|
|
5
|
-
* activity boundary as typed, non-retryable {@link ApplicationFailure}s and
|
|
6
|
-
* marks deterministic Strands model errors as non-retryable.
|
|
7
|
-
*
|
|
8
|
-
* Read by the worker and client via {@link DataConverter.failureConverterPath};
|
|
9
|
-
* the named export below is what {@link StrandsPlugin} wires in.
|
|
10
|
-
*/
|
|
11
|
-
export declare class StrandsFailureConverter extends DefaultFailureConverter implements FailureConverter {
|
|
12
|
-
errorToFailure(err: unknown, payloadConverter: PayloadConverter, context?: SerializationContext): ProtoFailure;
|
|
13
|
-
}
|
|
14
|
-
export declare const failureConverter: FailureConverter;
|
package/lib/failure-converter.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.failureConverter = exports.StrandsFailureConverter = exports.STRANDS_INTERRUPT_TYPE = void 0;
|
|
4
|
-
const sdk_1 = require("@strands-agents/sdk");
|
|
5
|
-
const common_1 = require("@temporalio/common");
|
|
6
|
-
exports.STRANDS_INTERRUPT_TYPE = 'StrandsInterrupt';
|
|
7
|
-
function isInterruptError(err) {
|
|
8
|
-
if (err === null || typeof err !== 'object')
|
|
9
|
-
return false;
|
|
10
|
-
const candidate = err;
|
|
11
|
-
if (!Array.isArray(candidate.interrupts) || candidate.interrupts.length === 0)
|
|
12
|
-
return false;
|
|
13
|
-
const first = candidate.interrupts[0];
|
|
14
|
-
return typeof first?.toJSON === 'function';
|
|
15
|
-
}
|
|
16
|
-
const TERMINAL_ERROR_TYPES = [
|
|
17
|
-
{ ctor: sdk_1.ContextWindowOverflowError, name: 'ContextWindowOverflowError' },
|
|
18
|
-
{ ctor: sdk_1.MaxTokensError, name: 'MaxTokensError' },
|
|
19
|
-
{ ctor: sdk_1.StructuredOutputError, name: 'StructuredOutputError' },
|
|
20
|
-
];
|
|
21
|
-
function rewriteError(err) {
|
|
22
|
-
if (isInterruptError(err)) {
|
|
23
|
-
const details = err.interrupts.map((i) => i.toJSON());
|
|
24
|
-
const names = err.interrupts.map((i) => i.toJSON().name).join(',');
|
|
25
|
-
return common_1.ApplicationFailure.create({
|
|
26
|
-
message: `interrupt:${names}`,
|
|
27
|
-
type: exports.STRANDS_INTERRUPT_TYPE,
|
|
28
|
-
nonRetryable: true,
|
|
29
|
-
details,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
if (err instanceof Error) {
|
|
33
|
-
for (const { ctor, name } of TERMINAL_ERROR_TYPES) {
|
|
34
|
-
if (err instanceof ctor) {
|
|
35
|
-
return common_1.ApplicationFailure.create({
|
|
36
|
-
message: err.message,
|
|
37
|
-
type: name,
|
|
38
|
-
nonRetryable: true,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return err;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Failure converter that preserves Strands `InterruptError` payloads across the
|
|
47
|
-
* activity boundary as typed, non-retryable {@link ApplicationFailure}s and
|
|
48
|
-
* marks deterministic Strands model errors as non-retryable.
|
|
49
|
-
*
|
|
50
|
-
* Read by the worker and client via {@link DataConverter.failureConverterPath};
|
|
51
|
-
* the named export below is what {@link StrandsPlugin} wires in.
|
|
52
|
-
*/
|
|
53
|
-
class StrandsFailureConverter extends common_1.DefaultFailureConverter {
|
|
54
|
-
errorToFailure(err, payloadConverter, context) {
|
|
55
|
-
return super.errorToFailure(rewriteError(err), payloadConverter, context);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
exports.StrandsFailureConverter = StrandsFailureConverter;
|
|
59
|
-
// ts-prune-ignore-next
|
|
60
|
-
exports.failureConverter = new StrandsFailureConverter();
|
|
61
|
-
//# sourceMappingURL=failure-converter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"failure-converter.js","sourceRoot":"","sources":["../src/failure-converter.ts"],"names":[],"mappings":";;;AAAA,6CAAwG;AACxG,+CAO4B;AAEf,QAAA,sBAAsB,GAAG,kBAAkB,CAAC;AAQzD,SAAS,gBAAgB,CAAC,GAAY;IACpC,IAAI,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC1D,MAAM,SAAS,GAAG,GAA+B,CAAC;IAClD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,SAAS,CAAC,UAAU,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC5F,MAAM,KAAK,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC,CAAqC,CAAC;IAC1E,OAAO,OAAO,KAAK,EAAE,MAAM,KAAK,UAAU,CAAC;AAC7C,CAAC;AAED,MAAM,oBAAoB,GAAG;IAC3B,EAAE,IAAI,EAAE,gCAA0B,EAAE,IAAI,EAAE,4BAA4B,EAAE;IACxE,EAAE,IAAI,EAAE,oBAAc,EAAE,IAAI,EAAE,gBAAgB,EAAE;IAChD,EAAE,IAAI,EAAE,2BAAqB,EAAE,IAAI,EAAE,uBAAuB,EAAE;CAC/D,CAAC;AAEF,SAAS,YAAY,CAAC,GAAY;IAChC,IAAI,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1B,MAAM,OAAO,GAAG,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;QACtD,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnE,OAAO,2BAAkB,CAAC,MAAM,CAAC;YAC/B,OAAO,EAAE,aAAa,KAAK,EAAE;YAC7B,IAAI,EAAE,8BAAsB;YAC5B,YAAY,EAAE,IAAI;YAClB,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IACD,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;QACzB,KAAK,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,oBAAoB,EAAE,CAAC;YAClD,IAAI,GAAG,YAAY,IAAI,EAAE,CAAC;gBACxB,OAAO,2BAAkB,CAAC,MAAM,CAAC;oBAC/B,OAAO,EAAE,GAAG,CAAC,OAAO;oBACpB,IAAI,EAAE,IAAI;oBACV,YAAY,EAAE,IAAI;iBACnB,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;;;GAOG;AACH,MAAa,uBAAwB,SAAQ,gCAAuB;IACzD,cAAc,CACrB,GAAY,EACZ,gBAAkC,EAClC,OAA8B;QAE9B,OAAO,KAAK,CAAC,cAAc,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;IAC5E,CAAC;CACF;AARD,0DAQC;AAED,uBAAuB;AACV,QAAA,gBAAgB,GAAqB,IAAI,uBAAuB,EAAE,CAAC"}
|
package/src/failure-converter.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { ContextWindowOverflowError, MaxTokensError, StructuredOutputError } from '@strands-agents/sdk';
|
|
2
|
-
import {
|
|
3
|
-
ApplicationFailure,
|
|
4
|
-
DefaultFailureConverter,
|
|
5
|
-
type FailureConverter,
|
|
6
|
-
type PayloadConverter,
|
|
7
|
-
type ProtoFailure,
|
|
8
|
-
type SerializationContext,
|
|
9
|
-
} from '@temporalio/common';
|
|
10
|
-
|
|
11
|
-
export const STRANDS_INTERRUPT_TYPE = 'StrandsInterrupt';
|
|
12
|
-
|
|
13
|
-
// Strands does not re-export `InterruptError` from its public index, so detect
|
|
14
|
-
// it structurally instead of via `instanceof`.
|
|
15
|
-
interface InterruptLike {
|
|
16
|
-
toJSON(): { id: string; name: string; reason?: unknown; response?: unknown; source: string };
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function isInterruptError(err: unknown): err is { interrupts: InterruptLike[] } {
|
|
20
|
-
if (err === null || typeof err !== 'object') return false;
|
|
21
|
-
const candidate = err as { interrupts?: unknown };
|
|
22
|
-
if (!Array.isArray(candidate.interrupts) || candidate.interrupts.length === 0) return false;
|
|
23
|
-
const first = candidate.interrupts[0] as { toJSON?: unknown } | undefined;
|
|
24
|
-
return typeof first?.toJSON === 'function';
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const TERMINAL_ERROR_TYPES = [
|
|
28
|
-
{ ctor: ContextWindowOverflowError, name: 'ContextWindowOverflowError' },
|
|
29
|
-
{ ctor: MaxTokensError, name: 'MaxTokensError' },
|
|
30
|
-
{ ctor: StructuredOutputError, name: 'StructuredOutputError' },
|
|
31
|
-
];
|
|
32
|
-
|
|
33
|
-
function rewriteError(err: unknown): unknown {
|
|
34
|
-
if (isInterruptError(err)) {
|
|
35
|
-
const details = err.interrupts.map((i) => i.toJSON());
|
|
36
|
-
const names = err.interrupts.map((i) => i.toJSON().name).join(',');
|
|
37
|
-
return ApplicationFailure.create({
|
|
38
|
-
message: `interrupt:${names}`,
|
|
39
|
-
type: STRANDS_INTERRUPT_TYPE,
|
|
40
|
-
nonRetryable: true,
|
|
41
|
-
details,
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
if (err instanceof Error) {
|
|
45
|
-
for (const { ctor, name } of TERMINAL_ERROR_TYPES) {
|
|
46
|
-
if (err instanceof ctor) {
|
|
47
|
-
return ApplicationFailure.create({
|
|
48
|
-
message: err.message,
|
|
49
|
-
type: name,
|
|
50
|
-
nonRetryable: true,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return err;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Failure converter that preserves Strands `InterruptError` payloads across the
|
|
60
|
-
* activity boundary as typed, non-retryable {@link ApplicationFailure}s and
|
|
61
|
-
* marks deterministic Strands model errors as non-retryable.
|
|
62
|
-
*
|
|
63
|
-
* Read by the worker and client via {@link DataConverter.failureConverterPath};
|
|
64
|
-
* the named export below is what {@link StrandsPlugin} wires in.
|
|
65
|
-
*/
|
|
66
|
-
export class StrandsFailureConverter extends DefaultFailureConverter implements FailureConverter {
|
|
67
|
-
override errorToFailure(
|
|
68
|
-
err: unknown,
|
|
69
|
-
payloadConverter: PayloadConverter,
|
|
70
|
-
context?: SerializationContext
|
|
71
|
-
): ProtoFailure {
|
|
72
|
-
return super.errorToFailure(rewriteError(err), payloadConverter, context);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ts-prune-ignore-next
|
|
77
|
-
export const failureConverter: FailureConverter = new StrandsFailureConverter();
|