diff --git a/packages/trae/enterprise.test.mjs b/packages/trae/enterprise.test.mjs index f4c5a03..d870ff4 100644 --- a/packages/trae/enterprise.test.mjs +++ b/packages/trae/enterprise.test.mjs @@ -20,7 +20,7 @@ const page = (over = {}) => json({ is_credits_billing: true, user_entitlement_pa { display_desc: "每月登录赠送", entitlement_base_info: { quota: { credits_limit: 500 }, end_time: 1896054399 }, usage: {} }, ...(over.extra ?? []), ] }) -const creditsWindow = { name: "Credits", used: (4.396 / 4500) * 100, display: "4.4 / 4500", amount: 4.4, limit: 4500, unit: "credits", aside: true, resetsAt: new Date(1895012095 * 1000).toISOString() } +const creditsWindow = { name: "Credits", used: (4.396 / 4500) * 100, display: "4.4 / 4500", amount: 4.4, limit: 4500, unit: "credits", aside: true, resetsAt: new Date(1895012095 * 1000).toISOString(), runsOutAt: new Date(1895012095 * 1000).toISOString(), runsOut: 3995.6 } // the console's own period, which the usage page reads its reset date from const RESET = 1796054400000 const core = (over = {}) => json({ code: 0, message: "", Data: { ChatUsed: 0, ChatTotal: 500, PayAsYouGoUsed: 0, PayAsYouGoTotal: 0, ResetTime: RESET, ChatStartTime: 1793462399000, ChatEndTime: 1796054399000, PayAsYouGoStartTime: 1793462399000, PayAsYouGoEndTime: 1796054399000, TotalSeats: 163, ...over }, count: 0 }) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index c0080a3..e68d4ed 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -186,6 +186,7 @@ const SITES = { aside: true, } if (c.until) w.resetsAt = new Date(c.until).toISOString() + if (c.until && c.lapsing > 0) Object.assign(w, { runsOutAt: new Date(c.until).toISOString(), runsOut: round(c.lapsing) }) windows.push(w) } if (windows.length) out.windows = windows @@ -203,6 +204,7 @@ const SITES = { // English balance line beside it (yetone/magpie#694) const w = { name: "Credits", used: Math.max(0, Math.min(100, (c.used / c.limit) * 100)), amount: round(c.used), limit: round(c.limit), unit: "credits" } if (c.until) w.resetsAt = new Date(c.until).toISOString() + if (c.until && c.lapsing > 0) Object.assign(w, { runsOutAt: new Date(c.until).toISOString(), runsOut: round(c.lapsing) }) out.windows = [w] } return out @@ -3242,6 +3244,7 @@ function credits(v, now = Date.now()) { let used = 0 let unlimited = false let soonest = 0 + let lapsing = 0 for (const p of v?.user_entitlement_pack_list ?? []) { const bi = p?.entitlement_base_info ?? {} const l = Number(bi?.quota?.credits_limit) @@ -3252,9 +3255,16 @@ function credits(v, now = Date.now()) { if (u > 0) used += u const end = whenOf(bi.end_time ?? bi.expire_time ?? p.end_time) const left = l === -1 || (l > 0 && u < l) - if (end && end > now && left && (!soonest || end < soonest)) soonest = end + if (!(end && end > now && left)) continue + // what a finite pack has left is lost at its end, whether or not a + // new pack follows: lapsing is the credits that go at the soonest end + // (#718: a TraeCode CN account's sign-in rewards are 31-day packs of + // 100, one a day) + const rest = l === -1 ? 0 : Math.max(0, l - Math.max(0, u)) + if (!soonest || end < soonest) (soonest = end), (lapsing = rest) + else if (end === soonest) lapsing += rest } - return { limit, used, unlimited, until: soonest } + return { limit, used, unlimited, until: soonest, lapsing } } const round = (n) => Math.round(n * 100) / 100 diff --git a/packages/trae/package.json b/packages/trae/package.json index f81affd..8af39cf 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.37", + "version": "0.2.38", "description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in", "type": "module", "main": "./opencode.mjs", diff --git a/packages/trae/usage.test.mjs b/packages/trae/usage.test.mjs index 7ddb1bf..f94cd70 100644 --- a/packages/trae/usage.test.mjs +++ b/packages/trae/usage.test.mjs @@ -23,7 +23,7 @@ test("credits are the packs' limits and what they used", async () => { expect(u.user).toBe("Ann") expect(u.signIn).toBe("kept") expect(u.balance).toBeUndefined() // the window carries the count (yetone/magpie#694) - expect(u.windows).toEqual([{ name: "Credits", used: 25, amount: 100, limit: 400, unit: "credits", resetsAt: new Date(4102444800 * 1000).toISOString() }]) + expect(u.windows).toEqual([{ name: "Credits", used: 25, amount: 100, limit: 400, unit: "credits", resetsAt: new Date(4102444800 * 1000).toISOString(), runsOutAt: new Date(4102444800 * 1000).toISOString(), runsOut: 225 }]) }) test("unlimited packs say so", () => { @@ -388,3 +388,25 @@ test("the soonest end is a live pack's", () => { // nothing live: no date at all expect(_internal.credits({ user_entitlement_pack_list: [pack(300, 300, "2026-11-20T00:00:00Z")] }, now).until).toBe(0) }) + +// gcq909893267's account B on #718, its packs as Trae answered them (ids +// out): the monthly pack, a spent sign-in reward and one with 95.85 left +test("a sign-in reward pack's credits run out at its end", async () => { + const packs = [ + { display_desc: "每月登录赠送", entitlement_base_info: { charge_amount: 0, currency: 1, end_time: 1796054399, ent_status: 0, product_extra: { package_extra: { duration: 1, package_duration_type: 1, package_source_type: 0, quota: { credits_limit: 500 } } }, product_id: 221, product_type: 2, quota: { credits_limit: 500 }, start_time: 1793462400 }, expire_time: 1796054399, group_name: "每月登录积分", group_type: 3, is_hide: false, is_last_period: false, is_oneweek: false, next_billing_time: 0, source_id: "", status: 1, usage: {}, yearly_expire_time: 0 }, + { display_desc: "签到奖励", entitlement_base_info: { charge_amount: 0, currency: 1, end_time: 1794758624, ent_status: 0, product_extra: { package_extra: { duration: 31, package_duration_type: 0, package_name: "签到奖励", package_source_type: 9, quota: { credits_limit: 100 } } }, product_id: 208, product_type: 2, quota: { credits_limit: 100 }, start_time: 1792080224 }, expire_time: 1794758624, group_name: "每日签到", group_type: 1, is_hide: false, is_last_period: false, is_oneweek: false, next_billing_time: 0, source_id: "", status: 1, usage: { credits_amount: 100 }, yearly_expire_time: 0 }, + { display_desc: "签到奖励", entitlement_base_info: { charge_amount: 0, currency: 1, end_time: 1794931244, ent_status: 0, product_extra: { package_extra: { duration: 31, package_duration_type: 0, package_name: "签到奖励", package_source_type: 9, quota: { credits_limit: 100 } } }, product_id: 208, product_type: 2, quota: { credits_limit: 100 }, start_time: 1792252844 }, expire_time: 1794931244, group_name: "每日签到", group_type: 1, is_hide: false, is_last_period: false, is_oneweek: false, next_billing_time: 0, source_id: "", status: 1, usage: { credits_amount: 4.1456 }, yearly_expire_time: 0 }, + ] + const now = Date.parse("2026-11-14T16:31:21Z") + const c = _internal.credits({ user_entitlement_pack_list: packs }, now) + expect(c.until).toBe(1794931244 * 1000) // 11-18 00:00:44 Beijing, not the spent 11-16 pack + expect(Math.round(c.lapsing * 100) / 100).toBe(95.85) + // two rewards ending at the same second lapse together + const twin = structuredClone(packs[2]) + twin.usage = { credits_amount: 0 } + expect(Math.round(_internal.credits({ user_entitlement_pack_list: [...packs, twin] }, now).lapsing * 100) / 100).toBe(195.85) + // once both rewards are past, the monthly pack is what lapses, all 500 + const later = _internal.credits({ user_entitlement_pack_list: packs }, Date.parse("2026-11-20T00:00:00Z")) + expect(later.until).toBe(1796054399 * 1000) + expect(later.lapsing).toBe(500) +})