|
| 1 | +# Design: Tool Execute Signature Fix |
| 2 | + |
| 3 | +## Root Cause |
| 4 | + |
| 5 | +The `createTool` function expects `execute` to receive a `ToolExecutionContext` object: |
| 6 | + |
| 7 | +```typescript |
| 8 | +execute: (context: ToolExecutionContext<TInput, TOutput, TContext>) => Promise<TOutput> |
| 9 | +``` |
| 10 | + |
| 11 | +Where `ToolExecutionContext` has this shape: |
| 12 | + |
| 13 | +```typescript |
| 14 | +interface ToolExecutionContext<TInput, TOutput, TContext> { |
| 15 | + context: z.infer<TInput>; // REQUIRED - the parsed input |
| 16 | + runtimeContext?: RuntimeContext; // OPTIONAL |
| 17 | + tracingContext?: TracingContext; // OPTIONAL |
| 18 | + writer?: StreamWriter; // OPTIONAL |
| 19 | + mastra?: Mastra; // OPTIONAL |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +## The Problem |
| 24 | + |
| 25 | +**Current (WRONG):** |
| 26 | + |
| 27 | +```typescript |
| 28 | +execute: async ({ context, writer, tracingContext }: { |
| 29 | + context: any; |
| 30 | + writer: any; // <-- Treated as REQUIRED |
| 31 | + tracingContext: any; // <-- Treated as REQUIRED |
| 32 | +}) => { ... } |
| 33 | +``` |
| 34 | + |
| 35 | +This explicit type annotation declares `writer` and `tracingContext` as required properties, conflicting with `ToolExecutionContext`. |
| 36 | + |
| 37 | +## Solution Options |
| 38 | + |
| 39 | +### Option A: Remove explicit type annotation (RECOMMENDED) |
| 40 | + |
| 41 | +```typescript |
| 42 | +execute: async ({ context, writer, tracingContext }) => { |
| 43 | + await writer?.write({ ... }); // Still use optional chaining |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +TypeScript will infer the correct types from `ToolExecutionContext`. |
| 48 | + |
| 49 | +### Option B: Mark properties as optional in annotation |
| 50 | + |
| 51 | +```typescript |
| 52 | +execute: async ({ context, writer, tracingContext }: { |
| 53 | + context: any; |
| 54 | + writer?: any; // <-- Add ? |
| 55 | + tracingContext?: any; // <-- Add ? |
| 56 | +}) => { ... } |
| 57 | +``` |
| 58 | + |
| 59 | +### Option C: Use intermediate variable |
| 60 | + |
| 61 | +```typescript |
| 62 | +execute: async (execContext) => { |
| 63 | + const { context, writer, tracingContext } = execContext; |
| 64 | + await writer?.write({ ... }); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Decision |
| 69 | + |
| 70 | +**Option A** - Remove explicit type annotations. |
| 71 | + |
| 72 | +- Cleanest solution |
| 73 | +- Lets TypeScript infer correct types |
| 74 | +- Requires fewest code changes |
| 75 | +- Already working in tools without explicit annotations |
0 commit comments