@delali/narsil 0.3.1-next.112 → 0.3.1-next.115

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 (71) hide show
  1. package/dist/browser/{chunk-UR5MNN53.mjs → chunk-2TMWIATH.mjs} +108 -67
  2. package/dist/browser/{chunk-OVY4RPDS.mjs → chunk-73U5DWOZ.mjs} +39 -4
  3. package/dist/browser/{chunk-O5IKTA27.mjs → chunk-FODCC3MK.mjs} +1 -1
  4. package/dist/browser/{chunk-YRYBTRWB.mjs → chunk-I45FAGOF.mjs} +2 -2
  5. package/dist/browser/{durability-wiring-CPEMDDK7.mjs → durability-wiring-CSMTKEYX.mjs} +3 -3
  6. package/dist/browser/{module-DYKR4INA.mjs → module-G2PPZV3B.mjs} +127 -33
  7. package/dist/browser/{restore-queue-2H4QDQV4.mjs → restore-queue-N7N42PBS.mjs} +1 -1
  8. package/dist/browser/{runtime-FL5K4JOH.mjs → runtime-BX6XRFYR.mjs} +2 -2
  9. package/dist/browser/{snapshot-ISZMBDCN.mjs → snapshot-EBH6RXQO.mjs} +6 -5
  10. package/dist/browser/{write-IABOLE4N.mjs → write-5ZKFINX3.mjs} +2 -2
  11. package/dist/{capabilities-ByaX8Arf.d.ts → capabilities-DOgB3pPf.d.ts} +2 -2
  12. package/dist/{chunk-ZXZ26WSO.mjs → chunk-22YPZVMG.mjs} +5 -1
  13. package/dist/{chunk-R2MRGYU5.mjs → chunk-4ZKBKPBV.mjs} +22 -2
  14. package/dist/{chunk-GLZI636G.mjs → chunk-5VVSHCXT.mjs} +27 -12
  15. package/dist/{chunk-3COL24MT.mjs → chunk-7EWQZQDT.mjs} +1 -1
  16. package/dist/{chunk-QI6JW4PM.mjs → chunk-BCNUJVTT.mjs} +1 -1
  17. package/dist/{chunk-3RRATITJ.mjs → chunk-BCSWODXP.mjs} +75 -55
  18. package/dist/{thread-WGBCVJII.mjs → chunk-DEXID4ZT.mjs} +301 -200
  19. package/dist/chunk-EBIHRL7B.mjs +21 -0
  20. package/dist/{chunk-YIDSHBQS.mjs → chunk-HC5NURS6.mjs} +33 -4
  21. package/dist/{chunk-V7G627TA.mjs → chunk-HFJXEFFV.mjs} +13 -62
  22. package/dist/{chunk-63HFWQEA.mjs → chunk-IE43SRL3.mjs} +276 -102
  23. package/dist/chunk-IWVEG5CX.mjs +67 -0
  24. package/dist/{chunk-2IFU7SXG.mjs → chunk-KGVC6KJ3.mjs} +4 -4
  25. package/dist/{chunk-7CI34JAP.mjs → chunk-KVLX65KD.mjs} +4 -4
  26. package/dist/{chunk-5WEBVYOA.mjs → chunk-N53HN5VA.mjs} +16 -9
  27. package/dist/chunk-N6J6EJW3.mjs +198 -0
  28. package/dist/{chunk-PT7VRWS7.mjs → chunk-NX7KU77V.mjs} +3 -2
  29. package/dist/{chunk-QIFOXLWF.mjs → chunk-OGFSUXYY.mjs} +2 -2
  30. package/dist/chunk-PELHVTO7.mjs +24 -0
  31. package/dist/{chunk-V7ISFBFC.mjs → chunk-Q62NDKEC.mjs} +5 -4
  32. package/dist/{chunk-6OXNHLNZ.mjs → chunk-RIIUZJQ3.mjs} +1 -1
  33. package/dist/{chunk-BVOUSX3U.mjs → chunk-UDZYIWDP.mjs} +3 -3
  34. package/dist/{chunk-HNAP2KTG.mjs → chunk-VFAXPDBH.mjs} +1 -0
  35. package/dist/{chunk-IBTKJIV7.mjs → chunk-VOKEYJHU.mjs} +1 -1
  36. package/dist/{chunk-C7SLA6CM.mjs → chunk-W55X2BYN.mjs} +5 -1
  37. package/dist/{chunk-IM76DLA7.mjs → chunk-YCFQ5754.mjs} +2 -2
  38. package/dist/{chunk-VLRT47GU.mjs → chunk-YCSRB3IN.mjs} +3 -3
  39. package/dist/{chunk-CQUMNIIG.mjs → chunk-Z4DZ4KK2.mjs} +1 -0
  40. package/dist/{chunk-SOADQINJ.mjs → chunk-ZATRDAYU.mjs} +31 -3
  41. package/dist/{chunk-2RAQKITH.mjs → chunk-ZR63OGT2.mjs} +1 -1
  42. package/dist/{client-BdnzDFtO.d.ts → client-BGzLNt78.d.ts} +2 -2
  43. package/dist/client.d.ts +6 -6
  44. package/dist/{config-ujZESkJz.d.ts → config-CS5ADSrx.d.ts} +18 -2
  45. package/dist/{config-CSgvApt0.d.ts → config-CSYMTTm1.d.ts} +1 -1
  46. package/dist/{direct-executor-ZSTKRA6Z.mjs → direct-executor-OUY4RPTU.mjs} +7 -6
  47. package/dist/distribution.d.ts +5 -5
  48. package/dist/distribution.mjs +79 -42
  49. package/dist/{durability-wiring-EQTFL3B4.mjs → durability-wiring-5V6DVHFK.mjs} +17 -16
  50. package/dist/{engine-C_HSy3iz.d.ts → engine-CCBBGmMR.d.ts} +1 -1
  51. package/dist/{events-CJsXF1F9.d.ts → events-D8GPB1-H.d.ts} +5 -0
  52. package/dist/index.browser.mjs +1 -1
  53. package/dist/index.d.ts +5 -5
  54. package/dist/index.mjs +10 -9
  55. package/dist/long-query-thread-HMQM2FWL.mjs +118 -0
  56. package/dist/{module-WP4VVGV2.mjs → module-3IMTXNPW.mjs} +92 -29
  57. package/dist/pattern.mjs +1 -1
  58. package/dist/persistence/durability/checkpoint-worker.mjs +14 -13
  59. package/dist/react.d.ts +6 -6
  60. package/dist/{rerank-3POMPQNX.mjs → rerank-NYX5S7OG.mjs} +3 -3
  61. package/dist/{restore-queue-WZTMTJP2.mjs → restore-queue-J6PCG4ON.mjs} +1 -1
  62. package/dist/{runtime-C2435MPK.mjs → runtime-XNYW6AWX.mjs} +2 -2
  63. package/dist/server.d.ts +8 -8
  64. package/dist/server.mjs +110 -258
  65. package/dist/{snapshot-EU6NNB2P.mjs → snapshot-57WF53MK.mjs} +18 -17
  66. package/dist/{tasks-CI3Fvkxw.d.ts → tasks-5_LCFEzF.d.ts} +1 -1
  67. package/dist/thread-FDEHU5G3.mjs +31 -0
  68. package/dist/vector/search-worker.mjs +2 -2
  69. package/dist/workers/entry.mjs +19 -7
  70. package/dist/{write-CKRWS3P5.mjs → write-YEI7A5OR.mjs} +11 -10
  71. package/package.json +7 -7
