* Sanitize tools array by removing UI state and redundant fields
(tools: ToolInput[])
| 203 | * Sanitize tools array by removing UI state and redundant fields |
| 204 | */ |
| 205 | function sanitizeTools(tools: ToolInput[]): SanitizedTool[] { |
| 206 | return tools.map((tool): SanitizedTool => { |
| 207 | if (tool.type === 'custom-tool') { |
| 208 | // New reference format: minimal fields only |
| 209 | if (tool.customToolId && !tool.schema && !tool.code) { |
| 210 | return { |
| 211 | type: tool.type, |
| 212 | customToolId: tool.customToolId, |
| 213 | usageControl: tool.usageControl, |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Legacy inline format: include all fields |
| 218 | const sanitized: SanitizedTool = { |
| 219 | type: tool.type, |
| 220 | title: tool.title, |
| 221 | toolId: tool.toolId, |
| 222 | usageControl: tool.usageControl, |
| 223 | } |
| 224 | |
| 225 | // Include schema for inline format (legacy format) |
| 226 | if (tool.schema?.function) { |
| 227 | sanitized.schema = { |
| 228 | type: tool.schema.type || 'function', |
| 229 | function: { |
| 230 | name: tool.schema.function.name, |
| 231 | description: tool.schema.function.description, |
| 232 | parameters: tool.schema.function.parameters, |
| 233 | }, |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Include code for inline format (legacy format) |
| 238 | if (tool.code) { |
| 239 | sanitized.code = tool.code |
| 240 | } |
| 241 | |
| 242 | return sanitized |
| 243 | } |
| 244 | |
| 245 | const { isExpanded: _isExpanded, ...cleanTool } = tool |
| 246 | return cleanTool as SanitizedTool |
| 247 | }) |
| 248 | } |
| 249 | |
| 250 | function isToolInput(value: unknown): value is ToolInput { |
| 251 | return isRecordLike(value) && typeof value.type === 'string' |