@njuptlzf/agent-chat-server 0.1.1 → 0.2.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/dist/audit.d.ts +54 -0
- package/dist/audit.js +133 -0
- package/dist/authz.d.ts +134 -0
- package/dist/authz.js +238 -0
- package/dist/events.d.ts +33 -0
- package/dist/events.js +169 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +4 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +126 -0
- package/dist/manifest.d.ts +147 -0
- package/dist/manifest.js +491 -0
- package/dist/mcp-config.d.ts +87 -0
- package/dist/mcp-config.js +353 -0
- package/dist/model-config.d.ts +123 -0
- package/dist/model-config.js +371 -0
- package/dist/model-failover.d.ts +42 -0
- package/dist/model-failover.js +82 -0
- package/dist/models-api.d.ts +98 -0
- package/dist/models-api.js +326 -0
- package/dist/persona-config.d.ts +112 -0
- package/dist/persona-config.js +368 -0
- package/dist/persona-trial.d.ts +50 -0
- package/dist/persona-trial.js +77 -0
- package/dist/product-routes.d.ts +35 -0
- package/dist/product-routes.js +92 -0
- package/dist/server.d.ts +162 -0
- package/dist/server.js +2572 -0
- package/dist/session.d.ts +218 -0
- package/dist/session.js +545 -0
- package/dist/sqlite-stores.d.ts +173 -0
- package/dist/sqlite-stores.js +921 -0
- package/dist/stores.d.ts +506 -0
- package/dist/stores.js +614 -0
- package/dist/testing.d.ts +42 -0
- package/dist/testing.js +47 -0
- package/dist/trial-grant.d.ts +40 -0
- package/dist/trial-grant.js +24 -0
- package/dist/usage-report.d.ts +23 -0
- package/dist/usage-report.js +68 -0
- package/dist/util.d.ts +3 -0
- package/dist/util.js +5 -0
- package/package.json +64 -58
- package/scripts/agentchat-integrate.mjs +607 -601
- package/src/audit.ts +169 -169
- package/src/authz.ts +307 -307
- package/src/events.ts +206 -206
- package/src/index.ts +26 -26
- package/src/main.ts +142 -142
- package/src/manifest.ts +562 -562
- package/src/mcp-config.ts +385 -385
- package/src/model-config.ts +424 -424
- package/src/model-failover.ts +95 -95
- package/src/models-api.ts +360 -360
- package/src/persona-config.ts +436 -436
- package/src/persona-trial.ts +121 -121
- package/src/product-routes.ts +102 -102
- package/src/server.ts +2647 -2647
- package/src/session.ts +688 -688
- package/src/sqlite-stores.ts +1040 -1040
- package/src/stores.ts +899 -899
- package/src/testing.ts +80 -80
- package/src/trial-grant.ts +62 -62
- package/src/usage-report.ts +86 -86
- package/src/util.ts +6 -6
package/src/models-api.ts
CHANGED
|
@@ -1,360 +1,360 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import type { ModelCandidateRef, ModelProviderView, ModelsResponse, ModelTestResponse } from "@njuptlzf/agent-chat-protocol";
|
|
4
|
-
import type { ModelConfig, ModelProviderEntry } from "./model-config.ts";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 模型配置面的纯逻辑。
|
|
8
|
-
*
|
|
9
|
-
* 这里只有「校验 + 变形 + 两个出网探测」,没有 HTTP 也没有落盘——路由与审计写点
|
|
10
|
-
* 留在 server.ts(与 handleAdminAudit 同款分层),落盘走 model-config.ts 的
|
|
11
|
-
* saveModelConfig(原子替换 + 作废缓存)。
|
|
12
|
-
*
|
|
13
|
-
* 校验口径必须与 的 parseModelConfig 同源:**页面放行、运行时丢掉**是最坏的组合
|
|
14
|
-
* (管理员以为配好了,实际每次 run 都在回落 manifest)。
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
export interface ApiError {
|
|
18
|
-
code: "bad_request" | "not_found" | "provider_in_use" | "model_in_use" | "model_not_in_whitelist" | "provider_unreachable";
|
|
19
|
-
message: string;
|
|
20
|
-
status: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const STATUS: Record<ApiError["code"], number> = {
|
|
24
|
-
bad_request: 400,
|
|
25
|
-
not_found: 404,
|
|
26
|
-
provider_in_use: 409,
|
|
27
|
-
model_in_use: 400,
|
|
28
|
-
model_not_in_whitelist: 400,
|
|
29
|
-
provider_unreachable: 502,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export function apiError(code: ApiError["code"], message: string): ApiError {
|
|
33
|
-
return { code, message, status: STATUS[code] };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** `env:NAME` → 实际密钥。没配/空串都算没配(keyConfigured=false)。 */
|
|
37
|
-
export function keyOf(entry: ModelProviderEntry): string | undefined {
|
|
38
|
-
const ref = entry.keyRef;
|
|
39
|
-
if (!ref || !ref.startsWith("env:")) return undefined;
|
|
40
|
-
const value = process.env[ref.slice(4)];
|
|
41
|
-
return value && value.trim() !== "" ? value : undefined;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** `env:SENSENOVA_API_KEY` → `SENSENOVA_API_KEY`(展示用,不是密钥)。 */
|
|
45
|
-
export function keyEnvOf(entry: ModelProviderEntry): string | undefined {
|
|
46
|
-
const ref = entry.keyRef;
|
|
47
|
-
return ref && ref.startsWith("env:") ? ref.slice(4) : undefined;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** 档位的展示元信息(label/multiplier)来自 manifest,配置只重绑不新增档位。 */
|
|
51
|
-
export interface TierMeta {
|
|
52
|
-
label?: string;
|
|
53
|
-
multiplier?: number;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function toModelsResponse(cfg: ModelConfig, configPath: string, tierMeta: Record<string, TierMeta> = {}): ModelsResponse {
|
|
57
|
-
const providers: ModelProviderView[] = Object.entries(cfg.providers).map(([id, p]) => {
|
|
58
|
-
const keyEnv = keyEnvOf(p);
|
|
59
|
-
return {
|
|
60
|
-
id,
|
|
61
|
-
enabled: p.enabled,
|
|
62
|
-
...(p.api ? { api: p.api } : {}),
|
|
63
|
-
...(p.baseUrl ? { baseUrl: p.baseUrl } : {}),
|
|
64
|
-
keyConfigured: keyOf(p) !== undefined,
|
|
65
|
-
...(keyEnv ? { keyEnv } : {}),
|
|
66
|
-
models: [...p.models],
|
|
67
|
-
catalog: [...p.catalog],
|
|
68
|
-
...(p.fetchedAt ? { fetchedAt: p.fetchedAt } : {}),
|
|
69
|
-
};
|
|
70
|
-
});
|
|
71
|
-
const tiers: ModelsResponse["tiers"] = {};
|
|
72
|
-
for (const [id, t] of Object.entries(cfg.tiers)) {
|
|
73
|
-
const meta = tierMeta[id];
|
|
74
|
-
tiers[id] = {
|
|
75
|
-
...(meta?.label ? { label: meta.label } : {}),
|
|
76
|
-
...(meta?.multiplier !== undefined ? { multiplier: meta.multiplier } : {}),
|
|
77
|
-
candidates: t.candidates.map((c) => ({ ...c })),
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
source: cfg.source,
|
|
82
|
-
maxCandidates: cfg.maxCandidates,
|
|
83
|
-
providers,
|
|
84
|
-
tiers,
|
|
85
|
-
routing: { ...cfg.routing },
|
|
86
|
-
problems: [...cfg.problems],
|
|
87
|
-
configPath,
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
92
|
-
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* `PUT /api/admin/models/providers/:id`:只允许改白名单字段。
|
|
99
|
-
* 不做 upsert——新增 provider 必须在 `models.json` 里也有端点,浏览器填 key 是 的延后项。
|
|
100
|
-
*/
|
|
101
|
-
export function patchProvider(cfg: ModelConfig, id: string, body: unknown): { cfg: ModelConfig } | { error: ApiError } {
|
|
102
|
-
const current = cfg.providers[id];
|
|
103
|
-
if (!current) return { error: apiError("not_found", `unknown provider "${id}"`) };
|
|
104
|
-
if (!isRecord(body)) return { error: apiError("bad_request", "body must be a JSON object") };
|
|
105
|
-
|
|
106
|
-
const next: ModelProviderEntry = { ...current, models: [...current.models], catalog: [...current.catalog] };
|
|
107
|
-
|
|
108
|
-
if (body.enabled !== undefined) {
|
|
109
|
-
if (typeof body.enabled !== "boolean") return { error: apiError("bad_request", "`enabled` must be a boolean") };
|
|
110
|
-
next.enabled = body.enabled;
|
|
111
|
-
}
|
|
112
|
-
if (body.api !== undefined) {
|
|
113
|
-
if (typeof body.api !== "string" || body.api.trim() === "") return { error: apiError("bad_request", "`api` must be a non-empty string") };
|
|
114
|
-
next.api = body.api.trim();
|
|
115
|
-
}
|
|
116
|
-
if (body.baseUrl !== undefined) {
|
|
117
|
-
if (typeof body.baseUrl !== "string") return { error: apiError("bad_request", "`baseUrl` must be a string") };
|
|
118
|
-
let url: URL;
|
|
119
|
-
try {
|
|
120
|
-
url = new URL(body.baseUrl.trim());
|
|
121
|
-
} catch {
|
|
122
|
-
return { error: apiError("bad_request", "`baseUrl` must be an absolute URL") };
|
|
123
|
-
}
|
|
124
|
-
if (url.protocol !== "http:" && url.protocol !== "https:") return { error: apiError("bad_request", "`baseUrl` must be http(s)") };
|
|
125
|
-
next.baseUrl = body.baseUrl.trim().replace(/\/+$/, "");
|
|
126
|
-
}
|
|
127
|
-
if (body.keyEnv !== undefined) {
|
|
128
|
-
// 只接受环境变量名:密钥永远不经过浏览器。
|
|
129
|
-
if (body.keyEnv === null || body.keyEnv === "") delete next.keyRef;
|
|
130
|
-
else if (typeof body.keyEnv === "string" && ENV_NAME.test(body.keyEnv.trim())) next.keyRef = `env:${body.keyEnv.trim()}`;
|
|
131
|
-
else return { error: apiError("bad_request", "`keyEnv` must be an env var name like SENSENOVA_API_KEY") };
|
|
132
|
-
}
|
|
133
|
-
if (body.models !== undefined) {
|
|
134
|
-
if (!Array.isArray(body.models) || body.models.some((m) => typeof m !== "string" || m.trim() === "")) {
|
|
135
|
-
return { error: apiError("bad_request", "`models` must be a list of non-empty strings") };
|
|
136
|
-
}
|
|
137
|
-
const whitelist = [...new Set((body.models as string[]).map((m) => m.trim()))];
|
|
138
|
-
// 撤白名单前先看有没有档位还引用它——否则运行时会静默丢掉这个候选(的反面)。
|
|
139
|
-
for (const [tierId, tier] of Object.entries(cfg.tiers)) {
|
|
140
|
-
for (const c of tier.candidates) {
|
|
141
|
-
if (c.provider === id && !whitelist.includes(c.model)) {
|
|
142
|
-
return { error: apiError("model_in_use", `model "${c.model}" is still bound to tier "${tierId}"; unbind it first`) };
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
next.models = whitelist;
|
|
147
|
-
// 手工写进白名单的模型也要在目录里可见,否则页面永远显示「不在目录」。
|
|
148
|
-
for (const m of whitelist) if (!next.catalog.includes(m)) next.catalog.push(m);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return { cfg: { ...cfg, providers: { ...cfg.providers, [id]: next } } };
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** `DELETE /api/admin/models/providers/:id`:被档位引用 → 409。 */
|
|
155
|
-
export function deleteProvider(cfg: ModelConfig, id: string): { cfg: ModelConfig } | { error: ApiError } {
|
|
156
|
-
if (!cfg.providers[id]) return { error: apiError("not_found", `unknown provider "${id}"`) };
|
|
157
|
-
const usedBy = Object.entries(cfg.tiers).filter(([, t]) => t.candidates.some((c) => c.provider === id)).map(([tierId]) => tierId);
|
|
158
|
-
if (usedBy.length > 0) return { error: apiError("provider_in_use", `provider "${id}" is still bound to tier(s) ${usedBy.join(", ")}`) };
|
|
159
|
-
const providers = { ...cfg.providers };
|
|
160
|
-
delete providers[id];
|
|
161
|
-
return { cfg: { ...cfg, providers } };
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* `PUT /api/admin/models/tiers`:整档替换(只动请求里出现的档位)。
|
|
166
|
-
* 档位集合来自 manifest——配置只能重绑,不能新增档位(否则 `/api/chat` 仍然 400,
|
|
167
|
-
* 页面却显示配好了)。
|
|
168
|
-
*/
|
|
169
|
-
export function patchTiers(cfg: ModelConfig, body: unknown, allowedTierIds: readonly string[]): { cfg: ModelConfig } | { error: ApiError } {
|
|
170
|
-
if (!isRecord(body) || !isRecord(body.tiers)) {
|
|
171
|
-
return { error: apiError("bad_request", "expected {tiers:{<id>:{candidates:[{provider,model}]}}}") };
|
|
172
|
-
}
|
|
173
|
-
const tiers = { ...cfg.tiers };
|
|
174
|
-
for (const [tierId, raw] of Object.entries(body.tiers)) {
|
|
175
|
-
if (!allowedTierIds.includes(tierId)) {
|
|
176
|
-
return { error: apiError("bad_request", `unknown tier "${tierId}"(档位集合来自 manifest,配置只能重绑不能新增)`) };
|
|
177
|
-
}
|
|
178
|
-
if (!isRecord(raw) || !Array.isArray(raw.candidates)) {
|
|
179
|
-
return { error: apiError("bad_request", `tiers.${tierId}.candidates must be a list`) };
|
|
180
|
-
}
|
|
181
|
-
const list = raw.candidates;
|
|
182
|
-
if (list.length === 0) return { error: apiError("bad_request", `tiers.${tierId}: at least one candidate is required`) };
|
|
183
|
-
if (list.length > cfg.maxCandidates) {
|
|
184
|
-
return { error: apiError("bad_request", `tiers.${tierId}: ${list.length} candidates exceed maxCandidates ${cfg.maxCandidates}`) };
|
|
185
|
-
}
|
|
186
|
-
const out: ModelCandidateRef[] = [];
|
|
187
|
-
for (const [i, c] of list.entries()) {
|
|
188
|
-
const at = `tiers.${tierId}.candidates[${i}]`;
|
|
189
|
-
if (!isRecord(c) || typeof c.provider !== "string" || typeof c.model !== "string") {
|
|
190
|
-
return { error: apiError("bad_request", `${at} must be {provider, model}`) };
|
|
191
|
-
}
|
|
192
|
-
const provider = cfg.providers[c.provider];
|
|
193
|
-
if (!provider) return { error: apiError("bad_request", `${at}: unknown provider "${c.provider}"`) };
|
|
194
|
-
if (!provider.enabled) return { error: apiError("bad_request", `${at}: provider "${c.provider}" is disabled`) };
|
|
195
|
-
if (!provider.models.includes(c.model)) {
|
|
196
|
-
return { error: apiError("model_not_in_whitelist", `${at}: model "${c.model}" is not in the whitelist of "${c.provider}"`) };
|
|
197
|
-
}
|
|
198
|
-
if (out.some((o) => o.provider === c.provider && o.model === c.model)) {
|
|
199
|
-
return { error: apiError("bad_request", `${at} duplicates an earlier candidate`) };
|
|
200
|
-
}
|
|
201
|
-
out.push({ provider: c.provider, model: c.model });
|
|
202
|
-
}
|
|
203
|
-
tiers[tierId] = { candidates: out };
|
|
204
|
-
}
|
|
205
|
-
return { cfg: { ...cfg, tiers } };
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/** `PUT /api/admin/models/routing`。 */
|
|
209
|
-
export function patchRouting(cfg: ModelConfig, body: unknown): { cfg: ModelConfig } | { error: ApiError } {
|
|
210
|
-
if (!isRecord(body)) return { error: apiError("bad_request", "body must be a JSON object") };
|
|
211
|
-
const routing = { ...cfg.routing };
|
|
212
|
-
if (body.failover !== undefined) {
|
|
213
|
-
if (typeof body.failover !== "boolean") return { error: apiError("bad_request", "`failover` must be a boolean") };
|
|
214
|
-
routing.failover = body.failover;
|
|
215
|
-
}
|
|
216
|
-
if (body.stickyWithinRun !== undefined) {
|
|
217
|
-
if (typeof body.stickyWithinRun !== "boolean") return { error: apiError("bad_request", "`stickyWithinRun` must be a boolean") };
|
|
218
|
-
routing.stickyWithinRun = body.stickyWithinRun;
|
|
219
|
-
}
|
|
220
|
-
if (body.cooldownSeconds !== undefined) {
|
|
221
|
-
const n = body.cooldownSeconds;
|
|
222
|
-
if (typeof n !== "number" || !Number.isFinite(n) || n < 0 || n > 3600) {
|
|
223
|
-
return { error: apiError("bad_request", "`cooldownSeconds` must be a number 0..3600") };
|
|
224
|
-
}
|
|
225
|
-
routing.cooldownSeconds = Math.round(n);
|
|
226
|
-
}
|
|
227
|
-
return { cfg: { ...cfg, routing } };
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// ---------------------------------------------------------------- 出网探测
|
|
231
|
-
|
|
232
|
-
const PROBE_TIMEOUT_MS = 8000;
|
|
233
|
-
|
|
234
|
-
export interface ProbeOptions {
|
|
235
|
-
/** 注入 fetch(测试用;生产用全局 fetch)。 */
|
|
236
|
-
fetchImpl?: typeof fetch;
|
|
237
|
-
timeoutMs?: number;
|
|
238
|
-
now?: () => number;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function errText(err: unknown): string {
|
|
242
|
-
return err instanceof Error ? err.message : String(err);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
interface RawModel {
|
|
246
|
-
id?: unknown;
|
|
247
|
-
name?: unknown;
|
|
248
|
-
context_length?: unknown;
|
|
249
|
-
context_window?: unknown;
|
|
250
|
-
contextWindow?: unknown;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* `POST .../refresh`:`GET {baseUrl}/models`(OpenAI 标准,带 Bearer)。
|
|
255
|
-
*
|
|
256
|
-
* 提到「pi 有原生 refreshModels 的 provider 优先走 runtime.refresh」。不走:
|
|
257
|
-
* `runtime.getModels()` 返回的是 models.json 里的静态目录,`refresh` 对没有原生
|
|
258
|
-
* 实现的 provider 是 no-op——那样会「看起来刷新成功、其实拿到旧列表」。等真的接了
|
|
259
|
-
* 一个有原生实现的 provider(OpenRouter 按 不接)再开这条分支。
|
|
260
|
-
*/
|
|
261
|
-
export async function refreshProvider(
|
|
262
|
-
entry: ModelProviderEntry,
|
|
263
|
-
opts: ProbeOptions = {},
|
|
264
|
-
): Promise<{ models: Array<{ id: string; name?: string; contextWindow?: number }>; fetchedAt: string; source: "openai-models" } | { error: ApiError }> {
|
|
265
|
-
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
266
|
-
const now = opts.now ?? Date.now;
|
|
267
|
-
if (!entry.baseUrl) return { error: apiError("bad_request", "provider has no baseUrl") };
|
|
268
|
-
const key = keyOf(entry);
|
|
269
|
-
if (!key) {
|
|
270
|
-
return { error: apiError("provider_unreachable", `key is not configured (${entry.keyRef ?? "no keyRef"}); set the env var on the server`) };
|
|
271
|
-
}
|
|
272
|
-
const url = `${entry.baseUrl.replace(/\/+$/, "")}/models`;
|
|
273
|
-
let res: Response;
|
|
274
|
-
try {
|
|
275
|
-
res = await fetchImpl(url, {
|
|
276
|
-
headers: { authorization: `Bearer ${key}` },
|
|
277
|
-
signal: AbortSignal.timeout(opts.timeoutMs ?? PROBE_TIMEOUT_MS),
|
|
278
|
-
});
|
|
279
|
-
} catch (err) {
|
|
280
|
-
return { error: apiError("provider_unreachable", `GET ${url} failed: ${errText(err)}`) };
|
|
281
|
-
}
|
|
282
|
-
if (!res.ok) return { error: apiError("provider_unreachable", `GET ${url} → ${res.status} ${res.statusText}`) };
|
|
283
|
-
|
|
284
|
-
let body: unknown;
|
|
285
|
-
try {
|
|
286
|
-
body = await res.json();
|
|
287
|
-
} catch (err) {
|
|
288
|
-
return { error: apiError("provider_unreachable", `GET ${url} returned non-JSON: ${errText(err)}`) };
|
|
289
|
-
}
|
|
290
|
-
// OpenAI: {data:[{id,…}]};部分网关: {models:[…]} 或裸数组。
|
|
291
|
-
const list: unknown = isRecord(body) && Array.isArray(body.data) ? body.data : isRecord(body) && Array.isArray(body.models) ? body.models : Array.isArray(body) ? body : null;
|
|
292
|
-
if (!list) return { error: apiError("provider_unreachable", `GET ${url} did not return a model list`) };
|
|
293
|
-
|
|
294
|
-
const models: Array<{ id: string; name?: string; contextWindow?: number }> = [];
|
|
295
|
-
for (const raw of list as RawModel[]) {
|
|
296
|
-
const id = typeof raw === "string" ? raw : typeof raw?.id === "string" ? raw.id : undefined;
|
|
297
|
-
if (!id) continue;
|
|
298
|
-
const cw = raw?.contextWindow ?? raw?.context_length ?? raw?.context_window;
|
|
299
|
-
models.push({
|
|
300
|
-
id,
|
|
301
|
-
...(typeof raw?.name === "string" ? { name: raw.name } : {}),
|
|
302
|
-
...(typeof cw === "number" && Number.isFinite(cw) ? { contextWindow: cw } : {}),
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
return { models, fetchedAt: new Date(now()).toISOString(), source: "openai-models" };
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* `POST .../test`:发一次 `max_tokens:1` 的最小请求——`checkAuth` 只看凭证形状,
|
|
310
|
-
* 验不了 baseUrl 通不通、key 有没有效。
|
|
311
|
-
*/
|
|
312
|
-
export async function testProvider(entry: ModelProviderEntry, model: string, opts: ProbeOptions = {}): Promise<ModelTestResponse> {
|
|
313
|
-
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
314
|
-
const now = opts.now ?? Date.now;
|
|
315
|
-
const started = now();
|
|
316
|
-
const fail = (error: string): ModelTestResponse => ({ ok: false, latencyMs: now() - started, model, error });
|
|
317
|
-
if (!entry.baseUrl) return fail("provider has no baseUrl");
|
|
318
|
-
const key = keyOf(entry);
|
|
319
|
-
if (!key) return fail(`key is not configured (${entry.keyRef ?? "no keyRef"})`);
|
|
320
|
-
|
|
321
|
-
const url = `${entry.baseUrl.replace(/\/+$/, "")}/chat/completions`;
|
|
322
|
-
try {
|
|
323
|
-
const res = await fetchImpl(url, {
|
|
324
|
-
method: "POST",
|
|
325
|
-
headers: { authorization: `Bearer ${key}`, "content-type": "application/json" },
|
|
326
|
-
body: JSON.stringify({ model, messages: [{ role: "user", content: "ping" }], max_tokens: 1 }),
|
|
327
|
-
signal: AbortSignal.timeout(opts.timeoutMs ?? PROBE_TIMEOUT_MS),
|
|
328
|
-
});
|
|
329
|
-
if (!res.ok) {
|
|
330
|
-
const text = await res.text().catch(() => "");
|
|
331
|
-
return fail(`${res.status} ${res.statusText}${text ? `: ${text.slice(0, 200)}` : ""}`);
|
|
332
|
-
}
|
|
333
|
-
return { ok: true, latencyMs: now() - started, model };
|
|
334
|
-
} catch (err) {
|
|
335
|
-
return fail(errText(err));
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/** 探测用哪个模型:白名单第一个,退而求其次目录第一个。 */
|
|
340
|
-
export function modelToProbe(entry: ModelProviderEntry): string | undefined {
|
|
341
|
-
return entry.models[0] ?? entry.catalog[0];
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* 从 `models.json`(pi 真正读的那份)里取端点。`model-config.json` 的 baseUrl 是
|
|
346
|
-
* 给管理页看的副本;配置里还没写的时候(首次进页面、或文件缺失回落 manifest),
|
|
347
|
-
* refresh/test 必须能借 models.json 的端点跑起来——否则「先去 models.json 写端点,
|
|
348
|
-
* 回来点刷新」这个流程第一步就死。
|
|
349
|
-
*/
|
|
350
|
-
export function baseUrlFromModelsJson(dataDir: string, providerId: string): string | undefined {
|
|
351
|
-
try {
|
|
352
|
-
const raw = JSON.parse(readFileSync(path.join(dataDir, "models.json"), "utf8")) as unknown;
|
|
353
|
-
if (!isRecord(raw) || !isRecord(raw.providers)) return undefined;
|
|
354
|
-
const entry = raw.providers[providerId];
|
|
355
|
-
if (!isRecord(entry)) return undefined;
|
|
356
|
-
return typeof entry.baseUrl === "string" && entry.baseUrl.trim() !== "" ? entry.baseUrl.trim().replace(/\/+$/, "") : undefined;
|
|
357
|
-
} catch {
|
|
358
|
-
return undefined;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { ModelCandidateRef, ModelProviderView, ModelsResponse, ModelTestResponse } from "@njuptlzf/agent-chat-protocol";
|
|
4
|
+
import type { ModelConfig, ModelProviderEntry } from "./model-config.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 模型配置面的纯逻辑。
|
|
8
|
+
*
|
|
9
|
+
* 这里只有「校验 + 变形 + 两个出网探测」,没有 HTTP 也没有落盘——路由与审计写点
|
|
10
|
+
* 留在 server.ts(与 handleAdminAudit 同款分层),落盘走 model-config.ts 的
|
|
11
|
+
* saveModelConfig(原子替换 + 作废缓存)。
|
|
12
|
+
*
|
|
13
|
+
* 校验口径必须与 的 parseModelConfig 同源:**页面放行、运行时丢掉**是最坏的组合
|
|
14
|
+
* (管理员以为配好了,实际每次 run 都在回落 manifest)。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export interface ApiError {
|
|
18
|
+
code: "bad_request" | "not_found" | "provider_in_use" | "model_in_use" | "model_not_in_whitelist" | "provider_unreachable";
|
|
19
|
+
message: string;
|
|
20
|
+
status: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const STATUS: Record<ApiError["code"], number> = {
|
|
24
|
+
bad_request: 400,
|
|
25
|
+
not_found: 404,
|
|
26
|
+
provider_in_use: 409,
|
|
27
|
+
model_in_use: 400,
|
|
28
|
+
model_not_in_whitelist: 400,
|
|
29
|
+
provider_unreachable: 502,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function apiError(code: ApiError["code"], message: string): ApiError {
|
|
33
|
+
return { code, message, status: STATUS[code] };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** `env:NAME` → 实际密钥。没配/空串都算没配(keyConfigured=false)。 */
|
|
37
|
+
export function keyOf(entry: ModelProviderEntry): string | undefined {
|
|
38
|
+
const ref = entry.keyRef;
|
|
39
|
+
if (!ref || !ref.startsWith("env:")) return undefined;
|
|
40
|
+
const value = process.env[ref.slice(4)];
|
|
41
|
+
return value && value.trim() !== "" ? value : undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `env:SENSENOVA_API_KEY` → `SENSENOVA_API_KEY`(展示用,不是密钥)。 */
|
|
45
|
+
export function keyEnvOf(entry: ModelProviderEntry): string | undefined {
|
|
46
|
+
const ref = entry.keyRef;
|
|
47
|
+
return ref && ref.startsWith("env:") ? ref.slice(4) : undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 档位的展示元信息(label/multiplier)来自 manifest,配置只重绑不新增档位。 */
|
|
51
|
+
export interface TierMeta {
|
|
52
|
+
label?: string;
|
|
53
|
+
multiplier?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function toModelsResponse(cfg: ModelConfig, configPath: string, tierMeta: Record<string, TierMeta> = {}): ModelsResponse {
|
|
57
|
+
const providers: ModelProviderView[] = Object.entries(cfg.providers).map(([id, p]) => {
|
|
58
|
+
const keyEnv = keyEnvOf(p);
|
|
59
|
+
return {
|
|
60
|
+
id,
|
|
61
|
+
enabled: p.enabled,
|
|
62
|
+
...(p.api ? { api: p.api } : {}),
|
|
63
|
+
...(p.baseUrl ? { baseUrl: p.baseUrl } : {}),
|
|
64
|
+
keyConfigured: keyOf(p) !== undefined,
|
|
65
|
+
...(keyEnv ? { keyEnv } : {}),
|
|
66
|
+
models: [...p.models],
|
|
67
|
+
catalog: [...p.catalog],
|
|
68
|
+
...(p.fetchedAt ? { fetchedAt: p.fetchedAt } : {}),
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
const tiers: ModelsResponse["tiers"] = {};
|
|
72
|
+
for (const [id, t] of Object.entries(cfg.tiers)) {
|
|
73
|
+
const meta = tierMeta[id];
|
|
74
|
+
tiers[id] = {
|
|
75
|
+
...(meta?.label ? { label: meta.label } : {}),
|
|
76
|
+
...(meta?.multiplier !== undefined ? { multiplier: meta.multiplier } : {}),
|
|
77
|
+
candidates: t.candidates.map((c) => ({ ...c })),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
source: cfg.source,
|
|
82
|
+
maxCandidates: cfg.maxCandidates,
|
|
83
|
+
providers,
|
|
84
|
+
tiers,
|
|
85
|
+
routing: { ...cfg.routing },
|
|
86
|
+
problems: [...cfg.problems],
|
|
87
|
+
configPath,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
92
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* `PUT /api/admin/models/providers/:id`:只允许改白名单字段。
|
|
99
|
+
* 不做 upsert——新增 provider 必须在 `models.json` 里也有端点,浏览器填 key 是 的延后项。
|
|
100
|
+
*/
|
|
101
|
+
export function patchProvider(cfg: ModelConfig, id: string, body: unknown): { cfg: ModelConfig } | { error: ApiError } {
|
|
102
|
+
const current = cfg.providers[id];
|
|
103
|
+
if (!current) return { error: apiError("not_found", `unknown provider "${id}"`) };
|
|
104
|
+
if (!isRecord(body)) return { error: apiError("bad_request", "body must be a JSON object") };
|
|
105
|
+
|
|
106
|
+
const next: ModelProviderEntry = { ...current, models: [...current.models], catalog: [...current.catalog] };
|
|
107
|
+
|
|
108
|
+
if (body.enabled !== undefined) {
|
|
109
|
+
if (typeof body.enabled !== "boolean") return { error: apiError("bad_request", "`enabled` must be a boolean") };
|
|
110
|
+
next.enabled = body.enabled;
|
|
111
|
+
}
|
|
112
|
+
if (body.api !== undefined) {
|
|
113
|
+
if (typeof body.api !== "string" || body.api.trim() === "") return { error: apiError("bad_request", "`api` must be a non-empty string") };
|
|
114
|
+
next.api = body.api.trim();
|
|
115
|
+
}
|
|
116
|
+
if (body.baseUrl !== undefined) {
|
|
117
|
+
if (typeof body.baseUrl !== "string") return { error: apiError("bad_request", "`baseUrl` must be a string") };
|
|
118
|
+
let url: URL;
|
|
119
|
+
try {
|
|
120
|
+
url = new URL(body.baseUrl.trim());
|
|
121
|
+
} catch {
|
|
122
|
+
return { error: apiError("bad_request", "`baseUrl` must be an absolute URL") };
|
|
123
|
+
}
|
|
124
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return { error: apiError("bad_request", "`baseUrl` must be http(s)") };
|
|
125
|
+
next.baseUrl = body.baseUrl.trim().replace(/\/+$/, "");
|
|
126
|
+
}
|
|
127
|
+
if (body.keyEnv !== undefined) {
|
|
128
|
+
// 只接受环境变量名:密钥永远不经过浏览器。
|
|
129
|
+
if (body.keyEnv === null || body.keyEnv === "") delete next.keyRef;
|
|
130
|
+
else if (typeof body.keyEnv === "string" && ENV_NAME.test(body.keyEnv.trim())) next.keyRef = `env:${body.keyEnv.trim()}`;
|
|
131
|
+
else return { error: apiError("bad_request", "`keyEnv` must be an env var name like SENSENOVA_API_KEY") };
|
|
132
|
+
}
|
|
133
|
+
if (body.models !== undefined) {
|
|
134
|
+
if (!Array.isArray(body.models) || body.models.some((m) => typeof m !== "string" || m.trim() === "")) {
|
|
135
|
+
return { error: apiError("bad_request", "`models` must be a list of non-empty strings") };
|
|
136
|
+
}
|
|
137
|
+
const whitelist = [...new Set((body.models as string[]).map((m) => m.trim()))];
|
|
138
|
+
// 撤白名单前先看有没有档位还引用它——否则运行时会静默丢掉这个候选(的反面)。
|
|
139
|
+
for (const [tierId, tier] of Object.entries(cfg.tiers)) {
|
|
140
|
+
for (const c of tier.candidates) {
|
|
141
|
+
if (c.provider === id && !whitelist.includes(c.model)) {
|
|
142
|
+
return { error: apiError("model_in_use", `model "${c.model}" is still bound to tier "${tierId}"; unbind it first`) };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
next.models = whitelist;
|
|
147
|
+
// 手工写进白名单的模型也要在目录里可见,否则页面永远显示「不在目录」。
|
|
148
|
+
for (const m of whitelist) if (!next.catalog.includes(m)) next.catalog.push(m);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return { cfg: { ...cfg, providers: { ...cfg.providers, [id]: next } } };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** `DELETE /api/admin/models/providers/:id`:被档位引用 → 409。 */
|
|
155
|
+
export function deleteProvider(cfg: ModelConfig, id: string): { cfg: ModelConfig } | { error: ApiError } {
|
|
156
|
+
if (!cfg.providers[id]) return { error: apiError("not_found", `unknown provider "${id}"`) };
|
|
157
|
+
const usedBy = Object.entries(cfg.tiers).filter(([, t]) => t.candidates.some((c) => c.provider === id)).map(([tierId]) => tierId);
|
|
158
|
+
if (usedBy.length > 0) return { error: apiError("provider_in_use", `provider "${id}" is still bound to tier(s) ${usedBy.join(", ")}`) };
|
|
159
|
+
const providers = { ...cfg.providers };
|
|
160
|
+
delete providers[id];
|
|
161
|
+
return { cfg: { ...cfg, providers } };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* `PUT /api/admin/models/tiers`:整档替换(只动请求里出现的档位)。
|
|
166
|
+
* 档位集合来自 manifest——配置只能重绑,不能新增档位(否则 `/api/chat` 仍然 400,
|
|
167
|
+
* 页面却显示配好了)。
|
|
168
|
+
*/
|
|
169
|
+
export function patchTiers(cfg: ModelConfig, body: unknown, allowedTierIds: readonly string[]): { cfg: ModelConfig } | { error: ApiError } {
|
|
170
|
+
if (!isRecord(body) || !isRecord(body.tiers)) {
|
|
171
|
+
return { error: apiError("bad_request", "expected {tiers:{<id>:{candidates:[{provider,model}]}}}") };
|
|
172
|
+
}
|
|
173
|
+
const tiers = { ...cfg.tiers };
|
|
174
|
+
for (const [tierId, raw] of Object.entries(body.tiers)) {
|
|
175
|
+
if (!allowedTierIds.includes(tierId)) {
|
|
176
|
+
return { error: apiError("bad_request", `unknown tier "${tierId}"(档位集合来自 manifest,配置只能重绑不能新增)`) };
|
|
177
|
+
}
|
|
178
|
+
if (!isRecord(raw) || !Array.isArray(raw.candidates)) {
|
|
179
|
+
return { error: apiError("bad_request", `tiers.${tierId}.candidates must be a list`) };
|
|
180
|
+
}
|
|
181
|
+
const list = raw.candidates;
|
|
182
|
+
if (list.length === 0) return { error: apiError("bad_request", `tiers.${tierId}: at least one candidate is required`) };
|
|
183
|
+
if (list.length > cfg.maxCandidates) {
|
|
184
|
+
return { error: apiError("bad_request", `tiers.${tierId}: ${list.length} candidates exceed maxCandidates ${cfg.maxCandidates}`) };
|
|
185
|
+
}
|
|
186
|
+
const out: ModelCandidateRef[] = [];
|
|
187
|
+
for (const [i, c] of list.entries()) {
|
|
188
|
+
const at = `tiers.${tierId}.candidates[${i}]`;
|
|
189
|
+
if (!isRecord(c) || typeof c.provider !== "string" || typeof c.model !== "string") {
|
|
190
|
+
return { error: apiError("bad_request", `${at} must be {provider, model}`) };
|
|
191
|
+
}
|
|
192
|
+
const provider = cfg.providers[c.provider];
|
|
193
|
+
if (!provider) return { error: apiError("bad_request", `${at}: unknown provider "${c.provider}"`) };
|
|
194
|
+
if (!provider.enabled) return { error: apiError("bad_request", `${at}: provider "${c.provider}" is disabled`) };
|
|
195
|
+
if (!provider.models.includes(c.model)) {
|
|
196
|
+
return { error: apiError("model_not_in_whitelist", `${at}: model "${c.model}" is not in the whitelist of "${c.provider}"`) };
|
|
197
|
+
}
|
|
198
|
+
if (out.some((o) => o.provider === c.provider && o.model === c.model)) {
|
|
199
|
+
return { error: apiError("bad_request", `${at} duplicates an earlier candidate`) };
|
|
200
|
+
}
|
|
201
|
+
out.push({ provider: c.provider, model: c.model });
|
|
202
|
+
}
|
|
203
|
+
tiers[tierId] = { candidates: out };
|
|
204
|
+
}
|
|
205
|
+
return { cfg: { ...cfg, tiers } };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** `PUT /api/admin/models/routing`。 */
|
|
209
|
+
export function patchRouting(cfg: ModelConfig, body: unknown): { cfg: ModelConfig } | { error: ApiError } {
|
|
210
|
+
if (!isRecord(body)) return { error: apiError("bad_request", "body must be a JSON object") };
|
|
211
|
+
const routing = { ...cfg.routing };
|
|
212
|
+
if (body.failover !== undefined) {
|
|
213
|
+
if (typeof body.failover !== "boolean") return { error: apiError("bad_request", "`failover` must be a boolean") };
|
|
214
|
+
routing.failover = body.failover;
|
|
215
|
+
}
|
|
216
|
+
if (body.stickyWithinRun !== undefined) {
|
|
217
|
+
if (typeof body.stickyWithinRun !== "boolean") return { error: apiError("bad_request", "`stickyWithinRun` must be a boolean") };
|
|
218
|
+
routing.stickyWithinRun = body.stickyWithinRun;
|
|
219
|
+
}
|
|
220
|
+
if (body.cooldownSeconds !== undefined) {
|
|
221
|
+
const n = body.cooldownSeconds;
|
|
222
|
+
if (typeof n !== "number" || !Number.isFinite(n) || n < 0 || n > 3600) {
|
|
223
|
+
return { error: apiError("bad_request", "`cooldownSeconds` must be a number 0..3600") };
|
|
224
|
+
}
|
|
225
|
+
routing.cooldownSeconds = Math.round(n);
|
|
226
|
+
}
|
|
227
|
+
return { cfg: { ...cfg, routing } };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ---------------------------------------------------------------- 出网探测
|
|
231
|
+
|
|
232
|
+
const PROBE_TIMEOUT_MS = 8000;
|
|
233
|
+
|
|
234
|
+
export interface ProbeOptions {
|
|
235
|
+
/** 注入 fetch(测试用;生产用全局 fetch)。 */
|
|
236
|
+
fetchImpl?: typeof fetch;
|
|
237
|
+
timeoutMs?: number;
|
|
238
|
+
now?: () => number;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function errText(err: unknown): string {
|
|
242
|
+
return err instanceof Error ? err.message : String(err);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
interface RawModel {
|
|
246
|
+
id?: unknown;
|
|
247
|
+
name?: unknown;
|
|
248
|
+
context_length?: unknown;
|
|
249
|
+
context_window?: unknown;
|
|
250
|
+
contextWindow?: unknown;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* `POST .../refresh`:`GET {baseUrl}/models`(OpenAI 标准,带 Bearer)。
|
|
255
|
+
*
|
|
256
|
+
* 提到「pi 有原生 refreshModels 的 provider 优先走 runtime.refresh」。不走:
|
|
257
|
+
* `runtime.getModels()` 返回的是 models.json 里的静态目录,`refresh` 对没有原生
|
|
258
|
+
* 实现的 provider 是 no-op——那样会「看起来刷新成功、其实拿到旧列表」。等真的接了
|
|
259
|
+
* 一个有原生实现的 provider(OpenRouter 按 不接)再开这条分支。
|
|
260
|
+
*/
|
|
261
|
+
export async function refreshProvider(
|
|
262
|
+
entry: ModelProviderEntry,
|
|
263
|
+
opts: ProbeOptions = {},
|
|
264
|
+
): Promise<{ models: Array<{ id: string; name?: string; contextWindow?: number }>; fetchedAt: string; source: "openai-models" } | { error: ApiError }> {
|
|
265
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
266
|
+
const now = opts.now ?? Date.now;
|
|
267
|
+
if (!entry.baseUrl) return { error: apiError("bad_request", "provider has no baseUrl") };
|
|
268
|
+
const key = keyOf(entry);
|
|
269
|
+
if (!key) {
|
|
270
|
+
return { error: apiError("provider_unreachable", `key is not configured (${entry.keyRef ?? "no keyRef"}); set the env var on the server`) };
|
|
271
|
+
}
|
|
272
|
+
const url = `${entry.baseUrl.replace(/\/+$/, "")}/models`;
|
|
273
|
+
let res: Response;
|
|
274
|
+
try {
|
|
275
|
+
res = await fetchImpl(url, {
|
|
276
|
+
headers: { authorization: `Bearer ${key}` },
|
|
277
|
+
signal: AbortSignal.timeout(opts.timeoutMs ?? PROBE_TIMEOUT_MS),
|
|
278
|
+
});
|
|
279
|
+
} catch (err) {
|
|
280
|
+
return { error: apiError("provider_unreachable", `GET ${url} failed: ${errText(err)}`) };
|
|
281
|
+
}
|
|
282
|
+
if (!res.ok) return { error: apiError("provider_unreachable", `GET ${url} → ${res.status} ${res.statusText}`) };
|
|
283
|
+
|
|
284
|
+
let body: unknown;
|
|
285
|
+
try {
|
|
286
|
+
body = await res.json();
|
|
287
|
+
} catch (err) {
|
|
288
|
+
return { error: apiError("provider_unreachable", `GET ${url} returned non-JSON: ${errText(err)}`) };
|
|
289
|
+
}
|
|
290
|
+
// OpenAI: {data:[{id,…}]};部分网关: {models:[…]} 或裸数组。
|
|
291
|
+
const list: unknown = isRecord(body) && Array.isArray(body.data) ? body.data : isRecord(body) && Array.isArray(body.models) ? body.models : Array.isArray(body) ? body : null;
|
|
292
|
+
if (!list) return { error: apiError("provider_unreachable", `GET ${url} did not return a model list`) };
|
|
293
|
+
|
|
294
|
+
const models: Array<{ id: string; name?: string; contextWindow?: number }> = [];
|
|
295
|
+
for (const raw of list as RawModel[]) {
|
|
296
|
+
const id = typeof raw === "string" ? raw : typeof raw?.id === "string" ? raw.id : undefined;
|
|
297
|
+
if (!id) continue;
|
|
298
|
+
const cw = raw?.contextWindow ?? raw?.context_length ?? raw?.context_window;
|
|
299
|
+
models.push({
|
|
300
|
+
id,
|
|
301
|
+
...(typeof raw?.name === "string" ? { name: raw.name } : {}),
|
|
302
|
+
...(typeof cw === "number" && Number.isFinite(cw) ? { contextWindow: cw } : {}),
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
return { models, fetchedAt: new Date(now()).toISOString(), source: "openai-models" };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* `POST .../test`:发一次 `max_tokens:1` 的最小请求——`checkAuth` 只看凭证形状,
|
|
310
|
+
* 验不了 baseUrl 通不通、key 有没有效。
|
|
311
|
+
*/
|
|
312
|
+
export async function testProvider(entry: ModelProviderEntry, model: string, opts: ProbeOptions = {}): Promise<ModelTestResponse> {
|
|
313
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
314
|
+
const now = opts.now ?? Date.now;
|
|
315
|
+
const started = now();
|
|
316
|
+
const fail = (error: string): ModelTestResponse => ({ ok: false, latencyMs: now() - started, model, error });
|
|
317
|
+
if (!entry.baseUrl) return fail("provider has no baseUrl");
|
|
318
|
+
const key = keyOf(entry);
|
|
319
|
+
if (!key) return fail(`key is not configured (${entry.keyRef ?? "no keyRef"})`);
|
|
320
|
+
|
|
321
|
+
const url = `${entry.baseUrl.replace(/\/+$/, "")}/chat/completions`;
|
|
322
|
+
try {
|
|
323
|
+
const res = await fetchImpl(url, {
|
|
324
|
+
method: "POST",
|
|
325
|
+
headers: { authorization: `Bearer ${key}`, "content-type": "application/json" },
|
|
326
|
+
body: JSON.stringify({ model, messages: [{ role: "user", content: "ping" }], max_tokens: 1 }),
|
|
327
|
+
signal: AbortSignal.timeout(opts.timeoutMs ?? PROBE_TIMEOUT_MS),
|
|
328
|
+
});
|
|
329
|
+
if (!res.ok) {
|
|
330
|
+
const text = await res.text().catch(() => "");
|
|
331
|
+
return fail(`${res.status} ${res.statusText}${text ? `: ${text.slice(0, 200)}` : ""}`);
|
|
332
|
+
}
|
|
333
|
+
return { ok: true, latencyMs: now() - started, model };
|
|
334
|
+
} catch (err) {
|
|
335
|
+
return fail(errText(err));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** 探测用哪个模型:白名单第一个,退而求其次目录第一个。 */
|
|
340
|
+
export function modelToProbe(entry: ModelProviderEntry): string | undefined {
|
|
341
|
+
return entry.models[0] ?? entry.catalog[0];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* 从 `models.json`(pi 真正读的那份)里取端点。`model-config.json` 的 baseUrl 是
|
|
346
|
+
* 给管理页看的副本;配置里还没写的时候(首次进页面、或文件缺失回落 manifest),
|
|
347
|
+
* refresh/test 必须能借 models.json 的端点跑起来——否则「先去 models.json 写端点,
|
|
348
|
+
* 回来点刷新」这个流程第一步就死。
|
|
349
|
+
*/
|
|
350
|
+
export function baseUrlFromModelsJson(dataDir: string, providerId: string): string | undefined {
|
|
351
|
+
try {
|
|
352
|
+
const raw = JSON.parse(readFileSync(path.join(dataDir, "models.json"), "utf8")) as unknown;
|
|
353
|
+
if (!isRecord(raw) || !isRecord(raw.providers)) return undefined;
|
|
354
|
+
const entry = raw.providers[providerId];
|
|
355
|
+
if (!isRecord(entry)) return undefined;
|
|
356
|
+
return typeof entry.baseUrl === "string" && entry.baseUrl.trim() !== "" ? entry.baseUrl.trim().replace(/\/+$/, "") : undefined;
|
|
357
|
+
} catch {
|
|
358
|
+
return undefined;
|
|
359
|
+
}
|
|
360
|
+
}
|