diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 795946d..44fb88a 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -21,6 +21,7 @@ module.exports = { SillyTavern: 'readonly', ePub: 'readonly', pdfjsLib: 'readonly', + echarts: 'readonly', }, parserOptions: { ecmaVersion: 'latest', diff --git a/core/server-storage.js b/core/server-storage.js index 2459d41..4797524 100644 --- a/core/server-storage.js +++ b/core/server-storage.js @@ -183,3 +183,4 @@ export const StoryOutlineStorage = new StorageFile('LittleWhiteBox_StoryOutline. export const NovelDrawStorage = new StorageFile('LittleWhiteBox_NovelDraw.json', { debounceMs: 800 }); export const TtsStorage = new StorageFile('LittleWhiteBox_TTS.json', { debounceMs: 800 }); export const CommonSettingStorage = new StorageFile('LittleWhiteBox_CommonSettings.json', { debounceMs: 1000 }); +export const VectorStorage = new StorageFile('LittleWhiteBox_Vectors.json', { debounceMs: 3000 }); diff --git a/libs/dexie.mjs b/libs/dexie.mjs new file mode 100644 index 0000000..fe55a2a --- /dev/null +++ b/libs/dexie.mjs @@ -0,0 +1,5912 @@ +/* + * Dexie.js - a minimalistic wrapper for IndexedDB + * =============================================== + * + * By David Fahlander, david.fahlander@gmail.com + * + * Version 4.0.10, Fri Nov 15 2024 + * + * https://dexie.org + * + * Apache License Version 2.0, January 2004, http://www.apache.org/licenses/ + */ + +/*! ***************************************************************************** +Copyright (c) Microsoft Corporation. +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +***************************************************************************** */ +var extendStatics = function(d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); +}; +function __extends(d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +} +var __assign = function() { + __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; +function __spreadArray(to, from, pack) { + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { + if (ar || !(i in from)) { + if (!ar) ar = Array.prototype.slice.call(from, 0, i); + ar[i] = from[i]; + } + } + return to.concat(ar || Array.prototype.slice.call(from)); +} + +var _global = typeof globalThis !== 'undefined' ? globalThis : + typeof self !== 'undefined' ? self : + typeof window !== 'undefined' ? window : + global; + +var keys = Object.keys; +var isArray = Array.isArray; +if (typeof Promise !== 'undefined' && !_global.Promise) { + _global.Promise = Promise; +} +function extend(obj, extension) { + if (typeof extension !== 'object') + return obj; + keys(extension).forEach(function (key) { + obj[key] = extension[key]; + }); + return obj; +} +var getProto = Object.getPrototypeOf; +var _hasOwn = {}.hasOwnProperty; +function hasOwn(obj, prop) { + return _hasOwn.call(obj, prop); +} +function props(proto, extension) { + if (typeof extension === 'function') + extension = extension(getProto(proto)); + (typeof Reflect === "undefined" ? keys : Reflect.ownKeys)(extension).forEach(function (key) { + setProp(proto, key, extension[key]); + }); +} +var defineProperty = Object.defineProperty; +function setProp(obj, prop, functionOrGetSet, options) { + defineProperty(obj, prop, extend(functionOrGetSet && hasOwn(functionOrGetSet, "get") && typeof functionOrGetSet.get === 'function' ? + { get: functionOrGetSet.get, set: functionOrGetSet.set, configurable: true } : + { value: functionOrGetSet, configurable: true, writable: true }, options)); +} +function derive(Child) { + return { + from: function (Parent) { + Child.prototype = Object.create(Parent.prototype); + setProp(Child.prototype, "constructor", Child); + return { + extend: props.bind(null, Child.prototype) + }; + } + }; +} +var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; +function getPropertyDescriptor(obj, prop) { + var pd = getOwnPropertyDescriptor(obj, prop); + var proto; + return pd || (proto = getProto(obj)) && getPropertyDescriptor(proto, prop); +} +var _slice = [].slice; +function slice(args, start, end) { + return _slice.call(args, start, end); +} +function override(origFunc, overridedFactory) { + return overridedFactory(origFunc); +} +function assert(b) { + if (!b) + throw new Error("Assertion Failed"); +} +function asap$1(fn) { + if (_global.setImmediate) + setImmediate(fn); + else + setTimeout(fn, 0); +} +function arrayToObject(array, extractor) { + return array.reduce(function (result, item, i) { + var nameAndValue = extractor(item, i); + if (nameAndValue) + result[nameAndValue[0]] = nameAndValue[1]; + return result; + }, {}); +} +function getByKeyPath(obj, keyPath) { + if (typeof keyPath === 'string' && hasOwn(obj, keyPath)) + return obj[keyPath]; + if (!keyPath) + return obj; + if (typeof keyPath !== 'string') { + var rv = []; + for (var i = 0, l = keyPath.length; i < l; ++i) { + var val = getByKeyPath(obj, keyPath[i]); + rv.push(val); + } + return rv; + } + var period = keyPath.indexOf('.'); + if (period !== -1) { + var innerObj = obj[keyPath.substr(0, period)]; + return innerObj == null ? undefined : getByKeyPath(innerObj, keyPath.substr(period + 1)); + } + return undefined; +} +function setByKeyPath(obj, keyPath, value) { + if (!obj || keyPath === undefined) + return; + if ('isFrozen' in Object && Object.isFrozen(obj)) + return; + if (typeof keyPath !== 'string' && 'length' in keyPath) { + assert(typeof value !== 'string' && 'length' in value); + for (var i = 0, l = keyPath.length; i < l; ++i) { + setByKeyPath(obj, keyPath[i], value[i]); + } + } + else { + var period = keyPath.indexOf('.'); + if (period !== -1) { + var currentKeyPath = keyPath.substr(0, period); + var remainingKeyPath = keyPath.substr(period + 1); + if (remainingKeyPath === "") + if (value === undefined) { + if (isArray(obj) && !isNaN(parseInt(currentKeyPath))) + obj.splice(currentKeyPath, 1); + else + delete obj[currentKeyPath]; + } + else + obj[currentKeyPath] = value; + else { + var innerObj = obj[currentKeyPath]; + if (!innerObj || !hasOwn(obj, currentKeyPath)) + innerObj = (obj[currentKeyPath] = {}); + setByKeyPath(innerObj, remainingKeyPath, value); + } + } + else { + if (value === undefined) { + if (isArray(obj) && !isNaN(parseInt(keyPath))) + obj.splice(keyPath, 1); + else + delete obj[keyPath]; + } + else + obj[keyPath] = value; + } + } +} +function delByKeyPath(obj, keyPath) { + if (typeof keyPath === 'string') + setByKeyPath(obj, keyPath, undefined); + else if ('length' in keyPath) + [].map.call(keyPath, function (kp) { + setByKeyPath(obj, kp, undefined); + }); +} +function shallowClone(obj) { + var rv = {}; + for (var m in obj) { + if (hasOwn(obj, m)) + rv[m] = obj[m]; + } + return rv; +} +var concat = [].concat; +function flatten(a) { + return concat.apply([], a); +} +var intrinsicTypeNames = "BigUint64Array,BigInt64Array,Array,Boolean,String,Date,RegExp,Blob,File,FileList,FileSystemFileHandle,FileSystemDirectoryHandle,ArrayBuffer,DataView,Uint8ClampedArray,ImageBitmap,ImageData,Map,Set,CryptoKey" + .split(',').concat(flatten([8, 16, 32, 64].map(function (num) { return ["Int", "Uint", "Float"].map(function (t) { return t + num + "Array"; }); }))).filter(function (t) { return _global[t]; }); +var intrinsicTypes = new Set(intrinsicTypeNames.map(function (t) { return _global[t]; })); +function cloneSimpleObjectTree(o) { + var rv = {}; + for (var k in o) + if (hasOwn(o, k)) { + var v = o[k]; + rv[k] = !v || typeof v !== 'object' || intrinsicTypes.has(v.constructor) ? v : cloneSimpleObjectTree(v); + } + return rv; +} +function objectIsEmpty(o) { + for (var k in o) + if (hasOwn(o, k)) + return false; + return true; +} +var circularRefs = null; +function deepClone(any) { + circularRefs = new WeakMap(); + var rv = innerDeepClone(any); + circularRefs = null; + return rv; +} +function innerDeepClone(x) { + if (!x || typeof x !== 'object') + return x; + var rv = circularRefs.get(x); + if (rv) + return rv; + if (isArray(x)) { + rv = []; + circularRefs.set(x, rv); + for (var i = 0, l = x.length; i < l; ++i) { + rv.push(innerDeepClone(x[i])); + } + } + else if (intrinsicTypes.has(x.constructor)) { + rv = x; + } + else { + var proto = getProto(x); + rv = proto === Object.prototype ? {} : Object.create(proto); + circularRefs.set(x, rv); + for (var prop in x) { + if (hasOwn(x, prop)) { + rv[prop] = innerDeepClone(x[prop]); + } + } + } + return rv; +} +var toString = {}.toString; +function toStringTag(o) { + return toString.call(o).slice(8, -1); +} +var iteratorSymbol = typeof Symbol !== 'undefined' ? + Symbol.iterator : + '@@iterator'; +var getIteratorOf = typeof iteratorSymbol === "symbol" ? function (x) { + var i; + return x != null && (i = x[iteratorSymbol]) && i.apply(x); +} : function () { return null; }; +function delArrayItem(a, x) { + var i = a.indexOf(x); + if (i >= 0) + a.splice(i, 1); + return i >= 0; +} +var NO_CHAR_ARRAY = {}; +function getArrayOf(arrayLike) { + var i, a, x, it; + if (arguments.length === 1) { + if (isArray(arrayLike)) + return arrayLike.slice(); + if (this === NO_CHAR_ARRAY && typeof arrayLike === 'string') + return [arrayLike]; + if ((it = getIteratorOf(arrayLike))) { + a = []; + while ((x = it.next()), !x.done) + a.push(x.value); + return a; + } + if (arrayLike == null) + return [arrayLike]; + i = arrayLike.length; + if (typeof i === 'number') { + a = new Array(i); + while (i--) + a[i] = arrayLike[i]; + return a; + } + return [arrayLike]; + } + i = arguments.length; + a = new Array(i); + while (i--) + a[i] = arguments[i]; + return a; +} +var isAsyncFunction = typeof Symbol !== 'undefined' + ? function (fn) { return fn[Symbol.toStringTag] === 'AsyncFunction'; } + : function () { return false; }; + +var dexieErrorNames = [ + 'Modify', + 'Bulk', + 'OpenFailed', + 'VersionChange', + 'Schema', + 'Upgrade', + 'InvalidTable', + 'MissingAPI', + 'NoSuchDatabase', + 'InvalidArgument', + 'SubTransaction', + 'Unsupported', + 'Internal', + 'DatabaseClosed', + 'PrematureCommit', + 'ForeignAwait' +]; +var idbDomErrorNames = [ + 'Unknown', + 'Constraint', + 'Data', + 'TransactionInactive', + 'ReadOnly', + 'Version', + 'NotFound', + 'InvalidState', + 'InvalidAccess', + 'Abort', + 'Timeout', + 'QuotaExceeded', + 'Syntax', + 'DataClone' +]; +var errorList = dexieErrorNames.concat(idbDomErrorNames); +var defaultTexts = { + VersionChanged: "Database version changed by other database connection", + DatabaseClosed: "Database has been closed", + Abort: "Transaction aborted", + TransactionInactive: "Transaction has already completed or failed", + MissingAPI: "IndexedDB API missing. Please visit https://tinyurl.com/y2uuvskb" +}; +function DexieError(name, msg) { + this.name = name; + this.message = msg; +} +derive(DexieError).from(Error).extend({ + toString: function () { return this.name + ": " + this.message; } +}); +function getMultiErrorMessage(msg, failures) { + return msg + ". Errors: " + Object.keys(failures) + .map(function (key) { return failures[key].toString(); }) + .filter(function (v, i, s) { return s.indexOf(v) === i; }) + .join('\n'); +} +function ModifyError(msg, failures, successCount, failedKeys) { + this.failures = failures; + this.failedKeys = failedKeys; + this.successCount = successCount; + this.message = getMultiErrorMessage(msg, failures); +} +derive(ModifyError).from(DexieError); +function BulkError(msg, failures) { + this.name = "BulkError"; + this.failures = Object.keys(failures).map(function (pos) { return failures[pos]; }); + this.failuresByPos = failures; + this.message = getMultiErrorMessage(msg, this.failures); +} +derive(BulkError).from(DexieError); +var errnames = errorList.reduce(function (obj, name) { return (obj[name] = name + "Error", obj); }, {}); +var BaseException = DexieError; +var exceptions = errorList.reduce(function (obj, name) { + var fullName = name + "Error"; + function DexieError(msgOrInner, inner) { + this.name = fullName; + if (!msgOrInner) { + this.message = defaultTexts[name] || fullName; + this.inner = null; + } + else if (typeof msgOrInner === 'string') { + this.message = "".concat(msgOrInner).concat(!inner ? '' : '\n ' + inner); + this.inner = inner || null; + } + else if (typeof msgOrInner === 'object') { + this.message = "".concat(msgOrInner.name, " ").concat(msgOrInner.message); + this.inner = msgOrInner; + } + } + derive(DexieError).from(BaseException); + obj[name] = DexieError; + return obj; +}, {}); +exceptions.Syntax = SyntaxError; +exceptions.Type = TypeError; +exceptions.Range = RangeError; +var exceptionMap = idbDomErrorNames.reduce(function (obj, name) { + obj[name + "Error"] = exceptions[name]; + return obj; +}, {}); +function mapError(domError, message) { + if (!domError || domError instanceof DexieError || domError instanceof TypeError || domError instanceof SyntaxError || !domError.name || !exceptionMap[domError.name]) + return domError; + var rv = new exceptionMap[domError.name](message || domError.message, domError); + if ("stack" in domError) { + setProp(rv, "stack", { get: function () { + return this.inner.stack; + } }); + } + return rv; +} +var fullNameExceptions = errorList.reduce(function (obj, name) { + if (["Syntax", "Type", "Range"].indexOf(name) === -1) + obj[name + "Error"] = exceptions[name]; + return obj; +}, {}); +fullNameExceptions.ModifyError = ModifyError; +fullNameExceptions.DexieError = DexieError; +fullNameExceptions.BulkError = BulkError; + +function nop() { } +function mirror(val) { return val; } +function pureFunctionChain(f1, f2) { + if (f1 == null || f1 === mirror) + return f2; + return function (val) { + return f2(f1(val)); + }; +} +function callBoth(on1, on2) { + return function () { + on1.apply(this, arguments); + on2.apply(this, arguments); + }; +} +function hookCreatingChain(f1, f2) { + if (f1 === nop) + return f2; + return function () { + var res = f1.apply(this, arguments); + if (res !== undefined) + arguments[0] = res; + var onsuccess = this.onsuccess, + onerror = this.onerror; + this.onsuccess = null; + this.onerror = null; + var res2 = f2.apply(this, arguments); + if (onsuccess) + this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess; + if (onerror) + this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror; + return res2 !== undefined ? res2 : res; + }; +} +function hookDeletingChain(f1, f2) { + if (f1 === nop) + return f2; + return function () { + f1.apply(this, arguments); + var onsuccess = this.onsuccess, + onerror = this.onerror; + this.onsuccess = this.onerror = null; + f2.apply(this, arguments); + if (onsuccess) + this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess; + if (onerror) + this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror; + }; +} +function hookUpdatingChain(f1, f2) { + if (f1 === nop) + return f2; + return function (modifications) { + var res = f1.apply(this, arguments); + extend(modifications, res); + var onsuccess = this.onsuccess, + onerror = this.onerror; + this.onsuccess = null; + this.onerror = null; + var res2 = f2.apply(this, arguments); + if (onsuccess) + this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess; + if (onerror) + this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror; + return res === undefined ? + (res2 === undefined ? undefined : res2) : + (extend(res, res2)); + }; +} +function reverseStoppableEventChain(f1, f2) { + if (f1 === nop) + return f2; + return function () { + if (f2.apply(this, arguments) === false) + return false; + return f1.apply(this, arguments); + }; +} +function promisableChain(f1, f2) { + if (f1 === nop) + return f2; + return function () { + var res = f1.apply(this, arguments); + if (res && typeof res.then === 'function') { + var thiz = this, i = arguments.length, args = new Array(i); + while (i--) + args[i] = arguments[i]; + return res.then(function () { + return f2.apply(thiz, args); + }); + } + return f2.apply(this, arguments); + }; +} + +var debug = typeof location !== 'undefined' && + /^(http|https):\/\/(localhost|127\.0\.0\.1)/.test(location.href); +function setDebug(value, filter) { + debug = value; +} + +var INTERNAL = {}; +var ZONE_ECHO_LIMIT = 100, _a$1 = typeof Promise === 'undefined' ? + [] : + (function () { + var globalP = Promise.resolve(); + if (typeof crypto === 'undefined' || !crypto.subtle) + return [globalP, getProto(globalP), globalP]; + var nativeP = crypto.subtle.digest("SHA-512", new Uint8Array([0])); + return [ + nativeP, + getProto(nativeP), + globalP + ]; + })(), resolvedNativePromise = _a$1[0], nativePromiseProto = _a$1[1], resolvedGlobalPromise = _a$1[2], nativePromiseThen = nativePromiseProto && nativePromiseProto.then; +var NativePromise = resolvedNativePromise && resolvedNativePromise.constructor; +var patchGlobalPromise = !!resolvedGlobalPromise; +function schedulePhysicalTick() { + queueMicrotask(physicalTick); +} +var asap = function (callback, args) { + microtickQueue.push([callback, args]); + if (needsNewPhysicalTick) { + schedulePhysicalTick(); + needsNewPhysicalTick = false; + } +}; +var isOutsideMicroTick = true, +needsNewPhysicalTick = true, +unhandledErrors = [], +rejectingErrors = [], +rejectionMapper = mirror; +var globalPSD = { + id: 'global', + global: true, + ref: 0, + unhandleds: [], + onunhandled: nop, + pgp: false, + env: {}, + finalize: nop +}; +var PSD = globalPSD; +var microtickQueue = []; +var numScheduledCalls = 0; +var tickFinalizers = []; +function DexiePromise(fn) { + if (typeof this !== 'object') + throw new TypeError('Promises must be constructed via new'); + this._listeners = []; + this._lib = false; + var psd = (this._PSD = PSD); + if (typeof fn !== 'function') { + if (fn !== INTERNAL) + throw new TypeError('Not a function'); + this._state = arguments[1]; + this._value = arguments[2]; + if (this._state === false) + handleRejection(this, this._value); + return; + } + this._state = null; + this._value = null; + ++psd.ref; + executePromiseTask(this, fn); +} +var thenProp = { + get: function () { + var psd = PSD, microTaskId = totalEchoes; + function then(onFulfilled, onRejected) { + var _this = this; + var possibleAwait = !psd.global && (psd !== PSD || microTaskId !== totalEchoes); + var cleanup = possibleAwait && !decrementExpectedAwaits(); + var rv = new DexiePromise(function (resolve, reject) { + propagateToListener(_this, new Listener(nativeAwaitCompatibleWrap(onFulfilled, psd, possibleAwait, cleanup), nativeAwaitCompatibleWrap(onRejected, psd, possibleAwait, cleanup), resolve, reject, psd)); + }); + if (this._consoleTask) + rv._consoleTask = this._consoleTask; + return rv; + } + then.prototype = INTERNAL; + return then; + }, + set: function (value) { + setProp(this, 'then', value && value.prototype === INTERNAL ? + thenProp : + { + get: function () { + return value; + }, + set: thenProp.set + }); + } +}; +props(DexiePromise.prototype, { + then: thenProp, + _then: function (onFulfilled, onRejected) { + propagateToListener(this, new Listener(null, null, onFulfilled, onRejected, PSD)); + }, + catch: function (onRejected) { + if (arguments.length === 1) + return this.then(null, onRejected); + var type = arguments[0], handler = arguments[1]; + return typeof type === 'function' ? this.then(null, function (err) { + return err instanceof type ? handler(err) : PromiseReject(err); + }) + : this.then(null, function (err) { + return err && err.name === type ? handler(err) : PromiseReject(err); + }); + }, + finally: function (onFinally) { + return this.then(function (value) { + return DexiePromise.resolve(onFinally()).then(function () { return value; }); + }, function (err) { + return DexiePromise.resolve(onFinally()).then(function () { return PromiseReject(err); }); + }); + }, + timeout: function (ms, msg) { + var _this = this; + return ms < Infinity ? + new DexiePromise(function (resolve, reject) { + var handle = setTimeout(function () { return reject(new exceptions.Timeout(msg)); }, ms); + _this.then(resolve, reject).finally(clearTimeout.bind(null, handle)); + }) : this; + } +}); +if (typeof Symbol !== 'undefined' && Symbol.toStringTag) + setProp(DexiePromise.prototype, Symbol.toStringTag, 'Dexie.Promise'); +globalPSD.env = snapShot(); +function Listener(onFulfilled, onRejected, resolve, reject, zone) { + this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null; + this.onRejected = typeof onRejected === 'function' ? onRejected : null; + this.resolve = resolve; + this.reject = reject; + this.psd = zone; +} +props(DexiePromise, { + all: function () { + var values = getArrayOf.apply(null, arguments) + .map(onPossibleParallellAsync); + return new DexiePromise(function (resolve, reject) { + if (values.length === 0) + resolve([]); + var remaining = values.length; + values.forEach(function (a, i) { return DexiePromise.resolve(a).then(function (x) { + values[i] = x; + if (!--remaining) + resolve(values); + }, reject); }); + }); + }, + resolve: function (value) { + if (value instanceof DexiePromise) + return value; + if (value && typeof value.then === 'function') + return new DexiePromise(function (resolve, reject) { + value.then(resolve, reject); + }); + var rv = new DexiePromise(INTERNAL, true, value); + return rv; + }, + reject: PromiseReject, + race: function () { + var values = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync); + return new DexiePromise(function (resolve, reject) { + values.map(function (value) { return DexiePromise.resolve(value).then(resolve, reject); }); + }); + }, + PSD: { + get: function () { return PSD; }, + set: function (value) { return PSD = value; } + }, + totalEchoes: { get: function () { return totalEchoes; } }, + newPSD: newScope, + usePSD: usePSD, + scheduler: { + get: function () { return asap; }, + set: function (value) { asap = value; } + }, + rejectionMapper: { + get: function () { return rejectionMapper; }, + set: function (value) { rejectionMapper = value; } + }, + follow: function (fn, zoneProps) { + return new DexiePromise(function (resolve, reject) { + return newScope(function (resolve, reject) { + var psd = PSD; + psd.unhandleds = []; + psd.onunhandled = reject; + psd.finalize = callBoth(function () { + var _this = this; + run_at_end_of_this_or_next_physical_tick(function () { + _this.unhandleds.length === 0 ? resolve() : reject(_this.unhandleds[0]); + }); + }, psd.finalize); + fn(); + }, zoneProps, resolve, reject); + }); + } +}); +if (NativePromise) { + if (NativePromise.allSettled) + setProp(DexiePromise, "allSettled", function () { + var possiblePromises = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync); + return new DexiePromise(function (resolve) { + if (possiblePromises.length === 0) + resolve([]); + var remaining = possiblePromises.length; + var results = new Array(remaining); + possiblePromises.forEach(function (p, i) { return DexiePromise.resolve(p).then(function (value) { return results[i] = { status: "fulfilled", value: value }; }, function (reason) { return results[i] = { status: "rejected", reason: reason }; }) + .then(function () { return --remaining || resolve(results); }); }); + }); + }); + if (NativePromise.any && typeof AggregateError !== 'undefined') + setProp(DexiePromise, "any", function () { + var possiblePromises = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync); + return new DexiePromise(function (resolve, reject) { + if (possiblePromises.length === 0) + reject(new AggregateError([])); + var remaining = possiblePromises.length; + var failures = new Array(remaining); + possiblePromises.forEach(function (p, i) { return DexiePromise.resolve(p).then(function (value) { return resolve(value); }, function (failure) { + failures[i] = failure; + if (!--remaining) + reject(new AggregateError(failures)); + }); }); + }); + }); + if (NativePromise.withResolvers) + DexiePromise.withResolvers = NativePromise.withResolvers; +} +function executePromiseTask(promise, fn) { + try { + fn(function (value) { + if (promise._state !== null) + return; + if (value === promise) + throw new TypeError('A promise cannot be resolved with itself.'); + var shouldExecuteTick = promise._lib && beginMicroTickScope(); + if (value && typeof value.then === 'function') { + executePromiseTask(promise, function (resolve, reject) { + value instanceof DexiePromise ? + value._then(resolve, reject) : + value.then(resolve, reject); + }); + } + else { + promise._state = true; + promise._value = value; + propagateAllListeners(promise); + } + if (shouldExecuteTick) + endMicroTickScope(); + }, handleRejection.bind(null, promise)); + } + catch (ex) { + handleRejection(promise, ex); + } +} +function handleRejection(promise, reason) { + rejectingErrors.push(reason); + if (promise._state !== null) + return; + var shouldExecuteTick = promise._lib && beginMicroTickScope(); + reason = rejectionMapper(reason); + promise._state = false; + promise._value = reason; + addPossiblyUnhandledError(promise); + propagateAllListeners(promise); + if (shouldExecuteTick) + endMicroTickScope(); +} +function propagateAllListeners(promise) { + var listeners = promise._listeners; + promise._listeners = []; + for (var i = 0, len = listeners.length; i < len; ++i) { + propagateToListener(promise, listeners[i]); + } + var psd = promise._PSD; + --psd.ref || psd.finalize(); + if (numScheduledCalls === 0) { + ++numScheduledCalls; + asap(function () { + if (--numScheduledCalls === 0) + finalizePhysicalTick(); + }, []); + } +} +function propagateToListener(promise, listener) { + if (promise._state === null) { + promise._listeners.push(listener); + return; + } + var cb = promise._state ? listener.onFulfilled : listener.onRejected; + if (cb === null) { + return (promise._state ? listener.resolve : listener.reject)(promise._value); + } + ++listener.psd.ref; + ++numScheduledCalls; + asap(callListener, [cb, promise, listener]); +} +function callListener(cb, promise, listener) { + try { + var ret, value = promise._value; + if (!promise._state && rejectingErrors.length) + rejectingErrors = []; + ret = debug && promise._consoleTask ? promise._consoleTask.run(function () { return cb(value); }) : cb(value); + if (!promise._state && rejectingErrors.indexOf(value) === -1) { + markErrorAsHandled(promise); + } + listener.resolve(ret); + } + catch (e) { + listener.reject(e); + } + finally { + if (--numScheduledCalls === 0) + finalizePhysicalTick(); + --listener.psd.ref || listener.psd.finalize(); + } +} +function physicalTick() { + usePSD(globalPSD, function () { + beginMicroTickScope() && endMicroTickScope(); + }); +} +function beginMicroTickScope() { + var wasRootExec = isOutsideMicroTick; + isOutsideMicroTick = false; + needsNewPhysicalTick = false; + return wasRootExec; +} +function endMicroTickScope() { + var callbacks, i, l; + do { + while (microtickQueue.length > 0) { + callbacks = microtickQueue; + microtickQueue = []; + l = callbacks.length; + for (i = 0; i < l; ++i) { + var item = callbacks[i]; + item[0].apply(null, item[1]); + } + } + } while (microtickQueue.length > 0); + isOutsideMicroTick = true; + needsNewPhysicalTick = true; +} +function finalizePhysicalTick() { + var unhandledErrs = unhandledErrors; + unhandledErrors = []; + unhandledErrs.forEach(function (p) { + p._PSD.onunhandled.call(null, p._value, p); + }); + var finalizers = tickFinalizers.slice(0); + var i = finalizers.length; + while (i) + finalizers[--i](); +} +function run_at_end_of_this_or_next_physical_tick(fn) { + function finalizer() { + fn(); + tickFinalizers.splice(tickFinalizers.indexOf(finalizer), 1); + } + tickFinalizers.push(finalizer); + ++numScheduledCalls; + asap(function () { + if (--numScheduledCalls === 0) + finalizePhysicalTick(); + }, []); +} +function addPossiblyUnhandledError(promise) { + if (!unhandledErrors.some(function (p) { return p._value === promise._value; })) + unhandledErrors.push(promise); +} +function markErrorAsHandled(promise) { + var i = unhandledErrors.length; + while (i) + if (unhandledErrors[--i]._value === promise._value) { + unhandledErrors.splice(i, 1); + return; + } +} +function PromiseReject(reason) { + return new DexiePromise(INTERNAL, false, reason); +} +function wrap(fn, errorCatcher) { + var psd = PSD; + return function () { + var wasRootExec = beginMicroTickScope(), outerScope = PSD; + try { + switchToZone(psd, true); + return fn.apply(this, arguments); + } + catch (e) { + errorCatcher && errorCatcher(e); + } + finally { + switchToZone(outerScope, false); + if (wasRootExec) + endMicroTickScope(); + } + }; +} +var task = { awaits: 0, echoes: 0, id: 0 }; +var taskCounter = 0; +var zoneStack = []; +var zoneEchoes = 0; +var totalEchoes = 0; +var zone_id_counter = 0; +function newScope(fn, props, a1, a2) { + var parent = PSD, psd = Object.create(parent); + psd.parent = parent; + psd.ref = 0; + psd.global = false; + psd.id = ++zone_id_counter; + globalPSD.env; + psd.env = patchGlobalPromise ? { + Promise: DexiePromise, + PromiseProp: { value: DexiePromise, configurable: true, writable: true }, + all: DexiePromise.all, + race: DexiePromise.race, + allSettled: DexiePromise.allSettled, + any: DexiePromise.any, + resolve: DexiePromise.resolve, + reject: DexiePromise.reject, + } : {}; + if (props) + extend(psd, props); + ++parent.ref; + psd.finalize = function () { + --this.parent.ref || this.parent.finalize(); + }; + var rv = usePSD(psd, fn, a1, a2); + if (psd.ref === 0) + psd.finalize(); + return rv; +} +function incrementExpectedAwaits() { + if (!task.id) + task.id = ++taskCounter; + ++task.awaits; + task.echoes += ZONE_ECHO_LIMIT; + return task.id; +} +function decrementExpectedAwaits() { + if (!task.awaits) + return false; + if (--task.awaits === 0) + task.id = 0; + task.echoes = task.awaits * ZONE_ECHO_LIMIT; + return true; +} +if (('' + nativePromiseThen).indexOf('[native code]') === -1) { + incrementExpectedAwaits = decrementExpectedAwaits = nop; +} +function onPossibleParallellAsync(possiblePromise) { + if (task.echoes && possiblePromise && possiblePromise.constructor === NativePromise) { + incrementExpectedAwaits(); + return possiblePromise.then(function (x) { + decrementExpectedAwaits(); + return x; + }, function (e) { + decrementExpectedAwaits(); + return rejection(e); + }); + } + return possiblePromise; +} +function zoneEnterEcho(targetZone) { + ++totalEchoes; + if (!task.echoes || --task.echoes === 0) { + task.echoes = task.awaits = task.id = 0; + } + zoneStack.push(PSD); + switchToZone(targetZone, true); +} +function zoneLeaveEcho() { + var zone = zoneStack[zoneStack.length - 1]; + zoneStack.pop(); + switchToZone(zone, false); +} +function switchToZone(targetZone, bEnteringZone) { + var currentZone = PSD; + if (bEnteringZone ? task.echoes && (!zoneEchoes++ || targetZone !== PSD) : zoneEchoes && (!--zoneEchoes || targetZone !== PSD)) { + queueMicrotask(bEnteringZone ? zoneEnterEcho.bind(null, targetZone) : zoneLeaveEcho); + } + if (targetZone === PSD) + return; + PSD = targetZone; + if (currentZone === globalPSD) + globalPSD.env = snapShot(); + if (patchGlobalPromise) { + var GlobalPromise = globalPSD.env.Promise; + var targetEnv = targetZone.env; + if (currentZone.global || targetZone.global) { + Object.defineProperty(_global, 'Promise', targetEnv.PromiseProp); + GlobalPromise.all = targetEnv.all; + GlobalPromise.race = targetEnv.race; + GlobalPromise.resolve = targetEnv.resolve; + GlobalPromise.reject = targetEnv.reject; + if (targetEnv.allSettled) + GlobalPromise.allSettled = targetEnv.allSettled; + if (targetEnv.any) + GlobalPromise.any = targetEnv.any; + } + } +} +function snapShot() { + var GlobalPromise = _global.Promise; + return patchGlobalPromise ? { + Promise: GlobalPromise, + PromiseProp: Object.getOwnPropertyDescriptor(_global, "Promise"), + all: GlobalPromise.all, + race: GlobalPromise.race, + allSettled: GlobalPromise.allSettled, + any: GlobalPromise.any, + resolve: GlobalPromise.resolve, + reject: GlobalPromise.reject, + } : {}; +} +function usePSD(psd, fn, a1, a2, a3) { + var outerScope = PSD; + try { + switchToZone(psd, true); + return fn(a1, a2, a3); + } + finally { + switchToZone(outerScope, false); + } +} +function nativeAwaitCompatibleWrap(fn, zone, possibleAwait, cleanup) { + return typeof fn !== 'function' ? fn : function () { + var outerZone = PSD; + if (possibleAwait) + incrementExpectedAwaits(); + switchToZone(zone, true); + try { + return fn.apply(this, arguments); + } + finally { + switchToZone(outerZone, false); + if (cleanup) + queueMicrotask(decrementExpectedAwaits); + } + }; +} +function execInGlobalContext(cb) { + if (Promise === NativePromise && task.echoes === 0) { + if (zoneEchoes === 0) { + cb(); + } + else { + enqueueNativeMicroTask(cb); + } + } + else { + setTimeout(cb, 0); + } +} +var rejection = DexiePromise.reject; + +function tempTransaction(db, mode, storeNames, fn) { + if (!db.idbdb || (!db._state.openComplete && (!PSD.letThrough && !db._vip))) { + if (db._state.openComplete) { + return rejection(new exceptions.DatabaseClosed(db._state.dbOpenError)); + } + if (!db._state.isBeingOpened) { + if (!db._state.autoOpen) + return rejection(new exceptions.DatabaseClosed()); + db.open().catch(nop); + } + return db._state.dbReadyPromise.then(function () { return tempTransaction(db, mode, storeNames, fn); }); + } + else { + var trans = db._createTransaction(mode, storeNames, db._dbSchema); + try { + trans.create(); + db._state.PR1398_maxLoop = 3; + } + catch (ex) { + if (ex.name === errnames.InvalidState && db.isOpen() && --db._state.PR1398_maxLoop > 0) { + console.warn('Dexie: Need to reopen db'); + db.close({ disableAutoOpen: false }); + return db.open().then(function () { return tempTransaction(db, mode, storeNames, fn); }); + } + return rejection(ex); + } + return trans._promise(mode, function (resolve, reject) { + return newScope(function () { + PSD.trans = trans; + return fn(resolve, reject, trans); + }); + }).then(function (result) { + if (mode === 'readwrite') + try { + trans.idbtrans.commit(); + } + catch (_a) { } + return mode === 'readonly' ? result : trans._completion.then(function () { return result; }); + }); + } +} + +var DEXIE_VERSION = '4.0.10'; +var maxString = String.fromCharCode(65535); +var minKey = -Infinity; +var INVALID_KEY_ARGUMENT = "Invalid key provided. Keys must be of type string, number, Date or Array."; +var STRING_EXPECTED = "String expected."; +var connections = []; +var DBNAMES_DB = '__dbnames'; +var READONLY = 'readonly'; +var READWRITE = 'readwrite'; + +function combine(filter1, filter2) { + return filter1 ? + filter2 ? + function () { return filter1.apply(this, arguments) && filter2.apply(this, arguments); } : + filter1 : + filter2; +} + +var AnyRange = { + type: 3 , + lower: -Infinity, + lowerOpen: false, + upper: [[]], + upperOpen: false +}; + +function workaroundForUndefinedPrimKey(keyPath) { + return typeof keyPath === "string" && !/\./.test(keyPath) + ? function (obj) { + if (obj[keyPath] === undefined && (keyPath in obj)) { + obj = deepClone(obj); + delete obj[keyPath]; + } + return obj; + } + : function (obj) { return obj; }; +} + +function Entity() { + throw exceptions.Type(); +} + +function cmp(a, b) { + try { + var ta = type(a); + var tb = type(b); + if (ta !== tb) { + if (ta === 'Array') + return 1; + if (tb === 'Array') + return -1; + if (ta === 'binary') + return 1; + if (tb === 'binary') + return -1; + if (ta === 'string') + return 1; + if (tb === 'string') + return -1; + if (ta === 'Date') + return 1; + if (tb !== 'Date') + return NaN; + return -1; + } + switch (ta) { + case 'number': + case 'Date': + case 'string': + return a > b ? 1 : a < b ? -1 : 0; + case 'binary': { + return compareUint8Arrays(getUint8Array(a), getUint8Array(b)); + } + case 'Array': + return compareArrays(a, b); + } + } + catch (_a) { } + return NaN; +} +function compareArrays(a, b) { + var al = a.length; + var bl = b.length; + var l = al < bl ? al : bl; + for (var i = 0; i < l; ++i) { + var res = cmp(a[i], b[i]); + if (res !== 0) + return res; + } + return al === bl ? 0 : al < bl ? -1 : 1; +} +function compareUint8Arrays(a, b) { + var al = a.length; + var bl = b.length; + var l = al < bl ? al : bl; + for (var i = 0; i < l; ++i) { + if (a[i] !== b[i]) + return a[i] < b[i] ? -1 : 1; + } + return al === bl ? 0 : al < bl ? -1 : 1; +} +function type(x) { + var t = typeof x; + if (t !== 'object') + return t; + if (ArrayBuffer.isView(x)) + return 'binary'; + var tsTag = toStringTag(x); + return tsTag === 'ArrayBuffer' ? 'binary' : tsTag; +} +function getUint8Array(a) { + if (a instanceof Uint8Array) + return a; + if (ArrayBuffer.isView(a)) + return new Uint8Array(a.buffer, a.byteOffset, a.byteLength); + return new Uint8Array(a); +} + +var Table = (function () { + function Table() { + } + Table.prototype._trans = function (mode, fn, writeLocked) { + var trans = this._tx || PSD.trans; + var tableName = this.name; + var task = debug && typeof console !== 'undefined' && console.createTask && console.createTask("Dexie: ".concat(mode === 'readonly' ? 'read' : 'write', " ").concat(this.name)); + function checkTableInTransaction(resolve, reject, trans) { + if (!trans.schema[tableName]) + throw new exceptions.NotFound("Table " + tableName + " not part of transaction"); + return fn(trans.idbtrans, trans); + } + var wasRootExec = beginMicroTickScope(); + try { + var p = trans && trans.db._novip === this.db._novip ? + trans === PSD.trans ? + trans._promise(mode, checkTableInTransaction, writeLocked) : + newScope(function () { return trans._promise(mode, checkTableInTransaction, writeLocked); }, { trans: trans, transless: PSD.transless || PSD }) : + tempTransaction(this.db, mode, [this.name], checkTableInTransaction); + if (task) { + p._consoleTask = task; + p = p.catch(function (err) { + console.trace(err); + return rejection(err); + }); + } + return p; + } + finally { + if (wasRootExec) + endMicroTickScope(); + } + }; + Table.prototype.get = function (keyOrCrit, cb) { + var _this = this; + if (keyOrCrit && keyOrCrit.constructor === Object) + return this.where(keyOrCrit).first(cb); + if (keyOrCrit == null) + return rejection(new exceptions.Type("Invalid argument to Table.get()")); + return this._trans('readonly', function (trans) { + return _this.core.get({ trans: trans, key: keyOrCrit }) + .then(function (res) { return _this.hook.reading.fire(res); }); + }).then(cb); + }; + Table.prototype.where = function (indexOrCrit) { + if (typeof indexOrCrit === 'string') + return new this.db.WhereClause(this, indexOrCrit); + if (isArray(indexOrCrit)) + return new this.db.WhereClause(this, "[".concat(indexOrCrit.join('+'), "]")); + var keyPaths = keys(indexOrCrit); + if (keyPaths.length === 1) + return this + .where(keyPaths[0]) + .equals(indexOrCrit[keyPaths[0]]); + var compoundIndex = this.schema.indexes.concat(this.schema.primKey).filter(function (ix) { + if (ix.compound && + keyPaths.every(function (keyPath) { return ix.keyPath.indexOf(keyPath) >= 0; })) { + for (var i = 0; i < keyPaths.length; ++i) { + if (keyPaths.indexOf(ix.keyPath[i]) === -1) + return false; + } + return true; + } + return false; + }).sort(function (a, b) { return a.keyPath.length - b.keyPath.length; })[0]; + if (compoundIndex && this.db._maxKey !== maxString) { + var keyPathsInValidOrder = compoundIndex.keyPath.slice(0, keyPaths.length); + return this + .where(keyPathsInValidOrder) + .equals(keyPathsInValidOrder.map(function (kp) { return indexOrCrit[kp]; })); + } + if (!compoundIndex && debug) + console.warn("The query ".concat(JSON.stringify(indexOrCrit), " on ").concat(this.name, " would benefit from a ") + + "compound index [".concat(keyPaths.join('+'), "]")); + var idxByName = this.schema.idxByName; + function equals(a, b) { + return cmp(a, b) === 0; + } + var _a = keyPaths.reduce(function (_a, keyPath) { + var prevIndex = _a[0], prevFilterFn = _a[1]; + var index = idxByName[keyPath]; + var value = indexOrCrit[keyPath]; + return [ + prevIndex || index, + prevIndex || !index ? + combine(prevFilterFn, index && index.multi ? + function (x) { + var prop = getByKeyPath(x, keyPath); + return isArray(prop) && prop.some(function (item) { return equals(value, item); }); + } : function (x) { return equals(value, getByKeyPath(x, keyPath)); }) + : prevFilterFn + ]; + }, [null, null]), idx = _a[0], filterFunction = _a[1]; + return idx ? + this.where(idx.name).equals(indexOrCrit[idx.keyPath]) + .filter(filterFunction) : + compoundIndex ? + this.filter(filterFunction) : + this.where(keyPaths).equals(''); + }; + Table.prototype.filter = function (filterFunction) { + return this.toCollection().and(filterFunction); + }; + Table.prototype.count = function (thenShortcut) { + return this.toCollection().count(thenShortcut); + }; + Table.prototype.offset = function (offset) { + return this.toCollection().offset(offset); + }; + Table.prototype.limit = function (numRows) { + return this.toCollection().limit(numRows); + }; + Table.prototype.each = function (callback) { + return this.toCollection().each(callback); + }; + Table.prototype.toArray = function (thenShortcut) { + return this.toCollection().toArray(thenShortcut); + }; + Table.prototype.toCollection = function () { + return new this.db.Collection(new this.db.WhereClause(this)); + }; + Table.prototype.orderBy = function (index) { + return new this.db.Collection(new this.db.WhereClause(this, isArray(index) ? + "[".concat(index.join('+'), "]") : + index)); + }; + Table.prototype.reverse = function () { + return this.toCollection().reverse(); + }; + Table.prototype.mapToClass = function (constructor) { + var _a = this, db = _a.db, tableName = _a.name; + this.schema.mappedClass = constructor; + if (constructor.prototype instanceof Entity) { + constructor = (function (_super) { + __extends(class_1, _super); + function class_1() { + return _super !== null && _super.apply(this, arguments) || this; + } + Object.defineProperty(class_1.prototype, "db", { + get: function () { return db; }, + enumerable: false, + configurable: true + }); + class_1.prototype.table = function () { return tableName; }; + return class_1; + }(constructor)); + } + var inheritedProps = new Set(); + for (var proto = constructor.prototype; proto; proto = getProto(proto)) { + Object.getOwnPropertyNames(proto).forEach(function (propName) { return inheritedProps.add(propName); }); + } + var readHook = function (obj) { + if (!obj) + return obj; + var res = Object.create(constructor.prototype); + for (var m in obj) + if (!inheritedProps.has(m)) + try { + res[m] = obj[m]; + } + catch (_) { } + return res; + }; + if (this.schema.readHook) { + this.hook.reading.unsubscribe(this.schema.readHook); + } + this.schema.readHook = readHook; + this.hook("reading", readHook); + return constructor; + }; + Table.prototype.defineClass = function () { + function Class(content) { + extend(this, content); + } + return this.mapToClass(Class); + }; + Table.prototype.add = function (obj, key) { + var _this = this; + var _a = this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath; + var objToAdd = obj; + if (keyPath && auto) { + objToAdd = workaroundForUndefinedPrimKey(keyPath)(obj); + } + return this._trans('readwrite', function (trans) { + return _this.core.mutate({ trans: trans, type: 'add', keys: key != null ? [key] : null, values: [objToAdd] }); + }).then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : res.lastResult; }) + .then(function (lastResult) { + if (keyPath) { + try { + setByKeyPath(obj, keyPath, lastResult); + } + catch (_) { } + } + return lastResult; + }); + }; + Table.prototype.update = function (keyOrObject, modifications) { + if (typeof keyOrObject === 'object' && !isArray(keyOrObject)) { + var key = getByKeyPath(keyOrObject, this.schema.primKey.keyPath); + if (key === undefined) + return rejection(new exceptions.InvalidArgument("Given object does not contain its primary key")); + return this.where(":id").equals(key).modify(modifications); + } + else { + return this.where(":id").equals(keyOrObject).modify(modifications); + } + }; + Table.prototype.put = function (obj, key) { + var _this = this; + var _a = this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath; + var objToAdd = obj; + if (keyPath && auto) { + objToAdd = workaroundForUndefinedPrimKey(keyPath)(obj); + } + return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'put', values: [objToAdd], keys: key != null ? [key] : null }); }) + .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : res.lastResult; }) + .then(function (lastResult) { + if (keyPath) { + try { + setByKeyPath(obj, keyPath, lastResult); + } + catch (_) { } + } + return lastResult; + }); + }; + Table.prototype.delete = function (key) { + var _this = this; + return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'delete', keys: [key] }); }) + .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; }); + }; + Table.prototype.clear = function () { + var _this = this; + return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'deleteRange', range: AnyRange }); }) + .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; }); + }; + Table.prototype.bulkGet = function (keys) { + var _this = this; + return this._trans('readonly', function (trans) { + return _this.core.getMany({ + keys: keys, + trans: trans + }).then(function (result) { return result.map(function (res) { return _this.hook.reading.fire(res); }); }); + }); + }; + Table.prototype.bulkAdd = function (objects, keysOrOptions, options) { + var _this = this; + var keys = Array.isArray(keysOrOptions) ? keysOrOptions : undefined; + options = options || (keys ? undefined : keysOrOptions); + var wantResults = options ? options.allKeys : undefined; + return this._trans('readwrite', function (trans) { + var _a = _this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath; + if (keyPath && keys) + throw new exceptions.InvalidArgument("bulkAdd(): keys argument invalid on tables with inbound keys"); + if (keys && keys.length !== objects.length) + throw new exceptions.InvalidArgument("Arguments objects and keys must have the same length"); + var numObjects = objects.length; + var objectsToAdd = keyPath && auto ? + objects.map(workaroundForUndefinedPrimKey(keyPath)) : + objects; + return _this.core.mutate({ trans: trans, type: 'add', keys: keys, values: objectsToAdd, wantResults: wantResults }) + .then(function (_a) { + var numFailures = _a.numFailures, results = _a.results, lastResult = _a.lastResult, failures = _a.failures; + var result = wantResults ? results : lastResult; + if (numFailures === 0) + return result; + throw new BulkError("".concat(_this.name, ".bulkAdd(): ").concat(numFailures, " of ").concat(numObjects, " operations failed"), failures); + }); + }); + }; + Table.prototype.bulkPut = function (objects, keysOrOptions, options) { + var _this = this; + var keys = Array.isArray(keysOrOptions) ? keysOrOptions : undefined; + options = options || (keys ? undefined : keysOrOptions); + var wantResults = options ? options.allKeys : undefined; + return this._trans('readwrite', function (trans) { + var _a = _this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath; + if (keyPath && keys) + throw new exceptions.InvalidArgument("bulkPut(): keys argument invalid on tables with inbound keys"); + if (keys && keys.length !== objects.length) + throw new exceptions.InvalidArgument("Arguments objects and keys must have the same length"); + var numObjects = objects.length; + var objectsToPut = keyPath && auto ? + objects.map(workaroundForUndefinedPrimKey(keyPath)) : + objects; + return _this.core.mutate({ trans: trans, type: 'put', keys: keys, values: objectsToPut, wantResults: wantResults }) + .then(function (_a) { + var numFailures = _a.numFailures, results = _a.results, lastResult = _a.lastResult, failures = _a.failures; + var result = wantResults ? results : lastResult; + if (numFailures === 0) + return result; + throw new BulkError("".concat(_this.name, ".bulkPut(): ").concat(numFailures, " of ").concat(numObjects, " operations failed"), failures); + }); + }); + }; + Table.prototype.bulkUpdate = function (keysAndChanges) { + var _this = this; + var coreTable = this.core; + var keys = keysAndChanges.map(function (entry) { return entry.key; }); + var changeSpecs = keysAndChanges.map(function (entry) { return entry.changes; }); + var offsetMap = []; + return this._trans('readwrite', function (trans) { + return coreTable.getMany({ trans: trans, keys: keys, cache: 'clone' }).then(function (objs) { + var resultKeys = []; + var resultObjs = []; + keysAndChanges.forEach(function (_a, idx) { + var key = _a.key, changes = _a.changes; + var obj = objs[idx]; + if (obj) { + for (var _i = 0, _b = Object.keys(changes); _i < _b.length; _i++) { + var keyPath = _b[_i]; + var value = changes[keyPath]; + if (keyPath === _this.schema.primKey.keyPath) { + if (cmp(value, key) !== 0) { + throw new exceptions.Constraint("Cannot update primary key in bulkUpdate()"); + } + } + else { + setByKeyPath(obj, keyPath, value); + } + } + offsetMap.push(idx); + resultKeys.push(key); + resultObjs.push(obj); + } + }); + var numEntries = resultKeys.length; + return coreTable + .mutate({ + trans: trans, + type: 'put', + keys: resultKeys, + values: resultObjs, + updates: { + keys: keys, + changeSpecs: changeSpecs + } + }) + .then(function (_a) { + var numFailures = _a.numFailures, failures = _a.failures; + if (numFailures === 0) + return numEntries; + for (var _i = 0, _b = Object.keys(failures); _i < _b.length; _i++) { + var offset = _b[_i]; + var mappedOffset = offsetMap[Number(offset)]; + if (mappedOffset != null) { + var failure = failures[offset]; + delete failures[offset]; + failures[mappedOffset] = failure; + } + } + throw new BulkError("".concat(_this.name, ".bulkUpdate(): ").concat(numFailures, " of ").concat(numEntries, " operations failed"), failures); + }); + }); + }); + }; + Table.prototype.bulkDelete = function (keys) { + var _this = this; + var numKeys = keys.length; + return this._trans('readwrite', function (trans) { + return _this.core.mutate({ trans: trans, type: 'delete', keys: keys }); + }).then(function (_a) { + var numFailures = _a.numFailures, lastResult = _a.lastResult, failures = _a.failures; + if (numFailures === 0) + return lastResult; + throw new BulkError("".concat(_this.name, ".bulkDelete(): ").concat(numFailures, " of ").concat(numKeys, " operations failed"), failures); + }); + }; + return Table; +}()); + +function Events(ctx) { + var evs = {}; + var rv = function (eventName, subscriber) { + if (subscriber) { + var i = arguments.length, args = new Array(i - 1); + while (--i) + args[i - 1] = arguments[i]; + evs[eventName].subscribe.apply(null, args); + return ctx; + } + else if (typeof (eventName) === 'string') { + return evs[eventName]; + } + }; + rv.addEventType = add; + for (var i = 1, l = arguments.length; i < l; ++i) { + add(arguments[i]); + } + return rv; + function add(eventName, chainFunction, defaultFunction) { + if (typeof eventName === 'object') + return addConfiguredEvents(eventName); + if (!chainFunction) + chainFunction = reverseStoppableEventChain; + if (!defaultFunction) + defaultFunction = nop; + var context = { + subscribers: [], + fire: defaultFunction, + subscribe: function (cb) { + if (context.subscribers.indexOf(cb) === -1) { + context.subscribers.push(cb); + context.fire = chainFunction(context.fire, cb); + } + }, + unsubscribe: function (cb) { + context.subscribers = context.subscribers.filter(function (fn) { return fn !== cb; }); + context.fire = context.subscribers.reduce(chainFunction, defaultFunction); + } + }; + evs[eventName] = rv[eventName] = context; + return context; + } + function addConfiguredEvents(cfg) { + keys(cfg).forEach(function (eventName) { + var args = cfg[eventName]; + if (isArray(args)) { + add(eventName, cfg[eventName][0], cfg[eventName][1]); + } + else if (args === 'asap') { + var context = add(eventName, mirror, function fire() { + var i = arguments.length, args = new Array(i); + while (i--) + args[i] = arguments[i]; + context.subscribers.forEach(function (fn) { + asap$1(function fireEvent() { + fn.apply(null, args); + }); + }); + }); + } + else + throw new exceptions.InvalidArgument("Invalid event config"); + }); + } +} + +function makeClassConstructor(prototype, constructor) { + derive(constructor).from({ prototype: prototype }); + return constructor; +} + +function createTableConstructor(db) { + return makeClassConstructor(Table.prototype, function Table(name, tableSchema, trans) { + this.db = db; + this._tx = trans; + this.name = name; + this.schema = tableSchema; + this.hook = db._allTables[name] ? db._allTables[name].hook : Events(null, { + "creating": [hookCreatingChain, nop], + "reading": [pureFunctionChain, mirror], + "updating": [hookUpdatingChain, nop], + "deleting": [hookDeletingChain, nop] + }); + }); +} + +function isPlainKeyRange(ctx, ignoreLimitFilter) { + return !(ctx.filter || ctx.algorithm || ctx.or) && + (ignoreLimitFilter ? ctx.justLimit : !ctx.replayFilter); +} +function addFilter(ctx, fn) { + ctx.filter = combine(ctx.filter, fn); +} +function addReplayFilter(ctx, factory, isLimitFilter) { + var curr = ctx.replayFilter; + ctx.replayFilter = curr ? function () { return combine(curr(), factory()); } : factory; + ctx.justLimit = isLimitFilter && !curr; +} +function addMatchFilter(ctx, fn) { + ctx.isMatch = combine(ctx.isMatch, fn); +} +function getIndexOrStore(ctx, coreSchema) { + if (ctx.isPrimKey) + return coreSchema.primaryKey; + var index = coreSchema.getIndexByKeyPath(ctx.index); + if (!index) + throw new exceptions.Schema("KeyPath " + ctx.index + " on object store " + coreSchema.name + " is not indexed"); + return index; +} +function openCursor(ctx, coreTable, trans) { + var index = getIndexOrStore(ctx, coreTable.schema); + return coreTable.openCursor({ + trans: trans, + values: !ctx.keysOnly, + reverse: ctx.dir === 'prev', + unique: !!ctx.unique, + query: { + index: index, + range: ctx.range + } + }); +} +function iter(ctx, fn, coreTrans, coreTable) { + var filter = ctx.replayFilter ? combine(ctx.filter, ctx.replayFilter()) : ctx.filter; + if (!ctx.or) { + return iterate(openCursor(ctx, coreTable, coreTrans), combine(ctx.algorithm, filter), fn, !ctx.keysOnly && ctx.valueMapper); + } + else { + var set_1 = {}; + var union = function (item, cursor, advance) { + if (!filter || filter(cursor, advance, function (result) { return cursor.stop(result); }, function (err) { return cursor.fail(err); })) { + var primaryKey = cursor.primaryKey; + var key = '' + primaryKey; + if (key === '[object ArrayBuffer]') + key = '' + new Uint8Array(primaryKey); + if (!hasOwn(set_1, key)) { + set_1[key] = true; + fn(item, cursor, advance); + } + } + }; + return Promise.all([ + ctx.or._iterate(union, coreTrans), + iterate(openCursor(ctx, coreTable, coreTrans), ctx.algorithm, union, !ctx.keysOnly && ctx.valueMapper) + ]); + } +} +function iterate(cursorPromise, filter, fn, valueMapper) { + var mappedFn = valueMapper ? function (x, c, a) { return fn(valueMapper(x), c, a); } : fn; + var wrappedFn = wrap(mappedFn); + return cursorPromise.then(function (cursor) { + if (cursor) { + return cursor.start(function () { + var c = function () { return cursor.continue(); }; + if (!filter || filter(cursor, function (advancer) { return c = advancer; }, function (val) { cursor.stop(val); c = nop; }, function (e) { cursor.fail(e); c = nop; })) + wrappedFn(cursor.value, cursor, function (advancer) { return c = advancer; }); + c(); + }); + } + }); +} + +var PropModSymbol = Symbol(); +var PropModification = (function () { + function PropModification(spec) { + Object.assign(this, spec); + } + PropModification.prototype.execute = function (value) { + var _a; + if (this.add !== undefined) { + var term = this.add; + if (isArray(term)) { + return __spreadArray(__spreadArray([], (isArray(value) ? value : []), true), term, true).sort(); + } + if (typeof term === 'number') + return (Number(value) || 0) + term; + if (typeof term === 'bigint') { + try { + return BigInt(value) + term; + } + catch (_b) { + return BigInt(0) + term; + } + } + throw new TypeError("Invalid term ".concat(term)); + } + if (this.remove !== undefined) { + var subtrahend_1 = this.remove; + if (isArray(subtrahend_1)) { + return isArray(value) ? value.filter(function (item) { return !subtrahend_1.includes(item); }).sort() : []; + } + if (typeof subtrahend_1 === 'number') + return Number(value) - subtrahend_1; + if (typeof subtrahend_1 === 'bigint') { + try { + return BigInt(value) - subtrahend_1; + } + catch (_c) { + return BigInt(0) - subtrahend_1; + } + } + throw new TypeError("Invalid subtrahend ".concat(subtrahend_1)); + } + var prefixToReplace = (_a = this.replacePrefix) === null || _a === void 0 ? void 0 : _a[0]; + if (prefixToReplace && typeof value === 'string' && value.startsWith(prefixToReplace)) { + return this.replacePrefix[1] + value.substring(prefixToReplace.length); + } + return value; + }; + return PropModification; +}()); + +var Collection = (function () { + function Collection() { + } + Collection.prototype._read = function (fn, cb) { + var ctx = this._ctx; + return ctx.error ? + ctx.table._trans(null, rejection.bind(null, ctx.error)) : + ctx.table._trans('readonly', fn).then(cb); + }; + Collection.prototype._write = function (fn) { + var ctx = this._ctx; + return ctx.error ? + ctx.table._trans(null, rejection.bind(null, ctx.error)) : + ctx.table._trans('readwrite', fn, "locked"); + }; + Collection.prototype._addAlgorithm = function (fn) { + var ctx = this._ctx; + ctx.algorithm = combine(ctx.algorithm, fn); + }; + Collection.prototype._iterate = function (fn, coreTrans) { + return iter(this._ctx, fn, coreTrans, this._ctx.table.core); + }; + Collection.prototype.clone = function (props) { + var rv = Object.create(this.constructor.prototype), ctx = Object.create(this._ctx); + if (props) + extend(ctx, props); + rv._ctx = ctx; + return rv; + }; + Collection.prototype.raw = function () { + this._ctx.valueMapper = null; + return this; + }; + Collection.prototype.each = function (fn) { + var ctx = this._ctx; + return this._read(function (trans) { return iter(ctx, fn, trans, ctx.table.core); }); + }; + Collection.prototype.count = function (cb) { + var _this = this; + return this._read(function (trans) { + var ctx = _this._ctx; + var coreTable = ctx.table.core; + if (isPlainKeyRange(ctx, true)) { + return coreTable.count({ + trans: trans, + query: { + index: getIndexOrStore(ctx, coreTable.schema), + range: ctx.range + } + }).then(function (count) { return Math.min(count, ctx.limit); }); + } + else { + var count = 0; + return iter(ctx, function () { ++count; return false; }, trans, coreTable) + .then(function () { return count; }); + } + }).then(cb); + }; + Collection.prototype.sortBy = function (keyPath, cb) { + var parts = keyPath.split('.').reverse(), lastPart = parts[0], lastIndex = parts.length - 1; + function getval(obj, i) { + if (i) + return getval(obj[parts[i]], i - 1); + return obj[lastPart]; + } + var order = this._ctx.dir === "next" ? 1 : -1; + function sorter(a, b) { + var aVal = getval(a, lastIndex), bVal = getval(b, lastIndex); + return cmp(aVal, bVal) * order; + } + return this.toArray(function (a) { + return a.sort(sorter); + }).then(cb); + }; + Collection.prototype.toArray = function (cb) { + var _this = this; + return this._read(function (trans) { + var ctx = _this._ctx; + if (ctx.dir === 'next' && isPlainKeyRange(ctx, true) && ctx.limit > 0) { + var valueMapper_1 = ctx.valueMapper; + var index = getIndexOrStore(ctx, ctx.table.core.schema); + return ctx.table.core.query({ + trans: trans, + limit: ctx.limit, + values: true, + query: { + index: index, + range: ctx.range + } + }).then(function (_a) { + var result = _a.result; + return valueMapper_1 ? result.map(valueMapper_1) : result; + }); + } + else { + var a_1 = []; + return iter(ctx, function (item) { return a_1.push(item); }, trans, ctx.table.core).then(function () { return a_1; }); + } + }, cb); + }; + Collection.prototype.offset = function (offset) { + var ctx = this._ctx; + if (offset <= 0) + return this; + ctx.offset += offset; + if (isPlainKeyRange(ctx)) { + addReplayFilter(ctx, function () { + var offsetLeft = offset; + return function (cursor, advance) { + if (offsetLeft === 0) + return true; + if (offsetLeft === 1) { + --offsetLeft; + return false; + } + advance(function () { + cursor.advance(offsetLeft); + offsetLeft = 0; + }); + return false; + }; + }); + } + else { + addReplayFilter(ctx, function () { + var offsetLeft = offset; + return function () { return (--offsetLeft < 0); }; + }); + } + return this; + }; + Collection.prototype.limit = function (numRows) { + this._ctx.limit = Math.min(this._ctx.limit, numRows); + addReplayFilter(this._ctx, function () { + var rowsLeft = numRows; + return function (cursor, advance, resolve) { + if (--rowsLeft <= 0) + advance(resolve); + return rowsLeft >= 0; + }; + }, true); + return this; + }; + Collection.prototype.until = function (filterFunction, bIncludeStopEntry) { + addFilter(this._ctx, function (cursor, advance, resolve) { + if (filterFunction(cursor.value)) { + advance(resolve); + return bIncludeStopEntry; + } + else { + return true; + } + }); + return this; + }; + Collection.prototype.first = function (cb) { + return this.limit(1).toArray(function (a) { return a[0]; }).then(cb); + }; + Collection.prototype.last = function (cb) { + return this.reverse().first(cb); + }; + Collection.prototype.filter = function (filterFunction) { + addFilter(this._ctx, function (cursor) { + return filterFunction(cursor.value); + }); + addMatchFilter(this._ctx, filterFunction); + return this; + }; + Collection.prototype.and = function (filter) { + return this.filter(filter); + }; + Collection.prototype.or = function (indexName) { + return new this.db.WhereClause(this._ctx.table, indexName, this); + }; + Collection.prototype.reverse = function () { + this._ctx.dir = (this._ctx.dir === "prev" ? "next" : "prev"); + if (this._ondirectionchange) + this._ondirectionchange(this._ctx.dir); + return this; + }; + Collection.prototype.desc = function () { + return this.reverse(); + }; + Collection.prototype.eachKey = function (cb) { + var ctx = this._ctx; + ctx.keysOnly = !ctx.isMatch; + return this.each(function (val, cursor) { cb(cursor.key, cursor); }); + }; + Collection.prototype.eachUniqueKey = function (cb) { + this._ctx.unique = "unique"; + return this.eachKey(cb); + }; + Collection.prototype.eachPrimaryKey = function (cb) { + var ctx = this._ctx; + ctx.keysOnly = !ctx.isMatch; + return this.each(function (val, cursor) { cb(cursor.primaryKey, cursor); }); + }; + Collection.prototype.keys = function (cb) { + var ctx = this._ctx; + ctx.keysOnly = !ctx.isMatch; + var a = []; + return this.each(function (item, cursor) { + a.push(cursor.key); + }).then(function () { + return a; + }).then(cb); + }; + Collection.prototype.primaryKeys = function (cb) { + var ctx = this._ctx; + if (ctx.dir === 'next' && isPlainKeyRange(ctx, true) && ctx.limit > 0) { + return this._read(function (trans) { + var index = getIndexOrStore(ctx, ctx.table.core.schema); + return ctx.table.core.query({ + trans: trans, + values: false, + limit: ctx.limit, + query: { + index: index, + range: ctx.range + } + }); + }).then(function (_a) { + var result = _a.result; + return result; + }).then(cb); + } + ctx.keysOnly = !ctx.isMatch; + var a = []; + return this.each(function (item, cursor) { + a.push(cursor.primaryKey); + }).then(function () { + return a; + }).then(cb); + }; + Collection.prototype.uniqueKeys = function (cb) { + this._ctx.unique = "unique"; + return this.keys(cb); + }; + Collection.prototype.firstKey = function (cb) { + return this.limit(1).keys(function (a) { return a[0]; }).then(cb); + }; + Collection.prototype.lastKey = function (cb) { + return this.reverse().firstKey(cb); + }; + Collection.prototype.distinct = function () { + var ctx = this._ctx, idx = ctx.index && ctx.table.schema.idxByName[ctx.index]; + if (!idx || !idx.multi) + return this; + var set = {}; + addFilter(this._ctx, function (cursor) { + var strKey = cursor.primaryKey.toString(); + var found = hasOwn(set, strKey); + set[strKey] = true; + return !found; + }); + return this; + }; + Collection.prototype.modify = function (changes) { + var _this = this; + var ctx = this._ctx; + return this._write(function (trans) { + var modifyer; + if (typeof changes === 'function') { + modifyer = changes; + } + else { + var keyPaths = keys(changes); + var numKeys = keyPaths.length; + modifyer = function (item) { + var anythingModified = false; + for (var i = 0; i < numKeys; ++i) { + var keyPath = keyPaths[i]; + var val = changes[keyPath]; + var origVal = getByKeyPath(item, keyPath); + if (val instanceof PropModification) { + setByKeyPath(item, keyPath, val.execute(origVal)); + anythingModified = true; + } + else if (origVal !== val) { + setByKeyPath(item, keyPath, val); + anythingModified = true; + } + } + return anythingModified; + }; + } + var coreTable = ctx.table.core; + var _a = coreTable.schema.primaryKey, outbound = _a.outbound, extractKey = _a.extractKey; + var limit = 200; + var modifyChunkSize = _this.db._options.modifyChunkSize; + if (modifyChunkSize) { + if (typeof modifyChunkSize == 'object') { + limit = modifyChunkSize[coreTable.name] || modifyChunkSize['*'] || 200; + } + else { + limit = modifyChunkSize; + } + } + var totalFailures = []; + var successCount = 0; + var failedKeys = []; + var applyMutateResult = function (expectedCount, res) { + var failures = res.failures, numFailures = res.numFailures; + successCount += expectedCount - numFailures; + for (var _i = 0, _a = keys(failures); _i < _a.length; _i++) { + var pos = _a[_i]; + totalFailures.push(failures[pos]); + } + }; + return _this.clone().primaryKeys().then(function (keys) { + var criteria = isPlainKeyRange(ctx) && + ctx.limit === Infinity && + (typeof changes !== 'function' || changes === deleteCallback) && { + index: ctx.index, + range: ctx.range + }; + var nextChunk = function (offset) { + var count = Math.min(limit, keys.length - offset); + return coreTable.getMany({ + trans: trans, + keys: keys.slice(offset, offset + count), + cache: "immutable" + }).then(function (values) { + var addValues = []; + var putValues = []; + var putKeys = outbound ? [] : null; + var deleteKeys = []; + for (var i = 0; i < count; ++i) { + var origValue = values[i]; + var ctx_1 = { + value: deepClone(origValue), + primKey: keys[offset + i] + }; + if (modifyer.call(ctx_1, ctx_1.value, ctx_1) !== false) { + if (ctx_1.value == null) { + deleteKeys.push(keys[offset + i]); + } + else if (!outbound && cmp(extractKey(origValue), extractKey(ctx_1.value)) !== 0) { + deleteKeys.push(keys[offset + i]); + addValues.push(ctx_1.value); + } + else { + putValues.push(ctx_1.value); + if (outbound) + putKeys.push(keys[offset + i]); + } + } + } + return Promise.resolve(addValues.length > 0 && + coreTable.mutate({ trans: trans, type: 'add', values: addValues }) + .then(function (res) { + for (var pos in res.failures) { + deleteKeys.splice(parseInt(pos), 1); + } + applyMutateResult(addValues.length, res); + })).then(function () { return (putValues.length > 0 || (criteria && typeof changes === 'object')) && + coreTable.mutate({ + trans: trans, + type: 'put', + keys: putKeys, + values: putValues, + criteria: criteria, + changeSpec: typeof changes !== 'function' + && changes, + isAdditionalChunk: offset > 0 + }).then(function (res) { return applyMutateResult(putValues.length, res); }); }).then(function () { return (deleteKeys.length > 0 || (criteria && changes === deleteCallback)) && + coreTable.mutate({ + trans: trans, + type: 'delete', + keys: deleteKeys, + criteria: criteria, + isAdditionalChunk: offset > 0 + }).then(function (res) { return applyMutateResult(deleteKeys.length, res); }); }).then(function () { + return keys.length > offset + count && nextChunk(offset + limit); + }); + }); + }; + return nextChunk(0).then(function () { + if (totalFailures.length > 0) + throw new ModifyError("Error modifying one or more objects", totalFailures, successCount, failedKeys); + return keys.length; + }); + }); + }); + }; + Collection.prototype.delete = function () { + var ctx = this._ctx, range = ctx.range; + if (isPlainKeyRange(ctx) && + (ctx.isPrimKey || range.type === 3 )) + { + return this._write(function (trans) { + var primaryKey = ctx.table.core.schema.primaryKey; + var coreRange = range; + return ctx.table.core.count({ trans: trans, query: { index: primaryKey, range: coreRange } }).then(function (count) { + return ctx.table.core.mutate({ trans: trans, type: 'deleteRange', range: coreRange }) + .then(function (_a) { + var failures = _a.failures; _a.lastResult; _a.results; var numFailures = _a.numFailures; + if (numFailures) + throw new ModifyError("Could not delete some values", Object.keys(failures).map(function (pos) { return failures[pos]; }), count - numFailures); + return count - numFailures; + }); + }); + }); + } + return this.modify(deleteCallback); + }; + return Collection; +}()); +var deleteCallback = function (value, ctx) { return ctx.value = null; }; + +function createCollectionConstructor(db) { + return makeClassConstructor(Collection.prototype, function Collection(whereClause, keyRangeGenerator) { + this.db = db; + var keyRange = AnyRange, error = null; + if (keyRangeGenerator) + try { + keyRange = keyRangeGenerator(); + } + catch (ex) { + error = ex; + } + var whereCtx = whereClause._ctx; + var table = whereCtx.table; + var readingHook = table.hook.reading.fire; + this._ctx = { + table: table, + index: whereCtx.index, + isPrimKey: (!whereCtx.index || (table.schema.primKey.keyPath && whereCtx.index === table.schema.primKey.name)), + range: keyRange, + keysOnly: false, + dir: "next", + unique: "", + algorithm: null, + filter: null, + replayFilter: null, + justLimit: true, + isMatch: null, + offset: 0, + limit: Infinity, + error: error, + or: whereCtx.or, + valueMapper: readingHook !== mirror ? readingHook : null + }; + }); +} + +function simpleCompare(a, b) { + return a < b ? -1 : a === b ? 0 : 1; +} +function simpleCompareReverse(a, b) { + return a > b ? -1 : a === b ? 0 : 1; +} + +function fail(collectionOrWhereClause, err, T) { + var collection = collectionOrWhereClause instanceof WhereClause ? + new collectionOrWhereClause.Collection(collectionOrWhereClause) : + collectionOrWhereClause; + collection._ctx.error = T ? new T(err) : new TypeError(err); + return collection; +} +function emptyCollection(whereClause) { + return new whereClause.Collection(whereClause, function () { return rangeEqual(""); }).limit(0); +} +function upperFactory(dir) { + return dir === "next" ? + function (s) { return s.toUpperCase(); } : + function (s) { return s.toLowerCase(); }; +} +function lowerFactory(dir) { + return dir === "next" ? + function (s) { return s.toLowerCase(); } : + function (s) { return s.toUpperCase(); }; +} +function nextCasing(key, lowerKey, upperNeedle, lowerNeedle, cmp, dir) { + var length = Math.min(key.length, lowerNeedle.length); + var llp = -1; + for (var i = 0; i < length; ++i) { + var lwrKeyChar = lowerKey[i]; + if (lwrKeyChar !== lowerNeedle[i]) { + if (cmp(key[i], upperNeedle[i]) < 0) + return key.substr(0, i) + upperNeedle[i] + upperNeedle.substr(i + 1); + if (cmp(key[i], lowerNeedle[i]) < 0) + return key.substr(0, i) + lowerNeedle[i] + upperNeedle.substr(i + 1); + if (llp >= 0) + return key.substr(0, llp) + lowerKey[llp] + upperNeedle.substr(llp + 1); + return null; + } + if (cmp(key[i], lwrKeyChar) < 0) + llp = i; + } + if (length < lowerNeedle.length && dir === "next") + return key + upperNeedle.substr(key.length); + if (length < key.length && dir === "prev") + return key.substr(0, upperNeedle.length); + return (llp < 0 ? null : key.substr(0, llp) + lowerNeedle[llp] + upperNeedle.substr(llp + 1)); +} +function addIgnoreCaseAlgorithm(whereClause, match, needles, suffix) { + var upper, lower, compare, upperNeedles, lowerNeedles, direction, nextKeySuffix, needlesLen = needles.length; + if (!needles.every(function (s) { return typeof s === 'string'; })) { + return fail(whereClause, STRING_EXPECTED); + } + function initDirection(dir) { + upper = upperFactory(dir); + lower = lowerFactory(dir); + compare = (dir === "next" ? simpleCompare : simpleCompareReverse); + var needleBounds = needles.map(function (needle) { + return { lower: lower(needle), upper: upper(needle) }; + }).sort(function (a, b) { + return compare(a.lower, b.lower); + }); + upperNeedles = needleBounds.map(function (nb) { return nb.upper; }); + lowerNeedles = needleBounds.map(function (nb) { return nb.lower; }); + direction = dir; + nextKeySuffix = (dir === "next" ? "" : suffix); + } + initDirection("next"); + var c = new whereClause.Collection(whereClause, function () { return createRange(upperNeedles[0], lowerNeedles[needlesLen - 1] + suffix); }); + c._ondirectionchange = function (direction) { + initDirection(direction); + }; + var firstPossibleNeedle = 0; + c._addAlgorithm(function (cursor, advance, resolve) { + var key = cursor.key; + if (typeof key !== 'string') + return false; + var lowerKey = lower(key); + if (match(lowerKey, lowerNeedles, firstPossibleNeedle)) { + return true; + } + else { + var lowestPossibleCasing = null; + for (var i = firstPossibleNeedle; i < needlesLen; ++i) { + var casing = nextCasing(key, lowerKey, upperNeedles[i], lowerNeedles[i], compare, direction); + if (casing === null && lowestPossibleCasing === null) + firstPossibleNeedle = i + 1; + else if (lowestPossibleCasing === null || compare(lowestPossibleCasing, casing) > 0) { + lowestPossibleCasing = casing; + } + } + if (lowestPossibleCasing !== null) { + advance(function () { cursor.continue(lowestPossibleCasing + nextKeySuffix); }); + } + else { + advance(resolve); + } + return false; + } + }); + return c; +} +function createRange(lower, upper, lowerOpen, upperOpen) { + return { + type: 2 , + lower: lower, + upper: upper, + lowerOpen: lowerOpen, + upperOpen: upperOpen + }; +} +function rangeEqual(value) { + return { + type: 1 , + lower: value, + upper: value + }; +} + +var WhereClause = (function () { + function WhereClause() { + } + Object.defineProperty(WhereClause.prototype, "Collection", { + get: function () { + return this._ctx.table.db.Collection; + }, + enumerable: false, + configurable: true + }); + WhereClause.prototype.between = function (lower, upper, includeLower, includeUpper) { + includeLower = includeLower !== false; + includeUpper = includeUpper === true; + try { + if ((this._cmp(lower, upper) > 0) || + (this._cmp(lower, upper) === 0 && (includeLower || includeUpper) && !(includeLower && includeUpper))) + return emptyCollection(this); + return new this.Collection(this, function () { return createRange(lower, upper, !includeLower, !includeUpper); }); + } + catch (e) { + return fail(this, INVALID_KEY_ARGUMENT); + } + }; + WhereClause.prototype.equals = function (value) { + if (value == null) + return fail(this, INVALID_KEY_ARGUMENT); + return new this.Collection(this, function () { return rangeEqual(value); }); + }; + WhereClause.prototype.above = function (value) { + if (value == null) + return fail(this, INVALID_KEY_ARGUMENT); + return new this.Collection(this, function () { return createRange(value, undefined, true); }); + }; + WhereClause.prototype.aboveOrEqual = function (value) { + if (value == null) + return fail(this, INVALID_KEY_ARGUMENT); + return new this.Collection(this, function () { return createRange(value, undefined, false); }); + }; + WhereClause.prototype.below = function (value) { + if (value == null) + return fail(this, INVALID_KEY_ARGUMENT); + return new this.Collection(this, function () { return createRange(undefined, value, false, true); }); + }; + WhereClause.prototype.belowOrEqual = function (value) { + if (value == null) + return fail(this, INVALID_KEY_ARGUMENT); + return new this.Collection(this, function () { return createRange(undefined, value); }); + }; + WhereClause.prototype.startsWith = function (str) { + if (typeof str !== 'string') + return fail(this, STRING_EXPECTED); + return this.between(str, str + maxString, true, true); + }; + WhereClause.prototype.startsWithIgnoreCase = function (str) { + if (str === "") + return this.startsWith(str); + return addIgnoreCaseAlgorithm(this, function (x, a) { return x.indexOf(a[0]) === 0; }, [str], maxString); + }; + WhereClause.prototype.equalsIgnoreCase = function (str) { + return addIgnoreCaseAlgorithm(this, function (x, a) { return x === a[0]; }, [str], ""); + }; + WhereClause.prototype.anyOfIgnoreCase = function () { + var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); + if (set.length === 0) + return emptyCollection(this); + return addIgnoreCaseAlgorithm(this, function (x, a) { return a.indexOf(x) !== -1; }, set, ""); + }; + WhereClause.prototype.startsWithAnyOfIgnoreCase = function () { + var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); + if (set.length === 0) + return emptyCollection(this); + return addIgnoreCaseAlgorithm(this, function (x, a) { return a.some(function (n) { return x.indexOf(n) === 0; }); }, set, maxString); + }; + WhereClause.prototype.anyOf = function () { + var _this = this; + var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); + var compare = this._cmp; + try { + set.sort(compare); + } + catch (e) { + return fail(this, INVALID_KEY_ARGUMENT); + } + if (set.length === 0) + return emptyCollection(this); + var c = new this.Collection(this, function () { return createRange(set[0], set[set.length - 1]); }); + c._ondirectionchange = function (direction) { + compare = (direction === "next" ? + _this._ascending : + _this._descending); + set.sort(compare); + }; + var i = 0; + c._addAlgorithm(function (cursor, advance, resolve) { + var key = cursor.key; + while (compare(key, set[i]) > 0) { + ++i; + if (i === set.length) { + advance(resolve); + return false; + } + } + if (compare(key, set[i]) === 0) { + return true; + } + else { + advance(function () { cursor.continue(set[i]); }); + return false; + } + }); + return c; + }; + WhereClause.prototype.notEqual = function (value) { + return this.inAnyRange([[minKey, value], [value, this.db._maxKey]], { includeLowers: false, includeUppers: false }); + }; + WhereClause.prototype.noneOf = function () { + var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); + if (set.length === 0) + return new this.Collection(this); + try { + set.sort(this._ascending); + } + catch (e) { + return fail(this, INVALID_KEY_ARGUMENT); + } + var ranges = set.reduce(function (res, val) { return res ? + res.concat([[res[res.length - 1][1], val]]) : + [[minKey, val]]; }, null); + ranges.push([set[set.length - 1], this.db._maxKey]); + return this.inAnyRange(ranges, { includeLowers: false, includeUppers: false }); + }; + WhereClause.prototype.inAnyRange = function (ranges, options) { + var _this = this; + var cmp = this._cmp, ascending = this._ascending, descending = this._descending, min = this._min, max = this._max; + if (ranges.length === 0) + return emptyCollection(this); + if (!ranges.every(function (range) { + return range[0] !== undefined && + range[1] !== undefined && + ascending(range[0], range[1]) <= 0; + })) { + return fail(this, "First argument to inAnyRange() must be an Array of two-value Arrays [lower,upper] where upper must not be lower than lower", exceptions.InvalidArgument); + } + var includeLowers = !options || options.includeLowers !== false; + var includeUppers = options && options.includeUppers === true; + function addRange(ranges, newRange) { + var i = 0, l = ranges.length; + for (; i < l; ++i) { + var range = ranges[i]; + if (cmp(newRange[0], range[1]) < 0 && cmp(newRange[1], range[0]) > 0) { + range[0] = min(range[0], newRange[0]); + range[1] = max(range[1], newRange[1]); + break; + } + } + if (i === l) + ranges.push(newRange); + return ranges; + } + var sortDirection = ascending; + function rangeSorter(a, b) { return sortDirection(a[0], b[0]); } + var set; + try { + set = ranges.reduce(addRange, []); + set.sort(rangeSorter); + } + catch (ex) { + return fail(this, INVALID_KEY_ARGUMENT); + } + var rangePos = 0; + var keyIsBeyondCurrentEntry = includeUppers ? + function (key) { return ascending(key, set[rangePos][1]) > 0; } : + function (key) { return ascending(key, set[rangePos][1]) >= 0; }; + var keyIsBeforeCurrentEntry = includeLowers ? + function (key) { return descending(key, set[rangePos][0]) > 0; } : + function (key) { return descending(key, set[rangePos][0]) >= 0; }; + function keyWithinCurrentRange(key) { + return !keyIsBeyondCurrentEntry(key) && !keyIsBeforeCurrentEntry(key); + } + var checkKey = keyIsBeyondCurrentEntry; + var c = new this.Collection(this, function () { return createRange(set[0][0], set[set.length - 1][1], !includeLowers, !includeUppers); }); + c._ondirectionchange = function (direction) { + if (direction === "next") { + checkKey = keyIsBeyondCurrentEntry; + sortDirection = ascending; + } + else { + checkKey = keyIsBeforeCurrentEntry; + sortDirection = descending; + } + set.sort(rangeSorter); + }; + c._addAlgorithm(function (cursor, advance, resolve) { + var key = cursor.key; + while (checkKey(key)) { + ++rangePos; + if (rangePos === set.length) { + advance(resolve); + return false; + } + } + if (keyWithinCurrentRange(key)) { + return true; + } + else if (_this._cmp(key, set[rangePos][1]) === 0 || _this._cmp(key, set[rangePos][0]) === 0) { + return false; + } + else { + advance(function () { + if (sortDirection === ascending) + cursor.continue(set[rangePos][0]); + else + cursor.continue(set[rangePos][1]); + }); + return false; + } + }); + return c; + }; + WhereClause.prototype.startsWithAnyOf = function () { + var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); + if (!set.every(function (s) { return typeof s === 'string'; })) { + return fail(this, "startsWithAnyOf() only works with strings"); + } + if (set.length === 0) + return emptyCollection(this); + return this.inAnyRange(set.map(function (str) { return [str, str + maxString]; })); + }; + return WhereClause; +}()); + +function createWhereClauseConstructor(db) { + return makeClassConstructor(WhereClause.prototype, function WhereClause(table, index, orCollection) { + this.db = db; + this._ctx = { + table: table, + index: index === ":id" ? null : index, + or: orCollection + }; + this._cmp = this._ascending = cmp; + this._descending = function (a, b) { return cmp(b, a); }; + this._max = function (a, b) { return cmp(a, b) > 0 ? a : b; }; + this._min = function (a, b) { return cmp(a, b) < 0 ? a : b; }; + this._IDBKeyRange = db._deps.IDBKeyRange; + if (!this._IDBKeyRange) + throw new exceptions.MissingAPI(); + }); +} + +function eventRejectHandler(reject) { + return wrap(function (event) { + preventDefault(event); + reject(event.target.error); + return false; + }); +} +function preventDefault(event) { + if (event.stopPropagation) + event.stopPropagation(); + if (event.preventDefault) + event.preventDefault(); +} + +var DEXIE_STORAGE_MUTATED_EVENT_NAME = 'storagemutated'; +var STORAGE_MUTATED_DOM_EVENT_NAME = 'x-storagemutated-1'; +var globalEvents = Events(null, DEXIE_STORAGE_MUTATED_EVENT_NAME); + +var Transaction = (function () { + function Transaction() { + } + Transaction.prototype._lock = function () { + assert(!PSD.global); + ++this._reculock; + if (this._reculock === 1 && !PSD.global) + PSD.lockOwnerFor = this; + return this; + }; + Transaction.prototype._unlock = function () { + assert(!PSD.global); + if (--this._reculock === 0) { + if (!PSD.global) + PSD.lockOwnerFor = null; + while (this._blockedFuncs.length > 0 && !this._locked()) { + var fnAndPSD = this._blockedFuncs.shift(); + try { + usePSD(fnAndPSD[1], fnAndPSD[0]); + } + catch (e) { } + } + } + return this; + }; + Transaction.prototype._locked = function () { + return this._reculock && PSD.lockOwnerFor !== this; + }; + Transaction.prototype.create = function (idbtrans) { + var _this = this; + if (!this.mode) + return this; + var idbdb = this.db.idbdb; + var dbOpenError = this.db._state.dbOpenError; + assert(!this.idbtrans); + if (!idbtrans && !idbdb) { + switch (dbOpenError && dbOpenError.name) { + case "DatabaseClosedError": + throw new exceptions.DatabaseClosed(dbOpenError); + case "MissingAPIError": + throw new exceptions.MissingAPI(dbOpenError.message, dbOpenError); + default: + throw new exceptions.OpenFailed(dbOpenError); + } + } + if (!this.active) + throw new exceptions.TransactionInactive(); + assert(this._completion._state === null); + idbtrans = this.idbtrans = idbtrans || + (this.db.core + ? this.db.core.transaction(this.storeNames, this.mode, { durability: this.chromeTransactionDurability }) + : idbdb.transaction(this.storeNames, this.mode, { durability: this.chromeTransactionDurability })); + idbtrans.onerror = wrap(function (ev) { + preventDefault(ev); + _this._reject(idbtrans.error); + }); + idbtrans.onabort = wrap(function (ev) { + preventDefault(ev); + _this.active && _this._reject(new exceptions.Abort(idbtrans.error)); + _this.active = false; + _this.on("abort").fire(ev); + }); + idbtrans.oncomplete = wrap(function () { + _this.active = false; + _this._resolve(); + if ('mutatedParts' in idbtrans) { + globalEvents.storagemutated.fire(idbtrans["mutatedParts"]); + } + }); + return this; + }; + Transaction.prototype._promise = function (mode, fn, bWriteLock) { + var _this = this; + if (mode === 'readwrite' && this.mode !== 'readwrite') + return rejection(new exceptions.ReadOnly("Transaction is readonly")); + if (!this.active) + return rejection(new exceptions.TransactionInactive()); + if (this._locked()) { + return new DexiePromise(function (resolve, reject) { + _this._blockedFuncs.push([function () { + _this._promise(mode, fn, bWriteLock).then(resolve, reject); + }, PSD]); + }); + } + else if (bWriteLock) { + return newScope(function () { + var p = new DexiePromise(function (resolve, reject) { + _this._lock(); + var rv = fn(resolve, reject, _this); + if (rv && rv.then) + rv.then(resolve, reject); + }); + p.finally(function () { return _this._unlock(); }); + p._lib = true; + return p; + }); + } + else { + var p = new DexiePromise(function (resolve, reject) { + var rv = fn(resolve, reject, _this); + if (rv && rv.then) + rv.then(resolve, reject); + }); + p._lib = true; + return p; + } + }; + Transaction.prototype._root = function () { + return this.parent ? this.parent._root() : this; + }; + Transaction.prototype.waitFor = function (promiseLike) { + var root = this._root(); + var promise = DexiePromise.resolve(promiseLike); + if (root._waitingFor) { + root._waitingFor = root._waitingFor.then(function () { return promise; }); + } + else { + root._waitingFor = promise; + root._waitingQueue = []; + var store = root.idbtrans.objectStore(root.storeNames[0]); + (function spin() { + ++root._spinCount; + while (root._waitingQueue.length) + (root._waitingQueue.shift())(); + if (root._waitingFor) + store.get(-Infinity).onsuccess = spin; + }()); + } + var currentWaitPromise = root._waitingFor; + return new DexiePromise(function (resolve, reject) { + promise.then(function (res) { return root._waitingQueue.push(wrap(resolve.bind(null, res))); }, function (err) { return root._waitingQueue.push(wrap(reject.bind(null, err))); }).finally(function () { + if (root._waitingFor === currentWaitPromise) { + root._waitingFor = null; + } + }); + }); + }; + Transaction.prototype.abort = function () { + if (this.active) { + this.active = false; + if (this.idbtrans) + this.idbtrans.abort(); + this._reject(new exceptions.Abort()); + } + }; + Transaction.prototype.table = function (tableName) { + var memoizedTables = (this._memoizedTables || (this._memoizedTables = {})); + if (hasOwn(memoizedTables, tableName)) + return memoizedTables[tableName]; + var tableSchema = this.schema[tableName]; + if (!tableSchema) { + throw new exceptions.NotFound("Table " + tableName + " not part of transaction"); + } + var transactionBoundTable = new this.db.Table(tableName, tableSchema, this); + transactionBoundTable.core = this.db.core.table(tableName); + memoizedTables[tableName] = transactionBoundTable; + return transactionBoundTable; + }; + return Transaction; +}()); + +function createTransactionConstructor(db) { + return makeClassConstructor(Transaction.prototype, function Transaction(mode, storeNames, dbschema, chromeTransactionDurability, parent) { + var _this = this; + this.db = db; + this.mode = mode; + this.storeNames = storeNames; + this.schema = dbschema; + this.chromeTransactionDurability = chromeTransactionDurability; + this.idbtrans = null; + this.on = Events(this, "complete", "error", "abort"); + this.parent = parent || null; + this.active = true; + this._reculock = 0; + this._blockedFuncs = []; + this._resolve = null; + this._reject = null; + this._waitingFor = null; + this._waitingQueue = null; + this._spinCount = 0; + this._completion = new DexiePromise(function (resolve, reject) { + _this._resolve = resolve; + _this._reject = reject; + }); + this._completion.then(function () { + _this.active = false; + _this.on.complete.fire(); + }, function (e) { + var wasActive = _this.active; + _this.active = false; + _this.on.error.fire(e); + _this.parent ? + _this.parent._reject(e) : + wasActive && _this.idbtrans && _this.idbtrans.abort(); + return rejection(e); + }); + }); +} + +function createIndexSpec(name, keyPath, unique, multi, auto, compound, isPrimKey) { + return { + name: name, + keyPath: keyPath, + unique: unique, + multi: multi, + auto: auto, + compound: compound, + src: (unique && !isPrimKey ? '&' : '') + (multi ? '*' : '') + (auto ? "++" : "") + nameFromKeyPath(keyPath) + }; +} +function nameFromKeyPath(keyPath) { + return typeof keyPath === 'string' ? + keyPath : + keyPath ? ('[' + [].join.call(keyPath, '+') + ']') : ""; +} + +function createTableSchema(name, primKey, indexes) { + return { + name: name, + primKey: primKey, + indexes: indexes, + mappedClass: null, + idxByName: arrayToObject(indexes, function (index) { return [index.name, index]; }) + }; +} + +function safariMultiStoreFix(storeNames) { + return storeNames.length === 1 ? storeNames[0] : storeNames; +} +var getMaxKey = function (IdbKeyRange) { + try { + IdbKeyRange.only([[]]); + getMaxKey = function () { return [[]]; }; + return [[]]; + } + catch (e) { + getMaxKey = function () { return maxString; }; + return maxString; + } +}; + +function getKeyExtractor(keyPath) { + if (keyPath == null) { + return function () { return undefined; }; + } + else if (typeof keyPath === 'string') { + return getSinglePathKeyExtractor(keyPath); + } + else { + return function (obj) { return getByKeyPath(obj, keyPath); }; + } +} +function getSinglePathKeyExtractor(keyPath) { + var split = keyPath.split('.'); + if (split.length === 1) { + return function (obj) { return obj[keyPath]; }; + } + else { + return function (obj) { return getByKeyPath(obj, keyPath); }; + } +} + +function arrayify(arrayLike) { + return [].slice.call(arrayLike); +} +var _id_counter = 0; +function getKeyPathAlias(keyPath) { + return keyPath == null ? + ":id" : + typeof keyPath === 'string' ? + keyPath : + "[".concat(keyPath.join('+'), "]"); +} +function createDBCore(db, IdbKeyRange, tmpTrans) { + function extractSchema(db, trans) { + var tables = arrayify(db.objectStoreNames); + return { + schema: { + name: db.name, + tables: tables.map(function (table) { return trans.objectStore(table); }).map(function (store) { + var keyPath = store.keyPath, autoIncrement = store.autoIncrement; + var compound = isArray(keyPath); + var outbound = keyPath == null; + var indexByKeyPath = {}; + var result = { + name: store.name, + primaryKey: { + name: null, + isPrimaryKey: true, + outbound: outbound, + compound: compound, + keyPath: keyPath, + autoIncrement: autoIncrement, + unique: true, + extractKey: getKeyExtractor(keyPath) + }, + indexes: arrayify(store.indexNames).map(function (indexName) { return store.index(indexName); }) + .map(function (index) { + var name = index.name, unique = index.unique, multiEntry = index.multiEntry, keyPath = index.keyPath; + var compound = isArray(keyPath); + var result = { + name: name, + compound: compound, + keyPath: keyPath, + unique: unique, + multiEntry: multiEntry, + extractKey: getKeyExtractor(keyPath) + }; + indexByKeyPath[getKeyPathAlias(keyPath)] = result; + return result; + }), + getIndexByKeyPath: function (keyPath) { return indexByKeyPath[getKeyPathAlias(keyPath)]; } + }; + indexByKeyPath[":id"] = result.primaryKey; + if (keyPath != null) { + indexByKeyPath[getKeyPathAlias(keyPath)] = result.primaryKey; + } + return result; + }) + }, + hasGetAll: tables.length > 0 && ('getAll' in trans.objectStore(tables[0])) && + !(typeof navigator !== 'undefined' && /Safari/.test(navigator.userAgent) && + !/(Chrome\/|Edge\/)/.test(navigator.userAgent) && + [].concat(navigator.userAgent.match(/Safari\/(\d*)/))[1] < 604) + }; + } + function makeIDBKeyRange(range) { + if (range.type === 3 ) + return null; + if (range.type === 4 ) + throw new Error("Cannot convert never type to IDBKeyRange"); + var lower = range.lower, upper = range.upper, lowerOpen = range.lowerOpen, upperOpen = range.upperOpen; + var idbRange = lower === undefined ? + upper === undefined ? + null : + IdbKeyRange.upperBound(upper, !!upperOpen) : + upper === undefined ? + IdbKeyRange.lowerBound(lower, !!lowerOpen) : + IdbKeyRange.bound(lower, upper, !!lowerOpen, !!upperOpen); + return idbRange; + } + function createDbCoreTable(tableSchema) { + var tableName = tableSchema.name; + function mutate(_a) { + var trans = _a.trans, type = _a.type, keys = _a.keys, values = _a.values, range = _a.range; + return new Promise(function (resolve, reject) { + resolve = wrap(resolve); + var store = trans.objectStore(tableName); + var outbound = store.keyPath == null; + var isAddOrPut = type === "put" || type === "add"; + if (!isAddOrPut && type !== 'delete' && type !== 'deleteRange') + throw new Error("Invalid operation type: " + type); + var length = (keys || values || { length: 1 }).length; + if (keys && values && keys.length !== values.length) { + throw new Error("Given keys array must have same length as given values array."); + } + if (length === 0) + return resolve({ numFailures: 0, failures: {}, results: [], lastResult: undefined }); + var req; + var reqs = []; + var failures = []; + var numFailures = 0; + var errorHandler = function (event) { + ++numFailures; + preventDefault(event); + }; + if (type === 'deleteRange') { + if (range.type === 4 ) + return resolve({ numFailures: numFailures, failures: failures, results: [], lastResult: undefined }); + if (range.type === 3 ) + reqs.push(req = store.clear()); + else + reqs.push(req = store.delete(makeIDBKeyRange(range))); + } + else { + var _a = isAddOrPut ? + outbound ? + [values, keys] : + [values, null] : + [keys, null], args1 = _a[0], args2 = _a[1]; + if (isAddOrPut) { + for (var i = 0; i < length; ++i) { + reqs.push(req = (args2 && args2[i] !== undefined ? + store[type](args1[i], args2[i]) : + store[type](args1[i]))); + req.onerror = errorHandler; + } + } + else { + for (var i = 0; i < length; ++i) { + reqs.push(req = store[type](args1[i])); + req.onerror = errorHandler; + } + } + } + var done = function (event) { + var lastResult = event.target.result; + reqs.forEach(function (req, i) { return req.error != null && (failures[i] = req.error); }); + resolve({ + numFailures: numFailures, + failures: failures, + results: type === "delete" ? keys : reqs.map(function (req) { return req.result; }), + lastResult: lastResult + }); + }; + req.onerror = function (event) { + errorHandler(event); + done(event); + }; + req.onsuccess = done; + }); + } + function openCursor(_a) { + var trans = _a.trans, values = _a.values, query = _a.query, reverse = _a.reverse, unique = _a.unique; + return new Promise(function (resolve, reject) { + resolve = wrap(resolve); + var index = query.index, range = query.range; + var store = trans.objectStore(tableName); + var source = index.isPrimaryKey ? + store : + store.index(index.name); + var direction = reverse ? + unique ? + "prevunique" : + "prev" : + unique ? + "nextunique" : + "next"; + var req = values || !('openKeyCursor' in source) ? + source.openCursor(makeIDBKeyRange(range), direction) : + source.openKeyCursor(makeIDBKeyRange(range), direction); + req.onerror = eventRejectHandler(reject); + req.onsuccess = wrap(function (ev) { + var cursor = req.result; + if (!cursor) { + resolve(null); + return; + } + cursor.___id = ++_id_counter; + cursor.done = false; + var _cursorContinue = cursor.continue.bind(cursor); + var _cursorContinuePrimaryKey = cursor.continuePrimaryKey; + if (_cursorContinuePrimaryKey) + _cursorContinuePrimaryKey = _cursorContinuePrimaryKey.bind(cursor); + var _cursorAdvance = cursor.advance.bind(cursor); + var doThrowCursorIsNotStarted = function () { throw new Error("Cursor not started"); }; + var doThrowCursorIsStopped = function () { throw new Error("Cursor not stopped"); }; + cursor.trans = trans; + cursor.stop = cursor.continue = cursor.continuePrimaryKey = cursor.advance = doThrowCursorIsNotStarted; + cursor.fail = wrap(reject); + cursor.next = function () { + var _this = this; + var gotOne = 1; + return this.start(function () { return gotOne-- ? _this.continue() : _this.stop(); }).then(function () { return _this; }); + }; + cursor.start = function (callback) { + var iterationPromise = new Promise(function (resolveIteration, rejectIteration) { + resolveIteration = wrap(resolveIteration); + req.onerror = eventRejectHandler(rejectIteration); + cursor.fail = rejectIteration; + cursor.stop = function (value) { + cursor.stop = cursor.continue = cursor.continuePrimaryKey = cursor.advance = doThrowCursorIsStopped; + resolveIteration(value); + }; + }); + var guardedCallback = function () { + if (req.result) { + try { + callback(); + } + catch (err) { + cursor.fail(err); + } + } + else { + cursor.done = true; + cursor.start = function () { throw new Error("Cursor behind last entry"); }; + cursor.stop(); + } + }; + req.onsuccess = wrap(function (ev) { + req.onsuccess = guardedCallback; + guardedCallback(); + }); + cursor.continue = _cursorContinue; + cursor.continuePrimaryKey = _cursorContinuePrimaryKey; + cursor.advance = _cursorAdvance; + guardedCallback(); + return iterationPromise; + }; + resolve(cursor); + }, reject); + }); + } + function query(hasGetAll) { + return function (request) { + return new Promise(function (resolve, reject) { + resolve = wrap(resolve); + var trans = request.trans, values = request.values, limit = request.limit, query = request.query; + var nonInfinitLimit = limit === Infinity ? undefined : limit; + var index = query.index, range = query.range; + var store = trans.objectStore(tableName); + var source = index.isPrimaryKey ? store : store.index(index.name); + var idbKeyRange = makeIDBKeyRange(range); + if (limit === 0) + return resolve({ result: [] }); + if (hasGetAll) { + var req = values ? + source.getAll(idbKeyRange, nonInfinitLimit) : + source.getAllKeys(idbKeyRange, nonInfinitLimit); + req.onsuccess = function (event) { return resolve({ result: event.target.result }); }; + req.onerror = eventRejectHandler(reject); + } + else { + var count_1 = 0; + var req_1 = values || !('openKeyCursor' in source) ? + source.openCursor(idbKeyRange) : + source.openKeyCursor(idbKeyRange); + var result_1 = []; + req_1.onsuccess = function (event) { + var cursor = req_1.result; + if (!cursor) + return resolve({ result: result_1 }); + result_1.push(values ? cursor.value : cursor.primaryKey); + if (++count_1 === limit) + return resolve({ result: result_1 }); + cursor.continue(); + }; + req_1.onerror = eventRejectHandler(reject); + } + }); + }; + } + return { + name: tableName, + schema: tableSchema, + mutate: mutate, + getMany: function (_a) { + var trans = _a.trans, keys = _a.keys; + return new Promise(function (resolve, reject) { + resolve = wrap(resolve); + var store = trans.objectStore(tableName); + var length = keys.length; + var result = new Array(length); + var keyCount = 0; + var callbackCount = 0; + var req; + var successHandler = function (event) { + var req = event.target; + if ((result[req._pos] = req.result) != null) + ; + if (++callbackCount === keyCount) + resolve(result); + }; + var errorHandler = eventRejectHandler(reject); + for (var i = 0; i < length; ++i) { + var key = keys[i]; + if (key != null) { + req = store.get(keys[i]); + req._pos = i; + req.onsuccess = successHandler; + req.onerror = errorHandler; + ++keyCount; + } + } + if (keyCount === 0) + resolve(result); + }); + }, + get: function (_a) { + var trans = _a.trans, key = _a.key; + return new Promise(function (resolve, reject) { + resolve = wrap(resolve); + var store = trans.objectStore(tableName); + var req = store.get(key); + req.onsuccess = function (event) { return resolve(event.target.result); }; + req.onerror = eventRejectHandler(reject); + }); + }, + query: query(hasGetAll), + openCursor: openCursor, + count: function (_a) { + var query = _a.query, trans = _a.trans; + var index = query.index, range = query.range; + return new Promise(function (resolve, reject) { + var store = trans.objectStore(tableName); + var source = index.isPrimaryKey ? store : store.index(index.name); + var idbKeyRange = makeIDBKeyRange(range); + var req = idbKeyRange ? source.count(idbKeyRange) : source.count(); + req.onsuccess = wrap(function (ev) { return resolve(ev.target.result); }); + req.onerror = eventRejectHandler(reject); + }); + } + }; + } + var _a = extractSchema(db, tmpTrans), schema = _a.schema, hasGetAll = _a.hasGetAll; + var tables = schema.tables.map(function (tableSchema) { return createDbCoreTable(tableSchema); }); + var tableMap = {}; + tables.forEach(function (table) { return tableMap[table.name] = table; }); + return { + stack: "dbcore", + transaction: db.transaction.bind(db), + table: function (name) { + var result = tableMap[name]; + if (!result) + throw new Error("Table '".concat(name, "' not found")); + return tableMap[name]; + }, + MIN_KEY: -Infinity, + MAX_KEY: getMaxKey(IdbKeyRange), + schema: schema + }; +} + +function createMiddlewareStack(stackImpl, middlewares) { + return middlewares.reduce(function (down, _a) { + var create = _a.create; + return (__assign(__assign({}, down), create(down))); + }, stackImpl); +} +function createMiddlewareStacks(middlewares, idbdb, _a, tmpTrans) { + var IDBKeyRange = _a.IDBKeyRange; _a.indexedDB; + var dbcore = createMiddlewareStack(createDBCore(idbdb, IDBKeyRange, tmpTrans), middlewares.dbcore); + return { + dbcore: dbcore + }; +} +function generateMiddlewareStacks(db, tmpTrans) { + var idbdb = tmpTrans.db; + var stacks = createMiddlewareStacks(db._middlewares, idbdb, db._deps, tmpTrans); + db.core = stacks.dbcore; + db.tables.forEach(function (table) { + var tableName = table.name; + if (db.core.schema.tables.some(function (tbl) { return tbl.name === tableName; })) { + table.core = db.core.table(tableName); + if (db[tableName] instanceof db.Table) { + db[tableName].core = table.core; + } + } + }); +} + +function setApiOnPlace(db, objs, tableNames, dbschema) { + tableNames.forEach(function (tableName) { + var schema = dbschema[tableName]; + objs.forEach(function (obj) { + var propDesc = getPropertyDescriptor(obj, tableName); + if (!propDesc || ("value" in propDesc && propDesc.value === undefined)) { + if (obj === db.Transaction.prototype || obj instanceof db.Transaction) { + setProp(obj, tableName, { + get: function () { return this.table(tableName); }, + set: function (value) { + defineProperty(this, tableName, { value: value, writable: true, configurable: true, enumerable: true }); + } + }); + } + else { + obj[tableName] = new db.Table(tableName, schema); + } + } + }); + }); +} +function removeTablesApi(db, objs) { + objs.forEach(function (obj) { + for (var key in obj) { + if (obj[key] instanceof db.Table) + delete obj[key]; + } + }); +} +function lowerVersionFirst(a, b) { + return a._cfg.version - b._cfg.version; +} +function runUpgraders(db, oldVersion, idbUpgradeTrans, reject) { + var globalSchema = db._dbSchema; + if (idbUpgradeTrans.objectStoreNames.contains('$meta') && !globalSchema.$meta) { + globalSchema.$meta = createTableSchema("$meta", parseIndexSyntax("")[0], []); + db._storeNames.push('$meta'); + } + var trans = db._createTransaction('readwrite', db._storeNames, globalSchema); + trans.create(idbUpgradeTrans); + trans._completion.catch(reject); + var rejectTransaction = trans._reject.bind(trans); + var transless = PSD.transless || PSD; + newScope(function () { + PSD.trans = trans; + PSD.transless = transless; + if (oldVersion === 0) { + keys(globalSchema).forEach(function (tableName) { + createTable(idbUpgradeTrans, tableName, globalSchema[tableName].primKey, globalSchema[tableName].indexes); + }); + generateMiddlewareStacks(db, idbUpgradeTrans); + DexiePromise.follow(function () { return db.on.populate.fire(trans); }).catch(rejectTransaction); + } + else { + generateMiddlewareStacks(db, idbUpgradeTrans); + return getExistingVersion(db, trans, oldVersion) + .then(function (oldVersion) { return updateTablesAndIndexes(db, oldVersion, trans, idbUpgradeTrans); }) + .catch(rejectTransaction); + } + }); +} +function patchCurrentVersion(db, idbUpgradeTrans) { + createMissingTables(db._dbSchema, idbUpgradeTrans); + if (idbUpgradeTrans.db.version % 10 === 0 && !idbUpgradeTrans.objectStoreNames.contains('$meta')) { + idbUpgradeTrans.db.createObjectStore('$meta').add(Math.ceil((idbUpgradeTrans.db.version / 10) - 1), 'version'); + } + var globalSchema = buildGlobalSchema(db, db.idbdb, idbUpgradeTrans); + adjustToExistingIndexNames(db, db._dbSchema, idbUpgradeTrans); + var diff = getSchemaDiff(globalSchema, db._dbSchema); + var _loop_1 = function (tableChange) { + if (tableChange.change.length || tableChange.recreate) { + console.warn("Unable to patch indexes of table ".concat(tableChange.name, " because it has changes on the type of index or primary key.")); + return { value: void 0 }; + } + var store = idbUpgradeTrans.objectStore(tableChange.name); + tableChange.add.forEach(function (idx) { + if (debug) + console.debug("Dexie upgrade patch: Creating missing index ".concat(tableChange.name, ".").concat(idx.src)); + addIndex(store, idx); + }); + }; + for (var _i = 0, _a = diff.change; _i < _a.length; _i++) { + var tableChange = _a[_i]; + var state_1 = _loop_1(tableChange); + if (typeof state_1 === "object") + return state_1.value; + } +} +function getExistingVersion(db, trans, oldVersion) { + if (trans.storeNames.includes('$meta')) { + return trans.table('$meta').get('version').then(function (metaVersion) { + return metaVersion != null ? metaVersion : oldVersion; + }); + } + else { + return DexiePromise.resolve(oldVersion); + } +} +function updateTablesAndIndexes(db, oldVersion, trans, idbUpgradeTrans) { + var queue = []; + var versions = db._versions; + var globalSchema = db._dbSchema = buildGlobalSchema(db, db.idbdb, idbUpgradeTrans); + var versToRun = versions.filter(function (v) { return v._cfg.version >= oldVersion; }); + if (versToRun.length === 0) { + return DexiePromise.resolve(); + } + versToRun.forEach(function (version) { + queue.push(function () { + var oldSchema = globalSchema; + var newSchema = version._cfg.dbschema; + adjustToExistingIndexNames(db, oldSchema, idbUpgradeTrans); + adjustToExistingIndexNames(db, newSchema, idbUpgradeTrans); + globalSchema = db._dbSchema = newSchema; + var diff = getSchemaDiff(oldSchema, newSchema); + diff.add.forEach(function (tuple) { + createTable(idbUpgradeTrans, tuple[0], tuple[1].primKey, tuple[1].indexes); + }); + diff.change.forEach(function (change) { + if (change.recreate) { + throw new exceptions.Upgrade("Not yet support for changing primary key"); + } + else { + var store_1 = idbUpgradeTrans.objectStore(change.name); + change.add.forEach(function (idx) { return addIndex(store_1, idx); }); + change.change.forEach(function (idx) { + store_1.deleteIndex(idx.name); + addIndex(store_1, idx); + }); + change.del.forEach(function (idxName) { return store_1.deleteIndex(idxName); }); + } + }); + var contentUpgrade = version._cfg.contentUpgrade; + if (contentUpgrade && version._cfg.version > oldVersion) { + generateMiddlewareStacks(db, idbUpgradeTrans); + trans._memoizedTables = {}; + var upgradeSchema_1 = shallowClone(newSchema); + diff.del.forEach(function (table) { + upgradeSchema_1[table] = oldSchema[table]; + }); + removeTablesApi(db, [db.Transaction.prototype]); + setApiOnPlace(db, [db.Transaction.prototype], keys(upgradeSchema_1), upgradeSchema_1); + trans.schema = upgradeSchema_1; + var contentUpgradeIsAsync_1 = isAsyncFunction(contentUpgrade); + if (contentUpgradeIsAsync_1) { + incrementExpectedAwaits(); + } + var returnValue_1; + var promiseFollowed = DexiePromise.follow(function () { + returnValue_1 = contentUpgrade(trans); + if (returnValue_1) { + if (contentUpgradeIsAsync_1) { + var decrementor = decrementExpectedAwaits.bind(null, null); + returnValue_1.then(decrementor, decrementor); + } + } + }); + return (returnValue_1 && typeof returnValue_1.then === 'function' ? + DexiePromise.resolve(returnValue_1) : promiseFollowed.then(function () { return returnValue_1; })); + } + }); + queue.push(function (idbtrans) { + var newSchema = version._cfg.dbschema; + deleteRemovedTables(newSchema, idbtrans); + removeTablesApi(db, [db.Transaction.prototype]); + setApiOnPlace(db, [db.Transaction.prototype], db._storeNames, db._dbSchema); + trans.schema = db._dbSchema; + }); + queue.push(function (idbtrans) { + if (db.idbdb.objectStoreNames.contains('$meta')) { + if (Math.ceil(db.idbdb.version / 10) === version._cfg.version) { + db.idbdb.deleteObjectStore('$meta'); + delete db._dbSchema.$meta; + db._storeNames = db._storeNames.filter(function (name) { return name !== '$meta'; }); + } + else { + idbtrans.objectStore('$meta').put(version._cfg.version, 'version'); + } + } + }); + }); + function runQueue() { + return queue.length ? DexiePromise.resolve(queue.shift()(trans.idbtrans)).then(runQueue) : + DexiePromise.resolve(); + } + return runQueue().then(function () { + createMissingTables(globalSchema, idbUpgradeTrans); + }); +} +function getSchemaDiff(oldSchema, newSchema) { + var diff = { + del: [], + add: [], + change: [] + }; + var table; + for (table in oldSchema) { + if (!newSchema[table]) + diff.del.push(table); + } + for (table in newSchema) { + var oldDef = oldSchema[table], newDef = newSchema[table]; + if (!oldDef) { + diff.add.push([table, newDef]); + } + else { + var change = { + name: table, + def: newDef, + recreate: false, + del: [], + add: [], + change: [] + }; + if (( + '' + (oldDef.primKey.keyPath || '')) !== ('' + (newDef.primKey.keyPath || '')) || + (oldDef.primKey.auto !== newDef.primKey.auto)) { + change.recreate = true; + diff.change.push(change); + } + else { + var oldIndexes = oldDef.idxByName; + var newIndexes = newDef.idxByName; + var idxName = void 0; + for (idxName in oldIndexes) { + if (!newIndexes[idxName]) + change.del.push(idxName); + } + for (idxName in newIndexes) { + var oldIdx = oldIndexes[idxName], newIdx = newIndexes[idxName]; + if (!oldIdx) + change.add.push(newIdx); + else if (oldIdx.src !== newIdx.src) + change.change.push(newIdx); + } + if (change.del.length > 0 || change.add.length > 0 || change.change.length > 0) { + diff.change.push(change); + } + } + } + } + return diff; +} +function createTable(idbtrans, tableName, primKey, indexes) { + var store = idbtrans.db.createObjectStore(tableName, primKey.keyPath ? + { keyPath: primKey.keyPath, autoIncrement: primKey.auto } : + { autoIncrement: primKey.auto }); + indexes.forEach(function (idx) { return addIndex(store, idx); }); + return store; +} +function createMissingTables(newSchema, idbtrans) { + keys(newSchema).forEach(function (tableName) { + if (!idbtrans.db.objectStoreNames.contains(tableName)) { + if (debug) + console.debug('Dexie: Creating missing table', tableName); + createTable(idbtrans, tableName, newSchema[tableName].primKey, newSchema[tableName].indexes); + } + }); +} +function deleteRemovedTables(newSchema, idbtrans) { + [].slice.call(idbtrans.db.objectStoreNames).forEach(function (storeName) { + return newSchema[storeName] == null && idbtrans.db.deleteObjectStore(storeName); + }); +} +function addIndex(store, idx) { + store.createIndex(idx.name, idx.keyPath, { unique: idx.unique, multiEntry: idx.multi }); +} +function buildGlobalSchema(db, idbdb, tmpTrans) { + var globalSchema = {}; + var dbStoreNames = slice(idbdb.objectStoreNames, 0); + dbStoreNames.forEach(function (storeName) { + var store = tmpTrans.objectStore(storeName); + var keyPath = store.keyPath; + var primKey = createIndexSpec(nameFromKeyPath(keyPath), keyPath || "", true, false, !!store.autoIncrement, keyPath && typeof keyPath !== "string", true); + var indexes = []; + for (var j = 0; j < store.indexNames.length; ++j) { + var idbindex = store.index(store.indexNames[j]); + keyPath = idbindex.keyPath; + var index = createIndexSpec(idbindex.name, keyPath, !!idbindex.unique, !!idbindex.multiEntry, false, keyPath && typeof keyPath !== "string", false); + indexes.push(index); + } + globalSchema[storeName] = createTableSchema(storeName, primKey, indexes); + }); + return globalSchema; +} +function readGlobalSchema(db, idbdb, tmpTrans) { + db.verno = idbdb.version / 10; + var globalSchema = db._dbSchema = buildGlobalSchema(db, idbdb, tmpTrans); + db._storeNames = slice(idbdb.objectStoreNames, 0); + setApiOnPlace(db, [db._allTables], keys(globalSchema), globalSchema); +} +function verifyInstalledSchema(db, tmpTrans) { + var installedSchema = buildGlobalSchema(db, db.idbdb, tmpTrans); + var diff = getSchemaDiff(installedSchema, db._dbSchema); + return !(diff.add.length || diff.change.some(function (ch) { return ch.add.length || ch.change.length; })); +} +function adjustToExistingIndexNames(db, schema, idbtrans) { + var storeNames = idbtrans.db.objectStoreNames; + for (var i = 0; i < storeNames.length; ++i) { + var storeName = storeNames[i]; + var store = idbtrans.objectStore(storeName); + db._hasGetAll = 'getAll' in store; + for (var j = 0; j < store.indexNames.length; ++j) { + var indexName = store.indexNames[j]; + var keyPath = store.index(indexName).keyPath; + var dexieName = typeof keyPath === 'string' ? keyPath : "[" + slice(keyPath).join('+') + "]"; + if (schema[storeName]) { + var indexSpec = schema[storeName].idxByName[dexieName]; + if (indexSpec) { + indexSpec.name = indexName; + delete schema[storeName].idxByName[dexieName]; + schema[storeName].idxByName[indexName] = indexSpec; + } + } + } + } + if (typeof navigator !== 'undefined' && /Safari/.test(navigator.userAgent) && + !/(Chrome\/|Edge\/)/.test(navigator.userAgent) && + _global.WorkerGlobalScope && _global instanceof _global.WorkerGlobalScope && + [].concat(navigator.userAgent.match(/Safari\/(\d*)/))[1] < 604) { + db._hasGetAll = false; + } +} +function parseIndexSyntax(primKeyAndIndexes) { + return primKeyAndIndexes.split(',').map(function (index, indexNum) { + index = index.trim(); + var name = index.replace(/([&*]|\+\+)/g, ""); + var keyPath = /^\[/.test(name) ? name.match(/^\[(.*)\]$/)[1].split('+') : name; + return createIndexSpec(name, keyPath || null, /\&/.test(index), /\*/.test(index), /\+\+/.test(index), isArray(keyPath), indexNum === 0); + }); +} + +var Version = (function () { + function Version() { + } + Version.prototype._parseStoresSpec = function (stores, outSchema) { + keys(stores).forEach(function (tableName) { + if (stores[tableName] !== null) { + var indexes = parseIndexSyntax(stores[tableName]); + var primKey = indexes.shift(); + primKey.unique = true; + if (primKey.multi) + throw new exceptions.Schema("Primary key cannot be multi-valued"); + indexes.forEach(function (idx) { + if (idx.auto) + throw new exceptions.Schema("Only primary key can be marked as autoIncrement (++)"); + if (!idx.keyPath) + throw new exceptions.Schema("Index must have a name and cannot be an empty string"); + }); + outSchema[tableName] = createTableSchema(tableName, primKey, indexes); + } + }); + }; + Version.prototype.stores = function (stores) { + var db = this.db; + this._cfg.storesSource = this._cfg.storesSource ? + extend(this._cfg.storesSource, stores) : + stores; + var versions = db._versions; + var storesSpec = {}; + var dbschema = {}; + versions.forEach(function (version) { + extend(storesSpec, version._cfg.storesSource); + dbschema = (version._cfg.dbschema = {}); + version._parseStoresSpec(storesSpec, dbschema); + }); + db._dbSchema = dbschema; + removeTablesApi(db, [db._allTables, db, db.Transaction.prototype]); + setApiOnPlace(db, [db._allTables, db, db.Transaction.prototype, this._cfg.tables], keys(dbschema), dbschema); + db._storeNames = keys(dbschema); + return this; + }; + Version.prototype.upgrade = function (upgradeFunction) { + this._cfg.contentUpgrade = promisableChain(this._cfg.contentUpgrade || nop, upgradeFunction); + return this; + }; + return Version; +}()); + +function createVersionConstructor(db) { + return makeClassConstructor(Version.prototype, function Version(versionNumber) { + this.db = db; + this._cfg = { + version: versionNumber, + storesSource: null, + dbschema: {}, + tables: {}, + contentUpgrade: null + }; + }); +} + +function getDbNamesTable(indexedDB, IDBKeyRange) { + var dbNamesDB = indexedDB["_dbNamesDB"]; + if (!dbNamesDB) { + dbNamesDB = indexedDB["_dbNamesDB"] = new Dexie$1(DBNAMES_DB, { + addons: [], + indexedDB: indexedDB, + IDBKeyRange: IDBKeyRange, + }); + dbNamesDB.version(1).stores({ dbnames: "name" }); + } + return dbNamesDB.table("dbnames"); +} +function hasDatabasesNative(indexedDB) { + return indexedDB && typeof indexedDB.databases === "function"; +} +function getDatabaseNames(_a) { + var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange; + return hasDatabasesNative(indexedDB) + ? Promise.resolve(indexedDB.databases()).then(function (infos) { + return infos + .map(function (info) { return info.name; }) + .filter(function (name) { return name !== DBNAMES_DB; }); + }) + : getDbNamesTable(indexedDB, IDBKeyRange).toCollection().primaryKeys(); +} +function _onDatabaseCreated(_a, name) { + var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange; + !hasDatabasesNative(indexedDB) && + name !== DBNAMES_DB && + getDbNamesTable(indexedDB, IDBKeyRange).put({ name: name }).catch(nop); +} +function _onDatabaseDeleted(_a, name) { + var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange; + !hasDatabasesNative(indexedDB) && + name !== DBNAMES_DB && + getDbNamesTable(indexedDB, IDBKeyRange).delete(name).catch(nop); +} + +function vip(fn) { + return newScope(function () { + PSD.letThrough = true; + return fn(); + }); +} + +function idbReady() { + var isSafari = !navigator.userAgentData && + /Safari\//.test(navigator.userAgent) && + !/Chrom(e|ium)\//.test(navigator.userAgent); + if (!isSafari || !indexedDB.databases) + return Promise.resolve(); + var intervalId; + return new Promise(function (resolve) { + var tryIdb = function () { return indexedDB.databases().finally(resolve); }; + intervalId = setInterval(tryIdb, 100); + tryIdb(); + }).finally(function () { return clearInterval(intervalId); }); +} + +var _a; +function isEmptyRange(node) { + return !("from" in node); +} +var RangeSet = function (fromOrTree, to) { + if (this) { + extend(this, arguments.length ? { d: 1, from: fromOrTree, to: arguments.length > 1 ? to : fromOrTree } : { d: 0 }); + } + else { + var rv = new RangeSet(); + if (fromOrTree && ("d" in fromOrTree)) { + extend(rv, fromOrTree); + } + return rv; + } +}; +props(RangeSet.prototype, (_a = { + add: function (rangeSet) { + mergeRanges(this, rangeSet); + return this; + }, + addKey: function (key) { + addRange(this, key, key); + return this; + }, + addKeys: function (keys) { + var _this = this; + keys.forEach(function (key) { return addRange(_this, key, key); }); + return this; + }, + hasKey: function (key) { + var node = getRangeSetIterator(this).next(key).value; + return node && cmp(node.from, key) <= 0 && cmp(node.to, key) >= 0; + } + }, + _a[iteratorSymbol] = function () { + return getRangeSetIterator(this); + }, + _a)); +function addRange(target, from, to) { + var diff = cmp(from, to); + if (isNaN(diff)) + return; + if (diff > 0) + throw RangeError(); + if (isEmptyRange(target)) + return extend(target, { from: from, to: to, d: 1 }); + var left = target.l; + var right = target.r; + if (cmp(to, target.from) < 0) { + left + ? addRange(left, from, to) + : (target.l = { from: from, to: to, d: 1, l: null, r: null }); + return rebalance(target); + } + if (cmp(from, target.to) > 0) { + right + ? addRange(right, from, to) + : (target.r = { from: from, to: to, d: 1, l: null, r: null }); + return rebalance(target); + } + if (cmp(from, target.from) < 0) { + target.from = from; + target.l = null; + target.d = right ? right.d + 1 : 1; + } + if (cmp(to, target.to) > 0) { + target.to = to; + target.r = null; + target.d = target.l ? target.l.d + 1 : 1; + } + var rightWasCutOff = !target.r; + if (left && !target.l) { + mergeRanges(target, left); + } + if (right && rightWasCutOff) { + mergeRanges(target, right); + } +} +function mergeRanges(target, newSet) { + function _addRangeSet(target, _a) { + var from = _a.from, to = _a.to, l = _a.l, r = _a.r; + addRange(target, from, to); + if (l) + _addRangeSet(target, l); + if (r) + _addRangeSet(target, r); + } + if (!isEmptyRange(newSet)) + _addRangeSet(target, newSet); +} +function rangesOverlap(rangeSet1, rangeSet2) { + var i1 = getRangeSetIterator(rangeSet2); + var nextResult1 = i1.next(); + if (nextResult1.done) + return false; + var a = nextResult1.value; + var i2 = getRangeSetIterator(rangeSet1); + var nextResult2 = i2.next(a.from); + var b = nextResult2.value; + while (!nextResult1.done && !nextResult2.done) { + if (cmp(b.from, a.to) <= 0 && cmp(b.to, a.from) >= 0) + return true; + cmp(a.from, b.from) < 0 + ? (a = (nextResult1 = i1.next(b.from)).value) + : (b = (nextResult2 = i2.next(a.from)).value); + } + return false; +} +function getRangeSetIterator(node) { + var state = isEmptyRange(node) ? null : { s: 0, n: node }; + return { + next: function (key) { + var keyProvided = arguments.length > 0; + while (state) { + switch (state.s) { + case 0: + state.s = 1; + if (keyProvided) { + while (state.n.l && cmp(key, state.n.from) < 0) + state = { up: state, n: state.n.l, s: 1 }; + } + else { + while (state.n.l) + state = { up: state, n: state.n.l, s: 1 }; + } + case 1: + state.s = 2; + if (!keyProvided || cmp(key, state.n.to) <= 0) + return { value: state.n, done: false }; + case 2: + if (state.n.r) { + state.s = 3; + state = { up: state, n: state.n.r, s: 0 }; + continue; + } + case 3: + state = state.up; + } + } + return { done: true }; + }, + }; +} +function rebalance(target) { + var _a, _b; + var diff = (((_a = target.r) === null || _a === void 0 ? void 0 : _a.d) || 0) - (((_b = target.l) === null || _b === void 0 ? void 0 : _b.d) || 0); + var r = diff > 1 ? "r" : diff < -1 ? "l" : ""; + if (r) { + var l = r === "r" ? "l" : "r"; + var rootClone = __assign({}, target); + var oldRootRight = target[r]; + target.from = oldRootRight.from; + target.to = oldRootRight.to; + target[r] = oldRootRight[r]; + rootClone[r] = oldRootRight[l]; + target[l] = rootClone; + rootClone.d = computeDepth(rootClone); + } + target.d = computeDepth(target); +} +function computeDepth(_a) { + var r = _a.r, l = _a.l; + return (r ? (l ? Math.max(r.d, l.d) : r.d) : l ? l.d : 0) + 1; +} + +function extendObservabilitySet(target, newSet) { + keys(newSet).forEach(function (part) { + if (target[part]) + mergeRanges(target[part], newSet[part]); + else + target[part] = cloneSimpleObjectTree(newSet[part]); + }); + return target; +} + +function obsSetsOverlap(os1, os2) { + return os1.all || os2.all || Object.keys(os1).some(function (key) { return os2[key] && rangesOverlap(os2[key], os1[key]); }); +} + +var cache = {}; + +var unsignaledParts = {}; +var isTaskEnqueued = false; +function signalSubscribersLazily(part, optimistic) { + extendObservabilitySet(unsignaledParts, part); + if (!isTaskEnqueued) { + isTaskEnqueued = true; + setTimeout(function () { + isTaskEnqueued = false; + var parts = unsignaledParts; + unsignaledParts = {}; + signalSubscribersNow(parts, false); + }, 0); + } +} +function signalSubscribersNow(updatedParts, deleteAffectedCacheEntries) { + if (deleteAffectedCacheEntries === void 0) { deleteAffectedCacheEntries = false; } + var queriesToSignal = new Set(); + if (updatedParts.all) { + for (var _i = 0, _a = Object.values(cache); _i < _a.length; _i++) { + var tblCache = _a[_i]; + collectTableSubscribers(tblCache, updatedParts, queriesToSignal, deleteAffectedCacheEntries); + } + } + else { + for (var key in updatedParts) { + var parts = /^idb\:\/\/(.*)\/(.*)\//.exec(key); + if (parts) { + var dbName = parts[1], tableName = parts[2]; + var tblCache = cache["idb://".concat(dbName, "/").concat(tableName)]; + if (tblCache) + collectTableSubscribers(tblCache, updatedParts, queriesToSignal, deleteAffectedCacheEntries); + } + } + } + queriesToSignal.forEach(function (requery) { return requery(); }); +} +function collectTableSubscribers(tblCache, updatedParts, outQueriesToSignal, deleteAffectedCacheEntries) { + var updatedEntryLists = []; + for (var _i = 0, _a = Object.entries(tblCache.queries.query); _i < _a.length; _i++) { + var _b = _a[_i], indexName = _b[0], entries = _b[1]; + var filteredEntries = []; + for (var _c = 0, entries_1 = entries; _c < entries_1.length; _c++) { + var entry = entries_1[_c]; + if (obsSetsOverlap(updatedParts, entry.obsSet)) { + entry.subscribers.forEach(function (requery) { return outQueriesToSignal.add(requery); }); + } + else if (deleteAffectedCacheEntries) { + filteredEntries.push(entry); + } + } + if (deleteAffectedCacheEntries) + updatedEntryLists.push([indexName, filteredEntries]); + } + if (deleteAffectedCacheEntries) { + for (var _d = 0, updatedEntryLists_1 = updatedEntryLists; _d < updatedEntryLists_1.length; _d++) { + var _e = updatedEntryLists_1[_d], indexName = _e[0], filteredEntries = _e[1]; + tblCache.queries.query[indexName] = filteredEntries; + } + } +} + +function dexieOpen(db) { + var state = db._state; + var indexedDB = db._deps.indexedDB; + if (state.isBeingOpened || db.idbdb) + return state.dbReadyPromise.then(function () { return state.dbOpenError ? + rejection(state.dbOpenError) : + db; }); + state.isBeingOpened = true; + state.dbOpenError = null; + state.openComplete = false; + var openCanceller = state.openCanceller; + var nativeVerToOpen = Math.round(db.verno * 10); + var schemaPatchMode = false; + function throwIfCancelled() { + if (state.openCanceller !== openCanceller) + throw new exceptions.DatabaseClosed('db.open() was cancelled'); + } + var resolveDbReady = state.dbReadyResolve, + upgradeTransaction = null, wasCreated = false; + var tryOpenDB = function () { return new DexiePromise(function (resolve, reject) { + throwIfCancelled(); + if (!indexedDB) + throw new exceptions.MissingAPI(); + var dbName = db.name; + var req = state.autoSchema || !nativeVerToOpen ? + indexedDB.open(dbName) : + indexedDB.open(dbName, nativeVerToOpen); + if (!req) + throw new exceptions.MissingAPI(); + req.onerror = eventRejectHandler(reject); + req.onblocked = wrap(db._fireOnBlocked); + req.onupgradeneeded = wrap(function (e) { + upgradeTransaction = req.transaction; + if (state.autoSchema && !db._options.allowEmptyDB) { + req.onerror = preventDefault; + upgradeTransaction.abort(); + req.result.close(); + var delreq = indexedDB.deleteDatabase(dbName); + delreq.onsuccess = delreq.onerror = wrap(function () { + reject(new exceptions.NoSuchDatabase("Database ".concat(dbName, " doesnt exist"))); + }); + } + else { + upgradeTransaction.onerror = eventRejectHandler(reject); + var oldVer = e.oldVersion > Math.pow(2, 62) ? 0 : e.oldVersion; + wasCreated = oldVer < 1; + db.idbdb = req.result; + if (schemaPatchMode) { + patchCurrentVersion(db, upgradeTransaction); + } + runUpgraders(db, oldVer / 10, upgradeTransaction, reject); + } + }, reject); + req.onsuccess = wrap(function () { + upgradeTransaction = null; + var idbdb = db.idbdb = req.result; + var objectStoreNames = slice(idbdb.objectStoreNames); + if (objectStoreNames.length > 0) + try { + var tmpTrans = idbdb.transaction(safariMultiStoreFix(objectStoreNames), 'readonly'); + if (state.autoSchema) + readGlobalSchema(db, idbdb, tmpTrans); + else { + adjustToExistingIndexNames(db, db._dbSchema, tmpTrans); + if (!verifyInstalledSchema(db, tmpTrans) && !schemaPatchMode) { + console.warn("Dexie SchemaDiff: Schema was extended without increasing the number passed to db.version(). Dexie will add missing parts and increment native version number to workaround this."); + idbdb.close(); + nativeVerToOpen = idbdb.version + 1; + schemaPatchMode = true; + return resolve(tryOpenDB()); + } + } + generateMiddlewareStacks(db, tmpTrans); + } + catch (e) { + } + connections.push(db); + idbdb.onversionchange = wrap(function (ev) { + state.vcFired = true; + db.on("versionchange").fire(ev); + }); + idbdb.onclose = wrap(function (ev) { + db.on("close").fire(ev); + }); + if (wasCreated) + _onDatabaseCreated(db._deps, dbName); + resolve(); + }, reject); + }).catch(function (err) { + switch (err === null || err === void 0 ? void 0 : err.name) { + case "UnknownError": + if (state.PR1398_maxLoop > 0) { + state.PR1398_maxLoop--; + console.warn('Dexie: Workaround for Chrome UnknownError on open()'); + return tryOpenDB(); + } + break; + case "VersionError": + if (nativeVerToOpen > 0) { + nativeVerToOpen = 0; + return tryOpenDB(); + } + break; + } + return DexiePromise.reject(err); + }); }; + return DexiePromise.race([ + openCanceller, + (typeof navigator === 'undefined' ? DexiePromise.resolve() : idbReady()).then(tryOpenDB) + ]).then(function () { + throwIfCancelled(); + state.onReadyBeingFired = []; + return DexiePromise.resolve(vip(function () { return db.on.ready.fire(db.vip); })).then(function fireRemainders() { + if (state.onReadyBeingFired.length > 0) { + var remainders_1 = state.onReadyBeingFired.reduce(promisableChain, nop); + state.onReadyBeingFired = []; + return DexiePromise.resolve(vip(function () { return remainders_1(db.vip); })).then(fireRemainders); + } + }); + }).finally(function () { + if (state.openCanceller === openCanceller) { + state.onReadyBeingFired = null; + state.isBeingOpened = false; + } + }).catch(function (err) { + state.dbOpenError = err; + try { + upgradeTransaction && upgradeTransaction.abort(); + } + catch (_a) { } + if (openCanceller === state.openCanceller) { + db._close(); + } + return rejection(err); + }).finally(function () { + state.openComplete = true; + resolveDbReady(); + }).then(function () { + if (wasCreated) { + var everything_1 = {}; + db.tables.forEach(function (table) { + table.schema.indexes.forEach(function (idx) { + if (idx.name) + everything_1["idb://".concat(db.name, "/").concat(table.name, "/").concat(idx.name)] = new RangeSet(-Infinity, [[[]]]); + }); + everything_1["idb://".concat(db.name, "/").concat(table.name, "/")] = everything_1["idb://".concat(db.name, "/").concat(table.name, "/:dels")] = new RangeSet(-Infinity, [[[]]]); + }); + globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME).fire(everything_1); + signalSubscribersNow(everything_1, true); + } + return db; + }); +} + +function awaitIterator(iterator) { + var callNext = function (result) { return iterator.next(result); }, doThrow = function (error) { return iterator.throw(error); }, onSuccess = step(callNext), onError = step(doThrow); + function step(getNext) { + return function (val) { + var next = getNext(val), value = next.value; + return next.done ? value : + (!value || typeof value.then !== 'function' ? + isArray(value) ? Promise.all(value).then(onSuccess, onError) : onSuccess(value) : + value.then(onSuccess, onError)); + }; + } + return step(callNext)(); +} + +function extractTransactionArgs(mode, _tableArgs_, scopeFunc) { + var i = arguments.length; + if (i < 2) + throw new exceptions.InvalidArgument("Too few arguments"); + var args = new Array(i - 1); + while (--i) + args[i - 1] = arguments[i]; + scopeFunc = args.pop(); + var tables = flatten(args); + return [mode, tables, scopeFunc]; +} +function enterTransactionScope(db, mode, storeNames, parentTransaction, scopeFunc) { + return DexiePromise.resolve().then(function () { + var transless = PSD.transless || PSD; + var trans = db._createTransaction(mode, storeNames, db._dbSchema, parentTransaction); + trans.explicit = true; + var zoneProps = { + trans: trans, + transless: transless + }; + if (parentTransaction) { + trans.idbtrans = parentTransaction.idbtrans; + } + else { + try { + trans.create(); + trans.idbtrans._explicit = true; + db._state.PR1398_maxLoop = 3; + } + catch (ex) { + if (ex.name === errnames.InvalidState && db.isOpen() && --db._state.PR1398_maxLoop > 0) { + console.warn('Dexie: Need to reopen db'); + db.close({ disableAutoOpen: false }); + return db.open().then(function () { return enterTransactionScope(db, mode, storeNames, null, scopeFunc); }); + } + return rejection(ex); + } + } + var scopeFuncIsAsync = isAsyncFunction(scopeFunc); + if (scopeFuncIsAsync) { + incrementExpectedAwaits(); + } + var returnValue; + var promiseFollowed = DexiePromise.follow(function () { + returnValue = scopeFunc.call(trans, trans); + if (returnValue) { + if (scopeFuncIsAsync) { + var decrementor = decrementExpectedAwaits.bind(null, null); + returnValue.then(decrementor, decrementor); + } + else if (typeof returnValue.next === 'function' && typeof returnValue.throw === 'function') { + returnValue = awaitIterator(returnValue); + } + } + }, zoneProps); + return (returnValue && typeof returnValue.then === 'function' ? + DexiePromise.resolve(returnValue).then(function (x) { return trans.active ? + x + : rejection(new exceptions.PrematureCommit("Transaction committed too early. See http://bit.ly/2kdckMn")); }) + : promiseFollowed.then(function () { return returnValue; })).then(function (x) { + if (parentTransaction) + trans._resolve(); + return trans._completion.then(function () { return x; }); + }).catch(function (e) { + trans._reject(e); + return rejection(e); + }); + }); +} + +function pad(a, value, count) { + var result = isArray(a) ? a.slice() : [a]; + for (var i = 0; i < count; ++i) + result.push(value); + return result; +} +function createVirtualIndexMiddleware(down) { + return __assign(__assign({}, down), { table: function (tableName) { + var table = down.table(tableName); + var schema = table.schema; + var indexLookup = {}; + var allVirtualIndexes = []; + function addVirtualIndexes(keyPath, keyTail, lowLevelIndex) { + var keyPathAlias = getKeyPathAlias(keyPath); + var indexList = (indexLookup[keyPathAlias] = indexLookup[keyPathAlias] || []); + var keyLength = keyPath == null ? 0 : typeof keyPath === 'string' ? 1 : keyPath.length; + var isVirtual = keyTail > 0; + var virtualIndex = __assign(__assign({}, lowLevelIndex), { name: isVirtual + ? "".concat(keyPathAlias, "(virtual-from:").concat(lowLevelIndex.name, ")") + : lowLevelIndex.name, lowLevelIndex: lowLevelIndex, isVirtual: isVirtual, keyTail: keyTail, keyLength: keyLength, extractKey: getKeyExtractor(keyPath), unique: !isVirtual && lowLevelIndex.unique }); + indexList.push(virtualIndex); + if (!virtualIndex.isPrimaryKey) { + allVirtualIndexes.push(virtualIndex); + } + if (keyLength > 1) { + var virtualKeyPath = keyLength === 2 ? + keyPath[0] : + keyPath.slice(0, keyLength - 1); + addVirtualIndexes(virtualKeyPath, keyTail + 1, lowLevelIndex); + } + indexList.sort(function (a, b) { return a.keyTail - b.keyTail; }); + return virtualIndex; + } + var primaryKey = addVirtualIndexes(schema.primaryKey.keyPath, 0, schema.primaryKey); + indexLookup[":id"] = [primaryKey]; + for (var _i = 0, _a = schema.indexes; _i < _a.length; _i++) { + var index = _a[_i]; + addVirtualIndexes(index.keyPath, 0, index); + } + function findBestIndex(keyPath) { + var result = indexLookup[getKeyPathAlias(keyPath)]; + return result && result[0]; + } + function translateRange(range, keyTail) { + return { + type: range.type === 1 ? + 2 : + range.type, + lower: pad(range.lower, range.lowerOpen ? down.MAX_KEY : down.MIN_KEY, keyTail), + lowerOpen: true, + upper: pad(range.upper, range.upperOpen ? down.MIN_KEY : down.MAX_KEY, keyTail), + upperOpen: true + }; + } + function translateRequest(req) { + var index = req.query.index; + return index.isVirtual ? __assign(__assign({}, req), { query: { + index: index.lowLevelIndex, + range: translateRange(req.query.range, index.keyTail) + } }) : req; + } + var result = __assign(__assign({}, table), { schema: __assign(__assign({}, schema), { primaryKey: primaryKey, indexes: allVirtualIndexes, getIndexByKeyPath: findBestIndex }), count: function (req) { + return table.count(translateRequest(req)); + }, query: function (req) { + return table.query(translateRequest(req)); + }, openCursor: function (req) { + var _a = req.query.index, keyTail = _a.keyTail, isVirtual = _a.isVirtual, keyLength = _a.keyLength; + if (!isVirtual) + return table.openCursor(req); + function createVirtualCursor(cursor) { + function _continue(key) { + key != null ? + cursor.continue(pad(key, req.reverse ? down.MAX_KEY : down.MIN_KEY, keyTail)) : + req.unique ? + cursor.continue(cursor.key.slice(0, keyLength) + .concat(req.reverse + ? down.MIN_KEY + : down.MAX_KEY, keyTail)) : + cursor.continue(); + } + var virtualCursor = Object.create(cursor, { + continue: { value: _continue }, + continuePrimaryKey: { + value: function (key, primaryKey) { + cursor.continuePrimaryKey(pad(key, down.MAX_KEY, keyTail), primaryKey); + } + }, + primaryKey: { + get: function () { + return cursor.primaryKey; + } + }, + key: { + get: function () { + var key = cursor.key; + return keyLength === 1 ? + key[0] : + key.slice(0, keyLength); + } + }, + value: { + get: function () { + return cursor.value; + } + } + }); + return virtualCursor; + } + return table.openCursor(translateRequest(req)) + .then(function (cursor) { return cursor && createVirtualCursor(cursor); }); + } }); + return result; + } }); +} +var virtualIndexMiddleware = { + stack: "dbcore", + name: "VirtualIndexMiddleware", + level: 1, + create: createVirtualIndexMiddleware +}; + +function getObjectDiff(a, b, rv, prfx) { + rv = rv || {}; + prfx = prfx || ''; + keys(a).forEach(function (prop) { + if (!hasOwn(b, prop)) { + rv[prfx + prop] = undefined; + } + else { + var ap = a[prop], bp = b[prop]; + if (typeof ap === 'object' && typeof bp === 'object' && ap && bp) { + var apTypeName = toStringTag(ap); + var bpTypeName = toStringTag(bp); + if (apTypeName !== bpTypeName) { + rv[prfx + prop] = b[prop]; + } + else if (apTypeName === 'Object') { + getObjectDiff(ap, bp, rv, prfx + prop + '.'); + } + else if (ap !== bp) { + rv[prfx + prop] = b[prop]; + } + } + else if (ap !== bp) + rv[prfx + prop] = b[prop]; + } + }); + keys(b).forEach(function (prop) { + if (!hasOwn(a, prop)) { + rv[prfx + prop] = b[prop]; + } + }); + return rv; +} + +function getEffectiveKeys(primaryKey, req) { + if (req.type === 'delete') + return req.keys; + return req.keys || req.values.map(primaryKey.extractKey); +} + +var hooksMiddleware = { + stack: "dbcore", + name: "HooksMiddleware", + level: 2, + create: function (downCore) { return (__assign(__assign({}, downCore), { table: function (tableName) { + var downTable = downCore.table(tableName); + var primaryKey = downTable.schema.primaryKey; + var tableMiddleware = __assign(__assign({}, downTable), { mutate: function (req) { + var dxTrans = PSD.trans; + var _a = dxTrans.table(tableName).hook, deleting = _a.deleting, creating = _a.creating, updating = _a.updating; + switch (req.type) { + case 'add': + if (creating.fire === nop) + break; + return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true); + case 'put': + if (creating.fire === nop && updating.fire === nop) + break; + return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true); + case 'delete': + if (deleting.fire === nop) + break; + return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true); + case 'deleteRange': + if (deleting.fire === nop) + break; + return dxTrans._promise('readwrite', function () { return deleteRange(req); }, true); + } + return downTable.mutate(req); + function addPutOrDelete(req) { + var dxTrans = PSD.trans; + var keys = req.keys || getEffectiveKeys(primaryKey, req); + if (!keys) + throw new Error("Keys missing"); + req = req.type === 'add' || req.type === 'put' ? __assign(__assign({}, req), { keys: keys }) : __assign({}, req); + if (req.type !== 'delete') + req.values = __spreadArray([], req.values, true); + if (req.keys) + req.keys = __spreadArray([], req.keys, true); + return getExistingValues(downTable, req, keys).then(function (existingValues) { + var contexts = keys.map(function (key, i) { + var existingValue = existingValues[i]; + var ctx = { onerror: null, onsuccess: null }; + if (req.type === 'delete') { + deleting.fire.call(ctx, key, existingValue, dxTrans); + } + else if (req.type === 'add' || existingValue === undefined) { + var generatedPrimaryKey = creating.fire.call(ctx, key, req.values[i], dxTrans); + if (key == null && generatedPrimaryKey != null) { + key = generatedPrimaryKey; + req.keys[i] = key; + if (!primaryKey.outbound) { + setByKeyPath(req.values[i], primaryKey.keyPath, key); + } + } + } + else { + var objectDiff = getObjectDiff(existingValue, req.values[i]); + var additionalChanges_1 = updating.fire.call(ctx, objectDiff, key, existingValue, dxTrans); + if (additionalChanges_1) { + var requestedValue_1 = req.values[i]; + Object.keys(additionalChanges_1).forEach(function (keyPath) { + if (hasOwn(requestedValue_1, keyPath)) { + requestedValue_1[keyPath] = additionalChanges_1[keyPath]; + } + else { + setByKeyPath(requestedValue_1, keyPath, additionalChanges_1[keyPath]); + } + }); + } + } + return ctx; + }); + return downTable.mutate(req).then(function (_a) { + var failures = _a.failures, results = _a.results, numFailures = _a.numFailures, lastResult = _a.lastResult; + for (var i = 0; i < keys.length; ++i) { + var primKey = results ? results[i] : keys[i]; + var ctx = contexts[i]; + if (primKey == null) { + ctx.onerror && ctx.onerror(failures[i]); + } + else { + ctx.onsuccess && ctx.onsuccess(req.type === 'put' && existingValues[i] ? + req.values[i] : + primKey + ); + } + } + return { failures: failures, results: results, numFailures: numFailures, lastResult: lastResult }; + }).catch(function (error) { + contexts.forEach(function (ctx) { return ctx.onerror && ctx.onerror(error); }); + return Promise.reject(error); + }); + }); + } + function deleteRange(req) { + return deleteNextChunk(req.trans, req.range, 10000); + } + function deleteNextChunk(trans, range, limit) { + return downTable.query({ trans: trans, values: false, query: { index: primaryKey, range: range }, limit: limit }) + .then(function (_a) { + var result = _a.result; + return addPutOrDelete({ type: 'delete', keys: result, trans: trans }).then(function (res) { + if (res.numFailures > 0) + return Promise.reject(res.failures[0]); + if (result.length < limit) { + return { failures: [], numFailures: 0, lastResult: undefined }; + } + else { + return deleteNextChunk(trans, __assign(__assign({}, range), { lower: result[result.length - 1], lowerOpen: true }), limit); + } + }); + }); + } + } }); + return tableMiddleware; + } })); } +}; +function getExistingValues(table, req, effectiveKeys) { + return req.type === "add" + ? Promise.resolve([]) + : table.getMany({ trans: req.trans, keys: effectiveKeys, cache: "immutable" }); +} + +function getFromTransactionCache(keys, cache, clone) { + try { + if (!cache) + return null; + if (cache.keys.length < keys.length) + return null; + var result = []; + for (var i = 0, j = 0; i < cache.keys.length && j < keys.length; ++i) { + if (cmp(cache.keys[i], keys[j]) !== 0) + continue; + result.push(clone ? deepClone(cache.values[i]) : cache.values[i]); + ++j; + } + return result.length === keys.length ? result : null; + } + catch (_a) { + return null; + } +} +var cacheExistingValuesMiddleware = { + stack: "dbcore", + level: -1, + create: function (core) { + return { + table: function (tableName) { + var table = core.table(tableName); + return __assign(__assign({}, table), { getMany: function (req) { + if (!req.cache) { + return table.getMany(req); + } + var cachedResult = getFromTransactionCache(req.keys, req.trans["_cache"], req.cache === "clone"); + if (cachedResult) { + return DexiePromise.resolve(cachedResult); + } + return table.getMany(req).then(function (res) { + req.trans["_cache"] = { + keys: req.keys, + values: req.cache === "clone" ? deepClone(res) : res, + }; + return res; + }); + }, mutate: function (req) { + if (req.type !== "add") + req.trans["_cache"] = null; + return table.mutate(req); + } }); + }, + }; + }, +}; + +function isCachableContext(ctx, table) { + return (ctx.trans.mode === 'readonly' && + !!ctx.subscr && + !ctx.trans.explicit && + ctx.trans.db._options.cache !== 'disabled' && + !table.schema.primaryKey.outbound); +} + +function isCachableRequest(type, req) { + switch (type) { + case 'query': + return req.values && !req.unique; + case 'get': + return false; + case 'getMany': + return false; + case 'count': + return false; + case 'openCursor': + return false; + } +} + +var observabilityMiddleware = { + stack: "dbcore", + level: 0, + name: "Observability", + create: function (core) { + var dbName = core.schema.name; + var FULL_RANGE = new RangeSet(core.MIN_KEY, core.MAX_KEY); + return __assign(__assign({}, core), { transaction: function (stores, mode, options) { + if (PSD.subscr && mode !== 'readonly') { + throw new exceptions.ReadOnly("Readwrite transaction in liveQuery context. Querier source: ".concat(PSD.querier)); + } + return core.transaction(stores, mode, options); + }, table: function (tableName) { + var table = core.table(tableName); + var schema = table.schema; + var primaryKey = schema.primaryKey, indexes = schema.indexes; + var extractKey = primaryKey.extractKey, outbound = primaryKey.outbound; + var indexesWithAutoIncPK = primaryKey.autoIncrement && indexes.filter(function (index) { return index.compound && index.keyPath.includes(primaryKey.keyPath); }); + var tableClone = __assign(__assign({}, table), { mutate: function (req) { + var _a, _b; + var trans = req.trans; + var mutatedParts = req.mutatedParts || (req.mutatedParts = {}); + var getRangeSet = function (indexName) { + var part = "idb://".concat(dbName, "/").concat(tableName, "/").concat(indexName); + return (mutatedParts[part] || + (mutatedParts[part] = new RangeSet())); + }; + var pkRangeSet = getRangeSet(""); + var delsRangeSet = getRangeSet(":dels"); + var type = req.type; + var _c = req.type === "deleteRange" + ? [req.range] + : req.type === "delete" + ? [req.keys] + : req.values.length < 50 + ? [getEffectiveKeys(primaryKey, req).filter(function (id) { return id; }), req.values] + : [], keys = _c[0], newObjs = _c[1]; + var oldCache = req.trans["_cache"]; + if (isArray(keys)) { + pkRangeSet.addKeys(keys); + var oldObjs = type === 'delete' || keys.length === newObjs.length ? getFromTransactionCache(keys, oldCache) : null; + if (!oldObjs) { + delsRangeSet.addKeys(keys); + } + if (oldObjs || newObjs) { + trackAffectedIndexes(getRangeSet, schema, oldObjs, newObjs); + } + } + else if (keys) { + var range = { + from: (_a = keys.lower) !== null && _a !== void 0 ? _a : core.MIN_KEY, + to: (_b = keys.upper) !== null && _b !== void 0 ? _b : core.MAX_KEY + }; + delsRangeSet.add(range); + pkRangeSet.add(range); + } + else { + pkRangeSet.add(FULL_RANGE); + delsRangeSet.add(FULL_RANGE); + schema.indexes.forEach(function (idx) { return getRangeSet(idx.name).add(FULL_RANGE); }); + } + return table.mutate(req).then(function (res) { + if (keys && (req.type === 'add' || req.type === 'put')) { + pkRangeSet.addKeys(res.results); + if (indexesWithAutoIncPK) { + indexesWithAutoIncPK.forEach(function (idx) { + var idxVals = req.values.map(function (v) { return idx.extractKey(v); }); + var pkPos = idx.keyPath.findIndex(function (prop) { return prop === primaryKey.keyPath; }); + for (var i = 0, len = res.results.length; i < len; ++i) { + idxVals[i][pkPos] = res.results[i]; + } + getRangeSet(idx.name).addKeys(idxVals); + }); + } + } + trans.mutatedParts = extendObservabilitySet(trans.mutatedParts || {}, mutatedParts); + return res; + }); + } }); + var getRange = function (_a) { + var _b, _c; + var _d = _a.query, index = _d.index, range = _d.range; + return [ + index, + new RangeSet((_b = range.lower) !== null && _b !== void 0 ? _b : core.MIN_KEY, (_c = range.upper) !== null && _c !== void 0 ? _c : core.MAX_KEY), + ]; + }; + var readSubscribers = { + get: function (req) { return [primaryKey, new RangeSet(req.key)]; }, + getMany: function (req) { return [primaryKey, new RangeSet().addKeys(req.keys)]; }, + count: getRange, + query: getRange, + openCursor: getRange, + }; + keys(readSubscribers).forEach(function (method) { + tableClone[method] = function (req) { + var subscr = PSD.subscr; + var isLiveQuery = !!subscr; + var cachable = isCachableContext(PSD, table) && isCachableRequest(method, req); + var obsSet = cachable + ? req.obsSet = {} + : subscr; + if (isLiveQuery) { + var getRangeSet = function (indexName) { + var part = "idb://".concat(dbName, "/").concat(tableName, "/").concat(indexName); + return (obsSet[part] || + (obsSet[part] = new RangeSet())); + }; + var pkRangeSet_1 = getRangeSet(""); + var delsRangeSet_1 = getRangeSet(":dels"); + var _a = readSubscribers[method](req), queriedIndex = _a[0], queriedRanges = _a[1]; + if (method === 'query' && queriedIndex.isPrimaryKey && !req.values) { + delsRangeSet_1.add(queriedRanges); + } + else { + getRangeSet(queriedIndex.name || "").add(queriedRanges); + } + if (!queriedIndex.isPrimaryKey) { + if (method === "count") { + delsRangeSet_1.add(FULL_RANGE); + } + else { + var keysPromise_1 = method === "query" && + outbound && + req.values && + table.query(__assign(__assign({}, req), { values: false })); + return table[method].apply(this, arguments).then(function (res) { + if (method === "query") { + if (outbound && req.values) { + return keysPromise_1.then(function (_a) { + var resultingKeys = _a.result; + pkRangeSet_1.addKeys(resultingKeys); + return res; + }); + } + var pKeys = req.values + ? res.result.map(extractKey) + : res.result; + if (req.values) { + pkRangeSet_1.addKeys(pKeys); + } + else { + delsRangeSet_1.addKeys(pKeys); + } + } + else if (method === "openCursor") { + var cursor_1 = res; + var wantValues_1 = req.values; + return (cursor_1 && + Object.create(cursor_1, { + key: { + get: function () { + delsRangeSet_1.addKey(cursor_1.primaryKey); + return cursor_1.key; + }, + }, + primaryKey: { + get: function () { + var pkey = cursor_1.primaryKey; + delsRangeSet_1.addKey(pkey); + return pkey; + }, + }, + value: { + get: function () { + wantValues_1 && pkRangeSet_1.addKey(cursor_1.primaryKey); + return cursor_1.value; + }, + }, + })); + } + return res; + }); + } + } + } + return table[method].apply(this, arguments); + }; + }); + return tableClone; + } }); + }, +}; +function trackAffectedIndexes(getRangeSet, schema, oldObjs, newObjs) { + function addAffectedIndex(ix) { + var rangeSet = getRangeSet(ix.name || ""); + function extractKey(obj) { + return obj != null ? ix.extractKey(obj) : null; + } + var addKeyOrKeys = function (key) { return ix.multiEntry && isArray(key) + ? key.forEach(function (key) { return rangeSet.addKey(key); }) + : rangeSet.addKey(key); }; + (oldObjs || newObjs).forEach(function (_, i) { + var oldKey = oldObjs && extractKey(oldObjs[i]); + var newKey = newObjs && extractKey(newObjs[i]); + if (cmp(oldKey, newKey) !== 0) { + if (oldKey != null) + addKeyOrKeys(oldKey); + if (newKey != null) + addKeyOrKeys(newKey); + } + }); + } + schema.indexes.forEach(addAffectedIndex); +} + +function adjustOptimisticFromFailures(tblCache, req, res) { + if (res.numFailures === 0) + return req; + if (req.type === 'deleteRange') { + return null; + } + var numBulkOps = req.keys + ? req.keys.length + : 'values' in req && req.values + ? req.values.length + : 1; + if (res.numFailures === numBulkOps) { + return null; + } + var clone = __assign({}, req); + if (isArray(clone.keys)) { + clone.keys = clone.keys.filter(function (_, i) { return !(i in res.failures); }); + } + if ('values' in clone && isArray(clone.values)) { + clone.values = clone.values.filter(function (_, i) { return !(i in res.failures); }); + } + return clone; +} + +function isAboveLower(key, range) { + return range.lower === undefined + ? true + : range.lowerOpen + ? cmp(key, range.lower) > 0 + : cmp(key, range.lower) >= 0; +} +function isBelowUpper(key, range) { + return range.upper === undefined + ? true + : range.upperOpen + ? cmp(key, range.upper) < 0 + : cmp(key, range.upper) <= 0; +} +function isWithinRange(key, range) { + return isAboveLower(key, range) && isBelowUpper(key, range); +} + +function applyOptimisticOps(result, req, ops, table, cacheEntry, immutable) { + if (!ops || ops.length === 0) + return result; + var index = req.query.index; + var multiEntry = index.multiEntry; + var queryRange = req.query.range; + var primaryKey = table.schema.primaryKey; + var extractPrimKey = primaryKey.extractKey; + var extractIndex = index.extractKey; + var extractLowLevelIndex = (index.lowLevelIndex || index).extractKey; + var finalResult = ops.reduce(function (result, op) { + var modifedResult = result; + var includedValues = []; + if (op.type === 'add' || op.type === 'put') { + var includedPKs = new RangeSet(); + for (var i = op.values.length - 1; i >= 0; --i) { + var value = op.values[i]; + var pk = extractPrimKey(value); + if (includedPKs.hasKey(pk)) + continue; + var key = extractIndex(value); + if (multiEntry && isArray(key) + ? key.some(function (k) { return isWithinRange(k, queryRange); }) + : isWithinRange(key, queryRange)) { + includedPKs.addKey(pk); + includedValues.push(value); + } + } + } + switch (op.type) { + case 'add': { + var existingKeys_1 = new RangeSet().addKeys(req.values ? result.map(function (v) { return extractPrimKey(v); }) : result); + modifedResult = result.concat(req.values + ? includedValues.filter(function (v) { + var key = extractPrimKey(v); + if (existingKeys_1.hasKey(key)) + return false; + existingKeys_1.addKey(key); + return true; + }) + : includedValues + .map(function (v) { return extractPrimKey(v); }) + .filter(function (k) { + if (existingKeys_1.hasKey(k)) + return false; + existingKeys_1.addKey(k); + return true; + })); + break; + } + case 'put': { + var keySet_1 = new RangeSet().addKeys(op.values.map(function (v) { return extractPrimKey(v); })); + modifedResult = result + .filter( + function (item) { return !keySet_1.hasKey(req.values ? extractPrimKey(item) : item); }) + .concat( + req.values + ? includedValues + : includedValues.map(function (v) { return extractPrimKey(v); })); + break; + } + case 'delete': + var keysToDelete_1 = new RangeSet().addKeys(op.keys); + modifedResult = result.filter(function (item) { + return !keysToDelete_1.hasKey(req.values ? extractPrimKey(item) : item); + }); + break; + case 'deleteRange': + var range_1 = op.range; + modifedResult = result.filter(function (item) { return !isWithinRange(extractPrimKey(item), range_1); }); + break; + } + return modifedResult; + }, result); + if (finalResult === result) + return result; + finalResult.sort(function (a, b) { + return cmp(extractLowLevelIndex(a), extractLowLevelIndex(b)) || + cmp(extractPrimKey(a), extractPrimKey(b)); + }); + if (req.limit && req.limit < Infinity) { + if (finalResult.length > req.limit) { + finalResult.length = req.limit; + } + else if (result.length === req.limit && finalResult.length < req.limit) { + cacheEntry.dirty = true; + } + } + return immutable ? Object.freeze(finalResult) : finalResult; +} + +function areRangesEqual(r1, r2) { + return (cmp(r1.lower, r2.lower) === 0 && + cmp(r1.upper, r2.upper) === 0 && + !!r1.lowerOpen === !!r2.lowerOpen && + !!r1.upperOpen === !!r2.upperOpen); +} + +function compareLowers(lower1, lower2, lowerOpen1, lowerOpen2) { + if (lower1 === undefined) + return lower2 !== undefined ? -1 : 0; + if (lower2 === undefined) + return 1; + var c = cmp(lower1, lower2); + if (c === 0) { + if (lowerOpen1 && lowerOpen2) + return 0; + if (lowerOpen1) + return 1; + if (lowerOpen2) + return -1; + } + return c; +} +function compareUppers(upper1, upper2, upperOpen1, upperOpen2) { + if (upper1 === undefined) + return upper2 !== undefined ? 1 : 0; + if (upper2 === undefined) + return -1; + var c = cmp(upper1, upper2); + if (c === 0) { + if (upperOpen1 && upperOpen2) + return 0; + if (upperOpen1) + return -1; + if (upperOpen2) + return 1; + } + return c; +} +function isSuperRange(r1, r2) { + return (compareLowers(r1.lower, r2.lower, r1.lowerOpen, r2.lowerOpen) <= 0 && + compareUppers(r1.upper, r2.upper, r1.upperOpen, r2.upperOpen) >= 0); +} + +function findCompatibleQuery(dbName, tableName, type, req) { + var tblCache = cache["idb://".concat(dbName, "/").concat(tableName)]; + if (!tblCache) + return []; + var queries = tblCache.queries[type]; + if (!queries) + return [null, false, tblCache, null]; + var indexName = req.query ? req.query.index.name : null; + var entries = queries[indexName || '']; + if (!entries) + return [null, false, tblCache, null]; + switch (type) { + case 'query': + var equalEntry = entries.find(function (entry) { + return entry.req.limit === req.limit && + entry.req.values === req.values && + areRangesEqual(entry.req.query.range, req.query.range); + }); + if (equalEntry) + return [ + equalEntry, + true, + tblCache, + entries, + ]; + var superEntry = entries.find(function (entry) { + var limit = 'limit' in entry.req ? entry.req.limit : Infinity; + return (limit >= req.limit && + (req.values ? entry.req.values : true) && + isSuperRange(entry.req.query.range, req.query.range)); + }); + return [superEntry, false, tblCache, entries]; + case 'count': + var countQuery = entries.find(function (entry) { + return areRangesEqual(entry.req.query.range, req.query.range); + }); + return [countQuery, !!countQuery, tblCache, entries]; + } +} + +function subscribeToCacheEntry(cacheEntry, container, requery, signal) { + cacheEntry.subscribers.add(requery); + signal.addEventListener("abort", function () { + cacheEntry.subscribers.delete(requery); + if (cacheEntry.subscribers.size === 0) { + enqueForDeletion(cacheEntry, container); + } + }); +} +function enqueForDeletion(cacheEntry, container) { + setTimeout(function () { + if (cacheEntry.subscribers.size === 0) { + delArrayItem(container, cacheEntry); + } + }, 3000); +} + +var cacheMiddleware = { + stack: 'dbcore', + level: 0, + name: 'Cache', + create: function (core) { + var dbName = core.schema.name; + var coreMW = __assign(__assign({}, core), { transaction: function (stores, mode, options) { + var idbtrans = core.transaction(stores, mode, options); + if (mode === 'readwrite') { + var ac_1 = new AbortController(); + var signal = ac_1.signal; + var endTransaction = function (wasCommitted) { return function () { + ac_1.abort(); + if (mode === 'readwrite') { + var affectedSubscribers_1 = new Set(); + for (var _i = 0, stores_1 = stores; _i < stores_1.length; _i++) { + var storeName = stores_1[_i]; + var tblCache = cache["idb://".concat(dbName, "/").concat(storeName)]; + if (tblCache) { + var table = core.table(storeName); + var ops = tblCache.optimisticOps.filter(function (op) { return op.trans === idbtrans; }); + if (idbtrans._explicit && wasCommitted && idbtrans.mutatedParts) { + for (var _a = 0, _b = Object.values(tblCache.queries.query); _a < _b.length; _a++) { + var entries = _b[_a]; + for (var _c = 0, _d = entries.slice(); _c < _d.length; _c++) { + var entry = _d[_c]; + if (obsSetsOverlap(entry.obsSet, idbtrans.mutatedParts)) { + delArrayItem(entries, entry); + entry.subscribers.forEach(function (requery) { return affectedSubscribers_1.add(requery); }); + } + } + } + } + else if (ops.length > 0) { + tblCache.optimisticOps = tblCache.optimisticOps.filter(function (op) { return op.trans !== idbtrans; }); + for (var _e = 0, _f = Object.values(tblCache.queries.query); _e < _f.length; _e++) { + var entries = _f[_e]; + for (var _g = 0, _h = entries.slice(); _g < _h.length; _g++) { + var entry = _h[_g]; + if (entry.res != null && + idbtrans.mutatedParts +) { + if (wasCommitted && !entry.dirty) { + var freezeResults = Object.isFrozen(entry.res); + var modRes = applyOptimisticOps(entry.res, entry.req, ops, table, entry, freezeResults); + if (entry.dirty) { + delArrayItem(entries, entry); + entry.subscribers.forEach(function (requery) { return affectedSubscribers_1.add(requery); }); + } + else if (modRes !== entry.res) { + entry.res = modRes; + entry.promise = DexiePromise.resolve({ result: modRes }); + } + } + else { + if (entry.dirty) { + delArrayItem(entries, entry); + } + entry.subscribers.forEach(function (requery) { return affectedSubscribers_1.add(requery); }); + } + } + } + } + } + } + } + affectedSubscribers_1.forEach(function (requery) { return requery(); }); + } + }; }; + idbtrans.addEventListener('abort', endTransaction(false), { + signal: signal, + }); + idbtrans.addEventListener('error', endTransaction(false), { + signal: signal, + }); + idbtrans.addEventListener('complete', endTransaction(true), { + signal: signal, + }); + } + return idbtrans; + }, table: function (tableName) { + var downTable = core.table(tableName); + var primKey = downTable.schema.primaryKey; + var tableMW = __assign(__assign({}, downTable), { mutate: function (req) { + var trans = PSD.trans; + if (primKey.outbound || + trans.db._options.cache === 'disabled' || + trans.explicit || + trans.idbtrans.mode !== 'readwrite' + ) { + return downTable.mutate(req); + } + var tblCache = cache["idb://".concat(dbName, "/").concat(tableName)]; + if (!tblCache) + return downTable.mutate(req); + var promise = downTable.mutate(req); + if ((req.type === 'add' || req.type === 'put') && (req.values.length >= 50 || getEffectiveKeys(primKey, req).some(function (key) { return key == null; }))) { + promise.then(function (res) { + var reqWithResolvedKeys = __assign(__assign({}, req), { values: req.values.map(function (value, i) { + var _a; + if (res.failures[i]) + return value; + var valueWithKey = ((_a = primKey.keyPath) === null || _a === void 0 ? void 0 : _a.includes('.')) + ? deepClone(value) + : __assign({}, value); + setByKeyPath(valueWithKey, primKey.keyPath, res.results[i]); + return valueWithKey; + }) }); + var adjustedReq = adjustOptimisticFromFailures(tblCache, reqWithResolvedKeys, res); + tblCache.optimisticOps.push(adjustedReq); + queueMicrotask(function () { return req.mutatedParts && signalSubscribersLazily(req.mutatedParts); }); + }); + } + else { + tblCache.optimisticOps.push(req); + req.mutatedParts && signalSubscribersLazily(req.mutatedParts); + promise.then(function (res) { + if (res.numFailures > 0) { + delArrayItem(tblCache.optimisticOps, req); + var adjustedReq = adjustOptimisticFromFailures(tblCache, req, res); + if (adjustedReq) { + tblCache.optimisticOps.push(adjustedReq); + } + req.mutatedParts && signalSubscribersLazily(req.mutatedParts); + } + }); + promise.catch(function () { + delArrayItem(tblCache.optimisticOps, req); + req.mutatedParts && signalSubscribersLazily(req.mutatedParts); + }); + } + return promise; + }, query: function (req) { + var _a; + if (!isCachableContext(PSD, downTable) || !isCachableRequest("query", req)) + return downTable.query(req); + var freezeResults = ((_a = PSD.trans) === null || _a === void 0 ? void 0 : _a.db._options.cache) === 'immutable'; + var _b = PSD, requery = _b.requery, signal = _b.signal; + var _c = findCompatibleQuery(dbName, tableName, 'query', req), cacheEntry = _c[0], exactMatch = _c[1], tblCache = _c[2], container = _c[3]; + if (cacheEntry && exactMatch) { + cacheEntry.obsSet = req.obsSet; + } + else { + var promise = downTable.query(req).then(function (res) { + var result = res.result; + if (cacheEntry) + cacheEntry.res = result; + if (freezeResults) { + for (var i = 0, l = result.length; i < l; ++i) { + Object.freeze(result[i]); + } + Object.freeze(result); + } + else { + res.result = deepClone(result); + } + return res; + }).catch(function (error) { + if (container && cacheEntry) + delArrayItem(container, cacheEntry); + return Promise.reject(error); + }); + cacheEntry = { + obsSet: req.obsSet, + promise: promise, + subscribers: new Set(), + type: 'query', + req: req, + dirty: false, + }; + if (container) { + container.push(cacheEntry); + } + else { + container = [cacheEntry]; + if (!tblCache) { + tblCache = cache["idb://".concat(dbName, "/").concat(tableName)] = { + queries: { + query: {}, + count: {}, + }, + objs: new Map(), + optimisticOps: [], + unsignaledParts: {} + }; + } + tblCache.queries.query[req.query.index.name || ''] = container; + } + } + subscribeToCacheEntry(cacheEntry, container, requery, signal); + return cacheEntry.promise.then(function (res) { + return { + result: applyOptimisticOps(res.result, req, tblCache === null || tblCache === void 0 ? void 0 : tblCache.optimisticOps, downTable, cacheEntry, freezeResults), + }; + }); + } }); + return tableMW; + } }); + return coreMW; + }, +}; + +function vipify(target, vipDb) { + return new Proxy(target, { + get: function (target, prop, receiver) { + if (prop === 'db') + return vipDb; + return Reflect.get(target, prop, receiver); + } + }); +} + +var Dexie$1 = (function () { + function Dexie(name, options) { + var _this = this; + this._middlewares = {}; + this.verno = 0; + var deps = Dexie.dependencies; + this._options = options = __assign({ + addons: Dexie.addons, autoOpen: true, + indexedDB: deps.indexedDB, IDBKeyRange: deps.IDBKeyRange, cache: 'cloned' }, options); + this._deps = { + indexedDB: options.indexedDB, + IDBKeyRange: options.IDBKeyRange + }; + var addons = options.addons; + this._dbSchema = {}; + this._versions = []; + this._storeNames = []; + this._allTables = {}; + this.idbdb = null; + this._novip = this; + var state = { + dbOpenError: null, + isBeingOpened: false, + onReadyBeingFired: null, + openComplete: false, + dbReadyResolve: nop, + dbReadyPromise: null, + cancelOpen: nop, + openCanceller: null, + autoSchema: true, + PR1398_maxLoop: 3, + autoOpen: options.autoOpen, + }; + state.dbReadyPromise = new DexiePromise(function (resolve) { + state.dbReadyResolve = resolve; + }); + state.openCanceller = new DexiePromise(function (_, reject) { + state.cancelOpen = reject; + }); + this._state = state; + this.name = name; + this.on = Events(this, "populate", "blocked", "versionchange", "close", { ready: [promisableChain, nop] }); + this.on.ready.subscribe = override(this.on.ready.subscribe, function (subscribe) { + return function (subscriber, bSticky) { + Dexie.vip(function () { + var state = _this._state; + if (state.openComplete) { + if (!state.dbOpenError) + DexiePromise.resolve().then(subscriber); + if (bSticky) + subscribe(subscriber); + } + else if (state.onReadyBeingFired) { + state.onReadyBeingFired.push(subscriber); + if (bSticky) + subscribe(subscriber); + } + else { + subscribe(subscriber); + var db_1 = _this; + if (!bSticky) + subscribe(function unsubscribe() { + db_1.on.ready.unsubscribe(subscriber); + db_1.on.ready.unsubscribe(unsubscribe); + }); + } + }); + }; + }); + this.Collection = createCollectionConstructor(this); + this.Table = createTableConstructor(this); + this.Transaction = createTransactionConstructor(this); + this.Version = createVersionConstructor(this); + this.WhereClause = createWhereClauseConstructor(this); + this.on("versionchange", function (ev) { + if (ev.newVersion > 0) + console.warn("Another connection wants to upgrade database '".concat(_this.name, "'. Closing db now to resume the upgrade.")); + else + console.warn("Another connection wants to delete database '".concat(_this.name, "'. Closing db now to resume the delete request.")); + _this.close({ disableAutoOpen: false }); + }); + this.on("blocked", function (ev) { + if (!ev.newVersion || ev.newVersion < ev.oldVersion) + console.warn("Dexie.delete('".concat(_this.name, "') was blocked")); + else + console.warn("Upgrade '".concat(_this.name, "' blocked by other connection holding version ").concat(ev.oldVersion / 10)); + }); + this._maxKey = getMaxKey(options.IDBKeyRange); + this._createTransaction = function (mode, storeNames, dbschema, parentTransaction) { return new _this.Transaction(mode, storeNames, dbschema, _this._options.chromeTransactionDurability, parentTransaction); }; + this._fireOnBlocked = function (ev) { + _this.on("blocked").fire(ev); + connections + .filter(function (c) { return c.name === _this.name && c !== _this && !c._state.vcFired; }) + .map(function (c) { return c.on("versionchange").fire(ev); }); + }; + this.use(cacheExistingValuesMiddleware); + this.use(cacheMiddleware); + this.use(observabilityMiddleware); + this.use(virtualIndexMiddleware); + this.use(hooksMiddleware); + var vipDB = new Proxy(this, { + get: function (_, prop, receiver) { + if (prop === '_vip') + return true; + if (prop === 'table') + return function (tableName) { return vipify(_this.table(tableName), vipDB); }; + var rv = Reflect.get(_, prop, receiver); + if (rv instanceof Table) + return vipify(rv, vipDB); + if (prop === 'tables') + return rv.map(function (t) { return vipify(t, vipDB); }); + if (prop === '_createTransaction') + return function () { + var tx = rv.apply(this, arguments); + return vipify(tx, vipDB); + }; + return rv; + } + }); + this.vip = vipDB; + addons.forEach(function (addon) { return addon(_this); }); + } + Dexie.prototype.version = function (versionNumber) { + if (isNaN(versionNumber) || versionNumber < 0.1) + throw new exceptions.Type("Given version is not a positive number"); + versionNumber = Math.round(versionNumber * 10) / 10; + if (this.idbdb || this._state.isBeingOpened) + throw new exceptions.Schema("Cannot add version when database is open"); + this.verno = Math.max(this.verno, versionNumber); + var versions = this._versions; + var versionInstance = versions.filter(function (v) { return v._cfg.version === versionNumber; })[0]; + if (versionInstance) + return versionInstance; + versionInstance = new this.Version(versionNumber); + versions.push(versionInstance); + versions.sort(lowerVersionFirst); + versionInstance.stores({}); + this._state.autoSchema = false; + return versionInstance; + }; + Dexie.prototype._whenReady = function (fn) { + var _this = this; + return (this.idbdb && (this._state.openComplete || PSD.letThrough || this._vip)) ? fn() : new DexiePromise(function (resolve, reject) { + if (_this._state.openComplete) { + return reject(new exceptions.DatabaseClosed(_this._state.dbOpenError)); + } + if (!_this._state.isBeingOpened) { + if (!_this._state.autoOpen) { + reject(new exceptions.DatabaseClosed()); + return; + } + _this.open().catch(nop); + } + _this._state.dbReadyPromise.then(resolve, reject); + }).then(fn); + }; + Dexie.prototype.use = function (_a) { + var stack = _a.stack, create = _a.create, level = _a.level, name = _a.name; + if (name) + this.unuse({ stack: stack, name: name }); + var middlewares = this._middlewares[stack] || (this._middlewares[stack] = []); + middlewares.push({ stack: stack, create: create, level: level == null ? 10 : level, name: name }); + middlewares.sort(function (a, b) { return a.level - b.level; }); + return this; + }; + Dexie.prototype.unuse = function (_a) { + var stack = _a.stack, name = _a.name, create = _a.create; + if (stack && this._middlewares[stack]) { + this._middlewares[stack] = this._middlewares[stack].filter(function (mw) { + return create ? mw.create !== create : + name ? mw.name !== name : + false; + }); + } + return this; + }; + Dexie.prototype.open = function () { + var _this = this; + return usePSD(globalPSD, + function () { return dexieOpen(_this); }); + }; + Dexie.prototype._close = function () { + var state = this._state; + var idx = connections.indexOf(this); + if (idx >= 0) + connections.splice(idx, 1); + if (this.idbdb) { + try { + this.idbdb.close(); + } + catch (e) { } + this.idbdb = null; + } + if (!state.isBeingOpened) { + state.dbReadyPromise = new DexiePromise(function (resolve) { + state.dbReadyResolve = resolve; + }); + state.openCanceller = new DexiePromise(function (_, reject) { + state.cancelOpen = reject; + }); + } + }; + Dexie.prototype.close = function (_a) { + var _b = _a === void 0 ? { disableAutoOpen: true } : _a, disableAutoOpen = _b.disableAutoOpen; + var state = this._state; + if (disableAutoOpen) { + if (state.isBeingOpened) { + state.cancelOpen(new exceptions.DatabaseClosed()); + } + this._close(); + state.autoOpen = false; + state.dbOpenError = new exceptions.DatabaseClosed(); + } + else { + this._close(); + state.autoOpen = this._options.autoOpen || + state.isBeingOpened; + state.openComplete = false; + state.dbOpenError = null; + } + }; + Dexie.prototype.delete = function (closeOptions) { + var _this = this; + if (closeOptions === void 0) { closeOptions = { disableAutoOpen: true }; } + var hasInvalidArguments = arguments.length > 0 && typeof arguments[0] !== 'object'; + var state = this._state; + return new DexiePromise(function (resolve, reject) { + var doDelete = function () { + _this.close(closeOptions); + var req = _this._deps.indexedDB.deleteDatabase(_this.name); + req.onsuccess = wrap(function () { + _onDatabaseDeleted(_this._deps, _this.name); + resolve(); + }); + req.onerror = eventRejectHandler(reject); + req.onblocked = _this._fireOnBlocked; + }; + if (hasInvalidArguments) + throw new exceptions.InvalidArgument("Invalid closeOptions argument to db.delete()"); + if (state.isBeingOpened) { + state.dbReadyPromise.then(doDelete); + } + else { + doDelete(); + } + }); + }; + Dexie.prototype.backendDB = function () { + return this.idbdb; + }; + Dexie.prototype.isOpen = function () { + return this.idbdb !== null; + }; + Dexie.prototype.hasBeenClosed = function () { + var dbOpenError = this._state.dbOpenError; + return dbOpenError && (dbOpenError.name === 'DatabaseClosed'); + }; + Dexie.prototype.hasFailed = function () { + return this._state.dbOpenError !== null; + }; + Dexie.prototype.dynamicallyOpened = function () { + return this._state.autoSchema; + }; + Object.defineProperty(Dexie.prototype, "tables", { + get: function () { + var _this = this; + return keys(this._allTables).map(function (name) { return _this._allTables[name]; }); + }, + enumerable: false, + configurable: true + }); + Dexie.prototype.transaction = function () { + var args = extractTransactionArgs.apply(this, arguments); + return this._transaction.apply(this, args); + }; + Dexie.prototype._transaction = function (mode, tables, scopeFunc) { + var _this = this; + var parentTransaction = PSD.trans; + if (!parentTransaction || parentTransaction.db !== this || mode.indexOf('!') !== -1) + parentTransaction = null; + var onlyIfCompatible = mode.indexOf('?') !== -1; + mode = mode.replace('!', '').replace('?', ''); + var idbMode, storeNames; + try { + storeNames = tables.map(function (table) { + var storeName = table instanceof _this.Table ? table.name : table; + if (typeof storeName !== 'string') + throw new TypeError("Invalid table argument to Dexie.transaction(). Only Table or String are allowed"); + return storeName; + }); + if (mode == "r" || mode === READONLY) + idbMode = READONLY; + else if (mode == "rw" || mode == READWRITE) + idbMode = READWRITE; + else + throw new exceptions.InvalidArgument("Invalid transaction mode: " + mode); + if (parentTransaction) { + if (parentTransaction.mode === READONLY && idbMode === READWRITE) { + if (onlyIfCompatible) { + parentTransaction = null; + } + else + throw new exceptions.SubTransaction("Cannot enter a sub-transaction with READWRITE mode when parent transaction is READONLY"); + } + if (parentTransaction) { + storeNames.forEach(function (storeName) { + if (parentTransaction && parentTransaction.storeNames.indexOf(storeName) === -1) { + if (onlyIfCompatible) { + parentTransaction = null; + } + else + throw new exceptions.SubTransaction("Table " + storeName + + " not included in parent transaction."); + } + }); + } + if (onlyIfCompatible && parentTransaction && !parentTransaction.active) { + parentTransaction = null; + } + } + } + catch (e) { + return parentTransaction ? + parentTransaction._promise(null, function (_, reject) { reject(e); }) : + rejection(e); + } + var enterTransaction = enterTransactionScope.bind(null, this, idbMode, storeNames, parentTransaction, scopeFunc); + return (parentTransaction ? + parentTransaction._promise(idbMode, enterTransaction, "lock") : + PSD.trans ? + usePSD(PSD.transless, function () { return _this._whenReady(enterTransaction); }) : + this._whenReady(enterTransaction)); + }; + Dexie.prototype.table = function (tableName) { + if (!hasOwn(this._allTables, tableName)) { + throw new exceptions.InvalidTable("Table ".concat(tableName, " does not exist")); + } + return this._allTables[tableName]; + }; + return Dexie; +}()); + +var symbolObservable = typeof Symbol !== "undefined" && "observable" in Symbol + ? Symbol.observable + : "@@observable"; +var Observable = (function () { + function Observable(subscribe) { + this._subscribe = subscribe; + } + Observable.prototype.subscribe = function (x, error, complete) { + return this._subscribe(!x || typeof x === "function" ? { next: x, error: error, complete: complete } : x); + }; + Observable.prototype[symbolObservable] = function () { + return this; + }; + return Observable; +}()); + +var domDeps; +try { + domDeps = { + indexedDB: _global.indexedDB || _global.mozIndexedDB || _global.webkitIndexedDB || _global.msIndexedDB, + IDBKeyRange: _global.IDBKeyRange || _global.webkitIDBKeyRange + }; +} +catch (e) { + domDeps = { indexedDB: null, IDBKeyRange: null }; +} + +function liveQuery(querier) { + var hasValue = false; + var currentValue; + var observable = new Observable(function (observer) { + var scopeFuncIsAsync = isAsyncFunction(querier); + function execute(ctx) { + var wasRootExec = beginMicroTickScope(); + try { + if (scopeFuncIsAsync) { + incrementExpectedAwaits(); + } + var rv = newScope(querier, ctx); + if (scopeFuncIsAsync) { + rv = rv.finally(decrementExpectedAwaits); + } + return rv; + } + finally { + wasRootExec && endMicroTickScope(); + } + } + var closed = false; + var abortController; + var accumMuts = {}; + var currentObs = {}; + var subscription = { + get closed() { + return closed; + }, + unsubscribe: function () { + if (closed) + return; + closed = true; + if (abortController) + abortController.abort(); + if (startedListening) + globalEvents.storagemutated.unsubscribe(mutationListener); + }, + }; + observer.start && observer.start(subscription); + var startedListening = false; + var doQuery = function () { return execInGlobalContext(_doQuery); }; + function shouldNotify() { + return obsSetsOverlap(currentObs, accumMuts); + } + var mutationListener = function (parts) { + extendObservabilitySet(accumMuts, parts); + if (shouldNotify()) { + doQuery(); + } + }; + var _doQuery = function () { + if (closed || + !domDeps.indexedDB) + { + return; + } + accumMuts = {}; + var subscr = {}; + if (abortController) + abortController.abort(); + abortController = new AbortController(); + var ctx = { + subscr: subscr, + signal: abortController.signal, + requery: doQuery, + querier: querier, + trans: null + }; + var ret = execute(ctx); + Promise.resolve(ret).then(function (result) { + hasValue = true; + currentValue = result; + if (closed || ctx.signal.aborted) { + return; + } + accumMuts = {}; + currentObs = subscr; + if (!objectIsEmpty(currentObs) && !startedListening) { + globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, mutationListener); + startedListening = true; + } + execInGlobalContext(function () { return !closed && observer.next && observer.next(result); }); + }, function (err) { + hasValue = false; + if (!['DatabaseClosedError', 'AbortError'].includes(err === null || err === void 0 ? void 0 : err.name)) { + if (!closed) + execInGlobalContext(function () { + if (closed) + return; + observer.error && observer.error(err); + }); + } + }); + }; + setTimeout(doQuery, 0); + return subscription; + }); + observable.hasValue = function () { return hasValue; }; + observable.getValue = function () { return currentValue; }; + return observable; +} + +var Dexie = Dexie$1; +props(Dexie, __assign(__assign({}, fullNameExceptions), { + delete: function (databaseName) { + var db = new Dexie(databaseName, { addons: [] }); + return db.delete(); + }, + exists: function (name) { + return new Dexie(name, { addons: [] }).open().then(function (db) { + db.close(); + return true; + }).catch('NoSuchDatabaseError', function () { return false; }); + }, + getDatabaseNames: function (cb) { + try { + return getDatabaseNames(Dexie.dependencies).then(cb); + } + catch (_a) { + return rejection(new exceptions.MissingAPI()); + } + }, + defineClass: function () { + function Class(content) { + extend(this, content); + } + return Class; + }, ignoreTransaction: function (scopeFunc) { + return PSD.trans ? + usePSD(PSD.transless, scopeFunc) : + scopeFunc(); + }, vip: vip, async: function (generatorFn) { + return function () { + try { + var rv = awaitIterator(generatorFn.apply(this, arguments)); + if (!rv || typeof rv.then !== 'function') + return DexiePromise.resolve(rv); + return rv; + } + catch (e) { + return rejection(e); + } + }; + }, spawn: function (generatorFn, args, thiz) { + try { + var rv = awaitIterator(generatorFn.apply(thiz, args || [])); + if (!rv || typeof rv.then !== 'function') + return DexiePromise.resolve(rv); + return rv; + } + catch (e) { + return rejection(e); + } + }, + currentTransaction: { + get: function () { return PSD.trans || null; } + }, waitFor: function (promiseOrFunction, optionalTimeout) { + var promise = DexiePromise.resolve(typeof promiseOrFunction === 'function' ? + Dexie.ignoreTransaction(promiseOrFunction) : + promiseOrFunction) + .timeout(optionalTimeout || 60000); + return PSD.trans ? + PSD.trans.waitFor(promise) : + promise; + }, + Promise: DexiePromise, + debug: { + get: function () { return debug; }, + set: function (value) { + setDebug(value); + } + }, + derive: derive, extend: extend, props: props, override: override, + Events: Events, on: globalEvents, liveQuery: liveQuery, extendObservabilitySet: extendObservabilitySet, + getByKeyPath: getByKeyPath, setByKeyPath: setByKeyPath, delByKeyPath: delByKeyPath, shallowClone: shallowClone, deepClone: deepClone, getObjectDiff: getObjectDiff, cmp: cmp, asap: asap$1, + minKey: minKey, + addons: [], + connections: connections, + errnames: errnames, + dependencies: domDeps, cache: cache, + semVer: DEXIE_VERSION, version: DEXIE_VERSION.split('.') + .map(function (n) { return parseInt(n); }) + .reduce(function (p, c, i) { return p + (c / Math.pow(10, i * 2)); }) })); +Dexie.maxKey = getMaxKey(Dexie.dependencies.IDBKeyRange); + +if (typeof dispatchEvent !== 'undefined' && typeof addEventListener !== 'undefined') { + globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, function (updatedParts) { + if (!propagatingLocally) { + var event_1; + event_1 = new CustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME, { + detail: updatedParts + }); + propagatingLocally = true; + dispatchEvent(event_1); + propagatingLocally = false; + } + }); + addEventListener(STORAGE_MUTATED_DOM_EVENT_NAME, function (_a) { + var detail = _a.detail; + if (!propagatingLocally) { + propagateLocally(detail); + } + }); +} +function propagateLocally(updateParts) { + var wasMe = propagatingLocally; + try { + propagatingLocally = true; + globalEvents.storagemutated.fire(updateParts); + signalSubscribersNow(updateParts, true); + } + finally { + propagatingLocally = wasMe; + } +} +var propagatingLocally = false; + +var bc; +var createBC = function () { }; +if (typeof BroadcastChannel !== 'undefined') { + createBC = function () { + bc = new BroadcastChannel(STORAGE_MUTATED_DOM_EVENT_NAME); + bc.onmessage = function (ev) { return ev.data && propagateLocally(ev.data); }; + }; + createBC(); + if (typeof bc.unref === 'function') { + bc.unref(); + } + globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, function (changedParts) { + if (!propagatingLocally) { + bc.postMessage(changedParts); + } + }); +} + +if (typeof addEventListener !== 'undefined') { + addEventListener('pagehide', function (event) { + if (!Dexie$1.disableBfCache && event.persisted) { + if (debug) + console.debug('Dexie: handling persisted pagehide'); + bc === null || bc === void 0 ? void 0 : bc.close(); + for (var _i = 0, connections_1 = connections; _i < connections_1.length; _i++) { + var db = connections_1[_i]; + db.close({ disableAutoOpen: false }); + } + } + }); + addEventListener('pageshow', function (event) { + if (!Dexie$1.disableBfCache && event.persisted) { + if (debug) + console.debug('Dexie: handling persisted pageshow'); + createBC(); + propagateLocally({ all: new RangeSet(-Infinity, [[]]) }); + } + }); +} + +function add(value) { + return new PropModification({ add: value }); +} + +function remove(value) { + return new PropModification({ remove: value }); +} + +function replacePrefix(a, b) { + return new PropModification({ replacePrefix: [a, b] }); +} + +DexiePromise.rejectionMapper = mapError; +setDebug(debug); + +export { Dexie$1 as Dexie, Entity, PropModSymbol, PropModification, RangeSet, add, cmp, Dexie$1 as default, liveQuery, mergeRanges, rangesOverlap, remove, replacePrefix }; +//# sourceMappingURL=dexie.mjs.map diff --git a/libs/jieba-wasm/jieba_rs_wasm.js b/libs/jieba-wasm/jieba_rs_wasm.js new file mode 100644 index 0000000..d6af3f7 --- /dev/null +++ b/libs/jieba-wasm/jieba_rs_wasm.js @@ -0,0 +1,390 @@ + +let wasm; + +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + +cachedTextDecoder.decode(); + +let cachegetUint8Memory0 = null; +function getUint8Memory0() { + if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) { + cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachegetUint8Memory0; +} + +function getStringFromWasm0(ptr, len) { + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); +} + +const heap = new Array(32).fill(undefined); + +heap.push(undefined, null, true, false); + +let heap_next = heap.length; + +function addHeapObject(obj) { + if (heap_next === heap.length) heap.push(heap.length + 1); + const idx = heap_next; + heap_next = heap[idx]; + + heap[idx] = obj; + return idx; +} + +function getObject(idx) { return heap[idx]; } + +function dropObject(idx) { + if (idx < 36) return; + heap[idx] = heap_next; + heap_next = idx; +} + +function takeObject(idx) { + const ret = getObject(idx); + dropObject(idx); + return ret; +} + +function debugString(val) { + // primitive types + const type = typeof val; + if (type == 'number' || type == 'boolean' || val == null) { + return `${val}`; + } + if (type == 'string') { + return `"${val}"`; + } + if (type == 'symbol') { + const description = val.description; + if (description == null) { + return 'Symbol'; + } else { + return `Symbol(${description})`; + } + } + if (type == 'function') { + const name = val.name; + if (typeof name == 'string' && name.length > 0) { + return `Function(${name})`; + } else { + return 'Function'; + } + } + // objects + if (Array.isArray(val)) { + const length = val.length; + let debug = '['; + if (length > 0) { + debug += debugString(val[0]); + } + for(let i = 1; i < length; i++) { + debug += ', ' + debugString(val[i]); + } + debug += ']'; + return debug; + } + // Test for built-in + const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); + let className; + if (builtInMatches.length > 1) { + className = builtInMatches[1]; + } else { + // Failed to match the standard '[object ClassName]' + return toString.call(val); + } + if (className == 'Object') { + // we're a user defined class or Object + // JSON.stringify avoids problems with cycles, and is generally much + // easier than looping through ownProperties of `val`. + try { + return 'Object(' + JSON.stringify(val) + ')'; + } catch (_) { + return 'Object'; + } + } + // errors + if (val instanceof Error) { + return `${val.name}: ${val.message}\n${val.stack}`; + } + // TODO we could test for more things here, like `Set`s and `Map`s. + return className; +} + +let WASM_VECTOR_LEN = 0; + +let cachedTextEncoder = new TextEncoder('utf-8'); + +const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' + ? function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); +} + : function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; +}); + +function passStringToWasm0(arg, malloc, realloc) { + + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length); + getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len); + + const mem = getUint8Memory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3); + const view = getUint8Memory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); + + offset += ret.written; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +let cachegetInt32Memory0 = null; +function getInt32Memory0() { + if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) { + cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachegetInt32Memory0; +} + +let cachegetUint32Memory0 = null; +function getUint32Memory0() { + if (cachegetUint32Memory0 === null || cachegetUint32Memory0.buffer !== wasm.memory.buffer) { + cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer); + } + return cachegetUint32Memory0; +} + +function getArrayJsValueFromWasm0(ptr, len) { + const mem = getUint32Memory0(); + const slice = mem.subarray(ptr / 4, ptr / 4 + len); + const result = []; + for (let i = 0; i < slice.length; i++) { + result.push(takeObject(slice[i])); + } + return result; +} +/** +* @param {string} text +* @param {boolean} hmm +* @returns {any[]} +*/ +export function cut(text, hmm) { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + var ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len0 = WASM_VECTOR_LEN; + wasm.cut(retptr, ptr0, len0, hmm); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4); + return v1; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } +} + +/** +* @param {string} text +* @returns {any[]} +*/ +export function cut_all(text) { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + var ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len0 = WASM_VECTOR_LEN; + wasm.cut_all(retptr, ptr0, len0); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4); + return v1; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } +} + +/** +* @param {string} text +* @param {boolean} hmm +* @returns {any[]} +*/ +export function cut_for_search(text, hmm) { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + var ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len0 = WASM_VECTOR_LEN; + wasm.cut_for_search(retptr, ptr0, len0, hmm); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v1 = getArrayJsValueFromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4); + return v1; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } +} + +/** +* @param {string} text +* @param {string} mode +* @param {boolean} hmm +* @returns {any[]} +*/ +export function tokenize(text, mode, hmm) { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + var ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len0 = WASM_VECTOR_LEN; + var ptr1 = passStringToWasm0(mode, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + wasm.tokenize(retptr, ptr0, len0, ptr1, len1, hmm); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v2 = getArrayJsValueFromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 4); + return v2; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } +} + +function isLikeNone(x) { + return x === undefined || x === null; +} +/** +* @param {string} word +* @param {number | undefined} freq +* @param {string | undefined} tag +* @returns {number} +*/ +export function add_word(word, freq, tag) { + var ptr0 = passStringToWasm0(word, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len0 = WASM_VECTOR_LEN; + var ptr1 = isLikeNone(tag) ? 0 : passStringToWasm0(tag, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len1 = WASM_VECTOR_LEN; + var ret = wasm.add_word(ptr0, len0, !isLikeNone(freq), isLikeNone(freq) ? 0 : freq, ptr1, len1); + return ret >>> 0; +} + +async function load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } +} + +async function init(input) { + if (typeof input === 'undefined') { + input = new URL('jieba_rs_wasm_bg.wasm', import.meta.url); + } + const imports = {}; + imports.wbg = {}; + imports.wbg.__wbindgen_string_new = function(arg0, arg1) { + var ret = getStringFromWasm0(arg0, arg1); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_object_drop_ref = function(arg0) { + takeObject(arg0); + }; + imports.wbg.__wbg_new_68adb0d58759a4ed = function() { + var ret = new Object(); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_number_new = function(arg0) { + var ret = arg0; + return addHeapObject(ret); + }; + imports.wbg.__wbg_set_2e79e744454afade = function(arg0, arg1, arg2) { + getObject(arg0)[takeObject(arg1)] = takeObject(arg2); + }; + imports.wbg.__wbindgen_object_clone_ref = function(arg0) { + var ret = getObject(arg0); + return addHeapObject(ret); + }; + imports.wbg.__wbg_new_7031805939a80203 = function(arg0, arg1) { + var ret = new Error(getStringFromWasm0(arg0, arg1)); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_debug_string = function(arg0, arg1) { + var ret = debugString(getObject(arg1)); + var ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + var len0 = WASM_VECTOR_LEN; + getInt32Memory0()[arg0 / 4 + 1] = len0; + getInt32Memory0()[arg0 / 4 + 0] = ptr0; + }; + imports.wbg.__wbindgen_throw = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }; + imports.wbg.__wbindgen_rethrow = function(arg0) { + throw takeObject(arg0); + }; + + if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) { + input = fetch(input); + } + + + + const { instance, module } = await load(await input, imports); + + wasm = instance.exports; + init.__wbindgen_wasm_module = module; + + return wasm; +} + +export default init; + diff --git a/libs/jieba-wasm/jieba_rs_wasm_bg.wasm b/libs/jieba-wasm/jieba_rs_wasm_bg.wasm new file mode 100644 index 0000000..d78f47c Binary files /dev/null and b/libs/jieba-wasm/jieba_rs_wasm_bg.wasm differ diff --git a/libs/jieba-wasm/jieba_rs_wasm_bg.wasm.d.ts b/libs/jieba-wasm/jieba_rs_wasm_bg.wasm.d.ts new file mode 100644 index 0000000..0e1390d --- /dev/null +++ b/libs/jieba-wasm/jieba_rs_wasm_bg.wasm.d.ts @@ -0,0 +1,12 @@ +/* tslint:disable */ +/* eslint-disable */ +export const memory: WebAssembly.Memory; +export function cut(a: number, b: number, c: number, d: number): void; +export function cut_all(a: number, b: number, c: number): void; +export function cut_for_search(a: number, b: number, c: number, d: number): void; +export function tokenize(a: number, b: number, c: number, d: number, e: number, f: number): void; +export function add_word(a: number, b: number, c: number, d: number, e: number, f: number): number; +export function __wbindgen_malloc(a: number): number; +export function __wbindgen_realloc(a: number, b: number, c: number): number; +export function __wbindgen_add_to_stack_pointer(a: number): number; +export function __wbindgen_free(a: number, b: number): void; diff --git a/libs/minisearch.mjs b/libs/minisearch.mjs new file mode 100644 index 0000000..a05fe7b --- /dev/null +++ b/libs/minisearch.mjs @@ -0,0 +1,2036 @@ +/****************************************************************************** +Copyright (c) Microsoft Corporation. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +***************************************************************************** */ +/* global Reflect, Promise, SuppressedError, Symbol */ + + +function __awaiter(thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +} + +typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}; + +/** @ignore */ +const ENTRIES = 'ENTRIES'; +/** @ignore */ +const KEYS = 'KEYS'; +/** @ignore */ +const VALUES = 'VALUES'; +/** @ignore */ +const LEAF = ''; +/** + * @private + */ +class TreeIterator { + constructor(set, type) { + const node = set._tree; + const keys = Array.from(node.keys()); + this.set = set; + this._type = type; + this._path = keys.length > 0 ? [{ node, keys }] : []; + } + next() { + const value = this.dive(); + this.backtrack(); + return value; + } + dive() { + if (this._path.length === 0) { + return { done: true, value: undefined }; + } + const { node, keys } = last$1(this._path); + if (last$1(keys) === LEAF) { + return { done: false, value: this.result() }; + } + const child = node.get(last$1(keys)); + this._path.push({ node: child, keys: Array.from(child.keys()) }); + return this.dive(); + } + backtrack() { + if (this._path.length === 0) { + return; + } + const keys = last$1(this._path).keys; + keys.pop(); + if (keys.length > 0) { + return; + } + this._path.pop(); + this.backtrack(); + } + key() { + return this.set._prefix + this._path + .map(({ keys }) => last$1(keys)) + .filter(key => key !== LEAF) + .join(''); + } + value() { + return last$1(this._path).node.get(LEAF); + } + result() { + switch (this._type) { + case VALUES: return this.value(); + case KEYS: return this.key(); + default: return [this.key(), this.value()]; + } + } + [Symbol.iterator]() { + return this; + } +} +const last$1 = (array) => { + return array[array.length - 1]; +}; + +/* eslint-disable no-labels */ +/** + * @ignore + */ +const fuzzySearch = (node, query, maxDistance) => { + const results = new Map(); + if (query === undefined) + return results; + // Number of columns in the Levenshtein matrix. + const n = query.length + 1; + // Matching terms can never be longer than N + maxDistance. + const m = n + maxDistance; + // Fill first matrix row and column with numbers: 0 1 2 3 ... + const matrix = new Uint8Array(m * n).fill(maxDistance + 1); + for (let j = 0; j < n; ++j) + matrix[j] = j; + for (let i = 1; i < m; ++i) + matrix[i * n] = i; + recurse(node, query, maxDistance, results, matrix, 1, n, ''); + return results; +}; +// Modified version of http://stevehanov.ca/blog/?id=114 +// This builds a Levenshtein matrix for a given query and continuously updates +// it for nodes in the radix tree that fall within the given maximum edit +// distance. Keeping the same matrix around is beneficial especially for larger +// edit distances. +// +// k a t e <-- query +// 0 1 2 3 4 +// c 1 1 2 3 4 +// a 2 2 1 2 3 +// t 3 3 2 1 [2] <-- edit distance +// ^ +// ^ term in radix tree, rows are added and removed as needed +const recurse = (node, query, maxDistance, results, matrix, m, n, prefix) => { + const offset = m * n; + key: for (const key of node.keys()) { + if (key === LEAF) { + // We've reached a leaf node. Check if the edit distance acceptable and + // store the result if it is. + const distance = matrix[offset - 1]; + if (distance <= maxDistance) { + results.set(prefix, [node.get(key), distance]); + } + } + else { + // Iterate over all characters in the key. Update the Levenshtein matrix + // and check if the minimum distance in the last row is still within the + // maximum edit distance. If it is, we can recurse over all child nodes. + let i = m; + for (let pos = 0; pos < key.length; ++pos, ++i) { + const char = key[pos]; + const thisRowOffset = n * i; + const prevRowOffset = thisRowOffset - n; + // Set the first column based on the previous row, and initialize the + // minimum distance in the current row. + let minDistance = matrix[thisRowOffset]; + const jmin = Math.max(0, i - maxDistance - 1); + const jmax = Math.min(n - 1, i + maxDistance); + // Iterate over remaining columns (characters in the query). + for (let j = jmin; j < jmax; ++j) { + const different = char !== query[j]; + // It might make sense to only read the matrix positions used for + // deletion/insertion if the characters are different. But we want to + // avoid conditional reads for performance reasons. + const rpl = matrix[prevRowOffset + j] + +different; + const del = matrix[prevRowOffset + j + 1] + 1; + const ins = matrix[thisRowOffset + j] + 1; + const dist = matrix[thisRowOffset + j + 1] = Math.min(rpl, del, ins); + if (dist < minDistance) + minDistance = dist; + } + // Because distance will never decrease, we can stop. There will be no + // matching child nodes. + if (minDistance > maxDistance) { + continue key; + } + } + recurse(node.get(key), query, maxDistance, results, matrix, i, n, prefix + key); + } + } +}; + +/* eslint-disable no-labels */ +/** + * A class implementing the same interface as a standard JavaScript + * [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) + * with string keys, but adding support for efficiently searching entries with + * prefix or fuzzy search. This class is used internally by {@link MiniSearch} + * as the inverted index data structure. The implementation is a radix tree + * (compressed prefix tree). + * + * Since this class can be of general utility beyond _MiniSearch_, it is + * exported by the `minisearch` package and can be imported (or required) as + * `minisearch/SearchableMap`. + * + * @typeParam T The type of the values stored in the map. + */ +class SearchableMap { + /** + * The constructor is normally called without arguments, creating an empty + * map. In order to create a {@link SearchableMap} from an iterable or from an + * object, check {@link SearchableMap.from} and {@link + * SearchableMap.fromObject}. + * + * The constructor arguments are for internal use, when creating derived + * mutable views of a map at a prefix. + */ + constructor(tree = new Map(), prefix = '') { + this._size = undefined; + this._tree = tree; + this._prefix = prefix; + } + /** + * Creates and returns a mutable view of this {@link SearchableMap}, + * containing only entries that share the given prefix. + * + * ### Usage: + * + * ```javascript + * let map = new SearchableMap() + * map.set("unicorn", 1) + * map.set("universe", 2) + * map.set("university", 3) + * map.set("unique", 4) + * map.set("hello", 5) + * + * let uni = map.atPrefix("uni") + * uni.get("unique") // => 4 + * uni.get("unicorn") // => 1 + * uni.get("hello") // => undefined + * + * let univer = map.atPrefix("univer") + * univer.get("unique") // => undefined + * univer.get("universe") // => 2 + * univer.get("university") // => 3 + * ``` + * + * @param prefix The prefix + * @return A {@link SearchableMap} representing a mutable view of the original + * Map at the given prefix + */ + atPrefix(prefix) { + if (!prefix.startsWith(this._prefix)) { + throw new Error('Mismatched prefix'); + } + const [node, path] = trackDown(this._tree, prefix.slice(this._prefix.length)); + if (node === undefined) { + const [parentNode, key] = last(path); + for (const k of parentNode.keys()) { + if (k !== LEAF && k.startsWith(key)) { + const node = new Map(); + node.set(k.slice(key.length), parentNode.get(k)); + return new SearchableMap(node, prefix); + } + } + } + return new SearchableMap(node, prefix); + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear + */ + clear() { + this._size = undefined; + this._tree.clear(); + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete + * @param key Key to delete + */ + delete(key) { + this._size = undefined; + return remove(this._tree, key); + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries + * @return An iterator iterating through `[key, value]` entries. + */ + entries() { + return new TreeIterator(this, ENTRIES); + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach + * @param fn Iteration function + */ + forEach(fn) { + for (const [key, value] of this) { + fn(key, value, this); + } + } + /** + * Returns a Map of all the entries that have a key within the given edit + * distance from the search key. The keys of the returned Map are the matching + * keys, while the values are two-element arrays where the first element is + * the value associated to the key, and the second is the edit distance of the + * key to the search key. + * + * ### Usage: + * + * ```javascript + * let map = new SearchableMap() + * map.set('hello', 'world') + * map.set('hell', 'yeah') + * map.set('ciao', 'mondo') + * + * // Get all entries that match the key 'hallo' with a maximum edit distance of 2 + * map.fuzzyGet('hallo', 2) + * // => Map(2) { 'hello' => ['world', 1], 'hell' => ['yeah', 2] } + * + * // In the example, the "hello" key has value "world" and edit distance of 1 + * // (change "e" to "a"), the key "hell" has value "yeah" and edit distance of 2 + * // (change "e" to "a", delete "o") + * ``` + * + * @param key The search key + * @param maxEditDistance The maximum edit distance (Levenshtein) + * @return A Map of the matching keys to their value and edit distance + */ + fuzzyGet(key, maxEditDistance) { + return fuzzySearch(this._tree, key, maxEditDistance); + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get + * @param key Key to get + * @return Value associated to the key, or `undefined` if the key is not + * found. + */ + get(key) { + const node = lookup(this._tree, key); + return node !== undefined ? node.get(LEAF) : undefined; + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has + * @param key Key + * @return True if the key is in the map, false otherwise + */ + has(key) { + const node = lookup(this._tree, key); + return node !== undefined && node.has(LEAF); + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys + * @return An `Iterable` iterating through keys + */ + keys() { + return new TreeIterator(this, KEYS); + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set + * @param key Key to set + * @param value Value to associate to the key + * @return The {@link SearchableMap} itself, to allow chaining + */ + set(key, value) { + if (typeof key !== 'string') { + throw new Error('key must be a string'); + } + this._size = undefined; + const node = createPath(this._tree, key); + node.set(LEAF, value); + return this; + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size + */ + get size() { + if (this._size) { + return this._size; + } + /** @ignore */ + this._size = 0; + const iter = this.entries(); + while (!iter.next().done) + this._size += 1; + return this._size; + } + /** + * Updates the value at the given key using the provided function. The function + * is called with the current value at the key, and its return value is used as + * the new value to be set. + * + * ### Example: + * + * ```javascript + * // Increment the current value by one + * searchableMap.update('somekey', (currentValue) => currentValue == null ? 0 : currentValue + 1) + * ``` + * + * If the value at the given key is or will be an object, it might not require + * re-assignment. In that case it is better to use `fetch()`, because it is + * faster. + * + * @param key The key to update + * @param fn The function used to compute the new value from the current one + * @return The {@link SearchableMap} itself, to allow chaining + */ + update(key, fn) { + if (typeof key !== 'string') { + throw new Error('key must be a string'); + } + this._size = undefined; + const node = createPath(this._tree, key); + node.set(LEAF, fn(node.get(LEAF))); + return this; + } + /** + * Fetches the value of the given key. If the value does not exist, calls the + * given function to create a new value, which is inserted at the given key + * and subsequently returned. + * + * ### Example: + * + * ```javascript + * const map = searchableMap.fetch('somekey', () => new Map()) + * map.set('foo', 'bar') + * ``` + * + * @param key The key to update + * @param initial A function that creates a new value if the key does not exist + * @return The existing or new value at the given key + */ + fetch(key, initial) { + if (typeof key !== 'string') { + throw new Error('key must be a string'); + } + this._size = undefined; + const node = createPath(this._tree, key); + let value = node.get(LEAF); + if (value === undefined) { + node.set(LEAF, value = initial()); + } + return value; + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values + * @return An `Iterable` iterating through values. + */ + values() { + return new TreeIterator(this, VALUES); + } + /** + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator + */ + [Symbol.iterator]() { + return this.entries(); + } + /** + * Creates a {@link SearchableMap} from an `Iterable` of entries + * + * @param entries Entries to be inserted in the {@link SearchableMap} + * @return A new {@link SearchableMap} with the given entries + */ + static from(entries) { + const tree = new SearchableMap(); + for (const [key, value] of entries) { + tree.set(key, value); + } + return tree; + } + /** + * Creates a {@link SearchableMap} from the iterable properties of a JavaScript object + * + * @param object Object of entries for the {@link SearchableMap} + * @return A new {@link SearchableMap} with the given entries + */ + static fromObject(object) { + return SearchableMap.from(Object.entries(object)); + } +} +const trackDown = (tree, key, path = []) => { + if (key.length === 0 || tree == null) { + return [tree, path]; + } + for (const k of tree.keys()) { + if (k !== LEAF && key.startsWith(k)) { + path.push([tree, k]); // performance: update in place + return trackDown(tree.get(k), key.slice(k.length), path); + } + } + path.push([tree, key]); // performance: update in place + return trackDown(undefined, '', path); +}; +const lookup = (tree, key) => { + if (key.length === 0 || tree == null) { + return tree; + } + for (const k of tree.keys()) { + if (k !== LEAF && key.startsWith(k)) { + return lookup(tree.get(k), key.slice(k.length)); + } + } +}; +// Create a path in the radix tree for the given key, and returns the deepest +// node. This function is in the hot path for indexing. It avoids unnecessary +// string operations and recursion for performance. +const createPath = (node, key) => { + const keyLength = key.length; + outer: for (let pos = 0; node && pos < keyLength;) { + for (const k of node.keys()) { + // Check whether this key is a candidate: the first characters must match. + if (k !== LEAF && key[pos] === k[0]) { + const len = Math.min(keyLength - pos, k.length); + // Advance offset to the point where key and k no longer match. + let offset = 1; + while (offset < len && key[pos + offset] === k[offset]) + ++offset; + const child = node.get(k); + if (offset === k.length) { + // The existing key is shorter than the key we need to create. + node = child; + } + else { + // Partial match: we need to insert an intermediate node to contain + // both the existing subtree and the new node. + const intermediate = new Map(); + intermediate.set(k.slice(offset), child); + node.set(key.slice(pos, pos + offset), intermediate); + node.delete(k); + node = intermediate; + } + pos += offset; + continue outer; + } + } + // Create a final child node to contain the final suffix of the key. + const child = new Map(); + node.set(key.slice(pos), child); + return child; + } + return node; +}; +const remove = (tree, key) => { + const [node, path] = trackDown(tree, key); + if (node === undefined) { + return; + } + node.delete(LEAF); + if (node.size === 0) { + cleanup(path); + } + else if (node.size === 1) { + const [key, value] = node.entries().next().value; + merge(path, key, value); + } +}; +const cleanup = (path) => { + if (path.length === 0) { + return; + } + const [node, key] = last(path); + node.delete(key); + if (node.size === 0) { + cleanup(path.slice(0, -1)); + } + else if (node.size === 1) { + const [key, value] = node.entries().next().value; + if (key !== LEAF) { + merge(path.slice(0, -1), key, value); + } + } +}; +const merge = (path, key, value) => { + if (path.length === 0) { + return; + } + const [node, nodeKey] = last(path); + node.set(nodeKey + key, value); + node.delete(nodeKey); +}; +const last = (array) => { + return array[array.length - 1]; +}; + +const OR = 'or'; +const AND = 'and'; +const AND_NOT = 'and_not'; +/** + * {@link MiniSearch} is the main entrypoint class, implementing a full-text + * search engine in memory. + * + * @typeParam T The type of the documents being indexed. + * + * ### Basic example: + * + * ```javascript + * const documents = [ + * { + * id: 1, + * title: 'Moby Dick', + * text: 'Call me Ishmael. Some years ago...', + * category: 'fiction' + * }, + * { + * id: 2, + * title: 'Zen and the Art of Motorcycle Maintenance', + * text: 'I can see by my watch...', + * category: 'fiction' + * }, + * { + * id: 3, + * title: 'Neuromancer', + * text: 'The sky above the port was...', + * category: 'fiction' + * }, + * { + * id: 4, + * title: 'Zen and the Art of Archery', + * text: 'At first sight it must seem...', + * category: 'non-fiction' + * }, + * // ...and more + * ] + * + * // Create a search engine that indexes the 'title' and 'text' fields for + * // full-text search. Search results will include 'title' and 'category' (plus the + * // id field, that is always stored and returned) + * const miniSearch = new MiniSearch({ + * fields: ['title', 'text'], + * storeFields: ['title', 'category'] + * }) + * + * // Add documents to the index + * miniSearch.addAll(documents) + * + * // Search for documents: + * let results = miniSearch.search('zen art motorcycle') + * // => [ + * // { id: 2, title: 'Zen and the Art of Motorcycle Maintenance', category: 'fiction', score: 2.77258 }, + * // { id: 4, title: 'Zen and the Art of Archery', category: 'non-fiction', score: 1.38629 } + * // ] + * ``` + */ +class MiniSearch { + /** + * @param options Configuration options + * + * ### Examples: + * + * ```javascript + * // Create a search engine that indexes the 'title' and 'text' fields of your + * // documents: + * const miniSearch = new MiniSearch({ fields: ['title', 'text'] }) + * ``` + * + * ### ID Field: + * + * ```javascript + * // Your documents are assumed to include a unique 'id' field, but if you want + * // to use a different field for document identification, you can set the + * // 'idField' option: + * const miniSearch = new MiniSearch({ idField: 'key', fields: ['title', 'text'] }) + * ``` + * + * ### Options and defaults: + * + * ```javascript + * // The full set of options (here with their default value) is: + * const miniSearch = new MiniSearch({ + * // idField: field that uniquely identifies a document + * idField: 'id', + * + * // extractField: function used to get the value of a field in a document. + * // By default, it assumes the document is a flat object with field names as + * // property keys and field values as string property values, but custom logic + * // can be implemented by setting this option to a custom extractor function. + * extractField: (document, fieldName) => document[fieldName], + * + * // tokenize: function used to split fields into individual terms. By + * // default, it is also used to tokenize search queries, unless a specific + * // `tokenize` search option is supplied. When tokenizing an indexed field, + * // the field name is passed as the second argument. + * tokenize: (string, _fieldName) => string.split(SPACE_OR_PUNCTUATION), + * + * // processTerm: function used to process each tokenized term before + * // indexing. It can be used for stemming and normalization. Return a falsy + * // value in order to discard a term. By default, it is also used to process + * // search queries, unless a specific `processTerm` option is supplied as a + * // search option. When processing a term from a indexed field, the field + * // name is passed as the second argument. + * processTerm: (term, _fieldName) => term.toLowerCase(), + * + * // searchOptions: default search options, see the `search` method for + * // details + * searchOptions: undefined, + * + * // fields: document fields to be indexed. Mandatory, but not set by default + * fields: undefined + * + * // storeFields: document fields to be stored and returned as part of the + * // search results. + * storeFields: [] + * }) + * ``` + */ + constructor(options) { + if ((options === null || options === void 0 ? void 0 : options.fields) == null) { + throw new Error('MiniSearch: option "fields" must be provided'); + } + const autoVacuum = (options.autoVacuum == null || options.autoVacuum === true) ? defaultAutoVacuumOptions : options.autoVacuum; + this._options = Object.assign(Object.assign(Object.assign({}, defaultOptions), options), { autoVacuum, searchOptions: Object.assign(Object.assign({}, defaultSearchOptions), (options.searchOptions || {})), autoSuggestOptions: Object.assign(Object.assign({}, defaultAutoSuggestOptions), (options.autoSuggestOptions || {})) }); + this._index = new SearchableMap(); + this._documentCount = 0; + this._documentIds = new Map(); + this._idToShortId = new Map(); + // Fields are defined during initialization, don't change, are few in + // number, rarely need iterating over, and have string keys. Therefore in + // this case an object is a better candidate than a Map to store the mapping + // from field key to ID. + this._fieldIds = {}; + this._fieldLength = new Map(); + this._avgFieldLength = []; + this._nextId = 0; + this._storedFields = new Map(); + this._dirtCount = 0; + this._currentVacuum = null; + this._enqueuedVacuum = null; + this._enqueuedVacuumConditions = defaultVacuumConditions; + this.addFields(this._options.fields); + } + /** + * Adds a document to the index + * + * @param document The document to be indexed + */ + add(document) { + const { extractField, tokenize, processTerm, fields, idField } = this._options; + const id = extractField(document, idField); + if (id == null) { + throw new Error(`MiniSearch: document does not have ID field "${idField}"`); + } + if (this._idToShortId.has(id)) { + throw new Error(`MiniSearch: duplicate ID ${id}`); + } + const shortDocumentId = this.addDocumentId(id); + this.saveStoredFields(shortDocumentId, document); + for (const field of fields) { + const fieldValue = extractField(document, field); + if (fieldValue == null) + continue; + const tokens = tokenize(fieldValue.toString(), field); + const fieldId = this._fieldIds[field]; + const uniqueTerms = new Set(tokens).size; + this.addFieldLength(shortDocumentId, fieldId, this._documentCount - 1, uniqueTerms); + for (const term of tokens) { + const processedTerm = processTerm(term, field); + if (Array.isArray(processedTerm)) { + for (const t of processedTerm) { + this.addTerm(fieldId, shortDocumentId, t); + } + } + else if (processedTerm) { + this.addTerm(fieldId, shortDocumentId, processedTerm); + } + } + } + } + /** + * Adds all the given documents to the index + * + * @param documents An array of documents to be indexed + */ + addAll(documents) { + for (const document of documents) + this.add(document); + } + /** + * Adds all the given documents to the index asynchronously. + * + * Returns a promise that resolves (to `undefined`) when the indexing is done. + * This method is useful when index many documents, to avoid blocking the main + * thread. The indexing is performed asynchronously and in chunks. + * + * @param documents An array of documents to be indexed + * @param options Configuration options + * @return A promise resolving to `undefined` when the indexing is done + */ + addAllAsync(documents, options = {}) { + const { chunkSize = 10 } = options; + const acc = { chunk: [], promise: Promise.resolve() }; + const { chunk, promise } = documents.reduce(({ chunk, promise }, document, i) => { + chunk.push(document); + if ((i + 1) % chunkSize === 0) { + return { + chunk: [], + promise: promise + .then(() => new Promise(resolve => setTimeout(resolve, 0))) + .then(() => this.addAll(chunk)) + }; + } + else { + return { chunk, promise }; + } + }, acc); + return promise.then(() => this.addAll(chunk)); + } + /** + * Removes the given document from the index. + * + * The document to remove must NOT have changed between indexing and removal, + * otherwise the index will be corrupted. + * + * This method requires passing the full document to be removed (not just the + * ID), and immediately removes the document from the inverted index, allowing + * memory to be released. A convenient alternative is {@link + * MiniSearch#discard}, which needs only the document ID, and has the same + * visible effect, but delays cleaning up the index until the next vacuuming. + * + * @param document The document to be removed + */ + remove(document) { + const { tokenize, processTerm, extractField, fields, idField } = this._options; + const id = extractField(document, idField); + if (id == null) { + throw new Error(`MiniSearch: document does not have ID field "${idField}"`); + } + const shortId = this._idToShortId.get(id); + if (shortId == null) { + throw new Error(`MiniSearch: cannot remove document with ID ${id}: it is not in the index`); + } + for (const field of fields) { + const fieldValue = extractField(document, field); + if (fieldValue == null) + continue; + const tokens = tokenize(fieldValue.toString(), field); + const fieldId = this._fieldIds[field]; + const uniqueTerms = new Set(tokens).size; + this.removeFieldLength(shortId, fieldId, this._documentCount, uniqueTerms); + for (const term of tokens) { + const processedTerm = processTerm(term, field); + if (Array.isArray(processedTerm)) { + for (const t of processedTerm) { + this.removeTerm(fieldId, shortId, t); + } + } + else if (processedTerm) { + this.removeTerm(fieldId, shortId, processedTerm); + } + } + } + this._storedFields.delete(shortId); + this._documentIds.delete(shortId); + this._idToShortId.delete(id); + this._fieldLength.delete(shortId); + this._documentCount -= 1; + } + /** + * Removes all the given documents from the index. If called with no arguments, + * it removes _all_ documents from the index. + * + * @param documents The documents to be removed. If this argument is omitted, + * all documents are removed. Note that, for removing all documents, it is + * more efficient to call this method with no arguments than to pass all + * documents. + */ + removeAll(documents) { + if (documents) { + for (const document of documents) + this.remove(document); + } + else if (arguments.length > 0) { + throw new Error('Expected documents to be present. Omit the argument to remove all documents.'); + } + else { + this._index = new SearchableMap(); + this._documentCount = 0; + this._documentIds = new Map(); + this._idToShortId = new Map(); + this._fieldLength = new Map(); + this._avgFieldLength = []; + this._storedFields = new Map(); + this._nextId = 0; + } + } + /** + * Discards the document with the given ID, so it won't appear in search results + * + * It has the same visible effect of {@link MiniSearch.remove} (both cause the + * document to stop appearing in searches), but a different effect on the + * internal data structures: + * + * - {@link MiniSearch#remove} requires passing the full document to be + * removed as argument, and removes it from the inverted index immediately. + * + * - {@link MiniSearch#discard} instead only needs the document ID, and + * works by marking the current version of the document as discarded, so it + * is immediately ignored by searches. This is faster and more convenient + * than {@link MiniSearch#remove}, but the index is not immediately + * modified. To take care of that, vacuuming is performed after a certain + * number of documents are discarded, cleaning up the index and allowing + * memory to be released. + * + * After discarding a document, it is possible to re-add a new version, and + * only the new version will appear in searches. In other words, discarding + * and re-adding a document works exactly like removing and re-adding it. The + * {@link MiniSearch.replace} method can also be used to replace a document + * with a new version. + * + * #### Details about vacuuming + * + * Repetite calls to this method would leave obsolete document references in + * the index, invisible to searches. Two mechanisms take care of cleaning up: + * clean up during search, and vacuuming. + * + * - Upon search, whenever a discarded ID is found (and ignored for the + * results), references to the discarded document are removed from the + * inverted index entries for the search terms. This ensures that subsequent + * searches for the same terms do not need to skip these obsolete references + * again. + * + * - In addition, vacuuming is performed automatically by default (see the + * `autoVacuum` field in {@link Options}) after a certain number of + * documents are discarded. Vacuuming traverses all terms in the index, + * cleaning up all references to discarded documents. Vacuuming can also be + * triggered manually by calling {@link MiniSearch#vacuum}. + * + * @param id The ID of the document to be discarded + */ + discard(id) { + const shortId = this._idToShortId.get(id); + if (shortId == null) { + throw new Error(`MiniSearch: cannot discard document with ID ${id}: it is not in the index`); + } + this._idToShortId.delete(id); + this._documentIds.delete(shortId); + this._storedFields.delete(shortId); + (this._fieldLength.get(shortId) || []).forEach((fieldLength, fieldId) => { + this.removeFieldLength(shortId, fieldId, this._documentCount, fieldLength); + }); + this._fieldLength.delete(shortId); + this._documentCount -= 1; + this._dirtCount += 1; + this.maybeAutoVacuum(); + } + maybeAutoVacuum() { + if (this._options.autoVacuum === false) { + return; + } + const { minDirtFactor, minDirtCount, batchSize, batchWait } = this._options.autoVacuum; + this.conditionalVacuum({ batchSize, batchWait }, { minDirtCount, minDirtFactor }); + } + /** + * Discards the documents with the given IDs, so they won't appear in search + * results + * + * It is equivalent to calling {@link MiniSearch#discard} for all the given + * IDs, but with the optimization of triggering at most one automatic + * vacuuming at the end. + * + * Note: to remove all documents from the index, it is faster and more + * convenient to call {@link MiniSearch.removeAll} with no argument, instead + * of passing all IDs to this method. + */ + discardAll(ids) { + const autoVacuum = this._options.autoVacuum; + try { + this._options.autoVacuum = false; + for (const id of ids) { + this.discard(id); + } + } + finally { + this._options.autoVacuum = autoVacuum; + } + this.maybeAutoVacuum(); + } + /** + * It replaces an existing document with the given updated version + * + * It works by discarding the current version and adding the updated one, so + * it is functionally equivalent to calling {@link MiniSearch#discard} + * followed by {@link MiniSearch#add}. The ID of the updated document should + * be the same as the original one. + * + * Since it uses {@link MiniSearch#discard} internally, this method relies on + * vacuuming to clean up obsolete document references from the index, allowing + * memory to be released (see {@link MiniSearch#discard}). + * + * @param updatedDocument The updated document to replace the old version + * with + */ + replace(updatedDocument) { + const { idField, extractField } = this._options; + const id = extractField(updatedDocument, idField); + this.discard(id); + this.add(updatedDocument); + } + /** + * Triggers a manual vacuuming, cleaning up references to discarded documents + * from the inverted index + * + * Vacuuming is only useful for applications that use the {@link + * MiniSearch#discard} or {@link MiniSearch#replace} methods. + * + * By default, vacuuming is performed automatically when needed (controlled by + * the `autoVacuum` field in {@link Options}), so there is usually no need to + * call this method, unless one wants to make sure to perform vacuuming at a + * specific moment. + * + * Vacuuming traverses all terms in the inverted index in batches, and cleans + * up references to discarded documents from the posting list, allowing memory + * to be released. + * + * The method takes an optional object as argument with the following keys: + * + * - `batchSize`: the size of each batch (1000 by default) + * + * - `batchWait`: the number of milliseconds to wait between batches (10 by + * default) + * + * On large indexes, vacuuming could have a non-negligible cost: batching + * avoids blocking the thread for long, diluting this cost so that it is not + * negatively affecting the application. Nonetheless, this method should only + * be called when necessary, and relying on automatic vacuuming is usually + * better. + * + * It returns a promise that resolves (to undefined) when the clean up is + * completed. If vacuuming is already ongoing at the time this method is + * called, a new one is enqueued immediately after the ongoing one, and a + * corresponding promise is returned. However, no more than one vacuuming is + * enqueued on top of the ongoing one, even if this method is called more + * times (enqueuing multiple ones would be useless). + * + * @param options Configuration options for the batch size and delay. See + * {@link VacuumOptions}. + */ + vacuum(options = {}) { + return this.conditionalVacuum(options); + } + conditionalVacuum(options, conditions) { + // If a vacuum is already ongoing, schedule another as soon as it finishes, + // unless there's already one enqueued. If one was already enqueued, do not + // enqueue another on top, but make sure that the conditions are the + // broadest. + if (this._currentVacuum) { + this._enqueuedVacuumConditions = this._enqueuedVacuumConditions && conditions; + if (this._enqueuedVacuum != null) { + return this._enqueuedVacuum; + } + this._enqueuedVacuum = this._currentVacuum.then(() => { + const conditions = this._enqueuedVacuumConditions; + this._enqueuedVacuumConditions = defaultVacuumConditions; + return this.performVacuuming(options, conditions); + }); + return this._enqueuedVacuum; + } + if (this.vacuumConditionsMet(conditions) === false) { + return Promise.resolve(); + } + this._currentVacuum = this.performVacuuming(options); + return this._currentVacuum; + } + performVacuuming(options, conditions) { + return __awaiter(this, void 0, void 0, function* () { + const initialDirtCount = this._dirtCount; + if (this.vacuumConditionsMet(conditions)) { + const batchSize = options.batchSize || defaultVacuumOptions.batchSize; + const batchWait = options.batchWait || defaultVacuumOptions.batchWait; + let i = 1; + for (const [term, fieldsData] of this._index) { + for (const [fieldId, fieldIndex] of fieldsData) { + for (const [shortId] of fieldIndex) { + if (this._documentIds.has(shortId)) { + continue; + } + if (fieldIndex.size <= 1) { + fieldsData.delete(fieldId); + } + else { + fieldIndex.delete(shortId); + } + } + } + if (this._index.get(term).size === 0) { + this._index.delete(term); + } + if (i % batchSize === 0) { + yield new Promise((resolve) => setTimeout(resolve, batchWait)); + } + i += 1; + } + this._dirtCount -= initialDirtCount; + } + // Make the next lines always async, so they execute after this function returns + yield null; + this._currentVacuum = this._enqueuedVacuum; + this._enqueuedVacuum = null; + }); + } + vacuumConditionsMet(conditions) { + if (conditions == null) { + return true; + } + let { minDirtCount, minDirtFactor } = conditions; + minDirtCount = minDirtCount || defaultAutoVacuumOptions.minDirtCount; + minDirtFactor = minDirtFactor || defaultAutoVacuumOptions.minDirtFactor; + return this.dirtCount >= minDirtCount && this.dirtFactor >= minDirtFactor; + } + /** + * Is `true` if a vacuuming operation is ongoing, `false` otherwise + */ + get isVacuuming() { + return this._currentVacuum != null; + } + /** + * The number of documents discarded since the most recent vacuuming + */ + get dirtCount() { + return this._dirtCount; + } + /** + * A number between 0 and 1 giving an indication about the proportion of + * documents that are discarded, and can therefore be cleaned up by vacuuming. + * A value close to 0 means that the index is relatively clean, while a higher + * value means that the index is relatively dirty, and vacuuming could release + * memory. + */ + get dirtFactor() { + return this._dirtCount / (1 + this._documentCount + this._dirtCount); + } + /** + * Returns `true` if a document with the given ID is present in the index and + * available for search, `false` otherwise + * + * @param id The document ID + */ + has(id) { + return this._idToShortId.has(id); + } + /** + * Returns the stored fields (as configured in the `storeFields` constructor + * option) for the given document ID. Returns `undefined` if the document is + * not present in the index. + * + * @param id The document ID + */ + getStoredFields(id) { + const shortId = this._idToShortId.get(id); + if (shortId == null) { + return undefined; + } + return this._storedFields.get(shortId); + } + /** + * Search for documents matching the given search query. + * + * The result is a list of scored document IDs matching the query, sorted by + * descending score, and each including data about which terms were matched and + * in which fields. + * + * ### Basic usage: + * + * ```javascript + * // Search for "zen art motorcycle" with default options: terms have to match + * // exactly, and individual terms are joined with OR + * miniSearch.search('zen art motorcycle') + * // => [ { id: 2, score: 2.77258, match: { ... } }, { id: 4, score: 1.38629, match: { ... } } ] + * ``` + * + * ### Restrict search to specific fields: + * + * ```javascript + * // Search only in the 'title' field + * miniSearch.search('zen', { fields: ['title'] }) + * ``` + * + * ### Field boosting: + * + * ```javascript + * // Boost a field + * miniSearch.search('zen', { boost: { title: 2 } }) + * ``` + * + * ### Prefix search: + * + * ```javascript + * // Search for "moto" with prefix search (it will match documents + * // containing terms that start with "moto" or "neuro") + * miniSearch.search('moto neuro', { prefix: true }) + * ``` + * + * ### Fuzzy search: + * + * ```javascript + * // Search for "ismael" with fuzzy search (it will match documents containing + * // terms similar to "ismael", with a maximum edit distance of 0.2 term.length + * // (rounded to nearest integer) + * miniSearch.search('ismael', { fuzzy: 0.2 }) + * ``` + * + * ### Combining strategies: + * + * ```javascript + * // Mix of exact match, prefix search, and fuzzy search + * miniSearch.search('ismael mob', { + * prefix: true, + * fuzzy: 0.2 + * }) + * ``` + * + * ### Advanced prefix and fuzzy search: + * + * ```javascript + * // Perform fuzzy and prefix search depending on the search term. Here + * // performing prefix and fuzzy search only on terms longer than 3 characters + * miniSearch.search('ismael mob', { + * prefix: term => term.length > 3 + * fuzzy: term => term.length > 3 ? 0.2 : null + * }) + * ``` + * + * ### Combine with AND: + * + * ```javascript + * // Combine search terms with AND (to match only documents that contain both + * // "motorcycle" and "art") + * miniSearch.search('motorcycle art', { combineWith: 'AND' }) + * ``` + * + * ### Combine with AND_NOT: + * + * There is also an AND_NOT combinator, that finds documents that match the + * first term, but do not match any of the other terms. This combinator is + * rarely useful with simple queries, and is meant to be used with advanced + * query combinations (see later for more details). + * + * ### Filtering results: + * + * ```javascript + * // Filter only results in the 'fiction' category (assuming that 'category' + * // is a stored field) + * miniSearch.search('motorcycle art', { + * filter: (result) => result.category === 'fiction' + * }) + * ``` + * + * ### Wildcard query + * + * Searching for an empty string (assuming the default tokenizer) returns no + * results. Sometimes though, one needs to match all documents, like in a + * "wildcard" search. This is possible by passing the special value + * {@link MiniSearch.wildcard} as the query: + * + * ```javascript + * // Return search results for all documents + * miniSearch.search(MiniSearch.wildcard) + * ``` + * + * Note that search options such as `filter` and `boostDocument` are still + * applied, influencing which results are returned, and their order: + * + * ```javascript + * // Return search results for all documents in the 'fiction' category + * miniSearch.search(MiniSearch.wildcard, { + * filter: (result) => result.category === 'fiction' + * }) + * ``` + * + * ### Advanced combination of queries: + * + * It is possible to combine different subqueries with OR, AND, and AND_NOT, + * and even with different search options, by passing a query expression + * tree object as the first argument, instead of a string. + * + * ```javascript + * // Search for documents that contain "zen" and ("motorcycle" or "archery") + * miniSearch.search({ + * combineWith: 'AND', + * queries: [ + * 'zen', + * { + * combineWith: 'OR', + * queries: ['motorcycle', 'archery'] + * } + * ] + * }) + * + * // Search for documents that contain ("apple" or "pear") but not "juice" and + * // not "tree" + * miniSearch.search({ + * combineWith: 'AND_NOT', + * queries: [ + * { + * combineWith: 'OR', + * queries: ['apple', 'pear'] + * }, + * 'juice', + * 'tree' + * ] + * }) + * ``` + * + * Each node in the expression tree can be either a string, or an object that + * supports all {@link SearchOptions} fields, plus a `queries` array field for + * subqueries. + * + * Note that, while this can become complicated to do by hand for complex or + * deeply nested queries, it provides a formalized expression tree API for + * external libraries that implement a parser for custom query languages. + * + * @param query Search query + * @param searchOptions Search options. Each option, if not given, defaults to the corresponding value of `searchOptions` given to the constructor, or to the library default. + */ + search(query, searchOptions = {}) { + const { searchOptions: globalSearchOptions } = this._options; + const searchOptionsWithDefaults = Object.assign(Object.assign({}, globalSearchOptions), searchOptions); + const rawResults = this.executeQuery(query, searchOptions); + const results = []; + for (const [docId, { score, terms, match }] of rawResults) { + // terms are the matched query terms, which will be returned to the user + // as queryTerms. The quality is calculated based on them, as opposed to + // the matched terms in the document (which can be different due to + // prefix and fuzzy match) + const quality = terms.length || 1; + const result = { + id: this._documentIds.get(docId), + score: score * quality, + terms: Object.keys(match), + queryTerms: terms, + match + }; + Object.assign(result, this._storedFields.get(docId)); + if (searchOptionsWithDefaults.filter == null || searchOptionsWithDefaults.filter(result)) { + results.push(result); + } + } + // If it's a wildcard query, and no document boost is applied, skip sorting + // the results, as all results have the same score of 1 + if (query === MiniSearch.wildcard && searchOptionsWithDefaults.boostDocument == null) { + return results; + } + results.sort(byScore); + return results; + } + /** + * Provide suggestions for the given search query + * + * The result is a list of suggested modified search queries, derived from the + * given search query, each with a relevance score, sorted by descending score. + * + * By default, it uses the same options used for search, except that by + * default it performs prefix search on the last term of the query, and + * combine terms with `'AND'` (requiring all query terms to match). Custom + * options can be passed as a second argument. Defaults can be changed upon + * calling the {@link MiniSearch} constructor, by passing a + * `autoSuggestOptions` option. + * + * ### Basic usage: + * + * ```javascript + * // Get suggestions for 'neuro': + * miniSearch.autoSuggest('neuro') + * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ] + * ``` + * + * ### Multiple words: + * + * ```javascript + * // Get suggestions for 'zen ar': + * miniSearch.autoSuggest('zen ar') + * // => [ + * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 }, + * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 } + * // ] + * ``` + * + * ### Fuzzy suggestions: + * + * ```javascript + * // Correct spelling mistakes using fuzzy search: + * miniSearch.autoSuggest('neromancer', { fuzzy: 0.2 }) + * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ] + * ``` + * + * ### Filtering: + * + * ```javascript + * // Get suggestions for 'zen ar', but only within the 'fiction' category + * // (assuming that 'category' is a stored field): + * miniSearch.autoSuggest('zen ar', { + * filter: (result) => result.category === 'fiction' + * }) + * // => [ + * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 }, + * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 } + * // ] + * ``` + * + * @param queryString Query string to be expanded into suggestions + * @param options Search options. The supported options and default values + * are the same as for the {@link MiniSearch#search} method, except that by + * default prefix search is performed on the last term in the query, and terms + * are combined with `'AND'`. + * @return A sorted array of suggestions sorted by relevance score. + */ + autoSuggest(queryString, options = {}) { + options = Object.assign(Object.assign({}, this._options.autoSuggestOptions), options); + const suggestions = new Map(); + for (const { score, terms } of this.search(queryString, options)) { + const phrase = terms.join(' '); + const suggestion = suggestions.get(phrase); + if (suggestion != null) { + suggestion.score += score; + suggestion.count += 1; + } + else { + suggestions.set(phrase, { score, terms, count: 1 }); + } + } + const results = []; + for (const [suggestion, { score, terms, count }] of suggestions) { + results.push({ suggestion, terms, score: score / count }); + } + results.sort(byScore); + return results; + } + /** + * Total number of documents available to search + */ + get documentCount() { + return this._documentCount; + } + /** + * Number of terms in the index + */ + get termCount() { + return this._index.size; + } + /** + * Deserializes a JSON index (serialized with `JSON.stringify(miniSearch)`) + * and instantiates a MiniSearch instance. It should be given the same options + * originally used when serializing the index. + * + * ### Usage: + * + * ```javascript + * // If the index was serialized with: + * let miniSearch = new MiniSearch({ fields: ['title', 'text'] }) + * miniSearch.addAll(documents) + * + * const json = JSON.stringify(miniSearch) + * // It can later be deserialized like this: + * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] }) + * ``` + * + * @param json JSON-serialized index + * @param options configuration options, same as the constructor + * @return An instance of MiniSearch deserialized from the given JSON. + */ + static loadJSON(json, options) { + if (options == null) { + throw new Error('MiniSearch: loadJSON should be given the same options used when serializing the index'); + } + return this.loadJS(JSON.parse(json), options); + } + /** + * Async equivalent of {@link MiniSearch.loadJSON} + * + * This function is an alternative to {@link MiniSearch.loadJSON} that returns + * a promise, and loads the index in batches, leaving pauses between them to avoid + * blocking the main thread. It tends to be slower than the synchronous + * version, but does not block the main thread, so it can be a better choice + * when deserializing very large indexes. + * + * @param json JSON-serialized index + * @param options configuration options, same as the constructor + * @return A Promise that will resolve to an instance of MiniSearch deserialized from the given JSON. + */ + static loadJSONAsync(json, options) { + return __awaiter(this, void 0, void 0, function* () { + if (options == null) { + throw new Error('MiniSearch: loadJSON should be given the same options used when serializing the index'); + } + return this.loadJSAsync(JSON.parse(json), options); + }); + } + /** + * Returns the default value of an option. It will throw an error if no option + * with the given name exists. + * + * @param optionName Name of the option + * @return The default value of the given option + * + * ### Usage: + * + * ```javascript + * // Get default tokenizer + * MiniSearch.getDefault('tokenize') + * + * // Get default term processor + * MiniSearch.getDefault('processTerm') + * + * // Unknown options will throw an error + * MiniSearch.getDefault('notExisting') + * // => throws 'MiniSearch: unknown option "notExisting"' + * ``` + */ + static getDefault(optionName) { + if (defaultOptions.hasOwnProperty(optionName)) { + return getOwnProperty(defaultOptions, optionName); + } + else { + throw new Error(`MiniSearch: unknown option "${optionName}"`); + } + } + /** + * @ignore + */ + static loadJS(js, options) { + const { index, documentIds, fieldLength, storedFields, serializationVersion } = js; + const miniSearch = this.instantiateMiniSearch(js, options); + miniSearch._documentIds = objectToNumericMap(documentIds); + miniSearch._fieldLength = objectToNumericMap(fieldLength); + miniSearch._storedFields = objectToNumericMap(storedFields); + for (const [shortId, id] of miniSearch._documentIds) { + miniSearch._idToShortId.set(id, shortId); + } + for (const [term, data] of index) { + const dataMap = new Map(); + for (const fieldId of Object.keys(data)) { + let indexEntry = data[fieldId]; + // Version 1 used to nest the index entry inside a field called ds + if (serializationVersion === 1) { + indexEntry = indexEntry.ds; + } + dataMap.set(parseInt(fieldId, 10), objectToNumericMap(indexEntry)); + } + miniSearch._index.set(term, dataMap); + } + return miniSearch; + } + /** + * @ignore + */ + static loadJSAsync(js, options) { + return __awaiter(this, void 0, void 0, function* () { + const { index, documentIds, fieldLength, storedFields, serializationVersion } = js; + const miniSearch = this.instantiateMiniSearch(js, options); + miniSearch._documentIds = yield objectToNumericMapAsync(documentIds); + miniSearch._fieldLength = yield objectToNumericMapAsync(fieldLength); + miniSearch._storedFields = yield objectToNumericMapAsync(storedFields); + for (const [shortId, id] of miniSearch._documentIds) { + miniSearch._idToShortId.set(id, shortId); + } + let count = 0; + for (const [term, data] of index) { + const dataMap = new Map(); + for (const fieldId of Object.keys(data)) { + let indexEntry = data[fieldId]; + // Version 1 used to nest the index entry inside a field called ds + if (serializationVersion === 1) { + indexEntry = indexEntry.ds; + } + dataMap.set(parseInt(fieldId, 10), yield objectToNumericMapAsync(indexEntry)); + } + if (++count % 1000 === 0) + yield wait(0); + miniSearch._index.set(term, dataMap); + } + return miniSearch; + }); + } + /** + * @ignore + */ + static instantiateMiniSearch(js, options) { + const { documentCount, nextId, fieldIds, averageFieldLength, dirtCount, serializationVersion } = js; + if (serializationVersion !== 1 && serializationVersion !== 2) { + throw new Error('MiniSearch: cannot deserialize an index created with an incompatible version'); + } + const miniSearch = new MiniSearch(options); + miniSearch._documentCount = documentCount; + miniSearch._nextId = nextId; + miniSearch._idToShortId = new Map(); + miniSearch._fieldIds = fieldIds; + miniSearch._avgFieldLength = averageFieldLength; + miniSearch._dirtCount = dirtCount || 0; + miniSearch._index = new SearchableMap(); + return miniSearch; + } + /** + * @ignore + */ + executeQuery(query, searchOptions = {}) { + if (query === MiniSearch.wildcard) { + return this.executeWildcardQuery(searchOptions); + } + if (typeof query !== 'string') { + const options = Object.assign(Object.assign(Object.assign({}, searchOptions), query), { queries: undefined }); + const results = query.queries.map((subquery) => this.executeQuery(subquery, options)); + return this.combineResults(results, options.combineWith); + } + const { tokenize, processTerm, searchOptions: globalSearchOptions } = this._options; + const options = Object.assign(Object.assign({ tokenize, processTerm }, globalSearchOptions), searchOptions); + const { tokenize: searchTokenize, processTerm: searchProcessTerm } = options; + const terms = searchTokenize(query) + .flatMap((term) => searchProcessTerm(term)) + .filter((term) => !!term); + const queries = terms.map(termToQuerySpec(options)); + const results = queries.map(query => this.executeQuerySpec(query, options)); + return this.combineResults(results, options.combineWith); + } + /** + * @ignore + */ + executeQuerySpec(query, searchOptions) { + const options = Object.assign(Object.assign({}, this._options.searchOptions), searchOptions); + const boosts = (options.fields || this._options.fields).reduce((boosts, field) => (Object.assign(Object.assign({}, boosts), { [field]: getOwnProperty(options.boost, field) || 1 })), {}); + const { boostDocument, weights, maxFuzzy, bm25: bm25params } = options; + const { fuzzy: fuzzyWeight, prefix: prefixWeight } = Object.assign(Object.assign({}, defaultSearchOptions.weights), weights); + const data = this._index.get(query.term); + const results = this.termResults(query.term, query.term, 1, query.termBoost, data, boosts, boostDocument, bm25params); + let prefixMatches; + let fuzzyMatches; + if (query.prefix) { + prefixMatches = this._index.atPrefix(query.term); + } + if (query.fuzzy) { + const fuzzy = (query.fuzzy === true) ? 0.2 : query.fuzzy; + const maxDistance = fuzzy < 1 ? Math.min(maxFuzzy, Math.round(query.term.length * fuzzy)) : fuzzy; + if (maxDistance) + fuzzyMatches = this._index.fuzzyGet(query.term, maxDistance); + } + if (prefixMatches) { + for (const [term, data] of prefixMatches) { + const distance = term.length - query.term.length; + if (!distance) { + continue; + } // Skip exact match. + // Delete the term from fuzzy results (if present) if it is also a + // prefix result. This entry will always be scored as a prefix result. + fuzzyMatches === null || fuzzyMatches === void 0 ? void 0 : fuzzyMatches.delete(term); + // Weight gradually approaches 0 as distance goes to infinity, with the + // weight for the hypothetical distance 0 being equal to prefixWeight. + // The rate of change is much lower than that of fuzzy matches to + // account for the fact that prefix matches stay more relevant than + // fuzzy matches for longer distances. + const weight = prefixWeight * term.length / (term.length + 0.3 * distance); + this.termResults(query.term, term, weight, query.termBoost, data, boosts, boostDocument, bm25params, results); + } + } + if (fuzzyMatches) { + for (const term of fuzzyMatches.keys()) { + const [data, distance] = fuzzyMatches.get(term); + if (!distance) { + continue; + } // Skip exact match. + // Weight gradually approaches 0 as distance goes to infinity, with the + // weight for the hypothetical distance 0 being equal to fuzzyWeight. + const weight = fuzzyWeight * term.length / (term.length + distance); + this.termResults(query.term, term, weight, query.termBoost, data, boosts, boostDocument, bm25params, results); + } + } + return results; + } + /** + * @ignore + */ + executeWildcardQuery(searchOptions) { + const results = new Map(); + const options = Object.assign(Object.assign({}, this._options.searchOptions), searchOptions); + for (const [shortId, id] of this._documentIds) { + const score = options.boostDocument ? options.boostDocument(id, '', this._storedFields.get(shortId)) : 1; + results.set(shortId, { + score, + terms: [], + match: {} + }); + } + return results; + } + /** + * @ignore + */ + combineResults(results, combineWith = OR) { + if (results.length === 0) { + return new Map(); + } + const operator = combineWith.toLowerCase(); + const combinator = combinators[operator]; + if (!combinator) { + throw new Error(`Invalid combination operator: ${combineWith}`); + } + return results.reduce(combinator) || new Map(); + } + /** + * Allows serialization of the index to JSON, to possibly store it and later + * deserialize it with {@link MiniSearch.loadJSON}. + * + * Normally one does not directly call this method, but rather call the + * standard JavaScript `JSON.stringify()` passing the {@link MiniSearch} + * instance, and JavaScript will internally call this method. Upon + * deserialization, one must pass to {@link MiniSearch.loadJSON} the same + * options used to create the original instance that was serialized. + * + * ### Usage: + * + * ```javascript + * // Serialize the index: + * let miniSearch = new MiniSearch({ fields: ['title', 'text'] }) + * miniSearch.addAll(documents) + * const json = JSON.stringify(miniSearch) + * + * // Later, to deserialize it: + * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] }) + * ``` + * + * @return A plain-object serializable representation of the search index. + */ + toJSON() { + const index = []; + for (const [term, fieldIndex] of this._index) { + const data = {}; + for (const [fieldId, freqs] of fieldIndex) { + data[fieldId] = Object.fromEntries(freqs); + } + index.push([term, data]); + } + return { + documentCount: this._documentCount, + nextId: this._nextId, + documentIds: Object.fromEntries(this._documentIds), + fieldIds: this._fieldIds, + fieldLength: Object.fromEntries(this._fieldLength), + averageFieldLength: this._avgFieldLength, + storedFields: Object.fromEntries(this._storedFields), + dirtCount: this._dirtCount, + index, + serializationVersion: 2 + }; + } + /** + * @ignore + */ + termResults(sourceTerm, derivedTerm, termWeight, termBoost, fieldTermData, fieldBoosts, boostDocumentFn, bm25params, results = new Map()) { + if (fieldTermData == null) + return results; + for (const field of Object.keys(fieldBoosts)) { + const fieldBoost = fieldBoosts[field]; + const fieldId = this._fieldIds[field]; + const fieldTermFreqs = fieldTermData.get(fieldId); + if (fieldTermFreqs == null) + continue; + let matchingFields = fieldTermFreqs.size; + const avgFieldLength = this._avgFieldLength[fieldId]; + for (const docId of fieldTermFreqs.keys()) { + if (!this._documentIds.has(docId)) { + this.removeTerm(fieldId, docId, derivedTerm); + matchingFields -= 1; + continue; + } + const docBoost = boostDocumentFn ? boostDocumentFn(this._documentIds.get(docId), derivedTerm, this._storedFields.get(docId)) : 1; + if (!docBoost) + continue; + const termFreq = fieldTermFreqs.get(docId); + const fieldLength = this._fieldLength.get(docId)[fieldId]; + // NOTE: The total number of fields is set to the number of documents + // `this._documentCount`. It could also make sense to use the number of + // documents where the current field is non-blank as a normalization + // factor. This will make a difference in scoring if the field is rarely + // present. This is currently not supported, and may require further + // analysis to see if it is a valid use case. + const rawScore = calcBM25Score(termFreq, matchingFields, this._documentCount, fieldLength, avgFieldLength, bm25params); + const weightedScore = termWeight * termBoost * fieldBoost * docBoost * rawScore; + const result = results.get(docId); + if (result) { + result.score += weightedScore; + assignUniqueTerm(result.terms, sourceTerm); + const match = getOwnProperty(result.match, derivedTerm); + if (match) { + match.push(field); + } + else { + result.match[derivedTerm] = [field]; + } + } + else { + results.set(docId, { + score: weightedScore, + terms: [sourceTerm], + match: { [derivedTerm]: [field] } + }); + } + } + } + return results; + } + /** + * @ignore + */ + addTerm(fieldId, documentId, term) { + const indexData = this._index.fetch(term, createMap); + let fieldIndex = indexData.get(fieldId); + if (fieldIndex == null) { + fieldIndex = new Map(); + fieldIndex.set(documentId, 1); + indexData.set(fieldId, fieldIndex); + } + else { + const docs = fieldIndex.get(documentId); + fieldIndex.set(documentId, (docs || 0) + 1); + } + } + /** + * @ignore + */ + removeTerm(fieldId, documentId, term) { + if (!this._index.has(term)) { + this.warnDocumentChanged(documentId, fieldId, term); + return; + } + const indexData = this._index.fetch(term, createMap); + const fieldIndex = indexData.get(fieldId); + if (fieldIndex == null || fieldIndex.get(documentId) == null) { + this.warnDocumentChanged(documentId, fieldId, term); + } + else if (fieldIndex.get(documentId) <= 1) { + if (fieldIndex.size <= 1) { + indexData.delete(fieldId); + } + else { + fieldIndex.delete(documentId); + } + } + else { + fieldIndex.set(documentId, fieldIndex.get(documentId) - 1); + } + if (this._index.get(term).size === 0) { + this._index.delete(term); + } + } + /** + * @ignore + */ + warnDocumentChanged(shortDocumentId, fieldId, term) { + for (const fieldName of Object.keys(this._fieldIds)) { + if (this._fieldIds[fieldName] === fieldId) { + this._options.logger('warn', `MiniSearch: document with ID ${this._documentIds.get(shortDocumentId)} has changed before removal: term "${term}" was not present in field "${fieldName}". Removing a document after it has changed can corrupt the index!`, 'version_conflict'); + return; + } + } + } + /** + * @ignore + */ + addDocumentId(documentId) { + const shortDocumentId = this._nextId; + this._idToShortId.set(documentId, shortDocumentId); + this._documentIds.set(shortDocumentId, documentId); + this._documentCount += 1; + this._nextId += 1; + return shortDocumentId; + } + /** + * @ignore + */ + addFields(fields) { + for (let i = 0; i < fields.length; i++) { + this._fieldIds[fields[i]] = i; + } + } + /** + * @ignore + */ + addFieldLength(documentId, fieldId, count, length) { + let fieldLengths = this._fieldLength.get(documentId); + if (fieldLengths == null) + this._fieldLength.set(documentId, fieldLengths = []); + fieldLengths[fieldId] = length; + const averageFieldLength = this._avgFieldLength[fieldId] || 0; + const totalFieldLength = (averageFieldLength * count) + length; + this._avgFieldLength[fieldId] = totalFieldLength / (count + 1); + } + /** + * @ignore + */ + removeFieldLength(documentId, fieldId, count, length) { + if (count === 1) { + this._avgFieldLength[fieldId] = 0; + return; + } + const totalFieldLength = (this._avgFieldLength[fieldId] * count) - length; + this._avgFieldLength[fieldId] = totalFieldLength / (count - 1); + } + /** + * @ignore + */ + saveStoredFields(documentId, doc) { + const { storeFields, extractField } = this._options; + if (storeFields == null || storeFields.length === 0) { + return; + } + let documentFields = this._storedFields.get(documentId); + if (documentFields == null) + this._storedFields.set(documentId, documentFields = {}); + for (const fieldName of storeFields) { + const fieldValue = extractField(doc, fieldName); + if (fieldValue !== undefined) + documentFields[fieldName] = fieldValue; + } + } +} +/** + * The special wildcard symbol that can be passed to {@link MiniSearch#search} + * to match all documents + */ +MiniSearch.wildcard = Symbol('*'); +const getOwnProperty = (object, property) => Object.prototype.hasOwnProperty.call(object, property) ? object[property] : undefined; +const combinators = { + [OR]: (a, b) => { + for (const docId of b.keys()) { + const existing = a.get(docId); + if (existing == null) { + a.set(docId, b.get(docId)); + } + else { + const { score, terms, match } = b.get(docId); + existing.score = existing.score + score; + existing.match = Object.assign(existing.match, match); + assignUniqueTerms(existing.terms, terms); + } + } + return a; + }, + [AND]: (a, b) => { + const combined = new Map(); + for (const docId of b.keys()) { + const existing = a.get(docId); + if (existing == null) + continue; + const { score, terms, match } = b.get(docId); + assignUniqueTerms(existing.terms, terms); + combined.set(docId, { + score: existing.score + score, + terms: existing.terms, + match: Object.assign(existing.match, match) + }); + } + return combined; + }, + [AND_NOT]: (a, b) => { + for (const docId of b.keys()) + a.delete(docId); + return a; + } +}; +const defaultBM25params = { k: 1.2, b: 0.7, d: 0.5 }; +const calcBM25Score = (termFreq, matchingCount, totalCount, fieldLength, avgFieldLength, bm25params) => { + const { k, b, d } = bm25params; + const invDocFreq = Math.log(1 + (totalCount - matchingCount + 0.5) / (matchingCount + 0.5)); + return invDocFreq * (d + termFreq * (k + 1) / (termFreq + k * (1 - b + b * fieldLength / avgFieldLength))); +}; +const termToQuerySpec = (options) => (term, i, terms) => { + const fuzzy = (typeof options.fuzzy === 'function') + ? options.fuzzy(term, i, terms) + : (options.fuzzy || false); + const prefix = (typeof options.prefix === 'function') + ? options.prefix(term, i, terms) + : (options.prefix === true); + const termBoost = (typeof options.boostTerm === 'function') + ? options.boostTerm(term, i, terms) + : 1; + return { term, fuzzy, prefix, termBoost }; +}; +const defaultOptions = { + idField: 'id', + extractField: (document, fieldName) => document[fieldName], + tokenize: (text) => text.split(SPACE_OR_PUNCTUATION), + processTerm: (term) => term.toLowerCase(), + fields: undefined, + searchOptions: undefined, + storeFields: [], + logger: (level, message) => { + if (typeof (console === null || console === void 0 ? void 0 : console[level]) === 'function') + console[level](message); + }, + autoVacuum: true +}; +const defaultSearchOptions = { + combineWith: OR, + prefix: false, + fuzzy: false, + maxFuzzy: 6, + boost: {}, + weights: { fuzzy: 0.45, prefix: 0.375 }, + bm25: defaultBM25params +}; +const defaultAutoSuggestOptions = { + combineWith: AND, + prefix: (term, i, terms) => i === terms.length - 1 +}; +const defaultVacuumOptions = { batchSize: 1000, batchWait: 10 }; +const defaultVacuumConditions = { minDirtFactor: 0.1, minDirtCount: 20 }; +const defaultAutoVacuumOptions = Object.assign(Object.assign({}, defaultVacuumOptions), defaultVacuumConditions); +const assignUniqueTerm = (target, term) => { + // Avoid adding duplicate terms. + if (!target.includes(term)) + target.push(term); +}; +const assignUniqueTerms = (target, source) => { + for (const term of source) { + // Avoid adding duplicate terms. + if (!target.includes(term)) + target.push(term); + } +}; +const byScore = ({ score: a }, { score: b }) => b - a; +const createMap = () => new Map(); +const objectToNumericMap = (object) => { + const map = new Map(); + for (const key of Object.keys(object)) { + map.set(parseInt(key, 10), object[key]); + } + return map; +}; +const objectToNumericMapAsync = (object) => __awaiter(void 0, void 0, void 0, function* () { + const map = new Map(); + let count = 0; + for (const key of Object.keys(object)) { + map.set(parseInt(key, 10), object[key]); + if (++count % 1000 === 0) { + yield wait(0); + } + } + return map; +}); +const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); +// This regular expression matches any Unicode space, newline, or punctuation +// character +const SPACE_OR_PUNCTUATION = /[\n\r\p{Z}\p{P}]+/u; + +export { MiniSearch as default }; +//# sourceMappingURL=index.js.map diff --git a/modules/story-summary/data/config.js b/modules/story-summary/data/config.js new file mode 100644 index 0000000..3f11e11 --- /dev/null +++ b/modules/story-summary/data/config.js @@ -0,0 +1,97 @@ +// Story Summary - Config +// Plugin settings, panel config, and vector config. + +import { extension_settings } from "../../../../../../extensions.js"; +import { EXT_ID } from "../../../core/constants.js"; +import { xbLog } from "../../../core/debug-core.js"; +import { CommonSettingStorage } from "../../../core/server-storage.js"; + +const MODULE_ID = 'summaryConfig'; +const SUMMARY_CONFIG_KEY = 'storySummaryPanelConfig'; + +export function getSettings() { + const ext = extension_settings[EXT_ID] ||= {}; + ext.storySummary ||= { enabled: true }; + return ext; +} + +export function getSummaryPanelConfig() { + const defaults = { + api: { provider: 'st', url: '', key: '', model: '', modelCache: [] }, + gen: { temperature: null, top_p: null, top_k: null, presence_penalty: null, frequency_penalty: null }, + trigger: { + enabled: false, + interval: 20, + timing: 'after_ai', + useStream: true, + maxPerRun: 100, + wrapperHead: '', + wrapperTail: '', + forceInsertAtEnd: false, + }, + }; + try { + const raw = localStorage.getItem('summary_panel_config'); + if (!raw) return defaults; + const parsed = JSON.parse(raw); + + const result = { + api: { ...defaults.api, ...(parsed.api || {}) }, + gen: { ...defaults.gen, ...(parsed.gen || {}) }, + trigger: { ...defaults.trigger, ...(parsed.trigger || {}) }, + }; + + if (result.trigger.timing === 'manual') result.trigger.enabled = false; + if (result.trigger.useStream === undefined) result.trigger.useStream = true; + + return result; + } catch { + return defaults; + } +} + +export function saveSummaryPanelConfig(config) { + try { + localStorage.setItem('summary_panel_config', JSON.stringify(config)); + CommonSettingStorage.set(SUMMARY_CONFIG_KEY, config); + } catch (e) { + xbLog.error(MODULE_ID, '保存面板配置失败', e); + } +} + +export function getVectorConfig() { + try { + const raw = localStorage.getItem('summary_panel_config'); + if (!raw) return null; + const parsed = JSON.parse(raw); + return parsed.vector || null; + } catch { + return null; + } +} + +export function saveVectorConfig(vectorCfg) { + try { + const raw = localStorage.getItem('summary_panel_config') || '{}'; + const parsed = JSON.parse(raw); + parsed.vector = vectorCfg; + localStorage.setItem('summary_panel_config', JSON.stringify(parsed)); + CommonSettingStorage.set(SUMMARY_CONFIG_KEY, parsed); + } catch (e) { + xbLog.error(MODULE_ID, '保存向量配置失败', e); + } +} + +export async function loadConfigFromServer() { + try { + const savedConfig = await CommonSettingStorage.get(SUMMARY_CONFIG_KEY, null); + if (savedConfig) { + localStorage.setItem('summary_panel_config', JSON.stringify(savedConfig)); + xbLog.info(MODULE_ID, '已从服务器加载面板配置'); + return savedConfig; + } + } catch (e) { + xbLog.warn(MODULE_ID, '加载面板配置失败', e); + } + return null; +} diff --git a/modules/story-summary/data/db.js b/modules/story-summary/data/db.js new file mode 100644 index 0000000..bf10720 --- /dev/null +++ b/modules/story-summary/data/db.js @@ -0,0 +1,24 @@ +// Memory Database (Dexie schema) + +import Dexie from '../../../libs/dexie.mjs'; + +const DB_NAME = 'LittleWhiteBox_Memory'; +const DB_VERSION = 2; + +// Chunk parameters +export const CHUNK_MAX_TOKENS = 200; + +const db = new Dexie(DB_NAME); + +db.version(DB_VERSION).stores({ + meta: 'chatId', + chunks: '[chatId+chunkId], chatId, [chatId+floor]', + chunkVectors: '[chatId+chunkId], chatId', + eventVectors: '[chatId+eventId], chatId', +}); + +export { db }; +export const metaTable = db.meta; +export const chunksTable = db.chunks; +export const chunkVectorsTable = db.chunkVectors; +export const eventVectorsTable = db.eventVectors; diff --git a/modules/story-summary/data/store.js b/modules/story-summary/data/store.js new file mode 100644 index 0000000..2af2404 --- /dev/null +++ b/modules/story-summary/data/store.js @@ -0,0 +1,288 @@ +// Story Summary - Store +// L2 (events/characters/arcs) + L3 (world) 统一存储 + +import { getContext, saveMetadataDebounced } from "../../../../../../extensions.js"; +import { chat_metadata } from "../../../../../../../script.js"; +import { EXT_ID } from "../../../core/constants.js"; +import { xbLog } from "../../../core/debug-core.js"; +import { clearEventVectors, deleteEventVectorsByIds } from "../vector/chunk-store.js"; + +const MODULE_ID = 'summaryStore'; + +// ═══════════════════════════════════════════════════════════════════════════ +// 基础存取 +// ═══════════════════════════════════════════════════════════════════════════ + +export function getSummaryStore() { + const { chatId } = getContext(); + if (!chatId) return null; + chat_metadata.extensions ||= {}; + chat_metadata.extensions[EXT_ID] ||= {}; + chat_metadata.extensions[EXT_ID].storySummary ||= {}; + return chat_metadata.extensions[EXT_ID].storySummary; +} + +export function saveSummaryStore() { + saveMetadataDebounced?.(); +} + +export function getKeepVisibleCount() { + const store = getSummaryStore(); + return store?.keepVisibleCount ?? 3; +} + +export function calcHideRange(lastSummarized) { + const keepCount = getKeepVisibleCount(); + const hideEnd = lastSummarized - keepCount; + if (hideEnd < 0) return null; + return { start: 0, end: hideEnd }; +} + +export function addSummarySnapshot(store, endMesId) { + store.summaryHistory ||= []; + store.summaryHistory.push({ endMesId }); +} + +// ═══════════════════════════════════════════════════════════════════════════ +// L3 世界状态合并 +// ═══════════════════════════════════════════════════════════════════════════ + +export function mergeWorldState(existingList, updates, floor) { + const map = new Map(); + + (existingList || []).forEach(item => { + const key = `${item.category}:${item.topic}`; + map.set(key, item); + }); + + (updates || []).forEach(up => { + if (!up.category || !up.topic) return; + + const key = `${up.category}:${up.topic}`; + + if (up.cleared === true) { + map.delete(key); + return; + } + + const content = up.content?.trim(); + if (!content) return; + + map.set(key, { + category: up.category, + topic: up.topic, + content: content, + floor: floor, + _addedAt: floor, + }); + }); + + return Array.from(map.values()); +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 数据合并(L2 + L3) +// ═══════════════════════════════════════════════════════════════════════════ + +export function mergeNewData(oldJson, parsed, endMesId) { + const merged = structuredClone(oldJson || {}); + + // L2 初始化 + merged.keywords ||= []; + merged.events ||= []; + merged.characters ||= {}; + merged.characters.main ||= []; + merged.characters.relationships ||= []; + merged.arcs ||= []; + + // L3 初始化 + merged.world ||= []; + + // L2 数据合并 + if (parsed.keywords?.length) { + merged.keywords = parsed.keywords.map(k => ({ ...k, _addedAt: endMesId })); + } + + (parsed.events || []).forEach(e => { + e._addedAt = endMesId; + merged.events.push(e); + }); + + const existingMain = new Set( + (merged.characters.main || []).map(m => typeof m === 'string' ? m : m.name) + ); + (parsed.newCharacters || []).forEach(name => { + if (!existingMain.has(name)) { + merged.characters.main.push({ name, _addedAt: endMesId }); + } + }); + + const relMap = new Map( + (merged.characters.relationships || []).map(r => [`${r.from}->${r.to}`, r]) + ); + (parsed.newRelationships || []).forEach(r => { + const key = `${r.from}->${r.to}`; + const existing = relMap.get(key); + if (existing) { + existing.label = r.label; + existing.trend = r.trend; + } else { + r._addedAt = endMesId; + relMap.set(key, r); + } + }); + merged.characters.relationships = Array.from(relMap.values()); + + const arcMap = new Map((merged.arcs || []).map(a => [a.name, a])); + (parsed.arcUpdates || []).forEach(update => { + const existing = arcMap.get(update.name); + if (existing) { + existing.trajectory = update.trajectory; + existing.progress = update.progress; + if (update.newMoment) { + existing.moments = existing.moments || []; + existing.moments.push({ text: update.newMoment, _addedAt: endMesId }); + } + } else { + arcMap.set(update.name, { + name: update.name, + trajectory: update.trajectory, + progress: update.progress, + moments: update.newMoment ? [{ text: update.newMoment, _addedAt: endMesId }] : [], + _addedAt: endMesId, + }); + } + }); + merged.arcs = Array.from(arcMap.values()); + + // L3 世界状态合并 + merged.world = mergeWorldState( + merged.world || [], + parsed.worldUpdate || [], + endMesId + ); + + return merged; +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 回滚 +// ═══════════════════════════════════════════════════════════════════════════ + +export async function rollbackSummaryIfNeeded() { + const { chat, chatId } = getContext(); + const currentLength = Array.isArray(chat) ? chat.length : 0; + const store = getSummaryStore(); + + if (!store || store.lastSummarizedMesId == null || store.lastSummarizedMesId < 0) { + return false; + } + + const lastSummarized = store.lastSummarizedMesId; + + if (currentLength <= lastSummarized) { + const deletedCount = lastSummarized + 1 - currentLength; + + if (deletedCount < 2) { + return false; + } + + xbLog.warn(MODULE_ID, `删除已总结楼层 ${deletedCount} 条,触发回滚`); + + const history = store.summaryHistory || []; + let targetEndMesId = -1; + + for (let i = history.length - 1; i >= 0; i--) { + if (history[i].endMesId < currentLength) { + targetEndMesId = history[i].endMesId; + break; + } + } + + await executeRollback(chatId, store, targetEndMesId, currentLength); + return true; + } + + return false; +} + +export async function executeRollback(chatId, store, targetEndMesId, currentLength) { + const oldEvents = store.json?.events || []; + + if (targetEndMesId < 0) { + store.lastSummarizedMesId = -1; + store.json = null; + store.summaryHistory = []; + store.hideSummarizedHistory = false; + + await clearEventVectors(chatId); + + } else { + const deletedEventIds = oldEvents + .filter(e => (e._addedAt ?? 0) > targetEndMesId) + .map(e => e.id); + + const json = store.json || {}; + + // L2 回滚 + json.events = (json.events || []).filter(e => (e._addedAt ?? 0) <= targetEndMesId); + json.keywords = (json.keywords || []).filter(k => (k._addedAt ?? 0) <= targetEndMesId); + json.arcs = (json.arcs || []).filter(a => (a._addedAt ?? 0) <= targetEndMesId); + json.arcs.forEach(a => { + a.moments = (a.moments || []).filter(m => + typeof m === 'string' || (m._addedAt ?? 0) <= targetEndMesId + ); + }); + + if (json.characters) { + json.characters.main = (json.characters.main || []).filter(m => + typeof m === 'string' || (m._addedAt ?? 0) <= targetEndMesId + ); + json.characters.relationships = (json.characters.relationships || []).filter(r => + (r._addedAt ?? 0) <= targetEndMesId + ); + } + + // L3 回滚 + json.world = (json.world || []).filter(w => (w._addedAt ?? 0) <= targetEndMesId); + + store.json = json; + store.lastSummarizedMesId = targetEndMesId; + store.summaryHistory = (store.summaryHistory || []).filter(h => h.endMesId <= targetEndMesId); + + if (deletedEventIds.length > 0) { + await deleteEventVectorsByIds(chatId, deletedEventIds); + xbLog.info(MODULE_ID, `回滚删除 ${deletedEventIds.length} 个事件向量`); + } + } + + store.updatedAt = Date.now(); + saveSummaryStore(); + + xbLog.info(MODULE_ID, `回滚完成,目标楼层: ${targetEndMesId}`); +} + +export async function clearSummaryData(chatId) { + const store = getSummaryStore(); + if (store) { + delete store.json; + store.lastSummarizedMesId = -1; + store.updatedAt = Date.now(); + saveSummaryStore(); + } + + if (chatId) { + await clearEventVectors(chatId); + } + + xbLog.info(MODULE_ID, '总结数据已清空'); +} + +// ═══════════════════════════════════════════════════════════════════════════ +// L3 数据读取(供 prompt.js 使用) +// ═══════════════════════════════════════════════════════════════════════════ + +export function getWorldSnapshot() { + const store = getSummaryStore(); + return store?.json?.world || []; +} \ No newline at end of file diff --git a/modules/story-summary/generate/generator.js b/modules/story-summary/generate/generator.js new file mode 100644 index 0000000..de22c56 --- /dev/null +++ b/modules/story-summary/generate/generator.js @@ -0,0 +1,208 @@ +// Story Summary - Generator +// 调用 LLM 生成总结 + +import { getContext } from "../../../../../../extensions.js"; +import { xbLog } from "../../../core/debug-core.js"; +import { getSummaryStore, saveSummaryStore, addSummarySnapshot, mergeNewData } from "../data/store.js"; +import { generateSummary, parseSummaryJson } from "./llm.js"; + +const MODULE_ID = 'summaryGenerator'; +const SUMMARY_SESSION_ID = 'xb9'; + +// ═══════════════════════════════════════════════════════════════════════════ +// worldUpdate 清洗 +// ═══════════════════════════════════════════════════════════════════════════ + +function sanitizeWorldUpdate(parsed) { + if (!parsed) return; + + const wu = Array.isArray(parsed.worldUpdate) ? parsed.worldUpdate : []; + const ok = []; + + for (const item of wu) { + const category = String(item?.category || '').trim().toLowerCase(); + const topic = String(item?.topic || '').trim(); + + if (!category || !topic) continue; + + // status/knowledge/relation 必须包含 "::" + if (['status', 'knowledge', 'relation'].includes(category) && !topic.includes('::')) { + xbLog.warn(MODULE_ID, `丢弃不合格 worldUpdate: ${category}/${topic}`); + continue; + } + + if (item.cleared === true) { + ok.push({ category, topic, cleared: true }); + continue; + } + + const content = String(item?.content || '').trim(); + if (!content) continue; + + ok.push({ category, topic, content }); + } + + parsed.worldUpdate = ok; +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 辅助函数 +// ═══════════════════════════════════════════════════════════════════════════ + +export function formatExistingSummaryForAI(store) { + if (!store?.json) return "(空白,这是首次总结)"; + + const data = store.json; + const parts = []; + + if (data.events?.length) { + parts.push("【已记录事件】"); + data.events.forEach((ev, i) => parts.push(`${i + 1}. [${ev.timeLabel}] ${ev.title}:${ev.summary}`)); + } + + if (data.characters?.main?.length) { + const names = data.characters.main.map(m => typeof m === 'string' ? m : m.name); + parts.push(`\n【主要角色】${names.join("、")}`); + } + + if (data.characters?.relationships?.length) { + parts.push("【人物关系】"); + data.characters.relationships.forEach(r => parts.push(`- ${r.from} → ${r.to}:${r.label}(${r.trend})`)); + } + + if (data.arcs?.length) { + parts.push("【角色弧光】"); + data.arcs.forEach(a => parts.push(`- ${a.name}:${a.trajectory}(进度${Math.round(a.progress * 100)}%)`)); + } + + if (data.keywords?.length) { + parts.push(`\n【关键词】${data.keywords.map(k => k.text).join("、")}`); + } + + return parts.join("\n") || "(空白,这是首次总结)"; +} + +export function getNextEventId(store) { + const events = store?.json?.events || []; + if (!events.length) return 1; + + const maxId = Math.max(...events.map(e => { + const match = e.id?.match(/evt-(\d+)/); + return match ? parseInt(match[1]) : 0; + })); + + return maxId + 1; +} + +export function buildIncrementalSlice(targetMesId, lastSummarizedMesId, maxPerRun = 100) { + const { chat, name1, name2 } = getContext(); + + const start = Math.max(0, (lastSummarizedMesId ?? -1) + 1); + const rawEnd = Math.min(targetMesId, chat.length - 1); + const end = Math.min(rawEnd, start + maxPerRun - 1); + + if (start > end) return { text: "", count: 0, range: "", endMesId: -1 }; + + const userLabel = name1 || '用户'; + const charLabel = name2 || '角色'; + const slice = chat.slice(start, end + 1); + + const text = slice.map((m, i) => { + const speaker = m.name || (m.is_user ? userLabel : charLabel); + return `#${start + i + 1} 【${speaker}】\n${m.mes}`; + }).join('\n\n'); + + return { text, count: slice.length, range: `${start + 1}-${end + 1}楼`, endMesId: end }; +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 主生成函数 +// ═══════════════════════════════════════════════════════════════════════════ + +export async function runSummaryGeneration(mesId, config, callbacks = {}) { + const { onStatus, onError, onComplete } = callbacks; + + const store = getSummaryStore(); + const lastSummarized = store?.lastSummarizedMesId ?? -1; + const maxPerRun = config.trigger?.maxPerRun || 100; + const slice = buildIncrementalSlice(mesId, lastSummarized, maxPerRun); + + if (slice.count === 0) { + onStatus?.("没有新的对话需要总结"); + return { success: true, noContent: true }; + } + + onStatus?.(`正在总结 ${slice.range}(${slice.count}楼新内容)...`); + + const existingSummary = formatExistingSummaryForAI(store); + const existingWorld = store?.json?.world || []; + const nextEventId = getNextEventId(store); + const existingEventCount = store?.json?.events?.length || 0; + const useStream = config.trigger?.useStream !== false; + + let raw; + try { + raw = await generateSummary({ + existingSummary, + existingWorld, + newHistoryText: slice.text, + historyRange: slice.range, + nextEventId, + existingEventCount, + llmApi: { + provider: config.api?.provider, + url: config.api?.url, + key: config.api?.key, + model: config.api?.model, + }, + genParams: config.gen || {}, + useStream, + timeout: 120000, + sessionId: SUMMARY_SESSION_ID, + }); + } catch (err) { + xbLog.error(MODULE_ID, '生成失败', err); + onError?.(err?.message || "生成失败"); + return { success: false, error: err }; + } + + if (!raw?.trim()) { + xbLog.error(MODULE_ID, 'AI返回为空'); + onError?.("AI返回为空"); + return { success: false, error: "empty" }; + } + + const parsed = parseSummaryJson(raw); + if (!parsed) { + xbLog.error(MODULE_ID, 'JSON解析失败'); + onError?.("AI未返回有效JSON"); + return { success: false, error: "parse" }; + } + + sanitizeWorldUpdate(parsed); + + const merged = mergeNewData(store?.json || {}, parsed, slice.endMesId); + + store.lastSummarizedMesId = slice.endMesId; + store.json = merged; + store.updatedAt = Date.now(); + addSummarySnapshot(store, slice.endMesId); + saveSummaryStore(); + + xbLog.info(MODULE_ID, `总结完成,已更新至 ${slice.endMesId + 1} 楼`); + + if (parsed.worldUpdate?.length) { + xbLog.info(MODULE_ID, `世界状态更新: ${parsed.worldUpdate.length} 条`); + } + + const newEventIds = (parsed.events || []).map(e => e.id); + + onComplete?.({ + merged, + endMesId: slice.endMesId, + newEventIds, + l3Stats: { worldUpdate: parsed.worldUpdate?.length || 0 }, + }); + + return { success: true, merged, endMesId: slice.endMesId, newEventIds }; +} diff --git a/modules/story-summary/llm-service.js b/modules/story-summary/generate/llm.js similarity index 75% rename from modules/story-summary/llm-service.js rename to modules/story-summary/generate/llm.js index 7539d2b..8783b86 100644 --- a/modules/story-summary/llm-service.js +++ b/modules/story-summary/generate/llm.js @@ -1,10 +1,4 @@ -// ═══════════════════════════════════════════════════════════════════════════ -// Story Summary - LLM Service -// ═══════════════════════════════════════════════════════════════════════════ - -// ═══════════════════════════════════════════════════════════════════════════ -// 常量 -// ═══════════════════════════════════════════════════════════════════════════ +// LLM Service const PROVIDER_MAP = { openai: "openai", @@ -43,27 +37,35 @@ Incremental_Summary_Requirements: - 氛围: 纯粹氛围片段 - Character_Dynamics: 识别新角色,追踪关系趋势(破裂/厌恶/反感/陌生/投缘/亲密/交融) - Arc_Tracking: 更新角色弧光轨迹与成长进度(0.0-1.0) + - World_State_Tracking: 维护当前世界的硬性约束。解决"什么不能违反"。采用 KV 覆盖模型,追踪生死、物品归属、秘密知情、关系状态、环境规则等不可违背的事实。(覆盖式更新) + categories: + - status: 角色生死、位置锁定、重大状态 + - inventory: 重要物品归属 + - knowledge: 秘密的知情状态 + - relation: 硬性关系(在一起/决裂) + - rule: 环境规则/契约限制 --- Story Analyst: [Responsibility Definition] \`\`\`yaml analysis_task: - title: Incremental Story Summarization + title: Incremental Story Summarization with World State Story Analyst: role: Antigravity task: >- To analyze provided dialogue content against existing summary state, extract only NEW plot elements, character developments, relationship - changes, and arc progressions, outputting structured JSON for - incremental summary database updates. + changes, arc progressions, AND world state changes, outputting + structured JSON for incremental summary database updates. assistant: role: Summary Specialist - description: Incremental Story Summary Analyst + description: Incremental Story Summary & World State Analyst behavior: >- To compare new dialogue against existing summary, identify genuinely new events and character interactions, classify events by narrative type and weight, track character arc progression with percentage, + maintain world state as key-value updates with clear flags, and output structured JSON containing only incremental updates. Must strictly avoid repeating any existing summary content. user: @@ -71,7 +73,7 @@ analysis_task: description: Supplies existing summary state and new dialogue behavior: >- To provide existing summary state (events, characters, relationships, - arcs) and new dialogue content for incremental analysis. + arcs, world state) and new dialogue content for incremental analysis. interaction_mode: type: incremental_analysis output_format: structured_json @@ -80,6 +82,7 @@ execution_context: summary_active: true incremental_only: true memory_album_style: true + world_state_tracking: true \`\`\` --- Summary Specialist: @@ -102,6 +105,12 @@ Acknowledged. Now reviewing the incremental summarization specifications: ├─ progress: 0.0 to 1.0 └─ newMoment: 仅记录本次新增的关键时刻 +[World State Maintenance] +├─ 维护方式: Key-Value 覆盖(category + topic 为键) +├─ 只输出有变化的条目 +├─ 清除时使用 cleared: true,不要填 content +└─ 不记录情绪、衣着、临时动作 + Ready to process incremental summary requests with strict deduplication.`, assistantAskSummary: ` @@ -110,7 +119,8 @@ Specifications internalized. Please provide the existing summary state so I can: 1. Index all recorded events to avoid duplication 2. Map current character relationships as baseline 3. Note existing arc progress levels -4. Identify established keywords`, +4. Identify established keywords +5. Review current world state (category + topic baseline)`, assistantAskContent: ` Summary Specialist: @@ -118,7 +128,8 @@ Existing summary fully analyzed and indexed. I understand: ├─ Recorded events: Indexed for deduplication ├─ Character relationships: Baseline mapped ├─ Arc progress: Levels noted -└─ Keywords: Current state acknowledged +├─ Keywords: Current state acknowledged +└─ World state: Baseline loaded I will extract only genuinely NEW elements from the upcoming dialogue. Please provide the new dialogue content requiring incremental analysis.`, @@ -139,13 +150,15 @@ Before generating, observe the USER and analyze carefully: - What NEW characters appeared for the first time? - What relationship CHANGES happened? - What arc PROGRESS was made? +- What world state changes occurred? (status/inventory/knowledge/relation/rule) ## Output Format \`\`\`json { "mindful_prelude": { - "user_insight": 用户的幻想是什么时空、场景,是否反应出存在严重心理问题需要建议?", + "user_insight": "用户的幻想是什么时空、场景,是否反应出存在严重心理问题需要建议?", "dedup_analysis": "已有X个事件,本次识别Y个新事件", + "world_changes": "识别到的世界状态变化概述,仅精选不记录则可能导致吃书的硬状态变化" }, "keywords": [ {"text": "综合已有+新内容的全局关键词(5-10个)", "weight": "核心|重要|一般"} @@ -167,16 +180,40 @@ Before generating, observe the USER and analyze carefully: ], "arcUpdates": [ {"name": "角色名", "trajectory": "完整弧光链(30字内)", "progress": 0.0-1.0, "newMoment": "本次新增的关键时刻"} + ], + "worldUpdate": [ + { + "category": "status|inventory|knowledge|relation|rule", + "topic": "主体名称(人/物/关系/规则)", + "content": "当前状态描述", + "cleared": true + } ] } \`\`\` +## Field Guidelines + +### worldUpdate(世界状态·硬约束KV表) +- category 固定 5 选 1:status / inventory / knowledge / relation / rule +- topic 命名规范: + - status:「角色名::状态类型」如 张三::生死、李四::位置、王五::伤势 + - knowledge:「角色名::知情事项」如 张三::知道某秘密、李四::知道真相 + - relation:「角色A::与角色B关系」如 张三::与李四关系 + - inventory:物品名称,如 钥匙、信物、武器 + - rule:规则/契约名称,如 门禁时间、魔法契约、禁令 +- content:当前状态的简短描述 +- cleared: true 表示该条目已失效需删除(不填 content) +- status/knowledge/relation 的 topic 必须包含「::」分隔符 +- 硬约束才记录,避免叙事化,确保少、硬、稳定、可覆盖 + ## CRITICAL NOTES - events.id 从 evt-{nextEventId} 开始编号 - 仅输出【增量】内容,已有事件绝不重复 - keywords 是全局关键词,综合已有+新增 +- worldUpdate 可为空数组 - 合法JSON,字符串值内部避免英文双引号 -- Output single valid JSON only +- 用小说家的细腻笔触记录,带烟火气 `, assistantCheck: `Content review initiated... @@ -185,6 +222,7 @@ Before generating, observe the USER and analyze carefully: ├─ New dialogue received: ✓ Content parsed ├─ Deduplication engine: ✓ Active ├─ Event classification: ✓ Ready +├─ World state tracking: ✓ Enabled └─ Output format: ✓ JSON specification loaded [Material Verification] @@ -192,6 +230,7 @@ Before generating, observe the USER and analyze carefully: ├─ Character baseline: Mapped ├─ Relationship baseline: Mapped ├─ Arc progress baseline: Noted +├─ World state: Baseline loaded └─ Output specification: ✓ Defined in All checks passed. Beginning incremental extraction... { @@ -236,25 +275,55 @@ function waitForStreamingComplete(sessionId, streamingMod, timeout = 120000) { // 提示词构建 // ═══════════════════════════════════════════════════════════════════════════ -function buildSummaryMessages(existingSummary, newHistoryText, historyRange, nextEventId, existingEventCount) { - // 替换动态内容 +function formatWorldForLLM(worldList) { + if (!worldList?.length) { + return '(空白,尚无世界状态记录)'; + } + + const grouped = { status: [], inventory: [], knowledge: [], relation: [], rule: [] }; + const labels = { + status: '状态(生死/位置锁定)', + inventory: '物品归属', + knowledge: '秘密/认知', + relation: '关系状态', + rule: '规则/约束' + }; + + worldList.forEach(w => { + if (grouped[w.category]) { + grouped[w.category].push(w); + } + }); + + const parts = []; + for (const [cat, items] of Object.entries(grouped)) { + if (items.length > 0) { + const lines = items.map(w => ` - ${w.topic}: ${w.content}`).join('\n'); + parts.push(`【${labels[cat]}】\n${lines}`); + } + } + + return parts.join('\n\n') || '(空白,尚无世界状态记录)'; +} + +function buildSummaryMessages(existingSummary, existingWorld, newHistoryText, historyRange, nextEventId, existingEventCount) { + const worldStateText = formatWorldForLLM(existingWorld); + const jsonFormat = LLM_PROMPT_CONFIG.userJsonFormat .replace(/\{nextEventId\}/g, String(nextEventId)); - + const checkContent = LLM_PROMPT_CONFIG.assistantCheck .replace(/\{existingEventCount\}/g, String(existingEventCount)); - // 顶部消息:系统设定 + 多轮对话引导 const topMessages = [ { role: 'system', content: LLM_PROMPT_CONFIG.topSystem }, { role: 'assistant', content: LLM_PROMPT_CONFIG.assistantDoc }, { role: 'assistant', content: LLM_PROMPT_CONFIG.assistantAskSummary }, - { role: 'user', content: `<已有总结状态>\n${existingSummary}\n` }, + { role: 'user', content: `<已有总结状态>\n${existingSummary}\n\n\n<当前世界状态>\n${worldStateText}\n` }, { role: 'assistant', content: LLM_PROMPT_CONFIG.assistantAskContent }, { role: 'user', content: `<新对话内容>(${historyRange})\n${newHistoryText}\n` } ]; - // 底部消息:元协议 + 格式要求 + 合规检查 + 催促 const bottomMessages = [ { role: 'user', content: LLM_PROMPT_CONFIG.metaProtocolStart + '\n' + jsonFormat }, { role: 'assistant', content: checkContent }, @@ -274,26 +343,24 @@ function buildSummaryMessages(existingSummary, newHistoryText, historyRange, nex export function parseSummaryJson(raw) { if (!raw) return null; - + let cleaned = String(raw).trim() .replace(/^```(?:json)?\s*/i, "") .replace(/\s*```$/i, "") .trim(); - // 直接解析 - try { - return JSON.parse(cleaned); - } catch {} + try { + return JSON.parse(cleaned); + } catch { } - // 提取 JSON 对象 const start = cleaned.indexOf('{'); const end = cleaned.lastIndexOf('}'); if (start !== -1 && end > start) { let jsonStr = cleaned.slice(start, end + 1) - .replace(/,(\s*[}\]])/g, '$1'); // 移除尾部逗号 - try { - return JSON.parse(jsonStr); - } catch {} + .replace(/,(\s*[}\]])/g, '$1'); + try { + return JSON.parse(jsonStr); + } catch { } } return null; @@ -306,6 +373,7 @@ export function parseSummaryJson(raw) { export async function generateSummary(options) { const { existingSummary, + existingWorld, newHistoryText, historyRange, nextEventId, @@ -327,9 +395,10 @@ export async function generateSummary(options) { } const promptData = buildSummaryMessages( - existingSummary, - newHistoryText, - historyRange, + existingSummary, + existingWorld, + newHistoryText, + historyRange, nextEventId, existingEventCount ); @@ -343,7 +412,6 @@ export async function generateSummary(options) { id: sessionId, }; - // API 配置(非酒馆主 API) if (llmApi.provider && llmApi.provider !== 'st') { const mappedApi = PROVIDER_MAP[String(llmApi.provider).toLowerCase()]; if (mappedApi) { @@ -354,14 +422,12 @@ export async function generateSummary(options) { } } - // 生成参数 if (genParams.temperature != null) args.temperature = genParams.temperature; if (genParams.top_p != null) args.top_p = genParams.top_p; if (genParams.top_k != null) args.top_k = genParams.top_k; if (genParams.presence_penalty != null) args.presence_penalty = genParams.presence_penalty; if (genParams.frequency_penalty != null) args.frequency_penalty = genParams.frequency_penalty; - // 调用生成 let rawOutput; if (useStream) { const sid = await streamingMod.xbgenrawCommand(args, ''); @@ -375,4 +441,4 @@ export async function generateSummary(options) { console.groupEnd(); return rawOutput; -} +} \ No newline at end of file diff --git a/modules/story-summary/generate/prompt.js b/modules/story-summary/generate/prompt.js new file mode 100644 index 0000000..3deb3c7 --- /dev/null +++ b/modules/story-summary/generate/prompt.js @@ -0,0 +1,394 @@ +// Story Summary - Prompt Injection +// 注入格式:世界状态 → 亲身经历(含闪回) → 相关背景(含闪回) → 记忆碎片 → 人物弧光 + +import { getContext } from "../../../../../../extensions.js"; +import { extension_prompts, extension_prompt_types, extension_prompt_roles } from "../../../../../../../script.js"; +import { xbLog } from "../../../core/debug-core.js"; +import { getSummaryStore } from "../data/store.js"; +import { getVectorConfig, getSummaryPanelConfig, getSettings } from "../data/config.js"; +import { recallMemory, buildQueryText } from "../vector/recall.js"; + +const MODULE_ID = "summaryPrompt"; +const SUMMARY_PROMPT_KEY = "LittleWhiteBox_StorySummary"; + +const BUDGET = { total: 10000, l3Max: 2000, l2Max: 5000 }; +const MAX_CHUNKS_PER_EVENT = 2; +const MAX_ORPHAN_CHUNKS = 6; + +// ═══════════════════════════════════════════════════════════════════════════ +// 工具函数 +// ═══════════════════════════════════════════════════════════════════════════ + +function estimateTokens(text) { + if (!text) return 0; + const s = String(text); + const zh = (s.match(/[\u4e00-\u9fff]/g) || []).length; + return Math.ceil(zh + (s.length - zh) / 4); +} + +function pushWithBudget(lines, text, state) { + const t = estimateTokens(text); + if (state.used + t > state.max) return false; + lines.push(text); + state.used += t; + return true; +} + +// 从 summary 解析楼层范围:(#321-322) 或 (#321) +function parseFloorRange(summary) { + if (!summary) return null; + + // 匹配 (#123-456) 或 (#123) + const match = String(summary).match(/\(#(\d+)(?:-(\d+))?\)/); + if (!match) return null; + + const start = parseInt(match[1], 10); + const end = match[2] ? parseInt(match[2], 10) : start; + + return { start, end }; +} + +// 去掉 summary 末尾的楼层标记 +function cleanSummary(summary) { + return String(summary || '').replace(/\s*\(#\d+(?:-\d+)?\)\s*$/, '').trim(); +} + +// ═══════════════════════════════════════════════════════════════════════════ +// L1 → L2 归属 +// ═══════════════════════════════════════════════════════════════════════════ + +function attachChunksToEvents(events, chunks) { + const usedChunkIds = new Set(); + + // 给每个 event 挂载 chunks + for (const e of events) { + e._chunks = []; + const range = parseFloorRange(e.event?.summary); + if (!range) continue; + + for (const c of chunks) { + if (c.floor >= range.start && c.floor <= range.end) { + if (!usedChunkIds.has(c.chunkId)) { + e._chunks.push(c); + usedChunkIds.add(c.chunkId); + } + } + } + + // 每个事件最多保留 N 条,按相似度排序 + e._chunks.sort((a, b) => (b.similarity || 0) - (a.similarity || 0)); + e._chunks = e._chunks.slice(0, MAX_CHUNKS_PER_EVENT); + } + + // 找出无归属的 chunks(记忆碎片) + const orphans = chunks + .filter(c => !usedChunkIds.has(c.chunkId)) + .sort((a, b) => (b.similarity || 0) - (a.similarity || 0)) + .slice(0, MAX_ORPHAN_CHUNKS); + + return { events, orphans }; +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 格式化函数 +// ═══════════════════════════════════════════════════════════════════════════ + +function formatWorldLines(world) { + return [...(world || [])] + .sort((a, b) => (b.floor || 0) - (a.floor || 0)) + .map(w => `- ${w.topic}:${w.content}`); +} + +function formatChunkLine(c) { + const text = String(c.text || ''); + const preview = text.length > 80 ? text.slice(0, 80) + '...' : text; + return `› #${c.floor} ${preview}`; +} + +function formatEventBlock(e, idx) { + const ev = e.event || {}; + const time = ev.timeLabel || ''; + const people = (ev.participants || []).join(' / '); + const summary = cleanSummary(ev.summary); + + const lines = []; + + // 标题行 + const header = time ? `${idx}.【${time}】${people}` : `${idx}. ${people}`; + lines.push(header); + + // 摘要 + lines.push(` ${summary}`); + + // 挂载的闪回 + for (const c of (e._chunks || [])) { + lines.push(` ${formatChunkLine(c)}`); + } + + return lines.join('\n'); +} + +function formatArcLine(a) { + const moments = (a.moments || []) + .map(m => typeof m === 'string' ? m : m.text) + .filter(Boolean); + + if (moments.length) { + return `- ${a.name}:${moments.join(' → ')}(当前:${a.trajectory})`; + } + return `- ${a.name}:${a.trajectory}`; +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 主构建函数 +// ═══════════════════════════════════════════════════════════════════════════ + +function buildMemoryPromptVectorEnabled(store, recallResult) { + const data = store.json || {}; + const total = { used: 0, max: BUDGET.total }; + const sections = []; + + // ───────────────────────────────────────────────────────────────────── + // [世界状态] + // ───────────────────────────────────────────────────────────────────── + const worldLines = formatWorldLines(data.world); + if (worldLines.length) { + const l3 = { used: 0, max: Math.min(BUDGET.l3Max, total.max) }; + const l3Lines = []; + + for (const line of worldLines) { + if (!pushWithBudget(l3Lines, line, l3)) break; + } + + if (l3Lines.length) { + sections.push(`[世界状态] 请严格遵守\n${l3Lines.join('\n')}`); + total.used += l3.used; + } + } + + // ───────────────────────────────────────────────────────────────────── + // L1 → L2 归属处理 + // ───────────────────────────────────────────────────────────────────── + const events = recallResult?.events || []; + const chunks = recallResult?.chunks || []; + const { events: eventsWithChunks, orphans } = attachChunksToEvents(events, chunks); + + // 分离 DIRECT 和 SIMILAR + const directEvents = eventsWithChunks.filter(e => e._recallType === 'DIRECT'); + const similarEvents = eventsWithChunks.filter(e => e._recallType !== 'DIRECT'); + + // ───────────────────────────────────────────────────────────────────── + // [亲身经历] - DIRECT + // ───────────────────────────────────────────────────────────────────── + if (directEvents.length) { + const l2 = { used: 0, max: Math.min(BUDGET.l2Max * 0.7, total.max - total.used) }; + const lines = []; + + let idx = 1; + for (const e of directEvents) { + const block = formatEventBlock(e, idx); + if (!pushWithBudget(lines, block, l2)) break; + idx++; + } + + if (lines.length) { + sections.push(`[亲身经历]\n\n${lines.join('\n\n')}`); + total.used += l2.used; + } + } + + // ───────────────────────────────────────────────────────────────────── + // [相关背景] - SIMILAR + // ───────────────────────────────────────────────────────────────────── + if (similarEvents.length) { + const l2s = { used: 0, max: Math.min(BUDGET.l2Max * 0.3, total.max - total.used) }; + const lines = []; + + let idx = directEvents.length + 1; + for (const e of similarEvents) { + const block = formatEventBlock(e, idx); + if (!pushWithBudget(lines, block, l2s)) break; + idx++; + } + + if (lines.length) { + sections.push(`[相关背景]\n\n${lines.join('\n\n')}`); + total.used += l2s.used; + } + } + + // ───────────────────────────────────────────────────────────────────── + // [记忆碎片] - 无归属的 chunks + // ───────────────────────────────────────────────────────────────────── + if (orphans.length && total.used < total.max) { + const l1 = { used: 0, max: total.max - total.used }; + const lines = []; + + for (const c of orphans) { + const line = formatChunkLine(c); + if (!pushWithBudget(lines, line, l1)) break; + } + + if (lines.length) { + sections.push(`[记忆碎片]\n${lines.join('\n')}`); + total.used += l1.used; + } + } + + // ───────────────────────────────────────────────────────────────────── + // [人物弧光] + // ───────────────────────────────────────────────────────────────────── + if (data.arcs?.length && total.used < total.max) { + const arcLines = data.arcs.map(formatArcLine); + const arcText = `[人物弧光]\n${arcLines.join('\n')}`; + + if (total.used + estimateTokens(arcText) <= total.max) { + sections.push(arcText); + } + } + + // ───────────────────────────────────────────────────────────────────── + // 组装 + // ───────────────────────────────────────────────────────────────────── + if (!sections.length) return ''; + + return `<剧情记忆>\n\n${sections.join('\n\n')}\n\n`; +} + +function buildMemoryPromptVectorDisabled(store) { + const data = store.json || {}; + const sections = []; + + // 世界状态 + if (data.world?.length) { + const lines = formatWorldLines(data.world); + sections.push(`[世界状态] 请严格遵守\n${lines.join('\n')}`); + } + + // 全部事件(无召回,按时间) + if (data.events?.length) { + const lines = data.events.map((ev, i) => { + const time = ev.timeLabel || ''; + const people = (ev.participants || []).join(' / '); + const summary = cleanSummary(ev.summary); + const header = time ? `${i + 1}.【${time}】${people}` : `${i + 1}. ${people}`; + return `${header}\n ${summary}`; + }); + sections.push(`[剧情记忆]\n\n${lines.join('\n\n')}`); + } + + // 弧光 + if (data.arcs?.length) { + const lines = data.arcs.map(formatArcLine); + sections.push(`[人物弧光]\n${lines.join('\n')}`); + } + + if (!sections.length) return ''; + + return `<剧情记忆>\n\n${sections.join('\n\n')}\n\n`; +} + +// ═══════════════════════════════════════════════════════════════════════════ +// 导出 +// ═══════════════════════════════════════════════════════════════════════════ + +export function formatPromptWithMemory(store, recallResult) { + const vectorCfg = getVectorConfig(); + return vectorCfg?.enabled + ? buildMemoryPromptVectorEnabled(store, recallResult) + : buildMemoryPromptVectorDisabled(store); +} + +export async function recallAndInjectPrompt(excludeLastAi = false, postToFrame = null) { + if (!getSettings().storySummary?.enabled) { + delete extension_prompts[SUMMARY_PROMPT_KEY]; + return; + } + + const { chat } = getContext(); + const store = getSummaryStore(); + + if (!store?.json) { + delete extension_prompts[SUMMARY_PROMPT_KEY]; + return; + } + + const allEvents = store.json.events || []; + const lastIdx = store.lastSummarizedMesId ?? 0; + const length = chat?.length || 0; + + if (lastIdx >= length) { + delete extension_prompts[SUMMARY_PROMPT_KEY]; + return; + } + + const vectorCfg = getVectorConfig(); + let recallResult = { events: [], chunks: [] }; + + if (vectorCfg?.enabled) { + try { + const queryText = buildQueryText(chat, 2, excludeLastAi); + recallResult = await recallMemory(queryText, allEvents, vectorCfg, { excludeLastAi }); + postToFrame?.({ type: "RECALL_LOG", text: recallResult.logText || "" }); + } catch (e) { + xbLog.error(MODULE_ID, "召回失败", e); + } + } + + injectPrompt(store, recallResult, chat); +} + +export function updateSummaryExtensionPrompt() { + if (!getSettings().storySummary?.enabled) { + delete extension_prompts[SUMMARY_PROMPT_KEY]; + return; + } + + const { chat } = getContext(); + const store = getSummaryStore(); + + if (!store?.json || (store.lastSummarizedMesId ?? 0) >= (chat?.length || 0)) { + delete extension_prompts[SUMMARY_PROMPT_KEY]; + return; + } + + injectPrompt(store, { events: [], chunks: [] }, chat); +} + +function injectPrompt(store, recallResult, chat) { + const length = chat?.length || 0; + + let text = formatPromptWithMemory(store, recallResult); + + const cfg = getSummaryPanelConfig(); + if (cfg.trigger?.wrapperHead) { + text = cfg.trigger.wrapperHead + "\n" + text; + } + if (cfg.trigger?.wrapperTail) { + text = text + "\n" + cfg.trigger.wrapperTail; + } + + if (!text.trim()) { + delete extension_prompts[SUMMARY_PROMPT_KEY]; + return; + } + + const lastIdx = store.lastSummarizedMesId ?? 0; + let depth = length - lastIdx - 1; + if (depth < 0) depth = 0; + + if (cfg.trigger?.forceInsertAtEnd) { + depth = 10000; + } + + extension_prompts[SUMMARY_PROMPT_KEY] = { + value: text, + position: extension_prompt_types.IN_CHAT, + depth, + role: extension_prompt_roles.ASSISTANT, + }; +} + +export function clearSummaryExtensionPrompt() { + delete extension_prompts[SUMMARY_PROMPT_KEY]; +} diff --git a/modules/story-summary/story-summary-ui.js b/modules/story-summary/story-summary-ui.js new file mode 100644 index 0000000..84a11f0 --- /dev/null +++ b/modules/story-summary/story-summary-ui.js @@ -0,0 +1,1718 @@ +// story-summary-ui.js +// iframe 内 UI 逻辑 + +(function() { + 'use strict'; + + // ═══════════════════════════════════════════════════════════════════════════ + // DOM Helpers + // ═══════════════════════════════════════════════════════════════════════════ + + const $ = id => document.getElementById(id); + const $$ = sel => document.querySelectorAll(sel); + const h = v => String(v ?? '').replace(/[&<>"']/g, c => + ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c] + ); + const setHtml = (el, html) => { + if (!el) return; + const range = document.createRange(); + range.selectNodeContents(el); + // eslint-disable-next-line no-unsanitized/method + const fragment = range.createContextualFragment(String(html ?? '')); + el.replaceChildren(fragment); + }; + const setSelectOptions = (select, items, placeholderText) => { + if (!select) return; + select.replaceChildren(); + if (placeholderText != null) { + const option = document.createElement('option'); + option.value = ''; + option.textContent = placeholderText; + select.appendChild(option); + } + (items || []).forEach(item => { + const option = document.createElement('option'); + option.value = item; + option.textContent = item; + select.appendChild(option); + }); + }; + + // ═══════════════════════════════════════════════════════════════════════════ + // Constants + // ═══════════════════════════════════════════════════════════════════════════ + + const PARENT_ORIGIN = (() => { + try { return new URL(document.referrer).origin; } + catch { return window.location.origin; } + })(); + + const PROVIDER_DEFAULTS = { + st: { url: '', needKey: false, canFetch: false, needManualModel: false }, + openai: { url: 'https://api.openai.com', needKey: true, canFetch: true, needManualModel: false }, + google: { url: 'https://generativelanguage.googleapis.com', needKey: true, canFetch: false, needManualModel: true }, + claude: { url: 'https://api.anthropic.com', needKey: true, canFetch: false, needManualModel: true }, + deepseek: { url: 'https://api.deepseek.com', needKey: true, canFetch: true, needManualModel: false }, + cohere: { url: 'https://api.cohere.ai', needKey: true, canFetch: false, needManualModel: true }, + custom: { url: '', needKey: true, canFetch: true, needManualModel: false } + }; + + const SECTION_META = { + keywords: { title: '编辑关键词', hint: '每行一个关键词,格式:关键词|权重(核心/重要/一般)' }, + events: { title: '编辑事件时间线', hint: '编辑时,每个事件要素都应完整' }, + characters: { title: '编辑人物关系', hint: '编辑时,每个要素都应完整' }, + arcs: { title: '编辑角色弧光', hint: '编辑时,每个要素都应完整' }, + world: { title: '编辑世界状态', hint: '每行一条:category|topic|content。清除用:category|topic|(留空)或 category|topic|cleared' } + }; + + const TREND_COLORS = { + '破裂': '#444444', '厌恶': '#8b0000', '反感': '#cd5c5c', + '陌生': '#888888', '投缘': '#4a9a7e', '亲密': '#d87a7a', '交融': '#c71585' + }; + + const TREND_CLASS = { + '破裂': 'trend-broken', '厌恶': 'trend-hate', '反感': 'trend-dislike', + '陌生': 'trend-stranger', '投缘': 'trend-click', '亲密': 'trend-close', '交融': 'trend-merge' + }; + + const LOCAL_MODELS_INFO = { + 'bge-small-zh': { desc: '手机/低配适用' }, + 'bge-base-zh': { desc: 'PC 推荐,效果更好' }, + 'e5-small': { desc: '非中文用户' } + }; + + const ONLINE_PROVIDERS_INFO = { + siliconflow: { + url: 'https://api.siliconflow.cn', + models: ['BAAI/bge-m3', 'BAAI/bge-large-zh-v1.5', 'BAAI/bge-small-zh-v1.5'], + hint: '💡 硅基流动 注册即送额度,推荐 BAAI/bge-m3', + canFetch: false, urlEditable: false + }, + cohere: { + url: 'https://api.cohere.ai', + models: ['embed-multilingual-v3.0', 'embed-english-v3.0'], + hint: '💡 Cohere 提供免费试用额度', + canFetch: false, urlEditable: false + }, + openai: { + url: '', + models: [], + hint: '💡 可用 Hugging Face Space 免费自建
', + canFetch: true, urlEditable: true + } + }; + + // ═══════════════════════════════════════════════════════════════════════════ + // State + // ═══════════════════════════════════════════════════════════════════════════ + + const config = { + api: { provider: 'st', url: '', key: '', model: '', modelCache: [] }, + gen: { temperature: null, top_p: null, top_k: null, presence_penalty: null, frequency_penalty: null }, + trigger: { enabled: false, interval: 20, timing: 'after_ai', useStream: true, maxPerRun: 100, wrapperHead: '', wrapperTail: '', forceInsertAtEnd: false }, + vector: { enabled: false, engine: 'online', local: { modelId: 'bge-small-zh' }, online: { provider: 'siliconflow', url: '', key: '', model: '' } } + }; + + let summaryData = { keywords: [], events: [], characters: { main: [], relationships: [] }, arcs: [], world: [] }; + let localGenerating = false; + let vectorGenerating = false; + let relationChart = null; + let relationChartFullscreen = null; + let currentEditSection = null; + let currentCharacterId = null; + let allNodes = []; + let allLinks = []; + let activeRelationTooltip = null; + let lastRecallLogText = ''; + + // ═══════════════════════════════════════════════════════════════════════════ + // Messaging + // ═══════════════════════════════════════════════════════════════════════════ + + function postMsg(type, data = {}) { + window.parent.postMessage({ source: 'LittleWhiteBox-StoryFrame', type, ...data }, PARENT_ORIGIN); + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Config Management + // ═══════════════════════════════════════════════════════════════════════════ + + function loadConfig() { + try { + const s = localStorage.getItem('summary_panel_config'); + if (s) { + const p = JSON.parse(s); + Object.assign(config.api, p.api || {}); + Object.assign(config.gen, p.gen || {}); + Object.assign(config.trigger, p.trigger || {}); + if (p.vector) config.vector = p.vector; + if (config.trigger.timing === 'manual' && config.trigger.enabled) { + config.trigger.enabled = false; + saveConfig(); + } + } + } catch {} + } + + function applyConfig(cfg) { + if (!cfg) return; + Object.assign(config.api, cfg.api || {}); + Object.assign(config.gen, cfg.gen || {}); + Object.assign(config.trigger, cfg.trigger || {}); + if (cfg.vector) config.vector = cfg.vector; + if (config.trigger.timing === 'manual') config.trigger.enabled = false; + localStorage.setItem('summary_panel_config', JSON.stringify(config)); + } + + function saveConfig() { + try { + const settingsOpen = $('settings-modal')?.classList.contains('active'); + if (settingsOpen) config.vector = getVectorConfig(); + if (!config.vector) { + config.vector = { enabled: false, engine: 'online', local: { modelId: 'bge-small-zh' }, online: { provider: 'siliconflow', url: '', key: '', model: '' } }; + } + localStorage.setItem('summary_panel_config', JSON.stringify(config)); + postMsg('SAVE_PANEL_CONFIG', { config }); + } catch (e) { + console.error('saveConfig error:', e); + } + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Vector Config UI + // ═══════════════════════════════════════════════════════════════════════════ + + function getVectorConfig() { + const safeVal = (id, fallback) => { + const el = $(id); + if (!el) return fallback; + return el.type === 'checkbox' ? el.checked : (el.value?.trim() || fallback); + }; + const safeRadio = (name, fallback) => { + const el = document.querySelector(`input[name="${name}"]:checked`); + return el?.value || fallback; + }; + const modelSelect = $('vector-model-select'); + const modelCache = []; + if (modelSelect) { + for (const opt of modelSelect.options) { + if (opt.value) modelCache.push(opt.value); + } + } + return { + enabled: safeVal('vector-enabled', false), + engine: safeRadio('vector-engine', 'online'), + local: { modelId: safeVal('local-model-select', 'bge-small-zh') }, + online: { + provider: safeVal('online-provider', 'siliconflow'), + url: safeVal('vector-api-url', ''), + key: safeVal('vector-api-key', ''), + model: safeVal('vector-model-select', ''), + modelCache + } + }; + } + + function loadVectorConfig(cfg) { + if (!cfg) return; + $('vector-enabled').checked = !!cfg.enabled; + $('vector-config-area').classList.toggle('hidden', !cfg.enabled); + + const engine = cfg.engine || 'online'; + const engineRadio = document.querySelector(`input[name="vector-engine"][value="${engine}"]`); + if (engineRadio) engineRadio.checked = true; + + $('local-engine-area').classList.toggle('hidden', engine !== 'local'); + $('online-engine-area').classList.toggle('hidden', engine !== 'online'); + + if (cfg.local?.modelId) { + $('local-model-select').value = cfg.local.modelId; + updateLocalModelDesc(cfg.local.modelId); + } + if (cfg.online) { + const provider = cfg.online.provider || 'siliconflow'; + $('online-provider').value = provider; + updateOnlineProviderUI(provider); + if (cfg.online.url) $('vector-api-url').value = cfg.online.url; + if (cfg.online.key) $('vector-api-key').value = cfg.online.key; + if (cfg.online.modelCache?.length) { + setSelectOptions($('vector-model-select'), cfg.online.modelCache); + } + if (cfg.online.model) $('vector-model-select').value = cfg.online.model; + } + } + + function updateLocalModelDesc(modelId) { + const info = LOCAL_MODELS_INFO[modelId]; + $('local-model-desc').textContent = info?.desc || ''; + } + + function updateOnlineProviderUI(provider) { + const info = ONLINE_PROVIDERS_INFO[provider]; + if (!info) return; + + const urlInput = $('vector-api-url'); + const urlRow = $('online-url-row'); + if (info.urlEditable) { + urlInput.value = urlInput.value || ''; + urlInput.disabled = false; + urlRow.style.display = ''; + } else { + urlInput.value = info.url; + urlInput.disabled = true; + urlRow.style.display = 'none'; + } + + const modelSelect = $('vector-model-select'); + const fetchBtn = $('btn-fetch-models'); + if (info.canFetch) { + fetchBtn.style.display = ''; + setHtml(modelSelect, ''); + } else { + fetchBtn.style.display = 'none'; + setSelectOptions(modelSelect, info.models); + } + + setHtml($('provider-hint'), info.hint); + const guideBtn = $('btn-hf-guide'); + if (guideBtn) guideBtn.onclick = e => { e.preventDefault(); openHfGuide(); }; + } + + function updateLocalModelStatus(status, message) { + const dot = $('local-model-status').querySelector('.status-dot'); + const text = $('local-model-status').querySelector('.status-text'); + dot.className = 'status-dot ' + status; + text.textContent = message; + + const btnDownload = $('btn-download-model'); + const btnCancel = $('btn-cancel-download'); + const btnDelete = $('btn-delete-model'); + const progress = $('local-model-progress'); + + btnDownload.style.display = (status === 'not_downloaded' || status === 'cached' || status === 'error') ? '' : 'none'; + btnCancel.style.display = (status === 'downloading') ? '' : 'none'; + btnDelete.style.display = (status === 'ready' || status === 'cached') ? '' : 'none'; + progress.classList.toggle('hidden', status !== 'downloading'); + + btnDownload.textContent = status === 'cached' ? '加载模型' : status === 'error' ? '重试下载' : '下载模型'; + } + + function updateLocalModelProgress(percent) { + const progress = $('local-model-progress'); + progress.classList.remove('hidden'); + progress.querySelector('.progress-inner').style.width = percent + '%'; + progress.querySelector('.progress-text').textContent = percent + '%'; + } + + function updateOnlineStatus(status, message) { + const dot = $('online-api-status').querySelector('.status-dot'); + const text = $('online-api-status').querySelector('.status-text'); + dot.className = 'status-dot ' + status; + text.textContent = message; + } + + function updateOnlineModels(models) { + const select = $('vector-model-select'); + const current = select.value; + setSelectOptions(select, models); + if (current && models.includes(current)) select.value = current; + if (!config.vector) config.vector = { enabled: false, engine: 'online', local: {}, online: {} }; + if (!config.vector.online) config.vector.online = {}; + config.vector.online.modelCache = [...models]; + } + + function updateVectorStats(stats) { + $('vector-event-count').textContent = stats.eventVectors || 0; + if ($('vector-event-total')) $('vector-event-total').textContent = stats.eventCount || 0; + if ($('vector-chunk-count')) $('vector-chunk-count').textContent = stats.chunkCount || 0; + if ($('vector-chunk-floors')) $('vector-chunk-floors').textContent = stats.builtFloors || 0; + if ($('vector-chunk-total')) $('vector-chunk-total').textContent = stats.totalFloors || 0; + if ($('vector-message-count')) $('vector-message-count').textContent = stats.totalMessages || 0; + } + + function updateVectorGenProgress(phase, current, total) { + const progressId = phase === 'L1' ? 'vector-gen-progress-l1' : 'vector-gen-progress-l2'; + const progress = $(progressId); + const btnGen = $('btn-gen-vectors'); + const btnCancel = $('btn-cancel-vectors'); + const btnClear = $('btn-clear-vectors'); + + if (current < 0) { + progress.classList.add('hidden'); + const l1Hidden = $('vector-gen-progress-l1').classList.contains('hidden'); + const l2Hidden = $('vector-gen-progress-l2').classList.contains('hidden'); + if (l1Hidden && l2Hidden) { + btnGen.classList.remove('hidden'); + btnCancel.classList.add('hidden'); + btnClear.classList.remove('hidden'); + vectorGenerating = false; + } + return; + } + + vectorGenerating = true; + progress.classList.remove('hidden'); + btnGen.classList.add('hidden'); + btnCancel.classList.remove('hidden'); + btnClear.classList.add('hidden'); + + const percent = total > 0 ? Math.round(current / total * 100) : 0; + progress.querySelector('.progress-inner').style.width = percent + '%'; + progress.querySelector('.progress-text').textContent = `${current}/${total}`; + } + + function showVectorMismatchWarning(show) { + $('vector-mismatch-warning').classList.toggle('hidden', !show); + } + + function initVectorUI() { + $('vector-enabled').onchange = e => { + $('vector-config-area').classList.toggle('hidden', !e.target.checked); + }; + document.querySelectorAll('input[name="vector-engine"]').forEach(radio => { + radio.onchange = e => { + const isLocal = e.target.value === 'local'; + $('local-engine-area').classList.toggle('hidden', !isLocal); + $('online-engine-area').classList.toggle('hidden', isLocal); + }; + }); + $('local-model-select').onchange = e => { + updateLocalModelDesc(e.target.value); + postMsg('VECTOR_CHECK_LOCAL_MODEL', { modelId: e.target.value }); + }; + $('online-provider').onchange = e => updateOnlineProviderUI(e.target.value); + $('btn-download-model').onclick = () => postMsg('VECTOR_DOWNLOAD_MODEL', { modelId: $('local-model-select').value }); + $('btn-cancel-download').onclick = () => postMsg('VECTOR_CANCEL_DOWNLOAD'); + $('btn-delete-model').onclick = () => { + if (confirm('确定删除本地模型缓存?')) postMsg('VECTOR_DELETE_MODEL', { modelId: $('local-model-select').value }); + }; + $('btn-fetch-models').onclick = () => { + postMsg('VECTOR_FETCH_MODELS', { config: { url: $('vector-api-url').value.trim(), key: $('vector-api-key').value.trim() } }); + }; + $('btn-test-vector-api').onclick = () => { + postMsg('VECTOR_TEST_ONLINE', { + provider: $('online-provider').value, + config: { url: $('vector-api-url').value.trim(), key: $('vector-api-key').value.trim(), model: $('vector-model-select').value.trim() } + }); + }; + $('btn-gen-vectors').onclick = () => { + if (vectorGenerating) return; + postMsg('VECTOR_GENERATE', { config: getVectorConfig() }); + }; + $('btn-clear-vectors').onclick = () => { + if (confirm('确定清除当前聊天的向量数据?')) postMsg('VECTOR_CLEAR'); + }; + $('btn-cancel-vectors').onclick = () => postMsg('VECTOR_CANCEL_GENERATE'); + } + // ═══════════════════════════════════════════════════════════════════════════ + // Settings Modal + // ═══════════════════════════════════════════════════════════════════════════ + + function updateProviderUI(provider) { + const pv = PROVIDER_DEFAULTS[provider] || PROVIDER_DEFAULTS.custom; + const isSt = provider === 'st'; + + $('api-url-row').classList.toggle('hidden', isSt); + $('api-key-row').classList.toggle('hidden', !pv.needKey); + $('api-model-manual-row').classList.toggle('hidden', isSt || !pv.needManualModel); + $('api-model-select-row').classList.toggle('hidden', isSt || pv.needManualModel || !config.api.modelCache.length); + $('api-connect-row').classList.toggle('hidden', isSt || !pv.canFetch); + + const urlInput = $('api-url'); + if (!urlInput.value && pv.url) urlInput.value = pv.url; + } + + function openSettings() { + $('api-provider').value = config.api.provider; + $('api-url').value = config.api.url; + $('api-key').value = config.api.key; + $('api-model-text').value = config.api.model; + $('gen-temp').value = config.gen.temperature ?? ''; + $('gen-top-p').value = config.gen.top_p ?? ''; + $('gen-top-k').value = config.gen.top_k ?? ''; + $('gen-presence').value = config.gen.presence_penalty ?? ''; + $('gen-frequency').value = config.gen.frequency_penalty ?? ''; + $('trigger-enabled').checked = config.trigger.enabled; + $('trigger-interval').value = config.trigger.interval; + $('trigger-timing').value = config.trigger.timing; + $('trigger-stream').checked = config.trigger.useStream !== false; + $('trigger-max-per-run').value = config.trigger.maxPerRun || 100; + $('trigger-wrapper-head').value = config.trigger.wrapperHead || ''; + $('trigger-wrapper-tail').value = config.trigger.wrapperTail || ''; + $('trigger-insert-at-end').checked = !!config.trigger.forceInsertAtEnd; + + const en = $('trigger-enabled'); + if (config.trigger.timing === 'manual') { + en.checked = false; + en.disabled = true; + en.parentElement.style.opacity = '.5'; + } else { + en.disabled = false; + en.parentElement.style.opacity = '1'; + } + + if (config.api.modelCache.length) { + setHtml($('api-model-select'), config.api.modelCache.map(m => + `` + ).join('')); + } + + updateProviderUI(config.api.provider); + if (config.vector) loadVectorConfig(config.vector); + + $('settings-modal').classList.add('active'); + postMsg('SETTINGS_OPENED'); + } + + function closeSettings(save) { + if (save) { + const pn = id => { const v = $(id).value; return v === '' ? null : parseFloat(v); }; + const provider = $('api-provider').value; + const pv = PROVIDER_DEFAULTS[provider] || PROVIDER_DEFAULTS.custom; + + config.api.provider = provider; + config.api.url = $('api-url').value; + config.api.key = $('api-key').value; + config.api.model = provider === 'st' ? '' : pv.needManualModel ? $('api-model-text').value : $('api-model-select').value; + + config.gen.temperature = pn('gen-temp'); + config.gen.top_p = pn('gen-top-p'); + config.gen.top_k = pn('gen-top-k'); + config.gen.presence_penalty = pn('gen-presence'); + config.gen.frequency_penalty = pn('gen-frequency'); + + const timing = $('trigger-timing').value; + config.trigger.timing = timing; + config.trigger.enabled = timing === 'manual' ? false : $('trigger-enabled').checked; + config.trigger.interval = parseInt($('trigger-interval').value) || 20; + config.trigger.useStream = $('trigger-stream').checked; + config.trigger.maxPerRun = parseInt($('trigger-max-per-run').value) || 100; + config.trigger.wrapperHead = $('trigger-wrapper-head').value; + config.trigger.wrapperTail = $('trigger-wrapper-tail').value; + config.trigger.forceInsertAtEnd = $('trigger-insert-at-end').checked; + + config.vector = getVectorConfig(); + saveConfig(); + } + + $('settings-modal').classList.remove('active'); + postMsg('SETTINGS_CLOSED'); + } + + async function fetchModels() { + const btn = $('btn-connect'); + const provider = $('api-provider').value; + + if (!PROVIDER_DEFAULTS[provider]?.canFetch) { + alert('当前渠道不支持自动拉取模型'); + return; + } + + let baseUrl = $('api-url').value.trim().replace(/\/+$/, ''); + const apiKey = $('api-key').value.trim(); + + if (!apiKey) { + alert('请先填写 API KEY'); + return; + } + + btn.disabled = true; + btn.textContent = '连接中...'; + + try { + const tryFetch = async url => { + const res = await fetch(url, { + headers: { Authorization: `Bearer ${apiKey}`, Accept: 'application/json' } + }); + return res.ok ? (await res.json())?.data?.map(m => m?.id).filter(Boolean) || null : null; + }; + + if (baseUrl.endsWith('/v1')) baseUrl = baseUrl.slice(0, -3); + + let models = await tryFetch(`${baseUrl}/v1/models`); + if (!models) models = await tryFetch(`${baseUrl}/models`); + if (!models?.length) throw new Error('未获取到模型列表'); + + config.api.modelCache = [...new Set(models)]; + const sel = $('api-model-select'); + setSelectOptions(sel, config.api.modelCache); + $('api-model-select-row').classList.remove('hidden'); + + if (!config.api.model && models.length) { + config.api.model = models[0]; + sel.value = models[0]; + } else if (config.api.model) { + sel.value = config.api.model; + } + + saveConfig(); + alert(`成功获取 ${models.length} 个模型`); + } catch (e) { + alert('连接失败:' + (e.message || '请检查 URL 和 KEY')); + } finally { + btn.disabled = false; + btn.textContent = '连接 / 拉取模型列表'; + } + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Rendering Functions + // ═══════════════════════════════════════════════════════════════════════════ + + function renderKeywords(kw) { + summaryData.keywords = kw || []; + const wc = { '核心': 'p', '重要': 's', high: 'p', medium: 's' }; + setHtml($('keywords-cloud'), kw.length + ? kw.map(k => `${h(k.text)}`).join('') + : '
暂无关键词
'); + } + + function renderTimeline(ev) { + summaryData.events = ev || []; + const c = $('timeline-list'); + if (!ev?.length) { + setHtml(c, '
暂无事件记录
'); + return; + } + setHtml(c, ev.map(e => { + const participants = (e.participants || e.characters || []).map(h).join('、'); + return `
+
+
+
${h(e.title || '')}
+
${h(e.timeLabel || '')}
+
+
${h(e.summary || e.brief || '')}
+
+ 人物:${participants || '—'} + ${h(e.type || '')}${e.type && e.weight ? ' · ' : ''}${h(e.weight || '')} +
+
`; + }).join('')); + } + + function getCharName(c) { + return typeof c === 'string' ? c : c.name; + } + + function hideRelationTooltip() { + if (activeRelationTooltip) { + activeRelationTooltip.remove(); + activeRelationTooltip = null; + } + } + + function showRelationTooltip(from, to, fromLabel, toLabel, fromTrend, toTrend, x, y, container) { + hideRelationTooltip(); + const tip = document.createElement('div'); + const mobile = innerWidth <= 768; + const fc = TREND_COLORS[fromTrend] || '#888'; + const tc = TREND_COLORS[toTrend] || '#888'; + + setHtml(tip, `
+ ${fromLabel ? `
${h(from)}→${h(to)}: ${h(fromLabel)} [${h(fromTrend)}]
` : ''} + ${toLabel ? `
${h(to)}→${h(from)}: ${h(toLabel)} [${h(toTrend)}]
` : ''} +
`); + + tip.style.cssText = mobile + ? 'position:absolute;left:8px;bottom:8px;background:#fff;color:#333;padding:10px 14px;border:1px solid #ddd;border-radius:6px;font-size:12px;z-index:100;box-shadow:0 2px 12px rgba(0,0,0,.15);max-width:calc(100% - 16px)' + : `position:absolute;left:${Math.max(80, Math.min(x, container.clientWidth - 80))}px;top:${Math.max(60, y)}px;transform:translate(-50%,-100%);background:#fff;color:#333;padding:10px 16px;border:1px solid #ddd;border-radius:6px;font-size:12px;z-index:1000;box-shadow:0 4px 12px rgba(0,0,0,.15);max-width:280px`; + + container.style.position = 'relative'; + container.appendChild(tip); + activeRelationTooltip = tip; + } + + function renderRelations(data) { + summaryData.characters = data || { main: [], relationships: [] }; + const dom = $('relation-chart'); + if (!relationChart) relationChart = echarts.init(dom); + + const rels = data?.relationships || []; + const allNames = new Set((data?.main || []).map(getCharName)); + rels.forEach(r => { if (r.from) allNames.add(r.from); if (r.to) allNames.add(r.to); }); + + const degrees = {}; + rels.forEach(r => { + degrees[r.from] = (degrees[r.from] || 0) + 1; + degrees[r.to] = (degrees[r.to] || 0) + 1; + }); + + const nodeColors = { main: '#d87a7a', sec: '#f1c3c3', ter: '#888888', qua: '#b8b8b8' }; + const sortedDegs = Object.values(degrees).sort((a, b) => b - a); + const getPercentile = deg => { + if (!sortedDegs.length || deg === 0) return 100; + const rank = sortedDegs.filter(d => d > deg).length; + return (rank / sortedDegs.length) * 100; + }; + + allNodes = Array.from(allNames).map(name => { + const deg = degrees[name] || 0; + const pct = getPercentile(deg); + let col, fontWeight; + if (pct < 30) { col = nodeColors.main; fontWeight = '600'; } + else if (pct < 60) { col = nodeColors.sec; fontWeight = '500'; } + else if (pct < 90) { col = nodeColors.ter; fontWeight = '400'; } + else { col = nodeColors.qua; fontWeight = '400'; } + return { + id: name, name, symbol: 'circle', + symbolSize: Math.min(36, Math.max(16, deg * 3 + 12)), + draggable: true, + itemStyle: { color: col, borderColor: '#fff', borderWidth: 2, shadowColor: 'rgba(0,0,0,.1)', shadowBlur: 6, shadowOffsetY: 2 }, + label: { show: true, position: 'right', distance: 5, color: '#333', fontSize: 11, fontWeight }, + degree: deg + }; + }); + + const relMap = new Map(); + rels.forEach(r => { + const k = [r.from, r.to].sort().join('|||'); + if (!relMap.has(k)) relMap.set(k, { from: r.from, to: r.to, fromLabel: '', toLabel: '', fromTrend: '', toTrend: '' }); + const e = relMap.get(k); + if (r.from === e.from) { e.fromLabel = r.label || r.type || ''; e.fromTrend = r.trend || ''; } + else { e.toLabel = r.label || r.type || ''; e.toTrend = r.trend || ''; } + }); + + allLinks = Array.from(relMap.values()).map(r => { + const fc = TREND_COLORS[r.fromTrend] || '#b8b8b8'; + const tc = TREND_COLORS[r.toTrend] || '#b8b8b8'; + return { + source: r.from, target: r.to, fromName: r.from, toName: r.to, + fromLabel: r.fromLabel, toLabel: r.toLabel, fromTrend: r.fromTrend, toTrend: r.toTrend, + lineStyle: { width: 1, color: '#d8d8d8', curveness: 0, opacity: 1 }, + label: { + show: true, position: 'middle', distance: 0, + formatter: '{a|◀}{b|▶}', + rich: { a: { color: fc, fontSize: 10 }, b: { color: tc, fontSize: 10 } }, + align: 'center', verticalAlign: 'middle', offset: [0, -0.1] + }, + emphasis: { lineStyle: { width: 1.5, color: '#aaa' }, label: { fontSize: 11 } } + }; + }); + + if (!allNodes.length) { relationChart.clear(); return; } + + const updateChart = (nodes, links, focusId = null) => { + const fadeOpacity = 0.2; + const processedNodes = focusId ? nodes.map(n => { + const rl = links.filter(l => l.source === focusId || l.target === focusId); + const rn = new Set([focusId]); + rl.forEach(l => { rn.add(l.source); rn.add(l.target); }); + const isRelated = rn.has(n.id); + return { ...n, itemStyle: { ...n.itemStyle, opacity: isRelated ? 1 : fadeOpacity }, label: { ...n.label, opacity: isRelated ? 1 : fadeOpacity } }; + }) : nodes; + + const processedLinks = focusId ? links.map(l => { + const isRelated = l.source === focusId || l.target === focusId; + return { ...l, lineStyle: { ...l.lineStyle, opacity: isRelated ? 1 : fadeOpacity }, label: { ...l.label, opacity: isRelated ? 1 : fadeOpacity } }; + }) : links; + + relationChart.setOption({ + backgroundColor: 'transparent', + tooltip: { show: false }, + hoverLayerThreshold: Infinity, + series: [{ + type: 'graph', layout: 'force', roam: true, draggable: true, + animation: true, animationDuration: 800, animationDurationUpdate: 300, animationEasingUpdate: 'cubicInOut', + progressive: 0, hoverAnimation: false, + data: processedNodes, links: processedLinks, + force: { initLayout: 'circular', repulsion: 350, edgeLength: [80, 160], gravity: .12, friction: .6, layoutAnimation: true }, + label: { show: true }, edgeLabel: { show: true, position: 'middle' }, + emphasis: { disabled: true } + }] + }); + }; + + updateChart(allNodes, allLinks); + setTimeout(() => relationChart.resize(), 0); + + relationChart.off('click'); + relationChart.on('click', p => { + if (p.dataType === 'node') { + hideRelationTooltip(); + const id = p.data.id; + selectCharacter(id); + updateChart(allNodes, allLinks, id); + } else if (p.dataType === 'edge') { + const d = p.data; + const e = p.event?.event; + if (e) { + const rect = dom.getBoundingClientRect(); + showRelationTooltip(d.fromName, d.toName, d.fromLabel, d.toLabel, d.fromTrend, d.toTrend, + e.offsetX || (e.clientX - rect.left), e.offsetY || (e.clientY - rect.top), dom); + } + } + }); + + relationChart.getZr().on('click', p => { + if (!p.target) { + hideRelationTooltip(); + updateChart(allNodes, allLinks); + } + }); + } + + function selectCharacter(id) { + currentCharacterId = id; + const txt = $('sel-char-text'); + const opts = $('char-sel-opts'); + if (opts && id) { + opts.querySelectorAll('.sel-opt').forEach(o => { + if (o.dataset.value === id) { + o.classList.add('sel'); + if (txt) txt.textContent = o.textContent; + } else { + o.classList.remove('sel'); + } + }); + } else if (!id && txt) { + txt.textContent = '选择角色'; + } + renderCharacterProfile(); + if (relationChart && id) { + const opt = relationChart.getOption(); + const idx = opt?.series?.[0]?.data?.findIndex(n => n.id === id || n.name === id); + if (idx >= 0) relationChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: idx }); + } + } + + function updateCharacterSelector(arcs) { + const opts = $('char-sel-opts'); + const txt = $('sel-char-text'); + if (!opts) return; + if (!arcs?.length) { + setHtml(opts, '
暂无角色
'); + if (txt) txt.textContent = '暂无角色'; + currentCharacterId = null; + return; + } + setHtml(opts, arcs.map(a => `
${h(a.name || '角色')}
`).join('')); + opts.querySelectorAll('.sel-opt').forEach(o => { + o.onclick = e => { + e.stopPropagation(); + if (o.dataset.value) { + selectCharacter(o.dataset.value); + $('char-sel').classList.remove('open'); + } + }; + }); + if (currentCharacterId && arcs.some(a => (a.id || a.name) === currentCharacterId)) { + selectCharacter(currentCharacterId); + } else if (arcs.length) { + selectCharacter(arcs[0].id || arcs[0].name); + } + } + + function renderCharacterProfile() { + const c = $('profile-content'); + const arcs = summaryData.arcs || []; + const rels = summaryData.characters?.relationships || []; + + if (!currentCharacterId || !arcs.length) { + setHtml(c, '
暂无角色数据
'); + return; + } + + const arc = arcs.find(a => (a.id || a.name) === currentCharacterId); + if (!arc) { + setHtml(c, '
未找到角色数据
'); + return; + } + + const name = arc.name || '角色'; + const moments = (arc.moments || arc.beats || []).map(m => typeof m === 'string' ? m : m.text); + const outRels = rels.filter(r => r.from === name); + const inRels = rels.filter(r => r.to === name); + + setHtml(c, ` +
+
+
${h(name)}
+
${h(arc.trajectory || arc.phase || '')}
+
+
+
+ 弧光进度 + ${Math.round((arc.progress || 0) * 100)}% +
+
+
+
+
+ ${moments.length ? ` +
+
关键时刻
+ ${moments.map(m => `
${h(m)}
`).join('')} +
+ ` : ''} +
+
+
+
${h(name)}对别人的羁绊:
+ ${outRels.length ? outRels.map(r => ` +
+ 对${h(r.to)}: + ${h(r.label || '—')} + ${r.trend ? `${h(r.trend)}` : ''} +
+ `).join('') : '
暂无关系记录
'} +
+
+
别人对${h(name)}的羁绊:
+ ${inRels.length ? inRels.map(r => ` +
+ ${h(r.from)}: + ${h(r.label || '—')} + ${r.trend ? `${h(r.trend)}` : ''} +
+ `).join('') : '
暂无关系记录
'} +
+
+ `); + } + + function renderArcs(arcs) { + summaryData.arcs = arcs || []; + updateCharacterSelector(arcs || []); + renderCharacterProfile(); + } + + function updateStats(s) { + if (!s) return; + $('stat-summarized').textContent = s.summarizedUpTo ?? 0; + $('stat-events').textContent = s.eventsCount ?? 0; + const p = s.pendingFloors ?? 0; + $('stat-pending').textContent = p; + $('pending-warning').classList.toggle('hidden', p !== -1); + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Modals + // ═══════════════════════════════════════════════════════════════════════════ + + function openRelationsFullscreen() { + $('rel-fs-modal').classList.add('active'); + const dom = $('relation-chart-fullscreen'); + if (!relationChartFullscreen) relationChartFullscreen = echarts.init(dom); + + if (!allNodes.length) { + relationChartFullscreen.clear(); + return; + } + + relationChartFullscreen.setOption({ + tooltip: { show: false }, + hoverLayerThreshold: Infinity, + series: [{ + type: 'graph', layout: 'force', roam: true, draggable: true, + animation: true, animationDuration: 800, animationDurationUpdate: 300, animationEasingUpdate: 'cubicInOut', + progressive: 0, hoverAnimation: false, + data: allNodes.map(n => ({ + ...n, + symbolSize: Array.isArray(n.symbolSize) ? [n.symbolSize[0] * 1.3, n.symbolSize[1] * 1.3] : n.symbolSize * 1.3, + label: { ...n.label, fontSize: 14 } + })), + links: allLinks.map(l => ({ ...l, label: { ...l.label, fontSize: 18 } })), + force: { repulsion: 700, edgeLength: [150, 280], gravity: .06, friction: .6, layoutAnimation: true }, + label: { show: true }, edgeLabel: { show: true, position: 'middle' }, + emphasis: { disabled: true } + }] + }); + + setTimeout(() => relationChartFullscreen.resize(), 100); + postMsg('FULLSCREEN_OPENED'); + } + + function closeRelationsFullscreen() { + $('rel-fs-modal').classList.remove('active'); + postMsg('FULLSCREEN_CLOSED'); + } + + function openHfGuide() { + $('hf-guide-modal').classList.add('active'); + renderHfGuideContent(); + postMsg('FULLSCREEN_OPENED'); + } + + function closeHfGuide() { + $('hf-guide-modal').classList.remove('active'); + postMsg('FULLSCREEN_CLOSED'); + } + + function renderHfGuideContent() { + const body = $('hf-guide-body'); + if (!body || body.innerHTML.trim()) return; + + setHtml(body, ` +
+
+
免费自建 Embedding 服务,10 分钟搞定
+
+ 🆓 完全免费 + ⚡ 速度不快 + 🔐 数据私有 +
+
+
+
1创建 Space
+
+

访问 huggingface.co/new-space,登录后创建:

+
    +
  • Space name: 随便取(如 my-embedding
  • +
  • SDK: 选 Docker
  • +
  • Hardware: 选 CPU basic (Free)
  • +
+
+
+
+
2上传 3 个文件
+
+

在 Space 的 Files 页面,依次创建以下文件:

+
+
📄requirements.txt
+
fastapi
+uvicorn
+sentence-transformers
+torch
+
+
+
🐍app.py主程序
+
import os
+os.environ["OMP_NUM_THREADS"] = "1"
+os.environ["MKL_NUM_THREADS"] = "1"
+os.environ["TOKENIZERS_PARALLELISM"] = "false"
+
+import torch
+torch.set_num_threads(1)
+
+import threading
+from functools import lru_cache
+from typing import List, Optional
+from fastapi import FastAPI, HTTPException, Header
+from fastapi.middleware.cors import CORSMiddleware
+from pydantic import BaseModel
+from sentence_transformers import SentenceTransformer
+
+ACCESS_KEY = os.environ.get("ACCESS_KEY", "")
+MODEL_ID = "BAAI/bge-m3"
+
+app = FastAPI()
+app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
+
+@lru_cache(maxsize=1)
+def get_model():
+    return SentenceTransformer(MODEL_ID, trust_remote_code=True)
+
+class EmbedRequest(BaseModel):
+    input: List[str]
+    model: Optional[str] = "bge-m3"
+
+@app.post("/v1/embeddings")
+async def embed(req: EmbedRequest, authorization: Optional[str] = Header(None)):
+    if ACCESS_KEY and (authorization or "").replace("Bearer ", "").strip() != ACCESS_KEY:
+        raise HTTPException(401, "Unauthorized")
+    embeddings = get_model().encode(req.input, normalize_embeddings=True)
+    return {"data": [{"embedding": e.tolist(), "index": i} for i, e in enumerate(embeddings)]}
+
+@app.get("/v1/models")
+async def models():
+    return {"data": [{"id": "bge-m3"}]}
+
+@app.get("/health")
+async def health():
+    return {"status": "ok"}
+
+@app.on_event("startup")
+async def startup():
+    threading.Thread(target=get_model, daemon=True).start()
+
+
+
🐳Dockerfile
+
FROM python:3.10-slim
+WORKDIR /app
+COPY requirements.txt .
+RUN pip install --no-cache-dir -r requirements.txt
+COPY app.py ./
+RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('BAAI/bge-m3', trust_remote_code=True)"
+EXPOSE 7860
+CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "2"]
+
+
+
+
+
3等待构建
+
+

上传完成后自动开始构建,约需 10 分钟(下载模型)。

+

成功后状态变为 Running

+
+
+
+
4在插件中配置
+
+
+
服务渠道OpenAI 兼容
+
API URLhttps://用户名-空间名.hf.space
+
API Key随便填
+
模型点"拉取" → 选 bge-m3
+
+
+
+
+
💡 小提示
+
    +
  • URL 格式:https://用户名-空间名.hf.space(减号连接,非斜杠)
  • +
  • 免费 Space 一段时间无请求会休眠,首次唤醒需等 20-30 秒
  • +
  • 如需保持常驻,可用 cron-job.org 每 5 分钟 ping /health
  • +
  • 如需密码,在 Space Settings 设置 ACCESS_KEY 环境变量
  • +
+
+
+ `); + + // Add copy button handlers + body.querySelectorAll('.copy-btn').forEach(btn => { + btn.onclick = async () => { + const code = btn.previousElementSibling?.textContent || ''; + try { + await navigator.clipboard.writeText(code); + btn.textContent = '已复制'; + setTimeout(() => btn.textContent = '复制', 1200); + } catch { + const ta = document.createElement('textarea'); + ta.value = code; + document.body.appendChild(ta); + ta.select(); + document.execCommand('copy'); + ta.remove(); + btn.textContent = '已复制'; + setTimeout(() => btn.textContent = '复制', 1200); + } + }; + }); + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Recall Log + // ═══════════════════════════════════════════════════════════════════════════ + + function setRecallLog(text) { + lastRecallLogText = text || ''; + updateRecallLogDisplay(); + } + + function updateRecallLogDisplay() { + const content = $('recall-log-content'); + if (!content) return; + if (lastRecallLogText) { + content.textContent = lastRecallLogText; + content.classList.remove('recall-empty'); + } else { + setHtml(content, '
暂无召回日志

当 AI 生成回复时,系统会自动进行记忆召回。
召回日志将显示:
• 查询文本
• L1 片段匹配结果
• L2 事件召回详情
• 耗时统计
'); + } + } + + function openRecallLog() { + updateRecallLogDisplay(); + $('recall-log-modal').classList.add('active'); + postMsg('FULLSCREEN_OPENED'); + } + + function closeRecallLog() { + $('recall-log-modal').classList.remove('active'); + postMsg('FULLSCREEN_CLOSED'); + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Editor + // ═══════════════════════════════════════════════════════════════════════════ + + function preserveAddedAt(n, o) { + if (o?._addedAt != null) n._addedAt = o._addedAt; + return n; + } + + function createDelBtn() { + const b = document.createElement('button'); + b.type = 'button'; + b.className = 'btn btn-sm btn-del'; + b.textContent = '删除'; + return b; + } + + function addDeleteHandler(item) { + const del = createDelBtn(); + (item.querySelector('.struct-actions') || item).appendChild(del); + del.onclick = () => item.remove(); + } + + function renderEventsEditor(events) { + const list = events?.length ? events : [{ id: 'evt-1', title: '', timeLabel: '', summary: '', participants: [], type: '日常', weight: '点睛' }]; + let maxId = 0; + list.forEach(e => { + const m = e.id?.match(/evt-(\d+)/); + if (m) maxId = Math.max(maxId, +m[1]); + }); + + const es = $('editor-struct'); + setHtml(es, list.map(ev => { + const id = ev.id || `evt-${++maxId}`; + return `
+
+ + +
+
+ +
+
+ +
+
+ + +
+
ID:${h(id)}
+
`; + }).join('') + '
'); + + es.querySelectorAll('.event-item').forEach(addDeleteHandler); + + $('event-add').onclick = () => { + let nmax = maxId; + es.querySelectorAll('.event-item').forEach(it => { + const m = it.dataset.id?.match(/evt-(\d+)/); + if (m) nmax = Math.max(nmax, +m[1]); + }); + const nid = `evt-${nmax + 1}`; + const div = document.createElement('div'); + div.className = 'struct-item event-item'; + div.dataset.id = nid; + setHtml(div, ` +
+
+
+
+ + +
+
ID:${h(nid)}
+ `); + addDeleteHandler(div); + es.insertBefore(div, $('event-add').parentElement); + }; + } + + function renderCharactersEditor(data) { + const d = data || { main: [], relationships: [] }; + const main = (d.main || []).map(getCharName); + const rels = d.relationships || []; + const trendOpts = ['破裂', '厌恶', '反感', '陌生', '投缘', '亲密', '交融']; + + const es = $('editor-struct'); + setHtml(es, ` +
+
角色列表
+
+ ${(main.length ? main : ['']).map(n => `
`).join('')} +
+
+
+
+
人物关系
+
+ ${(rels.length ? rels : [{ from: '', to: '', label: '', trend: '陌生' }]).map(r => ` +
+ + + + +
+ `).join('')} +
+
+
+ `); + + es.querySelectorAll('.char-main-item,.char-rel-item').forEach(addDeleteHandler); + + $('char-main-add').onclick = () => { + const div = document.createElement('div'); + div.className = 'struct-row char-main-item'; + setHtml(div, ''); + addDeleteHandler(div); + $('char-main-list').appendChild(div); + }; + + $('char-rel-add').onclick = () => { + const div = document.createElement('div'); + div.className = 'struct-row char-rel-item'; + setHtml(div, ` + + + + + `); + addDeleteHandler(div); + $('char-rel-list').appendChild(div); + }; + } + + function renderArcsEditor(arcs) { + const list = arcs?.length ? arcs : [{ name: '', trajectory: '', progress: 0, moments: [] }]; + const es = $('editor-struct'); + + setHtml(es, ` +
+ ${list.map((a, i) => ` +
+
+
+
+ +
+
+
角色弧光 ${i + 1}
+
+ `).join('')} +
+
+ `); + + es.querySelectorAll('.arc-item').forEach(addDeleteHandler); + + $('arc-add').onclick = () => { + const listEl = $('arc-list'); + const idx = listEl.querySelectorAll('.arc-item').length; + const div = document.createElement('div'); + div.className = 'struct-item arc-item'; + div.dataset.index = idx; + setHtml(div, ` +
+
+
+ +
+
+
角色弧光 ${idx + 1}
+ `); + addDeleteHandler(div); + listEl.appendChild(div); + }; + } + + function openEditor(section) { + currentEditSection = section; + const meta = SECTION_META[section]; + const es = $('editor-struct'); + const ta = $('editor-ta'); + + $('editor-title').textContent = meta.title; + $('editor-hint').textContent = meta.hint; + $('editor-err').classList.remove('visible'); + $('editor-err').textContent = ''; + es.classList.add('hidden'); + ta.classList.remove('hidden'); + + if (section === 'keywords') { + ta.value = summaryData.keywords.map(k => `${k.text}|${k.weight || '一般'}`).join('\n'); + } else if (section === 'world') { + ta.value = (summaryData.world || []) + .map(w => `${w.category || ''}|${w.topic || ''}|${w.content || ''}`) + .join('\n'); + } else { + ta.classList.add('hidden'); + es.classList.remove('hidden'); + if (section === 'events') renderEventsEditor(summaryData.events || []); + else if (section === 'characters') renderCharactersEditor(summaryData.characters || { main: [], relationships: [] }); + else if (section === 'arcs') renderArcsEditor(summaryData.arcs || []); + } + + $('editor-modal').classList.add('active'); + postMsg('EDITOR_OPENED'); + } + + function closeEditor() { + $('editor-modal').classList.remove('active'); + currentEditSection = null; + postMsg('EDITOR_CLOSED'); + } + + function saveEditor() { + const section = currentEditSection; + const es = $('editor-struct'); + const ta = $('editor-ta'); + let parsed; + + try { + if (section === 'keywords') { + const oldMap = new Map((summaryData.keywords || []).map(k => [k.text, k])); + parsed = ta.value.trim().split('\n').filter(l => l.trim()).map(line => { + const [text, weight] = line.split('|').map(s => s.trim()); + return preserveAddedAt({ text: text || '', weight: weight || '一般' }, oldMap.get(text)); + }); + } else if (section === 'events') { + const oldMap = new Map((summaryData.events || []).map(e => [e.id, e])); + parsed = Array.from(es.querySelectorAll('.event-item')).map(it => { + const id = it.dataset.id; + return preserveAddedAt({ + id, + title: it.querySelector('.event-title').value.trim(), + timeLabel: it.querySelector('.event-time').value.trim(), + summary: it.querySelector('.event-summary').value.trim(), + participants: it.querySelector('.event-participants').value.trim().split(/[,、,]/).map(s => s.trim()).filter(Boolean), + type: it.querySelector('.event-type').value, + weight: it.querySelector('.event-weight').value + }, oldMap.get(id)); + }).filter(e => e.title || e.summary); + } else if (section === 'characters') { + const oldMainMap = new Map((summaryData.characters?.main || []).map(m => [getCharName(m), m])); + const mainNames = Array.from(es.querySelectorAll('.char-main-name')).map(i => i.value.trim()).filter(Boolean); + const main = mainNames.map(n => preserveAddedAt({ name: n }, oldMainMap.get(n))); + + const oldRelMap = new Map((summaryData.characters?.relationships || []).map(r => [`${r.from}->${r.to}`, r])); + const rels = Array.from(es.querySelectorAll('.char-rel-item')).map(it => { + const from = it.querySelector('.char-rel-from').value.trim(); + const to = it.querySelector('.char-rel-to').value.trim(); + return preserveAddedAt({ + from, to, + label: it.querySelector('.char-rel-label').value.trim(), + trend: it.querySelector('.char-rel-trend').value + }, oldRelMap.get(`${from}->${to}`)); + }).filter(r => r.from && r.to); + + parsed = { main, relationships: rels }; + } else if (section === 'arcs') { + const oldArcMap = new Map((summaryData.arcs || []).map(a => [a.name, a])); + parsed = Array.from(es.querySelectorAll('.arc-item')).map(it => { + const name = it.querySelector('.arc-name').value.trim(); + const oldArc = oldArcMap.get(name); + const oldMomentMap = new Map((oldArc?.moments || []).map(m => [typeof m === 'string' ? m : m.text, m])); + const momentsRaw = it.querySelector('.arc-moments').value.trim(); + const moments = momentsRaw ? momentsRaw.split('\n').map(s => s.trim()).filter(Boolean).map(t => preserveAddedAt({ text: t }, oldMomentMap.get(t))) : []; + return preserveAddedAt({ + name, + trajectory: it.querySelector('.arc-trajectory').value.trim(), + progress: Math.max(0, Math.min(1, (parseFloat(it.querySelector('.arc-progress').value) || 0) / 100)), + moments + }, oldArc); + }).filter(a => a.name || a.trajectory || a.moments?.length); + } else if (section === 'world') { + const oldWorldMap = new Map((summaryData.world || []).map(w => [`${w.category}|${w.topic}`, w])); + parsed = ta.value + .split('\n') + .map(l => l.trim()) + .filter(Boolean) + .map(line => { + const parts = line.split('|').map(s => s.trim()); + const category = parts[0]; + const topic = parts[1]; + const content = parts.slice(2).join('|').trim(); + if (!category || !topic) return null; + if (!content || content.toLowerCase() === 'cleared') return null; + const key = `${category}|${topic}`; + return preserveAddedAt({ category, topic, content }, oldWorldMap.get(key)); + }) + .filter(Boolean); + } + } catch (e) { + $('editor-err').textContent = `格式错误: ${e.message}`; + $('editor-err').classList.add('visible'); + return; + } + + postMsg('UPDATE_SECTION', { section, data: parsed }); + + if (section === 'keywords') renderKeywords(parsed); + else if (section === 'events') { renderTimeline(parsed); $('stat-events').textContent = parsed.length; } + else if (section === 'characters') renderRelations(parsed); + else if (section === 'arcs') renderArcs(parsed); + else if (section === 'world') renderWorldState(parsed); + + closeEditor(); + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Message Handler + // ═══════════════════════════════════════════════════════════════════════════ + + function handleParentMessage(e) { + if (e.origin !== PARENT_ORIGIN || e.source !== window.parent) return; + + const d = e.data; + if (!d || d.source !== 'LittleWhiteBox') return; + + const btn = $('btn-generate'); + + switch (d.type) { + case 'GENERATION_STATE': + localGenerating = !!d.isGenerating; + btn.textContent = localGenerating ? '停止' : '总结'; + break; + + case 'SUMMARY_BASE_DATA': + if (d.stats) { + updateStats(d.stats); + $('summarized-count').textContent = d.stats.hiddenCount ?? 0; + } + if (d.hideSummarized !== undefined) $('hide-summarized').checked = d.hideSummarized; + if (d.keepVisibleCount !== undefined) $('keep-visible-count').value = d.keepVisibleCount; + break; + + case 'SUMMARY_FULL_DATA': + if (d.payload) { + const p = d.payload; + if (p.keywords) renderKeywords(p.keywords); + if (p.events) renderTimeline(p.events); + if (p.characters) renderRelations(p.characters); + if (p.arcs) renderArcs(p.arcs); + if (p.world) renderWorldState(p.world); + $('stat-events').textContent = p.events?.length || 0; + if (p.lastSummarizedMesId != null) $('stat-summarized').textContent = p.lastSummarizedMesId + 1; + if (p.stats) updateStats(p.stats); + } + break; + + case 'SUMMARY_ERROR': + console.error('Summary error:', d.message); + break; + + case 'SUMMARY_CLEARED': { + const t = d.payload?.totalFloors || 0; + $('stat-events').textContent = 0; + $('stat-summarized').textContent = 0; + $('stat-pending').textContent = t; + $('summarized-count').textContent = 0; + summaryData = { keywords: [], events: [], characters: { main: [], relationships: [] }, arcs: [], world: [] }; + renderKeywords([]); + renderTimeline([]); + renderRelations(null); + renderArcs([]); + renderWorldState([]); + break; + } + + case 'LOAD_PANEL_CONFIG': + if (d.config) applyConfig(d.config); + break; + + case 'VECTOR_CONFIG': + if (d.config) loadVectorConfig(d.config); + break; + + case 'VECTOR_LOCAL_MODEL_STATUS': + updateLocalModelStatus(d.status, d.message); + break; + + case 'VECTOR_LOCAL_MODEL_PROGRESS': + updateLocalModelProgress(d.percent); + break; + + case 'VECTOR_ONLINE_STATUS': + updateOnlineStatus(d.status, d.message); + break; + + case 'VECTOR_ONLINE_MODELS': + updateOnlineModels(d.models || []); + break; + + case 'VECTOR_STATS': + updateVectorStats(d.stats); + if (d.mismatch !== undefined) showVectorMismatchWarning(d.mismatch); + break; + + case 'VECTOR_GEN_PROGRESS': + updateVectorGenProgress(d.phase, d.current, d.total); + break; + + case 'RECALL_LOG': + setRecallLog(d.text || ''); + break; + } + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Event Bindings + // ═══════════════════════════════════════════════════════════════════════════ + + function bindEvents() { + // Section edit buttons + $$('.sec-btn[data-section]').forEach(b => b.onclick = () => openEditor(b.dataset.section)); + + // Editor modal + $('editor-backdrop').onclick = closeEditor; + $('editor-close').onclick = closeEditor; + $('editor-cancel').onclick = closeEditor; + $('editor-save').onclick = saveEditor; + + // Settings modal + $('btn-settings').onclick = openSettings; + $('settings-backdrop').onclick = () => closeSettings(false); + $('settings-close').onclick = () => closeSettings(false); + $('settings-cancel').onclick = () => closeSettings(false); + $('settings-save').onclick = () => closeSettings(true); + + // API provider change + $('api-provider').onchange = e => { + const pv = PROVIDER_DEFAULTS[e.target.value]; + $('api-url').value = ''; + if (!pv.canFetch) config.api.modelCache = []; + updateProviderUI(e.target.value); + }; + + $('btn-connect').onclick = fetchModels; + $('api-model-select').onchange = e => { config.api.model = e.target.value; }; + + // Trigger timing + $('trigger-timing').onchange = e => { + const en = $('trigger-enabled'); + if (e.target.value === 'manual') { + en.checked = false; + en.disabled = true; + en.parentElement.style.opacity = '.5'; + } else { + en.disabled = false; + en.parentElement.style.opacity = '1'; + } + }; + + // Main actions + $('btn-clear').onclick = () => postMsg('REQUEST_CLEAR'); + $('btn-generate').onclick = () => { + const btn = $('btn-generate'); + if (!localGenerating) { + localGenerating = true; + btn.textContent = '停止'; + postMsg('REQUEST_GENERATE', { config: { api: config.api, gen: config.gen, trigger: config.trigger } }); + } else { + localGenerating = false; + btn.textContent = '总结'; + postMsg('REQUEST_CANCEL'); + } + }; + + // Hide summarized + $('hide-summarized').onchange = e => postMsg('TOGGLE_HIDE_SUMMARIZED', { enabled: e.target.checked }); + $('keep-visible-count').onchange = e => { + const c = Math.max(0, Math.min(50, parseInt(e.target.value) || 3)); + e.target.value = c; + postMsg('UPDATE_KEEP_VISIBLE', { count: c }); + }; + + // Fullscreen relations + $('btn-fullscreen-relations').onclick = openRelationsFullscreen; + $('rel-fs-backdrop').onclick = closeRelationsFullscreen; + $('rel-fs-close').onclick = closeRelationsFullscreen; + + // HF guide + $('hf-guide-backdrop').onclick = closeHfGuide; + $('hf-guide-close').onclick = closeHfGuide; + + // Recall log + $('btn-recall').onclick = openRecallLog; + $('recall-log-backdrop').onclick = closeRecallLog; + $('recall-log-close').onclick = closeRecallLog; + + // Character selector + $('char-sel-trigger').onclick = e => { + e.stopPropagation(); + $('char-sel').classList.toggle('open'); + }; + + document.onclick = e => { + const cs = $('char-sel'); + if (cs && !cs.contains(e.target)) cs.classList.remove('open'); + }; + + // Vector UI + initVectorUI(); + + // Resize + window.onresize = () => { + relationChart?.resize(); + relationChartFullscreen?.resize(); + }; + + // Parent messages + window.onmessage = handleParentMessage; + } + + // ═══════════════════════════════════════════════════════════════════════════ + // Init + // ═══════════════════════════════════════════════════════════════════════════ + + function init() { + loadConfig(); + + // Initial state + $('stat-events').textContent = '—'; + $('stat-summarized').textContent = '—'; + $('stat-pending').textContent = '—'; + $('summarized-count').textContent = '0'; + + renderKeywords([]); + renderTimeline([]); + renderArcs([]); + renderWorldState([]); + + bindEvents(); + + // Notify parent + postMsg('FRAME_READY'); + } + + // Start + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', init); + } else { + init(); + } + + + function renderWorldState(world) { + summaryData.world = world || []; + + const container = $('world-state-list'); + if (!container) return; + + if (!world?.length) { + setHtml(container, '
暂无世界状态
'); + return; + } + + const labels = { + status: '状态', + inventory: '物品', + knowledge: '认知', + relation: '关系', + rule: '规则' + }; + + const categoryOrder = ['status', 'inventory', 'relation', 'knowledge', 'rule']; + + const grouped = {}; + world.forEach(w => { + const cat = w.category || 'other'; + if (!grouped[cat]) grouped[cat] = []; + grouped[cat].push(w); + }); + + const html = categoryOrder + .filter(cat => grouped[cat]?.length) + .map(cat => { + const items = grouped[cat].sort((a, b) => (b.floor || 0) - (a.floor || 0)); + return ` +
+
${labels[cat] || cat}
+ ${items.map(w => ` +
+ ${h(w.topic)} + ${h(w.content)} +
+ `).join('')} +
+ `; + }).join(''); + + setHtml(container, html || '
暂无世界状态
'); + } +})(); diff --git a/modules/story-summary/story-summary.css b/modules/story-summary/story-summary.css new file mode 100644 index 0000000..4bdc429 --- /dev/null +++ b/modules/story-summary/story-summary.css @@ -0,0 +1,2141 @@ +/* story-summary.css */ + +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +:root { + --bg: #fafafa; + --bg2: #fff; + --bg3: #f5f5f5; + --txt: #1a1a1a; + --txt2: #444; + --txt3: #666; + --bdr: #dcdcdc; + --bdr2: #e8e8e8; + --acc: #1a1a1a; + --hl: #d87a7a; + --hl-soft: rgba(184, 90, 90, .1); +} + +body { + font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif; + background: var(--bg); + color: var(--txt); + line-height: 1.6; + min-height: 100vh; + -webkit-overflow-scrolling: touch; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Layout + ═══════════════════════════════════════════════════════════════════════════ */ + +.container { + display: flex; + flex-direction: column; + min-height: 100vh; + padding: 24px 40px; + max-width: 1800px; + margin: 0 auto; +} + +header { + display: flex; + justify-content: space-between; + align-items: flex-start; + padding-bottom: 24px; + border-bottom: 1px solid var(--bdr); + margin-bottom: 24px; +} + +main { + display: grid; + grid-template-columns: 1fr 480px; + gap: 24px; + flex: 1; + min-height: 0; +} + +.left, .right { + display: flex; + flex-direction: column; + gap: 24px; + min-height: 0; +} + +/* 关键词卡片:固定高度 */ +.left > .card:first-child { + flex: 0 0 auto; /* 关键词:不伸缩 */ +} +/* ═══════════════════════════════════════════════════════════════════════════ + Typography + ═══════════════════════════════════════════════════════════════════════════ */ + +h1 { + font-size: 2rem; + font-weight: 300; + letter-spacing: -.02em; + margin-bottom: 4px; +} + +h1 span { + font-weight: 600; +} + +.subtitle { + font-size: .875rem; + color: var(--txt3); + letter-spacing: .05em; + text-transform: uppercase; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Stats + ═══════════════════════════════════════════════════════════════════════════ */ + +.stats { + display: flex; + gap: 48px; + text-align: right; +} + +.stat { + display: flex; + flex-direction: column; +} + +.stat-val { + font-size: 2.5rem; + font-weight: 200; + line-height: 1; + letter-spacing: -.03em; +} + +.stat-val .hl { + color: var(--hl); +} + +.stat-lbl { + font-size: .75rem; + color: var(--txt3); + text-transform: uppercase; + letter-spacing: .1em; + margin-top: 4px; +} + +.stat-warning { + font-size: .625rem; + color: #ff9800; + margin-top: 4px; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Controls + ═══════════════════════════════════════════════════════════════════════════ */ + +.controls { + display: flex; + align-items: center; + gap: 12px; + padding: 12px 0; + margin-bottom: 20px; + flex-wrap: wrap; +} + +.spacer { + flex: 1; +} + +.chk-label { + display: flex; + align-items: center; + gap: 8px; + padding: 8px 16px; + background: var(--bg3); + border: 1px solid var(--bdr); + font-size: .8125rem; + color: var(--txt2); + cursor: pointer; + transition: all .2s; +} + +.chk-label:hover { + border-color: var(--acc); +} + +.chk-label input { + width: 16px; + height: 16px; + accent-color: var(--hl); + cursor: pointer; +} + +.chk-label strong { + color: var(--hl); +} + +#keep-visible-count { + width: 32px; + padding: 2px 4px; + margin: 0 2px; + background: var(--bg2); + border: 1px solid var(--bdr); + font-size: inherit; + font-weight: bold; + color: var(--hl); + text-align: center; + border-radius: 3px; +} + +#keep-visible-count:focus { + border-color: var(--acc); + outline: none; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Buttons + ═══════════════════════════════════════════════════════════════════════════ */ + +.btn { + padding: 12px 28px; + background: var(--bg2); + color: var(--txt); + border: 1px solid var(--bdr); + font-size: .875rem; + font-weight: 500; + cursor: pointer; + transition: all .2s; +} + +.btn:hover { + border-color: var(--acc); + background: var(--bg3); +} + +.btn-p { + background: var(--acc); + color: #fff; + border-color: var(--acc); +} + +.btn-p:hover { + background: #555; +} + +.btn-p:disabled { + background: #999; + border-color: #999; + cursor: not-allowed; + opacity: .7; +} + +.btn-icon { + padding: 10px 16px; + display: flex; + align-items: center; + gap: 6px; +} + +.btn-icon svg { + width: 16px; + height: 16px; +} + +.btn-sm { + padding: 8px 16px; + font-size: .8125rem; +} + +.btn-del { + background: transparent; + color: var(--hl); + border-color: var(--hl); +} + +.btn-del:hover { + background: var(--hl-soft); +} + +.btn-group { + display: flex; + gap: 8px; + flex-wrap: nowrap; +} + +.btn-group .btn { + flex: 1; + min-width: 0; + padding: 10px 14px; + text-align: center; + white-space: nowrap; +} + +.btn-group .btn-icon { + padding: 10px 14px; +} + +.btn-recall { + background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); + color: #fff; + border-color: #667eea; + position: relative; + overflow: hidden; +} + +.btn-recall:hover { + background: linear-gradient(135deg, #5a67d8 0%, #6b46a1 100%); + border-color: #5a67d8; +} + +.btn-recall::after { + content: ''; + position: absolute; + top: -50%; + left: -50%; + width: 200%; + height: 200%; + background: linear-gradient(45deg, transparent 40%, rgba(255,255,255,.15) 50%, transparent 60%); + animation: shimmer 3s infinite; +} + +@keyframes shimmer { + 0% { transform: translateX(-100%) rotate(45deg); } + 100% { transform: translateX(100%) rotate(45deg); } +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Cards & Sections + ═══════════════════════════════════════════════════════════════════════════ */ + +.card { + background: var(--bg2); + border: 1px solid var(--bdr); + padding: 24px; +} + +.sec-head { + display: flex; + justify-content: space-between; + align-items: center; + margin-bottom: 16px; +} + +.sec-title { + font-size: .75rem; + font-weight: 600; + text-transform: uppercase; + letter-spacing: .15em; + color: var(--txt2); +} + +.sec-btn { + padding: 4px 12px; + background: transparent; + border: 1px solid var(--bdr); + font-size: .6875rem; + color: var(--txt3); + cursor: pointer; + transition: all .2s; + text-transform: uppercase; + letter-spacing: .05em; +} + +.sec-btn:hover { + border-color: var(--acc); + color: var(--txt); + background: var(--bg3); +} + +.sec-actions { + display: flex; + gap: 8px; + align-items: center; +} + +.sec-icon { + padding: 4px 8px; + display: flex; + align-items: center; + justify-content: center; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Keywords + ═══════════════════════════════════════════════════════════════════════════ */ + +.keywords { + display: flex; + flex-wrap: wrap; + gap: 12px; + margin-top: 16px; +} + +.tag { + padding: 8px 20px; + background: var(--bg3); + border: 1px solid var(--bdr2); + font-size: .875rem; + color: var(--txt2); + transition: all .2s; + cursor: default; +} + +.tag.p { + background: var(--acc); + color: #fff; + border-color: var(--acc); + font-weight: 500; +} + +.tag.s { + background: var(--hl-soft); + border-color: rgba(255, 68, 68, .2); + color: var(--hl); +} + +.tag:hover { + transform: translateY(-2px); + box-shadow: 0 4px 12px rgba(0, 0, 0, .08); +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Timeline + ═══════════════════════════════════════════════════════════════════════════ */ + +.timeline { + flex: 1; + display: flex; + flex-direction: column; + min-height: 0; + max-height: 1140px; +} + +.tl-list { + flex: 1; + overflow-y: auto; + padding-right: 8px; + min-height: 0; +} + +.tl-list::-webkit-scrollbar, +.scroll::-webkit-scrollbar { + width: 4px; +} + +.tl-list::-webkit-scrollbar-thumb, +.scroll::-webkit-scrollbar-thumb { + background: var(--bdr); +} + +.tl-item { + position: relative; + padding-left: 32px; + padding-bottom: 32px; + border-left: 1px solid var(--bdr); + margin-left: 8px; +} + +.tl-item:last-child { + border-left-color: transparent; + padding-bottom: 0; +} + +.tl-dot { + position: absolute; + left: -5px; + top: 0; + width: 9px; + height: 9px; + background: var(--bg2); + border: 2px solid var(--txt3); + border-radius: 50%; + transition: all .2s; +} + +.tl-item:hover .tl-dot { + border-color: var(--hl); + background: var(--hl); + transform: scale(1.3); +} + +.tl-item.crit .tl-dot { + border-color: var(--hl); + background: var(--hl); +} + +.tl-head { + display: flex; + justify-content: space-between; + align-items: baseline; + margin-bottom: 8px; +} + +.tl-title { + font-size: 1rem; + font-weight: 500; +} + +.tl-time { + font-size: .75rem; + color: var(--txt3); + font-variant-numeric: tabular-nums; +} + +.tl-brief { + font-size: .875rem; + color: var(--txt2); + line-height: 1.7; + margin-bottom: 12px; +} + +.tl-meta { + display: flex; + gap: 16px; + font-size: .75rem; + color: var(--txt3); +} + +.tl-meta .imp { + color: var(--hl); + font-weight: 500; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Relations Chart + ═══════════════════════════════════════════════════════════════════════════ */ + +.relations { + flex: 1; + display: flex; + flex-direction: column; + min-height: 0; + max-height: 480px; +} + +#relation-chart, +#relation-chart-fullscreen { + width: 100%; + flex: 1; + min-height: 0; + touch-action: none; +} + + +/* ═══════════════════════════════════════════════════════════════════════════ + Profile + ═══════════════════════════════════════════════════════════════════════════ */ + +.profile { + flex: 1; + display: flex; + flex-direction: column; + min-height: 0; + max-height: 480px; +} + +.profile-content { + flex: 1; + overflow-y: auto; + padding-right: 8px; + min-height: 0; +} + +.prof-arc { + padding: 16px; + margin-bottom: 24px; +} + +.prof-name { + font-size: 1.125rem; + font-weight: 600; + margin-bottom: 4px; +} + +.prof-traj { + font-size: .8125rem; + color: var(--txt3); + line-height: 1.5; +} + +.prof-prog-wrap { + margin-bottom: 16px; +} + +.prof-prog-lbl { + display: flex; + justify-content: space-between; + font-size: .75rem; + color: var(--txt3); + margin-bottom: 6px; +} + +.prof-prog { + height: 4px; + background: var(--bdr); + border-radius: 2px; + overflow: hidden; +} + +.prof-prog-inner { + height: 100%; + background: linear-gradient(90deg, var(--hl), #d85858); + border-radius: 2px; + transition: width .6s; +} + +.prof-moments { + background: var(--bg2); + border-left: 3px solid var(--hl); + padding: 12px 16px; +} + +.prof-moments-title { + font-size: .6875rem; + font-weight: 600; + text-transform: uppercase; + letter-spacing: .1em; + color: var(--txt3); + margin-bottom: 8px; +} + +.prof-moment { + position: relative; + padding-left: 16px; + margin-bottom: 6px; + font-size: .8125rem; + color: var(--txt2); + line-height: 1.5; +} + +.prof-moment::before { + content: ''; + position: absolute; + left: 0; + top: 7px; + width: 6px; + height: 6px; + background: var(--hl); + border-radius: 50%; +} + +.prof-moment:last-child { + margin-bottom: 0; +} + +.prof-rels { + display: flex; + flex-direction: column; +} + +.rels-group { + border-bottom: 1px solid var(--bdr2); + padding: 16px 0; +} + +.rels-group:last-child { + border-bottom: none; + padding-bottom: 0; +} + +.rels-group:first-child { + padding-top: 0; +} + +.rels-group-title { + font-size: .75rem; + font-weight: 600; + color: var(--txt3); + margin-bottom: 12px; + display: flex; + align-items: center; + gap: 8px; +} + +.rel-item { + display: flex; + align-items: baseline; + gap: 8px; + padding: 4px 8px; + border-radius: 4px; + margin-bottom: 2px; +} + +.rel-item:hover { + background: var(--bg3); +} + +.rel-target { + font-size: .9rem; + color: var(--txt2); + white-space: nowrap; + min-width: 60px; +} + +.rel-label { + font-size: .7rem; + line-height: 1.5; + flex: 1; +} + +.rel-trend { + font-size: .6875rem; + padding: 2px 8px; + border-radius: 10px; + white-space: nowrap; +} + +.trend-broken { background: rgba(68, 68, 68, .15); color: #444; } +.trend-hate { background: rgba(139, 0, 0, .15); color: #8b0000; } +.trend-dislike { background: rgba(205, 92, 92, .15); color: #cd5c5c; } +.trend-stranger { background: rgba(136, 136, 136, .15); color: #888; } +.trend-click { background: rgba(102, 205, 170, .15); color: #4a9a7e; } +.trend-close { background: rgba(235, 106, 106, .15); color: var(--hl); } +.trend-merge { background: rgba(199, 21, 133, .2); color: #c71585; } + +/* ═══════════════════════════════════════════════════════════════════════════ + Custom Select + ═══════════════════════════════════════════════════════════════════════════ */ + +.custom-select { + position: relative; + min-width: 140px; + font-size: .8125rem; +} + +.sel-trigger { + display: flex; + align-items: center; + justify-content: space-between; + padding: 6px 12px; + background: var(--bg3); + border: 1px solid var(--bdr); + border-radius: 6px; + cursor: pointer; + transition: all .2s; + user-select: none; +} + +.sel-trigger:hover { + border-color: var(--acc); + background: var(--bg2); +} + +.sel-trigger::after { + content: ''; + width: 16px; + height: 16px; + background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238d8d8d' stroke-width='2'%3e%3cpath d='M6 9l6 6 6-6'/%3e%3c/svg%3e") center/16px no-repeat; + transition: transform .2s; +} + +.custom-select.open .sel-trigger::after { + transform: rotate(180deg); +} + +.sel-opts { + position: absolute; + top: calc(100% + 4px); + left: 0; + right: 0; + background: var(--bg2); + border: 1px solid var(--bdr); + border-radius: 6px; + box-shadow: 0 4px 20px rgba(0, 0, 0, .15); + z-index: 100; + display: none; + max-height: 240px; + overflow-y: auto; + padding: 4px; +} + +.custom-select.open .sel-opts { + display: block; + animation: fadeIn .2s; +} + +.sel-opt { + padding: 8px 12px; + cursor: pointer; + border-radius: 4px; + transition: background .1s; +} + +.sel-opt:hover { + background: var(--bg3); +} + +.sel-opt.sel { + background: var(--hl-soft); + color: var(--hl); + font-weight: 600; +} + +@keyframes fadeIn { + from { opacity: 0; transform: translateY(-4px); } + to { opacity: 1; transform: translateY(0); } +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Modal + ═══════════════════════════════════════════════════════════════════════════ */ + +.modal { + position: fixed; + inset: 0; + z-index: 10000; + display: none; + align-items: center; + justify-content: center; +} + +.modal.active { + display: flex; +} + +.modal-bg { + position: absolute; + inset: 0; + background: rgba(0, 0, 0, .5); + backdrop-filter: blur(4px); +} + +.modal-box { + position: relative; + width: 100%; + max-width: 720px; + max-height: 90vh; + background: var(--bg2); + border: 1px solid var(--bdr); + overflow: hidden; + display: flex; + flex-direction: column; +} + +.modal-head { + display: flex; + justify-content: space-between; + align-items: center; + padding: 20px 24px; + border-bottom: 1px solid var(--bdr); +} + +.modal-head h2 { + font-size: 1rem; + font-weight: 600; + text-transform: uppercase; + letter-spacing: .1em; +} + +.modal-close { + width: 32px; + height: 32px; + display: flex; + align-items: center; + justify-content: center; + background: transparent; + border: 1px solid var(--bdr); + cursor: pointer; + transition: all .2s; +} + +.modal-close:hover { + background: var(--bg3); + border-color: var(--acc); +} + +.modal-close svg { + width: 14px; + height: 14px; +} + +.modal-body { + flex: 1; + overflow-y: auto; + padding: 24px; +} + +.modal-foot { + display: flex; + justify-content: flex-end; + gap: 12px; + padding: 16px 24px; + border-top: 1px solid var(--bdr); +} + +.fullscreen .modal-box { + width: calc(100vw - 48px); + max-width: none; + height: calc(100vh - 48px); + max-height: none; +} + +.fullscreen .modal-body { + flex: 1; + padding: 0; + overflow: hidden; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Editor + ═══════════════════════════════════════════════════════════════════════════ */ + +.editor-ta { + width: 100%; + min-height: 300px; + padding: 16px; + background: var(--bg3); + border: 1px solid var(--bdr); + font-family: 'SF Mono', Monaco, Consolas, monospace; + font-size: .8125rem; + line-height: 1.6; + color: var(--txt); + resize: vertical; + outline: none; +} + +.editor-ta:focus { + border-color: var(--acc); +} + +.editor-hint { + font-size: .75rem; + color: var(--txt3); + margin-bottom: 12px; + line-height: 1.5; +} + +.editor-err { + padding: 12px; + background: var(--hl-soft); + border: 1px solid rgba(255, 68, 68, .3); + color: var(--hl); + font-size: .8125rem; + margin-top: 12px; + display: none; +} + +.editor-err.visible { + display: block; +} + +.struct-item { + border: 1px solid var(--bdr); + background: var(--bg3); + padding: 12px; + margin-bottom: 8px; + display: flex; + flex-direction: column; + gap: 6px; +} + +.struct-row { + display: flex; + gap: 8px; + flex-wrap: wrap; +} + +.struct-row input, +.struct-row select, +.struct-row textarea { + flex: 1; + min-width: 0; + padding: 8px 10px; + background: var(--bg2); + border: 1px solid var(--bdr); + font-size: .8125rem; + color: var(--txt); + outline: none; + transition: border-color .2s; +} + +.struct-row input:focus, +.struct-row select:focus, +.struct-row textarea:focus { + border-color: var(--acc); +} + +.struct-row textarea { + resize: vertical; + font-family: inherit; + min-height: 60px; +} + +.struct-actions { + display: flex; + justify-content: space-between; + align-items: center; + margin-top: 4px; +} + +.struct-actions span { + font-size: .75rem; + color: var(--txt3); +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Settings + ═══════════════════════════════════════════════════════════════════════════ */ + +.settings-section { + margin-bottom: 32px; +} + +.settings-section:last-child { + margin-bottom: 0; +} + +.settings-section-title { + font-size: .6875rem; + font-weight: 600; + text-transform: uppercase; + letter-spacing: .15em; + color: var(--txt3); + margin-bottom: 16px; + padding-bottom: 8px; + border-bottom: 1px solid var(--bdr2); +} + +.settings-row { + display: flex; + gap: 16px; + margin-bottom: 16px; + flex-wrap: wrap; +} + +.settings-row:last-child { + margin-bottom: 0; +} + +.settings-field { + display: flex; + flex-direction: column; + gap: 6px; + flex: 1; + min-width: 200px; +} + +.settings-field.full { + flex: 100%; +} + +.settings-field label { + font-size: .75rem; + color: var(--txt3); + text-transform: uppercase; + letter-spacing: .05em; +} + +.settings-field input, +.settings-field select { + padding: 10px 14px; + background: var(--bg3); + border: 1px solid var(--bdr); + font-size: .875rem; + color: var(--txt); + outline: none; + transition: border-color .2s; +} + +.settings-field input:focus, +.settings-field select:focus { + border-color: var(--acc); +} + +.settings-field input[type="password"] { + letter-spacing: .15em; +} + +.settings-field-inline { + display: flex; + align-items: center; + gap: 8px; +} + +.settings-field-inline input[type="checkbox"] { + width: 18px; + height: 18px; + accent-color: var(--acc); +} + +.settings-field-inline label { + font-size: .8125rem; + color: var(--txt2); + text-transform: none; + letter-spacing: 0; +} + +.settings-hint { + font-size: .75rem; + color: var(--txt3); + margin-top: 4px; +} + +.settings-btn-row { + display: flex; + gap: 12px; + margin-top: 8px; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Vector Settings + ═══════════════════════════════════════════════════════════════════════════ */ + +.engine-selector { + display: flex; + gap: 16px; + margin-top: 8px; +} + +.engine-option { + display: flex; + align-items: center; + gap: 6px; + cursor: pointer; + font-size: .875rem; + color: var(--txt2); +} + +.engine-option input { + accent-color: var(--hl); +} + +.engine-area { + margin-top: 12px; + padding: 16px; + background: var(--bg3); + border: 1px solid var(--bdr); +} + +.engine-card { + text-align: center; +} + +.engine-card-title { + font-size: 1rem; + font-weight: 600; + margin-bottom: 4px; +} + +.engine-card-desc { + font-size: .75rem; + color: var(--txt3); + margin-bottom: 12px; +} + +.engine-status { + display: flex; + align-items: center; + justify-content: center; + gap: 6px; + font-size: .8125rem; + margin-bottom: 12px; +} + +.status-dot { + width: 8px; + height: 8px; + border-radius: 50%; + background: var(--txt3); +} + +.status-dot.ready { background: #22c55e; } +.status-dot.cached { background: #3b82f6; } +.status-dot.downloading { background: #f59e0b; animation: pulse 1s infinite; } +.status-dot.error { background: #ef4444; } +.status-dot.success { background: #22c55e; } + +@keyframes pulse { + 0%, 100% { opacity: 1; } + 50% { opacity: .5; } +} + +.engine-progress { + margin: 12px 0; +} + +.progress-bar { + height: 6px; + background: var(--bdr); + border-radius: 3px; + overflow: hidden; +} + +.progress-inner { + height: 100%; + background: linear-gradient(90deg, var(--hl), #d85858); + border-radius: 3px; + width: 0%; + transition: width .3s; +} + +.progress-text { + font-size: .75rem; + color: var(--txt3); + display: block; + text-align: center; + margin-top: 4px; +} + +.engine-actions { + display: flex; + gap: 8px; + justify-content: center; + flex-wrap: wrap; +} + +.model-select-row { + display: flex; + gap: 8px; + align-items: center; + margin-bottom: 12px; +} + +.model-select-row select { + flex: 1; + padding: 8px 12px; + background: var(--bg2); + border: 1px solid var(--bdr); + font-size: .875rem; + color: var(--txt); +} + +.model-desc { + font-size: .75rem; + color: var(--txt3); + text-align: center; + margin-bottom: 12px; +} + +.vector-stats { + display: flex; + gap: 8px; + font-size: .875rem; + color: var(--txt2); + margin-top: 8px; +} + +.vector-stats strong { + color: var(--hl); +} + +.vector-mismatch-warning { + font-size: .75rem; + color: #f59e0b; + margin-top: 6px; +} + +.vector-chat-section { + border-top: 1px solid var(--bdr); + padding-top: 16px; + margin-top: 16px; +} + +#vector-action-row { + display: flex; + gap: 8px; + justify-content: center; + width: 100%; +} + +#vector-action-row .btn { + flex: 1; + min-width: 0; +} + +.provider-hint { + font-size: .75rem; + color: var(--txt3); + margin-top: 4px; +} + +.provider-hint a { + color: var(--hl); +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Recall Log + ═══════════════════════════════════════════════════════════════════════════ */ + +#recall-log-modal .modal-box { + max-width: 900px; +} + +#recall-log-content { + white-space: pre-wrap; + font-family: 'SF Mono', Monaco, Consolas, 'Courier New', monospace; + font-size: 12px; + line-height: 1.6; + background: var(--bg3); + padding: 16px; + border-radius: 4px; + min-height: 200px; + max-height: 60vh; + overflow-y: auto; +} + +.recall-empty { + color: var(--txt3); + text-align: center; + padding: 40px; + font-style: italic; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + HF Guide + ═══════════════════════════════════════════════════════════════════════════ */ + +.hf-guide { + font-size: .875rem; + line-height: 1.7; +} + +.hf-section { + margin-bottom: 28px; + padding-bottom: 24px; + border-bottom: 1px solid var(--bdr2); +} + +.hf-section:last-child { + border-bottom: none; + margin-bottom: 0; + padding-bottom: 0; +} + +.hf-intro { + background: linear-gradient(135deg, rgba(102,126,234,.08), rgba(118,75,162,.08)); + border: 1px solid rgba(102,126,234,.2); + border-radius: 8px; + padding: 20px; + text-align: center; + border-bottom: none; +} + +.hf-intro-text { + font-size: 1.1rem; + margin-bottom: 12px; +} + +.hf-intro-badges { + display: flex; + gap: 12px; + justify-content: center; + flex-wrap: wrap; +} + +.hf-badge { + padding: 4px 12px; + background: var(--bg2); + border: 1px solid var(--bdr); + border-radius: 20px; + font-size: .75rem; + color: var(--txt2); +} + +.hf-step-header { + display: flex; + align-items: center; + gap: 12px; + margin-bottom: 16px; +} + +.hf-step-num { + width: 28px; + height: 28px; + background: var(--acc); + color: #fff; + border-radius: 50%; + display: flex; + align-items: center; + justify-content: center; + font-weight: 600; + font-size: .875rem; + flex-shrink: 0; +} + +.hf-step-title { + font-size: 1rem; + font-weight: 600; + color: var(--txt); +} + +.hf-step-content { + padding-left: 40px; +} + +.hf-step-content p { + margin: 0 0 12px; +} + +.hf-step-content a { + color: var(--hl); + text-decoration: none; +} + +.hf-step-content a:hover { + text-decoration: underline; +} + +.hf-checklist { + margin: 12px 0; + padding-left: 20px; +} + +.hf-checklist li { + margin-bottom: 6px; +} + +.hf-checklist li::marker { + color: var(--hl); +} + +.hf-checklist code, +.hf-faq code { + background: var(--bg3); + padding: 2px 6px; + border-radius: 3px; + font-size: .8125rem; +} + +.hf-file { + margin-bottom: 16px; + border: 1px solid var(--bdr); + border-radius: 6px; + overflow: hidden; +} + +.hf-file:last-child { + margin-bottom: 0; +} + +.hf-file-header { + display: flex; + align-items: center; + gap: 8px; + padding: 10px 14px; + background: var(--bg3); + border-bottom: 1px solid var(--bdr); + font-size: .8125rem; +} + +.hf-file-icon { + font-size: 1rem; +} + +.hf-file-name { + font-weight: 600; + font-family: 'SF Mono', Monaco, Consolas, monospace; +} + +.hf-file-note { + color: var(--txt3); + font-size: .75rem; + margin-left: auto; +} + +.hf-code { + margin: 0; + padding: 14px; + background: #1e1e1e; + overflow-x: auto; + position: relative; +} + +.hf-code code { + font-family: 'SF Mono', Monaco, Consolas, 'Courier New', monospace; + font-size: .75rem; + line-height: 1.5; + color: #d4d4d4; + display: block; + white-space: pre; +} + +.hf-code .copy-btn { + position: absolute; + right: 8px; + top: 8px; + padding: 4px 10px; + background: rgba(255,255,255,.1); + border: 1px solid rgba(255,255,255,.2); + color: #999; + font-size: .6875rem; + cursor: pointer; + border-radius: 4px; + transition: all .2s; +} + +.hf-code .copy-btn:hover { + background: rgba(255,255,255,.2); + color: #fff; +} + +.hf-status-badge { + display: inline-block; + padding: 2px 10px; + background: rgba(34,197,94,.15); + color: #22c55e; + border-radius: 10px; + font-size: .75rem; + font-weight: 500; +} + +.hf-config-table { + background: var(--bg3); + border: 1px solid var(--bdr); + border-radius: 6px; + overflow: hidden; +} + +.hf-config-row { + display: flex; + padding: 12px 16px; + border-bottom: 1px solid var(--bdr); +} + +.hf-config-row:last-child { + border-bottom: none; +} + +.hf-config-label { + width: 100px; + flex-shrink: 0; + font-weight: 500; + color: var(--txt2); +} + +.hf-config-value { + flex: 1; + color: var(--txt); +} + +.hf-config-value code { + background: var(--bg2); + padding: 2px 6px; + border-radius: 3px; + font-size: .8125rem; + word-break: break-all; +} + +.hf-faq { + background: var(--bg3); + border: 1px solid var(--bdr); + border-radius: 6px; + padding: 16px 20px; + border-bottom: none; +} + +.hf-faq-title { + font-weight: 600; + margin-bottom: 12px; + color: var(--txt); +} + +.hf-faq ul { + margin: 0; + padding-left: 20px; +} + +.hf-faq li { + margin-bottom: 8px; + color: var(--txt2); +} + +.hf-faq li:last-child { + margin-bottom: 0; +} + +.hf-faq a { + color: var(--hl); +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Utilities + ═══════════════════════════════════════════════════════════════════════════ */ + +.hidden { + display: none !important; +} + +.empty { + text-align: center; + padding: 40px; + color: var(--txt3); + font-size: .875rem; +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Responsive - Tablet + ═══════════════════════════════════════════════════════════════════════════ */ + +@media (max-width: 1200px) { + .container { + padding: 16px 24px; + } + + main { + grid-template-columns: 1fr; + } + + .right { + display: grid; + grid-template-columns: 1fr 1fr; + gap: 16px; + } + + .relations, + .world-state, + .profile { + min-height: 280px; + } +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Responsive - Mobile + ═══════════════════════════════════════════════════════════════════════════ */ + +@media (max-width: 768px) { + .container { + height: auto; + min-height: 100vh; + padding: 16px; + } + + header { + flex-direction: column; + gap: 16px; + padding-bottom: 16px; + margin-bottom: 16px; + } + + h1 { + font-size: 1.5rem; + } + + .stats { + width: 100%; + justify-content: space-between; + gap: 16px; + text-align: center; + } + + .stat-val { + font-size: 1.75rem; + } + + .stat-lbl { + font-size: .625rem; + } + + .controls { + flex-wrap: wrap; + gap: 8px; + padding: 10px 0; + margin-bottom: 16px; + } + + .spacer { + display: none; + } + + .chk-label { + width: 100%; + justify-content: center; + } + + .btn-group { + width: 100%; + display: grid; + grid-template-columns: repeat(4, 1fr); + gap: 6px; + } + + .btn-group .btn { + padding: 10px 8px; + font-size: .75rem; + } + + .btn-group .btn-icon { + padding: 10px 8px; + justify-content: center; + } + + .btn-group .btn-icon svg { + width: 14px; + height: 14px; + } + + .btn-group .btn-icon span { + display: none; + } + + main { + display: flex; + flex-direction: column; + gap: 16px; + } + + .left, .right { + gap: 16px; + } + + .right { + display: flex; + flex-direction: column; + } + + .timeline { + max-height: 400px; + } + + /* 关键:relations 和 profile 完全一致的高度 */ + .relations, + .profile { + min-height: 350px; + max-height: 350px; + height: 350px; + } + + #relation-chart { + height: 100%; + min-height: 300px; + } + + .world-state { + min-height: 180px; + max-height: 180px; + } + + .card { + padding: 16px; + } + + .keywords { + gap: 8px; + margin-top: 12px; + } + + .tag { + padding: 6px 14px; + font-size: .8125rem; + } + + .tl-item { + padding-left: 24px; + padding-bottom: 24px; + } + + .tl-title { + font-size: .9375rem; + } + + .tl-brief { + font-size: .8125rem; + line-height: 1.6; + } + + .modal-box { + max-width: 100%; + max-height: 100%; + height: 100%; + border: none; + } + + .modal-head, + .modal-body, + .modal-foot { + padding: 16px; + } + + .settings-row { + flex-direction: column; + gap: 12px; + } + + .settings-field { + min-width: 100%; + } + + .settings-field input, + .settings-field select { + padding: 12px 14px; + font-size: 1rem; + } + + .fullscreen .modal-box { + width: 100%; + height: 100%; + border-radius: 0; + } + + .hf-step-content { + padding-left: 0; + margin-top: 12px; + } + + .hf-config-row { + flex-direction: column; + gap: 4px; + } + + .hf-config-label { + width: auto; + font-size: .75rem; + color: var(--txt3); + } + + .hf-intro-badges { + gap: 8px; + } + + .hf-badge { + font-size: .6875rem; + padding: 3px 10px; + } +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Responsive - Small Mobile + ═══════════════════════════════════════════════════════════════════════════ */ + +@media (max-width: 480px) { + .container { + padding: 12px; + } + + header { + padding-bottom: 12px; + margin-bottom: 12px; + } + + h1 { + font-size: 1.25rem; + } + + .subtitle { + font-size: .6875rem; + } + + .stats { + gap: 8px; + } + + .stat { + flex: 1; + } + + .stat-val { + font-size: 1.5rem; + } + + .controls { + gap: 6px; + padding: 8px 0; + margin-bottom: 12px; + } + + .btn-group { + gap: 4px; + } + + .btn-group .btn { + padding: 10px 6px; + font-size: .6875rem; + } + + main, .left, .right { + gap: 12px; + } + + .card { + padding: 12px; + } + + .sec-title { + font-size: .6875rem; + } + + .sec-btn { + font-size: .625rem; + padding: 3px 8px; + } + + /* 小屏也保持一致 */ + .relations, + .profile { + min-height: 300px; + max-height: 300px; + height: 300px; + } + + #relation-chart { + height: 100%; + min-height: 250px; + } + + .world-state { + min-height: 150px; + max-height: 150px; + } + + .keywords { + gap: 6px; + margin-top: 10px; + } + + .tag { + padding: 5px 10px; + font-size: .75rem; + } + + .tl-item { + padding-left: 20px; + padding-bottom: 20px; + margin-left: 6px; + } + + .tl-dot { + width: 7px; + height: 7px; + left: -4px; + } + + .tl-head { + flex-direction: column; + align-items: flex-start; + gap: 2px; + } + + .tl-title { + font-size: .875rem; + } + + .tl-time { + font-size: .6875rem; + } + + .tl-brief { + font-size: .8rem; + margin-bottom: 8px; + } + + .tl-meta { + flex-direction: column; + gap: 4px; + font-size: .6875rem; + } + + .modal-head h2 { + font-size: .875rem; + } + + .settings-section-title { + font-size: .625rem; + } + + .settings-field label { + font-size: .6875rem; + } + + .settings-field-inline label { + font-size: .75rem; + } + + .settings-hint { + font-size: .6875rem; + } + + .btn-sm { + padding: 10px 14px; + font-size: .75rem; + width: 100%; + } + + .editor-ta { + min-height: 200px; + font-size: .75rem; + } +} + +/* ═══════════════════════════════════════════════════════════════════════════ + Touch Devices + ═══════════════════════════════════════════════════════════════════════════ */ + +@media (hover: none) and (pointer: coarse) { + .btn { + min-height: 44px; + } + + .tag { + min-height: 36px; + display: flex; + align-items: center; + } + + .tag:hover { + transform: none; + } + + .tl-item:hover .tl-dot { + transform: none; + } + + .modal-close { + width: 44px; + height: 44px; + } + + .settings-field input, + .settings-field select { + min-height: 44px; + } + + .settings-field-inline input[type="checkbox"] { + width: 22px; + height: 22px; + } + + .sec-btn { + min-height: 32px; + padding: 6px 12px; + } +} + +/* ═══════════════════════════════════════════════════════════════════════════ + World State (L3) + ═══════════════════════════════════════════════════════════════════════════ */ + +.world-state { + flex: 0 0 auto; +} + +.world-state-list { + max-height: 200px; + overflow-y: auto; + padding-right: 4px; +} + +.world-group { + margin-bottom: 16px; +} + +.world-group:last-child { + margin-bottom: 0; +} + +.world-group-title { + display: flex; + align-items: center; + gap: 6px; + font-size: 0.6875rem; + font-weight: 600; + text-transform: uppercase; + letter-spacing: 0.08em; + color: var(--txt3); + margin-bottom: 8px; + padding-bottom: 6px; + border-bottom: 1px solid var(--bdr2); +} + +.world-item { + display: flex; + align-items: flex-start; + gap: 8px; + padding: 8px 10px; + margin-bottom: 6px; + background: var(--bg3); + border: 1px solid var(--bdr2); + border-radius: 6px; + font-size: 0.8125rem; + transition: all 0.15s ease; +} + +.world-item:hover { + border-color: var(--bdr); + background: var(--bg2); +} + +.world-item:last-child { + margin-bottom: 0; +} + +.world-topic { + font-weight: 600; + color: var(--txt); + white-space: nowrap; + flex-shrink: 0; +} + +.world-topic::after { + content: ''; +} + +.world-content { + color: var(--txt2); + flex: 1; + line-height: 1.5; +} + +/* 分类图标颜色 */ +.world-group[data-category="status"] .world-group-title { + color: #e57373; +} + +.world-group[data-category="inventory"] .world-group-title { + color: #64b5f6; +} + +.world-group[data-category="relation"] .world-group-title { + color: #ba68c8; +} + +.world-group[data-category="knowledge"] .world-group-title { + color: #4db6ac; +} + +.world-group[data-category="rule"] .world-group-title { + color: #ffd54f; +} + +/* 空状态 */ +.world-state-list .empty { + padding: 24px; + font-size: 0.8125rem; +} + +/* 响应式调整 */ +@media (max-width: 768px) { + .world-state { + max-height: none; + } + + .world-state-list { + max-height: 180px; + } + + .world-item { + flex-direction: column; + gap: 4px; + padding: 8px; + } + + .world-topic::after { + content: ''; + } +} + +@media (max-width: 480px) { + .world-state-list { + max-height: 150px; + } + + .world-group-title { + font-size: 0.625rem; + } + + .world-item { + font-size: 0.75rem; + padding: 6px 8px; + } +} diff --git a/modules/story-summary/story-summary.html b/modules/story-summary/story-summary.html index 0d011fa..1071b87 100644 --- a/modules/story-summary/story-summary.html +++ b/modules/story-summary/story-summary.html @@ -1,1393 +1,16 @@ + - 剧情总结 · Story Summary - + -
+

剧情总结

@@ -1409,59 +32,84 @@
+ +
- - - +
+ + + + +
+ +
+
-
核心关键词
+
核心关键词
+
+ +
-
剧情时间线
+
剧情时间线
+
+
+ +
+
+
世界状态
+ +
+
+
+ +
人物关系
- +
+ +
人物档案
-
选择角色 +
+ 选择角色
暂无角色
@@ -1476,15 +124,17 @@
+ + + - + +
+ + +
@@ -2155,6 +2162,7 @@ function normalizeMySpeakers(list) { name: String(item?.name || '').trim(), value: String(item?.value || '').trim(), source: item?.source || getVoiceSource(item?.value || ''), + resourceId: item?.resourceId || null, })).filter(item => item.value); } @@ -2265,11 +2273,14 @@ function doTestVoice(speaker, source, textElId, statusElId) { setTestStatus(statusElId, 'playing', '正在合成...'); + const speakerItem = mySpeakers.find(s => s.value === speaker); + const resolvedResourceId = speakerItem?.resourceId; + post('xb-tts:test-speak', { text, speaker, source, - resourceId: source === 'auth' ? inferResourceIdBySpeaker(speaker) : '', + resourceId: source === 'auth' ? (resolvedResourceId || inferResourceIdBySpeaker(speaker)) : '', }); } @@ -2437,10 +2448,11 @@ document.addEventListener('DOMContentLoaded', () => { $('addMySpeakerBtn').addEventListener('click', () => { const id = $('newVoiceId').value.trim(); const name = $('newVoiceName').value.trim(); + const resourceId = $('newVoiceResourceId').value; if (!id) { post('xb-tts:toast', { type: 'error', message: '请输入音色ID' }); return; } if (!isInMyList(id)) { - mySpeakers.push({ name: name || id, value: id, source: 'auth' }); + mySpeakers.push({ name: name || id, value: id, source: 'auth', resourceId }); } selectedVoiceValue = id; $('newVoiceId').value = ''; diff --git a/modules/tts/tts.js b/modules/tts/tts.js index 42267ad..561aeb3 100644 --- a/modules/tts/tts.js +++ b/modules/tts/tts.js @@ -254,7 +254,8 @@ function resolveSpeakerWithSource(speakerName, mySpeakers, defaultSpeaker) { const defaultItem = list.find(s => s.value === defaultSpeaker); return { value: defaultSpeaker, - source: defaultItem?.source || getVoiceSource(defaultSpeaker) + source: defaultItem?.source || getVoiceSource(defaultSpeaker), + resourceId: defaultItem?.resourceId || null }; } @@ -264,7 +265,8 @@ function resolveSpeakerWithSource(speakerName, mySpeakers, defaultSpeaker) { if (byName?.value) { return { value: byName.value, - source: byName.source || getVoiceSource(byName.value) + source: byName.source || getVoiceSource(byName.value), + resourceId: byName.resourceId || null }; } @@ -274,12 +276,13 @@ function resolveSpeakerWithSource(speakerName, mySpeakers, defaultSpeaker) { if (byValue?.value) { return { value: byValue.value, - source: byValue.source || getVoiceSource(byValue.value) + source: byValue.source || getVoiceSource(byValue.value), + resourceId: byValue.resourceId || null }; } if (FREE_VOICE_KEYS.has(speakerName)) { - return { value: speakerName, source: 'free' }; + return { value: speakerName, source: 'free', resourceId: null }; } // ★ 回退到默认,这是问题发生的地方 @@ -288,7 +291,8 @@ function resolveSpeakerWithSource(speakerName, mySpeakers, defaultSpeaker) { const defaultItem = list.find(s => s.value === defaultSpeaker); return { value: defaultSpeaker, - source: defaultItem?.source || getVoiceSource(defaultSpeaker) + source: defaultItem?.source || getVoiceSource(defaultSpeaker), + resourceId: defaultItem?.resourceId || null }; } @@ -623,7 +627,8 @@ async function speakMessage(messageId, { mode = 'manual' } = {}) { return { ...seg, resolvedSpeaker: resolved.value, - resolvedSource: resolved.source + resolvedSource: resolved.source, + resolvedResourceId: resolved.resourceId }; }); @@ -1325,7 +1330,7 @@ export async function initTts() { return; } - const resourceId = options.resourceId || inferResourceIdBySpeaker(resolved.value); + const resourceId = options.resourceId || resolved.resourceId || inferResourceIdBySpeaker(resolved.value); const result = await synthesizeV3({ appId: config.volc.appId, accessKey: config.volc.accessKey,