@@ -1,202 +1,21 @@
1
- import { loadUWebSockets, childAppOf, createRouteRunner, corsHeaderReader, createSharedBodyBudget, createSharedRequestGate, registerServerRoutes, closeWhenTheProcessExits, sendRelayed, createDocumentReadHandlers, respondJson, SERVER_CAPABILITIES, versionReport, statusLine, sendError, serverRoutes, parseJson, validateSuggest, rejectInvalid, respondError, validateQuery, refuseRegex, withServerRerankWindow, awaitDepartureConfirmation, departureConfirmation, REQUEST_THREAD_DEPARTURE_DRAIN_MS, departureConfirmed, encodeJson } from './chunk-63HFWQEA.mjs';
1
+ import { sendRelayed, parseJson, validateQuery, rejectInvalid, refuseRegex, respondJson, withServerRerankWindow, respondError, loadUWebSockets, childAppOf, createRouteRunner, corsHeaderReader, createSharedBodyBudget, createSharedRequestGate, registerServerRoutes, closeWhenTheProcessExits, statusLine, sendError, REQUEST_THREAD_MOVE_AFTER_MS, MAX_LONG_QUERIES_IN_FLIGHT, createDocumentReadHandlers, SERVER_CAPABILITIES, versionReport, serverRoutes, validateSuggest, SEARCH_CLOCK_SAMPLE_UNITS, awaitDepartureConfirmation, departureConfirmation, REQUEST_THREAD_DEPARTURE_DRAIN_MS, departureConfirmed, encodeJson } from './chunk-IE43SRL3.mjs';
2
2
  import { withRerankQueryText } from './chunk-KZ3NX2P7.mjs';
3
- import './chunk-V7G627TA.mjs';
4
- import './chunk-DJHHF2L5.mjs';
5
- import { executeListDocuments, executeSuggest, executePreflight, queryBindingOf, executeQuery } from './chunk-GLZI636G.mjs';
6
- import { resolveProjection, projectionKeepsField, setNestedValue } from './chunk-5WEBVYOA.mjs';
7
- import './chunk-SOADQINJ.mjs';
8
- import './chunk-HJSN7UYG.mjs';
9
- import './chunk-LXWDQLTW.mjs';
10
- import './chunk-QSK35UC2.mjs';
11
- import './chunk-VK45J47R.mjs';
12
- import './chunk-HNAP2KTG.mjs';
13
- import './chunk-WQBCBKY3.mjs';
14
- import './chunk-KCQG2PJT.mjs';
3
+ import { executeListDocuments, executeSuggest, executeQuery, lazyQueryBinding, executePreflight } from './chunk-5VVSHCXT.mjs';
4
+ import { resolveProjection, projectionKeepsField, setNestedValue } from './chunk-N53HN5VA.mjs';
15
5
  import { extractVectorFieldsFromSchema } from './chunk-DG62H7OD.mjs';
