@simulacra-ai/core 0.0.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/README.md +232 -0
- package/dist/checkpoints/default-summarization-strategy.d.ts +13 -0
- package/dist/checkpoints/default-summarization-strategy.d.ts.map +1 -0
- package/dist/checkpoints/default-summarization-strategy.js +63 -0
- package/dist/checkpoints/default-summarization-strategy.js.map +1 -0
- package/dist/checkpoints/index.d.ts +3 -0
- package/dist/checkpoints/index.d.ts.map +1 -0
- package/dist/checkpoints/index.js +3 -0
- package/dist/checkpoints/index.js.map +1 -0
- package/dist/checkpoints/types.d.ts +32 -0
- package/dist/checkpoints/types.d.ts.map +1 -0
- package/dist/checkpoints/types.js +2 -0
- package/dist/checkpoints/types.js.map +1 -0
- package/dist/context-transformers/checkpoint-context-transformer.d.ts +13 -0
- package/dist/context-transformers/checkpoint-context-transformer.d.ts.map +1 -0
- package/dist/context-transformers/checkpoint-context-transformer.js +32 -0
- package/dist/context-transformers/checkpoint-context-transformer.js.map +1 -0
- package/dist/context-transformers/composite-context-transformer.d.ts +31 -0
- package/dist/context-transformers/composite-context-transformer.d.ts.map +1 -0
- package/dist/context-transformers/composite-context-transformer.js +41 -0
- package/dist/context-transformers/composite-context-transformer.js.map +1 -0
- package/dist/context-transformers/index.d.ts +6 -0
- package/dist/context-transformers/index.d.ts.map +1 -0
- package/dist/context-transformers/index.js +6 -0
- package/dist/context-transformers/index.js.map +1 -0
- package/dist/context-transformers/noop-context-transformer.d.ts +24 -0
- package/dist/context-transformers/noop-context-transformer.d.ts.map +1 -0
- package/dist/context-transformers/noop-context-transformer.js +26 -0
- package/dist/context-transformers/noop-context-transformer.js.map +1 -0
- package/dist/context-transformers/tool-context-transformer.d.ts +26 -0
- package/dist/context-transformers/tool-context-transformer.d.ts.map +1 -0
- package/dist/context-transformers/tool-context-transformer.js +52 -0
- package/dist/context-transformers/tool-context-transformer.js.map +1 -0
- package/dist/context-transformers/types.d.ts +65 -0
- package/dist/context-transformers/types.d.ts.map +1 -0
- package/dist/context-transformers/types.js +2 -0
- package/dist/context-transformers/types.js.map +1 -0
- package/dist/conversations/conversation.d.ts +146 -0
- package/dist/conversations/conversation.d.ts.map +1 -0
- package/dist/conversations/conversation.js +642 -0
- package/dist/conversations/conversation.js.map +1 -0
- package/dist/conversations/index.d.ts +5 -0
- package/dist/conversations/index.d.ts.map +1 -0
- package/dist/conversations/index.js +5 -0
- package/dist/conversations/index.js.map +1 -0
- package/dist/conversations/stream-listener.d.ts +37 -0
- package/dist/conversations/stream-listener.d.ts.map +1 -0
- package/dist/conversations/stream-listener.js +68 -0
- package/dist/conversations/stream-listener.js.map +1 -0
- package/dist/conversations/token-tracker.d.ts +59 -0
- package/dist/conversations/token-tracker.d.ts.map +1 -0
- package/dist/conversations/token-tracker.js +98 -0
- package/dist/conversations/token-tracker.js.map +1 -0
- package/dist/conversations/types.d.ts +346 -0
- package/dist/conversations/types.d.ts.map +1 -0
- package/dist/conversations/types.js +2 -0
- package/dist/conversations/types.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/policies/composite-policy.d.ts +29 -0
- package/dist/policies/composite-policy.d.ts.map +1 -0
- package/dist/policies/composite-policy.js +77 -0
- package/dist/policies/composite-policy.js.map +1 -0
- package/dist/policies/default-policy.d.ts +7 -0
- package/dist/policies/default-policy.d.ts.map +1 -0
- package/dist/policies/default-policy.js +14 -0
- package/dist/policies/default-policy.js.map +1 -0
- package/dist/policies/index.d.ts +8 -0
- package/dist/policies/index.d.ts.map +1 -0
- package/dist/policies/index.js +8 -0
- package/dist/policies/index.js.map +1 -0
- package/dist/policies/noop-policy.d.ts +21 -0
- package/dist/policies/noop-policy.d.ts.map +1 -0
- package/dist/policies/noop-policy.js +36 -0
- package/dist/policies/noop-policy.js.map +1 -0
- package/dist/policies/rate-limit-policy.d.ts +44 -0
- package/dist/policies/rate-limit-policy.d.ts.map +1 -0
- package/dist/policies/rate-limit-policy.js +98 -0
- package/dist/policies/rate-limit-policy.js.map +1 -0
- package/dist/policies/retry-policy.d.ts +42 -0
- package/dist/policies/retry-policy.d.ts.map +1 -0
- package/dist/policies/retry-policy.js +114 -0
- package/dist/policies/retry-policy.js.map +1 -0
- package/dist/policies/token-limit-policy.d.ts +76 -0
- package/dist/policies/token-limit-policy.d.ts.map +1 -0
- package/dist/policies/token-limit-policy.js +134 -0
- package/dist/policies/token-limit-policy.js.map +1 -0
- package/dist/policies/types.d.ts +49 -0
- package/dist/policies/types.d.ts.map +1 -0
- package/dist/policies/types.js +9 -0
- package/dist/policies/types.js.map +1 -0
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +2 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/types.d.ts +180 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/utils/async.d.ts +125 -0
- package/dist/utils/async.d.ts.map +1 -0
- package/dist/utils/async.js +217 -0
- package/dist/utils/async.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/object.d.ts +80 -0
- package/dist/utils/object.d.ts.map +1 -0
- package/dist/utils/object.js +189 -0
- package/dist/utils/object.js.map +1 -0
- package/dist/utils/types.d.ts +17 -0
- package/dist/utils/types.d.ts.map +1 -0
- package/dist/utils/types.js +2 -0
- package/dist/utils/types.js.map +1 -0
- package/dist/workflows/index.d.ts +4 -0
- package/dist/workflows/index.d.ts.map +1 -0
- package/dist/workflows/index.js +4 -0
- package/dist/workflows/index.js.map +1 -0
- package/dist/workflows/types.d.ts +70 -0
- package/dist/workflows/types.d.ts.map +1 -0
- package/dist/workflows/types.js +2 -0
- package/dist/workflows/types.js.map +1 -0
- package/dist/workflows/workflow-manager.d.ts +74 -0
- package/dist/workflows/workflow-manager.d.ts.map +1 -0
- package/dist/workflows/workflow-manager.js +165 -0
- package/dist/workflows/workflow-manager.js.map +1 -0
- package/dist/workflows/workflow.d.ts +116 -0
- package/dist/workflows/workflow.d.ts.map +1 -0
- package/dist/workflows/workflow.js +351 -0
- package/dist/workflows/workflow.js.map +1 -0
- package/package.json +20 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./checkpoints/index.ts";
|
|
2
|
+
export * from "./conversations/index.ts";
|
|
3
|
+
export * from "./context-transformers/index.ts";
|
|
4
|
+
export * from "./policies/index.ts";
|
|
5
|
+
export * from "./workflows/index.ts";
|
|
6
|
+
export * from "./tools/index.ts";
|
|
7
|
+
export { CancellationToken, CancellationTokenSource, OperationCanceledError, peek_generator, sleep, } from "./utils/async.ts";
|
|
8
|
+
export { deep_merge, undefined_if_empty } from "./utils/object.ts";
|
|
9
|
+
export type { DeepPartial, Prettify } from "./utils/types.ts";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,iCAAiC,CAAC;AAChD,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,OAAO,EACL,iBAAiB,EACjB,uBAAuB,EACvB,sBAAsB,EACtB,cAAc,EACd,KAAK,GACN,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACnE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./checkpoints/index.js";
|
|
2
|
+
export * from "./conversations/index.js";
|
|
3
|
+
export * from "./context-transformers/index.js";
|
|
4
|
+
export * from "./policies/index.js";
|
|
5
|
+
export * from "./workflows/index.js";
|
|
6
|
+
export * from "./tools/index.js";
|
|
7
|
+
export { CancellationToken, CancellationTokenSource, OperationCanceledError, peek_generator, sleep, } from "./utils/async.js";
|
|
8
|
+
export { deep_merge, undefined_if_empty } from "./utils/object.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,iCAAiC,CAAC;AAChD,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,OAAO,EACL,iBAAiB,EACjB,uBAAuB,EACvB,sBAAsB,EACtB,cAAc,EACd,KAAK,GACN,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CancellationToken } from "../utils/async.ts";
|
|
2
|
+
import { Policy, PolicyResult } from "./types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Policy that combines multiple policies into a single execution chain.
|
|
5
|
+
*
|
|
6
|
+
* Policies are applied in the order provided, with each policy wrapping
|
|
7
|
+
* the execution of the next.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CompositePolicy extends Policy {
|
|
10
|
+
#private;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new composite policy.
|
|
13
|
+
*
|
|
14
|
+
* @param policies - The policies to compose. If empty, uses a NoopPolicy.
|
|
15
|
+
*/
|
|
16
|
+
constructor(...policies: Policy[]);
|
|
17
|
+
/**
|
|
18
|
+
* Executes a function with all composed policies applied.
|
|
19
|
+
*
|
|
20
|
+
* @template TResult - The return type of the function.
|
|
21
|
+
* @template TArgs - The argument types of the function.
|
|
22
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
23
|
+
* @param fn - The function to execute.
|
|
24
|
+
* @param args - Arguments to pass to the function.
|
|
25
|
+
* @returns A promise that resolves to the policy result.
|
|
26
|
+
*/
|
|
27
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=composite-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composite-policy.d.ts","sourceRoot":"","sources":["../../src/policies/composite-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;GAKG;AACH,qBAAa,eAAgB,SAAQ,MAAM;;IAGzC;;;;OAIG;gBACS,GAAG,QAAQ,EAAE,MAAM,EAAE;IAKjC;;;;;;;;;OASG;IACG,OAAO,CAAC,OAAO,EAAE,KAAK,SAAS,OAAO,EAAE,EAC5C,kBAAkB,EAAE,iBAAiB,EACrC,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,EACxC,GAAG,IAAI,EAAE,KAAK,GACb,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;CAiDlC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { NoopPolicy } from "./noop-policy.js";
|
|
2
|
+
import { Policy } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Policy that combines multiple policies into a single execution chain.
|
|
5
|
+
*
|
|
6
|
+
* Policies are applied in the order provided, with each policy wrapping
|
|
7
|
+
* the execution of the next.
|
|
8
|
+
*/
|
|
9
|
+
export class CompositePolicy extends Policy {
|
|
10
|
+
#policies;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new composite policy.
|
|
13
|
+
*
|
|
14
|
+
* @param policies - The policies to compose. If empty, uses a NoopPolicy.
|
|
15
|
+
*/
|
|
16
|
+
constructor(...policies) {
|
|
17
|
+
super();
|
|
18
|
+
this.#policies = policies.length > 0 ? policies : [new NoopPolicy()];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Executes a function with all composed policies applied.
|
|
22
|
+
*
|
|
23
|
+
* @template TResult - The return type of the function.
|
|
24
|
+
* @template TArgs - The argument types of the function.
|
|
25
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
26
|
+
* @param fn - The function to execute.
|
|
27
|
+
* @param args - Arguments to pass to the function.
|
|
28
|
+
* @returns A promise that resolves to the policy result.
|
|
29
|
+
*/
|
|
30
|
+
async execute(cancellation_token, fn, ...args) {
|
|
31
|
+
let current_fn = fn;
|
|
32
|
+
const policy_metadata = {};
|
|
33
|
+
const execution_order = [];
|
|
34
|
+
for (let i = this.#policies.length - 1; i >= 0; i--) {
|
|
35
|
+
cancellation_token.throw_if_cancellation_requested();
|
|
36
|
+
const policy = this.#policies[i];
|
|
37
|
+
const policy_name = `${policy.constructor.name}[${i}]`;
|
|
38
|
+
execution_order.push(policy_name);
|
|
39
|
+
const wrapped_fn = current_fn;
|
|
40
|
+
current_fn = async (...wrapped_args) => {
|
|
41
|
+
const policy_result = await policy.execute(cancellation_token, wrapped_fn, ...wrapped_args);
|
|
42
|
+
policy_metadata[policy_name] = policy_result.metadata;
|
|
43
|
+
if (policy_result.result) {
|
|
44
|
+
return policy_result.value;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
throw policy_result.error;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return {
|
|
53
|
+
result: true,
|
|
54
|
+
value: await current_fn(...args),
|
|
55
|
+
metadata: {
|
|
56
|
+
policy: CompositePolicy.name,
|
|
57
|
+
policies: policy_metadata,
|
|
58
|
+
execution_order: [...execution_order].reverse(),
|
|
59
|
+
policy_count: this.#policies.length,
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return {
|
|
65
|
+
result: false,
|
|
66
|
+
error,
|
|
67
|
+
metadata: {
|
|
68
|
+
policy: CompositePolicy.name,
|
|
69
|
+
policies: policy_metadata,
|
|
70
|
+
execution_order: [...execution_order].reverse(),
|
|
71
|
+
policy_count: this.#policies.length,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=composite-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composite-policy.js","sourceRoot":"","sources":["../../src/policies/composite-policy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAgB,MAAM,YAAY,CAAC;AAElD;;;;;GAKG;AACH,MAAM,OAAO,eAAgB,SAAQ,MAAM;IAChC,SAAS,CAAW;IAE7B;;;;OAIG;IACH,YAAY,GAAG,QAAkB;QAC/B,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,UAAU,EAAE,CAAC,CAAC;IACvE,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,OAAO,CACX,kBAAqC,EACrC,EAAwC,EACxC,GAAG,IAAW;QAEd,IAAI,UAAU,GAAG,EAAE,CAAC;QACpB,MAAM,eAAe,GAA4B,EAAE,CAAC;QACpD,MAAM,eAAe,GAAa,EAAE,CAAC;QAErC,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAA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AE,eAAe;oBACzB,eAAe,EAAE,CAAC,GAAG,eAAe,CAAC,CAAC,OAAO,EAAE;oBAC/C,YAAY,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM;iBACpC;aACF,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Policy } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The default policy used when none is passed to Conversation.
|
|
4
|
+
* A composite that includes retry with exponential backoff. Rate and token limits require attach(conversation) so are not in the default.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getDefaultPolicy(): Policy;
|
|
7
|
+
//# sourceMappingURL=default-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-policy.d.ts","sourceRoot":"","sources":["../../src/policies/default-policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAIzC;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,CAQzC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CompositePolicy } from "./composite-policy.js";
|
|
2
|
+
import { RetryPolicy } from "./retry-policy.js";
|
|
3
|
+
/**
|
|
4
|
+
* The default policy used when none is passed to Conversation.
|
|
5
|
+
* A composite that includes retry with exponential backoff. Rate and token limits require attach(conversation) so are not in the default.
|
|
6
|
+
*/
|
|
7
|
+
export function getDefaultPolicy() {
|
|
8
|
+
return new CompositePolicy(new RetryPolicy({
|
|
9
|
+
max_attempts: 3,
|
|
10
|
+
initial_backoff_ms: 1000,
|
|
11
|
+
backoff_factor: 2,
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=default-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-policy.js","sourceRoot":"","sources":["../../src/policies/default-policy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;GAGG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,IAAI,eAAe,CACxB,IAAI,WAAW,CAAC;QACd,YAAY,EAAE,CAAC;QACf,kBAAkB,EAAE,IAAI;QACxB,cAAc,EAAE,CAAC;KAClB,CAAC,CACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./composite-policy.ts";
|
|
2
|
+
export * from "./default-policy.ts";
|
|
3
|
+
export * from "./noop-policy.ts";
|
|
4
|
+
export * from "./rate-limit-policy.ts";
|
|
5
|
+
export * from "./retry-policy.ts";
|
|
6
|
+
export * from "./token-limit-policy.ts";
|
|
7
|
+
export * from "./types.ts";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/policies/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./composite-policy.js";
|
|
2
|
+
export * from "./default-policy.js";
|
|
3
|
+
export * from "./noop-policy.js";
|
|
4
|
+
export * from "./rate-limit-policy.js";
|
|
5
|
+
export * from "./retry-policy.js";
|
|
6
|
+
export * from "./token-limit-policy.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/policies/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,yBAAyB,CAAC;AACxC,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CancellationToken } from "../utils/async.ts";
|
|
2
|
+
import { Policy, PolicyResult } from "./types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Policy that performs no special operations.
|
|
5
|
+
*
|
|
6
|
+
* Simply executes the provided function with cancellation support.
|
|
7
|
+
*/
|
|
8
|
+
export declare class NoopPolicy extends Policy {
|
|
9
|
+
/**
|
|
10
|
+
* Executes a function with no additional policy logic.
|
|
11
|
+
*
|
|
12
|
+
* @template TResult - The return type of the function.
|
|
13
|
+
* @template TArgs - The argument types of the function.
|
|
14
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
15
|
+
* @param fn - The function to execute.
|
|
16
|
+
* @param args - Arguments to pass to the function.
|
|
17
|
+
* @returns A promise that resolves to the policy result.
|
|
18
|
+
*/
|
|
19
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=noop-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop-policy.d.ts","sourceRoot":"","sources":["../../src/policies/noop-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElD;;;;GAIG;AACH,qBAAa,UAAW,SAAQ,MAAM;IACpC;;;;;;;;;OASG;IACG,OAAO,CAAC,OAAO,EAAE,KAAK,SAAS,OAAO,EAAE,EAC5C,kBAAkB,EAAE,iBAAiB,EACrC,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,EACxC,GAAG,IAAI,EAAE,KAAK,GACb,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;CAgBlC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Policy } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Policy that performs no special operations.
|
|
4
|
+
*
|
|
5
|
+
* Simply executes the provided function with cancellation support.
|
|
6
|
+
*/
|
|
7
|
+
export class NoopPolicy extends Policy {
|
|
8
|
+
/**
|
|
9
|
+
* Executes a function with no additional policy logic.
|
|
10
|
+
*
|
|
11
|
+
* @template TResult - The return type of the function.
|
|
12
|
+
* @template TArgs - The argument types of the function.
|
|
13
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
14
|
+
* @param fn - The function to execute.
|
|
15
|
+
* @param args - Arguments to pass to the function.
|
|
16
|
+
* @returns A promise that resolves to the policy result.
|
|
17
|
+
*/
|
|
18
|
+
async execute(cancellation_token, fn, ...args) {
|
|
19
|
+
try {
|
|
20
|
+
cancellation_token.throw_if_cancellation_requested();
|
|
21
|
+
return {
|
|
22
|
+
result: true,
|
|
23
|
+
value: await Promise.race([fn(...args), cancellation_token.await_cancellation()]),
|
|
24
|
+
metadata: { policy: NoopPolicy.name },
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
return {
|
|
29
|
+
result: false,
|
|
30
|
+
error,
|
|
31
|
+
metadata: { policy: NoopPolicy.name },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=noop-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop-policy.js","sourceRoot":"","sources":["../../src/policies/noop-policy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAgB,MAAM,YAAY,CAAC;AAElD;;;;GAIG;AACH,MAAM,OAAO,UAAW,SAAQ,MAAM;IACpC;;;;;;;;;OASG;IACH,KAAK,CAAC,OAAO,CACX,kBAAqC,EACrC,EAAwC,EACxC,GAAG,IAAW;QAEd,IAAI,CAAC;YACH,kBAAkB,CAAC,+BAA+B,EAAE,CAAC;YACrD,OAAO;gBACL,MAAM,EAAE,IAAI;gBACZ,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,EAAE,kBAAkB,CAAC,kBAAkB,EAAE,CAAC,CAAC;gBACjF,QAAQ,EAAE,EAAE,MAAM,EAAE,UAAU,CAAC,IAAI,EAAE;aACtC,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO;gBACL,MAAM,EAAE,KAAK;gBACb,KAAK;gBACL,QAAQ,EAAE,EAAE,MAAM,EAAE,UAAU,CAAC,IAAI,EAAE;aACtC,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Conversation } from "../conversations/index.ts";
|
|
2
|
+
import { CancellationToken } from "../utils/async.ts";
|
|
3
|
+
import { Policy, PolicyResult } from "./types.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for rate limiting.
|
|
6
|
+
*/
|
|
7
|
+
export type RateLimitPolicyOptions = {
|
|
8
|
+
/** Maximum number of requests allowed in the time period. */
|
|
9
|
+
limit: number;
|
|
10
|
+
/** Time period in milliseconds. */
|
|
11
|
+
period_ms: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Policy that enforces rate limits on operations.
|
|
15
|
+
*
|
|
16
|
+
* Tracks request timestamps and delays execution when the rate limit is exceeded.
|
|
17
|
+
*/
|
|
18
|
+
export declare class RateLimitPolicy extends Policy {
|
|
19
|
+
#private;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new rate limit policy.
|
|
22
|
+
*
|
|
23
|
+
* @param options - The rate limiting configuration.
|
|
24
|
+
*/
|
|
25
|
+
constructor(options: RateLimitPolicyOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Attaches this policy to a conversation to track its requests.
|
|
28
|
+
*
|
|
29
|
+
* @param conversation - The conversation to monitor.
|
|
30
|
+
*/
|
|
31
|
+
attach(conversation: Conversation): void;
|
|
32
|
+
/**
|
|
33
|
+
* Executes a function with rate limiting applied.
|
|
34
|
+
*
|
|
35
|
+
* @template TResult - The return type of the function.
|
|
36
|
+
* @template TArgs - The argument types of the function.
|
|
37
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
38
|
+
* @param fn - The function to execute.
|
|
39
|
+
* @param args - Arguments to pass to the function.
|
|
40
|
+
* @returns A promise that resolves to the policy result.
|
|
41
|
+
*/
|
|
42
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=rate-limit-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit-policy.d.ts","sourceRoot":"","sources":["../../src/policies/rate-limit-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAsB,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,iBAAiB,EAAS,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElD;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,6DAA6D;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,mCAAmC;IACnC,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;GAIG;AACH,qBAAa,eAAgB,SAAQ,MAAM;;IAKzC;;;;OAIG;gBACS,OAAO,EAAE,sBAAsB;IAK3C;;;;OAIG;IACH,MAAM,CAAC,YAAY,EAAE,YAAY;IASjC;;;;;;;;;OASG;IACG,OAAO,CAAC,OAAO,EAAE,KAAK,SAAS,OAAO,EAAE,EAC5C,kBAAkB,EAAE,iBAAiB,EACrC,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,EACxC,GAAG,IAAI,EAAE,KAAK,GACb,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;CAsElC"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { sleep } from "../utils/async.js";
|
|
2
|
+
import { Policy } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Policy that enforces rate limits on operations.
|
|
5
|
+
*
|
|
6
|
+
* Tracks request timestamps and delays execution when the rate limit is exceeded.
|
|
7
|
+
*/
|
|
8
|
+
export class RateLimitPolicy extends Policy {
|
|
9
|
+
#options;
|
|
10
|
+
#requests = [];
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new rate limit policy.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The rate limiting configuration.
|
|
15
|
+
*/
|
|
16
|
+
constructor(options) {
|
|
17
|
+
super();
|
|
18
|
+
this.#options = options;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Attaches this policy to a conversation to track its requests.
|
|
22
|
+
*
|
|
23
|
+
* @param conversation - The conversation to monitor.
|
|
24
|
+
*/
|
|
25
|
+
attach(conversation) {
|
|
26
|
+
conversation.on("request_success", this.#on_request_success);
|
|
27
|
+
conversation.on("child_event", this.#on_child_event);
|
|
28
|
+
conversation.once("dispose", () => {
|
|
29
|
+
conversation.off("request_success", this.#on_request_success);
|
|
30
|
+
conversation.off("child_event", this.#on_child_event);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Executes a function with rate limiting applied.
|
|
35
|
+
*
|
|
36
|
+
* @template TResult - The return type of the function.
|
|
37
|
+
* @template TArgs - The argument types of the function.
|
|
38
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
39
|
+
* @param fn - The function to execute.
|
|
40
|
+
* @param args - Arguments to pass to the function.
|
|
41
|
+
* @returns A promise that resolves to the policy result.
|
|
42
|
+
*/
|
|
43
|
+
async execute(cancellation_token, fn, ...args) {
|
|
44
|
+
const now = Date.now();
|
|
45
|
+
this.#requests = this.#requests.filter((timestamp) => now - timestamp < this.#options.period_ms);
|
|
46
|
+
const current_usage = this.#requests.length;
|
|
47
|
+
const metadata = {
|
|
48
|
+
policy: RateLimitPolicy.name,
|
|
49
|
+
limit: this.#options.limit,
|
|
50
|
+
period_ms: this.#options.period_ms,
|
|
51
|
+
current_usage,
|
|
52
|
+
};
|
|
53
|
+
const wait_time = this.#calculate_wait_time(current_usage, this.#options.limit, this.#options.period_ms);
|
|
54
|
+
if (wait_time > 0) {
|
|
55
|
+
metadata.wait_time_ms = wait_time;
|
|
56
|
+
await sleep(wait_time, cancellation_token);
|
|
57
|
+
}
|
|
58
|
+
cancellation_token.throw_if_cancellation_requested();
|
|
59
|
+
try {
|
|
60
|
+
const value = await Promise.race([fn(...args), cancellation_token.await_cancellation()]);
|
|
61
|
+
return {
|
|
62
|
+
result: true,
|
|
63
|
+
value,
|
|
64
|
+
metadata,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
return {
|
|
69
|
+
result: false,
|
|
70
|
+
error: error,
|
|
71
|
+
metadata,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
#calculate_wait_time(current_usage, limit, period_ms) {
|
|
76
|
+
if (current_usage < limit) {
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
const requests_over_limit = current_usage - limit + 1;
|
|
80
|
+
const time_per_request = period_ms / limit;
|
|
81
|
+
return requests_over_limit * time_per_request;
|
|
82
|
+
}
|
|
83
|
+
#on_request_success = () => {
|
|
84
|
+
const now = Date.now();
|
|
85
|
+
this.#requests.push(now);
|
|
86
|
+
};
|
|
87
|
+
#on_child_event = (event_data) => {
|
|
88
|
+
const { event_name, event_args } = event_data;
|
|
89
|
+
if (event_name === "request_success") {
|
|
90
|
+
this.#on_request_success();
|
|
91
|
+
}
|
|
92
|
+
if (event_name === "child_event") {
|
|
93
|
+
const [child_event_data] = event_args;
|
|
94
|
+
this.#on_child_event(child_event_data);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=rate-limit-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit-policy.js","sourceRoot":"","sources":["../../src/policies/rate-limit-policy.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAgB,MAAM,YAAY,CAAC;AAYlD;;;;GAIG;AACH,MAAM,OAAO,eAAgB,SAAQ,MAAM;IAChC,QAAQ,CAAyB;IAE1C,SAAS,GAAa,EAAE,CAAC;IAEzB;;;;OAIG;IACH,YAAY,OAA+B;QACzC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,YAA0B;QAC/B,YAAY,CAAC,EAAE,CAAC,iBAAiB,EAAE,IAAI,CAAC,mBAAmB,CAAC,CAAC;QAC7D,YAAY,CAAC,EAAE,CAAC,aAAa,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;QACrD,YAAY,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE;YAChC,YAAY,CAAC,GAAG,CAAC,iBAAiB,EAAE,IAAI,CAAC,mBAAmB,CAAC,CAAC;YAC9D,YAAY,CAAC,GAAG,CAAC,aAAa,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;QACxD,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,OAAO,CACX,kBAAqC,EACrC,EAAwC,EACxC,GAAG,IAAW;QAEd,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;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B,OAAO,CAAC,CAAC;QACX,CAAC;QACD,MAAM,mBAAmB,GAAG,aAAa,GAAG,KAAK,GAAG,CAAC,CAAC;QACtD,MAAM,gBAAgB,GAAG,SAAS,GAAG,KAAK,CAAC;QAC3C,OAAO,mBAAmB,GAAG,gBAAgB,CAAC;IAChD,CAAC;IAED,mBAAmB,GAAG,GAAG,EAAE;QACzB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC,CAAC;IAEF,eAAe,GAAG,CAAC,UAAgD,EAAE,EAAE;QACrE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,UAAU,CAAC;QAE9C,IAAI,UAAU,KAAK,iBAAiB,EAAE,CAAC;YACrC,IAAI,CAAC,mBAAmB,EAAE,CAAC;QAC7B,CAAC;QAED,IAAI,UAAU,KAAK,aAAa,EAAE,CAAC;YACjC,MAAM,CAAC,gBAAgB,CAAC,GAAG,UAAU,CAAC;YACtC,IAAI,CAAC,eAAe,CAAC,gBAAgB,CAAC,CAAC;QACzC,CAAC;IACH,CAAC,CAAC;CACH"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { CancellationToken } from "../utils/async.ts";
|
|
2
|
+
import { Policy, PolicyErrorResult, PolicyResult } from "./types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for retry behavior.
|
|
5
|
+
*/
|
|
6
|
+
export interface RetryPolicyOptions {
|
|
7
|
+
/** Maximum number of attempts (including the initial attempt). */
|
|
8
|
+
max_attempts: number;
|
|
9
|
+
/** Initial backoff delay in milliseconds. */
|
|
10
|
+
initial_backoff_ms: number;
|
|
11
|
+
/** Factor by which to multiply the backoff after each retry. */
|
|
12
|
+
backoff_factor: number;
|
|
13
|
+
/** Optional function to determine if an error is retryable. */
|
|
14
|
+
retryable?: (result: PolicyErrorResult) => boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare function defaultRetryable(result: PolicyErrorResult): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Policy that retries failed operations with exponential backoff.
|
|
19
|
+
*
|
|
20
|
+
* @template _TResult - The result type of the operation.
|
|
21
|
+
*/
|
|
22
|
+
export declare class RetryPolicy<_TResult> extends Policy {
|
|
23
|
+
#private;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new retry policy.
|
|
26
|
+
*
|
|
27
|
+
* @param options - The retry configuration options.
|
|
28
|
+
*/
|
|
29
|
+
constructor(options: RetryPolicyOptions);
|
|
30
|
+
/**
|
|
31
|
+
* Executes a function with retry logic applied.
|
|
32
|
+
*
|
|
33
|
+
* @template TResult - The return type of the function.
|
|
34
|
+
* @template TArgs - The argument types of the function.
|
|
35
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
36
|
+
* @param fn - The function to execute.
|
|
37
|
+
* @param args - Arguments to pass to the function.
|
|
38
|
+
* @returns A promise that resolves to the policy result.
|
|
39
|
+
*/
|
|
40
|
+
execute<TResult, TArgs extends unknown[]>(cancellation_token: CancellationToken, fn: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<PolicyResult<TResult>>;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=retry-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry-policy.d.ts","sourceRoot":"","sources":["../../src/policies/retry-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAS,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAErE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,kEAAkE;IAClE,YAAY,EAAE,MAAM,CAAC;IACrB,6CAA6C;IAC7C,kBAAkB,EAAE,MAAM,CAAC;IAC3B,gEAAgE;IAChE,cAAc,EAAE,MAAM,CAAC;IACvB,+DAA+D;IAC/D,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,iBAAiB,KAAK,OAAO,CAAC;CACpD;AAiBD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAuCnE;AAED;;;;GAIG;AACH,qBAAa,WAAW,CAAC,QAAQ,CAAE,SAAQ,MAAM;;IAG/C;;;;OAIG;gBACS,OAAO,EAAE,kBAAkB;IAKvC;;;;;;;;;OASG;IACG,OAAO,CAAC,OAAO,EAAE,KAAK,SAAS,OAAO,EAAE,EAC5C,kBAAkB,EAAE,iBAAiB,EACrC,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,EACxC,GAAG,IAAI,EAAE,KAAK,GACb,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;CA0ClC"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { sleep } from "../utils/async.js";
|
|
2
|
+
import { Policy } from "./types.js";
|
|
3
|
+
const RETRYABLE_NETWORK_ERROR_CODES = new Set([
|
|
4
|
+
"ECONNRESET",
|
|
5
|
+
"ECONNREFUSED",
|
|
6
|
+
"ECONNABORTED",
|
|
7
|
+
"ETIMEDOUT",
|
|
8
|
+
"ENETUNREACH",
|
|
9
|
+
"EHOSTUNREACH",
|
|
10
|
+
"EPIPE",
|
|
11
|
+
"EAI_AGAIN",
|
|
12
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
13
|
+
"UND_ERR_SOCKET",
|
|
14
|
+
]);
|
|
15
|
+
const RETRYABLE_HTTP_STATUS_CODES = new Set([408, 429, 500, 502, 503, 504, 529]);
|
|
16
|
+
export function defaultRetryable(result) {
|
|
17
|
+
const error = result.error;
|
|
18
|
+
if (error !== null && error !== undefined && typeof error === "object" && "status" in error) {
|
|
19
|
+
const status = error.status;
|
|
20
|
+
if (typeof status === "number") {
|
|
21
|
+
return RETRYABLE_HTTP_STATUS_CODES.has(status);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (error !== null && error !== undefined && typeof error === "object") {
|
|
25
|
+
const code = error.code;
|
|
26
|
+
if (typeof code === "string" && RETRYABLE_NETWORK_ERROR_CODES.has(code)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
const cause = error.cause;
|
|
30
|
+
if (cause !== null && cause !== undefined && typeof cause === "object") {
|
|
31
|
+
const causeCode = cause.code;
|
|
32
|
+
if (typeof causeCode === "string" && RETRYABLE_NETWORK_ERROR_CODES.has(causeCode)) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (error instanceof Error) {
|
|
38
|
+
const msg = error.message.toLowerCase();
|
|
39
|
+
if (msg.includes("timeout") ||
|
|
40
|
+
msg.includes("econnreset") ||
|
|
41
|
+
msg.includes("econnrefused") ||
|
|
42
|
+
msg.includes("socket hang up") ||
|
|
43
|
+
msg.includes("network error") ||
|
|
44
|
+
msg.includes("fetch failed")) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Policy that retries failed operations with exponential backoff.
|
|
52
|
+
*
|
|
53
|
+
* @template _TResult - The result type of the operation.
|
|
54
|
+
*/
|
|
55
|
+
export class RetryPolicy extends Policy {
|
|
56
|
+
#options;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a new retry policy.
|
|
59
|
+
*
|
|
60
|
+
* @param options - The retry configuration options.
|
|
61
|
+
*/
|
|
62
|
+
constructor(options) {
|
|
63
|
+
super();
|
|
64
|
+
this.#options = { ...options, retryable: options.retryable ?? defaultRetryable };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Executes a function with retry logic applied.
|
|
68
|
+
*
|
|
69
|
+
* @template TResult - The return type of the function.
|
|
70
|
+
* @template TArgs - The argument types of the function.
|
|
71
|
+
* @param cancellation_token - Token for cancelling the operation.
|
|
72
|
+
* @param fn - The function to execute.
|
|
73
|
+
* @param args - Arguments to pass to the function.
|
|
74
|
+
* @returns A promise that resolves to the policy result.
|
|
75
|
+
*/
|
|
76
|
+
async execute(cancellation_token, fn, ...args) {
|
|
77
|
+
let attempt = 1;
|
|
78
|
+
let backoff_ms = this.#options.initial_backoff_ms;
|
|
79
|
+
const metadata = {
|
|
80
|
+
policy: RetryPolicy.name,
|
|
81
|
+
attempts: 0,
|
|
82
|
+
...this.#options,
|
|
83
|
+
};
|
|
84
|
+
while (true) {
|
|
85
|
+
cancellation_token.throw_if_cancellation_requested();
|
|
86
|
+
try {
|
|
87
|
+
const value = await Promise.race([fn(...args), cancellation_token.await_cancellation()]);
|
|
88
|
+
metadata.attempts = attempt;
|
|
89
|
+
return {
|
|
90
|
+
result: true,
|
|
91
|
+
value,
|
|
92
|
+
metadata,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
metadata.attempts = attempt;
|
|
97
|
+
const result = {
|
|
98
|
+
result: false,
|
|
99
|
+
error,
|
|
100
|
+
metadata,
|
|
101
|
+
};
|
|
102
|
+
if (attempt >= this.#options.max_attempts || !this.#options.retryable(result)) {
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
await sleep(backoff_ms, cancellation_token);
|
|
106
|
+
metadata.lastError = error;
|
|
107
|
+
metadata.lastBackoffMs = backoff_ms;
|
|
108
|
+
backoff_ms *= this.#options.backoff_factor;
|
|
109
|
+
attempt++;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=retry-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry-policy.js","sourceRoot":"","sources":["../../src/policies/retry-policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAmC,MAAM,YAAY,CAAC;AAgBrE,MAAM,6BA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