Skip to content

Commit 092c2ba

Browse files
Add ai compatibility tests and preserve sdk behavior
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent af510e6 commit 092c2ba

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

docs/tasks/schemaTask.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ await myTask.trigger({ name: "Alice", age: 30, dob: "2020-01-01" }); // this is
8080

8181
The `ai.tool` function allows you to create an AI tool from an existing `schemaTask` to use with the Vercel [AI SDK](https://vercel.com/docs/ai-sdk):
8282

83+
> `@trigger.dev/ai` is the recommended import path. For backwards compatibility,
84+
> `@trigger.dev/sdk/ai` continues to work.
85+
8386
```ts
8487
import { ai } from "@trigger.dev/ai";
8588
import { schemaTask } from "@trigger.dev/sdk";

packages/ai/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"build": "tshy && pnpm run update-version",
3434
"dev": "tshy --watch",
3535
"typecheck": "tsc --noEmit",
36-
"test": "vitest",
36+
"test": "vitest --exclude \"**/.tshy-build/**\"",
3737
"update-version": "tsx ../../scripts/updateVersion.ts",
3838
"check-exports": "attw --pack ."
3939
},

packages/ai/src/ai.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ describe("ai helper", function () {
7373
}).toThrowError("task has no schema");
7474
});
7575

76-
it("returns undefined for current tool options outside task execution context", function () {
77-
expect(ai.currentToolOptions()).toBeUndefined();
76+
it("throws for current tool options outside task execution context", function () {
77+
expect(function () {
78+
ai.currentToolOptions();
79+
}).toThrowError("Method not implemented.");
7880
});
7981
});

packages/ai/src/ai.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,7 @@ function toolFromTask<
8989
}
9090

9191
function getToolOptionsFromMetadata(): ToolCallExecutionOptions | undefined {
92-
let tool: unknown;
93-
try {
94-
tool = runMetadata.getKey(METADATA_KEY);
95-
} catch {
96-
return undefined;
97-
}
98-
92+
const tool = runMetadata.getKey(METADATA_KEY);
9993
if (!tool) {
10094
return undefined;
10195
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { describe, expect, it } from "vitest";
2+
import { z } from "zod";
3+
import { ai } from "./ai.js";
4+
import type { TaskWithSchema } from "@trigger.dev/core/v3";
5+
6+
describe("@trigger.dev/sdk/ai compatibility", function () {
7+
it("creates a tool from a schema task and executes triggerAndWait", async function () {
8+
let receivedInput: unknown = undefined;
9+
10+
const fakeTask = {
11+
id: "fake-task",
12+
description: "A fake task",
13+
schema: z.object({
14+
name: z.string(),
15+
}),
16+
triggerAndWait: function (payload: { name: string }) {
17+
receivedInput = payload;
18+
const resultPromise = Promise.resolve({
19+
ok: true,
20+
id: "run_123",
21+
taskIdentifier: "fake-task",
22+
output: {
23+
greeting: `Hello ${payload.name}`,
24+
},
25+
});
26+
27+
return Object.assign(resultPromise, {
28+
unwrap: async function () {
29+
return {
30+
greeting: `Hello ${payload.name}`,
31+
};
32+
},
33+
});
34+
},
35+
} as unknown as TaskWithSchema<
36+
"fake-task",
37+
z.ZodObject<{ name: z.ZodString }>,
38+
{ greeting: string }
39+
>;
40+
41+
const tool = ai.tool(fakeTask);
42+
const result = await tool.execute?.(
43+
{
44+
name: "Ada",
45+
},
46+
undefined as never
47+
);
48+
49+
expect(receivedInput).toEqual({
50+
name: "Ada",
51+
});
52+
expect(result).toEqual({
53+
greeting: "Hello Ada",
54+
});
55+
});
56+
57+
it("throws when converting tasks without schema", function () {
58+
const fakeTask = {
59+
id: "no-schema",
60+
description: "No schema task",
61+
schema: undefined,
62+
triggerAndWait: async function () {
63+
return {
64+
unwrap: async function () {
65+
return {};
66+
},
67+
};
68+
},
69+
} as unknown as TaskWithSchema<"no-schema", undefined, unknown>;
70+
71+
expect(function () {
72+
ai.tool(fakeTask);
73+
}).toThrowError("task has no schema");
74+
});
75+
76+
it("preserves currentToolOptions behavior outside task execution", function () {
77+
expect(function () {
78+
ai.currentToolOptions();
79+
}).toThrowError("Method not implemented.");
80+
});
81+
});

0 commit comments

Comments
 (0)