@kangyishuai/dsh-advisor 0.1.0 → 0.3.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/README.md +65 -37
- package/README.zh-CN.md +49 -27
- package/lib/adoption.js +12 -6
- package/lib/budget.js +100 -0
- package/lib/call.js +8 -3
- package/lib/catalog.js +143 -0
- package/lib/client.js +1790 -343
- package/lib/ledger.js +66 -1
- package/lib/prompt.js +21 -0
- package/lib/route.js +5 -2
- package/lib/service.js +354 -14
- package/lib/settings.js +79 -10
- package/lib/trace.js +31 -0
- package/lib/twopass.js +60 -0
- package/lib/verifier.js +107 -0
- package/locale/en.json +122 -26
- package/locale/zh.json +128 -32
- package/package.json +1 -1
package/lib/catalog.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 内置模型目录:**兜底用的能力档位与价格表**。
|
|
3
|
+
//
|
|
4
|
+
// DSH 的 `LlmModelInfo` 没有 rank、没有价格(见 docs/runtime §9.15),所以配对与金额只能靠元数据。
|
|
5
|
+
// 这里给一张很小的、刻意保守的表:
|
|
6
|
+
// - 只覆盖确定了解的型号;**认不出就返回 undefined**,绝不猜(猜错比不知道更糟);
|
|
7
|
+
// - 用户可以在设置里用 `tiers` / 单价覆盖这里的值(覆盖优先);
|
|
8
|
+
// - 表里的价格是「量级参考」,不是报价;设置页会标「估算」。
|
|
9
|
+
//
|
|
10
|
+
// 纯数据 + 纯函数,不 import 宿主包。
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 能力档位(越大越强)。同一族按代际与规格排序;跨族只保证「明显更强/更弱」的关系。
|
|
14
|
+
* 键是模型 id 的小写匹配片段,按**从长到短**匹配,避免 `deepseek-v4` 抢走 `deepseek-v4.1-flash`。
|
|
15
|
+
* @type {ReadonlyArray<{ match: string, rank: number }>}
|
|
16
|
+
*/
|
|
17
|
+
export const MODEL_RANKS = Object.freeze([
|
|
18
|
+
{ match: "deepseek-v4.1-flash", rank: 3 },
|
|
19
|
+
{ match: "deepseek-v4.1", rank: 5 },
|
|
20
|
+
{ match: "deepseek-v4-pro", rank: 6 },
|
|
21
|
+
{ match: "deepseek-v3.2", rank: 3 },
|
|
22
|
+
{ match: "deepseek-reasoner", rank: 5 },
|
|
23
|
+
{ match: "deepseek-chat", rank: 3 },
|
|
24
|
+
{ match: "claude-opus-4", rank: 7 },
|
|
25
|
+
{ match: "claude-sonnet-4", rank: 5 },
|
|
26
|
+
{ match: "claude-haiku-4", rank: 2 },
|
|
27
|
+
{ match: "gpt-5", rank: 6 },
|
|
28
|
+
{ match: "gpt-4.1", rank: 4 },
|
|
29
|
+
{ match: "o3", rank: 6 },
|
|
30
|
+
{ match: "o4-mini", rank: 4 },
|
|
31
|
+
{ match: "kimi-k3", rank: 5 },
|
|
32
|
+
{ match: "qwen3.8-max", rank: 6 },
|
|
33
|
+
{ match: "qwen3.8-flash", rank: 3 },
|
|
34
|
+
{ match: "glm-5.3", rank: 4 },
|
|
35
|
+
{ match: "glm-5.3-flash", rank: 3 },
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 价格(USD / 百万 token),只用于「估算」。缺失即未知。
|
|
40
|
+
* @type {ReadonlyArray<{ match: string, input: number, output: number }>}
|
|
41
|
+
*/
|
|
42
|
+
export const MODEL_PRICES = Object.freeze([
|
|
43
|
+
{ match: "deepseek-v4.1-flash", input: 0.1, output: 0.3 },
|
|
44
|
+
{ match: "deepseek-v4-pro", input: 0.6, output: 2.4 },
|
|
45
|
+
{ match: "deepseek-v4.1", input: 0.3, output: 1.2 },
|
|
46
|
+
{ match: "deepseek-reasoner", input: 0.6, output: 2.4 },
|
|
47
|
+
{ match: "deepseek-chat", input: 0.3, output: 1.2 },
|
|
48
|
+
{ match: "claude-haiku-4", input: 1, output: 5 },
|
|
49
|
+
{ match: "claude-sonnet-4", input: 3, output: 15 },
|
|
50
|
+
{ match: "claude-opus-4", input: 15, output: 75 },
|
|
51
|
+
{ match: "kimi-k3", input: 0.6, output: 2.4 },
|
|
52
|
+
{ match: "qwen3.8-flash", input: 0.1, output: 0.3 },
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 在表里按「最长匹配优先」找一行。
|
|
57
|
+
* @template T
|
|
58
|
+
* @param {readonly T[]} table
|
|
59
|
+
* @param {string} id
|
|
60
|
+
* @param {(row: T) => string} key
|
|
61
|
+
* @returns {T|undefined}
|
|
62
|
+
*/
|
|
63
|
+
function lookup(table, id, key) {
|
|
64
|
+
const model = String(id ?? "").toLowerCase();
|
|
65
|
+
if (model === "") return undefined;
|
|
66
|
+
/** @type {T|undefined} */
|
|
67
|
+
let best;
|
|
68
|
+
let bestLength = 0;
|
|
69
|
+
for (const row of table) {
|
|
70
|
+
const needle = key(row);
|
|
71
|
+
if (!model.includes(needle)) continue;
|
|
72
|
+
if (needle.length > bestLength) {
|
|
73
|
+
best = row;
|
|
74
|
+
bestLength = needle.length;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return best;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 内置档位;认不出返回 undefined。
|
|
82
|
+
* @param {string} modelId
|
|
83
|
+
* @returns {number|undefined}
|
|
84
|
+
*/
|
|
85
|
+
export function builtinRank(modelId) {
|
|
86
|
+
const row = lookup(MODEL_RANKS, modelId, (item) => item.match);
|
|
87
|
+
return row === undefined ? undefined : row.rank;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 内置价格;认不出返回 undefined。
|
|
92
|
+
* @param {string} modelId
|
|
93
|
+
* @returns {{ input: number, output: number }|undefined}
|
|
94
|
+
*/
|
|
95
|
+
export function builtinPrice(modelId) {
|
|
96
|
+
const row = lookup(MODEL_PRICES, modelId, (item) => item.match);
|
|
97
|
+
return row === undefined ? undefined : { input: row.input, output: row.output };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 有效档位:用户声明 > 内置;都没有返回 undefined(= 未校验)。
|
|
102
|
+
* @param {Record<string, number>} tiers
|
|
103
|
+
* @param {{ provider: string, model: string }} route
|
|
104
|
+
* @returns {number|undefined}
|
|
105
|
+
*/
|
|
106
|
+
export function effectiveRank(tiers, route) {
|
|
107
|
+
const key = `${route.provider}/${route.model}`;
|
|
108
|
+
const declared = tiers !== null && typeof tiers === "object" ? tiers[key] : undefined;
|
|
109
|
+
if (typeof declared === "number" && Number.isFinite(declared)) return declared;
|
|
110
|
+
return builtinRank(route.model);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 有效单价:用户声明 > 内置;都没有返回 `{ input: 0, output: 0 }`(= 不算钱)。
|
|
115
|
+
* @param {{ priceInputPerMTok: number, priceOutputPerMTok: number }} settings
|
|
116
|
+
* @param {string} modelId
|
|
117
|
+
* @returns {{ input: number, output: number, source: "settings"|"builtin"|"none" }}
|
|
118
|
+
*/
|
|
119
|
+
export function effectivePrice(settings, modelId) {
|
|
120
|
+
const input = Number(settings?.priceInputPerMTok ?? 0) || 0;
|
|
121
|
+
const output = Number(settings?.priceOutputPerMTok ?? 0) || 0;
|
|
122
|
+
if (input > 0 || output > 0) return { input, output, source: "settings" };
|
|
123
|
+
const builtin = builtinPrice(modelId);
|
|
124
|
+
if (builtin !== undefined) return { ...builtin, source: "builtin" };
|
|
125
|
+
return { input: 0, output: 0, source: "none" };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 用户白名单:`allowedModels` 为空表示不限制;否则只允许列出的 `provider/model` 或裸 model id。
|
|
130
|
+
* @param {string[]} allowedModels
|
|
131
|
+
* @param {{ provider: string, model: string }} route
|
|
132
|
+
* @returns {{ allowed: boolean, reason: string }}
|
|
133
|
+
*/
|
|
134
|
+
export function checkAllowlist(allowedModels, route) {
|
|
135
|
+
const list = Array.isArray(allowedModels)
|
|
136
|
+
? allowedModels.filter((item) => typeof item === "string" && item !== "")
|
|
137
|
+
: [];
|
|
138
|
+
if (list.length === 0) return { allowed: true, reason: "未设置白名单" };
|
|
139
|
+
const full = `${route.provider}/${route.model}`;
|
|
140
|
+
if (list.includes(full) || list.includes(route.model))
|
|
141
|
+
return { allowed: true, reason: "在白名单内" };
|
|
142
|
+
return { allowed: false, reason: `不在 allowedModels 白名单内(${list.length} 条)` };
|
|
143
|
+
}
|