copilot-relay 0.6.1 → 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 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 = buildUpstreamDispatcher(void 0);
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.close().catch(() => void 0);
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: upstreamDispatcher
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`, { headers: githubHeaders(githubToken, vsCodeVersion) });
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: await loadGitHubLogin(githubToken, config.vsCodeVersion),
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 authSession = await setupProxyAuth(config, { force: true });
1807
- if (authSession.githubLogin) log.info(`Logged in as ${authSession.githubLogin}`);
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 (const part of text$1.split(/(?<=\n)/)) {
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
- if (config.modelCatalog.baseUrl !== config.copilotBaseUrl || !config.modelCatalog.models.has(model)) {
3456
- await loadCopilotModelCatalog(config);
3457
- if (config.modelCatalog.baseUrl !== config.copilotBaseUrl) throw new Error("Copilot base URL changed during model discovery; retry the request.");
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 (!event.data) continue;
5005
- if (event.data === "[DONE]") continue;
5048
+ if (isEmptyChatStreamEvent(event) || isChatStreamDone(event)) continue;
5006
5049
  let chunk;
5007
5050
  try {
5008
- chunk = JSON.parse(event.data);
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 { data: JSON.stringify({
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 { data: JSON.stringify(chunk) };
5082
+ });
5083
+ } else yield chunkEvent(chunk);
5041
5084
  }
5042
5085
  if (!terminal) throw incompleteStreamError();
5043
- yield { data: JSON.stringify({
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.data === "[DONE]") break;
5054
- if (!event.data) continue;
5055
- const chunk = JSON.parse(event.data);
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.data === "[DONE]") {
5134
+ if (isChatStreamDone(raw)) {
5092
5135
  done = true;
5093
5136
  break;
5094
5137
  }
5095
- if (!raw.data) continue;
5096
- const chunk = JSON.parse(raw.data);
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.data === "[DONE]") break;
5133
- if (!raw.data) continue;
5134
- const chunk = JSON.parse(raw.data);
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.data === "[DONE]") break;
6852
- if (!rawEvent.data) continue;
6853
- await writeClaudeStreamEvents(translateChunkToClaudeEvents(JSON.parse(rawEvent.data), streamState, toolNameMapper), writeEvent);
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(0, end));
7023
- this.pending = this.pending.slice(end);
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,
@@ -10509,7 +10563,8 @@ async function startRelay(appConfig) {
10509
10563
  if (listenerWarning) log.error(listenerWarning);
10510
10564
  const configWarning = exposedConfigWarning(appConfig);
10511
10565
  if (configWarning) log.error(configWarning);
10512
- const authSession = await setupProxyAuth(config);
10566
+ const loginLookup = new AbortController();
10567
+ const authSession = await setupProxyAuth(config, { loginSignal: loginLookup.signal });
10513
10568
  try {
10514
10569
  await validateUpstream(config, appConfig.thinkEffort);
10515
10570
  } catch (error) {
@@ -10526,8 +10581,10 @@ async function startRelay(appConfig) {
10526
10581
  const server = await startServer(config);
10527
10582
  await writeRelayPidFile(config);
10528
10583
  log.info(`copilot-relay version: ${appVersion}`);
10529
- if (authSession.githubLogin) log.info(`GitHub user: ${authSession.githubLogin}`);
10530
- else log.error("GitHub user: unavailable");
10584
+ authSession.githubLogin.then((githubLogin) => {
10585
+ if (githubLogin) log.info(`GitHub user: ${githubLogin}`);
10586
+ else log.error("GitHub user: unavailable");
10587
+ });
10531
10588
  const baseUrl = getRelayBaseUrl(config.host, config.port);
10532
10589
  log.info(`copilot-relay listening on ${baseUrl}`);
10533
10590
  log.info(`copilot base url: ${formatUrlForDisplay(config.copilotBaseUrl)}`);
@@ -10579,6 +10636,7 @@ async function startRelay(appConfig) {
10579
10636
  const shutdownGraceMs = 2e3;
10580
10637
  const shutdown = (signal) => {
10581
10638
  log.info(`Received ${signal}; shutting down copilot-relay`);
10639
+ loginLookup.abort();
10582
10640
  server.close((error) => {
10583
10641
  if (error) {
10584
10642
  log.error("Error while shutting down copilot-relay:", error);
@@ -11127,8 +11185,9 @@ let relayAgent;
11127
11185
  * An object, so a test can replace fetch.
11128
11186
  */
11129
11187
  const relayListener = { fetch: (url, init) => {
11130
- relayAgent ??= new Agent();
11131
- return fetch(url, {
11188
+ const undici$1 = loadUndici();
11189
+ relayAgent ??= new undici$1.Agent();
11190
+ return undici$1.fetch(url, {
11132
11191
  ...init,
11133
11192
  dispatcher: relayAgent
11134
11193
  });