@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.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +351 -0
  3. package/lib/empty-module.d.ts +4 -0
  4. package/lib/empty-module.js +21 -0
  5. package/lib/empty-module.js.map +1 -0
  6. package/lib/failure-converter.d.ts +14 -0
  7. package/lib/failure-converter.js +60 -0
  8. package/lib/failure-converter.js.map +1 -0
  9. package/lib/heartbeat.d.ts +6 -0
  10. package/lib/heartbeat.js +27 -0
  11. package/lib/heartbeat.js.map +1 -0
  12. package/lib/index.d.ts +9 -0
  13. package/lib/index.js +45 -0
  14. package/lib/index.js.map +1 -0
  15. package/lib/json-schema.d.ts +10 -0
  16. package/lib/json-schema.js +17 -0
  17. package/lib/json-schema.js.map +1 -0
  18. package/lib/load-polyfills.d.ts +1 -0
  19. package/lib/load-polyfills.js +23 -0
  20. package/lib/load-polyfills.js.map +1 -0
  21. package/lib/model-activity.d.ts +41 -0
  22. package/lib/model-activity.js +100 -0
  23. package/lib/model-activity.js.map +1 -0
  24. package/lib/plugin.d.ts +79 -0
  25. package/lib/plugin.js +152 -0
  26. package/lib/plugin.js.map +1 -0
  27. package/lib/temporal-activity-tool.d.ts +42 -0
  28. package/lib/temporal-activity-tool.js +109 -0
  29. package/lib/temporal-activity-tool.js.map +1 -0
  30. package/lib/temporal-agent.d.ts +41 -0
  31. package/lib/temporal-agent.js +80 -0
  32. package/lib/temporal-agent.js.map +1 -0
  33. package/lib/temporal-mcp-client.d.ts +107 -0
  34. package/lib/temporal-mcp-client.js +237 -0
  35. package/lib/temporal-mcp-client.js.map +1 -0
  36. package/lib/temporal-mcp-tool.d.ts +24 -0
  37. package/lib/temporal-mcp-tool.js +109 -0
  38. package/lib/temporal-mcp-tool.js.map +1 -0
  39. package/lib/temporal-model.d.ts +41 -0
  40. package/lib/temporal-model.js +88 -0
  41. package/lib/temporal-model.js.map +1 -0
  42. package/lib/workflow.d.ts +39 -0
  43. package/lib/workflow.js +66 -0
  44. package/lib/workflow.js.map +1 -0
  45. package/package.json +72 -0
  46. package/src/empty-module.ts +20 -0
  47. package/src/failure-converter.ts +76 -0
  48. package/src/heartbeat.ts +24 -0
  49. package/src/index.ts +16 -0
  50. package/src/json-schema.ts +14 -0
  51. package/src/load-polyfills.ts +24 -0
  52. package/src/model-activity.ts +124 -0
  53. package/src/plugin.ts +215 -0
  54. package/src/temporal-activity-tool.ts +127 -0
  55. package/src/temporal-agent.ts +103 -0
  56. package/src/temporal-mcp-client.ts +274 -0
  57. package/src/temporal-mcp-tool.ts +89 -0
  58. package/src/temporal-model.ts +94 -0
  59. package/src/workflow.ts +60 -0
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Helpers for wiring Temporal activities into Strands' tool and hook surfaces.
3
+ *
4
+ * Both {@link activityAsTool} and {@link activityAsHook} produce workflow-side
5
+ * objects that dispatch user activities via {@link workflow.executeActivity},
6
+ * so the I/O actually happens off the workflow.
7
+ *
8
+ * @module
9
+ */
10
+ import type { HookableEvent, HookCallback } from '@strands-agents/sdk';
11
+ import type { ActivityOptions } from '@temporalio/workflow';
12
+ import { TemporalActivityTool, type ActivityAsToolOptions } from './temporal-activity-tool';
13
+ /**
14
+ * Wrap a Temporal activity as a Strands tool.
15
+ *
16
+ * `activityName` must match the `name` registered on the worker (which
17
+ * defaults to the activity function's name). The returned {@link Tool}
18
+ * can be passed directly in {@link AgentConfig.tools}.
19
+ */
20
+ export declare function activityAsTool(activityName: string, options?: ActivityAsToolOptions): TemporalActivityTool;
21
+ /**
22
+ * Options for {@link activityAsHook}.
23
+ *
24
+ * `activityInput` extracts a serializable value from the event to pass as
25
+ * the activity's input. This is needed because events hold references to
26
+ * the `Agent` and `Tool` instances, which are not serializable.
27
+ */
28
+ export interface ActivityAsHookOptions<E extends HookableEvent, I> {
29
+ activityInput: (event: E) => I;
30
+ activityOptions?: ActivityOptions;
31
+ }
32
+ /**
33
+ * Wrap a Temporal activity as a Strands hook callback.
34
+ *
35
+ * The returned callback dispatches `activityName` as a Temporal activity
36
+ * each time the associated event fires. Register the callback with
37
+ * {@link Agent.addHook} or a {@link Plugin}'s hook registration.
38
+ */
39
+ export declare function activityAsHook<E extends HookableEvent, I>(activityName: string, options: ActivityAsHookOptions<E, I>): HookCallback<E>;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ /**
3
+ * Helpers for wiring Temporal activities into Strands' tool and hook surfaces.
4
+ *
5
+ * Both {@link activityAsTool} and {@link activityAsHook} produce workflow-side
6
+ * objects that dispatch user activities via {@link workflow.executeActivity},
7
+ * so the I/O actually happens off the workflow.
8
+ *
9
+ * @module
10
+ */
11
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ var desc = Object.getOwnPropertyDescriptor(m, k);
14
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
15
+ desc = { enumerable: true, get: function() { return m[k]; } };
16
+ }
17
+ Object.defineProperty(o, k2, desc);
18
+ }) : (function(o, m, k, k2) {
19
+ if (k2 === undefined) k2 = k;
20
+ o[k2] = m[k];
21
+ }));
22
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
23
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
24
+ }) : function(o, v) {
25
+ o["default"] = v;
26
+ });
27
+ var __importStar = (this && this.__importStar) || function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.activityAsTool = activityAsTool;
36
+ exports.activityAsHook = activityAsHook;
37
+ const workflow = __importStar(require("@temporalio/workflow"));
38
+ const temporal_activity_tool_1 = require("./temporal-activity-tool");
39
+ /**
40
+ * Wrap a Temporal activity as a Strands tool.
41
+ *
42
+ * `activityName` must match the `name` registered on the worker (which
43
+ * defaults to the activity function's name). The returned {@link Tool}
44
+ * can be passed directly in {@link AgentConfig.tools}.
45
+ */
46
+ function activityAsTool(activityName, options = {}) {
47
+ return new temporal_activity_tool_1.TemporalActivityTool(activityName, options);
48
+ }
49
+ /**
50
+ * Wrap a Temporal activity as a Strands hook callback.
51
+ *
52
+ * The returned callback dispatches `activityName` as a Temporal activity
53
+ * each time the associated event fires. Register the callback with
54
+ * {@link Agent.addHook} or a {@link Plugin}'s hook registration.
55
+ */
56
+ function activityAsHook(activityName, options) {
57
+ const activityOptions = {
58
+ startToCloseTimeout: '10 minutes',
59
+ ...(options.activityOptions ?? {}),
60
+ };
61
+ return async function activityHookCallback(event) {
62
+ const activities = workflow.proxyActivities(activityOptions);
63
+ await activities[activityName](options.activityInput(event));
64
+ };
65
+ }
66
+ //# sourceMappingURL=workflow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workflow.js","sourceRoot":"","sources":["../src/workflow.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;;;;;;;;;;;;;;;;;;;;;;AAcH,wCAEC;AAqBD,wCAcC;AAhDD,+DAAiD;AAEjD,qEAA4F;AAE5F;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,YAAoB,EAAE,UAAiC,EAAE;IACtF,OAAO,IAAI,6CAAoB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;AACzD,CAAC;AAcD;;;;;;GAMG;AACH,SAAgB,cAAc,CAC5B,YAAoB,EACpB,OAAoC;IAEpC,MAAM,eAAe,GAAoB;QACvC,mBAAmB,EAAE,YAAY;QACjC,GAAG,CAAC,OAAO,CAAC,eAAe,IAAI,EAAE,CAAC;KACnC,CAAC;IACF,OAAO,KAAK,UAAU,oBAAoB,CAAC,KAAQ;QACjD,MAAM,UAAU,GAAG,QAAQ,CAAC,eAAe,CAExC,eAAe,CAAC,CAAC;QACpB,MAAM,UAAU,CAAC,YAAY,CAAE,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC;IAChE,CAAC,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@temporalio/strands-agents",
3
+ "version": "1.21.0",
4
+ "description": "Temporal Strands Agents integration package",
5
+ "main": "lib/index.js",
6
+ "types": "./lib/index.d.ts",
7
+ "keywords": [
8
+ "temporal",
9
+ "workflow",
10
+ "strands",
11
+ "strands-agents",
12
+ "agents",
13
+ "ai",
14
+ "llm"
15
+ ],
16
+ "author": "Temporal Technologies Inc. <sdk@temporal.io>",
17
+ "license": "MIT",
18
+ "ava": {
19
+ "timeout": "120s",
20
+ "concurrency": 1,
21
+ "workerThreads": false
22
+ },
23
+ "dependencies": {
24
+ "@ungap/structured-clone": "^1.3.1",
25
+ "headers-polyfill": "^4.0.3",
26
+ "web-streams-polyfill": "^4.2.0",
27
+ "@temporalio/activity": "1.21.0",
28
+ "@temporalio/common": "1.21.0",
29
+ "@temporalio/plugin": "1.21.0",
30
+ "@temporalio/workflow": "1.21.0",
31
+ "@temporalio/workflow-streams": "1.21.0"
32
+ },
33
+ "peerDependencies": {
34
+ "@strands-agents/sdk": "^1.3.0"
35
+ },
36
+ "devDependencies": {
37
+ "@modelcontextprotocol/sdk": "^1.25.2",
38
+ "@strands-agents/sdk": "^1.3.0",
39
+ "ava": "^5.3.1",
40
+ "zod": "^4.1.12",
41
+ "@temporalio/proto": "1.21.0",
42
+ "@temporalio/testing": "1.21.0",
43
+ "@temporalio/client": "1.21.0",
44
+ "@temporalio/worker": "1.21.0",
45
+ "@temporalio/test-helpers": "1.21.0"
46
+ },
47
+ "engines": {
48
+ "node": ">= 20.0.0"
49
+ },
50
+ "bugs": {
51
+ "url": "https://github.com/temporalio/sdk-typescript/issues"
52
+ },
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/temporalio/sdk-typescript.git",
56
+ "directory": "contrib/strands"
57
+ },
58
+ "homepage": "https://github.com/temporalio/sdk-typescript/tree/main/contrib/strands",
59
+ "publishConfig": {
60
+ "access": "public"
61
+ },
62
+ "files": [
63
+ "src",
64
+ "lib",
65
+ "!src/__tests__",
66
+ "!lib/__tests__"
67
+ ],
68
+ "scripts": {
69
+ "build": "tsc --build",
70
+ "test": "ava ./lib/__tests__/test-*.js"
71
+ }
72
+ }
@@ -0,0 +1,20 @@
1
+ // Stub module that the StrandsPlugin's bundler config uses to replace
2
+ // worker-only transport modules and `node:*` schemes that
3
+ // `@strands-agents/sdk/mcp.js`/`mcp-config.js` import statically but that
4
+ // the workflow never reaches at runtime.
5
+ //
6
+ // `@strands-agents/sdk/mcp.js` does named imports like
7
+ // `import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'`,
8
+ // so a pure-empty module would fail webpack's ES-module named-export check
9
+ // at build time. Export every name MCP/Strands' static imports reach as a
10
+ // no-op class. Workflow code never instantiates these — the real transports
11
+ // run worker-side from the user's `mcpClients` factories.
12
+
13
+ class WorkflowSandboxStub {}
14
+
15
+ export {
16
+ WorkflowSandboxStub as StreamableHTTPClientTransport,
17
+ WorkflowSandboxStub as StdioClientTransport,
18
+ WorkflowSandboxStub as SSEClientTransport,
19
+ };
20
+ export default WorkflowSandboxStub;
@@ -0,0 +1,76 @@
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
+ export const failureConverter: FailureConverter = new StrandsFailureConverter();
@@ -0,0 +1,24 @@
1
+ import { Context } from '@temporalio/activity';
2
+
3
+ /**
4
+ * Wraps an async function so it heartbeats at half the activity's
5
+ * heartbeat timeout for the duration of the call. No-op when the
6
+ * activity has no heartbeat timeout configured.
7
+ */
8
+ export function autoHeartbeat<Args extends unknown[], Ret>(
9
+ fn: (...args: Args) => Promise<Ret>
10
+ ): (...args: Args) => Promise<Ret> {
11
+ return async function autoHeartbeatWrapper(this: unknown, ...args: Args): Promise<Ret> {
12
+ const heartbeatTimeoutMs = Context.current().info.heartbeatTimeoutMs;
13
+ let timer: NodeJS.Timeout | undefined;
14
+ if (heartbeatTimeoutMs && heartbeatTimeoutMs > 0) {
15
+ const interval = heartbeatTimeoutMs / 2;
16
+ timer = setInterval(() => Context.current().heartbeat(), interval);
17
+ }
18
+ try {
19
+ return await fn.apply(this, args);
20
+ } finally {
21
+ if (timer !== undefined) clearInterval(timer);
22
+ }
23
+ };
24
+ }
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ // eslint-disable-next-line import/no-unassigned-import
2
+ import './load-polyfills';
3
+
4
+ export { StrandsPlugin, type StrandsPluginOptions } from './plugin';
5
+ export { TemporalAgent, type TemporalAgentOptions } from './temporal-agent';
6
+ export { TemporalModel, type TemporalModelOptions } from './temporal-model';
7
+ export {
8
+ TemporalMCPClient,
9
+ type TemporalMCPClientOptions,
10
+ type McpToolInfo,
11
+ type CallToolInput,
12
+ } from './temporal-mcp-client';
13
+ export { TemporalMCPTool } from './temporal-mcp-tool';
14
+ export { TemporalActivityTool, type ActivityAsToolOptions } from './temporal-activity-tool';
15
+ export { STRANDS_INTERRUPT_TYPE, StrandsFailureConverter } from './failure-converter';
16
+ export * as workflow from './workflow';
@@ -0,0 +1,14 @@
1
+ import { z } from 'zod';
2
+ import type { JSONSchema } from '@strands-agents/sdk';
3
+
4
+ /**
5
+ * Normalize a tool input schema to a JSON Schema.
6
+ *
7
+ * A literal JSON Schema is returned unchanged; a Zod schema is converted via
8
+ * zod 4's native {@link z.toJSONSchema}. The conversion is pure and
9
+ * deterministic, so this is safe to call from workflow code.
10
+ */
11
+ export function toJsonSchema(schema: JSONSchema | z.ZodType | undefined): JSONSchema | undefined {
12
+ if (schema === undefined) return undefined;
13
+ return schema instanceof z.ZodType ? (z.toJSONSchema(schema) as JSONSchema) : schema;
14
+ }
@@ -0,0 +1,24 @@
1
+ import { Headers } from 'headers-polyfill';
2
+ import { inWorkflowContext, uuid4 } from '@temporalio/workflow';
3
+
4
+ if (inWorkflowContext()) {
5
+ if (typeof globalThis.Headers === 'undefined') {
6
+ globalThis.Headers = Headers;
7
+ }
8
+
9
+ // eslint-disable-next-line @typescript-eslint/no-require-imports,import/no-unassigned-import
10
+ require('web-streams-polyfill/polyfill');
11
+
12
+ if (!('structuredClone' in globalThis)) {
13
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
14
+ const structuredClone = require('@ungap/structured-clone');
15
+ globalThis.structuredClone = structuredClone.default;
16
+ }
17
+
18
+ // `@ungap/structured-clone` calls `crypto.randomUUID()` internally, but the
19
+ // workflow sandbox bans the real `crypto` module. Use the SDK's
20
+ // deterministic `uuid4` so the polyfill still works at replay.
21
+ if (typeof globalThis.crypto?.randomUUID !== 'function') {
22
+ globalThis.crypto = { ...(globalThis.crypto ?? {}), randomUUID: uuid4 } as never;
23
+ }
24
+ }
@@ -0,0 +1,124 @@
1
+ import { Message } from '@strands-agents/sdk';
2
+ import type { Model, ModelStreamEvent, StreamOptions } from '@strands-agents/sdk';
3
+ import { WorkflowStreamClient } from '@temporalio/workflow-streams/client';
4
+ import type { Duration } from '@temporalio/common/lib/time';
5
+ import { autoHeartbeat } from './heartbeat';
6
+
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: unknown): Message[] {
25
+ if (!Array.isArray(messages)) {
26
+ return [];
27
+ }
28
+ return messages.map((m) =>
29
+ m instanceof Message ? m : Message.fromMessageData(m as Parameters<typeof Message.fromMessageData>[0])
30
+ );
31
+ }
32
+
33
+ /**
34
+ * Input for the `invokeModel` activity. Mirrors `Model.stream(messages, options)`
35
+ * with the chosen factory name carried alongside.
36
+ */
37
+ export interface InvokeModelInput {
38
+ modelName?: string;
39
+ messages: Message[];
40
+ options?: StreamOptions;
41
+ }
42
+
43
+ /**
44
+ * Input for the `invokeModelStreaming` activity. Adds the workflow-stream topic
45
+ * to which each event is published, and the batch interval for publishes.
46
+ */
47
+ export interface InvokeModelStreamingInput extends InvokeModelInput {
48
+ streamingTopic: string;
49
+ streamingBatchInterval: Duration;
50
+ }
51
+
52
+ /**
53
+ * Holds the model factory map and exposes the model activities.
54
+ *
55
+ * Models are constructed lazily on first use and cached for the worker's
56
+ * lifetime. `defaultName` is only set by the plugin's own implicit
57
+ * default-Bedrock fallback; user-supplied factories force `model: 'name'`
58
+ * on every {@link TemporalAgent}.
59
+ *
60
+ * Both activities heartbeat at half the configured `heartbeatTimeout` via
61
+ * {@link autoHeartbeat} so long model calls don't stall the worker's view
62
+ * of activity liveness.
63
+ */
64
+ export class ModelActivity {
65
+ private readonly models: Map<string, Model> = new Map();
66
+
67
+ constructor(
68
+ private readonly factories: Record<string, () => Model>,
69
+ private readonly defaultName?: string
70
+ ) {
71
+ this.invokeModel = autoHeartbeat(this.invokeModel.bind(this));
72
+ this.invokeModelStreaming = autoHeartbeat(this.invokeModelStreaming.bind(this));
73
+ }
74
+
75
+ private getModel(name?: string): Model {
76
+ const resolved = name ?? this.defaultName;
77
+ if (resolved === undefined) {
78
+ throw new Error(
79
+ `TemporalAgent was constructed without an explicit \`model\`, but the plugin was configured ` +
80
+ `with user-supplied \`models\`. Pass model='...' to TemporalAgent. ` +
81
+ `Known: ${Object.keys(this.factories).sort().join(', ')}`
82
+ );
83
+ }
84
+ let model = this.models.get(resolved);
85
+ if (model === undefined) {
86
+ const factory = this.factories[resolved];
87
+ if (factory === undefined) {
88
+ throw new Error(`Unknown model name '${resolved}'. Known: ${Object.keys(this.factories).sort().join(', ')}`);
89
+ }
90
+ model = factory();
91
+ this.models.set(resolved, model);
92
+ }
93
+ return model;
94
+ }
95
+
96
+ async invokeModel(input: InvokeModelInput): Promise<ModelStreamEvent[]> {
97
+ const model = this.getModel(input.modelName);
98
+ const messages = rebuildMessages(input.messages);
99
+ const events: ModelStreamEvent[] = [];
100
+ for await (const event of model.stream(messages, input.options)) {
101
+ events.push(event);
102
+ }
103
+ return events;
104
+ }
105
+
106
+ async invokeModelStreaming(input: InvokeModelStreamingInput): Promise<ModelStreamEvent[]> {
107
+ const model = this.getModel(input.modelName);
108
+ const messages = rebuildMessages(input.messages);
109
+ const stream = WorkflowStreamClient.fromWithinActivity({
110
+ batchInterval: input.streamingBatchInterval,
111
+ });
112
+ const topic = stream.topic(input.streamingTopic);
113
+ const events: ModelStreamEvent[] = [];
114
+ try {
115
+ for await (const event of model.stream(messages, input.options)) {
116
+ events.push(event);
117
+ topic.publish(event);
118
+ }
119
+ } finally {
120
+ await stream[Symbol.asyncDispose]();
121
+ }
122
+ return events;
123
+ }
124
+ }