github-router 0.3.269 → 0.3.270

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.
Files changed (37) hide show
  1. package/dist/{attribution-settings-B1txv_Lk.js → attribution-settings-of5gJM5a.js} +3 -3
  2. package/dist/{attribution-settings-B1txv_Lk.js.map → attribution-settings-of5gJM5a.js.map} +1 -1
  3. package/dist/{auth-r-my7efo.js → auth-DIFbHOu0.js} +2 -2
  4. package/dist/{auth-r-my7efo.js.map → auth-DIFbHOu0.js.map} +1 -1
  5. package/dist/browser-ext/manifest.json +1 -1
  6. package/dist/{check-usage-CZr2ZkG0.js → check-usage-DfnrR-d_.js} +3 -3
  7. package/dist/{check-usage-CZr2ZkG0.js.map → check-usage-DfnrR-d_.js.map} +1 -1
  8. package/dist/{claude-BjNY0J6e.js → claude-BlzCUbVq.js} +7 -7
  9. package/dist/{claude-BjNY0J6e.js.map → claude-BlzCUbVq.js.map} +1 -1
  10. package/dist/{codex-CoRWpkHL.js → codex-BaFcXOyg.js} +5 -5
  11. package/dist/{codex-CoRWpkHL.js.map → codex-BaFcXOyg.js.map} +1 -1
  12. package/dist/engine-mwIgHJoi.js +2 -0
  13. package/dist/{gate-discovery-D-7oua8b.js → gate-discovery-AUAjqSpV.js} +2 -2
  14. package/dist/{gate-discovery-D-7oua8b.js.map → gate-discovery-AUAjqSpV.js.map} +1 -1
  15. package/dist/{get-copilot-usage-CFEpeA7E.js → get-copilot-usage-Ds09xSmH.js} +2 -2
  16. package/dist/{get-copilot-usage-CFEpeA7E.js.map → get-copilot-usage-Ds09xSmH.js.map} +1 -1
  17. package/dist/{internal-stop-hook-Db5o9RcZ.js → internal-stop-hook-B1MIwmTR.js} +2 -2
  18. package/dist/{internal-stop-hook-Db5o9RcZ.js.map → internal-stop-hook-B1MIwmTR.js.map} +1 -1
  19. package/dist/main.js +8 -8
  20. package/dist/{models-CJLt4XjP.js → models-BFQUp0ov.js} +2 -2
  21. package/dist/{models-CJLt4XjP.js.map → models-BFQUp0ov.js.map} +1 -1
  22. package/dist/{peer-mcp-personas-DSbBlgFt.js → peer-mcp-personas-pWP3Eyt7.js} +152 -84
  23. package/dist/{peer-mcp-personas-DSbBlgFt.js.map → peer-mcp-personas-pWP3Eyt7.js.map} +1 -1
  24. package/dist/{provision-CA-xwnFu.js → provision-CsYUVdMT.js} +2 -2
  25. package/dist/{provision-CA-xwnFu.js.map → provision-CsYUVdMT.js.map} +1 -1
  26. package/dist/{serve-DHny4ewF.js → serve--c9n9hk4.js} +6 -6
  27. package/dist/{serve-DHny4ewF.js.map → serve--c9n9hk4.js.map} +1 -1
  28. package/dist/{server-setup-Dd293T1O.js → server-setup-BmyWS4WU.js} +393 -99
  29. package/dist/server-setup-BmyWS4WU.js.map +1 -0
  30. package/dist/{start-P2rmHBkL.js → start-CXnLBouJ.js} +3 -3
  31. package/dist/{start-P2rmHBkL.js.map → start-CXnLBouJ.js.map} +1 -1
  32. package/dist/{token-C_q95GF7.js → token-iaSGp76k.js} +303 -143
  33. package/dist/token-iaSGp76k.js.map +1 -0
  34. package/package.json +1 -1
  35. package/dist/engine-DpVC7HRL.js +0 -2
  36. package/dist/server-setup-Dd293T1O.js.map +0 -1
  37. package/dist/token-C_q95GF7.js.map +0 -1
@@ -1,15 +1,15 @@
1
- import { A as searchWeb, B as readIteratorWithTimeout, Dt as toolbeltPathOverride, F as isAdvisorRequested, H as handleMcpDelete, I as buildAnthropicErrorEvent, It as withOneMSuffix, L as buildOpenAIErrorEvent, Lt as withInstallLock, M as ADVISOR_TOOL_INSTRUCTIONS, Mt as UPSTREAM_FETCH_TIMEOUT_MS, N as buildAdvisorStream, Nt as UPSTREAM_INACTIVITY_TIMEOUT_MS, P as injectAdvisorTool, Pt as generateRandomPort, R as isControllerClosedError, U as handleMcpPost, V as relayAnthropicStream, W as agentToolsEnabled, at as countTokens, ct as assembleResponsesPayload, dt as createResponses, ft as createChatCompletions, ht as parseJsonOrDiagnose, it as shimDefaultsToXhigh, j as ADVISOR_INTERNAL_TOOL_NAME, lt as resolveMcpToolTimeoutMs, mt as readResponseBodyCapped, ot as createMessages, pt as MAX_RESPONSE_BODY_BYTES, r as assertMcpToolSurfaceConsistent, st as getTokenCount, ut as pickEndpoint, w as toolbeltEnabled, z as logStreamError } from "./peer-mcp-personas-DSbBlgFt.js";
1
+ import { A as searchWeb, B as readIteratorWithTimeout, Dt as toolbeltPathOverride, F as isAdvisorRequested, H as handleMcpDelete, I as buildAnthropicErrorEvent, It as withOneMSuffix, L as buildOpenAIErrorEvent, Lt as withInstallLock, M as ADVISOR_TOOL_INSTRUCTIONS, Mt as UPSTREAM_FETCH_TIMEOUT_MS, N as buildAdvisorStream, Nt as UPSTREAM_INACTIVITY_TIMEOUT_MS, P as injectAdvisorTool, Pt as generateRandomPort, R as isControllerClosedError, U as handleMcpPost, V as relayAnthropicStream, W as agentToolsEnabled, at as countTokens, ct as assembleResponsesPayload, dt as createResponses, ft as createChatCompletions, ht as parseJsonOrDiagnose, it as shimDefaultsToXhigh, j as ADVISOR_INTERNAL_TOOL_NAME, lt as resolveMcpToolTimeoutMs, mt as readResponseBodyCapped, ot as createMessages, pt as MAX_RESPONSE_BODY_BYTES, r as assertMcpToolSurfaceConsistent, st as getTokenCount, ut as pickEndpoint, w as toolbeltEnabled, z as logStreamError } from "./peer-mcp-personas-pWP3Eyt7.js";
2
2
  import { t as getPackageVersion } from "./version-_Q1WpsQp.js";
