copilot-relay 0.6.2 → 0.7.1
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 +733 -557
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -1113,38 +1113,161 @@ function getRequestReasoningEffort(request) {
|
|
|
1113
1113
|
return requested;
|
|
1114
1114
|
}
|
|
1115
1115
|
const resolveReasoningEffort = (requested) => requested ?? getRuntimeState().thinkEffort ?? defaultReasoningEffort;
|
|
1116
|
-
const
|
|
1117
|
-
const
|
|
1118
|
-
return
|
|
1116
|
+
const currentCatalog = () => {
|
|
1117
|
+
const state = getRuntimeState();
|
|
1118
|
+
return state.modelCatalog?.baseUrl === state.upstreamBaseUrl ? state.modelCatalog?.models : void 0;
|
|
1119
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
|
+
*/
|
|
1120
1127
|
const normalizeClaudeModelId = (model, contextWindowTokens) => {
|
|
1121
|
-
const
|
|
1122
|
-
|
|
1123
|
-
const
|
|
1124
|
-
|
|
1125
|
-
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;
|
|
1126
1133
|
};
|
|
1127
|
-
const normalizeCopilotModelId = (model) => normalizeOneMillionContextModel(model) ?? model;
|
|
1128
|
-
const isClaudeModelId = (model) => model.startsWith("claude-");
|
|
1129
1134
|
const getConfiguredModelRouting = () => getRuntimeState().modelRouting ?? defaultModelRouting;
|
|
1130
1135
|
const getModelRouting = () => {
|
|
1131
1136
|
const routing = getConfiguredModelRouting();
|
|
1132
1137
|
return {
|
|
1138
|
+
...routing,
|
|
1133
1139
|
gptModel: normalizeCopilotModelId(routing.gptModel),
|
|
1134
1140
|
opusModel: normalizeCopilotModelId(routing.opusModel)
|
|
1135
1141
|
};
|
|
1136
1142
|
};
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1143
|
+
let exposedCache;
|
|
1144
|
+
const sameSelection = (left, right) => {
|
|
1145
|
+
if (Array.isArray(left) && Array.isArray(right)) return left.length === right.length && left.every((id, index) => id === right[index]);
|
|
1146
|
+
return left === right;
|
|
1140
1147
|
};
|
|
1148
|
+
const exposedModels = () => {
|
|
1149
|
+
const catalog = currentCatalog();
|
|
1150
|
+
const routing = getConfiguredModelRouting();
|
|
1151
|
+
const cached = exposedCache;
|
|
1152
|
+
if (cached !== void 0 && cached.catalog === catalog && cached.gptModel === routing.gptModel && cached.opusModel === routing.opusModel && sameSelection(cached.models, routing.models)) return cached;
|
|
1153
|
+
const upstream = resolveExposedModels(routing, catalog);
|
|
1154
|
+
const slots = [routing.gptModel, routing.opusModel];
|
|
1155
|
+
const claude = upstream.map((id) => {
|
|
1156
|
+
return normalizeClaudeModelId(slots.find((configured) => canonicalModelId(configured, catalog).toLowerCase() === id.toLowerCase()) ?? id);
|
|
1157
|
+
});
|
|
1158
|
+
exposedCache = {
|
|
1159
|
+
catalog,
|
|
1160
|
+
gptModel: routing.gptModel,
|
|
1161
|
+
opusModel: routing.opusModel,
|
|
1162
|
+
models: Array.isArray(routing.models) ? [...routing.models] : routing.models,
|
|
1163
|
+
upstream,
|
|
1164
|
+
claude,
|
|
1165
|
+
byLowercase: new Map(upstream.map((id) => [id.toLowerCase(), id]))
|
|
1166
|
+
};
|
|
1167
|
+
return exposedCache;
|
|
1168
|
+
};
|
|
1169
|
+
const getExposedModelIds = () => [...exposedModels().claude];
|
|
1141
1170
|
const getUpstreamModelIds = () => {
|
|
1142
1171
|
const routing = getModelRouting();
|
|
1143
1172
|
return [routing.gptModel, routing.opusModel];
|
|
1144
1173
|
};
|
|
1145
1174
|
const routeModelId = (model) => {
|
|
1175
|
+
const exposedSet = exposedModels();
|
|
1176
|
+
const requested = canonicalModelId(model.trim(), exposedSet.catalog).toLowerCase();
|
|
1177
|
+
const exposed = exposedSet.byLowercase.get(requested);
|
|
1178
|
+
if (exposed !== void 0) return exposed;
|
|
1146
1179
|
const routing = getModelRouting();
|
|
1147
|
-
return
|
|
1180
|
+
return requested.includes("opus") ? routing.opusModel : routing.gptModel;
|
|
1181
|
+
};
|
|
1182
|
+
const modelIdPattern = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
1183
|
+
const invalidModelsMessage = "Invalid models: expected auto, empty, or comma-separated Copilot model IDs";
|
|
1184
|
+
const parseModelSelection = (value) => {
|
|
1185
|
+
const trimmed = value.trim();
|
|
1186
|
+
if (trimmed.toLowerCase() === "auto") return "auto";
|
|
1187
|
+
if (trimmed === "") return [];
|
|
1188
|
+
const ids = trimmed.split(",").map((id) => id.trim());
|
|
1189
|
+
if (ids.some((id) => !modelIdPattern.test(id) || id.toLowerCase() === "auto")) throw new Error(invalidModelsMessage);
|
|
1190
|
+
return ids;
|
|
1191
|
+
};
|
|
1192
|
+
const catalogIndexes = /* @__PURE__ */ new WeakMap();
|
|
1193
|
+
const catalogIndex = (catalog) => {
|
|
1194
|
+
const known = catalogIndexes.get(catalog);
|
|
1195
|
+
if (known !== void 0) return known;
|
|
1196
|
+
const index = /* @__PURE__ */ new Map();
|
|
1197
|
+
for (const key of catalog.keys()) {
|
|
1198
|
+
const lower = key.toLowerCase();
|
|
1199
|
+
if (!index.has(lower)) index.set(lower, key);
|
|
1200
|
+
}
|
|
1201
|
+
catalogIndexes.set(catalog, index);
|
|
1202
|
+
return index;
|
|
1203
|
+
};
|
|
1204
|
+
const canonicalModelId = (id, catalog) => {
|
|
1205
|
+
const lower = id.replace(/\[1m\]/gi, "").toLowerCase();
|
|
1206
|
+
if (catalog === void 0) return lower;
|
|
1207
|
+
return catalogIndex(catalog).get(lower) ?? lower;
|
|
1208
|
+
};
|
|
1209
|
+
const pickerCategories = [
|
|
1210
|
+
"powerful",
|
|
1211
|
+
"versatile",
|
|
1212
|
+
"lightweight"
|
|
1213
|
+
];
|
|
1214
|
+
const categoryRank = (model) => {
|
|
1215
|
+
const rank = pickerCategories.indexOf(model.pickerCategory ?? "");
|
|
1216
|
+
return rank === -1 ? pickerCategories.length : rank;
|
|
1217
|
+
};
|
|
1218
|
+
const compareVersions = (left, right) => {
|
|
1219
|
+
for (let index = 0; index < Math.max(left.length, right.length); index++) {
|
|
1220
|
+
const difference = (left[index] ?? 0) - (right[index] ?? 0);
|
|
1221
|
+
if (difference !== 0) return difference;
|
|
1222
|
+
}
|
|
1223
|
+
return 0;
|
|
1224
|
+
};
|
|
1225
|
+
const isBetterPick = (left, right) => {
|
|
1226
|
+
const byVersion = compareVersions(left.version, right.version);
|
|
1227
|
+
if (byVersion !== 0) return byVersion > 0;
|
|
1228
|
+
const byCategory = categoryRank(right.model) - categoryRank(left.model);
|
|
1229
|
+
if (byCategory !== 0) return byCategory > 0;
|
|
1230
|
+
return left.id < right.id;
|
|
1231
|
+
};
|
|
1232
|
+
/** The newest picker-enabled chat model of each vendor except Experimental, in catalog order. */
|
|
1233
|
+
const selectNewestPerVendor = (catalog) => {
|
|
1234
|
+
const best = /* @__PURE__ */ new Map();
|
|
1235
|
+
for (const [id, model] of catalog) {
|
|
1236
|
+
const vendor = model.vendor;
|
|
1237
|
+
const version = /\d+(?:\.\d+)*/.exec(id)?.[0].split(".").map(Number);
|
|
1238
|
+
if (!(model.pickerEnabled === true && model.type === "chat" && vendor !== "Experimental") || vendor === void 0 || version === void 0) continue;
|
|
1239
|
+
const candidate = {
|
|
1240
|
+
id,
|
|
1241
|
+
model,
|
|
1242
|
+
version
|
|
1243
|
+
};
|
|
1244
|
+
const current = best.get(vendor);
|
|
1245
|
+
if (current === void 0 || isBetterPick(candidate, current)) best.set(vendor, candidate);
|
|
1246
|
+
}
|
|
1247
|
+
const winners = new Set([...best.values()].map((candidate) => candidate.id));
|
|
1248
|
+
return [...catalog.keys()].filter((id) => winners.has(id));
|
|
1249
|
+
};
|
|
1250
|
+
/**
|
|
1251
|
+
* The upstream IDs clients are offered: gptModel, opusModel, then the selection, deduplicated
|
|
1252
|
+
* case-insensitively in catalog casing. A listed ID the catalog lacks is skipped; without a
|
|
1253
|
+
* catalog, auto adds nothing and every listed ID is kept, lowercased.
|
|
1254
|
+
*/
|
|
1255
|
+
const resolveExposedModels = (routing, catalog) => {
|
|
1256
|
+
const present = (routing.models === "auto" ? selectNewestPerVendor(catalog ?? /* @__PURE__ */ new Map()) : routing.models ?? []).filter((id) => catalog === void 0 || catalog.has(canonicalModelId(id, catalog)));
|
|
1257
|
+
const exposed = [];
|
|
1258
|
+
for (const id of [
|
|
1259
|
+
routing.gptModel,
|
|
1260
|
+
routing.opusModel,
|
|
1261
|
+
...present
|
|
1262
|
+
]) {
|
|
1263
|
+
const canonical$1 = canonicalModelId(id, catalog);
|
|
1264
|
+
if (!exposed.some((known) => known.toLowerCase() === canonical$1.toLowerCase())) exposed.push(canonical$1);
|
|
1265
|
+
}
|
|
1266
|
+
return exposed;
|
|
1267
|
+
};
|
|
1268
|
+
const findMissingModels = (routing, catalog) => {
|
|
1269
|
+
if (!Array.isArray(routing.models)) return [];
|
|
1270
|
+
return routing.models.filter((id) => !catalog.has(canonicalModelId(id, catalog)));
|
|
1148
1271
|
};
|
|
1149
1272
|
|
|
1150
1273
|
//#endregion
|
|
@@ -1343,6 +1466,7 @@ const defaultConfig = {
|
|
|
1343
1466
|
logLevel: "info",
|
|
1344
1467
|
logRetentionDays: 3,
|
|
1345
1468
|
opusModel: "claude-opus-5.5",
|
|
1469
|
+
models: "auto",
|
|
1346
1470
|
port: 4142,
|
|
1347
1471
|
thinkEffort: defaultReasoningEffort,
|
|
1348
1472
|
upstreamTimeoutSeconds: 180,
|
|
@@ -1378,6 +1502,12 @@ const normalizeInteger = (value, key, minimum, maximum = Number.MAX_SAFE_INTEGER
|
|
|
1378
1502
|
if (typeof candidate !== "number" || !Number.isSafeInteger(candidate) || candidate < minimum || candidate > maximum) throw new Error(`Invalid ${key}: expected an integer from ${minimum} to ${maximum}`);
|
|
1379
1503
|
return candidate;
|
|
1380
1504
|
};
|
|
1505
|
+
const normalizeModels = (value) => {
|
|
1506
|
+
if (value === void 0) return;
|
|
1507
|
+
if (typeof value !== "string") throw new Error("Invalid models: expected auto, empty, or comma-separated Copilot model IDs");
|
|
1508
|
+
parseModelSelection(value);
|
|
1509
|
+
return value.trim();
|
|
1510
|
+
};
|
|
1381
1511
|
const normalizeString = (value) => typeof value === "string" && value.trim() ? value.trim() : void 0;
|
|
1382
1512
|
const normalizeRequiredString = (value, key) => {
|
|
1383
1513
|
if (value === void 0) return;
|
|
@@ -1679,6 +1809,9 @@ const serializeConfig = (config) => [
|
|
|
1679
1809
|
"",
|
|
1680
1810
|
"# Upstream Copilot model used for requests containing \"opus\".",
|
|
1681
1811
|
`opusModel: ${config.opusModel}`,
|
|
1812
|
+
"",
|
|
1813
|
+
"# Extra models offered to clients: auto (newest model of each vendor), empty, or comma-separated Copilot IDs.",
|
|
1814
|
+
`models: ${config.models}`,
|
|
1682
1815
|
""
|
|
1683
1816
|
].join("\n");
|
|
1684
1817
|
const resolveConfig = (raw) => ({
|
|
@@ -1691,6 +1824,7 @@ const resolveConfig = (raw) => ({
|
|
|
1691
1824
|
logLevel: normalizeLogLevel(raw.logLevel) ?? defaultConfig.logLevel,
|
|
1692
1825
|
logRetentionDays: normalizeInteger(raw.logRetentionDays, "logRetentionDays", 1) ?? defaultConfig.logRetentionDays,
|
|
1693
1826
|
opusModel: normalizeRequiredString(raw.opusModel, "opusModel") ?? defaultConfig.opusModel,
|
|
1827
|
+
models: normalizeModels(raw.models) ?? defaultConfig.models,
|
|
1694
1828
|
port: normalizeInteger(raw.port, "port", 1, 65535) ?? defaultConfig.port,
|
|
1695
1829
|
thinkEffort: normalizeThinkEffort(raw.thinkEffort) ?? defaultConfig.thinkEffort,
|
|
1696
1830
|
upstreamTimeoutSeconds: normalizeUpstreamTimeoutSeconds(raw.upstreamTimeoutSeconds) ?? defaultConfig.upstreamTimeoutSeconds,
|
|
@@ -2715,7 +2849,6 @@ var RequestTrace = class RequestTrace {
|
|
|
2715
2849
|
},
|
|
2716
2850
|
runtime: {
|
|
2717
2851
|
...policy,
|
|
2718
|
-
models: catalog ? [...catalog.models.entries()] : void 0,
|
|
2719
2852
|
catalogCurrent: catalog?.baseUrl === config.copilotBaseUrl,
|
|
2720
2853
|
...catalog?.fetchedAt !== void 0 && { catalogAgeMs: Math.max(0, (config.admittedAt ?? Date.now()) - catalog.fetchedAt) }
|
|
2721
2854
|
}
|
|
@@ -2743,6 +2876,8 @@ var RequestTrace = class RequestTrace {
|
|
|
2743
2876
|
trace.captureRequested = capture;
|
|
2744
2877
|
traceObserver.getStore()?.(trace);
|
|
2745
2878
|
if (!capture) return trace;
|
|
2879
|
+
const catalog = runtime.modelCatalog ?? config.modelCatalog;
|
|
2880
|
+
trace.manifest.runtime.models = catalog ? [...catalog.models.entries()] : void 0;
|
|
2746
2881
|
try {
|
|
2747
2882
|
if (!uuidPattern.test(id)) throw new Error("Invalid capture identifier.");
|
|
2748
2883
|
const root = path.join(paths.appDir, "captures");
|
|
@@ -3397,8 +3532,43 @@ function getCachedCopilotModel(config, model) {
|
|
|
3397
3532
|
return catalog.models.get(normalizeCopilotModelId(model));
|
|
3398
3533
|
}
|
|
3399
3534
|
const advertisesNoReasoningEffort = (capabilities$1) => capabilities$1?.reasoningEfforts?.length === 0;
|
|
3535
|
+
const copilotReasoningEfforts = [
|
|
3536
|
+
"none",
|
|
3537
|
+
"minimal",
|
|
3538
|
+
"low",
|
|
3539
|
+
"medium",
|
|
3540
|
+
"high",
|
|
3541
|
+
"xhigh",
|
|
3542
|
+
"max"
|
|
3543
|
+
];
|
|
3544
|
+
const effortOrder = [
|
|
3545
|
+
"none",
|
|
3546
|
+
"minimal",
|
|
3547
|
+
"low",
|
|
3548
|
+
"medium",
|
|
3549
|
+
"high",
|
|
3550
|
+
"xhigh",
|
|
3551
|
+
"max",
|
|
3552
|
+
"ultra"
|
|
3553
|
+
];
|
|
3554
|
+
const effortRank = (effort) => effortOrder.indexOf(effort);
|
|
3555
|
+
/**
|
|
3556
|
+
* The reasoning effort to send for a requested one.
|
|
3557
|
+
*
|
|
3558
|
+
* A level the model advertises, or one this relay does not know, is sent as requested. Any other
|
|
3559
|
+
* level becomes the highest advertised level below it, or the lowest advertised level when none is
|
|
3560
|
+
* below it. A model that advertises no levels gets none, since Copilot rejects the field for it.
|
|
3561
|
+
*/
|
|
3562
|
+
const clampReasoningEffort = (requested, advertised) => {
|
|
3563
|
+
if (advertised?.length === 0) return;
|
|
3564
|
+
const accepted = advertised ?? copilotReasoningEfforts;
|
|
3565
|
+
if (accepted.includes(requested) || effortRank(requested) === -1) return requested;
|
|
3566
|
+
const ranked = accepted.filter((effort) => effortRank(effort) !== -1).sort((left, right) => effortRank(left) - effortRank(right));
|
|
3567
|
+
return ranked.filter((effort) => effortRank(effort) <= effortRank(requested)).at(-1) ?? ranked[0];
|
|
3568
|
+
};
|
|
3400
3569
|
function resolveModelReasoningEffort(config, model, requested) {
|
|
3401
|
-
|
|
3570
|
+
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
3571
|
+
if (!advertisesNoReasoningEffort(capabilities$1)) return clampReasoningEffort(resolveReasoningEffort(requested), capabilities$1?.reasoningEfforts);
|
|
3402
3572
|
if (requested === void 0) return;
|
|
3403
3573
|
const message = "The selected upstream model does not advertise reasoning effort support; omit the explicit effort control.";
|
|
3404
3574
|
throw new HTTPError(message, Response.json({
|
|
@@ -3449,7 +3619,10 @@ async function loadCopilotModelCatalog(config) {
|
|
|
3449
3619
|
...supportedEndpoints !== void 0 && { supportedEndpoints },
|
|
3450
3620
|
...typeof capabilities$1?.type === "string" && { type: capabilities$1.type },
|
|
3451
3621
|
...reasoningEfforts !== void 0 && { reasoningEfforts },
|
|
3452
|
-
...typeof model.name === "string" && model.name !== "" && { name: model.name }
|
|
3622
|
+
...typeof model.name === "string" && model.name !== "" && { name: model.name },
|
|
3623
|
+
...typeof model.vendor === "string" && { vendor: model.vendor },
|
|
3624
|
+
...typeof model.model_picker_enabled === "boolean" && { pickerEnabled: model.model_picker_enabled },
|
|
3625
|
+
...typeof model.model_picker_category === "string" && { pickerCategory: model.model_picker_category }
|
|
3453
3626
|
});
|
|
3454
3627
|
}
|
|
3455
3628
|
const catalog = {
|
|
@@ -6098,9 +6271,11 @@ function getUpstreamErrorDetail(response, body) {
|
|
|
6098
6271
|
return body.slice(0, 240);
|
|
6099
6272
|
}
|
|
6100
6273
|
}
|
|
6274
|
+
const responsesRetryCodes = new Set(["unsupported_api_for_model", "no_available_model_endpoints"]);
|
|
6101
6275
|
async function shouldRetryWithResponses(response, signal, timeoutMs) {
|
|
6102
6276
|
try {
|
|
6103
|
-
|
|
6277
|
+
const errorBody = await readCopilotJson(response.clone(), signal, timeoutMs);
|
|
6278
|
+
return responsesRetryCodes.has(errorBody.error?.code ?? "");
|
|
6104
6279
|
} catch {
|
|
6105
6280
|
return false;
|
|
6106
6281
|
}
|
|
@@ -6196,6 +6371,18 @@ function normalizeNativeHistory(messages) {
|
|
|
6196
6371
|
}
|
|
6197
6372
|
return combined;
|
|
6198
6373
|
}
|
|
6374
|
+
const clampSystemEfforts = (config, model, messages) => messages.map((message) => {
|
|
6375
|
+
if (message.role !== "system" || message.output_config?.effort === void 0) return message;
|
|
6376
|
+
const effort = resolveModelReasoningEffort(config, model, message.output_config.effort);
|
|
6377
|
+
if (effort === void 0) return message;
|
|
6378
|
+
return {
|
|
6379
|
+
...message,
|
|
6380
|
+
output_config: {
|
|
6381
|
+
...message.output_config,
|
|
6382
|
+
effort
|
|
6383
|
+
}
|
|
6384
|
+
};
|
|
6385
|
+
});
|
|
6199
6386
|
function shouldUseNativeMessages(config, model) {
|
|
6200
6387
|
return selectCopilotEndpoint(config, model).endpoint === "/v1/messages";
|
|
6201
6388
|
}
|
|
@@ -6234,7 +6421,7 @@ async function createNativeMessages(config, payload, options) {
|
|
|
6234
6421
|
};
|
|
6235
6422
|
const body = {
|
|
6236
6423
|
...payload,
|
|
6237
|
-
messages: normalizeNativeHistory(payload.messages),
|
|
6424
|
+
messages: clampSystemEfforts(config, payload.model, normalizeNativeHistory(payload.messages)),
|
|
6238
6425
|
max_tokens: maxTokens,
|
|
6239
6426
|
stream: Boolean(payload.stream || exceedsNonStreamingLimit),
|
|
6240
6427
|
output_config: outputConfig,
|
|
@@ -6248,7 +6435,8 @@ async function createNativeMessages(config, payload, options) {
|
|
|
6248
6435
|
};
|
|
6249
6436
|
const beta = options.headers?.get("anthropic-beta");
|
|
6250
6437
|
if (beta) headers["anthropic-beta"] = beta;
|
|
6251
|
-
const
|
|
6438
|
+
const advertised = getCachedCopilotModel(config, payload.model)?.reasoningEfforts;
|
|
6439
|
+
const effortLabel = effort === void 0 ? "omitted" : clampReasoningEffort(getClaudeTurnEffort(payload).effective, advertised) ?? "omitted";
|
|
6252
6440
|
const initiator = options.initiator ?? "agent";
|
|
6253
6441
|
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}`));
|
|
6254
6442
|
const response = await fetchCopilot(getCopilotProviderContext(config), "/v1/messages", {
|
|
@@ -6744,8 +6932,7 @@ const translationEndpoint = (config, model) => {
|
|
|
6744
6932
|
const selection = selectCopilotEndpoint(config, normalizeCopilotModelId(model));
|
|
6745
6933
|
return selection.endpoint && !selection.responsesFallback ? selection.endpoint : void 0;
|
|
6746
6934
|
};
|
|
6747
|
-
const handleClaudeMessageRequest = async (config, claudePayload, initiator, requestSignal, requestId, writeEvent, requestHeaders) => {
|
|
6748
|
-
const upstreamModel = translateModelName(claudePayload.model);
|
|
6935
|
+
const handleClaudeMessageRequest = async (config, claudePayload, upstreamModel, initiator, requestSignal, requestId, writeEvent, requestHeaders) => {
|
|
6749
6936
|
if (shouldUseNativeMessages(config, upstreamModel)) return handleNativeMessages(config, {
|
|
6750
6937
|
...claudePayload,
|
|
6751
6938
|
model: upstreamModel
|
|
@@ -6758,7 +6945,10 @@ const handleClaudeMessageRequest = async (config, claudePayload, initiator, requ
|
|
|
6758
6945
|
const shouldLetModelDecideWebSearch = hasClaudeWebSearch(claudePayload);
|
|
6759
6946
|
const decisionPayload = shouldLetModelDecideWebSearch ? prepareClaudeWebSearchDecisionPayload(claudePayload) : claudePayload;
|
|
6760
6947
|
const toolNameMapper = createClaudeToolNameMapper(decisionPayload.tools, { ...getToolNameMapperOptionsForModel(upstreamModel) });
|
|
6761
|
-
const openAIPayload = translateToOpenAI(
|
|
6948
|
+
const openAIPayload = translateToOpenAI({
|
|
6949
|
+
...decisionPayload,
|
|
6950
|
+
model: upstreamModel
|
|
6951
|
+
}, void 0, toolNameMapper, { endpoint: translationEndpoint(config, upstreamModel) });
|
|
6762
6952
|
const canStreamWebSearchDecision = shouldLetModelDecideWebSearch && !!writeEvent;
|
|
6763
6953
|
if (shouldLetModelDecideWebSearch && !canStreamWebSearchDecision) openAIPayload.stream = false;
|
|
6764
6954
|
const response = await createChatCompletions(config, openAIPayload, {
|
|
@@ -6920,9 +7110,10 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6920
7110
|
const initiator = c.env?.relayProbe === true ? "agent" : getClaudeRequestInitiator(receivedPayload);
|
|
6921
7111
|
const claudePayload = removeBillingLine(receivedPayload);
|
|
6922
7112
|
const requestSignal = createCopilotRequestSignal(c.req.raw.signal, config.upstreamTimeoutMs);
|
|
7113
|
+
let upstreamModel = "";
|
|
6923
7114
|
try {
|
|
6924
7115
|
getRequestReasoningEffort(claudePayload);
|
|
6925
|
-
|
|
7116
|
+
upstreamModel = translateModelName(claudePayload.model);
|
|
6926
7117
|
const validate = () => {
|
|
6927
7118
|
if (shouldUseNativeMessages(config, upstreamModel)) validateNativeMessages(claudePayload);
|
|
6928
7119
|
else validateClaudeMessages(claudePayload.messages);
|
|
@@ -6932,6 +7123,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6932
7123
|
await ensureCopilotModelCatalog(config, upstreamModel);
|
|
6933
7124
|
requestSignal?.throwIfAborted();
|
|
6934
7125
|
pinCopilotModelCatalog(config);
|
|
7126
|
+
upstreamModel = translateModelName(claudePayload.model);
|
|
6935
7127
|
const selection = requireCopilotEndpoint(config, upstreamModel);
|
|
6936
7128
|
validate();
|
|
6937
7129
|
if (selection.endpoint === "/v1/messages") validateNativeModelEffort(config, upstreamModel, claudePayload);
|
|
@@ -6969,7 +7161,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6969
7161
|
data: JSON.stringify(event)
|
|
6970
7162
|
}));
|
|
6971
7163
|
try {
|
|
6972
|
-
await handleClaudeMessageRequest(config, claudePayload, initiator, requestSignal, requestId, writeEvent, c.req.raw.headers);
|
|
7164
|
+
await handleClaudeMessageRequest(config, claudePayload, upstreamModel, initiator, requestSignal, requestId, writeEvent, c.req.raw.headers);
|
|
6973
7165
|
} catch (error) {
|
|
6974
7166
|
log.error(`request_id=${requestId} Error during Claude stream request:`, error);
|
|
6975
7167
|
if (error instanceof UpstreamToolInputError) trace?.recordFailure("invalid-tool-input");
|
|
@@ -6983,7 +7175,7 @@ claudeRoutes.post("/messages", async (c) => {
|
|
|
6983
7175
|
});
|
|
6984
7176
|
}
|
|
6985
7177
|
try {
|
|
6986
|
-
return c.json(await handleClaudeMessageRequest(config, claudePayload, initiator, requestSignal, requestId, void 0, c.req.raw.headers));
|
|
7178
|
+
return c.json(await handleClaudeMessageRequest(config, claudePayload, upstreamModel, initiator, requestSignal, requestId, void 0, c.req.raw.headers));
|
|
6987
7179
|
} catch (error) {
|
|
6988
7180
|
if (error instanceof ProxyNotImplementedError) {
|
|
6989
7181
|
c.set("requestErrorMessage", error.message);
|
|
@@ -7008,18 +7200,18 @@ claudeRoutes.post("/messages/count_tokens", async (c) => {
|
|
|
7008
7200
|
try {
|
|
7009
7201
|
const claudeBeta = c.req.header("claude-beta");
|
|
7010
7202
|
const claudePayload = removeBillingLine(await c.req.json());
|
|
7011
|
-
const
|
|
7203
|
+
const routedModel = translateModelName(claudePayload.model);
|
|
7204
|
+
const openAIPayload = translateToOpenAI(shouldUseNativeMessages(c.get("config"), routedModel) ? {
|
|
7012
7205
|
...claudePayload,
|
|
7013
7206
|
messages: claudePayload.messages.map((message) => {
|
|
7014
7207
|
if (message.role !== "system") return message;
|
|
7015
7208
|
const { output_config: _outputConfig, clear_at: _clearAt,...textMessage } = message;
|
|
7016
7209
|
return textMessage;
|
|
7017
7210
|
})
|
|
7018
|
-
} : claudePayload, void 0, void 0, { endpoint: translationEndpoint(c.get("config"),
|
|
7019
|
-
const exposedModels = getExposedModelIds();
|
|
7211
|
+
} : claudePayload, void 0, void 0, { endpoint: translationEndpoint(c.get("config"), routedModel) });
|
|
7020
7212
|
const upstreamModel = getCachedCopilotModel(c.get("config"), openAIPayload.model);
|
|
7021
7213
|
const hasDiscoveredTokenizer = upstreamModel?.tokenizer !== void 0 && isSupportedTokenizer(upstreamModel.tokenizer);
|
|
7022
|
-
const selectedModel = createTokenCountModel(openAIPayload.model
|
|
7214
|
+
const selectedModel = createTokenCountModel(openAIPayload.model, hasDiscoveredTokenizer ? upstreamModel?.tokenizer : void 0);
|
|
7023
7215
|
const tokenCount = await getTokenCount(openAIPayload, selectedModel);
|
|
7024
7216
|
const effectiveModelId = selectedModel.id;
|
|
7025
7217
|
if (claudePayload.tools && claudePayload.tools.length > 0) {
|
|
@@ -7158,187 +7350,6 @@ const decodeThinking = (encrypted) => {
|
|
|
7158
7350
|
};
|
|
7159
7351
|
};
|
|
7160
7352
|
|
|
7161
|
-
//#endregion
|
|
7162
|
-
//#region src/codex/request.ts
|
|
7163
|
-
const copilotReasoningEfforts = [
|
|
7164
|
-
"none",
|
|
7165
|
-
"minimal",
|
|
7166
|
-
"low",
|
|
7167
|
-
"medium",
|
|
7168
|
-
"high",
|
|
7169
|
-
"xhigh",
|
|
7170
|
-
"max"
|
|
7171
|
-
];
|
|
7172
|
-
const effortOrder = [
|
|
7173
|
-
"none",
|
|
7174
|
-
"minimal",
|
|
7175
|
-
"low",
|
|
7176
|
-
"medium",
|
|
7177
|
-
"high",
|
|
7178
|
-
"xhigh",
|
|
7179
|
-
"max",
|
|
7180
|
-
"ultra"
|
|
7181
|
-
];
|
|
7182
|
-
const isRecord$5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7183
|
-
const codexRequestError = (message, code, status$1 = 400) => new HTTPError(message, Response.json({ error: {
|
|
7184
|
-
message,
|
|
7185
|
-
type: "invalid_request_error",
|
|
7186
|
-
code
|
|
7187
|
-
} }, { status: status$1 }), message);
|
|
7188
|
-
const parseCodexPayload = (text$1) => {
|
|
7189
|
-
let body;
|
|
7190
|
-
try {
|
|
7191
|
-
body = JSON.parse(text$1);
|
|
7192
|
-
} catch {
|
|
7193
|
-
throw codexRequestError("The request body is not valid JSON.", "invalid_json");
|
|
7194
|
-
}
|
|
7195
|
-
if (!isRecord$5(body) || typeof body.model !== "string" || body.model.trim() === "") throw codexRequestError("The request needs a model.", "missing_model");
|
|
7196
|
-
if (typeof body.input !== "string" && !Array.isArray(body.input)) throw codexRequestError("The request needs an input string or array.", "missing_input");
|
|
7197
|
-
return body;
|
|
7198
|
-
};
|
|
7199
|
-
const catalogDescribesUpstream = (config) => config.modelCatalog?.baseUrl === config.copilotBaseUrl;
|
|
7200
|
-
/**
|
|
7201
|
-
* The Copilot model a Codex request is sent to, and the API that serves it.
|
|
7202
|
-
*
|
|
7203
|
-
* A model Copilot's catalog lists is used as named, so Codex's own model IDs reach the same
|
|
7204
|
-
* Copilot model. A GPT model listed with /responses is forwarded there; a Claude model listed with
|
|
7205
|
-
* /v1/messages is translated to Messages, because Copilot serves Claude only there and on Chat
|
|
7206
|
-
* Completions. A name the catalog does not list, such as Codex's internal codex-auto-review, goes
|
|
7207
|
-
* to the configured gptModel, as Claude Code's aliases do. Without a catalog a non-Claude name is
|
|
7208
|
-
* forwarded unchanged.
|
|
7209
|
-
*/
|
|
7210
|
-
const resolveCodexModel = async (config, requested) => {
|
|
7211
|
-
const normalized = normalizeCopilotModelId(requested);
|
|
7212
|
-
await ensureCopilotModelCatalog(config, normalized);
|
|
7213
|
-
pinCopilotModelCatalog(config);
|
|
7214
|
-
if (isClaudeModelId(normalized)) {
|
|
7215
|
-
const capabilities$2 = getCachedCopilotModel(config, normalized);
|
|
7216
|
-
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");
|
|
7217
|
-
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");
|
|
7218
|
-
return {
|
|
7219
|
-
model: normalized,
|
|
7220
|
-
api: "messages"
|
|
7221
|
-
};
|
|
7222
|
-
}
|
|
7223
|
-
const model = !(getCachedCopilotModel(config, normalized) !== void 0) && catalogDescribesUpstream(config) ? getModelRouting().gptModel : normalized;
|
|
7224
|
-
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
7225
|
-
const notChat = capabilities$1?.type !== void 0 && capabilities$1.type !== "chat";
|
|
7226
|
-
const noResponses = capabilities$1?.supportedEndpoints !== void 0 && !capabilities$1.supportedEndpoints.includes("/responses");
|
|
7227
|
-
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");
|
|
7228
|
-
return {
|
|
7229
|
-
model,
|
|
7230
|
-
api: "responses"
|
|
7231
|
-
};
|
|
7232
|
-
};
|
|
7233
|
-
const effortRank = (effort) => effortOrder.indexOf(effort);
|
|
7234
|
-
/**
|
|
7235
|
-
* The reasoning effort to send for a requested one.
|
|
7236
|
-
*
|
|
7237
|
-
* A level the model advertises, or one this relay does not know, is sent as requested. Any other
|
|
7238
|
-
* level becomes the highest advertised level below it, or the lowest advertised level when none is
|
|
7239
|
-
* below it. A model that advertises no levels gets none, since Copilot rejects the field for it.
|
|
7240
|
-
*/
|
|
7241
|
-
const clampReasoningEffort = (requested, advertised) => {
|
|
7242
|
-
if (advertised?.length === 0) return;
|
|
7243
|
-
const accepted = advertised ?? copilotReasoningEfforts;
|
|
7244
|
-
if (accepted.includes(requested) || effortRank(requested) === -1) return requested;
|
|
7245
|
-
const ranked = accepted.filter((effort) => effortRank(effort) !== -1).sort((left, right) => effortRank(left) - effortRank(right));
|
|
7246
|
-
return ranked.filter((effort) => effortRank(effort) <= effortRank(requested)).at(-1) ?? ranked[0];
|
|
7247
|
-
};
|
|
7248
|
-
/**
|
|
7249
|
-
* The body sent to Copilot for a Codex request.
|
|
7250
|
-
*
|
|
7251
|
-
* Copilot answers HTTP 400 to `service_tier`, to an `image_generation` tool, and to an effort it
|
|
7252
|
-
* does not accept, so those change. Reasoning items holding the relay's Claude thinking are removed.
|
|
7253
|
-
* Every other field, including Codex's prompt_cache_key and GPT's encrypted reasoning, is forwarded
|
|
7254
|
-
* unchanged.
|
|
7255
|
-
*/
|
|
7256
|
-
const prepareCodexPayload = (payload, model, capabilities$1) => {
|
|
7257
|
-
const prepared = {
|
|
7258
|
-
...payload,
|
|
7259
|
-
model
|
|
7260
|
-
};
|
|
7261
|
-
const removed$1 = [];
|
|
7262
|
-
if (Object.hasOwn(prepared, "service_tier")) {
|
|
7263
|
-
delete prepared.service_tier;
|
|
7264
|
-
removed$1.push("service_tier");
|
|
7265
|
-
}
|
|
7266
|
-
if (Array.isArray(prepared.input)) {
|
|
7267
|
-
const input = prepared.input.filter((item) => !(isRecord$5(item) && item.type === "reasoning" && isRelayThinking(item.encrypted_content)));
|
|
7268
|
-
if (input.length !== prepared.input.length) {
|
|
7269
|
-
prepared.input = input;
|
|
7270
|
-
removed$1.push("claude_reasoning");
|
|
7271
|
-
}
|
|
7272
|
-
}
|
|
7273
|
-
if (Array.isArray(prepared.tools)) {
|
|
7274
|
-
const tools = prepared.tools.filter((tool) => !(isRecord$5(tool) && tool.type === "image_generation"));
|
|
7275
|
-
if (tools.length !== prepared.tools.length) {
|
|
7276
|
-
prepared.tools = tools;
|
|
7277
|
-
removed$1.push("image_generation");
|
|
7278
|
-
}
|
|
7279
|
-
}
|
|
7280
|
-
const reasoning = isRecord$5(prepared.reasoning) ? prepared.reasoning : void 0;
|
|
7281
|
-
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
7282
|
-
let effectiveEffort = requestedEffort;
|
|
7283
|
-
if (reasoning !== void 0 && requestedEffort !== void 0) {
|
|
7284
|
-
effectiveEffort = clampReasoningEffort(requestedEffort, capabilities$1?.reasoningEfforts);
|
|
7285
|
-
if (effectiveEffort === void 0) {
|
|
7286
|
-
const withoutEffort = { ...reasoning };
|
|
7287
|
-
delete withoutEffort.effort;
|
|
7288
|
-
prepared.reasoning = withoutEffort;
|
|
7289
|
-
} else if (effectiveEffort !== requestedEffort) prepared.reasoning = {
|
|
7290
|
-
...reasoning,
|
|
7291
|
-
effort: effectiveEffort
|
|
7292
|
-
};
|
|
7293
|
-
}
|
|
7294
|
-
return {
|
|
7295
|
-
payload: prepared,
|
|
7296
|
-
removed: removed$1,
|
|
7297
|
-
requestedEffort,
|
|
7298
|
-
effectiveEffort
|
|
7299
|
-
};
|
|
7300
|
-
};
|
|
7301
|
-
/**
|
|
7302
|
-
* Who started a Codex request, for Copilot's x-initiator header.
|
|
7303
|
-
*
|
|
7304
|
-
* A request continues the agent's own work unless its last input item is a user message: after a
|
|
7305
|
-
* tool call the last item is the tool's output. Codex marks its subagents (review, compaction,
|
|
7306
|
-
* memory consolidation, spawned agents) with x-openai-subagent, and those are always the agent.
|
|
7307
|
-
*/
|
|
7308
|
-
const getCodexInitiator = (input, subagent) => {
|
|
7309
|
-
if (subagent !== void 0) return "agent";
|
|
7310
|
-
if (typeof input === "string") return "user";
|
|
7311
|
-
const last = Array.isArray(input) ? input.at(-1) : void 0;
|
|
7312
|
-
return isRecord$5(last) && last.role === "user" && (last.type === void 0 || last.type === "message") ? "user" : "agent";
|
|
7313
|
-
};
|
|
7314
|
-
const includesImage = (value) => {
|
|
7315
|
-
if (Array.isArray(value)) return value.some(includesImage);
|
|
7316
|
-
if (!isRecord$5(value)) return false;
|
|
7317
|
-
return value.type === "input_image" || Object.values(value).some(includesImage);
|
|
7318
|
-
};
|
|
7319
|
-
/**
|
|
7320
|
-
* A stream holding only `response.failed` with code `context_length_exceeded`.
|
|
7321
|
-
*
|
|
7322
|
-
* Copilot reports a prompt over the model's input limit as HTTP 400, which Codex treats as a fatal
|
|
7323
|
-
* error. Codex recognizes a context-window overflow only from this event, and answers it by
|
|
7324
|
-
* compacting the conversation.
|
|
7325
|
-
*/
|
|
7326
|
-
const contextOverflowStream = (requestId, message) => {
|
|
7327
|
-
const event = {
|
|
7328
|
-
type: "response.failed",
|
|
7329
|
-
response: {
|
|
7330
|
-
id: `resp_relay_${requestId}`,
|
|
7331
|
-
object: "response",
|
|
7332
|
-
status: "failed",
|
|
7333
|
-
error: {
|
|
7334
|
-
code: "context_length_exceeded",
|
|
7335
|
-
message
|
|
7336
|
-
}
|
|
7337
|
-
}
|
|
7338
|
-
};
|
|
7339
|
-
return `event: response.failed\ndata: ${JSON.stringify(event)}\n\n`;
|
|
7340
|
-
};
|
|
7341
|
-
|
|
7342
7353
|
//#endregion
|
|
7343
7354
|
//#region src/codex/messages.ts
|
|
7344
7355
|
const claudeReasoningEfforts = [
|
|
@@ -7349,7 +7360,7 @@ const claudeReasoningEfforts = [
|
|
|
7349
7360
|
"max"
|
|
7350
7361
|
];
|
|
7351
7362
|
const defaultMaxTokens = 32e3;
|
|
7352
|
-
const isRecord$
|
|
7363
|
+
const isRecord$5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7353
7364
|
const nameLimit = 64;
|
|
7354
7365
|
/**
|
|
7355
7366
|
* Claude tool names for Codex tools, and the way back.
|
|
@@ -7394,14 +7405,14 @@ const customInputSchema = {
|
|
|
7394
7405
|
};
|
|
7395
7406
|
const customDescription = (tool) => {
|
|
7396
7407
|
const description = typeof tool.description === "string" ? tool.description : "";
|
|
7397
|
-
const format = isRecord$
|
|
7408
|
+
const format = isRecord$5(tool.format) ? tool.format : void 0;
|
|
7398
7409
|
if (format?.type !== "grammar" || typeof format.definition !== "string") return description;
|
|
7399
7410
|
return `${description}\n\nPut the raw input in the input field. It must match this ${String(format.syntax ?? "")} grammar:\n${format.definition}`.trim();
|
|
7400
7411
|
};
|
|
7401
7412
|
const functionTool = (tool, name) => ({
|
|
7402
7413
|
name,
|
|
7403
7414
|
...typeof tool.description === "string" && tool.description && { description: tool.description },
|
|
7404
|
-
input_schema: isRecord$
|
|
7415
|
+
input_schema: isRecord$5(tool.parameters) ? tool.parameters : {
|
|
7405
7416
|
type: "object",
|
|
7406
7417
|
properties: {}
|
|
7407
7418
|
}
|
|
@@ -7428,10 +7439,10 @@ const convertTools = (tools, names, dropped) => {
|
|
|
7428
7439
|
if (!Array.isArray(tools)) return [];
|
|
7429
7440
|
const converted = [];
|
|
7430
7441
|
for (const tool of tools) {
|
|
7431
|
-
if (!isRecord$
|
|
7442
|
+
if (!isRecord$5(tool) || typeof tool.type !== "string") continue;
|
|
7432
7443
|
if (tool.type === "namespace" && typeof tool.name === "string" && Array.isArray(tool.tools)) {
|
|
7433
7444
|
for (const inner of tool.tools) {
|
|
7434
|
-
if (!isRecord$
|
|
7445
|
+
if (!isRecord$5(inner)) continue;
|
|
7435
7446
|
const claudeTool$1 = convertTool(inner, names, tool.name);
|
|
7436
7447
|
if (claudeTool$1) converted.push(claudeTool$1);
|
|
7437
7448
|
else dropped.add(`tool:${tool.name}.${String(inner.type)}`);
|
|
@@ -7470,7 +7481,7 @@ const contentBlocks = (content, allowImages) => {
|
|
|
7470
7481
|
if (!Array.isArray(content)) return [];
|
|
7471
7482
|
const blocks = [];
|
|
7472
7483
|
for (const part of content) {
|
|
7473
|
-
if (!isRecord$
|
|
7484
|
+
if (!isRecord$5(part)) continue;
|
|
7474
7485
|
const text$1 = part.type === "refusal" ? part.refusal : part.text;
|
|
7475
7486
|
if ((part.type === "input_text" || part.type === "output_text" || part.type === "refusal") && typeof text$1 === "string" && text$1) blocks.push({
|
|
7476
7487
|
type: "text",
|
|
@@ -7508,7 +7519,7 @@ const parseArguments = (value) => {
|
|
|
7508
7519
|
if (typeof value !== "string") return {};
|
|
7509
7520
|
try {
|
|
7510
7521
|
const parsed = JSON.parse(value);
|
|
7511
|
-
return isRecord$
|
|
7522
|
+
return isRecord$5(parsed) ? parsed : {};
|
|
7512
7523
|
} catch {
|
|
7513
7524
|
return {};
|
|
7514
7525
|
}
|
|
@@ -7533,7 +7544,7 @@ const toolChoice = (choice, parallel, names) => {
|
|
|
7533
7544
|
let selected;
|
|
7534
7545
|
if (choice === "required") selected = { type: "any" };
|
|
7535
7546
|
else if (choice === "none") selected = { type: "none" };
|
|
7536
|
-
else if (isRecord$
|
|
7547
|
+
else if (isRecord$5(choice) && choice.type === "function" && typeof choice.name === "string") selected = {
|
|
7537
7548
|
type: "tool",
|
|
7538
7549
|
name: names.claudeName({
|
|
7539
7550
|
kind: "function",
|
|
@@ -7545,8 +7556,8 @@ const toolChoice = (choice, parallel, names) => {
|
|
|
7545
7556
|
return selected;
|
|
7546
7557
|
};
|
|
7547
7558
|
const jsonSchemaFormat = (text$1) => {
|
|
7548
|
-
const format = isRecord$
|
|
7549
|
-
if (format?.type !== "json_schema" || !isRecord$
|
|
7559
|
+
const format = isRecord$5(text$1) && isRecord$5(text$1.format) ? text$1.format : void 0;
|
|
7560
|
+
if (format?.type !== "json_schema" || !isRecord$5(format.schema)) return;
|
|
7550
7561
|
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)}`;
|
|
7551
7562
|
};
|
|
7552
7563
|
/**
|
|
@@ -7578,7 +7589,7 @@ const toClaudeRequest = (payload, model, capabilities$1) => {
|
|
|
7578
7589
|
content: payload.input
|
|
7579
7590
|
}] : payload.input;
|
|
7580
7591
|
for (const item of items) {
|
|
7581
|
-
if (!isRecord$
|
|
7592
|
+
if (!isRecord$5(item)) continue;
|
|
7582
7593
|
const type = typeof item.type === "string" ? item.type : "message";
|
|
7583
7594
|
if (type === "message" && (item.role === "developer" || item.role === "system")) {
|
|
7584
7595
|
const blocks = contentBlocks(item.content, false);
|
|
@@ -7610,7 +7621,7 @@ const toClaudeRequest = (payload, model, capabilities$1) => {
|
|
|
7610
7621
|
}
|
|
7611
7622
|
for (const turn of turns) if (turn.role === "user") turn.content.sort((left, right) => Number(right.type === "tool_result") - Number(left.type === "tool_result"));
|
|
7612
7623
|
markCacheBreakpoints(system, tools, turns);
|
|
7613
|
-
const reasoning = isRecord$
|
|
7624
|
+
const reasoning = isRecord$5(payload.reasoning) ? payload.reasoning : void 0;
|
|
7614
7625
|
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
7615
7626
|
const advertised = capabilities$1?.reasoningEfforts?.filter((effort) => claudeReasoningEfforts.includes(effort));
|
|
7616
7627
|
const effectiveEffort = requestedEffort === void 0 ? void 0 : clampReasoningEffort(requestedEffort, advertised ?? claudeReasoningEfforts);
|
|
@@ -7641,7 +7652,7 @@ const callItem = (block, status$1) => {
|
|
|
7641
7652
|
let input = block.json;
|
|
7642
7653
|
try {
|
|
7643
7654
|
const parsed = JSON.parse(block.json);
|
|
7644
|
-
if (isRecord$
|
|
7655
|
+
if (isRecord$5(parsed) && typeof parsed.input === "string") input = parsed.input;
|
|
7645
7656
|
} catch {}
|
|
7646
7657
|
return {
|
|
7647
7658
|
type: "custom_tool_call",
|
|
@@ -7952,11 +7963,11 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7952
7963
|
};
|
|
7953
7964
|
}
|
|
7954
7965
|
if (event.type === "message_start") {
|
|
7955
|
-
const message = isRecord$
|
|
7966
|
+
const message = isRecord$5(event.message) ? event.message : {};
|
|
7956
7967
|
if (typeof message.id === "string") responseId = `resp_${message.id}`;
|
|
7957
7968
|
usage$1 = {
|
|
7958
7969
|
...usage$1,
|
|
7959
|
-
...isRecord$
|
|
7970
|
+
...isRecord$5(message.usage) ? message.usage : {}
|
|
7960
7971
|
};
|
|
7961
7972
|
yield {
|
|
7962
7973
|
type: "response.created",
|
|
@@ -7966,7 +7977,7 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7966
7977
|
type: "response.in_progress",
|
|
7967
7978
|
response: response("in_progress")
|
|
7968
7979
|
};
|
|
7969
|
-
} else if (event.type === "content_block_start" && typeof event.index === "number" && isRecord$
|
|
7980
|
+
} else if (event.type === "content_block_start" && typeof event.index === "number" && isRecord$5(event.content_block)) {
|
|
7970
7981
|
const block = newBlock(event.content_block, names, `${responseId}_call_${event.index}`);
|
|
7971
7982
|
if (block?.kind === "call" && !holding) {
|
|
7972
7983
|
holding = true;
|
|
@@ -7984,7 +7995,7 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7984
7995
|
yield* startEvents(placed);
|
|
7985
7996
|
yield* deltaEvents(placed, streamed(placed));
|
|
7986
7997
|
}
|
|
7987
|
-
} else if (event.type === "content_block_delta" && typeof event.index === "number" && isRecord$
|
|
7998
|
+
} else if (event.type === "content_block_delta" && typeof event.index === "number" && isRecord$5(event.delta)) {
|
|
7988
7999
|
const entry = open.get(event.index);
|
|
7989
8000
|
if (entry !== void 0) {
|
|
7990
8001
|
const piece = applyDelta(entry.block, event.delta);
|
|
@@ -7999,10 +8010,10 @@ async function* toCodexEvents(events$1, model, names) {
|
|
|
7999
8010
|
yield* doneEvents(entry.placed, item);
|
|
8000
8011
|
}
|
|
8001
8012
|
} else if (event.type === "message_delta") {
|
|
8002
|
-
if (isRecord$
|
|
8013
|
+
if (isRecord$5(event.delta) && typeof event.delta.stop_reason === "string") stopReason = event.delta.stop_reason;
|
|
8003
8014
|
usage$1 = {
|
|
8004
8015
|
...usage$1,
|
|
8005
|
-
...isRecord$
|
|
8016
|
+
...isRecord$5(event.usage) ? event.usage : {}
|
|
8006
8017
|
};
|
|
8007
8018
|
} else if (event.type === "message_stop") {
|
|
8008
8019
|
stopped = true;
|
|
@@ -8050,7 +8061,7 @@ const collectCodexResponse = async (events$1) => {
|
|
|
8050
8061
|
message: "Claude stream produced no response."
|
|
8051
8062
|
}
|
|
8052
8063
|
};
|
|
8053
|
-
for await (const event of events$1) if ((event.type === "response.completed" || event.type === "response.incomplete" || event.type === "response.failed") && isRecord$
|
|
8064
|
+
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;
|
|
8054
8065
|
return final;
|
|
8055
8066
|
};
|
|
8056
8067
|
/** Responses events as SSE bytes. */
|
|
@@ -8060,24 +8071,171 @@ async function* encodeSse(events$1) {
|
|
|
8060
8071
|
}
|
|
8061
8072
|
|
|
8062
8073
|
//#endregion
|
|
8063
|
-
//#region src/
|
|
8064
|
-
const
|
|
8065
|
-
const
|
|
8066
|
-
|
|
8067
|
-
"
|
|
8068
|
-
|
|
8069
|
-
|
|
8070
|
-
|
|
8071
|
-
|
|
8072
|
-
|
|
8073
|
-
|
|
8074
|
-
|
|
8075
|
-
|
|
8076
|
-
|
|
8077
|
-
|
|
8078
|
-
|
|
8079
|
-
return
|
|
8080
|
-
};
|
|
8074
|
+
//#region src/codex/request.ts
|
|
8075
|
+
const isRecord$4 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8076
|
+
const codexRequestError = (message, code, status$1 = 400) => new HTTPError(message, Response.json({ error: {
|
|
8077
|
+
message,
|
|
8078
|
+
type: "invalid_request_error",
|
|
8079
|
+
code
|
|
8080
|
+
} }, { status: status$1 }), message);
|
|
8081
|
+
const parseCodexPayload = (text$1) => {
|
|
8082
|
+
let body;
|
|
8083
|
+
try {
|
|
8084
|
+
body = JSON.parse(text$1);
|
|
8085
|
+
} catch {
|
|
8086
|
+
throw codexRequestError("The request body is not valid JSON.", "invalid_json");
|
|
8087
|
+
}
|
|
8088
|
+
if (!isRecord$4(body) || typeof body.model !== "string" || body.model.trim() === "") throw codexRequestError("The request needs a model.", "missing_model");
|
|
8089
|
+
if (typeof body.input !== "string" && !Array.isArray(body.input)) throw codexRequestError("The request needs an input string or array.", "missing_input");
|
|
8090
|
+
return body;
|
|
8091
|
+
};
|
|
8092
|
+
const catalogDescribesUpstream = (config) => config.modelCatalog?.baseUrl === config.copilotBaseUrl;
|
|
8093
|
+
/**
|
|
8094
|
+
* The Copilot model a Codex request is sent to, and the API that serves it.
|
|
8095
|
+
*
|
|
8096
|
+
* A model Copilot's catalog lists is used as named, so Codex's own model IDs reach the same
|
|
8097
|
+
* Copilot model. A GPT model listed with /responses is forwarded there; a Claude model listed with
|
|
8098
|
+
* /v1/messages is translated to Messages, because Copilot serves Claude only there and on Chat
|
|
8099
|
+
* Completions. A name the catalog does not list, such as Codex's internal codex-auto-review, goes
|
|
8100
|
+
* to the configured gptModel, as Claude Code's aliases do. Without a catalog a non-Claude name is
|
|
8101
|
+
* forwarded unchanged.
|
|
8102
|
+
*/
|
|
8103
|
+
const resolveCodexModel = async (config, requested) => {
|
|
8104
|
+
const normalized = normalizeCopilotModelId(requested);
|
|
8105
|
+
await ensureCopilotModelCatalog(config, normalized);
|
|
8106
|
+
pinCopilotModelCatalog(config);
|
|
8107
|
+
if (isClaudeModelId(normalized)) {
|
|
8108
|
+
const capabilities$2 = getCachedCopilotModel(config, normalized);
|
|
8109
|
+
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");
|
|
8110
|
+
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");
|
|
8111
|
+
return {
|
|
8112
|
+
model: normalized,
|
|
8113
|
+
api: "messages"
|
|
8114
|
+
};
|
|
8115
|
+
}
|
|
8116
|
+
const model = !(getCachedCopilotModel(config, normalized) !== void 0) && catalogDescribesUpstream(config) ? getModelRouting().gptModel : normalized;
|
|
8117
|
+
const capabilities$1 = getCachedCopilotModel(config, model);
|
|
8118
|
+
const notChat = capabilities$1?.type !== void 0 && capabilities$1.type !== "chat";
|
|
8119
|
+
const noResponses = capabilities$1?.supportedEndpoints !== void 0 && !capabilities$1.supportedEndpoints.includes("/responses");
|
|
8120
|
+
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");
|
|
8121
|
+
return {
|
|
8122
|
+
model,
|
|
8123
|
+
api: "responses"
|
|
8124
|
+
};
|
|
8125
|
+
};
|
|
8126
|
+
/**
|
|
8127
|
+
* The body sent to Copilot for a Codex request.
|
|
8128
|
+
*
|
|
8129
|
+
* Copilot answers HTTP 400 to `service_tier`, to an `image_generation` tool, and to an effort it
|
|
8130
|
+
* does not accept, so those change. Reasoning items holding the relay's Claude thinking are removed.
|
|
8131
|
+
* Every other field, including Codex's prompt_cache_key and GPT's encrypted reasoning, is forwarded
|
|
8132
|
+
* unchanged.
|
|
8133
|
+
*/
|
|
8134
|
+
const prepareCodexPayload = (payload, model, capabilities$1) => {
|
|
8135
|
+
const prepared = {
|
|
8136
|
+
...payload,
|
|
8137
|
+
model
|
|
8138
|
+
};
|
|
8139
|
+
const removed$1 = [];
|
|
8140
|
+
if (Object.hasOwn(prepared, "service_tier")) {
|
|
8141
|
+
delete prepared.service_tier;
|
|
8142
|
+
removed$1.push("service_tier");
|
|
8143
|
+
}
|
|
8144
|
+
if (Array.isArray(prepared.input)) {
|
|
8145
|
+
const input = prepared.input.filter((item) => !(isRecord$4(item) && item.type === "reasoning" && isRelayThinking(item.encrypted_content)));
|
|
8146
|
+
if (input.length !== prepared.input.length) {
|
|
8147
|
+
prepared.input = input;
|
|
8148
|
+
removed$1.push("claude_reasoning");
|
|
8149
|
+
}
|
|
8150
|
+
}
|
|
8151
|
+
if (Array.isArray(prepared.tools)) {
|
|
8152
|
+
const tools = prepared.tools.filter((tool) => !(isRecord$4(tool) && tool.type === "image_generation"));
|
|
8153
|
+
if (tools.length !== prepared.tools.length) {
|
|
8154
|
+
prepared.tools = tools;
|
|
8155
|
+
removed$1.push("image_generation");
|
|
8156
|
+
}
|
|
8157
|
+
}
|
|
8158
|
+
const reasoning = isRecord$4(prepared.reasoning) ? prepared.reasoning : void 0;
|
|
8159
|
+
const requestedEffort = typeof reasoning?.effort === "string" ? reasoning.effort : void 0;
|
|
8160
|
+
let effectiveEffort = requestedEffort;
|
|
8161
|
+
if (reasoning !== void 0 && requestedEffort !== void 0) {
|
|
8162
|
+
effectiveEffort = clampReasoningEffort(requestedEffort, capabilities$1?.reasoningEfforts);
|
|
8163
|
+
if (effectiveEffort === void 0) {
|
|
8164
|
+
const withoutEffort = { ...reasoning };
|
|
8165
|
+
delete withoutEffort.effort;
|
|
8166
|
+
prepared.reasoning = withoutEffort;
|
|
8167
|
+
} else if (effectiveEffort !== requestedEffort) prepared.reasoning = {
|
|
8168
|
+
...reasoning,
|
|
8169
|
+
effort: effectiveEffort
|
|
8170
|
+
};
|
|
8171
|
+
}
|
|
8172
|
+
return {
|
|
8173
|
+
payload: prepared,
|
|
8174
|
+
removed: removed$1,
|
|
8175
|
+
requestedEffort,
|
|
8176
|
+
effectiveEffort
|
|
8177
|
+
};
|
|
8178
|
+
};
|
|
8179
|
+
/**
|
|
8180
|
+
* Who started a Codex request, for Copilot's x-initiator header.
|
|
8181
|
+
*
|
|
8182
|
+
* A request continues the agent's own work unless its last input item is a user message: after a
|
|
8183
|
+
* tool call the last item is the tool's output. Codex marks its subagents (review, compaction,
|
|
8184
|
+
* memory consolidation, spawned agents) with x-openai-subagent, and those are always the agent.
|
|
8185
|
+
*/
|
|
8186
|
+
const getCodexInitiator = (input, subagent) => {
|
|
8187
|
+
if (subagent !== void 0) return "agent";
|
|
8188
|
+
if (typeof input === "string") return "user";
|
|
8189
|
+
const last = Array.isArray(input) ? input.at(-1) : void 0;
|
|
8190
|
+
return isRecord$4(last) && last.role === "user" && (last.type === void 0 || last.type === "message") ? "user" : "agent";
|
|
8191
|
+
};
|
|
8192
|
+
const includesImage = (value) => {
|
|
8193
|
+
if (Array.isArray(value)) return value.some(includesImage);
|
|
8194
|
+
if (!isRecord$4(value)) return false;
|
|
8195
|
+
return value.type === "input_image" || Object.values(value).some(includesImage);
|
|
8196
|
+
};
|
|
8197
|
+
/**
|
|
8198
|
+
* A stream holding only `response.failed` with code `context_length_exceeded`.
|
|
8199
|
+
*
|
|
8200
|
+
* Copilot reports a prompt over the model's input limit as HTTP 400, which Codex treats as a fatal
|
|
8201
|
+
* error. Codex recognizes a context-window overflow only from this event, and answers it by
|
|
8202
|
+
* compacting the conversation.
|
|
8203
|
+
*/
|
|
8204
|
+
const contextOverflowStream = (requestId, message) => {
|
|
8205
|
+
const event = {
|
|
8206
|
+
type: "response.failed",
|
|
8207
|
+
response: {
|
|
8208
|
+
id: `resp_relay_${requestId}`,
|
|
8209
|
+
object: "response",
|
|
8210
|
+
status: "failed",
|
|
8211
|
+
error: {
|
|
8212
|
+
code: "context_length_exceeded",
|
|
8213
|
+
message
|
|
8214
|
+
}
|
|
8215
|
+
}
|
|
8216
|
+
};
|
|
8217
|
+
return `event: response.failed\ndata: ${JSON.stringify(event)}\n\n`;
|
|
8218
|
+
};
|
|
8219
|
+
|
|
8220
|
+
//#endregion
|
|
8221
|
+
//#region src/routes/responses.ts
|
|
8222
|
+
const responsesRoutes = new Hono();
|
|
8223
|
+
const forwardedHeaders = [
|
|
8224
|
+
"content-type",
|
|
8225
|
+
"retry-after",
|
|
8226
|
+
"openai-model",
|
|
8227
|
+
"x-request-id",
|
|
8228
|
+
"x-github-request-id",
|
|
8229
|
+
"x-copilot-service-request-id"
|
|
8230
|
+
];
|
|
8231
|
+
const copyForwardedHeaders = (source) => {
|
|
8232
|
+
const headers = new Headers();
|
|
8233
|
+
for (const name of forwardedHeaders) {
|
|
8234
|
+
const value = source.get(name);
|
|
8235
|
+
if (value !== null) headers.set(name, value);
|
|
8236
|
+
}
|
|
8237
|
+
return headers;
|
|
8238
|
+
};
|
|
8081
8239
|
const readSubagent = (value) => {
|
|
8082
8240
|
if (value === void 0) return;
|
|
8083
8241
|
return /^[\w.-]{1,64}$/.test(value) ? value : "other";
|
|
@@ -9073,7 +9231,7 @@ const validateManifest = (value) => {
|
|
|
9073
9231
|
const runtime = value.runtime;
|
|
9074
9232
|
requireValid(record(runtime) && (runtime.debug === void 0 || typeof runtime.debug === "boolean"));
|
|
9075
9233
|
requireValid(runtime.thinkEffort === void 0 || isConfiguredReasoningEffort(runtime.thinkEffort));
|
|
9076
|
-
if (runtime.modelRouting !== void 0) requireValid(record(runtime.modelRouting) && text(runtime.modelRouting.gptModel) && text(runtime.modelRouting.opusModel));
|
|
9234
|
+
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))));
|
|
9077
9235
|
requireValid(runtime.catalogCurrent === void 0 || typeof runtime.catalogCurrent === "boolean");
|
|
9078
9236
|
requireValid(runtime.catalogAgeMs === void 0 || typeof runtime.catalogAgeMs === "number" && Number.isSafeInteger(runtime.catalogAgeMs) && runtime.catalogAgeMs >= 0);
|
|
9079
9237
|
if (runtime.models !== void 0) {
|
|
@@ -10029,10 +10187,11 @@ async function applyClaudeConfig(input) {
|
|
|
10029
10187
|
const previousBaseUrl = typeof env.ANTHROPIC_BASE_URL === "string" ? env.ANTHROPIC_BASE_URL : void 0;
|
|
10030
10188
|
const hadPrimaryModelOverride = hasPrimaryModelOverride(parsed, env);
|
|
10031
10189
|
const contextWindow = input.gptLimits?.max_context_window_tokens;
|
|
10032
|
-
const
|
|
10190
|
+
const windowOf = (model) => input.contextWindowOf?.(normalizeCopilotModelId(model));
|
|
10191
|
+
const claudeGptModel = normalizeClaudeModelId(gptModel, windowOf(gptModel) ?? contextWindow);
|
|
10033
10192
|
const shouldSeedTopLevelModel = !hadPrimaryModelOverride;
|
|
10034
|
-
const nextModel = typeof parsed.model === "string" ? normalizeClaudeModelId(parsed.model,
|
|
10035
|
-
for (const key of knownModelEnvKeys) if (typeof env[key] === "string") env[key] = normalizeClaudeModelId(env[key],
|
|
10193
|
+
const nextModel = typeof parsed.model === "string" ? normalizeClaudeModelId(parsed.model, windowOf(parsed.model)) : void 0;
|
|
10194
|
+
for (const key of knownModelEnvKeys) if (typeof env[key] === "string") env[key] = normalizeClaudeModelId(env[key], windowOf(env[key]));
|
|
10036
10195
|
env.ANTHROPIC_BASE_URL = baseUrl;
|
|
10037
10196
|
if (input.apiKey) env.ANTHROPIC_AUTH_TOKEN = input.apiKey;
|
|
10038
10197
|
else if (typeof env.ANTHROPIC_AUTH_TOKEN !== "string" || !env.ANTHROPIC_AUTH_TOKEN) env.ANTHROPIC_AUTH_TOKEN = "dummy";
|
|
@@ -10430,308 +10589,42 @@ const applyCodexSetup = async (input) => {
|
|
|
10430
10589
|
};
|
|
10431
10590
|
|
|
10432
10591
|
//#endregion
|
|
10433
|
-
//#region src/lib/
|
|
10434
|
-
const
|
|
10435
|
-
const
|
|
10436
|
-
|
|
10437
|
-
|
|
10438
|
-
|
|
10439
|
-
|
|
10440
|
-
const catalog = await loadCopilotModelCatalog(config);
|
|
10441
|
-
const requiredModels = getUpstreamModelIds();
|
|
10442
|
-
const missingModels = requiredModels.filter((model) => !catalog.models.has(model));
|
|
10443
|
-
if (missingModels.length > 0) throw new Error(`Required Copilot model(s) unavailable upstream: ${missingModels.join(", ")}`);
|
|
10444
|
-
log.info(`Upstream models available: ${requiredModels.join(", ")}`);
|
|
10445
|
-
for (const model of requiredModels) {
|
|
10446
|
-
const limits = catalog.models.get(model)?.limits;
|
|
10447
|
-
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}`);
|
|
10448
|
-
else log.error(`Model token limits unavailable: model=${model}; preserving client budgets without assuming a capacity.`);
|
|
10449
|
-
}
|
|
10450
|
-
};
|
|
10451
|
-
const createProbePayload = (model) => ({
|
|
10452
|
-
model,
|
|
10453
|
-
max_tokens: 16,
|
|
10454
|
-
stream: false,
|
|
10455
|
-
messages: [{
|
|
10456
|
-
role: "user",
|
|
10457
|
-
content: "Reply with OK only."
|
|
10458
|
-
}]
|
|
10592
|
+
//#region src/lib/claude-app.ts
|
|
10593
|
+
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10594
|
+
const configIdPattern = /^[a-f0-9-]{36}$/;
|
|
10595
|
+
const currentPlatform = () => ({
|
|
10596
|
+
platform: process.platform,
|
|
10597
|
+
env: process.env,
|
|
10598
|
+
home: os.homedir()
|
|
10459
10599
|
});
|
|
10460
|
-
|
|
10461
|
-
|
|
10462
|
-
|
|
10463
|
-
|
|
10464
|
-
|
|
10465
|
-
|
|
10466
|
-
|
|
10467
|
-
|
|
10468
|
-
timeoutMs: config.upstreamTimeoutMs
|
|
10469
|
-
});
|
|
10470
|
-
if (typeof response !== "object" || response === null || !("choices" in response)) throw new Error(`Preflight request for ${model} unexpectedly streamed`);
|
|
10471
|
-
}
|
|
10472
|
-
log.info(`Preflight OK: model=${model} think_effort=${effortLabel}`);
|
|
10473
|
-
} catch (error) {
|
|
10474
|
-
if (error instanceof HTTPError) {
|
|
10475
|
-
const responseBody = await error.response.text().catch(() => "");
|
|
10476
|
-
throw new Error(`Preflight failed for model=${model} think_effort=${effortLabel}: ${error.response.status} ${error.response.statusText}${responseBody ? ` ${responseBody}` : ""}`);
|
|
10477
|
-
}
|
|
10478
|
-
throw error;
|
|
10600
|
+
/**
|
|
10601
|
+
* The app's third-party data directory: Electron's userData for "Claude" with "-3p" appended, or
|
|
10602
|
+
* %LOCALAPPDATA%\Claude-3p on Windows.
|
|
10603
|
+
*/
|
|
10604
|
+
const claudeAppDataDir = ({ platform, env, home } = currentPlatform()) => {
|
|
10605
|
+
if (platform === "win32") {
|
|
10606
|
+
const localAppData = env.LOCALAPPDATA || path.win32.join(home, "AppData", "Local");
|
|
10607
|
+
return path.win32.join(localAppData, "Claude-3p");
|
|
10479
10608
|
}
|
|
10609
|
+
if (platform === "darwin") return path.posix.join(home, "Library", "Application Support", "Claude-3p");
|
|
10610
|
+
const configHome = env.XDG_CONFIG_HOME || path.posix.join(home, ".config");
|
|
10611
|
+
return path.posix.join(configHome, "Claude-3p");
|
|
10480
10612
|
};
|
|
10481
|
-
const
|
|
10482
|
-
|
|
10483
|
-
|
|
10484
|
-
|
|
10485
|
-
|
|
10486
|
-
|
|
10487
|
-
|
|
10488
|
-
|
|
10489
|
-
|
|
10613
|
+
const standardDataDir = (dataDir) => dataDir.replace(/-3p$/, "");
|
|
10614
|
+
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/;
|
|
10615
|
+
const looksAnthropic = (id) => {
|
|
10616
|
+
const lower = id.toLowerCase();
|
|
10617
|
+
return !otherVendorMarker.test(lower) && [
|
|
10618
|
+
"claude",
|
|
10619
|
+
"anthropic",
|
|
10620
|
+
"opus",
|
|
10621
|
+
"sonnet",
|
|
10622
|
+
"haiku",
|
|
10623
|
+
"fable",
|
|
10624
|
+
"mythos"
|
|
10625
|
+
].some((word) => lower.includes(word));
|
|
10490
10626
|
};
|
|
10491
|
-
|
|
10492
|
-
//#endregion
|
|
10493
|
-
//#region src/start.ts
|
|
10494
|
-
const canCloseConnections = (server) => typeof server.closeIdleConnections === "function" && typeof server.closeAllConnections === "function";
|
|
10495
|
-
/**
|
|
10496
|
-
* The startup warning for a listener that other machines may reach without a key.
|
|
10497
|
-
*
|
|
10498
|
-
* A warning rather than a refusal, so an existing `host: 0.0.0.0` setup keeps
|
|
10499
|
-
* working after an upgrade. It names the apiKey setting and never prints a key.
|
|
10500
|
-
*/
|
|
10501
|
-
const unauthenticatedListenerWarning = (config) => {
|
|
10502
|
-
if (config.apiKey || isLoopbackHost(config.host)) return;
|
|
10503
|
-
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.`);
|
|
10504
|
-
};
|
|
10505
|
-
/**
|
|
10506
|
-
* The warning for a config.yaml that other local users can access while it holds the apiKey.
|
|
10507
|
-
*
|
|
10508
|
-
* A new config.yaml is created 0600, but an existing one keeps its mode, and the relay never
|
|
10509
|
-
* changes the mode of the user's own file. stat follows a symlink, so the mode checked is the
|
|
10510
|
-
* target's. Windows has no POSIX mode bits. It never prints a key.
|
|
10511
|
-
*/
|
|
10512
|
-
const exposedConfigWarning = (config, configPath$1 = paths.configPath) => {
|
|
10513
|
-
if (!config.apiKey || process.platform === "win32") return;
|
|
10514
|
-
let mode;
|
|
10515
|
-
try {
|
|
10516
|
-
mode = statSync(configPath$1).mode & 511;
|
|
10517
|
-
} catch {
|
|
10518
|
-
return;
|
|
10519
|
-
}
|
|
10520
|
-
if ((mode & 63) === 0) return;
|
|
10521
|
-
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}.`);
|
|
10522
|
-
};
|
|
10523
|
-
async function startRelay(appConfig) {
|
|
10524
|
-
appConfig ??= await readAppConfig();
|
|
10525
|
-
appConfig = {
|
|
10526
|
-
...appConfig,
|
|
10527
|
-
thinkEffort: normalizeThinkEffort(appConfig.thinkEffort) ?? defaultReasoningEffort
|
|
10528
|
-
};
|
|
10529
|
-
setLogLevel(appConfig.logLevel);
|
|
10530
|
-
await cleanupLogs(appConfig.logRetentionDays);
|
|
10531
|
-
await cleanupCaptures(appConfig.logRetentionDays);
|
|
10532
|
-
configureUpstreamDispatcher(appConfig.upstreamProxy);
|
|
10533
|
-
const claudeConfigPath = getClaudeConfigPath();
|
|
10534
|
-
const config = readProxyConfig(appConfig);
|
|
10535
|
-
const applyRuntimeConfig = (nextConfig) => {
|
|
10536
|
-
registerSensitiveOrigin(nextConfig.copilotBaseUrl);
|
|
10537
|
-
registerLogSecret(nextConfig.apiKey);
|
|
10538
|
-
setLogLevel(nextConfig.logLevel);
|
|
10539
|
-
cleanupLogs(nextConfig.logRetentionDays).catch(() => log.error("Log retention failed; existing logs retained."));
|
|
10540
|
-
cleanupCaptures(nextConfig.logRetentionDays);
|
|
10541
|
-
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.");
|
|
10542
|
-
runtimeState.debug = nextConfig.logLevel === "debug";
|
|
10543
|
-
runtimeState.thinkEffort = nextConfig.thinkEffort;
|
|
10544
|
-
config.copilotBaseUrl = nextConfig.copilotBaseUrl;
|
|
10545
|
-
runtimeState.upstreamBaseUrl = nextConfig.copilotBaseUrl;
|
|
10546
|
-
config.host = nextConfig.host;
|
|
10547
|
-
config.port = nextConfig.port;
|
|
10548
|
-
config.upstreamTimeoutMs = nextConfig.upstreamTimeoutSeconds * 1e3;
|
|
10549
|
-
config.webSearchBackend = nextConfig.webSearchBackend;
|
|
10550
|
-
config.claudeUpstreamApi = nextConfig.claudeUpstreamApi;
|
|
10551
|
-
config.apiKey = nextConfig.apiKey;
|
|
10552
|
-
runtimeState.modelRouting = {
|
|
10553
|
-
gptModel: nextConfig.gptModel,
|
|
10554
|
-
opusModel: nextConfig.opusModel
|
|
10555
|
-
};
|
|
10556
|
-
};
|
|
10557
|
-
applyRuntimeConfig(appConfig);
|
|
10558
|
-
log.info(`Log level: ${appConfig.logLevel}`);
|
|
10559
|
-
log.info(`Default think effort: ${appConfig.thinkEffort}`);
|
|
10560
|
-
log.info(`Upstream timeout: ${appConfig.upstreamTimeoutSeconds}s`);
|
|
10561
|
-
if (appConfig.upstreamProxy !== void 0) log.info(`Upstream proxy: ${formatUpstreamProxyForDisplay(appConfig.upstreamProxy)}`);
|
|
10562
|
-
const listenerWarning = unauthenticatedListenerWarning(appConfig);
|
|
10563
|
-
if (listenerWarning) log.error(listenerWarning);
|
|
10564
|
-
const configWarning = exposedConfigWarning(appConfig);
|
|
10565
|
-
if (configWarning) log.error(configWarning);
|
|
10566
|
-
const loginLookup = new AbortController();
|
|
10567
|
-
const authSession = await setupProxyAuth(config, { loginSignal: loginLookup.signal });
|
|
10568
|
-
try {
|
|
10569
|
-
await validateUpstream(config, appConfig.thinkEffort);
|
|
10570
|
-
} catch (error) {
|
|
10571
|
-
log.error("Startup preflight failed:", error);
|
|
10572
|
-
process.exit(1);
|
|
10573
|
-
}
|
|
10574
|
-
log.info(`Exposed models: ${getExposedModelIds().join(", ")}`);
|
|
10575
|
-
try {
|
|
10576
|
-
await preloadTokenizers(getUpstreamModelIds().map((id) => getCachedCopilotModel(config, id)?.tokenizer));
|
|
10577
|
-
log.info(`Tokenizers loaded: ${loadedTokenizers().join(", ")}`);
|
|
10578
|
-
} catch (error) {
|
|
10579
|
-
log.error("Tokenizer preload failed; count_tokens loads tokenizers on first use:", error);
|
|
10580
|
-
}
|
|
10581
|
-
const server = await startServer(config);
|
|
10582
|
-
await writeRelayPidFile(config);
|
|
10583
|
-
log.info(`copilot-relay version: ${appVersion}`);
|
|
10584
|
-
authSession.githubLogin.then((githubLogin) => {
|
|
10585
|
-
if (githubLogin) log.info(`GitHub user: ${githubLogin}`);
|
|
10586
|
-
else log.error("GitHub user: unavailable");
|
|
10587
|
-
});
|
|
10588
|
-
const baseUrl = getRelayBaseUrl(config.host, config.port);
|
|
10589
|
-
log.info(`copilot-relay listening on ${baseUrl}`);
|
|
10590
|
-
log.info(`copilot base url: ${formatUrlForDisplay(config.copilotBaseUrl)}`);
|
|
10591
|
-
if (appConfig.claudeSetup) try {
|
|
10592
|
-
const gptLimits = getCachedCopilotModel(config, appConfig.gptModel)?.limits;
|
|
10593
|
-
const opusLimits = getCachedCopilotModel(config, appConfig.opusModel)?.limits;
|
|
10594
|
-
const claudeResult = await applyClaudeConfig({
|
|
10595
|
-
apiKey: appConfig.apiKey,
|
|
10596
|
-
baseUrl,
|
|
10597
|
-
configPath: claudeConfigPath,
|
|
10598
|
-
gptModel: appConfig.gptModel,
|
|
10599
|
-
gptLimits,
|
|
10600
|
-
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
10601
|
-
});
|
|
10602
|
-
if (claudeResult.changed) {
|
|
10603
|
-
log.info(`claude settings ${claudeResult.created ? "created" : "updated"}: ${claudeResult.configPath}`);
|
|
10604
|
-
if (claudeResult.previousBaseUrl && claudeResult.previousBaseUrl !== baseUrl) log.info(`ANTHROPIC_BASE_URL: ${claudeResult.previousBaseUrl} → ${baseUrl}`);
|
|
10605
|
-
} else log.info(`claude settings already up to date: ${claudeResult.configPath}`);
|
|
10606
|
-
} catch (error) {
|
|
10607
|
-
log.error(`Could not update claude settings (${claudeConfigPath}):`, error);
|
|
10608
|
-
}
|
|
10609
|
-
if (appConfig.codexSetup) {
|
|
10610
|
-
const codexHome = getCodexHome();
|
|
10611
|
-
try {
|
|
10612
|
-
const codexResult = await applyCodexSetup({
|
|
10613
|
-
apiKey: appConfig.apiKey,
|
|
10614
|
-
baseUrl,
|
|
10615
|
-
catalogPath: paths.codexCatalogPath,
|
|
10616
|
-
codexHome,
|
|
10617
|
-
models: [appConfig.gptModel, appConfig.opusModel].map((id) => ({
|
|
10618
|
-
id,
|
|
10619
|
-
capabilities: getCachedCopilotModel(config, id)
|
|
10620
|
-
}))
|
|
10621
|
-
});
|
|
10622
|
-
if (codexResult.skipped) log.info(`codex setup skipped: ${codexHome} does not exist`);
|
|
10623
|
-
else if (codexResult.changed) log.info(`codex settings updated: ${codexResult.configPath}`);
|
|
10624
|
-
else log.info(`codex settings already up to date: ${codexResult.configPath}`);
|
|
10625
|
-
} catch (error) {
|
|
10626
|
-
log.error(`Could not update codex settings (${codexHome}):`, error);
|
|
10627
|
-
}
|
|
10628
|
-
}
|
|
10629
|
-
const configWatcher = watchAppConfig((nextConfig) => {
|
|
10630
|
-
const previousApiKey = config.apiKey;
|
|
10631
|
-
applyRuntimeConfig(nextConfig);
|
|
10632
|
-
log.info(`Config reloaded: logLevel=${nextConfig.logLevel} thinkEffort=${nextConfig.thinkEffort} upstreamTimeoutSeconds=${nextConfig.upstreamTimeoutSeconds}`);
|
|
10633
|
-
const reloadedConfigWarning = nextConfig.apiKey === previousApiKey ? void 0 : exposedConfigWarning(nextConfig);
|
|
10634
|
-
if (reloadedConfigWarning) log.error(reloadedConfigWarning);
|
|
10635
|
-
});
|
|
10636
|
-
const shutdownGraceMs = 2e3;
|
|
10637
|
-
const shutdown = (signal) => {
|
|
10638
|
-
log.info(`Received ${signal}; shutting down copilot-relay`);
|
|
10639
|
-
loginLookup.abort();
|
|
10640
|
-
server.close((error) => {
|
|
10641
|
-
if (error) {
|
|
10642
|
-
log.error("Error while shutting down copilot-relay:", error);
|
|
10643
|
-
process.exitCode = 1;
|
|
10644
|
-
}
|
|
10645
|
-
});
|
|
10646
|
-
if (canCloseConnections(server)) {
|
|
10647
|
-
server.closeIdleConnections();
|
|
10648
|
-
setTimeout(() => {
|
|
10649
|
-
log.info("Grace period elapsed; closing remaining connections");
|
|
10650
|
-
server.closeAllConnections();
|
|
10651
|
-
}, shutdownGraceMs).unref();
|
|
10652
|
-
}
|
|
10653
|
-
};
|
|
10654
|
-
process.once("SIGINT", shutdown);
|
|
10655
|
-
process.once("SIGTERM", shutdown);
|
|
10656
|
-
try {
|
|
10657
|
-
await new Promise((resolve$1, reject) => {
|
|
10658
|
-
server.on("close", resolve$1);
|
|
10659
|
-
server.on("error", reject);
|
|
10660
|
-
});
|
|
10661
|
-
} finally {
|
|
10662
|
-
process.off("SIGINT", shutdown);
|
|
10663
|
-
process.off("SIGTERM", shutdown);
|
|
10664
|
-
clearInterval(configWatcher);
|
|
10665
|
-
await clearRelayPidFile();
|
|
10666
|
-
await flushCaptures();
|
|
10667
|
-
await flushLogs();
|
|
10668
|
-
}
|
|
10669
|
-
}
|
|
10670
|
-
const start = defineCommand({
|
|
10671
|
-
meta: {
|
|
10672
|
-
name: "start",
|
|
10673
|
-
description: "Start the copilot-relay HTTP server."
|
|
10674
|
-
},
|
|
10675
|
-
async run() {
|
|
10676
|
-
await startRelay();
|
|
10677
|
-
}
|
|
10678
|
-
});
|
|
10679
|
-
|
|
10680
|
-
//#endregion
|
|
10681
|
-
//#region src/restart.ts
|
|
10682
|
-
const restart = defineCommand({
|
|
10683
|
-
meta: {
|
|
10684
|
-
name: "restart",
|
|
10685
|
-
description: "Stop any existing copilot-relay instance and start a new one."
|
|
10686
|
-
},
|
|
10687
|
-
async run() {
|
|
10688
|
-
const appConfig = await readAppConfig();
|
|
10689
|
-
setLogLevel(appConfig.logLevel);
|
|
10690
|
-
await cleanupLogs(appConfig.logRetentionDays);
|
|
10691
|
-
const stopped = await stopExistingRelay(readProxyConfig(appConfig));
|
|
10692
|
-
if (stopped.length > 0) log.info(`Stopped copilot-relay pid(s): ${stopped.join(", ")}`);
|
|
10693
|
-
log.info("Starting copilot-relay");
|
|
10694
|
-
await startRelay(appConfig);
|
|
10695
|
-
}
|
|
10696
|
-
});
|
|
10697
|
-
|
|
10698
|
-
//#endregion
|
|
10699
|
-
//#region src/lib/claude-app.ts
|
|
10700
|
-
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10701
|
-
const configIdPattern = /^[a-f0-9-]{36}$/;
|
|
10702
|
-
const currentPlatform = () => ({
|
|
10703
|
-
platform: process.platform,
|
|
10704
|
-
env: process.env,
|
|
10705
|
-
home: os.homedir()
|
|
10706
|
-
});
|
|
10707
|
-
/**
|
|
10708
|
-
* The app's third-party data directory: Electron's userData for "Claude" with "-3p" appended, or
|
|
10709
|
-
* %LOCALAPPDATA%\Claude-3p on Windows.
|
|
10710
|
-
*/
|
|
10711
|
-
const claudeAppDataDir = ({ platform, env, home } = currentPlatform()) => {
|
|
10712
|
-
if (platform === "win32") {
|
|
10713
|
-
const localAppData = env.LOCALAPPDATA || path.win32.join(home, "AppData", "Local");
|
|
10714
|
-
return path.win32.join(localAppData, "Claude-3p");
|
|
10715
|
-
}
|
|
10716
|
-
if (platform === "darwin") return path.posix.join(home, "Library", "Application Support", "Claude-3p");
|
|
10717
|
-
const configHome = env.XDG_CONFIG_HOME || path.posix.join(home, ".config");
|
|
10718
|
-
return path.posix.join(configHome, "Claude-3p");
|
|
10719
|
-
};
|
|
10720
|
-
const standardDataDir = (dataDir) => dataDir.replace(/-3p$/, "");
|
|
10721
|
-
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/;
|
|
10722
|
-
const looksAnthropic = (id) => {
|
|
10723
|
-
const lower = id.toLowerCase();
|
|
10724
|
-
return !otherVendorMarker.test(lower) && [
|
|
10725
|
-
"claude",
|
|
10726
|
-
"anthropic",
|
|
10727
|
-
"opus",
|
|
10728
|
-
"sonnet",
|
|
10729
|
-
"haiku",
|
|
10730
|
-
"fable",
|
|
10731
|
-
"mythos"
|
|
10732
|
-
].some((word) => lower.includes(word));
|
|
10733
|
-
};
|
|
10734
|
-
const routesToTier = (name, tier) => name.toLowerCase().includes("opus") === (tier === "opus");
|
|
10627
|
+
const routesToTier = (name, tier) => name.toLowerCase().includes("opus") === (tier === "opus");
|
|
10735
10628
|
/**
|
|
10736
10629
|
* The name the app sends for a model: its own ID when the app accepts it and the relay routes that
|
|
10737
10630
|
* ID to the model's own slot, otherwise a Claude-style route for its tier, such as
|
|
@@ -10840,6 +10733,11 @@ const applyClaudeAppSetup = async (input) => {
|
|
|
10840
10733
|
};
|
|
10841
10734
|
};
|
|
10842
10735
|
|
|
10736
|
+
//#endregion
|
|
10737
|
+
//#region src/lib/defaults.ts
|
|
10738
|
+
const getClaudeConfigPath = () => path.join(process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), ".claude"), "settings.json");
|
|
10739
|
+
const getCodexHome = () => process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
|
|
10740
|
+
|
|
10843
10741
|
//#endregion
|
|
10844
10742
|
//#region src/lib/client-setup.ts
|
|
10845
10743
|
const setupTargets = [
|
|
@@ -10867,6 +10765,7 @@ const setupClaudeCodeCli = async (context) => {
|
|
|
10867
10765
|
configPath: configPath$1,
|
|
10868
10766
|
gptModel: appConfig.gptModel,
|
|
10869
10767
|
gptLimits,
|
|
10768
|
+
contextWindowOf: (model) => capabilities(context, model)?.limits?.max_context_window_tokens,
|
|
10870
10769
|
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
10871
10770
|
});
|
|
10872
10771
|
if (result.skippedReason) return {
|
|
@@ -10880,6 +10779,15 @@ const setupClaudeCodeCli = async (context) => {
|
|
|
10880
10779
|
status: result.changed ? "updated" : "already up to date"
|
|
10881
10780
|
};
|
|
10882
10781
|
};
|
|
10782
|
+
/** Codex's models: the exposed upstream IDs without [1m], gptModel first as Codex's default. */
|
|
10783
|
+
const codexModels = (appConfig, catalog) => resolveExposedModels({
|
|
10784
|
+
gptModel: appConfig.gptModel,
|
|
10785
|
+
opusModel: appConfig.opusModel,
|
|
10786
|
+
models: parseModelSelection(appConfig.models)
|
|
10787
|
+
}, catalog?.models).map((id) => ({
|
|
10788
|
+
id,
|
|
10789
|
+
capabilities: catalog?.models.get(id)
|
|
10790
|
+
}));
|
|
10883
10791
|
const setupCodex = async (context, target) => {
|
|
10884
10792
|
const { appConfig } = context;
|
|
10885
10793
|
const result = await applyCodexSetup({
|
|
@@ -10887,10 +10795,7 @@ const setupCodex = async (context, target) => {
|
|
|
10887
10795
|
baseUrl: context.baseUrl,
|
|
10888
10796
|
catalogPath: paths.codexCatalogPath,
|
|
10889
10797
|
codexHome: getCodexHome(),
|
|
10890
|
-
models:
|
|
10891
|
-
id,
|
|
10892
|
-
capabilities: capabilities(context, id)
|
|
10893
|
-
})),
|
|
10798
|
+
models: codexModels(appConfig, context.catalog),
|
|
10894
10799
|
force: context.force
|
|
10895
10800
|
});
|
|
10896
10801
|
if (result.skipped) return {
|
|
@@ -10904,6 +10809,26 @@ const setupCodex = async (context, target) => {
|
|
|
10904
10809
|
status: result.changed ? "updated" : "already up to date"
|
|
10905
10810
|
};
|
|
10906
10811
|
};
|
|
10812
|
+
/**
|
|
10813
|
+
* `start`'s Codex setup when codexSetup is on: the same setup as `copilot-relay setup`, never forced,
|
|
10814
|
+
* so a machine where Codex has not run is skipped. A failure is logged rather than stopping the relay.
|
|
10815
|
+
*/
|
|
10816
|
+
const startupCodexSetup = async (appConfig, catalog, baseUrl) => {
|
|
10817
|
+
const codexHome = getCodexHome();
|
|
10818
|
+
try {
|
|
10819
|
+
const outcome = await setupCodex({
|
|
10820
|
+
appConfig,
|
|
10821
|
+
baseUrl,
|
|
10822
|
+
catalog,
|
|
10823
|
+
force: false
|
|
10824
|
+
}, "codex");
|
|
10825
|
+
if (outcome.status.startsWith("skipped")) log.info(`codex setup skipped: ${codexHome} does not exist`);
|
|
10826
|
+
else if (outcome.status === "updated") log.info(`codex settings updated: ${outcome.path}`);
|
|
10827
|
+
else log.info(`codex settings already up to date: ${outcome.path}`);
|
|
10828
|
+
} catch (error) {
|
|
10829
|
+
log.error(`Could not update codex settings (${codexHome}):`, error);
|
|
10830
|
+
}
|
|
10831
|
+
};
|
|
10907
10832
|
const setupClaudeCodeApp = async (context) => {
|
|
10908
10833
|
const { appConfig } = context;
|
|
10909
10834
|
const result = await applyClaudeAppSetup({
|
|
@@ -10956,6 +10881,255 @@ const runSetup = async (targets, context) => {
|
|
|
10956
10881
|
return outcomes;
|
|
10957
10882
|
};
|
|
10958
10883
|
|
|
10884
|
+
//#endregion
|
|
10885
|
+
//#region src/lib/preflight.ts
|
|
10886
|
+
const ensureRequiredModels = async (config) => {
|
|
10887
|
+
const catalog = await loadCopilotModelCatalog(config);
|
|
10888
|
+
const requiredModels = getUpstreamModelIds();
|
|
10889
|
+
const missingModels = requiredModels.filter((model) => !catalog.models.has(model));
|
|
10890
|
+
if (missingModels.length > 0) throw new Error(`Required Copilot model(s) unavailable upstream: ${missingModels.join(", ")}`);
|
|
10891
|
+
log.info(`Upstream models available: ${requiredModels.join(", ")}`);
|
|
10892
|
+
const missingSelected = findMissingModels(getModelRouting(), catalog.models);
|
|
10893
|
+
if (missingSelected.length > 0) log.error(`Selected models not in Copilot's catalog, skipped: ${missingSelected.join(", ")}`);
|
|
10894
|
+
for (const model of requiredModels) {
|
|
10895
|
+
const limits = catalog.models.get(model)?.limits;
|
|
10896
|
+
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}`);
|
|
10897
|
+
else log.error(`Model token limits unavailable: model=${model}; preserving client budgets without assuming a capacity.`);
|
|
10898
|
+
}
|
|
10899
|
+
};
|
|
10900
|
+
const createProbePayload = (model) => ({
|
|
10901
|
+
model,
|
|
10902
|
+
max_tokens: 16,
|
|
10903
|
+
stream: false,
|
|
10904
|
+
messages: [{
|
|
10905
|
+
role: "user",
|
|
10906
|
+
content: "Reply with OK only."
|
|
10907
|
+
}]
|
|
10908
|
+
});
|
|
10909
|
+
const validateModelRequest = async (config, model) => {
|
|
10910
|
+
const effortLabel = resolveModelReasoningEffort(config, model) ?? "omitted";
|
|
10911
|
+
try {
|
|
10912
|
+
if (requireCopilotEndpoint(config, model).endpoint === "/v1/messages") await handleNativeMessages(config, createProbePayload(model), { requestId: randomUUID() });
|
|
10913
|
+
else {
|
|
10914
|
+
const response = await createChatCompletions(config, createProbePayload(model), {
|
|
10915
|
+
client: "generic",
|
|
10916
|
+
requestedModel: model,
|
|
10917
|
+
timeoutMs: config.upstreamTimeoutMs
|
|
10918
|
+
});
|
|
10919
|
+
if (typeof response !== "object" || response === null || !("choices" in response)) throw new Error(`Preflight request for ${model} unexpectedly streamed`);
|
|
10920
|
+
}
|
|
10921
|
+
log.info(`Preflight OK: model=${model} think_effort=${effortLabel}`);
|
|
10922
|
+
} catch (error) {
|
|
10923
|
+
if (error instanceof HTTPError) {
|
|
10924
|
+
const responseBody = await error.response.text().catch(() => "");
|
|
10925
|
+
throw new Error(`Preflight failed for model=${model} think_effort=${effortLabel}: ${error.response.status} ${error.response.statusText}${responseBody ? ` ${responseBody}` : ""}`);
|
|
10926
|
+
}
|
|
10927
|
+
throw error;
|
|
10928
|
+
}
|
|
10929
|
+
};
|
|
10930
|
+
const validateUpstream = async (config, thinkEffort) => {
|
|
10931
|
+
log.info("Running upstream preflight");
|
|
10932
|
+
await ensureRequiredModels(config);
|
|
10933
|
+
await withRuntimeState({
|
|
10934
|
+
...snapshotRuntimeState(),
|
|
10935
|
+
thinkEffort
|
|
10936
|
+
}, async () => {
|
|
10937
|
+
for (const model of getUpstreamModelIds()) await validateModelRequest(config, model);
|
|
10938
|
+
});
|
|
10939
|
+
};
|
|
10940
|
+
|
|
10941
|
+
//#endregion
|
|
10942
|
+
//#region src/start.ts
|
|
10943
|
+
const canCloseConnections = (server) => typeof server.closeIdleConnections === "function" && typeof server.closeAllConnections === "function";
|
|
10944
|
+
/**
|
|
10945
|
+
* The startup warning for a listener that other machines may reach without a key.
|
|
10946
|
+
*
|
|
10947
|
+
* A warning rather than a refusal, so an existing `host: 0.0.0.0` setup keeps
|
|
10948
|
+
* working after an upgrade. It names the apiKey setting and never prints a key.
|
|
10949
|
+
*/
|
|
10950
|
+
const unauthenticatedListenerWarning = (config) => {
|
|
10951
|
+
if (config.apiKey || isLoopbackHost(config.host)) return;
|
|
10952
|
+
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.`);
|
|
10953
|
+
};
|
|
10954
|
+
/**
|
|
10955
|
+
* The warning for a config.yaml that other local users can access while it holds the apiKey.
|
|
10956
|
+
*
|
|
10957
|
+
* A new config.yaml is created 0600, but an existing one keeps its mode, and the relay never
|
|
10958
|
+
* changes the mode of the user's own file. stat follows a symlink, so the mode checked is the
|
|
10959
|
+
* target's. Windows has no POSIX mode bits. It never prints a key.
|
|
10960
|
+
*/
|
|
10961
|
+
const exposedConfigWarning = (config, configPath$1 = paths.configPath) => {
|
|
10962
|
+
if (!config.apiKey || process.platform === "win32") return;
|
|
10963
|
+
let mode;
|
|
10964
|
+
try {
|
|
10965
|
+
mode = statSync(configPath$1).mode & 511;
|
|
10966
|
+
} catch {
|
|
10967
|
+
return;
|
|
10968
|
+
}
|
|
10969
|
+
if ((mode & 63) === 0) return;
|
|
10970
|
+
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}.`);
|
|
10971
|
+
};
|
|
10972
|
+
async function startRelay(appConfig) {
|
|
10973
|
+
appConfig ??= await readAppConfig();
|
|
10974
|
+
appConfig = {
|
|
10975
|
+
...appConfig,
|
|
10976
|
+
thinkEffort: normalizeThinkEffort(appConfig.thinkEffort) ?? defaultReasoningEffort
|
|
10977
|
+
};
|
|
10978
|
+
setLogLevel(appConfig.logLevel);
|
|
10979
|
+
await cleanupLogs(appConfig.logRetentionDays);
|
|
10980
|
+
await cleanupCaptures(appConfig.logRetentionDays);
|
|
10981
|
+
configureUpstreamDispatcher(appConfig.upstreamProxy);
|
|
10982
|
+
const claudeConfigPath = getClaudeConfigPath();
|
|
10983
|
+
const config = readProxyConfig(appConfig);
|
|
10984
|
+
const applyRuntimeConfig = (nextConfig) => {
|
|
10985
|
+
registerSensitiveOrigin(nextConfig.copilotBaseUrl);
|
|
10986
|
+
registerLogSecret(nextConfig.apiKey);
|
|
10987
|
+
setLogLevel(nextConfig.logLevel);
|
|
10988
|
+
cleanupLogs(nextConfig.logRetentionDays).catch(() => log.error("Log retention failed; existing logs retained."));
|
|
10989
|
+
cleanupCaptures(nextConfig.logRetentionDays);
|
|
10990
|
+
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.");
|
|
10991
|
+
runtimeState.debug = nextConfig.logLevel === "debug";
|
|
10992
|
+
runtimeState.thinkEffort = nextConfig.thinkEffort;
|
|
10993
|
+
config.copilotBaseUrl = nextConfig.copilotBaseUrl;
|
|
10994
|
+
runtimeState.upstreamBaseUrl = nextConfig.copilotBaseUrl;
|
|
10995
|
+
config.host = nextConfig.host;
|
|
10996
|
+
config.port = nextConfig.port;
|
|
10997
|
+
config.upstreamTimeoutMs = nextConfig.upstreamTimeoutSeconds * 1e3;
|
|
10998
|
+
config.webSearchBackend = nextConfig.webSearchBackend;
|
|
10999
|
+
config.claudeUpstreamApi = nextConfig.claudeUpstreamApi;
|
|
11000
|
+
config.apiKey = nextConfig.apiKey;
|
|
11001
|
+
runtimeState.modelRouting = {
|
|
11002
|
+
gptModel: nextConfig.gptModel,
|
|
11003
|
+
opusModel: nextConfig.opusModel,
|
|
11004
|
+
models: parseModelSelection(nextConfig.models)
|
|
11005
|
+
};
|
|
11006
|
+
};
|
|
11007
|
+
applyRuntimeConfig(appConfig);
|
|
11008
|
+
log.info(`Log level: ${appConfig.logLevel}`);
|
|
11009
|
+
log.info(`Default think effort: ${appConfig.thinkEffort}`);
|
|
11010
|
+
log.info(`Upstream timeout: ${appConfig.upstreamTimeoutSeconds}s`);
|
|
11011
|
+
if (appConfig.upstreamProxy !== void 0) log.info(`Upstream proxy: ${formatUpstreamProxyForDisplay(appConfig.upstreamProxy)}`);
|
|
11012
|
+
const listenerWarning = unauthenticatedListenerWarning(appConfig);
|
|
11013
|
+
if (listenerWarning) log.error(listenerWarning);
|
|
11014
|
+
const configWarning = exposedConfigWarning(appConfig);
|
|
11015
|
+
if (configWarning) log.error(configWarning);
|
|
11016
|
+
const loginLookup = new AbortController();
|
|
11017
|
+
const authSession = await setupProxyAuth(config, { loginSignal: loginLookup.signal });
|
|
11018
|
+
try {
|
|
11019
|
+
await validateUpstream(config, appConfig.thinkEffort);
|
|
11020
|
+
} catch (error) {
|
|
11021
|
+
log.error("Startup preflight failed:", error);
|
|
11022
|
+
process.exit(1);
|
|
11023
|
+
}
|
|
11024
|
+
log.info(`Exposed models: ${getExposedModelIds().join(", ")}`);
|
|
11025
|
+
try {
|
|
11026
|
+
await preloadTokenizers(getUpstreamModelIds().map((id) => getCachedCopilotModel(config, id)?.tokenizer));
|
|
11027
|
+
log.info(`Tokenizers loaded: ${loadedTokenizers().join(", ")}`);
|
|
11028
|
+
} catch (error) {
|
|
11029
|
+
log.error("Tokenizer preload failed; count_tokens loads tokenizers on first use:", error);
|
|
11030
|
+
}
|
|
11031
|
+
const server = await startServer(config);
|
|
11032
|
+
await writeRelayPidFile(config);
|
|
11033
|
+
log.info(`copilot-relay version: ${appVersion}`);
|
|
11034
|
+
authSession.githubLogin.then((githubLogin) => {
|
|
11035
|
+
if (githubLogin) log.info(`GitHub user: ${githubLogin}`);
|
|
11036
|
+
else log.error("GitHub user: unavailable");
|
|
11037
|
+
});
|
|
11038
|
+
const baseUrl = getRelayBaseUrl(config.host, config.port);
|
|
11039
|
+
log.info(`copilot-relay listening on ${baseUrl}`);
|
|
11040
|
+
log.info(`copilot base url: ${formatUrlForDisplay(config.copilotBaseUrl)}`);
|
|
11041
|
+
if (appConfig.claudeSetup) try {
|
|
11042
|
+
const gptLimits = getCachedCopilotModel(config, appConfig.gptModel)?.limits;
|
|
11043
|
+
const opusLimits = getCachedCopilotModel(config, appConfig.opusModel)?.limits;
|
|
11044
|
+
const claudeResult = await applyClaudeConfig({
|
|
11045
|
+
apiKey: appConfig.apiKey,
|
|
11046
|
+
baseUrl,
|
|
11047
|
+
configPath: claudeConfigPath,
|
|
11048
|
+
gptModel: appConfig.gptModel,
|
|
11049
|
+
gptLimits,
|
|
11050
|
+
contextWindowOf: (model) => getCachedCopilotModel(config, model)?.limits?.max_context_window_tokens,
|
|
11051
|
+
maxOutputTokens: gptLimits && opusLimits ? Math.max(gptLimits.max_output_tokens, opusLimits.max_output_tokens) : void 0
|
|
11052
|
+
});
|
|
11053
|
+
if (claudeResult.changed) {
|
|
11054
|
+
log.info(`claude settings ${claudeResult.created ? "created" : "updated"}: ${claudeResult.configPath}`);
|
|
11055
|
+
if (claudeResult.previousBaseUrl && claudeResult.previousBaseUrl !== baseUrl) log.info(`ANTHROPIC_BASE_URL: ${claudeResult.previousBaseUrl} → ${baseUrl}`);
|
|
11056
|
+
} else log.info(`claude settings already up to date: ${claudeResult.configPath}`);
|
|
11057
|
+
} catch (error) {
|
|
11058
|
+
log.error(`Could not update claude settings (${claudeConfigPath}):`, error);
|
|
11059
|
+
}
|
|
11060
|
+
if (appConfig.codexSetup) {
|
|
11061
|
+
const catalog = config.modelCatalog?.baseUrl === config.copilotBaseUrl ? config.modelCatalog : void 0;
|
|
11062
|
+
await startupCodexSetup(appConfig, catalog, baseUrl);
|
|
11063
|
+
}
|
|
11064
|
+
const configWatcher = watchAppConfig((nextConfig) => {
|
|
11065
|
+
const previousApiKey = config.apiKey;
|
|
11066
|
+
applyRuntimeConfig(nextConfig);
|
|
11067
|
+
log.info(`Config reloaded: logLevel=${nextConfig.logLevel} thinkEffort=${nextConfig.thinkEffort} upstreamTimeoutSeconds=${nextConfig.upstreamTimeoutSeconds}`);
|
|
11068
|
+
const reloadedConfigWarning = nextConfig.apiKey === previousApiKey ? void 0 : exposedConfigWarning(nextConfig);
|
|
11069
|
+
if (reloadedConfigWarning) log.error(reloadedConfigWarning);
|
|
11070
|
+
});
|
|
11071
|
+
const shutdownGraceMs = 2e3;
|
|
11072
|
+
const shutdown = (signal) => {
|
|
11073
|
+
log.info(`Received ${signal}; shutting down copilot-relay`);
|
|
11074
|
+
loginLookup.abort();
|
|
11075
|
+
server.close((error) => {
|
|
11076
|
+
if (error) {
|
|
11077
|
+
log.error("Error while shutting down copilot-relay:", error);
|
|
11078
|
+
process.exitCode = 1;
|
|
11079
|
+
}
|
|
11080
|
+
});
|
|
11081
|
+
if (canCloseConnections(server)) {
|
|
11082
|
+
server.closeIdleConnections();
|
|
11083
|
+
setTimeout(() => {
|
|
11084
|
+
log.info("Grace period elapsed; closing remaining connections");
|
|
11085
|
+
server.closeAllConnections();
|
|
11086
|
+
}, shutdownGraceMs).unref();
|
|
11087
|
+
}
|
|
11088
|
+
};
|
|
11089
|
+
process.once("SIGINT", shutdown);
|
|
11090
|
+
process.once("SIGTERM", shutdown);
|
|
11091
|
+
try {
|
|
11092
|
+
await new Promise((resolve$1, reject) => {
|
|
11093
|
+
server.on("close", resolve$1);
|
|
11094
|
+
server.on("error", reject);
|
|
11095
|
+
});
|
|
11096
|
+
} finally {
|
|
11097
|
+
process.off("SIGINT", shutdown);
|
|
11098
|
+
process.off("SIGTERM", shutdown);
|
|
11099
|
+
clearInterval(configWatcher);
|
|
11100
|
+
await clearRelayPidFile();
|
|
11101
|
+
await flushCaptures();
|
|
11102
|
+
await flushLogs();
|
|
11103
|
+
}
|
|
11104
|
+
}
|
|
11105
|
+
const start = defineCommand({
|
|
11106
|
+
meta: {
|
|
11107
|
+
name: "start",
|
|
11108
|
+
description: "Start the copilot-relay HTTP server."
|
|
11109
|
+
},
|
|
11110
|
+
async run() {
|
|
11111
|
+
await startRelay();
|
|
11112
|
+
}
|
|
11113
|
+
});
|
|
11114
|
+
|
|
11115
|
+
//#endregion
|
|
11116
|
+
//#region src/restart.ts
|
|
11117
|
+
const restart = defineCommand({
|
|
11118
|
+
meta: {
|
|
11119
|
+
name: "restart",
|
|
11120
|
+
description: "Stop any existing copilot-relay instance and start a new one."
|
|
11121
|
+
},
|
|
11122
|
+
async run() {
|
|
11123
|
+
const appConfig = await readAppConfig();
|
|
11124
|
+
setLogLevel(appConfig.logLevel);
|
|
11125
|
+
await cleanupLogs(appConfig.logRetentionDays);
|
|
11126
|
+
const stopped = await stopExistingRelay(readProxyConfig(appConfig));
|
|
11127
|
+
if (stopped.length > 0) log.info(`Stopped copilot-relay pid(s): ${stopped.join(", ")}`);
|
|
11128
|
+
log.info("Starting copilot-relay");
|
|
11129
|
+
await startRelay(appConfig);
|
|
11130
|
+
}
|
|
11131
|
+
});
|
|
11132
|
+
|
|
10959
11133
|
//#endregion
|
|
10960
11134
|
//#region src/setup.ts
|
|
10961
11135
|
const printable = (text$1) => scrubLogSecrets(terminalText(text$1));
|
|
@@ -11118,13 +11292,15 @@ const configRowOrder = {
|
|
|
11118
11292
|
webSearchBackend: 11,
|
|
11119
11293
|
gptModel: 12,
|
|
11120
11294
|
opusModel: 13,
|
|
11121
|
-
|
|
11295
|
+
models: 14,
|
|
11296
|
+
claudeUpstreamApi: 15
|
|
11122
11297
|
};
|
|
11123
11298
|
const configRowKeys = Object.keys(configRowOrder).sort((a, b) => configRowOrder[a] - configRowOrder[b]);
|
|
11124
11299
|
const unsetConfigText = {
|
|
11125
11300
|
apiKey: "(unset — clients need no key)",
|
|
11126
11301
|
upstreamProxy: "(unset — connects directly)",
|
|
11127
|
-
webSearchBackend: "(unset — uses gptModel)"
|
|
11302
|
+
webSearchBackend: "(unset — uses gptModel)",
|
|
11303
|
+
models: "(empty — gptModel and opusModel only)"
|
|
11128
11304
|
};
|
|
11129
11305
|
/**
|
|
11130
11306
|
* The resolved config, one key per line.
|