Skip to content

Commit 3d17ce5

Browse files
authored
feat: AI SDK 5.0 support (#2396)
* feat: AI SDK 5.0 support * Add changeset
1 parent a76905e commit 3d17ce5

File tree

9 files changed

+210
-121
lines changed

9 files changed

+210
-121
lines changed

.changeset/famous-clocks-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
feat: Support AI SDK 5.0. `ai.tool` now accepts either a schemaTask or a task with a provided jsonSchema

packages/core/src/v3/types/tasks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ export interface Task<TIdentifier extends string, TInput = void, TOutput = any>
547547

548548
description?: string;
549549

550+
jsonSchema?: JSONSchema;
551+
550552
/**
551553
* Trigger a task with the given payload, and continue without waiting for the result. If you want to wait for the result, use `triggerAndWait`. Returns the id of the triggered task run.
552554
* @param payload

packages/trigger-sdk/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"@types/slug": "^5.0.3",
6969
"@types/uuid": "^9.0.0",
7070
"@types/ws": "^8.5.3",
71-
"ai": "^4.2.0",
71+
"ai": "^5.0.0",
7272
"encoding": "^0.1.13",
7373
"rimraf": "^3.0.2",
7474
"tshy": "^3.0.2",
@@ -78,7 +78,7 @@
7878
},
7979
"peerDependencies": {
8080
"zod": "^3.0.0 || ^4.0.0",
81-
"ai": "^4.2.0"
81+
"ai": "^4.2.0 || ^5.0.0"
8282
},
8383
"peerDependenciesMeta": {
8484
"ai": {

packages/trigger-sdk/src/v3/ai.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import {
2+
AnyTask,
23
isSchemaZodEsque,
4+
Task,
35
type inferSchemaIn,
46
type TaskSchema,
57
type TaskWithSchema,
68
} from "@trigger.dev/core/v3";
7-
import { jsonSchema, Schema, tool, ToolExecutionOptions, zodSchema } from "ai";
9+
import { dynamicTool, jsonSchema, JSONSchema7, Schema, Tool, ToolCallOptions, zodSchema } from "ai";
810
import { metadata } from "./metadata.js";
911

1012
const METADATA_KEY = "tool.execute.options";
1113

12-
export type ToolCallExecutionOptions = Omit<ToolExecutionOptions, "abortSignal">;
14+
export type ToolCallExecutionOptions = Omit<ToolCallOptions, "abortSignal">;
1315

1416
type ToolResultContent = Array<
1517
| {
@@ -27,25 +29,43 @@ export type ToolOptions<TResult> = {
2729
experimental_toToolResultContent?: (result: TResult) => ToolResultContent;
2830
};
2931

32+
function toolFromTask<TIdentifier extends string, TInput = void, TOutput = unknown>(
33+
task: Task<TIdentifier, TInput, TOutput>,
34+
options?: ToolOptions<TOutput>
35+
): Tool<TInput, TOutput>;
3036
function toolFromTask<
3137
TIdentifier extends string,
3238
TTaskSchema extends TaskSchema | undefined = undefined,
3339
TOutput = unknown,
34-
>(task: TaskWithSchema<TIdentifier, TTaskSchema, TOutput>, options?: ToolOptions<TOutput>) {
35-
if (!task.schema) {
40+
>(
41+
task: TaskWithSchema<TIdentifier, TTaskSchema, TOutput>,
42+
options?: ToolOptions<TOutput>
43+
): Tool<inferSchemaIn<TTaskSchema>, TOutput>;
44+
function toolFromTask<
45+
TIdentifier extends string,
46+
TTaskSchema extends TaskSchema | undefined = undefined,
47+
TInput = void,
48+
TOutput = unknown,
49+
>(
50+
task: TaskWithSchema<TIdentifier, TTaskSchema, TOutput> | Task<TIdentifier, TInput, TOutput>,
51+
options?: ToolOptions<TOutput>
52+
): TTaskSchema extends TaskSchema
53+
? Tool<inferSchemaIn<TTaskSchema>, TOutput>
54+
: Tool<TInput, TOutput> {
55+
if (("schema" in task && !task.schema) || ("jsonSchema" in task && !task.jsonSchema)) {
3656
throw new Error(
37-
"Cannot convert schemaTask to a tool because the task has no schema. Make sure the schema used in the task is either zod, arktype, or another supported schema."
57+
"Cannot convert this task to to a tool because the task has no schema. Make sure to either use schemaTask or a task with an input jsonSchema."
3858
);
3959
}
4060

41-
return tool({
61+
const toolDefinition = dynamicTool({
4262
description: task.description,
43-
parameters: convertTaskSchemaToToolParameters(task.schema),
44-
execute: async (args, options) => {
63+
inputSchema: convertTaskSchemaToToolParameters(task),
64+
execute: async (input, options) => {
4565
const serializedOptions = options ? JSON.parse(JSON.stringify(options)) : undefined;
4666

4767
return await task
48-
.triggerAndWait(args, {
68+
.triggerAndWait(input as inferSchemaIn<TTaskSchema>, {
4969
metadata: {
5070
[METADATA_KEY]: serializedOptions,
5171
},
@@ -54,6 +74,10 @@ function toolFromTask<
5474
},
5575
...options,
5676
});
77+
78+
return toolDefinition as TTaskSchema extends TaskSchema
79+
? Tool<inferSchemaIn<TTaskSchema>, TOutput>
80+
: Tool<TInput, TOutput>;
5781
}
5882

5983
function getToolOptionsFromMetadata(): ToolCallExecutionOptions | undefined {
@@ -64,21 +88,27 @@ function getToolOptionsFromMetadata(): ToolCallExecutionOptions | undefined {
6488
return tool as ToolCallExecutionOptions;
6589
}
6690

67-
function convertTaskSchemaToToolParameters<TTaskSchema extends TaskSchema>(
68-
schema: TTaskSchema
69-
): Schema<inferSchemaIn<TTaskSchema>> {
70-
// If TaskSchema is ZodEsque, use ai.zodSchema to convert it to a Schema
71-
if (isSchemaZodEsque(schema)) {
72-
return zodSchema(schema as any);
91+
function convertTaskSchemaToToolParameters(
92+
task: AnyTask | TaskWithSchema<any, any, any>
93+
): Schema<unknown> {
94+
if ("schema" in task) {
95+
// If TaskSchema is ArkTypeEsque, use ai.jsonSchema to convert it to a Schema
96+
if ("toJsonSchema" in task.schema && typeof task.schema.toJsonSchema === "function") {
97+
return jsonSchema((task.schema as any).toJsonSchema());
98+
}
99+
100+
// If TaskSchema is ZodEsque, use ai.zodSchema to convert it to a Schema
101+
if (isSchemaZodEsque(task.schema)) {
102+
return zodSchema(task.schema as any);
103+
}
73104
}
74105

75-
// If TaskSchema is ArkTypeEsque, use ai.jsonSchema to convert it to a Schema
76-
if ("toJsonSchema" in schema && typeof schema.toJsonSchema === "function") {
77-
return jsonSchema((schema as any).toJsonSchema());
106+
if ("jsonSchema" in task) {
107+
return jsonSchema(task.jsonSchema as JSONSchema7);
78108
}
79109

80110
throw new Error(
81-
"Cannot convert schemaTask to a tool. Make sure the schema used in the task is either zod, arktype, or another supported schema."
111+
"Cannot convert task to a tool. Make sure to use a task with a schema or jsonSchema."
82112
);
83113
}
84114

packages/trigger-sdk/src/v3/shared.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export function createTask<
161161
const task: Task<TIdentifier, TInput, TOutput> = {
162162
id: params.id,
163163
description: params.description,
164+
jsonSchema: params.jsonSchema,
164165
trigger: async (payload, options) => {
165166
return await trigger_internal<RunTypes<TIdentifier, TInput, TOutput>>(
166167
"trigger()",

0 commit comments

Comments
 (0)