dal-code 1.0.13 → 1.0.15
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 +10 -0
- package/dist/core/agent-session-services.d.ts +7 -0
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +73 -0
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/model-runtime.d.ts +28 -0
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +49 -0
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +8 -7
- package/dist/main.js.map +1 -1
- package/dist/node_modules/@dalcode/dal-bridge/package.json +2 -2
- package/dist/node_modules/@dalcode/dal-gateway/package.json +2 -2
- package/dist/node_modules/@dalcode/dal-gateway/src/index.ts +84 -84
- package/dist/node_modules/@dalcode/dal-guard/package.json +4 -4
- package/dist/node_modules/@dalcode/dal-halo/package.json +1 -1
- package/dist/node_modules/@dalcode/dal-lang/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-lsp/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-mcp/package.json +5 -5
- package/dist/node_modules/@dalcode/dal-mcp/src/index.ts +136 -9
- package/dist/node_modules/@dalcode/dal-office/package.json +2 -2
- package/dist/node_modules/@dalcode/dal-pi-kit/package.json +1 -1
- package/dist/node_modules/@dalcode/dal-router/LICENSE +21 -0
- package/dist/node_modules/@dalcode/dal-router/package.json +36 -0
- package/dist/node_modules/@dalcode/dal-router/src/classifier.ts +80 -0
- package/dist/node_modules/@dalcode/dal-router/src/commands.ts +104 -0
- package/dist/node_modules/@dalcode/dal-router/src/config.ts +137 -0
- package/dist/node_modules/@dalcode/dal-router/src/features.ts +85 -0
- package/dist/node_modules/@dalcode/dal-router/src/index.ts +236 -0
- package/dist/node_modules/@dalcode/dal-router/src/router.ts +150 -0
- package/dist/node_modules/@dalcode/dal-router/src/runtime.ts +88 -0
- package/dist/node_modules/@dalcode/dal-router/src/small-model.ts +36 -0
- package/dist/node_modules/@dalcode/dal-router/src/subagent.ts +58 -0
- package/dist/node_modules/@dalcode/dal-router/src/tiers.ts +187 -0
- package/dist/node_modules/@dalcode/dal-skills/package.json +3 -3
- package/dist/node_modules/@dalcode/dal-skills/src/index.ts +65 -33
- package/dist/node_modules/@dalcode/dal-subagent/package.json +5 -5
- package/dist/node_modules/@dalcode/dal-telemetry/package.json +2 -2
- package/dist/node_modules/@dalcode/deepailab/package.json +4 -3
- package/dist/node_modules/@dalcode/deepailab/src/cache.ts +62 -0
- package/dist/node_modules/@dalcode/deepailab/src/index.ts +286 -127
- package/dist/node_modules/@earendil-works/chord/package.json +1 -1
- package/dist/node_modules/@earendil-works/pi-agent-core/package.json +3 -3
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/amazon-bedrock.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/anthropic.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/azure-openai-responses.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/cloudflare-workers-ai.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/github-copilot.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/openai.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/node_modules/@earendil-works/pi-ai/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-client/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-protocol/package.json +2 -2
- package/dist/node_modules/@earendil-works/pi-server/package.json +3 -3
- package/dist/node_modules/@earendil-works/pi-session-backend-sqlite-node/package.json +3 -3
- package/dist/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
- package/dist/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -17,13 +17,23 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { t } from "@dalcode/dal-pi-kit";
|
|
20
|
-
import type {
|
|
21
|
-
|
|
20
|
+
import type {
|
|
21
|
+
Api,
|
|
22
|
+
Credential,
|
|
23
|
+
Model,
|
|
24
|
+
ModelsStoreEntry,
|
|
25
|
+
OAuthCredentials,
|
|
26
|
+
OAuthLoginCallbacks,
|
|
27
|
+
RefreshModelsContext,
|
|
28
|
+
} from "@earendil-works/pi-ai";
|
|
29
|
+
import { type ExtensionAPI, getAgentDir, SettingsManager } from "@earendil-works/pi-coding-agent";
|
|
30
|
+
import { accountTag, readCache, writeCache } from "./cache.ts";
|
|
22
31
|
import { gatewayModelDefaults } from "./model-defaults.ts";
|
|
23
32
|
import {
|
|
24
33
|
defaultConfig,
|
|
25
34
|
discover,
|
|
26
35
|
fetchMembership,
|
|
36
|
+
type OidcEndpoints,
|
|
27
37
|
pollDeviceToken,
|
|
28
38
|
refresh,
|
|
29
39
|
startDeviceFlow,
|
|
@@ -161,17 +171,52 @@ export async function isVirtualKeyLive(apiBase: string, vk: string): Promise<boo
|
|
|
161
171
|
}
|
|
162
172
|
}
|
|
163
173
|
|
|
174
|
+
/** How long a persisted OIDC discovery document is trusted. */
|
|
175
|
+
const DISCOVERY_CACHE_MS = 24 * 60 * 60 * 1000;
|
|
176
|
+
|
|
177
|
+
function isEndpoints(value: unknown): value is OidcEndpoints {
|
|
178
|
+
if (typeof value !== "object" || value === null) return false;
|
|
179
|
+
const e = value as Partial<Record<keyof OidcEndpoints, unknown>>;
|
|
180
|
+
return typeof e.issuer === "string" && typeof e.tokenEndpoint === "string";
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* discover(), persisted across processes. A token refresh runs whenever the
|
|
185
|
+
* access token has expired, i.e. in roughly one dal process every 15 minutes,
|
|
186
|
+
* and it sits in front of the first model request. The discovery document
|
|
187
|
+
* for a realm practically never changes, but fetching it took 7.4s of a ~20s
|
|
188
|
+
* refresh behind a proxy (2026-09-23).
|
|
189
|
+
*/
|
|
190
|
+
async function discoverCached(issuer: string): Promise<OidcEndpoints> {
|
|
191
|
+
const name = `oidc-discovery-${accountTag(issuer)}`;
|
|
192
|
+
const cached = readCache(name, isEndpoints);
|
|
193
|
+
if (cached && Date.now() - cached.savedAt < DISCOVERY_CACHE_MS) return cached.value;
|
|
194
|
+
const endpoints = await discover(issuer);
|
|
195
|
+
try {
|
|
196
|
+
writeCache(name, endpoints);
|
|
197
|
+
} catch {
|
|
198
|
+
// An unwritable cache only costs the next refresh this same request.
|
|
199
|
+
}
|
|
200
|
+
return endpoints;
|
|
201
|
+
}
|
|
202
|
+
|
|
164
203
|
export async function refreshDeepAILab(credentials: OAuthCredentials): Promise<OAuthCredentials> {
|
|
165
204
|
const cfg = defaultConfig();
|
|
166
|
-
const endpoints = await
|
|
167
|
-
const tokens = await refresh(cfg, endpoints, credentials.refresh);
|
|
205
|
+
const endpoints = await discoverCached(cfg.issuer);
|
|
168
206
|
// The virtual key is long-lived, so it survives OIDC token refreshes — but
|
|
169
207
|
// only while the gateway still accepts it. Re-mint when it is missing
|
|
170
208
|
// (older credential format, or a failed exchange during login) or revoked,
|
|
171
209
|
// while we hold a fresh access token; otherwise inference fails on every
|
|
172
210
|
// call with an error that looks like an outage rather than a stale key.
|
|
211
|
+
// The liveness probe does not need the new tokens, so it runs alongside
|
|
212
|
+
// the token refresh instead of after it (each is a 5-6s request behind a
|
|
213
|
+
// proxy, and both delay the first model request).
|
|
173
214
|
let vk = String(credentials.vk ?? "");
|
|
174
|
-
|
|
215
|
+
const [tokens, live] = await Promise.all([
|
|
216
|
+
refresh(cfg, endpoints, credentials.refresh),
|
|
217
|
+
vk ? isVirtualKeyLive(cfg.apiBase, vk) : Promise.resolve(false),
|
|
218
|
+
]);
|
|
219
|
+
if (!live) {
|
|
175
220
|
vk = await createVirtualKey(cfg.apiBase, tokens.accessToken);
|
|
176
221
|
}
|
|
177
222
|
return toCredentials(tokens, vk);
|
|
@@ -190,17 +235,238 @@ export async function refreshDeepAILab(credentials: OAuthCredentials): Promise<O
|
|
|
190
235
|
// picked before logging in vanished from the list right after.
|
|
191
236
|
const SEED_MODELS: ReturnType<typeof gatewayModelDefaults>[] = [];
|
|
192
237
|
|
|
238
|
+
type GatewayModel = ReturnType<typeof gatewayModelDefaults>;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* How long a fetched catalog is used without asking the gateway again. The
|
|
242
|
+
* gateway's list changes a few times a week; DAL Office starts a dal process
|
|
243
|
+
* per chat turn and runs `--list-models` for its picker, and each gateway
|
|
244
|
+
* request costs 5-6s on a proxied connection. /login bypasses it (`force`).
|
|
245
|
+
*/
|
|
246
|
+
const CATALOG_FRESH_MS = 15 * 60 * 1000;
|
|
247
|
+
|
|
248
|
+
/** The key the gateway accepts for this credential; mirrors oauth.getApiKey below. */
|
|
249
|
+
function gatewayKey(credential: Credential | undefined): string | undefined {
|
|
250
|
+
if (credential?.type === "oauth") return String(credential.vk || credential.access || "") || undefined;
|
|
251
|
+
if (credential?.type === "api_key") return credential.key || undefined;
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Fetch the gateway's model list. Throws a user-facing message on failure;
|
|
257
|
+
* the caller keeps the last good catalog, and the message becomes a startup
|
|
258
|
+
* warning.
|
|
259
|
+
*
|
|
260
|
+
* Latency: the server itself answers in ~0.3-0.8s, but end to end a request
|
|
261
|
+
* took 2.8-8.3s (p50 5.8s, 40 requests at 8-way concurrency, 2026-09-23) on a
|
|
262
|
+
* machine behind a TUN proxy, almost all of it TLS setup. The 20s bound is on
|
|
263
|
+
* top of the caller's signal so an unreachable gateway cannot hang a caller
|
|
264
|
+
* that passed none.
|
|
265
|
+
*/
|
|
266
|
+
async function fetchGatewayCatalog(apiBase: string, key: string, signal: AbortSignal): Promise<GatewayModel[]> {
|
|
267
|
+
let res: Response;
|
|
268
|
+
try {
|
|
269
|
+
res = await fetch(`${apiBase}/models`, {
|
|
270
|
+
headers: { "x-bf-vk": key, Accept: "application/json" },
|
|
271
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(20_000)]),
|
|
272
|
+
});
|
|
273
|
+
} catch (error) {
|
|
274
|
+
const aborted = error instanceof Error && (error.name === "AbortError" || error.name === "TimeoutError");
|
|
275
|
+
if (aborted) throw new Error(`${t("无法获取模型列表")}:${t("请求超时")}`);
|
|
276
|
+
// undici reports every connection failure as `TypeError: fetch failed`;
|
|
277
|
+
// the actual reason (ECONNRESET, UND_ERR_CONNECT_TIMEOUT, ENOTFOUND...)
|
|
278
|
+
// is on `cause`. Without it a transient failure cannot be diagnosed.
|
|
279
|
+
const cause =
|
|
280
|
+
error instanceof Error ? (error.cause as { code?: string; message?: string } | undefined) : undefined;
|
|
281
|
+
const detail = cause?.code ?? cause?.message;
|
|
282
|
+
throw new Error(`${t("无法获取模型列表")}:${t("无法连接 DeepAILab 网关")}${detail ? ` (${detail})` : ""}`);
|
|
283
|
+
}
|
|
284
|
+
if (!res.ok) {
|
|
285
|
+
throw new Error(`${t("无法获取模型列表")}:${t("网关拒绝了请求")} (HTTP ${res.status})`);
|
|
286
|
+
}
|
|
287
|
+
const body = (await res.json()) as {
|
|
288
|
+
data?: Array<{
|
|
289
|
+
id?: string;
|
|
290
|
+
name?: string;
|
|
291
|
+
display_name?: string;
|
|
292
|
+
context_window?: number;
|
|
293
|
+
max_output_tokens?: number;
|
|
294
|
+
capabilities?: string[];
|
|
295
|
+
reasoning_efforts?: string[];
|
|
296
|
+
pricing?: {
|
|
297
|
+
input_per_1m?: number;
|
|
298
|
+
output_per_1m?: number;
|
|
299
|
+
cache_read_per_1m?: number;
|
|
300
|
+
cache_write_per_1m?: number;
|
|
301
|
+
currency?: string;
|
|
302
|
+
};
|
|
303
|
+
}>;
|
|
304
|
+
};
|
|
305
|
+
// The gateway publishes context window, output cap and capabilities from
|
|
306
|
+
// its sales catalog. Passing them through is what stops the CLI from
|
|
307
|
+
// inferring them off the model name, which had deepseek-flash at 128K
|
|
308
|
+
// against an actual 1M window.
|
|
309
|
+
return (body.data ?? [])
|
|
310
|
+
.filter((m) => typeof m.id === "string" && m.id.length > 0)
|
|
311
|
+
.map((m) =>
|
|
312
|
+
gatewayModelDefaults(m.id as string, m.display_name ?? m.name, {
|
|
313
|
+
contextWindow: m.context_window,
|
|
314
|
+
maxTokens: m.max_output_tokens,
|
|
315
|
+
capabilities: m.capabilities,
|
|
316
|
+
reasoningEfforts: m.reasoning_efforts,
|
|
317
|
+
pricing: m.pricing,
|
|
318
|
+
}),
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The persisted form of a catalog (dal's models-store.json). Store entries are
|
|
324
|
+
* full models, so provider and baseUrl are filled in here and dropped again on
|
|
325
|
+
* restore: the base URL must follow DAL_API_BASE, not whatever it was when the
|
|
326
|
+
* catalog was saved. The virtual key is never part of a catalog entry (it is
|
|
327
|
+
* added per request by oauth.modifyModels), so nothing secret is written.
|
|
328
|
+
*/
|
|
329
|
+
function toStoreEntry(models: GatewayModel[], apiBase: string): ModelsStoreEntry {
|
|
330
|
+
return {
|
|
331
|
+
models: models.map(
|
|
332
|
+
(m): Model<Api> => ({ ...m, api: "openai-completions", provider: "deepailab", baseUrl: apiBase }),
|
|
333
|
+
),
|
|
334
|
+
checkedAt: Date.now(),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function isGatewayModel(value: unknown): value is GatewayModel {
|
|
339
|
+
if (typeof value !== "object" || value === null) return false;
|
|
340
|
+
const m = value as Partial<Record<keyof GatewayModel, unknown>>;
|
|
341
|
+
return (
|
|
342
|
+
typeof m.id === "string" &&
|
|
343
|
+
m.id.length > 0 &&
|
|
344
|
+
typeof m.name === "string" &&
|
|
345
|
+
typeof m.reasoning === "boolean" &&
|
|
346
|
+
Array.isArray(m.input) &&
|
|
347
|
+
typeof m.cost === "object" &&
|
|
348
|
+
m.cost !== null &&
|
|
349
|
+
typeof m.contextWindow === "number" &&
|
|
350
|
+
typeof m.maxTokens === "number"
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** The last catalog a gateway fetch returned, from any earlier dal process. */
|
|
355
|
+
function restoreCatalog(stored: Readonly<ModelsStoreEntry> | undefined): GatewayModel[] {
|
|
356
|
+
// A malformed entry is skipped rather than published: an invalid model
|
|
357
|
+
// makes the cache phase throw, and the runtime then skips the network
|
|
358
|
+
// phase, so one bad entry would block every future refresh.
|
|
359
|
+
return (stored?.models ?? []).filter(isGatewayModel).map((model) => {
|
|
360
|
+
const { provider: _provider, baseUrl: _baseUrl, ...config } = model as GatewayModel & Partial<Model<Api>>;
|
|
361
|
+
return config;
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* The model a signed-in user had selected, as a one-entry catalog. Used only
|
|
367
|
+
* when there is no catalog at all: nothing persisted yet (first run of this
|
|
368
|
+
* version, or the fetch right after /login failed) and no fetch has
|
|
369
|
+
* succeeded. Without it, one failed fetch leaves deepailab with zero models,
|
|
370
|
+
* the saved default cannot resolve, model selection falls through to another
|
|
371
|
+
* provider, and the turn dies with "No API key found".
|
|
372
|
+
*
|
|
373
|
+
* Global settings only: this runs before project trust is decided, and the
|
|
374
|
+
* id is used just to address the gateway, which rejects an id it does not
|
|
375
|
+
* sell with a 404 that after_provider_response turns into a plain message.
|
|
376
|
+
*/
|
|
377
|
+
function configuredDefaultCatalog(): GatewayModel[] {
|
|
378
|
+
const settings = SettingsManager.create(process.cwd(), getAgentDir(), { projectTrusted: false });
|
|
379
|
+
const modelId = settings.getDefaultModel();
|
|
380
|
+
if (settings.getDefaultProvider() !== "deepailab" || !modelId) return [];
|
|
381
|
+
return [gatewayModelDefaults(modelId)];
|
|
382
|
+
}
|
|
383
|
+
|
|
193
384
|
export default function (pi: ExtensionAPI) {
|
|
194
385
|
const cfg = defaultConfig();
|
|
195
386
|
// Latest credentials seen by getApiKey; lets /account reuse the OIDC token.
|
|
196
387
|
let lastCredentials: OAuthCredentials | undefined;
|
|
388
|
+
// Current catalog: the last one the gateway returned, in this process or
|
|
389
|
+
// (restored from models-store.json) in an earlier one, and when it was
|
|
390
|
+
// fetched (0: not from the gateway, i.e. empty or the configured-default
|
|
391
|
+
// stand-in). Handed back on every refresh that does not fetch, so a
|
|
392
|
+
// cache-only refresh never empties the model list.
|
|
393
|
+
let catalog: GatewayModel[] = SEED_MODELS;
|
|
394
|
+
let catalogFetchedAt = 0;
|
|
395
|
+
// A fetched catalog whose write to models-store.json was refused (its
|
|
396
|
+
// refresh had been superseded or had ended); written by the next refresh.
|
|
397
|
+
let unpersisted: ModelsStoreEntry | undefined;
|
|
398
|
+
|
|
399
|
+
const persist = async (models: GatewayModel[], publish: RefreshModelsContext["publish"]): Promise<void> => {
|
|
400
|
+
const entry = toStoreEntry(models, cfg.apiBase);
|
|
401
|
+
catalog = models;
|
|
402
|
+
catalogFetchedAt = entry.checkedAt ?? Date.now();
|
|
403
|
+
let written = false;
|
|
404
|
+
try {
|
|
405
|
+
written = await publish({ persist: entry });
|
|
406
|
+
} catch {
|
|
407
|
+
// Aborted refresh; handled like a refused publication.
|
|
408
|
+
}
|
|
409
|
+
unpersisted = written ? undefined : entry;
|
|
410
|
+
};
|
|
197
411
|
|
|
412
|
+
// The catalog is loaded through `refreshModels`, i.e. by the model runtime,
|
|
413
|
+
// not by a session_start handler. The runtime refreshes extension catalogs
|
|
414
|
+
// before anything resolves a model (agent-session-services.ts), so
|
|
415
|
+
// `--model deepailab/<id>` and `--provider deepailab` see the same list
|
|
416
|
+
// `--list-models` prints. Fetching on session_start was too late for that:
|
|
417
|
+
// main.ts resolves the CLI model flags before any session exists, so
|
|
418
|
+
// deepailab had zero models there and the resolver reported
|
|
419
|
+
// `Unknown provider "deepailab"`.
|
|
420
|
+
//
|
|
421
|
+
// The runtime calls this twice per refresh: first cache-only
|
|
422
|
+
// (allowNetwork false, `stored` = the persisted catalog), then with the
|
|
423
|
+
// network allowed. Every fetched catalog is persisted and the cache phase
|
|
424
|
+
// restores it. Before that, every process started from zero models, so one
|
|
425
|
+
// failed fetch took the whole run down, and every process waited on the
|
|
426
|
+
// gateway (5-6s per request behind a proxy) before its first model call.
|
|
427
|
+
//
|
|
428
|
+
// With the network allowed, a catalog fetched less than CATALOG_FRESH_MS
|
|
429
|
+
// ago is used as is; otherwise (or with `force`, after /login) it is
|
|
430
|
+
// fetched, and a failure throws, which the runtime reports while keeping
|
|
431
|
+
// what the cache phase published. At startup a persisted catalog is
|
|
432
|
+
// refreshed in the background, so this fetch does not delay the first
|
|
433
|
+
// model request (agent-session-services.ts, refreshExtensionCatalogs).
|
|
198
434
|
pi.registerProvider("deepailab", {
|
|
199
435
|
name: "DeepAILab",
|
|
200
436
|
baseUrl: cfg.apiBase,
|
|
201
437
|
apiKey: "$DAL_VK",
|
|
202
438
|
api: "openai-completions",
|
|
203
439
|
models: SEED_MODELS,
|
|
440
|
+
refreshModels: async ({ credential, allowNetwork, force, signal, stored, publish }) => {
|
|
441
|
+
const key = gatewayKey(credential);
|
|
442
|
+
// Logged out: nothing to restore or fetch; no model can be called anyway.
|
|
443
|
+
if (!key) return catalog;
|
|
444
|
+
if (unpersisted) {
|
|
445
|
+
try {
|
|
446
|
+
if (await publish({ persist: unpersisted })) unpersisted = undefined;
|
|
447
|
+
} catch {
|
|
448
|
+
// Aborted; kept for the next refresh.
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
// Newest wins: another dal process may have stored a newer list since
|
|
452
|
+
// this one loaded its own.
|
|
453
|
+
const storedAt = stored?.checkedAt ?? 0;
|
|
454
|
+
if (storedAt > catalogFetchedAt) {
|
|
455
|
+
const restored = restoreCatalog(stored);
|
|
456
|
+
if (restored.length > 0) {
|
|
457
|
+
catalog = restored;
|
|
458
|
+
catalogFetchedAt = storedAt;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (!allowNetwork) {
|
|
462
|
+
if (catalog.length === 0) catalog = configuredDefaultCatalog();
|
|
463
|
+
return catalog;
|
|
464
|
+
}
|
|
465
|
+
if (!force && catalogFetchedAt > 0 && Date.now() - catalogFetchedAt < CATALOG_FRESH_MS) return catalog;
|
|
466
|
+
const models = await fetchGatewayCatalog(cfg.apiBase, key, signal);
|
|
467
|
+
if (models.length > 0) await persist(models, publish);
|
|
468
|
+
return catalog;
|
|
469
|
+
},
|
|
204
470
|
oauth: {
|
|
205
471
|
name: "DeepAILab",
|
|
206
472
|
login: loginDeepAILab,
|
|
@@ -215,130 +481,23 @@ export default function (pi: ExtensionAPI) {
|
|
|
215
481
|
},
|
|
216
482
|
});
|
|
217
483
|
|
|
218
|
-
//
|
|
219
|
-
//
|
|
220
|
-
//
|
|
221
|
-
//
|
|
222
|
-
// Runs on two events, and needs both. At session_start a returning user is
|
|
223
|
-
// already logged in and gets the full list immediately; a new user is not, so
|
|
224
|
-
// the fetch is skipped and they would sit on the seed model until they
|
|
225
|
-
// restarted. provider_authenticated closes that gap — it fires the moment
|
|
226
|
-
// /login completes, before the model picker opens.
|
|
227
|
-
const refreshCatalog = async (ctx: {
|
|
228
|
-
modelRegistry: { getApiKeyForProvider(id: string): Promise<string | undefined> };
|
|
229
|
-
ui?: { notify(message: string, level: "info" | "warning" | "error"): void };
|
|
230
|
-
hasUI?: boolean;
|
|
231
|
-
}) => {
|
|
232
|
-
// The catalog is the only source of models now that the seed list is gone,
|
|
233
|
-
// so a failure has to be visible. `ui` is optional because refreshCatalog
|
|
234
|
-
// also runs in headless (`dal -p`) sessions, where stderr is the channel.
|
|
235
|
-
const reportFailure = (reason: string): void => {
|
|
236
|
-
const message = `${t("无法获取模型列表")}:${reason}`;
|
|
237
|
-
// hasUI, not `ctx.ui`. In a headless run (`dal -p`) `ui` is still a real
|
|
238
|
-
// object — runner.ts hands out a no-op UI — so the truthiness check sent
|
|
239
|
-
// every message into a function that does nothing, and the stderr branch
|
|
240
|
-
// this comment used to promise was never taken.
|
|
241
|
-
if (ctx.hasUI && ctx.ui) ctx.ui.notify(message, "error");
|
|
242
|
-
else process.stderr.write(`${message}\n`);
|
|
243
|
-
};
|
|
244
|
-
// Hoisted so the finally below can clear it: declared inside the try, an
|
|
245
|
-
// error thrown by fetch or res.json() left the 20s timer running and
|
|
246
|
-
// delayed `dal -p` exiting.
|
|
247
|
-
//
|
|
248
|
-
// The gateway answers /models in a steady ~5.6s (measured 2026-09-11:
|
|
249
|
-
// 5.64 / 5.58 / 5.66). The previous 5s deadline therefore aborted every
|
|
250
|
-
// single refresh, and the catch below swallowed it — a signed-in user saw
|
|
251
|
-
// nothing on screen to say why. The budget is now well clear of that, and
|
|
252
|
-
// still bounded so an unreachable gateway cannot hang the session.
|
|
253
|
-
const controller = new AbortController();
|
|
254
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
255
|
-
try {
|
|
256
|
-
const token = await ctx.modelRegistry.getApiKeyForProvider("deepailab");
|
|
257
|
-
if (!token) return;
|
|
258
|
-
timer = setTimeout(() => controller.abort(), 20_000);
|
|
259
|
-
const res = await fetch(`${cfg.apiBase}/models`, {
|
|
260
|
-
headers: { "x-bf-vk": token, Accept: "application/json" },
|
|
261
|
-
signal: controller.signal,
|
|
262
|
-
});
|
|
263
|
-
if (!res.ok) {
|
|
264
|
-
// 401/402/429 used to be swallowed here together with the network
|
|
265
|
-
// errors below. With the seed catalog gone a rejected refresh means
|
|
266
|
-
// zero models, so it has to reach the screen.
|
|
267
|
-
reportFailure(`${t("网关拒绝了请求")} (HTTP ${res.status})`);
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
const body = (await res.json()) as {
|
|
271
|
-
data?: Array<{
|
|
272
|
-
id?: string;
|
|
273
|
-
name?: string;
|
|
274
|
-
display_name?: string;
|
|
275
|
-
context_window?: number;
|
|
276
|
-
max_output_tokens?: number;
|
|
277
|
-
capabilities?: string[];
|
|
278
|
-
reasoning_efforts?: string[];
|
|
279
|
-
pricing?: {
|
|
280
|
-
input_per_1m?: number;
|
|
281
|
-
output_per_1m?: number;
|
|
282
|
-
cache_read_per_1m?: number;
|
|
283
|
-
cache_write_per_1m?: number;
|
|
284
|
-
currency?: string;
|
|
285
|
-
};
|
|
286
|
-
}>;
|
|
287
|
-
};
|
|
288
|
-
// Known ids keep their verified seed config; new catalog entries get
|
|
289
|
-
// per-family defaults (builtin metadata + gateway compat dialect)
|
|
290
|
-
// instead of blind conservative placeholders.
|
|
291
|
-
// The gateway publishes context window, output cap and capabilities from
|
|
292
|
-
// its sales catalog. Passing them through is what stops the CLI from
|
|
293
|
-
// inferring them off the model name, which had deepseek-flash at 128K
|
|
294
|
-
// against an actual 1M window.
|
|
295
|
-
const models = (body.data ?? [])
|
|
296
|
-
.filter((m) => typeof m.id === "string" && m.id.length > 0)
|
|
297
|
-
.map((m) =>
|
|
298
|
-
gatewayModelDefaults(m.id as string, m.display_name ?? m.name, {
|
|
299
|
-
contextWindow: m.context_window,
|
|
300
|
-
maxTokens: m.max_output_tokens,
|
|
301
|
-
capabilities: m.capabilities,
|
|
302
|
-
reasoningEfforts: m.reasoning_efforts,
|
|
303
|
-
pricing: m.pricing,
|
|
304
|
-
}),
|
|
305
|
-
);
|
|
306
|
-
if (models.length > 0) {
|
|
307
|
-
// Must be the full config: registerProvider validates the incoming
|
|
308
|
-
// object standalone (models require baseUrl + auth to be present).
|
|
309
|
-
pi.registerProvider("deepailab", {
|
|
310
|
-
name: "DeepAILab",
|
|
311
|
-
baseUrl: cfg.apiBase,
|
|
312
|
-
apiKey: "$DAL_VK",
|
|
313
|
-
api: "openai-completions",
|
|
314
|
-
models,
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
} catch (error) {
|
|
318
|
-
// There is no seed catalog any more, so this is not a soft degradation:
|
|
319
|
-
// the model picker ends up empty. Name the reason instead.
|
|
320
|
-
const aborted = error instanceof Error && error.name === "AbortError";
|
|
321
|
-
reportFailure(aborted ? t("请求超时") : t("无法连接 DeepAILab 网关"));
|
|
322
|
-
} finally {
|
|
323
|
-
if (timer !== undefined) clearTimeout(timer);
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
// Awaited, deliberately. This used to run in the background because the
|
|
328
|
-
// gateway took ~5.6s to answer, which would have been added to every launch.
|
|
329
|
-
// It now answers in well under a second, and with no seed catalog there is
|
|
330
|
-
// nothing to fall back to: a backgrounded refresh means `dal -p` reaches the
|
|
331
|
-
// model lookup before any model exists and reports "no API key found" to a
|
|
332
|
-
// user who is perfectly well logged in.
|
|
333
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
334
|
-
await refreshCatalog(ctx);
|
|
335
|
-
});
|
|
336
|
-
// After /login it is awaited on purpose. The user has just authenticated and
|
|
337
|
-
// is about to open the model picker; showing them the seed list there is the
|
|
338
|
-
// exact failure this is meant to prevent.
|
|
484
|
+
// After /login the catalog has to be fetched before the model picker opens:
|
|
485
|
+
// the login itself only refreshes from cache, and a catalog cached for the
|
|
486
|
+
// previous sign-in may still count as fresh, hence `force`. Awaited on purpose — showing
|
|
487
|
+
// the pre-login (empty) list there is the failure this prevents.
|
|
339
488
|
pi.on("provider_authenticated", async (event, ctx) => {
|
|
340
489
|
if (event.providerId !== "deepailab") return;
|
|
341
|
-
await
|
|
490
|
+
const result = await ctx.modelRegistry.refresh({
|
|
491
|
+
providers: ["deepailab"],
|
|
492
|
+
force: true,
|
|
493
|
+
signal: AbortSignal.timeout(20_000),
|
|
494
|
+
});
|
|
495
|
+
const error = result.errors.get("deepailab");
|
|
496
|
+
const message = error ? error.message : result.aborted ? `${t("无法获取模型列表")}:${t("请求超时")}` : undefined;
|
|
497
|
+
if (!message) return;
|
|
498
|
+
// hasUI, not `ctx.ui`: headless runs get a real no-op ui object.
|
|
499
|
+
if (ctx.hasUI) ctx.ui.notify(message, "error");
|
|
500
|
+
else process.stderr.write(`${message}\n`);
|
|
342
501
|
});
|
|
343
502
|
|
|
344
503
|
pi.registerCommand("account", {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@earendil-works/pi-agent-core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "General-purpose agent with transport abstraction, state management, and attachment support",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"README.md"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@earendil-works/pi-ai": "^1.0.
|
|
45
|
-
"@earendil-works/pi-telemetry": "^1.0.
|
|
44
|
+
"@earendil-works/pi-ai": "^1.0.15",
|
|
45
|
+
"@earendil-works/pi-telemetry": "^1.0.15",
|
|
46
46
|
"diff": "8.0.4",
|
|
47
47
|
"ignore": "7.0.5",
|
|
48
48
|
"typebox": "1.3.7",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"schemaVersion":3,"generatedAt":"2026-09-
|
|
1
|
+
{"schemaVersion":3,"generatedAt":"2026-09-23T06:37:35.320Z","structureHash":"a3104a3b6ccd64ed9d034abef83241e110ae96498251bc5c240e54fb1503a2e9","files":{"amazon-bedrock.json":"25a5ba4552c6cec668a3a0199caa507e13ed792bc780ca44b6555649e7ee2963","ant-ling.json":"4979fe79d99ed97382d7ce40170c6132e932e77906de1eb54464c042e7d63633","anthropic.json":"f300da1570f60817c69978b40e9502d84e1def97b9a78f6b61ad9793e8b75fba","azure-openai-responses.json":"0a188b0a8c5eba337ee87eb810c902449c0cb3cb0358510192679402ccafcd21","baseten.json":"c71a236e1fbd57bc095dd9bf1db6a9f9588a27aa6cb4f6352697ac6682b38227","cerebras.json":"9eaa554e1b02f3665f8f63e6432d253bf81351af7c72001054dd8108aa528a52","cloudflare-ai-gateway.json":"db53895cd79e8a58cfd5b7620fd1ebacc36c226b15c387c3683e8e783d07c688","cloudflare-workers-ai.json":"8ee15540a01cf79d0e4063c493ae3c436af2fc1d456f9faf1b066418247183e9","deepseek.json":"b5f9693ec641e9a957dc5f7a22042b3bb928bac3d7268406619b6f18e21c4b83","fireworks.json":"42c45cdd4778479bd252a15d80c5da2595ee4f7f553b749d8b68d60a819f4107","github-copilot.json":"735a38e450ee8e2740cfab3b9b94e8fa164072f91dcf60f56dfad1660906a647","google-vertex.json":"e1631109172dc1f7d7977570420a5bb86258bdd1088427e244a2c51545c4f24d","google.json":"7619bf03edd2a7acb06a2274a14b13c3a61354288abe029a758401d4de36a8fd","groq.json":"e4728fd72e358cd8f25e9a518e9b49a521ef44523d3debe38a7db9e0663c4077","huggingface.json":"2f93082144a7b2a1cbf79148244ce7559e0428d6310c5eb0648bca89b15a8b4b","kimi-coding.json":"202d4b9a3e3327cebb01626fb1818f92627e59800f27858902c4ce69105f0bd7","minimax-cn.json":"65d37e55a6d9585855f94b3fb7739308d107fe248ebd9e1001d1c5d4606ad2c4","minimax.json":"c7dc5765995ae77242916430929c7cc9a782589f7e483e13f7e01663190d028e","mistral.json":"be522597b22bceeb0bb9a0ba1781647649ffd19637d0c8e67b056d7b0ec33d52","moonshotai-cn.json":"63f77de8782dce6a8d4649b6075ee04c9732863ae85561e0867871220c558bde","moonshotai.json":"2a8dfb97eb905e991c22081dc09bd4a61c05b7fa9ac0d5c72d6930f601f490f1","nvidia.json":"6493e09b5db8b8fe9c8fae5bc15dc287645f37f83a4ed3a1ad1181e5830b5ff7","openai-codex.json":"9050a9eadbcebc83dd7e027ea68466e4a02dcf6fd2cf71768f666469e508a514","openai.json":"c1578cbd0ebf7c3c38108965e293c1969dd4543d24fd9c9f8718c3e287a104bc","opencode-go.json":"0a272361d1791a4174405cebaf6b0baa726f9f22ff0dc4c898b4681028637f90","opencode.json":"05cb381151c4f5187cdbebe25b346654b8d0cea71733de1ba8fdbeb94ea81cbc","openrouter.json":"0790067e794238df46dcdc8aa33744f85722b88951c39f8f464218ad03aab1da","qwen-token-plan-cn.json":"c6bf63501837149d212795f26da3c492472bb79062b350f8a51f4d8dc7127ed3","qwen-token-plan-individual.json":"eb040e78595853e980011308b1bf9caacae6dc8a91abbac5ffebd402515dd2a6","qwen-token-plan.json":"d72a4a85dd473bbabb380add830f280442ac3bdc00cb47b7a44a5bdd4e1952ed","together.json":"927c707d06f2236cdab13f14c870fb58bce0614ba7287bbcd57cc9fd66d191ed","vercel-ai-gateway.json":"e8bea0ebcc1084f67eaa8dfb47027969e6d24d869852be357a45463866288b4e","xai.json":"307858b392caa8a4d88a83b9f8aca0db66e6a2cae022cc2b8d38ca9eed3c7453","xiaomi-token-plan-ams.json":"bc8be999579ca408d405ae398bb5a074808131d16f568d8df61ec62d19063b49","xiaomi-token-plan-cn.json":"e01223a54c913121a4fb81bfb9940cc1f4e94165f8e89eea2069b26335090029","xiaomi-token-plan-sgp.json":"e2e28b3999d1db4d9a9d3c1d148a16a9824a76711b68e5321da72feafc4cab3c","xiaomi.json":"6472efd618d7dacdbac5fda5d19a2154eabf8e045c0bc8aa3b5be574d66bbe32","zai-coding-cn.json":"69c572aa75104f16ccbddbaace60c267f4bcc74f86d34891ca43110d312fb1ad","zai.json":"15c8182a88e58bdc1b1c3eb0603c690475342c8a826ceddfe9af1b36b28345ee"}}
|