// fixtures/assist.mjs — the assistant's client: what the editor asks // POST /d/{sqid}/assist (internal/slides/assist.go), and what it does // with the answer. // // The server proposes, this applies. What comes back is a batch of ops // the server has already rebuilt and checked on a scratch copy of the // deck; it is applied here through the editor's own commit() — the one // write path: font-gated, one undo step, saved through the queue as // this person's change and live to everybody else — and through nothing // else. If this tab's deck has moved since (a colleague removed the // slide, say), the ops module refuses the batch whole and nothing is // applied. // // Errors are words, never sentences: the server answers a fixed English // identifier, this maps it onto one of the panel's catalog strings // (editor.html's data-text-error-*), and assist-panel.mjs says it. // // createAssist({fetch, url, commit, undoStep, flush, maxPrompt}) → // {ask({prompt, scope, slide, model}) → outcome, undo() → bool} // where an outcome is {state: 'applied'|'nothing', summary, focus} or // {state: 'error', word}. /** The error words the panel has a string for; anything else is 'generic'. */ export const ERROR_WORDS = ['prompt', 'busy', 'rate', 'gone', 'too-large', 'unavailable', 'timeout', 'refused', 'changed', 'fonts', 'noslide', 'generic'] export const DEFAULT_MAX_PROMPT = 2000 /** An outcome the person is told about in words. */ export class AssistError extends Error { constructor (word) { super(word) this.word = ERROR_WORDS.includes(word) ? word : 'generic' } } /** The server's word ('too_large') as the panel's ('too-large'). */ export function errorWord (serverWord) { return new AssistError(String(serverWord || '').replace(/_/g, '-')).word } /** * The request body for what the person asked, or an AssistError saying * why it cannot be sent: an empty or over-long prompt, or "this slide" * when there is no slide. */ export function buildRequest ({ prompt, scope, slide, model }, maxPrompt = DEFAULT_MAX_PROMPT) { const text = String(prompt || '').trim() if (!text || [...text].length > maxPrompt) throw new AssistError('prompt') const body = { prompt: text, scope: scope === 'deck' ? 'deck' : 'slide', model: String(model || '') } if (body.scope === 'slide') { if (!slide) throw new AssistError('noslide') body.slide = slide } return body } /** * Reads the server's answer. A 2xx must be {ops: [...], summary}; an * error is {error: word}; a body that is neither (an HTML error page, a * proxy's) is 'generic'. */ export async function readAnswer (res) { let data = null try { data = await res.json() } catch { data = null } if (!res.ok) throw new AssistError(data && typeof data.error === 'string' ? errorWord(data.error) : 'generic') if (!data || !Array.isArray(data.ops) || !data.ops.every(op => op && typeof op === 'object' && !Array.isArray(op))) { throw new AssistError('generic') } return { ops: data.ops, summary: typeof data.summary === 'string' ? data.summary : '', focus: typeof data.focus === 'string' ? data.focus : '' } } export function createAssist ({ fetch = globalThis.fetch, url, commit, undoStep, flush = async () => true, maxPrompt = DEFAULT_MAX_PROMPT }) { // The undo step the last applied answer became; undo() takes back // exactly that one, and only while nothing came after it. let step = null async function ask (request) { try { const body = buildRequest(request, maxPrompt) // The server reads the deck from its log, so what this tab has // typed and not yet saved goes first; otherwise the answer is // about a deck older than the one on screen (Claude Fable, #42). await flush() let res try { res = await fetch(url, { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify(body) }) } catch { throw new AssistError('unavailable') } const answer = await readAnswer(res) if (!answer.ops.length) { step = null return { state: 'nothing', summary: answer.summary, focus: '' } } const done = await commit(answer.ops) if (done.error) throw new AssistError('changed') if (!done.applied) throw new AssistError('fonts') step = done.step || null // The stage shows the server's own HTML for a slide, so where // the answer added one, the editor moves there once it is saved. const saved = await flush() return { state: 'applied', summary: answer.summary, focus: saved === false ? '' : answer.focus } } catch (err) { if (err instanceof AssistError) return { state: 'error', word: err.word } console.error('assist:', err) return { state: 'error', word: 'generic' } } } async function undo () { const s = step step = null if (!s) return false return (await undoStep(s)) === true } return { ask, undo, get canUndo () { return step !== null } } }