UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

190 lines (189 loc) • 7.9 kB
import { o as writeRuntimeJson, s as writeRuntimeStdout } from "./runtime-B-SLYP-m.mjs"; import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs"; import "./model-ref-shared-DqE2RqZv.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { r as modelKey } from "./provider-registry-index-DhPtwFh_.mjs"; import { t as captureRuntimeConfigAsyncReader } from "./io.runtime-DsJ2dLCQ.mjs"; import "./config-CMA-J084.mjs"; import { r as theme, t as colorize } from "./theme-BOI0xyRZ.mjs"; import { t as ExpectedCliError } from "./failure-output-DVVDqwuu.mjs"; import { n as requestExitAfterOneShotOutput } from "./one-shot-exit-DkbGSQD5.mjs"; import { a as stopProxy, i as startProxy } from "./proxy-lifecycle-78PDzQG7.mjs"; import { t as runWithLocalStateOwner } from "./local-state-owner-DTl7OHpC.mjs"; import { _ as truncate, g as padTerminalCell, h as isRich, l as resolveModelsTargetAgent, n as ensureFlagCompatibility, p as formatTokenK } from "./shared-jZuoMzdG.mjs"; import { r as readSelectedGatewayModelCatalog, t as getModelCatalogWarnings } from "./catalog-warnings-DifFmrGP.mjs"; import { t as loadModelsConfig } from "./load-config-CTxoGQxg.mjs"; //#region src/commands/models/list.list-command.ts /** Reads the selected Gateway catalog or an explicitly identified local published view. */ const MODEL_CATALOG_REFRESH_TIMEOUT_MS = 21e4; const MODEL_PAD = 42; const INPUT_PAD = 10; const CTX_PAD = 11; const STATE_PAD = 5; async function modelsListCommand(e, t) { ensureFlagCompatibility(e); const n = e.provider?.trim(); if (n && /\s/u.test(n)) { const e = `Invalid provider filter "${sanitizeTerminalText(n)}". Use a provider id such as "moonshot", not a display label.`; throw new ExpectedCliError({ message: e, humanOutput: e, machineOutput: e }); } const r = n ? normalizeProviderId(n) : void 0; const i = await captureRuntimeConfigAsyncReader({ skipPluginValidation: true })(); const a = { ...e.agent?.trim() ? { agentId: e.agent.trim() } : {}, view: e.all || r ? "all" : "default", ...r ? { provider: r } : {}, includeDetails: true, ...e.refresh ? { refresh: true } : {} }; let o = await readSelectedGatewayModelCatalog(i, a, e.refresh ? MODEL_CATALOG_REFRESH_TIMEOUT_MS : void 0); if (!o) { t.error(e.refresh ? "Gateway is not running. Refreshing the local model catalog." : "Gateway is not running. Showing the local cached model catalog. Use --refresh to discover provider models."); const [{ resolvePublishedModelCatalogOwner: n }, { withPreparedModelCatalogOwner: r }, { getPreparedModelRuntimeAuthMaterializations: i }, { buildModelsListResult: s }] = await Promise.all([ import("./prepared-model-catalog-owner-Zf_jCjE7.mjs"), import("./prepared-model-catalog-guV7T9CV.mjs"), import("./prepared-model-runtime-auth-BUHEJm6o.mjs"), import("./models-list-result-CrCsj4M9.mjs") ]); const readLocal = async (o) => { const c = await loadModelsConfig({ commandName: "models list", runtime: t }); const { agentId: l, agentDir: u } = resolveModelsTargetAgent(c, e.agent, { kind: "read" }); if (o) { const [{ prepareOpenClawAgentDatabaseExecution: e }, { resolveAuthProfileDatabasePath: t }, { prepareLocalCapabilityAccountSecrets: n }] = await Promise.all([ import("./openclaw-agent-execution-D8fPsYYC.mjs"), import("./sqlite-zfPAUB0i.mjs"), import("./local-account-secrets-B8YxDtK0.mjs") ]); await e({ agentId: l, path: t(u), env: o.env }, o.assertCurrent, o.signal); await n({ cfg: c, agentId: l }); o.assertCurrent(); } const d = e.refresh ? await startProxy(c.proxy) : null; try { return await r({ agentId: l, agentDir: u, config: c, readOnly: e.refresh !== true, ...e.refresh ? { refreshFullCatalog: true, persistOfflineRefresh: true } : {} }, async (e) => { const t = n(e); return await s({ source: { kind: "published", owner: { ...t, authMaterializations: i(e) } }, agentId: l, params: a }); }); } finally { await stopProxy(d); } }; o = e.refresh ? await runWithLocalStateOwner({ method: "models.list", params: a, target: "local model catalog", onForeignOwner: "refuse", runLocal: async (e) => { const t = await readLocal(e); e.assertCurrent(); return t; } }) : await readLocal(); } for (const e of getModelCatalogWarnings(o)) t.error(`${e.message} ${e.fixHint}`); if (o.refreshFailed) t.error("Model discovery could not refresh all providers. Showing the available published model list."); for (const e of o.providerOutcomes ?? []) { if (e.status === "ready") continue; const n = `${sanitizeTerminalText(e.provider)}${e.profileId ? ` (profile ${sanitizeTerminalText(e.profileId)})` : ""}`; t.error(e.status === "auth-rejected" ? `Model discovery authentication was rejected for ${n}. Open Models in the Control UI to check sign-in and catalog access, then retry with --refresh.` : `Model discovery is unavailable for ${n}. Retry with --refresh; if it still fails, check the provider in Models in the Control UI.`); } const s = o.models.filter((t) => !e.local || t.local === true).map((e) => ({ key: modelKey(e.provider, e.id), name: e.name, input: e.input?.join("+") || "-", contextWindow: e.contextWindow ?? null, contextTokens: e.contextTokens, local: e.local ?? null, available: e.available ?? null, tags: [.../* @__PURE__ */ new Set([...e.tags ?? [], ...e.alias ? [`alias:${e.alias}`] : []])] })); if (s.length === 0 && !e.json && !e.plain) t.log("No models found."); else if (e.json) { const e = o.providerOutcomes?.map(({ provider: e, profileId: t, status: n, unlistedConfiguredModelIds: r }) => ({ provider: e, profileId: t || void 0, status: n, unlistedConfiguredModelIds: n === "ready" && r?.length ? r : void 0 })); writeRuntimeJson(t, { count: s.length, models: s, ...e?.length ? { providerOutcomes: e } : {} }); } else if (e.plain) for (const e of s) writeRuntimeStdout(t, sanitizeTerminalText(e.key)); else { const n = isRich(e); const r = n ? (e) => { const t = sanitizeTerminalText(e); switch (t) { case "default": return theme.success(t); case "image": return theme.accentBright(t); case "configured": return theme.accent(t); case "missing": return theme.error(t); } if (t.startsWith("fallback#") || t.startsWith("img-fallback#")) return theme.warn(t); if (t.startsWith("alias:")) return theme.accentDim(t); return theme.muted(t); } : sanitizeTerminalText; const formatState = (e, t = theme.muted) => colorize(n, e === null ? theme.muted : e ? theme.success : t, padTerminalCell(e === null ? "-" : e ? "yes" : "no", STATE_PAD)); const i = [ padTerminalCell("Model", MODEL_PAD), padTerminalCell("Input", INPUT_PAD), padTerminalCell("Ctx", CTX_PAD), padTerminalCell("Local", STATE_PAD), padTerminalCell("Auth", STATE_PAD), "Tags" ].join(" "); t.log(n ? theme.heading(i) : i); for (const e of s) { const i = padTerminalCell(truncate(e.key, MODEL_PAD), MODEL_PAD); const a = colorize(n, e.input.includes("image") ? theme.accentBright : theme.info, padTerminalCell(sanitizeTerminalText(e.input) || "-", INPUT_PAD)); const o = typeof e.contextTokens === "number" && Number.isFinite(e.contextTokens) && e.contextTokens > 0 && e.contextTokens !== e.contextWindow ? `${formatTokenK(e.contextTokens)}/${formatTokenK(e.contextWindow)}` : formatTokenK(e.contextWindow); const s = [ n ? theme.accent(i) : i, a, padTerminalCell(o, CTX_PAD), formatState(e.local), formatState(e.available, theme.error), e.tags.map(r).join(",") ].join(" "); t.log(s); } } requestExitAfterOneShotOutput(t); } //#endregion export { modelsListCommand };