openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
142 lines (141 loc) • 5.84 kB
JavaScript
import { a as normalizeLowercaseStringOrEmpty } from "./string-coerce-DW4mBlAt.js";
import { _ as uniqueStrings, l as normalizeStringEntries } from "./string-normalization-CRyoFBPt.js";
//#region src/agents/openai-reasoning-effort.ts
/**
* OpenAI-compatible reasoning-effort normalization. Different GPT families
* expose different accepted effort enums, so callers map requested values here
* before constructing provider payloads.
*/
const GPT_5_REASONING_EFFORTS = [
"minimal",
"low",
"medium",
"high"
];
const GPT_51_REASONING_EFFORTS = [
"none",
"low",
"medium",
"high"
];
const GPT_52_REASONING_EFFORTS = [
"none",
"low",
"medium",
"high",
"xhigh"
];
const GPT_CODEX_REASONING_EFFORTS = [
"low",
"medium",
"high",
"xhigh"
];
const GPT_PRO_REASONING_EFFORTS = [
"medium",
"high",
"xhigh"
];
const GPT_5_PRO_REASONING_EFFORTS = ["high"];
const GPT_51_CODEX_MAX_REASONING_EFFORTS = [
"none",
"medium",
"high",
"xhigh"
];
const GPT_51_CODEX_MINI_REASONING_EFFORTS = ["medium"];
const GENERIC_REASONING_EFFORTS = [
"low",
"medium",
"high"
];
function normalizeModelId(id) {
return normalizeLowercaseStringOrEmpty(id ?? "").replace(/-\d{4}-\d{2}-\d{2}$/u, "");
}
/** Return whether a model is the GPT-5.4 mini family. */
function isOpenAIGpt54MiniModel(model) {
const id = normalizeModelId(typeof model.id === "string" ? model.id : void 0);
return /^gpt-5\.4-mini(?:-|$)/u.test(id);
}
/** Return whether a model is the GPT-5.5 family. */
function isOpenAIGpt55Model(model) {
const id = normalizeModelId(typeof model.id === "string" ? model.id : void 0);
const name = normalizeModelId(typeof model.name === "string" ? model.name : void 0);
return /^gpt-5\.5(?:-|$)/u.test(id) || /^gpt-5\.5(?:\s|\(|-|$)/u.test(name);
}
/** Normalize user-facing reasoning effort names to API effort names. */
function normalizeOpenAIReasoningEffort(effort) {
return effort === "minimal" ? "minimal" : effort;
}
function readCompatReasoningEfforts(compat) {
if (!compat || typeof compat !== "object") return;
if (compat.supportsReasoningEffort === false) return [];
const raw = compat.supportedReasoningEfforts;
if (!Array.isArray(raw)) return;
const supported = uniqueStrings(normalizeStringEntries(raw.filter((value) => typeof value === "string")));
return supported.length > 0 ? supported : void 0;
}
function isDisabledReasoningEffort(effort) {
return effort === "none" || effort === "off";
}
/** Resolve the reasoning efforts accepted by a specific OpenAI-compatible model. */
function resolveOpenAISupportedReasoningEfforts(model) {
const compatEfforts = readCompatReasoningEfforts(model.compat);
if (compatEfforts) return compatEfforts;
const id = normalizeModelId(typeof model.id === "string" ? model.id : void 0);
if (id === "gpt-5.1-codex-mini") return GPT_51_CODEX_MINI_REASONING_EFFORTS;
if (id === "gpt-5.1-codex-max") return GPT_51_CODEX_MAX_REASONING_EFFORTS;
if (/^gpt-5(?:\.\d+)?-codex(?:-|$)/u.test(id)) return GPT_CODEX_REASONING_EFFORTS;
if (id === "gpt-5-pro") return GPT_5_PRO_REASONING_EFFORTS;
if (/^gpt-5\.[2-9](?:\.\d+)?-pro(?:-|$)/u.test(id)) return GPT_PRO_REASONING_EFFORTS;
if (/^gpt-5\.[2-9](?:\.\d+)?(?:-|$)/u.test(id)) return GPT_52_REASONING_EFFORTS;
if (/^gpt-5\.1(?:-|$)/u.test(id)) return GPT_51_REASONING_EFFORTS;
if (/^gpt-5(?:-|$)/u.test(id)) return GPT_5_REASONING_EFFORTS;
return GENERIC_REASONING_EFFORTS;
}
/** Return whether a model accepts a requested reasoning effort. */
function supportsOpenAIReasoningEffort(model, effort) {
return resolveOpenAISupportedReasoningEfforts(model).includes(normalizeOpenAIReasoningEffort(effort));
}
/** Resolve a requested reasoning effort to the closest value supported by the model. */
function resolveOpenAIReasoningEffortForModel(params) {
const requested = normalizeOpenAIReasoningEffort(params.effort);
const normalized = normalizeOpenAIReasoningEffort(params.fallbackMap?.[requested] ?? requested);
const supported = resolveOpenAISupportedReasoningEfforts(params.model);
if (supported.includes(normalized)) return normalized;
if (isDisabledReasoningEffort(requested) || isDisabledReasoningEffort(normalized)) return;
if (requested === "minimal" && supported.includes("low")) return "low";
if ((requested === "minimal" || requested === "low") && supported.includes("medium")) return "medium";
if (requested === "xhigh" && supported.includes("high")) return "high";
return supported.find((effort) => effort !== "none");
}
//#endregion
//#region src/agents/openai-reasoning-compat.ts
/**
* OpenAI reasoning-effort compatibility helpers.
*
* Keeps provider metadata and built-in model exceptions on one path before request payloads are built.
*/
const OPENAI_MEDIUM_ONLY_REASONING_MODEL_IDS = new Set(["gpt-5.1-codex-mini"]);
function readCompatReasoningEffortMap(compat) {
if (!compat || typeof compat !== "object") return {};
const rawMap = compat.reasoningEffortMap;
if (!rawMap || typeof rawMap !== "object") return {};
return Object.fromEntries(Object.entries(rawMap).filter((entry) => typeof entry[0] === "string" && typeof entry[1] === "string"));
}
/** Resolves the reasoning effort remap for an OpenAI-compatible model. */
function resolveOpenAIReasoningEffortMap(model, fallbackMap = {}) {
const provider = normalizeLowercaseStringOrEmpty(model.provider ?? "");
const id = normalizeLowercaseStringOrEmpty(model.id ?? "");
const builtinMap = provider === "openai" && OPENAI_MEDIUM_ONLY_REASONING_MODEL_IDS.has(id) ? {
minimal: "medium",
low: "medium"
} : {};
return {
...fallbackMap,
...builtinMap,
...readCompatReasoningEffortMap(model.compat)
};
}
//#endregion
export { resolveOpenAIReasoningEffortForModel as a, normalizeOpenAIReasoningEffort as i, isOpenAIGpt54MiniModel as n, supportsOpenAIReasoningEffort as o, isOpenAIGpt55Model as r, resolveOpenAIReasoningEffortMap as t };