3
3
  import { i as ensurePaths, t as PATHS } from "./paths-Bg1_DWhi.js";
4
- import { C as copilotHeaders, O as state, S as copilotBaseUrl, a as cacheCopilotVersion, c as filterBetaHeader, d as resolveModel, f as sleep, g as fetchWithTransientRetry, i as tryRefreshAndRetry, l as isNullish, n as setupGitHubAgentToken, o as cacheModels, r as setupGitHubToken, s as cacheVSCodeVersion, t as setupCopilotToken, v as HTTPError, y as forwardError } from "./token-C_q95GF7.js";
5
- import { t as getCopilotUsage } from "./get-copilot-usage-CFEpeA7E.js";
4
+ import { A as state, T as copilotHeaders, _ as forwardError, a as cacheCopilotVersion, c as filterBetaHeader, d as resolveModel, f as sleep, g as HTTPError, i as tryRefreshAndRetry, l as isNullish, n as setupGitHubAgentToken, o as cacheModels, r as setupGitHubToken, s as cacheVSCodeVersion, t as setupCopilotToken, w as copilotBaseUrl, y as fetchWithTransientRetry } from "./token-iaSGp76k.js";
5
+ import { t as getCopilotUsage } from "./get-copilot-usage-Ds09xSmH.js";
6
6
  import { a as resolveExecutable, n as killManagedTree, o as runCommandCapture, r as parseBoolEnv, s as runCommandVoid } from "./exec-y8C_MU8A.js";
7
7
  import consola from "consola";
8
8
  import * as fs$2 from "node:fs";
9
9
  import fs, { existsSync } from "node:fs";
10
10
  import * as nodePath from "node:path";
11
11
  import path from "node:path";
12
- import { randomUUID } from "node:crypto";
12
+ import { createHash, randomBytes, randomUUID } from "node:crypto";
13
13
  import fs$1 from "node:fs/promises";
14
14
  import os from "node:os";
15
15
  import process$1 from "node:process";
