@kenkaiiii/gg-core 5.70.0 → 5.72.0

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/index.cjs CHANGED
@@ -150,6 +150,12 @@ function getAppPaths() {
150
150
  };
151
151
  }
152
152
 
153
+ // src/oauth/request-timeout.ts
154
+ var OAUTH_REQUEST_TIMEOUT_MS = 3e4;
155
+ function oauthRequestSignal() {
156
+ return AbortSignal.timeout(OAUTH_REQUEST_TIMEOUT_MS);
157
+ }
158
+
153
159
  // src/oauth/kimi.ts
154
160
  var CLIENT_ID = "17e5f671-d194-4dfb-9706-5516cb48c098";
155
161
  var DEFAULT_OAUTH_HOST = "https://auth.kimi.com";
@@ -237,7 +243,8 @@ async function postForm(endpoint, params) {
237
243
  "Content-Type": "application/x-www-form-urlencoded",
238
244
  Accept: "application/json"
239
245
  },
240
- body: new URLSearchParams(params).toString()
246
+ body: new URLSearchParams(params).toString(),
247
+ signal: oauthRequestSignal()
241
248
  });
242
249
  let data = {};
243
250
  try {
@@ -625,7 +632,12 @@ async function postTokenRequest(body, label) {
625
632
  for (const url of TOKEN_URLS) {
626
633
  let response;
627
634
  try {
628
- response = await fetch(url, { method: "POST", headers, body: encoded });
635
+ response = await fetch(url, {
636
+ method: "POST",
637
+ headers,
638
+ body: encoded,
639
+ signal: oauthRequestSignal()
640
+ });
629
641
  } catch (err) {
630
642
  lastError = err instanceof Error ? err : new Error(String(err));
631
643
  continue;
@@ -874,7 +886,8 @@ async function exchangeOpenAICode(code, verifier) {
874
886
  code,
875
887
  redirect_uri: REDIRECT_URI2,
876
888
  code_verifier: verifier
877
- })
889
+ }),
890
+ signal: oauthRequestSignal()
878
891
  });
879
892
  if (!response.ok) {
880
893
  const text = await response.text();
@@ -895,7 +908,8 @@ async function refreshOpenAIToken(refreshToken) {
895
908
  grant_type: "refresh_token",
896
909
  refresh_token: refreshToken,
897
910
  client_id: CLIENT_ID3
898
- })
911
+ }),
912
+ signal: oauthRequestSignal()
899
913
  });
