The spec: plan_write and plan_update (new, shared, offered in every mode) and plan_submit (now shared) keep one structured plan — findings, objectives, phases of tasks — in place of the todo list, which lives on as a former name of plan_write. The shared system text is one text for both projects, with variables for what differs; the mode texts, a prototypes block and a library block are shared. acp.md: the client hands the device what only it knows (context, library, prototypes), the plan travels as a structure (plan_state), library calls carry the chat's scope, and a client may rewind a session. loop.json says its numbers as numbers. Four conformance areas: plan, prototype, library_scope, toolcall_index. The CLI: the plan is kept per session in the store and drawn above the prompt and on the approval card; the prompt is assembled from the spec's texts with their variables filled; the link reads the instance's instructions, personalization, knowledge scope and prototype offer, sends the plan as a structure and answers rewind; library calls to the instance carry the session as scope; a prototype written in the terminal is saved as a file and named in the transcript; a tool-call fragment without an index continues the last call; the default theme is Moria; dead exports and Hermes leftovers removed. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
// Server-sent events → parsed JSON objects. Keep-alive comments and non-data lines are skipped;
|
|
// a malformed frame is skipped rather than killing the reply (LLeMbas generation.py, the same rule).
|
|
// Errors are surfaced in every shape seen in the wild — see the comments in `line()`.
|
|
export async function* sseJson(body: ReadableStream<Uint8Array>): AsyncGenerator<{ event?: string; data: unknown }> {
|
|
const decoder = new TextDecoder()
|
|
let buf = ""
|
|
let event: string | undefined
|
|
let data: string[] = []
|
|
const out: { event?: string; data: unknown }[] = []
|
|
|
|
const dispatch = () => {
|
|
if (data.length === 0) return
|
|
const raw = data.join("\n")
|
|
data = []
|
|
const ev = event
|
|
event = undefined
|
|
if (raw === "[DONE]") return
|
|
try {
|
|
out.push({ event: ev, data: JSON.parse(raw) as unknown })
|
|
} catch {
|
|
// unparseable frame: skip
|
|
}
|
|
}
|
|
|
|
const line = (l: string) => {
|
|
if (l.endsWith("\r")) l = l.slice(0, -1)
|
|
if (l === "") return dispatch()
|
|
if (l.startsWith(":")) return
|
|
// llama.cpp (seen behind llama-swap) writes a mid-stream failure as a bare JSON line with no
|
|
// field name: `{"error":{"code":500,"message":"…"}}` — and as the stream's LAST line, with no
|
|
// newline after it. By the SSE rules it is a field called `{"error"` and would be ignored,
|
|
// silently ending the reply.
|
|
if (l.startsWith("{")) {
|
|
try {
|
|
const bare = JSON.parse(l) as unknown
|
|
if (bare && typeof bare === "object" && "error" in bare) {
|
|
dispatch()
|
|
out.push({ event: "error", data: bare })
|
|
return
|
|
}
|
|
} catch {}
|
|
}
|
|
const colon = l.indexOf(":")
|
|
const field = colon === -1 ? l : l.slice(0, colon)
|
|
let value = colon === -1 ? "" : l.slice(colon + 1)
|
|
if (value.startsWith(" ")) value = value.slice(1)
|
|
if (field === "data") {
|
|
// Some servers omit the blank line between events; a complete JSON frame on its own
|
|
// `data:` line is dispatched before the next one starts.
|
|
if (data.length > 0 && isJson(data.join("\n"))) dispatch()
|
|
data.push(value)
|
|
} else if (field === "event") event = value
|
|
else if (field === "error") {
|
|
// Some servers name the field `error:`. Same treatment as the bare JSON line.
|
|
let parsed: unknown = value
|
|
try {
|
|
parsed = JSON.parse(value)
|
|
} catch {}
|
|
dispatch()
|
|
out.push({ event: "error", data: parsed && typeof parsed === "object" && "error" in parsed ? parsed : { error: parsed } })
|
|
}
|
|
}
|
|
|
|
for await (const chunk of body) {
|
|
buf += decoder.decode(chunk, { stream: true })
|
|
let nl: number
|
|
while ((nl = buf.indexOf("\n")) !== -1) {
|
|
line(buf.slice(0, nl))
|
|
buf = buf.slice(nl + 1)
|
|
}
|
|
yield* out.splice(0)
|
|
}
|
|
buf += decoder.decode()
|
|
// The last line may have no newline after it — and that is exactly where an error lands.
|
|
if (buf) line(buf)
|
|
dispatch()
|
|
yield* out.splice(0)
|
|
}
|
|
|
|
function isJson(s: string): boolean {
|
|
try {
|
|
JSON.parse(s)
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|