async function runWorkflow(workflowId: string, inputs: Record<string, unknown>) {
const res = await fetch('https://tryblend.ai/api/v1/responses', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.BLEND_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ workflowId, inputs }),
})
if (!res.ok) {
const { error } = await res.json()
if (error.code === 'insufficient_credits') {
throw new OutOfCreditsError(error.message)
}
if (error.code === 'rate_limit_exceeded') {
const retryAfter = Number(res.headers.get('Retry-After') ?? 5)
await new Promise(r => setTimeout(r, retryAfter * 1000))
return runWorkflow(workflowId, inputs) // retry once
}
throw new Error(`${error.type}: ${error.message}`)
}
return res.json()
}