UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

120 lines (119 loc) • 3.71 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { l as requiresClaudeMandatoryAdaptiveThinking } from "./anthropic-D3Mx6AZz.mjs"; import { a as resolveWindowsSpawnProgram, r as materializeWindowsSpawnProgram } from "./windows-spawn-BcHMKZuG.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import "./claude-model-runtime-B1ygZ5pC.mjs"; import { r as CLAUDE_CLI_CLEAR_ENV } from "./cli-constants-CDevEMvo.mjs"; import { t as createClaudeCliTransport } from "./cli-transport-Dkvuc_UC.mjs"; import { addAbortListener } from "node:events"; //#region extensions/anthropic/cli-model-discovery.ts const INITIALIZE_TIMEOUT_MS = 3e4; const NATIVE_EFFORT_LEVELS = [ "low", "medium", "high", "xhigh", "max" ]; /** Discover the native menu without submitting a user message or starting a turn. */ async function discoverClaudeCliModels(e) { e.signal?.throwIfAborted(); const t = Object.fromEntries(Object.entries(e.env).filter((e) => e[1] !== void 0)); for (const e of CLAUDE_CLI_CLEAR_ENV) delete t[e]; const n = resolveWindowsSpawnProgram({ command: "claude", env: t, packageName: "@anthropic-ai/claude-code" }); const r = materializeWindowsSpawnProgram(n, [ "--print", "--input-format", "stream-json", "--output-format", "stream-json", "--verbose", "--permission-prompt-tool", "stdio", "--setting-sources", "user", "--settings", "{\"disableAllHooks\":true,\"enabledPlugins\":{}}", "--strict-mcp-config", "--mcp-config", "{\"mcpServers\":{}}", "--tools", "" ]); const i = new AbortController(); const a = setTimeout(() => i.abort(/* @__PURE__ */ new Error("Claude CLI model discovery timed out.")), INITIALIZE_TIMEOUT_MS); a.unref(); const o = e.signal ? AbortSignal.any([e.signal, i.signal]) : i.signal; const s = { command: r.command, args: r.argv, cwd: t.HOME ?? process.cwd(), env: t, abortSignal: o, assertCurrent: () => o.throwIfAborted() }; let c; let l; try { c = await createClaudeCliTransport({ context: s, args: r.argv, currentContext: () => o.aborted ? void 0 : s, initialize: { hooks: {} }, onMessage: async () => {}, onRequest: async () => { throw new Error("Model discovery cannot authorize native requests."); }, onError: () => {} }); l = addAbortListener(o, () => c?.close()); const e = await c.initialize(); o.throwIfAborted(); if (!Array.isArray(e.models)) throw new Error("Claude CLI initialization did not return a model menu."); const t = /* @__PURE__ */ new Map(); const n = /* @__PURE__ */ new Set(); for (const r of e.models) { if (!isRecord(r) || typeof r.resolvedModel !== "string" || !r.resolvedModel.trim()) continue; const e = r.resolvedModel.trim(); const i = typeof r.displayName === "string" ? r.displayName : e; const a = t.get(e); if (a) { if (r.value !== "default" && n.delete(e)) a.name = i; continue; } if (r.value === "default") n.add(e); const o = Array.isArray(r.supportedEffortLevels) ? r.supportedEffortLevels : []; const s = NATIVE_EFFORT_LEVELS.filter((e) => o.includes(e)); t.set(e, { id: e, name: i, reasoning: s.length > 0, input: ["text", "image"], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, maxTokens: 128e3, thinkingLevelMap: { off: requiresClaudeMandatoryAdaptiveThinking({ id: e }) ? null : "off", minimal: null, ...Object.fromEntries(NATIVE_EFFORT_LEVELS.map((e) => [e, s.includes(e) ? e : null])) } }); } return [...t.values()]; } finally { clearTimeout(a); l?.[Symbol.dispose](); c?.close(); await c?.waitForExit(); } } //#endregion export { discoverClaudeCliModels as t };