// client/app.js: the page's script. It calls the backend on the same // address, so the browser sends the sign-in cookie with each request. The // conversation lives in this page for the page's life: each message posts the // turns so far with the new one, and the server keeps nothing. // Why the AI model gave no answer, by the error the route answers with 503. const UNANSWERED = { allowance_unavailable: 'The platform has no key for its AI route, so the model cannot answer.', account_paused: 'AI calls for this account are paused at a zero balance until the next UTC month starts.', upstream_quota: 'The AI model is out of capacity now: ask again in a while.', }; // The conversation after one exchange: the turns with the user's message and // the model's answer added; or, where the answer has no text or none came, // the turns as they were, the message dropped with it, so the turns keep // alternating and the next message ends them on the user's turn. export function afterExchange(turns, message, answer) { if (typeof answer !== 'string' || answer.trim() === '') return turns; return [...turns, { role: 'user', content: message }, { role: 'assistant', content: answer }]; } const byId = (id) => document.getElementById(id); // The signed-in user, from the platform's own route: null where no one is signed in. async function signedInUser() { const response = await fetch('/__account/me'); if (response.status === 200) return (await response.json()).user; if (response.status === 404) { byId('who').textContent = "Sign-in is unavailable at this address. For a local run, start with --sign-in and the application's hostname. Otherwise sign in at the application's deployed address."; return undefined; } return null; } function showTurns(turns) { const list = byId('turns'); list.replaceChildren(); for (const turn of turns) { const item = document.createElement('li'); item.className = turn.role; item.textContent = `${turn.role === 'user' ? 'You' : 'AI'}: ${turn.content}`; list.append(item); } } function buildChat() { let turns = []; byId('send').addEventListener('submit', async (event) => { event.preventDefault(); const message = byId('message').value; if (message.trim() === '') return; const status = byId('status'); status.textContent = 'Waiting for the answer.'; const messages = [...turns, { role: 'user', content: message }]; showTurns(messages); let answer = null; try { const response = await fetch('/api/ask', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ messages }) }); if (response.status === 200) answer = (await response.json()).answer; if (response.status === 200) status.textContent = answer?.trim() ? '' : 'The AI model gave no answer.'; else if (response.status === 503) status.textContent = UNANSWERED[(await response.json()).error] ?? 'The AI model gave no answer now.'; else status.textContent = `The message was not answered (status ${response.status}).`; } catch { status.textContent = 'The message was not sent.'; } // An answered message clears the box; an unanswered one stays in it to send again. const kept = afterExchange(turns, message, answer); if (kept !== turns) byId('send').reset(); turns = kept; showTurns(turns); }); } async function start() { const user = await signedInUser(); if (user === undefined) return; if (user === null) { byId('who').textContent = 'No one is signed in.'; byId('sign-in').hidden = false; return; } const address = user.addresses?.find((held) => held.verified)?.email; byId('who').textContent = `Signed in as ${address ?? user.id}.`; byId('sign-out').hidden = false; byId('signed-in').hidden = false; buildChat(); } // The page starts in a browser; a test imports afterExchange alone. if (typeof document !== 'undefined') start();