@langchain/langgraph-sdk 0.1.10 → 1.0.0
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/CHANGELOG.md +6 -0
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/auth/error.cjs +76 -86
- package/dist/auth/error.cjs.map +1 -0
- package/dist/auth/error.d.cts +13 -0
- package/dist/auth/error.d.cts.map +1 -0
- package/dist/auth/error.d.ts +12 -8
- package/dist/auth/error.d.ts.map +1 -0
- package/dist/auth/error.js +76 -83
- package/dist/auth/error.js.map +1 -0
- package/dist/auth/index.cjs +36 -44
- package/dist/auth/index.cjs.map +1 -0
- package/dist/auth/index.d.cts +31 -0
- package/dist/auth/index.d.cts.map +1 -0
- package/dist/auth/index.d.ts +20 -15
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +35 -39
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/types.d.cts +294 -0
- package/dist/auth/types.d.cts.map +1 -0
- package/dist/auth/types.d.ts +162 -165
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/client.cjs +1108 -1294
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +688 -0
- package/dist/client.d.cts.map +1 -0
- package/dist/client.d.ts +647 -639
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1099 -1281
- package/dist/client.js.map +1 -0
- package/dist/index.cjs +6 -8
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.js +4 -2
- package/dist/logging/index.cjs +30 -31
- package/dist/logging/index.cjs.map +1 -0
- package/dist/logging/index.d.cts +47 -0
- package/dist/logging/index.d.cts.map +1 -0
- package/dist/logging/index.d.ts +20 -16
- package/dist/logging/index.d.ts.map +1 -0
- package/dist/logging/index.js +31 -29
- package/dist/logging/index.js.map +1 -0
- package/dist/react/index.cjs +5 -7
- package/dist/react/index.d.cts +4 -0
- package/dist/react/index.d.ts +4 -3
- package/dist/react/index.js +4 -2
- package/dist/react/stream.cjs +14 -15
- package/dist/react/stream.cjs.map +1 -0
- package/dist/react/stream.custom.cjs +126 -145
- package/dist/react/stream.custom.cjs.map +1 -0
- package/dist/react/stream.custom.d.cts +39 -0
- package/dist/react/stream.custom.d.cts.map +1 -0
- package/dist/react/stream.custom.d.ts +35 -37
- package/dist/react/stream.custom.d.ts.map +1 -0
- package/dist/react/stream.custom.js +122 -137
- package/dist/react/stream.custom.js.map +1 -0
- package/dist/react/stream.d.cts +73 -0
- package/dist/react/stream.d.cts.map +1 -0
- package/dist/react/stream.d.ts +21 -15
- package/dist/react/stream.d.ts.map +1 -0
- package/dist/react/stream.js +12 -11
- package/dist/react/stream.js.map +1 -0
- package/dist/react/stream.lgp.cjs +438 -498
- package/dist/react/stream.lgp.cjs.map +1 -0
- package/dist/react/stream.lgp.js +433 -490
- package/dist/react/stream.lgp.js.map +1 -0
- package/dist/react/thread.cjs +18 -15
- package/dist/react/thread.cjs.map +1 -0
- package/dist/react/thread.js +19 -13
- package/dist/react/thread.js.map +1 -0
- package/dist/react/types.d.cts +355 -0
- package/dist/react/types.d.cts.map +1 -0
- package/dist/react/types.d.ts +333 -329
- package/dist/react/types.d.ts.map +1 -0
- package/dist/react-ui/client.cjs +123 -184
- package/dist/react-ui/client.cjs.map +1 -0
- package/dist/react-ui/client.d.cts +90 -0
- package/dist/react-ui/client.d.cts.map +1 -0
- package/dist/react-ui/client.d.ts +67 -51
- package/dist/react-ui/client.d.ts.map +1 -0
- package/dist/react-ui/client.js +118 -143
- package/dist/react-ui/client.js.map +1 -0
- package/dist/react-ui/index.cjs +14 -13
- package/dist/react-ui/index.cjs.map +1 -0
- package/dist/react-ui/index.d.cts +3 -0
- package/dist/react-ui/index.d.ts +3 -2
- package/dist/react-ui/index.js +8 -3
- package/dist/react-ui/index.js.map +1 -0
- package/dist/react-ui/server/index.cjs +5 -7
- package/dist/react-ui/server/index.d.cts +3 -0
- package/dist/react-ui/server/index.d.ts +3 -2
- package/dist/react-ui/server/index.js +4 -2
- package/dist/react-ui/server/server.cjs +53 -44
- package/dist/react-ui/server/server.cjs.map +1 -0
- package/dist/react-ui/server/server.d.cts +55 -0
- package/dist/react-ui/server/server.d.cts.map +1 -0
- package/dist/react-ui/server/server.d.ts +43 -39
- package/dist/react-ui/server/server.d.ts.map +1 -0
- package/dist/react-ui/server/server.js +54 -42
- package/dist/react-ui/server/server.js.map +1 -0
- package/dist/react-ui/types.cjs +32 -36
- package/dist/react-ui/types.cjs.map +1 -0
- package/dist/react-ui/types.d.cts +25 -0
- package/dist/react-ui/types.d.cts.map +1 -0
- package/dist/react-ui/types.d.ts +23 -19
- package/dist/react-ui/types.d.ts.map +1 -0
- package/dist/react-ui/types.js +32 -34
- package/dist/react-ui/types.js.map +1 -0
- package/dist/schema.d.cts +277 -0
- package/dist/schema.d.cts.map +1 -0
- package/dist/schema.d.ts +249 -242
- package/dist/schema.d.ts.map +1 -0
- package/dist/singletons/fetch.cjs +16 -20
- package/dist/singletons/fetch.cjs.map +1 -0
- package/dist/singletons/fetch.d.cts +14 -0
- package/dist/singletons/fetch.d.cts.map +1 -0
- package/dist/singletons/fetch.d.ts +5 -2
- package/dist/singletons/fetch.d.ts.map +1 -0
- package/dist/singletons/fetch.js +16 -17
- package/dist/singletons/fetch.js.map +1 -0
- package/dist/types.d.cts +187 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +178 -173
- package/dist/types.d.ts.map +1 -0
- package/dist/types.messages.d.cts +91 -0
- package/dist/types.messages.d.cts.map +1 -0
- package/dist/types.messages.d.ts +71 -66
- package/dist/types.messages.d.ts.map +1 -0
- package/dist/types.stream.d.cts +235 -0
- package/dist/types.stream.d.cts.map +1 -0
- package/dist/types.stream.d.ts +149 -142
- package/dist/types.stream.d.ts.map +1 -0
- package/dist/ui/branching.cjs +145 -141
- package/dist/ui/branching.cjs.map +1 -0
- package/dist/ui/branching.d.cts +22 -0
- package/dist/ui/branching.d.cts.map +1 -0
- package/dist/ui/branching.d.ts +17 -30
- package/dist/ui/branching.d.ts.map +1 -0
- package/dist/ui/branching.js +147 -139
- package/dist/ui/branching.js.map +1 -0
- package/dist/ui/errors.cjs +14 -12
- package/dist/ui/errors.cjs.map +1 -0
- package/dist/ui/errors.js +14 -9
- package/dist/ui/errors.js.map +1 -0
- package/dist/ui/manager.cjs +156 -232
- package/dist/ui/manager.cjs.map +1 -0
- package/dist/ui/manager.js +156 -228
- package/dist/ui/manager.js.map +1 -0
- package/dist/ui/messages.cjs +55 -69
- package/dist/ui/messages.cjs.map +1 -0
- package/dist/ui/messages.js +55 -66
- package/dist/ui/messages.js.map +1 -0
- package/dist/ui/utils.cjs +10 -10
- package/dist/ui/utils.cjs.map +1 -0
- package/dist/ui/utils.js +10 -8
- package/dist/ui/utils.js.map +1 -0
- package/dist/utils/async_caller.cjs +103 -185
- package/dist/utils/async_caller.cjs.map +1 -0
- package/dist/utils/async_caller.d.cts +53 -0
- package/dist/utils/async_caller.d.cts.map +1 -0
- package/dist/utils/async_caller.d.ts +36 -31
- package/dist/utils/async_caller.d.ts.map +1 -0
- package/dist/utils/async_caller.js +101 -177
- package/dist/utils/async_caller.js.map +1 -0
- package/dist/utils/env.cjs +11 -13
- package/dist/utils/env.cjs.map +1 -0
- package/dist/utils/env.js +11 -11
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/signals.cjs +18 -19
- package/dist/utils/signals.cjs.map +1 -0
- package/dist/utils/signals.js +18 -17
- package/dist/utils/signals.js.map +1 -0
- package/dist/utils/sse.cjs +110 -140
- package/dist/utils/sse.cjs.map +1 -0
- package/dist/utils/sse.js +110 -138
- package/dist/utils/sse.js.map +1 -0
- package/dist/utils/stream.cjs +90 -114
- package/dist/utils/stream.cjs.map +1 -0
- package/dist/utils/stream.js +90 -111
- package/dist/utils/stream.js.map +1 -0
- package/package.json +72 -80
- package/auth.cjs +0 -1
- package/auth.d.cts +0 -1
- package/auth.d.ts +0 -1
- package/auth.js +0 -1
- package/client.cjs +0 -1
- package/client.d.cts +0 -1
- package/client.d.ts +0 -1
- package/client.js +0 -1
- package/dist/auth/types.cjs +0 -2
- package/dist/auth/types.js +0 -1
- package/dist/react/stream.lgp.d.ts +0 -7
- package/dist/react/thread.d.ts +0 -4
- package/dist/react/types.cjs +0 -2
- package/dist/react/types.js +0 -1
- package/dist/schema.cjs +0 -2
- package/dist/schema.js +0 -1
- package/dist/types.cjs +0 -2
- package/dist/types.js +0 -1
- package/dist/types.messages.cjs +0 -2
- package/dist/types.messages.js +0 -1
- package/dist/types.stream.cjs +0 -2
- package/dist/types.stream.js +0 -1
- package/dist/ui/branching.test.cjs +0 -370
- package/dist/ui/branching.test.d.ts +0 -1
- package/dist/ui/branching.test.js +0 -368
- package/dist/ui/errors.d.ts +0 -12
- package/dist/ui/manager.d.ts +0 -87
- package/dist/ui/messages.d.ts +0 -18
- package/dist/ui/utils.d.ts +0 -2
- package/dist/utils/env.d.ts +0 -1
- package/dist/utils/signals.d.ts +0 -1
- package/dist/utils/sse.d.ts +0 -8
- package/dist/utils/stream.d.ts +0 -13
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/logging.cjs +0 -1
- package/logging.d.cts +0 -1
- package/logging.d.ts +0 -1
- package/logging.js +0 -1
- package/react-ui/server.cjs +0 -1
- package/react-ui/server.d.cts +0 -1
- package/react-ui/server.d.ts +0 -1
- package/react-ui/server.js +0 -1
- package/react-ui.cjs +0 -1
- package/react-ui.d.cts +0 -1
- package/react-ui.d.ts +0 -1
- package/react-ui.js +0 -1
- package/react.cjs +0 -1
- package/react.d.cts +0 -1
- package/react.d.ts +0 -1
- package/react.js +0 -1
package/dist/ui/messages.js
CHANGED
|
@@ -1,71 +1,60 @@
|
|
|
1
|
-
import { RemoveMessage,
|
|
1
|
+
import { RemoveMessage, coerceMessageLikeToMessage, convertToChunk, isBaseMessageChunk } from "@langchain/core/messages";
|
|
2
|
+
|
|
3
|
+
//#region src/ui/messages.ts
|
|
2
4
|
function tryConvertToChunk(message) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
5
|
+
try {
|
|
6
|
+
return convertToChunk(message);
|
|
7
|
+
} catch {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
9
10
|
}
|
|
10
11
|
function tryCoerceMessageLikeToMessage(message) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
if (message.type === "remove" && message.id != null) return new RemoveMessage({
|
|
13
|
+
...message,
|
|
14
|
+
id: message.id
|
|
15
|
+
});
|
|
16
|
+
return coerceMessageLikeToMessage(message);
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
clear() {
|
|
56
|
-
this.chunks = {};
|
|
57
|
-
}
|
|
58
|
-
get(id, defaultIndex) {
|
|
59
|
-
if (id == null)
|
|
60
|
-
return null;
|
|
61
|
-
if (this.chunks[id] == null)
|
|
62
|
-
return null;
|
|
63
|
-
if (defaultIndex != null)
|
|
64
|
-
this.chunks[id].index ??= defaultIndex;
|
|
65
|
-
return this.chunks[id];
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
export const toMessageDict = (chunk) => {
|
|
69
|
-
const { type, data } = chunk.toDict();
|
|
70
|
-
return { ...data, type };
|
|
18
|
+
var MessageTupleManager = class {
|
|
19
|
+
chunks = {};
|
|
20
|
+
constructor() {
|
|
21
|
+
this.chunks = {};
|
|
22
|
+
}
|
|
23
|
+
add(serialized, metadata) {
|
|
24
|
+
if (serialized.type.endsWith("MessageChunk")) serialized.type = serialized.type.slice(0, -12).toLowerCase();
|
|
25
|
+
const message = tryCoerceMessageLikeToMessage(serialized);
|
|
26
|
+
const chunk = tryConvertToChunk(message);
|
|
27
|
+
const { id } = chunk ?? message;
|
|
28
|
+
if (!id) {
|
|
29
|
+
console.warn("No message ID found for chunk, ignoring in state", serialized);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
this.chunks[id] ??= {};
|
|
33
|
+
this.chunks[id].metadata = metadata ?? this.chunks[id].metadata;
|
|
34
|
+
if (chunk) {
|
|
35
|
+
const prev = this.chunks[id].chunk;
|
|
36
|
+
this.chunks[id].chunk = (isBaseMessageChunk(prev) ? prev : null)?.concat(chunk) ?? chunk;
|
|
37
|
+
} else this.chunks[id].chunk = message;
|
|
38
|
+
return id;
|
|
39
|
+
}
|
|
40
|
+
clear() {
|
|
41
|
+
this.chunks = {};
|
|
42
|
+
}
|
|
43
|
+
get(id, defaultIndex) {
|
|
44
|
+
if (id == null) return null;
|
|
45
|
+
if (this.chunks[id] == null) return null;
|
|
46
|
+
if (defaultIndex != null) this.chunks[id].index ??= defaultIndex;
|
|
47
|
+
return this.chunks[id];
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const toMessageDict = (chunk) => {
|
|
51
|
+
const { type, data } = chunk.toDict();
|
|
52
|
+
return {
|
|
53
|
+
...data,
|
|
54
|
+
type
|
|
55
|
+
};
|
|
71
56
|
};
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
export { MessageTupleManager, toMessageDict };
|
|
60
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","names":[],"sources":["../../src/ui/messages.ts"],"sourcesContent":["import {\n type BaseMessage,\n type BaseMessageChunk,\n RemoveMessage,\n convertToChunk,\n coerceMessageLikeToMessage,\n isBaseMessageChunk,\n} from \"@langchain/core/messages\";\n\nimport type { Message } from \"../types.messages.js\";\n\nfunction tryConvertToChunk(message: BaseMessage): BaseMessageChunk | null {\n try {\n return convertToChunk(message);\n } catch {\n return null;\n }\n}\n\nfunction tryCoerceMessageLikeToMessage(message: Message): BaseMessage {\n // TODO: this is unnecessary with https://github.com/langchain-ai/langchainjs/pull/8941\n if (message.type === \"remove\" && message.id != null) {\n return new RemoveMessage({ ...message, id: message.id });\n }\n\n return coerceMessageLikeToMessage(message);\n}\n\nexport class MessageTupleManager {\n chunks: Record<\n string,\n {\n chunk?: BaseMessageChunk | BaseMessage;\n metadata?: Record<string, unknown>;\n index?: number;\n }\n > = {};\n\n constructor() {\n this.chunks = {};\n }\n\n add(\n serialized: Message,\n metadata: Record<string, unknown> | undefined\n ): string | null {\n // TODO: this is sometimes sent from the API\n // figure out how to prevent this or move this to LC.js\n if (serialized.type.endsWith(\"MessageChunk\")) {\n // eslint-disable-next-line no-param-reassign\n serialized.type = serialized.type\n .slice(0, -\"MessageChunk\".length)\n .toLowerCase() as Message[\"type\"];\n }\n\n const message = tryCoerceMessageLikeToMessage(serialized);\n const chunk = tryConvertToChunk(message);\n\n const { id } = chunk ?? message;\n if (!id) {\n console.warn(\n \"No message ID found for chunk, ignoring in state\",\n serialized\n );\n return null;\n }\n\n this.chunks[id] ??= {};\n this.chunks[id].metadata = metadata ?? this.chunks[id].metadata;\n if (chunk) {\n const prev = this.chunks[id].chunk;\n this.chunks[id].chunk =\n (isBaseMessageChunk(prev) ? prev : null)?.concat(chunk) ?? chunk;\n } else {\n this.chunks[id].chunk = message;\n }\n\n return id;\n }\n\n clear() {\n this.chunks = {};\n }\n\n get(id: string | null | undefined, defaultIndex?: number) {\n if (id == null) return null;\n if (this.chunks[id] == null) return null;\n if (defaultIndex != null) this.chunks[id].index ??= defaultIndex;\n return this.chunks[id];\n }\n}\n\nexport const toMessageDict = (chunk: BaseMessage): Message => {\n const { type, data } = chunk.toDict();\n return { ...data, type } as Message;\n};\n"],"mappings":";;;AAWA,SAAS,kBAAkB,SAA+C;AACxE,KAAI;AACF,SAAO,eAAe;SAChB;AACN,SAAO;;;AAIX,SAAS,8BAA8B,SAA+B;AAEpE,KAAI,QAAQ,SAAS,YAAY,QAAQ,MAAM,KAC7C,QAAO,IAAI,cAAc;EAAE,GAAG;EAAS,IAAI,QAAQ;;AAGrD,QAAO,2BAA2B;;AAGpC,IAAa,sBAAb,MAAiC;CAC/B,SAOI;CAEJ,cAAc;AACZ,OAAK,SAAS;;CAGhB,IACE,YACA,UACe;AAGf,MAAI,WAAW,KAAK,SAAS,gBAE3B,YAAW,OAAO,WAAW,KAC1B,MAAM,GAAG,KACT;EAGL,MAAM,UAAU,8BAA8B;EAC9C,MAAM,QAAQ,kBAAkB;EAEhC,MAAM,EAAE,OAAO,SAAS;AACxB,MAAI,CAAC,IAAI;AACP,WAAQ,KACN,oDACA;AAEF,UAAO;;AAGT,OAAK,OAAO,QAAQ;AACpB,OAAK,OAAO,IAAI,WAAW,YAAY,KAAK,OAAO,IAAI;AACvD,MAAI,OAAO;GACT,MAAM,OAAO,KAAK,OAAO,IAAI;AAC7B,QAAK,OAAO,IAAI,SACb,mBAAmB,QAAQ,OAAO,OAAO,OAAO,UAAU;QAE7D,MAAK,OAAO,IAAI,QAAQ;AAG1B,SAAO;;CAGT,QAAQ;AACN,OAAK,SAAS;;CAGhB,IAAI,IAA+B,cAAuB;AACxD,MAAI,MAAM,KAAM,QAAO;AACvB,MAAI,KAAK,OAAO,OAAO,KAAM,QAAO;AACpC,MAAI,gBAAgB,KAAM,MAAK,OAAO,IAAI,UAAU;AACpD,SAAO,KAAK,OAAO;;;AAIvB,MAAa,iBAAiB,UAAgC;CAC5D,MAAM,EAAE,MAAM,SAAS,MAAM;AAC7B,QAAO;EAAE,GAAG;EAAM"}
|
package/dist/ui/utils.cjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.unique = unique;
|
|
4
|
-
exports.findLast = findLast;
|
|
1
|
+
|
|
2
|
+
//#region src/ui/utils.ts
|
|
5
3
|
function unique(array) {
|
|
6
|
-
|
|
4
|
+
return [...new Set(array)];
|
|
7
5
|
}
|
|
8
6
|
function findLast(array, predicate) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return array[i];
|
|
12
|
-
}
|
|
13
|
-
return undefined;
|
|
7
|
+
for (let i = array.length - 1; i >= 0; i -= 1) if (predicate(array[i])) return array[i];
|
|
8
|
+
return void 0;
|
|
14
9
|
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
exports.findLast = findLast;
|
|
13
|
+
exports.unique = unique;
|
|
14
|
+
//# sourceMappingURL=utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs","names":[],"sources":["../../src/ui/utils.ts"],"sourcesContent":["export function unique<T>(array: T[]) {\n return [...new Set(array)] as T[];\n}\n\nexport function findLast<T>(array: T[], predicate: (item: T) => boolean) {\n for (let i = array.length - 1; i >= 0; i -= 1) {\n if (predicate(array[i])) return array[i];\n }\n return undefined;\n}\n"],"mappings":";;AAAA,SAAgB,OAAU,OAAY;AACpC,QAAO,CAAC,GAAG,IAAI,IAAI;;AAGrB,SAAgB,SAAY,OAAY,WAAiC;AACvE,MAAK,IAAI,IAAI,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK,EAC1C,KAAI,UAAU,MAAM,IAAK,QAAO,MAAM;AAExC,QAAO"}
|
package/dist/ui/utils.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/ui/utils.ts
|
|
2
|
+
function unique(array) {
|
|
3
|
+
return [...new Set(array)];
|
|
3
4
|
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return array[i];
|
|
8
|
-
}
|
|
9
|
-
return undefined;
|
|
5
|
+
function findLast(array, predicate) {
|
|
6
|
+
for (let i = array.length - 1; i >= 0; i -= 1) if (predicate(array[i])) return array[i];
|
|
7
|
+
return void 0;
|
|
10
8
|
}
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { findLast, unique };
|
|
12
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":[],"sources":["../../src/ui/utils.ts"],"sourcesContent":["export function unique<T>(array: T[]) {\n return [...new Set(array)] as T[];\n}\n\nexport function findLast<T>(array: T[], predicate: (item: T) => boolean) {\n for (let i = array.length - 1; i >= 0; i -= 1) {\n if (predicate(array[i])) return array[i];\n }\n return undefined;\n}\n"],"mappings":";AAAA,SAAgB,OAAU,OAAY;AACpC,QAAO,CAAC,GAAG,IAAI,IAAI;;AAGrB,SAAgB,SAAY,OAAY,WAAiC;AACvE,MAAK,IAAI,IAAI,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK,EAC1C,KAAI,UAAU,MAAM,IAAK,QAAO,MAAM;AAExC,QAAO"}
|
|
@@ -1,193 +1,111 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const p_retry_1 = __importDefault(require("p-retry"));
|
|
8
|
-
const p_queue_1 = __importDefault(require("p-queue"));
|
|
9
|
-
const fetch_js_1 = require("../singletons/fetch.cjs");
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_fetch = require('../singletons/fetch.cjs');
|
|
3
|
+
const p_retry = require_rolldown_runtime.__toESM(require("p-retry"));
|
|
4
|
+
const p_queue = require_rolldown_runtime.__toESM(require("p-queue"));
|
|
5
|
+
|
|
6
|
+
//#region src/utils/async_caller.ts
|
|
10
7
|
const STATUS_NO_RETRY = [
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
8
|
+
400,
|
|
9
|
+
401,
|
|
10
|
+
402,
|
|
11
|
+
403,
|
|
12
|
+
404,
|
|
13
|
+
405,
|
|
14
|
+
406,
|
|
15
|
+
407,
|
|
16
|
+
408,
|
|
17
|
+
409,
|
|
18
|
+
422
|
|
22
19
|
];
|
|
23
20
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
* Do not rely on globalThis.Response, rather just
|
|
22
|
+
* do duck typing
|
|
23
|
+
*/
|
|
27
24
|
function isResponse(x) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return "status" in x && "statusText" in x && "text" in x;
|
|
25
|
+
if (x == null || typeof x !== "object") return false;
|
|
26
|
+
return "status" in x && "statusText" in x && "text" in x;
|
|
31
27
|
}
|
|
32
28
|
/**
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
class HTTPError extends Error {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
writable: true,
|
|
54
|
-
value: void 0
|
|
55
|
-
});
|
|
56
|
-
this.status = status;
|
|
57
|
-
this.text = message;
|
|
58
|
-
this.response = response;
|
|
59
|
-
}
|
|
60
|
-
static async fromResponse(response, options) {
|
|
61
|
-
try {
|
|
62
|
-
return new HTTPError(response.status, await response.text(), options?.includeResponse ? response : undefined);
|
|
63
|
-
}
|
|
64
|
-
catch {
|
|
65
|
-
return new HTTPError(response.status, response.statusText, options?.includeResponse ? response : undefined);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
29
|
+
* Utility error to properly handle failed requests
|
|
30
|
+
*/
|
|
31
|
+
var HTTPError = class HTTPError extends Error {
|
|
32
|
+
status;
|
|
33
|
+
text;
|
|
34
|
+
response;
|
|
35
|
+
constructor(status, message, response) {
|
|
36
|
+
super(`HTTP ${status}: ${message}`);
|
|
37
|
+
this.status = status;
|
|
38
|
+
this.text = message;
|
|
39
|
+
this.response = response;
|
|
40
|
+
}
|
|
41
|
+
static async fromResponse(response, options) {
|
|
42
|
+
try {
|
|
43
|
+
return new HTTPError(response.status, await response.text(), options?.includeResponse ? response : void 0);
|
|
44
|
+
} catch {
|
|
45
|
+
return new HTTPError(response.status, response.statusText, options?.includeResponse ? response : void 0);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
69
49
|
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
call(callable, ...args) {
|
|
131
|
-
const { onFailedResponseHook } = this;
|
|
132
|
-
return this.queue.add(() => (0, p_retry_1.default)(() => callable(...args).catch(async (error) => {
|
|
133
|
-
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
134
|
-
if (error instanceof Error) {
|
|
135
|
-
throw error;
|
|
136
|
-
}
|
|
137
|
-
else if (isResponse(error)) {
|
|
138
|
-
throw await HTTPError.fromResponse(error, {
|
|
139
|
-
includeResponse: !!onFailedResponseHook,
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
throw new Error(error);
|
|
144
|
-
}
|
|
145
|
-
}), {
|
|
146
|
-
async onFailedAttempt(error) {
|
|
147
|
-
if (error.message.startsWith("Cancel") ||
|
|
148
|
-
error.message.startsWith("TimeoutError") ||
|
|
149
|
-
error.message.startsWith("AbortError")) {
|
|
150
|
-
throw error;
|
|
151
|
-
}
|
|
152
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
-
if (error?.code === "ECONNABORTED") {
|
|
154
|
-
throw error;
|
|
155
|
-
}
|
|
156
|
-
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
157
|
-
if (error instanceof HTTPError) {
|
|
158
|
-
if (STATUS_NO_RETRY.includes(error.status)) {
|
|
159
|
-
throw error;
|
|
160
|
-
}
|
|
161
|
-
if (onFailedResponseHook && error.response) {
|
|
162
|
-
await onFailedResponseHook(error.response);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
// If needed we can change some of the defaults here,
|
|
167
|
-
// but they're quite sensible.
|
|
168
|
-
retries: this.maxRetries,
|
|
169
|
-
randomize: true,
|
|
170
|
-
}), { throwOnTimeout: true });
|
|
171
|
-
}
|
|
172
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
173
|
-
callWithOptions(options, callable, ...args) {
|
|
174
|
-
// Note this doesn't cancel the underlying request,
|
|
175
|
-
// when available prefer to use the signal option of the underlying call
|
|
176
|
-
if (options.signal) {
|
|
177
|
-
return Promise.race([
|
|
178
|
-
this.call(callable, ...args),
|
|
179
|
-
new Promise((_, reject) => {
|
|
180
|
-
options.signal?.addEventListener("abort", () => {
|
|
181
|
-
reject(new Error("AbortError"));
|
|
182
|
-
});
|
|
183
|
-
}),
|
|
184
|
-
]);
|
|
185
|
-
}
|
|
186
|
-
return this.call(callable, ...args);
|
|
187
|
-
}
|
|
188
|
-
fetch(...args) {
|
|
189
|
-
const fetchFn = this.customFetch ?? (0, fetch_js_1._getFetchImplementation)();
|
|
190
|
-
return this.call(() => fetchFn(...args).then((res) => (res.ok ? res : Promise.reject(res))));
|
|
191
|
-
}
|
|
192
|
-
}
|
|
50
|
+
* A class that can be used to make async calls with concurrency and retry logic.
|
|
51
|
+
*
|
|
52
|
+
* This is useful for making calls to any kind of "expensive" external resource,
|
|
53
|
+
* be it because it's rate-limited, subject to network issues, etc.
|
|
54
|
+
*
|
|
55
|
+
* Concurrent calls are limited by the `maxConcurrency` parameter, which defaults
|
|
56
|
+
* to `Infinity`. This means that by default, all calls will be made in parallel.
|
|
57
|
+
*
|
|
58
|
+
* Retries are limited by the `maxRetries` parameter, which defaults to 5. This
|
|
59
|
+
* means that by default, each call will be retried up to 5 times, with an
|
|
60
|
+
* exponential backoff between each attempt.
|
|
61
|
+
*/
|
|
62
|
+
var AsyncCaller = class {
|
|
63
|
+
maxConcurrency;
|
|
64
|
+
maxRetries;
|
|
65
|
+
queue;
|
|
66
|
+
onFailedResponseHook;
|
|
67
|
+
customFetch;
|
|
68
|
+
constructor(params) {
|
|
69
|
+
this.maxConcurrency = params.maxConcurrency ?? Infinity;
|
|
70
|
+
this.maxRetries = params.maxRetries ?? 4;
|
|
71
|
+
if ("default" in p_queue.default) this.queue = new p_queue.default.default({ concurrency: this.maxConcurrency });
|
|
72
|
+
else this.queue = new p_queue.default({ concurrency: this.maxConcurrency });
|
|
73
|
+
this.onFailedResponseHook = params?.onFailedResponseHook;
|
|
74
|
+
this.customFetch = params.fetch;
|
|
75
|
+
}
|
|
76
|
+
call(callable, ...args) {
|
|
77
|
+
const { onFailedResponseHook } = this;
|
|
78
|
+
return this.queue.add(() => (0, p_retry.default)(() => callable(...args).catch(async (error) => {
|
|
79
|
+
if (error instanceof Error) throw error;
|
|
80
|
+
else if (isResponse(error)) throw await HTTPError.fromResponse(error, { includeResponse: !!onFailedResponseHook });
|
|
81
|
+
else throw new Error(error);
|
|
82
|
+
}), {
|
|
83
|
+
async onFailedAttempt(error) {
|
|
84
|
+
if (error.message.startsWith("Cancel") || error.message.startsWith("TimeoutError") || error.message.startsWith("AbortError")) throw error;
|
|
85
|
+
if (error?.code === "ECONNABORTED") throw error;
|
|
86
|
+
if (error instanceof HTTPError) {
|
|
87
|
+
if (STATUS_NO_RETRY.includes(error.status)) throw error;
|
|
88
|
+
if (onFailedResponseHook && error.response) await onFailedResponseHook(error.response);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
retries: this.maxRetries,
|
|
92
|
+
randomize: true
|
|
93
|
+
}), { throwOnTimeout: true });
|
|
94
|
+
}
|
|
95
|
+
callWithOptions(options, callable, ...args) {
|
|
96
|
+
if (options.signal) return Promise.race([this.call(callable, ...args), new Promise((_, reject) => {
|
|
97
|
+
options.signal?.addEventListener("abort", () => {
|
|
98
|
+
reject(/* @__PURE__ */ new Error("AbortError"));
|
|
99
|
+
});
|
|
100
|
+
})]);
|
|
101
|
+
return this.call(callable, ...args);
|
|
102
|
+
}
|
|
103
|
+
fetch(...args) {
|
|
104
|
+
const fetchFn = this.customFetch ?? require_fetch._getFetchImplementation();
|
|
105
|
+
return this.call(() => fetchFn(...args).then((res) => res.ok ? res : Promise.reject(res)));
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
//#endregion
|
|
193
110
|
exports.AsyncCaller = AsyncCaller;
|
|
111
|
+
//# sourceMappingURL=async_caller.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async_caller.cjs","names":["PQueueMod","_getFetchImplementation"],"sources":["../../src/utils/async_caller.ts"],"sourcesContent":["import pRetry from \"p-retry\";\nimport PQueueMod from \"p-queue\";\nimport { _getFetchImplementation } from \"../singletons/fetch.js\";\n\nconst STATUS_NO_RETRY = [\n 400, // Bad Request\n 401, // Unauthorized\n 402, // Payment required\n 403, // Forbidden\n 404, // Not Found\n 405, // Method Not Allowed\n 406, // Not Acceptable\n 407, // Proxy Authentication Required\n 408, // Request Timeout\n 409, // Conflict\n 422, // Unprocessable Entity\n];\n\ntype ResponseCallback = (response?: Response) => Promise<boolean>;\n\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n\n onFailedResponseHook?: ResponseCallback;\n\n /**\n * Specify a custom fetch implementation.\n *\n * By default we expect the `fetch` is available in the global scope.\n */\n fetch?: typeof fetch | ((...args: any[]) => any); // eslint-disable-line @typescript-eslint/no-explicit-any\n}\n\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n}\n\n/**\n * Do not rely on globalThis.Response, rather just\n * do duck typing\n */\nfunction isResponse(x: unknown): x is Response {\n if (x == null || typeof x !== \"object\") return false;\n return \"status\" in x && \"statusText\" in x && \"text\" in x;\n}\n\n/**\n * Utility error to properly handle failed requests\n */\nclass HTTPError extends Error {\n status: number;\n\n text: string;\n\n response?: Response;\n\n constructor(status: number, message: string, response?: Response) {\n super(`HTTP ${status}: ${message}`);\n this.status = status;\n this.text = message;\n this.response = response;\n }\n\n static async fromResponse(\n response: Response,\n options?: { includeResponse?: boolean }\n ): Promise<HTTPError> {\n try {\n return new HTTPError(\n response.status,\n await response.text(),\n options?.includeResponse ? response : undefined\n );\n } catch {\n return new HTTPError(\n response.status,\n response.statusText,\n options?.includeResponse ? response : undefined\n );\n }\n }\n}\n\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 5. This\n * means that by default, each call will be retried up to 5 times, with an\n * exponential backoff between each attempt.\n */\nexport class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n\n private queue: typeof import(\"p-queue\")[\"default\"][\"prototype\"];\n\n private onFailedResponseHook?: ResponseCallback;\n\n private customFetch?: typeof fetch;\n\n constructor(params: AsyncCallerParams) {\n this.maxConcurrency = params.maxConcurrency ?? Infinity;\n this.maxRetries = params.maxRetries ?? 4;\n\n if (\"default\" in PQueueMod) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.queue = new (PQueueMod.default as any)({\n concurrency: this.maxConcurrency,\n });\n } else {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.queue = new (PQueueMod as any)({ concurrency: this.maxConcurrency });\n }\n this.onFailedResponseHook = params?.onFailedResponseHook;\n this.customFetch = params.fetch;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n call<A extends any[], T extends (...args: A) => Promise<any>>(\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n const { onFailedResponseHook } = this;\n return this.queue.add(\n () =>\n pRetry(\n () =>\n callable(...(args as Parameters<T>)).catch(async (error) => {\n // eslint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof Error) {\n throw error;\n } else if (isResponse(error)) {\n throw await HTTPError.fromResponse(error, {\n includeResponse: !!onFailedResponseHook,\n });\n } else {\n throw new Error(error);\n }\n }),\n {\n async onFailedAttempt(error) {\n if (\n error.message.startsWith(\"Cancel\") ||\n error.message.startsWith(\"TimeoutError\") ||\n error.message.startsWith(\"AbortError\")\n ) {\n throw error;\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n if ((error as any)?.code === \"ECONNABORTED\") {\n throw error;\n }\n\n // eslint-disable-next-line no-instanceof/no-instanceof\n if (error instanceof HTTPError) {\n if (STATUS_NO_RETRY.includes(error.status)) {\n throw error;\n }\n if (onFailedResponseHook && error.response) {\n await onFailedResponseHook(error.response);\n }\n }\n },\n // If needed we can change some of the defaults here,\n // but they're quite sensible.\n retries: this.maxRetries,\n randomize: true,\n }\n ),\n { throwOnTimeout: true }\n );\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(\n options: AsyncCallerCallOptions,\n callable: T,\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>>> {\n // Note this doesn't cancel the underlying request,\n // when available prefer to use the signal option of the underlying call\n if (options.signal) {\n return Promise.race([\n this.call<A, T>(callable, ...args),\n new Promise<never>((_, reject) => {\n options.signal?.addEventListener(\"abort\", () => {\n reject(new Error(\"AbortError\"));\n });\n }),\n ]);\n }\n return this.call<A, T>(callable, ...args);\n }\n\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch> {\n const fetchFn =\n this.customFetch ?? (_getFetchImplementation() as typeof fetch);\n return this.call(() =>\n fetchFn(...args).then((res) => (res.ok ? res : Promise.reject(res)))\n );\n }\n}\n"],"mappings":";;;;;;AAIA,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;AAmCF,SAAS,WAAW,GAA2B;AAC7C,KAAI,KAAK,QAAQ,OAAO,MAAM,SAAU,QAAO;AAC/C,QAAO,YAAY,KAAK,gBAAgB,KAAK,UAAU;;;;;AAMzD,IAAM,YAAN,MAAM,kBAAkB,MAAM;CAC5B;CAEA;CAEA;CAEA,YAAY,QAAgB,SAAiB,UAAqB;AAChE,QAAM,QAAQ,OAAO,IAAI;AACzB,OAAK,SAAS;AACd,OAAK,OAAO;AACZ,OAAK,WAAW;;CAGlB,aAAa,aACX,UACA,SACoB;AACpB,MAAI;AACF,UAAO,IAAI,UACT,SAAS,QACT,MAAM,SAAS,QACf,SAAS,kBAAkB,WAAW;UAElC;AACN,UAAO,IAAI,UACT,SAAS,QACT,SAAS,YACT,SAAS,kBAAkB,WAAW;;;;;;;;;;;;;;;;;AAmB9C,IAAa,cAAb,MAAyB;CACvB,AAAU;CAEV,AAAU;CAEV,AAAQ;CAER,AAAQ;CAER,AAAQ;CAER,YAAY,QAA2B;AACrC,OAAK,iBAAiB,OAAO,kBAAkB;AAC/C,OAAK,aAAa,OAAO,cAAc;AAEvC,MAAI,aAAaA,gBAEf,MAAK,QAAQ,IAAKA,gBAAU,QAAgB,EAC1C,aAAa,KAAK;MAIpB,MAAK,QAAQ,IAAKA,gBAAkB,EAAE,aAAa,KAAK;AAE1D,OAAK,uBAAuB,QAAQ;AACpC,OAAK,cAAc,OAAO;;CAI5B,KACE,UACA,GAAG,MAC8B;EACjC,MAAM,EAAE,yBAAyB;AACjC,SAAO,KAAK,MAAM,qCAIV,SAAS,GAAI,MAAwB,MAAM,OAAO,UAAU;AAE1D,OAAI,iBAAiB,MACnB,OAAM;YACG,WAAW,OACpB,OAAM,MAAM,UAAU,aAAa,OAAO,EACxC,iBAAiB,CAAC,CAAC;OAGrB,OAAM,IAAI,MAAM;MAGtB;GACE,MAAM,gBAAgB,OAAO;AAC3B,QACE,MAAM,QAAQ,WAAW,aACzB,MAAM,QAAQ,WAAW,mBACzB,MAAM,QAAQ,WAAW,cAEzB,OAAM;AAGR,QAAK,OAAe,SAAS,eAC3B,OAAM;AAIR,QAAI,iBAAiB,WAAW;AAC9B,SAAI,gBAAgB,SAAS,MAAM,QACjC,OAAM;AAER,SAAI,wBAAwB,MAAM,SAChC,OAAM,qBAAqB,MAAM;;;GAMvC,SAAS,KAAK;GACd,WAAW;MAGjB,EAAE,gBAAgB;;CAKtB,gBACE,SACA,UACA,GAAG,MAC8B;AAGjC,MAAI,QAAQ,OACV,QAAO,QAAQ,KAAK,CAClB,KAAK,KAAW,UAAU,GAAG,OAC7B,IAAI,SAAgB,GAAG,WAAW;AAChC,WAAQ,QAAQ,iBAAiB,eAAe;AAC9C,2BAAO,IAAI,MAAM;;;AAKzB,SAAO,KAAK,KAAW,UAAU,GAAG;;CAGtC,MAAM,GAAG,MAA0D;EACjE,MAAM,UACJ,KAAK,eAAgBC;AACvB,SAAO,KAAK,WACV,QAAQ,GAAG,MAAM,MAAM,QAAS,IAAI,KAAK,MAAM,QAAQ,OAAO"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//#region src/utils/async_caller.d.ts
|
|
2
|
+
type ResponseCallback = (response?: Response) => Promise<boolean>;
|
|
3
|
+
interface AsyncCallerParams {
|
|
4
|
+
/**
|
|
5
|
+
* The maximum number of concurrent calls that can be made.
|
|
6
|
+
* Defaults to `Infinity`, which means no limit.
|
|
7
|
+
*/
|
|
8
|
+
maxConcurrency?: number;
|
|
9
|
+
/**
|
|
10
|
+
* The maximum number of retries that can be made for a single call,
|
|
11
|
+
* with an exponential backoff between each attempt. Defaults to 6.
|
|
12
|
+
*/
|
|
13
|
+
maxRetries?: number;
|
|
14
|
+
onFailedResponseHook?: ResponseCallback;
|
|
15
|
+
/**
|
|
16
|
+
* Specify a custom fetch implementation.
|
|
17
|
+
*
|
|
18
|
+
* By default we expect the `fetch` is available in the global scope.
|
|
19
|
+
*/
|
|
20
|
+
fetch?: typeof fetch | ((...args: any[]) => any); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
21
|
+
}
|
|
22
|
+
interface AsyncCallerCallOptions {
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A class that can be used to make async calls with concurrency and retry logic.
|
|
27
|
+
*
|
|
28
|
+
* This is useful for making calls to any kind of "expensive" external resource,
|
|
29
|
+
* be it because it's rate-limited, subject to network issues, etc.
|
|
30
|
+
*
|
|
31
|
+
* Concurrent calls are limited by the `maxConcurrency` parameter, which defaults
|
|
32
|
+
* to `Infinity`. This means that by default, all calls will be made in parallel.
|
|
33
|
+
*
|
|
34
|
+
* Retries are limited by the `maxRetries` parameter, which defaults to 5. This
|
|
35
|
+
* means that by default, each call will be retried up to 5 times, with an
|
|
36
|
+
* exponential backoff between each attempt.
|
|
37
|
+
*/
|
|
38
|
+
declare class AsyncCaller {
|
|
39
|
+
protected maxConcurrency: AsyncCallerParams["maxConcurrency"];
|
|
40
|
+
protected maxRetries: AsyncCallerParams["maxRetries"];
|
|
41
|
+
private queue;
|
|
42
|
+
private onFailedResponseHook?;
|
|
43
|
+
private customFetch?;
|
|
44
|
+
constructor(params: AsyncCallerParams);
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
|
+
call<A extends any[], T extends (...args: A) => Promise<any>>(callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(options: AsyncCallerCallOptions, callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
|
|
49
|
+
fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { AsyncCaller, AsyncCallerParams };
|
|
53
|
+
//# sourceMappingURL=async_caller.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async_caller.d.cts","names":["ResponseCallback","Response","Promise","AsyncCallerParams","fetch","AsyncCallerCallOptions","AbortSignal","AsyncCaller","A","T","Parameters","ReturnType","Awaited"],"sources":["../../src/utils/async_caller.d.ts"],"sourcesContent":["type ResponseCallback = (response?: Response) => Promise<boolean>;\nexport interface AsyncCallerParams {\n /**\n * The maximum number of concurrent calls that can be made.\n * Defaults to `Infinity`, which means no limit.\n */\n maxConcurrency?: number;\n /**\n * The maximum number of retries that can be made for a single call,\n * with an exponential backoff between each attempt. Defaults to 6.\n */\n maxRetries?: number;\n onFailedResponseHook?: ResponseCallback;\n /**\n * Specify a custom fetch implementation.\n *\n * By default we expect the `fetch` is available in the global scope.\n */\n fetch?: typeof fetch | ((...args: any[]) => any); // eslint-disable-line @typescript-eslint/no-explicit-any\n}\nexport interface AsyncCallerCallOptions {\n signal?: AbortSignal;\n}\n/**\n * A class that can be used to make async calls with concurrency and retry logic.\n *\n * This is useful for making calls to any kind of \"expensive\" external resource,\n * be it because it's rate-limited, subject to network issues, etc.\n *\n * Concurrent calls are limited by the `maxConcurrency` parameter, which defaults\n * to `Infinity`. This means that by default, all calls will be made in parallel.\n *\n * Retries are limited by the `maxRetries` parameter, which defaults to 5. This\n * means that by default, each call will be retried up to 5 times, with an\n * exponential backoff between each attempt.\n */\nexport declare class AsyncCaller {\n protected maxConcurrency: AsyncCallerParams[\"maxConcurrency\"];\n protected maxRetries: AsyncCallerParams[\"maxRetries\"];\n private queue;\n private onFailedResponseHook?;\n private customFetch?;\n constructor(params: AsyncCallerParams);\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n call<A extends any[], T extends (...args: A) => Promise<any>>(callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callWithOptions<A extends any[], T extends (...args: A) => Promise<any>>(options: AsyncCallerCallOptions, callable: T, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;\n fetch(...args: Parameters<typeof fetch>): ReturnType<typeof fetch>;\n}\nexport {};\n"],"mappings":";KAAKA,gBAAAA,eAA+BC,aAAaC;AAA5CF,UACYG,iBAAAA,CADI;EAAA;;;;EACJA,cAAAA,CAAAA,EAAAA,MAAiB;EAAA;;;;EAmBjBE,UAAAA,CAAAA,EAAAA,MAAAA;EAgBIE,oBAAW,CAAA,EAxBLP,gBAwBK;EAAA;;;;;OAQoBE,CAAAA,EAAAA,OA1BjCE,KA0BiCF,GAAAA,CAAAA,CAAAA,GAAAA,IAAAA,EAAAA,GAAAA,EAAAA,EAAAA,GAAAA,GAAAA,CAAAA,CAAAA,CAAAA;;AAA+CO,UAxBlFJ,sBAAAA,CAwBkFI;QAAXC,CAAAA,EAvB3EJ,WAuB2EI;;;;;;;;;;;;;;;AAGnDN,cAXhBG,WAAAA,CAWgBH;YAAlBM,cAAAA,EAVWP,iBAUXO,CAAAA,gBAAAA,CAAAA;YAA6CN,UAAAA,EATtCD,iBASsCC,CAAAA,YAAAA,CAAAA;UAAlBO,KAAAA;;;sBALtBR;;4CAEsBK,MAAMN,wBAAwBO,YAAYC,WAAWD,KAAKP,QAAQU,QAAQD,WAAWF;;uDAE1ED,MAAMN,uBAAuBG,kCAAkCI,YAAYC,WAAWD,KAAKP,QAAQU,QAAQD,WAAWF;iBAC5JC,kBAAkBN,SAASO,kBAAkBP"}
|