@temporalio/ai-sdk 1.14.2 → 1.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/activities.d.ts +32 -22
- package/lib/activities.js +12 -2
- package/lib/activities.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/load-polyfills.js +1 -1
- package/lib/load-polyfills.js.map +1 -1
- package/lib/mcp.d.ts +1 -1
- package/lib/mcp.js +8 -4
- package/lib/mcp.js.map +1 -1
- package/lib/plugin.d.ts +2 -2
- package/lib/provider.d.ts +58 -22
- package/lib/provider.js +70 -8
- package/lib/provider.js.map +1 -1
- package/package.json +10 -8
- package/src/activities.ts +51 -29
- package/src/index.ts +1 -0
- package/src/load-polyfills.ts +1 -1
- package/src/mcp.ts +10 -6
- package/src/plugin.ts +2 -2
- package/src/provider.ts +113 -26
package/lib/activities.d.ts
CHANGED
|
@@ -1,36 +1,46 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
1
|
+
import type { LanguageModelV3CallOptions, LanguageModelV3GenerateResult, EmbeddingModelV3Result, SharedV3ProviderOptions, SharedV3Headers, ProviderV3 } from '@ai-sdk/provider';
|
|
2
|
+
import { type Schema, type ToolExecutionOptions } from 'ai';
|
|
3
3
|
import type { McpClientFactories } from './mcp';
|
|
4
|
+
/**
|
|
5
|
+
* Arguments for invoking a language model activity.
|
|
6
|
+
*/
|
|
4
7
|
export interface InvokeModelArgs {
|
|
5
8
|
modelId: string;
|
|
6
|
-
options:
|
|
9
|
+
options: LanguageModelV3CallOptions;
|
|
7
10
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Result from a language model invocation.
|
|
13
|
+
* This is an alias to the AI SDK's LanguageModelV3GenerateResult for type safety.
|
|
14
|
+
*/
|
|
15
|
+
export type InvokeModelResult = LanguageModelV3GenerateResult;
|
|
16
|
+
/**
|
|
17
|
+
* Arguments for invoking an embedding model activity.
|
|
18
|
+
* Note: AbortSignal is not included as it cannot be serialized across activity boundaries.
|
|
19
|
+
* Temporal's workflow cancellation provides equivalent functionality.
|
|
20
|
+
*/
|
|
21
|
+
export interface InvokeEmbeddingModelArgs {
|
|
22
|
+
modelId: string;
|
|
23
|
+
values: string[];
|
|
24
|
+
providerOptions?: SharedV3ProviderOptions;
|
|
25
|
+
headers?: SharedV3Headers;
|
|
21
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Result from an embedding model invocation.
|
|
29
|
+
* This is an alias to the AI SDK's EmbeddingModelV3Result for type safety.
|
|
30
|
+
*/
|
|
31
|
+
export type InvokeEmbeddingModelResult = EmbeddingModelV3Result;
|
|
22
32
|
export interface ListToolResult {
|
|
23
33
|
description?: string;
|
|
24
|
-
inputSchema:
|
|
34
|
+
inputSchema: Schema<unknown>;
|
|
25
35
|
}
|
|
26
36
|
export interface ListToolArgs {
|
|
27
|
-
clientArgs?:
|
|
37
|
+
clientArgs?: unknown;
|
|
28
38
|
}
|
|
29
39
|
export interface CallToolArgs {
|
|
30
|
-
clientArgs?:
|
|
40
|
+
clientArgs?: unknown;
|
|
31
41
|
name: string;
|
|
32
|
-
|
|
33
|
-
options:
|
|
42
|
+
input: unknown;
|
|
43
|
+
options: ToolExecutionOptions;
|
|
34
44
|
}
|
|
35
45
|
/**
|
|
36
46
|
* Creates Temporal activities for AI model invocation using the provided AI SDK provider.
|
|
@@ -43,4 +53,4 @@ export interface CallToolArgs {
|
|
|
43
53
|
*
|
|
44
54
|
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
45
55
|
*/
|
|
46
|
-
export declare function createActivities(provider:
|
|
56
|
+
export declare function createActivities(provider: ProviderV3, mcpClientFactories?: McpClientFactories): object;
|
package/lib/activities.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createActivities = createActivities;
|
|
4
|
+
const ai_1 = require("ai");
|
|
4
5
|
const common_1 = require("@temporalio/common");
|
|
5
6
|
/**
|
|
6
7
|
* Creates Temporal activities for AI model invocation using the provided AI SDK provider.
|
|
@@ -19,6 +20,14 @@ function createActivities(provider, mcpClientFactories) {
|
|
|
19
20
|
const model = provider.languageModel(args.modelId);
|
|
20
21
|
return await model.doGenerate(args.options);
|
|
21
22
|
},
|
|
23
|
+
async invokeEmbeddingModel(args) {
|
|
24
|
+
const model = provider.embeddingModel(args.modelId);
|
|
25
|
+
return await model.doEmbed({
|
|
26
|
+
values: args.values,
|
|
27
|
+
providerOptions: args.providerOptions,
|
|
28
|
+
headers: args.headers,
|
|
29
|
+
});
|
|
30
|
+
},
|
|
22
31
|
};
|
|
23
32
|
if (mcpClientFactories !== undefined) {
|
|
24
33
|
Object.entries(mcpClientFactories).forEach(([name, func]) => {
|
|
@@ -39,7 +48,8 @@ function activitiesForName(name, mcpClientFactory) {
|
|
|
39
48
|
k,
|
|
40
49
|
{
|
|
41
50
|
description: v.description,
|
|
42
|
-
|
|
51
|
+
// Convert the FlexibleSchema to a Schema so that the shape is known outside the activity
|
|
52
|
+
inputSchema: (0, ai_1.asSchema)(v.inputSchema),
|
|
43
53
|
},
|
|
44
54
|
]));
|
|
45
55
|
}
|
|
@@ -55,7 +65,7 @@ function activitiesForName(name, mcpClientFactory) {
|
|
|
55
65
|
if (tool === undefined) {
|
|
56
66
|
throw common_1.ApplicationFailure.retryable(`Tool ${args.name} not found.`);
|
|
57
67
|
}
|
|
58
|
-
return tool.execute(args.
|
|
68
|
+
return await tool.execute(args.input, args.options);
|
|
59
69
|
}
|
|
60
70
|
finally {
|
|
61
71
|
await mcpClient.close();
|
package/lib/activities.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activities.js","sourceRoot":"","sources":["../src/activities.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"activities.js","sourceRoot":"","sources":["../src/activities.ts"],"names":[],"mappings":";;AAuEA,4CAwBC;AAvFD,2BAAsE;AACtE,+CAAwD;AAmDxD;;;;;;;;;;GAUG;AACH,SAAgB,gBAAgB,CAAC,QAAoB,EAAE,kBAAuC;IAC5F,IAAI,UAAU,GAAG;QACf,KAAK,CAAC,WAAW,CAAC,IAAqB;YACrC,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACnD,OAAO,MAAM,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9C,CAAC;QACD,KAAK,CAAC,oBAAoB,CAAC,IAA8B;YACvD,MAAM,KAAK,GAAG,QAAQ,CAAC,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACpD,OAAO,MAAM,KAAK,CAAC,OAAO,CAAC;gBACzB,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,eAAe,EAAE,IAAI,CAAC,eAAe;gBACrC,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAC;QACL,CAAC;KACF,CAAC;IACF,IAAI,kBAAkB,KAAK,SAAS,EAAE,CAAC;QACrC,MAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE;YAC1D,UAAU,GAAG;gBACX,GAAG,UAAU;gBACb,GAAG,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC;aACjC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAY,EAAE,gBAAkC;IACzE,KAAK,UAAU,iBAAiB,CAAC,IAAkB;QACjD,MAAM,S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vC,CAAC;AACJ,CAAC"}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
// eslint-disable-next-line import/no-unassigned-import
|
|
18
18
|
require("./load-polyfills");
|
|
19
|
+
__exportStar(require("./activities"), exports);
|
|
19
20
|
__exportStar(require("./mcp"), exports);
|
|
20
21
|
__exportStar(require("./plugin"), exports);
|
|
21
22
|
__exportStar(require("./provider"), exports);
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAuD;AACvD,4BAA0B;AAE1B,wCAAsB;AACtB,2CAAyB;AACzB,6CAA2B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAuD;AACvD,4BAA0B;AAE1B,+CAA6B;AAC7B,wCAAsB;AACtB,2CAAyB;AACzB,6CAA2B"}
|
package/lib/load-polyfills.js
CHANGED
|
@@ -11,7 +11,7 @@ if ((0, workflow_1.inWorkflowContext)()) {
|
|
|
11
11
|
require('web-streams-polyfill/polyfill');
|
|
12
12
|
// Attach the polyfill as a Global function
|
|
13
13
|
if (!('structuredClone' in globalThis)) {
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
15
15
|
const structuredClone = require('@ungap/structured-clone');
|
|
16
16
|
globalThis.structuredClone = structuredClone.default;
|
|
17
17
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"load-polyfills.js","sourceRoot":"","sources":["../src/load-polyfills.ts"],"names":[],"mappings":";;AAAA,uDAA2C;AAC3C,mDAAyD;AAEzD,IAAI,IAAA,4BAAiB,GAAE,EAAE,CAAC;IACxB,yBAAyB;IACzB,IAAI,OAAO,UAAU,CAAC,OAAO,KAAK,WAAW,EAAE,CAAC;QAC9C,UAAU,CAAC,OAAO,GAAG,0BAAO,CAAC;IAC/B,CAAC;IAED,6FAA6F;IAC7F,OAAO,CAAC,+BAA+B,CAAC,CAAC;IACzC,2CAA2C;IAC3C,IAAI,CAAC,CAAC,iBAAiB,IAAI,UAAU,CAAC,EAAE,CAAC;QACvC,
|
|
1
|
+
{"version":3,"file":"load-polyfills.js","sourceRoot":"","sources":["../src/load-polyfills.ts"],"names":[],"mappings":";;AAAA,uDAA2C;AAC3C,mDAAyD;AAEzD,IAAI,IAAA,4BAAiB,GAAE,EAAE,CAAC;IACxB,yBAAyB;IACzB,IAAI,OAAO,UAAU,CAAC,OAAO,KAAK,WAAW,EAAE,CAAC;QAC9C,UAAU,CAAC,OAAO,GAAG,0BAAO,CAAC;IAC/B,CAAC;IAED,6FAA6F;IAC7F,OAAO,CAAC,+BAA+B,CAAC,CAAC;IACzC,2CAA2C;IAC3C,IAAI,CAAC,CAAC,iBAAiB,IAAI,UAAU,CAAC,EAAE,CAAC;QACvC,iEAAiE;QACjE,MAAM,eAAe,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAC;QAC3D,UAAU,CAAC,eAAe,GAAG,eAAe,CAAC,OAAO,CAAC;IACvD,CAAC;AACH,CAAC"}
|
package/lib/mcp.d.ts
CHANGED
package/lib/mcp.js
CHANGED
|
@@ -40,22 +40,26 @@ class TemporalMCPClient {
|
|
|
40
40
|
this.options = options;
|
|
41
41
|
}
|
|
42
42
|
async tools() {
|
|
43
|
-
workflow.
|
|
44
|
-
|
|
43
|
+
const activities = workflow.proxyActivities({
|
|
44
|
+
startToCloseTimeout: '10 minutes',
|
|
45
|
+
...this.options.activityOptions,
|
|
46
|
+
});
|
|
45
47
|
const listActivity = activities[this.options.name + '-listTools'];
|
|
46
48
|
const tools = await listActivity({ clientArgs: this.options.clientArgs });
|
|
47
49
|
return Object.fromEntries(Object.entries(tools).map(([toolName, toolResult]) => [
|
|
48
50
|
toolName,
|
|
49
51
|
{
|
|
50
|
-
|
|
52
|
+
description: toolResult.description,
|
|
53
|
+
execute: async (input, options) => {
|
|
51
54
|
const activities = workflow.proxyActivities({
|
|
52
55
|
summary: toolName,
|
|
53
56
|
startToCloseTimeout: '10 minutes',
|
|
54
57
|
...this.options,
|
|
55
58
|
});
|
|
56
59
|
const callActivity = activities[this.options.name + '-callTool'];
|
|
57
|
-
return await callActivity({ name: toolName,
|
|
60
|
+
return await callActivity({ name: toolName, input, options, clientArgs: this.options.clientArgs });
|
|
58
61
|
},
|
|
62
|
+
// Symbols and undefined values are lost on serialization, and need to be replaced to ensure the schema is used correctly
|
|
59
63
|
inputSchema: {
|
|
60
64
|
...toolResult.inputSchema,
|
|
61
65
|
_type: undefined,
|
package/lib/mcp.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mcp.js","sourceRoot":"","sources":["../src/mcp.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,+DAAiD;
|
|
1
|
+
{"version":3,"file":"mcp.js","sourceRoot":"","sources":["../src/mcp.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,+DAAiD;AAkBjD;;;;;;;;GAQG;AACH,MAAa,iBAAiB;IACP;IAArB,YAAqB,OAAiC;QAAjC,YAAO,GAAP,OAAO,CAA0B;IAAG,CAAC;IAE1D,KAAK,CAAC,KAAK;QACT,MAAM,UAAU,GAAG,QAAQ,CAAC,eAAe,CAAC;YAC1C,mBAAmB,EAAE,YAAY;YACjC,GAAG,IAAI,CAAC,OAAO,CAAC,eAAe;SAChC,CAAC,CAAC;QAEH,MAAM,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,GAAG,YAAY,CAAC,CAAC;QAClE,MAAM,KAAK,GAAmC,MAAM,Y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@temporalio/plugin';
|
|
3
3
|
import type { McpClientFactories } from './mcp';
|
|
4
4
|
/**
|
|
@@ -7,7 +7,7 @@ import type { McpClientFactories } from './mcp';
|
|
|
7
7
|
* @experimental The AI SDK plugin is an experimental feature; APIs may change without notice.
|
|
8
8
|
*/
|
|
9
9
|
export interface AiSdkPluginOptions {
|
|
10
|
-
modelProvider:
|
|
10
|
+
modelProvider: ProviderV3;
|
|
11
11
|
/**
|
|
12
12
|
* This object contains a mapping of server names to functions which create MCP clients.
|
|
13
13
|
* Any TemporalMCPClient used in a workflow should have its associated servername listed in this object.
|
package/lib/provider.d.ts
CHANGED
|
@@ -1,29 +1,63 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { ActivityOptions } from '@temporalio/workflow';
|
|
3
|
-
|
|
1
|
+
import type { EmbeddingModelV3, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, ImageModelV3, LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, ProviderV3, TranscriptionModelV3 } from '@ai-sdk/provider';
|
|
2
|
+
import type { ActivityOptions } from '@temporalio/workflow';
|
|
3
|
+
/**
|
|
4
|
+
* Options for configuring the TemporalProvider with per-model activity settings.
|
|
5
|
+
*/
|
|
6
|
+
export interface TemporalProviderOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Default activity options applied to all model types.
|
|
9
|
+
* These can be overridden by model-specific options.
|
|
10
|
+
*/
|
|
11
|
+
default?: ActivityOptions;
|
|
12
|
+
/**
|
|
13
|
+
* Activity options specific to language model calls.
|
|
14
|
+
* Merged with default options, with these taking precedence.
|
|
15
|
+
*/
|
|
16
|
+
languageModel?: ActivityOptions;
|
|
17
|
+
/**
|
|
18
|
+
* Activity options specific to embedding model calls.
|
|
19
|
+
* Merged with default options, with these taking precedence.
|
|
20
|
+
*/
|
|
21
|
+
embeddingModel?: ActivityOptions;
|
|
22
|
+
}
|
|
4
23
|
/**
|
|
5
24
|
* A language model implementation that delegates AI model calls to Temporal activities.
|
|
6
25
|
* This allows workflows to invoke AI models through the Temporal execution model.
|
|
7
26
|
*
|
|
8
27
|
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
9
28
|
*/
|
|
10
|
-
export declare class TemporalLanguageModel implements
|
|
29
|
+
export declare class TemporalLanguageModel implements LanguageModelV3 {
|
|
11
30
|
readonly modelId: string;
|
|
12
31
|
readonly options?: ActivityOptions | undefined;
|
|
13
|
-
readonly specificationVersion = "
|
|
32
|
+
readonly specificationVersion = "v3";
|
|
14
33
|
readonly provider = "temporal";
|
|
15
|
-
readonly supportedUrls: {};
|
|
16
34
|
constructor(modelId: string, options?: ActivityOptions | undefined);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
35
|
+
get supportedUrls(): Record<string, RegExp[]>;
|
|
36
|
+
doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
|
|
37
|
+
doStream(_options: LanguageModelV3CallOptions): PromiseLike<LanguageModelV3StreamResult>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* An embedding model implementation that delegates embedding generation to Temporal activities.
|
|
41
|
+
* This allows workflows to generate embeddings through the Temporal execution model.
|
|
42
|
+
*
|
|
43
|
+
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
44
|
+
*/
|
|
45
|
+
export declare class TemporalEmbeddingModel implements EmbeddingModelV3 {
|
|
46
|
+
readonly modelId: string;
|
|
47
|
+
readonly options?: ActivityOptions | undefined;
|
|
48
|
+
readonly specificationVersion = "v3";
|
|
49
|
+
readonly provider = "temporal";
|
|
50
|
+
/**
|
|
51
|
+
* Undefined to let the underlying provider handle chunking, as it knows its own limits.
|
|
52
|
+
*/
|
|
53
|
+
readonly maxEmbeddingsPerCall: undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Indicates the underlying embedding model API can handle concurrent requests.
|
|
56
|
+
* Set to true since we delegate to the actual provider which manages its own concurrency.
|
|
57
|
+
*/
|
|
58
|
+
readonly supportsParallelCalls = true;
|
|
59
|
+
constructor(modelId: string, options?: ActivityOptions | undefined);
|
|
60
|
+
doEmbed(options: EmbeddingModelV3CallOptions): Promise<EmbeddingModelV3Result>;
|
|
27
61
|
}
|
|
28
62
|
/**
|
|
29
63
|
* A Temporal-specific provider implementation that creates AI models which execute
|
|
@@ -32,12 +66,14 @@ export declare class TemporalLanguageModel implements LanguageModelV2 {
|
|
|
32
66
|
*
|
|
33
67
|
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
34
68
|
*/
|
|
35
|
-
export declare class TemporalProvider implements
|
|
36
|
-
readonly options?:
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
69
|
+
export declare class TemporalProvider implements ProviderV3 {
|
|
70
|
+
readonly options?: TemporalProviderOptions | undefined;
|
|
71
|
+
readonly specificationVersion = "v3";
|
|
72
|
+
constructor(options?: TemporalProviderOptions | undefined);
|
|
73
|
+
imageModel(_modelId: string): ImageModelV3;
|
|
74
|
+
languageModel(modelId: string): LanguageModelV3;
|
|
75
|
+
embeddingModel(modelId: string): EmbeddingModelV3;
|
|
76
|
+
transcriptionModel(_modelId: string): TranscriptionModelV3;
|
|
41
77
|
}
|
|
42
78
|
/**
|
|
43
79
|
* A singleton instance of TemporalProvider for convenient use in applications.
|
package/lib/provider.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.temporalProvider = exports.TemporalProvider = exports.TemporalLanguageModel = void 0;
|
|
26
|
+
exports.temporalProvider = exports.TemporalProvider = exports.TemporalEmbeddingModel = exports.TemporalLanguageModel = void 0;
|
|
27
27
|
const workflow = __importStar(require("@temporalio/workflow"));
|
|
28
28
|
const common_1 = require("@temporalio/common");
|
|
29
29
|
/**
|
|
@@ -35,21 +35,30 @@ const common_1 = require("@temporalio/common");
|
|
|
35
35
|
class TemporalLanguageModel {
|
|
36
36
|
modelId;
|
|
37
37
|
options;
|
|
38
|
-
specificationVersion = '
|
|
38
|
+
specificationVersion = 'v3';
|
|
39
39
|
provider = 'temporal';
|
|
40
|
-
supportedUrls = {};
|
|
41
40
|
constructor(modelId, options) {
|
|
42
41
|
this.modelId = modelId;
|
|
43
42
|
this.options = options;
|
|
44
43
|
}
|
|
44
|
+
get supportedUrls() {
|
|
45
|
+
return {};
|
|
46
|
+
}
|
|
45
47
|
async doGenerate(options) {
|
|
46
|
-
const activities = workflow.proxyActivities({
|
|
48
|
+
const activities = workflow.proxyActivities({
|
|
49
|
+
startToCloseTimeout: '10 minutes',
|
|
50
|
+
...this.options,
|
|
51
|
+
});
|
|
47
52
|
const result = await activities.invokeModel({ modelId: this.modelId, options });
|
|
48
53
|
if (result === undefined) {
|
|
49
54
|
throw common_1.ApplicationFailure.nonRetryable('Received undefined response from model activity.');
|
|
50
55
|
}
|
|
51
|
-
if (result.response !== undefined) {
|
|
52
|
-
|
|
56
|
+
if (result.response !== undefined && result.response.timestamp) {
|
|
57
|
+
const timestamp = new Date(result.response.timestamp);
|
|
58
|
+
// Only set if it's a valid date
|
|
59
|
+
if (!isNaN(timestamp.getTime())) {
|
|
60
|
+
result.response.timestamp = timestamp;
|
|
61
|
+
}
|
|
53
62
|
}
|
|
54
63
|
return result;
|
|
55
64
|
}
|
|
@@ -58,6 +67,49 @@ class TemporalLanguageModel {
|
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
69
|
exports.TemporalLanguageModel = TemporalLanguageModel;
|
|
70
|
+
/**
|
|
71
|
+
* An embedding model implementation that delegates embedding generation to Temporal activities.
|
|
72
|
+
* This allows workflows to generate embeddings through the Temporal execution model.
|
|
73
|
+
*
|
|
74
|
+
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
75
|
+
*/
|
|
76
|
+
class TemporalEmbeddingModel {
|
|
77
|
+
modelId;
|
|
78
|
+
options;
|
|
79
|
+
specificationVersion = 'v3';
|
|
80
|
+
provider = 'temporal';
|
|
81
|
+
/**
|
|
82
|
+
* Undefined to let the underlying provider handle chunking, as it knows its own limits.
|
|
83
|
+
*/
|
|
84
|
+
maxEmbeddingsPerCall = undefined;
|
|
85
|
+
/**
|
|
86
|
+
* Indicates the underlying embedding model API can handle concurrent requests.
|
|
87
|
+
* Set to true since we delegate to the actual provider which manages its own concurrency.
|
|
88
|
+
*/
|
|
89
|
+
supportsParallelCalls = true;
|
|
90
|
+
constructor(modelId, options) {
|
|
91
|
+
this.modelId = modelId;
|
|
92
|
+
this.options = options;
|
|
93
|
+
}
|
|
94
|
+
async doEmbed(options) {
|
|
95
|
+
const activities = workflow.proxyActivities({
|
|
96
|
+
startToCloseTimeout: '10 minutes',
|
|
97
|
+
...this.options,
|
|
98
|
+
});
|
|
99
|
+
const result = await activities.invokeEmbeddingModel({
|
|
100
|
+
modelId: this.modelId,
|
|
101
|
+
values: options.values,
|
|
102
|
+
providerOptions: options.providerOptions,
|
|
103
|
+
headers: options.headers,
|
|
104
|
+
// Note: abortSignal is not serializable, Temporal's cancellation handles this
|
|
105
|
+
});
|
|
106
|
+
if (result === undefined) {
|
|
107
|
+
throw common_1.ApplicationFailure.nonRetryable('Received undefined response from embedding model activity.');
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.TemporalEmbeddingModel = TemporalEmbeddingModel;
|
|
61
113
|
/**
|
|
62
114
|
* A Temporal-specific provider implementation that creates AI models which execute
|
|
63
115
|
* through Temporal activities. This provider integrates AI SDK models with Temporal's
|
|
@@ -67,6 +119,7 @@ exports.TemporalLanguageModel = TemporalLanguageModel;
|
|
|
67
119
|
*/
|
|
68
120
|
class TemporalProvider {
|
|
69
121
|
options;
|
|
122
|
+
specificationVersion = 'v3';
|
|
70
123
|
constructor(options) {
|
|
71
124
|
this.options = options;
|
|
72
125
|
}
|
|
@@ -74,9 +127,18 @@ class TemporalProvider {
|
|
|
74
127
|
throw new Error('Not implemented');
|
|
75
128
|
}
|
|
76
129
|
languageModel(modelId) {
|
|
77
|
-
return new TemporalLanguageModel(modelId,
|
|
130
|
+
return new TemporalLanguageModel(modelId, {
|
|
131
|
+
...this.options?.default,
|
|
132
|
+
...this.options?.languageModel,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
embeddingModel(modelId) {
|
|
136
|
+
return new TemporalEmbeddingModel(modelId, {
|
|
137
|
+
...this.options?.default,
|
|
138
|
+
...this.options?.embeddingModel,
|
|
139
|
+
});
|
|
78
140
|
}
|
|
79
|
-
|
|
141
|
+
transcriptionModel(_modelId) {
|
|
80
142
|
throw new Error('Not implemented');
|
|
81
143
|
}
|
|
82
144
|
}
|
package/lib/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,+DAAiD;AAEjD,+CAAwD;AAyBxD;;;;;GAKG;AACH,MAAa,qBAAqB;IAKrB;IACA;IALF,oBAAoB,GAAG,IAAI,CAAC;IAC5B,QAAQ,GAAG,UAAU,CAAC;IAE/B,YACW,OAAe,EACf,OAAyB;QADzB,YAAO,GAAP,OAAO,CAAQ;QACf,YAAO,GAAP,OAAO,CAAkB;IACjC,CAAC;IAEJ,IAAI,aAAa;QACf,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,OAAmC;QAClD,MAAM,UAAU,GAAG,QAAQ,CAAC,eAAe,CAAC;YAC1C,mBAAmB,EAAE,YAAY;YACjC,GAAG,IAAI,CAA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
|
"name": "@temporalio/ai-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.1",
|
|
4
4
|
"description": "Temporal AI SDK integration package",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"workflow",
|
|
10
10
|
"ai",
|
|
11
11
|
"ai-sdk",
|
|
12
|
-
"llm"
|
|
12
|
+
"llm",
|
|
13
|
+
"embeddings"
|
|
13
14
|
],
|
|
14
15
|
"author": "Temporal Technologies Inc. <sdk@temporal.io>",
|
|
15
16
|
"license": "MIT",
|
|
@@ -17,16 +18,17 @@
|
|
|
17
18
|
"@ungap/structured-clone": "^1.3.0",
|
|
18
19
|
"headers-polyfill": "^4.0.3",
|
|
19
20
|
"web-streams-polyfill": "^4.2.0",
|
|
20
|
-
"@temporalio/
|
|
21
|
-
"@temporalio/
|
|
21
|
+
"@temporalio/workflow": "1.15.1",
|
|
22
|
+
"@temporalio/common": "1.15.1",
|
|
23
|
+
"@temporalio/plugin": "1.15.1"
|
|
22
24
|
},
|
|
23
25
|
"peerDependencies": {
|
|
24
|
-
"@ai-sdk/mcp": "^0.0
|
|
25
|
-
"@ai-sdk/provider": "^
|
|
26
|
-
"ai": "^
|
|
26
|
+
"@ai-sdk/mcp": "^1.0.0",
|
|
27
|
+
"@ai-sdk/provider": "^3.0.0",
|
|
28
|
+
"ai": "^6.0.0"
|
|
27
29
|
},
|
|
28
30
|
"engines": {
|
|
29
|
-
"node": ">=
|
|
31
|
+
"node": ">= 20.0.0"
|
|
30
32
|
},
|
|
31
33
|
"bugs": {
|
|
32
34
|
"url": "https://github.com/temporalio/sdk-typescript/issues"
|
package/src/activities.ts
CHANGED
|
@@ -1,47 +1,61 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
ProviderV2,
|
|
9
|
-
SharedV2Headers,
|
|
10
|
-
SharedV2ProviderMetadata,
|
|
2
|
+
LanguageModelV3CallOptions,
|
|
3
|
+
LanguageModelV3GenerateResult,
|
|
4
|
+
EmbeddingModelV3Result,
|
|
5
|
+
SharedV3ProviderOptions,
|
|
6
|
+
SharedV3Headers,
|
|
7
|
+
ProviderV3,
|
|
11
8
|
} from '@ai-sdk/provider';
|
|
12
|
-
import type
|
|
9
|
+
import { asSchema, type Schema, type ToolExecutionOptions } from 'ai';
|
|
13
10
|
import { ApplicationFailure } from '@temporalio/common';
|
|
14
11
|
import type { McpClientFactories, McpClientFactory } from './mcp';
|
|
15
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Arguments for invoking a language model activity.
|
|
15
|
+
*/
|
|
16
16
|
export interface InvokeModelArgs {
|
|
17
17
|
modelId: string;
|
|
18
|
-
options:
|
|
18
|
+
options: LanguageModelV3CallOptions;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Result from a language model invocation.
|
|
23
|
+
* This is an alias to the AI SDK's LanguageModelV3GenerateResult for type safety.
|
|
24
|
+
*/
|
|
25
|
+
export type InvokeModelResult = LanguageModelV3GenerateResult;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Arguments for invoking an embedding model activity.
|
|
29
|
+
* Note: AbortSignal is not included as it cannot be serialized across activity boundaries.
|
|
30
|
+
* Temporal's workflow cancellation provides equivalent functionality.
|
|
31
|
+
*/
|
|
32
|
+
export interface InvokeEmbeddingModelArgs {
|
|
33
|
+
modelId: string;
|
|
34
|
+
values: string[];
|
|
35
|
+
providerOptions?: SharedV3ProviderOptions;
|
|
36
|
+
headers?: SharedV3Headers;
|
|
29
37
|
}
|
|
30
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Result from an embedding model invocation.
|
|
41
|
+
* This is an alias to the AI SDK's EmbeddingModelV3Result for type safety.
|
|
42
|
+
*/
|
|
43
|
+
export type InvokeEmbeddingModelResult = EmbeddingModelV3Result;
|
|
44
|
+
|
|
31
45
|
export interface ListToolResult {
|
|
32
46
|
description?: string;
|
|
33
|
-
inputSchema:
|
|
47
|
+
inputSchema: Schema<unknown>;
|
|
34
48
|
}
|
|
35
49
|
|
|
36
50
|
export interface ListToolArgs {
|
|
37
|
-
clientArgs?:
|
|
51
|
+
clientArgs?: unknown;
|
|
38
52
|
}
|
|
39
53
|
|
|
40
54
|
export interface CallToolArgs {
|
|
41
|
-
clientArgs?:
|
|
55
|
+
clientArgs?: unknown;
|
|
42
56
|
name: string;
|
|
43
|
-
|
|
44
|
-
options:
|
|
57
|
+
input: unknown;
|
|
58
|
+
options: ToolExecutionOptions;
|
|
45
59
|
}
|
|
46
60
|
|
|
47
61
|
/**
|
|
@@ -55,12 +69,20 @@ export interface CallToolArgs {
|
|
|
55
69
|
*
|
|
56
70
|
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
57
71
|
*/
|
|
58
|
-
export function createActivities(provider:
|
|
72
|
+
export function createActivities(provider: ProviderV3, mcpClientFactories?: McpClientFactories): object {
|
|
59
73
|
let activities = {
|
|
60
74
|
async invokeModel(args: InvokeModelArgs): Promise<InvokeModelResult> {
|
|
61
75
|
const model = provider.languageModel(args.modelId);
|
|
62
76
|
return await model.doGenerate(args.options);
|
|
63
77
|
},
|
|
78
|
+
async invokeEmbeddingModel(args: InvokeEmbeddingModelArgs): Promise<InvokeEmbeddingModelResult> {
|
|
79
|
+
const model = provider.embeddingModel(args.modelId);
|
|
80
|
+
return await model.doEmbed({
|
|
81
|
+
values: args.values,
|
|
82
|
+
providerOptions: args.providerOptions,
|
|
83
|
+
headers: args.headers,
|
|
84
|
+
});
|
|
85
|
+
},
|
|
64
86
|
};
|
|
65
87
|
if (mcpClientFactories !== undefined) {
|
|
66
88
|
Object.entries(mcpClientFactories).forEach(([name, func]) => {
|
|
@@ -78,13 +100,13 @@ function activitiesForName(name: string, mcpClientFactory: McpClientFactory): ob
|
|
|
78
100
|
const mcpClient = await mcpClientFactory(args.clientArgs);
|
|
79
101
|
try {
|
|
80
102
|
const tools = await mcpClient.tools();
|
|
81
|
-
|
|
82
103
|
return Object.fromEntries(
|
|
83
104
|
Object.entries(tools).map(([k, v]) => [
|
|
84
105
|
k,
|
|
85
106
|
{
|
|
86
107
|
description: v.description,
|
|
87
|
-
|
|
108
|
+
// Convert the FlexibleSchema to a Schema so that the shape is known outside the activity
|
|
109
|
+
inputSchema: asSchema(v.inputSchema),
|
|
88
110
|
},
|
|
89
111
|
])
|
|
90
112
|
);
|
|
@@ -92,7 +114,7 @@ function activitiesForName(name: string, mcpClientFactory: McpClientFactory): ob
|
|
|
92
114
|
await mcpClient.close();
|
|
93
115
|
}
|
|
94
116
|
}
|
|
95
|
-
async function callToolActivity(args: CallToolArgs): Promise<
|
|
117
|
+
async function callToolActivity(args: CallToolArgs): Promise<unknown> {
|
|
96
118
|
const mcpClient = await mcpClientFactory(args.clientArgs);
|
|
97
119
|
try {
|
|
98
120
|
const tools = await mcpClient.tools();
|
|
@@ -100,7 +122,7 @@ function activitiesForName(name: string, mcpClientFactory: McpClientFactory): ob
|
|
|
100
122
|
if (tool === undefined) {
|
|
101
123
|
throw ApplicationFailure.retryable(`Tool ${args.name} not found.`);
|
|
102
124
|
}
|
|
103
|
-
return tool.execute(args.
|
|
125
|
+
return await tool.execute(args.input, args.options);
|
|
104
126
|
} finally {
|
|
105
127
|
await mcpClient.close();
|
|
106
128
|
}
|
package/src/index.ts
CHANGED
package/src/load-polyfills.ts
CHANGED
|
@@ -11,7 +11,7 @@ if (inWorkflowContext()) {
|
|
|
11
11
|
require('web-streams-polyfill/polyfill');
|
|
12
12
|
// Attach the polyfill as a Global function
|
|
13
13
|
if (!('structuredClone' in globalThis)) {
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
15
15
|
const structuredClone = require('@ungap/structured-clone');
|
|
16
16
|
globalThis.structuredClone = structuredClone.default;
|
|
17
17
|
}
|
package/src/mcp.ts
CHANGED
|
@@ -14,8 +14,7 @@ export type McpClientFactories = { [serviceName: string]: McpClientFactory };
|
|
|
14
14
|
*/
|
|
15
15
|
export interface TemporalMCPClientOptions {
|
|
16
16
|
readonly name: string;
|
|
17
|
-
|
|
18
|
-
readonly clientArgs?: any;
|
|
17
|
+
readonly clientArgs?: unknown;
|
|
19
18
|
readonly activityOptions?: ActivityOptions;
|
|
20
19
|
}
|
|
21
20
|
|
|
@@ -32,24 +31,29 @@ export class TemporalMCPClient {
|
|
|
32
31
|
constructor(readonly options: TemporalMCPClientOptions) {}
|
|
33
32
|
|
|
34
33
|
async tools(): Promise<ToolSet> {
|
|
35
|
-
workflow.
|
|
36
|
-
|
|
34
|
+
const activities = workflow.proxyActivities({
|
|
35
|
+
startToCloseTimeout: '10 minutes',
|
|
36
|
+
...this.options.activityOptions,
|
|
37
|
+
});
|
|
37
38
|
|
|
38
39
|
const listActivity = activities[this.options.name + '-listTools'];
|
|
39
40
|
const tools: Record<string, ListToolResult> = await listActivity!({ clientArgs: this.options.clientArgs });
|
|
41
|
+
|
|
40
42
|
return Object.fromEntries(
|
|
41
43
|
Object.entries(tools).map(([toolName, toolResult]) => [
|
|
42
44
|
toolName,
|
|
43
45
|
{
|
|
44
|
-
|
|
46
|
+
description: toolResult.description,
|
|
47
|
+
execute: async (input, options) => {
|
|
45
48
|
const activities = workflow.proxyActivities({
|
|
46
49
|
summary: toolName,
|
|
47
50
|
startToCloseTimeout: '10 minutes',
|
|
48
51
|
...this.options,
|
|
49
52
|
});
|
|
50
53
|
const callActivity = activities[this.options.name + '-callTool']!;
|
|
51
|
-
return await callActivity({ name: toolName,
|
|
54
|
+
return await callActivity({ name: toolName, input, options, clientArgs: this.options.clientArgs });
|
|
52
55
|
},
|
|
56
|
+
// Symbols and undefined values are lost on serialization, and need to be replaced to ensure the schema is used correctly
|
|
53
57
|
inputSchema: {
|
|
54
58
|
...toolResult.inputSchema,
|
|
55
59
|
_type: undefined,
|
package/src/plugin.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ProviderV3 } from '@ai-sdk/provider';
|
|
2
2
|
import { SimplePlugin } from '@temporalio/plugin';
|
|
3
3
|
import { createActivities } from './activities';
|
|
4
4
|
import type { McpClientFactories } from './mcp';
|
|
@@ -9,7 +9,7 @@ import type { McpClientFactories } from './mcp';
|
|
|
9
9
|
* @experimental The AI SDK plugin is an experimental feature; APIs may change without notice.
|
|
10
10
|
*/
|
|
11
11
|
export interface AiSdkPluginOptions {
|
|
12
|
-
modelProvider:
|
|
12
|
+
modelProvider: ProviderV3;
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* This object contains a mapping of server names to functions which create MCP clients.
|
package/src/provider.ts
CHANGED
|
@@ -1,15 +1,41 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
EmbeddingModelV3,
|
|
3
|
+
EmbeddingModelV3CallOptions,
|
|
4
|
+
EmbeddingModelV3Result,
|
|
5
|
+
ImageModelV3,
|
|
6
|
+
LanguageModelV3,
|
|
7
|
+
LanguageModelV3CallOptions,
|
|
8
|
+
LanguageModelV3GenerateResult,
|
|
9
|
+
LanguageModelV3StreamResult,
|
|
10
|
+
ProviderV3,
|
|
11
|
+
TranscriptionModelV3,
|
|
8
12
|
} from '@ai-sdk/provider';
|
|
9
13
|
import * as workflow from '@temporalio/workflow';
|
|
10
|
-
import { ActivityOptions } from '@temporalio/workflow';
|
|
14
|
+
import type { ActivityOptions } from '@temporalio/workflow';
|
|
11
15
|
import { ApplicationFailure } from '@temporalio/common';
|
|
12
|
-
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Options for configuring the TemporalProvider with per-model activity settings.
|
|
19
|
+
*/
|
|
20
|
+
export interface TemporalProviderOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Default activity options applied to all model types.
|
|
23
|
+
* These can be overridden by model-specific options.
|
|
24
|
+
*/
|
|
25
|
+
default?: ActivityOptions;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Activity options specific to language model calls.
|
|
29
|
+
* Merged with default options, with these taking precedence.
|
|
30
|
+
*/
|
|
31
|
+
languageModel?: ActivityOptions;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Activity options specific to embedding model calls.
|
|
35
|
+
* Merged with default options, with these taking precedence.
|
|
36
|
+
*/
|
|
37
|
+
embeddingModel?: ActivityOptions;
|
|
38
|
+
}
|
|
13
39
|
|
|
14
40
|
/**
|
|
15
41
|
* A language model implementation that delegates AI model calls to Temporal activities.
|
|
@@ -17,37 +43,86 @@ import { InvokeModelResult } from './activities';
|
|
|
17
43
|
*
|
|
18
44
|
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
19
45
|
*/
|
|
20
|
-
export class TemporalLanguageModel implements
|
|
21
|
-
readonly specificationVersion = '
|
|
46
|
+
export class TemporalLanguageModel implements LanguageModelV3 {
|
|
47
|
+
readonly specificationVersion = 'v3';
|
|
22
48
|
readonly provider = 'temporal';
|
|
23
|
-
readonly supportedUrls = {};
|
|
24
49
|
|
|
25
50
|
constructor(
|
|
26
51
|
readonly modelId: string,
|
|
27
52
|
readonly options?: ActivityOptions
|
|
28
53
|
) {}
|
|
29
54
|
|
|
30
|
-
|
|
31
|
-
|
|
55
|
+
get supportedUrls(): Record<string, RegExp[]> {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult> {
|
|
60
|
+
const activities = workflow.proxyActivities({
|
|
61
|
+
startToCloseTimeout: '10 minutes',
|
|
62
|
+
...this.options,
|
|
63
|
+
});
|
|
32
64
|
const result = await activities.invokeModel!({ modelId: this.modelId, options });
|
|
33
65
|
if (result === undefined) {
|
|
34
66
|
throw ApplicationFailure.nonRetryable('Received undefined response from model activity.');
|
|
35
67
|
}
|
|
36
|
-
if (result.response !== undefined) {
|
|
37
|
-
|
|
68
|
+
if (result.response !== undefined && result.response.timestamp) {
|
|
69
|
+
const timestamp = new Date(result.response.timestamp);
|
|
70
|
+
// Only set if it's a valid date
|
|
71
|
+
if (!isNaN(timestamp.getTime())) {
|
|
72
|
+
result.response.timestamp = timestamp;
|
|
73
|
+
}
|
|
38
74
|
}
|
|
39
75
|
return result;
|
|
40
76
|
}
|
|
41
77
|
|
|
42
|
-
doStream(_options:
|
|
43
|
-
stream: any;
|
|
44
|
-
request?: { body?: unknown };
|
|
45
|
-
response?: { headers?: SharedV2Headers };
|
|
46
|
-
}> {
|
|
78
|
+
doStream(_options: LanguageModelV3CallOptions): PromiseLike<LanguageModelV3StreamResult> {
|
|
47
79
|
throw ApplicationFailure.nonRetryable('Streaming not supported.');
|
|
48
80
|
}
|
|
49
81
|
}
|
|
50
82
|
|
|
83
|
+
/**
|
|
84
|
+
* An embedding model implementation that delegates embedding generation to Temporal activities.
|
|
85
|
+
* This allows workflows to generate embeddings through the Temporal execution model.
|
|
86
|
+
*
|
|
87
|
+
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
88
|
+
*/
|
|
89
|
+
export class TemporalEmbeddingModel implements EmbeddingModelV3 {
|
|
90
|
+
readonly specificationVersion = 'v3';
|
|
91
|
+
readonly provider = 'temporal';
|
|
92
|
+
/**
|
|
93
|
+
* Undefined to let the underlying provider handle chunking, as it knows its own limits.
|
|
94
|
+
*/
|
|
95
|
+
readonly maxEmbeddingsPerCall = undefined;
|
|
96
|
+
/**
|
|
97
|
+
* Indicates the underlying embedding model API can handle concurrent requests.
|
|
98
|
+
* Set to true since we delegate to the actual provider which manages its own concurrency.
|
|
99
|
+
*/
|
|
100
|
+
readonly supportsParallelCalls = true;
|
|
101
|
+
|
|
102
|
+
constructor(
|
|
103
|
+
readonly modelId: string,
|
|
104
|
+
readonly options?: ActivityOptions
|
|
105
|
+
) {}
|
|
106
|
+
|
|
107
|
+
async doEmbed(options: EmbeddingModelV3CallOptions): Promise<EmbeddingModelV3Result> {
|
|
108
|
+
const activities = workflow.proxyActivities({
|
|
109
|
+
startToCloseTimeout: '10 minutes',
|
|
110
|
+
...this.options,
|
|
111
|
+
});
|
|
112
|
+
const result = await activities.invokeEmbeddingModel!({
|
|
113
|
+
modelId: this.modelId,
|
|
114
|
+
values: options.values,
|
|
115
|
+
providerOptions: options.providerOptions,
|
|
116
|
+
headers: options.headers,
|
|
117
|
+
// Note: abortSignal is not serializable, Temporal's cancellation handles this
|
|
118
|
+
});
|
|
119
|
+
if (result === undefined) {
|
|
120
|
+
throw ApplicationFailure.nonRetryable('Received undefined response from embedding model activity.');
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
51
126
|
/**
|
|
52
127
|
* A Temporal-specific provider implementation that creates AI models which execute
|
|
53
128
|
* through Temporal activities. This provider integrates AI SDK models with Temporal's
|
|
@@ -55,18 +130,30 @@ export class TemporalLanguageModel implements LanguageModelV2 {
|
|
|
55
130
|
*
|
|
56
131
|
* @experimental The AI SDK integration is an experimental feature; APIs may change without notice.
|
|
57
132
|
*/
|
|
58
|
-
export class TemporalProvider implements
|
|
59
|
-
|
|
133
|
+
export class TemporalProvider implements ProviderV3 {
|
|
134
|
+
readonly specificationVersion = 'v3';
|
|
60
135
|
|
|
61
|
-
|
|
136
|
+
constructor(readonly options?: TemporalProviderOptions) {}
|
|
137
|
+
|
|
138
|
+
imageModel(_modelId: string): ImageModelV3 {
|
|
62
139
|
throw new Error('Not implemented');
|
|
63
140
|
}
|
|
64
141
|
|
|
65
|
-
languageModel(modelId: string):
|
|
66
|
-
return new TemporalLanguageModel(modelId,
|
|
142
|
+
languageModel(modelId: string): LanguageModelV3 {
|
|
143
|
+
return new TemporalLanguageModel(modelId, {
|
|
144
|
+
...this.options?.default,
|
|
145
|
+
...this.options?.languageModel,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
embeddingModel(modelId: string): EmbeddingModelV3 {
|
|
150
|
+
return new TemporalEmbeddingModel(modelId, {
|
|
151
|
+
...this.options?.default,
|
|
152
|
+
...this.options?.embeddingModel,
|
|
153
|
+
});
|
|
67
154
|
}
|
|
68
155
|
|
|
69
|
-
|
|
156
|
+
transcriptionModel(_modelId: string): TranscriptionModelV3 {
|
|
70
157
|
throw new Error('Not implemented');
|
|
71
158
|
}
|
|
72
159
|
}
|