900
914
  if (!response.ok) {
901
915
  const text = await response.text();
@@ -1105,7 +1119,8 @@ async function postTokenRequest2(body) {
1105
1119
  const response = await fetch(TOKEN_URL2, {
1106
1120
  method: "POST",
1107
1121
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
1108
- body: new URLSearchParams(body)
1122
+ body: new URLSearchParams(body),
1123
+ signal: oauthRequestSignal()
1109
1124
  });
1110
1125
  if (!response.ok) {
1111
1126
  const text = await response.text();
@@ -1234,7 +1249,8 @@ async function codeAssistGet(accessToken, operationName) {
1234
1249
  async function codeAssistRequest(url, accessToken, label, init) {
1235
1250
  const response = await fetch(url, {
1236
1251
  ...init,
1237
- headers: codeAssistHeaders(accessToken)
1252
+ headers: codeAssistHeaders(accessToken),
1253
+ signal: oauthRequestSignal()
1238
1254
  });
1239
1255
  if (!response.ok) {
1240
1256
  const text = await response.text();
@@ -1320,7 +1336,8 @@ async function postForm2(endpoint, params) {
1320
1336
  Accept: "application/json",
1321
1337
  "User-Agent": `grok-cli/${grokCliVersion()}`
1322
1338
  },
1323
- body: new URLSearchParams(params).toString()
1339
+ body: new URLSearchParams(params).toString(),
1340
+ signal: oauthRequestSignal()
1324
1341
  });
1325
1342
  let data = {};
1326
1343
  try {
@@ -3361,12 +3378,16 @@ async function fetchSubscriptionUsage(provider, credentials, options = {}) {
3361
3378
 
3362
3379
  // src/telegram.ts
3363
3380
  var TELEGRAM_API = "https://api.telegram.org";
3381
+ var LONG_POLL_SECONDS = 30;
3382
+ var API_TIMEOUT_MS = 3e4;
3364
3383
  var MAX_MESSAGE_LENGTH = 4096;
3365
3384
  var TelegramBot = class {
3366
3385
  token;
3367
3386
  allowedUserId;
3368
3387
  offset = 0;
3369
3388
  running = false;
3389
+ /** Aborts the in-flight long poll so stop() takes effect immediately. */
3390
+ pollAbort = null;
3370
3391
  onMessage = null;
3371
3392
  onVoiceMessage = null;
3372
3393
  onCallback = null;
@@ -3419,6 +3440,7 @@ var TelegramBot = class {
3419
3440
  /** Stop long polling. */
3420
3441
  stop() {
3421
3442
  this.running = false;
3443
+ this.pollAbort?.abort();
3422
3444
  }
3423
3445
  /** Send a text message to a specific chat. Converts markdown and splits long messages. */
3424
3446
  async send(chatId, text, buttons) {
@@ -3465,12 +3487,26 @@ var TelegramBot = class {
3465
3487
  }
3466
3488
  // ── Private ───────────────────────────────────────────
3467
3489
  async getUpdates() {
3468
- const result = await this.apiCall("getUpdates", {
3469
- offset: this.offset,
3470
- timeout: 30,
3471
- allowed_updates: ["message", "callback_query", "my_chat_member"]
3472
- });
3473
- if (!result.ok || !Array.isArray(result.result)) return [];
3490
+ const poll = new AbortController();
3491
+ this.pollAbort = poll;
3492
+ let result;
3493
+ try {
3494
+ result = await this.apiCall(
3495
+ "getUpdates",
3496
+ {
3497
+ offset: this.offset,
3498
+ timeout: LONG_POLL_SECONDS,
3499
+ allowed_updates: ["message", "callback_query", "my_chat_member"]
3500
+ },
3501
+ // A half-open socket would otherwise hang the poll forever.
3502
+ AbortSignal.any([poll.signal, AbortSignal.timeout((LONG_POLL_SECONDS + 15) * 1e3)])
3503
+ );
3504
+ } finally {
3505
+ if (this.pollAbort === poll) this.pollAbort = null;
3506
+ }
3507
+ if (!result.ok || !Array.isArray(result.result)) {
3508
+ throw new Error(`getUpdates failed: ${JSON.stringify(result)}`);
3509
+ }
3474
3510
  const updates = result.result;
3475
3511
  if (updates.length > 0) {
3476
3512
  this.offset = updates[updates.length - 1].update_id + 1;
@@ -3518,12 +3554,13 @@ var TelegramBot = class {
3518
3554
  }
3519
3555
  }
3520
3556
  }
3521
- async apiCall(method, body) {
3557
+ async apiCall(method, body, signal = AbortSignal.timeout(API_TIMEOUT_MS)) {
3522
3558
  const url = `${TELEGRAM_API}/bot${this.token}/${method}`;
3523
3559
  const response = await fetch(url, {
3524
3560
  method: "POST",
3525
3561
  headers: { "Content-Type": "application/json" },
3526
- body: body ? JSON.stringify(body) : void 0
3562
+ body: body ? JSON.stringify(body) : void 0,
3563
+ signal
3527
3564
  });
3528
3565
  if (!response.ok) {
3529
3566
  return { ok: false };
@@ -3656,7 +3693,7 @@ function isModelLoaded() {
3656
3693
  return transcriber !== null;
3657
3694
  }
3658
3695
  async function transcribeVoice(fileUrl) {
3659
- const response = await fetch(fileUrl);
3696
+ const response = await fetch(fileUrl, { signal: AbortSignal.timeout(6e4) });
3660
3697
  if (!response.ok) throw new Error(`Failed to download voice file: ${response.status}`);
3661
3698
  const buffer = new Uint8Array(await response.arrayBuffer());
3662
3699
  const pcm = await decodeOggOpus(buffer);