16
- import './chunk-U476IXKQ.mjs';
17
- import './chunk-E74YLAIG.mjs';
18
- import './chunk-VJS4JFBQ.mjs';
19
- import './chunk-GES7U2PM.mjs';
20
- import './chunk-6ODIY6ZB.mjs';
21
- import './chunk-X5CV2E2B.mjs';
22
- import './chunk-OFYKBKKV.mjs';
23
- import './chunk-R6A773WK.mjs';
24
- import './chunk-YTJCGKM4.mjs';
25
- import './chunk-6OXNHLNZ.mjs';
26
- import './chunk-ZXZ26WSO.mjs';
27
- import './chunk-L5ORED7V.mjs';
28
6
  import { ServerErrorCodes, ErrorCodes, NarsilError, isNarsilError } from './chunk-OBWUWZRA.mjs';
29
7
 
30
- // src/server/request-threads/held-vectors.ts
31
- function managerWithHeldVectors(manager, fieldPaths, vectorOf) {
32
- function get(docId, projection) {
33
- const document = manager.get(docId, projection);
34
- if (document === void 0) return void 0;
35
- for (const fieldPath of fieldPaths) {
36
- if (projection !== void 0 && !projectionKeepsField(projection, fieldPath)) continue;
37
- const vector = vectorOf(fieldPath, docId);
38
- if (vector !== void 0)
39
- setNestedValue(document, fieldPath, Float32Array.from(vector));
40
- }
41
- return document;
42
- }
43
- const reader = Object.create(manager);
44
- reader.get = get;
45
- return reader;
46
- }
47
-
48
- // src/server/request-threads/read-engine.ts
49
- function createThreadReadEngine(options) {
50
- const { executor, relay, searchHooks, patternWorkCap, maxRerankWindow } = options;
51
- function vectorFieldsOf(context) {
52
- return [...extractVectorFieldsFromSchema(context.config.schema).keys()];
53
- }
54
- function holdsEveryVectorField(indexName, context) {
55
- return vectorFieldsOf(context).every((fieldPath) => executor.holdsVectorField(indexName, fieldPath));
56
- }
57
- function holdsEveryVectorFieldRead(indexName, context, params) {
58
- if (params.document === false) return true;
59
- const projection = resolveProjection(params.document === true ? void 0 : params.document);
60
- return vectorFieldsOf(context).every(
61
- (fieldPath) => !projectionKeepsField(projection, fieldPath) || executor.holdsVectorField(indexName, fieldPath)
62
- );
63
- }
64
- function managerOf(indexName, context) {
65
- const fieldPaths = vectorFieldsOf(context);
66
- if (fieldPaths.length === 0) return context.manager;
67
- return managerWithHeldVectors(
68
- context.manager,
69
- fieldPaths,
70
- (fieldPath, docId) => executor.heldVectorOf(indexName, fieldPath, docId)
71
- );
72
- }
73
- function heldContext(indexName) {
74
- return executor.queryContextOf(indexName) ?? null;
75
- }
76
- function wholeDocumentsContext(indexName) {
77
- const context = heldContext(indexName);
78
- return context !== null && holdsEveryVectorField(indexName, context) ? context : null;
79
- }
80
- function requireAnswerable(indexName, context) {
81
- if (context === null) {
82
- throw new NarsilError(
83
- ErrorCodes.INDEX_NOT_FOUND,
84
- `This thread holds no copy of index "${indexName}" that answers this request yet`,
85
- { indexName }
86
- );
87
- }
88
- relay.touch(indexName);
89
- return context;
90
- }
91
- function requireWholeDocuments(indexName) {
92
- const context = requireAnswerable(indexName, wholeDocumentsContext(indexName));
93
- return { context, manager: managerOf(indexName, context) };
94
- }
95
- function rerankRunnerFor(params) {
96
- if (params.rerank === void 0) return void 0;
97
- return {
98
- maxWindow: maxRerankWindow,
99
- score: (adapter, query, documents) => relay.rerank(adapter, query, documents)
100
- };
101
- }
102
- async function resolvedParams(indexName, request) {
103
- const params = withRerankQueryText(request);
104
- if (params.vector?.text === void 0) return params;
105
- const value = await relay.embed(indexName, params.vector);
106
- const { text: _text, ...vector } = params.vector;
107
- return { ...params, vector: { ...vector, value } };
108
- }
109
- function searchableContext(indexName, params) {
110
- if (searchHooks) return null;
111
- const context = executor.queryContextOf(indexName);
112
- if (context === void 0) return null;
113
- const field = params.vector?.field;
114
- if (field === void 0) return context;
115
- const vectorFields = extractVectorFieldsFromSchema(context.config.schema);
116
- if (!vectorFields.has(field)) return context;
117
- return context.vectorSearchers.has(field) && executor.holdsVectorField(indexName, field) ? context : null;
118
- }
119
- function answerableContext(indexName, params) {
120
- const context = searchableContext(indexName, params);
121
- return context !== null && holdsEveryVectorFieldRead(indexName, context, params) ? context : null;
122
- }
123
- return {
124
- holdsCopyOf: (indexName) => heldContext(indexName) !== null,
125
- holdsDocumentsOf: (indexName) => wholeDocumentsContext(indexName) !== null,
126
- canAnswer: (indexName, params) => answerableContext(indexName, params) !== null,
127
- canCount: (indexName, params) => searchableContext(indexName, params) !== null,
128
- async query(indexName, params) {
129
- const resolved = await resolvedParams(indexName, params);
130
- const context = requireAnswerable(indexName, answerableContext(indexName, params));
131
- const result = await executeQuery(resolved, {
132
- manager: managerOf(indexName, context),
133
- language: context.language,
134
- config: context.config,
135
- indexName,
136
- cursorBinding: queryBindingOf(params),
137
- vectorSearchers: context.vectorSearchers,
138
- patternWorkCap,
139
- rerank: rerankRunnerFor(params)
140
- });
141
- if (context.analysisStale) result.analysisStale = true;
142
- return result;
143
- },
144
- async preflight(indexName, params) {
145
- const resolved = await resolvedParams(indexName, params);
146
- const context = requireAnswerable(indexName, searchableContext(indexName, params));
147
- const result = await executePreflight(resolved, {
148
- manager: context.manager,
149
- language: context.language,
150
- config: context.config,
151
- indexName,
152
- cursorBinding: queryBindingOf(params),
153
- vectorSearchers: context.vectorSearchers,
154
- patternWorkCap
155
- });
156
- if (context.analysisStale) result.analysisStale = true;
157
- return result;
158
- },
159
- async suggest(indexName, params) {
160
- const context = requireAnswerable(indexName, heldContext(indexName));
161
- const result = executeSuggest(context.manager, context.language, params);
162
- if (context.analysisStale) result.analysisStale = true;
163
- return result;
164
- },
165
- async get(indexName, docId) {
166
- return requireWholeDocuments(indexName).manager.get(docId);
167
- },
168
- async getMultiple(indexName, docIds) {
169
- const { manager } = requireWholeDocuments(indexName);
170
- const found = /* @__PURE__ */ new Map();
171
- for (const docId of docIds) {
172
- const document = manager.get(docId);
173
- if (document !== void 0) found.set(docId, document);
174
- }
175
- return found;
176
- },
177
- async has(indexName, docId) {
178
- return requireAnswerable(indexName, heldContext(indexName)).manager.has(docId);
179
- },
180
- async countDocuments(indexName) {
181
- return requireAnswerable(indexName, heldContext(indexName)).manager.countDocuments();
182
- },
183
- async listDocuments(indexName, params) {
184
- const { context, manager } = requireWholeDocuments(indexName);
185
- return executeListDocuments(params ?? {}, {
186
- manager,
187
- schema: context.config.schema,
188
- strict: context.config.strict,
189
- patternWorkCap
190
- });
191
- }
192
- };
8
+ // src/server/request-threads/messages.ts
9
+ function isLongQueryPortNote(message) {
10
+ if (typeof message !== "object" || message === null) return false;
11
+ return message.type === "long-query-port";
193
12
  }
194
13
 
195
14
  // src/server/request-threads/relay-client.ts
196
15
  function isRelayReply(message) {
197
16
  if (typeof message !== "object" || message === null) return false;
198
17
  const { type, requestId } = message;
199
- return typeof requestId === "number" && (type === "response" || type === "authorized" || type === "embedded" || type === "reranked");
18
+ return typeof requestId === "number" && (type === "response" || type === "authorized" || type === "embedded" || type === "reranked" || type === "writes-applied");
200
19
  }
201
20
  function isStreamPart(message) {
202
21
  if (typeof message !== "object" || message === null) return false;
@@ -311,7 +130,7 @@ function createRelayClient(port, touchIntervalMs) {
311
130
  if (reply.denial !== null) return { allowed: false, denial: reply.denial };
312
131
  return { allowed: true };
313
132
  },
314
- async request(route, ctx) {
133
+ async request(route, ctx, onUnavailable) {
315
134
  let reply;
316
135
  const body = relayedBody(ctx.rawBody);
317
136
  try {
@@ -335,13 +154,16 @@ function createRelayClient(port, touchIntervalMs) {
335
154
  query: ctx.query.toString(),
336
155
  contentType: ctx.contentType,
337
156
  rawBody: body.bytes,
157
+ bodyRefusal: ctx.bodyRefusal,
338
158
  spooledBody: ctx.spooledBody === null ? null : { path: ctx.spooledBody.path, bytes: ctx.spooledBody.bytes },
339
159
  hookContext: ctx.hookContext
340
160
  };
341
161
  }, body.moved).then(resolve, reject);
342
162
  });
343
163
  } catch {
344
- if (!ctx.abort.aborted) sendUnavailable(ctx.res);
164
+ if (ctx.abort.aborted) return;
165
+ if (onUnavailable === void 0) sendUnavailable(ctx.res);
166
+ else onUnavailable();
345
167
  return;
346
168
  }
