Repository navigation
Expand file tree
/
Copy pathapi-config.js
More file actions
49 lines (44 loc) · 2.03 KB
/
Copy pathapi-config.js
File metadata and controls
49 lines (44 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"use strict";
function resolveApiConfig(value, formatOverride) {
if (!value) return null;
const requestedFormat = String(formatOverride || "").trim().toLowerCase();
if (requestedFormat && !["openai", "anthropic"].includes(requestedFormat)) return null;
try {
const url = new URL(value);
if (!["http:", "https:"].includes(url.protocol) || !url.hostname || url.username || url.password) return null;
url.hash = "";
const basePath = url.pathname.replace(/\/+$/, ""), path = basePath.toLowerCase(),
explicitAnthropic = path.endsWith("/v1/messages"), explicitOpenAi = path.endsWith("/chat/completions");
if (requestedFormat === "openai" && explicitAnthropic || requestedFormat === "anthropic" && explicitOpenAi) return null;
if (explicitAnthropic) {
url.pathname = basePath;
return { format: "anthropic", endpoint: url.href };
}
if (explicitOpenAi) {
url.pathname = basePath;
return { format: "openai", endpoint: url.href };
}
const openaiBase = path.endsWith("/v1") || /\/(?:v1beta\/)?openai$/i.test(path),
format = requestedFormat || (openaiBase ? "openai" : "anthropic");
url.pathname = format === "openai" ? `${basePath}/chat/completions` : `${basePath}/v1/messages`;
return { format, endpoint: url.href };
} catch {
return null;
}
}
function normalizedApiEffort(format, value) {
const effort = String(value || "").trim();
return effort || (format === "anthropic" ? "medium" : "max");
}
function anthropicEffortParameters(effort, enableThinking = true) {
const normalized = normalizedApiEffort("anthropic", effort);
if (normalized.toLowerCase() === "none") return { thinking: { type:"disabled" } };
return {
...(enableThinking ? { thinking: { type:"adaptive" } } : {}),
output_config: { effort:normalized },
};
}
function anthropicResponseMaxTokens(effort) {
return String(effort || "").trim().toLowerCase() === "max" ? 16384 : 8192;
}
module.exports = { anthropicEffortParameters, anthropicResponseMaxTokens, normalizedApiEffort, resolveApiConfig };