Skip to content

Commit 1492cb7

Browse files
committed
Added AI tool tasks, descriptions to tasks
1 parent 04d3762 commit 1492cb7

File tree

15 files changed

+394
-90
lines changed

15 files changed

+394
-90
lines changed

apps/webapp/app/v3/services/createBackgroundWorker.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export async function createBackgroundTasks(
150150
runtimeEnvironmentId: worker.runtimeEnvironmentId,
151151
workerId: worker.id,
152152
slug: task.id,
153+
description: task.description,
153154
filePath: task.filePath,
154155
exportName: task.exportName,
155156
retryConfig: task.retry,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "BackgroundWorkerTask" ADD COLUMN "description" TEXT;

internal-packages/database/prisma/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,8 @@ model BackgroundWorkerTask {
16101610
id String @id @default(cuid())
16111611
slug String
16121612
1613+
description String?
1614+
16131615
friendlyId String @unique
16141616
16151617
filePath String

packages/core/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,18 @@
204204
"nanoid": "^3.3.4",
205205
"socket.io-client": "4.7.5",
206206
"superjson": "^2.2.1",
207-
"zod": "3.22.3",
208207
"zod-error": "1.5.0",
209-
"zod-validation-error": "^1.5.0"
208+
"zod-validation-error": "^1.5.0",
209+
"zod": "3.22.3"
210210
},
211211
"devDependencies": {
212+
"@ai-sdk/provider-utils": "^1.0.22",
212213
"@arethetypeswrong/cli": "^0.15.4",
213214
"@epic-web/test-server": "^0.1.0",
214215
"@types/humanize-duration": "^3.27.1",
215216
"@types/node": "20.14.14",
216217
"@types/readable-stream": "^4.0.14",
218+
"ai": "^3.4.33",
217219
"defu": "^6.1.4",
218220
"esbuild": "^0.23.0",
219221
"rimraf": "^3.0.2",

packages/core/src/v3/schemas/resources.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { MachineConfig } from "./common.js";
44

55
export const TaskResource = z.object({
66
id: z.string(),
7+
description: z.string().optional(),
78
filePath: z.string(),
89
exportName: z.string(),
910
queue: QueueOptions.optional(),

packages/core/src/v3/schemas/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export const ScheduleMetadata = z.object({
149149

150150
const taskMetadata = {
151151
id: z.string(),
152+
description: z.string().optional(),
152153
queue: QueueOptions.optional(),
153154
retry: RetryOptions.optional(),
154155
machine: MachineConfig.optional(),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Prettify } from "./utils.js";
55
export * from "./utils.js";
66
export * from "./tasks.js";
77
export * from "./idempotencyKeys.js";
8+
export * from "./tools.js";
89

910
type ResolveEnvironmentVariablesOptions = {
1011
variables: Record<string, string> | Array<{ name: string; value: string }>;

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
import type { Schema as AISchema } from "ai";
2+
import { z } from "zod";
13
import { SerializableJson } from "../../schemas/json.js";
4+
import { TriggerApiRequestOptions } from "../apiClient/index.js";
25
import { RunTags } from "../schemas/api.js";
3-
import { QueueOptions } from "../schemas/schemas.js";
4-
import { IdempotencyKey } from "./idempotencyKeys.js";
56
import {
67
MachineCpu,
78
MachineMemory,
89
RetryOptions,
910
TaskMetadata,
1011
TaskRunContext,
1112
} from "../schemas/index.js";
13+
import { QueueOptions } from "../schemas/schemas.js";
14+
import { IdempotencyKey } from "./idempotencyKeys.js";
15+
import { AnySchemaParseFn, inferSchemaIn, inferSchemaOut, Schema } from "./schemas.js";
1216
import { Prettify } from "./utils.js";
13-
import { AnySchemaParseFn, inferSchemaOut, Schema } from "./schemas.js";
14-
import { TriggerApiRequestOptions } from "../apiClient/index.js";
17+
import { inferToolParameters, ToolTaskParameters } from "./tools.js";
1518

1619
type RequireOne<T, K extends keyof T> = {
1720
[X in Exclude<keyof T, K>]?: T[X];
@@ -150,6 +153,8 @@ type CommonTaskOptions<
150153
/** An id for your task. This must be unique inside your project and not change between versions. */
151154
id: TIdentifier;
152155

156+
description?: string;
157+
153158
/** The retry settings when an uncaught error is thrown.
154159
*
155160
* If omitted it will use the values in your `trigger.config.ts` file.
@@ -337,6 +342,15 @@ export type TaskWithSchemaOptions<
337342
schema?: TSchema;
338343
};
339344

345+
export type TaskWithToolOptions<
346+
TIdentifier extends string,
347+
TParameters extends ToolTaskParameters,
348+
TOutput = unknown,
349+
TInitOutput extends InitOutput = any,
350+
> = CommonTaskOptions<TIdentifier, inferToolParameters<TParameters>, TOutput, TInitOutput> & {
351+
parameters: TParameters;
352+
};
353+
340354
declare const __output: unique symbol;
341355
declare const __payload: unique symbol;
342356
type BrandRun<P, O> = { [__output]: O; [__payload]: P };
@@ -413,6 +427,9 @@ export interface Task<TIdentifier extends string, TInput = void, TOutput = any>
413427
* The id of the task.
414428
*/
415429
id: TIdentifier;
430+
431+
description?: string;
432+
416433
/**
417434
* 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.
418435
* @param payload
@@ -479,6 +496,26 @@ export interface Task<TIdentifier extends string, TInput = void, TOutput = any>
479496
batchTriggerAndWait: (items: Array<BatchItem<TInput>>) => Promise<BatchResult<TOutput>>;
480497
}
481498

499+
export interface TaskWithSchema<
500+
TIdentifier extends string,
501+
TSchema extends TaskSchema | undefined = undefined,
502+
TOutput = any,
503+
> extends Task<TIdentifier, inferSchemaIn<TSchema>, TOutput> {
504+
schema?: TSchema;
505+
}
506+
507+
export interface ToolTask<
508+
TIdentifier extends string,
509+
TParameters extends ToolTaskParameters,
510+
TOutput = any,
511+
> extends Task<TIdentifier, inferToolParameters<TParameters>, TOutput> {
512+
tool: {
513+
parameters: TParameters;
514+
description?: string;
515+
execute: (args: inferToolParameters<TParameters>) => Promise<TOutput>;
516+
};
517+
}
518+
482519
export type AnyTask = Task<string, any, any>;
483520

484521
export type TaskPayload<TTask extends AnyTask> = TTask extends Task<string, infer TInput, any>

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { z } from "zod";
2+
import type { Schema as AISchema } from "ai";
3+
import { Schema } from "./schemas.js";
4+
5+
export type ToolTaskParameters = z.ZodTypeAny | AISchema<any>;
6+
7+
export type inferToolParameters<PARAMETERS extends ToolTaskParameters> =
8+
PARAMETERS extends AISchema<any>
9+
? PARAMETERS["_type"]
10+
: PARAMETERS extends z.ZodTypeAny
11+
? z.infer<PARAMETERS>
12+
: never;
13+
14+
export function convertToolParametersToSchema<TToolParameters extends ToolTaskParameters>(
15+
toolParameters: TToolParameters
16+
): Schema {
17+
return toolParameters instanceof z.ZodSchema
18+
? toolParameters
19+
: convertAISchemaToTaskSchema(toolParameters);
20+
}
21+
22+
function convertAISchemaToTaskSchema(schema: AISchema<any>): Schema {
23+
return (payload: unknown) => {
24+
const result = schema.validate?.(payload);
25+
26+
if (!result) {
27+
throw new Error("Invalid payload");
28+
}
29+
30+
if (!result.success) {
31+
throw result.error;
32+
}
33+
34+
return result.value;
35+
};
36+
}

packages/trigger-sdk/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
"terminal-link": "^3.0.0",
5858
"ulid": "^2.3.0",
5959
"uuid": "^9.0.0",
60-
"ws": "^8.11.0",
61-
"zod": "3.22.3"
60+
"ws": "^8.11.0"
6261
},
6362
"devDependencies": {
6463
"@arethetypeswrong/cli": "^0.15.4",
@@ -67,12 +66,17 @@
6766
"@types/slug": "^5.0.3",
6867
"@types/uuid": "^9.0.0",
6968
"@types/ws": "^8.5.3",
69+
"ai": "^3.4.33",
7070
"encoding": "^0.1.13",
7171
"rimraf": "^3.0.2",
7272
"tshy": "^3.0.2",
7373
"tsx": "4.17.0",
7474
"typed-emitter": "^2.1.0",
75-
"typescript": "^5.5.4"
75+
"typescript": "^5.5.4",
76+
"zod": "3.22.3"
77+
},
78+
"peerDependencies": {
79+
"zod": "^3.0.0"
7680
},
7781
"engines": {
7882
"node": ">=18.20.0"

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ApiRequestOptions,
1212
BatchTaskRunExecutionResult,
1313
conditionallyImportPacket,
14+
convertToolParametersToSchema,
1415
createErrorTaskError,
1516
defaultRetryOptions,
1617
getSchemaParseFn,
@@ -32,6 +33,7 @@ import {
3233
import { IdempotencyKey, idempotencyKeys, isIdempotencyKey } from "./idempotencyKeys.js";
3334
import { PollOptions, runs } from "./runs.js";
3435
import { tracer } from "./tracer.js";
36+
import type { Schema as AISchema, CoreTool } from "ai";
3537

3638
import type {
3739
AnyRunHandle,
@@ -60,9 +62,14 @@ import type {
6062
TaskRunOptions,
6163
TaskRunResult,
6264
TaskSchema,
65+
TaskWithSchema,
6366
TaskWithSchemaOptions,
67+
TaskWithToolOptions,
68+
ToolTask,
69+
ToolTaskParameters,
6470
TriggerApiRequestOptions,
6571
} from "@trigger.dev/core/v3";
72+
import { z } from "zod";
6673

6774
export type {
6875
AnyRunHandle,
@@ -111,6 +118,7 @@ export function createTask<
111118

112119
const task: Task<TIdentifier, TInput, TOutput> = {
113120
id: params.id,
121+
description: params.description,
114122
trigger: async (payload, options) => {
115123
const taskMetadata = taskCatalog.getTaskManifest(params.id);
116124

@@ -183,6 +191,7 @@ export function createTask<
183191

184192
taskCatalog.registerTaskMetadata({
185193
id: params.id,
194+
description: params.description,
186195
queue: params.queue,
187196
retry: params.retry ? { ...defaultRetryOptions, ...params.retry } : undefined,
188197
machine: params.machine,
@@ -205,14 +214,39 @@ export function createTask<
205214
return task;
206215
}
207216

217+
export function createToolTask<
218+
TIdentifier extends string,
219+
TParameters extends ToolTaskParameters,
220+
TOutput = unknown,
221+
TInitOutput extends InitOutput = any,
222+
>(
223+
params: TaskWithToolOptions<TIdentifier, TParameters, TOutput, TInitOutput>
224+
): ToolTask<TIdentifier, TParameters, TOutput> {
225+
const task = createSchemaTask({
226+
...params,
227+
schema: convertToolParametersToSchema(params.parameters),
228+
});
229+
230+
return {
231+
...task,
232+
tool: {
233+
parameters: params.parameters,
234+
description: params.description,
235+
execute: async (args: any) => {
236+
return task.triggerAndWait(args).unwrap();
237+
},
238+
},
239+
};
240+
}
241+
208242
export function createSchemaTask<
209243
TIdentifier extends string,
210244
TSchema extends TaskSchema | undefined = undefined,
211245
TOutput = unknown,
212246
TInitOutput extends InitOutput = any,
213247
>(
214248
params: TaskWithSchemaOptions<TIdentifier, TSchema, TOutput, TInitOutput>
215-
): Task<TIdentifier, inferSchemaIn<TSchema>, TOutput> {
249+
): TaskWithSchema<TIdentifier, TSchema, TOutput> {
216250
const customQueue = params.queue
217251
? queue({
218252
name: params.queue?.name ?? `task/${params.id}`,
@@ -224,8 +258,10 @@ export function createSchemaTask<
224258
? getSchemaParseFn<inferSchemaIn<TSchema>>(params.schema)
225259
: undefined;
226260

227-
const task: Task<TIdentifier, inferSchemaIn<TSchema>, TOutput> = {
261+
const task: TaskWithSchema<TIdentifier, TSchema, TOutput> = {
228262
id: params.id,
263+
description: params.description,
264+
schema: params.schema,
229265
trigger: async (payload, options, requestOptions) => {
230266
const taskMetadata = taskCatalog.getTaskManifest(params.id);
231267

@@ -299,6 +335,7 @@ export function createSchemaTask<
299335

300336
taskCatalog.registerTaskMetadata({
301337
id: params.id,
338+
description: params.description,
302339
queue: params.queue,
303340
retry: params.retry ? { ...defaultRetryOptions, ...params.retry } : undefined,
304341
machine: params.machine,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
batchTriggerAndWait,
44
createTask,
55
createSchemaTask,
6+
createToolTask,
67
SubtaskUnwrapError,
78
trigger,
89
triggerAndPoll,
@@ -65,6 +66,8 @@ export const task = createTask;
6566

6667
export const schemaTask = createSchemaTask;
6768

69+
export const toolTask = createToolTask;
70+
6871
export const tasks = {
6972
trigger,
7073
triggerAndPoll,

0 commit comments

Comments
 (0)