@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 @@
|
|
|
1
|
+
{"version":3,"file":"temporal-agent.js","sourceRoot":"","sources":["../src/temporal-agent.ts"],"names":[],"mappings":";;;AAAA,6CAA6D;AAI7D,qDAAiD;AACjD,+DAA0D;AAE1D,MAAM,iBAAiB,GACrB,2EAA2E;IAC3E,uEAAuE;IACvE,2EAA2E;IAC3E,uCAAuC,CAAC;AAE1C,MAAM,uBAAuB,GAC3B,4EAA4E;IAC5E,8EAA8E;IAC9E,oFAAoF;IACpF,2EAA2E,CAAC;AAqB9E;;;;;;;;;;;;;GAaG;AACH,MAAa,aAAc,SAAQ,WAAK;IACtC,YAAY,UAAgC,EAAE;QAC5C,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,sBAAsB,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;QAC5F,IAAK,IAAoC,CAAC,aAAa,KAAK,SAAS,EAAE,CAAC;YACtE,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM,aAAa,GAAG,IAAI,8BAAa,CAAC;YACtC,SAAS,EAAE,KAAK;YAChB,eAAe;YACf,cAAc;YACd,sBAAsB;SACvB,CAAC,CAAC;QACH,KAAK,CAAC;YACJ,GAAG,IAAI;YACP,KAAK,EAAE,aAAa;YACpB,aAAa,EAAE,IAAI;SACpB,CAAC,CAAC;QAEH,yEAAyE;QACzE,yEAAyE;QACzE,0EAA0E;QAC1E,wEAAwE;QACxE,0EAA0E;QAC1E,KAAK,MAAM,MAAM,IAAI,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACnD,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;gBACvB,IAAI,CAAC,OAAO,CAAC,qBAAe,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;YAC1F,CAAC;QACH,CAAC;IACH,CAAC;IAEQ,YAAY;QACnB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IAEQ,YAAY;QACnB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;CACF;AArCD,sCAqCC;AAED,gFAAgF;AAChF,SAAS,iBAAiB,CAAC,KAA2B;IACpD,MAAM,KAAK,GAAwB,EAAE,CAAC;IACtC,MAAM,IAAI,GAAG,CAAC,KAAwC,EAAQ,EAAE;QAC9D,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;gBAAE,IAAI,CAAC,IAAI,CAAC,CAAC;iBAC/B,IAAI,IAAI,YAAY,uCAAiB;gBAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC,CAAC;IACF,IAAI,KAAK;QAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IACvB,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { McpClient } from '@strands-agents/sdk';
|
|
2
|
+
import type { JSONSchema, JSONValue } from '@strands-agents/sdk';
|
|
3
|
+
import type { ActivityOptions } from '@temporalio/workflow';
|
|
4
|
+
/** Tool descriptor returned by the per-server `{server}-listTools` activity. */
|
|
5
|
+
export interface McpToolInfo {
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
inputSchema?: JSONSchema;
|
|
9
|
+
}
|
|
10
|
+
/** Activity input for the per-server `{server}-callTool` activity. */
|
|
11
|
+
export interface CallToolInput {
|
|
12
|
+
toolName: string;
|
|
13
|
+
args: JSONValue;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Options for {@link TemporalMCPClient}.
|
|
17
|
+
*
|
|
18
|
+
* The {@link ActivityOptions} apply to every per-tool activity invocation
|
|
19
|
+
* the agent makes for this MCP server.
|
|
20
|
+
*
|
|
21
|
+
* `cacheTools` controls how often the agent re-lists this server's tools.
|
|
22
|
+
* Strands lists an MCP client's tools once when the agent initializes and then
|
|
23
|
+
* reuses that list for every turn, so the list is otherwise frozen for the
|
|
24
|
+
* whole workflow.
|
|
25
|
+
*
|
|
26
|
+
* - `false` (default) — re-list the tools on each agent turn, so the agent
|
|
27
|
+
* picks up an MCP server that's restarted or redeployed mid-workflow. Costs
|
|
28
|
+
* one extra `{server}-listTools` activity per tool round.
|
|
29
|
+
* - `true` — list once at the beginning of the workflow and reuse for all
|
|
30
|
+
* turns. Cheaper, but the tool list is fixed for the workflow's lifetime.
|
|
31
|
+
*/
|
|
32
|
+
export interface TemporalMCPClientOptions {
|
|
33
|
+
server: string;
|
|
34
|
+
activityOptions?: ActivityOptions;
|
|
35
|
+
cacheTools?: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Minimal view of Strands' `ToolRegistry` used by {@link
|
|
39
|
+
* TemporalMCPClient.refreshTools}. Typed structurally because `ToolRegistry`
|
|
40
|
+
* isn't re-exported from `@strands-agents/sdk`'s public index.
|
|
41
|
+
*/
|
|
42
|
+
export interface ToolRegistryView {
|
|
43
|
+
list(): {
|
|
44
|
+
name: string;
|
|
45
|
+
}[];
|
|
46
|
+
remove(name: string): void;
|
|
47
|
+
addOrReplace(tools: never[]): void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Workflow-side handle to an MCP server registered on the worker.
|
|
51
|
+
*
|
|
52
|
+
* The transport factory and tool discovery live worker-side via
|
|
53
|
+
* `StrandsPlugin({ mcpClients: { server: () => new McpClient(...) } })`.
|
|
54
|
+
* This handle only carries the server name (which selects the registered
|
|
55
|
+
* factory) and the per-call activity options.
|
|
56
|
+
*
|
|
57
|
+
* Extends {@link McpClient} so it can be passed directly in an
|
|
58
|
+
* {@link AgentConfig.tools | Agent.tools} array (Strands' `flattenTools`
|
|
59
|
+
* dispatches on `instanceof McpClient`). `connect()` and `disconnect()`
|
|
60
|
+
* are no-ops; `listTools()` returns lightweight {@link TemporalMCPTool}
|
|
61
|
+
* wrappers whose `stream()` dispatches to the per-server `{server}-callTool`
|
|
62
|
+
* activity registered by {@link StrandsPlugin}.
|
|
63
|
+
*/
|
|
64
|
+
export declare class TemporalMCPClient extends McpClient {
|
|
65
|
+
private readonly server;
|
|
66
|
+
private readonly activityOptions;
|
|
67
|
+
/** Whether the agent lists this server's tools once or re-lists each turn. */
|
|
68
|
+
readonly cacheTools: boolean;
|
|
69
|
+
constructor(options: TemporalMCPClientOptions);
|
|
70
|
+
connect(): Promise<void>;
|
|
71
|
+
disconnect(): Promise<void>;
|
|
72
|
+
listTools(): Promise<never[]>;
|
|
73
|
+
/**
|
|
74
|
+
* Re-list this server's tools and reconcile them into the agent's tool
|
|
75
|
+
* registry, dropping tools that no longer exist and adding new ones.
|
|
76
|
+
* {@link TemporalAgent} calls this on each agent turn when `cacheTools` is
|
|
77
|
+
* `false` so a mid-workflow MCP-server restart is reflected in the next call.
|
|
78
|
+
*/
|
|
79
|
+
refreshTools(registry: ToolRegistryView): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
export declare function listToolsActivityName(server: string): string;
|
|
82
|
+
export declare function callToolActivityName(server: string): string;
|
|
83
|
+
/**
|
|
84
|
+
* Builds the per-server `{server}-listTools` activity. Enumerates the server's
|
|
85
|
+
* tools live over the shared worker-process connection on every call, so the
|
|
86
|
+
* agent sees the server's current tools even if it is redeployed mid-workflow.
|
|
87
|
+
* Runs whenever a workflow calls {@link TemporalMCPClient.listTools}.
|
|
88
|
+
*/
|
|
89
|
+
export declare function buildListToolsActivity(server: string, factory: () => McpClient, idleMs?: number): (input: {
|
|
90
|
+
server: string;
|
|
91
|
+
}) => Promise<McpToolInfo[]>;
|
|
92
|
+
/**
|
|
93
|
+
* Default for how long an idle MCP connection is kept open before it's
|
|
94
|
+
* disconnected. The timer resets on every {@link callToolActivityName |
|
|
95
|
+
* callTool} that reuses the connection. Override per worker via
|
|
96
|
+
* `StrandsPlugin({ mcpConnectionIdleMs })`.
|
|
97
|
+
*/
|
|
98
|
+
export declare const MCP_CONNECTION_IDLE_MS: number;
|
|
99
|
+
export declare function _evictConnection(server: string): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Builds the per-server `{server}-callTool` activity. Reuses a worker-process
|
|
102
|
+
* MCP session opened lazily through the user-supplied factory — successive
|
|
103
|
+
* calls share one connection rather than reconnecting per call. Idle
|
|
104
|
+
* connections are disconnected after `idleMs` (defaults to
|
|
105
|
+
* {@link MCP_CONNECTION_IDLE_MS}).
|
|
106
|
+
*/
|
|
107
|
+
export declare function buildCallToolActivity(server: string, factory: () => McpClient, idleMs?: number): (input: CallToolInput) => Promise<JSONValue>;
|
|
@@ -0,0 +1,237 @@
|
|
|
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.MCP_CONNECTION_IDLE_MS = exports.TemporalMCPClient = void 0;
|
|
27
|
+
exports.listToolsActivityName = listToolsActivityName;
|
|
28
|
+
exports.callToolActivityName = callToolActivityName;
|
|
29
|
+
exports.buildListToolsActivity = buildListToolsActivity;
|
|
30
|
+
exports._evictConnection = _evictConnection;
|
|
31
|
+
exports.buildCallToolActivity = buildCallToolActivity;
|
|
32
|
+
const sdk_1 = require("@strands-agents/sdk");
|
|
33
|
+
const workflow = __importStar(require("@temporalio/workflow"));
|
|
34
|
+
const activity_1 = require("@temporalio/activity");
|
|
35
|
+
const STUB_TRANSPORT = {
|
|
36
|
+
async start() {
|
|
37
|
+
// No-op; TemporalMCPClient never connects from inside a workflow.
|
|
38
|
+
},
|
|
39
|
+
async send() {
|
|
40
|
+
// No-op.
|
|
41
|
+
},
|
|
42
|
+
async close() {
|
|
43
|
+
// No-op.
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Workflow-side handle to an MCP server registered on the worker.
|
|
48
|
+
*
|
|
49
|
+
* The transport factory and tool discovery live worker-side via
|
|
50
|
+
* `StrandsPlugin({ mcpClients: { server: () => new McpClient(...) } })`.
|
|
51
|
+
* This handle only carries the server name (which selects the registered
|
|
52
|
+
* factory) and the per-call activity options.
|
|
53
|
+
*
|
|
54
|
+
* Extends {@link McpClient} so it can be passed directly in an
|
|
55
|
+
* {@link AgentConfig.tools | Agent.tools} array (Strands' `flattenTools`
|
|
56
|
+
* dispatches on `instanceof McpClient`). `connect()` and `disconnect()`
|
|
57
|
+
* are no-ops; `listTools()` returns lightweight {@link TemporalMCPTool}
|
|
58
|
+
* wrappers whose `stream()` dispatches to the per-server `{server}-callTool`
|
|
59
|
+
* activity registered by {@link StrandsPlugin}.
|
|
60
|
+
*/
|
|
61
|
+
class TemporalMCPClient extends sdk_1.McpClient {
|
|
62
|
+
server;
|
|
63
|
+
activityOptions;
|
|
64
|
+
/** Whether the agent lists this server's tools once or re-lists each turn. */
|
|
65
|
+
cacheTools;
|
|
66
|
+
constructor(options) {
|
|
67
|
+
super({ transport: STUB_TRANSPORT });
|
|
68
|
+
this.server = options.server;
|
|
69
|
+
this.activityOptions = options.activityOptions ?? {};
|
|
70
|
+
this.cacheTools = options.cacheTools ?? false;
|
|
71
|
+
}
|
|
72
|
+
async connect() {
|
|
73
|
+
// No-op; all I/O happens inside the per-server callTool activity.
|
|
74
|
+
}
|
|
75
|
+
async disconnect() {
|
|
76
|
+
// No-op.
|
|
77
|
+
}
|
|
78
|
+
// The base `McpClient.listTools` returns `Promise<McpTool[]>`, but `McpTool`
|
|
79
|
+
// isn't re-exported from `@strands-agents/sdk`'s public index. The cast to
|
|
80
|
+
// `never[]` keeps the override compatible; the agent only uses
|
|
81
|
+
// `name`/`toolSpec`/`stream` on each tool, all of which
|
|
82
|
+
// {@link TemporalMCPTool} provides.
|
|
83
|
+
async listTools() {
|
|
84
|
+
// Imported lazily to avoid a circular module cycle with TemporalMCPTool.
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
86
|
+
const { TemporalMCPTool } = require('./temporal-mcp-tool');
|
|
87
|
+
const activities = workflow.proxyActivities({
|
|
88
|
+
startToCloseTimeout: '10 minutes',
|
|
89
|
+
...this.activityOptions,
|
|
90
|
+
});
|
|
91
|
+
const infos = await activities[listToolsActivityName(this.server)]({ server: this.server });
|
|
92
|
+
return infos.map((info) => new TemporalMCPTool(this.server, info, this.activityOptions));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Re-list this server's tools and reconcile them into the agent's tool
|
|
96
|
+
* registry, dropping tools that no longer exist and adding new ones.
|
|
97
|
+
* {@link TemporalAgent} calls this on each agent turn when `cacheTools` is
|
|
98
|
+
* `false` so a mid-workflow MCP-server restart is reflected in the next call.
|
|
99
|
+
*/
|
|
100
|
+
async refreshTools(registry) {
|
|
101
|
+
// Imported lazily to avoid a circular module cycle with TemporalMCPTool.
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
103
|
+
const { TemporalMCPTool } = require('./temporal-mcp-tool');
|
|
104
|
+
const fresh = await this.listTools();
|
|
105
|
+
const freshNames = new Set(fresh.map((t) => t.name));
|
|
106
|
+
for (const tool of registry.list()) {
|
|
107
|
+
if (tool instanceof TemporalMCPTool && tool.server === this.server && !freshNames.has(tool.name)) {
|
|
108
|
+
registry.remove(tool.name);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
registry.addOrReplace(fresh);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.TemporalMCPClient = TemporalMCPClient;
|
|
115
|
+
function listToolsActivityName(server) {
|
|
116
|
+
return `${server}-listTools`;
|
|
117
|
+
}
|
|
118
|
+
function callToolActivityName(server) {
|
|
119
|
+
return `${server}-callTool`;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Builds the per-server `{server}-listTools` activity. Enumerates the server's
|
|
123
|
+
* tools live over the shared worker-process connection on every call, so the
|
|
124
|
+
* agent sees the server's current tools even if it is redeployed mid-workflow.
|
|
125
|
+
* Runs whenever a workflow calls {@link TemporalMCPClient.listTools}.
|
|
126
|
+
*/
|
|
127
|
+
function buildListToolsActivity(server, factory, idleMs = exports.MCP_CONNECTION_IDLE_MS) {
|
|
128
|
+
const name = listToolsActivityName(server);
|
|
129
|
+
const fn = async (_input) => {
|
|
130
|
+
const client = await getConnection(server, factory, idleMs);
|
|
131
|
+
try {
|
|
132
|
+
const tools = await client.listTools();
|
|
133
|
+
return tools.map((t) => ({
|
|
134
|
+
name: t.name,
|
|
135
|
+
description: t.description,
|
|
136
|
+
inputSchema: t.toolSpec.inputSchema,
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
// The session may be broken (e.g. the server was redeployed); drop it so
|
|
141
|
+
// the next call reconnects to the current deployment.
|
|
142
|
+
await _evictConnection(server);
|
|
143
|
+
throw err;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
Object.defineProperty(fn, 'name', { value: name });
|
|
147
|
+
return fn;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Default for how long an idle MCP connection is kept open before it's
|
|
151
|
+
* disconnected. The timer resets on every {@link callToolActivityName |
|
|
152
|
+
* callTool} that reuses the connection. Override per worker via
|
|
153
|
+
* `StrandsPlugin({ mcpConnectionIdleMs })`.
|
|
154
|
+
*/
|
|
155
|
+
exports.MCP_CONNECTION_IDLE_MS = 5 * 60 * 1000;
|
|
156
|
+
// Worker-process caches, keyed by server name. Activities run in the worker's
|
|
157
|
+
// Node process, so this module state is shared across every activity invocation on the worker.
|
|
158
|
+
const CONNECTIONS = new Map();
|
|
159
|
+
const IDLE_TIMERS = new Map();
|
|
160
|
+
function resetIdleTimer(server, idleMs) {
|
|
161
|
+
const existing = IDLE_TIMERS.get(server);
|
|
162
|
+
if (existing !== undefined)
|
|
163
|
+
clearTimeout(existing);
|
|
164
|
+
const timer = setTimeout(() => void _evictConnection(server), idleMs);
|
|
165
|
+
// Don't let an idle MCP connection keep the worker process alive.
|
|
166
|
+
timer.unref?.();
|
|
167
|
+
IDLE_TIMERS.set(server, timer);
|
|
168
|
+
}
|
|
169
|
+
async function _evictConnection(server) {
|
|
170
|
+
const timer = IDLE_TIMERS.get(server);
|
|
171
|
+
if (timer !== undefined) {
|
|
172
|
+
clearTimeout(timer);
|
|
173
|
+
IDLE_TIMERS.delete(server);
|
|
174
|
+
}
|
|
175
|
+
const entry = CONNECTIONS.get(server);
|
|
176
|
+
if (entry === undefined)
|
|
177
|
+
return;
|
|
178
|
+
CONNECTIONS.delete(server);
|
|
179
|
+
try {
|
|
180
|
+
const client = await entry;
|
|
181
|
+
await client.disconnect();
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// Best-effort; the session may already be broken.
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function getConnection(server, factory, idleMs) {
|
|
188
|
+
let entry = CONNECTIONS.get(server);
|
|
189
|
+
if (entry === undefined) {
|
|
190
|
+
entry = (async () => {
|
|
191
|
+
const client = factory();
|
|
192
|
+
await client.connect();
|
|
193
|
+
return client;
|
|
194
|
+
})();
|
|
195
|
+
CONNECTIONS.set(server, entry);
|
|
196
|
+
// If the connect handshake fails, drop the rejected promise so the next
|
|
197
|
+
// call retries instead of caching the failure forever.
|
|
198
|
+
entry.catch(() => {
|
|
199
|
+
if (CONNECTIONS.get(server) === entry)
|
|
200
|
+
CONNECTIONS.delete(server);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
resetIdleTimer(server, idleMs);
|
|
204
|
+
return entry;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Builds the per-server `{server}-callTool` activity. Reuses a worker-process
|
|
208
|
+
* MCP session opened lazily through the user-supplied factory — successive
|
|
209
|
+
* calls share one connection rather than reconnecting per call. Idle
|
|
210
|
+
* connections are disconnected after `idleMs` (defaults to
|
|
211
|
+
* {@link MCP_CONNECTION_IDLE_MS}).
|
|
212
|
+
*/
|
|
213
|
+
function buildCallToolActivity(server, factory, idleMs = exports.MCP_CONNECTION_IDLE_MS) {
|
|
214
|
+
const name = callToolActivityName(server);
|
|
215
|
+
const fn = async (input) => {
|
|
216
|
+
activity_1.Context.current().log.debug(`Calling MCP tool ${input.toolName} on server ${server}`, {
|
|
217
|
+
activityId: (0, activity_1.activityInfo)().activityId,
|
|
218
|
+
});
|
|
219
|
+
const client = await getConnection(server, factory, idleMs);
|
|
220
|
+
// Dispatch by name without re-listing tools on the connection: the agent
|
|
221
|
+
// already has the schema from `listTools`, and `McpClient.callTool` only
|
|
222
|
+
// reads `tool.name` to build the request. A minimal `{ name }` matches the
|
|
223
|
+
// by-name dispatch the Python SDK does via `session.call_tool`.
|
|
224
|
+
const tool = { name: input.toolName };
|
|
225
|
+
try {
|
|
226
|
+
return await client.callTool(tool, input.args);
|
|
227
|
+
}
|
|
228
|
+
catch (err) {
|
|
229
|
+
// The session may be broken; drop it so the next call reconnects.
|
|
230
|
+
await _evictConnection(server);
|
|
231
|
+
throw err;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
Object.defineProperty(fn, 'name', { value: name });
|
|
235
|
+
return fn;
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=temporal-mcp-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temporal-mcp-client.js","sourceRoot":"","sources":["../src/temporal-mcp-client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA8IA,sDAEC;AAED,oDAEC;AAQD,wDAwBC;AAwBD,4CAeC;AA4BD,sDA0BC;AAjRD,6CAAgD;AAGhD,+DAAiD;AAEjD,mDAAgF;AAehF,MAAM,cAAc,GAAc;IAChC,KAAK,CAAC,KAAK;QACT,kEAAkE;IACpE,CAAC;IACD,KAAK,CAAC,IAAI;QACR,SAAS;IACX,CAAC;IACD,KAAK,CAAC,KAAK;QACT,SAAS;IACX,CAAC;CACF,CAAC;AAoCF;;;;;;;;;;;;;;GAcG;AACH,MAAa,iBAAkB,SAAQ,eAAS;IAC7B,MAAM,CAAS;IACf,eAAe,CAAkB;IAClD,8EAA8E;IACrE,UAAU,CAAU;IAE7B,YAAY,OAAiC;QAC3C,KAAK,CAAC,EAAE,SA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AAQ,cAAc,MAAM,EAAE,EAAE;YAC5F,UAAU,EAAE,IAAA,uBAAY,GAAE,CAAC,UAAU;SACtC,CAAC,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QAC5D,yEAAyE;QACzE,yEAAyE;QACzE,2EAA2E;QAC3E,gEAAgE;QAChE,MAAM,IAAI,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAqD,CAAC;QACzF,IAAI,CAAC;YACH,OAAO,MAAM,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;QACjD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,kEAAkE;YAClE,MAAM,gBAAgB,CAAC,MAAM,CAAC,CAAC;YAC/B,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC,CAAC;IACF,MAAM,CAAC,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACnD,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Tool } from '@strands-agents/sdk';
|
|
2
|
+
import type { ToolContext, ToolSpec, ToolStreamGenerator } from '@strands-agents/sdk';
|
|
3
|
+
import type { ActivityOptions } from '@temporalio/workflow';
|
|
4
|
+
import type { McpToolInfo } from './temporal-mcp-client';
|
|
5
|
+
/**
|
|
6
|
+
* Workflow-side stub for a single MCP tool. Each invocation dispatches the
|
|
7
|
+
* per-server `{server}-callTool` activity built by {@link StrandsPlugin}, then
|
|
8
|
+
* maps the raw MCP result (`{ content: [...], isError }`) into Strands
|
|
9
|
+
* content blocks.
|
|
10
|
+
*
|
|
11
|
+
* Text items map to {@link TextBlock}; everything else falls back to
|
|
12
|
+
* {@link JsonBlock}. Richer mappings (images, embedded resources) would
|
|
13
|
+
* require `McpTool`, which is bundled in `@strands-agents/sdk` but not
|
|
14
|
+
* re-exported from its public index.
|
|
15
|
+
*/
|
|
16
|
+
export declare class TemporalMCPTool extends Tool {
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly description: string;
|
|
19
|
+
readonly toolSpec: ToolSpec;
|
|
20
|
+
readonly server: string;
|
|
21
|
+
private readonly activityOptions;
|
|
22
|
+
constructor(server: string, info: McpToolInfo, activityOptions: ActivityOptions);
|
|
23
|
+
stream(toolContext: ToolContext): ToolStreamGenerator;
|
|
24
|
+
}
|
|
@@ -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.TemporalMCPTool = void 0;
|
|
27
|
+
const sdk_1 = require("@strands-agents/sdk");
|
|
28
|
+
const workflow = __importStar(require("@temporalio/workflow"));
|
|
29
|
+
const temporal_mcp_client_1 = require("./temporal-mcp-client");
|
|
30
|
+
const json_schema_1 = require("./json-schema");
|
|
31
|
+
/**
|
|
32
|
+
* Workflow-side stub for a single MCP tool. Each invocation dispatches the
|
|
33
|
+
* per-server `{server}-callTool` activity built by {@link StrandsPlugin}, then
|
|
34
|
+
* maps the raw MCP result (`{ content: [...], isError }`) into Strands
|
|
35
|
+
* content blocks.
|
|
36
|
+
*
|
|
37
|
+
* Text items map to {@link TextBlock}; everything else falls back to
|
|
38
|
+
* {@link JsonBlock}. Richer mappings (images, embedded resources) would
|
|
39
|
+
* require `McpTool`, which is bundled in `@strands-agents/sdk` but not
|
|
40
|
+
* re-exported from its public index.
|
|
41
|
+
*/
|
|
42
|
+
class TemporalMCPTool extends sdk_1.Tool {
|
|
43
|
+
name;
|
|
44
|
+
description;
|
|
45
|
+
toolSpec;
|
|
46
|
+
server;
|
|
47
|
+
activityOptions;
|
|
48
|
+
constructor(server, info, activityOptions) {
|
|
49
|
+
super();
|
|
50
|
+
this.server = server;
|
|
51
|
+
this.name = info.name;
|
|
52
|
+
this.description = info.description || `Tool which performs ${info.name}`;
|
|
53
|
+
this.toolSpec = {
|
|
54
|
+
name: info.name,
|
|
55
|
+
description: this.description,
|
|
56
|
+
inputSchema: (0, json_schema_1.toJsonSchema)(info.inputSchema),
|
|
57
|
+
};
|
|
58
|
+
this.activityOptions = 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
|
+
summary: this.name,
|
|
65
|
+
startToCloseTimeout: '10 minutes',
|
|
66
|
+
...this.activityOptions,
|
|
67
|
+
});
|
|
68
|
+
try {
|
|
69
|
+
const raw = await activities[(0, temporal_mcp_client_1.callToolActivityName)(this.server)]({
|
|
70
|
+
toolName: this.name,
|
|
71
|
+
args: input,
|
|
72
|
+
});
|
|
73
|
+
const result = raw;
|
|
74
|
+
if (result === null || typeof result !== 'object' || !Array.isArray(result.content)) {
|
|
75
|
+
return new sdk_1.ToolResultBlock({
|
|
76
|
+
toolUseId,
|
|
77
|
+
status: 'error',
|
|
78
|
+
content: [new sdk_1.TextBlock('Invalid MCP tool result: missing content array')],
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const content = result.content.length
|
|
82
|
+
? result.content.map(mapMcpContent)
|
|
83
|
+
: [new sdk_1.TextBlock('Tool execution completed successfully with no output.')];
|
|
84
|
+
return new sdk_1.ToolResultBlock({
|
|
85
|
+
toolUseId,
|
|
86
|
+
status: result.isError ? 'error' : 'success',
|
|
87
|
+
content,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
return new sdk_1.ToolResultBlock({
|
|
92
|
+
toolUseId,
|
|
93
|
+
status: 'error',
|
|
94
|
+
content: [new sdk_1.TextBlock(String(err instanceof Error ? err.message : err))],
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.TemporalMCPTool = TemporalMCPTool;
|
|
100
|
+
function mapMcpContent(item) {
|
|
101
|
+
if (typeof item === 'object' && item !== null) {
|
|
102
|
+
const obj = item;
|
|
103
|
+
if (obj.type === 'text' && typeof obj.text === 'string') {
|
|
104
|
+
return new sdk_1.TextBlock(obj.text);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return new sdk_1.JsonBlock({ json: item });
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=temporal-mcp-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temporal-mcp-tool.js","sourceRoot":"","sources":["../src/temporal-mcp-tool.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;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,CAAC,IAAI,CAAC,CAAC;QACjC,CAAC;IACH,CAAC;IACD,OAAO,IAAI,eAAS,CAAC,EAAE,IAAI,EAAE,IAAiB,EAAE,CAAC,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Model } from '@strands-agents/sdk';
|
|
2
|
+
import type { BaseModelConfig, Message, ModelStreamEvent, StreamOptions } from '@strands-agents/sdk';
|
|
3
|
+
import type { ActivityOptions } from '@temporalio/workflow';
|
|
4
|
+
import type { Duration } from '@temporalio/common/lib/time';
|
|
5
|
+
import type { ModelActivity } from './model-activity';
|
|
6
|
+
/**
|
|
7
|
+
* Options for {@link TemporalModel}.
|
|
8
|
+
*
|
|
9
|
+
* The {@link ActivityOptions} are applied to every activity invocation
|
|
10
|
+
* `stream()` schedules. `streamingTopic` and `streamingBatchInterval`
|
|
11
|
+
* forward {@link ModelStreamEvent}s on a {@link WorkflowStream} as
|
|
12
|
+
* they are produced inside the model activity.
|
|
13
|
+
*/
|
|
14
|
+
export interface TemporalModelOptions {
|
|
15
|
+
modelName?: string;
|
|
16
|
+
activityOptions?: ActivityOptions;
|
|
17
|
+
streamingTopic?: string;
|
|
18
|
+
streamingBatchInterval?: Duration;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A Strands {@link Model} whose {@link Model.stream} runs as a Temporal
|
|
22
|
+
* activity. Constructing a `TemporalModel` does no I/O, so instances are
|
|
23
|
+
* safe to create at module load time.
|
|
24
|
+
*
|
|
25
|
+
* The `modelName` selects which factory the plugin will invoke worker-side;
|
|
26
|
+
* it must match a key in {@link StrandsPlugin}'s `models` map. A name not
|
|
27
|
+
* present in `models` raises an error inside the model activity at first call.
|
|
28
|
+
*/
|
|
29
|
+
export declare class TemporalModel extends Model<BaseModelConfig> {
|
|
30
|
+
private readonly modelName?;
|
|
31
|
+
private readonly activityOptions;
|
|
32
|
+
private readonly streamingTopic?;
|
|
33
|
+
private readonly streamingBatchInterval;
|
|
34
|
+
constructor(options?: TemporalModelOptions);
|
|
35
|
+
updateConfig(_config: BaseModelConfig): void;
|
|
36
|
+
getConfig(): BaseModelConfig;
|
|
37
|
+
stream(messages: Message[], options?: StreamOptions): AsyncIterable<ModelStreamEvent>;
|
|
38
|
+
}
|
|
39
|
+
/** Type tag used only to forward the `ModelActivity` type from the activity side. */
|
|
40
|
+
export type _ModelActivityRef = ModelActivity;
|
|
41
|
+
export declare function rejectStructuredOutput(): never;
|
|
@@ -0,0 +1,88 @@
|
|
|
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.TemporalModel = void 0;
|
|
27
|
+
exports.rejectStructuredOutput = rejectStructuredOutput;
|
|
28
|
+
const sdk_1 = require("@strands-agents/sdk");
|
|
29
|
+
const workflow = __importStar(require("@temporalio/workflow"));
|
|
30
|
+
const common_1 = require("@temporalio/common");
|
|
31
|
+
const STRUCTURED_OUTPUT_DISABLED = 'TemporalModel.structuredOutput is not supported. Use ' +
|
|
32
|
+
'TemporalAgent({ structuredOutputSchema: ... }) which routes structured ' +
|
|
33
|
+
'output through stream() via the structured-output tool.';
|
|
34
|
+
/**
|
|
35
|
+
* A Strands {@link Model} whose {@link Model.stream} runs as a Temporal
|
|
36
|
+
* activity. Constructing a `TemporalModel` does no I/O, so instances are
|
|
37
|
+
* safe to create at module load time.
|
|
38
|
+
*
|
|
39
|
+
* The `modelName` selects which factory the plugin will invoke worker-side;
|
|
40
|
+
* it must match a key in {@link StrandsPlugin}'s `models` map. A name not
|
|
41
|
+
* present in `models` raises an error inside the model activity at first call.
|
|
42
|
+
*/
|
|
43
|
+
class TemporalModel extends sdk_1.Model {
|
|
44
|
+
modelName;
|
|
45
|
+
activityOptions;
|
|
46
|
+
streamingTopic;
|
|
47
|
+
streamingBatchInterval;
|
|
48
|
+
constructor(options = {}) {
|
|
49
|
+
super();
|
|
50
|
+
this.modelName = options.modelName;
|
|
51
|
+
this.activityOptions = options.activityOptions ?? {};
|
|
52
|
+
this.streamingTopic = options.streamingTopic;
|
|
53
|
+
this.streamingBatchInterval = options.streamingBatchInterval ?? '100 milliseconds';
|
|
54
|
+
}
|
|
55
|
+
updateConfig(_config) {
|
|
56
|
+
// No-op; the real model is configured worker-side via the plugin's factories.
|
|
57
|
+
}
|
|
58
|
+
getConfig() {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
async *stream(messages, options) {
|
|
62
|
+
const proxiedActivities = workflow.proxyActivities({
|
|
63
|
+
startToCloseTimeout: '10 minutes',
|
|
64
|
+
...this.activityOptions,
|
|
65
|
+
});
|
|
66
|
+
const events = this.streamingTopic
|
|
67
|
+
? await proxiedActivities.invokeModelStreaming({
|
|
68
|
+
modelName: this.modelName,
|
|
69
|
+
messages,
|
|
70
|
+
options,
|
|
71
|
+
streamingTopic: this.streamingTopic,
|
|
72
|
+
streamingBatchInterval: this.streamingBatchInterval,
|
|
73
|
+
})
|
|
74
|
+
: await proxiedActivities.invokeModel({
|
|
75
|
+
modelName: this.modelName,
|
|
76
|
+
messages,
|
|
77
|
+
options,
|
|
78
|
+
});
|
|
79
|
+
for (const event of events) {
|
|
80
|
+
yield event;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.TemporalModel = TemporalModel;
|
|
85
|
+
function rejectStructuredOutput() {
|
|
86
|
+
throw common_1.ApplicationFailure.nonRetryable(STRUCTURED_OUTPUT_DISABLED);
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=temporal-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temporal-model.js","sourceRoot":"","sources":["../src/temporal-model.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA2FA,wDAEC;AA7FD,6CAA4C;AAE5C,+DAAiD;AAEjD,+CAAwD;AAIxD,MAAM,0BAA0B,GAC9B,uDAAuD;IACvD,yEAAyE;IACzE,yDAAyD,CAAC;AAiB5D;;;;;;;;GAQG;AACH,MAAa,aAAc,SAAQ,WAAsB;IACtC,SAAS,CAAU;IACnB,eAAe,CAAkB;IACjC,cAAc,CAAU;IACxB,sBAAsB,CAAW;IAElD,YAAY,UAAgC,EAAE;QAC5C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,EAAE,CAAC;QACrD,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;QAC7C,IAAI,CAAC,sBAAsB,GAAG,OAAO,CAAC,sBAAsB,IAAI,kBAAkB,CAAC;IACrF,CAAC;IAEQ,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