@anvia/core 0.13.2 → 0.13.4

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.
Files changed (33) hide show
  1. package/dist/agent/index.d.ts +2 -2
  2. package/dist/agent/index.js +7 -7
  3. package/dist/{agent-3qSfIKIj.d.ts → agent-DJr2ITZY.d.ts} +19 -1
  4. package/dist/{chunk-BCGCTBDN.js → chunk-BKKHXEWS.js} +4 -4
  5. package/dist/{chunk-77D7XBWL.js → chunk-DC2GJDPR.js} +2 -2
  6. package/dist/{chunk-YOXG2K7N.js → chunk-HRZV6BQL.js} +270 -29
  7. package/dist/chunk-HRZV6BQL.js.map +1 -0
  8. package/dist/{chunk-F3C5653H.js → chunk-I7TM7TKX.js} +4 -4
  9. package/dist/{chunk-BHQ5UPSK.js → chunk-IWVNXLGQ.js} +3 -3
  10. package/dist/{chunk-VSGL6LBZ.js → chunk-TPFK5YJG.js} +4 -4
  11. package/dist/{chunk-JIUZWN45.js → chunk-WHHQWT7D.js} +5 -5
  12. package/dist/evals/index.d.ts +1 -1
  13. package/dist/evals/index.js +9 -9
  14. package/dist/extractor/index.d.ts +1 -1
  15. package/dist/extractor/index.js +8 -8
  16. package/dist/index.d.ts +3 -3
  17. package/dist/index.js +8 -8
  18. package/dist/internal/agent.d.ts +1 -1
  19. package/dist/internal/agent.js +6 -6
  20. package/dist/pipeline/index.d.ts +1 -1
  21. package/dist/request/index.d.ts +1 -1
  22. package/dist/request/index.js +5 -5
  23. package/dist/skills/index.js +5 -5
  24. package/dist/tool/index.js +4 -4
  25. package/dist/vector-store/index.js +2 -2
  26. package/package.json +1 -1
  27. package/dist/chunk-YOXG2K7N.js.map +0 -1
  28. /package/dist/{chunk-BCGCTBDN.js.map → chunk-BKKHXEWS.js.map} +0 -0
  29. /package/dist/{chunk-77D7XBWL.js.map → chunk-DC2GJDPR.js.map} +0 -0
  30. /package/dist/{chunk-F3C5653H.js.map → chunk-I7TM7TKX.js.map} +0 -0
  31. /package/dist/{chunk-BHQ5UPSK.js.map → chunk-IWVNXLGQ.js.map} +0 -0
  32. /package/dist/{chunk-VSGL6LBZ.js.map → chunk-TPFK5YJG.js.map} +0 -0
  33. /package/dist/{chunk-JIUZWN45.js.map → chunk-WHHQWT7D.js.map} +0 -0
@@ -1,5 +1,5 @@
1
- import { D as DynamicContextOptions, a as DynamicToolOptions, b as AgentEventStore, c as AgentEventStoreOptions, A as Agent } from '../agent-3qSfIKIj.js';
2
- export { d as AgentEventAppendInput, e as AgentEventRecord, f as AgentEventStoreInclude, g as AgentSession, h as AgentToolOptions } from '../agent-3qSfIKIj.js';
1
+ import { D as DynamicContextOptions, b as DynamicToolOptions, c as AgentEventStore, d as AgentEventStoreOptions, A as Agent } from '../agent-DJr2ITZY.js';
2
+ export { e as AgentEventAppendInput, f as AgentEventRecord, g as AgentEventStoreInclude, h as AgentSession, i as AgentToolOptions } from '../agent-DJr2ITZY.js';
3
3
  import { Z as ZodSchema } from '../zod-schema-C7F4clpm.js';
4
4
  import { C as CompletionModel, f as JsonValue, t as ToolChoice } from '../types-DzdJOf35.js';
5
5
  import { GuardrailPolicyInput } from '../guardrails/index.js';
@@ -1,19 +1,19 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "../chunk-BHQ5UPSK.js";
4
- import "../chunk-BCGCTBDN.js";
5
- import "../chunk-YOXG2K7N.js";
3
+ } from "../chunk-IWVNXLGQ.js";
4
+ import "../chunk-BKKHXEWS.js";
5
+ import "../chunk-HRZV6BQL.js";
6
6
  import "../chunk-YK4WAAS4.js";
