repair.ts
1,092 bytes
| 1 | import type { LlmMessage } from '../llm-client'; |
|---|---|
| 2 | |
| 3 | /** |
| 4 | * JSON repair prompt. |
| 5 | * |
| 6 | * Sent as a single retry when a model's structured output failed to parse or |
| 7 | * validate. We echo back the raw response and the precise validation error and |
| 8 | * ask only for corrected JSON - cheaper and more reliable than re-running the |
| 9 | * whole stage. |
| 10 | */ |
| 11 | export function buildRepairPrompt(rawResponse: string, validationError: string): LlmMessage[] { |
| 12 | return [ |
| 13 | { |
| 14 | role: 'system', |
| 15 | content: |
| 16 | 'You are a strict JSON fixer. You will be given a malformed or invalid response ' + |
| 17 | 'and the reason it failed validation. Return ONLY the corrected JSON object that ' + |
| 18 | 'satisfies the requirement - no prose, no code fences. Preserve all of the ' + |
| 19 | 'original content and meaning; change only what is needed to make it valid.', |
| 20 | }, |
| 21 | { |
| 22 | role: 'user', |
| 23 | content: |
| 24 | `The following response was invalid.\n\nValidation error: ${validationError}\n\n` + |
| 25 | `Original response:\n${rawResponse}\n\n` + |
| 26 | 'Return the corrected JSON now.', |
| 27 | }, |
| 28 | ]; |
| 29 | } |
| 30 | |