






















@@ -1,11 +1,13 @@
11import { randomBytes, randomUUID } from "node:crypto";
2+import { writeSync } from "node:fs";
23import fs from "node:fs/promises";
34import { createServer } from "node:net";
45import os from "node:os";
56import path from "node:path";
67import {
78clampThinkingLevel,
89type Api,
10+getModels,
911type Model,
1012type ModelThinkingLevel,
1113} from "@mariozechner/pi-ai";
@@ -279,7 +281,7 @@ async function withGatewayLiveModelTimeout<T>(operation: Promise<T>, context: st
279281}
280282281283function logProgress(message: string): void {
282-process.stderr.write(`[live] ${message}\n`);
284+writeSync(2, `[live] ${message}\n`);
283285}
284286285287function enterProductionEnvForLiveRun() {
@@ -1408,6 +1410,43 @@ type LiveModelRegistry = {
14081410getAll(): Array<Model<Api>>;
14091411};
141014121413+function loadProviderScopedBuiltInModels(providerList: readonly string[]): Array<Model<Api>> {
1414+const models: Array<Model<Api>> = [];
1415+const seen = new Set<string>();
1416+for (const rawProvider of providerList) {
1417+const provider = normalizeProviderId(rawProvider);
1418+if (!provider) {
1419+continue;
1420+}
1421+for (const model of getModels(provider)) {
1422+const key = `${normalizeProviderId(model.provider)}/${model.id.toLowerCase()}`;
1423+if (seen.has(key)) {
1424+continue;
1425+}
1426+seen.add(key);
1427+models.push(model);
1428+}
1429+}
1430+return models;
1431+}
1432+1433+function createStaticLiveModelRegistry(models: Array<Model<Api>>): LiveModelRegistry {
1434+return {
1435+find(provider, modelId) {
1436+const normalizedProvider = normalizeProviderId(provider);
1437+const normalizedModelId = modelId.toLowerCase();
1438+return models.find(
1439+(model) =>
1440+normalizeProviderId(model.provider) === normalizedProvider &&
1441+model.id.toLowerCase() === normalizedModelId,
1442+);
1443+},
1444+getAll() {
1445+return models;
1446+},
1447+};
1448+}
1449+14111450function parseExplicitLiveModelRef(
14121451raw: string,
14131452providerFilter: Set<string> | null,
@@ -2410,45 +2449,54 @@ describeLive("gateway live (dev agent, profile keys)", () => {
24102449);
2411245024122451const agentDir = resolveDefaultAgentDir(cfg);
2413-logProgress("[all-models] loading auth profiles");
2414-const authProfileStore = await withGatewayLiveSetupTimeout(
2415-Promise.resolve().then(() =>
2416-providerList
2417- ? ensureAuthProfileStoreWithoutExternalProfiles(agentDir, {
2418-allowKeychainPrompt: false,
2419-})
2420- : ensureAuthProfileStore(agentDir, {
2421-allowKeychainPrompt: false,
2422-}),
2423-),
2424-"[all-models] load auth profiles",
2425-);
2426-const authStorage = await withGatewayLiveSetupTimeout(
2427-Promise.resolve().then(() =>
2428-discoverAuthStorage(agentDir, {
2429-config: cfg,
2430-env: process.env,
2431- ...(providerList
2432- ? {
2433-skipExternalAuthProfiles: true,
2434-syntheticAuthProviderRefs: [],
2435-}
2436- : {}),
2437-}),
2438-),
2439-"[all-models] load auth storage",
2440-);
2441-logProgress("[all-models] loading model registry");
2442-const modelRegistry = discoverModels(authStorage, agentDir);
2443-const all = await withGatewayLiveSetupTimeout(
2444-Promise.resolve().then(() => modelRegistry.getAll()),
2445-"[all-models] load model registry",
2446-);
2447-24482452const rawModels = process.env.OPENCLAW_LIVE_GATEWAY_MODELS?.trim();
24492453const useModern = !rawModels || rawModels === "modern" || rawModels === "all";
24502454const useExplicit = Boolean(rawModels) && !useModern;
24512455const filter = useExplicit ? parseFilter(rawModels) : null;
2456+const useProviderScopedBuiltIns = providerList !== null && !useExplicit;
2457+let authProfileStore: AuthProfileStore | undefined;
2458+let modelRegistry: LiveModelRegistry;
2459+let all: Array<Model<Api>>;
2460+if (useProviderScopedBuiltIns) {
2461+logProgress("[all-models] loading provider-scoped model refs");
2462+all = loadProviderScopedBuiltInModels(providerList);
2463+modelRegistry = createStaticLiveModelRegistry(all);
2464+} else {
2465+logProgress("[all-models] loading auth profiles");
2466+authProfileStore = await withGatewayLiveSetupTimeout(
2467+Promise.resolve().then(() =>
2468+providerList
2469+ ? ensureAuthProfileStoreWithoutExternalProfiles(agentDir, {
2470+allowKeychainPrompt: false,
2471+})
2472+ : ensureAuthProfileStore(agentDir, {
2473+allowKeychainPrompt: false,
2474+}),
2475+),
2476+"[all-models] load auth profiles",
2477+);
2478+const authStorage = await withGatewayLiveSetupTimeout(
2479+Promise.resolve().then(() =>
2480+discoverAuthStorage(agentDir, {
2481+config: cfg,
2482+env: process.env,
2483+ ...(providerList
2484+ ? {
2485+skipExternalAuthProfiles: true,
2486+syntheticAuthProviderRefs: [],
2487+}
2488+ : {}),
2489+}),
2490+),
2491+"[all-models] load auth storage",
2492+);
2493+logProgress("[all-models] loading model registry");
2494+modelRegistry = discoverModels(authStorage, agentDir);
2495+all = await withGatewayLiveSetupTimeout(
2496+Promise.resolve().then(() => modelRegistry.getAll()),
2497+"[all-models] load model registry",
2498+);
2499+}
24522500const maxModels = GATEWAY_LIVE_MAX_MODELS;
24532501const targetMatcher = createLiveTargetMatcher({
24542502providerFilter: PROVIDERS,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。