UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

79 lines (78 loc) • 2.92 kB
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs"; import { t as CODEX_APP_SERVER_AUTH_MARKER } from "./model-auth-markers-4YSUG71a.mjs"; //#region src/status/codex-synthetic-usage.ts const CODEX_SYNTHETIC_USAGE_PROVIDER = "openai"; const CODEX_SYNTHETIC_USAGE_HOOK_PROVIDER = "codex"; /** Maps a provider auth label onto the usage credential type buckets. */ function resolveUsageCredentialType(e) { const t = normalizeOptionalLowercaseString(e); if (!t) return; if (t.startsWith("oauth")) return "oauth"; if (t.startsWith("token")) return "token"; if (t.startsWith("api-key") || t.startsWith("api key")) return "api_key"; } function buildCodexSyntheticUsageAuth(e = {}) { return { provider: CODEX_SYNTHETIC_USAGE_PROVIDER, token: CODEX_APP_SERVER_AUTH_MARKER, ...e.authProfileId ? { authProfileId: e.authProfileId } : {}, hookProvider: CODEX_SYNTHETIC_USAGE_HOOK_PROVIDER }; } function shouldUseCodexSyntheticUsageForRuntime(e) { const t = normalizeOptionalLowercaseString(e.effectiveHarness); const n = normalizeOptionalLowercaseString(e.sessionHarnessId); const r = normalizeOptionalLowercaseString(e.provider); return (t === CODEX_SYNTHETIC_USAGE_HOOK_PROVIDER || n === CODEX_SYNTHETIC_USAGE_HOOK_PROVIDER) && (r === CODEX_SYNTHETIC_USAGE_PROVIDER || r === "codex"); } function usageSnapshotRank(e) { if (e.windows.length > 0 || Boolean(e.billing?.length) || Boolean(e.summary?.trim())) return 2; return e.error ? 0 : 1; } function billingEntryKey(e) { const t = "period" in e ? e.period ?? "" : ""; return [ e.type, e.label ?? "", e.unit, t ].join("\0"); } function mergeUsageSnapshots(e, t) { const n = usageSnapshotRank(e); const r = usageSnapshotRank(t); const i = n >= r && !(n === 0 && r === 0); const a = i ? e : t; const o = i ? t : e; const s = /* @__PURE__ */ new Map(); for (const e of o.billing ?? []) s.set(billingEntryKey(e), e); for (const e of a.billing ?? []) s.set(billingEntryKey(e), e); const c = s.size > 0 ? [...s.values()] : void 0; return { ...o, ...a, windows: a.windows.length > 0 ? a.windows : o.windows, ...c ? { billing: c } : {}, ...a.summary?.trim() ? { summary: a.summary } : o.summary?.trim() ? { summary: o.summary } : {}, ...a.plan?.trim() ? { plan: a.plan } : o.plan?.trim() ? { plan: o.plan } : {}, ...!a.error ? { error: void 0 } : {} }; } function mergeUsageSummaries(e, t) { if (!t || t.providers.length === 0) return e; const n = new Map(e.providers.map((e) => [e.provider, e])); for (const e of t.providers) { const t = n.get(e.provider); if (!t) { n.set(e.provider, e); continue; } n.set(e.provider, mergeUsageSnapshots(e, t)); } return { updatedAt: e.updatedAt, providers: [...n.values()] }; } //#endregion export { shouldUseCodexSyntheticUsageForRuntime as i, mergeUsageSummaries as n, resolveUsageCredentialType as r, buildCodexSyntheticUsageAuth as t };