@delali/narsil 0.3.0 → 0.3.1-next.101

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/README.md +9 -8
  2. package/dist/adapters/filesystem.d.ts +1 -1
  3. package/dist/adapters/filesystem.mjs +1 -1
  4. package/dist/adapters/indexeddb.d.ts +1 -1
  5. package/dist/adapters/indexeddb.mjs +1 -1
  6. package/dist/adapters/memory.d.ts +1 -1
  7. package/dist/{adapters-79OIa4xm.d.ts → adapters-BQFQ4PRz.d.ts} +27 -1
  8. package/dist/browser/chunk-BDOSMEEP.mjs +493 -0
  9. package/dist/browser/chunk-EJDUFWSO.mjs +602 -0
  10. package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
  11. package/dist/browser/chunk-JOVZ6HMT.mjs +4358 -0
  12. package/dist/browser/chunk-JPTQZWJQ.mjs +10 -0
  13. package/dist/browser/chunk-KPJMDPHG.mjs +24558 -0
  14. package/dist/browser/chunk-PGIM3HIM.mjs +2743 -0
  15. package/dist/browser/chunk-RHG4O77A.mjs +206 -0
  16. package/dist/browser/chunk-RIJSLY2I.mjs +360 -0
  17. package/dist/browser/durability-wiring-SRUYT3VV.mjs +3875 -0
  18. package/dist/browser/module-CBB2ETYP.mjs +3111 -0
  19. package/dist/browser/release-76MXRPGV.mjs +33 -0
  20. package/dist/browser/replace-RURXTGVG.mjs +57 -0
  21. package/dist/browser/restore-queue-EEZQO6RF.mjs +35 -0
  22. package/dist/browser/runtime-7ZAHCHWR.mjs +557 -0
  23. package/dist/browser/snapshot-OAAA5UJ7.mjs +1210 -0
  24. package/dist/browser/write-7D6VIK6O.mjs +208 -0
  25. package/dist/{capabilities-DSsOGz6F.d.ts → capabilities-DiGlB6Sr.d.ts} +68 -7
  26. package/dist/chunk-2RIRGOUL.mjs +113 -0
  27. package/dist/chunk-37CD3OKS.mjs +87 -0
  28. package/dist/chunk-44W3CBNG.mjs +7 -0
  29. package/dist/chunk-46D6JQIG.mjs +426 -0
  30. package/dist/{chunk-VMRR3DQU.mjs → chunk-5JNA5SKS.mjs} +546 -35
  31. package/dist/chunk-747OHOUA.mjs +11 -0
  32. package/dist/chunk-75XIEF6H.mjs +126 -0
  33. package/dist/chunk-7X5JST5L.mjs +114 -0
  34. package/dist/chunk-BOFBLJFZ.mjs +209 -0
  35. package/dist/{chunk-USKFU7J5.mjs → chunk-BWJVNZPR.mjs} +8 -1
  36. package/dist/chunk-C7SLA6CM.mjs +19 -0
  37. package/dist/{chunk-POWOTUKJ.mjs → chunk-CQUMNIIG.mjs} +3 -6
  38. package/dist/chunk-CVXLLEQ3.mjs +1011 -0
  39. package/dist/chunk-DJHHF2L5.mjs +17 -0
  40. package/dist/chunk-E74YLAIG.mjs +24 -0
  41. package/dist/{chunk-UXNWQZST.mjs → chunk-EMRMGYRE.mjs} +236 -18
  42. package/dist/chunk-EWZLIYHV.mjs +57 -0
  43. package/dist/chunk-GBEEG5AN.mjs +35 -0
  44. package/dist/chunk-GES7U2PM.mjs +68 -0
  45. package/dist/chunk-HJSN7UYG.mjs +10 -0
  46. package/dist/chunk-HLPRHOSO.mjs +17 -0
  47. package/dist/chunk-HMFNFVP7.mjs +14 -0
  48. package/dist/chunk-J5QP52HA.mjs +1751 -0
  49. package/dist/{chunk-YNVIUGF5.mjs → chunk-JZVARCRZ.mjs} +3 -28
  50. package/dist/chunk-KOKM4PQN.mjs +52 -0
  51. package/dist/{chunk-B2TFROFX.mjs → chunk-KWTW6VTG.mjs} +465 -250
  52. package/dist/chunk-KZ3NX2P7.mjs +21 -0
  53. package/dist/chunk-L5ORED7V.mjs +25 -0
  54. package/dist/{chunk-THYRHHO2.mjs → chunk-LGAMKHA2.mjs} +2009 -2351
  55. package/dist/chunk-LKOB5COA.mjs +117 -0
  56. package/dist/chunk-LPQOEU5M.mjs +96 -0
  57. package/dist/{chunk-3LHGNRYE.mjs → chunk-LXWDQLTW.mjs} +7 -2
  58. package/dist/{chunk-L6TNPLAR.mjs → chunk-OEYI3KRB.mjs} +270 -108
  59. package/dist/chunk-OM362BRO.mjs +948 -0
  60. package/dist/chunk-OZDFA4WC.mjs +67 -0
  61. package/dist/chunk-P36MUFEJ.mjs +10 -0
  62. package/dist/chunk-PC2G76VW.mjs +2540 -0
  63. package/dist/{chunk-GCM3JFVA.mjs → chunk-PNEYIHA4.mjs} +11 -2
  64. package/dist/chunk-PNFKUCG3.mjs +1130 -0
  65. package/dist/chunk-Q6XWVUZX.mjs +44 -0
  66. package/dist/chunk-R27EMH5M.mjs +617 -0
  67. package/dist/{chunk-YMZCMH5M.mjs → chunk-R6A773WK.mjs} +19 -24
  68. package/dist/chunk-RF7PLG43.mjs +106 -0
  69. package/dist/chunk-RFBLQLLZ.mjs +16 -0
  70. package/dist/{chunk-Y3L5MDRV.mjs → chunk-RFVUDM5E.mjs} +17 -2
  71. package/dist/chunk-S7FL6UAH.mjs +521 -0
  72. package/dist/chunk-S7GNDRPS.mjs +651 -0
  73. package/dist/chunk-SP3NM3ZG.mjs +3537 -0
  74. package/dist/chunk-U476IXKQ.mjs +10 -0
  75. package/dist/chunk-UA3AUGNV.mjs +462 -0
  76. package/dist/chunk-UTG5YNXV.mjs +507 -0
  77. package/dist/chunk-VI4R6W25.mjs +8484 -0
  78. package/dist/chunk-WQBCBKY3.mjs +8 -0
  79. package/dist/{chunk-24RXHMEL.mjs → chunk-WVCOCKBU.mjs} +3794 -7570
  80. package/dist/chunk-X5CV2E2B.mjs +1 -0
  81. package/dist/chunk-XADZPPZI.mjs +614 -0
  82. package/dist/chunk-YEEQHT4V.mjs +212 -0
  83. package/dist/chunk-YTJCGKM4.mjs +56 -0
  84. package/dist/chunk-YYQIODMF.mjs +275 -0
  85. package/dist/chunk-ZI34TVOA.mjs +144 -0
  86. package/dist/{chunk-OOSIDHA3.mjs → chunk-ZKWYWW4Y.mjs} +1 -1
  87. package/dist/chunk-ZM7KU4YW.mjs +35 -0
  88. package/dist/{client-BQK6Tf6A.d.ts → client-BOStAkc5.d.ts} +160 -37
  89. package/dist/client.d.ts +11 -10
  90. package/dist/client.mjs +209 -45
  91. package/dist/{config-DmXULPa-.d.ts → config-Cjvzbs_j.d.ts} +51 -3
  92. package/dist/config-DffQlmRq.d.ts +421 -0
  93. package/dist/direct-executor-ZD64PPEZ.mjs +24 -0
  94. package/dist/distribution/coordinator/etcd.d.ts +79 -18
  95. package/dist/distribution/coordinator/etcd.mjs +553 -109
  96. package/dist/distribution/coordinator/in-memory.d.ts +4 -3
  97. package/dist/distribution/coordinator/in-memory.mjs +22 -3
  98. package/dist/distribution/transport/grpc.d.ts +2 -2
  99. package/dist/distribution/transport/grpc.mjs +79 -14
  100. package/dist/distribution/transport/in-memory.d.ts +2 -2
  101. package/dist/distribution/transport/in-memory.mjs +12 -7
  102. package/dist/distribution/transport/tcp.d.ts +2 -2
  103. package/dist/distribution/transport/tcp.mjs +50 -28
  104. package/dist/distribution.d.ts +144 -23
  105. package/dist/distribution.mjs +14479 -8173
  106. package/dist/durability-wiring-FEUBR5BT.mjs +3330 -0
  107. package/dist/embeddings/openai.d.ts +2 -2
  108. package/dist/embeddings/openai.mjs +22 -133
  109. package/dist/{engine-CIOyFozK.d.ts → engine-rC0vjjY-.d.ts} +165 -141
  110. package/dist/{memory-yFaVn2xZ.d.ts → events-DqOIg5xa.d.ts} +645 -136
  111. package/dist/{types-DAK7mnUZ.d.ts → index-Bkdt8nDw.d.ts} +182 -45
  112. package/dist/index.browser.mjs +4 -32180
  113. package/dist/index.d.ts +91 -20
  114. package/dist/index.mjs +33 -16
  115. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  116. package/dist/invalidation/filesystem.d.ts +1 -1
  117. package/dist/invalidation/filesystem.mjs +2 -1
  118. package/dist/invalidation/noop.d.ts +1 -1
  119. package/dist/module-3S65MVCM.mjs +2151 -0
  120. package/dist/partial-words-DTIHO8G4.d.ts +19 -0
  121. package/dist/partial-words.d.ts +22 -0
  122. package/dist/partial-words.mjs +17 -0
  123. package/dist/pattern-Cledccbl.d.ts +18 -0
  124. package/dist/pattern.d.ts +22 -0
  125. package/dist/pattern.mjs +12 -0
  126. package/dist/persistence/durability/checkpoint-worker.d.ts +119 -72
  127. package/dist/persistence/durability/checkpoint-worker.mjs +51 -18
  128. package/dist/react.d.ts +279 -183
  129. package/dist/react.mjs +201 -48
  130. package/dist/release-BH3DKW3T.mjs +33 -0
  131. package/dist/replace-CEO5KJJ3.mjs +57 -0
  132. package/dist/rerank-K7C4WZFG.mjs +60 -0
  133. package/dist/rerankers/http.d.ts +50 -0
  134. package/dist/rerankers/http.mjs +104 -0
  135. package/dist/restore-queue-KQHA7ZV2.mjs +35 -0
  136. package/dist/runtime-6Y4ZYXOG.mjs +177 -0
  137. package/dist/{schema-d5Afu03A.d.ts → schema-4W_7tk1f.d.ts} +81 -11
  138. package/dist/server.d.ts +13 -12
  139. package/dist/server.mjs +611 -80
  140. package/dist/snapshot-QGZPODM5.mjs +799 -0
  141. package/dist/synonyms-CpOAHPa-.d.ts +52 -0
  142. package/dist/synonyms.d.ts +22 -0
  143. package/dist/synonyms.mjs +12 -0
  144. package/dist/{tasks-CH2Dod8w.d.ts → tasks-CwPTz-tP.d.ts} +10 -3
  145. package/dist/thread-N3IUGUS3.mjs +697 -0
  146. package/dist/{types-BQDlIvfS.d.ts → types-2qoitTXg.d.ts} +1 -1
  147. package/dist/{types-D5OwhtsQ.d.ts → types-Cz5okLml.d.ts} +2 -2
  148. package/dist/vector/search-worker.d.ts +46 -4
  149. package/dist/vector/search-worker.mjs +21 -11
  150. package/dist/workers/entry.mjs +9 -6
  151. package/dist/write-U5RI7MAX.mjs +241 -0
  152. package/package.json +55 -9
  153. package/dist/chunk-3L4U6EJQ.mjs +0 -49
  154. package/dist/chunk-IFBKLBGB.mjs +0 -7
  155. package/dist/chunk-ORBLD3EL.mjs +0 -6
  156. package/dist/chunk-U7KYW4BA.mjs +0 -135
  157. package/dist/chunk-ZJ4CSAB2.mjs +0 -13
  158. package/dist/chunk-ZNZBXMP5.mjs +0 -2117
  159. package/dist/chunk-ZZT3EAUC.mjs +0 -11168
  160. package/dist/config-BhldZgaK.d.ts +0 -194
  161. package/dist/direct-executor-ALAVE7FC.mjs +0 -12
  162. package/dist/serialization/crc32-worker.d.ts +0 -20
  163. package/dist/serialization/crc32-worker.mjs +0 -62
  164. package/dist/simd-DW5AtJ6B.d.ts +0 -41
  165. package/dist/thread-DYHEQRVY.mjs +0 -402
