@angelyeye/dsh-cost-tracker 1.8.15 → 1.9.1
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/CHANGELOG.md +163 -0
- package/README.en.md +90 -1
- package/README.md +95 -3
- package/client.js +738 -149
- package/config.js +132 -1
- package/credstore.js +248 -0
- package/docs/THIRD-PARTY-NOTICES.md +60 -0
- package/docs/provider-pricing.json +777 -0
- package/import.js +363 -0
- package/index.js +581 -85
- package/package.json +11 -3
- package/price-sync.js +216 -0
- package/pricing.js +111 -8
- package/schema.js +4 -0
- package/sync.js +25 -4
- package/vendor-catalog.js +198 -0
package/config.js
CHANGED
|
@@ -59,6 +59,22 @@
|
|
|
59
59
|
// 因此老配置文件升级后为零改动、开箱可用。配额查询走方舟管控面 OpenAPI,
|
|
60
60
|
// 需要 IAM 子用户具备 ArkReadOnlyAccess + BillingCenterReadOnlyAccess。
|
|
61
61
|
// 注意与推理用的 ARK API Key 是**两套不同的凭据**,不要混填。
|
|
62
|
+
//
|
|
63
|
+
// 字段(双轨计费 + 价格目录,v1.9.0):
|
|
64
|
+
// showTotalWithPlan 金额展示口径:false=只算按量(默认);true=含 Plan 等值总额
|
|
65
|
+
// planOverrides 订阅归类覆盖 {'provider/model'|'provider/*': 'plan'|'api'}
|
|
66
|
+
// priceMatch 目录匹配模式:fuzzy(默认,归一化包含匹配)/ exact
|
|
67
|
+
// catalogFxRate 目录价 USD→CNY 汇率(默认 7.2)
|
|
68
|
+
//
|
|
69
|
+
// 字段(历史导入,v1.9.0):
|
|
70
|
+
// autoImport 启动时自动回放宿主会话日志补录装插件前的调用(幂等)
|
|
71
|
+
//
|
|
72
|
+
// 字段(官方价格同步,v1.9.0):
|
|
73
|
+
// priceSyncUrl 官方定价页地址(中文页 = 人民币价)
|
|
74
|
+
// priceSyncAutoCheck 每日自动核对官方价(只记录差异,应用需手动确认)
|
|
75
|
+
//
|
|
76
|
+
// 字段(安全,v1.9.0,内部标记):
|
|
77
|
+
// secretsMigrated v1.8.x 明文密钥已迁入 DSH 凭据库(幂等迁移标记)
|
|
62
78
|
// ============================================================
|
|
63
79
|
|
|
64
80
|
/** 默认峰谷计价生效时间(UTC;两档方案已即时生效,门控恒通过) */
|
|
@@ -237,13 +253,128 @@ export function normalizeVolcengineConfig(raw) {
|
|
|
237
253
|
}
|
|
238
254
|
}
|
|
239
255
|
|
|
240
|
-
|
|
256
|
+
// ------------------------------------------------------------
|
|
257
|
+
// 双轨计费 + 价格目录(v1.9.0)
|
|
258
|
+
// ------------------------------------------------------------
|
|
259
|
+
|
|
260
|
+
/** 双轨计费与价格目录默认配置 */
|
|
261
|
+
export function defaultBillingConfig() {
|
|
262
|
+
return {
|
|
263
|
+
// 金额展示口径:false = 各金额卡只算按量(真金白银,订阅以附注展示);
|
|
264
|
+
// true = 含 Plan 等值金额(总口径)。
|
|
265
|
+
showTotalWithPlan: false,
|
|
266
|
+
// 订阅归类覆盖:'provider/model' 或 'provider/*' → 'plan' | 'api'
|
|
267
|
+
planOverrides: {},
|
|
268
|
+
// 多厂商目录匹配模式:fuzzy(归一化包含匹配,默认)/ exact(全等)
|
|
269
|
+
priceMatch: 'fuzzy',
|
|
270
|
+
// 目录价 USD→CNY 汇率(目录数据为 USD / 1M tokens,账本为 CNY)
|
|
271
|
+
catalogFxRate: 7.2,
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* 规范化双轨计费与价格目录配置。
|
|
277
|
+
* planOverrides 键统一为小写(provider 与 model 各自归一化拼 '/'),
|
|
278
|
+
* 值只接受 'plan' / 'api',最多 128 条。
|
|
279
|
+
*/
|
|
280
|
+
export function normalizeBillingConfig(raw) {
|
|
281
|
+
const def = defaultBillingConfig()
|
|
282
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return def
|
|
283
|
+
const planOverrides = {}
|
|
284
|
+
if (raw.planOverrides && typeof raw.planOverrides === 'object' && !Array.isArray(raw.planOverrides)) {
|
|
285
|
+
for (const [k, v] of Object.entries(raw.planOverrides)) {
|
|
286
|
+
if (v !== 'plan' && v !== 'api') continue
|
|
287
|
+
const parts = String(k).split('/')
|
|
288
|
+
if (parts.length !== 2 || !parts[0].trim() || !parts[1].trim()) continue
|
|
289
|
+
const norm = (s) => s.trim().toLowerCase().replace(/[^a-z0-9*]/g, '') // 通配键保留 *
|
|
290
|
+
const key = norm(parts[0]) + '/' + norm(parts[1])
|
|
291
|
+
if (!key.split('/')[1]) continue
|
|
292
|
+
planOverrides[key] = v
|
|
293
|
+
if (Object.keys(planOverrides).length >= 128) break
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const fx = Number(raw.catalogFxRate)
|
|
297
|
+
return {
|
|
298
|
+
showTotalWithPlan: raw.showTotalWithPlan === true,
|
|
299
|
+
planOverrides,
|
|
300
|
+
priceMatch: raw.priceMatch === 'exact' ? 'exact' : 'fuzzy',
|
|
301
|
+
catalogFxRate: Number.isFinite(fx) && fx >= 0.1 && fx <= 100 ? fx : def.catalogFxRate,
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ------------------------------------------------------------
|
|
306
|
+
// 历史导入(v1.9.0)
|
|
307
|
+
// ------------------------------------------------------------
|
|
308
|
+
|
|
309
|
+
/** 历史导入默认配置 */
|
|
310
|
+
export function defaultImportConfig() {
|
|
311
|
+
return {
|
|
312
|
+
// 启动时自动回放宿主会话日志,补录「装插件之前」的调用(幂等,可关闭)
|
|
313
|
+
autoImport: true,
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export function normalizeImportConfig(raw) {
|
|
318
|
+
const def = defaultImportConfig()
|
|
319
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return def
|
|
320
|
+
return { autoImport: raw.autoImport !== false }
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// ------------------------------------------------------------
|
|
324
|
+
// 官方价格同步(v1.9.0)
|
|
325
|
+
// ------------------------------------------------------------
|
|
326
|
+
|
|
327
|
+
/** 官方定价页(中文页直接给出人民币价,与账本币种一致) */
|
|
328
|
+
export const DEFAULT_PRICING_DOC_URL = 'https://api-docs.deepseek.com/zh-cn/quick_start/pricing'
|
|
329
|
+
|
|
330
|
+
/** 价格同步默认配置 */
|
|
331
|
+
export function defaultPriceSyncConfig() {
|
|
332
|
+
return {
|
|
333
|
+
priceSyncUrl: DEFAULT_PRICING_DOC_URL,
|
|
334
|
+
// 每日自动核对官方价:只核对并记录差异,应用仍需在配置卡手动确认
|
|
335
|
+
priceSyncAutoCheck: true,
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function normalizePriceSyncConfig(raw) {
|
|
340
|
+
const def = defaultPriceSyncConfig()
|
|
341
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return def
|
|
342
|
+
const url = typeof raw.priceSyncUrl === 'string' ? raw.priceSyncUrl.trim() : ''
|
|
343
|
+
return {
|
|
344
|
+
priceSyncUrl: /^https:\/\/\S+$/i.test(url) ? url.slice(0, 512) : def.priceSyncUrl,
|
|
345
|
+
priceSyncAutoCheck: raw.priceSyncAutoCheck !== false,
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ------------------------------------------------------------
|
|
350
|
+
// 安全(v1.9.0):密钥迁移等内部标记
|
|
351
|
+
// ------------------------------------------------------------
|
|
352
|
+
|
|
353
|
+
export function defaultSecurityConfig() {
|
|
354
|
+
return {
|
|
355
|
+
// v1.8.x 明文密钥是否已迁入 DSH 凭据库(幂等迁移标记,配置卡不展示)
|
|
356
|
+
secretsMigrated: false,
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function normalizeSecurityConfig(raw) {
|
|
361
|
+
const def = defaultSecurityConfig()
|
|
362
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return def
|
|
363
|
+
return { secretsMigrated: raw.secretsMigrated === true }
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** 合并规范化:一份配置文件同时承载峰谷、云端同步、界面显示、计费/目录、
|
|
367
|
+
* 历史导入、价格同步与安全标记等各组字段 */
|
|
241
368
|
export function normalizePluginConfig(raw) {
|
|
242
369
|
return Object.assign(
|
|
243
370
|
normalizePeakConfig(raw),
|
|
244
371
|
normalizeCloudConfig(raw),
|
|
245
372
|
normalizeUiConfig(raw),
|
|
246
373
|
normalizeVolcengineConfig(raw),
|
|
374
|
+
normalizeBillingConfig(raw),
|
|
375
|
+
normalizeImportConfig(raw),
|
|
376
|
+
normalizePriceSyncConfig(raw),
|
|
377
|
+
normalizeSecurityConfig(raw),
|
|
247
378
|
)
|
|
248
379
|
}
|
|
249
380
|
|
package/credstore.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// DSH 花费统计插件 —— 凭据库封套 + 出站防护(纯逻辑 + seam 注入,可独立测试)
|
|
3
|
+
//
|
|
4
|
+
// 设计目标:**密钥零落盘**。密钥只存 DSH 凭据库(ctx.credentials,.credentials.yaml
|
|
5
|
+
// 由宿主凭据服务托管),配置文件里只允许出现「是否已配置」的布尔标记。
|
|
6
|
+
//
|
|
7
|
+
// 三层防线:
|
|
8
|
+
// 1. 存取封套:save / resolve / describe / clear —— 统一命名(COST_TRACKER_* 引用名),
|
|
9
|
+
// 未挂载凭据服务时回落「内存态」并在返回值里如实标注(绝不为兼容而悄悄把
|
|
10
|
+
// 明文写回配置文件);save() 的返回值明确区分「已持久化」与「只在内存里」。
|
|
11
|
+
// 2. 旧明文迁移:migrateLegacySecrets() —— 启动时把 v1.8.x 明文落盘的
|
|
12
|
+
// volcengineSecretAccessKey(base64 混淆)与 cloudToken 搬进凭据库,
|
|
13
|
+
// 然后从配置对象里清除并回写净化后的配置文件(幂等,带 secretsMigrated 标记)。
|
|
14
|
+
// **只有确认已持久化才清除明文**:凭据库不可写时保留明文,绝不丢凭据。
|
|
15
|
+
// 3. 出站防护:safeFetch() —— 每一跳都过主机白名单;非 loopback 强制 https;
|
|
16
|
+
// redirect:'manual' + 自管跳转(最多 3 跳):**携带凭据的请求不跨主机跟随**
|
|
17
|
+
// (防重定向把凭据带去别处),不带凭据的请求可在白名单内跟随
|
|
18
|
+
// (官方定价页 `/pricing` → `/pricing/` 这种同站加斜杠的 302 必须放行)。
|
|
19
|
+
// ============================================================
|
|
20
|
+
|
|
21
|
+
/** 本插件占用的凭据引用名(POSIX shell 标识符风格,宿主凭据库通用) */
|
|
22
|
+
export const CRED_REFS = {
|
|
23
|
+
cloudToken: 'COST_TRACKER_CLOUD_TOKEN',
|
|
24
|
+
volcengineSecret: 'COST_TRACKER_VOLCENGINE_SK',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isLoopbackHost(host) {
|
|
28
|
+
return /^(localhost|127\.0\.0\.1|\[::1?\]|::1)$/i.test(String(host || ''))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 从 URL 提取 hostname(不含端口);非法 URL 返回 '' */
|
|
32
|
+
export function hostnameOf(url) {
|
|
33
|
+
try {
|
|
34
|
+
return String(new URL(String(url)).hostname)
|
|
35
|
+
} catch (e) {
|
|
36
|
+
return ''
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 出站白名单校验。
|
|
42
|
+
* @param {string} url - 目标地址
|
|
43
|
+
* @param {string[]} allowHosts - 允许的 hostname 精确匹配列表(大小写不敏感)
|
|
44
|
+
* @returns {{ok:boolean, reason:string, host:string}}
|
|
45
|
+
*/
|
|
46
|
+
export function assertAllowedHost(url, allowHosts) {
|
|
47
|
+
const host = hostnameOf(url)
|
|
48
|
+
if (!host) return { ok: false, reason: 'URL 无法解析', host: '' }
|
|
49
|
+
const list = (allowHosts || []).map((h) => String(h || '').toLowerCase()).filter(Boolean)
|
|
50
|
+
const lower = host.toLowerCase()
|
|
51
|
+
if (list.length && list.indexOf(lower) < 0) {
|
|
52
|
+
return { ok: false, reason: '目标主机不在出站白名单内:' + lower + '(允许:' + list.join(', ') + ')', host: lower }
|
|
53
|
+
}
|
|
54
|
+
const proto = String(new URL(String(url)).protocol || '').toLowerCase()
|
|
55
|
+
if (proto !== 'https:' && !isLoopbackHost(host)) {
|
|
56
|
+
return { ok: false, reason: '非本机地址必须使用 https(当前 ' + proto + ')', host: lower }
|
|
57
|
+
}
|
|
58
|
+
return { ok: true, reason: '', host: lower }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 带防护的 fetch 封套(v1.9.0,含重定向策略)。
|
|
63
|
+
*
|
|
64
|
+
* ⚠️ **必须同时接受两种入参形状**(v1.9.0 首个版本的缺陷教训):
|
|
65
|
+
* · `safeFetch(url, { init: { method, body }, allowHosts })` —— 本模块自己的文档形状;
|
|
66
|
+
* · `safeFetch(url, { method, body, headers, allowHosts })` —— **fetch 原生签名**,
|
|
67
|
+
* 即 `<typeof fetch>` 位置上的封装写法 `(url, init) => safeFetch(url, { ...init, allowHosts })`。
|
|
68
|
+
* 只认 `opts.init` 会让后者丢掉 method/body:POST 静默退化成 GET、请求体消失,
|
|
69
|
+
* 云端回 405「方法不允许」——真实故障现场就是同步全面失败、水位不前进。
|
|
70
|
+
* 因此 fetch 的 init 字段在顶层也识别一遍(`opts.init` 优先)。
|
|
71
|
+
*
|
|
72
|
+
* 规则(按「凭据是否会跟着跑」区分,而不是一刀切拒绝 3xx):
|
|
73
|
+
* · 每次请求(含每一跳)都过 `assertAllowedHost`:主机白名单 + 非本机强制 https;
|
|
74
|
+
* · `redirect: 'manual'` 自己处理跳转,最多 3 跳;
|
|
75
|
+
* · **携带凭据**(authorization / api-key 等头)的请求**不跨主机跟随** ——
|
|
76
|
+
* 跨主机重定向一律失败并点名去向主机,这就是「防凭据外带」的硬边界;
|
|
77
|
+
* · 不带凭据的请求(如抓取官方定价页:`/pricing` → `/pricing/` 这种同站加斜杠)
|
|
78
|
+
* 可以在白名单内跟随,否则会把正常功能也一并挡死。
|
|
79
|
+
*
|
|
80
|
+
* @param {string} url
|
|
81
|
+
* @param {{headers?:object, init?:object, allowHosts?:string[], timeoutMs?:number, fetchFn?:typeof fetch}} opts
|
|
82
|
+
*/
|
|
83
|
+
export async function safeFetch(url, opts) {
|
|
84
|
+
const o = opts || {}
|
|
85
|
+
const f = o.fetchFn || globalThis.fetch
|
|
86
|
+
const allowHosts = o.allowHosts || []
|
|
87
|
+
const MAX_HOPS = 3
|
|
88
|
+
const init = Object.assign({}, o.init || {})
|
|
89
|
+
// ⚠️ 顶层 fetch 同名字段兜底(`opts.init` 优先)。v1.9.0 首个版本的缺陷就在
|
|
90
|
+
// 这里:index.js 按 fetch 原生签名接线 `(url, init) => safeFetch(url, {...init, allowHosts})`,
|
|
91
|
+
// 而本函数当时只读 `opts.init`,于是 method/body 被丢掉 —— POST 静默退化成 GET、
|
|
92
|
+
// 请求体消失,云端回 405「方法不允许」,同步全面失败(水位不前进)。
|
|
93
|
+
for (const k of ['method', 'body', 'headers', 'signal', 'cache', 'credentials', 'mode', 'referrer', 'referrerPolicy', 'integrity', 'keepalive', 'window', 'duplex']) {
|
|
94
|
+
if (o[k] !== undefined && init[k] === undefined) init[k] = o[k]
|
|
95
|
+
}
|
|
96
|
+
// init.headers 在前、opts.headers 在后:调用方经 init 传的签名头不被覆盖
|
|
97
|
+
const headers = Object.assign({}, init.headers || {}, o.headers || {})
|
|
98
|
+
const credentialHeader = Object.keys(headers).find((k) => /^(authorization|proxy-authorization|x-api-key|api-key)$/i.test(k))
|
|
99
|
+
let current = String(url)
|
|
100
|
+
let method = init.method
|
|
101
|
+
let body = init.body
|
|
102
|
+
|
|
103
|
+
for (let hop = 0; hop <= MAX_HOPS; hop += 1) {
|
|
104
|
+
const guard = assertAllowedHost(current, allowHosts)
|
|
105
|
+
if (!guard.ok) throw new Error('出站被拒绝:' + guard.reason)
|
|
106
|
+
const ac = new AbortController()
|
|
107
|
+
const timer = setTimeout(() => ac.abort(), o.timeoutMs || 15000)
|
|
108
|
+
let res
|
|
109
|
+
try {
|
|
110
|
+
res = await f(current, Object.assign({}, init, { method, body, headers, redirect: 'manual', signal: ac.signal }))
|
|
111
|
+
} finally {
|
|
112
|
+
clearTimeout(timer)
|
|
113
|
+
}
|
|
114
|
+
if (!(res.status >= 300 && res.status < 400)) return res
|
|
115
|
+
const loc = typeof res.headers.get === 'function' ? (res.headers.get('location') || '') : ''
|
|
116
|
+
if (!loc) return res // 3xx 但没给 Location:交给调用方按状态自行判断
|
|
117
|
+
let target
|
|
118
|
+
try {
|
|
119
|
+
target = new URL(loc, current).toString()
|
|
120
|
+
} catch (e) {
|
|
121
|
+
throw new Error('重定向目标无法解析:' + loc)
|
|
122
|
+
}
|
|
123
|
+
const fromHost = hostnameOf(current)
|
|
124
|
+
const toHost = hostnameOf(target)
|
|
125
|
+
if (credentialHeader && toHost !== fromHost) {
|
|
126
|
+
throw new Error('目标返回跨主机重定向 ' + res.status + ' → ' + toHost + '(携带 ' + credentialHeader + ' 的请求不跨主机跟随,防止凭据外带)')
|
|
127
|
+
}
|
|
128
|
+
const hopGuard = assertAllowedHost(target, allowHosts)
|
|
129
|
+
if (!hopGuard.ok) throw new Error('重定向被拒绝(' + res.status + ' → ' + toHost + '):' + hopGuard.reason)
|
|
130
|
+
if (hop === MAX_HOPS) throw new Error('重定向次数过多(>' + MAX_HOPS + ' 跳),已放弃:' + target)
|
|
131
|
+
// 303(以及 301/302 上的 POST)按 HTTP 语义降级为 GET 并丢弃 body
|
|
132
|
+
if (res.status === 303 || ((res.status === 301 || res.status === 302) && String(method || '').toUpperCase() === 'POST')) {
|
|
133
|
+
method = 'GET'
|
|
134
|
+
body = undefined
|
|
135
|
+
}
|
|
136
|
+
current = target
|
|
137
|
+
}
|
|
138
|
+
throw new Error('重定向处理异常')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 凭据封套工厂:包装宿主 credentials 服务;未挂载或只读时退化为**内存态**存储,
|
|
143
|
+
* 并通过 backend 如实标注('memory' / 'credentials'),调用方据此在 UI 提示
|
|
144
|
+
* 「凭据库不可用,密钥仅保存在本进程内存,重启后需重填」。
|
|
145
|
+
*
|
|
146
|
+
* `save()` 的返回值语义(v1.9.0 起明确):
|
|
147
|
+
* true = **已持久化**到凭据库(重启后仍在);
|
|
148
|
+
* false = 只写进了进程内存(凭据服务缺席或只读)—— 迁移逻辑据此决定
|
|
149
|
+
* 「能不能把配置文件里的明文删掉」,避免为了「零明文」而丢凭据。
|
|
150
|
+
*
|
|
151
|
+
* @param {object|undefined} cred - ctx.get('credentials')(可能为 undefined)
|
|
152
|
+
* @returns {{backend:string, durable:boolean, save:async, resolve:async, describe:async, clear:async}}
|
|
153
|
+
*/
|
|
154
|
+
export function createCredSeam(cred) {
|
|
155
|
+
const memory = new Map()
|
|
156
|
+
const usable = !!cred && typeof cred.resolve === 'function'
|
|
157
|
+
const durable = usable && typeof cred.set === 'function' && typeof cred.unset === 'function'
|
|
158
|
+
return {
|
|
159
|
+
backend: durable ? 'credentials' : 'memory',
|
|
160
|
+
durable,
|
|
161
|
+
async save(ref, value) {
|
|
162
|
+
const v = String(value == null ? '' : value).trim()
|
|
163
|
+
if (!v) return false
|
|
164
|
+
if (durable) {
|
|
165
|
+
await cred.set(ref, v)
|
|
166
|
+
return true
|
|
167
|
+
}
|
|
168
|
+
// 只读/无凭据服务:留一份内存副本让本次进程可用,但**不算持久化**
|
|
169
|
+
memory.set(ref, v)
|
|
170
|
+
return false
|
|
171
|
+
},
|
|
172
|
+
async resolve(ref) {
|
|
173
|
+
if (usable) {
|
|
174
|
+
try {
|
|
175
|
+
const r = await cred.resolve(ref)
|
|
176
|
+
if (r && r.value) return { value: String(r.value), backend: 'credentials' }
|
|
177
|
+
} catch (e) { /* 落到内存态 */ }
|
|
178
|
+
}
|
|
179
|
+
if (memory.has(ref)) return { value: memory.get(ref), backend: 'memory' }
|
|
180
|
+
return { value: '', backend: usable ? 'credentials' : 'memory' }
|
|
181
|
+
},
|
|
182
|
+
async describe(ref) {
|
|
183
|
+
if (usable && typeof cred.describe === 'function') {
|
|
184
|
+
try {
|
|
185
|
+
const d = await cred.describe(ref)
|
|
186
|
+
if (d && (d.configured || d.configured === 'local-store')) return { configured: true, writable: d.writable !== false, backend: 'credentials' }
|
|
187
|
+
} catch (e) { /* 落内存态 */ }
|
|
188
|
+
}
|
|
189
|
+
return { configured: memory.has(ref), writable: durable, backend: durable ? 'credentials' : 'memory' }
|
|
190
|
+
},
|
|
191
|
+
async clear(ref) {
|
|
192
|
+
memory.delete(ref)
|
|
193
|
+
if (durable) {
|
|
194
|
+
try { await cred.unset(ref) } catch (e) { /* 尽力而为 */ }
|
|
195
|
+
}
|
|
196
|
+
return true
|
|
197
|
+
},
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* v1.8.x 明文密钥迁移(幂等)。
|
|
203
|
+
*
|
|
204
|
+
* 旧版把 volcengineSecretAccessKey(base64 混淆后)与 cloudToken 直接写进
|
|
205
|
+
* cost-tracker-config.json。本函数把它们搬进凭据库,然后**清除配置对象里的
|
|
206
|
+
* 明文字段**并返回 {changed};调用方负责把净化后的配置落盘。
|
|
207
|
+
*
|
|
208
|
+
* **安全边界**:只有当凭据库确实可写(`save()` 返回 true,即已持久化)时才清除
|
|
209
|
+
* 配置里的明文。宿主没有凭据服务、或凭据服务只读时,明文**原样保留** ——
|
|
210
|
+
* 「配置文件零明文」不能以丢凭据为代价(那种情况下密钥只在内存里,重启即失效)。
|
|
211
|
+
*
|
|
212
|
+
* 迁移后 SK/AK 查询链:凭据库 → 环境变量(原有链保持不变)。
|
|
213
|
+
*
|
|
214
|
+
* @param {object} cfg - normalizePluginConfig 产出的配置对象(会被就地修改)
|
|
215
|
+
* @param {object} seam - createCredSeam 的实例
|
|
216
|
+
* @returns {Promise<{changed:boolean, moved:string[], notes:string[]}>}
|
|
217
|
+
*/
|
|
218
|
+
export async function migrateLegacySecrets(cfg, seam) {
|
|
219
|
+
const moved = []
|
|
220
|
+
const notes = []
|
|
221
|
+
let pending = false
|
|
222
|
+
if (!cfg || typeof cfg !== 'object') return { changed: false, moved, notes, pending }
|
|
223
|
+
/** 逐项搬运:只有持久化成功才清空配置字段 */
|
|
224
|
+
async function move(field, ref, label) {
|
|
225
|
+
const value = String(cfg[field] || '').trim()
|
|
226
|
+
if (!value) return
|
|
227
|
+
const d = await seam.describe(ref)
|
|
228
|
+
if (d.configured) {
|
|
229
|
+
// 凭据库已有值 → 不覆盖(旧文件里的可能是陈值),但仍清掉配置里的明文
|
|
230
|
+
cfg[field] = ''
|
|
231
|
+
notes.push('凭据库中已有 ' + ref + ',配置文件里的旧值直接丢弃(不覆盖凭据库)')
|
|
232
|
+
return
|
|
233
|
+
}
|
|
234
|
+
const persisted = await seam.save(ref, value)
|
|
235
|
+
if (persisted) {
|
|
236
|
+
cfg[field] = ''
|
|
237
|
+
moved.push(label + ' → ' + ref)
|
|
238
|
+
} else {
|
|
239
|
+
// 无法持久化:保留明文,避免「文件干净了、密钥却没了」
|
|
240
|
+
pending = true
|
|
241
|
+
notes.push('凭据库不可写(宿主未提供凭据服务?):' + label + ' 的明文**保留在配置文件**中以免丢失,'
|
|
242
|
+
+ '请修复凭据库后重启以完成迁移')
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
await move('volcengineSecretAccessKey', CRED_REFS.volcengineSecret, 'volcengineSecretAccessKey')
|
|
246
|
+
await move('cloudToken', CRED_REFS.cloudToken, 'cloudToken')
|
|
247
|
+
return { changed: moved.length > 0, moved, notes, pending }
|
|
248
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# 第三方组件与数据来源声明
|
|
2
|
+
|
|
3
|
+
本插件(`@angelyeye/dsh-cost-tracker`)自身以 MIT 许可发布(见仓库根 `LICENSE`)。
|
|
4
|
+
以下列出**随包分发**的第三方成果及其许可,按要求保留原始版权声明。
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 1. 多厂商模型价格目录(数据)
|
|
9
|
+
|
|
10
|
+
- **文件**:`docs/provider-pricing.json`(由 `vendor-catalog.js` 读取,随 npm 包分发)
|
|
11
|
+
- **上游**:`dsh-cost-meter` v1.5.41 · <https://github.com/Han-1413141/dsh-cost-meter>
|
|
12
|
+
- **用途**:本插件在其基础上**改编**为该目录:字段保持(`input` / `cachedInput` /
|
|
13
|
+
`output` / `billingMode` / `sourceUrl` / `checkedAt`),并在运行时按
|
|
14
|
+
`catalogFxRate`(默认 7.2)把 USD/1M tokens 折算为人民币入账;
|
|
15
|
+
provider 别名归一与匹配逻辑为本项目自行实现(`vendor-catalog.js`)。
|
|
16
|
+
- **许可**:MIT
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
MIT License
|
|
20
|
+
|
|
21
|
+
Copyright (c) 2026 dsh-cost-meter contributors
|
|
22
|
+
|
|
23
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
24
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
25
|
+
in the Software without restriction, including without limitation the rights
|
|
26
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
27
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
28
|
+
furnished to do so, subject to the following conditions:
|
|
29
|
+
|
|
30
|
+
The above copyright notice and this permission notice shall be included in all
|
|
31
|
+
copies or substantial portions of the Software.
|
|
32
|
+
|
|
33
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
34
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
35
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
36
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
37
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
38
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
39
|
+
SOFTWARE.
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
> 目录中的单价来自各厂商公开定价页(每条带 `sourceUrl` / `checkedAt` 便于溯源),
|
|
43
|
+
> 仅作**参考折算**用途;实际账单请以各厂商控制台为准。
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 2. 会话日志回放思路(技术参考,无代码拷贝)
|
|
48
|
+
|
|
49
|
+
`import.js` 的 zstd frame 边界扫描(`scanZstdFrames`)**改编自** `dsh-cost-meter`
|
|
50
|
+
的 `lib/backfill.js`(MIT,同上许可),用于逐个追加批次解压宿主会话日志;
|
|
51
|
+
其余解析/去重/覆盖判定逻辑为本项目自行实现,并针对本插件的「实时记录 + 导入记录」
|
|
52
|
+
双源口径做了重新设计(清单快跳、逐调用键去重、覆盖判定)。
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## 3. 其他
|
|
57
|
+
|
|
58
|
+
- 官方价格同步(`price-sync.js`)解析的是 DeepSeek 官方定价页的公开 HTML,
|
|
59
|
+
不包含第三方代码。
|
|
60
|
+
- 运行时**零第三方 npm 依赖**:仅使用 Node 内置模块。
|