11import { z } from "zod" ;
2- import { generateText , jsonSchema , zodSchema } from "ai" ;
3- import {
4- Context ,
5- DurableContext ,
6- TaskWorkflowDeclaration ,
7- } from "@hatchet-dev/typescript-sdk" ;
2+ import { generateText , zodSchema } from "ai" ;
3+ import { TaskWorkflowDeclaration } from "@hatchet-dev/typescript-sdk" ;
84import { Pickaxe , Registerable } from "./pickaxe" ;
9- import jsonSchemaToZod from "json-schema-to-zod" ;
105
116export interface ToolDeclaration <
127 InputSchema extends z . ZodType ,
@@ -145,14 +140,12 @@ export class Toolbox<T extends ReadonlyArray<ToolDeclaration<any, any>>>
145140 * Uses the language model to choose up to `maxTools` tools from this toolbox that best satisfy
146141 * the provided prompt. Only the selection step is performed—no tool execution happens here.
147142 *
148- * @param ctx - Durable context provided by the Hatchet runtime.
149143 * @param prompt - Natural-language description of what the caller wants to achieve.
150144 * @param [maxTools] - Optional upper bound on how many tools may be selected (defaults to 1).
151145 *
152146 * @returns An array containing the chosen tool names together with the generated input arguments.
153147 */
154148 async pick (
155- ctx : DurableContext < any > | Context < any > ,
156149 { prompt, maxTools } : PickInput
157150 ) {
158151 const result = await pickToolFactory ( this . client ) . run ( {
@@ -177,42 +170,43 @@ export class Toolbox<T extends ReadonlyArray<ToolDeclaration<any, any>>>
177170 * tools were selected.
178171 *
179172 * @typeParam R - Inferred map of tool names to transformer functions derived from the toolbox.
180- * @param ctx - Durable context provided by the Hatchet runtime.
181173 * @param opts - Same input accepted by `pick`; the `maxTools` property determines the shape of the response.
182174 */
183175 async pickAndRun < R extends TransformersFor < T > > (
184- ctx : DurableContext < any > | Context < any > ,
185176 opts : Omit < PickInput , "maxTools" > & { maxTools ?: undefined }
186177 ) : Promise < ToolResultMap < T > > ;
187178
188179 async pickAndRun < R extends TransformersFor < T > > (
189- ctx : DurableContext < any > | Context < any > ,
190180 opts : PickInput & { maxTools : 1 }
191181 ) : Promise < ToolResultMap < T > > ;
192182
193183 // Overload: `maxTools` > 1 ➜ array of results
194184 async pickAndRun < R extends TransformersFor < T > > (
195- ctx : DurableContext < any > | Context < any > ,
196185 opts : PickInput & { maxTools : number }
197186 ) : Promise < ToolResultMap < T > [ ] > ;
198187
199188 // Implementation
200189 async pickAndRun < R extends TransformersFor < T > > (
201- ctx : DurableContext < any > | Context < any > ,
202190 opts : PickInput
203191 ) : Promise < any > {
204192 // 1) pick tools
205- const picked = await this . pick ( ctx , opts ) ;
193+ const picked = await this . pick ( opts ) ;
206194
207195 // 2) run them
208- const results = await ctx . bulkRunChildren (
209- picked . map ( ( { name, input } ) => ( { workflow : name , input } ) )
196+ const results = await Promise . all (
197+ picked . map ( async ( { name, input } ) => {
198+ const tool = this . props . tools . find ( t => t . name === name ) ;
199+ if ( ! tool ) {
200+ throw new Error ( `Tool "${ name } " not found in toolbox` ) ;
201+ }
202+ return await tool . run ( input ) ;
203+ } )
210204 ) ;
211205
212206 // 3) zip back into the correctly typed union
213207 const zipped = picked . map ( ( { name, input } , i ) => ( {
214208 name,
215- output : results [ i ] [ name ] , // FIXME: this is a hack to get the output of the tool
209+ output : results [ i ] ,
216210 args : input ,
217211 } ) ) as ToolResultMap < T > [ ] ;
218212
0 commit comments