Files
granthi-review/src/lib/router.js
T

23 lines
954 B
JavaScript
Raw Normal View History

// shre-router client. POST /v1/chat with stream:false.
// Real model answer lives in .message.content; top-level .content can carry an
// upstream-down apology — always prefer .message.content.
export async function chat({ routerUrl, model, messages, agentId, sessionId, tenantId, timeoutMs }) {
const res = await fetch(`${routerUrl.replace(/\/$/, '')}/v1/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model, messages, agentId, sessionId, tenantId, stream: false }),
signal: AbortSignal.timeout(timeoutMs)
});
if (!res.ok) {
const text = await res.text().catch(() => '');
throw new Error(`router -> ${res.status}: ${text.slice(0, 300)}`);
}
const data = await res.json();
const content = data?.message?.content ?? data?.content;
if (typeof content !== 'string' || !content.trim()) {
throw new Error('router returned empty content');
}
return content;
}