UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

567 lines (566 loc) • 19.8 kB
import { A as OPENAI_GPT_61_SOL_MODEL_ID, C as OPENAI_GPT_54_NANO_MODEL_ID, D as OPENAI_GPT_56_MODEL_ID, E as OPENAI_GPT_55_PRO_MODEL_ID, F as isOpenAIPlatformOnlyRouteModelId, M as OPENAI_GPT_6_MODEL_IDS, O as OPENAI_GPT_56_SOL_MODEL_ID, R as resolveOpenAICodexReasoningEfforts, S as OPENAI_GPT_54_MODEL_ID, T as OPENAI_GPT_55_MODEL_ID, _ as OPENAI_DEFAULT_RUNTIME_CONTEXT_TOKENS, a as isOpenAIApiBaseUrl, c as resolveOpenAIDefaultBaseUrl, f as projectOpenAIAccountModels, g as OPENAI_DAYBREAK_MODEL_IDS, h as OPENAI_CODEX_GPT_61_SOL_CONTEXT_TOKENS, k as OPENAI_GPT_56_VARIANT_MODEL_IDS, m as OPENAI_CHAT_LATEST_MODEL_ID, n as OPENAI_API_BASE_URL, o as isOpenAICodexBaseUrl, r as OPENAI_CODEX_RESPONSES_BASE_URL, s as resolveOpenAICodexModelsEndpoint, t as modelCatalog, u as OPENAI_UNKNOWN_MODEL_COST, w as OPENAI_GPT_54_PRO_MODEL_ID, x as OPENAI_GPT_54_MINI_MODEL_ID } from "./openclaw.plugin-DQsQ0kf1.mjs"; import { c as TOKEN_SHARING_RESOURCE, u as isSIWCAuthFlow } from "./provider-metadata-h9vYLGeZ.mjs"; import { isNonSecretApiKeyMarker } from "openclaw/plugin-sdk/provider-auth-markers"; import { DEFAULT_CONTEXT_TOKENS, buildManifestModelProviderConfig, findCatalogTemplate, matchesExactOrPrefix, normalizeProviderId } from "openclaw/plugin-sdk/provider-model-metadata"; import { asOptionalRecord, normalizeLowercaseStringOrEmpty, normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime"; //#region extensions/openai/codex-model-rows.ts const OPENAI_CODEX_GPT_56_CONTEXT_WINDOW = 372e3; function readCodexReasoningLevels(e) { const t = asOptionalRecord(e); const n = t?.supported_reasoning_levels ?? t?.supportedReasoningLevels; if (!Array.isArray(n)) return; return n.flatMap((e) => { if (typeof e === "string" && e.trim().length > 0) return [e.trim()]; const t = asOptionalRecord(e)?.effort; return typeof t === "string" && t.trim().length > 0 ? [t.trim()] : []; }); } function readCodexModelRows(e) { const t = asOptionalRecord(e)?.models; if (!Array.isArray(t)) throw new Error("OpenAI Codex model discovery response must be { models: [] }"); return t; } function shouldIncludeCodexModelRow(e, t) { const { readLiveModelCatalogStringField: n, readLiveModelCatalogBooleanField: r } = t; const i = normalizeLowercaseStringOrEmpty(n(e, "visibility") ?? ""); if (i && i !== "list") return false; return (r(e, "show_in_picker") ?? r(e, "showInPicker")) !== false; } function resolveCodexModelInput(e, t) { const n = asOptionalRecord(e); const r = [n?.input_modalities, n?.inputModalities].find(Array.isArray)?.filter((e) => typeof e === "string") ?? []; if (r.length === 0) return t?.input ?? ["text", "image"]; const i = new Set(r.map((e) => normalizeLowercaseStringOrEmpty(e))); const a = [ "text", "image", "audio", "video" ].filter((e) => i.has(e) || e === "image" && i.has("vision")); return a.length > 0 ? a : t?.input ?? ["text", "image"]; } function normalizeOpenAICodexCatalogModel(e) { const t = normalizeLowercaseStringOrEmpty(e.id); if (OPENAI_GPT_56_VARIANT_MODEL_IDS.some((e) => e === t)) { const n = resolveOpenAICodexReasoningEfforts(t, e.compat?.supportedReasoningEfforts?.filter((e) => e !== "none")); return { ...e, contextWindow: OPENAI_CODEX_GPT_56_CONTEXT_WINDOW, contextTokens: OPENAI_DEFAULT_RUNTIME_CONTEXT_TOKENS, thinkingLevelMap: { ...e.thinkingLevelMap, off: null }, ...e.compat ? { compat: { ...e.compat, ...n ? { supportedReasoningEfforts: n } : {} } } : {} }; } return e; } //#endregion //#region extensions/openai/model-service-tiers.ts /** Only authenticated remote rows may supply this private observation, never fallback seeds. */ function readOpenAICodexServiceTiers(e) { return e.flatMap((e) => { const t = asOptionalRecord(e); const n = normalizeOptionalString(t?.slug) ?? normalizeOptionalString(t?.id); const r = t?.service_tiers; if (!n || !Array.isArray(r)) return []; const i = []; for (const e of r) { const t = normalizeOptionalString(asOptionalRecord(e)?.id); if (!t) return []; i.push(t); } return [{ modelId: n, runtimeId: "codex", api: "openai-chatgpt-responses", baseUrl: OPENAI_CODEX_RESPONSES_BASE_URL, serviceTiers: [...new Set(i)] }]; }); } /** Only explicit caller-specific metadata grants a program, never a model name or plan. */ function readOpenAIModelAccessPrograms(e, t) { return e.flatMap((e) => { const n = asOptionalRecord(e); const r = normalizeOptionalString(n?.slug) ?? normalizeOptionalString(n?.id); const i = asOptionalRecord(n?.available_access_programs)?.cyber; if (!r || !Array.isArray(i) || !i.every((e) => typeof e === "string")) return []; const a = i.filter((e) => e === "standard" || e === "daybreak_blue" || e === "daybreak_red"); return t.runtimeIds.map((e) => ({ modelId: r, runtimeId: e, api: t.api, baseUrl: t.baseUrl, cyber: a })); }); } /** * A successful account-scoped listing is authoritative even when every row is hidden; static * hints must not invent subscription access. Hidden rows stay listed: the account is entitled. */ function buildOpenAICodexReadyOutcome(e) { const t = readOpenAICodexServiceTiers(e); const n = readOpenAIModelAccessPrograms(e, { api: "openai-chatgpt-responses", baseUrl: OPENAI_CODEX_RESPONSES_BASE_URL, runtimeIds: ["openclaw", "codex"] }); return { provider: "openai", status: "ready", ...t.length ? { modelServiceTiers: t } : {}, ...n.length ? { modelAccessPrograms: n } : {}, listedModelIds: e.flatMap((e) => { const t = asOptionalRecord(e); return normalizeOptionalString(t?.slug) ?? normalizeOptionalString(t?.id) ?? []; }) }; } function projectOpenAICatalog(e, t) { const n = t?.trim(); return { providers: { openai: e.provider }, ...e.outcome ? { outcomes: [n ? { ...e.outcome, profileId: n } : e.outcome] } : {} }; } //#endregion //#region extensions/openai/platform-access-programs.ts /** * Codex uses this same read-only endpoint with Platform API keys. Public /v1/models * lists models, not program grants; keep that endpoint as the inventory owner. */ async function discoverOpenAIPlatformAccessPrograms(e) { const { getCachedLiveCatalogValue: t } = await import("openclaw/plugin-sdk/provider-catalog-shared"); const { fetchLiveProviderModelRows: n } = await import("openclaw/plugin-sdk/provider-catalog-live-runtime"); try { const r = await resolveOpenAICodexModelsEndpoint(); return await t({ keyParts: [ "openai", "platform-access-programs", r, e.apiKey ], ttlMs: 6e4, load: async (t) => { const i = await n({ providerId: "openai", endpoint: r, discoveryApiKey: e.apiKey, signal: t, auditContext: "openai-access-program-discovery", readRows: readCodexModelRows }); return readOpenAIModelAccessPrograms(i, { api: "openai-responses", baseUrl: OPENAI_API_BASE_URL, runtimeIds: ["openclaw", "codex"] }); } }); } catch { return; } } //#endregion //#region extensions/openai/model-catalog-config.ts const PROVIDER_ID$1 = "openai"; const OPENAI_MODELS_ENDPOINT = "https://api.openai.com/v1/models"; const OPENAI_MODELS_CACHE_TTL_MS = 6e4; const OPENAI_GPT_54_MAX_TOKENS = 128e3; const OPENAI_CHAT_LATEST_COST = { input: 5, output: 30, cacheRead: .5, cacheWrite: 0 }; const OPENAI_MANIFEST_PROVIDER = buildManifestModelProviderConfig({ providerId: PROVIDER_ID$1, catalog: modelCatalog.providers.openai }); function buildOpenAIManifestModelsForBaseUrl(e) { return OPENAI_MANIFEST_PROVIDER.models.map((t) => t.api === "openai-chatgpt-responses" || isOpenAICodexBaseUrl(t.baseUrl) ? Object.assign({}, t) : Object.assign({}, t, { api: t.api ?? OPENAI_MANIFEST_PROVIDER.api ?? "openai-responses", baseUrl: e })); } function buildOpenAIStaticPlatformProviderConfig(e, t = resolveOpenAIDefaultBaseUrl()) { return { baseUrl: t, api: "openai-responses", ...e ? { apiKey: e } : {}, models: buildOpenAIManifestModelsForBaseUrl(t) }; } async function buildOpenAILiveProviderConfig(e) { const t = e.baseUrl; const n = buildOpenAIStaticPlatformProviderConfig(e.apiKey, t); if (!isOpenAIApiBaseUrl(t)) return { provider: n }; const { buildLiveModelProviderConfig: r, buildOpenAICompatibleLiveModels: i, readLiveModelCatalogStringField: a, LiveModelCatalogHttpError: o } = await import("openclaw/plugin-sdk/provider-catalog-live-runtime"); const s = e.apiKey && !e.discoveryApiKey && isNonSecretApiKeyMarker(e.apiKey) ? "catalog" : void 0; const { models: c, ...l } = n; const u = [...c, { id: OPENAI_CHAT_LATEST_MODEL_ID, name: "Chat Latest", reasoning: false, cost: OPENAI_CHAT_LATEST_COST, contextWindow: 4e5, api: "openai-responses", baseUrl: t, input: ["text", "image"], maxTokens: OPENAI_GPT_54_MAX_TOKENS }]; try { let n = []; const o = await r({ discoveryMode: "strict", providerId: PROVIDER_ID$1, endpoint: OPENAI_MODELS_ENDPOINT, providerConfig: l, models: u, apiKey: e.apiKey, discoveryApiKey: e.discoveryApiKey, ttlMs: OPENAI_MODELS_CACHE_TTL_MS, auditContext: "openai-model-discovery", projectRows: (e, t) => { n = e.flatMap((e) => { const t = a(e, "id"); return t ? [t] : []; }); return i(e.filter((e) => { const t = asOptionalRecord(e); return t && (t.object === void 0 || t.object === "model") && a(e, "id") !== void 0; }), t); } }); const s = new Set(o.models.map((e) => e.id)); const c = (await discoverOpenAIPlatformAccessPrograms({ apiKey: e.discoveryApiKey ?? e.apiKey }))?.filter((e) => s.has(e.modelId)); return { provider: { ...o, models: projectOpenAIAccountModels({ listedModels: o.models, catalogModels: u, baseUrl: t }) }, outcome: { provider: PROVIDER_ID$1, status: "ready", ...c?.length ? { modelAccessPrograms: c } : {}, modelIdCatalog: { baseUrl: t, modelIds: n } } }; } catch (e) { if (e instanceof o && (e.status === 401 || e.status === 403)) return { provider: { ...n, models: [] }, outcome: { provider: PROVIDER_ID$1, ...s ? { rejectionScope: s } : {}, status: "auth-rejected" } }; return { provider: n, outcome: { provider: PROVIDER_ID$1, status: "unavailable" } }; } } //#endregion //#region extensions/openai/openai-provider-catalog.ts const PROVIDER_ID = "openai"; const OPENAI_CODEX_MODELS_CACHE_TTL_MS = 6e4; const OPENAI_CATALOG_AUGMENTATIONS = [ { id: OPENAI_GPT_55_PRO_MODEL_ID, templateIds: [OPENAI_GPT_54_PRO_MODEL_ID, OPENAI_GPT_54_MODEL_ID], contextWindow: 105e4, contextTokens: OPENAI_DEFAULT_RUNTIME_CONTEXT_TOKENS }, { id: OPENAI_GPT_54_MODEL_ID, templateIds: [OPENAI_GPT_54_MODEL_ID, OPENAI_GPT_55_MODEL_ID], contextWindow: 105e4 }, { id: OPENAI_GPT_54_PRO_MODEL_ID, templateIds: [OPENAI_GPT_54_PRO_MODEL_ID, OPENAI_GPT_55_PRO_MODEL_ID], contextWindow: 105e4 }, { id: OPENAI_GPT_54_MINI_MODEL_ID, templateIds: [OPENAI_GPT_54_MINI_MODEL_ID, "gpt-5-mini"], contextWindow: 4e5 }, { id: OPENAI_GPT_54_NANO_MODEL_ID, templateIds: [ OPENAI_GPT_54_NANO_MODEL_ID, "gpt-5-nano", "gpt-5-mini" ], contextWindow: 4e5 } ]; function resolveCodexModelFallback(e) { const t = OPENAI_MANIFEST_PROVIDER.models.find((t) => normalizeLowercaseStringOrEmpty(t.id) === normalizeLowercaseStringOrEmpty(e)); if (t?.id === "gpt-6.1-sol") return { ...t, contextTokens: OPENAI_CODEX_GPT_61_SOL_CONTEXT_TOKENS }; return t ? normalizeOpenAICodexCatalogModel(t) : void 0; } function buildOpenAICodexModelFromLiveRow(e, t) { const { readLiveModelCatalogStringField: n, readLiveModelCatalogPositiveSafeIntegerField: r } = t; if (!shouldIncludeCodexModelRow(e, t)) return; const i = n(e, "slug") ?? n(e, "id"); if (!i) return; if (isOpenAIPlatformOnlyRouteModelId(i)) return; const a = normalizeLowercaseStringOrEmpty(i); const o = resolveCodexModelFallback(i); const s = readCodexReasoningLevels(e); const c = r(e, ["context_window", "contextWindow"]); const l = matchesExactOrPrefix(a, ["gpt-5.6"]) ? Math.min(c ?? o?.contextTokens ?? 272e3, OPENAI_DEFAULT_RUNTIME_CONTEXT_TOKENS) : c; const u = r(e, ["max_context_window", "maxContextWindow"]) ?? o?.contextWindow ?? c ?? DEFAULT_CONTEXT_TOKENS; const d = r(e, [ "max_output_tokens", "maxOutputTokens", "max_completion_tokens", "maxCompletionTokens" ]) ?? o?.maxTokens ?? 128e3; const f = s !== void 0 ? { ...o?.compat, supportsReasoningEffort: true, supportedReasoningEfforts: [...s] } : o?.compat; const p = { ...s === void 0 ? o?.thinkingLevelMap : {}, ...o?.thinkingLevelMap?.off === null || a.startsWith("gpt-5.6") ? { off: null } : {}, ...s?.includes("xhigh") ? { xhigh: "xhigh" } : {}, ...s?.includes("max") ? { max: "max" } : {} }; return normalizeOpenAICodexCatalogModel({ id: i, name: n(e, "display_name") ?? o?.name ?? i, api: "openai-chatgpt-responses", baseUrl: OPENAI_CODEX_RESPONSES_BASE_URL, reasoning: (s?.length ?? 0) > 0 || o?.reasoning || false, input: resolveCodexModelInput(e, o), cost: o?.cost ?? OPENAI_UNKNOWN_MODEL_COST, contextWindow: u, maxTokens: d, ...l ?? o?.contextTokens ? { contextTokens: l ?? o?.contextTokens } : {}, ...o?.mediaInput ? { mediaInput: o.mediaInput } : {}, ...f ? { compat: f } : {}, ...Object.keys(p).length > 0 ? { thinkingLevelMap: p } : {} }); } function buildOpenAICodexStaticProviderConfig() { return { baseUrl: OPENAI_CODEX_RESPONSES_BASE_URL, api: "openai-chatgpt-responses", auth: "oauth", models: OPENAI_MANIFEST_PROVIDER.models.flatMap((e) => { const t = normalizeLowercaseStringOrEmpty(e.id); if (isOpenAIPlatformOnlyRouteModelId(t)) return []; if (OPENAI_GPT_6_MODEL_IDS.some((e) => e === t) || OPENAI_DAYBREAK_MODEL_IDS.some((e) => e === t) || t.startsWith("gpt-5.6") && t !== "gpt-5.6-sol") return []; return [normalizeOpenAICodexCatalogModel(e)]; }) }; } async function buildOpenAICodexLiveProviderConfig(e) { const t = await import("openclaw/plugin-sdk/provider-catalog-live-runtime"); const { getCachedLiveCatalogValue: n } = await import("openclaw/plugin-sdk/provider-catalog-shared"); const { fetchLiveProviderModelRows: r, LiveModelCatalogHttpError: i } = t; const a = await resolveOpenAICodexModelsEndpoint(); try { return await n({ ttlMs: OPENAI_CODEX_MODELS_CACHE_TTL_MS, keyParts: [ PROVIDER_ID, "codex-catalog", a, e.discoveryApiKey, e.accountId ?? "" ], load: async (n) => { const i = await r({ providerId: PROVIDER_ID, endpoint: a, discoveryApiKey: e.discoveryApiKey, signal: n, auditContext: "openai-model-discovery", readRows: readCodexModelRows, buildRequestHeaders: ({ discoveryApiKey: t }) => ({ Accept: "application/json", ...t ? { Authorization: `Bearer ${t}` } : {}, ...e.accountId ? { "ChatGPT-Account-ID": e.accountId } : {} }) }); return { provider: { baseUrl: OPENAI_CODEX_RESPONSES_BASE_URL, api: "openai-chatgpt-responses", auth: "oauth", models: i.map((e) => buildOpenAICodexModelFromLiveRow(e, t)).filter((e) => Boolean(e)) }, outcome: buildOpenAICodexReadyOutcome(i) }; } }); } catch (e) { if (e instanceof i && (e.status === 401 || e.status === 403)) return { provider: { ...buildOpenAICodexStaticProviderConfig(), models: [] }, outcome: { provider: PROVIDER_ID, status: "auth-rejected" } }; } return { provider: buildOpenAICodexStaticProviderConfig(), outcome: { provider: PROVIDER_ID, status: "unavailable" } }; } function isCodexCatalogAuthMode(e) { return e === "oauth" || e === "token"; } function resolveOpenAICatalogBaseUrl(e) { const t = Object.entries(e.config?.models?.providers ?? {}).find(([e]) => normalizeProviderId(e) === PROVIDER_ID)?.[1]; return normalizeOptionalString(t?.baseUrl) ?? resolveOpenAIDefaultBaseUrl(e.env ?? process.env); } function buildOpenAIProviderCatalog() { return { catalog: { order: "simple", run: async (e) => { if (e.providerIds && !e.providerIds.includes(PROVIDER_ID)) return null; const t = e.resolveProviderAuth(PROVIDER_ID); if (isSIWCAuthFlow(t.authFlow)) { const { buildTokenSharingCatalog: n } = await import("./token-sharing-catalog-CkgNq4tz.mjs"); return await n({ auth: t, models: buildOpenAIStaticPlatformProviderConfig(void 0, TOKEN_SHARING_RESOURCE).models, signal: e.signal }); } if (t.preparationFailed) return null; let n; if (t.profileId || t.mode === "none") { const { resolveApiKeyForProvider: r } = await import("openclaw/plugin-sdk/provider-auth-runtime"); try { n = await r({ provider: PROVIDER_ID, cfg: e.config, ...e.agentDir ? { agentDir: e.agentDir } : {}, ...e.workspaceDir ? { workspaceDir: e.workspaceDir } : {}, ...t.profileId ? { profileId: t.profileId, lockedProfile: true } : {} }); } catch { n = void 0; } } if (n && isCodexCatalogAuthMode(n.mode) && n.apiKey) { const { resolveProviderAuthProfileMetadataAsync: r } = await import("openclaw/plugin-sdk/provider-auth-runtime"); const i = await r({ provider: PROVIDER_ID, cfg: e.config, ...e.agentDir ? { agentDir: e.agentDir } : {}, ...n.profileId ?? t.profileId ? { profileId: n.profileId ?? t.profileId } : {} }); return projectOpenAICatalog(await buildOpenAICodexLiveProviderConfig({ discoveryApiKey: n.apiKey, accountId: i.accountId }), n.profileId ?? t.profileId); } if (!t.profileId && isCodexCatalogAuthMode(t.mode) && t.apiKey) { const e = t.discoveryApiKey ?? (isNonSecretApiKeyMarker(t.apiKey) ? void 0 : t.apiKey); if (!e) return { providers: { [PROVIDER_ID]: buildOpenAICodexStaticProviderConfig() }, outcomes: [{ provider: PROVIDER_ID, status: "unavailable" }] }; return projectOpenAICatalog(await buildOpenAICodexLiveProviderConfig({ discoveryApiKey: e })); } if (t.profileId && isCodexCatalogAuthMode(t.mode)) return { providers: { [PROVIDER_ID]: buildOpenAICodexStaticProviderConfig() }, outcomes: [{ provider: PROVIDER_ID, profileId: t.profileId, status: "unavailable" }] }; if (t.mode === "api_key" && t.apiKey) { const r = t.discoveryApiKey ?? (n?.mode === "api-key" ? n.apiKey : void 0); if (!r && isNonSecretApiKeyMarker(t.apiKey)) return { providers: { [PROVIDER_ID]: buildOpenAIStaticPlatformProviderConfig(t.apiKey, resolveOpenAICatalogBaseUrl(e)) }, outcomes: [{ provider: PROVIDER_ID, ...t.profileId ? { profileId: t.profileId } : {}, rejectionScope: "catalog", status: "unavailable" }] }; return projectOpenAICatalog(await buildOpenAILiveProviderConfig({ apiKey: t.apiKey, baseUrl: resolveOpenAICatalogBaseUrl(e), discoveryApiKey: r }), t.profileId); } return null; } }, staticCatalog: { order: "simple", run: async () => ({ providers: { [PROVIDER_ID]: OPENAI_MANIFEST_PROVIDER } }) }, augmentModelCatalog: (e) => { return OPENAI_CATALOG_AUGMENTATIONS.flatMap(({ templateIds: t, ...n }) => { const r = findCatalogTemplate({ entries: e.entries, providerId: PROVIDER_ID, templateIds: t }); const i = ["text", "image"]; return r ? [{ ...r, ...n, name: n.id, reasoning: true, input: i }] : []; }); } }; } //#endregion export { OPENAI_GPT_54_MAX_TOKENS as a, OPENAI_CHAT_LATEST_COST as i, buildOpenAIProviderCatalog as n, OPENAI_MANIFEST_PROVIDER as o, isCodexCatalogAuthMode as r, OPENAI_CATALOG_AUGMENTATIONS as t };