copilot-relay 0.6.0 → 0.6.2
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/dist/main.js +127 -60
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
2
3
|
import { defineCommand, runMain } from "citty";
|
|
3
4
|
import fs from "node:fs/promises";
|
|
4
5
|
import { createHash, randomUUID, timingSafeEqual } from "node:crypto";
|
|
@@ -8,7 +9,6 @@ import { inspect, isDeepStrictEqual, promisify } from "node:util";
|
|
|
8
9
|
import consola from "consola";
|
|
9
10
|
import os from "node:os";
|
|
10
11
|
import path, { dirname, posix, resolve } from "node:path";
|
|
11
|
-
import { Agent, EnvHttpProxyAgent, ProxyAgent, fetch } from "undici";
|
|
12
12
|
import { fileURLToPath } from "node:url";
|
|
13
13
|
import { createReadStream, readFileSync, statSync } from "node:fs";
|
|
14
14
|
import { BlockList, isIP, isIPv4 } from "node:net";
|
|
@@ -594,6 +594,14 @@ const cleanupLogs = async (retentionDays) => {
|
|
|
594
594
|
};
|
|
595
595
|
const log = consola;
|
|
596
596
|
|
|
597
|
+
//#endregion
|
|
598
|
+
//#region src/lib/undici.ts
|
|
599
|
+
let undici;
|
|
600
|
+
const loadUndici = () => {
|
|
601
|
+
undici ??= createRequire(import.meta.url)("undici");
|
|
602
|
+
return undici;
|
|
603
|
+
};
|
|
604
|
+
|
|
597
605
|
//#endregion
|
|
598
606
|
//#region src/lib/upstream-dispatcher.ts
|
|
599
607
|
const connectionOptions = {
|
|
@@ -620,6 +628,7 @@ var InvalidProxyEnvironmentError = class extends Error {
|
|
|
620
628
|
* normalizeUpstreamProxy has already validated.
|
|
621
629
|
*/
|
|
622
630
|
const buildUpstreamDispatcher = (upstreamProxy) => {
|
|
631
|
+
const { Agent, EnvHttpProxyAgent, ProxyAgent } = loadUndici();
|
|
623
632
|
if (upstreamProxy === void 0) return new Agent(connectionOptions);
|
|
624
633
|
if (upstreamProxy === "env") try {
|
|
625
634
|
return new EnvHttpProxyAgent(connectionOptions);
|
|
@@ -631,7 +640,7 @@ const buildUpstreamDispatcher = (upstreamProxy) => {
|
|
|
631
640
|
uri: upstreamProxy
|
|
632
641
|
});
|
|
633
642
|
};
|
|
634
|
-
let upstreamDispatcher
|
|
643
|
+
let upstreamDispatcher;
|
|
635
644
|
let configuredUpstreamProxy;
|
|
636
645
|
/**
|
|
637
646
|
* The proxy URLs a dispatcher for this upstreamProxy can send through: none when it is empty, the
|
|
@@ -708,7 +717,11 @@ const configureUpstreamDispatcher = (upstreamProxy) => {
|
|
|
708
717
|
for (const form of proxyUrls.flatMap((proxyUrl) => credentialPairForms(proxyUrl))) if (form.length >= minimumRegisteredLength) registerLogSecret(form);
|
|
709
718
|
const forms = [...new Set(proxyUrls.flatMap((proxyUrl) => credentialForms(proxyUrl)))].sort((left, right) => right.length - left.length);
|
|
710
719
|
activeCredentialPattern = forms.length === 0 ? void 0 : new RegExp(forms.map((form) => escapeRegExp(form)).join("|"), "g");
|
|
711
|
-
previous
|
|
720
|
+
previous?.close().catch(() => void 0);
|
|
721
|
+
};
|
|
722
|
+
const getUpstreamDispatcher = () => {
|
|
723
|
+
upstreamDispatcher ??= buildUpstreamDispatcher(void 0);
|
|
724
|
+
return upstreamDispatcher;
|
|
712
725
|
};
|
|
713
726
|
const proxyVariables = [
|
|
714
727
|
"HTTPS_PROXY",
|
|
@@ -787,9 +800,9 @@ const sanitizeTransportError = (error, seen = /* @__PURE__ */ new Set()) => {
|
|
|
787
800
|
* uses.
|
|
788
801
|
*/
|
|
789
802
|
const fetchUpstream = async (url, init) => {
|
|
790
|
-
const response = await fetch(url, {
|
|
803
|
+
const response = await loadUndici().fetch(url, {
|
|
791
804
|
...init,
|
|
792
|
-
dispatcher:
|
|
805
|
+
dispatcher: getUpstreamDispatcher()
|
|
793
806
|
}).catch((error) => {
|
|
794
807
|
sanitizeTransportError(error);
|
|
795
808
|
noteConnectionFailure(init.signal);
|
|
@@ -934,8 +947,11 @@ const pollAccessToken = async (deviceCode) => {
|
|
|
934
947
|
await sleep$1(sleepDuration);
|
|
935
948
|
}
|
|
936
949
|
};
|
|
937
|
-
const getGitHubUser = async (githubToken, vsCodeVersion) => {
|
|
938
|
-
const response = await fetchUpstream(`${githubApiBaseUrl}/user`, {
|
|
950
|
+
const getGitHubUser = async (githubToken, vsCodeVersion, signal) => {
|
|
951
|
+
const response = await fetchUpstream(`${githubApiBaseUrl}/user`, {
|
|
952
|
+
headers: githubHeaders(githubToken, vsCodeVersion),
|
|
953
|
+
signal
|
|
954
|
+
});
|
|
939
955
|
if (!response.ok) throw new HTTPError("Failed to get GitHub user", response);
|
|
940
956
|
return await response.json();
|
|
941
957
|
};
|
|
@@ -970,11 +986,11 @@ const ensureGitHubToken = async (options = {}) => {
|
|
|
970
986
|
log.info(`GitHub token synced to ${paths.githubTokenPath}`);
|
|
971
987
|
return githubToken;
|
|
972
988
|
};
|
|
973
|
-
const loadGitHubLogin = async (githubToken, vsCodeVersion) => {
|
|
989
|
+
const loadGitHubLogin = async (githubToken, vsCodeVersion, signal) => {
|
|
974
990
|
try {
|
|
975
|
-
return (await getGitHubUser(githubToken, vsCodeVersion)).login;
|
|
991
|
+
return (await getGitHubUser(githubToken, vsCodeVersion, signal)).login;
|
|
976
992
|
} catch (error) {
|
|
977
|
-
log.error("Could not load GitHub user:", error);
|
|
993
|
+
if (!signal?.aborted) log.error("Could not load GitHub user:", error);
|
|
978
994
|
return;
|
|
979
995
|
}
|
|
980
996
|
};
|
|
@@ -1039,7 +1055,7 @@ const setupProxyAuth = async (config, options = {}) => {
|
|
|
1039
1055
|
}
|
|
1040
1056
|
};
|
|
1041
1057
|
return {
|
|
1042
|
-
githubLogin:
|
|
1058
|
+
githubLogin: loadGitHubLogin(githubToken, config.vsCodeVersion, options.loginSignal),
|
|
1043
1059
|
source: "github-token"
|
|
1044
1060
|
};
|
|
1045
1061
|
};
|
|
@@ -1764,6 +1780,7 @@ const snapshotRoots = /* @__PURE__ */ new WeakMap();
|
|
|
1764
1780
|
const snapshotProxyConfig = (config) => {
|
|
1765
1781
|
const snapshot = {
|
|
1766
1782
|
...config,
|
|
1783
|
+
admittedAt: config.admittedAt ?? Date.now(),
|
|
1767
1784
|
get copilotToken() {
|
|
1768
1785
|
return config.copilotToken;
|
|
1769
1786
|
},
|
|
@@ -1803,8 +1820,8 @@ const auth = defineCommand({
|
|
|
1803
1820
|
configureUpstreamDispatcher(appConfig.upstreamProxy);
|
|
1804
1821
|
const config = readProxyConfig(appConfig);
|
|
1805
1822
|
config.copilotToken = void 0;
|
|
1806
|
-
const
|
|
1807
|
-
if (
|
|
1823
|
+
const githubLogin = await (await setupProxyAuth(config, { force: true })).githubLogin;
|
|
1824
|
+
if (githubLogin) log.info(`Logged in as ${githubLogin}`);
|
|
1808
1825
|
log.info("copilot-relay auth completed");
|
|
1809
1826
|
}
|
|
1810
1827
|
});
|
|
@@ -2367,7 +2384,11 @@ var OutcomeObserver = class {
|
|
|
2367
2384
|
else this.overflow = true;
|
|
2368
2385
|
return;
|
|
2369
2386
|
}
|
|
2370
|
-
for (
|
|
2387
|
+
for (let start$1 = 0; start$1 < text$1.length;) {
|
|
2388
|
+
const lineEnd = text$1.indexOf("\n", start$1);
|
|
2389
|
+
const end = lineEnd === -1 ? text$1.length : lineEnd + 1;
|
|
2390
|
+
const part = text$1.slice(start$1, end);
|
|
2391
|
+
start$1 = end;
|
|
2371
2392
|
if (this.pending.length + part.length <= 2 * 1024 * 1024 && !this.overflow) this.pending += part;
|
|
2372
2393
|
else this.overflow = true;
|
|
2373
2394
|
if (part.endsWith("\n")) {
|
|
@@ -2695,7 +2716,8 @@ var RequestTrace = class RequestTrace {
|
|
|
2695
2716
|
runtime: {
|
|
2696
2717
|
...policy,
|
|
2697
2718
|
models: catalog ? [...catalog.models.entries()] : void 0,
|
|
2698
|
-
catalogCurrent: catalog?.baseUrl === config.copilotBaseUrl
|
|
2719
|
+
catalogCurrent: catalog?.baseUrl === config.copilotBaseUrl,
|
|
2720
|
+
...catalog?.fetchedAt !== void 0 && { catalogAgeMs: Math.max(0, (config.admittedAt ?? Date.now()) - catalog.fetchedAt) }
|
|
2699
2721
|
}
|
|
2700
2722
|
};
|
|
2701
2723
|
const abort = () => {
|
|
@@ -3351,6 +3373,7 @@ const pinnedCatalogs = /* @__PURE__ */ new WeakMap();
|
|
|
3351
3373
|
const pinCopilotModelCatalog = (config) => {
|
|
3352
3374
|
pinnedCatalogs.set(config, config.modelCatalog);
|
|
3353
3375
|
};
|
|
3376
|
+
const missingModelRecheckMs = 6e4;
|
|
3354
3377
|
const pendingCatalogs = /* @__PURE__ */ new WeakMap();
|
|
3355
3378
|
const isRecord$11 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3356
3379
|
const isPositiveInteger = (value) => typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
@@ -3431,7 +3454,8 @@ async function loadCopilotModelCatalog(config) {
|
|
|
3431
3454
|
}
|
|
3432
3455
|
const catalog = {
|
|
3433
3456
|
baseUrl: provider.baseUrl,
|
|
3434
|
-
models: models$1
|
|
3457
|
+
models: models$1,
|
|
3458
|
+
fetchedAt: Date.now()
|
|
3435
3459
|
};
|
|
3436
3460
|
publishCopilotModelCatalog(config, catalog);
|
|
3437
3461
|
const state = getRuntimeState();
|
|
@@ -3452,10 +3476,14 @@ async function loadCopilotModelCatalog(config) {
|
|
|
3452
3476
|
async function ensureCopilotModelCatalog(config, model) {
|
|
3453
3477
|
if (pinnedCatalogs.has(config)) return;
|
|
3454
3478
|
if (!config.modelCatalog) return;
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
if (
|
|
3458
|
-
|
|
3479
|
+
const catalog = config.modelCatalog;
|
|
3480
|
+
if (catalog.baseUrl === config.copilotBaseUrl) {
|
|
3481
|
+
if (catalog.models.has(model)) return;
|
|
3482
|
+
const now = config.admittedAt ?? Date.now();
|
|
3483
|
+
if (catalog.fetchedAt !== void 0 && now - catalog.fetchedAt < missingModelRecheckMs) return;
|
|
3484
|
+
}
|
|
3485
|
+
await loadCopilotModelCatalog(config);
|
|
3486
|
+
if (config.modelCatalog.baseUrl !== config.copilotBaseUrl) throw new Error("Copilot base URL changed during model discovery; retry the request.");
|
|
3459
3487
|
}
|
|
3460
3488
|
async function boundModelOutputTokens(config, model, requested) {
|
|
3461
3489
|
await ensureCopilotModelCatalog(config, model);
|
|
@@ -4995,17 +5023,32 @@ const incompleteStreamError = () => {
|
|
|
4995
5023
|
headers: { "content-type": "application/json" }
|
|
4996
5024
|
}), message);
|
|
4997
5025
|
};
|
|
5026
|
+
const chunkEvent = (chunk) => {
|
|
5027
|
+
let data;
|
|
5028
|
+
return {
|
|
5029
|
+
chunk,
|
|
5030
|
+
get data() {
|
|
5031
|
+
data ??= JSON.stringify(chunk);
|
|
5032
|
+
return data;
|
|
5033
|
+
}
|
|
5034
|
+
};
|
|
5035
|
+
};
|
|
5036
|
+
/** True for the [DONE] marker. A chunk event answers without serializing its chunk. */
|
|
5037
|
+
const isChatStreamDone = (event) => !event.chunk && event.data === "[DONE]";
|
|
5038
|
+
/** True for an event with neither a chunk nor data, such as a comment or keep-alive. */
|
|
5039
|
+
const isEmptyChatStreamEvent = (event) => !event.chunk && !event.data;
|
|
5040
|
+
/** The chunk an event carries, parsed from its data when it came from elsewhere. */
|
|
5041
|
+
const readChatChunk = (event) => event.chunk ?? JSON.parse(event.data);
|
|
4998
5042
|
async function* normalizeChatCompletionStream(stream) {
|
|
4999
5043
|
let terminal;
|
|
5000
5044
|
let refusal = false;
|
|
5001
5045
|
let usage$1;
|
|
5002
5046
|
for await (const event of stream) {
|
|
5003
5047
|
if (event.event === "error") throw incompleteStreamError();
|
|
5004
|
-
if (
|
|
5005
|
-
if (event.data === "[DONE]") continue;
|
|
5048
|
+
if (isEmptyChatStreamEvent(event) || isChatStreamDone(event)) continue;
|
|
5006
5049
|
let chunk;
|
|
5007
5050
|
try {
|
|
5008
|
-
chunk =
|
|
5051
|
+
chunk = readChatChunk(event);
|
|
5009
5052
|
} catch {
|
|
5010
5053
|
throw incompleteStreamError();
|
|
5011
5054
|
}
|
|
@@ -5030,29 +5073,29 @@ async function* normalizeChatCompletionStream(stream) {
|
|
|
5030
5073
|
delta: {}
|
|
5031
5074
|
}]
|
|
5032
5075
|
};
|
|
5033
|
-
if (Object.keys(choice.delta).length > 0) yield
|
|
5076
|
+
if (Object.keys(choice.delta).length > 0) yield chunkEvent({
|
|
5034
5077
|
...chunk,
|
|
5035
5078
|
choices: [{
|
|
5036
5079
|
...choice,
|
|
5037
5080
|
finish_reason: null
|
|
5038
5081
|
}]
|
|
5039
|
-
})
|
|
5040
|
-
} else yield
|
|
5082
|
+
});
|
|
5083
|
+
} else yield chunkEvent(chunk);
|
|
5041
5084
|
}
|
|
5042
5085
|
if (!terminal) throw incompleteStreamError();
|
|
5043
|
-
yield
|
|
5086
|
+
yield chunkEvent({
|
|
5044
5087
|
...terminal,
|
|
5045
5088
|
...usage$1 && { usage: usage$1 }
|
|
5046
|
-
})
|
|
5089
|
+
});
|
|
5047
5090
|
yield { data: "[DONE]" };
|
|
5048
5091
|
}
|
|
5049
5092
|
async function collectChatCompletionStream(stream, signal, timeoutMs) {
|
|
5050
5093
|
const chunks = [];
|
|
5051
5094
|
try {
|
|
5052
5095
|
for await (const event of stream) {
|
|
5053
|
-
if (event
|
|
5054
|
-
if (
|
|
5055
|
-
const chunk =
|
|
5096
|
+
if (isChatStreamDone(event)) break;
|
|
5097
|
+
if (isEmptyChatStreamEvent(event)) continue;
|
|
5098
|
+
const chunk = readChatChunk(event);
|
|
5056
5099
|
if (!chunk || !Array.isArray(chunk.choices)) throw incompleteStreamError();
|
|
5057
5100
|
chunks.push(chunk);
|
|
5058
5101
|
}
|
|
@@ -5088,12 +5131,12 @@ const resolveWebSearchStreamDecision = async (stream, toolNameMapper, isWebSearc
|
|
|
5088
5131
|
break;
|
|
5089
5132
|
}
|
|
5090
5133
|
const raw = next.value;
|
|
5091
|
-
if (raw
|
|
5134
|
+
if (isChatStreamDone(raw)) {
|
|
5092
5135
|
done = true;
|
|
5093
5136
|
break;
|
|
5094
5137
|
}
|
|
5095
|
-
if (
|
|
5096
|
-
const chunk =
|
|
5138
|
+
if (isEmptyChatStreamEvent(raw)) continue;
|
|
5139
|
+
const chunk = readChatChunk(raw);
|
|
5097
5140
|
if (!chunk || !Array.isArray(chunk.choices)) throw new Error("Invalid upstream chat stream chunk.");
|
|
5098
5141
|
buffered.push(chunk);
|
|
5099
5142
|
if (chunkHasWebSearchCall(chunk, toolNameMapper, isWebSearchToolName)) {
|
|
@@ -5129,9 +5172,9 @@ const resolveWebSearchStreamDecision = async (stream, toolNameMapper, isWebSearc
|
|
|
5129
5172
|
streamedThinking
|
|
5130
5173
|
};
|
|
5131
5174
|
for await (const raw of rest) {
|
|
5132
|
-
if (raw
|
|
5133
|
-
if (
|
|
5134
|
-
const chunk =
|
|
5175
|
+
if (isChatStreamDone(raw)) break;
|
|
5176
|
+
if (isEmptyChatStreamEvent(raw)) continue;
|
|
5177
|
+
const chunk = readChatChunk(raw);
|
|
5135
5178
|
if (!chunk || !Array.isArray(chunk.choices)) throw new Error("Invalid upstream chat stream chunk.");
|
|
5136
5179
|
buffered.push(chunk);
|
|
5137
5180
|
}
|
|
@@ -6848,9 +6891,9 @@ const handleClaudeMessageRequest = async (config, claudePayload, initiator, requ
|
|
|
6848
6891
|
for (const chunk of bufferedChunks) await writeClaudeStreamEvents(translateChunkToClaudeEvents(chunk, streamState, toolNameMapper), writeEvent);
|
|
6849
6892
|
const upstreamStream = remainingStream ?? effectiveResponse;
|
|
6850
6893
|
for await (const rawEvent of upstreamStream) {
|
|
6851
|
-
if (rawEvent
|
|
6852
|
-
if (
|
|
6853
|
-
await writeClaudeStreamEvents(translateChunkToClaudeEvents(
|
|
6894
|
+
if (isChatStreamDone(rawEvent)) break;
|
|
6895
|
+
if (isEmptyChatStreamEvent(rawEvent)) continue;
|
|
6896
|
+
await writeClaudeStreamEvents(translateChunkToClaudeEvents(readChatChunk(rawEvent), streamState, toolNameMapper), writeEvent);
|
|
6854
6897
|
}
|
|
6855
6898
|
};
|
|
6856
6899
|
claudeRoutes.get("/models", (c) => c.json({
|
|
@@ -7000,7 +7043,8 @@ claudeRoutes.post("/messages/count_tokens", async (c) => {
|
|
|
7000
7043
|
//#endregion
|
|
7001
7044
|
//#region src/codex/item-ids.ts
|
|
7002
7045
|
const isRecord$7 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7003
|
-
const frameEnd = /\r?\n\r?\n
|
|
7046
|
+
const frameEnd = /\r?\n\r?\n/g;
|
|
7047
|
+
const frameEndOverlap = 3;
|
|
7004
7048
|
/**
|
|
7005
7049
|
* Rewrites SSE frames as they stream so that every event about one output item names it by the same
|
|
7006
7050
|
* id. A frame that needs no change is passed on byte for byte, except that a UTF-8 byte order mark at
|
|
@@ -7012,23 +7056,29 @@ var StableItemIds = class {
|
|
|
7012
7056
|
encoder = new TextEncoder();
|
|
7013
7057
|
ids = /* @__PURE__ */ new Map();
|
|
7014
7058
|
pending = "";
|
|
7059
|
+
searched = 0;
|
|
7015
7060
|
/** The bytes of every frame this chunk completes; a partial frame waits for the next chunk. */
|
|
7016
7061
|
push(chunk) {
|
|
7017
7062
|
this.pending += this.decoder.decode(chunk, { stream: true });
|
|
7018
7063
|
let output = "";
|
|
7064
|
+
let frameStart = 0;
|
|
7065
|
+
frameEnd.lastIndex = Math.max(0, this.searched - frameEndOverlap);
|
|
7019
7066
|
let match = frameEnd.exec(this.pending);
|
|
7020
7067
|
while (match) {
|
|
7021
7068
|
const end = match.index + match[0].length;
|
|
7022
|
-
output += this.rewriteFrame(this.pending.slice(
|
|
7023
|
-
|
|
7069
|
+
output += this.rewriteFrame(this.pending.slice(frameStart, end));
|
|
7070
|
+
frameStart = end;
|
|
7024
7071
|
match = frameEnd.exec(this.pending);
|
|
7025
7072
|
}
|
|
7073
|
+
this.pending = this.pending.slice(frameStart);
|
|
7074
|
+
this.searched = this.pending.length;
|
|
7026
7075
|
return this.encoder.encode(output);
|
|
7027
7076
|
}
|
|
7028
7077
|
/** Whatever is left when the stream ends, unchanged: an incomplete frame has no event to fix. */
|
|
7029
7078
|
flush() {
|
|
7030
7079
|
const rest = this.pending + this.decoder.decode();
|
|
7031
7080
|
this.pending = "";
|
|
7081
|
+
this.searched = 0;
|
|
7032
7082
|
return this.encoder.encode(rest);
|
|
7033
7083
|
}
|
|
7034
7084
|
rewriteFrame(frame) {
|
|
@@ -9025,6 +9075,7 @@ const validateManifest = (value) => {
|
|
|
9025
9075
|
requireValid(runtime.thinkEffort === void 0 || isConfiguredReasoningEffort(runtime.thinkEffort));
|
|
9026
9076
|
if (runtime.modelRouting !== void 0) requireValid(record(runtime.modelRouting) && text(runtime.modelRouting.gptModel) && text(runtime.modelRouting.opusModel));
|
|
9027
9077
|
requireValid(runtime.catalogCurrent === void 0 || typeof runtime.catalogCurrent === "boolean");
|
|
9078
|
+
requireValid(runtime.catalogAgeMs === void 0 || typeof runtime.catalogAgeMs === "number" && Number.isSafeInteger(runtime.catalogAgeMs) && runtime.catalogAgeMs >= 0);
|
|
9028
9079
|
if (runtime.models !== void 0) {
|
|
9029
9080
|
requireValid(Array.isArray(runtime.models) && runtime.models.length <= 1e4);
|
|
9030
9081
|
const ids = /* @__PURE__ */ new Set();
|
|
@@ -9309,15 +9360,18 @@ const replayCapture = async (target) => {
|
|
|
9309
9360
|
requireValid(bytes.length === body.bytes);
|
|
9310
9361
|
files.set(body.file, bytes);
|
|
9311
9362
|
}
|
|
9363
|
+
const admittedAt = Date.now();
|
|
9312
9364
|
const catalog = manifest.runtime.models === void 0 ? void 0 : {
|
|
9313
9365
|
baseUrl: manifest.runtime.catalogCurrent ? "https://replay.invalid" : "https://old.invalid",
|
|
9314
|
-
models: new Map(manifest.runtime.models)
|
|
9366
|
+
models: new Map(manifest.runtime.models),
|
|
9367
|
+
...typeof manifest.runtime.catalogAgeMs === "number" && { fetchedAt: admittedAt - manifest.runtime.catalogAgeMs }
|
|
9315
9368
|
};
|
|
9316
9369
|
const config = {
|
|
9317
9370
|
host: "localhost",
|
|
9318
9371
|
port: 0,
|
|
9319
9372
|
copilotBaseUrl: "https://replay.invalid",
|
|
9320
9373
|
copilotToken: "offline-replay-token",
|
|
9374
|
+
admittedAt,
|
|
9321
9375
|
upstreamTimeoutMs: manifest.config.upstreamTimeoutMs,
|
|
9322
9376
|
vsCodeVersion: manifest.config.vsCodeVersion,
|
|
9323
9377
|
webSearchBackend: manifest.config.webSearchBackend,
|
|
@@ -10020,7 +10074,15 @@ const effortDescriptions = {
|
|
|
10020
10074
|
xhigh: "Extra high reasoning depth for complex problems",
|
|
10021
10075
|
max: "Maximum reasoning depth for the hardest problems"
|
|
10022
10076
|
};
|
|
10023
|
-
const
|
|
10077
|
+
const contextWindows = (model) => {
|
|
10078
|
+
const limits = model.capabilities?.limits;
|
|
10079
|
+
if (limits === void 0) return {};
|
|
10080
|
+
const roomBesideReply = limits.max_context_window_tokens - limits.max_output_tokens;
|
|
10081
|
+
return {
|
|
10082
|
+
context_window: roomBesideReply > 0 ? Math.min(limits.max_prompt_tokens, roomBesideReply) : limits.max_prompt_tokens,
|
|
10083
|
+
max_context_window: limits.max_prompt_tokens
|
|
10084
|
+
};
|
|
10085
|
+
};
|
|
10024
10086
|
const isRecord$2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10025
10087
|
const readVendorFile = async (name) => {
|
|
10026
10088
|
let currentDir = dirname(fileURLToPath(import.meta.url));
|
|
@@ -10056,7 +10118,6 @@ const generatedEntry = (model, priority, prompt) => {
|
|
|
10056
10118
|
description: effortDescriptions[effort]
|
|
10057
10119
|
}));
|
|
10058
10120
|
const defaultLevel = levels.find((level) => level.effort === "medium") ?? levels[0];
|
|
10059
|
-
const maxPrompt = model.capabilities?.limits?.max_prompt_tokens;
|
|
10060
10121
|
return {
|
|
10061
10122
|
slug: model.id,
|
|
10062
10123
|
display_name: displayName(model),
|
|
@@ -10076,10 +10137,7 @@ const generatedEntry = (model, priority, prompt) => {
|
|
|
10076
10137
|
apply_patch_tool_type: "freeform",
|
|
10077
10138
|
supports_parallel_tool_calls: true,
|
|
10078
10139
|
input_modalities: ["text", "image"],
|
|
10079
|
-
...
|
|
10080
|
-
context_window: Math.min(codexContextWindow, maxPrompt),
|
|
10081
|
-
max_context_window: maxPrompt
|
|
10082
|
-
},
|
|
10140
|
+
...contextWindows(model),
|
|
10083
10141
|
model_messages: { instructions_template: prompt }
|
|
10084
10142
|
};
|
|
10085
10143
|
};
|
|
@@ -10087,8 +10145,8 @@ const generatedEntry = (model, priority, prompt) => {
|
|
|
10087
10145
|
* The catalog for these models, in order: the first is Codex's default.
|
|
10088
10146
|
*
|
|
10089
10147
|
* A model Codex ships keeps Codex's own entry, so its tuned instructions and settings are unchanged;
|
|
10090
|
-
* only its position
|
|
10091
|
-
* entry.
|
|
10148
|
+
* only its position, visibility and context window change, the window to the one Copilot allows. Any
|
|
10149
|
+
* other model, such as a Claude model, gets a generated entry.
|
|
10092
10150
|
*/
|
|
10093
10151
|
const buildCodexCatalog = (models$1, vendor) => {
|
|
10094
10152
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -10101,7 +10159,8 @@ const buildCodexCatalog = (models$1, vendor) => {
|
|
|
10101
10159
|
entries.push(bundled ? {
|
|
10102
10160
|
...bundled,
|
|
10103
10161
|
visibility: "list",
|
|
10104
|
-
priority
|
|
10162
|
+
priority,
|
|
10163
|
+
...contextWindows(model)
|
|
10105
10164
|
} : generatedEntry(model, priority, vendor.prompt));
|
|
10106
10165
|
}
|
|
10107
10166
|
return { models: entries };
|
|
@@ -10504,7 +10563,8 @@ async function startRelay(appConfig) {
|
|
|
10504
10563
|
if (listenerWarning) log.error(listenerWarning);
|
|
10505
10564
|
const configWarning = exposedConfigWarning(appConfig);
|
|
10506
10565
|
if (configWarning) log.error(configWarning);
|
|
10507
|
-
const
|
|
10566
|
+
const loginLookup = new AbortController();
|
|
10567
|
+
const authSession = await setupProxyAuth(config, { loginSignal: loginLookup.signal });
|
|
10508
10568
|
try {
|
|
10509
10569
|
await validateUpstream(config, appConfig.thinkEffort);
|
|
10510
10570
|
} catch (error) {
|
|
@@ -10521,8 +10581,10 @@ async function startRelay(appConfig) {
|
|
|
10521
10581
|
const server = await startServer(config);
|
|
10522
10582
|
await writeRelayPidFile(config);
|
|
10523
10583
|
log.info(`copilot-relay version: ${appVersion}`);
|
|
10524
|
-
|
|
10525
|
-
|
|
10584
|
+
authSession.githubLogin.then((githubLogin) => {
|
|
10585
|
+
if (githubLogin) log.info(`GitHub user: ${githubLogin}`);
|
|
10586
|
+
else log.error("GitHub user: unavailable");
|
|
10587
|
+
});
|
|
10526
10588
|
const baseUrl = getRelayBaseUrl(config.host, config.port);
|
|
10527
10589
|
log.info(`copilot-relay listening on ${baseUrl}`);
|
|
10528
10590
|
log.info(`copilot base url: ${formatUrlForDisplay(config.copilotBaseUrl)}`);
|
|
@@ -10574,6 +10636,7 @@ async function startRelay(appConfig) {
|
|
|
10574
10636
|
const shutdownGraceMs = 2e3;
|
|
10575
10637
|
const shutdown = (signal) => {
|
|
10576
10638
|
log.info(`Received ${signal}; shutting down copilot-relay`);
|
|
10639
|
+
loginLookup.abort();
|
|
10577
10640
|
server.close((error) => {
|
|
10578
10641
|
if (error) {
|
|
10579
10642
|
log.error("Error while shutting down copilot-relay:", error);
|
|
@@ -10675,7 +10738,7 @@ const routesToTier = (name, tier) => name.toLowerCase().includes("opus") === (ti
|
|
|
10675
10738
|
* claude-sonnet-relay. Each tier therefore has its own name. The relay sends every model name without "opus"
|
|
10676
10739
|
* to gptModel and every one with it to opusModel, so the route reaches the same upstream model. The
|
|
10677
10740
|
* route names no real Claude model, so the app and its Claude Code take none of that model's
|
|
10678
|
-
* built-in context window or tuning; supports1m on the entry
|
|
10741
|
+
* built-in context window or tuning; supports1m and prefer1m on the entry say what the window is.
|
|
10679
10742
|
*/
|
|
10680
10743
|
const appModelName = (model) => looksAnthropic(model.id) && routesToTier(model.id, model.tier) ? model.id : `claude-${model.tier}-relay`;
|
|
10681
10744
|
const inferenceModel = (model) => {
|
|
@@ -10685,7 +10748,10 @@ const inferenceModel = (model) => {
|
|
|
10685
10748
|
return {
|
|
10686
10749
|
name,
|
|
10687
10750
|
...label ? { labelOverride: label } : {},
|
|
10688
|
-
...promptTokens >= 1e6 ? {
|
|
10751
|
+
...promptTokens >= 1e6 ? {
|
|
10752
|
+
supports1m: true,
|
|
10753
|
+
prefer1m: true
|
|
10754
|
+
} : {},
|
|
10689
10755
|
anthropicFamilyTier: model.tier
|
|
10690
10756
|
};
|
|
10691
10757
|
};
|
|
@@ -11119,8 +11185,9 @@ let relayAgent;
|
|
|
11119
11185
|
* An object, so a test can replace fetch.
|
|
11120
11186
|
*/
|
|
11121
11187
|
const relayListener = { fetch: (url, init) => {
|
|
11122
|
-
|
|
11123
|
-
|
|
11188
|
+
const undici$1 = loadUndici();
|
|
11189
|
+
relayAgent ??= new undici$1.Agent();
|
|
11190
|
+
return undici$1.fetch(url, {
|
|
11124
11191
|
...init,
|
|
11125
11192
|
dispatcher: relayAgent
|
|
11126
11193
|
});
|