347
169
  if (reply === null || reply.type !== "response" || ctx.abort.aborted) return;
@@ -363,6 +185,9 @@ function createRelayClient(port, touchIntervalMs) {
363
185
  new NarsilError(ErrorCodes.RERANK_FAILED, "The main thread returned no scores")
364
186
  );
365
187
  },
188
+ async awaitWrites(indexName) {
189
+ await send((requestId) => ({ type: "await-writes", requestId, indexName }));
190
+ },
366
191
  touch(indexName) {
367
192
  if (closed) return;
368
193
  const now = Date.now();
@@ -382,6 +207,260 @@ function createRelayClient(port, touchIntervalMs) {
382
207
  };
383
208
  }
384
209
 
210
+ // src/server/request-threads/long-query-route.ts
211
+ function routeLongQueries(mainPort, inFlight, touchIntervalMs, firstPort) {
212
+ const counter = new Int32Array(inFlight);
213
+ let relay = null;
214
+ function relayThrough(port) {
215
+ relay?.close();
216
+ const opened = createRelayClient(port, touchIntervalMs);
217
+ relay = opened;
218
+ port.on("close", () => {
219
+ if (relay === opened) relay = null;
220
+ });
221
+ }
222
+ if (firstPort !== null) relayThrough(firstPort);
223
+ mainPort.on("message", (message) => {
224
+ if (isLongQueryPortNote(message)) relayThrough(message.port);
225
+ });
226
+ return {
227
+ moveAfterMs: () => relay === null ? void 0 : REQUEST_THREAD_MOVE_AFTER_MS,
228
+ async move(route, ctx) {
229
+ const target = relay;
230
+ if (target === null) return false;
231
+ if (Atomics.add(counter, 0, 1) >= MAX_LONG_QUERIES_IN_FLIGHT) {
232
+ Atomics.sub(counter, 0, 1);
233
+ sendError(
234
+ ctx.res,
235
+ 429,
236
+ ServerErrorCodes.TOO_MANY_REQUESTS,
237
+ `The server is already answering ${MAX_LONG_QUERIES_IN_FLIGHT} searches whose pattern work is long, so it refuses another. Retry once those searches answer.`,
238
+ { limit: MAX_LONG_QUERIES_IN_FLIGHT }
239
+ );
240
+ return true;
241
+ }
242
+ let counted = true;
243
+ const release = () => {
244
+ if (!counted) return;
245
+ counted = false;
246
+ Atomics.sub(counter, 0, 1);
247
+ };
248
+ ctx.abort.onAbort(release);
249
+ let lost = false;
250
+ try {
251
+ if (!ctx.abort.aborted) {
252
+ await target.request(route, ctx, () => {
253
+ lost = true;
254
+ });
255
+ }
256
+ } finally {
257
+ release();
258
+ }
259
+ return !lost;
260
+ },
261
+ close() {
262
+ relay?.close();
263
+ relay = null;
264
+ }
265
+ };
266
+ }
267
+
268
+ // src/server/request-threads/move-clock.ts
269
+ var SearchMoved = class extends Error {
270
+ constructor() {
271
+ super("This search ran past the point where its thread hands it on");
272
+ this.name = "SearchMoved";
273
+ }
274
+ };
275
+ function moveClock(moveAfterMs) {
276
+ const moveAt = performance.now() + moveAfterMs;
277
+ let pending = 0;
278
+ const clock = {
279
+ tick(units) {
280
+ pending += units;
281
+ if (pending >= SEARCH_CLOCK_SAMPLE_UNITS) clock.check();
282
+ },
283
+ check() {
284
+ pending = 0;
285
+ if (performance.now() > moveAt) throw new SearchMoved();
286
+ }
287
+ };
288
+ return clock;
289
+ }
290
+
291
+ // src/server/request-threads/held-vectors.ts
292
+ function managerWithHeldVectors(manager, fieldPaths, vectorOf) {
293
+ function get(docId, projection) {
294
+ const document = manager.get(docId, projection);
295
+ if (document === void 0) return void 0;
296
+ for (const fieldPath of fieldPaths) {
297
+ if (projection !== void 0 && !projectionKeepsField(projection, fieldPath)) continue;
298
+ const vector = vectorOf(fieldPath, docId);
299
+ if (vector !== void 0)
300
+ setNestedValue(document, fieldPath, Float32Array.from(vector));
301
+ }
302
+ return document;
303
+ }
304
+ const reader = Object.create(manager);
305
+ reader.get = get;
306
+ return reader;
307
+ }
308
+
309
+ // src/server/request-threads/read-engine.ts
310
+ function createThreadReadEngine(options) {
311
+ const { executor, relay, searchHooks, patternWorkCap, maxRerankWindow, moveAfterMs } = options;
312
+ function vectorFieldsOf(context) {
313
+ return [...extractVectorFieldsFromSchema(context.config.schema).keys()];
314
+ }
315
+ function holdsEveryVectorField(indexName, context) {
316
+ return vectorFieldsOf(context).every((fieldPath) => executor.holdsVectorField(indexName, fieldPath));
317
+ }
318
+ function holdsEveryVectorFieldRead(indexName, context, params) {
319
+ if (params.document === false) return true;
320
+ const projection = resolveProjection(params.document === true ? void 0 : params.document);
321
+ return vectorFieldsOf(context).every(
322
+ (fieldPath) => !projectionKeepsField(projection, fieldPath) || executor.holdsVectorField(indexName, fieldPath)
323
+ );
324
+ }
325
+ function managerOf(indexName, context) {
326
+ const fieldPaths = vectorFieldsOf(context);
327
+ if (fieldPaths.length === 0) return context.manager;
328
+ return managerWithHeldVectors(
329
+ context.manager,
330
+ fieldPaths,
331
+ (fieldPath, docId) => executor.heldVectorOf(indexName, fieldPath, docId)
332
+ );
333
+ }
334
+ function heldContext(indexName) {
335
+ return executor.queryContextOf(indexName) ?? null;
336
+ }
337
+ function wholeDocumentsContext(indexName) {
338
+ const context = heldContext(indexName);
339
+ return context !== null && holdsEveryVectorField(indexName, context) ? context : null;
340
+ }
341
+ function requireAnswerable(indexName, context) {
342
+ if (context === null) {
343
+ throw new NarsilError(
344
+ ErrorCodes.INDEX_NOT_FOUND,
345
+ `This thread holds no copy of index "${indexName}" that answers this request yet`,
346
+ { indexName }
347
+ );
348
+ }
349
+ relay.touch(indexName);
350
+ return context;
351
+ }
352
+ function requireWholeDocuments(indexName) {
353
+ const context = requireAnswerable(indexName, wholeDocumentsContext(indexName));
354
+ return { context, manager: managerOf(indexName, context) };
355
+ }
356
+ function rerankRunnerFor(params) {
357
+ if (params.rerank === void 0) return void 0;
358
+ return {
359
+ maxWindow: maxRerankWindow,
360
+ score: (adapter, query2, documents) => relay.rerank(adapter, query2, documents)
361
+ };
362
+ }
363
+ async function resolvedParams(indexName, request) {
364
+ const params = withRerankQueryText(request);
365
+ if (params.vector?.text === void 0) return params;
366
+ const value = await relay.embed(indexName, params.vector);
367
+ const { text: _text, ...vector } = params.vector;
368
+ return { ...params, vector: { ...vector, value } };
369
+ }
370
+ function searchableContext(indexName, params) {
371
+ if (searchHooks) return null;
372
+ const context = executor.queryContextOf(indexName);
373
+ if (context === void 0) return null;
374
+ const field = params.vector?.field;
375
+ if (field === void 0) return context;
376
+ const vectorFields = extractVectorFieldsFromSchema(context.config.schema);
377
+ if (!vectorFields.has(field)) return context;
378
+ return context.vectorSearchers.has(field) && executor.holdsVectorField(indexName, field) ? context : null;
379
+ }
380
+ function answerableContext(indexName, params) {
381
+ const context = searchableContext(indexName, params);
382
+ return context !== null && holdsEveryVectorFieldRead(indexName, context, params) ? context : null;
383
+ }
384
+ async function query(indexName, params, moveAfter) {
385
+ const resolved = await resolvedParams(indexName, params);
386
+ const context = requireAnswerable(indexName, answerableContext(indexName, params));
387
+ const clock = moveAfter === void 0 ? void 0 : moveClock(moveAfter);
388
+ const result = await executeQuery(resolved, {
389
+ manager: managerOf(indexName, context),
390
+ language: context.language,
391
+ config: context.config,
392
+ indexName,
393
+ cursorBinding: lazyQueryBinding(params, clock),
394
+ vectorSearchers: context.vectorSearchers,
395
+ patternWorkCap,
396
+ clock,
397
+ rerank: rerankRunnerFor(params)
398
+ });
399
+ if (context.analysisStale) result.analysisStale = true;
400
+ return result;
401
+ }
402
+ async function preflight(indexName, params, moveAfter) {
403
+ const resolved = await resolvedParams(indexName, params);
404
+ const context = requireAnswerable(indexName, searchableContext(indexName, params));
405
+ const clock = moveAfter === void 0 ? void 0 : moveClock(moveAfter);
406
+ const result = await executePreflight(resolved, {
407
+ manager: context.manager,
408
+ language: context.language,
409
+ config: context.config,
410
+ indexName,
411
+ cursorBinding: lazyQueryBinding(params, clock),
412
+ vectorSearchers: context.vectorSearchers,
413
+ patternWorkCap,
414
+ clock
415
+ });
416
+ if (context.analysisStale) result.analysisStale = true;
417
+ return result;
418
+ }
419
+ return {
420
+ holdsCopyOf: (indexName) => heldContext(indexName) !== null,
421
+ holdsDocumentsOf: (indexName) => wholeDocumentsContext(indexName) !== null,
422
+ canAnswer: (indexName, params) => answerableContext(indexName, params) !== null,
423
+ canCount: (indexName, params) => searchableContext(indexName, params) !== null,
424
+ query: (indexName, params) => query(indexName, params, moveAfterMs?.()),
425
+ queryHere: (indexName, params) => query(indexName, params, void 0),
426
+ preflight: (indexName, params) => preflight(indexName, params, moveAfterMs?.()),
427
+ preflightHere: (indexName, params) => preflight(indexName, params, void 0),
428
+ async suggest(indexName, params) {
429
+ const context = requireAnswerable(indexName, heldContext(indexName));
430
+ const result = executeSuggest(context.manager, context.language, params);
431
+ if (context.analysisStale) result.analysisStale = true;
432
+ return result;
433
+ },
434
+ async get(indexName, docId) {
435
+ return requireWholeDocuments(indexName).manager.get(docId);
436
+ },
437
+ async getMultiple(indexName, docIds) {
438
+ const { manager } = requireWholeDocuments(indexName);
439
+ const found = /* @__PURE__ */ new Map();
440
+ for (const docId of docIds) {
441
+ const document = manager.get(docId);
442
+ if (document !== void 0) found.set(docId, document);
443
+ }
444
+ return found;
445
+ },
446
+ async has(indexName, docId) {
447
+ return requireAnswerable(indexName, heldContext(indexName)).manager.has(docId);
448
+ },
449
+ async countDocuments(indexName) {
450
+ return requireAnswerable(indexName, heldContext(indexName)).manager.countDocuments();
451
+ },
452
+ async listDocuments(indexName, params) {
453
+ const { context, manager } = requireWholeDocuments(indexName);
454
+ return executeListDocuments(params ?? {}, {
455
+ manager,
456
+ schema: context.config.schema,
457
+ strict: context.config.strict,
458
+ patternWorkCap
459
+ });
460
+ }
461
+ };
462
+ }
463
+
385
464
  // src/server/request-threads/thread-departure.ts
