@prestyj/core 5.28.1 → 5.29.1

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
@@ -69,7 +69,7 @@ __export(index_exports, {
69
69
  getContextWindow: () => getContextWindow,
70
70
  getDefaultModel: () => getDefaultModel,
71
71
  getDefaultThinkingLevel: () => getDefaultThinkingLevel,
72
- getFastModel: () => getFastModel,
72
+ getLowestThinkingLevel: () => getLowestThinkingLevel,
73
73
  getMaxThinkingLevel: () => getMaxThinkingLevel,
74
74
  getModel: () => getModel,
75
75
  getModelsForProvider: () => getModelsForProvider,
@@ -152,6 +152,12 @@ function getAppPaths() {
152
152
  };
153
153
  }
154
154
 
155
+ // src/oauth/request-timeout.ts
156
+ var OAUTH_REQUEST_TIMEOUT_MS = 3e4;
157
+ function oauthRequestSignal() {
158
+ return AbortSignal.timeout(OAUTH_REQUEST_TIMEOUT_MS);
159
+ }
160
+
155
161
  // src/oauth/kimi.ts
156
162
  var CLIENT_ID = "17e5f671-d194-4dfb-9706-5516cb48c098";
157
163
  var DEFAULT_OAUTH_HOST = "https://auth.kimi.com";
@@ -239,7 +245,8 @@ async function postForm(endpoint, params) {
239
245
  "Content-Type": "application/x-www-form-urlencoded",
240
246
  Accept: "application/json"
241
247
  },
242
- body: new URLSearchParams(params).toString()
248
+ body: new URLSearchParams(params).toString(),
249
+ signal: oauthRequestSignal()
243
250
  });
244
251
  let data = {};
245
252
  try {
@@ -713,7 +720,12 @@ async function postTokenRequest(body, label) {
713
720
  for (const url of TOKEN_URLS) {
714
721
  let response;
715
722
  try {
716
- response = await fetch(url, { method: "POST", headers, body: encoded });
723
+ response = await fetch(url, {
724
+ method: "POST",
725
+ headers,
726
+ body: encoded,
727
+ signal: oauthRequestSignal()
728
+ });
717
729
  } catch (err) {
718
730
  lastError = err instanceof Error ? err : new Error(String(err));
719
731
  continue;
@@ -962,7 +974,8 @@ async function exchangeOpenAICode(code, verifier) {
962
974
  code,
963
975
  redirect_uri: REDIRECT_URI2,
964
976
  code_verifier: verifier
965
- })
977
+ }),
978
+ signal: oauthRequestSignal()
966
979
  });
967
980
  if (!response.ok) {
968
981
  const text = await response.text();
@@ -983,7 +996,8 @@ async function refreshOpenAIToken(refreshToken) {
983
996
  grant_type: "refresh_token",
984
997
  refresh_token: refreshToken,
985
998
  client_id: CLIENT_ID3
986
- })
999
+ }),
1000
+ signal: oauthRequestSignal()
987
1001
  });