7
7
  import "../chunk-XUUY2L2D.js";
8
- import "../chunk-JIUZWN45.js";
9
- import "../chunk-F3C5653H.js";
8
+ import "../chunk-WHHQWT7D.js";
9
+ import "../chunk-I7TM7TKX.js";
10
10
  import "../chunk-2ODTMRHP.js";
11
11
  import "../chunk-M6RPFHG7.js";
12
- import "../chunk-YBBD4HZ3.js";
13
- import "../chunk-OIMLU4SF.js";
14
12
  import "../chunk-YTF65GYU.js";
15
13
  import "../chunk-CNTGKNZ3.js";
16
14
  import "../chunk-WQKHFADH.js";
15
+ import "../chunk-YBBD4HZ3.js";
16
+ import "../chunk-OIMLU4SF.js";
17
17
  import "../chunk-CWUJUSOS.js";
18
18
  export {
19
19
  AgentBuilder
@@ -7,6 +7,20 @@ import { e as ToolApprovalsOptions, T as Tool, A as AnyTool, f as ToolCallContex
7
7
  import { A as AgentMiddleware, e as ToolMiddleware, k as ToolSearchDocument, l as ToolSet } from './middleware-Cc_-sosu.js';
8
8
  import { V as VectorFilter, c as VectorSearchResult, a as VectorSearchIndex } from './types-C708DOhL.js';
9
9
 
10
+ type CompletionRetryContext = {
11
+ error: unknown;
12
+ attempt: number;
13
+ maxAttempts: number;
14
+ turn: number;
15
+ streaming: boolean;
16
+ };
17
+ type CompletionRetryOptions = {
18
+ maxAttempts?: number | undefined;
19
+ initialDelayMs?: number | undefined;
20
+ maxDelayMs?: number | undefined;
21
+ shouldRetry?: ((context: CompletionRetryContext) => boolean) | undefined;
22
+ };
23
+
10
24
  declare class PromptRequest<M extends CompletionModel = CompletionModel> {
11
25
  private readonly agent;
12
26
  private promptMessage;
@@ -19,6 +33,7 @@ declare class PromptRequest<M extends CompletionModel = CompletionModel> {
19
33
  private guardrailDecisions;
20
34
  private concurrency;
21
35
  private traceOptions;
36
+ private completionRetryOptions;
22
37
  private requestMiddlewares;
23
38
  private readonly steeringMessages;
24
39
  private runState;
@@ -28,6 +43,7 @@ declare class PromptRequest<M extends CompletionModel = CompletionModel> {
28
43
  memoryContext?: MemoryContext | undefined;
29
44
  }): PromptRequest<M>;
30
45
  maxTurns(maxTurns: number): this;
46
+ withCompletionRetries(options?: CompletionRetryOptions): this;
31
47
  withHook(hook: PromptHook): this;
32
48
  approvals(options: ToolApprovalsOptions): this;
33
49
  guardrails(policies: GuardrailPolicyInput): this;
@@ -54,6 +70,8 @@ declare class PromptRequest<M extends CompletionModel = CompletionModel> {
54
70
  private runCompletion;
55
71
  private providerTraceRequest;
56
72
  private generationStartArgs;
73
+ private retryOptionsForFailure;
74
+ private scheduleCompletionRetry;
57
75
  private executeToolCalls;
58
76
  private runOutputGuardrailsForResponse;
59
77
  private recordGuardrailDecision;
@@ -209,4 +227,4 @@ declare class AgentSession<M extends CompletionModel = CompletionModel> {
209
227
  clear(): Promise<void>;
210
228
  }
211
229
 
212
- export { Agent as A, type DynamicContextOptions as D, PromptRequest as P, type DynamicToolOptions as a, type AgentEventStore as b, type AgentEventStoreOptions as c, type AgentEventAppendInput as d, type AgentEventRecord as e, type AgentEventStoreInclude as f, AgentSession as g, type AgentToolOptions as h, type AgentEventStoreRegistration as i, type AgentOptions as j, DEFAULT_MAX_TURNS as k, type DynamicContextRegistration as l, type DynamicToolRegistration as m };
230
+ export { Agent as A, type CompletionRetryContext as C, type DynamicContextOptions as D, PromptRequest as P, type CompletionRetryOptions as a, type DynamicToolOptions as b, type AgentEventStore as c, type AgentEventStoreOptions as d, type AgentEventAppendInput as e, type AgentEventRecord as f, type AgentEventStoreInclude as g, AgentSession as h, type AgentToolOptions as i, type AgentEventStoreRegistration as j, type AgentOptions as k, DEFAULT_MAX_TURNS as l, type DynamicContextRegistration as m, type DynamicToolRegistration as n };
@@ -1,15 +1,15 @@
1
1
  import {
2
2
  PromptRequest
3
- } from "./chunk-YOXG2K7N.js";
3
+ } from "./chunk-HRZV6BQL.js";
4
4
  import {
5
5
  isSkillTool
6
6
  } from "./chunk-YK4WAAS4.js";
7
7
  import {
8
8
  ToolSet
9
- } from "./chunk-JIUZWN45.js";
9
+ } from "./chunk-WHHQWT7D.js";
10
10
  import {
11
11
  createTool
12
- } from "./chunk-F3C5653H.js";
12
+ } from "./chunk-I7TM7TKX.js";
13
13
  import {
14
14
  isStreamingCompletionModel
15
15
  } from "./chunk-YTF65GYU.js";
@@ -207,4 +207,4 @@ export {
207
207
  Agent,
208
208
  AgentSession
209
209
  };
210
- //# sourceMappingURL=chunk-BCGCTBDN.js.map
210
+ //# sourceMappingURL=chunk-BKKHXEWS.js.map
@@ -6,7 +6,7 @@ import {
6
6
  } from "./chunk-YK4WAAS4.js";
7
7
  import {
8
8
  createTool
9
- } from "./chunk-F3C5653H.js";
9
+ } from "./chunk-I7TM7TKX.js";
10
10
 
