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"

0 commit comments

Comments
 (0)