@mastra/react 0.1.0-beta.20 → 0.1.0-beta.22
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 +82 -0
- package/dist/{chunk-55VPMN3N-DKEsyXxY.js → chunk-55VPMN3N-C6D-FCqV.js} +2 -2
- package/dist/{chunk-55VPMN3N-DKEsyXxY.js.map → chunk-55VPMN3N-C6D-FCqV.js.map} +1 -1
- package/dist/{chunk-55VPMN3N-BA1FzyXn.cjs → chunk-55VPMN3N-CXxvOk-2.cjs} +2 -2
- package/dist/{chunk-55VPMN3N-BA1FzyXn.cjs.map → chunk-55VPMN3N-CXxvOk-2.cjs.map} +1 -1
- package/dist/{index-T2p2LQJ_.js → index-DFYpOuhE.js} +1918 -161
- package/dist/index-DFYpOuhE.js.map +1 -0
- package/dist/{index-CetwyRpA.cjs → index-DiwNdkF4.cjs} +1918 -161
- package/dist/index-DiwNdkF4.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/src/agent/hooks.d.ts +7 -0
- package/dist/src/lib/ai-sdk/types.d.ts +36 -1
- package/dist/{token-6GSAFR2W-SPYPLMBM-BUdoiW7P.js → token-6GSAFR2W-SPYPLMBM-BeIpRVyM.js} +3 -3
- package/dist/{token-6GSAFR2W-SPYPLMBM-BUdoiW7P.js.map → token-6GSAFR2W-SPYPLMBM-BeIpRVyM.js.map} +1 -1
- package/dist/{token-6GSAFR2W-SPYPLMBM-aoT9nIbD.cjs → token-6GSAFR2W-SPYPLMBM-CKILUODb.cjs} +3 -3
- package/dist/{token-6GSAFR2W-SPYPLMBM-aoT9nIbD.cjs.map → token-6GSAFR2W-SPYPLMBM-CKILUODb.cjs.map} +1 -1
- package/dist/{token-util-NEHG7TUY-JRJTGTAB-_6pEAvyD.cjs → token-util-NEHG7TUY-JRJTGTAB-DgJBvxO4.cjs} +2 -2
- package/dist/{token-util-NEHG7TUY-JRJTGTAB-_6pEAvyD.cjs.map → token-util-NEHG7TUY-JRJTGTAB-DgJBvxO4.cjs.map} +1 -1
- package/dist/{token-util-NEHG7TUY-JRJTGTAB-BVMAhdst.js → token-util-NEHG7TUY-JRJTGTAB-N8sYk0L_.js} +2 -2
- package/dist/{token-util-NEHG7TUY-JRJTGTAB-BVMAhdst.js.map → token-util-NEHG7TUY-JRJTGTAB-N8sYk0L_.js.map} +1 -1
- package/package.json +5 -5
- package/dist/index-CetwyRpA.cjs.map +0 -1
- package/dist/index-T2p2LQJ_.js.map +0 -1
|
@@ -163,38 +163,57 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
163
163
|
};
|
|
164
164
|
return [...result, newMessage];
|
|
165
165
|
}
|
|
166
|
-
case "text-start":
|
|
167
|
-
case "text-delta": {
|
|
166
|
+
case "text-start": {
|
|
168
167
|
const lastMessage = result[result.length - 1];
|
|
169
168
|
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
170
169
|
const parts = [...lastMessage.parts];
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
170
|
+
const textId = chunk.payload.id || `text-${Date.now()}`;
|
|
171
|
+
const newTextPart = {
|
|
172
|
+
type: "text",
|
|
173
|
+
text: "",
|
|
174
|
+
state: "streaming",
|
|
175
|
+
textId,
|
|
176
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
177
|
+
};
|
|
178
|
+
parts.push(newTextPart);
|
|
179
|
+
return [
|
|
180
|
+
...result.slice(0, -1),
|
|
181
|
+
{
|
|
182
|
+
...lastMessage,
|
|
183
|
+
parts
|
|
180
184
|
}
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
case "text-delta": {
|
|
188
|
+
const lastMessage = result[result.length - 1];
|
|
189
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
190
|
+
const parts = [...lastMessage.parts];
|
|
191
|
+
const textId = chunk.payload.id;
|
|
192
|
+
let textPartIndex = textId ? parts.findLastIndex((part) => part.type === "text" && part.textId === textId) : -1;
|
|
193
|
+
if (textPartIndex === -1) {
|
|
194
|
+
textPartIndex = parts.findLastIndex(
|
|
195
|
+
(part) => part.type === "text" && part.state === "streaming"
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (textPartIndex === -1) {
|
|
199
|
+
const newTextPart = {
|
|
200
|
+
type: "text",
|
|
201
|
+
text: chunk.payload.text,
|
|
202
|
+
state: "streaming",
|
|
203
|
+
textId,
|
|
204
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
205
|
+
};
|
|
206
|
+
parts.push(newTextPart);
|
|
181
207
|
} else {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (textPart.type === "text") {
|
|
192
|
-
parts[textPartIndex] = {
|
|
193
|
-
...textPart,
|
|
194
|
-
text: textPart.text + chunk.payload.text,
|
|
195
|
-
state: "streaming"
|
|
196
|
-
};
|
|
197
|
-
}
|
|
208
|
+
const textPart = parts[textPartIndex];
|
|
209
|
+
if (textPart.type === "text") {
|
|
210
|
+
const extendedTextPart = textPart;
|
|
211
|
+
const updatedTextPart = {
|
|
212
|
+
...extendedTextPart,
|
|
213
|
+
text: extendedTextPart.text + chunk.payload.text,
|
|
214
|
+
state: "streaming"
|
|
215
|
+
};
|
|
216
|
+
parts[textPartIndex] = updatedTextPart;
|
|
198
217
|
}
|
|
199
218
|
}
|
|
200
219
|
return [
|
|
@@ -792,7 +811,8 @@ const toAssistantUIMessage = (message) => {
|
|
|
792
811
|
};
|
|
793
812
|
|
|
794
813
|
const resolveInitialMessages = (messages) => {
|
|
795
|
-
|
|
814
|
+
const messagesLength = messages.length;
|
|
815
|
+
return messages.map((message, index) => {
|
|
796
816
|
const networkPart = message.parts.find(
|
|
797
817
|
(part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
|
|
798
818
|
);
|
|
@@ -840,7 +860,7 @@ const resolveInitialMessages = (messages) => {
|
|
|
840
860
|
content: finalResult.text
|
|
841
861
|
});
|
|
842
862
|
}
|
|
843
|
-
const result = {
|
|
863
|
+
const result = primitiveType === "tool" ? finalResult?.result : {
|
|
844
864
|
childMessages,
|
|
845
865
|
result: finalResult?.text || ""
|
|
846
866
|
};
|
|
@@ -862,6 +882,7 @@ const resolveInitialMessages = (messages) => {
|
|
|
862
882
|
mode: "network",
|
|
863
883
|
selectionReason,
|
|
864
884
|
agentInput: json.input,
|
|
885
|
+
hasMoreMessages: index < messagesLength - 1,
|
|
865
886
|
from: primitiveType === "agent" ? "AGENT" : primitiveType === "tool" ? "TOOL" : "WORKFLOW"
|
|
866
887
|
}
|
|
867
888
|
};
|
|
@@ -3108,8 +3129,8 @@ var require_get_vercel_oidc_token = __commonJS$2({
|
|
|
3108
3129
|
}
|
|
3109
3130
|
try {
|
|
3110
3131
|
const [{ getTokenPayload, isExpired }, { refreshToken }] = await Promise.all([
|
|
3111
|
-
await import('./token-util-NEHG7TUY-JRJTGTAB-
|
|
3112
|
-
await import('./token-6GSAFR2W-SPYPLMBM-
|
|
3132
|
+
await import('./token-util-NEHG7TUY-JRJTGTAB-N8sYk0L_.js'),
|
|
3133
|
+
await import('./token-6GSAFR2W-SPYPLMBM-BeIpRVyM.js')
|
|
3113
3134
|
]);
|
|
3114
3135
|
if (!token || isExpired(getTokenPayload(token))) {
|
|
3115
3136
|
await refreshToken();
|
|
@@ -4941,20 +4962,6 @@ createIdGenerator$3({
|
|
|
4941
4962
|
prefix: "aitxt",
|
|
4942
4963
|
size: 24
|
|
4943
4964
|
});
|
|
4944
|
-
(class extends TransformStream {
|
|
4945
|
-
constructor() {
|
|
4946
|
-
super({
|
|
4947
|
-
transform(part, controller) {
|
|
4948
|
-
controller.enqueue(`data: ${JSON.stringify(part)}
|
|
4949
|
-
|
|
4950
|
-
`);
|
|
4951
|
-
},
|
|
4952
|
-
flush(controller) {
|
|
4953
|
-
controller.enqueue("data: [DONE]\n\n");
|
|
4954
|
-
}
|
|
4955
|
-
});
|
|
4956
|
-
}
|
|
4957
|
-
});
|
|
4958
4965
|
function fixJson$2(input) {
|
|
4959
4966
|
const stack = ["ROOT"];
|
|
4960
4967
|
let lastValidIndex = -1;
|
|
@@ -10069,15 +10076,16 @@ var AIV4Adapter = class {
|
|
|
10069
10076
|
*/
|
|
10070
10077
|
static toUIMessage(m) {
|
|
10071
10078
|
const experimentalAttachments = m.content.experimental_attachments ? [...m.content.experimental_attachments] : [];
|
|
10072
|
-
const contentString = typeof m.content.content === `string` && m.content.content !== "" ? m.content.content : m.content.parts.reduce((prev, part) => {
|
|
10079
|
+
const contentString = typeof m.content.content === `string` && m.content.content !== "" ? m.content.content : (m.content.parts ?? []).reduce((prev, part) => {
|
|
10073
10080
|
if (part.type === `text`) {
|
|
10074
10081
|
return part.text;
|
|
10075
10082
|
}
|
|
10076
10083
|
return prev;
|
|
10077
10084
|
}, "");
|
|
10078
10085
|
const parts = [];
|
|
10079
|
-
|
|
10080
|
-
|
|
10086
|
+
const sourceParts = m.content.parts ?? [];
|
|
10087
|
+
if (sourceParts.length) {
|
|
10088
|
+
for (const part of sourceParts) {
|
|
10081
10089
|
if (part.type === `file`) {
|
|
10082
10090
|
let normalizedUrl;
|
|
10083
10091
|
if (typeof part.data === "string") {
|
|
@@ -10100,7 +10108,7 @@ var AIV4Adapter = class {
|
|
|
10100
10108
|
const toolInvocation = { ...part.toolInvocation };
|
|
10101
10109
|
let currentStep = -1;
|
|
10102
10110
|
let toolStep = -1;
|
|
10103
|
-
for (const innerPart of
|
|
10111
|
+
for (const innerPart of sourceParts) {
|
|
10104
10112
|
if (innerPart.type === `step-start`) currentStep++;
|
|
10105
10113
|
if (innerPart.type === `tool-invocation` && innerPart.toolInvocation.toolCallId === part.toolInvocation.toolCallId) {
|
|
10106
10114
|
toolStep = currentStep;
|
|
@@ -13483,9 +13491,15 @@ var MessageList$1 = class MessageList {
|
|
|
13483
13491
|
this.lastCreatedAt = nowTime;
|
|
13484
13492
|
return now;
|
|
13485
13493
|
}
|
|
13486
|
-
newMessageId() {
|
|
13494
|
+
newMessageId(role) {
|
|
13487
13495
|
if (this.generateMessageId) {
|
|
13488
|
-
return this.generateMessageId(
|
|
13496
|
+
return this.generateMessageId({
|
|
13497
|
+
idType: "message",
|
|
13498
|
+
source: "agent",
|
|
13499
|
+
threadId: this.memoryInfo?.threadId,
|
|
13500
|
+
resourceId: this.memoryInfo?.resourceId,
|
|
13501
|
+
role
|
|
13502
|
+
});
|
|
13489
13503
|
}
|
|
13490
13504
|
return v4();
|
|
13491
13505
|
}
|
|
@@ -17393,110 +17407,1673 @@ __publicField(Tiktoken, "specialTokenRegex", (tokens) => {
|
|
|
17393
17407
|
return new RegExp(tokens.map((i) => escapeRegex(i)).join("|"), "g");
|
|
17394
17408
|
});
|
|
17395
17409
|
|
|
17396
|
-
|
|
17397
|
-
|
|
17398
|
-
|
|
17399
|
-
|
|
17400
|
-
|
|
17401
|
-
|
|
17402
|
-
|
|
17403
|
-
|
|
17404
|
-
|
|
17405
|
-
|
|
17406
|
-
|
|
17407
|
-
|
|
17408
|
-
|
|
17409
|
-
|
|
17410
|
+
/**
|
|
17411
|
+
* @module LRUCache
|
|
17412
|
+
*/
|
|
17413
|
+
const defaultPerf = (typeof performance === 'object' &&
|
|
17414
|
+
performance &&
|
|
17415
|
+
typeof performance.now === 'function') ?
|
|
17416
|
+
performance
|
|
17417
|
+
: Date;
|
|
17418
|
+
const warned = new Set();
|
|
17419
|
+
/* c8 ignore start */
|
|
17420
|
+
const PROCESS = (typeof process === 'object' && !!process ?
|
|
17421
|
+
process
|
|
17422
|
+
: {});
|
|
17423
|
+
/* c8 ignore start */
|
|
17424
|
+
const emitWarning = (msg, type, code, fn) => {
|
|
17425
|
+
typeof PROCESS.emitWarning === 'function' ?
|
|
17426
|
+
PROCESS.emitWarning(msg, type, code, fn)
|
|
17427
|
+
: console.error(`[${code}] ${type}: ${msg}`);
|
|
17428
|
+
};
|
|
17429
|
+
let AC = globalThis.AbortController;
|
|
17430
|
+
let AS = globalThis.AbortSignal;
|
|
17431
|
+
/* c8 ignore start */
|
|
17432
|
+
if (typeof AC === 'undefined') {
|
|
17433
|
+
//@ts-ignore
|
|
17434
|
+
AS = class AbortSignal {
|
|
17435
|
+
onabort;
|
|
17436
|
+
_onabort = [];
|
|
17437
|
+
reason;
|
|
17438
|
+
aborted = false;
|
|
17439
|
+
addEventListener(_, fn) {
|
|
17440
|
+
this._onabort.push(fn);
|
|
17441
|
+
}
|
|
17442
|
+
};
|
|
17443
|
+
//@ts-ignore
|
|
17444
|
+
AC = class AbortController {
|
|
17445
|
+
constructor() {
|
|
17446
|
+
warnACPolyfill();
|
|
17447
|
+
}
|
|
17448
|
+
signal = new AS();
|
|
17449
|
+
abort(reason) {
|
|
17450
|
+
if (this.signal.aborted)
|
|
17451
|
+
return;
|
|
17452
|
+
//@ts-ignore
|
|
17453
|
+
this.signal.reason = reason;
|
|
17454
|
+
//@ts-ignore
|
|
17455
|
+
this.signal.aborted = true;
|
|
17456
|
+
//@ts-ignore
|
|
17457
|
+
for (const fn of this.signal._onabort) {
|
|
17458
|
+
fn(reason);
|
|
17459
|
+
}
|
|
17460
|
+
this.signal.onabort?.(reason);
|
|
17410
17461
|
}
|
|
17411
|
-
|
|
17412
|
-
|
|
17413
|
-
|
|
17414
|
-
|
|
17415
|
-
|
|
17416
|
-
|
|
17417
|
-
|
|
17418
|
-
|
|
17419
|
-
|
|
17420
|
-
|
|
17421
|
-
|
|
17462
|
+
};
|
|
17463
|
+
let printACPolyfillWarning = PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1';
|
|
17464
|
+
const warnACPolyfill = () => {
|
|
17465
|
+
if (!printACPolyfillWarning)
|
|
17466
|
+
return;
|
|
17467
|
+
printACPolyfillWarning = false;
|
|
17468
|
+
emitWarning('AbortController is not defined. If using lru-cache in ' +
|
|
17469
|
+
'node 14, load an AbortController polyfill from the ' +
|
|
17470
|
+
'`node-abort-controller` package. A minimal polyfill is ' +
|
|
17471
|
+
'provided for use by LRUCache.fetch(), but it should not be ' +
|
|
17472
|
+
'relied upon in other contexts (eg, passing it to other APIs that ' +
|
|
17473
|
+
'use AbortController/AbortSignal might have undesirable effects). ' +
|
|
17474
|
+
'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.', 'NO_ABORT_CONTROLLER', 'ENOTSUP', warnACPolyfill);
|
|
17475
|
+
};
|
|
17476
|
+
}
|
|
17477
|
+
/* c8 ignore stop */
|
|
17478
|
+
const shouldWarn = (code) => !warned.has(code);
|
|
17479
|
+
const isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
|
|
17480
|
+
/* c8 ignore start */
|
|
17481
|
+
// This is a little bit ridiculous, tbh.
|
|
17482
|
+
// The maximum array length is 2^32-1 or thereabouts on most JS impls.
|
|
17483
|
+
// And well before that point, you're caching the entire world, I mean,
|
|
17484
|
+
// that's ~32GB of just integers for the next/prev links, plus whatever
|
|
17485
|
+
// else to hold that many keys and values. Just filling the memory with
|
|
17486
|
+
// zeroes at init time is brutal when you get that big.
|
|
17487
|
+
// But why not be complete?
|
|
17488
|
+
// Maybe in the future, these limits will have expanded.
|
|
17489
|
+
const getUintArray = (max) => !isPosInt(max) ? null
|
|
17490
|
+
: max <= Math.pow(2, 8) ? Uint8Array
|
|
17491
|
+
: max <= Math.pow(2, 16) ? Uint16Array
|
|
17492
|
+
: max <= Math.pow(2, 32) ? Uint32Array
|
|
17493
|
+
: max <= Number.MAX_SAFE_INTEGER ? ZeroArray
|
|
17494
|
+
: null;
|
|
17495
|
+
/* c8 ignore stop */
|
|
17496
|
+
class ZeroArray extends Array {
|
|
17497
|
+
constructor(size) {
|
|
17498
|
+
super(size);
|
|
17499
|
+
this.fill(0);
|
|
17500
|
+
}
|
|
17501
|
+
}
|
|
17502
|
+
class Stack {
|
|
17503
|
+
heap;
|
|
17504
|
+
length;
|
|
17505
|
+
// private constructor
|
|
17506
|
+
static #constructing = false;
|
|
17507
|
+
static create(max) {
|
|
17508
|
+
const HeapCls = getUintArray(max);
|
|
17509
|
+
if (!HeapCls)
|
|
17510
|
+
return [];
|
|
17511
|
+
Stack.#constructing = true;
|
|
17512
|
+
const s = new Stack(max, HeapCls);
|
|
17513
|
+
Stack.#constructing = false;
|
|
17514
|
+
return s;
|
|
17515
|
+
}
|
|
17516
|
+
constructor(max, HeapCls) {
|
|
17517
|
+
/* c8 ignore start */
|
|
17518
|
+
if (!Stack.#constructing) {
|
|
17519
|
+
throw new TypeError('instantiate Stack using Stack.create(n)');
|
|
17520
|
+
}
|
|
17521
|
+
/* c8 ignore stop */
|
|
17522
|
+
this.heap = new HeapCls(max);
|
|
17523
|
+
this.length = 0;
|
|
17524
|
+
}
|
|
17525
|
+
push(n) {
|
|
17526
|
+
this.heap[this.length++] = n;
|
|
17527
|
+
}
|
|
17528
|
+
pop() {
|
|
17529
|
+
return this.heap[--this.length];
|
|
17530
|
+
}
|
|
17531
|
+
}
|
|
17532
|
+
/**
|
|
17533
|
+
* Default export, the thing you're using this module to get.
|
|
17534
|
+
*
|
|
17535
|
+
* The `K` and `V` types define the key and value types, respectively. The
|
|
17536
|
+
* optional `FC` type defines the type of the `context` object passed to
|
|
17537
|
+
* `cache.fetch()` and `cache.memo()`.
|
|
17538
|
+
*
|
|
17539
|
+
* Keys and values **must not** be `null` or `undefined`.
|
|
17540
|
+
*
|
|
17541
|
+
* All properties from the options object (with the exception of `max`,
|
|
17542
|
+
* `maxSize`, `fetchMethod`, `memoMethod`, `dispose` and `disposeAfter`) are
|
|
17543
|
+
* added as normal public members. (The listed options are read-only getters.)
|
|
17544
|
+
*
|
|
17545
|
+
* Changing any of these will alter the defaults for subsequent method calls.
|
|
17546
|
+
*/
|
|
17547
|
+
class LRUCache {
|
|
17548
|
+
// options that cannot be changed without disaster
|
|
17549
|
+
#max;
|
|
17550
|
+
#maxSize;
|
|
17551
|
+
#dispose;
|
|
17552
|
+
#onInsert;
|
|
17553
|
+
#disposeAfter;
|
|
17554
|
+
#fetchMethod;
|
|
17555
|
+
#memoMethod;
|
|
17556
|
+
#perf;
|
|
17557
|
+
/**
|
|
17558
|
+
* {@link LRUCache.OptionsBase.perf}
|
|
17559
|
+
*/
|
|
17560
|
+
get perf() {
|
|
17561
|
+
return this.#perf;
|
|
17562
|
+
}
|
|
17563
|
+
/**
|
|
17564
|
+
* {@link LRUCache.OptionsBase.ttl}
|
|
17565
|
+
*/
|
|
17566
|
+
ttl;
|
|
17567
|
+
/**
|
|
17568
|
+
* {@link LRUCache.OptionsBase.ttlResolution}
|
|
17569
|
+
*/
|
|
17570
|
+
ttlResolution;
|
|
17571
|
+
/**
|
|
17572
|
+
* {@link LRUCache.OptionsBase.ttlAutopurge}
|
|
17573
|
+
*/
|
|
17574
|
+
ttlAutopurge;
|
|
17575
|
+
/**
|
|
17576
|
+
* {@link LRUCache.OptionsBase.updateAgeOnGet}
|
|
17577
|
+
*/
|
|
17578
|
+
updateAgeOnGet;
|
|
17579
|
+
/**
|
|
17580
|
+
* {@link LRUCache.OptionsBase.updateAgeOnHas}
|
|
17581
|
+
*/
|
|
17582
|
+
updateAgeOnHas;
|
|
17583
|
+
/**
|
|
17584
|
+
* {@link LRUCache.OptionsBase.allowStale}
|
|
17585
|
+
*/
|
|
17586
|
+
allowStale;
|
|
17587
|
+
/**
|
|
17588
|
+
* {@link LRUCache.OptionsBase.noDisposeOnSet}
|
|
17589
|
+
*/
|
|
17590
|
+
noDisposeOnSet;
|
|
17591
|
+
/**
|
|
17592
|
+
* {@link LRUCache.OptionsBase.noUpdateTTL}
|
|
17593
|
+
*/
|
|
17594
|
+
noUpdateTTL;
|
|
17595
|
+
/**
|
|
17596
|
+
* {@link LRUCache.OptionsBase.maxEntrySize}
|
|
17597
|
+
*/
|
|
17598
|
+
maxEntrySize;
|
|
17599
|
+
/**
|
|
17600
|
+
* {@link LRUCache.OptionsBase.sizeCalculation}
|
|
17601
|
+
*/
|
|
17602
|
+
sizeCalculation;
|
|
17603
|
+
/**
|
|
17604
|
+
* {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
|
|
17605
|
+
*/
|
|
17606
|
+
noDeleteOnFetchRejection;
|
|
17607
|
+
/**
|
|
17608
|
+
* {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
|
|
17609
|
+
*/
|
|
17610
|
+
noDeleteOnStaleGet;
|
|
17611
|
+
/**
|
|
17612
|
+
* {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
|
|
17613
|
+
*/
|
|
17614
|
+
allowStaleOnFetchAbort;
|
|
17615
|
+
/**
|
|
17616
|
+
* {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
|
|
17617
|
+
*/
|
|
17618
|
+
allowStaleOnFetchRejection;
|
|
17619
|
+
/**
|
|
17620
|
+
* {@link LRUCache.OptionsBase.ignoreFetchAbort}
|
|
17621
|
+
*/
|
|
17622
|
+
ignoreFetchAbort;
|
|
17623
|
+
// computed properties
|
|
17624
|
+
#size;
|
|
17625
|
+
#calculatedSize;
|
|
17626
|
+
#keyMap;
|
|
17627
|
+
#keyList;
|
|
17628
|
+
#valList;
|
|
17629
|
+
#next;
|
|
17630
|
+
#prev;
|
|
17631
|
+
#head;
|
|
17632
|
+
#tail;
|
|
17633
|
+
#free;
|
|
17634
|
+
#disposed;
|
|
17635
|
+
#sizes;
|
|
17636
|
+
#starts;
|
|
17637
|
+
#ttls;
|
|
17638
|
+
#hasDispose;
|
|
17639
|
+
#hasFetchMethod;
|
|
17640
|
+
#hasDisposeAfter;
|
|
17641
|
+
#hasOnInsert;
|
|
17642
|
+
/**
|
|
17643
|
+
* Do not call this method unless you need to inspect the
|
|
17644
|
+
* inner workings of the cache. If anything returned by this
|
|
17645
|
+
* object is modified in any way, strange breakage may occur.
|
|
17646
|
+
*
|
|
17647
|
+
* These fields are private for a reason!
|
|
17648
|
+
*
|
|
17649
|
+
* @internal
|
|
17650
|
+
*/
|
|
17651
|
+
static unsafeExposeInternals(c) {
|
|
17652
|
+
return {
|
|
17653
|
+
// properties
|
|
17654
|
+
starts: c.#starts,
|
|
17655
|
+
ttls: c.#ttls,
|
|
17656
|
+
sizes: c.#sizes,
|
|
17657
|
+
keyMap: c.#keyMap,
|
|
17658
|
+
keyList: c.#keyList,
|
|
17659
|
+
valList: c.#valList,
|
|
17660
|
+
next: c.#next,
|
|
17661
|
+
prev: c.#prev,
|
|
17662
|
+
get head() {
|
|
17663
|
+
return c.#head;
|
|
17664
|
+
},
|
|
17665
|
+
get tail() {
|
|
17666
|
+
return c.#tail;
|
|
17667
|
+
},
|
|
17668
|
+
free: c.#free,
|
|
17669
|
+
// methods
|
|
17670
|
+
isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
|
|
17671
|
+
backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
|
|
17672
|
+
moveToTail: (index) => c.#moveToTail(index),
|
|
17673
|
+
indexes: (options) => c.#indexes(options),
|
|
17674
|
+
rindexes: (options) => c.#rindexes(options),
|
|
17675
|
+
isStale: (index) => c.#isStale(index),
|
|
17676
|
+
};
|
|
17677
|
+
}
|
|
17678
|
+
// Protected read-only members
|
|
17679
|
+
/**
|
|
17680
|
+
* {@link LRUCache.OptionsBase.max} (read-only)
|
|
17681
|
+
*/
|
|
17682
|
+
get max() {
|
|
17683
|
+
return this.#max;
|
|
17684
|
+
}
|
|
17685
|
+
/**
|
|
17686
|
+
* {@link LRUCache.OptionsBase.maxSize} (read-only)
|
|
17687
|
+
*/
|
|
17688
|
+
get maxSize() {
|
|
17689
|
+
return this.#maxSize;
|
|
17690
|
+
}
|
|
17691
|
+
/**
|
|
17692
|
+
* The total computed size of items in the cache (read-only)
|
|
17693
|
+
*/
|
|
17694
|
+
get calculatedSize() {
|
|
17695
|
+
return this.#calculatedSize;
|
|
17696
|
+
}
|
|
17697
|
+
/**
|
|
17698
|
+
* The number of items stored in the cache (read-only)
|
|
17699
|
+
*/
|
|
17700
|
+
get size() {
|
|
17701
|
+
return this.#size;
|
|
17702
|
+
}
|
|
17703
|
+
/**
|
|
17704
|
+
* {@link LRUCache.OptionsBase.fetchMethod} (read-only)
|
|
17705
|
+
*/
|
|
17706
|
+
get fetchMethod() {
|
|
17707
|
+
return this.#fetchMethod;
|
|
17708
|
+
}
|
|
17709
|
+
get memoMethod() {
|
|
17710
|
+
return this.#memoMethod;
|
|
17711
|
+
}
|
|
17712
|
+
/**
|
|
17713
|
+
* {@link LRUCache.OptionsBase.dispose} (read-only)
|
|
17714
|
+
*/
|
|
17715
|
+
get dispose() {
|
|
17716
|
+
return this.#dispose;
|
|
17717
|
+
}
|
|
17718
|
+
/**
|
|
17719
|
+
* {@link LRUCache.OptionsBase.onInsert} (read-only)
|
|
17720
|
+
*/
|
|
17721
|
+
get onInsert() {
|
|
17722
|
+
return this.#onInsert;
|
|
17723
|
+
}
|
|
17724
|
+
/**
|
|
17725
|
+
* {@link LRUCache.OptionsBase.disposeAfter} (read-only)
|
|
17726
|
+
*/
|
|
17727
|
+
get disposeAfter() {
|
|
17728
|
+
return this.#disposeAfter;
|
|
17729
|
+
}
|
|
17730
|
+
constructor(options) {
|
|
17731
|
+
const { max = 0, ttl, ttlResolution = 1, ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, onInsert, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0, maxEntrySize = 0, sizeCalculation, fetchMethod, memoMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, perf, } = options;
|
|
17732
|
+
if (perf !== undefined) {
|
|
17733
|
+
if (typeof perf?.now !== 'function') {
|
|
17734
|
+
throw new TypeError('perf option must have a now() method if specified');
|
|
17735
|
+
}
|
|
17422
17736
|
}
|
|
17423
|
-
|
|
17424
|
-
|
|
17425
|
-
|
|
17737
|
+
this.#perf = perf ?? defaultPerf;
|
|
17738
|
+
if (max !== 0 && !isPosInt(max)) {
|
|
17739
|
+
throw new TypeError('max option must be a nonnegative integer');
|
|
17740
|
+
}
|
|
17741
|
+
const UintArray = max ? getUintArray(max) : Array;
|
|
17742
|
+
if (!UintArray) {
|
|
17743
|
+
throw new Error('invalid max value: ' + max);
|
|
17744
|
+
}
|
|
17745
|
+
this.#max = max;
|
|
17746
|
+
this.#maxSize = maxSize;
|
|
17747
|
+
this.maxEntrySize = maxEntrySize || this.#maxSize;
|
|
17748
|
+
this.sizeCalculation = sizeCalculation;
|
|
17749
|
+
if (this.sizeCalculation) {
|
|
17750
|
+
if (!this.#maxSize && !this.maxEntrySize) {
|
|
17751
|
+
throw new TypeError('cannot set sizeCalculation without setting maxSize or maxEntrySize');
|
|
17752
|
+
}
|
|
17753
|
+
if (typeof this.sizeCalculation !== 'function') {
|
|
17754
|
+
throw new TypeError('sizeCalculation set to non-function');
|
|
17755
|
+
}
|
|
17756
|
+
}
|
|
17757
|
+
if (memoMethod !== undefined &&
|
|
17758
|
+
typeof memoMethod !== 'function') {
|
|
17759
|
+
throw new TypeError('memoMethod must be a function if defined');
|
|
17760
|
+
}
|
|
17761
|
+
this.#memoMethod = memoMethod;
|
|
17762
|
+
if (fetchMethod !== undefined &&
|
|
17763
|
+
typeof fetchMethod !== 'function') {
|
|
17764
|
+
throw new TypeError('fetchMethod must be a function if specified');
|
|
17765
|
+
}
|
|
17766
|
+
this.#fetchMethod = fetchMethod;
|
|
17767
|
+
this.#hasFetchMethod = !!fetchMethod;
|
|
17768
|
+
this.#keyMap = new Map();
|
|
17769
|
+
this.#keyList = new Array(max).fill(undefined);
|
|
17770
|
+
this.#valList = new Array(max).fill(undefined);
|
|
17771
|
+
this.#next = new UintArray(max);
|
|
17772
|
+
this.#prev = new UintArray(max);
|
|
17773
|
+
this.#head = 0;
|
|
17774
|
+
this.#tail = 0;
|
|
17775
|
+
this.#free = Stack.create(max);
|
|
17776
|
+
this.#size = 0;
|
|
17777
|
+
this.#calculatedSize = 0;
|
|
17778
|
+
if (typeof dispose === 'function') {
|
|
17779
|
+
this.#dispose = dispose;
|
|
17780
|
+
}
|
|
17781
|
+
if (typeof onInsert === 'function') {
|
|
17782
|
+
this.#onInsert = onInsert;
|
|
17783
|
+
}
|
|
17784
|
+
if (typeof disposeAfter === 'function') {
|
|
17785
|
+
this.#disposeAfter = disposeAfter;
|
|
17786
|
+
this.#disposed = [];
|
|
17787
|
+
}
|
|
17788
|
+
else {
|
|
17789
|
+
this.#disposeAfter = undefined;
|
|
17790
|
+
this.#disposed = undefined;
|
|
17791
|
+
}
|
|
17792
|
+
this.#hasDispose = !!this.#dispose;
|
|
17793
|
+
this.#hasOnInsert = !!this.#onInsert;
|
|
17794
|
+
this.#hasDisposeAfter = !!this.#disposeAfter;
|
|
17795
|
+
this.noDisposeOnSet = !!noDisposeOnSet;
|
|
17796
|
+
this.noUpdateTTL = !!noUpdateTTL;
|
|
17797
|
+
this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
|
|
17798
|
+
this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
|
|
17799
|
+
this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
|
|
17800
|
+
this.ignoreFetchAbort = !!ignoreFetchAbort;
|
|
17801
|
+
// NB: maxEntrySize is set to maxSize if it's set
|
|
17802
|
+
if (this.maxEntrySize !== 0) {
|
|
17803
|
+
if (this.#maxSize !== 0) {
|
|
17804
|
+
if (!isPosInt(this.#maxSize)) {
|
|
17805
|
+
throw new TypeError('maxSize must be a positive integer if specified');
|
|
17806
|
+
}
|
|
17807
|
+
}
|
|
17808
|
+
if (!isPosInt(this.maxEntrySize)) {
|
|
17809
|
+
throw new TypeError('maxEntrySize must be a positive integer if specified');
|
|
17810
|
+
}
|
|
17811
|
+
this.#initializeSizeTracking();
|
|
17812
|
+
}
|
|
17813
|
+
this.allowStale = !!allowStale;
|
|
17814
|
+
this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
|
|
17815
|
+
this.updateAgeOnGet = !!updateAgeOnGet;
|
|
17816
|
+
this.updateAgeOnHas = !!updateAgeOnHas;
|
|
17817
|
+
this.ttlResolution =
|
|
17818
|
+
isPosInt(ttlResolution) || ttlResolution === 0 ?
|
|
17819
|
+
ttlResolution
|
|
17820
|
+
: 1;
|
|
17821
|
+
this.ttlAutopurge = !!ttlAutopurge;
|
|
17822
|
+
this.ttl = ttl || 0;
|
|
17823
|
+
if (this.ttl) {
|
|
17824
|
+
if (!isPosInt(this.ttl)) {
|
|
17825
|
+
throw new TypeError('ttl must be a positive integer if specified');
|
|
17826
|
+
}
|
|
17827
|
+
this.#initializeTTLTracking();
|
|
17828
|
+
}
|
|
17829
|
+
// do not allow completely unbounded caches
|
|
17830
|
+
if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
|
|
17831
|
+
throw new TypeError('At least one of max, maxSize, or ttl is required');
|
|
17832
|
+
}
|
|
17833
|
+
if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
|
|
17834
|
+
const code = 'LRU_CACHE_UNBOUNDED';
|
|
17835
|
+
if (shouldWarn(code)) {
|
|
17836
|
+
warned.add(code);
|
|
17837
|
+
const msg = 'TTL caching without ttlAutopurge, max, or maxSize can ' +
|
|
17838
|
+
'result in unbounded memory consumption.';
|
|
17839
|
+
emitWarning(msg, 'UnboundedCacheWarning', code, LRUCache);
|
|
17840
|
+
}
|
|
17841
|
+
}
|
|
17842
|
+
}
|
|
17843
|
+
/**
|
|
17844
|
+
* Return the number of ms left in the item's TTL. If item is not in cache,
|
|
17845
|
+
* returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
|
|
17846
|
+
*/
|
|
17847
|
+
getRemainingTTL(key) {
|
|
17848
|
+
return this.#keyMap.has(key) ? Infinity : 0;
|
|
17849
|
+
}
|
|
17850
|
+
#initializeTTLTracking() {
|
|
17851
|
+
const ttls = new ZeroArray(this.#max);
|
|
17852
|
+
const starts = new ZeroArray(this.#max);
|
|
17853
|
+
this.#ttls = ttls;
|
|
17854
|
+
this.#starts = starts;
|
|
17855
|
+
this.#setItemTTL = (index, ttl, start = this.#perf.now()) => {
|
|
17856
|
+
starts[index] = ttl !== 0 ? start : 0;
|
|
17857
|
+
ttls[index] = ttl;
|
|
17858
|
+
if (ttl !== 0 && this.ttlAutopurge) {
|
|
17859
|
+
const t = setTimeout(() => {
|
|
17860
|
+
if (this.#isStale(index)) {
|
|
17861
|
+
this.#delete(this.#keyList[index], 'expire');
|
|
17862
|
+
}
|
|
17863
|
+
}, ttl + 1);
|
|
17864
|
+
// unref() not supported on all platforms
|
|
17865
|
+
/* c8 ignore start */
|
|
17866
|
+
if (t.unref) {
|
|
17867
|
+
t.unref();
|
|
17868
|
+
}
|
|
17869
|
+
/* c8 ignore stop */
|
|
17870
|
+
}
|
|
17871
|
+
};
|
|
17872
|
+
this.#updateItemAge = index => {
|
|
17873
|
+
starts[index] = ttls[index] !== 0 ? this.#perf.now() : 0;
|
|
17874
|
+
};
|
|
17875
|
+
this.#statusTTL = (status, index) => {
|
|
17876
|
+
if (ttls[index]) {
|
|
17877
|
+
const ttl = ttls[index];
|
|
17878
|
+
const start = starts[index];
|
|
17879
|
+
/* c8 ignore next */
|
|
17880
|
+
if (!ttl || !start)
|
|
17881
|
+
return;
|
|
17882
|
+
status.ttl = ttl;
|
|
17883
|
+
status.start = start;
|
|
17884
|
+
status.now = cachedNow || getNow();
|
|
17885
|
+
const age = status.now - start;
|
|
17886
|
+
status.remainingTTL = ttl - age;
|
|
17887
|
+
}
|
|
17888
|
+
};
|
|
17889
|
+
// debounce calls to perf.now() to 1s so we're not hitting
|
|
17890
|
+
// that costly call repeatedly.
|
|
17891
|
+
let cachedNow = 0;
|
|
17892
|
+
const getNow = () => {
|
|
17893
|
+
const n = this.#perf.now();
|
|
17894
|
+
if (this.ttlResolution > 0) {
|
|
17895
|
+
cachedNow = n;
|
|
17896
|
+
const t = setTimeout(() => (cachedNow = 0), this.ttlResolution);
|
|
17897
|
+
// not available on all platforms
|
|
17898
|
+
/* c8 ignore start */
|
|
17899
|
+
if (t.unref) {
|
|
17900
|
+
t.unref();
|
|
17901
|
+
}
|
|
17902
|
+
/* c8 ignore stop */
|
|
17903
|
+
}
|
|
17904
|
+
return n;
|
|
17905
|
+
};
|
|
17906
|
+
this.getRemainingTTL = key => {
|
|
17907
|
+
const index = this.#keyMap.get(key);
|
|
17908
|
+
if (index === undefined) {
|
|
17909
|
+
return 0;
|
|
17910
|
+
}
|
|
17911
|
+
const ttl = ttls[index];
|
|
17912
|
+
const start = starts[index];
|
|
17913
|
+
if (!ttl || !start) {
|
|
17914
|
+
return Infinity;
|
|
17915
|
+
}
|
|
17916
|
+
const age = (cachedNow || getNow()) - start;
|
|
17917
|
+
return ttl - age;
|
|
17918
|
+
};
|
|
17919
|
+
this.#isStale = index => {
|
|
17920
|
+
const s = starts[index];
|
|
17921
|
+
const t = ttls[index];
|
|
17922
|
+
return !!t && !!s && (cachedNow || getNow()) - s > t;
|
|
17923
|
+
};
|
|
17924
|
+
}
|
|
17925
|
+
// conditionally set private methods related to TTL
|
|
17926
|
+
#updateItemAge = () => { };
|
|
17927
|
+
#statusTTL = () => { };
|
|
17928
|
+
#setItemTTL = () => { };
|
|
17929
|
+
/* c8 ignore stop */
|
|
17930
|
+
#isStale = () => false;
|
|
17931
|
+
#initializeSizeTracking() {
|
|
17932
|
+
const sizes = new ZeroArray(this.#max);
|
|
17933
|
+
this.#calculatedSize = 0;
|
|
17934
|
+
this.#sizes = sizes;
|
|
17935
|
+
this.#removeItemSize = index => {
|
|
17936
|
+
this.#calculatedSize -= sizes[index];
|
|
17937
|
+
sizes[index] = 0;
|
|
17938
|
+
};
|
|
17939
|
+
this.#requireSize = (k, v, size, sizeCalculation) => {
|
|
17940
|
+
// provisionally accept background fetches.
|
|
17941
|
+
// actual value size will be checked when they return.
|
|
17942
|
+
if (this.#isBackgroundFetch(v)) {
|
|
17943
|
+
return 0;
|
|
17944
|
+
}
|
|
17945
|
+
if (!isPosInt(size)) {
|
|
17946
|
+
if (sizeCalculation) {
|
|
17947
|
+
if (typeof sizeCalculation !== 'function') {
|
|
17948
|
+
throw new TypeError('sizeCalculation must be a function');
|
|
17949
|
+
}
|
|
17950
|
+
size = sizeCalculation(v, k);
|
|
17951
|
+
if (!isPosInt(size)) {
|
|
17952
|
+
throw new TypeError('sizeCalculation return invalid (expect positive integer)');
|
|
17953
|
+
}
|
|
17954
|
+
}
|
|
17955
|
+
else {
|
|
17956
|
+
throw new TypeError('invalid size value (must be positive integer). ' +
|
|
17957
|
+
'When maxSize or maxEntrySize is used, sizeCalculation ' +
|
|
17958
|
+
'or size must be set.');
|
|
17959
|
+
}
|
|
17960
|
+
}
|
|
17961
|
+
return size;
|
|
17962
|
+
};
|
|
17963
|
+
this.#addItemSize = (index, size, status) => {
|
|
17964
|
+
sizes[index] = size;
|
|
17965
|
+
if (this.#maxSize) {
|
|
17966
|
+
const maxSize = this.#maxSize - sizes[index];
|
|
17967
|
+
while (this.#calculatedSize > maxSize) {
|
|
17968
|
+
this.#evict(true);
|
|
17969
|
+
}
|
|
17970
|
+
}
|
|
17971
|
+
this.#calculatedSize += sizes[index];
|
|
17972
|
+
if (status) {
|
|
17973
|
+
status.entrySize = size;
|
|
17974
|
+
status.totalCalculatedSize = this.#calculatedSize;
|
|
17975
|
+
}
|
|
17976
|
+
};
|
|
17977
|
+
}
|
|
17978
|
+
#removeItemSize = _i => { };
|
|
17979
|
+
#addItemSize = (_i, _s, _st) => { };
|
|
17980
|
+
#requireSize = (_k, _v, size, sizeCalculation) => {
|
|
17981
|
+
if (size || sizeCalculation) {
|
|
17982
|
+
throw new TypeError('cannot set size without setting maxSize or maxEntrySize on cache');
|
|
17983
|
+
}
|
|
17984
|
+
return 0;
|
|
17426
17985
|
};
|
|
17427
|
-
|
|
17428
|
-
|
|
17429
|
-
|
|
17430
|
-
|
|
17431
|
-
|
|
17432
|
-
}
|
|
17433
|
-
|
|
17434
|
-
|
|
17435
|
-
|
|
17436
|
-
|
|
17437
|
-
|
|
17438
|
-
|
|
17439
|
-
|
|
17440
|
-
|
|
17441
|
-
|
|
17442
|
-
}
|
|
17443
|
-
|
|
17444
|
-
|
|
17445
|
-
|
|
17446
|
-
|
|
17447
|
-
|
|
17448
|
-
|
|
17449
|
-
|
|
17450
|
-
|
|
17451
|
-
|
|
17452
|
-
|
|
17453
|
-
|
|
17454
|
-
|
|
17455
|
-
|
|
17456
|
-
|
|
17457
|
-
|
|
17458
|
-
|
|
17459
|
-
|
|
17460
|
-
|
|
17461
|
-
|
|
17462
|
-
|
|
17463
|
-
|
|
17464
|
-
|
|
17465
|
-
|
|
17466
|
-
|
|
17467
|
-
|
|
17468
|
-
|
|
17469
|
-
|
|
17470
|
-
|
|
17471
|
-
|
|
17472
|
-
|
|
17473
|
-
|
|
17474
|
-
|
|
17475
|
-
|
|
17476
|
-
|
|
17477
|
-
|
|
17478
|
-
|
|
17479
|
-
|
|
17480
|
-
|
|
17481
|
-
|
|
17482
|
-
|
|
17483
|
-
|
|
17484
|
-
|
|
17485
|
-
|
|
17486
|
-
|
|
17487
|
-
|
|
17488
|
-
|
|
17489
|
-
|
|
17490
|
-
])
|
|
17491
|
-
|
|
17492
|
-
|
|
17493
|
-
|
|
17494
|
-
|
|
17495
|
-
|
|
17496
|
-
|
|
17497
|
-
|
|
17498
|
-
|
|
17499
|
-
|
|
17986
|
+
*#indexes({ allowStale = this.allowStale } = {}) {
|
|
17987
|
+
if (this.#size) {
|
|
17988
|
+
for (let i = this.#tail; true;) {
|
|
17989
|
+
if (!this.#isValidIndex(i)) {
|
|
17990
|
+
break;
|
|
17991
|
+
}
|
|
17992
|
+
if (allowStale || !this.#isStale(i)) {
|
|
17993
|
+
yield i;
|
|
17994
|
+
}
|
|
17995
|
+
if (i === this.#head) {
|
|
17996
|
+
break;
|
|
17997
|
+
}
|
|
17998
|
+
else {
|
|
17999
|
+
i = this.#prev[i];
|
|
18000
|
+
}
|
|
18001
|
+
}
|
|
18002
|
+
}
|
|
18003
|
+
}
|
|
18004
|
+
*#rindexes({ allowStale = this.allowStale } = {}) {
|
|
18005
|
+
if (this.#size) {
|
|
18006
|
+
for (let i = this.#head; true;) {
|
|
18007
|
+
if (!this.#isValidIndex(i)) {
|
|
18008
|
+
break;
|
|
18009
|
+
}
|
|
18010
|
+
if (allowStale || !this.#isStale(i)) {
|
|
18011
|
+
yield i;
|
|
18012
|
+
}
|
|
18013
|
+
if (i === this.#tail) {
|
|
18014
|
+
break;
|
|
18015
|
+
}
|
|
18016
|
+
else {
|
|
18017
|
+
i = this.#next[i];
|
|
18018
|
+
}
|
|
18019
|
+
}
|
|
18020
|
+
}
|
|
18021
|
+
}
|
|
18022
|
+
#isValidIndex(index) {
|
|
18023
|
+
return (index !== undefined &&
|
|
18024
|
+
this.#keyMap.get(this.#keyList[index]) === index);
|
|
18025
|
+
}
|
|
18026
|
+
/**
|
|
18027
|
+
* Return a generator yielding `[key, value]` pairs,
|
|
18028
|
+
* in order from most recently used to least recently used.
|
|
18029
|
+
*/
|
|
18030
|
+
*entries() {
|
|
18031
|
+
for (const i of this.#indexes()) {
|
|
18032
|
+
if (this.#valList[i] !== undefined &&
|
|
18033
|
+
this.#keyList[i] !== undefined &&
|
|
18034
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
18035
|
+
yield [this.#keyList[i], this.#valList[i]];
|
|
18036
|
+
}
|
|
18037
|
+
}
|
|
18038
|
+
}
|
|
18039
|
+
/**
|
|
18040
|
+
* Inverse order version of {@link LRUCache.entries}
|
|
18041
|
+
*
|
|
18042
|
+
* Return a generator yielding `[key, value]` pairs,
|
|
18043
|
+
* in order from least recently used to most recently used.
|
|
18044
|
+
*/
|
|
18045
|
+
*rentries() {
|
|
18046
|
+
for (const i of this.#rindexes()) {
|
|
18047
|
+
if (this.#valList[i] !== undefined &&
|
|
18048
|
+
this.#keyList[i] !== undefined &&
|
|
18049
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
18050
|
+
yield [this.#keyList[i], this.#valList[i]];
|
|
18051
|
+
}
|
|
18052
|
+
}
|
|
18053
|
+
}
|
|
18054
|
+
/**
|
|
18055
|
+
* Return a generator yielding the keys in the cache,
|
|
18056
|
+
* in order from most recently used to least recently used.
|
|
18057
|
+
*/
|
|
18058
|
+
*keys() {
|
|
18059
|
+
for (const i of this.#indexes()) {
|
|
18060
|
+
const k = this.#keyList[i];
|
|
18061
|
+
if (k !== undefined &&
|
|
18062
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
18063
|
+
yield k;
|
|
18064
|
+
}
|
|
18065
|
+
}
|
|
18066
|
+
}
|
|
18067
|
+
/**
|
|
18068
|
+
* Inverse order version of {@link LRUCache.keys}
|
|
18069
|
+
*
|
|
18070
|
+
* Return a generator yielding the keys in the cache,
|
|
18071
|
+
* in order from least recently used to most recently used.
|
|
18072
|
+
*/
|
|
18073
|
+
*rkeys() {
|
|
18074
|
+
for (const i of this.#rindexes()) {
|
|
18075
|
+
const k = this.#keyList[i];
|
|
18076
|
+
if (k !== undefined &&
|
|
18077
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
18078
|
+
yield k;
|
|
18079
|
+
}
|
|
18080
|
+
}
|
|
18081
|
+
}
|
|
18082
|
+
/**
|
|
18083
|
+
* Return a generator yielding the values in the cache,
|
|
18084
|
+
* in order from most recently used to least recently used.
|
|
18085
|
+
*/
|
|
18086
|
+
*values() {
|
|
18087
|
+
for (const i of this.#indexes()) {
|
|
18088
|
+
const v = this.#valList[i];
|
|
18089
|
+
if (v !== undefined &&
|
|
18090
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
18091
|
+
yield this.#valList[i];
|
|
18092
|
+
}
|
|
18093
|
+
}
|
|
18094
|
+
}
|
|
18095
|
+
/**
|
|
18096
|
+
* Inverse order version of {@link LRUCache.values}
|
|
18097
|
+
*
|
|
18098
|
+
* Return a generator yielding the values in the cache,
|
|
18099
|
+
* in order from least recently used to most recently used.
|
|
18100
|
+
*/
|
|
18101
|
+
*rvalues() {
|
|
18102
|
+
for (const i of this.#rindexes()) {
|
|
18103
|
+
const v = this.#valList[i];
|
|
18104
|
+
if (v !== undefined &&
|
|
18105
|
+
!this.#isBackgroundFetch(this.#valList[i])) {
|
|
18106
|
+
yield this.#valList[i];
|
|
18107
|
+
}
|
|
18108
|
+
}
|
|
18109
|
+
}
|
|
18110
|
+
/**
|
|
18111
|
+
* Iterating over the cache itself yields the same results as
|
|
18112
|
+
* {@link LRUCache.entries}
|
|
18113
|
+
*/
|
|
18114
|
+
[Symbol.iterator]() {
|
|
18115
|
+
return this.entries();
|
|
18116
|
+
}
|
|
18117
|
+
/**
|
|
18118
|
+
* A String value that is used in the creation of the default string
|
|
18119
|
+
* description of an object. Called by the built-in method
|
|
18120
|
+
* `Object.prototype.toString`.
|
|
18121
|
+
*/
|
|
18122
|
+
[Symbol.toStringTag] = 'LRUCache';
|
|
18123
|
+
/**
|
|
18124
|
+
* Find a value for which the supplied fn method returns a truthy value,
|
|
18125
|
+
* similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
|
|
18126
|
+
*/
|
|
18127
|
+
find(fn, getOptions = {}) {
|
|
18128
|
+
for (const i of this.#indexes()) {
|
|
18129
|
+
const v = this.#valList[i];
|
|
18130
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
18131
|
+
if (value === undefined)
|
|
18132
|
+
continue;
|
|
18133
|
+
if (fn(value, this.#keyList[i], this)) {
|
|
18134
|
+
return this.get(this.#keyList[i], getOptions);
|
|
18135
|
+
}
|
|
18136
|
+
}
|
|
18137
|
+
}
|
|
18138
|
+
/**
|
|
18139
|
+
* Call the supplied function on each item in the cache, in order from most
|
|
18140
|
+
* recently used to least recently used.
|
|
18141
|
+
*
|
|
18142
|
+
* `fn` is called as `fn(value, key, cache)`.
|
|
18143
|
+
*
|
|
18144
|
+
* If `thisp` is provided, function will be called in the `this`-context of
|
|
18145
|
+
* the provided object, or the cache if no `thisp` object is provided.
|
|
18146
|
+
*
|
|
18147
|
+
* Does not update age or recenty of use, or iterate over stale values.
|
|
18148
|
+
*/
|
|
18149
|
+
forEach(fn, thisp = this) {
|
|
18150
|
+
for (const i of this.#indexes()) {
|
|
18151
|
+
const v = this.#valList[i];
|
|
18152
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
18153
|
+
if (value === undefined)
|
|
18154
|
+
continue;
|
|
18155
|
+
fn.call(thisp, value, this.#keyList[i], this);
|
|
18156
|
+
}
|
|
18157
|
+
}
|
|
18158
|
+
/**
|
|
18159
|
+
* The same as {@link LRUCache.forEach} but items are iterated over in
|
|
18160
|
+
* reverse order. (ie, less recently used items are iterated over first.)
|
|
18161
|
+
*/
|
|
18162
|
+
rforEach(fn, thisp = this) {
|
|
18163
|
+
for (const i of this.#rindexes()) {
|
|
18164
|
+
const v = this.#valList[i];
|
|
18165
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
18166
|
+
if (value === undefined)
|
|
18167
|
+
continue;
|
|
18168
|
+
fn.call(thisp, value, this.#keyList[i], this);
|
|
18169
|
+
}
|
|
18170
|
+
}
|
|
18171
|
+
/**
|
|
18172
|
+
* Delete any stale entries. Returns true if anything was removed,
|
|
18173
|
+
* false otherwise.
|
|
18174
|
+
*/
|
|
18175
|
+
purgeStale() {
|
|
18176
|
+
let deleted = false;
|
|
18177
|
+
for (const i of this.#rindexes({ allowStale: true })) {
|
|
18178
|
+
if (this.#isStale(i)) {
|
|
18179
|
+
this.#delete(this.#keyList[i], 'expire');
|
|
18180
|
+
deleted = true;
|
|
18181
|
+
}
|
|
18182
|
+
}
|
|
18183
|
+
return deleted;
|
|
18184
|
+
}
|
|
18185
|
+
/**
|
|
18186
|
+
* Get the extended info about a given entry, to get its value, size, and
|
|
18187
|
+
* TTL info simultaneously. Returns `undefined` if the key is not present.
|
|
18188
|
+
*
|
|
18189
|
+
* Unlike {@link LRUCache#dump}, which is designed to be portable and survive
|
|
18190
|
+
* serialization, the `start` value is always the current timestamp, and the
|
|
18191
|
+
* `ttl` is a calculated remaining time to live (negative if expired).
|
|
18192
|
+
*
|
|
18193
|
+
* Always returns stale values, if their info is found in the cache, so be
|
|
18194
|
+
* sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
|
|
18195
|
+
* if relevant.
|
|
18196
|
+
*/
|
|
18197
|
+
info(key) {
|
|
18198
|
+
const i = this.#keyMap.get(key);
|
|
18199
|
+
if (i === undefined)
|
|
18200
|
+
return undefined;
|
|
18201
|
+
const v = this.#valList[i];
|
|
18202
|
+
/* c8 ignore start - this isn't tested for the info function,
|
|
18203
|
+
* but it's the same logic as found in other places. */
|
|
18204
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
18205
|
+
if (value === undefined)
|
|
18206
|
+
return undefined;
|
|
18207
|
+
/* c8 ignore end */
|
|
18208
|
+
const entry = { value };
|
|
18209
|
+
if (this.#ttls && this.#starts) {
|
|
18210
|
+
const ttl = this.#ttls[i];
|
|
18211
|
+
const start = this.#starts[i];
|
|
18212
|
+
if (ttl && start) {
|
|
18213
|
+
const remain = ttl - (this.#perf.now() - start);
|
|
18214
|
+
entry.ttl = remain;
|
|
18215
|
+
entry.start = Date.now();
|
|
18216
|
+
}
|
|
18217
|
+
}
|
|
18218
|
+
if (this.#sizes) {
|
|
18219
|
+
entry.size = this.#sizes[i];
|
|
18220
|
+
}
|
|
18221
|
+
return entry;
|
|
18222
|
+
}
|
|
18223
|
+
/**
|
|
18224
|
+
* Return an array of [key, {@link LRUCache.Entry}] tuples which can be
|
|
18225
|
+
* passed to {@link LRUCache#load}.
|
|
18226
|
+
*
|
|
18227
|
+
* The `start` fields are calculated relative to a portable `Date.now()`
|
|
18228
|
+
* timestamp, even if `performance.now()` is available.
|
|
18229
|
+
*
|
|
18230
|
+
* Stale entries are always included in the `dump`, even if
|
|
18231
|
+
* {@link LRUCache.OptionsBase.allowStale} is false.
|
|
18232
|
+
*
|
|
18233
|
+
* Note: this returns an actual array, not a generator, so it can be more
|
|
18234
|
+
* easily passed around.
|
|
18235
|
+
*/
|
|
18236
|
+
dump() {
|
|
18237
|
+
const arr = [];
|
|
18238
|
+
for (const i of this.#indexes({ allowStale: true })) {
|
|
18239
|
+
const key = this.#keyList[i];
|
|
18240
|
+
const v = this.#valList[i];
|
|
18241
|
+
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
18242
|
+
if (value === undefined || key === undefined)
|
|
18243
|
+
continue;
|
|
18244
|
+
const entry = { value };
|
|
18245
|
+
if (this.#ttls && this.#starts) {
|
|
18246
|
+
entry.ttl = this.#ttls[i];
|
|
18247
|
+
// always dump the start relative to a portable timestamp
|
|
18248
|
+
// it's ok for this to be a bit slow, it's a rare operation.
|
|
18249
|
+
const age = this.#perf.now() - this.#starts[i];
|
|
18250
|
+
entry.start = Math.floor(Date.now() - age);
|
|
18251
|
+
}
|
|
18252
|
+
if (this.#sizes) {
|
|
18253
|
+
entry.size = this.#sizes[i];
|
|
18254
|
+
}
|
|
18255
|
+
arr.unshift([key, entry]);
|
|
18256
|
+
}
|
|
18257
|
+
return arr;
|
|
18258
|
+
}
|
|
18259
|
+
/**
|
|
18260
|
+
* Reset the cache and load in the items in entries in the order listed.
|
|
18261
|
+
*
|
|
18262
|
+
* The shape of the resulting cache may be different if the same options are
|
|
18263
|
+
* not used in both caches.
|
|
18264
|
+
*
|
|
18265
|
+
* The `start` fields are assumed to be calculated relative to a portable
|
|
18266
|
+
* `Date.now()` timestamp, even if `performance.now()` is available.
|
|
18267
|
+
*/
|
|
18268
|
+
load(arr) {
|
|
18269
|
+
this.clear();
|
|
18270
|
+
for (const [key, entry] of arr) {
|
|
18271
|
+
if (entry.start) {
|
|
18272
|
+
// entry.start is a portable timestamp, but we may be using
|
|
18273
|
+
// node's performance.now(), so calculate the offset, so that
|
|
18274
|
+
// we get the intended remaining TTL, no matter how long it's
|
|
18275
|
+
// been on ice.
|
|
18276
|
+
//
|
|
18277
|
+
// it's ok for this to be a bit slow, it's a rare operation.
|
|
18278
|
+
const age = Date.now() - entry.start;
|
|
18279
|
+
entry.start = this.#perf.now() - age;
|
|
18280
|
+
}
|
|
18281
|
+
this.set(key, entry.value, entry);
|
|
18282
|
+
}
|
|
18283
|
+
}
|
|
18284
|
+
/**
|
|
18285
|
+
* Add a value to the cache.
|
|
18286
|
+
*
|
|
18287
|
+
* Note: if `undefined` is specified as a value, this is an alias for
|
|
18288
|
+
* {@link LRUCache#delete}
|
|
18289
|
+
*
|
|
18290
|
+
* Fields on the {@link LRUCache.SetOptions} options param will override
|
|
18291
|
+
* their corresponding values in the constructor options for the scope
|
|
18292
|
+
* of this single `set()` operation.
|
|
18293
|
+
*
|
|
18294
|
+
* If `start` is provided, then that will set the effective start
|
|
18295
|
+
* time for the TTL calculation. Note that this must be a previous
|
|
18296
|
+
* value of `performance.now()` if supported, or a previous value of
|
|
18297
|
+
* `Date.now()` if not.
|
|
18298
|
+
*
|
|
18299
|
+
* Options object may also include `size`, which will prevent
|
|
18300
|
+
* calling the `sizeCalculation` function and just use the specified
|
|
18301
|
+
* number if it is a positive integer, and `noDisposeOnSet` which
|
|
18302
|
+
* will prevent calling a `dispose` function in the case of
|
|
18303
|
+
* overwrites.
|
|
18304
|
+
*
|
|
18305
|
+
* If the `size` (or return value of `sizeCalculation`) for a given
|
|
18306
|
+
* entry is greater than `maxEntrySize`, then the item will not be
|
|
18307
|
+
* added to the cache.
|
|
18308
|
+
*
|
|
18309
|
+
* Will update the recency of the entry.
|
|
18310
|
+
*
|
|
18311
|
+
* If the value is `undefined`, then this is an alias for
|
|
18312
|
+
* `cache.delete(key)`. `undefined` is never stored in the cache.
|
|
18313
|
+
*/
|
|
18314
|
+
set(k, v, setOptions = {}) {
|
|
18315
|
+
if (v === undefined) {
|
|
18316
|
+
this.delete(k);
|
|
18317
|
+
return this;
|
|
18318
|
+
}
|
|
18319
|
+
const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status, } = setOptions;
|
|
18320
|
+
let { noUpdateTTL = this.noUpdateTTL } = setOptions;
|
|
18321
|
+
const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation);
|
|
18322
|
+
// if the item doesn't fit, don't do anything
|
|
18323
|
+
// NB: maxEntrySize set to maxSize by default
|
|
18324
|
+
if (this.maxEntrySize && size > this.maxEntrySize) {
|
|
18325
|
+
if (status) {
|
|
18326
|
+
status.set = 'miss';
|
|
18327
|
+
status.maxEntrySizeExceeded = true;
|
|
18328
|
+
}
|
|
18329
|
+
// have to delete, in case something is there already.
|
|
18330
|
+
this.#delete(k, 'set');
|
|
18331
|
+
return this;
|
|
18332
|
+
}
|
|
18333
|
+
let index = this.#size === 0 ? undefined : this.#keyMap.get(k);
|
|
18334
|
+
if (index === undefined) {
|
|
18335
|
+
// addition
|
|
18336
|
+
index = (this.#size === 0 ? this.#tail
|
|
18337
|
+
: this.#free.length !== 0 ? this.#free.pop()
|
|
18338
|
+
: this.#size === this.#max ? this.#evict(false)
|
|
18339
|
+
: this.#size);
|
|
18340
|
+
this.#keyList[index] = k;
|
|
18341
|
+
this.#valList[index] = v;
|
|
18342
|
+
this.#keyMap.set(k, index);
|
|
18343
|
+
this.#next[this.#tail] = index;
|
|
18344
|
+
this.#prev[index] = this.#tail;
|
|
18345
|
+
this.#tail = index;
|
|
18346
|
+
this.#size++;
|
|
18347
|
+
this.#addItemSize(index, size, status);
|
|
18348
|
+
if (status)
|
|
18349
|
+
status.set = 'add';
|
|
18350
|
+
noUpdateTTL = false;
|
|
18351
|
+
if (this.#hasOnInsert) {
|
|
18352
|
+
this.#onInsert?.(v, k, 'add');
|
|
18353
|
+
}
|
|
18354
|
+
}
|
|
18355
|
+
else {
|
|
18356
|
+
// update
|
|
18357
|
+
this.#moveToTail(index);
|
|
18358
|
+
const oldVal = this.#valList[index];
|
|
18359
|
+
if (v !== oldVal) {
|
|
18360
|
+
if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
|
|
18361
|
+
oldVal.__abortController.abort(new Error('replaced'));
|
|
18362
|
+
const { __staleWhileFetching: s } = oldVal;
|
|
18363
|
+
if (s !== undefined && !noDisposeOnSet) {
|
|
18364
|
+
if (this.#hasDispose) {
|
|
18365
|
+
this.#dispose?.(s, k, 'set');
|
|
18366
|
+
}
|
|
18367
|
+
if (this.#hasDisposeAfter) {
|
|
18368
|
+
this.#disposed?.push([s, k, 'set']);
|
|
18369
|
+
}
|
|
18370
|
+
}
|
|
18371
|
+
}
|
|
18372
|
+
else if (!noDisposeOnSet) {
|
|
18373
|
+
if (this.#hasDispose) {
|
|
18374
|
+
this.#dispose?.(oldVal, k, 'set');
|
|
18375
|
+
}
|
|
18376
|
+
if (this.#hasDisposeAfter) {
|
|
18377
|
+
this.#disposed?.push([oldVal, k, 'set']);
|
|
18378
|
+
}
|
|
18379
|
+
}
|
|
18380
|
+
this.#removeItemSize(index);
|
|
18381
|
+
this.#addItemSize(index, size, status);
|
|
18382
|
+
this.#valList[index] = v;
|
|
18383
|
+
if (status) {
|
|
18384
|
+
status.set = 'replace';
|
|
18385
|
+
const oldValue = oldVal && this.#isBackgroundFetch(oldVal) ?
|
|
18386
|
+
oldVal.__staleWhileFetching
|
|
18387
|
+
: oldVal;
|
|
18388
|
+
if (oldValue !== undefined)
|
|
18389
|
+
status.oldValue = oldValue;
|
|
18390
|
+
}
|
|
18391
|
+
}
|
|
18392
|
+
else if (status) {
|
|
18393
|
+
status.set = 'update';
|
|
18394
|
+
}
|
|
18395
|
+
if (this.#hasOnInsert) {
|
|
18396
|
+
this.onInsert?.(v, k, v === oldVal ? 'update' : 'replace');
|
|
18397
|
+
}
|
|
18398
|
+
}
|
|
18399
|
+
if (ttl !== 0 && !this.#ttls) {
|
|
18400
|
+
this.#initializeTTLTracking();
|
|
18401
|
+
}
|
|
18402
|
+
if (this.#ttls) {
|
|
18403
|
+
if (!noUpdateTTL) {
|
|
18404
|
+
this.#setItemTTL(index, ttl, start);
|
|
18405
|
+
}
|
|
18406
|
+
if (status)
|
|
18407
|
+
this.#statusTTL(status, index);
|
|
18408
|
+
}
|
|
18409
|
+
if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
|
|
18410
|
+
const dt = this.#disposed;
|
|
18411
|
+
let task;
|
|
18412
|
+
while ((task = dt?.shift())) {
|
|
18413
|
+
this.#disposeAfter?.(...task);
|
|
18414
|
+
}
|
|
18415
|
+
}
|
|
18416
|
+
return this;
|
|
18417
|
+
}
|
|
18418
|
+
/**
|
|
18419
|
+
* Evict the least recently used item, returning its value or
|
|
18420
|
+
* `undefined` if cache is empty.
|
|
18421
|
+
*/
|
|
18422
|
+
pop() {
|
|
18423
|
+
try {
|
|
18424
|
+
while (this.#size) {
|
|
18425
|
+
const val = this.#valList[this.#head];
|
|
18426
|
+
this.#evict(true);
|
|
18427
|
+
if (this.#isBackgroundFetch(val)) {
|
|
18428
|
+
if (val.__staleWhileFetching) {
|
|
18429
|
+
return val.__staleWhileFetching;
|
|
18430
|
+
}
|
|
18431
|
+
}
|
|
18432
|
+
else if (val !== undefined) {
|
|
18433
|
+
return val;
|
|
18434
|
+
}
|
|
18435
|
+
}
|
|
18436
|
+
}
|
|
18437
|
+
finally {
|
|
18438
|
+
if (this.#hasDisposeAfter && this.#disposed) {
|
|
18439
|
+
const dt = this.#disposed;
|
|
18440
|
+
let task;
|
|
18441
|
+
while ((task = dt?.shift())) {
|
|
18442
|
+
this.#disposeAfter?.(...task);
|
|
18443
|
+
}
|
|
18444
|
+
}
|
|
18445
|
+
}
|
|
18446
|
+
}
|
|
18447
|
+
#evict(free) {
|
|
18448
|
+
const head = this.#head;
|
|
18449
|
+
const k = this.#keyList[head];
|
|
18450
|
+
const v = this.#valList[head];
|
|
18451
|
+
if (this.#hasFetchMethod && this.#isBackgroundFetch(v)) {
|
|
18452
|
+
v.__abortController.abort(new Error('evicted'));
|
|
18453
|
+
}
|
|
18454
|
+
else if (this.#hasDispose || this.#hasDisposeAfter) {
|
|
18455
|
+
if (this.#hasDispose) {
|
|
18456
|
+
this.#dispose?.(v, k, 'evict');
|
|
18457
|
+
}
|
|
18458
|
+
if (this.#hasDisposeAfter) {
|
|
18459
|
+
this.#disposed?.push([v, k, 'evict']);
|
|
18460
|
+
}
|
|
18461
|
+
}
|
|
18462
|
+
this.#removeItemSize(head);
|
|
18463
|
+
// if we aren't about to use the index, then null these out
|
|
18464
|
+
if (free) {
|
|
18465
|
+
this.#keyList[head] = undefined;
|
|
18466
|
+
this.#valList[head] = undefined;
|
|
18467
|
+
this.#free.push(head);
|
|
18468
|
+
}
|
|
18469
|
+
if (this.#size === 1) {
|
|
18470
|
+
this.#head = this.#tail = 0;
|
|
18471
|
+
this.#free.length = 0;
|
|
18472
|
+
}
|
|
18473
|
+
else {
|
|
18474
|
+
this.#head = this.#next[head];
|
|
18475
|
+
}
|
|
18476
|
+
this.#keyMap.delete(k);
|
|
18477
|
+
this.#size--;
|
|
18478
|
+
return head;
|
|
18479
|
+
}
|
|
18480
|
+
/**
|
|
18481
|
+
* Check if a key is in the cache, without updating the recency of use.
|
|
18482
|
+
* Will return false if the item is stale, even though it is technically
|
|
18483
|
+
* in the cache.
|
|
18484
|
+
*
|
|
18485
|
+
* Check if a key is in the cache, without updating the recency of
|
|
18486
|
+
* use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
|
|
18487
|
+
* to `true` in either the options or the constructor.
|
|
18488
|
+
*
|
|
18489
|
+
* Will return `false` if the item is stale, even though it is technically in
|
|
18490
|
+
* the cache. The difference can be determined (if it matters) by using a
|
|
18491
|
+
* `status` argument, and inspecting the `has` field.
|
|
18492
|
+
*
|
|
18493
|
+
* Will not update item age unless
|
|
18494
|
+
* {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
|
|
18495
|
+
*/
|
|
18496
|
+
has(k, hasOptions = {}) {
|
|
18497
|
+
const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
|
|
18498
|
+
const index = this.#keyMap.get(k);
|
|
18499
|
+
if (index !== undefined) {
|
|
18500
|
+
const v = this.#valList[index];
|
|
18501
|
+
if (this.#isBackgroundFetch(v) &&
|
|
18502
|
+
v.__staleWhileFetching === undefined) {
|
|
18503
|
+
return false;
|
|
18504
|
+
}
|
|
18505
|
+
if (!this.#isStale(index)) {
|
|
18506
|
+
if (updateAgeOnHas) {
|
|
18507
|
+
this.#updateItemAge(index);
|
|
18508
|
+
}
|
|
18509
|
+
if (status) {
|
|
18510
|
+
status.has = 'hit';
|
|
18511
|
+
this.#statusTTL(status, index);
|
|
18512
|
+
}
|
|
18513
|
+
return true;
|
|
18514
|
+
}
|
|
18515
|
+
else if (status) {
|
|
18516
|
+
status.has = 'stale';
|
|
18517
|
+
this.#statusTTL(status, index);
|
|
18518
|
+
}
|
|
18519
|
+
}
|
|
18520
|
+
else if (status) {
|
|
18521
|
+
status.has = 'miss';
|
|
18522
|
+
}
|
|
18523
|
+
return false;
|
|
18524
|
+
}
|
|
18525
|
+
/**
|
|
18526
|
+
* Like {@link LRUCache#get} but doesn't update recency or delete stale
|
|
18527
|
+
* items.
|
|
18528
|
+
*
|
|
18529
|
+
* Returns `undefined` if the item is stale, unless
|
|
18530
|
+
* {@link LRUCache.OptionsBase.allowStale} is set.
|
|
18531
|
+
*/
|
|
18532
|
+
peek(k, peekOptions = {}) {
|
|
18533
|
+
const { allowStale = this.allowStale } = peekOptions;
|
|
18534
|
+
const index = this.#keyMap.get(k);
|
|
18535
|
+
if (index === undefined ||
|
|
18536
|
+
(!allowStale && this.#isStale(index))) {
|
|
18537
|
+
return;
|
|
18538
|
+
}
|
|
18539
|
+
const v = this.#valList[index];
|
|
18540
|
+
// either stale and allowed, or forcing a refresh of non-stale value
|
|
18541
|
+
return this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
|
|
18542
|
+
}
|
|
18543
|
+
#backgroundFetch(k, index, options, context) {
|
|
18544
|
+
const v = index === undefined ? undefined : this.#valList[index];
|
|
18545
|
+
if (this.#isBackgroundFetch(v)) {
|
|
18546
|
+
return v;
|
|
18547
|
+
}
|
|
18548
|
+
const ac = new AC();
|
|
18549
|
+
const { signal } = options;
|
|
18550
|
+
// when/if our AC signals, then stop listening to theirs.
|
|
18551
|
+
signal?.addEventListener('abort', () => ac.abort(signal.reason), {
|
|
18552
|
+
signal: ac.signal,
|
|
18553
|
+
});
|
|
18554
|
+
const fetchOpts = {
|
|
18555
|
+
signal: ac.signal,
|
|
18556
|
+
options,
|
|
18557
|
+
context,
|
|
18558
|
+
};
|
|
18559
|
+
const cb = (v, updateCache = false) => {
|
|
18560
|
+
const { aborted } = ac.signal;
|
|
18561
|
+
const ignoreAbort = options.ignoreFetchAbort && v !== undefined;
|
|
18562
|
+
if (options.status) {
|
|
18563
|
+
if (aborted && !updateCache) {
|
|
18564
|
+
options.status.fetchAborted = true;
|
|
18565
|
+
options.status.fetchError = ac.signal.reason;
|
|
18566
|
+
if (ignoreAbort)
|
|
18567
|
+
options.status.fetchAbortIgnored = true;
|
|
18568
|
+
}
|
|
18569
|
+
else {
|
|
18570
|
+
options.status.fetchResolved = true;
|
|
18571
|
+
}
|
|
18572
|
+
}
|
|
18573
|
+
if (aborted && !ignoreAbort && !updateCache) {
|
|
18574
|
+
return fetchFail(ac.signal.reason);
|
|
18575
|
+
}
|
|
18576
|
+
// either we didn't abort, and are still here, or we did, and ignored
|
|
18577
|
+
const bf = p;
|
|
18578
|
+
// if nothing else has been written there but we're set to update the
|
|
18579
|
+
// cache and ignore the abort, or if it's still pending on this specific
|
|
18580
|
+
// background request, then write it to the cache.
|
|
18581
|
+
const vl = this.#valList[index];
|
|
18582
|
+
if (vl === p || ignoreAbort && updateCache && vl === undefined) {
|
|
18583
|
+
if (v === undefined) {
|
|
18584
|
+
if (bf.__staleWhileFetching !== undefined) {
|
|
18585
|
+
this.#valList[index] = bf.__staleWhileFetching;
|
|
18586
|
+
}
|
|
18587
|
+
else {
|
|
18588
|
+
this.#delete(k, 'fetch');
|
|
18589
|
+
}
|
|
18590
|
+
}
|
|
18591
|
+
else {
|
|
18592
|
+
if (options.status)
|
|
18593
|
+
options.status.fetchUpdated = true;
|
|
18594
|
+
this.set(k, v, fetchOpts.options);
|
|
18595
|
+
}
|
|
18596
|
+
}
|
|
18597
|
+
return v;
|
|
18598
|
+
};
|
|
18599
|
+
const eb = (er) => {
|
|
18600
|
+
if (options.status) {
|
|
18601
|
+
options.status.fetchRejected = true;
|
|
18602
|
+
options.status.fetchError = er;
|
|
18603
|
+
}
|
|
18604
|
+
return fetchFail(er);
|
|
18605
|
+
};
|
|
18606
|
+
const fetchFail = (er) => {
|
|
18607
|
+
const { aborted } = ac.signal;
|
|
18608
|
+
const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
|
|
18609
|
+
const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
|
|
18610
|
+
const noDelete = allowStale || options.noDeleteOnFetchRejection;
|
|
18611
|
+
const bf = p;
|
|
18612
|
+
if (this.#valList[index] === p) {
|
|
18613
|
+
// if we allow stale on fetch rejections, then we need to ensure that
|
|
18614
|
+
// the stale value is not removed from the cache when the fetch fails.
|
|
18615
|
+
const del = !noDelete || bf.__staleWhileFetching === undefined;
|
|
18616
|
+
if (del) {
|
|
18617
|
+
this.#delete(k, 'fetch');
|
|
18618
|
+
}
|
|
18619
|
+
else if (!allowStaleAborted) {
|
|
18620
|
+
// still replace the *promise* with the stale value,
|
|
18621
|
+
// since we are done with the promise at this point.
|
|
18622
|
+
// leave it untouched if we're still waiting for an
|
|
18623
|
+
// aborted background fetch that hasn't yet returned.
|
|
18624
|
+
this.#valList[index] = bf.__staleWhileFetching;
|
|
18625
|
+
}
|
|
18626
|
+
}
|
|
18627
|
+
if (allowStale) {
|
|
18628
|
+
if (options.status && bf.__staleWhileFetching !== undefined) {
|
|
18629
|
+
options.status.returnedStale = true;
|
|
18630
|
+
}
|
|
18631
|
+
return bf.__staleWhileFetching;
|
|
18632
|
+
}
|
|
18633
|
+
else if (bf.__returned === bf) {
|
|
18634
|
+
throw er;
|
|
18635
|
+
}
|
|
18636
|
+
};
|
|
18637
|
+
const pcall = (res, rej) => {
|
|
18638
|
+
const fmp = this.#fetchMethod?.(k, v, fetchOpts);
|
|
18639
|
+
if (fmp && fmp instanceof Promise) {
|
|
18640
|
+
fmp.then(v => res(v === undefined ? undefined : v), rej);
|
|
18641
|
+
}
|
|
18642
|
+
// ignored, we go until we finish, regardless.
|
|
18643
|
+
// defer check until we are actually aborting,
|
|
18644
|
+
// so fetchMethod can override.
|
|
18645
|
+
ac.signal.addEventListener('abort', () => {
|
|
18646
|
+
if (!options.ignoreFetchAbort ||
|
|
18647
|
+
options.allowStaleOnFetchAbort) {
|
|
18648
|
+
res(undefined);
|
|
18649
|
+
// when it eventually resolves, update the cache.
|
|
18650
|
+
if (options.allowStaleOnFetchAbort) {
|
|
18651
|
+
res = v => cb(v, true);
|
|
18652
|
+
}
|
|
18653
|
+
}
|
|
18654
|
+
});
|
|
18655
|
+
};
|
|
18656
|
+
if (options.status)
|
|
18657
|
+
options.status.fetchDispatched = true;
|
|
18658
|
+
const p = new Promise(pcall).then(cb, eb);
|
|
18659
|
+
const bf = Object.assign(p, {
|
|
18660
|
+
__abortController: ac,
|
|
18661
|
+
__staleWhileFetching: v,
|
|
18662
|
+
__returned: undefined,
|
|
18663
|
+
});
|
|
18664
|
+
if (index === undefined) {
|
|
18665
|
+
// internal, don't expose status.
|
|
18666
|
+
this.set(k, bf, { ...fetchOpts.options, status: undefined });
|
|
18667
|
+
index = this.#keyMap.get(k);
|
|
18668
|
+
}
|
|
18669
|
+
else {
|
|
18670
|
+
this.#valList[index] = bf;
|
|
18671
|
+
}
|
|
18672
|
+
return bf;
|
|
18673
|
+
}
|
|
18674
|
+
#isBackgroundFetch(p) {
|
|
18675
|
+
if (!this.#hasFetchMethod)
|
|
18676
|
+
return false;
|
|
18677
|
+
const b = p;
|
|
18678
|
+
return (!!b &&
|
|
18679
|
+
b instanceof Promise &&
|
|
18680
|
+
b.hasOwnProperty('__staleWhileFetching') &&
|
|
18681
|
+
b.__abortController instanceof AC);
|
|
18682
|
+
}
|
|
18683
|
+
async fetch(k, fetchOptions = {}) {
|
|
18684
|
+
const {
|
|
18685
|
+
// get options
|
|
18686
|
+
allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet,
|
|
18687
|
+
// set options
|
|
18688
|
+
ttl = this.ttl, noDisposeOnSet = this.noDisposeOnSet, size = 0, sizeCalculation = this.sizeCalculation, noUpdateTTL = this.noUpdateTTL,
|
|
18689
|
+
// fetch exclusive options
|
|
18690
|
+
noDeleteOnFetchRejection = this.noDeleteOnFetchRejection, allowStaleOnFetchRejection = this.allowStaleOnFetchRejection, ignoreFetchAbort = this.ignoreFetchAbort, allowStaleOnFetchAbort = this.allowStaleOnFetchAbort, context, forceRefresh = false, status, signal, } = fetchOptions;
|
|
18691
|
+
if (!this.#hasFetchMethod) {
|
|
18692
|
+
if (status)
|
|
18693
|
+
status.fetch = 'get';
|
|
18694
|
+
return this.get(k, {
|
|
18695
|
+
allowStale,
|
|
18696
|
+
updateAgeOnGet,
|
|
18697
|
+
noDeleteOnStaleGet,
|
|
18698
|
+
status,
|
|
18699
|
+
});
|
|
18700
|
+
}
|
|
18701
|
+
const options = {
|
|
18702
|
+
allowStale,
|
|
18703
|
+
updateAgeOnGet,
|
|
18704
|
+
noDeleteOnStaleGet,
|
|
18705
|
+
ttl,
|
|
18706
|
+
noDisposeOnSet,
|
|
18707
|
+
size,
|
|
18708
|
+
sizeCalculation,
|
|
18709
|
+
noUpdateTTL,
|
|
18710
|
+
noDeleteOnFetchRejection,
|
|
18711
|
+
allowStaleOnFetchRejection,
|
|
18712
|
+
allowStaleOnFetchAbort,
|
|
18713
|
+
ignoreFetchAbort,
|
|
18714
|
+
status,
|
|
18715
|
+
signal,
|
|
18716
|
+
};
|
|
18717
|
+
let index = this.#keyMap.get(k);
|
|
18718
|
+
if (index === undefined) {
|
|
18719
|
+
if (status)
|
|
18720
|
+
status.fetch = 'miss';
|
|
18721
|
+
const p = this.#backgroundFetch(k, index, options, context);
|
|
18722
|
+
return (p.__returned = p);
|
|
18723
|
+
}
|
|
18724
|
+
else {
|
|
18725
|
+
// in cache, maybe already fetching
|
|
18726
|
+
const v = this.#valList[index];
|
|
18727
|
+
if (this.#isBackgroundFetch(v)) {
|
|
18728
|
+
const stale = allowStale && v.__staleWhileFetching !== undefined;
|
|
18729
|
+
if (status) {
|
|
18730
|
+
status.fetch = 'inflight';
|
|
18731
|
+
if (stale)
|
|
18732
|
+
status.returnedStale = true;
|
|
18733
|
+
}
|
|
18734
|
+
return stale ? v.__staleWhileFetching : (v.__returned = v);
|
|
18735
|
+
}
|
|
18736
|
+
// if we force a refresh, that means do NOT serve the cached value,
|
|
18737
|
+
// unless we are already in the process of refreshing the cache.
|
|
18738
|
+
const isStale = this.#isStale(index);
|
|
18739
|
+
if (!forceRefresh && !isStale) {
|
|
18740
|
+
if (status)
|
|
18741
|
+
status.fetch = 'hit';
|
|
18742
|
+
this.#moveToTail(index);
|
|
18743
|
+
if (updateAgeOnGet) {
|
|
18744
|
+
this.#updateItemAge(index);
|
|
18745
|
+
}
|
|
18746
|
+
if (status)
|
|
18747
|
+
this.#statusTTL(status, index);
|
|
18748
|
+
return v;
|
|
18749
|
+
}
|
|
18750
|
+
// ok, it is stale or a forced refresh, and not already fetching.
|
|
18751
|
+
// refresh the cache.
|
|
18752
|
+
const p = this.#backgroundFetch(k, index, options, context);
|
|
18753
|
+
const hasStale = p.__staleWhileFetching !== undefined;
|
|
18754
|
+
const staleVal = hasStale && allowStale;
|
|
18755
|
+
if (status) {
|
|
18756
|
+
status.fetch = isStale ? 'stale' : 'refresh';
|
|
18757
|
+
if (staleVal && isStale)
|
|
18758
|
+
status.returnedStale = true;
|
|
18759
|
+
}
|
|
18760
|
+
return staleVal ? p.__staleWhileFetching : (p.__returned = p);
|
|
18761
|
+
}
|
|
18762
|
+
}
|
|
18763
|
+
async forceFetch(k, fetchOptions = {}) {
|
|
18764
|
+
const v = await this.fetch(k, fetchOptions);
|
|
18765
|
+
if (v === undefined)
|
|
18766
|
+
throw new Error('fetch() returned undefined');
|
|
18767
|
+
return v;
|
|
18768
|
+
}
|
|
18769
|
+
memo(k, memoOptions = {}) {
|
|
18770
|
+
const memoMethod = this.#memoMethod;
|
|
18771
|
+
if (!memoMethod) {
|
|
18772
|
+
throw new Error('no memoMethod provided to constructor');
|
|
18773
|
+
}
|
|
18774
|
+
const { context, forceRefresh, ...options } = memoOptions;
|
|
18775
|
+
const v = this.get(k, options);
|
|
18776
|
+
if (!forceRefresh && v !== undefined)
|
|
18777
|
+
return v;
|
|
18778
|
+
const vv = memoMethod(k, v, {
|
|
18779
|
+
options,
|
|
18780
|
+
context,
|
|
18781
|
+
});
|
|
18782
|
+
this.set(k, vv, options);
|
|
18783
|
+
return vv;
|
|
18784
|
+
}
|
|
18785
|
+
/**
|
|
18786
|
+
* Return a value from the cache. Will update the recency of the cache
|
|
18787
|
+
* entry found.
|
|
18788
|
+
*
|
|
18789
|
+
* If the key is not found, get() will return `undefined`.
|
|
18790
|
+
*/
|
|
18791
|
+
get(k, getOptions = {}) {
|
|
18792
|
+
const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status, } = getOptions;
|
|
18793
|
+
const index = this.#keyMap.get(k);
|
|
18794
|
+
if (index !== undefined) {
|
|
18795
|
+
const value = this.#valList[index];
|
|
18796
|
+
const fetching = this.#isBackgroundFetch(value);
|
|
18797
|
+
if (status)
|
|
18798
|
+
this.#statusTTL(status, index);
|
|
18799
|
+
if (this.#isStale(index)) {
|
|
18800
|
+
if (status)
|
|
18801
|
+
status.get = 'stale';
|
|
18802
|
+
// delete only if not an in-flight background fetch
|
|
18803
|
+
if (!fetching) {
|
|
18804
|
+
if (!noDeleteOnStaleGet) {
|
|
18805
|
+
this.#delete(k, 'expire');
|
|
18806
|
+
}
|
|
18807
|
+
if (status && allowStale)
|
|
18808
|
+
status.returnedStale = true;
|
|
18809
|
+
return allowStale ? value : undefined;
|
|
18810
|
+
}
|
|
18811
|
+
else {
|
|
18812
|
+
if (status &&
|
|
18813
|
+
allowStale &&
|
|
18814
|
+
value.__staleWhileFetching !== undefined) {
|
|
18815
|
+
status.returnedStale = true;
|
|
18816
|
+
}
|
|
18817
|
+
return allowStale ? value.__staleWhileFetching : undefined;
|
|
18818
|
+
}
|
|
18819
|
+
}
|
|
18820
|
+
else {
|
|
18821
|
+
if (status)
|
|
18822
|
+
status.get = 'hit';
|
|
18823
|
+
// if we're currently fetching it, we don't actually have it yet
|
|
18824
|
+
// it's not stale, which means this isn't a staleWhileRefetching.
|
|
18825
|
+
// If it's not stale, and fetching, AND has a __staleWhileFetching
|
|
18826
|
+
// value, then that means the user fetched with {forceRefresh:true},
|
|
18827
|
+
// so it's safe to return that value.
|
|
18828
|
+
if (fetching) {
|
|
18829
|
+
return value.__staleWhileFetching;
|
|
18830
|
+
}
|
|
18831
|
+
this.#moveToTail(index);
|
|
18832
|
+
if (updateAgeOnGet) {
|
|
18833
|
+
this.#updateItemAge(index);
|
|
18834
|
+
}
|
|
18835
|
+
return value;
|
|
18836
|
+
}
|
|
18837
|
+
}
|
|
18838
|
+
else if (status) {
|
|
18839
|
+
status.get = 'miss';
|
|
18840
|
+
}
|
|
18841
|
+
}
|
|
18842
|
+
#connect(p, n) {
|
|
18843
|
+
this.#prev[n] = p;
|
|
18844
|
+
this.#next[p] = n;
|
|
18845
|
+
}
|
|
18846
|
+
#moveToTail(index) {
|
|
18847
|
+
// if tail already, nothing to do
|
|
18848
|
+
// if head, move head to next[index]
|
|
18849
|
+
// else
|
|
18850
|
+
// move next[prev[index]] to next[index] (head has no prev)
|
|
18851
|
+
// move prev[next[index]] to prev[index]
|
|
18852
|
+
// prev[index] = tail
|
|
18853
|
+
// next[tail] = index
|
|
18854
|
+
// tail = index
|
|
18855
|
+
if (index !== this.#tail) {
|
|
18856
|
+
if (index === this.#head) {
|
|
18857
|
+
this.#head = this.#next[index];
|
|
18858
|
+
}
|
|
18859
|
+
else {
|
|
18860
|
+
this.#connect(this.#prev[index], this.#next[index]);
|
|
18861
|
+
}
|
|
18862
|
+
this.#connect(this.#tail, index);
|
|
18863
|
+
this.#tail = index;
|
|
18864
|
+
}
|
|
18865
|
+
}
|
|
18866
|
+
/**
|
|
18867
|
+
* Deletes a key out of the cache.
|
|
18868
|
+
*
|
|
18869
|
+
* Returns true if the key was deleted, false otherwise.
|
|
18870
|
+
*/
|
|
18871
|
+
delete(k) {
|
|
18872
|
+
return this.#delete(k, 'delete');
|
|
18873
|
+
}
|
|
18874
|
+
#delete(k, reason) {
|
|
18875
|
+
let deleted = false;
|
|
18876
|
+
if (this.#size !== 0) {
|
|
18877
|
+
const index = this.#keyMap.get(k);
|
|
18878
|
+
if (index !== undefined) {
|
|
18879
|
+
deleted = true;
|
|
18880
|
+
if (this.#size === 1) {
|
|
18881
|
+
this.#clear(reason);
|
|
18882
|
+
}
|
|
18883
|
+
else {
|
|
18884
|
+
this.#removeItemSize(index);
|
|
18885
|
+
const v = this.#valList[index];
|
|
18886
|
+
if (this.#isBackgroundFetch(v)) {
|
|
18887
|
+
v.__abortController.abort(new Error('deleted'));
|
|
18888
|
+
}
|
|
18889
|
+
else if (this.#hasDispose || this.#hasDisposeAfter) {
|
|
18890
|
+
if (this.#hasDispose) {
|
|
18891
|
+
this.#dispose?.(v, k, reason);
|
|
18892
|
+
}
|
|
18893
|
+
if (this.#hasDisposeAfter) {
|
|
18894
|
+
this.#disposed?.push([v, k, reason]);
|
|
18895
|
+
}
|
|
18896
|
+
}
|
|
18897
|
+
this.#keyMap.delete(k);
|
|
18898
|
+
this.#keyList[index] = undefined;
|
|
18899
|
+
this.#valList[index] = undefined;
|
|
18900
|
+
if (index === this.#tail) {
|
|
18901
|
+
this.#tail = this.#prev[index];
|
|
18902
|
+
}
|
|
18903
|
+
else if (index === this.#head) {
|
|
18904
|
+
this.#head = this.#next[index];
|
|
18905
|
+
}
|
|
18906
|
+
else {
|
|
18907
|
+
const pi = this.#prev[index];
|
|
18908
|
+
this.#next[pi] = this.#next[index];
|
|
18909
|
+
const ni = this.#next[index];
|
|
18910
|
+
this.#prev[ni] = this.#prev[index];
|
|
18911
|
+
}
|
|
18912
|
+
this.#size--;
|
|
18913
|
+
this.#free.push(index);
|
|
18914
|
+
}
|
|
18915
|
+
}
|
|
18916
|
+
}
|
|
18917
|
+
if (this.#hasDisposeAfter && this.#disposed?.length) {
|
|
18918
|
+
const dt = this.#disposed;
|
|
18919
|
+
let task;
|
|
18920
|
+
while ((task = dt?.shift())) {
|
|
18921
|
+
this.#disposeAfter?.(...task);
|
|
18922
|
+
}
|
|
18923
|
+
}
|
|
18924
|
+
return deleted;
|
|
18925
|
+
}
|
|
18926
|
+
/**
|
|
18927
|
+
* Clear the cache entirely, throwing away all values.
|
|
18928
|
+
*/
|
|
18929
|
+
clear() {
|
|
18930
|
+
return this.#clear('delete');
|
|
18931
|
+
}
|
|
18932
|
+
#clear(reason) {
|
|
18933
|
+
for (const index of this.#rindexes({ allowStale: true })) {
|
|
18934
|
+
const v = this.#valList[index];
|
|
18935
|
+
if (this.#isBackgroundFetch(v)) {
|
|
18936
|
+
v.__abortController.abort(new Error('deleted'));
|
|
18937
|
+
}
|
|
18938
|
+
else {
|
|
18939
|
+
const k = this.#keyList[index];
|
|
18940
|
+
if (this.#hasDispose) {
|
|
18941
|
+
this.#dispose?.(v, k, reason);
|
|
18942
|
+
}
|
|
18943
|
+
if (this.#hasDisposeAfter) {
|
|
18944
|
+
this.#disposed?.push([v, k, reason]);
|
|
18945
|
+
}
|
|
18946
|
+
}
|
|
18947
|
+
}
|
|
18948
|
+
this.#keyMap.clear();
|
|
18949
|
+
this.#valList.fill(undefined);
|
|
18950
|
+
this.#keyList.fill(undefined);
|
|
18951
|
+
if (this.#ttls && this.#starts) {
|
|
18952
|
+
this.#ttls.fill(0);
|
|
18953
|
+
this.#starts.fill(0);
|
|
18954
|
+
}
|
|
18955
|
+
if (this.#sizes) {
|
|
18956
|
+
this.#sizes.fill(0);
|
|
18957
|
+
}
|
|
18958
|
+
this.#head = 0;
|
|
18959
|
+
this.#tail = 0;
|
|
18960
|
+
this.#free.length = 0;
|
|
18961
|
+
this.#calculatedSize = 0;
|
|
18962
|
+
this.#size = 0;
|
|
18963
|
+
if (this.#hasDisposeAfter && this.#disposed) {
|
|
18964
|
+
const dt = this.#disposed;
|
|
18965
|
+
let task;
|
|
18966
|
+
while ((task = dt?.shift())) {
|
|
18967
|
+
this.#disposeAfter?.(...task);
|
|
18968
|
+
}
|
|
18969
|
+
}
|
|
18970
|
+
}
|
|
18971
|
+
}
|
|
18972
|
+
|
|
18973
|
+
// ../../node_modules/.pnpm/fast-deep-equal@3.1.3/node_modules/fast-deep-equal/index.js
|
|
18974
|
+
var require_fast_deep_equal = __commonJS$3({
|
|
18975
|
+
"../../node_modules/.pnpm/fast-deep-equal@3.1.3/node_modules/fast-deep-equal/index.js"(exports, module) {
|
|
18976
|
+
module.exports = function equal(a, b) {
|
|
18977
|
+
if (a === b) return true;
|
|
18978
|
+
if (a && b && typeof a == "object" && typeof b == "object") {
|
|
18979
|
+
if (a.constructor !== b.constructor) return false;
|
|
18980
|
+
var length, i, keys;
|
|
18981
|
+
if (Array.isArray(a)) {
|
|
18982
|
+
length = a.length;
|
|
18983
|
+
if (length != b.length) return false;
|
|
18984
|
+
for (i = length; i-- !== 0; )
|
|
18985
|
+
if (!equal(a[i], b[i])) return false;
|
|
18986
|
+
return true;
|
|
18987
|
+
}
|
|
18988
|
+
if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
|
|
18989
|
+
if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
|
|
18990
|
+
if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
|
|
18991
|
+
keys = Object.keys(a);
|
|
18992
|
+
length = keys.length;
|
|
18993
|
+
if (length !== Object.keys(b).length) return false;
|
|
18994
|
+
for (i = length; i-- !== 0; )
|
|
18995
|
+
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
|
|
18996
|
+
for (i = length; i-- !== 0; ) {
|
|
18997
|
+
var key = keys[i];
|
|
18998
|
+
if (!equal(a[key], b[key])) return false;
|
|
18999
|
+
}
|
|
19000
|
+
return true;
|
|
19001
|
+
}
|
|
19002
|
+
return a !== a && b !== b;
|
|
19003
|
+
};
|
|
19004
|
+
}
|
|
19005
|
+
});
|
|
19006
|
+
var TextPartSchema = z$1.object({
|
|
19007
|
+
type: z$1.literal("text"),
|
|
19008
|
+
text: z$1.string()
|
|
19009
|
+
}).passthrough();
|
|
19010
|
+
var ImagePartSchema = z$1.object({
|
|
19011
|
+
type: z$1.literal("image"),
|
|
19012
|
+
image: z$1.union([z$1.string(), z$1.instanceof(URL), z$1.instanceof(Uint8Array)]),
|
|
19013
|
+
mimeType: z$1.string().optional()
|
|
19014
|
+
}).passthrough();
|
|
19015
|
+
var FilePartSchema = z$1.object({
|
|
19016
|
+
type: z$1.literal("file"),
|
|
19017
|
+
data: z$1.union([z$1.string(), z$1.instanceof(URL), z$1.instanceof(Uint8Array)]),
|
|
19018
|
+
mimeType: z$1.string()
|
|
19019
|
+
}).passthrough();
|
|
19020
|
+
var ToolInvocationPartSchema = z$1.object({
|
|
19021
|
+
type: z$1.literal("tool-invocation"),
|
|
19022
|
+
toolInvocation: z$1.object({
|
|
19023
|
+
toolCallId: z$1.string(),
|
|
19024
|
+
toolName: z$1.string(),
|
|
19025
|
+
args: z$1.unknown(),
|
|
19026
|
+
state: z$1.enum(["partial-call", "call", "result"]),
|
|
19027
|
+
result: z$1.unknown().optional()
|
|
19028
|
+
})
|
|
19029
|
+
}).passthrough();
|
|
19030
|
+
var ReasoningPartSchema = z$1.object({
|
|
19031
|
+
type: z$1.literal("reasoning"),
|
|
19032
|
+
reasoning: z$1.string(),
|
|
19033
|
+
details: z$1.array(
|
|
19034
|
+
z$1.object({
|
|
19035
|
+
type: z$1.enum(["text", "redacted"]),
|
|
19036
|
+
text: z$1.string().optional(),
|
|
19037
|
+
data: z$1.string().optional()
|
|
19038
|
+
})
|
|
19039
|
+
)
|
|
19040
|
+
}).passthrough();
|
|
19041
|
+
var SourcePartSchema = z$1.object({
|
|
19042
|
+
type: z$1.literal("source"),
|
|
19043
|
+
source: z$1.object({
|
|
19044
|
+
sourceType: z$1.string(),
|
|
19045
|
+
id: z$1.string(),
|
|
19046
|
+
url: z$1.string().optional(),
|
|
19047
|
+
title: z$1.string().optional()
|
|
19048
|
+
})
|
|
19049
|
+
}).passthrough();
|
|
19050
|
+
var StepStartPartSchema = z$1.object({
|
|
19051
|
+
type: z$1.literal("step-start")
|
|
19052
|
+
}).passthrough();
|
|
19053
|
+
var DataPartSchema = z$1.object({
|
|
19054
|
+
type: z$1.string().refine((t) => t.startsWith("data-"), { message: 'Type must start with "data-"' }),
|
|
19055
|
+
id: z$1.string().optional(),
|
|
19056
|
+
data: z$1.unknown()
|
|
19057
|
+
}).passthrough();
|
|
19058
|
+
var MessagePartSchema = z$1.union([
|
|
19059
|
+
TextPartSchema,
|
|
19060
|
+
ImagePartSchema,
|
|
19061
|
+
FilePartSchema,
|
|
19062
|
+
ToolInvocationPartSchema,
|
|
19063
|
+
ReasoningPartSchema,
|
|
19064
|
+
SourcePartSchema,
|
|
19065
|
+
StepStartPartSchema,
|
|
19066
|
+
DataPartSchema
|
|
19067
|
+
]);
|
|
19068
|
+
z$1.object({
|
|
19069
|
+
/** Format version - 2 corresponds to AI SDK v4 UIMessage format */
|
|
19070
|
+
format: z$1.literal(2),
|
|
19071
|
+
/** Array of message parts (text, images, tool calls, etc.) */
|
|
19072
|
+
parts: z$1.array(MessagePartSchema),
|
|
19073
|
+
/** Legacy content field for backwards compatibility */
|
|
19074
|
+
content: z$1.string().optional(),
|
|
19075
|
+
/** Additional metadata */
|
|
19076
|
+
metadata: z$1.record(z$1.unknown()).optional(),
|
|
17500
19077
|
/** Provider-specific metadata */
|
|
17501
19078
|
providerMetadata: z$1.record(z$1.unknown()).optional()
|
|
17502
19079
|
});
|
|
@@ -17637,9 +19214,9 @@ z$1.object({
|
|
|
17637
19214
|
steps: z$1.custom().optional()
|
|
17638
19215
|
});
|
|
17639
19216
|
var languageModelUsageSchema = z10.object({
|
|
17640
|
-
inputTokens: z10.number(),
|
|
17641
|
-
outputTokens: z10.number(),
|
|
17642
|
-
totalTokens: z10.number(),
|
|
19217
|
+
inputTokens: z10.number().optional(),
|
|
19218
|
+
outputTokens: z10.number().optional(),
|
|
19219
|
+
totalTokens: z10.number().optional(),
|
|
17643
19220
|
reasoningTokens: z10.number().optional(),
|
|
17644
19221
|
cachedInputTokens: z10.number().optional()
|
|
17645
19222
|
});
|
|
@@ -17763,7 +19340,7 @@ var coreToolSchema = z$1.object({
|
|
|
17763
19340
|
// Zod schema or other schema types - validated at tool execution
|
|
17764
19341
|
]),
|
|
17765
19342
|
outputSchema: z$1.union([z$1.record(z$1.string(), z$1.any()), z$1.any()]).optional(),
|
|
17766
|
-
execute: z$1.function(z$1.tuple([z$1.any(), z$1.any()]), z$1.promise(z$1.any()))
|
|
19343
|
+
execute: z$1.optional(z$1.function(z$1.tuple([z$1.any(), z$1.any()]), z$1.promise(z$1.any()))),
|
|
17767
19344
|
type: z$1.union([z$1.literal("function"), z$1.literal("provider-defined"), z$1.undefined()]).optional(),
|
|
17768
19345
|
args: z$1.record(z$1.string(), z$1.any()).optional()
|
|
17769
19346
|
});
|
|
@@ -17790,6 +19367,10 @@ z$1.object({
|
|
|
17790
19367
|
processorId: z$1.string().optional()
|
|
17791
19368
|
}).optional()
|
|
17792
19369
|
});
|
|
19370
|
+
var DEFAULT_CACHE_MAX_SIZE = 1e3;
|
|
19371
|
+
new LRUCache({
|
|
19372
|
+
max: DEFAULT_CACHE_MAX_SIZE
|
|
19373
|
+
});
|
|
17793
19374
|
|
|
17794
19375
|
class AISdkNetworkTransformer {
|
|
17795
19376
|
transform({ chunk, conversation, metadata }) {
|
|
@@ -17938,6 +19519,54 @@ class AISdkNetworkTransformer {
|
|
|
17938
19519
|
};
|
|
17939
19520
|
return [...newConversation, newMessage];
|
|
17940
19521
|
}
|
|
19522
|
+
if (chunk.type === "agent-execution-approval") {
|
|
19523
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
19524
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
19525
|
+
const lastRequireApprovalMetadata = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.requireApprovalMetadata : {};
|
|
19526
|
+
return [
|
|
19527
|
+
...newConversation.slice(0, -1),
|
|
19528
|
+
{
|
|
19529
|
+
...lastMessage,
|
|
19530
|
+
metadata: {
|
|
19531
|
+
...lastMessage.metadata,
|
|
19532
|
+
mode: "network",
|
|
19533
|
+
requireApprovalMetadata: {
|
|
19534
|
+
...lastRequireApprovalMetadata,
|
|
19535
|
+
[chunk.payload.toolName]: {
|
|
19536
|
+
toolCallId: chunk.payload.toolCallId,
|
|
19537
|
+
toolName: chunk.payload.toolName,
|
|
19538
|
+
args: chunk.payload.args,
|
|
19539
|
+
runId: chunk.payload.runId
|
|
19540
|
+
}
|
|
19541
|
+
}
|
|
19542
|
+
}
|
|
19543
|
+
}
|
|
19544
|
+
];
|
|
19545
|
+
}
|
|
19546
|
+
if (chunk.type === "agent-execution-suspended") {
|
|
19547
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
19548
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
19549
|
+
const lastSuspendedTools = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.suspendedTools : {};
|
|
19550
|
+
return [
|
|
19551
|
+
...newConversation.slice(0, -1),
|
|
19552
|
+
{
|
|
19553
|
+
...lastMessage,
|
|
19554
|
+
metadata: {
|
|
19555
|
+
...lastMessage.metadata,
|
|
19556
|
+
mode: "network",
|
|
19557
|
+
suspendedTools: {
|
|
19558
|
+
...lastSuspendedTools,
|
|
19559
|
+
[chunk.payload.toolName]: {
|
|
19560
|
+
toolCallId: chunk.payload.toolCallId,
|
|
19561
|
+
toolName: chunk.payload.toolName,
|
|
19562
|
+
args: chunk.payload.args,
|
|
19563
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
19564
|
+
}
|
|
19565
|
+
}
|
|
19566
|
+
}
|
|
19567
|
+
}
|
|
19568
|
+
];
|
|
19569
|
+
}
|
|
17941
19570
|
if (chunk.type === "agent-execution-end") {
|
|
17942
19571
|
const lastMessage = newConversation[newConversation.length - 1];
|
|
17943
19572
|
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
@@ -18093,6 +19722,30 @@ class AISdkNetworkTransformer {
|
|
|
18093
19722
|
};
|
|
18094
19723
|
return [...newConversation, newMessage];
|
|
18095
19724
|
}
|
|
19725
|
+
if (chunk.type === "workflow-execution-suspended") {
|
|
19726
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
19727
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
19728
|
+
const lastSuspendedTools = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.suspendedTools : {};
|
|
19729
|
+
return [
|
|
19730
|
+
...newConversation.slice(0, -1),
|
|
19731
|
+
{
|
|
19732
|
+
...lastMessage,
|
|
19733
|
+
metadata: {
|
|
19734
|
+
...lastMessage.metadata,
|
|
19735
|
+
mode: "network",
|
|
19736
|
+
suspendedTools: {
|
|
19737
|
+
...lastSuspendedTools,
|
|
19738
|
+
[chunk.payload.toolName]: {
|
|
19739
|
+
toolCallId: chunk.payload.toolCallId,
|
|
19740
|
+
toolName: chunk.payload.toolName,
|
|
19741
|
+
args: chunk.payload.args,
|
|
19742
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
19743
|
+
}
|
|
19744
|
+
}
|
|
19745
|
+
}
|
|
19746
|
+
}
|
|
19747
|
+
];
|
|
19748
|
+
}
|
|
18096
19749
|
if (chunk.type.startsWith("workflow-execution-event-")) {
|
|
18097
19750
|
const lastMessage = newConversation[newConversation.length - 1];
|
|
18098
19751
|
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
@@ -18160,6 +19813,54 @@ class AISdkNetworkTransformer {
|
|
|
18160
19813
|
}
|
|
18161
19814
|
];
|
|
18162
19815
|
}
|
|
19816
|
+
if (chunk.type === "tool-execution-approval") {
|
|
19817
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
19818
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
19819
|
+
const lastRequireApprovalMetadata = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.requireApprovalMetadata : {};
|
|
19820
|
+
return [
|
|
19821
|
+
...newConversation.slice(0, -1),
|
|
19822
|
+
{
|
|
19823
|
+
...lastMessage,
|
|
19824
|
+
metadata: {
|
|
19825
|
+
...lastMessage.metadata,
|
|
19826
|
+
mode: "network",
|
|
19827
|
+
requireApprovalMetadata: {
|
|
19828
|
+
...lastRequireApprovalMetadata,
|
|
19829
|
+
[chunk.payload.toolName]: {
|
|
19830
|
+
toolCallId: chunk.payload.toolCallId,
|
|
19831
|
+
toolName: chunk.payload.toolName,
|
|
19832
|
+
args: chunk.payload.args,
|
|
19833
|
+
runId: chunk.payload.runId
|
|
19834
|
+
}
|
|
19835
|
+
}
|
|
19836
|
+
}
|
|
19837
|
+
}
|
|
19838
|
+
];
|
|
19839
|
+
}
|
|
19840
|
+
if (chunk.type === "tool-execution-suspended") {
|
|
19841
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
19842
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
19843
|
+
const lastSuspendedTools = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.suspendedTools : {};
|
|
19844
|
+
return [
|
|
19845
|
+
...newConversation.slice(0, -1),
|
|
19846
|
+
{
|
|
19847
|
+
...lastMessage,
|
|
19848
|
+
metadata: {
|
|
19849
|
+
...lastMessage.metadata,
|
|
19850
|
+
mode: "network",
|
|
19851
|
+
suspendedTools: {
|
|
19852
|
+
...lastSuspendedTools,
|
|
19853
|
+
[chunk.payload.toolName]: {
|
|
19854
|
+
toolCallId: chunk.payload.toolCallId,
|
|
19855
|
+
toolName: chunk.payload.toolName,
|
|
19856
|
+
args: chunk.payload.args,
|
|
19857
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
19858
|
+
}
|
|
19859
|
+
}
|
|
19860
|
+
}
|
|
19861
|
+
}
|
|
19862
|
+
];
|
|
19863
|
+
}
|
|
18163
19864
|
if (chunk.type === "tool-execution-end") {
|
|
18164
19865
|
const lastMessage = newConversation[newConversation.length - 1];
|
|
18165
19866
|
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
@@ -18255,8 +19956,11 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
18255
19956
|
const initialRunId = extractRunIdFromMessages(initialMessages);
|
|
18256
19957
|
const _currentRunId = useRef(initialRunId);
|
|
18257
19958
|
const _onChunk = useRef(void 0);
|
|
19959
|
+
const _networkRunId = useRef(void 0);
|
|
19960
|
+
const _onNetworkChunk = useRef(void 0);
|
|
18258
19961
|
const [messages, setMessages] = useState(() => resolveInitialMessages(initialMessages));
|
|
18259
19962
|
const [toolCallApprovals, setToolCallApprovals] = useState({});
|
|
19963
|
+
const [networkToolCallApprovals, setNetworkToolCallApprovals] = useState({});
|
|
18260
19964
|
const baseClient = useMastraClient();
|
|
18261
19965
|
const [isRunning, setIsRunning] = useState(false);
|
|
18262
19966
|
const generate = async ({
|
|
@@ -18301,7 +20005,7 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
18301
20005
|
},
|
|
18302
20006
|
instructions,
|
|
18303
20007
|
requestContext,
|
|
18304
|
-
...threadId ? { threadId,
|
|
20008
|
+
...threadId ? { memory: { thread: threadId, resource: resourceId || agentId } } : {},
|
|
18305
20009
|
providerOptions,
|
|
18306
20010
|
tracingOptions
|
|
18307
20011
|
});
|
|
@@ -18361,7 +20065,7 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
18361
20065
|
},
|
|
18362
20066
|
instructions,
|
|
18363
20067
|
requestContext,
|
|
18364
|
-
...threadId ? { threadId,
|
|
20068
|
+
...threadId ? { memory: { thread: threadId, resource: resourceId || agentId } } : {},
|
|
18365
20069
|
providerOptions,
|
|
18366
20070
|
requireToolApproval,
|
|
18367
20071
|
tracingOptions
|
|
@@ -18407,9 +20111,11 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
18407
20111
|
},
|
|
18408
20112
|
runId,
|
|
18409
20113
|
requestContext,
|
|
18410
|
-
...threadId ? { thread: threadId,
|
|
20114
|
+
...threadId ? { memory: { thread: threadId, resource: resourceId || agentId } } : {},
|
|
18411
20115
|
tracingOptions
|
|
18412
20116
|
});
|
|
20117
|
+
_onNetworkChunk.current = onNetworkChunk;
|
|
20118
|
+
_networkRunId.current = runId;
|
|
18413
20119
|
const transformer = new AISdkNetworkTransformer();
|
|
18414
20120
|
await response.processDataStream({
|
|
18415
20121
|
onChunk: async (chunk) => {
|
|
@@ -18423,6 +20129,8 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
18423
20129
|
setIsRunning(false);
|
|
18424
20130
|
_currentRunId.current = void 0;
|
|
18425
20131
|
_onChunk.current = void 0;
|
|
20132
|
+
_networkRunId.current = void 0;
|
|
20133
|
+
_onNetworkChunk.current = void 0;
|
|
18426
20134
|
};
|
|
18427
20135
|
const approveToolCall = async (toolCallId) => {
|
|
18428
20136
|
const onChunk = _onChunk.current;
|
|
@@ -18458,6 +20166,52 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
18458
20166
|
});
|
|
18459
20167
|
setIsRunning(false);
|
|
18460
20168
|
};
|
|
20169
|
+
const approveNetworkToolCall = async (toolName, runId) => {
|
|
20170
|
+
const onNetworkChunk = _onNetworkChunk.current;
|
|
20171
|
+
const networkRunId = runId || _networkRunId.current;
|
|
20172
|
+
if (!networkRunId)
|
|
20173
|
+
return console.info(
|
|
20174
|
+
"[approveNetworkToolCall] approveNetworkToolCall can only be called after a network stream has started"
|
|
20175
|
+
);
|
|
20176
|
+
setIsRunning(true);
|
|
20177
|
+
setNetworkToolCallApprovals((prev) => ({
|
|
20178
|
+
...prev,
|
|
20179
|
+
[runId ? `${runId}-${toolName}` : toolName]: { status: "approved" }
|
|
20180
|
+
}));
|
|
20181
|
+
const agent = baseClient.getAgent(agentId);
|
|
20182
|
+
const response = await agent.approveNetworkToolCall({ runId: networkRunId });
|
|
20183
|
+
const transformer = new AISdkNetworkTransformer();
|
|
20184
|
+
await response.processDataStream({
|
|
20185
|
+
onChunk: async (chunk) => {
|
|
20186
|
+
setMessages((prev) => transformer.transform({ chunk, conversation: prev, metadata: { mode: "network" } }));
|
|
20187
|
+
onNetworkChunk?.(chunk);
|
|
20188
|
+
}
|
|
20189
|
+
});
|
|
20190
|
+
setIsRunning(false);
|
|
20191
|
+
};
|
|
20192
|
+
const declineNetworkToolCall = async (toolName, runId) => {
|
|
20193
|
+
const onNetworkChunk = _onNetworkChunk.current;
|
|
20194
|
+
const networkRunId = runId || _networkRunId.current;
|
|
20195
|
+
if (!networkRunId)
|
|
20196
|
+
return console.info(
|
|
20197
|
+
"[declineNetworkToolCall] declineNetworkToolCall can only be called after a network stream has started"
|
|
20198
|
+
);
|
|
20199
|
+
setIsRunning(true);
|
|
20200
|
+
setNetworkToolCallApprovals((prev) => ({
|
|
20201
|
+
...prev,
|
|
20202
|
+
[runId ? `${runId}-${toolName}` : toolName]: { status: "declined" }
|
|
20203
|
+
}));
|
|
20204
|
+
const agent = baseClient.getAgent(agentId);
|
|
20205
|
+
const response = await agent.declineNetworkToolCall({ runId: networkRunId });
|
|
20206
|
+
const transformer = new AISdkNetworkTransformer();
|
|
20207
|
+
await response.processDataStream({
|
|
20208
|
+
onChunk: async (chunk) => {
|
|
20209
|
+
setMessages((prev) => transformer.transform({ chunk, conversation: prev, metadata: { mode: "network" } }));
|
|
20210
|
+
onNetworkChunk?.(chunk);
|
|
20211
|
+
}
|
|
20212
|
+
});
|
|
20213
|
+
setIsRunning(false);
|
|
20214
|
+
};
|
|
18461
20215
|
const sendMessage = async ({ mode = "stream", ...args }) => {
|
|
18462
20216
|
const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
|
|
18463
20217
|
const coreUserMessages = [nextMessage];
|
|
@@ -18482,7 +20236,10 @@ const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
|
18482
20236
|
approveToolCall,
|
|
18483
20237
|
declineToolCall,
|
|
18484
20238
|
cancelRun: handleCancelRun,
|
|
18485
|
-
toolCallApprovals
|
|
20239
|
+
toolCallApprovals,
|
|
20240
|
+
approveNetworkToolCall,
|
|
20241
|
+
declineNetworkToolCall,
|
|
20242
|
+
networkToolCallApprovals
|
|
18486
20243
|
};
|
|
18487
20244
|
};
|
|
18488
20245
|
|
|
@@ -18802,4 +20559,4 @@ const MessageStreaming = ({ className, ...props }) => {
|
|
|
18802
20559
|
};
|
|
18803
20560
|
|
|
18804
20561
|
export { MessageUsages as $, EntryTitleClass as A, EntryTitle as B, CodeBlockClass as C, CodeBlock as D, Entity as E, CodeCopyButton as F, Icon as G, IconButtonClass as H, IconSizes as I, IconButton as J, AgentIcon as K, ToolsIcon as L, MastraReactProvider as M, Tooltip as N, TooltipContentClass as O, TooltipContent as P, TooltipTrigger as Q, MessageClass as R, Message as S, ToolApprovalClass as T, MessageContentClass as U, MessageContent as V, WorkflowIcon as W, MessageActionsClass as X, MessageActions as Y, MessageUsagesClass as Z, __commonJS$2 as _, __require2 as a, MessageUsageClass as a0, MessageUsage as a1, MessageUsageEntryClass as a2, MessageUsageEntry as a3, MessageUsageValueClass as a4, MessageUsageValue as a5, MessageListClass as a6, MessageList as a7, MessageStreamingClass as a8, MessageStreaming as a9, useChat as b, resolveToChildMessages as c, toAssistantUIMessage as d, useEntity as e, EntityTriggerClass as f, EntityTriggerVariantClasses as g, EntityTrigger as h, EntityContentClass as i, EntityContent as j, EntityCaret as k, ToolApproval as l, mapWorkflowStreamChunkToWatchResult as m, ToolApprovalTitleClass as n, ToolApprovalTitle as o, ToolApprovalHeaderClass as p, ToolApprovalHeader as q, require_token_error as r, ToolApprovalContentClass as s, toUIMessage as t, useMastraClient as u, ToolApprovalContent as v, ToolApprovalActionsClass as w, ToolApprovalActions as x, EntryClass as y, Entry as z };
|
|
18805
|
-
//# sourceMappingURL=index-
|
|
20562
|
+
//# sourceMappingURL=index-DFYpOuhE.js.map
|