@te-river/opencode-alibabatokenplan 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +86 -0
- package/dist/core.mjs +918 -0
- package/dist/server.js +1258 -0
- package/dist/tui.js +1142 -0
- package/package.json +79 -0
- package/src/core/bailian-snapshot.ts +146 -0
- package/src/core/index.ts +10 -0
- package/src/core/merge.ts +148 -0
- package/src/core/qoder-snapshot.ts +153 -0
- package/src/core/rules.ts +105 -0
- package/src/core/status.ts +407 -0
- package/src/core/text.ts +68 -0
- package/src/core/types.ts +102 -0
- package/src/server/index.ts +476 -0
- package/src/server/panel.html +137 -0
- package/src/tui/index.tsx +418 -0
- package/src/tui/read-status.ts +45 -0
package/dist/core.mjs
ADDED
|
@@ -0,0 +1,918 @@
|
|
|
1
|
+
// src/core/rules.ts
|
|
2
|
+
var SH_OFFSET_MIN = 8 * 60;
|
|
3
|
+
var DAY_MIN = 1440;
|
|
4
|
+
function shanghaiMinutes(now = /* @__PURE__ */ new Date()) {
|
|
5
|
+
const shifted = new Date(now.getTime() + (now.getTimezoneOffset() + SH_OFFSET_MIN) * 6e4);
|
|
6
|
+
return shifted.getHours() * 60 + shifted.getMinutes() + shifted.getSeconds() / 60;
|
|
7
|
+
}
|
|
8
|
+
function parseHm(s) {
|
|
9
|
+
const [h, min] = s.split(":").map(Number);
|
|
10
|
+
return h * 60 + (min || 0);
|
|
11
|
+
}
|
|
12
|
+
function windowState(rule, now = /* @__PURE__ */ new Date()) {
|
|
13
|
+
const start = parseHm(rule.start);
|
|
14
|
+
const end = parseHm(rule.end);
|
|
15
|
+
const m = shanghaiMinutes(now);
|
|
16
|
+
const wraps = start > end;
|
|
17
|
+
const active = wraps ? m >= start || m < end : m >= start && m < end;
|
|
18
|
+
let toNext;
|
|
19
|
+
let kind;
|
|
20
|
+
if (active) {
|
|
21
|
+
toNext = wraps ? m >= start ? end + DAY_MIN - m : end - m : end - m;
|
|
22
|
+
kind = "end";
|
|
23
|
+
} else {
|
|
24
|
+
toNext = m < start ? start - m : start + DAY_MIN - m;
|
|
25
|
+
kind = "start";
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
active,
|
|
29
|
+
minutesToNextBoundary: Math.max(0, Math.round(toNext)),
|
|
30
|
+
boundaryKind: kind,
|
|
31
|
+
nextBoundaryAtMs: now.getTime() + toNext * 6e4,
|
|
32
|
+
nextBoundaryAt: shanghaiISO(now.getTime() + toNext * 6e4)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function shanghaiISO(ms) {
|
|
36
|
+
const d = new Date(ms + SH_OFFSET_MIN * 6e4);
|
|
37
|
+
const p = (n, w = 2) => String(n).padStart(w, "0");
|
|
38
|
+
return d.getUTCFullYear() + "-" + p(d.getUTCMonth() + 1) + "-" + p(d.getUTCDate()) + "T" + p(d.getUTCHours()) + ":" + p(d.getUTCMinutes()) + ":" + p(d.getUTCSeconds()) + "." + p(d.getUTCMilliseconds(), 3) + "+08:00";
|
|
39
|
+
}
|
|
40
|
+
function ruleWindow(rule, now = /* @__PURE__ */ new Date()) {
|
|
41
|
+
const w = windowState(rule, now);
|
|
42
|
+
return { active: w.active, minutesToNextBoundary: w.minutesToNextBoundary };
|
|
43
|
+
}
|
|
44
|
+
function fmtCountdown(mins) {
|
|
45
|
+
if (mins <= 0) return "\u4E0D\u8DB3 1 \u5206\u949F";
|
|
46
|
+
const d = Math.floor(mins / DAY_MIN);
|
|
47
|
+
const h = Math.floor(mins % DAY_MIN / 60);
|
|
48
|
+
const m = mins % 60;
|
|
49
|
+
const parts = [];
|
|
50
|
+
if (d) parts.push(d + " \u5929");
|
|
51
|
+
if (h) parts.push(h + " \u5C0F\u65F6");
|
|
52
|
+
parts.push(m + " \u5206\u949F");
|
|
53
|
+
return parts.join(" ");
|
|
54
|
+
}
|
|
55
|
+
function fmtHMS(msRemaining) {
|
|
56
|
+
const total = Math.max(0, Math.floor(msRemaining / 1e3));
|
|
57
|
+
const h = Math.floor(total / 3600);
|
|
58
|
+
const m = Math.floor(total % 3600 / 60);
|
|
59
|
+
const s = total % 60;
|
|
60
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
61
|
+
return `${p(h)}:${p(m)}:${p(s)}`;
|
|
62
|
+
}
|
|
63
|
+
function fmtShortDur(msRemaining) {
|
|
64
|
+
const total = Math.max(0, Math.floor(msRemaining / 6e4));
|
|
65
|
+
const d = Math.floor(total / DAY_MIN);
|
|
66
|
+
const h = Math.floor(total % DAY_MIN / 60);
|
|
67
|
+
const m = total % 60;
|
|
68
|
+
if (d) return `${d}d${h}h`;
|
|
69
|
+
if (h) return `${h}h${m}m`;
|
|
70
|
+
return `${m}m`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/core/text.ts
|
|
74
|
+
function fmtFactor(f) {
|
|
75
|
+
if (f === void 0 || !Number.isFinite(f)) return void 0;
|
|
76
|
+
let s = f.toFixed(2);
|
|
77
|
+
s = s.replace(/0+$/, "").replace(/\.$/, ".0");
|
|
78
|
+
return s + "x";
|
|
79
|
+
}
|
|
80
|
+
function showStrike(base, cur) {
|
|
81
|
+
const b = fmtFactor(base), c = fmtFactor(cur);
|
|
82
|
+
return !!b && !!c && b !== c;
|
|
83
|
+
}
|
|
84
|
+
function strikethrough(s) {
|
|
85
|
+
return s.replace(/[^\u0300-\u036f]/g, (c) => c + "\u0336");
|
|
86
|
+
}
|
|
87
|
+
function windowCountdownText(start, end, now = /* @__PURE__ */ new Date()) {
|
|
88
|
+
const w = windowState({ start, end }, now);
|
|
89
|
+
return w.active ? `${fmtCountdown(w.minutesToNextBoundary)}\u540E\u7ED3\u675F\uFF08${end}\uFF09` : `${fmtCountdown(w.minutesToNextBoundary)}\u540E\u5F00\u59CB\uFF08${start}\uFF09`;
|
|
90
|
+
}
|
|
91
|
+
function sourceBadgeLine(st) {
|
|
92
|
+
const cat = st.sources.catalog;
|
|
93
|
+
const catLabel = cat === "qoder-snapshot" ? "Qoder\u5FEB\u7167" : cat === "bailian-live" ? "\u5B9E\u65F6" : cat === "bailian-cli" ? "CLI" : "\u5FEB\u7167";
|
|
94
|
+
const t = st.updatedAt ? new Date(st.updatedAt) : void 0;
|
|
95
|
+
const hhmm = t && !Number.isNaN(t.getTime()) ? t.toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit", hour12: false, timeZone: "Asia/Shanghai" }) : "";
|
|
96
|
+
return `\u6E90:${catLabel}${hhmm ? " " + hhmm : ""} \xB7 Qoder\u5FEB\u7167${st.sources.qoderSnapshot.slice(5)}`;
|
|
97
|
+
}
|
|
98
|
+
function homeSummaryLine(st, now = /* @__PURE__ */ new Date()) {
|
|
99
|
+
const w = st.windows.find((x) => x.id.startsWith("offpeak")) ?? st.windows[0];
|
|
100
|
+
if (!w) return "Token Plan \u6682\u65E0\u5DF2\u77E5\u4F18\u60E0";
|
|
101
|
+
const starModel = w.models.find((m) => m.model === "qwen3.8-max") ?? w.models.find((m) => m.model.startsWith("qwen")) ?? w.models[0];
|
|
102
|
+
const parts = [`\u9519\u5CF0 ${w.start}\u2013${w.end}`];
|
|
103
|
+
if (starModel) {
|
|
104
|
+
const seg = starModel.regular && starModel.effective && starModel.regular !== starModel.effective ? `${starModel.model}\xD7${starModel.regular.replace("x", "")}/\xD7${starModel.effective.replace("x", "")}` : `${starModel.model}\xD7${(starModel.effective ?? starModel.regular ?? "").replace("x", "")}`;
|
|
105
|
+
parts.push(seg);
|
|
106
|
+
}
|
|
107
|
+
const ws = windowState(w, now);
|
|
108
|
+
parts.push(`${ws.active ? "\u8DDD\u7ED3\u675F" : "\u8DDD\u5F00\u59CB"} ${fmtShortDur(ws.nextBoundaryAtMs - now.getTime())}`);
|
|
109
|
+
parts.push(sourceBadgeLine(st));
|
|
110
|
+
return parts.join(" \xB7 ");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// src/core/bailian-snapshot.ts
|
|
114
|
+
var CREDITS_PER_YUAN = 250;
|
|
115
|
+
var BAILIAN_SNAPSHOT_AT = "2026-08-30";
|
|
116
|
+
var TOKEN_PLAN_TEXT_MODELS = [
|
|
117
|
+
"qwen3.8-max",
|
|
118
|
+
"qwen3.8-flash",
|
|
119
|
+
"qwen3.7-max",
|
|
120
|
+
"qwen3.7-plus",
|
|
121
|
+
"qwen3.6-plus",
|
|
122
|
+
"qwen3.6-flash",
|
|
123
|
+
"glm-5.2",
|
|
124
|
+
"deepseek-v4-pro-0813"
|
|
125
|
+
];
|
|
126
|
+
var BAILIAN_WINDOW_RULES = [
|
|
127
|
+
{
|
|
128
|
+
id: "night-qwen38-max",
|
|
129
|
+
label: "Token Plan \u591C\u95F4\u4E94\u6298",
|
|
130
|
+
models: ["qwen3.8-max"],
|
|
131
|
+
factor: 0.5,
|
|
132
|
+
start: "22:00",
|
|
133
|
+
end: "08:00",
|
|
134
|
+
source: "\u5B98\u65B9\u6587\u6863 \xB7 Token Plan\uFF08\u4E2A\u4EBA\u7248\uFF09\u6982\u8FF0"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
id: "night-deepseek-v4-pro",
|
|
138
|
+
label: "Token Plan \u591C\u95F4\u4E94\u6298",
|
|
139
|
+
models: ["deepseek-v4-pro-0813"],
|
|
140
|
+
factor: 0.5,
|
|
141
|
+
start: "22:00",
|
|
142
|
+
end: "08:00",
|
|
143
|
+
source: "\u5B98\u65B9\u6587\u6863 \xB7 Token Plan\uFF08\u4E2A\u4EBA\u7248\uFF09\u6982\u8FF0"
|
|
144
|
+
}
|
|
145
|
+
];
|
|
146
|
+
function bailianSnapshotModels() {
|
|
147
|
+
const P = (name, type, price, unit = "\u6BCF\u767E\u4E07tokens", band, discount) => ({ name, type, price, unit, ...band ? { band } : {}, ...discount !== void 0 ? { discount } : {} });
|
|
148
|
+
return [
|
|
149
|
+
{
|
|
150
|
+
family: "qwen3.8-max",
|
|
151
|
+
model: "qwen3.8-max",
|
|
152
|
+
provider: "qwen",
|
|
153
|
+
context: 1e6,
|
|
154
|
+
prices: [P("\u8F93\u5165", "input_token", 12), P("\u8F93\u51FA", "output_token", 36), P("\u8F93\u5165\uFF08\u7F13\u5B58\u547D\u4E2D\uFF09", "input_token_cache", 1.5), P("\u8F93\u5165\uFF08Batch Chat\uFF09", "input_token_batch_chat", 12, "\u6BCF\u767E\u4E07tokens", void 0, 0.5), P("\u8F93\u51FA\uFF08Batch Chat\uFF09", "output_token_batch_chat", 36, "\u6BCF\u767E\u4E07tokens", void 0, 0.5)]
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
family: "qwen3.8-flash",
|
|
158
|
+
model: "qwen3.8-flash",
|
|
159
|
+
provider: "qwen",
|
|
160
|
+
context: 1e6,
|
|
161
|
+
prices: [P("\u8F93\u5165", "input_token", 0.8), P("\u8F93\u51FA", "output_token", 2.7), P("\u8F93\u5165\uFF08\u7F13\u5B58\u547D\u4E2D\uFF09", "input_token_cache", 0.1)]
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
family: "qwen3.7-max",
|
|
165
|
+
model: "qwen3.7-max",
|
|
166
|
+
provider: "qwen",
|
|
167
|
+
context: 1e6,
|
|
168
|
+
prices: [P("\u8F93\u5165", "input_token", 12), P("\u8F93\u51FA", "output_token", 36), P("\u8F93\u5165\uFF08\u7F13\u5B58\u547D\u4E2D\uFF09", "input_token_cache", 2.4), P("\u8F93\u5165", "input_token", 12, "\u6BCF\u767E\u4E07tokens", "standard", 0.5)]
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
family: "qwen3.6-plus",
|
|
172
|
+
model: "qwen3.6-plus",
|
|
173
|
+
provider: "qwen",
|
|
174
|
+
context: 1e6,
|
|
175
|
+
prices: [P("\u8F93\u5165", "input_token", 0.8), P("\u8F93\u51FA", "output_token", 4.8), P("\u8F93\u5165\uFF08\u7F13\u5B58\u547D\u4E2D\uFF09", "input_token_cache", 0.08)]
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
family: "deepseek",
|
|
179
|
+
model: "deepseek-v4-pro-0813",
|
|
180
|
+
provider: "deepseek",
|
|
181
|
+
context: 1e6,
|
|
182
|
+
prices: [P("\u8F93\u5165", "input_token", 9, "\u6BCF\u767E\u4E07tokens", "peak"), P("\u8F93\u5165", "input_token", 4.5, "\u6BCF\u767E\u4E07tokens", "offpeak"), P("\u8F93\u51FA", "output_token", 27, "\u6BCF\u767E\u4E07tokens", "peak"), P("\u8F93\u51FA", "output_token", 13.5, "\u6BCF\u767E\u4E07tokens", "offpeak"), P("\u8F93\u5165\uFF08\u7F13\u5B58\u547D\u4E2D\uFF09", "input_token_cache", 0.9, "\u6BCF\u767E\u4E07tokens", "peak"), P("\u8F93\u5165\uFF08\u7F13\u5B58\u547D\u4E2D\uFF09", "input_token_cache", 0.45, "\u6BCF\u767E\u4E07tokens", "offpeak")]
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
family: "deepseek",
|
|
186
|
+
model: "deepseek-v4-pro",
|
|
187
|
+
provider: "deepseek",
|
|
188
|
+
context: 1e6,
|
|
189
|
+
prices: [P("\u8F93\u5165", "input_token", 12), P("\u8F93\u51FA", "output_token", 24), P("\u8F93\u5165\uFF08\u7F13\u5B58\u547D\u4E2D\uFF09", "input_token_cache", 1)]
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
family: "glm-5.2",
|
|
193
|
+
model: "glm-5.2",
|
|
194
|
+
provider: "zhipu",
|
|
195
|
+
context: 2e5,
|
|
196
|
+
prices: [P("\u8F93\u5165", "input_token", 8), P("\u8F93\u51FA", "output_token", 28), P("\u8F93\u5165\uFF08\u7F13\u5B58\u547D\u4E2D\uFF09", "input_token_cache", 2)]
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
family: "wan-video",
|
|
200
|
+
model: "wan-video",
|
|
201
|
+
provider: "wan",
|
|
202
|
+
prices: [P("\u89C6\u9891\u751F\u6210\uFF08480P\uFF09", "video_ratio_480p", 0.45, "\u6BCF\u79D2"), P("\u89C6\u9891\u751F\u6210\uFF08720P\uFF09", "video_ratio_720p", 0.9, "\u6BCF\u79D2"), P("\u89C6\u9891\u751F\u6210\uFF081080P\uFF09", "video_ratio_1080p", 1.8, "\u6BCF\u79D2"), P("\u89C6\u9891\u751F\u6210\uFF08720P\xB7\u9650\u65F6\uFF09", "video_ratio_720p", 0.6, "\u6BCF\u79D2", void 0, 0.7)]
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
family: "qwen-image",
|
|
206
|
+
model: "qwen-image",
|
|
207
|
+
provider: "qwen",
|
|
208
|
+
prices: [P("\u56FE\u7247\u751F\u6210", "image_number", 0.25, "\u6BCF\u5F20")]
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
family: "qwen-image-plus",
|
|
212
|
+
model: "qwen-image-plus",
|
|
213
|
+
provider: "qwen",
|
|
214
|
+
prices: [P("\u56FE\u7247\u751F\u6210", "image_number", 0.2, "\u6BCF\u5F20")]
|
|
215
|
+
}
|
|
216
|
+
];
|
|
217
|
+
}
|
|
218
|
+
function parseGroups(list) {
|
|
219
|
+
const out = [];
|
|
220
|
+
const byName = /* @__PURE__ */ new Set();
|
|
221
|
+
for (const g of list || []) {
|
|
222
|
+
const items = Array.isArray(g.items) && g.items.length ? g.items : [g];
|
|
223
|
+
for (const it of items) {
|
|
224
|
+
const model = String(it.model || "");
|
|
225
|
+
if (!model) continue;
|
|
226
|
+
const m = model.match(/^(.*)-\d{4}-\d{2}-\d{2}$/);
|
|
227
|
+
if (m && byName.has(m[1])) continue;
|
|
228
|
+
const prices = [];
|
|
229
|
+
const mp = Array.isArray(it.multiPrices) ? it.multiPrices : [];
|
|
230
|
+
if (mp.length) {
|
|
231
|
+
for (const range of mp) {
|
|
232
|
+
for (const p of range.prices || []) {
|
|
233
|
+
prices.push({
|
|
234
|
+
name: p.priceName ?? p.type ?? "",
|
|
235
|
+
type: p.type ?? "",
|
|
236
|
+
price: Number(p.price),
|
|
237
|
+
unit: p.priceUnit ?? "",
|
|
238
|
+
...p.timeBand ? { band: p.timeBand } : {},
|
|
239
|
+
...p.discount !== void 0 && p.discount !== null ? { discount: Number(p.discount) } : {},
|
|
240
|
+
...range.rangeName ? { range: range.rangeName } : {}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
} else if (Array.isArray(it.prices)) {
|
|
245
|
+
for (const p of it.prices) {
|
|
246
|
+
prices.push({
|
|
247
|
+
name: p.priceName ?? p.type ?? "",
|
|
248
|
+
type: p.type ?? "",
|
|
249
|
+
price: Number(p.price),
|
|
250
|
+
unit: p.priceUnit ?? "",
|
|
251
|
+
...p.timeBand ? { band: p.timeBand } : {},
|
|
252
|
+
...p.discount !== void 0 && p.discount !== null ? { discount: Number(p.discount) } : {}
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
byName.add(model);
|
|
257
|
+
out.push({ family: model, model, provider: it.provider ?? g.provider ?? "", context: it.contextWindow, prices });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// src/core/merge.ts
|
|
264
|
+
var MODEL_ALIASES = {
|
|
265
|
+
qmodel_38max: "qwen3.8-max",
|
|
266
|
+
qmodel_latest: "qwen3.7-max",
|
|
267
|
+
qmodel: "qwen3.7-plus",
|
|
268
|
+
qfmodel: "qwen3.8-flash",
|
|
269
|
+
q37fmodel: "qwen3.7-flash",
|
|
270
|
+
dmodel: "deepseek-v4-pro",
|
|
271
|
+
gmodel: "glm-5.3",
|
|
272
|
+
gfmodel: "glm-5.3-flash",
|
|
273
|
+
gm51model: "glm-5.2",
|
|
274
|
+
kmodel: "kimi-k2.7-code",
|
|
275
|
+
mmodel: "minimax-m2.7"
|
|
276
|
+
};
|
|
277
|
+
function canonicalize(model) {
|
|
278
|
+
const k = String(model || "").toLowerCase();
|
|
279
|
+
return MODEL_ALIASES[k] ?? k;
|
|
280
|
+
}
|
|
281
|
+
function originRank(origin) {
|
|
282
|
+
return { "bailian-live": 0, "bailian-cli": 1, "qoder-snapshot": 2, "bailian-snapshot": 3 }[origin];
|
|
283
|
+
}
|
|
284
|
+
var OFFPEAK_WINDOW = { start: "22:00", end: "08:00", tz: "Asia/Shanghai" };
|
|
285
|
+
function catalogToOffers(models, origin, snapshotDate) {
|
|
286
|
+
const out = [];
|
|
287
|
+
const offpeakModels = models.filter((m) => m.prices.some((p) => p.band === "offpeak")).map((m) => m.model.toLowerCase());
|
|
288
|
+
if (offpeakModels.length) {
|
|
289
|
+
out.push({
|
|
290
|
+
id: "catalog-offpeak",
|
|
291
|
+
kind: "time-window",
|
|
292
|
+
label: "\u76EE\u5F55\u591C\u95F4\u4F18\u60E0\uFF08\u9519\u5CF0 5 \u6298\uFF09",
|
|
293
|
+
models: offpeakModels,
|
|
294
|
+
unit: "cny-price",
|
|
295
|
+
factor: 0.5,
|
|
296
|
+
window: { ...OFFPEAK_WINDOW },
|
|
297
|
+
note: "\u76EE\u5F55\u542B offpeak \u6863\u7684\u6A21\u578B\uFF0C\u8C37\u65F6\u4EF7=\u5CF0\u65F6 5 \u6298",
|
|
298
|
+
links: [],
|
|
299
|
+
origins: [origin],
|
|
300
|
+
...snapshotDate ? { snapshotDate } : {}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
const windowedModels = /* @__PURE__ */ new Set([
|
|
304
|
+
...out.filter((o) => o.kind === "time-window").flatMap((o) => o.models.map((m) => m.toLowerCase())),
|
|
305
|
+
...BAILIAN_WINDOW_RULES.flatMap((r) => r.models.map((m) => m.toLowerCase()))
|
|
306
|
+
]);
|
|
307
|
+
for (const m of models) {
|
|
308
|
+
const model = m.model.toLowerCase();
|
|
309
|
+
if (windowedModels.has(model)) continue;
|
|
310
|
+
const std = m.prices.find((p) => p.type === "input_token" && p.discount !== void 0 && p.discount < 1);
|
|
311
|
+
const modelFactor = std?.discount;
|
|
312
|
+
const batchEntry = m.prices.find((p) => /batch/i.test(p.type) && p.discount !== void 0 && p.discount < 1);
|
|
313
|
+
const batchCombined = batchEntry ? Math.round((batchEntry.discount ?? 1) * (modelFactor ?? 1) * 1e3) / 1e3 : void 0;
|
|
314
|
+
if (modelFactor === void 0 && batchCombined === void 0) continue;
|
|
315
|
+
const factors = [];
|
|
316
|
+
if (modelFactor !== void 0) factors.push(modelFactor);
|
|
317
|
+
else if (batchCombined !== void 0) factors.push(batchCombined);
|
|
318
|
+
out.push({
|
|
319
|
+
id: "limited-" + model,
|
|
320
|
+
kind: "flat-cut",
|
|
321
|
+
label: "\u9650\u65F6\u4F18\u60E0",
|
|
322
|
+
models: [model],
|
|
323
|
+
unit: "cny-price",
|
|
324
|
+
factor: factors[0],
|
|
325
|
+
...batchCombined !== void 0 ? { batchStack: batchCombined } : {},
|
|
326
|
+
note: modelFactor !== void 0 ? `\u6A21\u578B\u9650\u65F6 \xD7${modelFactor}` + (batchCombined !== void 0 ? `\uFF1BBatch \u53E0\u52A0 \xD7${batchCombined}` : "") : `\u4EC5 Batch \u7EF4\u5EA6 \xD7${batchCombined}`,
|
|
327
|
+
links: [],
|
|
328
|
+
origins: [origin],
|
|
329
|
+
...snapshotDate ? { snapshotDate } : {}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
return out;
|
|
333
|
+
}
|
|
334
|
+
function mergeKey(r) {
|
|
335
|
+
return [r.kind, r.unit, r.window ? `${r.window.start}-${r.window.end}@${r.window.tz}` : "-", r.factor ?? "-", r.baseFactor ?? "-", [...r.models].sort().join(",")].join("|");
|
|
336
|
+
}
|
|
337
|
+
function explodePerModel(rules) {
|
|
338
|
+
const out = [];
|
|
339
|
+
for (const r of rules) {
|
|
340
|
+
if (r.models.length <= 1) {
|
|
341
|
+
out.push(r);
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
for (const m of new Set(r.models)) out.push({ ...r, id: `${r.id}#${m}`, models: [m] });
|
|
345
|
+
}
|
|
346
|
+
return out;
|
|
347
|
+
}
|
|
348
|
+
function mergeOffers(all) {
|
|
349
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
350
|
+
for (const r of explodePerModel(all)) {
|
|
351
|
+
const k = mergeKey(r);
|
|
352
|
+
const prev = byKey.get(k);
|
|
353
|
+
if (!prev) {
|
|
354
|
+
byKey.set(k, { ...r, models: [...r.models], origins: [...r.origins], links: [...r.links] });
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
for (const m of r.models) if (!prev.models.includes(m)) prev.models.push(m);
|
|
358
|
+
for (const o of r.origins) if (!prev.origins.includes(o)) prev.origins.push(o);
|
|
359
|
+
for (const l of r.links) if (l && !prev.links.includes(l)) prev.links.push(l);
|
|
360
|
+
if (r.snapshotDate && (!prev.snapshotDate || r.snapshotDate > prev.snapshotDate)) prev.snapshotDate = r.snapshotDate;
|
|
361
|
+
}
|
|
362
|
+
const list = [...byKey.values()];
|
|
363
|
+
const rank = (r) => Math.min(...r.origins.map(originRank));
|
|
364
|
+
const kindOrder = { "time-window": 0, "flat-cut": 1, "free-calls": 2, policy: 3 };
|
|
365
|
+
list.sort((a, b) => {
|
|
366
|
+
const rd = rank(a) - rank(b);
|
|
367
|
+
if (rd) return rd;
|
|
368
|
+
const kd = kindOrder[a.kind] - kindOrder[b.kind];
|
|
369
|
+
if (kd) return kd;
|
|
370
|
+
const fa = a.factor ?? 1, fb = b.factor ?? 1;
|
|
371
|
+
if (fa !== fb) return fa - fb;
|
|
372
|
+
return a.id.localeCompare(b.id);
|
|
373
|
+
});
|
|
374
|
+
return list;
|
|
375
|
+
}
|
|
376
|
+
function isCrossVerified(r) {
|
|
377
|
+
const hasQ = r.origins.includes("qoder-snapshot");
|
|
378
|
+
const hasB = r.origins.some((o) => o.startsWith("bailian-"));
|
|
379
|
+
return hasQ && hasB;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// src/core/qoder-snapshot.ts
|
|
383
|
+
var QODER_SNAPSHOT_DATE = "2026-08-31";
|
|
384
|
+
var QODER_ORIGIN_LABEL = "Qoder \u5FEB\u7167 2026-08-31";
|
|
385
|
+
var W_OFFPEAK = { start: "22:00", end: "08:00", tz: "Asia/Shanghai" };
|
|
386
|
+
var L_QWENMAX = "https://docs.qoder.cn/events/qwen-max";
|
|
387
|
+
var L_QWEN37 = "https://docs.qoder.cn/product-overview/qwen-3-7-series-model-staggering-discount";
|
|
388
|
+
var L_OFFPEAK_RULES = "https://help.aliyun.com/document_detail/3042139.html";
|
|
389
|
+
var L_ULTIMATE = "https://docs.qoder.com/zh/events/ultimatediscount";
|
|
390
|
+
var L_CANTUS = "https://docs.qoder.com/zh/events/cantuslaunch";
|
|
391
|
+
var L_FREECALLS = "https://docs.qoder.com/zh/events/ultimatefreecalls";
|
|
392
|
+
var L_REFERRAL = "https://qoder.cn/referral";
|
|
393
|
+
function qoderSnapshotOffers() {
|
|
394
|
+
const S = QODER_SNAPSHOT_DATE;
|
|
395
|
+
const o = (r) => ({
|
|
396
|
+
...r,
|
|
397
|
+
origins: ["qoder-snapshot"],
|
|
398
|
+
snapshotDate: S
|
|
399
|
+
});
|
|
400
|
+
return [
|
|
401
|
+
o({
|
|
402
|
+
id: "atp-offpeak-qwen38",
|
|
403
|
+
kind: "time-window",
|
|
404
|
+
label: "\u9519\u5CF0 5 \u6298\uFF08Off-Peak 50% off\uFF09",
|
|
405
|
+
models: ["qwen3.8-max"],
|
|
406
|
+
unit: "credits-factor",
|
|
407
|
+
factor: 0.25,
|
|
408
|
+
baseFactor: 0.5,
|
|
409
|
+
window: { ...W_OFFPEAK },
|
|
410
|
+
note: "\u5E38\u89C4 0.5x \u2192 \u9519\u5CF0\u65F6\u6BB5 0.25x\uFF085 \u6298\uFF09",
|
|
411
|
+
links: [L_QWENMAX]
|
|
412
|
+
}),
|
|
413
|
+
o({
|
|
414
|
+
id: "atp-offpeak-qwen37max",
|
|
415
|
+
kind: "time-window",
|
|
416
|
+
label: "\u9519\u5CF0 2 \u6298\uFF08Off-Peak 80% off\uFF09",
|
|
417
|
+
models: ["qwen3.7-max"],
|
|
418
|
+
unit: "credits-factor",
|
|
419
|
+
factor: 0.1,
|
|
420
|
+
baseFactor: 0.5,
|
|
421
|
+
window: { ...W_OFFPEAK },
|
|
422
|
+
note: "\u5E38\u89C4 0.5x \u2192 \u9519\u5CF0\u65F6\u6BB5 0.1x\uFF082 \u6298\uFF09",
|
|
423
|
+
links: [L_QWEN37]
|
|
424
|
+
}),
|
|
425
|
+
o({
|
|
426
|
+
id: "atp-offpeak-qwen37plus",
|
|
427
|
+
kind: "time-window",
|
|
428
|
+
label: "\u9519\u5CF0 4 \u6298\uFF08Off-Peak 60% off\uFF09",
|
|
429
|
+
models: ["qwen3.7-plus"],
|
|
430
|
+
unit: "credits-factor",
|
|
431
|
+
factor: 0.04,
|
|
432
|
+
baseFactor: 0.1,
|
|
433
|
+
window: { ...W_OFFPEAK },
|
|
434
|
+
note: "\u5E38\u89C4 0.1x \u2192 \u9519\u5CF0\u65F6\u6BB5 0.04x\uFF084 \u6298\uFF09",
|
|
435
|
+
links: [L_QWEN37]
|
|
436
|
+
}),
|
|
437
|
+
o({
|
|
438
|
+
id: "atp-flat-preview",
|
|
439
|
+
kind: "flat-cut",
|
|
440
|
+
label: "\u9650\u65F6 1 \u6298",
|
|
441
|
+
models: ["qwen3.8-max-preview"],
|
|
442
|
+
unit: "credits-factor",
|
|
443
|
+
factor: 0.1,
|
|
444
|
+
note: "\u6A21\u578B\u63CF\u8FF0\u539F\u6587\uFF1A\u5E38\u89C4\u65F6\u6BB5 1 \u6298",
|
|
445
|
+
links: [L_OFFPEAK_RULES]
|
|
446
|
+
}),
|
|
447
|
+
o({
|
|
448
|
+
id: "atp-flat-glm53f",
|
|
449
|
+
kind: "flat-cut",
|
|
450
|
+
label: "\u5212\u7EBF\u534A\u4EF7\uFF080.1 \u2192 0.05\uFF09",
|
|
451
|
+
models: ["glm-5.3-flash"],
|
|
452
|
+
unit: "credits-factor",
|
|
453
|
+
factor: 0.05,
|
|
454
|
+
baseFactor: 0.1,
|
|
455
|
+
note: "originalPriceFactor \u7ED3\u6784\uFF0C\u65E0 promotion \u6807\u8BB0\uFF08\u5FEB\u7167\uFF09",
|
|
456
|
+
links: [L_OFFPEAK_RULES]
|
|
457
|
+
}),
|
|
458
|
+
o({
|
|
459
|
+
id: "atp-flat-ultimate",
|
|
460
|
+
kind: "flat-cut",
|
|
461
|
+
label: "\u9650\u65F6\u6298\u6263\u8FDB\u884C\u4E2D",
|
|
462
|
+
models: ["ultimate", "cantus"],
|
|
463
|
+
unit: "credits-factor",
|
|
464
|
+
note: "\u6570\u503C\u672A\u516C\u5F00\uFF0C\u4EC5\u6D3B\u52A8\u6587\u6848\uFF08\u5FEB\u7167\uFF09",
|
|
465
|
+
links: [L_ULTIMATE, L_CANTUS]
|
|
466
|
+
}),
|
|
467
|
+
o({
|
|
468
|
+
id: "atp-free-200",
|
|
469
|
+
kind: "free-calls",
|
|
470
|
+
label: "\u6781\u81F4\u6A21\u578B 200 \u6B21\u514D\u8D39",
|
|
471
|
+
models: [],
|
|
472
|
+
unit: "credits-factor",
|
|
473
|
+
note: "\u514D\u8D39\u989D\u5EA6\u6D3B\u52A8\uFF1B\u5DF2\u8FC7\u671F\uFF08expired:true\uFF09",
|
|
474
|
+
expiry: "2026-07-30T23:59:59+08:00",
|
|
475
|
+
links: [L_FREECALLS]
|
|
476
|
+
}),
|
|
477
|
+
o({
|
|
478
|
+
id: "atp-free-800-2000",
|
|
479
|
+
kind: "free-calls",
|
|
480
|
+
label: "qwen3.8-max 800/2000 \u6B21\u514D\u8D39\uFF08claim\uFF09",
|
|
481
|
+
models: ["qwen3.8-max"],
|
|
482
|
+
unit: "credits-factor",
|
|
483
|
+
note: "activityId: qwen38_800_invoke / qwen38_2000_invoke\uFF08\u5FEB\u7167\uFF09",
|
|
484
|
+
links: []
|
|
485
|
+
}),
|
|
486
|
+
o({
|
|
487
|
+
id: "atp-referral",
|
|
488
|
+
kind: "policy",
|
|
489
|
+
label: "\u63A8\u8350\u8FD4\u79EF\u5206 \u226440000+",
|
|
490
|
+
models: [],
|
|
491
|
+
unit: "credits-factor",
|
|
492
|
+
note: "\u63A8\u8350\u6D3B\u52A8\u81F3 2026-09-03",
|
|
493
|
+
expiry: "2026-09-03T23:59:59+08:00",
|
|
494
|
+
links: [L_REFERRAL]
|
|
495
|
+
}),
|
|
496
|
+
o({
|
|
497
|
+
id: "atp-lite",
|
|
498
|
+
kind: "policy",
|
|
499
|
+
label: "lite \u57FA\u7840\u6A21\u578B\u964D\u7EA7\u653F\u7B56",
|
|
500
|
+
models: ["lite"],
|
|
501
|
+
unit: "credits-factor",
|
|
502
|
+
note: "Credits \u8017\u5C3D\u540E\u964D\u7EA7\u4E3A lite \u57FA\u7840\u6A21\u578B\uFF08\u514D\u8D39/\u6BCF\u65E5\u9650\u989D/\u9AD8\u5CF0\u65F6\u6BB5\u53D8\u6162\uFF09\uFF1B\u4F59\u989D <200 \u4F4E\u989D\u63D0\u793A\uFF1B\u9996\u6708 prorated\uFF08\u5FEB\u7167\uFF09",
|
|
503
|
+
links: []
|
|
504
|
+
})
|
|
505
|
+
];
|
|
506
|
+
}
|
|
507
|
+
var QODER_INSALE_FACTORS = {
|
|
508
|
+
auto: { factor: 0.5 },
|
|
509
|
+
"qwen3.8-flash": { factor: 0.1 },
|
|
510
|
+
"qwen3.7-flash": { factor: 0.1 },
|
|
511
|
+
"deepseek-v4-pro": { factor: 0.8 },
|
|
512
|
+
"deepseek-v4-flash": { factor: 0.3 },
|
|
513
|
+
"glm-5.3": { factor: 0.6 },
|
|
514
|
+
"glm-5.2": { factor: 0.6 },
|
|
515
|
+
"kimi-k2.7-code": { factor: 0.3, note: "highspeed 0.6" },
|
|
516
|
+
"minimax-m2.7": { factor: 0.2 }
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// src/core/status.ts
|
|
520
|
+
import * as fs from "node:fs";
|
|
521
|
+
import * as path from "node:path";
|
|
522
|
+
import * as os from "node:os";
|
|
523
|
+
var WRITTEN_BY = "@te-river/opencode-alibabatokenplan/server@0.1.0";
|
|
524
|
+
var STALE_AFTER_SEC = 600;
|
|
525
|
+
var CATALOG_ORIGIN = {
|
|
526
|
+
live: "bailian-live",
|
|
527
|
+
cli: "bailian-cli",
|
|
528
|
+
snapshot: "bailian-snapshot"
|
|
529
|
+
};
|
|
530
|
+
function windowId(start, end) {
|
|
531
|
+
if (start === "22:00" && end === "08:00") return "offpeak-22-08";
|
|
532
|
+
return `win-${start.replace(":", "")}-${end.replace(":", "")}`;
|
|
533
|
+
}
|
|
534
|
+
function buildOffers(catalog) {
|
|
535
|
+
const origin = CATALOG_ORIGIN[catalog.source] ?? "bailian-snapshot";
|
|
536
|
+
const legacy = BAILIAN_WINDOW_RULES.map((r) => ({
|
|
537
|
+
id: r.id,
|
|
538
|
+
kind: "time-window",
|
|
539
|
+
label: r.label,
|
|
540
|
+
models: [...r.models],
|
|
541
|
+
unit: "cny-price",
|
|
542
|
+
factor: r.factor,
|
|
543
|
+
window: { start: r.start, end: r.end, tz: "Asia/Shanghai" },
|
|
544
|
+
note: r.source,
|
|
545
|
+
links: [],
|
|
546
|
+
origins: ["bailian-snapshot"],
|
|
547
|
+
snapshotDate: BAILIAN_SNAPSHOT_AT
|
|
548
|
+
}));
|
|
549
|
+
return mergeOffers([...qoderSnapshotOffers(), ...legacy, ...catalogToOffers(catalog.models, origin, catalog.fetchedAt?.slice(0, 10))]);
|
|
550
|
+
}
|
|
551
|
+
function buildWindows(offers, now) {
|
|
552
|
+
const byWindow = /* @__PURE__ */ new Map();
|
|
553
|
+
for (const r of offers) {
|
|
554
|
+
if (r.kind !== "time-window" || !r.window) continue;
|
|
555
|
+
const key = `${r.window.start}-${r.window.end}@${r.window.tz}`;
|
|
556
|
+
let g = byWindow.get(key);
|
|
557
|
+
if (!g) {
|
|
558
|
+
g = { ...r.window, rules: [] };
|
|
559
|
+
byWindow.set(key, g);
|
|
560
|
+
}
|
|
561
|
+
g.rules.push(r);
|
|
562
|
+
}
|
|
563
|
+
const out = [];
|
|
564
|
+
for (const g of byWindow.values()) {
|
|
565
|
+
const w = windowState(g, now);
|
|
566
|
+
const models = [];
|
|
567
|
+
for (const r of g.rules) {
|
|
568
|
+
const cross = isCrossVerified(r);
|
|
569
|
+
for (const m of r.models) {
|
|
570
|
+
models.push({
|
|
571
|
+
model: m,
|
|
572
|
+
regular: fmtFactor(r.baseFactor),
|
|
573
|
+
effective: fmtFactor(r.factor),
|
|
574
|
+
badge: r.label + (cross ? " \xB7 \u53CC\u6E90\u5370\u8BC1" : ""),
|
|
575
|
+
origins: r.origins,
|
|
576
|
+
links: r.links
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
models.sort((a, b) => {
|
|
581
|
+
const fa = parseFloat(a.effective ?? "1"), fb = parseFloat(b.effective ?? "1");
|
|
582
|
+
if (fa !== fb) return fa - fb;
|
|
583
|
+
return a.model.localeCompare(b.model);
|
|
584
|
+
});
|
|
585
|
+
out.push({
|
|
586
|
+
id: windowId(g.start, g.end),
|
|
587
|
+
label: g.rules[0].label,
|
|
588
|
+
start: g.start,
|
|
589
|
+
end: g.end,
|
|
590
|
+
tz: g.tz,
|
|
591
|
+
activeNow: w.active,
|
|
592
|
+
boundaryKind: w.boundaryKind,
|
|
593
|
+
nextBoundaryAt: w.nextBoundaryAt,
|
|
594
|
+
models
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
out.sort((a, b) => Number(b.id.startsWith("offpeak")) - Number(a.id.startsWith("offpeak")) || a.start.localeCompare(b.start));
|
|
598
|
+
return out;
|
|
599
|
+
}
|
|
600
|
+
function buildDiscounts(offers) {
|
|
601
|
+
const byModel = /* @__PURE__ */ new Map();
|
|
602
|
+
for (const r of offers) {
|
|
603
|
+
if (r.kind === "free-calls" || r.kind === "policy") continue;
|
|
604
|
+
for (const m of r.models) {
|
|
605
|
+
const arr = byModel.get(m) ?? [];
|
|
606
|
+
arr.push(r);
|
|
607
|
+
byModel.set(m, arr);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
const out = [];
|
|
611
|
+
for (const [model, applies] of byModel) {
|
|
612
|
+
const bestFactors = {};
|
|
613
|
+
for (const r of applies) {
|
|
614
|
+
if (r.factor === void 0) continue;
|
|
615
|
+
const cur = bestFactors[r.unit];
|
|
616
|
+
if (cur === void 0 || r.factor < cur) bestFactors[r.unit] = r.factor;
|
|
617
|
+
}
|
|
618
|
+
out.push({
|
|
619
|
+
model,
|
|
620
|
+
applies,
|
|
621
|
+
...Object.keys(bestFactors).length ? { bestFactors } : {}
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
const lowest = (d) => {
|
|
625
|
+
const vs = Object.values(d.bestFactors ?? {});
|
|
626
|
+
return vs.length ? Math.min(...vs) : 1;
|
|
627
|
+
};
|
|
628
|
+
const seriesOrder = (n) => /^qwen/.test(n) ? 0 : /^(deepseek|glm|kimi|minimax)/.test(n) ? 1 : 2;
|
|
629
|
+
out.sort((a, b) => {
|
|
630
|
+
const fa = lowest(a), fb = lowest(b);
|
|
631
|
+
if (fa !== fb) return fa - fb;
|
|
632
|
+
const sd = seriesOrder(a.model) - seriesOrder(b.model);
|
|
633
|
+
if (sd) return sd;
|
|
634
|
+
return a.model.localeCompare(b.model);
|
|
635
|
+
});
|
|
636
|
+
return out;
|
|
637
|
+
}
|
|
638
|
+
function pickPrice(prices, type, band) {
|
|
639
|
+
return prices.find((p) => p.type === type && (band ? p.band === band : !p.band || p.band === "standard"));
|
|
640
|
+
}
|
|
641
|
+
function creditsPerM(price) {
|
|
642
|
+
const v = price * CREDITS_PER_YUAN;
|
|
643
|
+
return Number.isInteger(v) ? v.toLocaleString("en-US") : String(Number(v.toFixed(1)));
|
|
644
|
+
}
|
|
645
|
+
function buildTextModels(models, windows, now) {
|
|
646
|
+
return TOKEN_PLAN_TEXT_MODELS.map((name) => {
|
|
647
|
+
const m = models.find((x) => x.model === name) || models.find((x) => x.family === name);
|
|
648
|
+
let badge;
|
|
649
|
+
for (const w of windows) {
|
|
650
|
+
if (w.models.some((x) => x.model === name)) {
|
|
651
|
+
const ws = windowState(w, now);
|
|
652
|
+
badge = {
|
|
653
|
+
label: w.label + (w.models.find((x) => x.model === name)?.badge?.includes("\u53CC\u6E90") ? " \xB7 \u53CC\u6E90\u5370\u8BC1" : ""),
|
|
654
|
+
countdown: ws.active ? `${fmtCountdown(ws.minutesToNextBoundary)}\u540E\u7ED3\u675F\uFF08${w.end}\uFF09` : `${fmtCountdown(ws.minutesToNextBoundary)}\u540E\u5F00\u59CB\uFF08${w.start}\uFF09`
|
|
655
|
+
};
|
|
656
|
+
break;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
const insale = QODER_INSALE_FACTORS[name];
|
|
660
|
+
if (!m) {
|
|
661
|
+
return { model: name, input: "\u2014", cache: "\u2014", output: "\u2014", ...insale ? { creditsFactor: fmtFactor(insale.factor) } : {}, ...badge ? { badge } : {} };
|
|
662
|
+
}
|
|
663
|
+
const peakIn = pickPrice(m.prices, "input_token", "peak");
|
|
664
|
+
const offIn = pickPrice(m.prices, "input_token", "offpeak");
|
|
665
|
+
const stdIn = pickPrice(m.prices, "input_token");
|
|
666
|
+
const stdOut = pickPrice(m.prices, "output_token", "peak") || pickPrice(m.prices, "output_token");
|
|
667
|
+
const offOut = pickPrice(m.prices, "output_token", "offpeak");
|
|
668
|
+
const stdCache = pickPrice(m.prices, "input_token_cache", "peak") || pickPrice(m.prices, "input_token_cache") || pickPrice(m.prices, "input_token_cache_read");
|
|
669
|
+
const offCache = pickPrice(m.prices, "input_token_cache", "offpeak") || pickPrice(m.prices, "input_token_cache_read", "offpeak");
|
|
670
|
+
const dual = !!(peakIn && offIn);
|
|
671
|
+
const pair = (a, b) => a ? b ? `${creditsPerM(a.price)} / ${creditsPerM(b.price)}` : creditsPerM(a.price) : "\u2014";
|
|
672
|
+
return {
|
|
673
|
+
model: name,
|
|
674
|
+
input: dual ? pair(peakIn, offIn) : pair(stdIn),
|
|
675
|
+
cache: dual ? pair(stdCache, offCache) : pair(stdCache),
|
|
676
|
+
output: dual ? pair(stdOut, offOut) : pair(stdOut),
|
|
677
|
+
...dual ? { bands: "\u5CF0/\u8C37" } : {},
|
|
678
|
+
...insale ? { creditsFactor: fmtFactor(insale.factor) } : {},
|
|
679
|
+
...badge ? { badge } : {}
|
|
680
|
+
};
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
function buildNotices(offers, now) {
|
|
684
|
+
return offers.filter((r) => r.kind === "free-calls" || r.kind === "policy").map((r) => ({
|
|
685
|
+
kind: r.kind,
|
|
686
|
+
text: `${r.label}${r.note ? " \u2014 " + r.note : ""}`,
|
|
687
|
+
source: r.origins.includes("qoder-snapshot") ? `Qoder \u5FEB\u7167 ${r.snapshotDate ?? QODER_SNAPSHOT_DATE}` : r.origins[0] ?? "unknown",
|
|
688
|
+
expired: r.expiry ? new Date(r.expiry).getTime() < now.getTime() : void 0
|
|
689
|
+
}));
|
|
690
|
+
}
|
|
691
|
+
function buildStatus(catalog, now = /* @__PURE__ */ new Date(), quota) {
|
|
692
|
+
const offers = buildOffers(catalog);
|
|
693
|
+
const windows = buildWindows(offers, now);
|
|
694
|
+
return {
|
|
695
|
+
schemaVersion: 1,
|
|
696
|
+
writtenBy: WRITTEN_BY,
|
|
697
|
+
updatedAt: now.toISOString(),
|
|
698
|
+
staleAfterSec: STALE_AFTER_SEC,
|
|
699
|
+
timezone: "Asia/Shanghai",
|
|
700
|
+
creditsPerYuan: CREDITS_PER_YUAN,
|
|
701
|
+
creditsRateNote: "\u636E\u767E\u70BC\u8BA1\u8D39\u793A\u4F8B\u6362\u7B97\uFF0C\u975E\u5B98\u65B9\u6C47\u7387",
|
|
702
|
+
sources: {
|
|
703
|
+
catalog: CATALOG_ORIGIN[catalog.source] ?? "bailian-snapshot",
|
|
704
|
+
qoderSnapshot: QODER_SNAPSHOT_DATE,
|
|
705
|
+
...catalog.error ? { error: catalog.error } : {}
|
|
706
|
+
},
|
|
707
|
+
windows,
|
|
708
|
+
offers,
|
|
709
|
+
discounts: buildDiscounts(offers),
|
|
710
|
+
textModels: buildTextModels(catalog.models, windows, now),
|
|
711
|
+
quota: quota ?? { available: false, hint: "\u5B9E\u65F6\u989D\u5EA6\u9700: bl auth login --console --console-site domestic" },
|
|
712
|
+
notices: buildNotices(offers, now)
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
function isStale(st, now = /* @__PURE__ */ new Date()) {
|
|
716
|
+
const t = new Date(st.updatedAt).getTime();
|
|
717
|
+
if (Number.isNaN(t)) return true;
|
|
718
|
+
return now.getTime() - t > st.staleAfterSec * 1e3;
|
|
719
|
+
}
|
|
720
|
+
var STATUS_SIZE_CAP = 256 * 1024;
|
|
721
|
+
function stateDir() {
|
|
722
|
+
return process.env.ATP_STATE_DIR || path.join(os.homedir(), ".cache", "opencode", "alibabatokenplan");
|
|
723
|
+
}
|
|
724
|
+
function statusFilePath() {
|
|
725
|
+
return path.join(stateDir(), "status.json");
|
|
726
|
+
}
|
|
727
|
+
function cacheFilePath() {
|
|
728
|
+
return path.join(stateDir(), "cache.json");
|
|
729
|
+
}
|
|
730
|
+
function portFilePath() {
|
|
731
|
+
return path.join(stateDir(), ".port");
|
|
732
|
+
}
|
|
733
|
+
function legacyCacheFile() {
|
|
734
|
+
return path.join(os.homedir(), ".cache", "opencode", "bailian-discount-cache.json");
|
|
735
|
+
}
|
|
736
|
+
function legacyPortFile() {
|
|
737
|
+
return path.join(os.homedir(), ".cache", "opencode", "bailian-discount-panel.port");
|
|
738
|
+
}
|
|
739
|
+
function configFilePath() {
|
|
740
|
+
return process.env.ATP_CONFIG_DIR ? path.join(process.env.ATP_CONFIG_DIR, "config.jsonc") : path.join(os.homedir(), ".config", "opencode", "alibabatokenplan", "config.jsonc");
|
|
741
|
+
}
|
|
742
|
+
function parseJsonc(text) {
|
|
743
|
+
let out = "";
|
|
744
|
+
let i = 0;
|
|
745
|
+
let inStr = false;
|
|
746
|
+
let esc = false;
|
|
747
|
+
while (i < text.length) {
|
|
748
|
+
const c = text[i];
|
|
749
|
+
const n = text[i + 1];
|
|
750
|
+
if (inStr) {
|
|
751
|
+
out += c;
|
|
752
|
+
if (esc) esc = false;
|
|
753
|
+
else if (c === "\\") esc = true;
|
|
754
|
+
else if (c === '"') inStr = false;
|
|
755
|
+
i++;
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
if (c === '"') {
|
|
759
|
+
inStr = true;
|
|
760
|
+
out += c;
|
|
761
|
+
i++;
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
if (c === "/" && n === "/") {
|
|
765
|
+
while (i < text.length && text[i] !== "\n") i++;
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
if (c === "/" && n === "*") {
|
|
769
|
+
i += 2;
|
|
770
|
+
while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++;
|
|
771
|
+
i += 2;
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
out += c;
|
|
775
|
+
i++;
|
|
776
|
+
}
|
|
777
|
+
let res = "";
|
|
778
|
+
inStr = false;
|
|
779
|
+
esc = false;
|
|
780
|
+
i = 0;
|
|
781
|
+
while (i < out.length) {
|
|
782
|
+
const c = out[i];
|
|
783
|
+
if (inStr) {
|
|
784
|
+
res += c;
|
|
785
|
+
if (esc) esc = false;
|
|
786
|
+
else if (c === "\\") esc = true;
|
|
787
|
+
else if (c === '"') inStr = false;
|
|
788
|
+
i++;
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
if (c === '"') {
|
|
792
|
+
inStr = true;
|
|
793
|
+
res += c;
|
|
794
|
+
i++;
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
if (c === ",") {
|
|
798
|
+
let j = i + 1;
|
|
799
|
+
while (j < out.length && /\s/.test(out[j])) j++;
|
|
800
|
+
if (out[j] === "}" || out[j] === "]") {
|
|
801
|
+
i++;
|
|
802
|
+
continue;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
res += c;
|
|
806
|
+
i++;
|
|
807
|
+
}
|
|
808
|
+
return JSON.parse(res);
|
|
809
|
+
}
|
|
810
|
+
var DEFAULT_CONFIG = {
|
|
811
|
+
webpanel: { enabled: false, port: 7777 },
|
|
812
|
+
sidebar: { order: 160, showNonTokenPlan: false },
|
|
813
|
+
toast: true,
|
|
814
|
+
refresh: { catalogMin: 10, quotaMin: 5 }
|
|
815
|
+
};
|
|
816
|
+
function loadConfig() {
|
|
817
|
+
const d = structuredClone(DEFAULT_CONFIG);
|
|
818
|
+
try {
|
|
819
|
+
const raw = parseJsonc(fs.readFileSync(configFilePath(), "utf8"));
|
|
820
|
+
if (raw && typeof raw === "object") {
|
|
821
|
+
if (raw.webpanel?.enabled !== void 0) d.webpanel.enabled = !!raw.webpanel.enabled;
|
|
822
|
+
if (Number.isInteger(raw.webpanel?.port) && raw.webpanel.port >= 1024 && raw.webpanel.port <= 65535) d.webpanel.port = raw.webpanel.port;
|
|
823
|
+
if (Number.isInteger(raw.sidebar?.order)) d.sidebar.order = raw.sidebar.order;
|
|
824
|
+
if (raw.sidebar?.showNonTokenPlan !== void 0) d.sidebar.showNonTokenPlan = !!raw.sidebar.showNonTokenPlan;
|
|
825
|
+
if (raw.toast !== void 0) d.toast = !!raw.toast;
|
|
826
|
+
if (Number.isInteger(raw.refresh?.catalogMin) && raw.refresh.catalogMin > 0) d.refresh.catalogMin = raw.refresh.catalogMin;
|
|
827
|
+
if (Number.isInteger(raw.refresh?.quotaMin) && raw.refresh.quotaMin > 0) d.refresh.quotaMin = raw.refresh.quotaMin;
|
|
828
|
+
}
|
|
829
|
+
} catch {
|
|
830
|
+
}
|
|
831
|
+
return d;
|
|
832
|
+
}
|
|
833
|
+
function writeStatusAtomic(st) {
|
|
834
|
+
try {
|
|
835
|
+
const dir = stateDir();
|
|
836
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
837
|
+
let payload = JSON.stringify(st);
|
|
838
|
+
if (Buffer.byteLength(payload, "utf8") > STATUS_SIZE_CAP) {
|
|
839
|
+
const shrunk = { ...st, textModels: st.textModels.slice(0, 20), discounts: st.discounts.slice(0, 60), offers: st.offers.slice(0, 60), windows: st.windows.slice(0, 8) };
|
|
840
|
+
payload = JSON.stringify(shrunk);
|
|
841
|
+
if (Buffer.byteLength(payload, "utf8") > STATUS_SIZE_CAP) payload = JSON.stringify({ ...shrunk, textModels: [], discounts: [] });
|
|
842
|
+
}
|
|
843
|
+
const tmp = path.join(dir, "status.json.tmp");
|
|
844
|
+
fs.writeFileSync(tmp, payload);
|
|
845
|
+
fs.renameSync(tmp, path.join(dir, "status.json"));
|
|
846
|
+
return true;
|
|
847
|
+
} catch {
|
|
848
|
+
return false;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
function readStatusFile(now = /* @__PURE__ */ new Date()) {
|
|
852
|
+
let text;
|
|
853
|
+
try {
|
|
854
|
+
text = fs.readFileSync(statusFilePath(), "utf8");
|
|
855
|
+
} catch {
|
|
856
|
+
return { kind: "missing" };
|
|
857
|
+
}
|
|
858
|
+
try {
|
|
859
|
+
const j = JSON.parse(text);
|
|
860
|
+
if (!j || j.schemaVersion !== 1 || !Array.isArray(j.windows)) return { kind: "corrupt", error: "schema" };
|
|
861
|
+
return isStale(j, now) ? { kind: "stale", status: j } : { kind: "live", status: j };
|
|
862
|
+
} catch (e) {
|
|
863
|
+
return { kind: "corrupt", error: String(e?.message || e) };
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
export {
|
|
867
|
+
BAILIAN_SNAPSHOT_AT,
|
|
868
|
+
BAILIAN_WINDOW_RULES,
|
|
869
|
+
CREDITS_PER_YUAN,
|
|
870
|
+
DAY_MIN,
|
|
871
|
+
DEFAULT_CONFIG,
|
|
872
|
+
MODEL_ALIASES,
|
|
873
|
+
QODER_INSALE_FACTORS,
|
|
874
|
+
QODER_ORIGIN_LABEL,
|
|
875
|
+
QODER_SNAPSHOT_DATE,
|
|
876
|
+
SH_OFFSET_MIN,
|
|
877
|
+
STALE_AFTER_SEC,
|
|
878
|
+
STATUS_SIZE_CAP,
|
|
879
|
+
TOKEN_PLAN_TEXT_MODELS,
|
|
880
|
+
WRITTEN_BY,
|
|
881
|
+
bailianSnapshotModels,
|
|
882
|
+
buildOffers,
|
|
883
|
+
buildStatus,
|
|
884
|
+
cacheFilePath,
|
|
885
|
+
canonicalize,
|
|
886
|
+
catalogToOffers,
|
|
887
|
+
configFilePath,
|
|
888
|
+
fmtCountdown,
|
|
889
|
+
fmtFactor,
|
|
890
|
+
fmtHMS,
|
|
891
|
+
fmtShortDur,
|
|
892
|
+
homeSummaryLine,
|
|
893
|
+
isCrossVerified,
|
|
894
|
+
isStale,
|
|
895
|
+
legacyCacheFile,
|
|
896
|
+
legacyPortFile,
|
|
897
|
+
loadConfig,
|
|
898
|
+
mergeOffers,
|
|
899
|
+
originRank,
|
|
900
|
+
parseGroups,
|
|
901
|
+
parseHm,
|
|
902
|
+
parseJsonc,
|
|
903
|
+
portFilePath,
|
|
904
|
+
qoderSnapshotOffers,
|
|
905
|
+
readStatusFile,
|
|
906
|
+
ruleWindow,
|
|
907
|
+
shanghaiISO,
|
|
908
|
+
shanghaiMinutes,
|
|
909
|
+
showStrike,
|
|
910
|
+
sourceBadgeLine,
|
|
911
|
+
stateDir,
|
|
912
|
+
statusFilePath,
|
|
913
|
+
strikethrough,
|
|
914
|
+
windowCountdownText,
|
|
915
|
+
windowId,
|
|
916
|
+
windowState,
|
|
917
|
+
writeStatusAtomic
|
|
918
|
+
};
|