@anvia/core 0.13.3 → 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.
@@ -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,8 +1,8 @@
1
1
  import {
2
2
  AgentBuilder
3
- } from "../chunk-AWTFWDMY.js";
4
- import "../chunk-J3R7WC2W.js";
5
- import "../chunk-VPRJIWTP.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
8
  import "../chunk-WHHQWT7D.js";
@@ -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,6 +1,6 @@
1
1
  import {
2
2
  PromptRequest
3
- } from "./chunk-VPRJIWTP.js";
3
+ } from "./chunk-HRZV6BQL.js";
4
4
  import {
5
5
  isSkillTool
6
6
  } from "./chunk-YK4WAAS4.js";
@@ -207,4 +207,4 @@ export {
207
207
  Agent,
208
208
  AgentSession
209
209
  };
210
- //# sourceMappingURL=chunk-J3R7WC2W.js.map
210
+ //# sourceMappingURL=chunk-BKKHXEWS.js.map
@@ -1277,6 +1277,155 @@ var ActiveToolObservers = class {
1277
1277
  }
1278
1278
  };
1279
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
+
1280
1429
  // src/request/prompt-request.ts
1281
1430
  var PromptRequest = class _PromptRequest {
1282
1431
  constructor(agent, promptMessage, initialHistory = [], memoryContext = void 0) {
@@ -1301,6 +1450,7 @@ var PromptRequest = class _PromptRequest {
1301
1450
  guardrailDecisions = [];
1302
1451
  concurrency = 1;
1303
1452
  traceOptions;
1453
+ completionRetryOptions;
1304
1454
  requestMiddlewares = [];
1305
1455
  steeringMessages = [];
1306
1456
  runState = "idle";
@@ -1313,6 +1463,10 @@ var PromptRequest = class _PromptRequest {
1313
1463
  this.maxTurnCount = maxTurns;
1314
1464
  return this;
1315
1465
  }
1466
+ withCompletionRetries(options = {}) {
1467
+ this.completionRetryOptions = resolveCompletionRetryOptions(options);
1468
+ return this;
1469
+ }
1316
1470
  withHook(hook) {
1317
1471
  this.activeHook = hook;
1318
1472
  return this;
@@ -1608,26 +1762,46 @@ var PromptRequest = class _PromptRequest {
1608
1762
  const emittedToolCallIds = /* @__PURE__ */ new Set();
1609
1763
  let response;
1610
1764
  try {
1611
- for await (const event of this.agent.model.streamCompletion(request)) {
1612
- if (firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
1613
- firstDeltaMs = Date.now() - generationStartedAt;
1614
- }
1615
- const mapped = accumulator.accept(event);
1616
- if (event.type === "error") {
1617
- throw event.error;
1618
- }
1619
- if (mapped !== void 0) {
1620
- await generationObservers.update?.({ turn: currentTurns, delta: mapped });
1621
- if (mapped.type === "tool_call") {
1622
- 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
+ }
1623
1787
  }
1624
- const shouldBuffer = bufferResponseEvents || bufferOutputDeltas && (mapped.type === "text_delta" || mapped.type === "reasoning_delta");
1625
- if (!shouldBuffer) {
1626
- 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;
1627
1794
  }
1795
+ await this.scheduleCompletionRetry(
1796
+ error,
1797
+ attempt,
1798
+ currentTurns,
1799
+ true,
1800
+ retryOptions,
1801
+ runObservers
1802
+ );
1628
1803
  }
1629
1804
  }
1630
- response = accumulator.response();
1631
1805
  } catch (error) {
1632
1806
  await generationObservers.error({ turn: currentTurns, error });
1633
1807
  await this.runCompletionErrorHook(prompt, error, newMessages);
@@ -1801,9 +1975,28 @@ var PromptRequest = class _PromptRequest {
1801
1975
  this.generationStartArgs(turn, request, providerRequest)
1802
1976
  );
1803
1977
  try {
1804
- const response = await this.agent.model.completion(request);
1805
- await generationObservers.end({ turn, response });
1806
- 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
+ }
1807
2000
  } catch (error) {
1808
2001
  await generationObservers.error({ turn, error });
1809
2002
  throw error;
@@ -1833,6 +2026,37 @@ var PromptRequest = class _PromptRequest {
1833
2026
  }
1834
2027
  return args;
1835
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
+ }
1836
2060
  async executeToolCalls(runId, toolCalls, newMessages, onResult, onStreamEvent, observation) {
1837
2061
  const executor = new ToolCallExecutor(
1838
2062
  this.agent,
@@ -2203,4 +2427,4 @@ export {
2203
2427
  extractRagText,
2204
2428
  PromptRequest
2205
2429
  };
2206
- //# sourceMappingURL=chunk-VPRJIWTP.js.map
2430
+ //# sourceMappingURL=chunk-HRZV6BQL.js.map