@temporalio/strands-agents 1.21.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/LICENSE +21 -0
- package/README.md +351 -0
- package/lib/empty-module.d.ts +4 -0
- package/lib/empty-module.js +21 -0
- package/lib/empty-module.js.map +1 -0
- package/lib/failure-converter.d.ts +14 -0
- package/lib/failure-converter.js +60 -0
- package/lib/failure-converter.js.map +1 -0
- package/lib/heartbeat.d.ts +6 -0
- package/lib/heartbeat.js +27 -0
- package/lib/heartbeat.js.map +1 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +45 -0
- package/lib/index.js.map +1 -0
- package/lib/json-schema.d.ts +10 -0
- package/lib/json-schema.js +17 -0
- package/lib/json-schema.js.map +1 -0
- package/lib/load-polyfills.d.ts +1 -0
- package/lib/load-polyfills.js +23 -0
- package/lib/load-polyfills.js.map +1 -0
- package/lib/model-activity.d.ts +41 -0
- package/lib/model-activity.js +100 -0
- package/lib/model-activity.js.map +1 -0
- package/lib/plugin.d.ts +79 -0
- package/lib/plugin.js +152 -0
- package/lib/plugin.js.map +1 -0
- package/lib/temporal-activity-tool.d.ts +42 -0
- package/lib/temporal-activity-tool.js +109 -0
- package/lib/temporal-activity-tool.js.map +1 -0
- package/lib/temporal-agent.d.ts +41 -0
- package/lib/temporal-agent.js +80 -0
- package/lib/temporal-agent.js.map +1 -0
- package/lib/temporal-mcp-client.d.ts +107 -0
- package/lib/temporal-mcp-client.js +237 -0
- package/lib/temporal-mcp-client.js.map +1 -0
- package/lib/temporal-mcp-tool.d.ts +24 -0
- package/lib/temporal-mcp-tool.js +109 -0
- package/lib/temporal-mcp-tool.js.map +1 -0
- package/lib/temporal-model.d.ts +41 -0
- package/lib/temporal-model.js +88 -0
- package/lib/temporal-model.js.map +1 -0
- package/lib/workflow.d.ts +39 -0
- package/lib/workflow.js +66 -0
- package/lib/workflow.js.map +1 -0
- package/package.json +72 -0
- package/src/empty-module.ts +20 -0
- package/src/failure-converter.ts +76 -0
- package/src/heartbeat.ts +24 -0
- package/src/index.ts +16 -0
- package/src/json-schema.ts +14 -0
- package/src/load-polyfills.ts +24 -0
- package/src/model-activity.ts +124 -0
- package/src/plugin.ts +215 -0
- package/src/temporal-activity-tool.ts +127 -0
- package/src/temporal-agent.ts +103 -0
- package/src/temporal-mcp-client.ts +274 -0
- package/src/temporal-mcp-tool.ts +89 -0
- package/src/temporal-model.ts +94 -0
- package/src/workflow.ts +60 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Message } from '@strands-agents/sdk';
|
|
2
|
+
import type { Model, ModelStreamEvent, StreamOptions } from '@strands-agents/sdk';
|
|
3
|
+
import type { Duration } from '@temporalio/common/lib/time';
|
|
4
|
+
/**
|
|
5
|
+
* Input for the `invokeModel` activity. Mirrors `Model.stream(messages, options)`
|
|
6
|
+
* with the chosen factory name carried alongside.
|
|
7
|
+
*/
|
|
8
|
+
export interface InvokeModelInput {
|
|
9
|
+
modelName?: string;
|
|
10
|
+
messages: Message[];
|
|
11
|
+
options?: StreamOptions;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Input for the `invokeModelStreaming` activity. Adds the workflow-stream topic
|
|
15
|
+
* to which each event is published, and the batch interval for publishes.
|
|
16
|
+
*/
|
|
17
|
+
export interface InvokeModelStreamingInput extends InvokeModelInput {
|
|
18
|
+
streamingTopic: string;
|
|
19
|
+
streamingBatchInterval: Duration;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Holds the model factory map and exposes the model activities.
|
|
23
|
+
*
|
|
24
|
+
* Models are constructed lazily on first use and cached for the worker's
|
|
25
|
+
* lifetime. `defaultName` is only set by the plugin's own implicit
|
|
26
|
+
* default-Bedrock fallback; user-supplied factories force `model: 'name'`
|
|
27
|
+
* on every {@link TemporalAgent}.
|
|
28
|
+
*
|
|
29
|
+
* Both activities heartbeat at half the configured `heartbeatTimeout` via
|
|
30
|
+
* {@link autoHeartbeat} so long model calls don't stall the worker's view
|
|
31
|
+
* of activity liveness.
|
|
32
|
+
*/
|
|
33
|
+
export declare class ModelActivity {
|
|
34
|
+
private readonly factories;
|
|
35
|
+
private readonly defaultName?;
|
|
36
|
+
private readonly models;
|
|
37
|
+
constructor(factories: Record<string, () => Model>, defaultName?: string | undefined);
|
|
38
|
+
private getModel;
|
|
39
|
+
invokeModel(input: InvokeModelInput): Promise<ModelStreamEvent[]>;
|
|
40
|
+
invokeModelStreaming(input: InvokeModelStreamingInput): Promise<ModelStreamEvent[]>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModelActivity = void 0;
|
|
4
|
+
const sdk_1 = require("@strands-agents/sdk");
|
|
5
|
+
const client_1 = require("@temporalio/workflow-streams/client");
|
|
6
|
+
const heartbeat_1 = require("./heartbeat");
|
|
7
|
+
/**
|
|
8
|
+
* Reconstruct Strands `Message` instances from the wire data the workflow
|
|
9
|
+
* payload converter delivered.
|
|
10
|
+
*
|
|
11
|
+
* Strands content-block and `Message` classes override `toJSON()` to project
|
|
12
|
+
* themselves into Bedrock-style wire data (e.g. `TextBlock` → `{ text }`,
|
|
13
|
+
* `ToolUseBlock` → `{ toolUse: {...} }`). Temporal's default payload
|
|
14
|
+
* converter serializes activity input via `JSON.stringify`, which calls
|
|
15
|
+
* those `toJSON`s, dropping the `type` discriminator and reshaping every
|
|
16
|
+
* block. The receiving side sees plain Bedrock-shape objects that don't
|
|
17
|
+
* match what `BedrockModel._formatContentBlock` (or any other Strands
|
|
18
|
+
* `Model.stream` implementation) expects.
|
|
19
|
+
*
|
|
20
|
+
* `Message.fromMessageData` knows how to rebuild a `Message` and its blocks
|
|
21
|
+
* from that wire shape via `contentBlockFromData`, so call it once on every
|
|
22
|
+
* incoming message before handing them to the user-supplied `Model`.
|
|
23
|
+
*/
|
|
24
|
+
function rebuildMessages(messages) {
|
|
25
|
+
if (!Array.isArray(messages)) {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
return messages.map((m) => m instanceof sdk_1.Message ? m : sdk_1.Message.fromMessageData(m));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Holds the model factory map and exposes the model activities.
|
|
32
|
+
*
|
|
33
|
+
* Models are constructed lazily on first use and cached for the worker's
|
|
34
|
+
* lifetime. `defaultName` is only set by the plugin's own implicit
|
|
35
|
+
* default-Bedrock fallback; user-supplied factories force `model: 'name'`
|
|
36
|
+
* on every {@link TemporalAgent}.
|
|
37
|
+
*
|
|
38
|
+
* Both activities heartbeat at half the configured `heartbeatTimeout` via
|
|
39
|
+
* {@link autoHeartbeat} so long model calls don't stall the worker's view
|
|
40
|
+
* of activity liveness.
|
|
41
|
+
*/
|
|
42
|
+
class ModelActivity {
|
|
43
|
+
factories;
|
|
44
|
+
defaultName;
|
|
45
|
+
models = new Map();
|
|
46
|
+
constructor(factories, defaultName) {
|
|
47
|
+
this.factories = factories;
|
|
48
|
+
this.defaultName = defaultName;
|
|
49
|
+
this.invokeModel = (0, heartbeat_1.autoHeartbeat)(this.invokeModel.bind(this));
|
|
50
|
+
this.invokeModelStreaming = (0, heartbeat_1.autoHeartbeat)(this.invokeModelStreaming.bind(this));
|
|
51
|
+
}
|
|
52
|
+
getModel(name) {
|
|
53
|
+
const resolved = name ?? this.defaultName;
|
|
54
|
+
if (resolved === undefined) {
|
|
55
|
+
throw new Error(`TemporalAgent was constructed without an explicit \`model\`, but the plugin was configured ` +
|
|
56
|
+
`with user-supplied \`models\`. Pass model='...' to TemporalAgent. ` +
|
|
57
|
+
`Known: ${Object.keys(this.factories).sort().join(', ')}`);
|
|
58
|
+
}
|
|
59
|
+
let model = this.models.get(resolved);
|
|
60
|
+
if (model === undefined) {
|
|
61
|
+
const factory = this.factories[resolved];
|
|
62
|
+
if (factory === undefined) {
|
|
63
|
+
throw new Error(`Unknown model name '${resolved}'. Known: ${Object.keys(this.factories).sort().join(', ')}`);
|
|
64
|
+
}
|
|
65
|
+
model = factory();
|
|
66
|
+
this.models.set(resolved, model);
|
|
67
|
+
}
|
|
68
|
+
return model;
|
|
69
|
+
}
|
|
70
|
+
async invokeModel(input) {
|
|
71
|
+
const model = this.getModel(input.modelName);
|
|
72
|
+
const messages = rebuildMessages(input.messages);
|
|
73
|
+
const events = [];
|
|
74
|
+
for await (const event of model.stream(messages, input.options)) {
|
|
75
|
+
events.push(event);
|
|
76
|
+
}
|
|
77
|
+
return events;
|
|
78
|
+
}
|
|
79
|
+
async invokeModelStreaming(input) {
|
|
80
|
+
const model = this.getModel(input.modelName);
|
|
81
|
+
const messages = rebuildMessages(input.messages);
|
|
82
|
+
const stream = client_1.WorkflowStreamClient.fromWithinActivity({
|
|
83
|
+
batchInterval: input.streamingBatchInterval,
|
|
84
|
+
});
|
|
85
|
+
const topic = stream.topic(input.streamingTopic);
|
|
86
|
+
const events = [];
|
|
87
|
+
try {
|
|
88
|
+
for await (const event of model.stream(messages, input.options)) {
|
|
89
|
+
events.push(event);
|
|
90
|
+
topic.publish(event);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
await stream[Symbol.asyncDispose]();
|
|
95
|
+
}
|
|
96
|
+
return events;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.ModelActivity = ModelActivity;
|
|
100
|
+
//# sourceMappingURL=model-activity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-activity.js","sourceRoot":"","sources":["../src/model-activity.ts"],"names":[],"mappings":";;;AAAA,6CAA8C;AAE9C,gEAA2E;AAE3E,2CAA4C;AAE5C;;;;;;;;;;;;;;;;GAgBG;AACH,SAAS,eAAe,CAAC,QAAiB;IACxC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC7B,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,OAAO,QAAQ,CAAC,GAAG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E,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBACnB,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC;QACtC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AA5DD,sCA4DC"}
|
package/lib/plugin.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { McpClient } from '@strands-agents/sdk';
|
|
2
|
+
import { type Model } from '@strands-agents/sdk';
|
|
3
|
+
import type { Duration } from '@temporalio/common';
|
|
4
|
+
import type { BundleOptions } from '@temporalio/worker';
|
|
5
|
+
import { SimplePlugin } from '@temporalio/plugin';
|
|
6
|
+
/**
|
|
7
|
+
* Options for {@link StrandsPlugin}.
|
|
8
|
+
*
|
|
9
|
+
* - `models` — name → factory map. Each factory is called lazily on first
|
|
10
|
+
* use and the constructed model is cached for the worker's lifetime.
|
|
11
|
+
* `TemporalAgent({ model: 'name', ... })` selects which factory to invoke.
|
|
12
|
+
* If omitted, the plugin registers a single `BedrockModel` factory under
|
|
13
|
+
* the name `'bedrock'` to match Strands' own implicit default.
|
|
14
|
+
*
|
|
15
|
+
* - `mcpClients` — name → MCP client factory map. Workflow-side
|
|
16
|
+
* `TemporalMCPClient({ server: 'name' })` selects a factory by name; the
|
|
17
|
+
* per-server `{name}-listTools` and `{name}-callTool` activities reuse one
|
|
18
|
+
* lazily-opened worker-process connection. Tools are enumerated live on each
|
|
19
|
+
* `listTools`. By default (`TemporalMCPClient`'s `cacheTools: false`) the
|
|
20
|
+
* agent re-lists on each turn, so it picks up MCP-server changes (including
|
|
21
|
+
* redeploys) mid-workflow without restarting the worker; set `cacheTools:
|
|
22
|
+
* true` to list once at the start of the workflow instead.
|
|
23
|
+
*
|
|
24
|
+
* - `mcpConnectionIdleTimeout` — how long a worker-process MCP connection is
|
|
25
|
+
* kept open between `callTool` activities before it's disconnected. The timer
|
|
26
|
+
* resets on every reuse. Accepts a millisecond number or a duration string
|
|
27
|
+
* (e.g. `'5 minutes'`), like `startToCloseTimeout`. Defaults to
|
|
28
|
+
* {@link MCP_CONNECTION_IDLE_MS} (5 minutes).
|
|
29
|
+
*/
|
|
30
|
+
export interface StrandsPluginOptions {
|
|
31
|
+
models?: Record<string, () => Model>;
|
|
32
|
+
mcpClients?: Record<string, () => McpClient>;
|
|
33
|
+
mcpConnectionIdleTimeout?: Duration;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Temporal plugin that runs Strands Agents inside Temporal workflows. Model,
|
|
37
|
+
* MCP tool, and (via {@link workflow.activityAsTool}) custom-tool invocations
|
|
38
|
+
* are routed through Temporal Activities for durable execution, retries, and
|
|
39
|
+
* timeouts.
|
|
40
|
+
*
|
|
41
|
+
* Register on the worker (and on the client when using `activityAsTool` with
|
|
42
|
+
* interrupts, so the client-side data converter picks up the failure
|
|
43
|
+
* converter):
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* const client = await Client.connect({ plugins: [new StrandsPlugin({...})] });
|
|
47
|
+
* const worker = new Worker({ ..., plugins: [new StrandsPlugin({...})] });
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare class StrandsPlugin extends SimplePlugin {
|
|
51
|
+
constructor(options?: StrandsPluginOptions);
|
|
52
|
+
/**
|
|
53
|
+
* Extend the bundler config so workflow code can bundle `@strands-agents/sdk`:
|
|
54
|
+
*
|
|
55
|
+
* - Ignore worker-only / non-workflow-safe modules that the SDK index
|
|
56
|
+
* transitively pulls in but that are unreachable from workflow code.
|
|
57
|
+
* This covers:
|
|
58
|
+
* - `fs` (statically imported by `vended-plugins` and `vended-tools`).
|
|
59
|
+
* - Every `@strands-agents/sdk` model provider's HTTP SDK
|
|
60
|
+
* (`@aws-sdk/client-bedrock-runtime`, `@anthropic-ai/sdk`, `openai`,
|
|
61
|
+
* `@google/genai`, `ai`). The workers constructs models worker-side;
|
|
62
|
+
* workflow code only goes through {@link TemporalModel}.
|
|
63
|
+
* - `@temporalio/activity` and `@temporalio/client`, which the
|
|
64
|
+
* worker-only halves of {@link TemporalMCPClient}, {@link autoHeartbeat},
|
|
65
|
+
* and `@temporalio/workflow-streams/client` import.
|
|
66
|
+
* - `path` / `crypto`, pulled in transitively by the same worker-only code.
|
|
67
|
+
*
|
|
68
|
+
* - Replace the dynamic-imported MCP transport helpers
|
|
69
|
+
* (`@modelcontextprotocol/sdk/client/{stdio,sse,streamableHttp}.js` and
|
|
70
|
+
* their `node:*` dependencies, plus `eventsource-parser/stream` which
|
|
71
|
+
* `streamableHttp.js` reaches) with an empty module. They live in
|
|
72
|
+
* `mcp-config.js`'s server-only code paths.
|
|
73
|
+
*
|
|
74
|
+
* - Inline async chunks so the bundle stays a single file — webpack's
|
|
75
|
+
* default JSONP chunk loader references `self`/`document`, neither of
|
|
76
|
+
* which exists in the workflow VM.
|
|
77
|
+
*/
|
|
78
|
+
configureBundler(options: BundleOptions): BundleOptions;
|
|
79
|
+
}
|
package/lib/plugin.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StrandsPlugin = void 0;
|
|
4
|
+
const sdk_1 = require("@strands-agents/sdk");
|
|
5
|
+
const time_1 = require("@temporalio/common/lib/time");
|
|
6
|
+
const plugin_1 = require("@temporalio/plugin");
|
|
7
|
+
const model_activity_1 = require("./model-activity");
|
|
8
|
+
const temporal_mcp_client_1 = require("./temporal-mcp-client");
|
|
9
|
+
/**
|
|
10
|
+
* Temporal plugin that runs Strands Agents inside Temporal workflows. Model,
|
|
11
|
+
* MCP tool, and (via {@link workflow.activityAsTool}) custom-tool invocations
|
|
12
|
+
* are routed through Temporal Activities for durable execution, retries, and
|
|
13
|
+
* timeouts.
|
|
14
|
+
*
|
|
15
|
+
* Register on the worker (and on the client when using `activityAsTool` with
|
|
16
|
+
* interrupts, so the client-side data converter picks up the failure
|
|
17
|
+
* converter):
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* const client = await Client.connect({ plugins: [new StrandsPlugin({...})] });
|
|
21
|
+
* const worker = new Worker({ ..., plugins: [new StrandsPlugin({...})] });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
class StrandsPlugin extends plugin_1.SimplePlugin {
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
let modelFactories = options.models;
|
|
27
|
+
let defaultName;
|
|
28
|
+
if (modelFactories === undefined) {
|
|
29
|
+
modelFactories = { bedrock: () => new sdk_1.BedrockModel({}) };
|
|
30
|
+
defaultName = 'bedrock';
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const names = Object.keys(modelFactories);
|
|
34
|
+
if (names.length === 1) {
|
|
35
|
+
defaultName = names[0];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const modelActivity = new model_activity_1.ModelActivity(modelFactories, defaultName);
|
|
39
|
+
const activities = {
|
|
40
|
+
invokeModel: (input) => modelActivity.invokeModel(input),
|
|
41
|
+
invokeModelStreaming: (input) => modelActivity.invokeModelStreaming(input),
|
|
42
|
+
};
|
|
43
|
+
const mcpClients = options.mcpClients ?? {};
|
|
44
|
+
const idleMs = (0, time_1.msOptionalToNumber)(options.mcpConnectionIdleTimeout);
|
|
45
|
+
for (const [server, factory] of Object.entries(mcpClients)) {
|
|
46
|
+
activities[(0, temporal_mcp_client_1.listToolsActivityName)(server)] = (0, temporal_mcp_client_1.buildListToolsActivity)(server, factory, idleMs);
|
|
47
|
+
activities[(0, temporal_mcp_client_1.callToolActivityName)(server)] = (0, temporal_mcp_client_1.buildCallToolActivity)(server, factory, idleMs);
|
|
48
|
+
}
|
|
49
|
+
const runContext = async (next) => {
|
|
50
|
+
try {
|
|
51
|
+
await next();
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
// Close any worker-process MCP connections opened lazily by the
|
|
55
|
+
// listTools/callTool activities.
|
|
56
|
+
for (const server of Object.keys(mcpClients)) {
|
|
57
|
+
await (0, temporal_mcp_client_1._evictConnection)(server);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
super({
|
|
62
|
+
name: 'StrandsPlugin',
|
|
63
|
+
activities,
|
|
64
|
+
runContext,
|
|
65
|
+
dataConverter: {
|
|
66
|
+
failureConverterPath: require.resolve('./failure-converter'),
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Extend the bundler config so workflow code can bundle `@strands-agents/sdk`:
|
|
72
|
+
*
|
|
73
|
+
* - Ignore worker-only / non-workflow-safe modules that the SDK index
|
|
74
|
+
* transitively pulls in but that are unreachable from workflow code.
|
|
75
|
+
* This covers:
|
|
76
|
+
* - `fs` (statically imported by `vended-plugins` and `vended-tools`).
|
|
77
|
+
* - Every `@strands-agents/sdk` model provider's HTTP SDK
|
|
78
|
+
* (`@aws-sdk/client-bedrock-runtime`, `@anthropic-ai/sdk`, `openai`,
|
|
79
|
+
* `@google/genai`, `ai`). The workers constructs models worker-side;
|
|
80
|
+
* workflow code only goes through {@link TemporalModel}.
|
|
81
|
+
* - `@temporalio/activity` and `@temporalio/client`, which the
|
|
82
|
+
* worker-only halves of {@link TemporalMCPClient}, {@link autoHeartbeat},
|
|
83
|
+
* and `@temporalio/workflow-streams/client` import.
|
|
84
|
+
* - `path` / `crypto`, pulled in transitively by the same worker-only code.
|
|
85
|
+
*
|
|
86
|
+
* - Replace the dynamic-imported MCP transport helpers
|
|
87
|
+
* (`@modelcontextprotocol/sdk/client/{stdio,sse,streamableHttp}.js` and
|
|
88
|
+
* their `node:*` dependencies, plus `eventsource-parser/stream` which
|
|
89
|
+
* `streamableHttp.js` reaches) with an empty module. They live in
|
|
90
|
+
* `mcp-config.js`'s server-only code paths.
|
|
91
|
+
*
|
|
92
|
+
* - Inline async chunks so the bundle stays a single file — webpack's
|
|
93
|
+
* default JSONP chunk loader references `self`/`document`, neither of
|
|
94
|
+
* which exists in the workflow VM.
|
|
95
|
+
*/
|
|
96
|
+
configureBundler(options) {
|
|
97
|
+
const base = super.configureBundler(options);
|
|
98
|
+
const prevHook = base.webpackConfigHook;
|
|
99
|
+
const ignoreModules = [
|
|
100
|
+
...(base.ignoreModules ?? []),
|
|
101
|
+
'fs',
|
|
102
|
+
'path',
|
|
103
|
+
'crypto',
|
|
104
|
+
'@temporalio/activity',
|
|
105
|
+
'@temporalio/client',
|
|
106
|
+
'@aws-sdk/client-bedrock-runtime',
|
|
107
|
+
'@aws-sdk/middleware-websocket',
|
|
108
|
+
'@anthropic-ai/sdk',
|
|
109
|
+
'openai',
|
|
110
|
+
'@google/genai',
|
|
111
|
+
'ai',
|
|
112
|
+
];
|
|
113
|
+
return {
|
|
114
|
+
...base,
|
|
115
|
+
ignoreModules,
|
|
116
|
+
webpackConfigHook: (config) => {
|
|
117
|
+
(config.output ??= {}).asyncChunks = false;
|
|
118
|
+
(config.optimization ??= {}).splitChunks = false;
|
|
119
|
+
const plugins = [...(config.plugins ?? [])];
|
|
120
|
+
plugins.push(emptyModuleRewritesPlugin());
|
|
121
|
+
const merged = { ...config, plugins };
|
|
122
|
+
return prevHook ? prevHook(merged) : merged;
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.StrandsPlugin = StrandsPlugin;
|
|
128
|
+
/**
|
|
129
|
+
* A webpack plugin that stubs out `@strands-agents/sdk`'s worker-only static and
|
|
130
|
+
* dynamic imports — `node:*` builtins and the MCP client transports — with an
|
|
131
|
+
* empty module, so they never reach the Workflow isolate. Paired with the
|
|
132
|
+
* `ignoreModules` whitelist (see {@link StrandsPlugin.configureBundler}).
|
|
133
|
+
*
|
|
134
|
+
* webpack is taken from `compiler.webpack` rather than imported or borrowed off
|
|
135
|
+
* another plugin's constructor: `@temporalio/strands-agents` doesn't depend on
|
|
136
|
+
* webpack directly, and using the compiler's own webpack guarantees the exact
|
|
137
|
+
* instance running the bundle regardless of how the SDK orders its own bundler
|
|
138
|
+
* plugins — never a second copy.
|
|
139
|
+
*/
|
|
140
|
+
function emptyModuleRewritesPlugin() {
|
|
141
|
+
const empty = require.resolve('./empty-module');
|
|
142
|
+
const ignorePattern = /^(?:node:(?:fs\/promises|os|path|process|stream)|@modelcontextprotocol\/sdk\/client\/(?:stdio|sse|streamableHttp)\.js|eventsource-parser\/stream)$/;
|
|
143
|
+
return {
|
|
144
|
+
apply(compiler) {
|
|
145
|
+
const { NormalModuleReplacementPlugin } = compiler.webpack;
|
|
146
|
+
new NormalModuleReplacementPlugin(ignorePattern, (resource) => {
|
|
147
|
+
resource.request = empty;
|
|
148
|
+
}).apply(compiler);
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;AACA,6CAA+D;AAE/D,sDAAiE;AAEjE,+CAAkD;AAClD,qDAAiD;AAEjD,+DAM+B;AAgC/B;;;;;;;;;;;;;;GAcG;AACH,MAAa,aAAc,SAAQ,qBAAY;IAC7C,YAAY,UAAgC,EAAE;QAC5C,IAAI,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;QACpC,IAAI,WAA+B,CAAC;QACpC,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;YACjC,cAAc,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI,kBAAY,CAAC,EAAE,CAAC,EAAE,CAAC;YACzD,WAAW,GAAG,SAAS,CAAC;QAC1B,CAAC;aAAM,CAAC;YACN,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;YAC1C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC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AAK,GAAG,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAChD,MAAM,aAAa,GACjB,oJAAoJ,CAAC;IACvJ,OAAO;QACL,KAAK,CAAC,QAAQ;YACZ,MAAM,EAAE,6BAA6B,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC;YAC3D,IAAI,6BAA6B,CAAC,aAAa,EAAE,CAAC,QAAQ,EAAE,EAAE;gBAC5D,QAAQ,CAAC,OAAO,GAAG,KAAK,CAAC;YAC3B,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACrB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Tool, type JSONSchema, type ToolContext, type ToolSpec, type ToolStreamGenerator } from '@strands-agents/sdk';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
import type { ActivityOptions } from '@temporalio/workflow';
|
|
4
|
+
/**
|
|
5
|
+
* Options accepted by {@link activityAsTool}.
|
|
6
|
+
*
|
|
7
|
+
* `name` defaults to the activity function's `.name`. `description` defaults
|
|
8
|
+
* to an empty string and SHOULD be supplied so the model has signal about
|
|
9
|
+
* when to call the tool. `inputSchema` describes the activity's single
|
|
10
|
+
* argument; pass either a literal JSON Schema object or a Zod schema, which
|
|
11
|
+
* is converted to JSON Schema for you.
|
|
12
|
+
*/
|
|
13
|
+
export interface ActivityAsToolOptions {
|
|
14
|
+
name?: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
inputSchema?: JSONSchema | z.ZodType;
|
|
17
|
+
activityOptions?: ActivityOptions;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A Strands {@link Tool} whose body dispatches a Temporal activity via
|
|
21
|
+
* {@link workflow.executeActivity}.
|
|
22
|
+
*
|
|
23
|
+
* When the activity fails with an {@link ApplicationFailure} whose `type`
|
|
24
|
+
* matches {@link STRANDS_INTERRUPT_TYPE} — see {@link StrandsFailureConverter} —
|
|
25
|
+
* `stream()` re-throws an {@link InterruptError} so the agent loop handles
|
|
26
|
+
* the interrupt the same way as a hook- or tool-body interrupt. Other
|
|
27
|
+
* failures are converted to an error {@link ToolResultBlock}.
|
|
28
|
+
*/
|
|
29
|
+
export declare class TemporalActivityTool extends Tool {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly description: string;
|
|
32
|
+
readonly toolSpec: ToolSpec;
|
|
33
|
+
private readonly activityName;
|
|
34
|
+
private readonly activityOptions;
|
|
35
|
+
constructor(activityName: string, options: {
|
|
36
|
+
name?: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
inputSchema?: JSONSchema | z.ZodType;
|
|
39
|
+
activityOptions?: ActivityOptions;
|
|
40
|
+
});
|
|
41
|
+
stream(toolContext: ToolContext): ToolStreamGenerator;
|
|
42
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.TemporalActivityTool = void 0;
|
|
27
|
+
const sdk_1 = require("@strands-agents/sdk");
|
|
28
|
+
const workflow = __importStar(require("@temporalio/workflow"));
|
|
29
|
+
const common_1 = require("@temporalio/common");
|
|
30
|
+
const failure_converter_1 = require("./failure-converter");
|
|
31
|
+
const json_schema_1 = require("./json-schema");
|
|
32
|
+
/**
|
|
33
|
+
* A Strands {@link Tool} whose body dispatches a Temporal activity via
|
|
34
|
+
* {@link workflow.executeActivity}.
|
|
35
|
+
*
|
|
36
|
+
* When the activity fails with an {@link ApplicationFailure} whose `type`
|
|
37
|
+
* matches {@link STRANDS_INTERRUPT_TYPE} — see {@link StrandsFailureConverter} —
|
|
38
|
+
* `stream()` re-throws an {@link InterruptError} so the agent loop handles
|
|
39
|
+
* the interrupt the same way as a hook- or tool-body interrupt. Other
|
|
40
|
+
* failures are converted to an error {@link ToolResultBlock}.
|
|
41
|
+
*/
|
|
42
|
+
class TemporalActivityTool extends sdk_1.Tool {
|
|
43
|
+
name;
|
|
44
|
+
description;
|
|
45
|
+
toolSpec;
|
|
46
|
+
activityName;
|
|
47
|
+
activityOptions;
|
|
48
|
+
constructor(activityName, options) {
|
|
49
|
+
super();
|
|
50
|
+
this.activityName = activityName;
|
|
51
|
+
this.name = options.name ?? activityName;
|
|
52
|
+
this.description = options.description ?? '';
|
|
53
|
+
this.toolSpec = {
|
|
54
|
+
name: this.name,
|
|
55
|
+
description: this.description,
|
|
56
|
+
inputSchema: (0, json_schema_1.toJsonSchema)(options.inputSchema) ?? { type: 'object', properties: {} },
|
|
57
|
+
};
|
|
58
|
+
this.activityOptions = options.activityOptions ?? {};
|
|
59
|
+
}
|
|
60
|
+
// eslint-disable-next-line require-yield
|
|
61
|
+
async *stream(toolContext) {
|
|
62
|
+
const { toolUseId, input } = toolContext.toolUse;
|
|
63
|
+
const activities = workflow.proxyActivities({
|
|
64
|
+
startToCloseTimeout: '10 minutes',
|
|
65
|
+
summary: this.name,
|
|
66
|
+
...this.activityOptions,
|
|
67
|
+
});
|
|
68
|
+
let activityResult;
|
|
69
|
+
try {
|
|
70
|
+
activityResult = await activities[this.activityName](input);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
if (err instanceof common_1.ActivityFailure &&
|
|
74
|
+
err.cause instanceof common_1.ApplicationFailure &&
|
|
75
|
+
err.cause.type === failure_converter_1.STRANDS_INTERRUPT_TYPE) {
|
|
76
|
+
const details = (err.cause.details ?? []);
|
|
77
|
+
const first = details[0];
|
|
78
|
+
// Route through the agent's interrupt() helper: throws InterruptError to
|
|
79
|
+
// halt on first call, returns the user's response on resume.
|
|
80
|
+
const response = toolContext.interrupt({
|
|
81
|
+
name: first?.name ?? 'interrupt',
|
|
82
|
+
reason: first?.reason,
|
|
83
|
+
});
|
|
84
|
+
return new sdk_1.ToolResultBlock({
|
|
85
|
+
toolUseId,
|
|
86
|
+
status: 'success',
|
|
87
|
+
content: [toResultContent(response)],
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return new sdk_1.ToolResultBlock({
|
|
91
|
+
toolUseId,
|
|
92
|
+
status: 'error',
|
|
93
|
+
content: [new sdk_1.TextBlock(String(err instanceof Error ? err.message : err))],
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return new sdk_1.ToolResultBlock({
|
|
97
|
+
toolUseId,
|
|
98
|
+
status: 'success',
|
|
99
|
+
content: [toResultContent(activityResult)],
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.TemporalActivityTool = TemporalActivityTool;
|
|
104
|
+
function toResultContent(value) {
|
|
105
|
+
if (typeof value === 'string')
|
|
106
|
+
return new sdk_1.TextBlock(value);
|
|
107
|
+
return new sdk_1.JsonBlock({ json: value });
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=temporal-activity-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temporal-activity-tool.js","sourceRoot":"","sources":["../src/temporal-activity-tool.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAU6B;AAE7B,+DAAiD;AAEjD,+CAAyE;AACzE,2DAA6D;AAC7D,+CAA6C;AAkB7C;;;;;;;;;GASG;AACH,MAAa,oBAAqB,SAAQ,UAAI;IACnC,IAAI,CAAS;IACb,WAAW,CAAS;IACpB,QAAQ,CAAW;IACX,YAAY,CAAS;IACrB,eAAe,CAAkB;IAElD,YACE,YAAoB,EACpB,OAKC;QAED,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,YAAY,CAAC;QACzC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,EAAE,CAAC;QAC7C,IAAI,CAAC,QAAQ,GAAG;YACd,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,WAAW,EAAE,IAAA,0BAAY,EAAC,OAAO,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE;SACrF,CAAC;QACF,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,EAAE,CAAC;IACvD,CAAC;IAED,yCAAyC;IACzC,KAAK,CAAC,CAAC,MAAM,CAAC,WAAwB;QACpC,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,WAAW,CAAC,OAAO,CAAC;QACjD,MAAM,UAAU,GAAG,QAAQ,CAAC,eAAe,CAExC;YACD,mBAAmB,EAAE,YAAY;YACjC,OAAO,EAAE,IAAI,CAAC,IAAI;YAClB,GAAG,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,oDA6EC;AAED,SAAS,eAAe,CAAC,KAAc;IACrC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,eAAS,CAAC,KAAK,CAAC,CAAC;IAC3D,OAAO,IAAI,eAAS,CAAC,EAAE,IAAI,EAAE,KAAkB,EAAE,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Agent } from '@strands-agents/sdk';
|
|
2
|
+
import type { AgentConfig } from '@strands-agents/sdk';
|
|
3
|
+
import type { ActivityOptions } from '@temporalio/workflow';
|
|
4
|
+
import type { Duration } from '@temporalio/common/lib/time';
|
|
5
|
+
/**
|
|
6
|
+
* Options for {@link TemporalAgent}.
|
|
7
|
+
*
|
|
8
|
+
* Accepts everything {@link AgentConfig} accepts except `model` (which is a
|
|
9
|
+
* factory name selecting a worker-side {@link Model}, not an instance) and
|
|
10
|
+
* `retryStrategy` (always disabled — Temporal handles retries via
|
|
11
|
+
* `activityOptions.retry`).
|
|
12
|
+
*
|
|
13
|
+
* The {@link ActivityOptions} apply to every model invocation this agent
|
|
14
|
+
* makes. `streamingTopic`/`streamingBatchInterval` route each
|
|
15
|
+
* {@link ModelStreamEvent} to a {@link WorkflowStream} topic of that name.
|
|
16
|
+
*/
|
|
17
|
+
export interface TemporalAgentOptions extends Omit<AgentConfig, 'model' | 'retryStrategy'> {
|
|
18
|
+
model?: string;
|
|
19
|
+
activityOptions?: ActivityOptions;
|
|
20
|
+
streamingTopic?: string;
|
|
21
|
+
streamingBatchInterval?: Duration;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A Strands {@link Agent} that routes every model call through a Temporal
|
|
25
|
+
* activity.
|
|
26
|
+
*
|
|
27
|
+
* `model` is the name of a factory registered in
|
|
28
|
+
* `StrandsPlugin({ models: {...} })`. The `activityOptions` apply to every
|
|
29
|
+
* model invocation this agent makes. All other options are forwarded to
|
|
30
|
+
* Strands' {@link Agent} constructor (`tools`, `plugins`, `systemPrompt`,
|
|
31
|
+
* `structuredOutputSchema`, `messages`, etc.).
|
|
32
|
+
*
|
|
33
|
+
* Strands' `retryStrategy` is disabled; configure retries via
|
|
34
|
+
* `activityOptions.retry` here and on the activity options accepted by
|
|
35
|
+
* {@link activityAsTool}, {@link activityAsHook}, and {@link TemporalMCPClient}.
|
|
36
|
+
*/
|
|
37
|
+
export declare class TemporalAgent extends Agent {
|
|
38
|
+
constructor(options?: TemporalAgentOptions);
|
|
39
|
+
takeSnapshot(): never;
|
|
40
|
+
loadSnapshot(): never;
|
|
41
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TemporalAgent = void 0;
|
|
4
|
+
const sdk_1 = require("@strands-agents/sdk");
|
|
5
|
+
const temporal_model_1 = require("./temporal-model");
|
|
6
|
+
const temporal_mcp_client_1 = require("./temporal-mcp-client");
|
|
7
|
+
const SNAPSHOT_DISABLED = 'TemporalAgent disables takeSnapshot()/loadSnapshot(). Temporal workflows ' +
|
|
8
|
+
'already persist agent state durably via the event history at a finer ' +
|
|
9
|
+
'granularity than Strands snapshots. Remove the snapshot call and rely on ' +
|
|
10
|
+
"Temporal's durable execution instead.";
|
|
11
|
+
const RETRY_STRATEGY_DISABLED = 'TemporalAgent disables Strands retries; configure retries via retryPolicy ' +
|
|
12
|
+
'in the model activity options on TemporalAgent, and on the activity options ' +
|
|
13
|
+
'passed to workflow.activityAsTool, workflow.activityAsHook, or TemporalMCPClient. ' +
|
|
14
|
+
'Remove retryStrategy from TemporalAgent(...) or pass retryStrategy: null.';
|
|
15
|
+
/**
|
|
16
|
+
* A Strands {@link Agent} that routes every model call through a Temporal
|
|
17
|
+
* activity.
|
|
18
|
+
*
|
|
19
|
+
* `model` is the name of a factory registered in
|
|
20
|
+
* `StrandsPlugin({ models: {...} })`. The `activityOptions` apply to every
|
|
21
|
+
* model invocation this agent makes. All other options are forwarded to
|
|
22
|
+
* Strands' {@link Agent} constructor (`tools`, `plugins`, `systemPrompt`,
|
|
23
|
+
* `structuredOutputSchema`, `messages`, etc.).
|
|
24
|
+
*
|
|
25
|
+
* Strands' `retryStrategy` is disabled; configure retries via
|
|
26
|
+
* `activityOptions.retry` here and on the activity options accepted by
|
|
27
|
+
* {@link activityAsTool}, {@link activityAsHook}, and {@link TemporalMCPClient}.
|
|
28
|
+
*/
|
|
29
|
+
class TemporalAgent extends sdk_1.Agent {
|
|
30
|
+
constructor(options = {}) {
|
|
31
|
+
const { model, activityOptions, streamingTopic, streamingBatchInterval, ...rest } = options;
|
|
32
|
+
if (rest.retryStrategy !== undefined) {
|
|
33
|
+
throw new Error(RETRY_STRATEGY_DISABLED);
|
|
34
|
+
}
|
|
35
|
+
const temporalModel = new temporal_model_1.TemporalModel({
|
|
36
|
+
modelName: model,
|
|
37
|
+
activityOptions,
|
|
38
|
+
streamingTopic,
|
|
39
|
+
streamingBatchInterval,
|
|
40
|
+
});
|
|
41
|
+
super({
|
|
42
|
+
...rest,
|
|
43
|
+
model: temporalModel,
|
|
44
|
+
retryStrategy: null,
|
|
45
|
+
});
|
|
46
|
+
// Strands lists each MCP client's tools once at agent initialization and
|
|
47
|
+
// reuses that list for every turn. For clients with `cacheTools: false`,
|
|
48
|
+
// re-list on each turn so a mid-workflow MCP-server restart is picked up.
|
|
49
|
+
// `AfterToolsEvent` fires after a tool round completes, before the next
|
|
50
|
+
// model call reads the tool registry — so the refresh lands on that call.
|
|
51
|
+
for (const client of collectMcpClients(rest.tools)) {
|
|
52
|
+
if (!client.cacheTools) {
|
|
53
|
+
this.addHook(sdk_1.AfterToolsEvent, (event) => client.refreshTools(event.agent.toolRegistry));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
takeSnapshot() {
|
|
58
|
+
throw new Error(SNAPSHOT_DISABLED);
|
|
59
|
+
}
|
|
60
|
+
loadSnapshot() {
|
|
61
|
+
throw new Error(SNAPSHOT_DISABLED);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.TemporalAgent = TemporalAgent;
|
|
65
|
+
/** Collect every {@link TemporalMCPClient} in a (possibly nested) tool list. */
|
|
66
|
+
function collectMcpClients(tools) {
|
|
67
|
+
const found = [];
|
|
68
|
+
const walk = (items) => {
|
|
69
|
+
for (const item of items) {
|
|
70
|
+
if (Array.isArray(item))
|
|
71
|
+
walk(item);
|
|
72
|
+
else if (item instanceof temporal_mcp_client_1.TemporalMCPClient)
|
|
73
|
+
found.push(item);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
if (tools)
|
|
77
|
+
walk(tools);
|
|
78
|
+
return found;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=temporal-agent.js.map
|