New package @magpie-community/opencode-orcaterm-auth 0.1.0: Tencent Cloud OrcaTerm's own AI assistant (its monthly question allowance) as provider `orcaterm`, with WeChat QR sign-in (the same sign-in OrcaTerm's clients use) or a pasted console cookie. Credentials go only to Tencent's hosts (orcaterm.com, *.cloud.tencent.com) and WeChat's widget (open.weixin.qq.com, res.wx.qq.com). The nine models OrcaTerm's picker offers are listed; others resolve only as aliases. Per-account pacing at 10 chats/min, maxConcurrency 1; a JSON or SSE refusal inside a 200 becomes 429/401/502. registry.json gains its entry. Author: hg1048596-pixel (#460, replaces #422) The last review's three points are fixed at this head: the index.mjs header, toUpstreamBody and plugin-entry comments describe the code; the README's License links the package's own LICENSE; /cb accepts a bridge error without a code and shows it, and a later scan still finishes. Run at a75a2ae0fb554b3cb6ae879d5571b5dd7b2300a7 on mote (macOS, Bun 1.4.2), HOME=$(mktemp -d) as CI: - bun test packages/orcaterm: 88 pass, 0 fail - bun test (whole repo): 3624 pass, 8 skip, 0 fail - bun scripts/check.mjs: every package ok, orcaterm included - npm pack: 5 files (index.mjs, wechat.mjs, README.md, LICENSE, package.json); the extracted index.mjs imports with bun and its plugin returns provider orcaterm; node scripts/dist.mjs packs opencode-orcaterm-auth@0.1.0 - The earlier review's fake-upstream script against the packed package: 27 of 27 (JSON 429 with and without rate-limit words, SSE quota, rate and sign-in errors, envelopes in stream and JSON mode, per-account pacing, bearer headers, nine listed models, credentials only to lightai.cloud.tencent.com) - The /cb script against the packed wechat.mjs: foreign, missing and rebinding origins 403, wrong state refused, right origin and state finish the sign-in Broken by hand: requiring a string code again turns "callback preserves bridge errors without a code" red (87 pass, 1 fail). Not tried: a real OrcaTerm account, so neither the QR sign-in nor the real upstream.
139 lines
6.0 KiB
JavaScript
139 lines
6.0 KiB
JavaScript
// A refusal arrives inside the stream with HTTP 200 still on the status line,
|
|
// so the status alone says nothing: the head of the stream has to be read
|
|
// before an answer is handed back. These check that a used-up allowance is
|
|
// answered as 429, a dead sign-in as 401, and a real answer as an answer —
|
|
// with the peeked bytes replayed, so no text is lost to the peek.
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { OrcaTermAuthPlugin, _internal } from "./index.mjs"
|
|
|
|
// The suite fires far more than the account's ten-a-minute budget; lift the
|
|
// pacer so these tests measure the plugin, not the limiter.
|
|
process.env.ORCATERM_NO_PACE = "1"
|
|
|
|
|
|
const real = globalThis.fetch
|
|
afterEach(() => (globalThis.fetch = real))
|
|
|
|
const frame = (o) => `data: ${JSON.stringify(o)}\n\n`
|
|
|
|
/** Answer a chat request, with the upstream body being `body`. */
|
|
const ask = async (body, { stream = true } = {}) => {
|
|
globalThis.fetch = async () => new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } })
|
|
const hooks = await OrcaTermAuthPlugin({ client: { app: { log: () => {} } } })
|
|
const opts = await hooks.auth.loader(async () => ({ type: "api", key: "uin=o1234567890; skey=deadbeef" }))
|
|
return opts.fetch("https://lightai.cloud.tencent.com/assistant/chat", {
|
|
method: "POST",
|
|
body: JSON.stringify({ model: "deepseek", stream, messages: [{ role: "user", content: "hi" }] }),
|
|
})
|
|
}
|
|
|
|
const bodyOf = (frames) =>
|
|
new ReadableStream({
|
|
start(c) {
|
|
for (const f of frames) c.enqueue(new TextEncoder().encode(f))
|
|
c.close()
|
|
},
|
|
})
|
|
|
|
// A used-up monthly allowance looks like an answer unless the head is read
|
|
// first: magpie would neither fail over nor rest the account.
|
|
test("a quota refusal is answered 429, not as text", async () => {
|
|
const res = await ask(bodyOf([frame({}) && "data: [ERROR]: 本月问答次数已用完\n\n"]))
|
|
expect(res.status).toBe(429)
|
|
const j = await res.json()
|
|
expect(j.error.message).toContain("次数已用完")
|
|
})
|
|
|
|
test("a rate-limit refusal is answered 429", async () => {
|
|
const res = await ask(bodyOf(["data: [ERROR]: rate limit exceeded\n\n"]))
|
|
expect(res.status).toBe(429)
|
|
})
|
|
|
|
// A dead sign-in is the account's problem, not the request's: magpie marks
|
|
// it and stops sending, rather than failing over forever.
|
|
test("a refused sign-in is answered 401 and marked", async () => {
|
|
const res = await ask(bodyOf(["data: [ERROR]: VERIFY_SKEY_UIN_FAILED\n\n"]))
|
|
expect(res.status).toBe(401)
|
|
expect(res.headers.get("x-magpie-sign-in")).toBe("expired")
|
|
})
|
|
|
|
test("any other upstream refusal is answered 502", async () => {
|
|
const res = await ask(bodyOf(["data: [ERROR]: model unavailable\n\n"]))
|
|
expect(res.status).toBe(502)
|
|
})
|
|
|
|
// The peek reads bytes before deciding; those bytes have to go back into the
|
|
// stream or the start of the answer is lost.
|
|
test("a real answer passes through with its first frame intact", async () => {
|
|
const res = await ask(bodyOf([frame({ content: "你" }), frame({ content: "好" }), "data: [DONE]\n\n"]))
|
|
expect(res.status).toBe(200)
|
|
const text = await res.text()
|
|
expect(text).toContain("你")
|
|
expect(text).toContain("好")
|
|
})
|
|
|
|
// An answer whose head is only whitespace before the first frame must still
|
|
// be recognised as an answer rather than held until the peek limit.
|
|
test("leading noise before the first frame does not hide the answer", async () => {
|
|
const res = await ask(bodyOf(["\n\n", frame({ content: "ok" }), "data: [DONE]\n\n"]))
|
|
expect(res.status).toBe(200)
|
|
expect(await res.text()).toContain("ok")
|
|
})
|
|
|
|
// The same refusal without streaming must not become a 200 either.
|
|
test("a quota refusal in a non-streaming request is 429", async () => {
|
|
const res = await ask(bodyOf(["data: [ERROR]: 本月问答次数已用完\n\n"]), { stream: false })
|
|
expect(res.status).toBe(429)
|
|
})
|
|
|
|
// Reading the head must not consume the whole stream: a long answer still
|
|
// arrives in full.
|
|
test("a long answer is not truncated by the peek", async () => {
|
|
const frames = []
|
|
for (let i = 0; i < 200; i++) frames.push(frame({ content: `w${i} ` }))
|
|
frames.push("data: [DONE]\n\n")
|
|
const res = await ask(bodyOf(frames))
|
|
expect(res.status).toBe(200)
|
|
// Compare the deltas joined, not the raw body: where a chunk boundary falls
|
|
// is the plugin's business, and asserting on the raw JSON made this fail the
|
|
// moment the extractor's hold-back changed size.
|
|
const text = await res.text()
|
|
let out = ""
|
|
for (const line of text.split("\n")) {
|
|
const t = line.trim()
|
|
if (!t.startsWith("data:")) continue
|
|
const payload = t.slice(5).trim()
|
|
if (payload === "[DONE]") continue
|
|
try {
|
|
const d = JSON.parse(payload).choices?.[0]?.delta
|
|
if (d?.content) out += d.content
|
|
} catch {
|
|
/* not a chunk */
|
|
}
|
|
}
|
|
const expected = Array.from({ length: 200 }, (_, i) => `w${i} `).join("")
|
|
expect(out).toBe(expected)
|
|
})
|
|
|
|
// The OpenAI error body carries both a class and a code, and they are not
|
|
// the same thing: `type` is the class magpie reasons about, `code` says what
|
|
// the upstream said. Passing the class in both places loses the upstream's
|
|
// own words, which is what a user needs to see.
|
|
test("a refusal keeps the upstream's words in code, the class in type", async () => {
|
|
const res = await ask(bodyOf(["data: [ERROR]: 本月问答次数已用完\n\n"]))
|
|
expect(res.status).toBe(429)
|
|
const j = await res.json()
|
|
expect(j.error.type).toBe("rate_limit_error")
|
|
expect(j.error.code).toContain("次数已用完")
|
|
expect(j.error.code).not.toBe("rate_limit_error")
|
|
})
|
|
|
|
// classifyHead is what decides: a frame carrying text is an answer, an
|
|
// [ERROR] frame is not, and a quiet stream that has ended is an answer.
|
|
test("classifyHead tells answers from refusals", () => {
|
|
expect(_internal.errorStatus("额度已用完")).toMatchObject({ status: 429 })
|
|
expect(_internal.errorStatus("限流")).toMatchObject({ status: 429 })
|
|
expect(_internal.errorStatus("VERIFY_USER_FAILED")).toMatchObject({ status: 401 })
|
|
expect(_internal.errorStatus("something else")).toMatchObject({ status: 502 })
|
|
})
|