copilot-relay 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/config.default.yaml +5 -2
- package/dist/main.js +791 -589
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
2
3
|
import { defineCommand, runMain } from "citty";
|
|
3
4
|
import fs from "node:fs/promises";
|
|
4
5
|
import { createHash, randomUUID, timingSafeEqual } from "node:crypto";
|
|
@@ -8,7 +9,6 @@ import { inspect, isDeepStrictEqual, promisify } from "node:util";
|
|
|
8
9
|
import consola from "consola";
|
|
9
10
|
import os from "node:os";
|
|
10
11
|
import path, { dirname, posix, resolve } from "node:path";
|
|
11
|
-
import { Agent, EnvHttpProxyAgent, ProxyAgent, fetch } from "undici";
|
|
12
12
|
import { fileURLToPath } from "node:url";
|
|
13
13
|
import { createReadStream, readFileSync, statSync } from "node:fs";
|
|
14
14
|
import { BlockList, isIP, isIPv4 } from "node:net";
|
|
@@ -594,6 +594,14 @@ const cleanupLogs = async (retentionDays) => {
|
|
|
594
594
|
};
|
|
595
595
|
const log = consola;
|
|
596
596
|
|
|
597
|
+
//#endregion
|
|
598
|
+
//#region src/lib/undici.ts
|
|
599
|
+
let undici;
|
|
600
|
+
const loadUndici = () => {
|
|
601
|
+
undici ??= createRequire(import.meta.url)("undici");
|
|
602
|
+
return undici;
|
|
603
|
+
};
|
|
604
|
+
|
|
597
605
|
//#endregion
|
|
598
606
|
//#region src/lib/upstream-dispatcher.ts
|
|
599
607
|
const connectionOptions = {
|
|
@@ -620,6 +628,7 @@ var InvalidProxyEnvironmentError = class extends Error {
|
|
|
620
628
|
* normalizeUpstreamProxy has already validated.
|
|
621
629
|
*/
|
|
622
630
|
const buildUpstreamDispatcher = (upstreamProxy) => {
|
|
631
|
+
const { Agent, EnvHttpProxyAgent, ProxyAgent } = loadUndici();
|
|
623
632
|
if (upstreamProxy === void 0) return new Agent(connectionOptions);
|
|
624
633
|
if (upstreamProxy === "env") try {
|
|
625
634
|
return new EnvHttpProxyAgent(connectionOptions);
|
|
@@ -631,7 +640,7 @@ const buildUpstreamDispatcher = (upstreamProxy) => {
|
|
|
631
640
|
uri: upstreamProxy
|
|
632
641
|
});
|
|
633
642
|
};
|
|
634
|
-
let upstreamDispatcher
|
|
643
|
+
let upstreamDispatcher;
|
|
635
644
|
let configuredUpstreamProxy;
|
|
636
645
|
/**
|
|
637
646
|
* The proxy URLs a dispatcher for this upstreamProxy can send through: none when it is empty, the
|
|
@@ -708,7 +717,11 @@ const configureUpstreamDispatcher = (upstreamProxy) => {
|
|
|
708
717
|
for (const form of proxyUrls.flatMap((proxyUrl) => credentialPairForms(proxyUrl))) if (form.length >= minimumRegisteredLength) registerLogSecret(form);
|
|
709
718
|
const forms = [...new Set(proxyUrls.flatMap((proxyUrl) => credentialForms(proxyUrl)))].sort((left, right) => right.length - left.length);
|
|
710
719
|
activeCredentialPattern = forms.length === 0 ? void 0 : new RegExp(forms.map((form) => escapeRegExp(form)).join("|"), "g");
|
|
711
|
-
previous
|
|
720
|
+
previous?.close().catch(() => void 0);
|
|
721
|
+
};
|
|
722
|
+
const getUpstreamDispatcher = () => {
|
|
723
|
+
upstreamDispatcher ??= buildUpstreamDispatcher(void 0);
|
|
724
|
+
return upstreamDispatcher;
|
|
712
725
|
};
|
|
713
726
|
const proxyVariables = [
|
|
714
727
|
"HTTPS_PROXY",
|
|
@@ -787,9 +800,9 @@ const sanitizeTransportError = (error, seen = /* @__PURE__ */ new Set()) => {
|
|
|
787
800
|
* uses.
|
|
788
801
|
*/
|
|
789
802
|
const fetchUpstream = async (url, init) => {
|
|
790
|
-
const response = await fetch(url, {
|
|
803
|
+
const response = await loadUndici().fetch(url, {
|
|
791
804
|
...init,
|
|
792
|
-
dispatcher:
|
|
805
|
+
dispatcher: getUpstreamDispatcher()
|
|
793
806
|
}).catch((error) => {
|
|
794
807
|
sanitizeTransportError(error);
|
|
795
808
|
noteConnectionFailure(init.signal);
|
|
@@ -934,8 +947,11 @@ const pollAccessToken = async (deviceCode) => {
|
|
|
934
947
|
await sleep$1(sleepDuration);
|
|
935
948
|
}
|
|
936
949
|
};
|
|
937
|
-
const getGitHubUser = async (githubToken, vsCodeVersion) => {
|
|
938
|
-
const response = await fetchUpstream(`${githubApiBaseUrl}/user`, {
|
|
950
|
+
const getGitHubUser = async (githubToken, vsCodeVersion, signal) => {
|
|
951
|
+
const response = await fetchUpstream(`${githubApiBaseUrl}/user`, {
|
|
952
|
+
headers: githubHeaders(githubToken, vsCodeVersion),
|
|
953
|
+
signal
|
|
954
|
+
});
|
|
939
955
|
if (!response.ok) throw new HTTPError("Failed to get GitHub user", response);
|
|
940
956
|
return await response.json();
|
|
941
957
|
};
|
|
@@ -970,11 +986,11 @@ const ensureGitHubToken = async (options = {}) => {
|
|
|
970
986
|
log.info(`GitHub token synced to ${paths.githubTokenPath}`);
|
|
971
987
|
return githubToken;
|
|
972
988
|
};
|
|
973
|
-
const loadGitHubLogin = async (githubToken, vsCodeVersion) => {
|
|
989
|
+
const loadGitHubLogin = async (githubToken, vsCodeVersion, signal) => {
|
|
974
990
|
try {
|
|
975
|
-
return (await getGitHubUser(githubToken, vsCodeVersion)).login;
|
|
991
|
+
return (await getGitHubUser(githubToken, vsCodeVersion, signal)).login;
|
|
976
992
|
} catch (error) {
|
|
977
|
-
log.error("Could not load GitHub user:", error);
|
|
993
|
+
if (!signal?.aborted) log.error("Could not load GitHub user:", error);
|
|
978
994
|
return;
|
|
979
995
|
}
|
|
980
996
|
};
|
|
@@ -1039,7 +1055,7 @@ const setupProxyAuth = async (config, options = {}) => {
|
|
|
1039
1055
|
}
|
|
1040
1056
|
};
|
|
1041
1057
|
return {
|
|
1042
|
-
githubLogin:
|
|
1058
|
+
githubLogin: loadGitHubLogin(githubToken, config.vsCodeVersion, options.loginSignal),
|
|
1043
1059
|
source: "github-token"
|
|
1044
1060
|
};
|
|
1045
1061
|
};
|
|
@@ -1097,38 +1113,129 @@ function getRequestReasoningEffort(request) {
|
|
|
1097
1113
|
return requested;
|
|
1098
1114
|
}
|
|
1099
1115
|
const resolveReasoningEffort = (requested) => requested ?? getRuntimeState().thinkEffort ?? defaultReasoningEffort;
|
|
1100
|
-
const
|
|
1101
|
-
const
|
|
1102
|
-
return
|
|
1116
|
+
const currentCatalog = () => {
|
|
1117
|
+
const state = getRuntimeState();
|
|
1118
|
+
return state.modelCatalog?.baseUrl === state.upstreamBaseUrl ? state.modelCatalog?.models : void 0;
|
|
1103
1119
|
};
|
|
1120
|
+
const normalizeCopilotModelId = (model) => canonicalModelId(model, currentCatalog());
|
|
1121
|
+
const isClaudeModelId = (model) => model.startsWith("claude-");
|
|
1122
|
+
/**
|
|
1123
|
+
* The ID Claude Code sees. A model whose own known window is at least 1M is offered only as its 1M
|
|
1124
|
+
* version, `<id>[1m]`, Claude models included; a smaller known window shows the plain ID, and an
|
|
1125
|
+
* unknown window leaves the ID as given. [1m] overrides Claude Code's numeric context setting.
|
|
1126
|
+
*/
|
|
1104
1127
|
const normalizeClaudeModelId = (model, contextWindowTokens) => {
|
|
1105
|
-
const
|
|
1106
|
-
|
|
1107
|
-
const
|
|
1108
|
-
|
|
1109
|
-
return
|
|
1128
|
+
const catalog = currentCatalog();
|
|
1129
|
+
const canonical$1 = canonicalModelId(model, catalog);
|
|
1130
|
+
const contextWindow = contextWindowTokens ?? catalog?.get(canonical$1)?.limits?.max_context_window_tokens;
|
|
1131
|
+
if (contextWindow === void 0) return model;
|
|
1132
|
+
return contextWindow >= 1e6 ? `${canonical$1}[1m]` : canonical$1;
|
|
1110
1133
|
};
|
|
1111
|
-
const normalizeCopilotModelId = (model) => normalizeOneMillionContextModel(model) ?? model;
|
|
1112
|
-
const isClaudeModelId = (model) => model.startsWith("claude-");
|
|
1113
1134
|
const getConfiguredModelRouting = () => getRuntimeState().modelRouting ?? defaultModelRouting;
|
|
1114
1135
|
const getModelRouting = () => {
|
|
1115
1136
|
const routing = getConfiguredModelRouting();
|
|
1116
1137
|
return {
|
|
1138
|
+
...routing,
|
|
1117
1139
|
gptModel: normalizeCopilotModelId(routing.gptModel),
|
|
1118
1140
|
opusModel: normalizeCopilotModelId(routing.opusModel)
|
|
1119
1141
|
};
|
|
1120
1142
|
};
|
|
1143
|
+
const getExposedUpstreamModelIds = () => resolveExposedModels(getConfiguredModelRouting(), currentCatalog());
|
|
1121
1144
|
const getExposedModelIds = () => {
|
|
1122
1145
|
const routing = getConfiguredModelRouting();
|
|
1123
|
-
|
|
1146
|
+
const slots = [routing.gptModel, routing.opusModel];
|
|
1147
|
+
return getExposedUpstreamModelIds().map((id) => {
|
|
1148
|
+
return normalizeClaudeModelId(slots.find((configured) => normalizeCopilotModelId(configured).toLowerCase() === id.toLowerCase()) ?? id);
|
|
1149
|
+
});
|
|
1124
1150
|
};
|
|
1125
1151
|
const getUpstreamModelIds = () => {
|
|
1126
1152
|
const routing = getModelRouting();
|
|
1127
1153
|
return [routing.gptModel, routing.opusModel];
|
|
1128
1154
|
};
|
|
1129
1155
|
const routeModelId = (model) => {
|
|
1156
|
+
const requested = normalizeCopilotModelId(model.trim()).toLowerCase();
|
|
1157
|
+
const exposed = getExposedUpstreamModelIds().find((id) => id.toLowerCase() === requested);
|
|
1158
|
+
if (exposed !== void 0) return exposed;
|
|
1130
1159
|
const routing = getModelRouting();
|
|
1131
|
-
return
|
|
1160
|
+
return requested.includes("opus") ? routing.opusModel : routing.gptModel;
|
|
1161
|
+
};
|
|
1162
|
+
const modelIdPattern = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
1163
|
+
const invalidModelsMessage = "Invalid models: expected auto, empty, or comma-separated Copilot model IDs";
|
|
1164
|
+
const parseModelSelection = (value) => {
|
|
1165
|
+
const trimmed = value.trim();
|
|
1166
|
+
if (trimmed.toLowerCase() === "auto") return "auto";
|
|
1167
|
+
if (trimmed === "") return [];
|
|
1168
|
+
const ids = trimmed.split(",").map((id) => id.trim());
|
|
1169
|
+
if (ids.some((id) => !modelIdPattern.test(id) || id.toLowerCase() === "auto")) throw new Error(invalidModelsMessage);
|
|
1170
|
+
return ids;
|
|
1171
|
+
};
|
|
1172
|
+
const canonicalModelId = (id, catalog) => {
|
|
1173
|
+
const lower = id.replace(/\[1m\]/gi, "").toLowerCase();
|
|
1174
|
+
for (const key of catalog?.keys() ?? []) if (key.toLowerCase() === lower) return key;
|
|
1175
|
+
return lower;
|
|
1176
|
+
};
|
|
1177
|
+
const pickerCategories = [
|
|
1178
|
+
"powerful",
|
|
1179
|
+
"versatile",
|
|
1180
|
+
"lightweight"
|
|
1181
|
+
];
|
|
1182
|
+
const categoryRank = (model) => {
|
|
1183
|
+
const rank = pickerCategories.indexOf(model.pickerCategory ?? "");
|
|
1184
|
+
return rank === -1 ? pickerCategories.length : rank;
|
|
1185
|
+
};
|
|
1186
|
+
const compareVersions = (left, right) => {
|
|
1187
|
+
for (let index = 0; index < Math.max(left.length, right.length); index++) {
|
|
1188
|
+
const difference = (left[index] ?? 0) - (right[index] ?? 0);
|
|
1189
|
+
if (difference !== 0) return difference;
|
|
1190
|
+
}
|
|
1191
|
+
return 0;
|
|
1192
|
+
};
|
|
1193
|
+
const isBetterPick = (left, right) => {
|
|
1194
|
+
const byVersion = compareVersions(left.version, right.version);
|
|
1195
|
+
if (byVersion !== 0) return byVersion > 0;
|
|
1196
|
+
const byCategory = categoryRank(right.model) - categoryRank(left.model);
|
|
1197
|
+
if (byCategory !== 0) return byCategory > 0;
|
|
1198
|
+
return left.id < right.id;
|
|
1199
|
+
};
|
|
1200
|
+
/** The newest picker-enabled chat model of each vendor except Experimental, in catalog order. */
|
|
1201
|
+
const selectNewestPerVendor = (catalog) => {
|
|
1202
|
+
const best = /* @__PURE__ */ new Map();
|
|
1203
|
+
for (const [id, model] of catalog) {
|
|
1204
|
+
const vendor = model.vendor;
|
|
1205
|
+
const version = /\d+(?:\.\d+)*/.exec(id)?.[0].split(".").map(Number);
|
|
1206
|
+
if (!(model.pickerEnabled === true && model.type === "chat" && vendor !== "Experimental") || vendor === void 0 || version === void 0) continue;
|
|
1207
|
+
const candidate = {
|
|
1208
|
+
id,
|
|
1209
|
+
model,
|
|
1210
|
+
version
|
|
1211
|
+
};
|
|
1212
|
+
const current = best.get(vendor);
|
|
1213
|
+
if (current === void 0 || isBetterPick(candidate, current)) best.set(vendor, candidate);
|
|
1214
|
+
}
|
|
1215
|
+
const winners = new Set([...best.values()].map((candidate) => candidate.id));
|
|
1216
|
+
return [...catalog.keys()].filter((id) => winners.has(id));
|
|
1217
|
+
};
|
|
1218
|
+
/**
|
|
1219
|
+
* The upstream IDs clients are offered: gptModel, opusModel, then the selection, deduplicated
|
|
1220
|
+
* case-insensitively in catalog casing. A listed ID the catalog lacks is skipped; without a
|
|
1221
|
+
* catalog, auto adds nothing and every listed ID is kept, lowercased.
|
|
1222
|
+
*/
|
|
1223
|
+
const resolveExposedModels = (routing, catalog) => {
|
|
1224
|
+
const present = (routing.models === "auto" ? selectNewestPerVendor(catalog ?? /* @__PURE__ */ new Map()) : routing.models ?? []).filter((id) => catalog === void 0 || catalog.has(canonicalModelId(id, catalog)));
|
|
1225
|
+
const exposed = [];
|
|
1226
|
+
for (const id of [
|
|
1227
|
+
routing.gptModel,
|
|
1228
|
+
routing.opusModel,
|
|
1229
|
+
...present
|
|
1230
|
+
]) {
|
|
1231
|
+
const canonical$1 = canonicalModelId(id, catalog);
|
|
1232
|
+
if (!exposed.some((known) => known.toLowerCase() === canonical$1.toLowerCase())) exposed.push(canonical$1);
|
|
1233
|
+
}
|
|
1234
|
+
return exposed;
|
|
1235
|
+
};
|
|
1236
|
+
const findMissingModels = (routing, catalog) => {
|
|
1237
|
+
if (!Array.isArray(routing.models)) return [];
|
|
1238
|
+
return routing.models.filter((id) => !catalog.has(canonicalModelId(id, catalog)));
|
|
1132
1239
|
};
|
|
1133
1240
|
|
|
1134
1241
|
//#endregion
|
|
@@ -1327,6 +1434,7 @@ const defaultConfig = {
|
|
|
1327
1434
|
logLevel: "info",
|
|
1328
1435
|
logRetentionDays: 3,
|
|
1329
1436
|
opusModel: "claude-opus-5.5",
|
|
1437
|
+
models: "auto",
|
|
1330
1438
|
port: 4142,
|
|
1331
1439
|
thinkEffort: defaultReasoningEffort,
|
|
1332
1440
|
upstreamTimeoutSeconds: 180,
|
|
@@ -1362,6 +1470,12 @@ const normalizeInteger = (value, key, minimum, maximum = Number.MAX_SAFE_INTEGER
|
|
|
1362
1470
|
if (typeof candidate !== "number" || !Number.isSafeInteger(candidate) || candidate < minimum || candidate > maximum) throw new Error(`Invalid ${key}: expected an integer from ${minimum} to ${maximum}`);
|
|
1363
1471
|
return candidate;
|
|
1364
1472
|
};
|
|
1473
|
+
const normalizeModels = (value) => {
|
|
1474
|
+
if (value === void 0) return;
|
|
1475
|
+
if (typeof value !== "string") throw new Error("Invalid models: expected auto, empty, or comma-separated Copilot model IDs");
|
|
1476
|
+
parseModelSelection(value);
|
|
1477
|
+
return value.trim();
|
|
1478
|
+
};
|
|
1365
1479
|
const normalizeString = (value) => typeof value === "string" && value.trim() ? value.trim() : void 0;
|
|
1366
1480
|
const normalizeRequiredString = (value, key) => {
|
|
1367
1481
|
if (value === void 0) return;
|
|
@@ -1663,6 +1777,9 @@ const serializeConfig = (config) => [
|
|
|
1663
1777
|
"",
|
|
1664
1778
|
"# Upstream Copilot model used for requests containing \"opus\".",
|
|
1665
1779
|
`opusModel: ${config.opusModel}`,
|
|
1780
|
+
"",
|
|
1781
|
+
"# Extra models offered to clients: auto (newest model of each vendor), empty, or comma-separated Copilot IDs.",
|
|
1782
|
+
`models: ${config.models}`,
|
|
1666
1783
|
""
|
|
1667
1784
|
].join("\n");
|
|
1668
1785
|
const resolveConfig = (raw) => ({
|
|
@@ -1675,6 +1792,7 @@ const resolveConfig = (raw) => ({
|
|
|
1675
1792
|
logLevel: normalizeLogLevel(raw.logLevel) ?? defaultConfig.logLevel,
|
|
1676
1793
|
logRetentionDays: normalizeInteger(raw.logRetentionDays, "logRetentionDays", 1) ?? defaultConfig.logRetentionDays,
|
|
1677
1794
|
opusModel: normalizeRequiredString(raw.opusModel, "opusModel") ?? defaultConfig.opusModel,
|
|
1795
|
+
models: normalizeModels(raw.models) ?? defaultConfig.models,
|
|
1678
1796
|
port: normalizeInteger(raw.port, "port", 1, 65535) ?? defaultConfig.port,
|
|
1679
1797
|
thinkEffort: normalizeThinkEffort(raw.thinkEffort) ?? defaultConfig.thinkEffort,
|
|
1680
1798
|
upstreamTimeoutSeconds: normalizeUpstreamTimeoutSeconds(raw.upstreamTimeoutSeconds) ?? defaultConfig.upstreamTimeoutSeconds,
|
|
@@ -1764,6 +1882,7 @@ const snapshotRoots = /* @__PURE__ */ new WeakMap();
|
|
|
1764
1882
|
const snapshotProxyConfig = (config) => {
|
|
1765
1883
|
const snapshot = {
|
|
1766
1884
|
...config,
|
|
1885
|
+
admittedAt: config.admittedAt ?? Date.now(),
|
|
1767
1886
|
get copilotToken() {
|
|
1768
1887
|
return config.copilotToken;
|
|
1769
1888
|
},
|
|
@@ -1803,8 +1922,8 @@ const auth = defineCommand({
|
|
|
1803
1922
|
configureUpstreamDispatcher(appConfig.upstreamProxy);
|
|
1804
1923
|
const config = readProxyConfig(appConfig);
|
|
1805
1924
|
config.copilotToken = void 0;
|
|
1806
|
-
const
|
|
1807
|
-
if (
|
|
1925
|
+
const githubLogin = await (await setupProxyAuth(config, { force: true })).githubLogin;
|
|
1926
|
+
if (githubLogin) log.info(`Logged in as ${githubLogin}`);
|
|
1808
1927
|
log.info("copilot-relay auth completed");
|
|
1809
1928
|
}
|
|
1810
1929
|
});
|
|
@@ -2367,7 +2486,11 @@ var OutcomeObserver = class {
|
|
|
2367
2486
|
else this.overflow = true;
|
|
2368
2487
|
return;
|
|
2369
2488
|
}
|
|
2370
|
-
for (
|
|
2489
|
+
for (let start$1 = 0; start$1 < text$1.length;) {
|
|
2490
|
+
const lineEnd = text$1.indexOf("\n", start$1);
|
|
2491
|
+
const end = lineEnd === -1 ? text$1.length : lineEnd + 1;
|
|
2492
|
+
const part = text$1.slice(start$1, end);
|
|
2493
|
+
start$1 = end;
|
|
2371
2494
|
if (this.pending.length + part.length <= 2 * 1024 * 1024 && !this.overflow) this.pending += part;
|
|
2372
2495
|
else this.overflow = true;
|
|
2373
2496
|
if (part.endsWith("\n")) {
|
|
@@ -2695,7 +2818,8 @@ var RequestTrace = class RequestTrace {
|
|
|
2695
2818
|
runtime: {
|
|
2696
2819
|
...policy,
|
|
2697
2820
|
models: catalog ? [...catalog.models.entries()] : void 0,
|
|
2698
|
-
catalogCurrent: catalog?.baseUrl === config.copilotBaseUrl
|
|
2821
|
+
catalogCurrent: catalog?.baseUrl === config.copilotBaseUrl,
|
|
2822
|
+
...catalog?.fetchedAt !== void 0 && { catalogAgeMs: Math.max(0, (config.admittedAt ?? Date.now()) - catalog.fetchedAt) }
|
|
2699
2823
|
}
|
|
2700
2824
|
};
|
|
2701
2825
|
const abort = () => {
|
|
@@ -3351,6 +3475,7 @@ const pinnedCatalogs = /* @__PURE__ */ new WeakMap();
|
|
|
3351
3475
|
const pinCopilotModelCatalog = (config) => {
|
|
3352
3476
|
pinnedCatalogs.set(config, config.modelCatalog);
|
|
3353
3477
|
};
|
|
3478
|
+
const missingModelRecheckMs = 6e4;
|
|
3354
3479
|
const pendingCatalogs = /* @__PURE__ */ new WeakMap();
|
|
3355
3480
|
const isRecord$11 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3356
3481
|
const isPositiveInteger = (value) => typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
@@ -3374,8 +3499,43 @@ function getCachedCopilotModel(config, model) {
|
|
|
3374
3499
|
return catalog.models.get(normalizeCopilotModelId(model));
|
|
3375
3500
|
}
|
|
3376
3501
|
const advertisesNoReasoningEffort = (capabilities$1) => capabilities$1?.reasoningEfforts?.length === 0;
|
|
3502
|
+
const copilotReasoningEfforts = [
|
|
3503
|
+
"none",
|
|
3504
|
+
"minimal",
|
|
3505
|
+
"low",
|
|
3506
|
+
"medium",
|
|
3507
|
+
"high",
|
|
3508
|
+
"xhigh",
|
|
3509
|
+
"max"
|
|
3510
|
+
];
|
|
3511
|
+
const effortOrder = [
|
|
3512
|
+
"none",
|
|
3513
|
+
"minimal",
|
|
3514
|
+
"low",
|
|
3515
|
+
"medium",
|
|
3516
|
+
"high",
|
|
3517
|
+
"xhigh",
|
|
3518
|
+
"max",
|
|
3519
|
+
"ultra"
|
|
3520
|
+
];
|
|
3521
|
+
const effortRank = (effort) => effortOrder.indexOf(effort);
|
|
3522
|
+
/**
|
|
3523
|
+
* The reasoning effort to send for a requested one.
|
|
3524
|
+
*
|
|
3525
|
+
* A level the model advertises, or one this relay does not know, is sent as requested. Any other
|
|
3526
|
+
* level becomes the highest advertised level below it, or the lowest advertised level when none is
|
|
3527
|
+
* below it. A model that advertises no levels gets none, since Copilot rejects the field for it.
|
|
3528
|
+
*/
|
|
3529
|
+
const clampReasoningEffort = (requested, advertised) => {
|
|
3530
|
+
if (advertised?.length === 0) return;
|
|
3531
|
+
const accepted = advertised ?? copilotReasoningEfforts;
|
|
3532
|
+
if (accepted.includes(requested) || effortRank(requested) === -1) return requested;
|
|
3533
|
+
const ranked = accepted.filter((effort) => effortRank(effort) !== -1).sort((left, right) => effortRank(left) - effortRank(right));
|
|
3534
|
+
return ranked.filter((effort) => effortRank(effort) <= effortRank(requested)).at(-1) ?? ranked[0];
|
|
3535
|
+
};
|
|
3377
3536
|
function resolveModelReasoningEffort(config, model, requested) {
|
|
3378
|
-
|
|
3537
|
+
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
3538
|
+
if (!advertisesNoReasoningEffort(capabilities$1)) return clampReasoningEffort(resolveReasoningEffort(requested), capabilities$1?.reasoningEfforts);
|
|
3379
3539
|
if (requested === void 0) return;
|
|
3380
3540
|
const message = "The selected upstream model does not advertise reasoning effort support; omit the explicit effort control.";
|
|
3381
3541
|
throw new HTTPError(message, Response.json({
|
|
@@ -3426,12 +3586,16 @@ async function loadCopilotModelCatalog(config) {
|
|
|
3426
3586
|
...supportedEndpoints !== void 0 && { supportedEndpoints },
|
|
3427
3587
|
...typeof capabilities$1?.type === "string" && { type: capabilities$1.type },
|
|
3428
3588
|
...reasoningEfforts !== void 0 && { reasoningEfforts },
|
|
3429
|
-
...typeof model.name === "string" && model.name !== "" && { name: model.name }
|
|
3589
|
+
...typeof model.name === "string" && model.name !== "" && { name: model.name },
|
|
3590
|
+
...typeof model.vendor === "string" && { vendor: model.vendor },
|
|
3591
|
+
...typeof model.model_picker_enabled === "boolean" && { pickerEnabled: model.model_picker_enabled },
|
|
3592
|
+
...typeof model.model_picker_category === "string" && { pickerCategory: model.model_picker_category }
|
|
3430
3593
|
});
|
|
3431
3594
|
}
|
|
3432
3595
|
const catalog = {
|
|
3433
3596
|
baseUrl: provider.baseUrl,
|
|
3434
|
-
models: models$1
|
|
3597
|
+
models: models$1,
|
|
3598
|
+
fetchedAt: Date.now()
|
|
3435
3599
|
};
|
|
3436
3600
|
publishCopilotModelCatalog(config, catalog);
|
|
3437
3601
|
const state = getRuntimeState();
|
|
@@ -3452,10 +3616,14 @@ async function loadCopilotModelCatalog(config) {
|
|
|
3452
3616
|
async function ensureCopilotModelCatalog(config, model) {
|
|
3453
3617
|
if (pinnedCatalogs.has(config)) return;
|
|
3454
3618
|
if (!config.modelCatalog) return;
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
if (
|
|
3458
|
-
|
|
3619
|
+
const catalog = config.modelCatalog;
|
|
3620
|
+
if (catalog.baseUrl === config.copilotBaseUrl) {
|
|
3621
|
+
if (catalog.models.has(model)) return;
|
|
3622
|
+
const now = config.admittedAt ?? Date.now();
|
|
3623
|
+
if (catalog.fetchedAt !== void 0 && now - catalog.fetchedAt < missingModelRecheckMs) return;
|
|
3624
|
+
}
|
|
3625
|
+
await loadCopilotModelCatalog(config);
|
|
3626
|
+
if (config.modelCatalog.baseUrl !== config.copilotBaseUrl) throw new Error("Copilot base URL changed during model discovery; retry the request.");
|
|
3459
3627
|
}
|
|
3460
3628
|
async function boundModelOutputTokens(config, model, requested) {
|
|
3461
3629
|
await ensureCopilotModelCatalog(config, model);
|
|
@@ -4995,17 +5163,32 @@ const incompleteStreamError = () => {
|
|
|
4995
5163
|
headers: { "content-type": "application/json" }
|
|
4996
5164
|
}), message);
|
|
4997
5165
|
};
|
|
5166
|
+
const chunkEvent = (chunk) => {
|
|
5167
|
+
let data;
|
|
5168
|
+
return {
|
|
5169
|
+
chunk,
|
|
5170
|
+
get data() {
|
|
5171
|
+
data ??= JSON.stringify(chunk);
|
|
5172
|
+
return data;
|
|
5173
|
+
}
|
|
5174
|
+
};
|
|
5175
|
+
};
|
|
5176
|
+
/** True for the [DONE] marker. A chunk event answers without serializing its chunk. */
|
|
5177
|
+
const isChatStreamDone = (event) => !event.chunk && event.data === "[DONE]";
|
|
5178
|
+
/** True for an event with neither a chunk nor data, such as a comment or keep-alive. */
|
|
5179
|
+
const isEmptyChatStreamEvent = (event) => !event.chunk && !event.data;
|
|
5180
|
+
/** The chunk an event carries, parsed from its data when it came from elsewhere. */
|
|
5181
|
+
const readChatChunk = (event) => event.chunk ?? JSON.parse(event.data);
|
|
4998
5182
|
async function* normalizeChatCompletionStream(stream) {
|
|
4999
5183
|
let terminal;
|
|
5000
5184
|
let refusal = false;
|
|
5001
5185
|
let usage$1;
|
|
5002
5186
|
for await (const event of stream) {
|
|
5003
5187
|
if (event.event === "error") throw incompleteStreamError();
|
|
5004
|
-
if (
|
|
5005
|
-
if (event.data === "[DONE]") continue;
|
|
5188
|
+
if (isEmptyChatStreamEvent(event) || isChatStreamDone(event)) continue;
|
|
5006
5189
|
let chunk;
|
|
5007
5190
|
try {
|
|
5008
|
-
chunk =
|
|
5191
|
+
chunk = readChatChunk(event);
|
|
5009
5192
|
} catch {
|
|
5010
5193
|
throw incompleteStreamError();
|
|
5011
5194
|
}
|
|
@@ -5030,29 +5213,29 @@ async function* normalizeChatCompletionStream(stream) {
|
|
|
5030
5213
|
delta: {}
|
|
5031
5214
|
}]
|
|
5032
5215
|
};
|
|
5033
|
-
if (Object.keys(choice.delta).length > 0) yield
|
|
5216
|
+
if (Object.keys(choice.delta).length > 0) yield chunkEvent({
|
|
5034
5217
|
...chunk,
|
|
5035
5218
|
choices: [{
|
|
5036
5219
|
...choice,
|
|
5037
5220
|
finish_reason: null
|
|
5038
5221
|
}]
|
|
5039
|
-
})
|
|
5040
|
-
} else yield
|
|
5222
|
+
});
|
|
5223
|
+
} else yield chunkEvent(chunk);
|
|
5041
5224
|
}
|
|
5042
5225
|
if (!terminal) throw incompleteStreamError();
|
|
5043
|
-
yield
|
|
5226
|
+
yield chunkEvent({
|
|
5044
5227
|
...terminal,
|
|
5045
5228
|
...usage$1 && { usage: usage$1 }
|
|
5046
|
-
})
|
|
5229
|
+
});
|
|
5047
5230
|
yield { data: "[DONE]" };
|
|
5048
5231
|
}
|
|
5049
5232
|
async function collectChatCompletionStream(stream, signal, timeoutMs) {
|
|
5050
5233
|
const chunks = [];
|
|
5051
5234
|
try {
|
|
5052
5235
|
for await (const event of stream) {
|
|
5053
|
-
if (event
|
|
5054
|
-
if (
|
|
5055
|
-
const chunk =
|
|
5236
|
+
if (isChatStreamDone(event)) break;
|
|
5237
|
+
if (isEmptyChatStreamEvent(event)) continue;
|
|
5238
|
+
const chunk = readChatChunk(event);
|
|
5056
5239
|
if (!chunk || !Array.isArray(chunk.choices)) throw incompleteStreamError();
|
|
5057
5240
|
chunks.push(chunk);
|
|
5058
5241
|
}
|
|
@@ -5088,12 +5271,12 @@ const resolveWebSearchStreamDecision = async (stream, toolNameMapper, isWebSearc
|
|
|
5088
5271
|
break;
|
|
5089
5272
|
}
|
|
5090
5273
|
const raw = next.value;
|
|
5091
|
-
if (raw
|
|
5274
|
+
if (isChatStreamDone(raw)) {
|
|
5092
5275
|
done = true;
|
|
5093
5276
|
break;
|
|
5094
5277
|
}
|
|
5095
|
-
if (
|
|
5096
|
-
const chunk =
|
|
5278
|
+
if (isEmptyChatStreamEvent(raw)) continue;
|
|
5279
|
+
const chunk = readChatChunk(raw);
|
|
5097
5280
|
if (!chunk || !Array.isArray(chunk.choices)) throw new Error("Invalid upstream chat stream chunk.");
|
|
5098
5281
|
buffered.push(chunk);
|
|
5099
5282
|
if (chunkHasWebSearchCall(chunk, toolNameMapper, isWebSearchToolName)) {
|
|
@@ -5129,9 +5312,9 @@ const resolveWebSearchStreamDecision = async (stream, toolNameMapper, isWebSearc
|
|
|
5129
5312
|
streamedThinking
|
|
5130
5313
|
};
|
|
5131
5314
|
for await (const raw of rest) {
|
|
5132
|
-
if (raw
|
|
5133
|
-
if (
|
|
5134
|
-
const chunk =
|
|
5315
|
+
if (isChatStreamDone(raw)) break;
|
|
5316
|
+
if (isEmptyChatStreamEvent(raw)) continue;
|
|
5317
|
+
const chunk = readChatChunk(raw);
|
|
5135
5318
|
if (!chunk || !Array.isArray(chunk.choices)) throw new Error("Invalid upstream chat stream chunk.");
|
|
5136
5319
|
buffered.push(chunk);
|
|
5137
5320
|
}
|
|
@@ -6055,9 +6238,11 @@ function getUpstreamErrorDetail(response, body) {
|
|
|
6055
6238
|
return body.slice(0, 240);
|
|
6056
6239
|
}
|
|
6057
6240
|
}
|
|
6241
|
+
const responsesRetryCodes = new Set(["unsupported_api_for_model", "no_available_model_endpoints"]);
|
|
6058
6242
|
async function shouldRetryWithResponses(response, signal, timeoutMs) {
|
|
6059
6243
|
try {
|
|
6060
|
-
|
|
6244
|
+
const errorBody = await readCopilotJson(response.clone(), signal, timeoutMs);
|
|
6245
|
+
return responsesRetryCodes.has(errorBody.error?.code ?? "");
|
|
6061
6246
|
} catch {
|
|
6062
6247
|
return false;
|
|
6063
6248
|
}
|
|
@@ -6153,6 +6338,18 @@ function normalizeNativeHistory(messages) {
|
|
|
6153
6338
|
}
|
|
6154
6339
|
return combined;
|
|
6155
6340
|
}
|
|
6341
|
+
const clampSystemEfforts = (config, model, messages) => messages.map((message) => {
|
|
6342
|
+
if (message.role !== "system" || message.output_config?.effort === void 0) return message;
|
|
6343
|
+
const effort = resolveModelReasoningEffort(config, model, message.output_config.effort);
|
|
6344
|
+
if (effort === void 0) return message;
|
|
6345
|
+
return {
|
|
6346
|
+
...message,
|
|
6347
|
+
output_config: {
|
|
6348
|
+
...message.output_config,
|
|
6349
|
+
effort
|
|
6350
|
+
}
|
|
6351
|
+
};
|
|
6352
|
+
});
|
|
6156
6353
|
function shouldUseNativeMessages(config, model) {
|
|
6157
6354
|
return selectCopilotEndpoint(config, model).endpoint === "/v1/messages";
|
|
6158
6355
|
}
|
|
@@ -6191,7 +6388,7 @@ async function createNativeMessages(config, payload, options) {
|
|
|
6191
6388
|
};
|
|
6192
6389
|
const body = {
|
|
6193
6390
|
...payload,
|
|
6194
|
-
messages: normalizeNativeHistory(payload.messages),
|
|
6391
|
+
messages: clampSystemEfforts(config, payload.model, normalizeNativeHistory(payload.messages)),
|
|
6195
6392
|
max_tokens: maxTokens,
|
|
6196
6393
|
stream: Boolean(payload.stream || exceedsNonStreamingLimit),
|
|
6197
6394
|
output_config: outputConfig,
|
|
@@ -6205,7 +6402,8 @@ async function createNativeMessages(config, payload, options) {
|
|
|
6205
6402
|
};
|
|
6206
6403
|
const beta = options.headers?.get("anthropic-beta");
|
|
6207
6404
|
if (beta) headers["anthropic-beta"] = beta;
|
|
6208
|
-
const
|
|
6405
|
+
const advertised = getCachedCopilotModel(config, payload.model)?.reasoningEfforts;
|
|
6406
|
+
const effortLabel = effort === void 0 ? "omitted" : clampReasoningEffort(getClaudeTurnEffort(payload).effective, advertised) ?? "omitted";
|
|
6209
6407
|
const initiator = options.initiator ?? "agent";
|
|
6210
6408
|
log.info(sanitizeTerminalString(`request_id=${options.requestId} Model request client=${options.client ?? "claude"} requested_model=${options.requestedModel ?? payload.model} upstream_model=${payload.model} upstream_api=messages effective_think_effort=${effortLabel} initiator=${initiator}`));
|
|
6211
6409
|
const response = await fetchCopilot(getCopilotProviderContext(config), "/v1/messages", {
|
|
@@ -6701,8 +6899,7 @@ const translationEndpoint = (config, model) => {
|
|
|
6701
6899
|
const selection = selectCopilotEndpoint(config, normalizeCopilotModelId(model));
|
|
6702
6900
|
return selection.endpoint && !selection.responsesFallback ? selection.endpoint : void 0;
|
|
6703
6901
|
};
|
|
6704
|
-
const handleClaudeMessageRequest = async (config, claudePayload, initiator, requestSignal, requestId, writeEvent, requestHeaders) => {
|
|
6705
|
-
const upstreamModel = translateModelName(claudePayload.model);
|
|
6902
|
+
const handleClaudeMessageRequest = async (config, claudePayload, upstreamModel, initiator, requestSignal, requestId, writeEvent, requestHeaders) => {
|
|
6706
6903
|
if (shouldUseNativeMessages(config, upstreamModel)) return handleNativeMessages(config, {
|
|
6707
6904
|
...claudePayload,
|
|
6708
6905
|
model: upstreamModel
|
|
@@ -6715,7 +6912,10 @@ const handleClaudeMessageRequest = async (config, claudePayload, initiator, requ
|
|
|
6715
6912
|
const shouldLetModelDecideWebSearch = hasClaudeWebSearch(claudePayload);
|
|
6716
6913
|
const decisionPayload = shouldLetModelDecideWebSearch ? prepareClaudeWebSearchDecisionPayload(claudePayload) : claudePayload;
|
|
6717
6914
|
const toolNameMapper = createClaudeToolNameMapper(decisionPayload.tools, { ...getToolNameMapperOptionsForModel(upstreamModel) });
|
|
6718
|
-
const openAIPayload = translateToOpenAI(
|
|
6915
|
+
const openAIPayload = translateToOpenAI({
|
|
6916
|
+
...decisionPayload,
|
|
6917
|
+
model: upstreamModel
|
|
6918
|
+
}, void 0, toolNameMapper, { endpoint: translationEndpoint(config, upstreamModel) });
|
|
6719
6919
|
const canStreamWebSearchDecision = shouldLetModelDecideWebSearch && !!writeEvent;
|
|
6720
6920
|
if (shouldLetModelDecideWebSearch && !canStreamWebSearchDecision) openAIPayload.stream = false;
|
|
6721
6921
|
const response = await createChatCompletions(config, openAIPayload, {
|
|
@@ -6848,9 +7048,9 @@ const handleClaudeMessageRequest = async (config, claudePayload, initiator, requ
|
|
|
6848
7048
|
for (const chunk of bufferedChunks) await writeClaudeStreamEvents(translateChunkToClaudeEvents(chunk, streamState, toolNameMapper), writeEvent);
|
|
6849
7049
|
const upstreamStream = remainingStream ?? effectiveResponse;
|
|
6850
7050
|
for await (const rawEvent of upstreamStream) {
|
|
6851
|
-
if (rawEvent
|
|
6852
|
-
if (
|
|
6853
|
-
await writeClaudeStreamEvents(translateChunkToClaudeEvents(
|
|
7051
|
+
if (isChatStreamDone(rawEvent)) break;
|
|
7052
|
+
if (isEmptyChatStreamEvent(rawEvent)) continue;
|
|
7053
|
+
await writeClaudeStreamEvents(translateChunkToClaudeEvents(readChatChunk(rawEvent), streamState, toolNameMapper), writeEvent);
|
|
6854
7054
|
}
|
|
6855
7055
|
};
|
|
6856
7056
|
claudeRoutes.get("/models", (c) => c.json({
|
|
@@ -6877,9 +7077,10 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6877
7077
|
const initiator = c.env?.relayProbe === true ? "agent" : getClaudeRequestInitiator(receivedPayload);
|
|
6878
7078
|
const claudePayload = removeBillingLine(receivedPayload);
|
|
6879
7079
|
const requestSignal = createCopilotRequestSignal(c.req.raw.signal, config.upstreamTimeoutMs);
|
|
7080
|
+
let upstreamModel = "";
|
|
6880
7081
|
try {
|
|
6881
7082
|
getRequestReasoningEffort(claudePayload);
|
|
6882
|
-
|
|
7083
|
+
upstreamModel = translateModelName(claudePayload.model);
|
|
6883
7084
|
const validate = () => {
|
|
6884
7085
|
if (shouldUseNativeMessages(config, upstreamModel)) validateNativeMessages(claudePayload);
|
|
6885
7086
|
else validateClaudeMessages(claudePayload.messages);
|
|
@@ -6889,6 +7090,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6889
7090
|
await ensureCopilotModelCatalog(config, upstreamModel);
|
|
6890
7091
|
requestSignal?.throwIfAborted();
|
|
6891
7092
|
pinCopilotModelCatalog(config);
|
|
7093
|
+
upstreamModel = translateModelName(claudePayload.model);
|
|
6892
7094
|
const selection = requireCopilotEndpoint(config, upstreamModel);
|
|
6893
7095
|
validate();
|
|
6894
7096
|
if (selection.endpoint === "/v1/messages") validateNativeModelEffort(config, upstreamModel, claudePayload);
|
|
@@ -6926,7 +7128,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6926
7128
|
data: JSON.stringify(event)
|
|
6927
7129
|
}));
|
|
6928
7130
|
try {
|
|
6929
|
-
await handleClaudeMessageRequest(config, claudePayload, initiator, requestSignal, requestId, writeEvent, c.req.raw.headers);
|
|
7131
|
+
await handleClaudeMessageRequest(config, claudePayload, upstreamModel, initiator, requestSignal, requestId, writeEvent, c.req.raw.headers);
|
|
6930
7132
|
} catch (error) {
|
|
6931
7133
|
log.error(`request_id=${requestId} Error during Claude stream request:`, error);
|
|
6932
7134
|
if (error instanceof UpstreamToolInputError) trace?.recordFailure("invalid-tool-input");
|
|
@@ -6940,7 +7142,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6940
7142
|
});
|
|
6941
7143
|
}
|
|
6942
7144
|
try {
|
|
6943
|
-
return c.json(await handleClaudeMessageRequest(config, claudePayload, initiator, requestSignal, requestId, void 0, c.req.raw.headers));
|
|
7145
|
+
return c.json(await handleClaudeMessageRequest(config, claudePayload, upstreamModel, initiator, requestSignal, requestId, void 0, c.req.raw.headers));
|
|
6944
7146
|
} catch (error) {
|
|
6945
7147
|
if (error instanceof ProxyNotImplementedError) {
|
|
6946
7148
|
c.set("requestErrorMessage", error.message);
|
|
@@ -6965,18 +7167,18 @@ claudeRoutes.post("/messages/count_tokens", async (c) => {
|
|
|
6965
7167
|
try {
|
|
6966
7168
|
const claudeBeta = c.req.header("claude-beta");
|
|
6967
7169
|
const claudePayload = removeBillingLine(await c.req.json());
|
|
6968
|
-
const
|
|
7170
|
+
const routedModel = translateModelName(claudePayload.model);
|
|
7171
|
+
const openAIPayload = translateToOpenAI(shouldUseNativeMessages(c.get("config"), routedModel) ? {
|
|
6969
7172
|
...claudePayload,
|
|
6970
7173
|
messages: claudePayload.messages.map((message) => {
|
|
6971
7174
|
if (message.role !== "system") return message;
|
|
6972
7175
|
const { output_config: _outputConfig, clear_at: _clearAt,...textMessage } = message;
|
|
6973
7176
|
return textMessage;
|
|
6974
7177
|
})
|
|
6975
|
-
} : claudePayload, void 0, void 0, { endpoint: translationEndpoint(c.get("config"),
|
|
6976
|
-
const exposedModels = getExposedModelIds();
|
|
7178
|
+
} : claudePayload, void 0, void 0, { endpoint: translationEndpoint(c.get("config"), routedModel) });
|
|
6977
7179
|
const upstreamModel = getCachedCopilotModel(c.get("config"), openAIPayload.model);
|
|
6978
7180
|
const hasDiscoveredTokenizer = upstreamModel?.tokenizer !== void 0 && isSupportedTokenizer(upstreamModel.tokenizer);
|
|
6979
|
-
const selectedModel = createTokenCountModel(openAIPayload.model
|
|
7181
|
+
const selectedModel = createTokenCountModel(openAIPayload.model, hasDiscoveredTokenizer ? upstreamModel?.tokenizer : void 0);
|
|
6980
7182
|
const tokenCount = await getTokenCount(openAIPayload, selectedModel);
|
|
6981
7183
|
const effectiveModelId = selectedModel.id;
|
|
6982
7184
|
if (claudePayload.tools && claudePayload.tools.length > 0) {
|
|
@@ -7000,7 +7202,8 @@ claudeRoutes.post("/messages/count_tokens", async (c) => {
|
|
|
7000
7202
|
//#endregion
|
|
7001
7203
|
//#region src/codex/item-ids.ts
|
|
7002
7204
|
const isRecord$7 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7003
|
-
const frameEnd = /\r?\n\r?\n
|
|
7205
|
+
const frameEnd = /\r?\n\r?\n/g;
|
|
7206
|
+
const frameEndOverlap = 3;
|
|
7004
7207
|
/**
|
|
7005
7208
|
* Rewrites SSE frames as they stream so that every event about one output item names it by the same
|
|
7006
7209
|
* id. A frame that needs no change is passed on byte for byte, except that a UTF-8 byte order mark at
|
|
@@ -7012,23 +7215,29 @@ var StableItemIds = class {
|
|
|
7012
7215
|
encoder = new TextEncoder();
|
|
7013
7216
|
ids = /* @__PURE__ */ new Map();
|
|
7014
7217
|
pending = "";
|
|
7218
|
+
searched = 0;
|
|
7015
7219
|
/** The bytes of every frame this chunk completes; a partial frame waits for the next chunk. */
|
|
7016
7220
|
push(chunk) {
|
|
7017
7221
|
this.pending += this.decoder.decode(chunk, { stream: true });
|
|
7018
7222
|
let output = "";
|
|
7223
|
+
let frameStart = 0;
|
|
7224
|
+
frameEnd.lastIndex = Math.max(0, this.searched - frameEndOverlap);
|
|
7019
7225
|
let match = frameEnd.exec(this.pending);
|
|
7020
7226
|
while (match) {
|
|
7021
7227
|
const end = match.index + match[0].length;
|
|
7022
|
-
output += this.rewriteFrame(this.pending.slice(
|
|
7023
|
-
|
|
7228
|
+
output += this.rewriteFrame(this.pending.slice(frameStart, end));
|
|
7229
|
+
frameStart = end;
|
|
7024
7230
|
match = frameEnd.exec(this.pending);
|
|
7025
7231
|
}
|
|
7232
|
+
this.pending = this.pending.slice(frameStart);
|
|
7233
|
+
this.searched = this.pending.length;
|
|
7026
7234
|
return this.encoder.encode(output);
|
|
7027
7235
|
}
|
|
7028
7236
|
/** Whatever is left when the stream ends, unchanged: an incomplete frame has no event to fix. */
|
|
7029
7237
|
flush() {
|
|
7030
7238
|
const rest = this.pending + this.decoder.decode();
|
|
7031
7239
|
this.pending = "";
|
|
7240
|
+
this.searched = 0;
|
|
7032
7241
|
return this.encoder.encode(rest);
|
|
7033
7242
|
}
|
|
7034
7243
|
rewriteFrame(frame) {
|
|
@@ -7108,187 +7317,6 @@ const decodeThinking = (encrypted) => {
|
|
|
7108
7317
|
};
|
|
7109
7318
|
};
|
|
7110
7319
|
|
|
7111
|
-
//#endregion
|
|
7112
|
-
//#region src/codex/request.ts
|
|
7113
|
-
const copilotReasoningEfforts = [
|
|
7114
|
-
"none",
|
|
7115
|
-
"minimal",
|
|
7116
|
-
"low",
|
|
7117
|
-
"medium",
|
|
7118
|
-
"high",
|
|
7119
|
-
"xhigh",
|
|
7120
|
-
"max"
|
|
7121
|
-
];
|
|
7122
|
-
const effortOrder = [
|
|
7123
|
-
"none",
|
|
7124
|
-
"minimal",
|
|
7125
|
-
"low",
|
|
7126
|
-
"medium",
|
|
7127
|
-
"high",
|
|
7128
|
-
"xhigh",
|
|
7129
|
-
"max",
|
|
7130
|
-
"ultra"
|
|
7131
|
-
];
|
|
7132
|
-
const isRecord$5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7133
|
-
const codexRequestError = (message, code, status$1 = 400) => new HTTPError(message, Response.json({ error: {
|
|
7134
|
-
message,
|
|
7135
|
-
type: "invalid_request_error",
|
|
7136
|
-
code
|
|
7137
|
-
} }, { status: status$1 }), message);
|
|
7138
|
-
const parseCodexPayload = (text$1) => {
|
|
7139
|
-
let body;
|
|
7140
|
-
try {
|
|
7141
|
-
body = JSON.parse(text$1);
|
|
7142
|
-
} catch {
|
|
7143
|
-
throw codexRequestError("The request body is not valid JSON.", "invalid_json");
|
|
7144
|
-
}
|
|
7145
|
-
if (!isRecord$5(body) || typeof body.model !== "string" || body.model.trim() === "") throw codexRequestError("The request needs a model.", "missing_model");
|
|
7146
|
-
if (typeof body.input !== "string" && !Array.isArray(body.input)) throw codexRequestError("The request needs an input string or array.", "missing_input");
|
|
7147
|
-
return body;
|
|
7148
|
-
};
|
|
7149
|
-
const catalogDescribesUpstream = (config) => config.modelCatalog?.baseUrl === config.copilotBaseUrl;
|
|
7150
|
-
/**
|
|
7151
|
-
* The Copilot model a Codex request is sent to, and the API that serves it.
|
|
7152
|
-
*
|
|
7153
|
-
* A model Copilot's catalog lists is used as named, so Codex's own model IDs reach the same
|
|
7154
|
-
* Copilot model. A GPT model listed with /responses is forwarded there; a Claude model listed with
|
|
7155
|
-
* /v1/messages is translated to Messages, because Copilot serves Claude only there and on Chat
|
|
7156
|
-
* Completions. A name the catalog does not list, such as Codex's internal codex-auto-review, goes
|
|
7157
|
-
* to the configured gptModel, as Claude Code's aliases do. Without a catalog a non-Claude name is
|
|
7158
|
-
* forwarded unchanged.
|
|
7159
|
-
*/
|
|
7160
|
-
const resolveCodexModel = async (config, requested) => {
|
|
7161
|
-
const normalized = normalizeCopilotModelId(requested);
|
|
7162
|
-
await ensureCopilotModelCatalog(config, normalized);
|
|
7163
|
-
pinCopilotModelCatalog(config);
|
|
7164
|
-
if (isClaudeModelId(normalized)) {
|
|
7165
|
-
const capabilities$2 = getCachedCopilotModel(config, normalized);
|
|
7166
|
-
if (capabilities$2 === void 0) throw codexRequestError(`${requested} is not in Copilot's model list. Check copilot-relay models for the exact Claude model ID.`, "relay_unsupported_model");
|
|
7167
|
-
if (capabilities$2.supportedEndpoints !== void 0 && !capabilities$2.supportedEndpoints.includes("/v1/messages")) throw codexRequestError(`${normalized} does not serve Claude Messages on Copilot, which Codex needs for Claude models.`, "relay_unsupported_endpoint");
|
|
7168
|
-
return {
|
|
7169
|
-
model: normalized,
|
|
7170
|
-
api: "messages"
|
|
7171
|
-
};
|
|
7172
|
-
}
|
|
7173
|
-
const model = !(getCachedCopilotModel(config, normalized) !== void 0) && catalogDescribesUpstream(config) ? getModelRouting().gptModel : normalized;
|
|
7174
|
-
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
7175
|
-
const notChat = capabilities$1?.type !== void 0 && capabilities$1.type !== "chat";
|
|
7176
|
-
const noResponses = capabilities$1?.supportedEndpoints !== void 0 && !capabilities$1.supportedEndpoints.includes("/responses");
|
|
7177
|
-
if (notChat || noResponses) throw codexRequestError(`${model} does not serve the Responses API on Copilot. Choose a model that lists /responses in copilot-relay models.`, "relay_unsupported_endpoint");
|
|
7178
|
-
return {
|
|
7179
|
-
model,
|
|
7180
|
-
api: "responses"
|
|
7181
|
-
};
|
|
7182
|
-
};
|
|
7183
|
-
const effortRank = (effort) => effortOrder.indexOf(effort);
|
|
7184
|
-
/**
|
|
7185
|
-
* The reasoning effort to send for a requested one.
|
|
7186
|
-
*
|
|
7187
|
-
* A level the model advertises, or one this relay does not know, is sent as requested. Any other
|
|
7188
|
-
* level becomes the highest advertised level below it, or the lowest advertised level when none is
|
|
7189
|
-
* below it. A model that advertises no levels gets none, since Copilot rejects the field for it.
|
|
7190
|
-
*/
|
|
7191
|
-
const clampReasoningEffort = (requested, advertised) => {
|
|
7192
|
-
if (advertised?.length === 0) return;
|
|
7193
|
-
const accepted = advertised ?? copilotReasoningEfforts;
|
|
7194
|
-
if (accepted.includes(requested) || effortRank(requested) === -1) return requested;
|
|
7195
|
-
const ranked = accepted.filter((effort) => effortRank(effort) !== -1).sort((left, right) => effortRank(left) - effortRank(right));
|
|
7196
|
-
return ranked.filter((effort) => effortRank(effort) <= effortRank(requested)).at(-1) ?? ranked[0];
|
|
7197
|
-
};
|
|
7198
|
-
/**
|
|
7199
|
-
* The body sent to Copilot for a Codex request.
|
|
7200
|
-
*
|
|
7201
|
-
* Copilot answers HTTP 400 to `service_tier`, to an `image_generation` tool, and to an effort it
|
|
7202
|
-
* does not accept, so those change. Reasoning items holding the relay's Claude thinking are removed.
|
|
7203
|
-
* Every other field, including Codex's prompt_cache_key and GPT's encrypted reasoning, is forwarded
|
|
7204
|
-
* unchanged.
|
|
7205
|
-
*/
|
|
7206
|
-
const prepareCodexPayload = (payload, model, capabilities$1) => {
|
|
7207
|
-
const prepared = {
|
|
7208
|
-
...payload,
|
|
7209
|
-
model
|
|
7210
|
-
};
|
|
7211
|
-
const removed$1 = [];
|
|
7212
|
-
if (Object.hasOwn(prepared, "service_tier")) {
|
|
7213
|
-
delete prepared.service_tier;
|
|
7214
|
-
removed$1.push("service_tier");
|
|
7215
|
-
}
|
|
7216
|
-
if (Array.isArray(prepared.input)) {
|
|
7217
|
-
const input = prepared.input.filter((item) => !(isRecord$5(item) && item.type === "reasoning" && isRelayThinking(item.encrypted_content)));
|
|
7218
|
-
if (input.length !== prepared.input.length) {
|
|
7219
|
-
prepared.input = input;
|
|
7220
|
-
removed$1.push("claude_reasoning");
|
|
7221
|
-
}
|
|
7222
|
-
}
|
|
7223
|
-
if (Array.isArray(prepared.tools)) {
|
|
7224
|
-
const tools = prepared.tools.filter((tool) => !(isRecord$5(tool) && tool.type === "image_generation"));
|
|
7225
|
-
if (tools.length !== prepared.tools.length) {
|
|
7226
|
-
prepared.tools = tools;
|
|
7227
|
-
removed$1.push("image_generation");
|
|
7228
|
-
}
|
|
7229
|
-
}
|
|
7230
|
-
const reasoning = isRecord$5(prepared.reasoning) ? prepared.reasoning : void 0;
|
|
7231
|
-
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
7232
|
-
let effectiveEffort = requestedEffort;
|
|
7233
|
-
if (reasoning !== void 0 && requestedEffort !== void 0) {
|
|
7234
|
-
effectiveEffort = clampReasoningEffort(requestedEffort, capabilities$1?.reasoningEfforts);
|
|
7235
|
-
if (effectiveEffort === void 0) {
|
|
7236
|
-
const withoutEffort = { ...reasoning };
|
|
7237
|
-
delete withoutEffort.effort;
|
|
7238
|
-
prepared.reasoning = withoutEffort;
|
|
7239
|
-
} else if (effectiveEffort !== requestedEffort) prepared.reasoning = {
|
|
7240
|
-
...reasoning,
|
|
7241
|
-
effort: effectiveEffort
|
|
7242
|
-
};
|
|
7243
|
-
}
|
|
7244
|
-
return {
|
|
7245
|
-
payload: prepared,
|
|
7246
|
-
removed: removed$1,
|
|
7247
|
-
requestedEffort,
|
|
7248
|
-
effectiveEffort
|
|
7249
|
-
};
|
|
7250
|
-
};
|
|
7251
|
-
/**
|
|
7252
|
-
* Who started a Codex request, for Copilot's x-initiator header.
|
|
7253
|
-
*
|
|
7254
|
-
* A request continues the agent's own work unless its last input item is a user message: after a
|
|
7255
|
-
* tool call the last item is the tool's output. Codex marks its subagents (review, compaction,
|
|
7256
|
-
* memory consolidation, spawned agents) with x-openai-subagent, and those are always the agent.
|
|
7257
|
-
*/
|
|
7258
|
-
const getCodexInitiator = (input, subagent) => {
|
|
7259
|
-
if (subagent !== void 0) return "agent";
|
|
7260
|
-
if (typeof input === "string") return "user";
|
|
7261
|
-
const last = Array.isArray(input) ? input.at(-1) : void 0;
|
|
7262
|
-
return isRecord$5(last) && last.role === "user" && (last.type === void 0 || last.type === "message") ? "user" : "agent";
|
|
7263
|
-
};
|
|
7264
|
-
const includesImage = (value) => {
|
|
7265
|
-
if (Array.isArray(value)) return value.some(includesImage);
|
|
7266
|
-
if (!isRecord$5(value)) return false;
|
|
7267
|
-
return value.type === "input_image" || Object.values(value).some(includesImage);
|
|
7268
|
-
};
|
|
7269
|
-
/**
|
|
7270
|
-
* A stream holding only `response.failed` with code `context_length_exceeded`.
|
|
7271
|
-
*
|
|
7272
|
-
* Copilot reports a prompt over the model's input limit as HTTP 400, which Codex treats as a fatal
|
|
7273
|
-
* error. Codex recognizes a context-window overflow only from this event, and answers it by
|
|
7274
|
-
* compacting the conversation.
|
|
7275
|
-
*/
|
|
7276
|
-
const contextOverflowStream = (requestId, message) => {
|
|
7277
|
-
const event = {
|
|
7278
|
-
type: "response.failed",
|
|
7279
|
-
response: {
|
|
7280
|
-
id: `resp_relay_${requestId}`,
|
|
7281
|
-
object: "response",
|
|
7282
|
-
status: "failed",
|
|
7283
|
-
error: {
|
|
7284
|
-
code: "context_length_exceeded",
|
|
7285
|
-
message
|
|
7286
|
-
}
|
|
7287
|
-
}
|
|
7288
|
-
};
|
|
7289
|
-
return `event: response.failed\ndata: ${JSON.stringify(event)}\n\n`;
|
|
7290
|
-
};
|
|
7291
|
-
|
|
7292
7320
|
//#endregion
|
|
7293
7321
|
//#region src/codex/messages.ts
|
|
7294
7322
|
const claudeReasoningEfforts = [
|
|
@@ -7299,7 +7327,7 @@ const claudeReasoningEfforts = [
|
|
|
7299
7327
|
"max"
|
|
7300
7328
|
];
|
|
7301
7329
|
const defaultMaxTokens = 32e3;
|
|
7302
|
-
const isRecord$
|
|
7330
|
+
const isRecord$5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7303
7331
|
const nameLimit = 64;
|
|
7304
7332
|
/**
|
|
7305
7333
|
* Claude tool names for Codex tools, and the way back.
|
|
@@ -7344,14 +7372,14 @@ const customInputSchema = {
|
|
|
7344
7372
|
};
|
|
7345
7373
|
const customDescription = (tool) => {
|
|
7346
7374
|
const description = typeof tool.description === "string" ? tool.description : "";
|
|
7347
|
-
const format = isRecord$
|
|
7375
|
+
const format = isRecord$5(tool.format) ? tool.format : void 0;
|
|
7348
7376
|
if (format?.type !== "grammar" || typeof format.definition !== "string") return description;
|
|
7349
7377
|
return `${description}\n\nPut the raw input in the input field. It must match this ${String(format.syntax ?? "")} grammar:\n${format.definition}`.trim();
|
|
7350
7378
|
};
|
|
7351
7379
|
const functionTool = (tool, name) => ({
|
|
7352
7380
|
name,
|
|
7353
7381
|
...typeof tool.description === "string" && tool.description && { description: tool.description },
|
|
7354
|
-
input_schema: isRecord$
|
|
7382
|
+
input_schema: isRecord$5(tool.parameters) ? tool.parameters : {
|
|
7355
7383
|
type: "object",
|
|
7356
7384
|
properties: {}
|
|
7357
7385
|
}
|
|
@@ -7378,10 +7406,10 @@ const convertTools = (tools, names, dropped) => {
|
|
|
7378
7406
|
if (!Array.isArray(tools)) return [];
|
|
7379
7407
|
const converted = [];
|
|
7380
7408
|
for (const tool of tools) {
|
|
7381
|
-
if (!isRecord$
|
|
7409
|
+
if (!isRecord$5(tool) || typeof tool.type !== "string") continue;
|
|
7382
7410
|
if (tool.type === "namespace" && typeof tool.name === "string" && Array.isArray(tool.tools)) {
|
|
7383
7411
|
for (const inner of tool.tools) {
|
|
7384
|
-
if (!isRecord$
|
|
7412
|
+
if (!isRecord$5(inner)) continue;
|
|
7385
7413
|
const claudeTool$1 = convertTool(inner, names, tool.name);
|
|
7386
7414
|
if (claudeTool$1) converted.push(claudeTool$1);
|
|
7387
7415
|
else dropped.add(`tool:${tool.name}.${String(inner.type)}`);
|
|
@@ -7420,7 +7448,7 @@ const contentBlocks = (content, allowImages) => {
|
|
|
7420
7448
|
if (!Array.isArray(content)) return [];
|
|
7421
7449
|
const blocks = [];
|
|
7422
7450
|
for (const part of content) {
|
|
7423
|
-
if (!isRecord$
|
|
7451
|
+
if (!isRecord$5(part)) continue;
|
|
7424
7452
|
const text$1 = part.type === "refusal" ? part.refusal : part.text;
|
|
7425
7453
|
if ((part.type === "input_text" || part.type === "output_text" || part.type === "refusal") && typeof text$1 === "string" && text$1) blocks.push({
|
|
7426
7454
|
type: "text",
|
|
@@ -7458,7 +7486,7 @@ const parseArguments = (value) => {
|
|
|
7458
7486
|
if (typeof value !== "string") return {};
|
|
7459
7487
|
try {
|
|
7460
7488
|
const parsed = JSON.parse(value);
|
|
7461
|
-
return isRecord$
|
|
7489
|
+
return isRecord$5(parsed) ? parsed : {};
|
|
7462
7490
|
} catch {
|
|
7463
7491
|
return {};
|
|
7464
7492
|
}
|
|
@@ -7483,7 +7511,7 @@ const toolChoice = (choice, parallel, names) => {
|
|
|
7483
7511
|
let selected;
|
|
7484
7512
|
if (choice === "required") selected = { type: "any" };
|
|
7485
7513
|
else if (choice === "none") selected = { type: "none" };
|
|
7486
|
-
else if (isRecord$
|
|
7514
|
+
else if (isRecord$5(choice) && choice.type === "function" && typeof choice.name === "string") selected = {
|
|
7487
7515
|
type: "tool",
|
|
7488
7516
|
name: names.claudeName({
|
|
7489
7517
|
kind: "function",
|
|
@@ -7495,8 +7523,8 @@ const toolChoice = (choice, parallel, names) => {
|
|
|
7495
7523
|
return selected;
|
|
7496
7524
|
};
|
|
7497
7525
|
const jsonSchemaFormat = (text$1) => {
|
|
7498
|
-
const format = isRecord$
|
|
7499
|
-
if (format?.type !== "json_schema" || !isRecord$
|
|
7526
|
+
const format = isRecord$5(text$1) && isRecord$5(text$1.format) ? text$1.format : void 0;
|
|
7527
|
+
if (format?.type !== "json_schema" || !isRecord$5(format.schema)) return;
|
|
7500
7528
|
return `Your final answer must be a single JSON value that matches this JSON Schema, with no other text and no code fence:\n${JSON.stringify(format.schema)}`;
|
|
7501
7529
|
};
|
|
7502
7530
|
/**
|
|
@@ -7528,7 +7556,7 @@ const toClaudeRequest = (payload, model, capabilities$1) => {
|
|
|
7528
7556
|
content: payload.input
|
|
7529
7557
|
}] : payload.input;
|
|
7530
7558
|
for (const item of items) {
|
|
7531
|
-
if (!isRecord$
|
|
7559
|
+
if (!isRecord$5(item)) continue;
|
|
7532
7560
|
const type = typeof item.type === "string" ? item.type : "message";
|
|
7533
7561
|
if (type === "message" && (item.role === "developer" || item.role === "system")) {
|
|
7534
7562
|
const blocks = contentBlocks(item.content, false);
|
|
@@ -7560,7 +7588,7 @@ const toClaudeRequest = (payload, model, capabilities$1) => {
|
|
|
7560
7588
|
}
|
|
7561
7589
|
for (const turn of turns) if (turn.role === "user") turn.content.sort((left, right) => Number(right.type === "tool_result") - Number(left.type === "tool_result"));
|
|
7562
7590
|
markCacheBreakpoints(system, tools, turns);
|
|
7563
|
-
const reasoning = isRecord$
|
|
7591
|
+
const reasoning = isRecord$5(payload.reasoning) ? payload.reasoning : void 0;
|
|
7564
7592
|
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
7565
7593
|
const advertised = capabilities$1?.reasoningEfforts?.filter((effort) => claudeReasoningEfforts.includes(effort));
|
|
7566
7594
|
const effectiveEffort = requestedEffort === void 0 ? void 0 : clampReasoningEffort(requestedEffort, advertised ?? claudeReasoningEfforts);
|
|
@@ -7591,7 +7619,7 @@ const callItem = (block, status$1) => {
|
|
|
7591
7619
|
let input = block.json;
|
|
7592
7620
|
try {
|
|
7593
7621
|
const parsed = JSON.parse(block.json);
|
|
7594
|
-
if (isRecord$
|
|
7622
|
+
if (isRecord$5(parsed) && typeof parsed.input === "string") input = parsed.input;
|
|
7595
7623
|
} catch {}
|
|
7596
7624
|
return {
|
|
7597
7625
|
type: "custom_tool_call",
|
|
@@ -7902,11 +7930,11 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7902
7930
|
};
|
|
7903
7931
|
}
|
|
7904
7932
|
if (event.type === "message_start") {
|
|
7905
|
-
const message = isRecord$
|
|
7933
|
+
const message = isRecord$5(event.message) ? event.message : {};
|
|
7906
7934
|
if (typeof message.id === "string") responseId = `resp_${message.id}`;
|
|
7907
7935
|
usage$1 = {
|
|
7908
7936
|
...usage$1,
|
|
7909
|
-
...isRecord$
|
|
7937
|
+
...isRecord$5(message.usage) ? message.usage : {}
|
|
7910
7938
|
};
|
|
7911
7939
|
yield {
|
|
7912
7940
|
type: "response.created",
|
|
@@ -7916,7 +7944,7 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7916
7944
|
type: "response.in_progress",
|
|
7917
7945
|
response: response("in_progress")
|
|
7918
7946
|
};
|
|
7919
|
-
} else if (event.type === "content_block_start" && typeof event.index === "number" && isRecord$
|
|
7947
|
+
} else if (event.type === "content_block_start" && typeof event.index === "number" && isRecord$5(event.content_block)) {
|
|
7920
7948
|
const block = newBlock(event.content_block, names, `${responseId}_call_${event.index}`);
|
|
7921
7949
|
if (block?.kind === "call" && !holding) {
|
|
7922
7950
|
holding = true;
|
|
@@ -7934,7 +7962,7 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7934
7962
|
yield* startEvents(placed);
|
|
7935
7963
|
yield* deltaEvents(placed, streamed(placed));
|
|
7936
7964
|
}
|
|
7937
|
-
} else if (event.type === "content_block_delta" && typeof event.index === "number" && isRecord$
|
|
7965
|
+
} else if (event.type === "content_block_delta" && typeof event.index === "number" && isRecord$5(event.delta)) {
|
|
7938
7966
|
const entry = open.get(event.index);
|
|
7939
7967
|
if (entry !== void 0) {
|
|
7940
7968
|
const piece = applyDelta(entry.block, event.delta);
|
|
@@ -7949,10 +7977,10 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7949
7977
|
yield* doneEvents(entry.placed, item);
|
|
7950
7978
|
}
|
|
7951
7979
|
} else if (event.type === "message_delta") {
|
|
7952
|
-
if (isRecord$
|
|
7980
|
+
if (isRecord$5(event.delta) && typeof event.delta.stop_reason === "string") stopReason = event.delta.stop_reason;
|
|
7953
7981
|
usage$1 = {
|
|
7954
7982
|
...usage$1,
|
|
7955
|
-
...isRecord$
|
|
7983
|
+
...isRecord$5(event.usage) ? event.usage : {}
|
|
7956
7984
|
};
|
|
7957
7985
|
} else if (event.type === "message_stop") {
|
|
7958
7986
|
stopped = true;
|
|
@@ -8000,7 +8028,7 @@ const collectCodexResponse = async (events$1) => {
|
|
|
8000
8028
|
message: "Claude stream produced no response."
|
|
8001
8029
|
}
|
|
8002
8030
|
};
|
|
8003
|
-
for await (const event of events$1) if ((event.type === "response.completed" || event.type === "response.incomplete" || event.type === "response.failed") && isRecord$
|
|
8031
|
+
for await (const event of events$1) if ((event.type === "response.completed" || event.type === "response.incomplete" || event.type === "response.failed") && isRecord$5(event.response)) final = event.response;
|
|
8004
8032
|
return final;
|
|
8005
8033
|
};
|
|
8006
8034
|
/** Responses events as SSE bytes. */
|
|
@@ -8010,11 +8038,158 @@ async function* encodeSse(events$1) {
|
|
|
8010
8038
|
}
|
|
8011
8039
|
|
|
8012
8040
|
//#endregion
|
|
8013
|
-
//#region src/
|
|
8014
|
-
const
|
|
8015
|
-
const
|
|
8016
|
-
|
|
8017
|
-
"
|
|
8041
|
+
//#region src/codex/request.ts
|
|
8042
|
+
const isRecord$4 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8043
|
+
const codexRequestError = (message, code, status$1 = 400) => new HTTPError(message, Response.json({ error: {
|
|
8044
|
+
message,
|
|
8045
|
+
type: "invalid_request_error",
|
|
8046
|
+
code
|
|
8047
|
+
} }, { status: status$1 }), message);
|
|
8048
|
+
const parseCodexPayload = (text$1) => {
|
|
8049
|
+
let body;
|
|
8050
|
+
try {
|
|
8051
|
+
body = JSON.parse(text$1);
|
|
8052
|
+
} catch {
|
|
8053
|
+
throw codexRequestError("The request body is not valid JSON.", "invalid_json");
|
|
8054
|
+
}
|
|
8055
|
+
if (!isRecord$4(body) || typeof body.model !== "string" || body.model.trim() === "") throw codexRequestError("The request needs a model.", "missing_model");
|
|
8056
|
+
if (typeof body.input !== "string" && !Array.isArray(body.input)) throw codexRequestError("The request needs an input string or array.", "missing_input");
|
|
8057
|
+
return body;
|
|
8058
|
+
};
|
|
8059
|
+
const catalogDescribesUpstream = (config) => config.modelCatalog?.baseUrl === config.copilotBaseUrl;
|
|
8060
|
+
/**
|
|
8061
|
+
* The Copilot model a Codex request is sent to, and the API that serves it.
|
|
8062
|
+
*
|
|
8063
|
+
* A model Copilot's catalog lists is used as named, so Codex's own model IDs reach the same
|
|
8064
|
+
* Copilot model. A GPT model listed with /responses is forwarded there; a Claude model listed with
|
|
8065
|
+
* /v1/messages is translated to Messages, because Copilot serves Claude only there and on Chat
|
|
8066
|
+
* Completions. A name the catalog does not list, such as Codex's internal codex-auto-review, goes
|
|
8067
|
+
* to the configured gptModel, as Claude Code's aliases do. Without a catalog a non-Claude name is
|
|
8068
|
+
* forwarded unchanged.
|
|
8069
|
+
*/
|
|
8070
|
+
const resolveCodexModel = async (config, requested) => {
|
|
8071
|
+
const normalized = normalizeCopilotModelId(requested);
|
|
8072
|
+
await ensureCopilotModelCatalog(config, normalized);
|
|
8073
|
+
pinCopilotModelCatalog(config);
|
|
8074
|
+
if (isClaudeModelId(normalized)) {
|
|
8075
|
+
const capabilities$2 = getCachedCopilotModel(config, normalized);
|
|
8076
|
+
if (capabilities$2 === void 0) throw codexRequestError(`${requested} is not in Copilot's model list. Check copilot-relay models for the exact Claude model ID.`, "relay_unsupported_model");
|
|
8077
|
+
if (capabilities$2.supportedEndpoints !== void 0 && !capabilities$2.supportedEndpoints.includes("/v1/messages")) throw codexRequestError(`${normalized} does not serve Claude Messages on Copilot, which Codex needs for Claude models.`, "relay_unsupported_endpoint");
|
|
8078
|
+
return {
|
|
8079
|
+
model: normalized,
|
|
8080
|
+
api: "messages"
|
|
8081
|
+
};
|
|
8082
|
+
}
|
|
8083
|
+
const model = !(getCachedCopilotModel(config, normalized) !== void 0) && catalogDescribesUpstream(config) ? getModelRouting().gptModel : normalized;
|
|
8084
|
+
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
8085
|
+
const notChat = capabilities$1?.type !== void 0 && capabilities$1.type !== "chat";
|
|
8086
|
+
const noResponses = capabilities$1?.supportedEndpoints !== void 0 && !capabilities$1.supportedEndpoints.includes("/responses");
|
|
8087
|
+
if (notChat || noResponses) throw codexRequestError(`${model} does not serve the Responses API on Copilot. Choose a model that lists /responses in copilot-relay models.`, "relay_unsupported_endpoint");
|
|
8088
|
+
return {
|
|
8089
|
+
model,
|
|
8090
|
+
api: "responses"
|
|
8091
|
+
};
|
|
8092
|
+
};
|
|
8093
|
+
/**
|
|
8094
|
+
* The body sent to Copilot for a Codex request.
|
|
8095
|
+
*
|
|
8096
|
+
* Copilot answers HTTP 400 to `service_tier`, to an `image_generation` tool, and to an effort it
|
|
8097
|
+
* does not accept, so those change. Reasoning items holding the relay's Claude thinking are removed.
|
|
8098
|
+
* Every other field, including Codex's prompt_cache_key and GPT's encrypted reasoning, is forwarded
|
|
8099
|
+
* unchanged.
|
|
8100
|
+
*/
|
|
8101
|
+
const prepareCodexPayload = (payload, model, capabilities$1) => {
|
|
8102
|
+
const prepared = {
|
|
8103
|
+
...payload,
|
|
8104
|
+
model
|
|
8105
|
+
};
|
|
8106
|
+
const removed$1 = [];
|
|
8107
|
+
if (Object.hasOwn(prepared, "service_tier")) {
|
|
8108
|
+
delete prepared.service_tier;
|
|
8109
|
+
removed$1.push("service_tier");
|
|
8110
|
+
}
|
|
8111
|
+
if (Array.isArray(prepared.input)) {
|
|
8112
|
+
const input = prepared.input.filter((item) => !(isRecord$4(item) && item.type === "reasoning" && isRelayThinking(item.encrypted_content)));
|
|
8113
|
+
if (input.length !== prepared.input.length) {
|
|
8114
|
+
prepared.input = input;
|
|
8115
|
+
removed$1.push("claude_reasoning");
|
|
8116
|
+
}
|
|
8117
|
+
}
|
|
8118
|
+
if (Array.isArray(prepared.tools)) {
|
|
8119
|
+
const tools = prepared.tools.filter((tool) => !(isRecord$4(tool) && tool.type === "image_generation"));
|
|
8120
|
+
if (tools.length !== prepared.tools.length) {
|
|
8121
|
+
prepared.tools = tools;
|
|
8122
|
+
removed$1.push("image_generation");
|
|
8123
|
+
}
|
|
8124
|
+
}
|
|
8125
|
+
const reasoning = isRecord$4(prepared.reasoning) ? prepared.reasoning : void 0;
|
|
8126
|
+
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
8127
|
+
let effectiveEffort = requestedEffort;
|
|
8128
|
+
if (reasoning !== void 0 && requestedEffort !== void 0) {
|
|
8129
|
+
effectiveEffort = clampReasoningEffort(requestedEffort, capabilities$1?.reasoningEfforts);
|
|
8130
|
+
if (effectiveEffort === void 0) {
|
|
8131
|
+
const withoutEffort = { ...reasoning };
|
|
8132
|
+
delete withoutEffort.effort;
|
|
8133
|
+
prepared.reasoning = withoutEffort;
|
|
8134
|
+
} else if (effectiveEffort !== requestedEffort) prepared.reasoning = {
|
|
8135
|
+
...reasoning,
|
|
8136
|
+
effort: effectiveEffort
|
|
8137
|
+
};
|
|
8138
|
+
}
|
|
8139
|
+
return {
|
|
8140
|
+
payload: prepared,
|
|
8141
|
+
removed: removed$1,
|
|
8142
|
+
requestedEffort,
|
|
8143
|
+
effectiveEffort
|
|
8144
|
+
};
|
|
8145
|
+
};
|
|
8146
|
+
/**
|
|
8147
|
+
* Who started a Codex request, for Copilot's x-initiator header.
|
|
8148
|
+
*
|
|
8149
|
+
* A request continues the agent's own work unless its last input item is a user message: after a
|
|
8150
|
+
* tool call the last item is the tool's output. Codex marks its subagents (review, compaction,
|
|
8151
|
+
* memory consolidation, spawned agents) with x-openai-subagent, and those are always the agent.
|
|
8152
|
+
*/
|
|
8153
|
+
const getCodexInitiator = (input, subagent) => {
|
|
8154
|
+
if (subagent !== void 0) return "agent";
|
|
8155
|
+
if (typeof input === "string") return "user";
|
|
8156
|
+
const last = Array.isArray(input) ? input.at(-1) : void 0;
|
|
8157
|
+
return isRecord$4(last) && last.role === "user" && (last.type === void 0 || last.type === "message") ? "user" : "agent";
|
|
8158
|
+
};
|
|
8159
|
+
const includesImage = (value) => {
|
|
8160
|
+
if (Array.isArray(value)) return value.some(includesImage);
|
|
8161
|
+
if (!isRecord$4(value)) return false;
|
|
8162
|
+
return value.type === "input_image" || Object.values(value).some(includesImage);
|
|
8163
|
+
};
|
|
8164
|
+
/**
|
|
8165
|
+
* A stream holding only `response.failed` with code `context_length_exceeded`.
|
|
8166
|
+
*
|
|
8167
|
+
* Copilot reports a prompt over the model's input limit as HTTP 400, which Codex treats as a fatal
|
|
8168
|
+
* error. Codex recognizes a context-window overflow only from this event, and answers it by
|
|
8169
|
+
* compacting the conversation.
|
|
8170
|
+
*/
|
|
8171
|
+
const contextOverflowStream = (requestId, message) => {
|
|
8172
|
+
const event = {
|
|
8173
|
+
type: "response.failed",
|
|
8174
|
+
response: {
|
|
8175
|
+
id: `resp_relay_${requestId}`,
|
|
8176
|
+
object: "response",
|
|
8177
|
+
status: "failed",
|
|
8178
|
+
error: {
|
|
8179
|
+
code: "context_length_exceeded",
|
|
8180
|
+
message
|
|
8181
|
+
}
|
|
8182
|
+
}
|
|
8183
|
+
};
|
|
8184
|
+
return `event: response.failed\ndata: ${JSON.stringify(event)}\n\n`;
|
|
8185
|
+
};
|
|
8186
|
+
|
|
8187
|
+
//#endregion
|
|
8188
|
+
//#region src/routes/responses.ts
|
|
8189
|
+
const responsesRoutes = new Hono();
|
|
8190
|
+
const forwardedHeaders = [
|
|
8191
|
+
"content-type",
|
|
8192
|
+
"retry-after",
|
|
8018
8193
|
"openai-model",
|
|
8019
8194
|
"x-request-id",
|
|
8020
8195
|
"x-github-request-id",
|
|
@@ -9023,8 +9198,9 @@ const validateManifest = (value) => {
|
|
|
9023
9198
|
const runtime = value.runtime;
|
|
9024
9199
|
requireValid(record(runtime) && (runtime.debug === void 0 || typeof runtime.debug === "boolean"));
|
|
9025
9200
|
requireValid(runtime.thinkEffort === void 0 || isConfiguredReasoningEffort(runtime.thinkEffort));
|
|
9026
|
-
if (runtime.modelRouting !== void 0) requireValid(record(runtime.modelRouting) && text(runtime.modelRouting.gptModel) && text(runtime.modelRouting.opusModel));
|
|
9201
|
+
if (runtime.modelRouting !== void 0) requireValid(record(runtime.modelRouting) && text(runtime.modelRouting.gptModel) && text(runtime.modelRouting.opusModel) && (runtime.modelRouting.models === void 0 || runtime.modelRouting.models === "auto" || Array.isArray(runtime.modelRouting.models) && runtime.modelRouting.models.every((id) => text(id))));
|
|
9027
9202
|
requireValid(runtime.catalogCurrent === void 0 || typeof runtime.catalogCurrent === "boolean");
|
|
9203
|
+
requireValid(runtime.catalogAgeMs === void 0 || typeof runtime.catalogAgeMs === "number" && Number.isSafeInteger(runtime.catalogAgeMs) && runtime.catalogAgeMs >= 0);
|
|
9028
9204
|
if (runtime.models !== void 0) {
|
|
9029
9205
|
requireValid(Array.isArray(runtime.models) && runtime.models.length <= 1e4);
|
|
9030
9206
|
const ids = /* @__PURE__ */ new Set();
|
|
@@ -9309,15 +9485,18 @@ const replayCapture = async (target) => {
|
|
|
9309
9485
|
requireValid(bytes.length === body.bytes);
|
|
9310
9486
|
files.set(body.file, bytes);
|
|
9311
9487
|
}
|
|
9488
|
+
const admittedAt = Date.now();
|
|
9312
9489
|
const catalog = manifest.runtime.models === void 0 ? void 0 : {
|
|
9313
9490
|
baseUrl: manifest.runtime.catalogCurrent ? "https://replay.invalid" : "https://old.invalid",
|
|
9314
|
-
models: new Map(manifest.runtime.models)
|
|
9491
|
+
models: new Map(manifest.runtime.models),
|
|
9492
|
+
...typeof manifest.runtime.catalogAgeMs === "number" && { fetchedAt: admittedAt - manifest.runtime.catalogAgeMs }
|
|
9315
9493
|
};
|
|
9316
9494
|
const config = {
|
|
9317
9495
|
host: "localhost",
|
|
9318
9496
|
port: 0,
|
|
9319
9497
|
copilotBaseUrl: "https://replay.invalid",
|
|
9320
9498
|
copilotToken: "offline-replay-token",
|
|
9499
|
+
admittedAt,
|
|
9321
9500
|
upstreamTimeoutMs: manifest.config.upstreamTimeoutMs,
|
|
9322
9501
|
vsCodeVersion: manifest.config.vsCodeVersion,
|
|
9323
9502
|
webSearchBackend: manifest.config.webSearchBackend,
|
|
@@ -9975,10 +10154,11 @@ async function applyClaudeConfig(input) {
|
|
|
9975
10154
|
const previousBaseUrl = typeof env.ANTHROPIC_BASE_URL === "string" ? env.ANTHROPIC_BASE_URL : void 0;
|
|
9976
10155
|
const hadPrimaryModelOverride = hasPrimaryModelOverride(parsed, env);
|
|
9977
10156
|
const contextWindow = input.gptLimits?.max_context_window_tokens;
|
|
9978
|
-
const
|
|
10157
|
+
const windowOf = (model) => input.contextWindowOf?.(normalizeCopilotModelId(model));
|
|
10158
|
+
const claudeGptModel = normalizeClaudeModelId(gptModel, windowOf(gptModel) ?? contextWindow);
|
|
9979
10159
|
const shouldSeedTopLevelModel = !hadPrimaryModelOverride;
|
|
9980
|
-
const nextModel = typeof parsed.model === "string" ? normalizeClaudeModelId(parsed.model,
|
|
9981
|
-
for (const key of knownModelEnvKeys) if (typeof env[key] === "string") env[key] = normalizeClaudeModelId(env[key],
|
|
10160
|
+
const nextModel = typeof parsed.model === "string" ? normalizeClaudeModelId(parsed.model, windowOf(parsed.model)) : void 0;
|
|
10161
|
+
for (const key of knownModelEnvKeys) if (typeof env[key] === "string") env[key] = normalizeClaudeModelId(env[key], windowOf(env[key]));
|
|
9982
10162
|
env.ANTHROPIC_BASE_URL = baseUrl;
|
|
9983
10163
|
if (input.apiKey) env.ANTHROPIC_AUTH_TOKEN = input.apiKey;
|
|
9984
10164
|
else if (typeof env.ANTHROPIC_AUTH_TOKEN !== "string" || !env.ANTHROPIC_AUTH_TOKEN) env.ANTHROPIC_AUTH_TOKEN = "dummy";
|
|
@@ -10376,311 +10556,49 @@ const applyCodexSetup = async (input) => {
|
|
|
10376
10556
|
};
|
|
10377
10557
|
|
|
10378
10558
|
//#endregion
|
|
10379
|
-
//#region src/lib/
|
|
10380
|
-
const
|
|
10381
|
-
const
|
|
10382
|
-
|
|
10383
|
-
|
|
10384
|
-
|
|
10385
|
-
|
|
10386
|
-
const catalog = await loadCopilotModelCatalog(config);
|
|
10387
|
-
const requiredModels = getUpstreamModelIds();
|
|
10388
|
-
const missingModels = requiredModels.filter((model) => !catalog.models.has(model));
|
|
10389
|
-
if (missingModels.length > 0) throw new Error(`Required Copilot model(s) unavailable upstream: ${missingModels.join(", ")}`);
|
|
10390
|
-
log.info(`Upstream models available: ${requiredModels.join(", ")}`);
|
|
10391
|
-
for (const model of requiredModels) {
|
|
10392
|
-
const limits = catalog.models.get(model)?.limits;
|
|
10393
|
-
if (limits) log.info(`Model token limits: model=${model} context=${limits.max_context_window_tokens} input=${limits.max_prompt_tokens} output=${limits.max_output_tokens} non_streaming_output=${limits.max_non_streaming_output_tokens ?? limits.max_output_tokens}`);
|
|
10394
|
-
else log.error(`Model token limits unavailable: model=${model}; preserving client budgets without assuming a capacity.`);
|
|
10395
|
-
}
|
|
10396
|
-
};
|
|
10397
|
-
const createProbePayload = (model) => ({
|
|
10398
|
-
model,
|
|
10399
|
-
max_tokens: 16,
|
|
10400
|
-
stream: false,
|
|
10401
|
-
messages: [{
|
|
10402
|
-
role: "user",
|
|
10403
|
-
content: "Reply with OK only."
|
|
10404
|
-
}]
|
|
10559
|
+
//#region src/lib/claude-app.ts
|
|
10560
|
+
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10561
|
+
const configIdPattern = /^[a-f0-9-]{36}$/;
|
|
10562
|
+
const currentPlatform = () => ({
|
|
10563
|
+
platform: process.platform,
|
|
10564
|
+
env: process.env,
|
|
10565
|
+
home: os.homedir()
|
|
10405
10566
|
});
|
|
10406
|
-
|
|
10407
|
-
|
|
10408
|
-
|
|
10409
|
-
|
|
10410
|
-
|
|
10411
|
-
|
|
10412
|
-
|
|
10413
|
-
|
|
10414
|
-
timeoutMs: config.upstreamTimeoutMs
|
|
10415
|
-
});
|
|
10416
|
-
if (typeof response !== "object" || response === null || !("choices" in response)) throw new Error(`Preflight request for ${model} unexpectedly streamed`);
|
|
10417
|
-
}
|
|
10418
|
-
log.info(`Preflight OK: model=${model} think_effort=${effortLabel}`);
|
|
10419
|
-
} catch (error) {
|
|
10420
|
-
if (error instanceof HTTPError) {
|
|
10421
|
-
const responseBody = await error.response.text().catch(() => "");
|
|
10422
|
-
throw new Error(`Preflight failed for model=${model} think_effort=${effortLabel}: ${error.response.status} ${error.response.statusText}${responseBody ? ` ${responseBody}` : ""}`);
|
|
10423
|
-
}
|
|
10424
|
-
throw error;
|
|
10567
|
+
/**
|
|
10568
|
+
* The app's third-party data directory: Electron's userData for "Claude" with "-3p" appended, or
|
|
10569
|
+
* %LOCALAPPDATA%\Claude-3p on Windows.
|
|
10570
|
+
*/
|
|
10571
|
+
const claudeAppDataDir = ({ platform, env, home } = currentPlatform()) => {
|
|
10572
|
+
if (platform === "win32") {
|
|
10573
|
+
const localAppData = env.LOCALAPPDATA || path.win32.join(home, "AppData", "Local");
|
|
10574
|
+
return path.win32.join(localAppData, "Claude-3p");
|
|
10425
10575
|
}
|
|
10576
|
+
if (platform === "darwin") return path.posix.join(home, "Library", "Application Support", "Claude-3p");
|
|
10577
|
+
const configHome = env.XDG_CONFIG_HOME || path.posix.join(home, ".config");
|
|
10578
|
+
return path.posix.join(configHome, "Claude-3p");
|
|
10426
10579
|
};
|
|
10427
|
-
const
|
|
10428
|
-
|
|
10429
|
-
|
|
10430
|
-
|
|
10431
|
-
|
|
10432
|
-
|
|
10433
|
-
|
|
10434
|
-
|
|
10435
|
-
|
|
10580
|
+
const standardDataDir = (dataDir) => dataDir.replace(/-3p$/, "");
|
|
10581
|
+
const otherVendorMarker = /ark-code|astron|command-r|deepseek|doubao|gemini|gemma|glm|gpt|grok|hermes|hy3|kimi|lfm|\bling\b|llama|longcat|mimo|minimax|mistral|mixtral|moonshot|nemotron|openai|phi-|qianfan|qwen|tc-code|\bunic\b|yi-|stepfun|step-3|seed-|bytedance|hunyuan|granite|amazon\.nova|nova-|devstral|ministral|ernie|codex|arcee|trinity|abab|phi\d|\bk2\.|\bm2\.|jamba|arctic|solar|mercury|zamba|kat-coder|\bds-|dpsk/;
|
|
10582
|
+
const looksAnthropic = (id) => {
|
|
10583
|
+
const lower = id.toLowerCase();
|
|
10584
|
+
return !otherVendorMarker.test(lower) && [
|
|
10585
|
+
"claude",
|
|
10586
|
+
"anthropic",
|
|
10587
|
+
"opus",
|
|
10588
|
+
"sonnet",
|
|
10589
|
+
"haiku",
|
|
10590
|
+
"fable",
|
|
10591
|
+
"mythos"
|
|
10592
|
+
].some((word) => lower.includes(word));
|
|
10436
10593
|
};
|
|
10437
|
-
|
|
10438
|
-
//#endregion
|
|
10439
|
-
//#region src/start.ts
|
|
10440
|
-
const canCloseConnections = (server) => typeof server.closeIdleConnections === "function" && typeof server.closeAllConnections === "function";
|
|
10594
|
+
const routesToTier = (name, tier) => name.toLowerCase().includes("opus") === (tier === "opus");
|
|
10441
10595
|
/**
|
|
10442
|
-
* The
|
|
10443
|
-
*
|
|
10444
|
-
*
|
|
10445
|
-
*
|
|
10446
|
-
|
|
10447
|
-
|
|
10448
|
-
if (config.apiKey || isLoopbackHost(config.host)) return;
|
|
10449
|
-
return terminalText(`apiKey is empty and host ${config.host} is not a loopback address: any client that can reach port ${config.port} can consume your Copilot usage. Set apiKey in ${paths.configPath}, or bind host to 127.0.0.1.`);
|
|
10450
|
-
};
|
|
10451
|
-
/**
|
|
10452
|
-
* The warning for a config.yaml that other local users can access while it holds the apiKey.
|
|
10453
|
-
*
|
|
10454
|
-
* A new config.yaml is created 0600, but an existing one keeps its mode, and the relay never
|
|
10455
|
-
* changes the mode of the user's own file. stat follows a symlink, so the mode checked is the
|
|
10456
|
-
* target's. Windows has no POSIX mode bits. It never prints a key.
|
|
10457
|
-
*/
|
|
10458
|
-
const exposedConfigWarning = (config, configPath$1 = paths.configPath) => {
|
|
10459
|
-
if (!config.apiKey || process.platform === "win32") return;
|
|
10460
|
-
let mode;
|
|
10461
|
-
try {
|
|
10462
|
-
mode = statSync(configPath$1).mode & 511;
|
|
10463
|
-
} catch {
|
|
10464
|
-
return;
|
|
10465
|
-
}
|
|
10466
|
-
if ((mode & 63) === 0) return;
|
|
10467
|
-
return terminalText(`${configPath$1} holds the apiKey and has mode ${mode.toString(8).padStart(4, "0")}, which lets other local users access it. Run chmod 600 ${configPath$1}.`);
|
|
10468
|
-
};
|
|
10469
|
-
async function startRelay(appConfig) {
|
|
10470
|
-
appConfig ??= await readAppConfig();
|
|
10471
|
-
appConfig = {
|
|
10472
|
-
...appConfig,
|
|
10473
|
-
thinkEffort: normalizeThinkEffort(appConfig.thinkEffort) ?? defaultReasoningEffort
|
|
10474
|
-
};
|
|
10475
|
-
setLogLevel(appConfig.logLevel);
|
|
10476
|
-
await cleanupLogs(appConfig.logRetentionDays);
|
|
10477
|
-
await cleanupCaptures(appConfig.logRetentionDays);
|
|
10478
|
-
configureUpstreamDispatcher(appConfig.upstreamProxy);
|
|
10479
|
-
const claudeConfigPath = getClaudeConfigPath();
|
|
10480
|
-
const config = readProxyConfig(appConfig);
|
|
10481
|
-
const applyRuntimeConfig = (nextConfig) => {
|
|
10482
|
-
registerSensitiveOrigin(nextConfig.copilotBaseUrl);
|
|
10483
|
-
registerLogSecret(nextConfig.apiKey);
|
|
10484
|
-
setLogLevel(nextConfig.logLevel);
|
|
10485
|
-
cleanupLogs(nextConfig.logRetentionDays).catch(() => log.error("Log retention failed; existing logs retained."));
|
|
10486
|
-
cleanupCaptures(nextConfig.logRetentionDays);
|
|
10487
|
-
if (nextConfig.logLevel === "debug" && !runtimeState.debug) log.info("Debug capture stores full request and response bodies privately; prompts may contain secrets. Do not share captures without review.");
|
|
10488
|
-
runtimeState.debug = nextConfig.logLevel === "debug";
|
|
10489
|
-
runtimeState.thinkEffort = nextConfig.thinkEffort;
|
|
10490
|
-
config.copilotBaseUrl = nextConfig.copilotBaseUrl;
|
|
10491
|
-
runtimeState.upstreamBaseUrl = nextConfig.copilotBaseUrl;
|
|
10492
|
-
config.host = nextConfig.host;
|
|
10493
|
-
config.port = nextConfig.port;
|
|
10494
|
-
config.upstreamTimeoutMs = nextConfig.upstreamTimeoutSeconds * 1e3;
|
|
10495
|
-
config.webSearchBackend = nextConfig.webSearchBackend;
|
|
10496
|
-
config.claudeUpstreamApi = nextConfig.claudeUpstreamApi;
|
|
10497
|
-
config.apiKey = nextConfig.apiKey;
|
|
10498
|
-
runtimeState.modelRouting = {
|
|
10499
|
-
gptModel: nextConfig.gptModel,
|
|
10500
|
-
opusModel: nextConfig.opusModel
|
|
10501
|
-
};
|
|
10502
|
-
};
|
|
10503
|
-
applyRuntimeConfig(appConfig);
|
|
10504
|
-
log.info(`Log level: ${appConfig.logLevel}`);
|
|
10505
|
-
log.info(`Default think effort: ${appConfig.thinkEffort}`);
|
|
10506
|
-
log.info(`Upstream timeout: ${appConfig.upstreamTimeoutSeconds}s`);
|
|
10507
|
-
if (appConfig.upstreamProxy !== void 0) log.info(`Upstream proxy: ${formatUpstreamProxyForDisplay(appConfig.upstreamProxy)}`);
|
|
10508
|
-
const listenerWarning = unauthenticatedListenerWarning(appConfig);
|
|
10509
|
-
if (listenerWarning) log.error(listenerWarning);
|
|
10510
|
-
const configWarning = exposedConfigWarning(appConfig);
|
|
10511
|
-
if (configWarning) log.error(configWarning);
|
|
10512
|
-
const authSession = await setupProxyAuth(config);
|
|
10513
|
-
try {
|
|
10514
|
-
await validateUpstream(config, appConfig.thinkEffort);
|
|
10515
|
-
} catch (error) {
|
|
10516
|
-
log.error("Startup preflight failed:", error);
|
|
10517
|
-
process.exit(1);
|
|
10518
|
-
}
|
|
10519
|
-
log.info(`Exposed models: ${getExposedModelIds().join(", ")}`);
|
|
10520
|
-
try {
|
|
10521
|
-
await preloadTokenizers(getUpstreamModelIds().map((id) => getCachedCopilotModel(config, id)?.tokenizer));
|
|
10522
|
-
log.info(`Tokenizers loaded: ${loadedTokenizers().join(", ")}`);
|
|
10523
|
-
} catch (error) {
|
|
10524
|
-
log.error("Tokenizer preload failed; count_tokens loads tokenizers on first use:", error);
|
|
10525
|
-
}
|
|
10526
|
-
const server = await startServer(config);
|
|
10527
|
-
await writeRelayPidFile(config);
|
|
10528
|
-
log.info(`copilot-relay version: ${appVersion}`);
|
|
10529
|
-
if (authSession.githubLogin) log.info(`GitHub user: ${authSession.githubLogin}`);
|
|
10530
|
-
else log.error("GitHub user: unavailable");
|
|
10531
|
-
const baseUrl = getRelayBaseUrl(config.host, config.port);
|
|
10532
|
-
log.info(`copilot-relay listening on ${baseUrl}`);
|
|
10533
|
-
log.info(`copilot base url: ${formatUrlForDisplay(config.copilotBaseUrl)}`);
|
|
10534
|
-
if (appConfig.claudeSetup) try {
|
|
10535
|
-
const gptLimits = getCachedCopilotModel(config, appConfig.gptModel)?.limits;
|
|
10536
|
-
const opusLimits = getCachedCopilotModel(config, appConfig.opusModel)?.limits;
|
|
10537
|
-
const claudeResult = await applyClaudeConfig({
|
|
10538
|
-
apiKey: appConfig.apiKey,
|
|
10539
|
-
baseUrl,
|
|
10540
|
-
configPath: claudeConfigPath,
|
|
10541
|
-
gptModel: appConfig.gptModel,
|
|
10542
|
-
gptLimits,
|
|
10543
|
-
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
10544
|
-
});
|
|
10545
|
-
if (claudeResult.changed) {
|
|
10546
|
-
log.info(`claude settings ${claudeResult.created ? "created" : "updated"}: ${claudeResult.configPath}`);
|
|
10547
|
-
if (claudeResult.previousBaseUrl && claudeResult.previousBaseUrl !== baseUrl) log.info(`ANTHROPIC_BASE_URL: ${claudeResult.previousBaseUrl} → ${baseUrl}`);
|
|
10548
|
-
} else log.info(`claude settings already up to date: ${claudeResult.configPath}`);
|
|
10549
|
-
} catch (error) {
|
|
10550
|
-
log.error(`Could not update claude settings (${claudeConfigPath}):`, error);
|
|
10551
|
-
}
|
|
10552
|
-
if (appConfig.codexSetup) {
|
|
10553
|
-
const codexHome = getCodexHome();
|
|
10554
|
-
try {
|
|
10555
|
-
const codexResult = await applyCodexSetup({
|
|
10556
|
-
apiKey: appConfig.apiKey,
|
|
10557
|
-
baseUrl,
|
|
10558
|
-
catalogPath: paths.codexCatalogPath,
|
|
10559
|
-
codexHome,
|
|
10560
|
-
models: [appConfig.gptModel, appConfig.opusModel].map((id) => ({
|
|
10561
|
-
id,
|
|
10562
|
-
capabilities: getCachedCopilotModel(config, id)
|
|
10563
|
-
}))
|
|
10564
|
-
});
|
|
10565
|
-
if (codexResult.skipped) log.info(`codex setup skipped: ${codexHome} does not exist`);
|
|
10566
|
-
else if (codexResult.changed) log.info(`codex settings updated: ${codexResult.configPath}`);
|
|
10567
|
-
else log.info(`codex settings already up to date: ${codexResult.configPath}`);
|
|
10568
|
-
} catch (error) {
|
|
10569
|
-
log.error(`Could not update codex settings (${codexHome}):`, error);
|
|
10570
|
-
}
|
|
10571
|
-
}
|
|
10572
|
-
const configWatcher = watchAppConfig((nextConfig) => {
|
|
10573
|
-
const previousApiKey = config.apiKey;
|
|
10574
|
-
applyRuntimeConfig(nextConfig);
|
|
10575
|
-
log.info(`Config reloaded: logLevel=${nextConfig.logLevel} thinkEffort=${nextConfig.thinkEffort} upstreamTimeoutSeconds=${nextConfig.upstreamTimeoutSeconds}`);
|
|
10576
|
-
const reloadedConfigWarning = nextConfig.apiKey === previousApiKey ? void 0 : exposedConfigWarning(nextConfig);
|
|
10577
|
-
if (reloadedConfigWarning) log.error(reloadedConfigWarning);
|
|
10578
|
-
});
|
|
10579
|
-
const shutdownGraceMs = 2e3;
|
|
10580
|
-
const shutdown = (signal) => {
|
|
10581
|
-
log.info(`Received ${signal}; shutting down copilot-relay`);
|
|
10582
|
-
server.close((error) => {
|
|
10583
|
-
if (error) {
|
|
10584
|
-
log.error("Error while shutting down copilot-relay:", error);
|
|
10585
|
-
process.exitCode = 1;
|
|
10586
|
-
}
|
|
10587
|
-
});
|
|
10588
|
-
if (canCloseConnections(server)) {
|
|
10589
|
-
server.closeIdleConnections();
|
|
10590
|
-
setTimeout(() => {
|
|
10591
|
-
log.info("Grace period elapsed; closing remaining connections");
|
|
10592
|
-
server.closeAllConnections();
|
|
10593
|
-
}, shutdownGraceMs).unref();
|
|
10594
|
-
}
|
|
10595
|
-
};
|
|
10596
|
-
process.once("SIGINT", shutdown);
|
|
10597
|
-
process.once("SIGTERM", shutdown);
|
|
10598
|
-
try {
|
|
10599
|
-
await new Promise((resolve$1, reject) => {
|
|
10600
|
-
server.on("close", resolve$1);
|
|
10601
|
-
server.on("error", reject);
|
|
10602
|
-
});
|
|
10603
|
-
} finally {
|
|
10604
|
-
process.off("SIGINT", shutdown);
|
|
10605
|
-
process.off("SIGTERM", shutdown);
|
|
10606
|
-
clearInterval(configWatcher);
|
|
10607
|
-
await clearRelayPidFile();
|
|
10608
|
-
await flushCaptures();
|
|
10609
|
-
await flushLogs();
|
|
10610
|
-
}
|
|
10611
|
-
}
|
|
10612
|
-
const start = defineCommand({
|
|
10613
|
-
meta: {
|
|
10614
|
-
name: "start",
|
|
10615
|
-
description: "Start the copilot-relay HTTP server."
|
|
10616
|
-
},
|
|
10617
|
-
async run() {
|
|
10618
|
-
await startRelay();
|
|
10619
|
-
}
|
|
10620
|
-
});
|
|
10621
|
-
|
|
10622
|
-
//#endregion
|
|
10623
|
-
//#region src/restart.ts
|
|
10624
|
-
const restart = defineCommand({
|
|
10625
|
-
meta: {
|
|
10626
|
-
name: "restart",
|
|
10627
|
-
description: "Stop any existing copilot-relay instance and start a new one."
|
|
10628
|
-
},
|
|
10629
|
-
async run() {
|
|
10630
|
-
const appConfig = await readAppConfig();
|
|
10631
|
-
setLogLevel(appConfig.logLevel);
|
|
10632
|
-
await cleanupLogs(appConfig.logRetentionDays);
|
|
10633
|
-
const stopped = await stopExistingRelay(readProxyConfig(appConfig));
|
|
10634
|
-
if (stopped.length > 0) log.info(`Stopped copilot-relay pid(s): ${stopped.join(", ")}`);
|
|
10635
|
-
log.info("Starting copilot-relay");
|
|
10636
|
-
await startRelay(appConfig);
|
|
10637
|
-
}
|
|
10638
|
-
});
|
|
10639
|
-
|
|
10640
|
-
//#endregion
|
|
10641
|
-
//#region src/lib/claude-app.ts
|
|
10642
|
-
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10643
|
-
const configIdPattern = /^[a-f0-9-]{36}$/;
|
|
10644
|
-
const currentPlatform = () => ({
|
|
10645
|
-
platform: process.platform,
|
|
10646
|
-
env: process.env,
|
|
10647
|
-
home: os.homedir()
|
|
10648
|
-
});
|
|
10649
|
-
/**
|
|
10650
|
-
* The app's third-party data directory: Electron's userData for "Claude" with "-3p" appended, or
|
|
10651
|
-
* %LOCALAPPDATA%\Claude-3p on Windows.
|
|
10652
|
-
*/
|
|
10653
|
-
const claudeAppDataDir = ({ platform, env, home } = currentPlatform()) => {
|
|
10654
|
-
if (platform === "win32") {
|
|
10655
|
-
const localAppData = env.LOCALAPPDATA || path.win32.join(home, "AppData", "Local");
|
|
10656
|
-
return path.win32.join(localAppData, "Claude-3p");
|
|
10657
|
-
}
|
|
10658
|
-
if (platform === "darwin") return path.posix.join(home, "Library", "Application Support", "Claude-3p");
|
|
10659
|
-
const configHome = env.XDG_CONFIG_HOME || path.posix.join(home, ".config");
|
|
10660
|
-
return path.posix.join(configHome, "Claude-3p");
|
|
10661
|
-
};
|
|
10662
|
-
const standardDataDir = (dataDir) => dataDir.replace(/-3p$/, "");
|
|
10663
|
-
const otherVendorMarker = /ark-code|astron|command-r|deepseek|doubao|gemini|gemma|glm|gpt|grok|hermes|hy3|kimi|lfm|\bling\b|llama|longcat|mimo|minimax|mistral|mixtral|moonshot|nemotron|openai|phi-|qianfan|qwen|tc-code|\bunic\b|yi-|stepfun|step-3|seed-|bytedance|hunyuan|granite|amazon\.nova|nova-|devstral|ministral|ernie|codex|arcee|trinity|abab|phi\d|\bk2\.|\bm2\.|jamba|arctic|solar|mercury|zamba|kat-coder|\bds-|dpsk/;
|
|
10664
|
-
const looksAnthropic = (id) => {
|
|
10665
|
-
const lower = id.toLowerCase();
|
|
10666
|
-
return !otherVendorMarker.test(lower) && [
|
|
10667
|
-
"claude",
|
|
10668
|
-
"anthropic",
|
|
10669
|
-
"opus",
|
|
10670
|
-
"sonnet",
|
|
10671
|
-
"haiku",
|
|
10672
|
-
"fable",
|
|
10673
|
-
"mythos"
|
|
10674
|
-
].some((word) => lower.includes(word));
|
|
10675
|
-
};
|
|
10676
|
-
const routesToTier = (name, tier) => name.toLowerCase().includes("opus") === (tier === "opus");
|
|
10677
|
-
/**
|
|
10678
|
-
* The name the app sends for a model: its own ID when the app accepts it and the relay routes that
|
|
10679
|
-
* ID to the model's own slot, otherwise a Claude-style route for its tier, such as
|
|
10680
|
-
* claude-sonnet-relay. Each tier therefore has its own name. The relay sends every model name without "opus"
|
|
10681
|
-
* to gptModel and every one with it to opusModel, so the route reaches the same upstream model. The
|
|
10682
|
-
* route names no real Claude model, so the app and its Claude Code take none of that model's
|
|
10683
|
-
* built-in context window or tuning; supports1m and prefer1m on the entry say what the window is.
|
|
10596
|
+
* The name the app sends for a model: its own ID when the app accepts it and the relay routes that
|
|
10597
|
+
* ID to the model's own slot, otherwise a Claude-style route for its tier, such as
|
|
10598
|
+
* claude-sonnet-relay. Each tier therefore has its own name. The relay sends every model name without "opus"
|
|
10599
|
+
* to gptModel and every one with it to opusModel, so the route reaches the same upstream model. The
|
|
10600
|
+
* route names no real Claude model, so the app and its Claude Code take none of that model's
|
|
10601
|
+
* built-in context window or tuning; supports1m and prefer1m on the entry say what the window is.
|
|
10684
10602
|
*/
|
|
10685
10603
|
const appModelName = (model) => looksAnthropic(model.id) && routesToTier(model.id, model.tier) ? model.id : `claude-${model.tier}-relay`;
|
|
10686
10604
|
const inferenceModel = (model) => {
|
|
@@ -10782,6 +10700,11 @@ const applyClaudeAppSetup = async (input) => {
|
|
|
10782
10700
|
};
|
|
10783
10701
|
};
|
|
10784
10702
|
|
|
10703
|
+
//#endregion
|
|
10704
|
+
//#region src/lib/defaults.ts
|
|
10705
|
+
const getClaudeConfigPath = () => path.join(process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), ".claude"), "settings.json");
|
|
10706
|
+
const getCodexHome = () => process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
|
|
10707
|
+
|
|
10785
10708
|
//#endregion
|
|
10786
10709
|
//#region src/lib/client-setup.ts
|
|
10787
10710
|
const setupTargets = [
|
|
@@ -10809,6 +10732,7 @@ const setupClaudeCodeCli = async (context) => {
|
|
|
10809
10732
|
configPath: configPath$1,
|
|
10810
10733
|
gptModel: appConfig.gptModel,
|
|
10811
10734
|
gptLimits,
|
|
10735
|
+
contextWindowOf: (model) => capabilities(context, model)?.limits?.max_context_window_tokens,
|
|
10812
10736
|
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
10813
10737
|
});
|
|
10814
10738
|
if (result.skippedReason) return {
|
|
@@ -10822,6 +10746,15 @@ const setupClaudeCodeCli = async (context) => {
|
|
|
10822
10746
|
status: result.changed ? "updated" : "already up to date"
|
|
10823
10747
|
};
|
|
10824
10748
|
};
|
|
10749
|
+
/** Codex's models: the exposed upstream IDs without [1m], gptModel first as Codex's default. */
|
|
10750
|
+
const codexModels = (appConfig, catalog) => resolveExposedModels({
|
|
10751
|
+
gptModel: appConfig.gptModel,
|
|
10752
|
+
opusModel: appConfig.opusModel,
|
|
10753
|
+
models: parseModelSelection(appConfig.models)
|
|
10754
|
+
}, catalog?.models).map((id) => ({
|
|
10755
|
+
id,
|
|
10756
|
+
capabilities: catalog?.models.get(id)
|
|
10757
|
+
}));
|
|
10825
10758
|
const setupCodex = async (context, target) => {
|
|
10826
10759
|
const { appConfig } = context;
|
|
10827
10760
|
const result = await applyCodexSetup({
|
|
@@ -10829,10 +10762,7 @@ const setupCodex = async (context, target) => {
|
|
|
10829
10762
|
baseUrl: context.baseUrl,
|
|
10830
10763
|
catalogPath: paths.codexCatalogPath,
|
|
10831
10764
|
codexHome: getCodexHome(),
|
|
10832
|
-
models:
|
|
10833
|
-
id,
|
|
10834
|
-
capabilities: capabilities(context, id)
|
|
10835
|
-
})),
|
|
10765
|
+
models: codexModels(appConfig, context.catalog),
|
|
10836
10766
|
force: context.force
|
|
10837
10767
|
});
|
|
10838
10768
|
if (result.skipped) return {
|
|
@@ -10846,6 +10776,26 @@ const setupCodex = async (context, target) => {
|
|
|
10846
10776
|
status: result.changed ? "updated" : "already up to date"
|
|
10847
10777
|
};
|
|
10848
10778
|
};
|
|
10779
|
+
/**
|
|
10780
|
+
* `start`'s Codex setup when codexSetup is on: the same setup as `copilot-relay setup`, never forced,
|
|
10781
|
+
* so a machine where Codex has not run is skipped. A failure is logged rather than stopping the relay.
|
|
10782
|
+
*/
|
|
10783
|
+
const startupCodexSetup = async (appConfig, catalog, baseUrl) => {
|
|
10784
|
+
const codexHome = getCodexHome();
|
|
10785
|
+
try {
|
|
10786
|
+
const outcome = await setupCodex({
|
|
10787
|
+
appConfig,
|
|
10788
|
+
baseUrl,
|
|
10789
|
+
catalog,
|
|
10790
|
+
force: false
|
|
10791
|
+
}, "codex");
|
|
10792
|
+
if (outcome.status.startsWith("skipped")) log.info(`codex setup skipped: ${codexHome} does not exist`);
|
|
10793
|
+
else if (outcome.status === "updated") log.info(`codex settings updated: ${outcome.path}`);
|
|
10794
|
+
else log.info(`codex settings already up to date: ${outcome.path}`);
|
|
10795
|
+
} catch (error) {
|
|
10796
|
+
log.error(`Could not update codex settings (${codexHome}):`, error);
|
|
10797
|
+
}
|
|
10798
|
+
};
|
|
10849
10799
|
const setupClaudeCodeApp = async (context) => {
|
|
10850
10800
|
const { appConfig } = context;
|
|
10851
10801
|
const result = await applyClaudeAppSetup({
|
|
@@ -10898,6 +10848,255 @@ const runSetup = async (targets, context) => {
|
|
|
10898
10848
|
return outcomes;
|
|
10899
10849
|
};
|
|
10900
10850
|
|
|
10851
|
+
//#endregion
|
|
10852
|
+
//#region src/lib/preflight.ts
|
|
10853
|
+
const ensureRequiredModels = async (config) => {
|
|
10854
|
+
const catalog = await loadCopilotModelCatalog(config);
|
|
10855
|
+
const requiredModels = getUpstreamModelIds();
|
|
10856
|
+
const missingModels = requiredModels.filter((model) => !catalog.models.has(model));
|
|
10857
|
+
if (missingModels.length > 0) throw new Error(`Required Copilot model(s) unavailable upstream: ${missingModels.join(", ")}`);
|
|
10858
|
+
log.info(`Upstream models available: ${requiredModels.join(", ")}`);
|
|
10859
|
+
const missingSelected = findMissingModels(getModelRouting(), catalog.models);
|
|
10860
|
+
if (missingSelected.length > 0) log.error(`Selected models not in Copilot's catalog, skipped: ${missingSelected.join(", ")}`);
|
|
10861
|
+
for (const model of requiredModels) {
|
|
10862
|
+
const limits = catalog.models.get(model)?.limits;
|
|
10863
|
+
if (limits) log.info(`Model token limits: model=${model} context=${limits.max_context_window_tokens} input=${limits.max_prompt_tokens} output=${limits.max_output_tokens} non_streaming_output=${limits.max_non_streaming_output_tokens ?? limits.max_output_tokens}`);
|
|
10864
|
+
else log.error(`Model token limits unavailable: model=${model}; preserving client budgets without assuming a capacity.`);
|
|
10865
|
+
}
|
|
10866
|
+
};
|
|
10867
|
+
const createProbePayload = (model) => ({
|
|
10868
|
+
model,
|
|
10869
|
+
max_tokens: 16,
|
|
10870
|
+
stream: false,
|
|
10871
|
+
messages: [{
|
|
10872
|
+
role: "user",
|
|
10873
|
+
content: "Reply with OK only."
|
|
10874
|
+
}]
|
|
10875
|
+
});
|
|
10876
|
+
const validateModelRequest = async (config, model) => {
|
|
10877
|
+
const effortLabel = resolveModelReasoningEffort(config, model) ?? "omitted";
|
|
10878
|
+
try {
|
|
10879
|
+
if (requireCopilotEndpoint(config, model).endpoint === "/v1/messages") await handleNativeMessages(config, createProbePayload(model), { requestId: randomUUID() });
|
|
10880
|
+
else {
|
|
10881
|
+
const response = await createChatCompletions(config, createProbePayload(model), {
|
|
10882
|
+
client: "generic",
|
|
10883
|
+
requestedModel: model,
|
|
10884
|
+
timeoutMs: config.upstreamTimeoutMs
|
|
10885
|
+
});
|
|
10886
|
+
if (typeof response !== "object" || response === null || !("choices" in response)) throw new Error(`Preflight request for ${model} unexpectedly streamed`);
|
|
10887
|
+
}
|
|
10888
|
+
log.info(`Preflight OK: model=${model} think_effort=${effortLabel}`);
|
|
10889
|
+
} catch (error) {
|
|
10890
|
+
if (error instanceof HTTPError) {
|
|
10891
|
+
const responseBody = await error.response.text().catch(() => "");
|
|
10892
|
+
throw new Error(`Preflight failed for model=${model} think_effort=${effortLabel}: ${error.response.status} ${error.response.statusText}${responseBody ? ` ${responseBody}` : ""}`);
|
|
10893
|
+
}
|
|
10894
|
+
throw error;
|
|
10895
|
+
}
|
|
10896
|
+
};
|
|
10897
|
+
const validateUpstream = async (config, thinkEffort) => {
|
|
10898
|
+
log.info("Running upstream preflight");
|
|
10899
|
+
await ensureRequiredModels(config);
|
|
10900
|
+
await withRuntimeState({
|
|
10901
|
+
...snapshotRuntimeState(),
|
|
10902
|
+
thinkEffort
|
|
10903
|
+
}, async () => {
|
|
10904
|
+
for (const model of getUpstreamModelIds()) await validateModelRequest(config, model);
|
|
10905
|
+
});
|
|
10906
|
+
};
|
|
10907
|
+
|
|
10908
|
+
//#endregion
|
|
10909
|
+
//#region src/start.ts
|
|
10910
|
+
const canCloseConnections = (server) => typeof server.closeIdleConnections === "function" && typeof server.closeAllConnections === "function";
|
|
10911
|
+
/**
|
|
10912
|
+
* The startup warning for a listener that other machines may reach without a key.
|
|
10913
|
+
*
|
|
10914
|
+
* A warning rather than a refusal, so an existing `host: 0.0.0.0` setup keeps
|
|
10915
|
+
* working after an upgrade. It names the apiKey setting and never prints a key.
|
|
10916
|
+
*/
|
|
10917
|
+
const unauthenticatedListenerWarning = (config) => {
|
|
10918
|
+
if (config.apiKey || isLoopbackHost(config.host)) return;
|
|
10919
|
+
return terminalText(`apiKey is empty and host ${config.host} is not a loopback address: any client that can reach port ${config.port} can consume your Copilot usage. Set apiKey in ${paths.configPath}, or bind host to 127.0.0.1.`);
|
|
10920
|
+
};
|
|
10921
|
+
/**
|
|
10922
|
+
* The warning for a config.yaml that other local users can access while it holds the apiKey.
|
|
10923
|
+
*
|
|
10924
|
+
* A new config.yaml is created 0600, but an existing one keeps its mode, and the relay never
|
|
10925
|
+
* changes the mode of the user's own file. stat follows a symlink, so the mode checked is the
|
|
10926
|
+
* target's. Windows has no POSIX mode bits. It never prints a key.
|
|
10927
|
+
*/
|
|
10928
|
+
const exposedConfigWarning = (config, configPath$1 = paths.configPath) => {
|
|
10929
|
+
if (!config.apiKey || process.platform === "win32") return;
|
|
10930
|
+
let mode;
|
|
10931
|
+
try {
|
|
10932
|
+
mode = statSync(configPath$1).mode & 511;
|
|
10933
|
+
} catch {
|
|
10934
|
+
return;
|
|
10935
|
+
}
|
|
10936
|
+
if ((mode & 63) === 0) return;
|
|
10937
|
+
return terminalText(`${configPath$1} holds the apiKey and has mode ${mode.toString(8).padStart(4, "0")}, which lets other local users access it. Run chmod 600 ${configPath$1}.`);
|
|
10938
|
+
};
|
|
10939
|
+
async function startRelay(appConfig) {
|
|
10940
|
+
appConfig ??= await readAppConfig();
|
|
10941
|
+
appConfig = {
|
|
10942
|
+
...appConfig,
|
|
10943
|
+
thinkEffort: normalizeThinkEffort(appConfig.thinkEffort) ?? defaultReasoningEffort
|
|
10944
|
+
};
|
|
10945
|
+
setLogLevel(appConfig.logLevel);
|
|
10946
|
+
await cleanupLogs(appConfig.logRetentionDays);
|
|
10947
|
+
await cleanupCaptures(appConfig.logRetentionDays);
|
|
10948
|
+
configureUpstreamDispatcher(appConfig.upstreamProxy);
|
|
10949
|
+
const claudeConfigPath = getClaudeConfigPath();
|
|
10950
|
+
const config = readProxyConfig(appConfig);
|
|
10951
|
+
const applyRuntimeConfig = (nextConfig) => {
|
|
10952
|
+
registerSensitiveOrigin(nextConfig.copilotBaseUrl);
|
|
10953
|
+
registerLogSecret(nextConfig.apiKey);
|
|
10954
|
+
setLogLevel(nextConfig.logLevel);
|
|
10955
|
+
cleanupLogs(nextConfig.logRetentionDays).catch(() => log.error("Log retention failed; existing logs retained."));
|
|
10956
|
+
cleanupCaptures(nextConfig.logRetentionDays);
|
|
10957
|
+
if (nextConfig.logLevel === "debug" && !runtimeState.debug) log.info("Debug capture stores full request and response bodies privately; prompts may contain secrets. Do not share captures without review.");
|
|
10958
|
+
runtimeState.debug = nextConfig.logLevel === "debug";
|
|
10959
|
+
runtimeState.thinkEffort = nextConfig.thinkEffort;
|
|
10960
|
+
config.copilotBaseUrl = nextConfig.copilotBaseUrl;
|
|
10961
|
+
runtimeState.upstreamBaseUrl = nextConfig.copilotBaseUrl;
|
|
10962
|
+
config.host = nextConfig.host;
|
|
10963
|
+
config.port = nextConfig.port;
|
|
10964
|
+
config.upstreamTimeoutMs = nextConfig.upstreamTimeoutSeconds * 1e3;
|
|
10965
|
+
config.webSearchBackend = nextConfig.webSearchBackend;
|
|
10966
|
+
config.claudeUpstreamApi = nextConfig.claudeUpstreamApi;
|
|
10967
|
+
config.apiKey = nextConfig.apiKey;
|
|
10968
|
+
runtimeState.modelRouting = {
|
|
10969
|
+
gptModel: nextConfig.gptModel,
|
|
10970
|
+
opusModel: nextConfig.opusModel,
|
|
10971
|
+
models: parseModelSelection(nextConfig.models)
|
|
10972
|
+
};
|
|
10973
|
+
};
|
|
10974
|
+
applyRuntimeConfig(appConfig);
|
|
10975
|
+
log.info(`Log level: ${appConfig.logLevel}`);
|
|
10976
|
+
log.info(`Default think effort: ${appConfig.thinkEffort}`);
|
|
10977
|
+
log.info(`Upstream timeout: ${appConfig.upstreamTimeoutSeconds}s`);
|
|
10978
|
+
if (appConfig.upstreamProxy !== void 0) log.info(`Upstream proxy: ${formatUpstreamProxyForDisplay(appConfig.upstreamProxy)}`);
|
|
10979
|
+
const listenerWarning = unauthenticatedListenerWarning(appConfig);
|
|
10980
|
+
if (listenerWarning) log.error(listenerWarning);
|
|
10981
|
+
const configWarning = exposedConfigWarning(appConfig);
|
|
10982
|
+
if (configWarning) log.error(configWarning);
|
|
10983
|
+
const loginLookup = new AbortController();
|
|
10984
|
+
const authSession = await setupProxyAuth(config, { loginSignal: loginLookup.signal });
|
|
10985
|
+
try {
|
|
10986
|
+
await validateUpstream(config, appConfig.thinkEffort);
|
|
10987
|
+
} catch (error) {
|
|
10988
|
+
log.error("Startup preflight failed:", error);
|
|
10989
|
+
process.exit(1);
|
|
10990
|
+
}
|
|
10991
|
+
log.info(`Exposed models: ${getExposedModelIds().join(", ")}`);
|
|
10992
|
+
try {
|
|
10993
|
+
await preloadTokenizers(getUpstreamModelIds().map((id) => getCachedCopilotModel(config, id)?.tokenizer));
|
|
10994
|
+
log.info(`Tokenizers loaded: ${loadedTokenizers().join(", ")}`);
|
|
10995
|
+
} catch (error) {
|
|
10996
|
+
log.error("Tokenizer preload failed; count_tokens loads tokenizers on first use:", error);
|
|
10997
|
+
}
|
|
10998
|
+
const server = await startServer(config);
|
|
10999
|
+
await writeRelayPidFile(config);
|
|
11000
|
+
log.info(`copilot-relay version: ${appVersion}`);
|
|
11001
|
+
authSession.githubLogin.then((githubLogin) => {
|
|
11002
|
+
if (githubLogin) log.info(`GitHub user: ${githubLogin}`);
|
|
11003
|
+
else log.error("GitHub user: unavailable");
|
|
11004
|
+
});
|
|
11005
|
+
const baseUrl = getRelayBaseUrl(config.host, config.port);
|
|
11006
|
+
log.info(`copilot-relay listening on ${baseUrl}`);
|
|
11007
|
+
log.info(`copilot base url: ${formatUrlForDisplay(config.copilotBaseUrl)}`);
|
|
11008
|
+
if (appConfig.claudeSetup) try {
|
|
11009
|
+
const gptLimits = getCachedCopilotModel(config, appConfig.gptModel)?.limits;
|
|
11010
|
+
const opusLimits = getCachedCopilotModel(config, appConfig.opusModel)?.limits;
|
|
11011
|
+
const claudeResult = await applyClaudeConfig({
|
|
11012
|
+
apiKey: appConfig.apiKey,
|
|
11013
|
+
baseUrl,
|
|
11014
|
+
configPath: claudeConfigPath,
|
|
11015
|
+
gptModel: appConfig.gptModel,
|
|
11016
|
+
gptLimits,
|
|
11017
|
+
contextWindowOf: (model) => getCachedCopilotModel(config, model)?.limits?.max_context_window_tokens,
|
|
11018
|
+
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
11019
|
+
});
|
|
11020
|
+
if (claudeResult.changed) {
|
|
11021
|
+
log.info(`claude settings ${claudeResult.created ? "created" : "updated"}: ${claudeResult.configPath}`);
|
|
11022
|
+
if (claudeResult.previousBaseUrl && claudeResult.previousBaseUrl !== baseUrl) log.info(`ANTHROPIC_BASE_URL: ${claudeResult.previousBaseUrl} → ${baseUrl}`);
|
|
11023
|
+
} else log.info(`claude settings already up to date: ${claudeResult.configPath}`);
|
|
11024
|
+
} catch (error) {
|
|
11025
|
+
log.error(`Could not update claude settings (${claudeConfigPath}):`, error);
|
|
11026
|
+
}
|
|
11027
|
+
if (appConfig.codexSetup) {
|
|
11028
|
+
const catalog = config.modelCatalog?.baseUrl === config.copilotBaseUrl ? config.modelCatalog : void 0;
|
|
11029
|
+
await startupCodexSetup(appConfig, catalog, baseUrl);
|
|
11030
|
+
}
|
|
11031
|
+
const configWatcher = watchAppConfig((nextConfig) => {
|
|
11032
|
+
const previousApiKey = config.apiKey;
|
|
11033
|
+
applyRuntimeConfig(nextConfig);
|
|
11034
|
+
log.info(`Config reloaded: logLevel=${nextConfig.logLevel} thinkEffort=${nextConfig.thinkEffort} upstreamTimeoutSeconds=${nextConfig.upstreamTimeoutSeconds}`);
|
|
11035
|
+
const reloadedConfigWarning = nextConfig.apiKey === previousApiKey ? void 0 : exposedConfigWarning(nextConfig);
|
|
11036
|
+
if (reloadedConfigWarning) log.error(reloadedConfigWarning);
|
|
11037
|
+
});
|
|
11038
|
+
const shutdownGraceMs = 2e3;
|
|
11039
|
+
const shutdown = (signal) => {
|
|
11040
|
+
log.info(`Received ${signal}; shutting down copilot-relay`);
|
|
11041
|
+
loginLookup.abort();
|
|
11042
|
+
server.close((error) => {
|
|
11043
|
+
if (error) {
|
|
11044
|
+
log.error("Error while shutting down copilot-relay:", error);
|
|
11045
|
+
process.exitCode = 1;
|
|
11046
|
+
}
|
|
11047
|
+
});
|
|
11048
|
+
if (canCloseConnections(server)) {
|
|
11049
|
+
server.closeIdleConnections();
|
|
11050
|
+
setTimeout(() => {
|
|
11051
|
+
log.info("Grace period elapsed; closing remaining connections");
|
|
11052
|
+
server.closeAllConnections();
|
|
11053
|
+
}, shutdownGraceMs).unref();
|
|
11054
|
+
}
|
|
11055
|
+
};
|
|
11056
|
+
process.once("SIGINT", shutdown);
|
|
11057
|
+
process.once("SIGTERM", shutdown);
|
|
11058
|
+
try {
|
|
11059
|
+
await new Promise((resolve$1, reject) => {
|
|
11060
|
+
server.on("close", resolve$1);
|
|
11061
|
+
server.on("error", reject);
|
|
11062
|
+
});
|
|
11063
|
+
} finally {
|
|
11064
|
+
process.off("SIGINT", shutdown);
|
|
11065
|
+
process.off("SIGTERM", shutdown);
|
|
11066
|
+
clearInterval(configWatcher);
|
|
11067
|
+
await clearRelayPidFile();
|
|
11068
|
+
await flushCaptures();
|
|
11069
|
+
await flushLogs();
|
|
11070
|
+
}
|
|
11071
|
+
}
|
|
11072
|
+
const start = defineCommand({
|
|
11073
|
+
meta: {
|
|
11074
|
+
name: "start",
|
|
11075
|
+
description: "Start the copilot-relay HTTP server."
|
|
11076
|
+
},
|
|
11077
|
+
async run() {
|
|
11078
|
+
await startRelay();
|
|
11079
|
+
}
|
|
11080
|
+
});
|
|
11081
|
+
|
|
11082
|
+
//#endregion
|
|
11083
|
+
//#region src/restart.ts
|
|
11084
|
+
const restart = defineCommand({
|
|
11085
|
+
meta: {
|
|
11086
|
+
name: "restart",
|
|
11087
|
+
description: "Stop any existing copilot-relay instance and start a new one."
|
|
11088
|
+
},
|
|
11089
|
+
async run() {
|
|
11090
|
+
const appConfig = await readAppConfig();
|
|
11091
|
+
setLogLevel(appConfig.logLevel);
|
|
11092
|
+
await cleanupLogs(appConfig.logRetentionDays);
|
|
11093
|
+
const stopped = await stopExistingRelay(readProxyConfig(appConfig));
|
|
11094
|
+
if (stopped.length > 0) log.info(`Stopped copilot-relay pid(s): ${stopped.join(", ")}`);
|
|
11095
|
+
log.info("Starting copilot-relay");
|
|
11096
|
+
await startRelay(appConfig);
|
|
11097
|
+
}
|
|
11098
|
+
});
|
|
11099
|
+
|
|
10901
11100
|
//#endregion
|
|
10902
11101
|
//#region src/setup.ts
|
|
10903
11102
|
const printable = (text$1) => scrubLogSecrets(terminalText(text$1));
|
|
@@ -11060,13 +11259,15 @@ const configRowOrder = {
|
|
|
11060
11259
|
webSearchBackend: 11,
|
|
11061
11260
|
gptModel: 12,
|
|
11062
11261
|
opusModel: 13,
|
|
11063
|
-
|
|
11262
|
+
models: 14,
|
|
11263
|
+
claudeUpstreamApi: 15
|
|
11064
11264
|
};
|
|
11065
11265
|
const configRowKeys = Object.keys(configRowOrder).sort((a, b) => configRowOrder[a] - configRowOrder[b]);
|
|
11066
11266
|
const unsetConfigText = {
|
|
11067
11267
|
apiKey: "(unset — clients need no key)",
|
|
11068
11268
|
upstreamProxy: "(unset — connects directly)",
|
|
11069
|
-
webSearchBackend: "(unset — uses gptModel)"
|
|
11269
|
+
webSearchBackend: "(unset — uses gptModel)",
|
|
11270
|
+
models: "(empty — gptModel and opusModel only)"
|
|
11070
11271
|
};
|
|
11071
11272
|
/**
|
|
11072
11273
|
* The resolved config, one key per line.
|
|
@@ -11127,8 +11328,9 @@ let relayAgent;
|
|
|
11127
11328
|
* An object, so a test can replace fetch.
|
|
11128
11329
|
*/
|
|
11129
11330
|
const relayListener = { fetch: (url, init) => {
|
|
11130
|
-
|
|
11131
|
-
|
|
11331
|
+
const undici$1 = loadUndici();
|
|
11332
|
+
relayAgent ??= new undici$1.Agent();
|
|
11333
|
+
return undici$1.fetch(url, {
|
|
11132
11334
|
...init,
|
|
11133
11335
|
dispatcher: relayAgent
|
|
11134
11336
|
});
|