refactor(models): share auth helpers and forward-compat list fallbacks

This commit is contained in:
Peter Steinberger
2026-02-14 01:07:19 +01:00
parent 363a56ab87
commit cf2524b8b9
8 changed files with 175 additions and 233 deletions

View File

@@ -1,14 +1,16 @@
import type { Api, Model } from "@mariozechner/pi-ai";
import type { ModelRegistry } from "../../agents/pi-model-discovery.js";
import type { RuntimeEnv } from "../../runtime.js";
import type { ModelRow } from "./list.types.js";
import { ensureAuthProfileStore } from "../../agents/auth-profiles.js";
import { resolveForwardCompatModel } from "../../agents/model-forward-compat.js";
import { parseModelRef } from "../../agents/model-selection.js";
import { loadConfig } from "../../config/config.js";
import { resolveConfiguredEntries } from "./list.configured.js";
import { formatErrorWithStack } from "./list.errors.js";
import { loadModelRegistry, toModelRow } from "./list.registry.js";
import { printModelTable } from "./list.table.js";
import { DEFAULT_PROVIDER, ensureFlagCompatibility, modelKey } from "./shared.js";
import { DEFAULT_PROVIDER, ensureFlagCompatibility, isLocalBaseUrl, modelKey } from "./shared.js";
export async function modelsListCommand(
opts: {
@@ -33,10 +35,12 @@ export async function modelsListCommand(
})();
let models: Model<Api>[] = [];
let modelRegistry: ModelRegistry | undefined;
let availableKeys: Set<string> | undefined;
let availabilityErrorMessage: string | undefined;
try {
const loaded = await loadModelRegistry(cfg);
modelRegistry = loaded.registry;
models = loaded.models;
availableKeys = loaded.availableKeys;
availabilityErrorMessage = loaded.availabilityErrorMessage;
@@ -58,22 +62,6 @@ export async function modelsListCommand(
const rows: ModelRow[] = [];
const isLocalBaseUrl = (baseUrl: string) => {
try {
const url = new URL(baseUrl);
const host = url.hostname.toLowerCase();
return (
host === "localhost" ||
host === "127.0.0.1" ||
host === "0.0.0.0" ||
host === "::1" ||
host.endsWith(".local")
);
} catch {
return false;
}
};
if (opts.all) {
const sorted = [...models].toSorted((a, b) => {
const p = a.provider.localeCompare(b.provider);
@@ -109,7 +97,18 @@ export async function modelsListCommand(
if (providerFilter && entry.ref.provider.toLowerCase() !== providerFilter) {
continue;
}
const model = modelByKey.get(entry.key);
let model = modelByKey.get(entry.key);
if (!model && modelRegistry) {
const forwardCompat = resolveForwardCompatModel(
entry.ref.provider,
entry.ref.model,
modelRegistry,
);
if (forwardCompat) {
model = forwardCompat;
modelByKey.set(entry.key, forwardCompat);
}
}
if (opts.local && model && !isLocalBaseUrl(model.baseUrl)) {
continue;
}