package/dist/react.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createContext, useState, useEffect, createElement, useRef, useEffectEvent, useCallback, useMemo, useContext, useSyncExternalStore } from 'react';
1
+ import { createContext, useState, createElement, useRef, useEffectEvent, useEffect, useCallback, useMemo, useContext, useSyncExternalStore } from 'react';
2
2
 
3
3
  // src/errors.ts
4
4
  var NARSIL_ERROR_MARKER = /* @__PURE__ */ Symbol.for("@delali/narsil.NarsilError");
@@ -33,14 +33,18 @@ var ErrorCodes = {
33
33
  SEARCH_INVALID_MODE: "SEARCH_INVALID_MODE",
34
34
  SEARCH_INVALID_CURSOR: "SEARCH_INVALID_CURSOR",
35
35
  SEARCH_RESULT_WINDOW_EXCEEDED: "SEARCH_RESULT_WINDOW_EXCEEDED",
36
+ SEARCH_WORK_CAP_EXCEEDED: "SEARCH_WORK_CAP_EXCEEDED",
36
37
  LANGUAGE_NOT_SUPPORTED: "LANGUAGE_NOT_SUPPORTED",
37
38
  ENVELOPE_VERSION_MISMATCH: "ENVELOPE_VERSION_MISMATCH",
38
39
  ENVELOPE_INVALID_MAGIC: "ENVELOPE_INVALID_MAGIC",
39
40
  PARTITION_CAPACITY_EXCEEDED: "PARTITION_CAPACITY_EXCEEDED",
41
+ RESTORE_MEMORY_EXCEEDED: "RESTORE_MEMORY_EXCEEDED",
40
42
  EMBEDDING_FAILED: "EMBEDDING_FAILED",
41
43
  EMBEDDING_DIMENSION_MISMATCH: "EMBEDDING_DIMENSION_MISMATCH",
42
44
  EMBEDDING_NO_SOURCE: "EMBEDDING_NO_SOURCE",
43
45
  EMBEDDING_CONFIG_INVALID: "EMBEDDING_CONFIG_INVALID",
46
+ RERANK_FAILED: "RERANK_FAILED",
47
+ RERANK_CONFIG_INVALID: "RERANK_CONFIG_INVALID",
44
48
  DOC_MISSING_REQUIRED_FIELD: "DOC_MISSING_REQUIRED_FIELD",
45
49
  CONFIG_INVALID: "CONFIG_INVALID",
46
50
  QUERY_ROUTING_FAILED: "QUERY_ROUTING_FAILED",
@@ -48,6 +52,7 @@ var ErrorCodes = {
48
52
  QUERY_NODE_TIMEOUT: "QUERY_NODE_TIMEOUT",
49
53
  QUERY_NO_ACTIVE_REPLICA: "QUERY_NO_ACTIVE_REPLICA",
50
54
  CLUSTER_OPERATION_UNSUPPORTED: "CLUSTER_OPERATION_UNSUPPORTED",
55
+ CLUSTER_WRITE_BLOCKED: "CLUSTER_WRITE_BLOCKED",
51
56
  ALLOCATION_NO_DATA_NODES: "ALLOCATION_NO_DATA_NODES",
52
57
  ALLOCATION_INVALID_CONFIG: "ALLOCATION_INVALID_CONFIG",
53
58
  ALLOCATION_FAILED: "ALLOCATION_FAILED",
@@ -57,7 +62,10 @@ var ErrorCodes = {
57
62
  NODE_NOT_JOINED: "NODE_NOT_JOINED",
58
63
  NODE_NOT_READY: "NODE_NOT_READY",
59
64
  NODE_NOT_CONTROLLER: "NODE_NOT_CONTROLLER",
65
+ NODE_VERSION_INCOMPATIBLE: "NODE_VERSION_INCOMPATIBLE",
60
66
  COORDINATOR_DEPENDENCY_MISSING: "COORDINATOR_DEPENDENCY_MISSING",
67
+ COORDINATOR_AUTH_FAILED: "COORDINATOR_AUTH_FAILED",
68
+ COORDINATOR_UNAVAILABLE: "COORDINATOR_UNAVAILABLE",
61
69
  TRANSPORT_DEPENDENCY_MISSING: "TRANSPORT_DEPENDENCY_MISSING",
62
70
  REPLICATION_ENTRY_INVALID: "REPLICATION_ENTRY_INVALID",
63
71
  REPLICATION_INSYNC_REMOVAL_FAILED: "REPLICATION_INSYNC_REMOVAL_FAILED",
@@ -73,7 +81,6 @@ var ErrorCodes = {
73
81
  SNAPSHOT_SYNC_UNAUTHORIZED: "SNAPSHOT_SYNC_UNAUTHORIZED",
74
82
  SNAPSHOT_SYNC_REQUEST_INVALID: "SNAPSHOT_SYNC_REQUEST_INVALID",
75
83
  SNAPSHOT_SYNC_INDEX_NOT_FOUND: "SNAPSHOT_SYNC_INDEX_NOT_FOUND",
76
- SNAPSHOT_SYNC_TOO_LARGE: "SNAPSHOT_SYNC_TOO_LARGE",
77
84
  SNAPSHOT_SYNC_CAPACITY_EXHAUSTED: "SNAPSHOT_SYNC_CAPACITY_EXHAUSTED",
78
85
  SNAPSHOT_SYNC_SNAPSHOT_FAILED: "SNAPSHOT_SYNC_SNAPSHOT_FAILED",
79
86
  SNAPSHOT_SYNC_DECODE_FAILED: "SNAPSHOT_SYNC_DECODE_FAILED",
@@ -147,8 +154,53 @@ function isNarsilError(value) {
147
154
  return typeof value === "object" && value !== null && NARSIL_ERROR_MARKER in value;
148
155
  }
149
156
 
157
+ // src/react/reader.ts
158
+ function plainNumbersIn(value) {
159
+ if (value === null || typeof value !== "object") return value;
160
+ if (ArrayBuffer.isView(value)) {
161
+ return value instanceof DataView ? value : Array.from(value);
162
+ }
163
+ if (Array.isArray(value)) {
164
+ let copy2 = null;
165
+ for (let at = 0; at < value.length; at++) {
166
+ const converted = plainNumbersIn(value[at]);
167
+ if (converted === value[at]) continue;
168
+ copy2 ??= value.slice();
169
+ copy2[at] = converted;
170
+ }
171
+ return copy2 ?? value;
172
+ }
173
+ let copy = null;
174
+ for (const [key, held] of Object.entries(value)) {
175
+ const converted = plainNumbersIn(held);
176
+ if (converted === held) continue;
177
+ copy ??= { ...value };
178
+ copy[key] = converted;
179
+ }
180
+ return copy ?? value;
181
+ }
182
+ function withPlainNumbers(answer) {
183
+ return answer.then((value) => plainNumbersIn(value));
184
+ }
185
+ function engineReader(engine) {
186
+ return {
187
+ query(indexName, params) {
188
+ return withPlainNumbers(engine.query(indexName, params));
189
+ },
190
+ preflight: (indexName, params) => engine.preflight(indexName, params),
191
+ suggest: (indexName, params) => engine.suggest(indexName, params),
192
+ get: (indexName, docId) => withPlainNumbers(engine.get(indexName, docId)),
193
+ listDocuments(indexName, params) {
194
+ return withPlainNumbers(engine.listDocuments(indexName, params));
195
+ },
196
+ listIndexes: async () => engine.listIndexes(),
197
+ getStats: async (indexName) => engine.getStats(indexName)
198
+ };
199
+ }
200
+
150
201
  // src/react/constants.ts
151
202
  var DEFAULT_KEEP_ALIVE_MS = 2e3;
203
+ var DEFAULT_REFRESH_AFTER_WRITE_MS = 200;
152
204
  var DEFAULT_TASK_POLL_INTERVAL_MS = 250;
153
205
  var ERROR_RETRY_INTERVAL_MS = 5e3;
154
206
  var MAX_QUERY_KEY_DEPTH = 32;
@@ -173,10 +225,13 @@ var IDLE_SNAPSHOT = Object.freeze({
173
225
  isLoading: false,
174
226
  isFetching: false
175
227
  });
176
- function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS) {
228
+ var ANY_INDEX = /* @__PURE__ */ Symbol("any index");
229
+ var NO_INDEX = /* @__PURE__ */ Symbol("no index");
230
+ function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS, refreshAfterWriteMs = DEFAULT_REFRESH_AFTER_WRITE_MS) {
177
231
  const entries = /* @__PURE__ */ new Map();
178
232
  let holders = 0;
179
233
  let closing = null;
234
+ const staleRefreshes = /* @__PURE__ */ new Map();
180
235
  function publish(entry, next) {
181
236
  entry.snapshot = next;
182
237
  for (const listener of entry.listeners) listener();
@@ -185,6 +240,7 @@ function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS) {
185
240
  if (entry.controller !== null) return;
186
241
  const controller = new AbortController();
187
242
  entry.controller = controller;
243
+ entry.stale = false;
188
244
  entry.version++;
189
245
  const version = entry.version;
190
246
  if (!entry.snapshot.isFetching) publish(entry, { ...entry.snapshot, isFetching: true });
@@ -209,9 +265,32 @@ function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS) {
209
265
  );
210
266
  }
211
267
  function disposeAll() {
268
+ for (const timer of staleRefreshes.values()) clearTimeout(timer);
269
+ staleRefreshes.clear();
212
270
  for (const [key, entry] of [...entries]) drop(key, entry);
213
271
  entries.clear();
214
272
  }
273
+ function refreshStale(scope) {
274
+ staleRefreshes.delete(scope);
275
+ for (const [key, entry] of entries) {
276
+ if (entry.writeScope === scope && entry.stale && entry.listeners.size > 0) load(key, entry);
277
+ }
278
+ }
279
+ function scheduleRefresh(scope) {
280
+ const pending = staleRefreshes.get(scope);
281
+ if (pending !== void 0) clearTimeout(pending);
282
+ staleRefreshes.set(
283
+ scope,
284
+ setTimeout(() => refreshStale(scope), refreshAfterWriteMs)
285
+ );
286
+ }
287
+ function markStale(entry) {
288
+ entry.stale = true;
289
+ if (entry.controller === null) return;
290
+ entry.controller.abort();
291
+ entry.controller = null;
292
+ entry.version++;
293
+ }
215
294
  function drop(key, entry) {
216
295
  if (entries.get(key) === entry) entries.delete(key);
217
296
  if (entry.disposal !== null) clearTimeout(entry.disposal);
@@ -220,7 +299,7 @@ function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS) {
220
299
  entry.controller = null;
221
300
  }
222
301
  return {
223
- subscribe(key, loader, onChange) {
302
+ subscribe(key, loader, onChange, writeScope) {
224
303
  let entry = entries.get(key);
225
304
  if (entry === void 0) {
226
305
  entry = {
@@ -229,7 +308,9 @@ function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS) {
229
308
  loader,
230
309
  controller: null,
231
310
  version: 0,
232
- disposal: null
311
+ disposal: null,
312
+ writeScope,
313
+ stale: false
233
314
  };
234
315
  entries.set(key, entry);
235
316
  } else {
@@ -241,7 +322,7 @@ function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS) {
241
322
  }
242
323
  const held = entry;
243
324
  held.listeners.add(onChange);
244
- const retryable = held.snapshot.isLoading || held.snapshot.error !== void 0;
325
+ const retryable = held.stale || held.snapshot.isLoading || held.snapshot.error !== void 0;
245
326
  if (retryable && held.controller === null) load(key, held);
246
327
  return () => {
247
328
  held.listeners.delete(onChange);
@@ -259,6 +340,15 @@ function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS) {
259
340
  const entry = entries.get(key);
260
341
  if (entry !== void 0) load(key, entry);
261
342
  },
343
+ invalidate(indexName) {
344
+ const marked = /* @__PURE__ */ new Set();
345
+ for (const entry of entries.values()) {
346
+ if (entry.writeScope !== ANY_INDEX && entry.writeScope !== indexName) continue;
347
+ markStale(entry);
348
+ marked.add(entry.writeScope);
349
+ }
350
+ for (const scope of marked) scheduleRefresh(scope);
351
+ },
262
352
  retain() {
263
353
  holders++;
264
354
  if (closing !== null) {
@@ -283,30 +373,72 @@ function createResourceStore(keepAliveMs = DEFAULT_KEEP_ALIVE_MS) {
283
373
 
284
374
  // src/react/context.ts
285
375
  var NarsilContext = createContext(null);
286
- function build(client, keepAliveMs) {
287
- return { client, store: createResourceStore(keepAliveMs), keepAliveMs };
376
+ function buildContextValue(props) {
377
+ const { client, engine, keepAliveMs, refreshAfterWriteMs } = props;
378
+ const shared = { store: createResourceStore(keepAliveMs, refreshAfterWriteMs), keepAliveMs, refreshAfterWriteMs };
379
+ if (client !== void 0 && engine === void 0) return { ...shared, reader: client, client, engine: null };
380
+ if (engine !== void 0 && client === void 0) {
381
+ return { ...shared, reader: engineReader(engine), client: null, engine };
382
+ }
383
+ throw new NarsilError(
384
+ ErrorCodes.CONFIG_INVALID,
385
+ "A NarsilProvider requires exactly one of client and engine, but its props set both or neither"
386
+ );
387
+ }
388
+ function providerPropsChanged(value, props) {
389
+ return value.client !== (props.client ?? null) || value.engine !== (props.engine ?? null) || value.keepAliveMs !== props.keepAliveMs || value.refreshAfterWriteMs !== props.refreshAfterWriteMs;
390
+ }
391
+ function useStoreRetention(store) {
392
+ useEffect(() => store.retain(), [store]);
393
+ }
394
+ function useRefreshAfterWrites(engine, store) {
395
+ useEffect(() => {
396
+ if (engine === null) return;
397
+ const onWrite = (payload) => {
398
+ store.invalidate(payload.indexName);
399
+ };
400
+ engine.on("write", onWrite);
401
+ return () => {
402
+ engine.off("write", onWrite);
403
+ };
404
+ }, [engine, store]);
288
405
  }
289
406
  function NarsilProvider(props) {
290
- const { client, keepAliveMs, children } = props;
291
- const [value, setValue] = useState(() => build(client, keepAliveMs));
292
- if (value.client !== client || value.keepAliveMs !== keepAliveMs) {
293
- setValue(build(client, keepAliveMs));
407
+ const [value, setValue] = useState(() => buildContextValue(props));
408
+ if (providerPropsChanged(value, props)) {
409
+ setValue(buildContextValue(props));
294
410
  }
295
- useEffect(() => value.store.retain(), [value]);
296
- return createElement(NarsilContext.Provider, { value }, children);
411
+ useStoreRetention(value.store);
412
+ useRefreshAfterWrites(value.engine, value.store);
413
+ return createElement(NarsilContext.Provider, { value }, props.children);
297
414
  }
298
415
  function useNarsilContext() {
299
416
  const value = useContext(NarsilContext);
300
417
  if (value === null) {
301
418
  throw new NarsilError(
302
419
  ErrorCodes.CONFIG_INVALID,
303
- "A Narsil hook ran outside a NarsilProvider, so it found no client to send through"
420
+ "A Narsil hook was called outside a NarsilProvider, so no client or engine is available to it"
304
421
  );
305
422
  }
306
423
  return value;
307
424
  }
425
+ function useServerClient(hookName) {
426
+ const { client } = useNarsilContext();
427
+ if (client !== null) return client;
428
+ throw new NarsilError(
429
+ ErrorCodes.CONFIG_INVALID,
430
+ `${hookName} works only under a client of a server, because only a server handles tasks and imports, while the NarsilProvider above it holds an engine`,
431
+ { hook: hookName }
432
+ );
433
+ }
308
434
  function useNarsilClient() {
309
- return useNarsilContext().client;
435
+ const { client } = useNarsilContext();
436
+ if (client !== null) return client;
437
+ throw new NarsilError(
438
+ ErrorCodes.CONFIG_INVALID,
439
+ "useNarsilClient returns a client, while the NarsilProvider above it holds an engine, so import that engine directly",
440
+ { hook: "useNarsilClient" }
441
+ );
310
442
  }
311
443
 
312
444
  // src/react/key.ts
@@ -457,8 +589,8 @@ function useKey(parts) {
457
589
  cached.current = { parts, key };
458
590
  return key;
459
591
  }
460
- function useRead(parts, run, options) {
461
- const { client, store } = useNarsilContext();
592
+ function useRead(source, parts, run, options, writeScope) {
593
+ const { store } = useNarsilContext();
462
594
  const enabled = options?.enabled ?? true;
463
595
  const headers = options?.headers;
464
596
  const timeoutMs = options?.timeoutMs;
@@ -472,11 +604,11 @@ function useRead(parts, run, options) {
472
604
  if (!enabled) return NO_SUBSCRIPTION;
473
605
  const loader = (signal) => {
474
606
  const held = call.current;
475
- return held.run(client, { ...held.request, signal });
607
+ return held.run(source, { ...held.request, signal });
476
608
  };
477
- return store.subscribe(key, loader, onChange);
609
+ return store.subscribe(key, loader, onChange, writeScope);
478
610
  },
479
- [enabled, store, client, key]
611
+ [enabled, store, source, key, writeScope]
480
612
  );
481
613
  const readSnapshot = useCallback(
482
614
  () => enabled ? store.snapshot(key) : IDLE_SNAPSHOT,
@@ -508,21 +640,28 @@ function useRead(parts, run, options) {
508
640
  [data, snapshot, refresh]
509
641
  );
510
642
  }
643
+ function useProviderRead(parts, run, options, writeScope) {
644
+ const { reader } = useNarsilContext();
645
+ return useRead(reader, parts, run, options, writeScope);
646
+ }
511
647
 
512
648
  // src/react/documents.ts
513
649
  function useDocument(indexName, docId, options) {
514
650
  const id = docId ?? "";
515
651
  const enabled = (options?.enabled ?? true) && id.length > 0;
516
- return useRead(["get", indexName, id], (client, request) => client.get(indexName, id, request), {
517
- ...options,
518
- enabled
519
- });
652
+ return useProviderRead(
653
+ ["get", indexName, id],
654
+ (reader, request) => reader.get(indexName, id, request),
655
+ { ...options, enabled },
656
+ indexName
657
+ );
520
658
  }
521
659
  function useDocuments(indexName, params, options) {
522
- return useRead(
660
+ return useProviderRead(
523
661
  ["listDocuments", indexName, params],
524
- (client, request) => client.listDocuments(indexName, params, request),
525
- options
662
+ (reader, request) => reader.listDocuments(indexName, params, request),
663
+ options,
664
+ indexName
526
665
  );
527
666
  }
528
667
 
@@ -544,7 +683,7 @@ function isTerminalTask(record) {
544
683
  // src/react/import.ts
545
684
  var NOTHING = { task: void 0, error: void 0, starting: false };
546
685
  function useImport(indexName, options) {
547
- const client = useNarsilClient();
686
+ const client = useServerClient("useImport");
548
687
  const [progress, setProgress] = useState(NOTHING);
549
688
  const polling = useRef(null);
550
689
  const sending = useRef(null);
@@ -596,7 +735,7 @@ function useImport(indexName, options) {
596
735
  inFlight.current = true;
597
736
  client.getTask(taskId, { ...request.current, signal: controller.signal }).then((next) => {
598
737
  setProgress((prev) => {
599
- if (prev.task?.id !== taskId) return prev;
738
+ if (prev.task?.id !== taskId || isTerminalTask(prev.task)) return prev;
600
739
  if (next === null) {
601
740
  return {
602
741
  ...prev,
@@ -611,7 +750,9 @@ function useImport(indexName, options) {
611
750
  });
612
751
  }).catch((err) => {
613
752
  if (controller.signal.aborted) return;
614
- setProgress((prev) => prev.task?.id === taskId ? { ...prev, error: asNarsilError(err, "The import") } : prev);
753
+ setProgress(
754
+ (prev) => prev.task?.id === taskId && !isTerminalTask(prev.task) ? { ...prev, error: asNarsilError(err, "The import") } : prev
755
+ );
615
756
  }).finally(() => {
616
757
  inFlight.current = false;
617
758
  });
@@ -652,51 +793,63 @@ function useImport(indexName, options) {
652
793
 
653
794
  // src/react/indexes.ts
654
795
  function useIndexes(options) {
655
- return useRead(["listIndexes"], (client, request) => client.listIndexes(request), options);
796
+ return useProviderRead(["listIndexes"], (reader, request) => reader.listIndexes(request), options, ANY_INDEX);
656
797
  }
657
798
  function useStats(indexName, options) {
658
- return useRead(["getStats", indexName], (client, request) => client.getStats(indexName, request), options);
799
+ return useProviderRead(
800
+ ["getStats", indexName],
801
+ (reader, request) => reader.getStats(indexName, request),
802
+ options,
803
+ indexName
804
+ );
659
805
  }
660
806
 
661
807
  // src/react/search.ts
662
808
  function useQuery(indexName, params, options) {
663
- return useRead(
809
+ return useProviderRead(
664
810
  ["query", indexName, params],
665
- (client, request) => client.query(indexName, params, request),
666
- options
811
+ (reader, request) => reader.query(indexName, params, request),
812
+ options,
813
+ indexName
667
814
  );
668
815
  }
669
816
  function usePreflight(indexName, params, options) {
670
- return useRead(
817
+ return useProviderRead(
671
818
  ["preflight", indexName, params],
672
- (client, request) => client.preflight(indexName, params, request),
673
- options
819
+ (reader, request) => reader.preflight(indexName, params, request),
820
+ options,
821
+ indexName
674
822
  );
675
823
  }
676
824
  function useSuggest(indexName, params, options) {
677
- return useRead(
825
+ return useProviderRead(
678
826
  ["suggest", indexName, params],
679
- (client, request) => client.suggest(indexName, params, request),
680
- options
827
+ (reader, request) => reader.suggest(indexName, params, request),
828
+ options,
829
+ indexName
681
830
  );
682
831
  }
683
832
 
684
833
  // src/react/tasks.ts
685
834
  function useTask(taskId, options) {
835
+ const client = useServerClient("useTask");
686
836
  const id = taskId ?? "";
687
837
  const enabled = (options?.enabled ?? true) && id.length > 0;
688
- const state = useRead(["getTask", id], (client, request) => client.getTask(id, request), {
689
- ...options,
690
- enabled,
691
- refreshIntervalMs: 0
692
- });
838
+ const state = useRead(
839
+ client,
840
+ ["getTask", id],
841
+ (server, request) => server.getTask(id, request),
842
+ { ...options, enabled, refreshIntervalMs: 0 },
843
+ NO_INDEX
844
+ );
693
845
  const running = state.data !== null && !isTerminalTask(state.data);
694
846
  const interval = pollInterval(options?.pollIntervalMs ?? DEFAULT_TASK_POLL_INTERVAL_MS, state.error !== void 0);
695
847
  usePolling(state.refresh, interval, enabled && running);
696
848
  return state;
697
849
  }
698
850
  function useTasks(query, options) {
699
- return useRead(["listTasks", query], (client, request) => client.listTasks(query, request), options);
851
+ const client = useServerClient("useTasks");
852
+ return useRead(client, ["listTasks", query], (server, request) => server.listTasks(query, request), options, NO_INDEX);
700
853
  }
701
854
 
702
855
  export { ClientErrorCodes, ErrorCodes, NarsilError, NarsilProvider, ServerErrorCodes, isNarsilError, useDocument, useDocuments, useImport, useIndexes, useNarsilClient, usePreflight, useQuery, useStats, useSuggest, useTask, useTasks };
@@ -0,0 +1,33 @@
1
+ // src/engine/orchestration/release.ts
2
+ async function releaseIndexCopies(state, indexName, steps) {
3
+ const wasScaledOut = state.scaledOutIndexes.delete(indexName);
4
+ const loading = state.copyTransitions.get(indexName);
5
+ if (loading !== void 0) await loading.done.catch(() => void 0);
6
+ await steps.awaitReplicationIdle(state, indexName);
7
+ steps.cancelIdleMerge(state, indexName);
8
+ const reloads = wasScaledOut || loading !== void 0 && loading.kind !== "drop";
9
+ const pool = state.workerPool;
10
+ if (pool === null) return reloads;
11
+ pool.removeIndex(indexName);
12
+ state.segmentLedger.delete(indexName);
13
+ state.unbroadcastSegments.delete(indexName);
14
+ await Promise.all(
15
+ pool.executorEntries().map(
16
+ ({ executor }) => executor.execute({ type: "dropIndex", indexName, requestId: `restore-release-${indexName}` }).catch((error) => {
17
+ if (!steps.heldNowhere(error)) throw error;
18
+ })
19
+ )
20
+ );
21
+ return reloads;
22
+ }
23
+ async function endCopyRelease(state, indexName, reloads, steps) {
24
+ const hold = state.copyReleaseHolds.get(indexName);
25
+ if (hold === void 0) return steps.resyncIndex(indexName, reloads);
26
+ hold.holders -= 1;
27
+ hold.reloads ||= reloads;
28
+ if (hold.holders > 0) return;
29
+ state.copyReleaseHolds.delete(indexName);
30
+ await steps.resyncIndex(indexName, hold.reloads);
31
+ }
32
+
33
+ export { endCopyRelease, releaseIndexCopies };
@@ -0,0 +1,57 @@
1
+ // src/engine/index-state/replace.ts
2
+ function runReplacement(context, entry, body, settle) {
3
+ const previous = entry.state;
4
+ entry.state = "replacing";
5
+ const run = (async () => {
6
+ await context.waitForDrain(entry);
7
+ await body();
8
+ })().then(
9
+ () => settle(true, previous),
10
+ (error) => {
11
+ settle(false, previous);
12
+ throw error;
13
+ }
14
+ ).finally(() => {
15
+ entry.replacePromise = null;
16
+ });
17
+ entry.replacePromise = run;
18
+ return run;
19
+ }
20
+ function replaceUnregistered(context, indexName, entry, action) {
21
+ return runReplacement(context, entry, action, (succeeded) => {
22
+ if (!succeeded) {
23
+ context.forget(indexName);
24
+ return;
25
+ }
26
+ entry.state = "open";
27
+ entry.lastAccessAt = Date.now();
28
+ });
29
+ }
30
+ async function replaceExclusively(context, indexName, action) {
31
+ let entry = context.entryOf(indexName);
32
+ while (true) {
33
+ const pending = entry.dropPromise ?? entry.replacePromise ?? entry.reopenPromise ?? entry.closePromise;
34
+ if (pending === null) break;
35
+ await pending.catch(() => void 0);
36
+ entry = context.entryOf(indexName);
37
+ }
38
+ const wasOpen = entry.state === "open";
39
+ await runReplacement(
40
+ context,
41
+ entry,
42
+ () => action(wasOpen),
43
+ (succeeded, previous) => {
44
+ if (!succeeded) {
45
+ entry.state = previous;
46
+ return;
47
+ }
48
+ entry.state = "open";
49
+ entry.cachedError = null;
50
+ entry.reopenFailures = 0;
51
+ entry.nextReopenAt = 0;
52
+ entry.lastAccessAt = Date.now();
53
+ }
54
+ );
55
+ }
56
+
57
+ export { replaceExclusively, replaceUnregistered };
@@ -0,0 +1,60 @@
1
+ import { runRerankedQuery, rerankTextOf } from './chunk-YEEQHT4V.mjs';
2
+ import './chunk-KZ3NX2P7.mjs';
3
+ import { rerankOnHost } from './chunk-2RIRGOUL.mjs';
4
+ import './chunk-EWZLIYHV.mjs';
5
+ import './chunk-HJSN7UYG.mjs';
6
+ import './chunk-LXWDQLTW.mjs';
7
+ import './chunk-E74YLAIG.mjs';
8
+ import { NarsilError, ErrorCodes } from './chunk-BWJVNZPR.mjs';
9
+
10
+ // src/distribution/cluster-node/reads/rerank.ts
11
+ async function rerankClusterQuery(deps, indexName, params, firstStage, readDocuments, host) {
12
+ const startTime = host.now();
13
+ const core = host.engineCoreOf(deps.engine);
14
+ if (core === void 0) {
15
+ throw new NarsilError(ErrorCodes.RERANK_CONFIG_INVALID, "This cluster node holds no rerank adapters");
16
+ }
17
+ const schema = await deps.config.coordinator.getSchema(indexName);
18
+ if (schema === null) {
19
+ throw new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" does not exist`, { indexName });
20
+ }
21
+ const result = await runRerankedQuery(params, {
22
+ host,
23
+ schema,
24
+ maxWindow: core.rerankSettings.maxWindow,
25
+ binding: host.queryBindingOf(params),
26
+ clampsPins: false,
27
+ fusesRankings: (request) => request.vector !== void 0 && (request.term !== void 0 || request.mode === "hybrid"),
28
+ firstStage,
29
+ async texts(docIds, fields) {
30
+ const documents = await readDocuments(docIds);
31
+ return docIds.map((docId) => {
32
+ const document = documents.get(docId);
33
+ return document === void 0 ? "" : rerankTextOf(fields.map((field) => host.getNestedValue(document, field)));
34
+ });
35
+ },
36
+ async storedPins(docIds) {
37
+ return new Set((await readDocuments(docIds)).keys());
38
+ },
39
+ score: (adapter, query, documents) => rerankOnHost(core, adapter, query, documents),
40
+ async fillDocuments(hits, groups, projection) {
41
+ const grouped = (groups ?? []).flatMap((group) => group.hits);
42
+ if (projection.kind === "none") {
43
+ for (const hit of hits) hit.document = {};
44
+ for (const hit of grouped) hit.document = {};
45
+ return;
46
+ }
47
+ const documents = await readDocuments([...hits.map((hit) => hit.id), ...grouped.map((hit) => hit.id)]);
48
+ const read = (docId) => {
49
+ const document = documents.get(docId);
50
+ return document === void 0 ? {} : host.applyProjection(document, projection);
51
+ };
52
+ for (const hit of hits) hit.document = read(hit.id);
53
+ for (const hit of grouped) hit.document = read(hit.id);
54
+ }
55
+ });
56
+ result.elapsed = host.now() - startTime;
57
+ return result;
58
+ }
59
+
60
+ export { rerankClusterQuery };
@@ -0,0 +1,50 @@
1
+ import { R as RerankAdapter } from '../adapters-BQFQ4PRz.js';
2
+
3
+ /**
4
+ * How {@link createHttpReranker} reaches a rerank endpoint.
5
+ *
6
+ * @public
7
+ */
8
+ interface HttpRerankerConfig {
9
+ /**
10
+ * The adapter sends each request to this address with `/rerank` added.
11
+ * Give the versioned API address of Cohere or Jina, or the address of a
12
+ * vLLM or Infinity server that you host.
13
+ */
14
+ baseUrl: string;
15
+ /** The service scores the documents with this model. */
16
+ model: string;
17
+ /**
18
+ * The adapter sends this key as a bearer token. Pass a function to fetch a
19
+ * fresh or short-lived key for every request, and leave the key out for a
20
+ * server that you host without one.
21
+ */
22
+ apiKey?: string | (() => string | Promise<string>);
23
+ /** The adapter aborts a request that takes longer than this many milliseconds, and waits 30,000 by default. */
24
+ timeout?: number;
25
+ /** The adapter retries a request this many times, and 3 times by default, when the service answers with status 429, 500, 502, 503, or 504 or the network fails. */
26
+ maxRetries?: number;
27
+ /** The adapter sends at most this many documents in one request, and 1,000 by default, splitting a larger list across several requests. */
28
+ maxDocumentsPerRequest?: number;
29
+ }
30
+ /**
31
+ * Builds a rerank adapter that calls a Cohere-compatible `/rerank` endpoint,
32
+ * which Cohere, Jina, vLLM, and Infinity serve.
33
+ *
34
+ * The adapter posts the model, the query, and the documents, and reads one
35
+ * `relevance_score` per document from the `results` list. It retries a
36
+ * request on a rate limit, a server error, or a network failure, with an
37
+ * exponential wait and jitter, and it keeps the API key out of every error
38
+ * message.
39
+ *
40
+ * @param config - The endpoint, model, key, and limits.
41
+ * @returns An adapter that you pass in `rerankAdapters` or register with
42
+ * {@link AdapterRegistrationOperations.registerRerankAdapter}.
43
+ * @throws A `NarsilError` with `RERANK_CONFIG_INVALID` when the base URL, the
44
+ * model, the key, or a limit is missing or out of range.
45
+ *
46
+ * @public
47
+ */
48
+ declare function createHttpReranker(config: HttpRerankerConfig): RerankAdapter;
49
+
50
+ export { type HttpRerankerConfig, createHttpReranker };