trae 0.2.28: a model's rate comes from display_contact_config (#690)

A Trae model's rate (and the rate before a discount) now comes from its
list entry's display_contact_config: consumption_rate when enabled, the
member discount's consumption_rate when that discount is enabled, and an
off-peak activity's after/before price by its time_windows in its own
time zone, computed when the list is read. A field that throws (an
unknown time_zone) costs that model its rate only; the rest of the live
list stays. Fixtures are the reporter's literal display_contact_config
strings from batch_get_detail_param.

Run at the reviewed head f47880ff on mote:
- bun test packages/trae: 234 pass, 0 fail.
- Broken by hand: making traeRateOf's outer catch rethrow turns "a broken
  time zone costs that model its rate only, not the list" red (1 fail).
- Real samples probed under TZ=UTC and TZ=America/Los_Angeles: deepseek
  0.06/was 0.12 at 08:59, 12:30, 18:00 weekdays and all weekend
  (Asia/Shanghai), 0.12 at 09:00 and 14:00; glm-5.3 0.58/was 0.78; a
  Bogus/Zone entry gives 0.12; missing, bad JSON, null and arrays give
  nothing.

Not tried: a real Trae account through magpie. The rate is computed when
the host reads the list, so it can lag a window edge until the next read.
This commit is contained in:
shadyrispy
2026-11-04 12:20:06 +08:00
committed by GitHub
parent 270b228a96
commit ef673b2b59
3 changed files with 167 additions and 6 deletions
+76 -1
View File
@@ -2781,6 +2781,80 @@ const maxModel = (...ms) => {
// deepseek-v4.1-flash in Max mode
const MAX = "-max"
// traeRateOf is a model's consumption multiplier, from the account's list:
// display_contact_config is a JSON string naming the rate now and the full
// price, and an activity's off-peak windows, which are computed here rather
// than trusted from the snapshot the list was read at (a list fetched in
// the morning would otherwise say the day price all evening).
const traeRateOf = (m, now = Date.now()) => {
// A field written badly (an unknown time_zone throws in Intl) must cost
// this model its rate alone: this runs inside modelOf, whose caller falls
// back to the static list on a throw, losing every model of the list.
const read = (raw) => {
if (!raw) return {}
let cfg
try { cfg = typeof raw === "string" ? JSON.parse(raw) : raw } catch { return {} }
if (!cfg || typeof cfg !== "object") return {}
const num = (x) => (x === null || x === "" || x === undefined ? undefined : Number.isFinite(Number(x)) ? Number(x) : undefined)
const activity = cfg.activity_discount
const offPeak = activity?.data?.off_peak
const windows = Array.isArray(offPeak?.time_windows) ? offPeak.time_windows : []
// enable: false says the rate is not in force; the list may keep a
// stale data.rate there, so it is not shown.
let rate = cfg.consumption_rate?.enable === false ? undefined : num(cfg.consumption_rate?.data?.rate)
// A member discount runs alongside consumption_rate, which keeps the
// price before it (0.78 while discount.data.consumption_rate is 0.58):
// the discounted price is the one to show.
const discount = cfg.discount
if (discount?.enable) {
const after = num(discount?.data?.consumption_rate)
if (after !== undefined) rate = after
}
const was = num(activity?.data?.current?.before_consumption_rate)
?? num(offPeak?.before_consumption_rate)
?? num(discount?.data?.original_consumption_rate)
if (activity?.enable && windows.length) {
const zone = typeof offPeak?.time_zone === "string" && offPeak.time_zone ? offPeak.time_zone : "Asia/Shanghai"
rate = inWindows(windows, zone, now)
? num(offPeak?.after_consumption_rate) ?? rate
: num(offPeak?.before_consumption_rate) ?? rate
}
return {
...(rate !== undefined ? { rate } : {}),
...(rate !== undefined && was !== undefined && was > rate ? { rateWas: was } : {}),
}
}
try {
return read(m?.display_contact_config)
} catch {
// a bad window costs the rate only; give back what the plain field says
try {
const cfg = typeof m?.display_contact_config === "string" ? JSON.parse(m.display_contact_config) : m?.display_contact_config
const plain = cfg?.consumption_rate?.enable === false ? undefined : Number(cfg?.consumption_rate?.data?.rate)
return Number.isFinite(plain) ? { rate: plain } : {}
} catch { return {} }
}
}
// inWindows says whether now falls in one of the windows, as the list
// spells them: weekdays 1–7 (Monday first), minutes from midnight, and
// the window's own time zone.
const inWindows = (windows, timeZone, now) => {
const parts = new Intl.DateTimeFormat("en-US", {
timeZone, weekday: "short", hour: "2-digit", minute: "2-digit", hourCycle: "h23",
}).formatToParts(new Date(now))
const get = (t) => parts.find((p) => p.type === t)?.value
const weekday = { Sun: 7, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 }[get("weekday")]
const hour = Number(get("hour"))
const minute = Number(get("minute"))
if (!Number.isFinite(weekday) || !Number.isFinite(hour) || !Number.isFinite(minute)) return false
const mins = hour * 60 + minute
return windows.some((w) => Array.isArray(w?.weekdays) && w.weekdays.includes(weekday)
&& Number.isFinite(w.start_minute) && Number.isFinite(w.end_minute)
&& mins >= w.start_minute && mins < w.end_minute)
}
// ---- reasoning levels --------------------------------------------------------------
//
// A list entry carries a reasoning_effort_config: the levels its model takes
@@ -3402,6 +3476,7 @@ const makePlugin = (site) => async ({ client }) => {
// for every model it hands over — so only a non-empty one is kept, or a
// model in the fallback table would lose the levels its entry names.
variants: Object.keys(was.variants ?? {}).length ? was.variants : variantsOf(levelsOf(m)),
...traeRateOf(m),
}
}
@@ -3598,4 +3673,4 @@ const makePlugin = (site) => async ({ client }) => {
export const TraeCNAuthPlugin = makePlugin(SITES["trae-cn"])
export const TraeGlobalAuthPlugin = makePlugin(SITES["trae-global"])
export const _internal = { ownClient, clientStoragePaths, clientDevice, clientProfiles, deviceNote, clientDecode, thisMachine, traeHost, accountOf, HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, nameCall, toolNamed, unwrapped, traeMessages, chatBody, nativeTools, moonshotSchema, credits, dollarUsageOf, whenOf, newDevice, ugDevice, proofOf, SITES, SESSION, conversationID, LEVELS, levelsOf, variantsOf, effortOf }
export const _internal = { ownClient, clientStoragePaths, clientDevice, clientProfiles, deviceNote, clientDecode, thisMachine, traeHost, accountOf, HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, nameCall, toolNamed, unwrapped, traeMessages, chatBody, nativeTools, moonshotSchema, credits, dollarUsageOf, whenOf, newDevice, ugDevice, proofOf, SITES, SESSION, conversationID, LEVELS, levelsOf, variantsOf, effortOf, traeRateOf }
+67
View File
@@ -107,3 +107,70 @@ test("Trae Global's models carry capabilities as well", async () => {
const ms = await hooks.provider.models({ models: { ..._internal.SITES["trae-global"].models } }, { auth: signedIn() })
expect(ms["gemini-3.1-pro-paygo"].capabilities.reasoning).toBe(true)
})
// ---- rate: the account's consumption multiplier ---------------------------
// Real display_contact_config strings, as Trae CN's batch_get_detail_param
// returned them on 2026-11-04 (the only field a model's list entry carries
// about its price; no account data beyond the identity list it always has).
const ACTIVITY_CONTACT = String.raw`{"access":{"data":{"identity_list":[0,5,1,2,3,100]}},"activity_discount":{"enable":true,"subKey":"off_peak_member_discount_18_9","data":{"current":{"discount_type":"none","before_consumption_rate":0.12,"consumption_rate":0.12,"discount":100},"member":{"before_consumption_rate":0.12,"after_consumption_rate":0.09,"discount":75},"off_peak":{"before_consumption_rate":0.12,"after_consumption_rate":0.06,"discount":50,"time_windows":[{"weekdays":[1,2,3,4,5],"start_minute":0,"end_minute":540},{"weekdays":[1,2,3,4,5],"start_minute":720,"end_minute":840},{"weekdays":[1,2,3,4,5],"start_minute":1080,"end_minute":1440},{"weekdays":[6,7],"start_minute":0,"end_minute":1440}],"time_zone":"Asia/Shanghai"}}},"consumption_rate":{"enable":true,"data":{"rate":0.12}},"multimodal":{"enable":true},"reasoning":{"enable":true}}`
const DISCOUNT_CONTACT = String.raw`{"access":{"data":{"identity_list":[0,5,1,2,3,100]}},"consumption_rate":{"enable":true,"data":{"rate":0.78}},"discount":{"enable":true,"subKey":"member_discount","data":{"original_consumption_rate":0.78,"consumption_rate":0.58,"member_discount":75,"is_discount_matched":false}},"reasoning":{"enable":true}}`
test("a model's rate comes from its real display_contact_config", () => {
const rateOf = _internal.traeRateOf
const contact = (raw) => ({ display_contact_config: raw })
// Tuesday 08:00 Beijing is inside the off-peak window, 10:00 is not
expect(rateOf(contact(ACTIVITY_CONTACT), Date.parse("2026-11-04T00:00:00Z"))).toEqual({ rate: 0.06, rateWas: 0.12 })
expect(rateOf(contact(ACTIVITY_CONTACT), Date.parse("2026-11-04T02:00:00Z"))).toEqual({ rate: 0.12 })
// the member discount: consumption_rate.rate is already the discounted price
expect(rateOf(contact(DISCOUNT_CONTACT))).toEqual({ rate: 0.58, rateWas: 0.78 })
// no contact config, broken JSON, or a non-object: no rate at all
expect(rateOf({})).toEqual({})
expect(rateOf(contact("{"))).toEqual({})
expect(rateOf(contact("null"))).toEqual({})
})
test("consumption_rate.enable: false is not a rate, even with a stale one in data", () => {
const rateOf = _internal.traeRateOf
const raw = JSON.stringify({ consumption_rate: { enable: false, data: { rate: 2 } } })
expect(rateOf({ display_contact_config: raw })).toEqual({})
})
test("a broken time zone costs that model its rate only, not the list", async () => {
const broken = JSON.stringify({
activity_discount: { enable: true, data: { off_peak: {
before_consumption_rate: 0.12, after_consumption_rate: 0.06,
time_windows: [{ weekdays: [1, 2, 3, 4, 5], start_minute: 0, end_minute: 540 }],
time_zone: "Bogus/Zone",
} } },
consumption_rate: { enable: true, data: { rate: 0.12 } },
})
const ms = await live([
{ config_name: "broken-model", usage: "chat_completion",
display_config: { display_name: "Broken" },
model_detail_list: [{ model_name: "broken-model__dev", max_tokens: 64000 }],
display_contact_config: broken },
{ config_name: "good-model", usage: "chat_completion",
display_config: { display_name: "Good" },
model_detail_list: [{ model_name: "good-model__dev", max_tokens: 64000 }],
display_contact_config: DISCOUNT_CONTACT },
])
// both models are still there: the bad field did not fall the list back
expect(Object.keys(ms).sort()).toEqual(["broken-model", "good-model"])
// the broken one falls back to the plain rate it can read
expect(ms["broken-model"].rate).toBe(0.12)
// and the good one keeps its own
expect(ms["good-model"].rate).toBe(0.58)
expect(ms["good-model"].rateWas).toBe(0.78)
})
test("provider.models carries the rate of a listed model", async () => {
const ms = await live([{
config_name: "deepseek-v4.1-flash", usage: "chat_completion",
display_config: { display_name: "DeepSeek-V4.1-Flash" },
context_window_tokens: { dev: 1_000_000 },
model_detail_list: [{ model_name: "deepseek-v4.1-flash__dev", max_tokens: 64000 }],
display_contact_config: ACTIVITY_CONTACT,
}])
expect(ms["deepseek-v4.1-flash"].rate).toBeDefined()
})
+24 -5
View File
@@ -1,12 +1,31 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.2.27",
"version": "0.2.28",
"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",
"exports": { ".": "./opencode2.mjs" },
"files": ["index.mjs", "opencode.mjs", "opencode2.mjs", "README.md"],
"keywords": ["opencode", "opencode-plugin", "magpie", "trae", "trae-cn", "trae-global", "bytedance"],
"exports": {
".": "./opencode2.mjs"
},
"files": [
"index.mjs",
"opencode.mjs",
"opencode2.mjs",
"README.md"
],
"keywords": [
"opencode",
"opencode-plugin",
"magpie",
"trae",
"trae-cn",
"trae-global",
"bytedance"
],
"license": "MIT",
"repository": { "type": "git", "url": "git+https://github.com/magpie-community/plugins.git", "directory": "packages/trae" }
"repository": {
"type": "git",
"url": "git+https://github.com/magpie-community/plugins.git",
"directory": "packages/trae"
}
}