| 1 | // fixtures/assist.mjs — the assistant's client: what the editor asks |
| 2 | // POST /d/{sqid}/assist (internal/slides/assist.go), and what it does |
| 3 | // with the answer. |
| 4 | // |
| 5 | // The server proposes, this applies. What comes back is a batch of ops |
| 6 | // the server has already rebuilt and checked on a scratch copy of the |
| 7 | // deck; it is applied here through the editor's own commit() — the one |
| 8 | // write path: font-gated, one undo step, saved through the queue as |
| 9 | // this person's change and live to everybody else — and through nothing |
| 10 | // else. If this tab's deck has moved since (a colleague removed the |
| 11 | // slide, say), the ops module refuses the batch whole and nothing is |
| 12 | // applied. |
| 13 | // |
| 14 | // Errors are words, never sentences: the server answers a fixed English |
| 15 | // identifier, this maps it onto one of the panel's catalog strings |
| 16 | // (editor.html's data-text-error-*), and assist-panel.mjs says it. |
| 17 | // |
| 18 | // createAssist({fetch, url, commit, undoStep, flush, maxPrompt}) → |
| 19 | // {ask({prompt, scope, slide, model}) → outcome, undo() → bool} |
| 20 | // where an outcome is {state: 'applied'|'nothing', summary, focus} or |
| 21 | // {state: 'error', word}. |
| 22 | |
| 23 | /** The error words the panel has a string for; anything else is 'generic'. */ |
| 24 | export const ERROR_WORDS = ['prompt', 'busy', 'rate', 'gone', 'too-large', 'unavailable', 'timeout', 'refused', 'changed', 'fonts', 'noslide', 'generic'] |
| 25 | |
| 26 | export const DEFAULT_MAX_PROMPT = 2000 |
| 27 | |
| 28 | /** An outcome the person is told about in words. */ |
| 29 | export class AssistError extends Error { |
| 30 | constructor (word) { |
| 31 | super(word) |
| 32 | this.word = ERROR_WORDS.includes(word) ? word : 'generic' |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** The server's word ('too_large') as the panel's ('too-large'). */ |
| 37 | export function errorWord (serverWord) { |
| 38 | return new AssistError(String(serverWord || '').replace(/_/g, '-')).word |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * The request body for what the person asked, or an AssistError saying |
| 43 | * why it cannot be sent: an empty or over-long prompt, or "this slide" |
| 44 | * when there is no slide. |
| 45 | */ |
| 46 | export function buildRequest ({ prompt, scope, slide, model }, maxPrompt = DEFAULT_MAX_PROMPT) { |
| 47 | const text = String(prompt || '').trim() |
| 48 | if (!text || [...text].length > maxPrompt) throw new AssistError('prompt') |
| 49 | const body = { prompt: text, scope: scope === 'deck' ? 'deck' : 'slide', model: String(model || '') } |
| 50 | if (body.scope === 'slide') { |
| 51 | if (!slide) throw new AssistError('noslide') |
| 52 | body.slide = slide |
| 53 | } |
| 54 | return body |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Reads the server's answer. A 2xx must be {ops: [...], summary}; an |
| 59 | * error is {error: word}; a body that is neither (an HTML error page, a |
| 60 | * proxy's) is 'generic'. |
| 61 | */ |
| 62 | export async function readAnswer (res) { |
| 63 | let data = null |
| 64 | try { data = await res.json() } catch { data = null } |
| 65 | if (!res.ok) throw new AssistError(data && typeof data.error === 'string' ? errorWord(data.error) : 'generic') |
| 66 | if (!data || !Array.isArray(data.ops) || !data.ops.every(op => op && typeof op === 'object' && !Array.isArray(op))) { |
| 67 | throw new AssistError('generic') |
| 68 | } |
| 69 | return { |
| 70 | ops: data.ops, |
| 71 | summary: typeof data.summary === 'string' ? data.summary : '', |
| 72 | focus: typeof data.focus === 'string' ? data.focus : '' |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | export function createAssist ({ fetch = globalThis.fetch, url, commit, undoStep, flush = async () => true, maxPrompt = DEFAULT_MAX_PROMPT }) { |
| 77 | // The undo step the last applied answer became; undo() takes back |
| 78 | // exactly that one, and only while nothing came after it. |
| 79 | let step = null |
| 80 | |
| 81 | async function ask (request) { |
| 82 | try { |
| 83 | const body = buildRequest(request, maxPrompt) |
| 84 | // The server reads the deck from its log, so what this tab has |
| 85 | // typed and not yet saved goes first; otherwise the answer is |
| 86 | // about a deck older than the one on screen (Claude Fable, #42). |
| 87 | await flush() |
| 88 | let res |
| 89 | try { |
| 90 | res = await fetch(url, { |
| 91 | method: 'POST', |
| 92 | credentials: 'same-origin', |
| 93 | headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, |
| 94 | body: JSON.stringify(body) |
| 95 | }) |
| 96 | } catch { |
| 97 | throw new AssistError('unavailable') |
| 98 | } |
| 99 | const answer = await readAnswer(res) |
| 100 | if (!answer.ops.length) { |
| 101 | step = null |
| 102 | return { state: 'nothing', summary: answer.summary, focus: '' } |
| 103 | } |
| 104 | const done = await commit(answer.ops) |
| 105 | if (done.error) throw new AssistError('changed') |
| 106 | if (!done.applied) throw new AssistError('fonts') |
| 107 | step = done.step || null |
| 108 | // The stage shows the server's own HTML for a slide, so where |
| 109 | // the answer added one, the editor moves there once it is saved. |
| 110 | const saved = await flush() |
| 111 | return { state: 'applied', summary: answer.summary, focus: saved === false ? '' : answer.focus } |
| 112 | } catch (err) { |
| 113 | if (err instanceof AssistError) return { state: 'error', word: err.word } |
| 114 | console.error('assist:', err) |
| 115 | return { state: 'error', word: 'generic' } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | async function undo () { |
| 120 | const s = step |
| 121 | step = null |
| 122 | if (!s) return false |
| 123 | return (await undoStep(s)) === true |
| 124 | } |
| 125 | |
| 126 | return { ask, undo, get canUndo () { return step !== null } } |
| 127 | } |
| 128 | |