dsh-sidebar-usage 0.0.0-stage → 1.0.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 +59 -2
- package/README.zh.md +71 -0
- package/cordis.patch.yml +5 -0
- package/icon.svg +21 -0
- package/lib/accounts.js +1009 -0
- package/lib/client.js +5112 -0
- package/lib/custom.js +427 -0
- package/lib/index.js +1488 -0
- package/lib/pricing.js +420 -0
- package/lib/providers.js +183 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +68 -4
- package/screenshot.png +0 -0
- package/screenshot2.png +0 -0
- package/screenshots.json +4 -0
package/lib/pricing.js
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-deepseek-usage — 定价表(每百万 tokens)。
|
|
3
|
+
*
|
|
4
|
+
* 一家提供商一份价格快照,全部放在 PRICE_TABLES 里;DeepSeek 只是其中一条,
|
|
5
|
+
* 没有单独一套计价代码:
|
|
6
|
+
* · PRICE_TABLES[provider] = { currency, asOf, schedule?, aliases?, models }
|
|
7
|
+
* · models[模型 id] = { standard: { input, cacheRead, cacheWrite?, output }, offPeak?: {…} }
|
|
8
|
+
* · schedule 描述计费区间:peakWindows 标出"贵"的时段(区间内走 standard,区间外走
|
|
9
|
+
* offPeak)。没写 schedule、或该模型没有 offPeak 时,全天同一价,卡片也就没有
|
|
10
|
+
* 指示灯与倒计时。
|
|
11
|
+
*
|
|
12
|
+
* 同一个 window 机制同时表达两种口径:
|
|
13
|
+
* · DeepSeek 官方峰谷:peakWindows = 工作日 9:00–12:00、14:00–18:00,
|
|
14
|
+
* peakDays = weekdays,holidays = true(中国法定节假日与周末全天空闲);
|
|
15
|
+
* · OpenRouter 上个别模型的分时 overrides:形状相同,但按 UTC 星期、不认中国
|
|
16
|
+
* 法定节假日(holidays = false)。所以同一个模型走官方路由与走网关,指示灯会
|
|
17
|
+
* 在节假日给出相反结论——这是两条真实计费规则,不是 bug。
|
|
18
|
+
*
|
|
19
|
+
* 模型改名:官方会把模型改名并把旧名保留为别名(如 deepseek-v4-flash →
|
|
20
|
+
* deepseek-flash)。旧名若落到表里就会长期显示过期价且无从察觉,因此每家提供商可以
|
|
21
|
+
* 带一份 aliases("旧名 → 现名"),quoteAt() 的 via 字段标出来源(exact / alias),
|
|
22
|
+
* 供排查,避免静默算错。
|
|
23
|
+
*
|
|
24
|
+
* 不做"同家族唯一匹配"(flash / pro / chat / reasoner):家族词没有词边界,
|
|
25
|
+
* deepseek-v5-pro、deepseek-prompt-helper 都会命中 pro 并按上代价格计价;官方发布
|
|
26
|
+
* 下一代模型后,卡片会自信地显示旧价而不是隐藏。表里没有的模型一律 known=false。
|
|
27
|
+
*
|
|
28
|
+
* 只按 provider + 模型 id 精确查表:第三方路由上的 deepseek-* 同名模型按各自规则扣费,
|
|
29
|
+
* 用官方表算出来的数字与账单无关;反过来,官方路由上不在表里的模型也不猜价。
|
|
30
|
+
*
|
|
31
|
+
* 价格快照均为手动采集,厂商调价时随插件版本更新(见 README「价格与官方不一致时」):
|
|
32
|
+
* 不联网抓取、不读写本地价格文件、不做后台同步。
|
|
33
|
+
*
|
|
34
|
+
* 时区:DeepSeek 的峰谷判定与"每日"边界按北京时间(Asia/Shanghai,固定 UTC+8,
|
|
35
|
+
* 无夏令时),不随部署机器时区变化;其它提供商用各自 schedule 的 offsetMinutes。
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/** 北京时间固定偏移(上海无夏令时)。 */
|
|
39
|
+
export const SHANGHAI_OFFSET_MS = 8 * 60 * 60 * 1000;
|
|
40
|
+
|
|
41
|
+
/** 默认模型(当 agentDefaultModel 不可用时的展示默认)。 */
|
|
42
|
+
export const DEFAULT_MODEL = 'deepseek-flash';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* DeepSeek 官方峰谷快照:2026-09-10 官方价格页。三条官方路由共用这一份。
|
|
46
|
+
*
|
|
47
|
+
* 原来的 [高峰, 空闲] 二元组在这里展开成 standard(高峰)/ offPeak(空闲)两个价;
|
|
48
|
+
* cacheWrite 官方暂不计费,两个区间都是 0。
|
|
49
|
+
*
|
|
50
|
+
* 只按模型名前缀判"官方"是不够的:Qwen token-plan、NVIDIA、opencode、workbuddy 这些
|
|
51
|
+
* 第三方路由上都有 `deepseek-*` 同名模型,它们按各自的计价规则扣费。所以查表以
|
|
52
|
+
* provider 为键——这些路由没有快照,一律 known=false;provider 缺失(旧宿主、路由
|
|
53
|
+
* 未上报)时同样隐藏价格,不显示一个与实际账单无关的数字。
|
|
54
|
+
*/
|
|
55
|
+
const DEEPSEEK = {
|
|
56
|
+
currency: 'CNY',
|
|
57
|
+
asOf: '2026-09-10',
|
|
58
|
+
schedule: { offsetMinutes: 480, peakWindows: [[9, 12], [14, 18]], peakDays: 'weekdays', holidays: true },
|
|
59
|
+
aliases: {
|
|
60
|
+
'deepseek-v4-flash': 'deepseek-flash',
|
|
61
|
+
'deepseek-v4-flash-vision-exp': 'deepseek-flash',
|
|
62
|
+
},
|
|
63
|
+
models: {
|
|
64
|
+
'deepseek-flash': {
|
|
65
|
+
standard: { input: 2, cacheRead: 0.04, cacheWrite: 0, output: 8 },
|
|
66
|
+
offPeak: { input: 1, cacheRead: 0.02, cacheWrite: 0, output: 4 },
|
|
67
|
+
},
|
|
68
|
+
'deepseek-v4-pro': {
|
|
69
|
+
standard: { input: 9, cacheRead: 0.3, cacheWrite: 0, output: 27 },
|
|
70
|
+
offPeak: { input: 4.5, cacheRead: 0.15, cacheWrite: 0, output: 13.5 },
|
|
71
|
+
},
|
|
72
|
+
// chat / reasoner 官方没有峰谷差价:两个区间同价,但仍是"有区间"的模型,
|
|
73
|
+
// 卡片照旧显示指示灯与倒计时(价格不变)。
|
|
74
|
+
'deepseek-chat': {
|
|
75
|
+
standard: { input: 2, cacheRead: 0.5, cacheWrite: 0, output: 8 },
|
|
76
|
+
offPeak: { input: 2, cacheRead: 0.5, cacheWrite: 0, output: 8 },
|
|
77
|
+
},
|
|
78
|
+
'deepseek-reasoner': {
|
|
79
|
+
standard: { input: 4, cacheRead: 1, cacheWrite: 0, output: 16 },
|
|
80
|
+
offPeak: { input: 4, cacheRead: 1, cacheWrite: 0, output: 16 },
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 各家提供商的按量计费价格表(手动采集的快照,每百万 tokens)。
|
|
87
|
+
*
|
|
88
|
+
* 只收**按 token 计费**的官方 API 模型:订阅套餐、按次计费、图片/语音等非 token
|
|
89
|
+
* 计费项都不收。表里没有的模型一律 known=false,界面隐藏价格。
|
|
90
|
+
*
|
|
91
|
+
* 币种按**一个站点**的口径维护——同一个模型 id 在不同站点价格不同,混用会与账单不符:
|
|
92
|
+
* · Moonshot / Kimi、Z.ai 取国际站美元价;
|
|
93
|
+
* · MiniMax 分 `minimax`(.io,美元)与 `minimax-cn`(.cn,人民币);
|
|
94
|
+
* · OpenRouter 是网关聚合价,记的是默认路由价,实际结算按响应里的上游 provider。
|
|
95
|
+
*/
|
|
96
|
+
const PRICE_TABLES = {
|
|
97
|
+
'deepseek-official': DEEPSEEK,
|
|
98
|
+
'deepseek-account': DEEPSEEK,
|
|
99
|
+
deepseek: DEEPSEEK,
|
|
100
|
+
|
|
101
|
+
// Moonshot / Kimi:platform.kimi.ai 国际站(USD)。国内站 kimi.com 是人民币价,
|
|
102
|
+
// 同 id 不同价,本表不混用。
|
|
103
|
+
moonshotai: {
|
|
104
|
+
currency: 'USD',
|
|
105
|
+
asOf: '2026-10-27',
|
|
106
|
+
models: {
|
|
107
|
+
'kimi-k3.1-pro': { standard: { input: 2, cacheRead: 0.2, output: 10 } },
|
|
108
|
+
'kimi-k3.1-pro-highspeed': { standard: { input: 4, cacheRead: 0.2, output: 20 } },
|
|
109
|
+
'kimi-k3': { standard: { input: 3, cacheRead: 0.3, output: 15 } },
|
|
110
|
+
'kimi-k2.7-code': { standard: { input: 0.95, cacheRead: 0.19, output: 4 } },
|
|
111
|
+
'kimi-k2.7-code-highspeed': { standard: { input: 1.9, cacheRead: 0.38, output: 8 } },
|
|
112
|
+
'kimi-k2.6': { standard: { input: 0.95, cacheRead: 0.16, output: 4 } },
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
// Z.ai / 智谱:docs.z.ai 国际站(USD)。BigModel 人民币价另有一份,本表不混用。
|
|
117
|
+
zai: {
|
|
118
|
+
currency: 'USD',
|
|
119
|
+
asOf: '2026-10-27',
|
|
120
|
+
models: {
|
|
121
|
+
'glm-5.3': { standard: { input: 1.4, cacheRead: 0.26, output: 4.4 } },
|
|
122
|
+
'glm-5.3-flash': { standard: { input: 0.15, cacheRead: 0.03, output: 0.5 } },
|
|
123
|
+
'glm-5.3-flashx': { standard: { input: 0.37, cacheRead: 0.075, output: 1.25 } },
|
|
124
|
+
'glm-5.2': { standard: { input: 1.4, cacheRead: 0.26, output: 4.4 } },
|
|
125
|
+
'glm-5.1': { standard: { input: 1.4, cacheRead: 0.26, output: 4.4 } },
|
|
126
|
+
'glm-5': { standard: { input: 1, cacheRead: 0.2, output: 3.2 } },
|
|
127
|
+
'glm-4.7': { standard: { input: 0.6, cacheRead: 0.11, output: 2.2 } },
|
|
128
|
+
'glm-4.7-flashx': { standard: { input: 0.07, cacheRead: 0.01, output: 0.4 } },
|
|
129
|
+
'glm-4.6': { standard: { input: 0.6, cacheRead: 0.11, output: 2.2 } },
|
|
130
|
+
'glm-4.6v': { standard: { input: 0.3, cacheRead: 0.05, output: 0.9 } },
|
|
131
|
+
'glm-4.5': { standard: { input: 0.6, cacheRead: 0.11, output: 2.2 } },
|
|
132
|
+
'glm-4.5-air': { standard: { input: 0.2, cacheRead: 0.03, output: 1.1 } },
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
// MiniMax 国际站(platform.minimax.io,USD)。
|
|
137
|
+
minimax: {
|
|
138
|
+
currency: 'USD',
|
|
139
|
+
asOf: '2026-10-27',
|
|
140
|
+
models: {
|
|
141
|
+
'minimax-m3.1-flash': { standard: { input: 0.15, cacheRead: 0.015, output: 0.6 } },
|
|
142
|
+
// M3 按输入长度分档(≤512k / >512k)与 priority 服务等级加价;这里取默认档
|
|
143
|
+
//(≤512k、标准服务),长上下文/优先服务会与账单有出入。
|
|
144
|
+
'minimax-m3': { standard: { input: 0.3, cacheRead: 0.06, output: 1.2 } },
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
// MiniMax 国内站(platform.minimax.cn,人民币)。
|
|
149
|
+
'minimax-cn': {
|
|
150
|
+
currency: 'CNY',
|
|
151
|
+
asOf: '2026-10-27',
|
|
152
|
+
models: {
|
|
153
|
+
'minimax-m3.1-flash': { standard: { input: 1, cacheRead: 0.1, output: 4 } },
|
|
154
|
+
'minimax-m3': { standard: { input: 2.1, cacheRead: 0.42, output: 8.4 } },
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
// OpenRouter(网关聚合价,USD)。`deepseek/deepseek-v4.1-flash-1017` 的分时 overrides
|
|
159
|
+
// 与官方 DeepSeek 峰谷同构(UTC 工作日 01–04、06–10 为贵的一侧,周末全天便宜),
|
|
160
|
+
// 但**没有**中国法定节假日概念——schedule 里 holidays 为 false。
|
|
161
|
+
openrouter: {
|
|
162
|
+
currency: 'USD',
|
|
163
|
+
asOf: '2026-10-27',
|
|
164
|
+
schedule: { offsetMinutes: 480, peakWindows: [[9, 12], [14, 18]], peakDays: 'weekdays', holidays: false },
|
|
165
|
+
models: {
|
|
166
|
+
'deepseek/deepseek-v4.1-flash-1017': {
|
|
167
|
+
standard: { input: 0.3, cacheRead: 0.006, output: 1.2 },
|
|
168
|
+
offPeak: { input: 0.15, cacheRead: 0.003, output: 0.6 },
|
|
169
|
+
},
|
|
170
|
+
'deepseek/deepseek-v4.1-flash': { standard: { input: 0.14, cacheRead: 0.0042, output: 0.42 } },
|
|
171
|
+
'claude-sonnet-5.5': { standard: { input: 2, cacheRead: 0.1, output: 10 } },
|
|
172
|
+
'gpt-6.1-sol': { standard: { input: 2, cacheRead: 0.1, output: 10 } },
|
|
173
|
+
'gemini-3.8-flash': { standard: { input: 0.825, cacheRead: 0.0825, output: 4.125 } },
|
|
174
|
+
'grok-4.7': { standard: { input: 2, cacheRead: 0.5, output: 6 } },
|
|
175
|
+
'glm-4.6': { standard: { input: 0.55, cacheRead: 0.11, output: 2.2 } },
|
|
176
|
+
'minimax-m3.1-flash': { standard: { input: 0.15, cacheRead: 0.015, output: 0.6 } },
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 中国法定节假日放假日期(含起止,闭区间;北京时间的日历日)。
|
|
183
|
+
* 来源:国务院办公厅《关于 2026 年部分节假日安排的通知》(国办发明电〔2025〕7号)。
|
|
184
|
+
* 官方口径:法定节假日全天、调休上班的周末均为空闲时段——周末本来就空闲,所以
|
|
185
|
+
* 只需列"放假日期",落在工作日里的那几天才会把判定从高峰翻成空闲。
|
|
186
|
+
* 下一年的安排通常在前一年 11 月发布,届时更新本表与 HOLIDAY_TABLE_THROUGH_YEAR;
|
|
187
|
+
* 当前年份未被覆盖时由 lib/index.js 告警(见 hasHolidayCoverage)。
|
|
188
|
+
*/
|
|
189
|
+
export const HOLIDAY_RANGES = [
|
|
190
|
+
['2026-01-01', '2026-01-03'], // 元旦
|
|
191
|
+
['2026-02-15', '2026-02-23'], // 春节
|
|
192
|
+
['2026-04-04', '2026-04-06'], // 清明节
|
|
193
|
+
['2026-05-01', '2026-05-05'], // 劳动节
|
|
194
|
+
['2026-06-19', '2026-06-21'], // 端午节
|
|
195
|
+
['2026-09-25', '2026-09-27'], // 中秋节
|
|
196
|
+
['2026-10-01', '2026-10-07'], // 国庆节
|
|
197
|
+
];
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 把 [起, 止] 闭区间展开成日期键集合(`yyyy-mm-dd`,O(1) 查表)。
|
|
201
|
+
*
|
|
202
|
+
* 非法日期(如 2027-02-30)会被 Date.parse 滚到 03-02,把一个不存在的日子变成
|
|
203
|
+
* 真的假日。这里先校验解析结果与输入一致,不一致就跳过该段。
|
|
204
|
+
*/
|
|
205
|
+
export function expandHolidayRanges(ranges) {
|
|
206
|
+
const set = new Set();
|
|
207
|
+
const DAY = 24 * 60 * 60 * 1000;
|
|
208
|
+
for (const [from, to] of ranges) {
|
|
209
|
+
if (typeof from !== 'string' || typeof to !== 'string') continue;
|
|
210
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(from) || !/^\d{4}-\d{2}-\d{2}$/.test(to)) continue;
|
|
211
|
+
const start = Date.parse(`${from}T00:00:00Z`);
|
|
212
|
+
const end = Date.parse(`${to}T00:00:00Z`);
|
|
213
|
+
if (!Number.isFinite(start) || !Number.isFinite(end) || end < start) continue;
|
|
214
|
+
// 解析结果必须与输入同一天,否则说明输入本身不是合法日期(已滚动到别的日子)。
|
|
215
|
+
if (new Date(start).toISOString().slice(0, 10) !== from) continue;
|
|
216
|
+
if (new Date(end).toISOString().slice(0, 10) !== to) continue;
|
|
217
|
+
for (let t = start; t <= end; t += DAY) set.add(new Date(t).toISOString().slice(0, 10));
|
|
218
|
+
}
|
|
219
|
+
return set;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const HOLIDAY_DAYS = expandHolidayRanges(HOLIDAY_RANGES);
|
|
223
|
+
|
|
224
|
+
/** 某时刻在指定固定偏移时区下的钟点、星期、日期键与当日零点(ms)。 */
|
|
225
|
+
function scheduleParts(ms, offsetMinutes) {
|
|
226
|
+
const local = new Date(ms + offsetMinutes * 60000);
|
|
227
|
+
const dateKey = local.toISOString().slice(0, 10);
|
|
228
|
+
return {
|
|
229
|
+
hour: local.getUTCHours(),
|
|
230
|
+
dow: local.getUTCDay(),
|
|
231
|
+
dateKey,
|
|
232
|
+
dayStart: Date.parse(`${dateKey}T00:00:00Z`) - offsetMinutes * 60000,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const scheduleOffsetOf = (schedule) =>
|
|
237
|
+
Number.isFinite(schedule.offsetMinutes) ? schedule.offsetMinutes : SHANGHAI_OFFSET_MS / 60000;
|
|
238
|
+
|
|
239
|
+
/** 某时刻是否落在"贵"的一侧(peakWindows 窗口内,且不属于节假日/非工作日豁免)。 */
|
|
240
|
+
function inExpensiveWindow(ms, schedule, holidays) {
|
|
241
|
+
const { hour, dow, dateKey } = scheduleParts(ms, scheduleOffsetOf(schedule));
|
|
242
|
+
if (!Array.isArray(schedule.peakWindows)) return false;
|
|
243
|
+
const weekday = dow !== 0 && dow !== 6;
|
|
244
|
+
const holiday = schedule.holidays === true && holidays.has(dateKey);
|
|
245
|
+
if (holiday) return false;
|
|
246
|
+
if ((schedule.peakDays ?? 'everyday') === 'weekdays' && !weekday) return false;
|
|
247
|
+
return schedule.peakWindows.some(([start, end]) => hour >= start && hour < end);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 距下一次计费区间翻转的剩余时间。
|
|
252
|
+
*
|
|
253
|
+
* 只枚举窗口边界的整点(高峰起止 / 优惠起止),逐日推进;上限 32 天,覆盖
|
|
254
|
+
* "长假最后一天 18:00 → 假期后第一个工作日 09:00"这类最长空档(2026 年春节假期
|
|
255
|
+
* 9 天,空档 10.7 天)。找不到时返回 at:null——调用方据此显示占位符,不要当成"此刻"。
|
|
256
|
+
*/
|
|
257
|
+
function nextWindowChange(ms, schedule, holidays) {
|
|
258
|
+
const current = inExpensiveWindow(ms, schedule, holidays);
|
|
259
|
+
const windows = schedule.peakWindows;
|
|
260
|
+
if (!Array.isArray(windows) || windows.length === 0) return { peak: current, at: null, ms: 0 };
|
|
261
|
+
const hours = [...new Set(windows.flat())].sort((a, b) => a - b);
|
|
262
|
+
const { dayStart } = scheduleParts(ms, scheduleOffsetOf(schedule));
|
|
263
|
+
const DAY = 24 * 60 * 60 * 1000;
|
|
264
|
+
for (let day = 0; day < 32; day += 1) {
|
|
265
|
+
for (const hour of hours) {
|
|
266
|
+
const at = dayStart + day * DAY + hour * 60 * 60 * 1000;
|
|
267
|
+
if (at <= ms) continue;
|
|
268
|
+
const expensive = inExpensiveWindow(at, schedule, holidays);
|
|
269
|
+
if (expensive !== current) return { peak: expensive, at, ms: at - ms };
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// 理论上不可达(不变量:一个月内必有工作日整点边界)。返回 at:null 而不是 now,
|
|
273
|
+
// 否则调用方会显示 switching 并永久卡住。
|
|
274
|
+
return { peak: !current, at: null, ms: 0 };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const DEEPSEEK_SCHEDULE = DEEPSEEK.schedule;
|
|
278
|
+
|
|
279
|
+
/** 某时刻是否为 DeepSeek 高峰时段(北京时间;法定节假日全天与周末恒为空闲)。
|
|
280
|
+
*
|
|
281
|
+
* `holidays` 可选:浏览器侧镜像无法 import 本模块,测试与宿主以外的调用方可以传入
|
|
282
|
+
* 自己的日期集合;缺省用内置表,保证 `isPeak(ms)` 的既有语义不变。
|
|
283
|
+
*/
|
|
284
|
+
export function isPeak(ms, holidays = HOLIDAY_DAYS) {
|
|
285
|
+
return inExpensiveWindow(Number.isFinite(ms) ? ms : Date.now(), DEEPSEEK_SCHEDULE, holidays);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* 距下一次 DeepSeek 峰谷翻转的剩余时间(供卡片显示倒计时)。
|
|
290
|
+
*
|
|
291
|
+
* 与 isPeak 同源同口径,只是换一个视图:返回「下一次 isPeak 结果发生翻转」的时刻。
|
|
292
|
+
* 注意空闲时段里的下一次切换**不等于**下一个整点:
|
|
293
|
+
* · 12:00–14:00 午休 → 下一次切换是 14:00(高峰开始);
|
|
294
|
+
* · 周五 18:00 之后、整个周末 → 下一次切换是下周一 09:00(跨 2 天多)。
|
|
295
|
+
* 因此倒计时必须能显示「天」。
|
|
296
|
+
*
|
|
297
|
+
* @returns {{ peak: boolean, at: number, ms: number }} peak=切换后所处的时段,
|
|
298
|
+
* at=切换发生的时刻(ms 时间戳,新时段在此刻生效),ms=剩余毫秒(不会为负)。
|
|
299
|
+
* 找不到切换点时返回 `at: null`——调用方据此判断"不知道",不要把它当成"此刻"。
|
|
300
|
+
*/
|
|
301
|
+
export function nextChange(ms, holidays = HOLIDAY_DAYS) {
|
|
302
|
+
return nextWindowChange(Number.isFinite(ms) ? ms : Date.now(), DEEPSEEK_SCHEDULE, holidays);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* 模型名是否属于 DeepSeek 官方峰谷口径(仅用于订阅制路由上的时段指示)。
|
|
307
|
+
*
|
|
308
|
+
* 订阅制路由(OpenCode Go 等)没有价格快照,单价仍是 known=false;但当当前模型
|
|
309
|
+
* 本身就是 DeepSeek 的峰谷型号时,卡片照旧显示峰谷指示灯与倒计时。这里只判断
|
|
310
|
+
* 模型名,不改变任何价格结论。
|
|
311
|
+
*/
|
|
312
|
+
export function isDeepSeekScheduleModel(model) {
|
|
313
|
+
if (typeof model !== 'string') return false;
|
|
314
|
+
const id = model.trim().toLowerCase();
|
|
315
|
+
if (id === '') return false;
|
|
316
|
+
return id === 'deepseek' || id.startsWith('deepseek-') || id.startsWith('deepseek.');
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* 放假日期表覆盖到的年份(含)。
|
|
321
|
+
*
|
|
322
|
+
* 国务院办公厅通常在前一年 11 月发布次年安排,届时更新 HOLIDAY_RANGES 并把这个
|
|
323
|
+
* 常量 +1。不能用"表里有没有这一年的某一天"来判定覆盖:只补一条元旦记录就会让
|
|
324
|
+
* 判定通过,而春节、国庆仍然按工作日计价。
|
|
325
|
+
*/
|
|
326
|
+
export const HOLIDAY_TABLE_THROUGH_YEAR = 2026;
|
|
327
|
+
|
|
328
|
+
/** 该时刻所在的北京时间年份是否已被放假日期表覆盖。 */
|
|
329
|
+
export function hasHolidayCoverage(ms = Date.now()) {
|
|
330
|
+
const year = new Date(ms + SHANGHAI_OFFSET_MS).getUTCFullYear();
|
|
331
|
+
return year <= HOLIDAY_TABLE_THROUGH_YEAR;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const has = (obj, key) => !!obj && Object.prototype.hasOwnProperty.call(obj, key);
|
|
335
|
+
|
|
336
|
+
/** 取某 provider 的价格快照(id 大小写、首尾空白无关)。 */
|
|
337
|
+
function tableOf(provider) {
|
|
338
|
+
if (typeof provider !== 'string') return null;
|
|
339
|
+
const id = provider.trim().toLowerCase();
|
|
340
|
+
return id !== '' && has(PRICE_TABLES, id) ? PRICE_TABLES[id] : null;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** 在快照里找模型:精确 → 别名。表里没有就是不知道,不做家族猜测。 */
|
|
344
|
+
function lookupModel(table, id) {
|
|
345
|
+
const key = id.trim().toLowerCase();
|
|
346
|
+
if (has(table.models, key)) return { id: key, via: 'exact' };
|
|
347
|
+
if (table.aliases !== undefined && has(table.aliases, key) && has(table.models, table.aliases[key])) {
|
|
348
|
+
return { id: table.aliases[key], via: 'alias' };
|
|
349
|
+
}
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** 按当前区间取价:便宜的一侧用 offPeak(若提供),否则一律 standard。 */
|
|
354
|
+
function priceOf(entry, expensive) {
|
|
355
|
+
const price = !expensive && entry.offPeak ? entry.offPeak : entry.standard;
|
|
356
|
+
return {
|
|
357
|
+
input: price.input,
|
|
358
|
+
cacheRead: price.cacheRead ?? null,
|
|
359
|
+
cacheWrite: price.cacheWrite ?? 0,
|
|
360
|
+
output: price.output,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* 某模型在某一时刻的单位价格(每百万 tokens,币种见返回的 currency)。
|
|
366
|
+
*
|
|
367
|
+
* 一条路径:provider → 该 provider 的价格快照 → 模型(精确 → 别名)→ 按 schedule
|
|
368
|
+
* 判断当前区间并取价。DeepSeek 与其它提供商的唯一差别都在表里(币种、schedule、
|
|
369
|
+
* 有没有法定节假日),代码不分叉。查不到 provider 或模型时返回 known=false 的占位
|
|
370
|
+
* 结果,调用方据此隐藏价格——绝不按模型名猜价。
|
|
371
|
+
*
|
|
372
|
+
* @returns {{ input, cacheRead, cacheWrite, output, currency, window, next, model,
|
|
373
|
+
* requested, provider, known, via }}
|
|
374
|
+
* window 为 'peak'(当前是贵的一侧)/ 'idle'(便宜的一侧)/ null(全天一价);
|
|
375
|
+
* next 为 { peak, at, ms }(下一次翻转)或 null(全天一价)。
|
|
376
|
+
*/
|
|
377
|
+
export function quoteAt(model, ms, provider, holidays = HOLIDAY_DAYS) {
|
|
378
|
+
const now = Number.isFinite(ms) ? ms : Date.now();
|
|
379
|
+
const requested = typeof model === 'string' ? model : '';
|
|
380
|
+
const providerId = typeof provider === 'string' ? provider.trim().toLowerCase() : '';
|
|
381
|
+
|
|
382
|
+
const table = tableOf(providerId);
|
|
383
|
+
const found = table !== null && requested.trim() !== '' ? lookupModel(table, requested) : null;
|
|
384
|
+
if (table !== null && found !== null) {
|
|
385
|
+
const entry = table.models[found.id];
|
|
386
|
+
// 没有 offPeak 的模型全天一价;有 offPeak 才按 provider 的 schedule 判区间。
|
|
387
|
+
const schedule = entry.offPeak ? table.schedule ?? null : null;
|
|
388
|
+
const expensive = schedule !== null && inExpensiveWindow(now, schedule, holidays);
|
|
389
|
+
return {
|
|
390
|
+
...priceOf(entry, expensive),
|
|
391
|
+
currency: table.currency ?? 'CNY',
|
|
392
|
+
window: schedule === null ? null : (expensive ? 'peak' : 'idle'),
|
|
393
|
+
next: schedule === null ? null : nextWindowChange(now, schedule, holidays),
|
|
394
|
+
model: found.id,
|
|
395
|
+
requested,
|
|
396
|
+
provider: providerId,
|
|
397
|
+
known: true,
|
|
398
|
+
via: found.via,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// 兜底:没有价格就是没有数字(known=false),不拿同名的其它模型顶替。
|
|
403
|
+
return {
|
|
404
|
+
input: null,
|
|
405
|
+
cacheRead: null,
|
|
406
|
+
cacheWrite: null,
|
|
407
|
+
output: null,
|
|
408
|
+
currency: 'CNY',
|
|
409
|
+
window: null,
|
|
410
|
+
next: null,
|
|
411
|
+
model: null,
|
|
412
|
+
requested,
|
|
413
|
+
provider: typeof provider === 'string' ? provider : '',
|
|
414
|
+
known: false,
|
|
415
|
+
via: 'fallback',
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** 兼容旧名:宿主与测试仍以 unitAt 调用。 */
|
|
420
|
+
export const unitAt = quoteAt;
|
package/lib/providers.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-deepseek-usage — provider 映射解析。
|
|
3
|
+
*
|
|
4
|
+
* 有些第三方插件会注册自己的 provider 路由(例如 dsh-opencode-go 注册的
|
|
5
|
+
* `dsh-opencode-go`),它调用的是同一个厂商的订阅账户,但 provider id 不在
|
|
6
|
+
* lib/accounts.js 的能力表里,插件就认不出来。这里给用户一个显式入口:在插件
|
|
7
|
+
* 设置页把「路由 id → 受支持提供商」写成映射,解析时按映射套用该提供商的端点
|
|
8
|
+
* 与凭据引用。
|
|
9
|
+
*
|
|
10
|
+
* 识别顺序固定:**精确 id 优先**(能力表是权威,映射不能覆盖已支持的路由),
|
|
11
|
+
* 命中不了才查用户映射;再查用户自行登记的自定义 provider(`customProviders`,
|
|
12
|
+
* 带 baseUrl 与字段路径,见 lib/custom.js);都不命中返回 null,调用方按「不支持」处理。
|
|
13
|
+
*
|
|
14
|
+
* 不按名字、不按端点猜:网关或中转可以起任何名字、指向任何地址,猜出来的读数
|
|
15
|
+
* 与账单无关,正是本插件一直避免的情况。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { accountCapabilityOf, aliasableProviderIds, candidateRefsOf } from './accounts.js';
|
|
19
|
+
|
|
20
|
+
/** 凭据引用名:DSH 与插件都用大写环境变量名。 */
|
|
21
|
+
const REF_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
22
|
+
|
|
23
|
+
/** provider id 的宽松上限;不限制字符集,只挡掉空白与异常长度。 */
|
|
24
|
+
const MAX_PROVIDER_ID_LENGTH = 128;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 把配置里的映射整理成可查询的 Map。
|
|
28
|
+
*
|
|
29
|
+
* 接受 `[{ provider, capability, refs? }]`;非法项整体丢弃并记一条 warning
|
|
30
|
+
* (由调用方写进日志,避免坏配置静默生效)。重复 provider 取第一条。
|
|
31
|
+
*
|
|
32
|
+
* schemastery 的 `volatile()` 字段在 Cordis 校验后会包成带 `get()` 的引用对象
|
|
33
|
+
* (设置页要就地更新表单),这里先解包;普通数组原样使用。
|
|
34
|
+
*
|
|
35
|
+
* @param {unknown} raw `config.providerAliases`
|
|
36
|
+
* @returns {{ aliases: Map<string, { provider: string, capability: string, refs: string[] }>, warnings: string[] }}
|
|
37
|
+
*/
|
|
38
|
+
export function normalizeAliases(raw) {
|
|
39
|
+
const aliases = new Map();
|
|
40
|
+
const warnings = [];
|
|
41
|
+
if (raw !== null && typeof raw === 'object' && !Array.isArray(raw) && typeof raw.get === 'function') {
|
|
42
|
+
try {
|
|
43
|
+
return normalizeAliases(raw.get());
|
|
44
|
+
} catch {
|
|
45
|
+
warnings.push('providerAliases 无法读取,已忽略');
|
|
46
|
+
return { aliases, warnings };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (raw === undefined || raw === null) return { aliases, warnings };
|
|
50
|
+
if (!Array.isArray(raw)) {
|
|
51
|
+
warnings.push('providerAliases 不是数组,已忽略');
|
|
52
|
+
return { aliases, warnings };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const aliasable = new Set(aliasableProviderIds());
|
|
56
|
+
for (const item of raw) {
|
|
57
|
+
if (item === null || typeof item !== 'object' || Array.isArray(item)) {
|
|
58
|
+
warnings.push('忽略了一条不是对象的 provider 映射');
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const provider = typeof item.provider === 'string' ? item.provider.trim() : '';
|
|
62
|
+
const capability = typeof item.capability === 'string' ? item.capability.trim() : '';
|
|
63
|
+
// 只挡控制字符与超长:用户会把自己起的名字写成 "DeepSeek ASNIA" 这种带空格的形式,
|
|
64
|
+
// 旧规则把空格也算非法,映射就被静默丢弃(设置页能保存、右键菜单里却看不到)。
|
|
65
|
+
if (provider === '' || provider.length > MAX_PROVIDER_ID_LENGTH || /[\u0000-\u001f]/.test(provider)) {
|
|
66
|
+
warnings.push('忽略了一条 provider id 非法的映射');
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (!aliasable.has(capability)) {
|
|
70
|
+
warnings.push(`忽略映射 ${provider}:目标提供商 "${capability}" 不可映射`);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const key = provider.toLowerCase();
|
|
74
|
+
if (aliases.has(key)) {
|
|
75
|
+
warnings.push(`忽略重复的 provider 映射:${provider}`);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
// OAuth 目标的凭据是订阅令牌(经 /token 写进凭据服务),映射行不存引用。
|
|
79
|
+
const oauthTarget = accountCapabilityOf(capability).kind === 'oauth';
|
|
80
|
+
const refs = !oauthTarget && Array.isArray(item.refs)
|
|
81
|
+
? [...new Set(item.refs
|
|
82
|
+
.filter((ref) => typeof ref === 'string')
|
|
83
|
+
.map((ref) => ref.trim())
|
|
84
|
+
.filter((ref) => REF_PATTERN.test(ref)))]
|
|
85
|
+
: [];
|
|
86
|
+
aliases.set(key, { provider, capability, refs });
|
|
87
|
+
}
|
|
88
|
+
return { aliases, warnings };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 造一个解析器。`resolve(id)` 返回
|
|
93
|
+
* `{ id, capabilityId, entry, refs, via }`(via 为 'exact' / 'alias' / 'custom'),
|
|
94
|
+
* 未识别返回 null。
|
|
95
|
+
*
|
|
96
|
+
* @param {Map<string, { provider: string, capability: string, refs: string[] }>} aliases
|
|
97
|
+
* @param {Map<string, object>} customs 自定义 provider(lib/custom.js 归一化后的 entry)
|
|
98
|
+
*/
|
|
99
|
+
export function createProviderResolver(aliases = new Map(), customs = new Map()) {
|
|
100
|
+
const table = aliases instanceof Map ? aliases : new Map();
|
|
101
|
+
const customTable = customs instanceof Map ? customs : new Map();
|
|
102
|
+
|
|
103
|
+
/** 用户映射的候选引用:显式引用 → 该路由自己的派生引用 → 能力表默认引用。 */
|
|
104
|
+
function aliasRefs(provider, refs, entry) {
|
|
105
|
+
const derived = `${String(provider).toUpperCase().replace(/[^A-Z0-9]+/g, '_')}_API_KEY`;
|
|
106
|
+
return [...new Set([...refs, derived, ...(entry.refs ?? [])])];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
/** 配置里实际生效的映射条数(诊断用)。 */
|
|
111
|
+
get size() {
|
|
112
|
+
return table.size;
|
|
113
|
+
},
|
|
114
|
+
/** 映射解析出的能力 id(去重)。/accounts 用它决定哪些 OAuth 令牌需要展示。 */
|
|
115
|
+
get capabilityIds() {
|
|
116
|
+
const ids = new Set();
|
|
117
|
+
for (const alias of table.values()) {
|
|
118
|
+
const entry = accountCapabilityOf(alias.capability);
|
|
119
|
+
if (entry !== null) ids.add(entry.id);
|
|
120
|
+
}
|
|
121
|
+
return [...ids];
|
|
122
|
+
},
|
|
123
|
+
/** 已登记的自定义 provider id(诊断 / 插件页展示用)。 */
|
|
124
|
+
get customIds() {
|
|
125
|
+
return [...customTable.values()].map((entry) => entry.id);
|
|
126
|
+
},
|
|
127
|
+
/**
|
|
128
|
+
* 用户真正添加过的 provider:映射表里的 + 自定义表里的。
|
|
129
|
+
* 侧栏只列这些——内置能力表里那十几个是"支持",不是"添加过"。
|
|
130
|
+
*/
|
|
131
|
+
get addedIds() {
|
|
132
|
+
return [...table.values()].map((alias) => alias.provider).concat([...customTable.values()].map((entry) => entry.id));
|
|
133
|
+
},
|
|
134
|
+
/**
|
|
135
|
+
* @param {unknown} id 原始 provider id(来自 agentDefaultModel)
|
|
136
|
+
* @returns {null | { id: string, capabilityId: string, entry: object, refs: string[], via: 'exact' | 'alias' }}
|
|
137
|
+
*/
|
|
138
|
+
resolve(id) {
|
|
139
|
+
const raw = typeof id === 'string' ? id.trim() : '';
|
|
140
|
+
if (raw === '') return null;
|
|
141
|
+
|
|
142
|
+
// 1. 精确 id:能力表是权威,永远优先。
|
|
143
|
+
const exact = accountCapabilityOf(raw);
|
|
144
|
+
if (exact !== null) {
|
|
145
|
+
return {
|
|
146
|
+
id: raw,
|
|
147
|
+
capabilityId: exact.id,
|
|
148
|
+
entry: exact,
|
|
149
|
+
refs: candidateRefsOf(exact.id),
|
|
150
|
+
via: 'exact',
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// 2. 用户映射:只对能力表里没有的路由生效。
|
|
155
|
+
const alias = table.get(raw.toLowerCase());
|
|
156
|
+
if (alias !== undefined) {
|
|
157
|
+
const entry = accountCapabilityOf(alias.capability);
|
|
158
|
+
if (entry !== null) {
|
|
159
|
+
return {
|
|
160
|
+
id: raw,
|
|
161
|
+
capabilityId: entry.id,
|
|
162
|
+
entry,
|
|
163
|
+
// OAuth 目标不读 API Key 引用:令牌固定从该提供商的 tokenRef 解析。
|
|
164
|
+
refs: entry.kind === 'oauth' ? [] : aliasRefs(raw, alias.refs, entry),
|
|
165
|
+
via: 'alias',
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 3. 自定义 provider:用户自己登记了地址与字段(能力表里没有的网关 / 中转)。
|
|
171
|
+
const custom = customTable.get(raw.toLowerCase());
|
|
172
|
+
if (custom === undefined) return null;
|
|
173
|
+
return {
|
|
174
|
+
id: raw,
|
|
175
|
+
// 前缀保证缓存桶与能力表 id 不撞车;/accounts 的 unmapped 判定只看 null。
|
|
176
|
+
capabilityId: `custom:${custom.id}`,
|
|
177
|
+
entry: custom,
|
|
178
|
+
refs: Array.isArray(custom.refs) ? custom.refs : [],
|
|
179
|
+
via: 'custom',
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
}
|
package/locale/en.json
ADDED