| 1562 | |
| 1563 | /** @internal Exported for testing */ |
| 1564 | export function createStructuredOutputTool(input: { |
| 1565 | schema: Record<string, any> |
| 1566 | onSuccess: (output: unknown) => void |
| 1567 | }): AITool { |
| 1568 | // Remove $schema property if present (not needed for tool input) |
| 1569 | const { $schema: _, ...toolSchema } = input.schema |
| 1570 | |
| 1571 | return tool({ |
| 1572 | description: STRUCTURED_OUTPUT_DESCRIPTION, |
| 1573 | inputSchema: jsonSchema(toolSchema as JSONSchema7), |
| 1574 | async execute(args) { |
| 1575 | // AI SDK validates args against inputSchema before calling execute() |
| 1576 | input.onSuccess(args) |
| 1577 | return { |
| 1578 | output: "Structured output captured successfully.", |
| 1579 | title: "Structured Output", |
| 1580 | metadata: { valid: true }, |
| 1581 | } |
| 1582 | }, |
| 1583 | toModelOutput({ output }) { |
| 1584 | return { |
| 1585 | type: "text", |
| 1586 | value: output.output, |
| 1587 | } |
| 1588 | }, |
| 1589 | }) |
| 1590 | } |
| 1591 | const bashRegex = /!`([^`]+)`/g |
| 1592 | // Match [Image N] as single token, quoted strings, or non-space sequences |
| 1593 | const argsRegex = /(?:\[Image\s+\d+\]|"[^"]*"|'[^']*'|[^\s"']+)/gi |