async function runWithRetry(
slug: string,
variables: Record<string, string>,
maxRetries = 3
) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await tracia.prompts.run(slug, variables);
} catch (error) {
if (error instanceof TraciaError) {
// Don't retry client errors
if ([
TraciaErrorCode.UNAUTHORIZED,
TraciaErrorCode.NOT_FOUND,
TraciaErrorCode.MISSING_VARIABLES,
TraciaErrorCode.INVALID_REQUEST,
].includes(error.code)) {
throw error;
}
// Retry on transient errors
if (attempt < maxRetries) {
const delay = Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
}
throw error;
}
}
}