Files
hclhcl 7ea611975a tabbit: Tabbit (web.tabbit.com) as a provider, with live catalog, check-in and cycle usage (#673)
Adds @magpie-community/opencode-tabbit-auth 0.1.0 (packages/tabbit) and its
registry.json entry. Tabbit (web.tabbit.com) becomes a provider:
- A live model catalog.
- Turns run as room -> run -> SSE join. Each turn has its own stream
  timeout (TABBIT_STREAM_TIMEOUT_MS, default 300 s), and the 30 s request
  timeout no longer covers join.
- A run is stopped through /proxy/v0/chat/stop/ when the client aborts or
  the stream times out.
- A cut turn ends as an error event (streaming) or a 502, never as a
  "stop" finish, and its room is not reused.
- Rooms are keyed by account and session, with a 30 min TTL and a cap of 256.
- 400/401/403/404/429 pass through with retry-after.
- The daily desktop_pet check-in is free.
- Cycle usage.
- macOS desktop sign-in reads the Tabbit Browser cookie store from a temp
  copy that is removed in a finally. Windows and Linux paste the token.

Run at 5be6e90 on mote (macOS):
- bun test: 58 pass. node --test tests/*.test.mjs: 58 pass.
  bun scripts/check.mjs: ok.
- Broken by hand, and each change turned its test red:
  - no rmSync of the cookie copy;
  - 429 not passed through;
  - room keyed by session only;
  - stop sent on an already-aborted signal;
  - no abort listener on the request;
  - streaming truncation ignored;
  - the old truncation rule;
  - totalTimeout back on join;
  - no wake of a quiet reader;
  - onTimeout not called;
  - no room pruning.
- Probes against a local SSE server:
  - A 35 s turn gives 200 under Bun (streaming and non-streaming) and under
    Node.
  - A silent stream with TABBIT_STREAM_TIMEOUT_MS=3000 ends at 3 s under
    Bun and Node (an error event, or a 502 non-streaming), and the server
    sees the stop at 3 s.
- tabbit-* temp dirs in TMPDIR: the same count before and after the bun and
  node runs.

Not tried: a real Tabbit account.
2026-11-03 21:39:56 +08:00

119 lines
5.5 KiB
JavaScript

// Reading a turn: the recorded frames of a real one, replayed.
import test from "node:test"
import assert from "node:assert/strict"
import { parseSSE, turnDeltas, readTurn } from "../src/sse.mjs"
// frame builds one SSE event as the wire carries it.
const frame = (event, data, id) => `${id ? `id: ${id}\n` : ""}event: ${event}\ndata: ${JSON.stringify(data)}\n\n`
function responseOf(chunks) {
const encoder = new TextEncoder()
return new Response(
new ReadableStream({
start(controller) {
for (const chunk of chunks) controller.enqueue(encoder.encode(chunk))
controller.close()
},
}),
{ status: 200, headers: { "content-type": "text/event-stream" } },
)
}
// A turn exactly as measured: thinking, then the answer, then the end.
const GOOD = [
frame("snapshot_begin", { payload: {} }),
frame("message_history", {
payload: { messages: [{ message_id: "a0", role: "user", content: "1+1=?" }, { message_id: "a1", role: "assistant", content: "" }] },
}),
frame("active_runs_snapshot", { payload: { runs: [{ run_id: "r1", status: "RUNNING", assistant_message_id: "a1" }] } }),
frame("user_message_created", { payload: { message_id: "m-user" } }),
frame("assistant_message_started", { payload: { run_id: "r1", message_id: "a1" } }),
frame("event_message_chunk", { payload: { chunk_type: "thinking", chunk_payload: { reasoning_content: "想想" } } }),
frame("event_message_chunk", { payload: { chunk_type: "thinking_finished", chunk_payload: {} } }),
frame("assistant_message_delta", { payload: { chunk_type: "message_chunk", chunk_payload: { content: "4" } } }),
frame("session_title_updated", { payload: { title: "算术" } }),
frame("assistant_message_finished", { payload: { message_id: "a1", model_name: null } }),
frame("run_completed", { payload: { status: "COMPLETED" } }, "e1"),
]
test("parseSSE splits events, data and ids", async () => {
const out = []
for await (const ev of parseSSE(responseOf(["event: a\ndata: {\"x\":1}\n\n", "id: 9\nevent: b\ndata: he", "llo\n\n"]).body)) out.push(ev)
assert.deepEqual(out[0], { event: "a", data: '{"x":1}', id: null })
assert.deepEqual(out[1], { event: "b", data: "hello", id: "9" })
})
test("a good turn gives text, thinking, ids and completed", async () => {
const state = await readTurn(responseOf(GOOD))
assert.equal(state.text, "4")
assert.equal(state.reasoning, "想想")
assert.equal(state.completed, true)
assert.equal(state.truncated, false)
assert.equal(state.assistantMessageId, "a1")
assert.equal(state.userMessageId, "m-user")
assert.equal(state.runId, "r1")
assert.equal(state.title, "算术")
assert.equal(state.lastEventId, "e1")
})
test("the deltas arrive in order, thinking before text", async () => {
const kinds = []
for await (const part of turnDeltas(responseOf(GOOD))) {
if (part.type === "thinking") kinds.push(`think:${part.value}`)
if (part.type === "text") kinds.push(`text:${part.value}`)
}
assert.deepEqual(kinds, ["think:想想", "text:4"])
})
test("a turn that stops early is truncated, not silently complete", async () => {
const state = await readTurn(responseOf(GOOD.slice(0, 8)))
assert.equal(state.completed, false)
assert.equal(state.truncated, true)
assert.equal(state.text, "4")
})
test("a failed turn is reported as failed", async () => {
const state = await readTurn(responseOf([...GOOD.slice(0, 8), frame("run_failed", { payload: { status: "FAILED", reason: "boom" } })]))
assert.deepEqual(state.failed, { status: "FAILED", reason: "boom" })
assert.equal(state.completed, false)
assert.equal(state.truncated, false)
})
test("a rejoin's snapshot recovers text already sent", async () => {
const chunk = frame("message_history", {
payload: { messages: [{ message_id: "a1", role: "assistant", content: "前半段", reasoning_content: "想过了" }] },
})
const runs = frame("active_runs_snapshot", { payload: { runs: [{ run_id: "r1", assistant_message_id: "a1" }] } })
const state = await readTurn(responseOf([chunk, runs, frame("assistant_message_delta", { payload: { chunk_payload: { content: "后半段" } } }), frame("run_completed", { payload: {} })]))
assert.equal(state.text, "前半段后半段")
assert.equal(state.reasoning, "想过了")
})
test("a new turn's snapshot is not replayed as this turn's text", async () => {
// The room's history holds the previous answer; only the assistant message
// the active run names is this turn's, and that one is still empty.
const history = frame("message_history", {
payload: { messages: [
{ message_id: "old", role: "assistant", content: "上一轮的完整回答" },
{ message_id: "new", role: "user", content: "新问题" },
{ message_id: "cur", role: "assistant", content: "" },
] },
})
const runs = frame("active_runs_snapshot", { payload: { runs: [{ run_id: "r2", assistant_message_id: "cur" }] } })
const deltas = frame("assistant_message_delta", { payload: { chunk_payload: { content: "新回答" } } })
const state = await readTurn(responseOf([history, runs, deltas, frame("run_completed", { payload: {} })]))
assert.equal(state.text, "新回答")
})
test("a stream that closes before any answer is unfinished, not empty-but-done", async () => {
// join closes after assistant_message_started, before any text or run_completed
const state = await readTurn(responseOf([
frame("snapshot_begin", { payload: {} }),
frame("assistant_message_started", { payload: { run_id: "r1", message_id: "a1" } }),
]))
assert.equal(state.text, "")
assert.equal(state.completed, false)
assert.equal(state.truncated, true)
})