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