openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
81 lines (80 loc) • 2.82 kB
JavaScript
import { l as normalizeOptionalString } from "../../string-coerce-CCIiB4H7.mjs";
import { a as asOptionalRecord } from "../../record-coerce-CVPuDqcc.mjs";
import { t as DEFAULT_CONTEXT_TOKENS } from "../../defaults-BWyoTPqn.mjs";
import { l as isNonSecretApiKeyMarker } from "../../model-auth-markers-4YSUG71a.mjs";
import "../../string-coerce-runtime-BwAZZDf5.mjs";
import "../../provider-auth-markers-ChTkmeEu.mjs";
import { g as LiveModelCatalogHttpError, m as getCachedLiveProviderModelRows } from "../../provider-catalog-live-runtime-D-M7f_ww.mjs";
import "../../provider-model-metadata-BgogdeyO.mjs";
import { n as TOKEN_SHARING_AUTH_FLOW, s as TOKEN_SHARING_RESOURCE } from "../../token-sharing-B3gRpTpZ.mjs";
//#region extensions/openai/token-sharing-catalog.ts
/** SIWC model discovery uses the already-prepared inference profile, never Codex auth. */
async function buildTokenSharingCatalog(e) {
const { auth: t } = e;
const n = t.authFlow === TOKEN_SHARING_AUTH_FLOW;
let r = n ? e.models.filter((e) => e.api === "openai-responses") : [];
let i = n ? "unavailable" : "auth-rejected";
let a;
const o = t.preparationFailed ? void 0 : t.discoveryApiKey ?? (t.apiKey && !isNonSecretApiKeyMarker(t.apiKey) ? t.apiKey : void 0);
if (n && o) try {
const t = await getCachedLiveProviderModelRows({
providerId: "openai",
endpoint: `${TOKEN_SHARING_RESOURCE}/models`,
discoveryApiKey: o,
signal: e.signal,
ttlMs: 6e4,
auditContext: "openai-model-discovery",
readRows: (e) => {
const t = asOptionalRecord(e)?.models;
if (!Array.isArray(t)) throw new Error("SIWC model discovery response must be { models: [] }");
return t;
}
});
const n = new Map(r.map((e) => [e.id, e]));
r = t.flatMap((e) => {
const t = asOptionalRecord(e);
const r = normalizeOptionalString(t?.slug);
if (t?.visibility !== "list" || !r) return [];
return [{
reasoning: false,
input: ["text"],
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0
},
contextWindow: DEFAULT_CONTEXT_TOKENS,
maxTokens: 128e3,
...n.get(r),
id: r,
name: normalizeOptionalString(t.display_name) ?? r,
api: "openai-responses",
baseUrl: TOKEN_SHARING_RESOURCE
}];
});
i = "ready";
} catch (e) {
if (e instanceof LiveModelCatalogHttpError && (e.status === 401 || e.status === 403)) {
r = [];
i = "auth-rejected";
a = e.status === 403 ? "catalog" : void 0;
}
}
return {
providers: { openai: {
baseUrl: TOKEN_SHARING_RESOURCE,
api: "openai-responses",
models: r
} },
outcomes: [{
provider: "openai",
profileId: t.profileId,
status: i,
...a ? { rejectionScope: a } : {},
...i === "ready" ? { modelOrder: r.map(({ id: e }) => e) } : {}
}]
};
}
//#endregion
export { buildTokenSharingCatalog };