|
1 | 1 | import { RegisteredTool } from "../types"; |
| 2 | +import { z } from "zod"; |
| 3 | + |
| 4 | +// Detect if something is a Zod schema (has _def and parse methods) |
| 5 | +function isZodSchema(schema: any): boolean { |
| 6 | + return ( |
| 7 | + schema && |
| 8 | + typeof schema === "object" && |
| 9 | + "_def" in schema && |
| 10 | + typeof schema.parse === "function" |
| 11 | + ); |
| 12 | +} |
| 13 | + |
| 14 | +// Detect if it's shorthand Zod syntax (object with z.* values) |
| 15 | +function isShorthandZodSyntax(schema: any): boolean { |
| 16 | + if (!schema || typeof schema !== "object" || Array.isArray(schema)) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + // Check if any value is a Zod schema |
| 21 | + return Object.values(schema).some((value) => isZodSchema(value)); |
| 22 | +} |
2 | 23 |
|
3 | 24 | export function addContextParameterToTool( |
4 | 25 | tool: RegisteredTool, |
5 | 26 | ): RegisteredTool { |
6 | | - if (!tool.inputSchema) { |
7 | | - tool.inputSchema = { |
| 27 | + // Create a shallow copy of the tool to avoid modifying the original |
| 28 | + const modifiedTool = { ...tool }; |
| 29 | + |
| 30 | + if (!modifiedTool.inputSchema) { |
| 31 | + modifiedTool.inputSchema = { |
8 | 32 | type: "object", |
9 | 33 | properties: {}, |
10 | 34 | required: [], |
11 | 35 | }; |
12 | 36 | } |
13 | 37 |
|
| 38 | + // Check if context already exists in JSON Schema format |
| 39 | + if (modifiedTool.inputSchema.properties?.context) { |
| 40 | + // Context already exists, don't override it |
| 41 | + return modifiedTool; |
| 42 | + } |
| 43 | + |
| 44 | + // Handle Zod z.object() schemas |
| 45 | + if (isZodSchema(modifiedTool.inputSchema)) { |
| 46 | + // Check if context already exists in Zod schema shape |
| 47 | + if ( |
| 48 | + modifiedTool.inputSchema.shape && |
| 49 | + "context" in modifiedTool.inputSchema.shape |
| 50 | + ) { |
| 51 | + return modifiedTool; |
| 52 | + } |
| 53 | + // It's a Zod schema, augment it with context |
| 54 | + const contextSchema = z.object({ |
| 55 | + context: z |
| 56 | + .string() |
| 57 | + .describe( |
| 58 | + "Describe why you are calling this tool and how it fits into your overall task", |
| 59 | + ), |
| 60 | + }); |
| 61 | + |
| 62 | + // Use extend to add context to the schema |
| 63 | + if (typeof modifiedTool.inputSchema.extend === "function") { |
| 64 | + modifiedTool.inputSchema = modifiedTool.inputSchema.extend( |
| 65 | + contextSchema.shape, |
| 66 | + ); |
| 67 | + } else if (typeof modifiedTool.inputSchema.augment === "function") { |
| 68 | + modifiedTool.inputSchema = |
| 69 | + modifiedTool.inputSchema.augment(contextSchema); |
| 70 | + } else { |
| 71 | + // Fallback: merge with new z.object |
| 72 | + modifiedTool.inputSchema = contextSchema.merge(modifiedTool.inputSchema); |
| 73 | + } |
| 74 | + |
| 75 | + return modifiedTool; |
| 76 | + } |
| 77 | + |
| 78 | + // Handle shorthand Zod syntax { a: z.number(), b: z.string() } |
| 79 | + if (isShorthandZodSyntax(modifiedTool.inputSchema)) { |
| 80 | + // Check if context already exists in shorthand syntax |
| 81 | + if ("context" in modifiedTool.inputSchema) { |
| 82 | + return modifiedTool; |
| 83 | + } |
| 84 | + |
| 85 | + // Create a new Zod schema with context |
| 86 | + const contextField = z |
| 87 | + .string() |
| 88 | + .describe( |
| 89 | + "Describe why you are calling this tool and how it fits into your overall task", |
| 90 | + ); |
| 91 | + |
| 92 | + // Create new z.object with context and all original fields |
| 93 | + modifiedTool.inputSchema = z.object({ |
| 94 | + context: contextField, |
| 95 | + ...modifiedTool.inputSchema, |
| 96 | + }); |
| 97 | + |
| 98 | + return modifiedTool; |
| 99 | + } |
| 100 | + |
| 101 | + // Handle regular JSON Schema format |
14 | 102 | // Add context property if it doesn't exist |
15 | | - if (!tool.inputSchema.properties?.context) { |
16 | | - tool.inputSchema.properties.context = { |
| 103 | + if (!modifiedTool.inputSchema.properties?.context) { |
| 104 | + // Deep copy the inputSchema for JSON Schema to avoid mutations |
| 105 | + modifiedTool.inputSchema = JSON.parse( |
| 106 | + JSON.stringify(modifiedTool.inputSchema), |
| 107 | + ); |
| 108 | + |
| 109 | + // Ensure properties object exists before trying to set context |
| 110 | + if (!modifiedTool.inputSchema.properties) { |
| 111 | + modifiedTool.inputSchema.properties = {}; |
| 112 | + } |
| 113 | + |
| 114 | + modifiedTool.inputSchema.properties.context = { |
17 | 115 | type: "string", |
18 | 116 | description: |
19 | 117 | "Describe why you are calling this tool and how it fits into your overall task", |
20 | 118 | }; |
21 | 119 |
|
22 | 120 | // Add context to required array if it exists |
23 | 121 | if ( |
24 | | - Array.isArray(tool.inputSchema.required) && |
25 | | - !tool.inputSchema.required.includes("context") |
| 122 | + Array.isArray(modifiedTool.inputSchema.required) && |
| 123 | + !modifiedTool.inputSchema.required.includes("context") |
26 | 124 | ) { |
27 | | - tool.inputSchema.required.push("context"); |
28 | | - } else if (!tool.inputSchema.required) { |
29 | | - tool.inputSchema.required = ["context"]; |
| 125 | + modifiedTool.inputSchema.required.push("context"); |
| 126 | + } else if (!modifiedTool.inputSchema.required) { |
| 127 | + modifiedTool.inputSchema.required = ["context"]; |
30 | 128 | } |
31 | 129 | } |
32 | 130 |
|
33 | | - return tool; |
| 131 | + return modifiedTool; |
34 | 132 | } |
35 | 133 |
|
36 | 134 | export function addContextParameterToTools( |
37 | 135 | tools: RegisteredTool[], |
38 | 136 | ): RegisteredTool[] { |
39 | | - return tools.map((tool) => addContextParameterToTool(tool)); |
| 137 | + return tools.map((tool) => { |
| 138 | + // Skip get_more_tools - it has its own special context parameter |
| 139 | + if ((tool as any).name === "get_more_tools") { |
| 140 | + return tool; |
| 141 | + } |
| 142 | + return addContextParameterToTool(tool); |
| 143 | + }); |
40 | 144 | } |
0 commit comments