Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192import { ApiError, askSideQuestion, cancelSideQuestion, continueSideQuestion, uuidv7, type SideQuestion,} from './api';
export type { SideQuestion };
const MAX_QUESTION_BYTES = 16_384;const MAX_ANSWER_BYTES = 65_536;const utf8 = new TextEncoder();
/** A stream or snapshot value as an exchange, or null when it is not one. */function sideQuestionOf(value: unknown): SideQuestion | null { if (!value || typeof value !== 'object') return null; const exchange = value as Record<string, unknown>; if ( typeof exchange.id !== 'string' || !exchange.id || typeof exchange.question !== 'string' || !exchange.question || exchange.question.includes('\0') || utf8.encode(exchange.question).length > MAX_QUESTION_BYTES || typeof exchange.asked_at !== 'number' || !Number.isSafeInteger(exchange.asked_at) || exchange.asked_at < 0 || (exchange.continuation_session_id !== null && (typeof exchange.continuation_session_id !== 'string' || !exchange.continuation_session_id || exchange.status !== 'answered')) ) return null; switch (exchange.status) { case 'running': case 'cancelled': if (exchange.answer !== null || exchange.error !== null) return null; break; case 'answered': if ( typeof exchange.answer !== 'string' || utf8.encode(exchange.answer).length > MAX_ANSWER_BYTES || exchange.error !== null ) return null; break; case 'failed': if ( exchange.answer !== null || typeof exchange.error !== 'string' || !exchange.error ) return null; break; default: return null; } return value as SideQuestion;}
/** * One session's latest side question and whether its card shows. The stream * (through the transcript) and this store's own calls are the only writers; * a reply that lands after a newer exchange replaced its own is dropped. */export class SideQuestions { exchange = $state<SideQuestion | null>(null); visible = $state(false); continuing = $state(false); continueError = $state<string | null>(null); /** The session id one Continue press created or is creating, kept per exchange so a retry reuses it. */ #continuationId: string | null = null; #continuationFor: string | null = null; #revision = 0; #pendingAsk: { id: string; dismissed: boolean } | null = null;
constructor(readonly sessionId: string) {}
/** A reset snapshot replaces the exchange, with null as with any value. */ applyReset(value: unknown) { this.#revision++; const exchange = value == null ? null : sideQuestionOf(value); this.receive(exchange); }
/** An event carries the session's current exchange, whatever its id. */ applyEvent(value: unknown) { const exchange = sideQuestionOf(value); if (!exchange) return; this.#revision++; this.receive(exchange); }
private receive(exchange: SideQuestion | null) { const changed = this.exchange?.id !== exchange?.id; this.exchange = exchange; if (!exchange) this.visible = false; else if (changed) this.visible = !( this.#pendingAsk?.id === exchange.id && this.#pendingAsk.dismissed ); }
/** Shows the latest exchange again; false when there is none. */ reopen(): boolean { if (!this.exchange) return false; if (this.#pendingAsk) this.#pendingAsk.dismissed = false; this.visible = true; return true; }
/** Asks a new question under a fresh id and shows its card. Errors reach the caller; nothing was sent as a prompt. */ async ask(question: string): Promise<void> { const id = uuidv7(); const revision = this.#revision; const pending = { id, dismissed: false }; this.#pendingAsk = pending; try { const asked = await askSideQuestion(this.sessionId, id, question); if (this.#pendingAsk !== pending) return; if (revision === this.#revision) this.receive(asked); if (this.exchange?.id !== id) return; this.visible = !pending.dismissed; if (pending.dismissed) await this.dismiss(); } finally { if (this.#pendingAsk === pending) this.#pendingAsk = null; } }
/** Hides the card and keeps the exchange; hiding a running question cancels it. */ async dismiss(): Promise<void> { this.visible = false; if (this.#pendingAsk) this.#pendingAsk.dismissed = true; const exchange = this.exchange; if (!exchange || exchange.status !== 'running') return; const revision = this.#revision; try { const cancelled = await cancelSideQuestion(this.sessionId, exchange.id); if (revision === this.#revision && this.exchange?.id === exchange.id) this.exchange = cancelled; } catch { // The stream still carries the outcome; a failed cancel hides nothing else. } }
/** * Opens the session this exchange continues in, creating it on the first * press. One creation id is kept per exchange, so pressing again after a * failure retries the same session instead of making another. */ async continueInNewSession(open: (sessionId: string) => void): Promise<void> { const exchange = this.exchange; if (!exchange || exchange.status !== 'answered' || this.continuing) return; if (exchange.continuation_session_id) return open(exchange.continuation_session_id); if (this.#continuationFor !== exchange.id) { this.#continuationFor = exchange.id; this.#continuationId = uuidv7(); } this.continuing = true; this.continueError = null; try { const created = await continueSideQuestion( this.sessionId, exchange.id, this.#continuationId!, ); if (this.exchange?.id === exchange.id) this.exchange = { ...this.exchange, continuation_session_id: created.id, }; open(created.id); } catch (error) { // Another press already continued it; the stream names that session. if ( error instanceof ApiError && error.code === 'side_question_continued' && this.exchange?.continuation_session_id ) { open(this.exchange.continuation_session_id); return; } this.continueError = String((error as Error).message ?? error); } finally { this.continuing = false; } }}