Files
plugins/packages/tabbit/src/checkin.mjs
T
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

84 lines
4.2 KiB
JavaScript

// The daily sign-in (签到), as magpie's check-in hook.
//
// magpie calls this once a day for each signed-in account while the user has
// the switch on (Settings → Usage → Plugins), and draws what came of it on
// the account's card. The outcomes it knows:
//
// claimed checked in just now
// done already checked in today
// inactive the vendor has no check-in event now
// ineligible this account can't check in at all
// captcha a human has to do it (never solved here)
// failed anything else; its message is shown, and magpie tries again
// later the same day
//
// Only what Tabbit actually answers is classified. A `sign_in_result` this
// file hasn't seen is reported as a failure carrying Tabbit's own word for
// it, rather than guessed as a success — a wrong "claimed" is worse than a
// "failed" someone can read.
import { signIn, signInStatus, SIGN_IN_SCENES } from "./api.mjs"
// Tabbit's own answers, measured: a fresh claim says "signed", a second try
// in the same day says "already_signed".
const SIGNED = new Set(["signed", "success", "signed_in"])
const ALREADY = new Set(["already_signed", "already", "duplicate"])
// rewardOf reads a reward row without assuming its shape: a number somewhere
// in it, or a nested amount, is what magpie's `credit` takes.
function rewardOf(row) {
for (const key of ["usage_reward_result", "reward_result", "reward"]) {
const reward = row?.[key]
if (reward === null || reward === undefined) continue
if (typeof reward === "number") return Number.isFinite(reward) ? reward : undefined
for (const field of ["amount", "credit", "value", "quota", "count"]) {
const value = Number(reward?.[field])
if (Number.isFinite(value)) return value
}
}
return undefined
}
// saidOf is a line for the card: what the sign-in gave, in Tabbit's terms.
function saidOf(row, status) {
const parts = []
const rewarded = rewardOf(row)
if (rewarded !== undefined && rewarded > 0) parts.push(`usage +${rewarded}`)
const days = Number(row?.signed_days ?? status?.signed_days)
if (Number.isFinite(days) && days > 0) {
const required = Number(row?.required_days ?? status?.required_days)
parts.push(Number.isFinite(required) && required > 0 ? `${days}/${required} days` : `${days} days`)
}
const pet = row?.pet_entitlement_result ?? status?.pet_entitlement_result
if (pet && pet !== "not_eligible") parts.push(`pet: ${pet}`)
return parts.join(" · ")
}
// checkinOf runs one day's sign-in. The status is read first: it costs
// nothing, it says whether the activity is even open, and it is what tells
// an already-signed day apart from a fresh claim.
export async function checkinOf(creds, { signal } = {}) {
const status = await signInStatus(creds, { signal })
const row = (status?.results ?? [])[0]
if (!row) return { outcome: "ineligible", message: `Tabbit knows no sign-in for ${SIGN_IN_SCENES.join(", ")}` }
if (row.activity_open === false) {
return { outcome: "inactive", message: "Tabbit's sign-in activity is closed", ...(saidOf(row, status) ? { message: `Tabbit's sign-in activity is closed · ${saidOf(row, status)}` } : {}) }
}
if (row.signed_today === true) {
return { outcome: "done", streak: Number(row.signed_days) || undefined, message: saidOf(row, status) || "already signed in today" }
}
const result = await signIn(creds, { signal })
const answered = (result?.results ?? [])[0]
const word = String(answered?.sign_in_result ?? "").toLowerCase()
const reward = rewardOf(answered) ?? rewardOf(row)
const said = saidOf(answered ?? row, status)
if (SIGNED.has(word)) return { outcome: "claimed", credit: reward, streak: Number(answered?.signed_days) || undefined, message: said }
if (ALREADY.has(word)) return { outcome: "done", streak: Number(answered?.signed_days) || undefined, message: said || "already signed in today" }
if (!word && (result?.results ?? []).length === 0) {
return { outcome: "failed", message: "Tabbit answered the sign-in with no result" }
}
return { outcome: "failed", message: `Tabbit answered the sign-in with "${answered?.sign_in_result}"${said ? ` · ${said}` : ""}` }
}
export const _internal = { rewardOf, saidOf, SIGNED, ALREADY }