troilus1011 on X: WorkBuddy's DeepSeek in OpenCode through magpie thought at low and high, never at the default. OpenCode's default variant names no reasoning_effort, and WorkBuddy doesn't think without one: measured on www.workbuddy.ai with 0.1.42, deepseek-v4.1-flash gave 0 reasoning tokens with no effort (2 of 2), 0 at none, 16/37 at low, 34/56 at high. WorkBuddy's own client (CLI 2.165.0 in WorkBuddy 5.9.3, configureThinkingSettings) sends the session's effort, else the reasoningEffort setting, else the model's reasoning.defaultEffort from /v3/config, which is "high" for this model on both builds. A chat that names no effort now gets the model's own defaultEffort when its listed levels hold it, read from the same cached /v3/config list (one read per minute; a failed read leaves the chat as it came and is not retried on every chat). An effort the client names, none included, is untouched and the list isn't read for it. Models with no reasoning config get nothing. runmiNeverstop on X: 2.89 GB up to copilot.tencent.com through magpie. WorkBuddy's client gzips a model request's body (GzipRequestProcessor, on with productFeatures.RequestBodyGzip, true in both builds' product.json and not turned off by the remote config); the plugin sent it as plain JSON. Agent chat JSON (the whole history each turn, tool schemas) compresses several-fold, so the upload was several times what the official client sends for the same turns. A chat-completions body now goes gzipped with Content-Encoding: gzip on both builds; unanswered() weighs the gzipped size against the front door's 2 MiB. Claude's Messages route goes as it came (not tried gzipped live). Run (on mote, HOME=$(mktemp -d) bun test in packages/workbuddy): 197 pass, 0 fail. bun scripts/check.mjs: exit 0. Fakes that parse the sent body gunzip it (testdata/unz.mjs); reset.test.mjs's 2 MiB bodies are random base64 so they stay over 2 MiB on the wire. Live (www.workbuddy.ai, a real account in a sandbox HOME): with 0.1.43 the default sent reasoning_effort high gzipped and thought (34 and 51 reasoning tokens), none stayed 0, low and high as before, all 200. A gzipped chat answered 200 the same as a plain one. Broken by hand: origin/main's index.mjs with the new default-effort.test.mjs: 5 of 8 red (default effort, failed-read reuse, both gzip tests, defaultEffortOf); the 3 that pass guard what must not change (named efforts, models without a default, Messages). Not tried: a live chat on the China build (copilot.tencent.com answered 401 to every token available here, its /v3/config read fine); Messages gzipped; the reporters' own OpenCode sessions end to end.
141 lines
6.8 KiB
JavaScript
141 lines
6.8 KiB
JavaScript
// An API key made at the build's /profile/keys page (plugins#407) is sent as
|
|
// WorkBuddy's own CLI sends CODEBUDDY_API_KEY (@tencent-ai/codebuddy-code
|
|
// 2.177.0): X-API-Key and Authorization: Bearer, both the key, X-User-Id
|
|
// "anonymous_" and the key's last eight characters, no X-Domain, to the
|
|
// build's own host. The browser and desktop sign-ins are unchanged. Every
|
|
// fetch here is a fake; nothing reaches the network.
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { WorkBuddyAuthPlugin, WorkBuddyAIAuthPlugin, _internal } from "./index.mjs"
|
|
|
|
const real = globalThis.fetch
|
|
afterEach(() => (globalThis.fetch = real))
|
|
|
|
const KEY = "ck_test0000000000abcdefgh"
|
|
const OK = () => new Response("data: [DONE]\n\n", { headers: { "content-type": "text/event-stream" } })
|
|
|
|
// chatsOf are the requests sent but the product config, which a chat with
|
|
// no effort of its own may read first for its default (withDefaultEffort)
|
|
const chatsOf = (sent) => sent.filter((s) => !s.url.endsWith("/v3/config"))
|
|
|
|
// record answers each fetch with reply and keeps the URL and headers sent
|
|
function record(reply = OK) {
|
|
const sent = []
|
|
globalThis.fetch = async (url, init) => {
|
|
sent.push({ url: String(url), headers: new Headers(init?.headers) })
|
|
return reply(String(url))
|
|
}
|
|
return sent
|
|
}
|
|
|
|
const chat = (opts, url) =>
|
|
opts.fetch(url, {
|
|
method: "POST",
|
|
// what an SDK given apiKey "" sends: an empty key header and Bearer
|
|
headers: { authorization: "Bearer ", "x-api-key": "" },
|
|
body: JSON.stringify({ model: "glm-5.3", stream: true, messages: [{ role: "user", content: "hi" }] }),
|
|
})
|
|
|
|
for (const [plugin, site, url] of [
|
|
[WorkBuddyAuthPlugin, "workbuddy", "https://copilot.tencent.com/v2/chat/completions"],
|
|
[WorkBuddyAIAuthPlugin, "workbuddy-ai", "https://www.workbuddy.ai/v2/chat/completions"],
|
|
]) {
|
|
test(`${site}: offers an API key sign-in beside the browser and desktop ones`, async () => {
|
|
const hooks = await plugin({ client: {} })
|
|
const ms = hooks.auth.methods
|
|
expect(ms.map((m) => m.type)).toEqual(["oauth", "oauth", "api"])
|
|
const name = _internal.SITES[site].name
|
|
expect(ms[2].label).toBe(`${name} API key`)
|
|
expect(ms[2].placeholder).toContain(site === "workbuddy" ? "www.workbuddy.cn/profile/keys" : "www.workbuddy.ai/profile/keys")
|
|
expect(ms[0].label).toBe(`${name} account (browser)`)
|
|
expect(ms[1].label).toBe(`${name} desktop's sign-in`)
|
|
})
|
|
|
|
test(`${site}: a key's chat carries X-API-Key and Bearer to ${new URL(url).host}`, async () => {
|
|
const sent = record()
|
|
const hooks = await plugin({ client: {} })
|
|
// the provider's own api is where a model's chat goes
|
|
const cfg = { provider: {} }
|
|
await hooks.config(cfg)
|
|
expect(cfg.provider[site].api + "/chat/completions").toBe(url)
|
|
const opts = await hooks.auth.loader(async () => ({ type: "api", key: KEY }))
|
|
expect(typeof opts.fetch).toBe("function")
|
|
const res = await chat(opts, url)
|
|
expect(res.status).toBe(200)
|
|
expect(chatsOf(sent)).toHaveLength(1)
|
|
const h = chatsOf(sent)[0].headers
|
|
expect(chatsOf(sent)[0].url).toBe(url)
|
|
expect(h.get("x-api-key")).toBe(KEY)
|
|
expect(h.get("authorization")).toBe("Bearer " + KEY)
|
|
expect(h.get("x-user-id")).toBe("anonymous_abcdefgh")
|
|
expect(h.has("x-domain")).toBe(false)
|
|
expect(h.has("x-enterprise-id")).toBe(false)
|
|
expect(h.get("user-agent")).toMatch(/^WorkBuddy\//)
|
|
expect(h.get("x-product")).toBe("SaaS")
|
|
})
|
|
|
|
test(`${site}: a browser sign-in's chat is as before, no X-API-Key`, async () => {
|
|
const sent = record()
|
|
const hooks = await plugin({ client: {} })
|
|
const opts = await hooks.auth.loader(async () => ({ type: "oauth", access: "tok", refresh: "r", uid: "u1", expires: Date.now() + 3600_000 }))
|
|
await chat(opts, url)
|
|
const h = chatsOf(sent)[0].headers
|
|
expect(chatsOf(sent)[0].url).toBe(url)
|
|
expect(h.get("authorization")).toBe("Bearer tok")
|
|
expect(h.has("x-api-key")).toBe(false)
|
|
expect(h.get("x-user-id")).toBe("u1")
|
|
expect(h.get("x-domain")).toBe(new URL(url).host)
|
|
})
|
|
|
|
test(`${site}: a refused key is a 401 that says to check the key, never a refresh`, async () => {
|
|
const sent = record(() => new Response('{"code":401}', { status: 401, headers: { "www-authenticate": 'Bearer realm="copilot", error="invalid_token"' } }))
|
|
const hooks = await plugin({ client: {} })
|
|
const opts = await hooks.auth.loader(async () => ({ type: "api", key: KEY }))
|
|
const res = await chat(opts, url)
|
|
expect(res.status).toBe(401)
|
|
expect(chatsOf(sent)).toHaveLength(1) // no refresh asked, no second send
|
|
expect(res.headers.get("X-Magpie-Sign-In")).toBe("expired")
|
|
const { error } = await res.json()
|
|
expect(error.message).toContain("refused this API key (401)")
|
|
expect(error.message).toContain(site === "workbuddy" ? "https://www.workbuddy.cn/profile/keys" : "https://www.workbuddy.ai/profile/keys")
|
|
})
|
|
|
|
test(`${site}: a key's credits are unknown, asked of nothing`, async () => {
|
|
const sent = record(() => new Response("{}", { status: 500 }))
|
|
const hooks = await plugin({ client: {} })
|
|
const u = await hooks.auth.usage(async () => ({ type: "api", key: KEY }))
|
|
expect(sent).toHaveLength(0)
|
|
expect(u.signIn).toBe("kept")
|
|
expect(u.error).toContain("credits unknown")
|
|
expect(u.windows).toBeUndefined()
|
|
})
|
|
}
|
|
|
|
// /v3/config is asked with a key only as the CLI asks it: for ck_ and pt_
|
|
// keys (shouldLoadRemoteConfig); any other key keeps the built-in list
|
|
test("models: a ck_ key asks /v3/config with the key's headers, another key asks nothing", async () => {
|
|
const sent = record(() =>
|
|
Response.json({ code: 0, data: { agents: [{ name: "cli", models: ["glm-9"] }], models: [{ id: "glm-9", name: "GLM-9", maxInputTokens: 1000, maxOutputTokens: 100 }] } }),
|
|
)
|
|
const hooks = await WorkBuddyAuthPlugin({ client: {} })
|
|
const provider = { id: "workbuddy", models: { builtin: { id: "builtin" } } }
|
|
const got = await hooks.provider.models(provider, { auth: { type: "api", key: KEY } })
|
|
expect(sent).toHaveLength(1)
|
|
expect(sent[0].url).toBe("https://copilot.tencent.com/v3/config")
|
|
expect(sent[0].headers.get("x-api-key")).toBe(KEY)
|
|
expect(sent[0].headers.get("authorization")).toBe("Bearer " + KEY)
|
|
// the CLI asks it with its own User-Agent (CLI/<v> in it picks the cli list)
|
|
expect(sent[0].headers.get("user-agent")).toBe("WorkBuddy/5.9.3 WorkBuddy/5.9.3 CLI/2.165.0")
|
|
expect(Object.keys(got)).toEqual(["glm-9"])
|
|
|
|
const other = await hooks.provider.models(provider, { auth: { type: "api", key: "sk-something-else" } })
|
|
expect(sent).toHaveLength(1)
|
|
expect(other).toBe(provider.models)
|
|
})
|
|
|
|
test("an empty key is not signed in", async () => {
|
|
record()
|
|
const hooks = await WorkBuddyAIAuthPlugin({ client: {} })
|
|
const opts = await hooks.auth.loader(async () => ({ type: "api", key: " " }))
|
|
await expect(chat(opts, "https://www.workbuddy.ai/v2/chat/completions")).rejects.toThrow("isn't signed in")
|
|
})
|