@ds-hu/dsh-token-quota 0.0.0-stage → 2.4.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/CHANGELOG.md +123 -0
- package/CONTEXT.md +179 -0
- package/README.md +270 -2
- package/cordis.patch.yml +10 -0
- package/docs/windows-desktop-adaptation.md +80 -0
- package/icon.svg +21 -0
- package/lib/client.js +2371 -0
- package/lib/detect.js +448 -0
- package/lib/index.js +1161 -0
- package/lib/legacy-config.js +129 -0
- package/lib/providers.js +1203 -0
- package/lib/quota-history.js +303 -0
- package/lib/routes.js +13 -0
- package/lib/scan-coordinator.js +227 -0
- package/lib/scheduler.js +86 -0
- package/lib/season.js +271 -0
- package/lib/storage.js +453 -0
- package/lib/usage.js +87 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +73 -4
- package/playwright.config.mjs +6 -0
- package/scripts/benchmark-runtime.mjs +227 -0
- package/scripts/benchmark-storage.mjs +104 -0
- package/scripts/check-package.mjs +46 -0
- package/scripts/verify-desktop.mjs +200 -0
- package/scripts/verify-lifecycle.cjs +75 -0
- package/scripts/verify-web.mjs +170 -0
- package/test/browser/edit-identity.spec.mjs +343 -0
- package/test/browser/interactions.spec.mjs +690 -0
- package/test/browser/server.mjs +123 -0
- package/test/browser/shell.html +33 -0
- package/test/client-lifecycle.mjs +515 -0
- package/test/client.mjs +878 -0
- package/test/copy.mjs +47 -0
- package/test/css.mjs +63 -0
- package/test/desktop-services.mjs +91 -0
- package/test/detect.mjs +164 -0
- package/test/draft-config.mjs +38 -0
- package/test/harness.mjs +136 -0
- package/test/legacy-config.mjs +106 -0
- package/test/lifecycle.mjs +81 -0
- package/test/migration.mjs +127 -0
- package/test/mock-dsh.mjs +399 -0
- package/test/package-meta.mjs +49 -0
- package/test/pagination.mjs +21 -0
- package/test/provider-contracts.mjs +145 -0
- package/test/quota-history.mjs +191 -0
- package/test/reset-times.mjs +68 -0
- package/test/route-consistency.mjs +45 -0
- package/test/runtime.mjs +389 -0
- package/test/scan-coordinator.mjs +353 -0
- package/test/scheduler.mjs +98 -0
- package/test/season.mjs +173 -0
- package/test/smoke.mjs +264 -0
- package/test/storage-recovery.mjs +81 -0
- package/test/storage.mjs +377 -0
- package/test/usage-saves.mjs +107 -0
- package/test/usage.mjs +119 -0
- package/ui-prototype.html +509 -0
package/lib/detect.js
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
// lib/detect.js — DSH 供应商自动探测(宿主半)
|
|
2
|
+
// 读取 DSH LLM 路由注册表(ctx.llm)与提供方设置文档(ctx.settings / ctx.credentials),
|
|
3
|
+
// 把 DSH 内已添加/启用的供应商映射到本插件的供应商,并生成「自动填入」设置补丁。
|
|
4
|
+
//
|
|
5
|
+
// 映射边界(对齐 Token-Consumption-Monitoring docs/query-coverage.md):
|
|
6
|
+
// - 「已添加」以 DSH 设置行(llm-deepseek / llm-pi-ai 的 profile 行 config)为事实来源,
|
|
7
|
+
// ctx.llm 目录/存活路由作为补充(服务未就绪/异常时探测不受影响)
|
|
8
|
+
// - 供应商按「凭据类别 × 地域」拆分;普通 API Key / Management Key / Admin Key
|
|
9
|
+
// 不互相尝试。pi-ai 普通聊天路由(openai / anthropic / openrouter 等)中,
|
|
10
|
+
// openai 与 anthropic 的账务查询需要 Admin Key → 探测时标记 reason,不自动填入;
|
|
11
|
+
// openrouter 普通 Key 可直接用于 Key 额度和今日费用 → 自动填入 openrouter;
|
|
12
|
+
// openrouter-account(Management)/ openai-org / anthropic-org(Admin)仅在有
|
|
13
|
+
// 匹配类别的 DSH 密钥时才可能自动填(当前 DSH 无该类接缝 → 手动配置)。
|
|
14
|
+
// - 地域拆分:moonshotai(api.moonshot.ai → 国际)/ moonshotai-cn(国内)、
|
|
15
|
+
// zai(api.z.ai)/ zai-coding-cn(open.bigmodel.cn)、
|
|
16
|
+
// minimax(www.minimax.io)/ minimax-cn(www.minimaxi.com)。
|
|
17
|
+
// - 同一插件供应商命中多条 DSH 路由时按 ROUTE_PRIORITY 去重(官方路由优先)
|
|
18
|
+
// - 补丁含 enabled / baseUrl / autoSource / autoApiKeyEnv / apiKeyEnv:
|
|
19
|
+
// 有引用名时只写引用(apiKeyEnv),密钥本体留在 DSH 凭据库/环境里;
|
|
20
|
+
// 没有引用名才拷贝密钥本体进插件 settings(role('secret') 自动脱敏);
|
|
21
|
+
// baseUrl 仅在 DSH 路由地址是该供应商官方地址时采用,否则保留插件官方默认值
|
|
22
|
+
// - 只填用户尚未手动设置过的字段(apiKey 或 apiKeyEnv 已设 = 手动接管;enabled 显式 false 由调用方跳过)
|
|
23
|
+
|
|
24
|
+
import { OFFICIAL_HOSTS, isOfficialBaseUrl, PROVIDERS } from "./providers.js";
|
|
25
|
+
|
|
26
|
+
/** 兼容来源(无官方端点白名单)的 Base URL 采纳门槛:仅要求安全的 HTTPS 形态。 */
|
|
27
|
+
function isSafeHttpsBaseUrl(baseUrl) {
|
|
28
|
+
try {
|
|
29
|
+
const url = new URL(String(baseUrl || "").trim());
|
|
30
|
+
const defaultPort = !url.port || (url.protocol === "https:" && url.port === "443");
|
|
31
|
+
return url.protocol === "https:" && defaultPort && !url.username && !url.password && !url.search && !url.hash;
|
|
32
|
+
} catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** DSH LLM 路由 id → 本插件供应商 id(官方/pi-ai 目录路由,精确匹配优先)。 */
|
|
38
|
+
export const DSH_ROUTE_MAP = {
|
|
39
|
+
// deepseek-official = 官方 DeepSeek 适配器;deepseek = llm-pi-ai 同名单路由
|
|
40
|
+
"deepseek-official": "deepseek",
|
|
41
|
+
deepseek: "deepseek",
|
|
42
|
+
// pi-ai 官方目录路由(普通 Key 可直接用于对应账务/额度接口)
|
|
43
|
+
openrouter: "openrouter",
|
|
44
|
+
moonshotai: "moonshot-intl",
|
|
45
|
+
"moonshotai-cn": "moonshot-cn",
|
|
46
|
+
zai: "zai",
|
|
47
|
+
"zai-coding-cn": "zai-cn",
|
|
48
|
+
minimax: "minimax",
|
|
49
|
+
"minimax-cn": "minimax-cn",
|
|
50
|
+
// OpenCode / Command Code 兼容来源(pi-ai declared / 目录)
|
|
51
|
+
opencode: "opencode",
|
|
52
|
+
"opencode-go": "opencode",
|
|
53
|
+
commandcode: "commandcode",
|
|
54
|
+
"commandcode-goat": "commandcode",
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** 前缀兜底:pi-ai declared 路由名带变体(commandcode-goat、opencode-xx 等)仍能命中。 */
|
|
58
|
+
const ROUTE_PREFIX_MAP = [
|
|
59
|
+
["commandcode", "commandcode"],
|
|
60
|
+
["opencode", "opencode"],
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* DSH 官方主机 → 供应商提示(仅用于 route 名不在精确/前缀表时的兜底归类;
|
|
65
|
+
* 不以密钥前缀、不以普通聊天 Key 推断 Admin/Management 账务)。
|
|
66
|
+
*/
|
|
67
|
+
const HOST_HINT = [
|
|
68
|
+
["deepseek.com", "deepseek"],
|
|
69
|
+
["opencode.ai", "opencode"],
|
|
70
|
+
["commandcode.ai", "commandcode"],
|
|
71
|
+
["openrouter.ai", "openrouter"],
|
|
72
|
+
["moonshot.cn", "moonshot-cn"],
|
|
73
|
+
["moonshot.ai", "moonshot-intl"],
|
|
74
|
+
["api.z.ai", "zai"],
|
|
75
|
+
["z.ai", "zai"],
|
|
76
|
+
["bigmodel.cn", "zai-cn"],
|
|
77
|
+
["minimax.io", "minimax"],
|
|
78
|
+
["minimaxi.com", "minimax-cn"],
|
|
79
|
+
// openai / anthropic 普通聊天 Key 无法查询组织账务(需 Admin Key)→ 只提示原因
|
|
80
|
+
["openai.com", null],
|
|
81
|
+
["anthropic.com", null],
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
/** 主机(含子域)→ 供应商/原因。null = 已知主机但需要更高凭据类别。 */
|
|
85
|
+
export function supplierHintForHost(hostname) {
|
|
86
|
+
if (!hostname) return { supplier: null, reason: null };
|
|
87
|
+
const host = String(hostname).toLowerCase().replace(/\.$/, "");
|
|
88
|
+
for (const [suffix, supplier] of HOST_HINT) {
|
|
89
|
+
if (host === suffix || host.endsWith(`.${suffix}`)) {
|
|
90
|
+
const reason = supplier === null
|
|
91
|
+
? "已知官方主机;组织账务查询需要 Admin Key(DSH 普通聊天 Key 不适用)"
|
|
92
|
+
: null;
|
|
93
|
+
return { supplier, reason };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return { supplier: null, reason: null };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** 路由 id → 插件供应商 id(精确表优先,其次前缀;不做主机推断)。 */
|
|
100
|
+
export function supplierForRoute(route) {
|
|
101
|
+
if (typeof route !== "string") return null;
|
|
102
|
+
if (Object.hasOwn(DSH_ROUTE_MAP, route)) return DSH_ROUTE_MAP[route];
|
|
103
|
+
for (const [prefix, supplier] of ROUTE_PREFIX_MAP) {
|
|
104
|
+
if (route.startsWith(prefix)) return supplier;
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 同供应商多路由并存时的优先序(越小越优先;缺省 99)。 */
|
|
110
|
+
const ROUTE_PRIORITY = {
|
|
111
|
+
"deepseek-official": 1,
|
|
112
|
+
deepseek: 2,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** 无 ctx.llm 时按已知适配器家族兜底读取的 settings 行 id。 */
|
|
116
|
+
const FALLBACK_NS = {
|
|
117
|
+
"deepseek-official": "llm-deepseek",
|
|
118
|
+
deepseek: "llm-pi-ai",
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** 按 settingsPath 下钻:[] → 整个 section,['providers','x'] → 逐个取键。 */
|
|
122
|
+
function dig(obj, path) {
|
|
123
|
+
let cur = obj;
|
|
124
|
+
for (const key of path || []) {
|
|
125
|
+
if (cur === null || typeof cur !== "object") return undefined;
|
|
126
|
+
cur = cur[key];
|
|
127
|
+
}
|
|
128
|
+
return cur;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 读取某个 settings 行的生效值(0.1.7:settings.describe() 按 profile 行 id 列出各表单,
|
|
133
|
+
* 每个描述符带脱敏后的 value/base/user;旧的 settings.get(ns) 已不存在)。
|
|
134
|
+
* 宿主内调用不带 redactSecrets,拿得到密钥字段本身;行不存在/描述失败一律 undefined。
|
|
135
|
+
*/
|
|
136
|
+
function settingsValue(settings, ns) {
|
|
137
|
+
if (!ns) return undefined;
|
|
138
|
+
try {
|
|
139
|
+
return settings?.describe?.()?.find((row) => row?.ns === ns)?.value;
|
|
140
|
+
} catch {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 提取 provider profile 的凭据引用与端点(apiKeyEnv/baseURL 为 DSH 适配器统一拼写)。 */
|
|
146
|
+
function profileFields(profile) {
|
|
147
|
+
if (!profile || typeof profile !== "object") return { apiKeyEnv: null, baseURL: null, configured: false };
|
|
148
|
+
const apiKeyEnv =
|
|
149
|
+
typeof profile.apiKeyEnv === "string" && profile.apiKeyEnv.trim() ? profile.apiKeyEnv.trim() : null;
|
|
150
|
+
const baseURL =
|
|
151
|
+
typeof profile.baseURL === "string" && profile.baseURL.trim() ? profile.baseURL.trim() : null;
|
|
152
|
+
return { apiKeyEnv, baseURL, configured: !!(apiKeyEnv || baseURL) };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 解析一个凭据引用:DSH 凭据库优先(credentials.resolve),进程环境变量兜底。
|
|
157
|
+
* 探测与供应商配置走同一条解析路径,所以配置里只写引用名也能在扫描后拿到同一个值。
|
|
158
|
+
* @param ctx Cordis ctx(credentials 为可选服务,用 ctx.get 获取)
|
|
159
|
+
* @param ref 凭据引用名(DSH 凭据库记录名 / 环境变量名)
|
|
160
|
+
* @returns { value, source };未解析到时 value 为空串、source 为 null
|
|
161
|
+
*/
|
|
162
|
+
export async function resolveCredentialRef(ctx, ref) {
|
|
163
|
+
const name = typeof ref === "string" ? ref.trim() : "";
|
|
164
|
+
if (!name) return { value: "", source: null };
|
|
165
|
+
const get = typeof ctx?.get === "function" ? ctx.get.bind(ctx) : (key) => ctx?.[key];
|
|
166
|
+
const credentials = get("credentials");
|
|
167
|
+
if (credentials?.resolve) {
|
|
168
|
+
try {
|
|
169
|
+
const hit = await credentials.resolve(name);
|
|
170
|
+
if (hit && typeof hit === "object" && hit.value) {
|
|
171
|
+
return { value: hit.value, source: hit.source || "credentials" };
|
|
172
|
+
}
|
|
173
|
+
} catch {
|
|
174
|
+
/* 解析失败 → 环境变量兜底 */
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
const envValue = typeof process !== "undefined" ? process.env?.[name] : undefined;
|
|
179
|
+
if (envValue) return { value: envValue, source: "env" };
|
|
180
|
+
} catch {
|
|
181
|
+
/* 忽略 */
|
|
182
|
+
}
|
|
183
|
+
return { value: "", source: null };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 探测 DSH 内已添加/启用的供应商。
|
|
188
|
+
* - 数据源(设置文档优先,llm 目录补充):
|
|
189
|
+
* 1) listConfigurableProviders()/listProviders()(ctx.llm,异常不影响);
|
|
190
|
+
* 2) settings.describe() 里 'llm-deepseek' 整行、'llm-pi-ai' 行的 providers 字典。
|
|
191
|
+
* - 密钥值通过 ctx.credentials.resolve(ref) 解析(失败时退回 process.env 直读);
|
|
192
|
+
* 返回记录含 key(仅供宿主侧自动填入使用,严禁经路由/状态暴露给浏览器)。
|
|
193
|
+
* - include(rec) 可由调用方过滤(如只认 DSH user 层已配置的路由)。
|
|
194
|
+
* - debug=true 时结果数组挂 meta(llmPresent / liveRoutes / directoryEntries / candidates),
|
|
195
|
+
* 供运行诊断;candidates 只含非密钥字段。
|
|
196
|
+
* @param ctx Cordis ctx(llm/credentials 为可选服务,用 ctx.get 获取)
|
|
197
|
+
*/
|
|
198
|
+
export async function detectHarnessSuppliers(ctx, { include = () => true, debug = false } = {}) {
|
|
199
|
+
// get() 返回 undefined 就代表可选服务不存在,不能再回退到严格注入的属性访问。
|
|
200
|
+
// 无 get() 的简单宿主仍可提供直接属性,便于兼容旧嵌入环境。
|
|
201
|
+
const get = typeof ctx?.get === "function" ? ctx.get.bind(ctx) : (name) => ctx?.[name];
|
|
202
|
+
const settings = get("settings");
|
|
203
|
+
if (!settings?.describe) throw new Error("宿主设置服务暂不可用,请稍后重新扫描");
|
|
204
|
+
const llm = get("llm");
|
|
205
|
+
const credentials = get("credentials");
|
|
206
|
+
const meta = {
|
|
207
|
+
llmPresent: !!llm,
|
|
208
|
+
credentialsPresent: !!credentials,
|
|
209
|
+
directoryEntries: 0,
|
|
210
|
+
liveRoutes: [],
|
|
211
|
+
candidates: [],
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const live = new Set();
|
|
215
|
+
try {
|
|
216
|
+
for (const p of llm?.listProviders?.() ?? []) {
|
|
217
|
+
if (p && p.id) live.add(p.id);
|
|
218
|
+
}
|
|
219
|
+
} catch { /* llm 异常 → 按设置文档兜底 */ }
|
|
220
|
+
meta.liveRoutes = [...live];
|
|
221
|
+
|
|
222
|
+
const entries = new Map();
|
|
223
|
+
try {
|
|
224
|
+
for (const entry of llm?.listConfigurableProviders?.() ?? []) {
|
|
225
|
+
if (entry && entry.provider) entries.set(entry.provider, entry);
|
|
226
|
+
}
|
|
227
|
+
} catch { /* llm 异常 → 按设置文档兜底 */ }
|
|
228
|
+
|
|
229
|
+
// 设置文档兜底:无论 ctx.llm 是否可用/就绪,配置节里已添加的供应商都纳入候选
|
|
230
|
+
try {
|
|
231
|
+
const ds = settingsValue(settings, "llm-deepseek");
|
|
232
|
+
if (profileFields(ds).configured) {
|
|
233
|
+
entries.set("deepseek-official", {
|
|
234
|
+
provider: "deepseek-official",
|
|
235
|
+
displayName: "DeepSeek",
|
|
236
|
+
settingsNs: "llm-deepseek",
|
|
237
|
+
settingsPath: [],
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
} catch { /* 忽略 */ }
|
|
241
|
+
try {
|
|
242
|
+
const pi = settingsValue(settings, "llm-pi-ai");
|
|
243
|
+
for (const [provider, profile] of Object.entries(pi?.providers || {})) {
|
|
244
|
+
if (profile && typeof profile === "object") {
|
|
245
|
+
entries.set(provider, {
|
|
246
|
+
provider,
|
|
247
|
+
displayName: profile.displayName || provider,
|
|
248
|
+
settingsNs: "llm-pi-ai",
|
|
249
|
+
settingsPath: ["providers", provider],
|
|
250
|
+
declared: true,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
} catch { /* 忽略 */ }
|
|
255
|
+
|
|
256
|
+
meta.directoryEntries = entries.size;
|
|
257
|
+
const routes = [...entries.keys()].sort(
|
|
258
|
+
(a, b) => (ROUTE_PRIORITY[a] ?? 99) - (ROUTE_PRIORITY[b] ?? 99) || a.localeCompare(b),
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
const raw = [];
|
|
262
|
+
for (const route of routes) {
|
|
263
|
+
const entry = entries.get(route) || {};
|
|
264
|
+
const ns = entry.settingsNs || FALLBACK_NS[route];
|
|
265
|
+
const path = entry.settingsPath || [];
|
|
266
|
+
// 0.1.7:适配器声明的 settingsNs 就是 profile 行 id,describe() 里按 id 找那一行。
|
|
267
|
+
// 无路径信息时退回该家族已知的兜底行(llm-deepseek / llm-pi-ai)。
|
|
268
|
+
const value = settingsValue(settings, ns) ?? settingsValue(settings, FALLBACK_NS[route]);
|
|
269
|
+
const profile = dig(value, path);
|
|
270
|
+
|
|
271
|
+
const { apiKeyEnv, baseURL, configured } = profileFields(profile);
|
|
272
|
+
const active = live.has(route);
|
|
273
|
+
if (!active && !configured) continue; // 未添加也未启用 → 不算「已添加」
|
|
274
|
+
|
|
275
|
+
// 供应商归属:精确/前缀表 → 已知主机兜底归类(null 时保留 reason)
|
|
276
|
+
let supplier = supplierForRoute(route);
|
|
277
|
+
let hostHint = { supplier: null, reason: null };
|
|
278
|
+
const accountRoute = route === "deepseek-account";
|
|
279
|
+
if (!supplier && baseURL && !accountRoute) {
|
|
280
|
+
try {
|
|
281
|
+
hostHint = supplierHintForHost(new URL(baseURL).hostname);
|
|
282
|
+
} catch {
|
|
283
|
+
/* 忽略非法 URL */
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (!supplier) supplier = hostHint.supplier;
|
|
287
|
+
const reason = accountRoute
|
|
288
|
+
? "桌面登录凭证不能用于 API Key 余额接口;可在 DeepSeek 配置页填写同一账户的 API Key 查询余额,无需重复添加账户"
|
|
289
|
+
: supplier ? null : hostHint.reason;
|
|
290
|
+
|
|
291
|
+
// 凭据类别守门:只有凭据类别为 api-key 的供应商才可从普通聊天路由自动接入;
|
|
292
|
+
// admin-key / management-key 供应商绝不因普通 Key 命中而自动填入。
|
|
293
|
+
let classMismatch = false;
|
|
294
|
+
if (supplier) {
|
|
295
|
+
const cls = PROVIDERS[supplier]?.credentialClass;
|
|
296
|
+
if (cls && cls !== "api-key") {
|
|
297
|
+
classMismatch = true;
|
|
298
|
+
supplier = null;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const rec = {
|
|
303
|
+
route,
|
|
304
|
+
supplier,
|
|
305
|
+
displayName: entry.displayName || route,
|
|
306
|
+
ns: ns || null,
|
|
307
|
+
path: [...(entry.settingsPath || [])],
|
|
308
|
+
active,
|
|
309
|
+
configured,
|
|
310
|
+
declared: entry.declared || null,
|
|
311
|
+
apiKeyEnv,
|
|
312
|
+
keyPresent: false,
|
|
313
|
+
keySource: null, // 'credentials' | 'file' | 'env' | null
|
|
314
|
+
key: null,
|
|
315
|
+
baseURL,
|
|
316
|
+
reason: classMismatch
|
|
317
|
+
? "该供应商需要更高凭据类别(Admin/Management Key),DSH 普通聊天 Key 不适用"
|
|
318
|
+
: reason,
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
if (apiKeyEnv) {
|
|
322
|
+
const { value, source } = await resolveCredentialRef(ctx, apiKeyEnv);
|
|
323
|
+
rec.keyPresent = !!value;
|
|
324
|
+
rec.key = value || null;
|
|
325
|
+
rec.keySource = source;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (debug) {
|
|
329
|
+
meta.candidates.push({
|
|
330
|
+
route: rec.route,
|
|
331
|
+
supplier: rec.supplier,
|
|
332
|
+
displayName: rec.displayName,
|
|
333
|
+
active: rec.active,
|
|
334
|
+
configured: rec.configured,
|
|
335
|
+
ns: rec.ns,
|
|
336
|
+
apiKeyEnv: rec.apiKeyEnv,
|
|
337
|
+
baseURL: rec.baseURL,
|
|
338
|
+
keyPresent: rec.keyPresent,
|
|
339
|
+
keySource: rec.keySource,
|
|
340
|
+
reason: rec.reason || null,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
if (!include(rec)) continue;
|
|
344
|
+
raw.push(rec);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 同供应商多路由去重:官方路由优先(routes 已按 ROUTE_PRIORITY 排序)
|
|
348
|
+
const picked = new Map();
|
|
349
|
+
const rest = [];
|
|
350
|
+
for (const rec of raw) {
|
|
351
|
+
if (rec.supplier && picked.has(rec.supplier)) continue;
|
|
352
|
+
if (rec.supplier) picked.set(rec.supplier, rec);
|
|
353
|
+
else rest.push(rec); // 未映射路由全部保留(供 UI 提示)
|
|
354
|
+
}
|
|
355
|
+
const result = [...picked.values(), ...rest];
|
|
356
|
+
result.routeSuppliers = Object.fromEntries(raw.filter((r) => r.supplier).map((r) => [r.route, r.supplier]));
|
|
357
|
+
if (debug) result.meta = meta;
|
|
358
|
+
return result;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* 由探测结果生成设置补丁(不复制密钥)。
|
|
363
|
+
* @param detected detectHarnessSuppliers(ctx) 的结果
|
|
364
|
+
* @param getCurrent (supplierId) => 当前插件设置(enabled/apiKey/baseUrl…)
|
|
365
|
+
* @param baseUrlDefaults { supplierId: 插件默认 baseUrl }
|
|
366
|
+
* @param opts.skipSupplier (supplierId) => boolean 用户显式关闭等情形跳过自动填入
|
|
367
|
+
* @returns { patch, applied, unfilled }
|
|
368
|
+
* patch.suppliers[id] 含 enabled/baseUrl/autoSource/autoApiKeyEnv/apiKeyEnv(apiKeyEnv 为引用名;
|
|
369
|
+
* apiKey 只在 DSH 路由没有可引用名字时才出现);
|
|
370
|
+
* applied: [{ supplier, fields }];
|
|
371
|
+
* unfilled: [{ supplier 或 route, reason: 'no-key' | 'unsupported' | string }]
|
|
372
|
+
*/
|
|
373
|
+
export function autoFillPatch(detected, getCurrent, baseUrlDefaults, { skipSupplier = () => false } = {}) {
|
|
374
|
+
const patch = { suppliers: {} };
|
|
375
|
+
const applied = [];
|
|
376
|
+
const unfilled = [];
|
|
377
|
+
for (const d of detected) {
|
|
378
|
+
if (!d.supplier) {
|
|
379
|
+
unfilled.push({
|
|
380
|
+
route: d.route,
|
|
381
|
+
displayName: d.displayName,
|
|
382
|
+
reason: "unsupported",
|
|
383
|
+
detail: d.reason || null,
|
|
384
|
+
});
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (skipSupplier(d.supplier)) continue;
|
|
388
|
+
// 凭据类别守门:仅 api-key 类别(含私有兼容)参与自动填入
|
|
389
|
+
if (PROVIDERS[d.supplier]?.credentialClass && PROVIDERS[d.supplier].credentialClass !== "api-key") {
|
|
390
|
+
unfilled.push({ supplier: d.supplier, route: d.route, reason: "class-mismatch", detail: "需要更高凭据类别,请手动配置" });
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
const cur = getCurrent(d.supplier) || {};
|
|
394
|
+
if (cur.apiKey) continue; // 已手动设置密钥 → 手动接管,不动
|
|
395
|
+
const fields = [];
|
|
396
|
+
const p = {};
|
|
397
|
+
const defaultBase = baseUrlDefaults[d.supplier];
|
|
398
|
+
const currentBase = String(cur.baseUrl || "");
|
|
399
|
+
// baseUrl 采纳:官方供应商要求 DSH 路由地址即其官方地址(路径白名单);
|
|
400
|
+
// 私有兼容来源(opencode/commandcode)只要求安全 HTTPS 形态(可带网关路径)
|
|
401
|
+
const adoptableBase = OFFICIAL_HOSTS[d.supplier]
|
|
402
|
+
? isOfficialBaseUrl(d.supplier, d.baseURL)
|
|
403
|
+
: isSafeHttpsBaseUrl(d.baseURL);
|
|
404
|
+
if ((!currentBase || currentBase === defaultBase) && d.baseURL && d.baseURL !== currentBase
|
|
405
|
+
&& adoptableBase) {
|
|
406
|
+
p.baseUrl = d.baseURL;
|
|
407
|
+
fields.push("Base URL");
|
|
408
|
+
}
|
|
409
|
+
if (d.ns && (cur.autoSource || "") !== d.ns) {
|
|
410
|
+
p.autoSource = d.ns;
|
|
411
|
+
fields.push("来源");
|
|
412
|
+
}
|
|
413
|
+
if ((cur.autoApiKeyEnv || "") !== (d.apiKeyEnv || "")) {
|
|
414
|
+
p.autoApiKeyEnv = d.apiKeyEnv || "";
|
|
415
|
+
fields.push("密钥引用");
|
|
416
|
+
}
|
|
417
|
+
const keyOk = d.keyPresent && !!d.key;
|
|
418
|
+
// 手动接管:密钥本体与凭据引用都算——用户手填的引用不能被 DSH 路由名顶掉
|
|
419
|
+
const manualCredential = !!cur.apiKey || !!String(cur.apiKeyEnv || "").trim();
|
|
420
|
+
if (keyOk && !manualCredential) {
|
|
421
|
+
// 凭据引用优先:只写引用名,密钥本体留在 DSH 凭据库/环境里,不落进本插件配置;
|
|
422
|
+
// 密钥本身是从该引用解析出来的,运行期按同一个名字再解析一次即可。
|
|
423
|
+
if (d.apiKeyEnv) {
|
|
424
|
+
// 走到这里 cur.apiKeyEnv 必为空(非空即 manualCredential)→ 直接写引用
|
|
425
|
+
p.apiKeyEnv = d.apiKeyEnv;
|
|
426
|
+
fields.push("凭据引用");
|
|
427
|
+
} else {
|
|
428
|
+
// 没有可引用的名字(适配器用默认环境变量而配置行里没写 apiKeyEnv)才拷贝本体
|
|
429
|
+
p.apiKey = d.key;
|
|
430
|
+
fields.push("API Key");
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (keyOk && !cur.enabled) {
|
|
434
|
+
p.enabled = true;
|
|
435
|
+
fields.push("启用");
|
|
436
|
+
} else if (!keyOk) {
|
|
437
|
+
unfilled.push({ supplier: d.supplier, route: d.route, reason: "no-key" });
|
|
438
|
+
}
|
|
439
|
+
if (fields.length) {
|
|
440
|
+
patch.suppliers[d.supplier] = p;
|
|
441
|
+
applied.push({ supplier: d.supplier, fields });
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return { patch, applied, unfilled };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// 供外部诊断使用(保留原语义导出)
|
|
448
|
+
export const OFFICIAL_HOSTS_FOR_DETECT = OFFICIAL_HOSTS;
|