(
provider: MemoryProvider,
systemPrompt: string,
userPrompt: string,
validator: (response: string) => { valid: boolean; errors?: string[] },
maxRetries = 1,
)
| 5 | IMPORTANT: Your previous response was invalid. Please ensure your output strictly follows the required XML format. Every required field must be present with valid values.`; |
| 6 | |
| 7 | export async function compressWithRetry( |
| 8 | provider: MemoryProvider, |
| 9 | systemPrompt: string, |
| 10 | userPrompt: string, |
| 11 | validator: (response: string) => { valid: boolean; errors?: string[] }, |
| 12 | maxRetries = 1, |
| 13 | ): Promise<{ response: string; retried: boolean }> { |
| 14 | const first = await provider.compress(systemPrompt, userPrompt); |
| 15 | const result = validator(first); |
| 16 | if (result.valid) return { response: first, retried: false }; |
| 17 | |
| 18 | for (let i = 0; i < maxRetries; i++) { |
| 19 | const retry = await provider.compress( |
| 20 | systemPrompt + STRICTER_SUFFIX, |
| 21 | userPrompt, |
| 22 | ); |
| 23 | const retryResult = validator(retry); |
| 24 | if (retryResult.valid) return { response: retry, retried: true }; |
| 25 | } |
| 26 | |
| 27 | return { response: first, retried: true }; |
| 28 | } |
no test coverage detected