fix: let replay recover from dead letter jobs

This commit is contained in:
ImBenji
2026-08-08 23:32:41 +01:00
parent 0344d5ca97
commit 2c023c8962
3 changed files with 104 additions and 32 deletions
+29 -14
View File
@@ -11,20 +11,35 @@ function extractJson(text) {
async function callCoordinator(config, prompt) {
const apiKey = String(config?.openRouter?.apiKey || '').trim();
if (!apiKey) throw new Error('OpenRouter API key is not configured');
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
model: config.openRouter.llmModel,
temperature: 0,
response_format: { type: 'json_object' },
messages: [
{ role: 'system', content: 'You are a coordinator. Extract only evidence-backed categorical hypotheses. Never output probabilities, expected returns, confidence scores, position sizes, or trade actions.' },
{ role: 'user', content: prompt },
],
}),
});
if (!response.ok) throw new Error(`coordinator request failed with ${response.status}`);
const timeoutMs = Math.max(1000, Number(config?.openRouter?.timeoutMs || process.env.OPEN_ROUTER_TIMEOUT_MS) || 60000);
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
let response;
try {
response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
signal: controller.signal,
headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
model: config.openRouter.llmModel,
temperature: 0,
response_format: { type: 'json_object' },
messages: [
{ role: 'system', content: 'You are a coordinator. Extract only evidence-backed categorical hypotheses. Never output probabilities, expected returns, confidence scores, position sizes, or trade actions.' },
{ role: 'user', content: prompt },
],
}),
});
} catch (error) {
const cause = error?.cause?.code || error?.code || error?.name || 'network_error';
throw new Error(`coordinator request failed before response (${cause})`);
} finally {
clearTimeout(timeout);
}
if (!response.ok) {
const body = await response.text().catch(() => '');
throw new Error(`coordinator request failed with ${response.status}: ${body.slice(0, 300)}`);
}
const body = await response.json();
return extractJson(body?.choices?.[0]?.message?.content);
}