Spaces:
Running

ManimCat / src /workflow /problem-framing /format-repair.ts
Bin29's picture
Deploy 5548f84: pre-sandbox v2.0.0-beta.1 snapshot
10dd572
Raw History Blame Contribute Delete
1.26 kB
import {
parseProblemFramingResponse,
ProblemFramingFormatError,
type ProblemFramingPlan,
} from './parser'
import type { PromptLocale } from '../../types'
interface ProblemFramingFormatRepairOptions {
content: string
locale: PromptLocale
requireDesignFields?: boolean
maxRepairAttempts: number
repair: (input: {
content: string
error: string
attempt: number
}) => Promise<string>
}
export async function parseProblemFramingWithRepair(
options: ProblemFramingFormatRepairOptions,
): Promise<ProblemFramingPlan> {
let content = options.content
let lastError: ProblemFramingFormatError | undefined
for (let attempt = 0; attempt <= options.maxRepairAttempts; attempt += 1) {
try {
return parseProblemFramingResponse(content, options.locale, options.requireDesignFields)
} catch (error) {
if (!(error instanceof ProblemFramingFormatError)) {
throw error
}
lastError = error
if (attempt >= options.maxRepairAttempts) {
break
}
content = await options.repair({
content,
error: error.message,
attempt: attempt + 1,
})
}
}
throw lastError || new ProblemFramingFormatError('Unable to parse problem framing response')
}