386
465
  var DEPARTURE_REFUSAL = {
387
466
  error: {
@@ -570,6 +649,15 @@ function onWholeDocuments(local, handler, fallback) {
570
649
  function mainThreadAnswersInstead(err) {
571
650
  return isNarsilError(err) && err.code === ErrorCodes.WORKER_BUSY;
572
651
  }
652
+ async function answerMoved(ctx, params, limits, options) {
653
+ if (await options.move(ctx)) return;
654
+ try {
655
+ respondJson(ctx, await options.answerHere(ctx.params[0], withServerRerankWindow(params, limits)));
656
+ } catch (err) {
657
+ if (mainThreadAnswersInstead(err)) await options.fallback(ctx);
658
+ else respondError(ctx, err);
659
+ }
660
+ }
573
661
  function searchHandler(options) {
574
662
  const { fallback, limits, allowRegex, staysOnThread, answer } = options;
575
663
  return async (ctx) => {
@@ -588,7 +676,8 @@ function searchHandler(options) {
588
676
  try {
589
677
  respondJson(ctx, await answer(ctx.params[0], withServerRerankWindow(params, limits)));
590
678
  } catch (err) {
591
- if (staysOnThread(ctx.params[0], params) && !mainThreadAnswersInstead(err)) respondError(ctx, err);
679
+ if (err instanceof SearchMoved) await answerMoved(ctx, params, limits, options);
680
+ else if (staysOnThread(ctx.params[0], params) && !mainThreadAnswersInstead(err)) respondError(ctx, err);
592
681
  else await fallback(ctx);
593
682
  }
594
683
  };
@@ -624,7 +713,7 @@ function relayedHandlers(relay, limits) {
624
713
  }
625
714
  return forwarded;
626
715
  }
627
- function threadHandlers(local, relay, settings) {
716
+ function threadHandlers(local, relay, longQueries, settings) {
628
717
  const { allowRegex } = settings;
629
718
  const reads = createDocumentReadHandlers({ engine: local, limits: settings.limits, allowRegex });
630
719
  const forwarded = relayedHandlers(relay, settings.limits);
@@ -638,14 +727,18 @@ function threadHandlers(local, relay, settings) {
638
727
  limits: settings.limits,
639
728
  allowRegex,
640
729
  staysOnThread: (name, params) => local.canAnswer(name, params),
641
- answer: (name, params) => local.query(name, params)
730
+ answer: (name, params) => local.query(name, params),
731
+ answerHere: (name, params) => local.queryHere(name, params),
732
+ move: (ctx) => longQueries.move("search", ctx)
642
733
  }),
643
734
  preflight: searchHandler({
644
735
  fallback: forwarded.preflight,
645
736
  limits: settings.limits,
646
737
  allowRegex,
647
738
  staysOnThread: (name, params) => local.canCount(name, params),
648
- answer: (name, params) => local.preflight(name, params)
739
+ answer: (name, params) => local.preflight(name, params),
740
+ answerHere: (name, params) => local.preflightHere(name, params),
741
+ move: (ctx) => longQueries.move("preflight", ctx)
649
742
  }),
650
743
  suggest: suggestHandler(local, forwarded.suggest),
651
744
  get: onWholeDocuments(local, reads.get, forwarded.get),
@@ -662,6 +755,12 @@ async function serveRequests(executor, settings) {
662
755
  const child = childAppOf(app);
663
756
  if (child === null) throw new Error("This uWebSockets.js build offers no child apps");
664
757
  const relay = createRelayClient(settings.port, settings.touchIntervalMs);
758
+ const longQueries = routeLongQueries(
759
+ settings.port,
760
+ settings.longQueriesInFlight,
761
+ settings.touchIntervalMs,
762
+ settings.longQueryPort
763
+ );
665
764
  const departure = watchForDeparture({
666
765
  relay,
667
766
  departure: new Int32Array(settings.departure),
@@ -672,7 +771,8 @@ async function serveRequests(executor, settings) {
672
771
  relay,
673
772
  searchHooks: settings.searchHooks,
674
773
  patternWorkCap: settings.patternWorkCap,
675
- maxRerankWindow: settings.maxRerankWindow
774
+ maxRerankWindow: settings.maxRerankWindow,
775
+ moveAfterMs: () => longQueries.moveAfterMs()
676
776
  });
677
777
  const run = createRouteRunner({
678
778
  authorize: settings.authorizes ? (context) => relay.authorize(context) : void 0,
@@ -681,19 +781,20 @@ async function serveRequests(executor, settings) {
681
781
  corsHeaders: settings.cors ? corsHeaderReader(settings.cors) : void 0,
682
782
  track: departure.track
683
783
  });
684
- const handlers = guardEveryRoute(threadHandlers(local, relay, settings), departure);
784
+ const handlers = guardEveryRoute(threadHandlers(local, relay, longQueries, settings), departure);
685
785
  registerServerRoutes(app, run, handlers, settings.limits, settings.cors);
686
- servingApp = { app: child, relay };
786
+ servingApp = { app: child, relay, longQueries };
687
787
  void closeWhenTheProcessExits(new Int32Array(settings.closeAtExit), closeRequestThread);
688
788
  return { descriptor: child.getDescriptor() };
689
789
  }
690
790
  function closeRequestThread() {
691
791
  leaveWithoutDeparture();
692
792
  if (servingApp === null) return;
693
- const { app, relay } = servingApp;
793
+ const { app, relay, longQueries } = servingApp;
694
794
  servingApp = null;
695
795
  app.close();
796
+ longQueries.close();
696
797
  relay.close();
697
798
  }
698
799
 
699
- export { closeRequestThread, serveRequests };
800
+ export { closeRequestThread, createRelayClient, createThreadReadEngine, isLongQueryPortNote, searchHandler, serveRequests };
@@ -0,0 +1,21 @@
1
+ import { V8_YOUNG_GENERATION_BYTES_BY_MAJOR } from './chunk-22YPZVMG.mjs';
2
+
3
+ // src/runtime/engine-threads.ts
4
+ var MEGABYTE_BYTES = 1048576;
5
+ var threadYoungGenerationBytes = 0;
6
+ function youngGenerationBytesOfThisV8() {
7
+ const major = Number(globalThis.process?.versions?.v8?.split(".")[0]);
8
+ return V8_YOUNG_GENERATION_BYTES_BY_MAJOR[major] ?? Math.max(...Object.values(V8_YOUNG_GENERATION_BYTES_BY_MAJOR));
9
+ }
10
+ function countEngineThread(thread, youngGenerationMb) {
11
+ const bytes = youngGenerationMb === void 0 ? youngGenerationBytesOfThisV8() : youngGenerationMb * MEGABYTE_BYTES;
12
+ threadYoungGenerationBytes += bytes;
13
+ thread.once("exit", () => {
14
+ threadYoungGenerationBytes -= bytes;
15
+ });
16
+ }
17
+ function engineThreadYoungGenerationBytes() {
18
+ return threadYoungGenerationBytes;
19
+ }
20
+
21
+ export { countEngineThread, engineThreadYoungGenerationBytes, youngGenerationBytesOfThisV8 };