988
1002
  if (!response.ok) {
989
1003
  const text = await response.text();
@@ -1193,7 +1207,8 @@ async function postTokenRequest2(body) {
1193
1207
  const response = await fetch(TOKEN_URL2, {
1194
1208
  method: "POST",
1195
1209
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
1196
- body: new URLSearchParams(body)
1210
+ body: new URLSearchParams(body),
1211
+ signal: oauthRequestSignal()
1197
1212
  });
1198
1213
  if (!response.ok) {
1199
1214
  const text = await response.text();
@@ -1322,7 +1337,8 @@ async function codeAssistGet(accessToken, operationName) {
1322
1337
  async function codeAssistRequest(url, accessToken, label, init) {
1323
1338
  const response = await fetch(url, {
1324
1339
  ...init,
1325
- headers: codeAssistHeaders(accessToken)
1340
+ headers: codeAssistHeaders(accessToken),
1341
+ signal: oauthRequestSignal()
1326
1342
  });
1327
1343
  if (!response.ok) {
1328
1344
  const text = await response.text();
@@ -1408,7 +1424,8 @@ async function postForm2(endpoint, params) {
1408
1424
  Accept: "application/json",
1409
1425
  "User-Agent": `grok-cli/${grokCliVersion()}`
1410
1426
  },
1411
- body: new URLSearchParams(params).toString()
1427
+ body: new URLSearchParams(params).toString(),
1428
+ signal: oauthRequestSignal()
1412
1429
  });
1413
1430
  let data = {};
1414
1431
  try {
@@ -2430,9 +2447,7 @@ var MODELS = [
2430
2447
  // Sent over our Code Assist (OAuth) transport ahead of gemini-cli — upstream
2431
2448
  // hasn't listed 3.7 yet (google-gemini/gemini-cli#28802, still open) — so
2432
2449
  // free/personal accounts 404 (entitlement-gated) while Code Assist
2433
- // Standard/Enterprise accounts get it. Kept after the working flash-lite:
2434
- // getFastModel picks the first low-tier entry, and flash-lite is the one
2435
- // that works on every account.
2450
+ // Standard/Enterprise accounts get it.
2436
2451
  id: "gemini-3.7-flash",
2437
2452
  name: "Gemini 3.7 Flash",
2438
2453
  provider: "gemini",
@@ -2843,19 +2858,17 @@ function getDefaultThinkingLevel(modelId, options) {
2843
2858
  if (model?.id === "kimi-k3" && isKimiCodingEndpoint(options?.baseUrl)) return "high";
2844
2859
  return model?.defaultThinkingLevel ?? model?.maxThinkingLevel ?? "high";
2845
2860
  }
2861
+ var SUMMARY_COST_TIER = {
2862
+ anthropic: "medium",
2863
+ openai: "low",
2864
+ glm: "low",
2865
+ deepseek: "low",
2866
+ huggingface: "low"
2867
+ };
2846
2868
  function getSummaryModel(provider, currentModelId) {
2847
- if (provider === "anthropic") {
2848
- return MODELS.find((m) => m.id === "claude-sonnet-5-5");
2849
- }
2850
- if (provider === "openai" || provider === "glm" || provider === "deepseek" || provider === "huggingface") {
2851
- const low = getModelsForProvider(provider).find((m) => m.costTier === "low");
2852
- if (low) return low;
2853
- }
2854
- return getModel(currentModelId) ?? getDefaultModel(provider);
2855
- }
2856
- function getFastModel(provider, currentModelId) {
2857
- const low = getModelsForProvider(provider).find((m) => m.costTier === "low");
2858
- return low ?? getModel(currentModelId) ?? getDefaultModel(provider);
2869
+ const tier = SUMMARY_COST_TIER[provider];
2870
+ const tagged = tier ? getModelsForProvider(provider).find((m) => m.costTier === tier) : void 0;
2871
+ return tagged ?? getModel(currentModelId) ?? getDefaultModel(provider);
2859
2872
  }
2860
2873
 
2861
2874
  // src/thinking-level.ts
@@ -2945,6 +2958,10 @@ function getSupportedThinkingLevels(provider, model) {
2945
2958
  if (maxIndex === -1) return ["medium"];
2946
2959
  return levels.slice(0, maxIndex + 1);
2947
2960
  }
2961
+ function getLowestThinkingLevel(provider, model) {
2962
+ if (getModel(model)?.supportsThinking === false) return void 0;
2963
+ return getSupportedThinkingLevels(provider, model)[0];
2964
+ }
2948
2965
  function isThinkingLevelSupported(provider, model, level) {
2949
2966
  return getSupportedThinkingLevels(provider, model).includes(level);
2950
2967
  }
@@ -3492,12 +3509,16 @@ async function fetchSubscriptionUsage(provider, credentials, options = {}) {
3492
3509
 
3493
3510
  // src/telegram.ts
3494
3511
  var TELEGRAM_API = "https://api.telegram.org";
3512
+ var LONG_POLL_SECONDS = 30;
3513
+ var API_TIMEOUT_MS = 3e4;
3495
3514
  var MAX_MESSAGE_LENGTH = 4096;
3496
3515
  var TelegramBot = class {
3497
3516
  token;
3498
3517
  allowedUserId;
3499
3518
  offset = 0;
3500
3519
  running = false;
3520
+ /** Aborts the in-flight long poll so stop() takes effect immediately. */
3521
+ pollAbort = null;
3501
3522
  onMessage = null;
3502
3523
  onVoiceMessage = null;
3503
3524
  onCallback = null;
@@ -3550,6 +3571,7 @@ var TelegramBot = class {
3550
3571
  /** Stop long polling. */
3551
3572
  stop() {
3552
3573
  this.running = false;
3574
+ this.pollAbort?.abort();
3553
3575
  }
3554
3576
  /** Send a text message to a specific chat. Converts markdown and splits long messages. */
3555
3577
  async send(chatId, text, buttons) {
@@ -3596,12 +3618,26 @@ var TelegramBot = class {
3596
3618
  }
3597
3619
  // ── Private ───────────────────────────────────────────
3598
3620
  async getUpdates() {
3599
- const result = await this.apiCall("getUpdates", {
3600
- offset: this.offset,
3601
- timeout: 30,
3602
- allowed_updates: ["message", "callback_query", "my_chat_member"]
3603
- });
3604
- if (!result.ok || !Array.isArray(result.result)) return [];
3621
+ const poll = new AbortController();
3622
+ this.pollAbort = poll;
3623
+ let result;
3624
+ try {
3625
+ result = await this.apiCall(
3626
+ "getUpdates",
3627
+ {
3628
+ offset: this.offset,
3629
+ timeout: LONG_POLL_SECONDS,
3630
+ allowed_updates: ["message", "callback_query", "my_chat_member"]
3631
+ },
3632
+ // A half-open socket would otherwise hang the poll forever.
3633
+ AbortSignal.any([poll.signal, AbortSignal.timeout((LONG_POLL_SECONDS + 15) * 1e3)])
3634
+ );
3635
+ } finally {
3636
+ if (this.pollAbort === poll) this.pollAbort = null;
3637
+ }
3638
+ if (!result.ok || !Array.isArray(result.result)) {
3639
+ throw new Error(`getUpdates failed: ${JSON.stringify(result)}`);
3640
+ }
3605
3641
  const updates = result.result;
3606
3642
  if (updates.length > 0) {
3607
3643
  this.offset = updates[updates.length - 1].update_id + 1;
@@ -3649,12 +3685,13 @@ var TelegramBot = class {
3649
3685
  }
3650
3686
  }
3651
3687
  }
3652
- async apiCall(method, body) {
3688
+ async apiCall(method, body, signal = AbortSignal.timeout(API_TIMEOUT_MS)) {
3653
3689
  const url = `${TELEGRAM_API}/bot${this.token}/${method}`;
3654
3690
  const response = await fetch(url, {
3655
3691
  method: "POST",
3656
3692
  headers: { "Content-Type": "application/json" },
3657
- body: body ? JSON.stringify(body) : void 0
3693
+ body: body ? JSON.stringify(body) : void 0,
3694
+ signal
3658
3695
  });
3659
3696
  if (!response.ok) {
3660
3697
  return { ok: false };
@@ -3787,7 +3824,7 @@ function isModelLoaded() {
3787
3824
  return transcriber !== null;
3788
3825
  }
3789
3826
  async function transcribeVoice(fileUrl) {
3790
- const response = await fetch(fileUrl);
3827
+ const response = await fetch(fileUrl, { signal: AbortSignal.timeout(6e4) });
3791
3828
  if (!response.ok) throw new Error(`Failed to download voice file: ${response.status}`);
3792
3829
  const buffer = new Uint8Array(await response.arrayBuffer());
3793
3830
  const pcm = await decodeOggOpus(buffer);
@@ -4006,7 +4043,7 @@ function createAutoUpdater(config) {
4006
4043
  getContextWindow,
4007
4044
  getDefaultModel,
4008
4045
  getDefaultThinkingLevel,
4009
- getFastModel,
4046
+ getLowestThinkingLevel,
4010
4047
  getMaxThinkingLevel,
4011
4048
  getModel,
4012
4049
  getModelsForProvider,