@@ -357,9 +357,12 @@ async function runSelfUpdate(opts) {
357
357
  //#region src/lib/file-log-reporter.ts
358
358
  const MAX_LOG_BYTES = 1048576;
359
359
  const DEDUP_MAX = 1e3;
360
+ const DEDUP_WINDOW_MS = 3e5;
360
361
  const ARG_MAX_LEN = 2048;
361
- const DEDUP_KEY_MAX_LEN = 200;
362
+ const ERROR_CAUSE_MAX_DEPTH = 4;
363
+ const ERROR_FIELD_MAX_LEN = 512;
362
364
  const CREDENTIAL_RE = /\b(eyJ[A-Za-z0-9_-]{20,}(?:\.[A-Za-z0-9_-]+){0,2}|gh[opsu]_[A-Za-z0-9_]{20,}|Bearer\s+\S{20,})\b/g;
365
+ const ERROR_BODY_RE = /\b(request\s+body|body)\s*[:=]\s*.*$/i;
363
366
  const ALLOWED_TYPES = /* @__PURE__ */ new Set([
364
367
  "fatal",
365
368
  "error",
@@ -368,25 +371,87 @@ const ALLOWED_TYPES = /* @__PURE__ */ new Set([
368
371
  function sanitize(line) {
369
372
  return line.replace(CREDENTIAL_RE, "[REDACTED]");
370
373
  }
374
+ function errorProperty(error, key) {
375
+ if (typeof error !== "object" && typeof error !== "function" || error === null) return;
376
+ try {
377
+ const value = error[key];
378
+ return typeof value === "string" ? value : void 0;
379
+ } catch {
380
+ return;
381
+ }
382
+ }
383
+ function errorCause(error) {
384
+ if (typeof error !== "object" && typeof error !== "function" || error === null) return;
385
+ try {
386
+ return error.cause;
387
+ } catch {
388
+ return;
389
+ }
390
+ }
391
+ function safeString(value) {
392
+ try {
393
+ return String(value);
394
+ } catch {
395
+ return "[unserializable]";
396
+ }
397
+ }
398
+ function boundErrorField(value) {
399
+ return value.length > ERROR_FIELD_MAX_LEN ? `${value.slice(0, ERROR_FIELD_MAX_LEN)}…` : value;
400
+ }
401
+ function sanitizeErrorMessage(value) {
402
+ return sanitize(value).replace(ERROR_BODY_RE, "$1=[REDACTED]");
403
+ }
404
+ function serializeError(error) {
405
+ const chain = [];
406
+ const seen = /* @__PURE__ */ new Set();
407
+ let current = error;
408
+ for (let depth = 0; current !== void 0 && depth <= ERROR_CAUSE_MAX_DEPTH; depth++) {
409
+ if ((typeof current === "object" || typeof current === "function") && current !== null) {
410
+ if (seen.has(current)) {
411
+ chain.push("[circular cause]");
412
+ break;
413
+ }
414
+ seen.add(current);
415
+ }
416
+ const name = boundErrorField(errorProperty(current, "name") ?? "Error");
417
+ const message = boundErrorField(sanitizeErrorMessage(errorProperty(current, "message") ?? (typeof current === "string" ? current : safeString(current))));
418
+ const rawCode = errorProperty(current, "code");
419
+ const code = rawCode ? boundErrorField(rawCode) : void 0;
420
+ chain.push(`${name}: ${message}${code ? ` code=${code}` : ""}`);
421
+ const cause = errorCause(current);
422
+ if (cause === void 0) break;
423
+ if (depth === ERROR_CAUSE_MAX_DEPTH) {
424
+ chain.push("[cause chain truncated]");
425
+ break;
426
+ }
427
+ current = cause;
428
+ }
429
+ return chain.join(" <- cause: ");
430
+ }
371
431
  function serializeArg(arg) {
372
432
  if (typeof arg === "string") return arg;
373
- if (arg instanceof Error) {
374
- const parts = [arg.message];
375
- if (arg.stack) parts.push(arg.stack);
376
- return parts.join("\n");
377
- }
378
- return String(arg);
433
+ if (arg instanceof Error) return serializeError(arg);
434
+ return safeString(arg);
435
+ }
436
+ function serializeArgs(logObj) {
437
+ return logObj.args.map((arg) => {
438
+ const serialized = sanitize(serializeArg(arg));
439
+ return serialized.length > ARG_MAX_LEN ? `${serialized.slice(0, ARG_MAX_LEN)}…` : serialized;
440
+ });
379
441
  }
380
- function formatLogLine(logObj) {
381
- return sanitize(`${logObj.date.toISOString()} [${(logObj.type ?? "error").toUpperCase()}] ${logObj.args.map((a) => {
382
- const s = serializeArg(a);
383
- return s.length > ARG_MAX_LEN ? s.slice(0, ARG_MAX_LEN) + "…" : s;
384
- }).join(" ").replace(/\r\n|\r|\n/g, "\\n")}\n`);
442
+ function formatLogLine(logObj, suppressed = 0) {
443
+ return `${logObj.date.toISOString()} [${(logObj.type ?? "error").toUpperCase()}] ${serializeArgs(logObj).join(" ").replace(/\r\n|\r|\n/g, "\\n")}${suppressed > 0 ? ` [${suppressed} identical occurrences suppressed]` : ""}\n`;
385
444
  }
386
445
  function makeDedupeKey(logObj) {
387
- const firstArg = logObj.args.length > 0 ? serializeArg(logObj.args[0]) : "";
388
- const key = `${logObj.type}:${firstArg}`;
389
- return key.length > DEDUP_KEY_MAX_LEN ? key.slice(0, DEDUP_KEY_MAX_LEN) : key;
446
+ const hash = createHash("sha256");
447
+ hash.update(logObj.type);
448
+ for (const arg of serializeArgs(logObj)) {
449
+ hash.update("\0");
450
+ hash.update(String(arg.length));
451
+ hash.update(":");
452
+ hash.update(arg);
453
+ }
454
+ return hash.digest("base64url");
390
455
  }
391
456
  /**
392
457
  * Construction-time rotation: rename the log aside if it's already over the
@@ -408,7 +473,7 @@ function rotateAtStartup(filePath) {
408
473
  }
409
474
  var FileLogReporter = class FileLogReporter {
410
475
  filePath;
411
- seen = /* @__PURE__ */ new Set();
476
+ seen = /* @__PURE__ */ new Map();
412
477
  bytesSinceCheck = 0;
413
478
  static ROTATE_CHECK_BYTES = MAX_LOG_BYTES / 2;
414
479
  fd;
@@ -466,10 +531,25 @@ var FileLogReporter = class FileLogReporter {
466
531
  log(logObj, _ctx) {
467
532
  if (!ALLOWED_TYPES.has(logObj.type)) return;
468
533
  const key = makeDedupeKey(logObj);
469
- if (this.seen.has(key)) return;
470
- if (this.seen.size >= DEDUP_MAX) this.seen.clear();
471
- this.seen.add(key);
472
- const line = formatLogLine(logObj);
534
+ const timestamp = logObj.date.getTime();
535
+ const previous = this.seen.get(key);
536
+ let suppressed = 0;
537
+ if (previous) {
538
+ if (timestamp - previous.lastWrittenAt < DEDUP_WINDOW_MS) {
539
+ previous.suppressed++;
540
+ return;
541
+ }
542
+ suppressed = previous.suppressed;
543
+ previous.lastWrittenAt = timestamp;
544
+ previous.suppressed = 0;
545
+ } else {
546
+ if (this.seen.size >= DEDUP_MAX) this.seen.clear();
547
+ this.seen.set(key, {
548
+ lastWrittenAt: timestamp,
549
+ suppressed: 0
550
+ });
551
+ }
552
+ const line = formatLogLine(logObj, suppressed);
473
553
  this.bytesSinceCheck += line.length;
474
554
  if (this.bytesSinceCheck >= FileLogReporter.ROTATE_CHECK_BYTES) {
475
555
  this.rotateIfNeeded();
@@ -1277,7 +1357,7 @@ function collectToolFieldKeys(body) {
1277
1357
  //#endregion
1278
1358
  //#region package.json
1279
1359
  var name = "github-router";
1280
- var version = "0.3.269";
1360
+ var version = "0.3.270";
1281
1361
  //#endregion
1282
1362
  //#region src/lib/approval.ts
1283
1363
  const awaitApproval = async () => {
@@ -1625,12 +1705,21 @@ mcpRoutes.delete("/:group", (c) => {
1625
1705
  * any other input shape, fall back to a synthesized `toolu_advisor_N`
1626
1706
  * id.
1627
1707
  */
1628
- function toCopilotToolUseId(srvId, fallbackIndex) {
1708
+ function toCopilotToolUseId(srvId) {
1629
1709
  if (srvId.startsWith("srvtoolu_")) {
1630
1710
  const suffix = srvId.slice(9);
1631
1711
  if (/^[a-zA-Z0-9_]+$/.test(suffix)) return `toolu_${suffix}`;
1632
1712
  }
1633
- return `toolu_advisor_${fallbackIndex}`;
1713
+ if (/^toolu_[a-zA-Z0-9_]+$/.test(srvId)) return srvId;
1714
+ }
1715
+ function invalidAdvisorHistory(message) {
1716
+ throw new HTTPError("Invalid advisor history", Response.json({
1717
+ type: "error",
1718
+ error: {
1719
+ type: "invalid_request_error",
1720
+ message: `Invalid advisor history: ${message}`
1721
+ }
1722
+ }, { status: 400 }));
1634
1723
  }
1635
1724
  /**
1636
1725
  * Fast-path detector: returns true if the raw body has any chance of
@@ -1645,85 +1734,148 @@ function bodyMightNeedSanitize(rawBody) {
1645
1734
  return rawBody.includes("\"server_tool_use\"") || rawBody.includes("\"advisor_tool_result\"") || /"type":"advisor_\d+"/.test(rawBody);
1646
1735
  }
1647
1736
  /**
1648
- * Translate one assistant turn's content array, splitting at advisor
1649
- * pairs into the multi-message structure Copilot accepts.
1650
- *
1651
- * Input shape (Claude Code stores everything in one assistant turn):
1652
- * [text*, server_tool_use{advisor}, advisor_tool_result, text*, ...]
1653
- *
1654
- * Output: array of {role, content[]} message objects, alternating
1655
- * assistant→user→assistant for each advisor pair encountered.
1737
+ * Translate one client-visible assistant turn into the internal messages
1738
+ * Copilot originally saw. Parallel advisor calls are replayed as one assistant
1739
+ * turn followed by one user turn containing every matching result. Sequential
1740
+ * advisor groups remain separate assistant/user pairs. When a client tool and
1741
+ * advisor coexist, the advisor results and the client's following tool results
1742
+ * are merged into the one immediate user message required by Anthropic.
1656
1743
  */
1657
- function splitAssistantTurnAtAdvisorPairs(originalContent, syntheticIndexRef) {
1744
+ function splitAssistantTurnAtAdvisorPairs(originalMessage, originalContent, followingMessage) {
1658
1745
  const messages = [];
1659
1746
  let currentAssistantContent = [];
1660
1747
  let translated = false;
1748
+ let emittedFirstAssistant = false;
1749
+ const pushAssistant = () => {
1750
+ if (currentAssistantContent.length === 0) invalidAdvisorHistory("advisor pair has no assistant content");
1751
+ messages.push(emittedFirstAssistant ? {
1752
+ role: "assistant",
1753
+ content: currentAssistantContent
1754
+ } : {
1755
+ ...originalMessage,
1756
+ content: currentAssistantContent
1757
+ });
1758
+ emittedFirstAssistant = true;
1759
+ };
1661
1760
  let i = 0;
1662
1761
  while (i < originalContent.length) {
1663
1762
  const block = originalContent[i];
1664
1763
  const b = typeof block === "object" && block !== null ? block : null;
1665
- if (b && b.type === "server_tool_use" && b.name === "__anthropic_advisor".replace(/^__anthropic_/, "")) {
1666
- const stuId = typeof b.id === "string" ? b.id : "";
1667
- const nextBlock = originalContent[i + 1];
1668
- const next = typeof nextBlock === "object" && nextBlock !== null ? nextBlock : null;
1669
- const copilotId = stuId.startsWith("srvtoolu_") ? toCopilotToolUseId(stuId, syntheticIndexRef.value++) : stuId.startsWith("toolu_") && /^toolu_[a-zA-Z0-9_]+$/.test(stuId) ? stuId : `toolu_advisor_${syntheticIndexRef.value++}`;
1670
- currentAssistantContent.push({
1671
- type: "tool_use",
1672
- id: copilotId,
1673
- name: ADVISOR_INTERNAL_TOOL_NAME,
1674
- input: {}
1675
- });
1676
- messages.push({
1677
- role: "assistant",
1678
- content: currentAssistantContent
1679
- });
1764
+ if (b?.type === "server_tool_use" && b.name === "__anthropic_advisor".replace(/^__anthropic_/, "")) {
1765
+ const uses = [];
1766
+ const results = /* @__PURE__ */ new Map();
1767
+ let readingResults = false;
1768
+ while (i < originalContent.length) {
1769
+ const candidate = originalContent[i];
1770
+ const record = typeof candidate === "object" && candidate !== null ? candidate : null;
1771
+ if (record?.type === "server_tool_use" && record.name === "__anthropic_advisor".replace(/^__anthropic_/, "")) {
1772
+ if (readingResults) {
1773
+ if (results.size === uses.length) break;
1774
+ invalidAdvisorHistory("advisor blocks overlap or are ambiguously nested");
1775
+ }
1776
+ const serverId = typeof record.id === "string" ? record.id : "";
1777
+ const copilotId = toCopilotToolUseId(serverId);
1778
+ if (!copilotId) invalidAdvisorHistory("advisor server_tool_use id is not round-trippable");
1779
+ if (uses.some((use) => use.serverId === serverId)) invalidAdvisorHistory("duplicate advisor server_tool_use id");
1780
+ uses.push({
1781
+ serverId,
1782
+ copilotId
1783
+ });
1784
+ currentAssistantContent.push({
1785
+ type: "tool_use",
1786
+ id: copilotId,
1787
+ name: ADVISOR_INTERNAL_TOOL_NAME,
1788
+ input: {}
1789
+ });
1790
+ i++;
1791
+ continue;
1792
+ }
1793
+ if (record?.type === "advisor_tool_result") {
1794
+ readingResults = true;
1795
+ const serverId = typeof record.tool_use_id === "string" ? record.tool_use_id : "";
1796
+ if (!uses.some((use) => use.serverId === serverId)) invalidAdvisorHistory("advisor_tool_result has no matching advisor server_tool_use");
1797
+ if (results.has(serverId)) invalidAdvisorHistory("duplicate advisor_tool_result");
1798
+ const resultContent = record.content;
1799
+ let resultText;
1800
+ if (typeof resultContent === "string") resultText = resultContent;
1801
+ else if (typeof resultContent === "object" && resultContent !== null) {
1802
+ const text = resultContent.text;
1803
+ if (typeof text === "string") resultText = text;
1804
+ }
1805
+ if (resultText === void 0) invalidAdvisorHistory("advisor_tool_result has no replayable text");
1806
+ results.set(serverId, resultText);
1807
+ i++;
1808
+ continue;
1809
+ }
1810
+ if (readingResults) {
1811
+ if (results.size !== uses.length) invalidAdvisorHistory("advisor server_tool_use has no matching advisor_tool_result in the same assistant turn");
1812
+ break;
1813
+ }
1814
+ currentAssistantContent.push(candidate);
1815
+ i++;
1816
+ }
1817
+ if (results.size !== uses.length) invalidAdvisorHistory("advisor server_tool_use has no matching advisor_tool_result in the same assistant turn");
1818
+ const ordinaryToolIds = currentAssistantContent.filter((entry) => typeof entry === "object" && entry !== null && entry.type === "tool_use" && entry.name !== "__anthropic_advisor").map((entry) => entry.id);
1819
+ if (ordinaryToolIds.some((id) => typeof id !== "string" || id.length === 0)) invalidAdvisorHistory("client tool_use has no matchable id");
1820
+ pushAssistant();
1680
1821
  translated = true;
1681
- let resultText = "";
1682
- if (next && next.type === "advisor_tool_result") {
1683
- const c = next.content;
1684
- if (typeof c === "string") resultText = c;
1685
- else if (typeof c === "object" && c !== null) {
1686
- const txt = c.text;
1687
- if (typeof txt === "string") resultText = txt;
1822
+ const advisorResults = uses.map((use) => ({
1823
+ type: "tool_result",
1824
+ tool_use_id: use.copilotId,
1825
+ content: results.get(use.serverId)
1826
+ }));
1827
+ if (ordinaryToolIds.length > 0) {
1828
+ if (i < originalContent.length) invalidAdvisorHistory("assistant continuation followed an unresolved client tool in an advisor turn");
1829
+ if (typeof followingMessage !== "object" || followingMessage === null || followingMessage.role !== "user" || !Array.isArray(followingMessage.content)) invalidAdvisorHistory("mixed advisor/client-tool turn has no immediate user tool results");
1830
+ const following = followingMessage;
1831
+ const followingContent = following.content;
1832
+ let sawNonResult = false;
1833
+ const resultIds = [];
1834
+ for (const entry of followingContent) {
1835
+ const record = typeof entry === "object" && entry !== null ? entry : null;
1836
+ if (record?.type === "tool_result") {
1837
+ if (sawNonResult) invalidAdvisorHistory("tool_result appears after user text/content");
1838
+ if (typeof record.tool_use_id !== "string") invalidAdvisorHistory("tool_result has no matchable tool_use_id");
1839
+ resultIds.push(record.tool_use_id);
1840
+ } else sawNonResult = true;
1688
1841
  }
1689
- i += 2;
1690
- } else {
1691
- resultText = "[Advisor result missing in conversation history.]";
1692
- i += 1;
1842
+ const expected = [...ordinaryToolIds].sort();
1843
+ const actual = [...resultIds].sort();
1844
+ if (expected.length !== actual.length || expected.some((id, index) => id !== actual[index])) invalidAdvisorHistory("mixed advisor/client-tool turn does not have exactly one result for every client tool");
1845
+ messages.push({
1846
+ ...following,
1847
+ content: [...advisorResults, ...followingContent]
1848
+ });
1849
+ return {
1850
+ messages,
1851
+ translated: true,
1852
+ consumedFollowingMessage: true
1853
+ };
1693
1854
  }
1694
1855
  messages.push({
1695
1856
  role: "user",
1696
- content: [{
1697
- type: "tool_result",
1698
- tool_use_id: copilotId,
1699
- content: resultText
1700
- }]
1857
+ content: advisorResults
1701
1858
  });
1702
1859
  currentAssistantContent = [];
1703
1860
  continue;
1704
1861
  }
1705
- if (b && b.type === "advisor_tool_result") {
1706
- translated = true;
1707
- i += 1;
1708
- continue;
1709
- }
1862
+ if (b?.type === "advisor_tool_result") invalidAdvisorHistory("advisor_tool_result has no immediately preceding advisor server_tool_use");
1710
1863
  currentAssistantContent.push(block);
1711
- i += 1;
1864
+ i++;
1712
1865
  }
1713
1866
  if (currentAssistantContent.length > 0) messages.push({
1714
1867
  role: "assistant",
1715
1868
  content: currentAssistantContent
1716
1869
  });
1717
1870
  if (!translated) return {
1718
- messages: [{
1719
- role: "assistant",
1720
- content: originalContent
1721
- }],
1722
- translated: false
1871
+ messages: [originalMessage],
1872
+ translated: false,
1873
+ consumedFollowingMessage: false
1723
1874
  };
1724
1875
  return {
1725
1876
  messages,
1726
- translated: true
1877
+ translated: true,
1878
+ consumedFollowingMessage: false
1727
1879
  };
1728
1880
  }
1729
1881
  function sanitizeAnthropicBody(rawBody) {
@@ -1752,8 +1904,8 @@ function sanitizeAnthropicBody(rawBody) {
1752
1904
  const original = parsed.messages;
1753
1905
  const rebuilt = [];
1754
1906
  let anyTranslated = false;
1755
- const syntheticIndexRef = { value: 0 };
1756
- for (const msg of original) {
1907
+ for (let messageIndex = 0; messageIndex < original.length; messageIndex++) {
1908
+ const msg = original[messageIndex];
1757
1909
  if (typeof msg !== "object" || msg === null || msg.role !== "assistant") {
1758
1910
  rebuilt.push(msg);
1759
1911
  continue;
@@ -1772,10 +1924,11 @@ function sanitizeAnthropicBody(rawBody) {
1772
1924
  rebuilt.push(msg);
1773
1925
  continue;
1774
1926
  }
1775
- const { messages: split, translated } = splitAssistantTurnAtAdvisorPairs(content, syntheticIndexRef);
1927
+ const { messages: split, translated, consumedFollowingMessage } = splitAssistantTurnAtAdvisorPairs(msg, content, original[messageIndex + 1]);
1776
1928
  if (translated) {
1777
1929
  anyTranslated = true;
1778
1930
  for (const m of split) rebuilt.push(m);
1931
+ if (consumedFollowingMessage) messageIndex++;
1779
1932
  } else rebuilt.push(msg);
1780
1933
  }
1781
1934
  if (anyTranslated) {
@@ -3501,7 +3654,119 @@ async function handleNonClaudeChat(c, opts) {
3501
3654
  return c.json(anthropic, 200);
3502
3655
  }
3503
3656
  //#endregion
3657
+ //#region src/lib/thinking-history-repair.ts
3658
+ const THINKING_INTEGRITY_ERROR_RES = [/messages\.(\d+)\.content\.\d+:\s*`thinking` or `redacted_thinking` blocks in the latest assistant message cannot be modified/, /messages\.(\d+)\.content\.\d+:\s*Invalid `signature` in `thinking` block/];
3659
+ const MAX_REMEMBERED_REPAIRS = 1e3;
3660
+ const OMITTED_THINKING_PLACEHOLDER = "[prior reasoning omitted]";
3661
+ const repairSalt = randomBytes(32);
3662
+ const rememberedRepairs = /* @__PURE__ */ new Set();
3663
+ function isRecord(value) {
3664
+ return typeof value === "object" && value !== null;
3665
+ }
3666
+ function signedBlockFingerprint(_messageIndex, content) {
3667
+ const hash = createHash("sha256");
3668
+ hash.update(repairSalt);
3669
+ let signedBlocks = 0;
3670
+ for (let index = 0; index < content.length; index++) {
3671
+ const block = content[index];
3672
+ if (!isRecord(block)) continue;
3673
+ if (block.type !== "thinking" && block.type !== "redacted_thinking") continue;
3674
+ signedBlocks++;
3675
+ hash.update(String(index));
3676
+ hash.update(String(block.type));
3677
+ if (typeof block.signature === "string") hash.update(block.signature);
3678
+ if (typeof block.data === "string") hash.update(block.data);
3679
+ }
3680
+ return signedBlocks > 0 ? hash.digest("hex") : void 0;
3681
+ }
3682
+ function repairMessageAt(parsed, messageIndex) {
3683
+ if (!Array.isArray(parsed.messages)) return void 0;
3684
+ const messages = [...parsed.messages];
3685
+ const message = messages[messageIndex];
3686
+ if (!isRecord(message) || message.role !== "assistant") return void 0;
3687
+ if (!Array.isArray(message.content)) return void 0;
3688
+ const fingerprint = signedBlockFingerprint(messageIndex, message.content);
3689
+ if (!fingerprint) return void 0;
3690
+ const repairedContent = message.content.filter((block) => !isRecord(block) || block.type !== "thinking" && block.type !== "redacted_thinking");
3691
+ const removedBlocks = message.content.length - repairedContent.length;
3692
+ if (removedBlocks === 0) return void 0;
3693
+ if (repairedContent.length === 0) repairedContent.push({
3694
+ type: "text",
3695
+ text: OMITTED_THINKING_PLACEHOLDER
3696
+ });
3697
+ messages[messageIndex] = {
3698
+ ...message,
3699
+ content: repairedContent
3700
+ };
3701
+ parsed.messages = messages;
3702
+ return {
3703
+ body: JSON.stringify(parsed),
3704
+ fingerprint,
3705
+ messageIndex,
3706
+ removedBlocks
3707
+ };
3708
+ }
3709
+ /**
3710
+ * Build a one-shot repair only for Copilot's known signed-thinking integrity
3711
+ * rejections. The upstream error is the oracle: valid omitted-display thinking
3712
+ * also has an empty `thinking` string, so request shape alone cannot identify a
3713
+ * corrupt signature safely.
3714
+ */
3715
+ function repairRejectedThinkingHistory(rawBody, upstreamErrorText) {
3716
+ const match = THINKING_INTEGRITY_ERROR_RES.map((pattern) => pattern.exec(upstreamErrorText)).find((candidate) => candidate !== null);
3717
+ if (!match) return void 0;
3718
+ const messageIndex = Number(match[1]);
3719
+ if (!Number.isSafeInteger(messageIndex) || messageIndex < 0) return void 0;
3720
+ let parsed;
3721
+ try {
3722
+ parsed = JSON.parse(rawBody);
3723
+ } catch {
3724
+ return;
3725
+ }
3726
+ return repairMessageAt(parsed, messageIndex);
3727
+ }
3728
+ /**
3729
+ * Reapply a repair already proven successful in this process. Only salted
3730
+ * digests are retained; no message, thinking, signature, or tool content is
3731
+ * stored.
3732
+ */
3733
+ function repairKnownThinkingHistory(rawBody) {
3734
+ if (rememberedRepairs.size === 0) return void 0;
3735
+ let parsed;
3736
+ try {
3737
+ parsed = JSON.parse(rawBody);
3738
+ } catch {
3739
+ return;
3740
+ }
3741
+ if (!Array.isArray(parsed.messages)) return void 0;
3742
+ let repaired;
3743
+ let totalRemovedBlocks = 0;
3744
+ for (let index = parsed.messages.length - 1; index >= 0; index--) {
3745
+ const message = parsed.messages[index];
3746
+ if (!isRecord(message) || !Array.isArray(message.content)) continue;
3747
+ const fingerprint = signedBlockFingerprint(index, message.content);
3748
+ if (!fingerprint || !rememberedRepairs.has(fingerprint)) continue;
3749
+ const attempt = repairMessageAt(parsed, index);
3750
+ if (!attempt) continue;
3751
+ totalRemovedBlocks += attempt.removedBlocks;
3752
+ repaired = {
3753
+ ...attempt,
3754
+ removedBlocks: totalRemovedBlocks
3755
+ };
3756
+ }
3757
+ return repaired;
3758
+ }
3759
+ function rememberThinkingHistoryRepair(fingerprint) {
3760
+ if (rememberedRepairs.has(fingerprint)) return;
3761
+ if (rememberedRepairs.size >= MAX_REMEMBERED_REPAIRS) {
3762
+ const oldest = rememberedRepairs.values().next().value;
3763
+ if (typeof oldest === "string") rememberedRepairs.delete(oldest);
3764
+ }
3765
+ rememberedRepairs.add(fingerprint);
3766
+ }
3767
+ //#endregion
3504
3768
  //#region src/routes/messages/handler.ts
3769
+ const MAX_THINKING_REPAIR_ATTEMPTS = 5;
3505
3770
  const isWebSearchTool$1 = (tool) => !!tool && typeof tool === "object" && (typeof tool.type === "string" && tool.type.startsWith("web_search") || tool.name === "web_search");
3506
3771
  /**
3507
3772
  * Extract whitelisted beta headers from the incoming request to forward
@@ -3698,15 +3963,30 @@ async function handleCompletion(c) {
3698
3963
  if (modelId) logEndpointMismatch(modelId, "/v1/messages");
3699
3964
  const effectiveBetas = applyDefaultBetas(betaHeaders, resolvedModel ?? originalModel);
3700
3965
  const advisorAborter = advisorEnabled ? new AbortController() : void 0;
3966
+ const requestHeaders = {
3967
+ ...selectedModel?.requestHeaders,
3968
+ ...effectiveBetas
3969
+ };
3970
+ let nativeBody = resolvedBody;
3971
+ const knownThinkingRepair = repairKnownThinkingHistory(nativeBody);
3972
+ if (knownThinkingRepair) {
3973
+ nativeBody = knownThinkingRepair.body;
3974
+ consola.warn(`Reapplying proven thinking-history repair: message=${knownThinkingRepair.messageIndex} removed_blocks=${knownThinkingRepair.removedBlocks}`);
3975
+ }
3701
3976
  let response;
3702
- try {
3703
- response = await createMessages(resolvedBody, {
3704
- ...selectedModel?.requestHeaders,
3705
- ...effectiveBetas
3706
- }, advisorAborter?.signal, true);
3977
+ let acceptedRepair;
3978
+ const repairedMessageIndices = /* @__PURE__ */ new Set();
3979
+ for (let attempt = 0; response === void 0; attempt++) try {
3980
+ response = await createMessages(nativeBody, requestHeaders, advisorAborter?.signal, true);
3981
+ if (acceptedRepair) {
3982
+ rememberThinkingHistoryRepair(acceptedRepair.fingerprint);
3983
+ consola.warn(`Thinking-history repair succeeded: message=${acceptedRepair.messageIndex} removed_blocks=${acceptedRepair.removedBlocks} attempts=${attempt}`);
3984
+ }
3707
3985
  } catch (error) {
3708
- if (error instanceof HTTPError) {
3709
- const errorBody = await error.response.clone().text().catch(() => "");
3986
+ if (!(error instanceof HTTPError)) throw error;
3987
+ const errorBody = await error.response.clone().text().catch(() => "");
3988
+ const thinkingRepair = attempt < MAX_THINKING_REPAIR_ATTEMPTS ? repairRejectedThinkingHistory(nativeBody, errorBody) : void 0;
3989
+ if (!thinkingRepair || repairedMessageIndices.has(thinkingRepair.messageIndex)) {
3710
3990
  logRequest({
3711
3991
  method: "POST",
3712
3992
  path: c.req.path,
@@ -3715,8 +3995,13 @@ async function handleCompletion(c) {
3715
3995
  status: error.response.status,
3716
3996
  errorBody
3717
3997
  }, selectedModel, startTime);
3998
+ throw error;
3718
3999
  }
3719
- throw error;
4000
+ if (acceptedRepair) rememberThinkingHistoryRepair(acceptedRepair.fingerprint);
4001
+ consola.warn(`Copilot rejected signed thinking history; retrying without rejected historical blocks: message=${thinkingRepair.messageIndex} removed_blocks=${thinkingRepair.removedBlocks} attempt=${attempt + 1}`);
4002
+ nativeBody = thinkingRepair.body;
4003
+ acceptedRepair = thinkingRepair;
4004
+ repairedMessageIndices.add(thinkingRepair.messageIndex);
3720
4005
  }
3721
4006
  const contentType = response.headers.get("content-type") ?? "";
3722
4007
  const clientAcceptsSSE = (c.req.header("accept") ?? "").includes("text/event-stream");
@@ -3751,17 +4036,14 @@ async function handleCompletion(c) {
3751
4036
  if (advisorEnabled && response.body) {
3752
4037
  let parsedBase = {};
3753
4038
  try {
3754
- parsedBase = JSON.parse(resolvedBody);
4039
+ parsedBase = JSON.parse(nativeBody);
3755
4040
  } catch {}
3756
4041
  const initialConversation = Array.isArray(parsedBase.messages) ? parsedBase.messages : [];
3757
4042
  return new Response(buildAdvisorStream({
3758
4043
  firstResponse: response,
3759
4044
  initialConversation,
3760
4045
  baseBody: parsedBase,
3761
- requestHeaders: {
3762
- ...selectedModel?.requestHeaders,
3763
- ...effectiveBetas
3764
- },
4046
+ requestHeaders,
3765
4047
  externalAborter: advisorAborter
3766
4048
  }), {
3767
4049
  status: response.status,
@@ -3821,7 +4103,7 @@ function resolveModelInBody$1(rawBody) {
3821
4103
  const selectedModel = resolvedModel ? state.models?.data.find((m) => m.id === resolvedModel) : void 0;
3822
4104
  if (translateThinking(parsed, selectedModel)) modified = true;
3823
4105
  if (clampOutputConfigEffortInPlace(parsed, selectedModel)) modified = true;
3824
- if (rawBody.includes("\"scope\"") && sanitizeCacheControl$1(parsed)) modified = true;
4106
+ if ((rawBody.includes("\"scope\"") || rawBody.includes("\"cache_control\"") && (rawBody.includes("\"thinking\"") || rawBody.includes("\"redacted_thinking\""))) && sanitizeCacheControl$1(parsed)) modified = true;
3825
4107
  if ((rawBody.includes("\"budget\"") || rawBody.includes("\"output_config\"") || rawBody.includes("\"betas\"") || rawBody.includes("\"eager_input_streaming\"")) && stripAnthropicOnlyFields$1(parsed)) modified = true;
3826
4108
  return {
3827
4109
  body: modified ? JSON.stringify(parsed) : rawBody,
@@ -3903,7 +4185,9 @@ function translateThinking(body, model) {
3903
4185
  return true;
3904
4186
  }
3905
4187
  /**
3906
- * Strip the `scope` field from all `cache_control` objects in the body.
4188
+ * Strip the `scope` field from ordinary `cache_control` objects. Copilot
4189
+ * rejects the entire cache_control object on signed thinking blocks, so remove
4190
+ * that unsigned transport metadata without touching thinking/signature/data.
3907
4191
  * Claude CLI 2.1.88+ sends {"type":"ephemeral","scope":"global"} which
3908
4192
  * Copilot rejects. Mutates the parsed object in place.
3909
4193
  *
@@ -3914,6 +4198,11 @@ function sanitizeCacheControl$1(body) {
3914
4198
  let stripped = false;
3915
4199
  function stripScope(block) {
3916
4200
  const cc = block.cache_control;
4201
+ if (cc && (block.type === "thinking" || block.type === "redacted_thinking")) {
4202
+ delete block.cache_control;
4203
+ stripped = true;
4204
+ return;
4205
+ }
3917
4206
  if (cc?.scope !== void 0) {
3918
4207
  delete cc.scope;
3919
4208
  if (Object.keys(cc).length === 0) delete block.cache_control;
@@ -4120,7 +4409,7 @@ function resolveModelInBody(rawBody) {
4120
4409
  modified = true;
4121
4410
  }
4122
4411
  }
4123
- if (rawBody.includes("\"scope\"") && sanitizeCacheControl(parsed)) modified = true;
4412
+ if ((rawBody.includes("\"scope\"") || rawBody.includes("\"cache_control\"") && (rawBody.includes("\"thinking\"") || rawBody.includes("\"redacted_thinking\""))) && sanitizeCacheControl(parsed)) modified = true;
4124
4413
  if ((rawBody.includes("\"budget\"") || rawBody.includes("\"output_config\"") || rawBody.includes("\"betas\"") || rawBody.includes("\"eager_input_streaming\"")) && stripAnthropicOnlyFields(parsed)) modified = true;
4125
4414
  const resolvedModel = typeof parsed.model === "string" ? parsed.model : originalModel;
4126
4415
  const selectedModel = resolvedModel ? state.models?.data.find((m) => m.id === resolvedModel) : void 0;
@@ -4134,6 +4423,11 @@ function resolveModelInBody(rawBody) {
4134
4423
  function sanitizeCacheControl(body) {
4135
4424
  let stripped = false;
4136
4425
  function stripScope(block) {
4426
+ if (block.cache_control && (block.type === "thinking" || block.type === "redacted_thinking")) {
4427
+ delete block.cache_control;
4428
+ stripped = true;
4429
+ return;
4430
+ }
4137
4431
  if (block.cache_control?.scope !== void 0) {
4138
4432
  delete block.cache_control.scope;
4139
4433
  if (Object.keys(block.cache_control).length === 0) delete block.cache_control;
@@ -5263,4 +5557,4 @@ function getCodexEnvVars(serverUrl) {
5263
5557
  //#endregion
5264
5558
  export { sharedServerArgs as a, listModelsForEndpoint as c, checkClaudeVersion as d, updateClaude as f, setupAndServe as i, enableFileLogging as l, getCodexEnvVars as n, startKeepAwake as o, parseSharedArgs as r, stopKeepAwake as s, getClaudeCodeEnvVars as t, runSelfUpdate as u };
5265
5559
 
5266
- //# sourceMappingURL=server-setup-Dd293T1O.js.map
5560
+ //# sourceMappingURL=server-setup-BmyWS4WU.js.map