11
11
  // src/skills/instructions.ts
12
12
  function skillInstructions(skills) {
@@ -395,4 +395,4 @@ export {
395
395
  SkillValidationError,
396
396
  skill
397
397
  };
398
- //# sourceMappingURL=chunk-77D7XBWL.js.map
398
+ //# sourceMappingURL=chunk-DC2GJDPR.js.map
@@ -4,14 +4,11 @@ import {
4
4
  import {
5
5
  parseToolArgs,
6
6
  toolResultContentToText
7
- } from "./chunk-JIUZWN45.js";
7
+ } from "./chunk-WHHQWT7D.js";
8
8
  import {
9
9
  runControl,
10
10
  toolCallControl
11
11
  } from "./chunk-2ODTMRHP.js";
12
- import {
13
- mapWithConcurrency
14
- } from "./chunk-OIMLU4SF.js";
15
12
  import {
16
13
  CompletionRequestBuilder,
17
14
  isStreamingCompletionModel
@@ -24,6 +21,9 @@ import {
24
21
  assertCompletionRequestSupported,
25
22
  textFromAssistantContent
26
23
  } from "./chunk-CNTGKNZ3.js";
24
+ import {
25
+ mapWithConcurrency
26
+ } from "./chunk-OIMLU4SF.js";
27
27
  import {
28
28
  appendGuardrailPolicies,
29
29
  hasEnforcedOutputGuardrails,
@@ -360,7 +360,9 @@ var CompletionStreamAccumulator = class {
360
360
  }
361
361
  const toolCall = this.toolCalls.get(part.key);
362
362
  if (toolCall !== void 0) {
363
- choice.push(toolCallContent(toolCall));
363
+ choice.push(
364
+ toolCallContent(toolCall, matchingFinalToolCall(toolCall, this.finalResponse?.choice))
365
+ );
364
366
  }
365
367
  }
366
368
  const response = {
@@ -545,13 +547,14 @@ function reasoningContent(reasoning) {
545
547
  const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
546
548
  return reasoning.id === void 0 ? content : { ...content, id: reasoning.id };
547
549
  }
548
- function toolCallContent(toolCall) {
550
+ function toolCallContent(toolCall, finalToolCall) {
551
+ const argumentsValue = finalToolCall !== void 0 && !isEmptyToolArguments(finalToolCall.function.arguments) ? finalToolCall.function.arguments : parseToolArguments(toolCall.id, toolCall.argumentsText);
549
552
  const content = {
550
553
  type: "tool_call",
551
554
  id: toolCall.id,
552
555
  function: {
553
556
  name: toolCall.name,
554
- arguments: parseJsonValue(toolCall.argumentsText)
557
+ arguments: argumentsValue
555
558
  }
556
559
  };
557
560
  if (toolCall.callId !== void 0) {
@@ -565,6 +568,17 @@ function toolCallContent(toolCall) {
565
568
  }
566
569
  return content;
567
570
  }
571
+ function matchingFinalToolCall(accumulated, finalChoice) {
572
+ const byId = finalChoice?.find(
573
+ (content) => content.type === "tool_call" && content.id === accumulated.id
574
+ );
575
+ if (byId !== void 0 || accumulated.callId === void 0) {
576
+ return byId;
577
+ }
578
+ return finalChoice?.find(
579
+ (content) => content.type === "tool_call" && content.callId === accumulated.callId
580
+ );
581
+ }
568
582
  function mergeFinalToolCall(accumulated, finalToolCall) {
569
583
  const argumentsValue = isEmptyToolArguments(finalToolCall.function.arguments) ? accumulated.function.arguments : finalToolCall.function.arguments;
570
584
  return {
@@ -599,14 +613,16 @@ function isEmptyToolArguments(value) {
599
613
  }
600
614
  return false;
601
615
  }
602
- function parseJsonValue(text) {
616
+ function parseToolArguments(toolCallId, text) {
603
617
  if (text.trim().length === 0) {
604
618
  return {};
605
619
  }
606
620
  try {
607
621
  return JSON.parse(text);
608
622
  } catch {
609
- return text;
623
+ throw new Error(
624
+ `Completion returned tool call "${toolCallId}" with malformed JSON arguments; this indicates invalid provider output or incomplete stream assembly.`
625
+ );
610
626
  }
611
627
  }
612
628
 
@@ -1261,6 +1277,155 @@ var ActiveToolObservers = class {
1261
1277
  }
1262
1278
  };
1263
1279
 
1280
+ // src/request/retry.ts
1281
+ var DEFAULT_MAX_ATTEMPTS = 3;
1282
+ var DEFAULT_INITIAL_DELAY_MS = 100;
1283
+ var DEFAULT_MAX_DELAY_MS = 1e3;
1284
+ var MAX_TIMER_DELAY_MS = 2147483647;
1285
+ var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([408, 409, 425, 429]);
1286
+ var RETRYABLE_NUMERIC_ERROR_CODES = /* @__PURE__ */ new Set([4, 8, 14]);
1287
+ var RETRYABLE_ERROR_NAMES = /* @__PURE__ */ new Set([
1288
+ "APIConnectionError",
1289
+ "APIConnectionTimeoutError",
1290
+ "TimeoutError"
1291
+ ]);
1292
+ var RETRYABLE_ERROR_CODES = /* @__PURE__ */ new Set([
1293
+ "DEADLINE_EXCEEDED",
1294
+ "EAI_AGAIN",
1295
+ "ECONNREFUSED",
1296
+ "ECONNRESET",
1297
+ "EHOSTUNREACH",
1298
+ "ENETDOWN",
1299
+ "ENETUNREACH",
1300
+ "EPIPE",
1301
+ "ETIMEDOUT",
1302
+ "RESOURCE_EXHAUSTED",
1303
+ "UNAVAILABLE",
1304
+ "UND_ERR_CONNECT_TIMEOUT",
1305
+ "UND_ERR_HEADERS_TIMEOUT",
1306
+ "UND_ERR_SOCKET"
1307
+ ]);
1308
+ function resolveCompletionRetryOptions(options) {
1309
+ const maxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
1310
+ const initialDelayMs = options.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS;
1311
+ const maxDelayMs = options.maxDelayMs ?? DEFAULT_MAX_DELAY_MS;
1312
+ if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1) {
1313
+ throw new RangeError("Completion retry maxAttempts must be a positive integer.");
1314
+ }
1315
+ assertDelay(initialDelayMs, "initialDelayMs");
1316
+ assertDelay(maxDelayMs, "maxDelayMs");
1317
+ if (maxDelayMs < initialDelayMs) {
1318
+ throw new RangeError(
1319
+ "Completion retry maxDelayMs must be greater than or equal to initialDelayMs."
1320
+ );
1321
+ }
1322
+ if (options.shouldRetry !== void 0 && typeof options.shouldRetry !== "function") {
1323
+ throw new TypeError("Completion retry shouldRetry must be a function.");
1324
+ }
1325
+ return {
1326
+ maxAttempts,
1327
+ initialDelayMs,
1328
+ maxDelayMs,
1329
+ shouldRetry: options.shouldRetry ?? defaultShouldRetry
1330
+ };
1331
+ }
1332
+ function completionRetryDelayMs(options, failedAttempt) {
1333
+ const exponentialDelay = options.initialDelayMs * 2 ** (failedAttempt - 1);
1334
+ const cappedDelay = Math.min(options.maxDelayMs, exponentialDelay);
1335
+ return Math.random() * cappedDelay;
1336
+ }
1337
+ function completionRetryErrorAttributes(error) {
1338
+ const errors = errorChain(error);
1339
+ const errorName = errors.map((candidate) => stringProperty(candidate, "name")).find((value) => value !== void 0);
1340
+ const statusCode = firstStatusCode(errors);
1341
+ const errorCode = errors.map((candidate) => errorCodeProperty(candidate)).find((value) => value !== void 0);
1342
+ return {
1343
+ errorName,
1344
+ statusCode,
1345
+ errorCode
1346
+ };
1347
+ }
1348
+ async function waitForCompletionRetry(delayMs) {
1349
+ if (delayMs === 0) {
1350
+ return;
1351
+ }
1352
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
1353
+ }
1354
+ function defaultShouldRetry(context) {
1355
+ const errors = errorChain(context.error);
1356
+ if (errors.some((error) => stringProperty(error, "name") === "AbortError")) {
1357
+ return false;
1358
+ }
1359
+ const statusCode = firstStatusCode(errors);
1360
+ if (statusCode !== void 0) {
1361
+ return RETRYABLE_STATUS_CODES.has(statusCode) || statusCode >= 500 && statusCode <= 599;
1362
+ }
1363
+ return errors.some((error) => {
1364
+ const name = stringProperty(error, "name");
1365
+ if (name !== void 0 && RETRYABLE_ERROR_NAMES.has(name)) {
1366
+ return true;
1367
+ }
1368
+ const code = stringProperty(error, "code")?.toUpperCase();
1369
+ if (code !== void 0 && RETRYABLE_ERROR_CODES.has(code)) {
1370
+ return true;
1371
+ }
1372
+ const numericCode = numberProperty(error, "code");
1373
+ return numericCode !== void 0 && RETRYABLE_NUMERIC_ERROR_CODES.has(numericCode);
1374
+ });
1375
+ }
1376
+ function firstStatusCode(errors) {
1377
+ for (const error of errors) {
1378
+ const status = numberProperty(error, "status") ?? numberProperty(error, "statusCode");
1379
+ if (status !== void 0) {
1380
+ return status;
1381
+ }
1382
+ const code = numberProperty(error, "code");
1383
+ if (code !== void 0 && code >= 100 && code <= 599) {
1384
+ return code;
1385
+ }
1386
+ }
1387
+ return void 0;
1388
+ }
1389
+ function errorChain(error) {
1390
+ const errors = [];
1391
+ const seen = /* @__PURE__ */ new Set();
1392
+ let current = error;
1393
+ while (isObject(current) && !seen.has(current)) {
1394
+ seen.add(current);
1395
+ errors.push(current);
1396
+ current = property(current, "cause");
1397
+ }
1398
+ return errors;
1399
+ }
1400
+ function assertDelay(value, name) {
1401
+ if (!Number.isFinite(value) || value < 0 || value > MAX_TIMER_DELAY_MS) {
1402
+ throw new RangeError(
1403
+ `Completion retry ${name} must be between 0 and ${MAX_TIMER_DELAY_MS} milliseconds.`
1404
+ );
1405
+ }
1406
+ }
1407
+ function numberProperty(value, name) {
1408
+ const candidate = property(value, name);
1409
+ return typeof candidate === "number" && Number.isFinite(candidate) ? candidate : void 0;
1410
+ }
1411
+ function stringProperty(value, name) {
1412
+ const candidate = property(value, name);
1413
+ return typeof candidate === "string" && candidate.length > 0 ? candidate : void 0;
1414
+ }
1415
+ function errorCodeProperty(value) {
1416
+ return stringProperty(value, "code") ?? numberProperty(value, "code");
1417
+ }
1418
+ function property(value, name) {
1419
+ try {
1420
+ return value[name];
1421
+ } catch {
1422
+ return void 0;
1423
+ }
1424
+ }
1425
+ function isObject(value) {
1426
+ return typeof value === "object" && value !== null;
1427
+ }
1428
+
1264
1429
  // src/request/prompt-request.ts
1265
1430
  var PromptRequest = class _PromptRequest {
1266
1431
  constructor(agent, promptMessage, initialHistory = [], memoryContext = void 0) {
@@ -1285,6 +1450,7 @@ var PromptRequest = class _PromptRequest {
1285
1450
  guardrailDecisions = [];
1286
1451
  concurrency = 1;
1287
1452
  traceOptions;
1453
+ completionRetryOptions;
1288
1454
  requestMiddlewares = [];
1289
1455
  steeringMessages = [];
1290
1456
  runState = "idle";
@@ -1297,6 +1463,10 @@ var PromptRequest = class _PromptRequest {
1297
1463
  this.maxTurnCount = maxTurns;
1298
1464
  return this;
1299
1465
  }
1466
+ withCompletionRetries(options = {}) {
1467
+ this.completionRetryOptions = resolveCompletionRetryOptions(options);
1468
+ return this;
1469
+ }
1300
1470
  withHook(hook) {
1301
1471
  this.activeHook = hook;
1302
1472
  return this;
@@ -1590,24 +1760,46 @@ var PromptRequest = class _PromptRequest {
1590
1760
  let firstDeltaMs;
1591
1761
  const bufferResponseEvents = this.shouldBufferStreamResponseEvents();
1592
1762
  const emittedToolCallIds = /* @__PURE__ */ new Set();
1763
+ let response;
1593
1764
  try {
1594
- for await (const event of this.agent.model.streamCompletion(request)) {
1595
- if (firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
1596
- firstDeltaMs = Date.now() - generationStartedAt;
1597
- }
1598
- const mapped = accumulator.accept(event);
1599
- if (event.type === "error") {
1600
- throw event.error;
1601
- }
1602
- if (mapped !== void 0) {
1603
- await generationObservers.update?.({ turn: currentTurns, delta: mapped });
1604
- if (mapped.type === "tool_call") {
1605
- emittedToolCallIds.add(mapped.toolCall.id);
1765
+ for (let attempt = 1; ; attempt += 1) {
1766
+ let hasProviderProgress = false;
1767
+ try {
1768
+ for await (const event of this.agent.model.streamCompletion(request)) {
1769
+ if (event.type === "error") {
1770
+ throw event.error;
1771
+ }
1772
+ hasProviderProgress = true;
1773
+ if (firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
1774
+ firstDeltaMs = Date.now() - generationStartedAt;
1775
+ }
1776
+ const mapped = accumulator.accept(event);
1777
+ if (mapped !== void 0) {
1778
+ await generationObservers.update?.({ turn: currentTurns, delta: mapped });
1779
+ if (mapped.type === "tool_call") {
1780
+ emittedToolCallIds.add(mapped.toolCall.id);
1781
+ }
1782
+ const shouldBuffer = bufferResponseEvents || bufferOutputDeltas && (mapped.type === "text_delta" || mapped.type === "reasoning_delta");
1783
+ if (!shouldBuffer) {
1784
+ yield await emit(addTurn(currentTurns, mapped));
1785
+ }
1786
+ }
1606
1787
  }
1607
- const shouldBuffer = bufferResponseEvents || bufferOutputDeltas && (mapped.type === "text_delta" || mapped.type === "reasoning_delta");
1608
- if (!shouldBuffer) {
1609
- yield await emit(addTurn(currentTurns, mapped));
1788
+ response = accumulator.response();
1789
+ break;
1790
+ } catch (error) {
1791
+ const retryOptions = hasProviderProgress ? void 0 : this.retryOptionsForFailure(error, attempt, currentTurns, true);
1792
+ if (retryOptions === void 0) {
1793
+ throw error;
1610
1794
  }
1795
+ await this.scheduleCompletionRetry(
1796
+ error,
1797
+ attempt,
1798
+ currentTurns,
1799
+ true,
1800
+ retryOptions,
1801
+ runObservers
1802
+ );
1611
1803
  }
1612
1804
  }
1613
1805
  } catch (error) {
@@ -1615,7 +1807,6 @@ var PromptRequest = class _PromptRequest {
1615
1807
  await this.runCompletionErrorHook(prompt, error, newMessages);
1616
1808
  throw error;
1617
1809
  }
1618
- let response = accumulator.response();
1619
1810
  const generationEndArgs = {
1620
1811
  turn: currentTurns,
1621
1812
  response
@@ -1784,9 +1975,28 @@ var PromptRequest = class _PromptRequest {
1784
1975
  this.generationStartArgs(turn, request, providerRequest)
1785
1976
  );
1786
1977
  try {
1787
- const response = await this.agent.model.completion(request);
1788
- await generationObservers.end({ turn, response });
1789
- return response;
1978
+ for (let attempt = 1; ; attempt += 1) {
1979
+ let response;
1980
+ try {
1981
+ response = await this.agent.model.completion(request);
1982
+ } catch (error) {
1983
+ const retryOptions = this.retryOptionsForFailure(error, attempt, turn, false);
1984
+ if (retryOptions === void 0) {
1985
+ throw error;
1986
+ }
1987
+ await this.scheduleCompletionRetry(
1988
+ error,
1989
+ attempt,
1990
+ turn,
1991
+ false,
1992
+ retryOptions,
1993
+ runObservers
1994
+ );
1995
+ continue;
1996
+ }
1997
+ await generationObservers.end({ turn, response });
1998
+ return response;
1999
+ }
1790
2000
  } catch (error) {
1791
2001
  await generationObservers.error({ turn, error });
1792
2002
  throw error;
@@ -1816,6 +2026,37 @@ var PromptRequest = class _PromptRequest {
1816
2026
  }
1817
2027
  return args;
1818
2028
  }
2029
+ retryOptionsForFailure(error, attempt, turn, streaming) {
2030
+ const options = this.completionRetryOptions;
2031
+ if (options === void 0 || attempt >= options.maxAttempts) {
2032
+ return void 0;
2033
+ }
2034
+ const shouldRetry = options.shouldRetry({
2035
+ error,
2036
+ attempt,
2037
+ maxAttempts: options.maxAttempts,
2038
+ turn,
2039
+ streaming
2040
+ });
2041
+ return shouldRetry ? options : void 0;
2042
+ }
2043
+ async scheduleCompletionRetry(error, attempt, turn, streaming, options, runObservers) {
2044
+ const delayMs = completionRetryDelayMs(options, attempt);
2045
+ await runObservers.event({
2046
+ name: "completion.retry",
2047
+ level: "WARNING",
2048
+ attributes: {
2049
+ turn,
2050
+ attempt,
2051
+ nextAttempt: attempt + 1,
2052
+ maxAttempts: options.maxAttempts,
2053
+ delayMs,
2054
+ streaming,
2055
+ ...completionRetryErrorAttributes(error)
2056
+ }
2057
+ });
2058
+ await waitForCompletionRetry(delayMs);
2059
+ }
1819
2060
  async executeToolCalls(runId, toolCalls, newMessages, onResult, onStreamEvent, observation) {
1820
2061
  const executor = new ToolCallExecutor(
1821
2062
  this.agent,
@@ -2186,4 +2427,4 @@ export {
2186
2427
  extractRagText,
2187
2428
  PromptRequest
2188
2429
  };
2189
- //# sourceMappingURL=chunk-YOXG2K7N.js.map
2430
+ //# sourceMappingURL=chunk-HRZV6BQL.js.map