copilot-api-plus 1.2.5 → 1.2.7

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
@@ -120,16 +120,18 @@ async function applyProxyConfig() {
120
120
  }
121
121
  //#endregion
122
122
  //#region src/lib/proxy.ts
123
+ const agentOptions = {
124
+ keepAliveTimeout: 3e4,
125
+ keepAliveMaxTimeout: 6e4,
126
+ connect: { timeout: 15e3 }
127
+ };
128
+ let direct;
129
+ let proxies = /* @__PURE__ */ new Map();
123
130
  function initProxyFromEnv() {
124
131
  if (typeof Bun !== "undefined") return;
125
132
  try {
126
- const agentOptions = {
127
- keepAliveTimeout: 3e4,
128
- keepAliveMaxTimeout: 6e4,
129
- connect: { timeout: 15e3 }
130
- };
131
- const direct = new Agent(agentOptions);
132
- const proxies = /* @__PURE__ */ new Map();
133
+ direct = new Agent(agentOptions);
134
+ proxies = /* @__PURE__ */ new Map();
133
135
  setGlobalDispatcher({
134
136
  dispatch(options, handler) {
135
137
  try {
@@ -173,6 +175,28 @@ function initProxyFromEnv() {
173
175
  consola.debug("Proxy setup skipped:", err);
174
176
  }
175
177
  }
178
+ /**
179
+ * Destroy all pooled connections (direct + proxy agents) and replace them
180
+ * with fresh instances. The global dispatcher's `dispatch` method captures
181
+ * `direct` and `proxies` by reference, so subsequent requests automatically
182
+ * use the new agents — no need to call `setGlobalDispatcher` again.
183
+ *
184
+ * Call this after a network error to discard stale/half-closed sockets that
185
+ * would otherwise cause every retry to wait ~60 s before timing out.
186
+ *
187
+ * Under the Bun runtime (which doesn't use undici) this is a no-op.
188
+ */
189
+ function resetConnections() {
190
+ if (typeof Bun !== "undefined") return;
191
+ if (!direct) return;
192
+ const oldDirect = direct;
193
+ const oldProxies = proxies;
194
+ direct = new Agent(agentOptions);
195
+ proxies = /* @__PURE__ */ new Map();
196
+ oldDirect.close().catch(() => {});
197
+ for (const agent of oldProxies.values()) agent.close().catch(() => {});
198
+ consola.debug("Connection pool reset — stale sockets cleared");
199
+ }
176
200
  //#endregion
177
201
  //#region src/account.ts
178
202
  const addAccount = defineCommand({
@@ -1684,15 +1708,12 @@ async function checkRateLimit(state) {
1684
1708
  */
1685
1709
  const FETCH_TIMEOUT_MS = 12e4;
1686
1710
  /**
1687
- * Retry delays in ms (exponential back-off).
1688
- * After a network error we wait longer before each retry to let the
1689
- * Copilot backend recover and avoid triggering connection-level throttling.
1711
+ * Retry delays in ms. After the first failure the connection pool is reset
1712
+ * (see `resetConnections`), so a single retry with a fresh socket is usually
1713
+ * enough. Keeping retries minimal avoids wasting Copilot request credits
1714
+ * (billed per request).
1690
1715
  */
1691
- const RETRY_DELAYS = [
1692
- 2e3,
1693
- 5e3,
1694
- 1e4
1695
- ];
1716
+ const RETRY_DELAYS = [2e3];
1696
1717
  /**
1697
1718
  * Wrapper around `fetch()` that aborts if the server doesn't respond within
1698
1719
  * `timeoutMs`. The timeout only covers the period until the response headers
@@ -1727,6 +1748,7 @@ async function fetchWithRetry(url, buildInit) {
1727
1748
  return await fetchWithTimeout(url, buildInit());
1728
1749
  } catch (error) {
1729
1750
  lastError = error;
1751
+ if (attempt === 0) resetConnections();
1730
1752
  if (attempt < maxAttempts - 1) {
1731
1753
  const delay = RETRY_DELAYS[attempt];
1732
1754
  consola.warn(`Network error on attempt ${attempt + 1}/${maxAttempts}, retrying in ${delay}ms:`, error instanceof Error ? error.message : error);
@@ -1930,16 +1952,8 @@ async function handleMultiAccountHttpError(error, account, retryContext) {
1930
1952
  return null;
1931
1953
  default:
1932
1954
  if (error.response.status >= 500) {
1933
- consola.warn(`Account ${account.label}: upstream ${error.response.status}, retrying in 2s...`);
1934
- await new Promise((r) => setTimeout(r, 2e3));
1935
- try {
1936
- const result = await doFetch(retryContext.payload, retryContext.tokenSource);
1937
- accountManager.markAccountSuccess(account.id);
1938
- return result;
1939
- } catch {
1940
- consola.warn(`Account ${account.label}: upstream ${error.response.status} persists, trying next account...`);
1941
- return null;
1942
- }
1955
+ accountManager.markAccountStatus(account.id, "error", `HTTP ${error.response.status}`);
1956
+ return null;
1943
1957
  }
1944
1958
  accountManager.markAccountStatus(account.id, "error", `HTTP ${error.response.status}`);
1945
1959
  return null;