diff --git a/packages/orcaterm/README.md b/packages/orcaterm/README.md index 6acd57f..1781f39 100644 --- a/packages/orcaterm/README.md +++ b/packages/orcaterm/README.md @@ -141,8 +141,10 @@ is in that page's HTML — so only do that behind something that restricts who c bind accepts the page from this machine's own interface addresses, so reaching it by LAN address works; every other origin is refused. -The token is renewed before it expires through `OAuthRefreshToken`, so a normal session never asks -you to scan again. +The token is renewed before it expires through `OAuthRefreshToken`. magpie renews it when the +account is used (a request, a usage read) in the last 90 minutes of the token's two hours: OrcaTerm +drops the sign-in together with an expired token, after which renewal is `SESSION_EXPIRED` for good +and only a new scan helps (#675). An account nothing touches for the whole two hours still expires. ### Paste the cookie pair (fallback) @@ -289,6 +291,7 @@ bun test | `audit.test.mjs` | invalid callback bodies; envelope whitespace and examples; EOF error frames; JSON error codes; reader cleanup; read failures and cancellation | | `identity.test.mjs` | stable identity across QR re-sign-ins with rotated tokens or changed nicknames; distinct users staying separate; token user ID fallback; refusing missing identity; renewal and account-scoped backfill preserving credentials and metadata | | `leak.test.mjs` | #564: the reported envelope, hand-written envelopes that aren't JSON, an envelope sent as thinking or kept in the history, a new upstream conversation on every request, and nothing added to another provider's request | +| `renew.test.mjs` | #675: under magpie's renewal rule, an account used once an hour is renewed before its 2h token expires, and a fresh token is not renewed again | | `review.test.mjs` | the cases the reviews turned up: a reply containing `{"action": …}` is not truncated (including one character at a time, and one that ends mid-envelope); the JSON 429 is a 429; pacers are per account; only nine models are listed; a chat makes exactly one request and it is not `/conversation`; and `/cb` refuses a foreign `Origin`, a rebinding page, and a `state` it did not issue | `chunks.test.mjs`'s cancel case fails if `releaseUpstream()` is removed, so the leak stays fixed. @@ -303,7 +306,9 @@ GLM、Kimi。provider id:`orcaterm`。 > 这可能不符合腾讯云的服务条款,接口也可能随时变更。使用风险(包括账号受限)由使用者自行承担。 - **登录**:首选**微信扫码**(`magpie plugin login orcaterm` 第一项),拿到的是 OrcaTerm 自己的 - OAuth accessToken —— 两小时有效、自动续期、只能用于 OrcaTerm。备选是粘贴 `uin` + `skey`: + OAuth accessToken —— 两小时有效、只能用于 OrcaTerm。到期前 90 分钟内账号被用到(请求、读用量)时 + magpie 会自动续期;令牌一旦过期,OrcaTerm 会连同登录会话一起作废,续期只会返回 `SESSION_EXPIRED`, + 只能重新扫码(#675)。两小时内完全没被用到的账号仍会过期。备选是粘贴 `uin` + `skey`: 这两个是**腾讯云控制台的完整会话 cookie**,不是 OrcaTerm 专用令牌 —— 拿到它们就能在腾讯云范围内 以你的身份操作,包括你的服务器和账单。**不要用持有生产资源的账号**,想试就用一个名下没有东西的独立账号。 - **条款**:OrcaTerm 用户协议 2.3 保留所有未明示授权的权利,2.1 把服务限定为 OrcaTerm 本身; diff --git a/packages/orcaterm/index.mjs b/packages/orcaterm/index.mjs index 6a803a5..1f437ac 100644 --- a/packages/orcaterm/index.mjs +++ b/packages/orcaterm/index.mjs @@ -1791,6 +1791,9 @@ async function capiRequest(auth, action, data = {}) { } } +// How long before a WeChat token expires magpie renews it (auth.refreshLead). +const REFRESH_LEAD = 90 * 60 * 1000 + // ---- the plugin itself ------------------------------------------------------ // The named OrcaTermAuthPlugin export is the plugin entry; _internal below is an // object for tests, so magpie does not call it as a second plugin. @@ -1909,6 +1912,19 @@ export const OrcaTermAuthPlugin = async ({ client } = {}) => { // shared with its own desktop client, so one in flight. maxConcurrency: 1, + /** + * How long before `expires` magpie renews the WeChat token (#675). + * OrcaTerm drops the OAuth session together with an expired access + * token: `OAuthRefreshToken` then answers SESSION_EXPIRED for good, and + * only a new scan helps. magpie renews when something uses the account + * (a request, a usage read) once the token is within this lead of + * expiring; the default five minutes left a 2h token renewed only by a + * request in its last five minutes, so an account idle past that was + * signed out. 90 minutes (orinsid's patch, 40+ hours with no expiry) + * makes anything in the last hour and a half renew it. + */ + refreshLead: REFRESH_LEAD, + methods: [ { type: "oauth", diff --git a/packages/orcaterm/package.json b/packages/orcaterm/package.json index 50edecb..02fbb9d 100644 --- a/packages/orcaterm/package.json +++ b/packages/orcaterm/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-orcaterm-auth", - "version": "0.1.3", + "version": "0.1.4", "description": "OrcaTerm (Tencent Cloud 遨驰终端) AI quota as an OpenAI-compatible provider for magpie and OpenCode", "type": "module", "main": "./opencode.mjs", diff --git a/packages/orcaterm/renew.test.mjs b/packages/orcaterm/renew.test.mjs new file mode 100644 index 0000000..873ebad --- /dev/null +++ b/packages/orcaterm/renew.test.mjs @@ -0,0 +1,46 @@ +// #675 (orinsid): a WeChat token lives two hours, and OrcaTerm drops the +// OAuth session with it, so a renewal after expiry is SESSION_EXPIRED for +// good. magpie renews a plugin's sign-in only when something uses the +// account (a request, a usage read) and the token is within auth.refreshLead +// of expiring, five minutes when the plugin doesn't say. These tests apply +// magpie's rule (host.js leadOf/renewAt) to the plugin's hook. +import { expect, test } from "bun:test" +import { OrcaTermAuthPlugin } from "./index.mjs" + +const HOUR = 3600e3 +const TOKEN_LIFE = 2 * HOUR + +// host.js: leadOf and renewAt +const leadOf = (a) => { + const v = a?.refreshLead + return Number.isFinite(v) && v >= 0 ? Math.min(v, 24 * HOUR) : 5 * 60e3 +} +const renewAt = (a, expires) => Math.max(1, expires - leadOf(a)) + +const auth = async () => (await OrcaTermAuthPlugin({ client: { app: { log: () => {} } } })).auth + +test("an account used once an hour is renewed before its token expires", async () => { + const a = await auth() + // a token signed in at 0; the account is used once an hour, at any + // minute past the sign-in: one use must fall where magpie renews + for (let first = 0; first < 60; first++) { + const uses = [] + for (let t = first * 60e3; t < TOKEN_LIFE; t += HOUR) uses.push(t) + const due = uses.filter((t) => t >= renewAt(a, TOKEN_LIFE)) + expect({ first, renewed: due.length > 0 }).toEqual({ first, renewed: true }) + } +}) + +test("a token just renewed isn't renewed again on the next use", async () => { + const a = await auth() + // the lead is shorter than the token's life, so a request right after a + // sign-in or a renewal doesn't spend another refresh + expect(leadOf(a)).toBeLessThan(TOKEN_LIFE - 15 * 60e3) + expect(Date.now() < renewAt(a, Date.now() + TOKEN_LIFE)).toBe(true) +}) + +test("the lead is one magpie takes as said, not its cap", async () => { + const a = await auth() + expect(Number.isFinite(a.refreshLead)).toBe(true) + expect(leadOf(a)).toBe(a.refreshLead) +})