@sunshaobei/dsh-balance-panel 3.0.6 → 3.0.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/lib/client.js CHANGED
@@ -25,7 +25,7 @@
25
25
  * follows the product theme (light/dark) instead of carrying its own palette.
26
26
  */
27
27
  window.__ModuleLoader__.load({
28
- id: "dsh-balance-panel",
28
+ id: "@sunshaobei/dsh-balance-panel",
29
29
  factory: (require) => {
30
30
  var module = { exports: {} };
31
31
  var exports = module.exports;
package/lib/index.js CHANGED
@@ -268,6 +268,31 @@ let chatgptTokenCache = null;
268
268
  // ChatGPT/Codex token resolver read the shared grant record that
269
269
  // dsh-codex-auth writes (llm-pi-ai/openai-codex).
270
270
  let codexCredentialsRef = null;
271
+ // Auto-detected local proxy for ChatGPT/Codex upstream calls. The usage and
272
+ // token endpoints are region-blocked on direct connections, and Node's fetch
273
+ // never reads the OS proxy — so when the panel row has no explicit proxy we
274
+ // probe the well-known local proxy ports once and remember the answer.
275
+ let localProxyCache = null; // null = not probed yet
276
+ async function detectLocalProxy() {
277
+ if (localProxyCache !== null)
278
+ return localProxyCache || undefined;
279
+ for (const port of [7897, 7890, 1087, 8118, 6152]) {
280
+ const ok = await new Promise((resolve) => {
281
+ const req = http.request({ host: '127.0.0.1', port, method: 'CONNECT', path: 'chatgpt.com:443', timeout: 2000 });
282
+ req.on('connect', (res, socket) => { socket.destroy(); resolve(res.statusCode === 200); });
283
+ req.on('error', () => resolve(false));
284
+ req.on('timeout', () => { req.destroy(); resolve(false); });
285
+ req.end();
286
+ });
287
+ if (ok) {
288
+ localProxyCache = 'http://127.0.0.1:' + port;
289
+ break;
290
+ }
291
+ }
292
+ if (localProxyCache === null)
293
+ localProxyCache = '';
294
+ return localProxyCache || undefined;
295
+ }
271
296
  async function readChatGPTAuthFile() {
272
297
  const authJson = chatgptAuthJsonPath();
273
298
  let raw;
@@ -1669,7 +1694,7 @@ async function fetchRow(ctx, provider, proxies, clientProxyUrl) {
1669
1694
  // query AND token refresh — same region constraints apply.
1670
1695
  const cgProxy = clientProxyUrl !== undefined
1671
1696
  ? clientProxyUrl
1672
- : (provider.proxy !== undefined ? proxies[provider.proxy] : undefined);
1697
+ : (provider.proxy !== undefined ? proxies[provider.proxy] : await detectLocalProxy());
1673
1698
  let { accessToken, accountId } = await resolveChatGPTToken(UPSTREAM_TIMEOUT_MS, cgProxy);
1674
1699
  let body;
1675
1700
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sunshaobei/dsh-balance-panel",
3
3
  "description": "Provider quota/balance widget for the dsh web surface: an inline sidebar card above the Settings row (collapsed glanceable row expanding into a Harness-native card with a settings panel), fed by a loopback Connection RPC channel whose host half proxies each provider with credentials that never reach the browser.",
4
- "version": "3.0.6",
4
+ "version": "3.0.7",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/index.d.ts",
package/src/client.ts CHANGED
@@ -25,7 +25,7 @@
25
25
  * follows the product theme (light/dark) instead of carrying its own palette.
26
26
  */
27
27
  (window as any).__ModuleLoader__.load({
28
- id: "dsh-balance-panel",
28
+ id: "@sunshaobei/dsh-balance-panel",
29
29
  factory: (require) => {
30
30
  var module = { exports: {} as any };
31
31
  var exports = module.exports;
package/src/index.ts CHANGED
@@ -292,6 +292,27 @@ let chatgptTokenCache: { bundle: ChatGPTTokenBundle } | null = null;
292
292
  // dsh-codex-auth writes (llm-pi-ai/openai-codex).
293
293
  let codexCredentialsRef: any = null;
294
294
 
295
+ // Auto-detected local proxy for ChatGPT/Codex upstream calls. The usage and
296
+ // token endpoints are region-blocked on direct connections, and Node's fetch
297
+ // never reads the OS proxy — so when the panel row has no explicit proxy we
298
+ // probe the well-known local proxy ports once and remember the answer.
299
+ let localProxyCache: string | undefined | null = null; // null = not probed yet
300
+ async function detectLocalProxy(): Promise<string | undefined> {
301
+ if (localProxyCache !== null) return localProxyCache || undefined;
302
+ for (const port of [7897, 7890, 1087, 8118, 6152]) {
303
+ const ok = await new Promise<boolean>((resolve) => {
304
+ const req = http.request({ host: '127.0.0.1', port, method: 'CONNECT', path: 'chatgpt.com:443', timeout: 2000 });
305
+ req.on('connect', (res, socket) => { socket.destroy(); resolve(res.statusCode === 200); });
306
+ req.on('error', () => resolve(false));
307
+ req.on('timeout', () => { req.destroy(); resolve(false); });
308
+ req.end();
309
+ });
310
+ if (ok) { localProxyCache = 'http://127.0.0.1:' + port; break; }
311
+ }
312
+ if (localProxyCache === null) localProxyCache = '';
313
+ return localProxyCache || undefined;
314
+ }
315
+
295
316
  async function readChatGPTAuthFile(): Promise<ChatGPTTokenBundle> {
296
317
  const authJson = chatgptAuthJsonPath();
297
318
  let raw: string;
@@ -1628,7 +1649,7 @@ async function fetchRow(ctx, provider, proxies, clientProxyUrl) {
1628
1649
  // query AND token refresh — same region constraints apply.
1629
1650
  const cgProxy = clientProxyUrl !== undefined
1630
1651
  ? clientProxyUrl
1631
- : (provider.proxy !== undefined ? proxies[provider.proxy] : undefined);
1652
+ : (provider.proxy !== undefined ? proxies[provider.proxy] : await detectLocalProxy());
1632
1653
  let { accessToken, accountId } = await resolveChatGPTToken(UPSTREAM_TIMEOUT_MS, cgProxy);
1633
1654
  let body: any;
1634
1655
  try {