@prestyj/agent 4.3.209 → 4.3.237

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
@@ -26,6 +26,7 @@ __export(index_exports, {
26
26
  isAbortError: () => isAbortError,
27
27
  isBillingError: () => isBillingError,
28
28
  isContextOverflow: () => isContextOverflow,
29
+ isUsageLimitError: () => isUsageLimitError,
29
30
  setStreamDiagnostic: () => setStreamDiagnostic
30
31
  });
31
32
  module.exports = __toCommonJS(index_exports);
@@ -36,7 +37,7 @@ var import_ai2 = require("@prestyj/ai");
36
37
  // src/agent-loop.ts
37
38
  var import_zod = require("zod");
38
39
  var import_ai = require("@prestyj/ai");
39
- var DEFAULT_MAX_TURNS = 200;
40
+ var DEFAULT_MAX_TURNS = 300;
40
41
  var _diagFn = null;
41
42
  function setStreamDiagnostic(fn) {
42
43
  _diagFn = fn;
@@ -99,15 +100,27 @@ function isBillingError(err) {
99
100
  const msg = err.message.toLowerCase();
100
101
  return msg.includes("insufficient balance") || msg.includes("no resource package") || msg.includes("quota exceeded") || msg.includes("billing") || msg.includes("recharge") || msg.includes("subscription plan") || msg.includes("does not yet include access") || msg.includes("token quota") || msg.includes("exceeded_current_quota_error") || msg.includes("check your account balance");
101
102
  }
103
+ function isUsageLimitError(err) {
104
+ if (!(err instanceof Error)) return false;
105
+ return /usage limit reached/i.test(err.message);
106
+ }
102
107
  function isToolPairingError(err) {
103
108
  if (!(err instanceof Error)) return false;
104
109
  const msg = err.message.toLowerCase();
105
110
  return msg.includes("tool_use") && msg.includes("tool_result") || msg.includes("unexpected `tool_use_id`") || msg.includes("tool_use ids found without") || // Moonshot/OpenAI-compatible: "tool call id <id> is not found"
106
111
  msg.includes("tool call id") && msg.includes("is not found");
107
112
  }
113
+ function isThinkingBlockError(err) {
114
+ if (!(err instanceof Error)) return false;
115
+ const msg = err.message.toLowerCase();
116
+ if (!msg.includes("thinking")) return false;
117
+ return msg.includes("cannot be modified") || msg.includes("must remain as they were") || msg.includes("signature") && msg.includes("invalid") || // "Expected `thinking` or `redacted_thinking`, but found `text`"
118
+ msg.includes("expected") && msg.includes("but found");
119
+ }
108
120
  function classifyOverload(err) {
109
121
  if (!(err instanceof Error)) return null;
110
122
  if (isBillingError(err)) return null;
123
+ if (isUsageLimitError(err)) return null;
111
124
  const msg = err.message.toLowerCase();
112
125
  const errorWithStatus = err;
113
126
  const statusCode = typeof errorWithStatus.statusCode === "number" ? errorWithStatus.statusCode : void 0;
@@ -196,6 +209,7 @@ async function* agentLoop(messages, options) {
196
209
  let firstTurn = true;
197
210
  let consecutivePauses = 0;
198
211
  let toolPairingRepaired = false;
212
+ let thinkingBlocksStripped = false;
199
213
  let overloadRetries = 0;
200
214
  let emptyResponseRetries = 0;
201
215
  let stallRetries = 0;
@@ -212,7 +226,7 @@ async function* agentLoop(messages, options) {
212
226
  const OVERLOAD_BASE_DELAY_MS = 2e3;
213
227
  const OVERLOAD_MAX_DELAY_MS = 3e4;
214
228
  const STREAM_FIRST_EVENT_TIMEOUT_MS = 45e3;
215
- const STREAM_IDLE_TIMEOUT_MS = 3e4;
229
+ const STREAM_IDLE_TIMEOUT_MS = 9e4;
216
230
  const STREAM_HARD_TIMEOUT_MS = 9e4;
217
231
  const STREAM_OUTPUT_HARD_TIMEOUT_MS = 3e5;
218
232
  const STREAM_THINKING_IDLE_TIMEOUT_MS = 3e5;
@@ -391,7 +405,10 @@ async function* agentLoop(messages, options) {
391
405
  });
392
406
  }
393
407
  lastEventTime = now;
394
- resetIdleTimer();
408
+ if (idleTimer) {
409
+ clearTimeout(idleTimer);
410
+ idleTimer = null;
411
+ }
395
412
  if (event.type === "text_delta") {
396
413
  yield { type: "text_delta", text: event.text };
397
414
  } else if (event.type === "thinking_delta") {
@@ -433,6 +450,7 @@ async function* agentLoop(messages, options) {
433
450
  };
434
451
  }
435
452
  lastYieldEndTime = Date.now();
453
+ resetIdleTimer();
436
454
  }
437
455
  diag("stream_done", {
438
456
  events: streamEventCount,
@@ -453,6 +471,13 @@ async function* agentLoop(messages, options) {
453
471
  provider: options.provider,
454
472
  model: options.model
455
473
  });
474
+ if (isUsageLimitError(err)) {
475
+ diag("usage_limit_reached", {
476
+ provider: options.provider,
477
+ model: options.model
478
+ });
479
+ throw err;
480
+ }
456
481
  if (isContextOverflow(err)) {
457
482
  const overflowDetails = extractContextOverflowDetails(err);
458
483
  diag("context_overflow_detected", {
@@ -626,6 +651,13 @@ async function* agentLoop(messages, options) {
626
651
  turn--;
627
652
  continue;
628
653
  }
654
+ if (isThinkingBlockError(err) && !thinkingBlocksStripped) {
655
+ thinkingBlocksStripped = true;
656
+ diag("thinking_block_repair", { error: errMsg.slice(0, 200) });
657
+ stripThinkingBlocks(messages);
658
+ turn--;
659
+ continue;
660
+ }
629
661
  if (isAbortError(err) || options.signal?.aborted) {
630
662
  diag("aborted", { turn, provider: options.provider, model: options.model });
631
663
  break;
@@ -1114,6 +1146,24 @@ function repairToolPairingAdjacent(messages) {
1114
1146
  }
1115
1147
  }
1116
1148
  }
1149
+ function stripThinkingBlocks(messages) {
1150
+ for (const msg of messages) {
1151
+ if (msg.role !== "assistant" || !Array.isArray(msg.content)) continue;
1152
+ const next = [];
1153
+ for (const part of msg.content) {
1154
+ if (part.type === "thinking") {
1155
+ if (part.text) next.push({ type: "text", text: part.text });
1156
+ continue;
1157
+ }
1158
+ if (part.type === "raw") {
1159
+ const t = part.data.type;
1160
+ if (t === "thinking" || t === "redacted_thinking") continue;
1161
+ }
1162
+ next.push(part);
1163
+ }
1164
+ msg.content = next;
1165
+ }
1166
+ }
1117
1167
 
1118
1168
  // src/agent.ts
1119
1169
  var AgentStream = class {
@@ -1236,6 +1286,7 @@ var Agent = class {
1236
1286
  isAbortError,
1237
1287
  isBillingError,
1238
1288
  isContextOverflow,
1289
+ isUsageLimitError,
1239
1290
  setStreamDiagnostic
1240
1291
  });
1241
1292
  //# sourceMappingURL=index.cjs.map