|
| 1 | +import type { ProgramStatus, ProgramStatusKind } from "@opentui/core" |
| 2 | +import { createEffect, onCleanup } from "solid-js" |
| 3 | + |
| 4 | +export type PendingSession = { id: string; title?: string } & ( |
| 5 | + | { state: "blocked"; kind: ProgramStatusKind; message?: string } |
| 6 | + | { state: "working" } |
| 7 | +) |
| 8 | + |
| 9 | +const APP = "opencode" |
| 10 | +const ROOT = "" |
| 11 | +const encoder = new TextEncoder() |
| 12 | +const decoder = new TextDecoder() |
| 13 | + |
| 14 | +// Mirrors pending session state into OSC 7501 records: one root summary plus one child record per |
| 15 | +// pending session. Terminals drop working and blocked records when the process exits. |
| 16 | +export function createProgramStatus(input: { |
| 17 | + renderer: { setProgramStatus(status: ProgramStatus): boolean } |
| 18 | + sessions: () => PendingSession[] |
| 19 | +}) { |
| 20 | + const sent = new Map<string, string>() |
| 21 | + |
| 22 | + const send = (key: string, status: ProgramStatus) => { |
| 23 | + if (!input.renderer.setProgramStatus(status)) return |
| 24 | + if (status.state === "clear") { |
| 25 | + sent.delete(key) |
| 26 | + return |
| 27 | + } |
| 28 | + sent.set(key, JSON.stringify(status)) |
| 29 | + } |
| 30 | + |
| 31 | + createEffect(() => { |
| 32 | + const reports = programStatusReports(input.sessions()) |
| 33 | + Array.from(sent.keys()) |
| 34 | + .filter((key) => key !== ROOT && !reports.has(key)) |
| 35 | + .forEach((key) => send(key, { state: "clear", id: key })) |
| 36 | + reports.forEach((status, key) => { |
| 37 | + if (sent.get(key) !== JSON.stringify(status)) send(key, status) |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + onCleanup(() => send(ROOT, { state: "clear" })) |
| 42 | +} |
| 43 | + |
| 44 | +export function programStatusReports(sessions: PendingSession[]) { |
| 45 | + return new Map<string, ProgramStatus>([ |
| 46 | + [ROOT, root(sessions)], |
| 47 | + ...sessions.map((session): [string, ProgramStatus] => [session.id, record(session, session.id)]), |
| 48 | + ]) |
| 49 | +} |
| 50 | + |
| 51 | +function root(sessions: PendingSession[]): ProgramStatus { |
| 52 | + const blocked = sessions.filter((session) => session.state === "blocked") |
| 53 | + const working = sessions.filter((session) => session.state === "working") |
| 54 | + if (blocked.length === 1) return record(blocked[0]) |
| 55 | + if (blocked.length > 1) |
| 56 | + return { |
| 57 | + state: "blocked", |
| 58 | + kind: blocked.every((session) => session.kind === blocked[0].kind) ? blocked[0].kind : undefined, |
| 59 | + app: APP, |
| 60 | + message: `${blocked.length} sessions need input`, |
| 61 | + } |
| 62 | + if (working.length === 1) return record(working[0]) |
| 63 | + if (working.length > 1) return { state: "working", app: APP, message: `${working.length} sessions working` } |
| 64 | + return { state: "idle", app: APP } |
| 65 | +} |
| 66 | + |
| 67 | +function record(session: PendingSession, id?: string): ProgramStatus { |
| 68 | + const title = clip(session.title, 192) |
| 69 | + if (session.state === "working") return { state: "working", id, app: APP, title } |
| 70 | + return { state: "blocked", kind: session.kind, id, app: APP, title, message: clip(session.message, 2048) } |
| 71 | +} |
| 72 | + |
| 73 | +// Records reject control characters and oversized fields, so normalize to one line within the byte limit. |
| 74 | +function clip(input: string | undefined, bytes: number) { |
| 75 | + const text = (input ?? "") |
| 76 | + .replace(/[\u0000-\u001F\u007F-\u009F]+/g, " ") |
| 77 | + .replace(/\s+/g, " ") |
| 78 | + .trim() |
| 79 | + if (!text) return |
| 80 | + const encoded = encoder.encode(text) |
| 81 | + if (encoded.byteLength <= bytes) return text |
| 82 | + // Leave room for the 3-byte ellipsis and drop a code point the byte cut split. |
| 83 | + return decoder.decode(encoded.subarray(0, bytes - 3)).replace(/\uFFFD+$/, "") + "…" |
| 84 | +} |
0 commit comments