fix: handle OpenAI-style router responses + log unparseable model output

The router's response shape varies by upstream: some backends return
.message.content, some choices[0].message.content, some only top-level
.content. Prefer them in that order. Record the actually-served model
(_shre.model) in the ledger and log a snippet of unparseable output for
diagnosis.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Nirav Patel
2026-08-18 23:35:12 -04:00
co-authored by Claude Fable 5
parent e3ad9c25fc
commit 74222a1631
2 changed files with 17 additions and 8 deletions
+7 -2
View File
@@ -14,9 +14,14 @@ export async function chat({ routerUrl, model, messages, agentId, sessionId, ten
throw new Error(`router -> ${res.status}: ${text.slice(0, 300)}`);
}
const data = await res.json();
const content = data?.message?.content ?? data?.content;
// Response shape varies by upstream: prefer .message.content, then
// OpenAI-style choices, then top-level .content (which can carry an
// upstream-down apology — hence last).
const content = data?.message?.content
?? data?.choices?.[0]?.message?.content
?? data?.content;
if (typeof content !== 'string' || !content.trim()) {
throw new Error('router returned empty content');
}
return content;
return { content, servedModel: data?._shre?.model ?? null };
}