From b9cbb9dd0187c7f95ecf456262ec7693a23bc78c Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 30 Oct 2026 11:01:20 +0800 Subject: [PATCH] =?UTF-8?q?workbuddy=200.2.1:=20an=20enterprise=20(?= =?UTF-8?q?=E6=95=99=E8=82=B2=E5=85=AC=E7=9B=8A=E7=89=88,=20team)=20sign-i?= =?UTF-8?q?n=20is=20named=20with=20its=20enterprise,=20kept=20beside=20the?= =?UTF-8?q?=20personal=20one?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A person's personal account and their enterprise account (a team, or 「WorkBuddy 教育公益版」, picked on WorkBuddy's sign-in page after the phone sign-in) share the uid and the nickname. magpie's plugin host keeps one account per name and uid (settle in host.js), so signing in to the education account replaced the same person's personal account, and the two could never sit in one pool. A browser sign-in that WorkBuddy places in an enterprise is now named "nickname · enterprise name": the name from /login/account, else from the matching /v2/plugin/accounts entry, else "enterprise ". A personal account's name is unchanged. Old data: an enterprise account signed in before keeps its old name until its Sign in again, which uses the same key and renames it in place; its free-model window, read from ledger rows under the old name, starts again (README says so). Run (mote, HOME=$(mktemp -d)): bun test in packages/workbuddy, 256 pass 0 fail; bun scripts/check.mjs ok. Broken by hand: accountName returning the nickname alone turns the 4 new enterprise.test.mjs tests red. Not tried: a real education account (the owner has none; the live /v2/plugin/accounts shows only a personal entry), so enterpriseName's presence in WorkBuddy's replies for 教育公益版 is from the web client's SelectAccount code, not a live reply; the id fallback covers its absence. --- packages/workbuddy/README.md | 13 +++++++ packages/workbuddy/enterprise.test.mjs | 36 +++++++++++++++++- packages/workbuddy/index.mjs | 51 ++++++++++++++++++++------ packages/workbuddy/package.json | 2 +- 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index 604e8aa..56b147e 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -61,6 +61,19 @@ as WorkBuddy's own client sends them; without them WorkBuddy answers as the personal account, with the personal plan's models. A browser sign-in made before 0.1.13 has no enterprise id kept: sign in again. +Which account a browser sign-in gives is chosen on WorkBuddy's own sign-in +page. After a phone sign-in it lists the person's accounts (personal, a +team, 「WorkBuddy 教育公益版」…) and binds the one picked to the sign-in, so +to add an education or team account, pick it there. Its account is named +with the enterprise too (`nickname · enterprise name`, or the end of the +enterprise id when WorkBuddy names none): one person's personal and +enterprise accounts share the uid and the nickname, and magpie keeps one +account per name and uid, so before 0.2.1 signing in to the enterprise +account replaced the personal one. An enterprise account signed in before +0.2.1 keeps its old name until its Sign in again, which renames it in +place; its free-model window, read from usage rows kept under the old +name, starts again from the new one. + Desktop's sign-in is refreshed only when the app hasn't refreshed it itself, and the new token is kept in memory, never written back, as magpie's built-in WorkBuddy does. diff --git a/packages/workbuddy/enterprise.test.mjs b/packages/workbuddy/enterprise.test.mjs index 57ab6b0..b13d7a7 100644 --- a/packages/workbuddy/enterprise.test.mjs +++ b/packages/workbuddy/enterprise.test.mjs @@ -64,12 +64,46 @@ const signIn = async (account, accounts) => { } test("signing in as an enterprise keeps the enterprise WorkBuddy named", async () => { - const { result, asked } = await signIn({ uid: "u", nickname: "n", type: "ultimate", enterpriseId: "ent-1" }, []) + const { result, asked } = await signIn( + { uid: "u", nickname: "n", type: "ultimate", enterpriseId: "ent-1", enterpriseName: "WorkBuddy 教育公益版" }, + [], + ) expect(result.type).toBe("success") expect(result.enterpriseId).toBe("ent-1") + expect(result.accountId).toBe("n · WorkBuddy 教育公益版") expect(asked.some((p) => p.endsWith("/accounts"))).toBe(false) }) +test("an enterprise named by id only takes its name from the account list", async () => { + const { result } = await signIn({ uid: "u", nickname: "n", type: "ultimate", enterpriseId: "ent-1" }, [ + { uid: "u", type: "personal" }, + { uid: "u", type: "ultimate", enterpriseId: "ent-1", enterpriseName: "某某中学" }, + ]) + expect(result.enterpriseId).toBe("ent-1") + expect(result.accountId).toBe("n · 某某中学") +}) + +test("an enterprise WorkBuddy names nowhere is told apart by its id", async () => { + const { result } = await signIn({ uid: "u", nickname: "n", type: "ultimate", enterpriseId: "ent-123456789" }, []) + expect(result.enterpriseId).toBe("ent-123456789") + expect(result.accountId).toBe("n · enterprise 456789") +}) + +// One person's personal and education accounts share the uid and the +// nickname; magpie keeps one account per name and uid, so the two names +// must differ or the second sign-in replaces the first. +test("a person's personal and enterprise sign-ins are named apart", async () => { + const list = [ + { uid: "u", type: "personal", lastLogin: false }, + { uid: "u", type: "ultimate", enterpriseId: "ent-2", enterpriseName: "WorkBuddy 教育公益版", lastLogin: true }, + ] + const personal = (await signIn({ uid: "u", nickname: "n", type: "personal" }, list)).result + const edu = (await signIn({ uid: "u", nickname: "n", type: "ultimate" }, list)).result + expect(personal.accountId).toBe("n") + expect(edu.accountId).toBe("n · WorkBuddy 教育公益版") + expect(personal.uid).toBe(edu.uid) +}) + test("an enterprise account named without its id is found in the account list", async () => { const { result } = await signIn({ uid: "u", nickname: "n", type: "ultimate" }, [ { uid: "u", type: "personal", lastLogin: false }, diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 97bd442..89b5fa4 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -415,16 +415,26 @@ function enterpriseHeaders(a) { return id ? { "X-Enterprise-Id": id, "X-Tenant-Id": id } : {} } -// enterpriseOf is the enterprise the account a sign-in gave belongs to: -// the one /login/account names, else, for an account that isn't personal, -// the one WorkBuddy's account list has for it — the same choice WorkBuddy's -// own client makes (mergeCurrentAccountDetails): the uid's only entry of that -// type, or the only one it last signed in to. None found is personal. +// enterpriseOf is the enterprise the account a sign-in gave belongs to, +// { id, name }: the one /login/account names, else, for an account that +// isn't personal, the one WorkBuddy's account list has for it — the same +// choice WorkBuddy's own client makes (mergeCurrentAccountDetails): the +// uid's only entry of that type, or the only one it last signed in to. None +// found is personal ({ id: "" }). +// +// Which account that is was chosen on WorkBuddy's sign-in page: after a +// phone sign-in it lists the person's accounts (personal, a team, +// 「WorkBuddy 教育公益版」…) and binds the one picked to the sign-in's state +// (POST /console/login/enterprise/?state=…), so the token and +// /login/account this plugin reads by that state are the chosen account's. async function enterpriseOf(site, a, who) { + const name = (x) => (typeof x?.enterpriseName === "string" ? x.enterpriseName.trim() : "") const own = typeof who?.enterpriseId === "string" ? who.enterpriseId.trim() : "" - if (own) return own const type = String(who?.type ?? "").trim().toLowerCase() - if (!type || type === "personal") return "" + if (!own && (!type || type === "personal")) return { id: "", name: "" } + // a name /login/account leaves out is the account list's, as the + // client merges the two + if (own && name(who)) return { id: own, name: name(who) } let list try { list = await call(site, "GET", "/v2/plugin/accounts", { @@ -432,13 +442,29 @@ async function enterpriseOf(site, a, who) { "X-Domain": domainOf(site, a), }) } catch { - return "" + return { id: own, name: "" } } const mine = (list?.accounts ?? []).filter((x) => x?.uid === who.uid && typeof x.enterpriseId === "string" && x.enterpriseId.trim()) + if (own) return { id: own, name: name(mine.find((x) => x.enterpriseId.trim() === own)) } const same = mine.filter((x) => String(x.type ?? "").trim().toLowerCase() === type) const last = mine.filter((x) => x.lastLogin) const pick = same.length === 1 ? same[0] : last.length === 1 ? last[0] : mine.length === 1 ? mine[0] : undefined - return pick ? pick.enterpriseId.trim() : "" + return pick ? { id: pick.enterpriseId.trim(), name: name(pick) } : { id: "", name: "" } +} + +// accountName is what magpie lists an account a sign-in gave as: the +// person's name, and for an enterprise (team, education) account the +// enterprise's too. One person's personal account and their enterprise one +// share the uid and the nickname, and magpie's plugin host keeps one +// account per name and uid (settle in host.js): named alike, signing in to +// 「WorkBuddy 教育公益版」 took the place of the same person's personal +// account, and the two could never be in one pool. An enterprise WorkBuddy +// names no name for is told by the end of its id. +function accountName(who, ent) { + const person = who?.nickname || who?.phoneNumber || who?.uid || "" + if (!ent?.id) return person + const team = ent.name || `enterprise ${ent.id.slice(-6)}` + return person ? `${person} · ${team}` : team } // WorkBuddy's own client names each chat to its gateway four ways: the @@ -1541,8 +1567,9 @@ async function browserSignIn(site) { "X-No-Enterprise-Id": "true", }, [12151], deadline) a.uid = who.uid ?? "" - a.enterpriseId = await enterpriseOf(site, a, who) - return success(site, a, who.nickname || who.phoneNumber || who.uid) + const ent = await enterpriseOf(site, a, who) + a.enterpriseId = ent.id + return success(site, a, accountName(who, ent)) } catch (e) { return { type: "failed", error: e.message } } @@ -3504,4 +3531,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { idFor, replyAfterToolsOn, withDefaultEffort, defaultEffortOf, gzipped, FREE_ONLY, PRICE_TTL, keyAccount, remoteConfig, cycleOf, typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, enterpriseUsageOf, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, asReasoning, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, scopeOf, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, prepared, answeredBefore, nativeOf, modelOf, configModels, freeWindowOf, tokensShort, freeWindowRows, freeWindowLedger, readLedgerTail, pinned, rowTime, rowAccount, ofAccount, windowOffset, windowReset, FREE_WINDOW_MODELS, FREE_WINDOW_SPAN, FREE_WINDOW_GAP, FREE_WINDOW_LEAD, checkinOf, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } +export const _internal = { idFor, replyAfterToolsOn, withDefaultEffort, defaultEffortOf, gzipped, FREE_ONLY, PRICE_TTL, keyAccount, remoteConfig, cycleOf, typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, enterpriseUsageOf, accountName, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, asReasoning, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, scopeOf, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, prepared, answeredBefore, nativeOf, modelOf, configModels, freeWindowOf, tokensShort, freeWindowRows, freeWindowLedger, readLedgerTail, pinned, rowTime, rowAccount, ofAccount, windowOffset, windowReset, FREE_WINDOW_MODELS, FREE_WINDOW_SPAN, FREE_WINDOW_GAP, FREE_WINDOW_LEAD, checkinOf, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 83e65f7..bb5ab1a 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.0", + "version": "0.2.1", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs",