11import {
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" ;
810import { metadata } from "./metadata.js" ;
911
1012const METADATA_KEY = "tool.execute.options" ;
1113
12- export type ToolCallExecutionOptions = Omit < ToolExecutionOptions , "abortSignal" > ;
14+ export type ToolCallExecutionOptions = Omit < ToolCallOptions , "abortSignal" > ;
1315
1416type 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 > ;
3036function 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
5983function 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
0 commit comments