| 39 | } |
| 40 | |
| 41 | export function createMockToolHandlerContext(): { |
| 42 | ctx: ToolHandlerContext; |
| 43 | result: MockToolHandlerResult; |
| 44 | run: <T>(fn: () => Promise<T>) => Promise<T>; |
| 45 | } { |
| 46 | const events: AnyFragment[] = []; |
| 47 | const attachments: ImageAttachment[] = []; |
| 48 | const session = createRenderSession('text'); |
| 49 | const ctx: ToolHandlerContext = { |
| 50 | emit: (fragment: AnyFragment) => { |
| 51 | events.push(fragment); |
| 52 | session.emit(fragment); |
| 53 | }, |
| 54 | attach: (image) => { |
| 55 | attachments.push(image); |
| 56 | session.attach(image); |
| 57 | }, |
| 58 | }; |
| 59 | const resultObj: MockToolHandlerResult = { |
| 60 | events, |
| 61 | attachments, |
| 62 | get nextStepParams() { |
| 63 | return ctx.nextStepParams; |
| 64 | }, |
| 65 | text() { |
| 66 | return renderCliTextTranscript({ |
| 67 | items: [], |
| 68 | structuredOutput: ctx.structuredOutput, |
| 69 | nextSteps: ctx.nextSteps, |
| 70 | }); |
| 71 | }, |
| 72 | isError() { |
| 73 | return ctx.structuredOutput?.result.didError === true; |
| 74 | }, |
| 75 | }; |
| 76 | return { |
| 77 | ctx, |
| 78 | result: resultObj, |
| 79 | run: async <T>(fn: () => Promise<T>): Promise<T> => { |
| 80 | return handlerContextStorage.run(ctx, fn); |
| 81 | }, |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | export async function runToolLogic<T>(logic: () => Promise<T>): Promise<{ |
| 86 | response: T; |