@delali/narsil 0.3.1-next.145 → 0.3.1-next.146

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 (207) hide show
  1. package/dist/adapters/filesystem.mjs +2 -2
  2. package/dist/adapters/gcs.mjs +2 -2
  3. package/dist/adapters/indexeddb.mjs +1 -1
  4. package/dist/adapters/s3.mjs +2 -2
  5. package/dist/analysis-change-ZCJNYBJM.mjs +39 -0
  6. package/dist/{analysis-change-entry-V4SQQSXQ.mjs → analysis-change-entry-YN3ELL6C.mjs} +1 -1
  7. package/dist/browser/{analysis-change-QTCQ5ETU.mjs → analysis-change-M65AZQQG.mjs} +8 -5
  8. package/dist/browser/{analysis-change-entry-KYEQSP3I.mjs → analysis-change-entry-H3YIOLXZ.mjs} +1 -1
  9. package/dist/browser/{chunk-GP4QLUQM.mjs → chunk-2JABMB4S.mjs} +10 -6
  10. package/dist/browser/{chunk-5WYWLTM5.mjs → chunk-4DYLCICG.mjs} +2 -2
  11. package/dist/browser/{chunk-75PMIFQA.mjs → chunk-4RD3AFSB.mjs} +1 -1
  12. package/dist/browser/{chunk-XWMEGKDC.mjs → chunk-5V74FFZE.mjs} +2 -2
  13. package/dist/browser/{chunk-PMBSBSCN.mjs → chunk-6OWX3OP2.mjs} +2 -2
  14. package/dist/browser/chunk-BRWY3JCN.mjs +678 -0
  15. package/dist/browser/{chunk-ZALEKEUH.mjs → chunk-BVX2MEHA.mjs} +306 -607
  16. package/dist/browser/{chunk-FXPMF65P.mjs → chunk-D6UFGBF5.mjs} +15 -45
  17. package/dist/browser/{chunk-QNDWMPV2.mjs → chunk-IAXZAVYL.mjs} +25 -66
  18. package/dist/browser/chunk-JNOAKDUM.mjs +31 -0
  19. package/dist/browser/{chunk-BTMRV5MY.mjs → chunk-KAEVGLYN.mjs} +2 -2
  20. package/dist/browser/{chunk-FHO5N7FD.mjs → chunk-LURXVCHG.mjs} +1 -1
  21. package/dist/browser/chunk-MPK76GVS.mjs +63 -0
  22. package/dist/browser/{chunk-6OFS7RUN.mjs → chunk-NVEJJP4R.mjs} +1 -1
  23. package/dist/browser/{chunk-U77XQJOY.mjs → chunk-ODQP7P3H.mjs} +455 -66
  24. package/dist/browser/{chunk-IFGTRRPU.mjs → chunk-PIS7ZAP7.mjs} +3 -25
  25. package/dist/browser/{chunk-XIHLIMFX.mjs → chunk-QMNZFFD3.mjs} +4 -3
  26. package/dist/browser/{chunk-AOKLYVTT.mjs → chunk-SRL47USS.mjs} +2 -2
  27. package/dist/browser/chunk-TY7L6OXO.mjs +69 -0
  28. package/dist/browser/{chunk-LGJ6XMH5.mjs → chunk-W643QV3L.mjs} +6 -5
  29. package/dist/browser/chunk-WXLBZJMT.mjs +25 -0
  30. package/dist/browser/{chunk-ZKVDQ5ZH.mjs → chunk-XILIT5MT.mjs} +8 -8
  31. package/dist/browser/{cold-module-Y2IJAP7S.mjs → cold-module-A5S6JFDN.mjs} +20 -20
  32. package/dist/browser/{durability-wiring-MLXZ5QQF.mjs → durability-wiring-W5AVV3RF.mjs} +53 -30
  33. package/dist/browser/gzip-5S7ROJFJ.mjs +1 -0
  34. package/dist/browser/import-PTT63AGK.mjs +12 -0
  35. package/dist/browser/{module-5LMDGXQH.mjs → module-CEKA7MQH.mjs} +13 -10
  36. package/dist/browser/module-X5PGOE47.mjs +4288 -0
  37. package/dist/browser/{phrase-module-KXNRTWCS.mjs → phrase-module-CNKMUY4U.mjs} +2 -1
  38. package/dist/browser/pool-DTYSPBMQ.mjs +296 -0
  39. package/dist/{restore-queue-UAVGBSJG.mjs → browser/restore-queue-DD4LWZK7.mjs} +2 -2
  40. package/dist/browser/{runtime-ZNEGK33G.mjs → runtime-D2ULZGCN.mjs} +3 -2
  41. package/dist/browser/{snapshot-2EGS2FYU.mjs → snapshot-DQ6A4PQP.mjs} +18 -14
  42. package/dist/browser/{write-WMZA6NSF.mjs → write-MAGRAIEH.mjs} +14 -11
  43. package/dist/{capabilities-CNnzGtqP.d.ts → capabilities-BT-HLDC8.d.ts} +2 -2
  44. package/dist/{chunk-FRJ5RGX7.mjs → chunk-2HJWQPR6.mjs} +2 -2
  45. package/dist/{chunk-DKMZ7JFW.mjs → chunk-3BVGJ4JR.mjs} +10 -4
  46. package/dist/{chunk-QYMAYU7M.mjs → chunk-3HV7M5OJ.mjs} +155 -149
  47. package/dist/chunk-3MXV3VHR.mjs +46 -0
  48. package/dist/{chunk-6FOJKNUR.mjs → chunk-3SBJVQLL.mjs} +2 -2
  49. package/dist/{chunk-PRID7CA6.mjs → chunk-3TVDTACC.mjs} +1 -1
  50. package/dist/{chunk-RTVAWNUL.mjs → chunk-5WB2A3XU.mjs} +9 -6
  51. package/dist/{chunk-LWFK3TC2.mjs → chunk-65WCV5JO.mjs} +1 -1
  52. package/dist/{chunk-G6Q5DEF5.mjs → chunk-6BWLDLS3.mjs} +3 -46
  53. package/dist/{chunk-W6M4RRAT.mjs → chunk-6C5EX2QR.mjs} +3 -3
  54. package/dist/{chunk-HOC5JP2H.mjs → chunk-6F6USYII.mjs} +119 -11
  55. package/dist/{chunk-7523VCM4.mjs → chunk-7HKWO4BJ.mjs} +7 -7
  56. package/dist/{chunk-ONDEEBTA.mjs → chunk-7N223GWV.mjs} +2 -2
  57. package/dist/{chunk-UGQULMSU.mjs → chunk-ADBS5XRI.mjs} +10 -10
  58. package/dist/{chunk-MJTEU2XK.mjs → chunk-AWNHDBKV.mjs} +429 -73
  59. package/dist/{chunk-2E2ZFOPT.mjs → chunk-B2FTZNNF.mjs} +2 -2
  60. package/dist/{chunk-NWTEJ2T7.mjs → chunk-B7XIOAMK.mjs} +3 -3
  61. package/dist/{chunk-W55X2BYN.mjs → chunk-BEZH7VEH.mjs} +1 -1
  62. package/dist/chunk-BTPGQC6Z.mjs +27 -0
  63. package/dist/{chunk-KG5N7LE6.mjs → chunk-CYYSZPVV.mjs} +11 -11
  64. package/dist/{chunk-HTMUGEFU.mjs → chunk-DOY6EG76.mjs} +6 -6
  65. package/dist/{chunk-UHVTEVF5.mjs → chunk-F22IOCR2.mjs} +10 -6
  66. package/dist/{chunk-FTQLF5JI.mjs → chunk-IRWP524D.mjs} +1 -1
  67. package/dist/{chunk-XLAI4OIO.mjs → chunk-IZT3G24Q.mjs} +2 -2
  68. package/dist/{chunk-SE7MKJVK.mjs → chunk-J7M73VY7.mjs} +1 -1
  69. package/dist/chunk-KAKQ74LS.mjs +53 -0
  70. package/dist/{chunk-GVPYMSLO.mjs → chunk-KYVRZAYP.mjs} +1 -1
  71. package/dist/{chunk-F442VZUL.mjs → chunk-KZ5GQE4Z.mjs} +5 -5
  72. package/dist/{chunk-WDEWU46C.mjs → chunk-KZQIVGW7.mjs} +2 -2
  73. package/dist/{chunk-7AJAW6NN.mjs → chunk-L7TIAYPX.mjs} +8 -8
  74. package/dist/{chunk-GRFXSEFF.mjs → chunk-LDGGR7YT.mjs} +4 -81
  75. package/dist/{chunk-WMIV7PJ7.mjs → chunk-LJ4ENXOD.mjs} +15 -14
  76. package/dist/chunk-LQG5G2LY.mjs +39 -0
  77. package/dist/{chunk-IQHZMCLX.mjs → chunk-MDF6PDIA.mjs} +2 -0
  78. package/dist/{chunk-QXH2CS4V.mjs → chunk-NVTFQFUL.mjs} +7 -26
  79. package/dist/{chunk-5K2JHKM3.mjs → chunk-NYSONUB7.mjs} +1 -1
  80. package/dist/{chunk-OH7SX5MY.mjs → chunk-O4KUNDVF.mjs} +12 -10
  81. package/dist/{chunk-EBIHRL7B.mjs → chunk-Q2UL2TPC.mjs} +1 -1
  82. package/dist/{chunk-RNUP6WZ6.mjs → chunk-QEYF2SQ6.mjs} +2 -2
  83. package/dist/{chunk-RVBUQITB.mjs → chunk-QN5WVV5K.mjs} +7 -7
  84. package/dist/{chunk-RIIUZJQ3.mjs → chunk-QYRNLLLI.mjs} +1 -1
  85. package/dist/{chunk-BTARFTEN.mjs → chunk-SOUZMPME.mjs} +4 -4
  86. package/dist/{chunk-ISUOSACK.mjs → chunk-T4CJDBBE.mjs} +1 -1
  87. package/dist/{chunk-DHEJAGVW.mjs → chunk-TTCKT2RT.mjs} +5 -5
  88. package/dist/{chunk-QU2XF7VI.mjs → chunk-U2726TL5.mjs} +5 -4
  89. package/dist/{chunk-LYKUJNAV.mjs → chunk-U46IZNCW.mjs} +2 -2
  90. package/dist/{chunk-SNB4DYL3.mjs → chunk-U6NE6RCV.mjs} +4 -4
  91. package/dist/{chunk-SA52N7G3.mjs → chunk-VP6XRMWR.mjs} +4 -4
  92. package/dist/{chunk-YUCGFPMS.mjs → chunk-VQDZVWN3.mjs} +449 -405
  93. package/dist/{chunk-IWVEG5CX.mjs → chunk-XIHUI4KB.mjs} +2 -2
  94. package/dist/{chunk-IV2XTSS2.mjs → chunk-XKPNKR3V.mjs} +16 -46
  95. package/dist/{chunk-UQS5PAS5.mjs → chunk-YGQOPB2Z.mjs} +1 -1
  96. package/dist/{chunk-SVO6NV4D.mjs → chunk-YKEHQUSZ.mjs} +4 -4
  97. package/dist/{chunk-6Z22L5RN.mjs → chunk-ZAYWOHVG.mjs} +1 -1
  98. package/dist/{chunk-QLDLQW36.mjs → chunk-ZEKDO7YX.mjs} +2 -2
  99. package/dist/chunk-ZXNNR73R.mjs +31 -0
  100. package/dist/{chunk-VJS4JFBQ.mjs → chunk-ZXX5GZEX.mjs} +3 -1
  101. package/dist/{chunk-X5SBWARI.mjs → chunk-ZYKUD5NX.mjs} +9 -7
  102. package/dist/{client-DoYPHcUn.d.ts → client-Cn3gNxUU.d.ts} +3 -3
  103. package/dist/client.d.ts +5 -5
  104. package/dist/client.mjs +2 -0
  105. package/dist/{cold-module-NUD332YD.mjs → cold-module-2WWGBFJC.mjs} +36 -33
  106. package/dist/{config-CuUfls_H.d.ts → config-CfhH1IWQ.d.ts} +1 -1
  107. package/dist/{config-Dzs46im6.d.ts → config-aIsy7mcL.d.ts} +52 -1
  108. package/dist/direct-executor-QLVHMELJ.mjs +24 -0
  109. package/dist/distribution/coordinator/etcd.mjs +1 -1
  110. package/dist/distribution/transport/grpc.mjs +1 -1
  111. package/dist/distribution.d.ts +4 -4
  112. package/dist/distribution.mjs +150 -255
  113. package/dist/{durability-wiring-K7RDXWL6.mjs → durability-wiring-GNWQSZI3.mjs} +67 -42
  114. package/dist/embeddings/openai.mjs +2 -2
  115. package/dist/{engine-C0ifUEcm.d.ts → engine-CJRUHXgf.d.ts} +1 -1
  116. package/dist/gzip-F4HMIEEQ.mjs +1 -0
  117. package/dist/import-DX55PN4G.mjs +41 -0
  118. package/dist/index.browser.mjs +7 -4
  119. package/dist/index.d.ts +539 -5
  120. package/dist/index.mjs +22 -16
  121. package/dist/index.react-native.mjs +19 -0
  122. package/dist/{long-query-thread-FKVHRCHF.mjs → long-query-thread-SCUE62TO.mjs} +20 -17
  123. package/dist/{module-GJUKW5VU.mjs → module-HW6IXUM6.mjs} +601 -108
  124. package/dist/{module-TMFIWOD2.mjs → module-V5K4ZP23.mjs} +29 -23
  125. package/dist/{paged-partition-archive-WNUEMYWM.mjs → paged-partition-archive-FDIIWZNR.mjs} +31 -25
  126. package/dist/partial-words.mjs +5 -5
  127. package/dist/pattern.mjs +3 -3
  128. package/dist/persistence/durability/checkpoint-worker.mjs +28 -22
  129. package/dist/{phrase-module-OMY3QQV7.mjs → phrase-module-OUQIRUQU.mjs} +1 -1
  130. package/dist/pool-UPV5PVPY.mjs +298 -0
  131. package/dist/react-native/chunks/analysis-change-HGW3L6NY.mjs +893 -0
  132. package/dist/react-native/chunks/analysis-change-entry-BNRU6STO.mjs +1 -0
  133. package/dist/react-native/chunks/chunk-2J2727V4.mjs +495 -0
  134. package/dist/react-native/chunks/chunk-3CTWSQRW.mjs +1251 -0
  135. package/dist/react-native/chunks/chunk-5FV4ZEVA.mjs +2619 -0
  136. package/dist/react-native/chunks/chunk-7VWQLEFV.mjs +642 -0
  137. package/dist/react-native/chunks/chunk-AAAETEJK.mjs +27 -0
  138. package/dist/react-native/chunks/chunk-AM7RSBPF.mjs +32 -0
  139. package/dist/react-native/chunks/chunk-B6OD63Z3.mjs +997 -0
  140. package/dist/react-native/chunks/chunk-BRNAVOKP.mjs +177 -0
  141. package/dist/react-native/chunks/chunk-DKJTZ625.mjs +207 -0
  142. package/dist/react-native/chunks/chunk-FKGG4UOF.mjs +177 -0
  143. package/dist/{browser/chunk-K7UFXEJS.mjs → react-native/chunks/chunk-GV5KFQ6M.mjs} +12 -6
  144. package/dist/react-native/chunks/chunk-HURV3ASY.mjs +4416 -0
  145. package/dist/react-native/chunks/chunk-IO2SMUQD.mjs +10 -0
  146. package/dist/react-native/chunks/chunk-JV6VQY2S.mjs +17 -0
  147. package/dist/react-native/chunks/chunk-KUJOBRKS.mjs +217 -0
  148. package/dist/react-native/chunks/chunk-KWRW5U65.mjs +226 -0
  149. package/dist/react-native/chunks/chunk-L4M7QJNP.mjs +110 -0
  150. package/dist/react-native/chunks/chunk-LQ6Q4L5B.mjs +360 -0
  151. package/dist/react-native/chunks/chunk-LQAKO5QV.mjs +523 -0
  152. package/dist/react-native/chunks/chunk-LRGWK367.mjs +11 -0
  153. package/dist/react-native/chunks/chunk-LVIKKAA6.mjs +93 -0
  154. package/dist/react-native/chunks/chunk-NFGBOI46.mjs +14 -0
  155. package/dist/react-native/chunks/chunk-NNE4VDMM.mjs +8 -0
  156. package/dist/react-native/chunks/chunk-PPEPTUVV.mjs +56 -0
  157. package/dist/react-native/chunks/chunk-PSWKQVVT.mjs +9 -0
  158. package/dist/react-native/chunks/chunk-PU55IOG4.mjs +910 -0
  159. package/dist/react-native/chunks/chunk-QC4ZRBSX.mjs +270 -0
  160. package/dist/react-native/chunks/chunk-R4EEMYM6.mjs +21 -0
  161. package/dist/react-native/chunks/chunk-R4OPUZQ5.mjs +86 -0
  162. package/dist/react-native/chunks/chunk-SLGIAD6B.mjs +2634 -0
  163. package/dist/react-native/chunks/chunk-TJMSKBEM.mjs +25 -0
  164. package/dist/react-native/chunks/chunk-TLYD7IJ2.mjs +23 -0
  165. package/dist/react-native/chunks/chunk-UFNCLEQM.mjs +73 -0
  166. package/dist/react-native/chunks/chunk-YH7W556M.mjs +179 -0
  167. package/dist/react-native/chunks/chunk-YJSMEKRE.mjs +22385 -0
  168. package/dist/react-native/chunks/chunk-ZCKNBPQ4.mjs +79 -0
  169. package/dist/react-native/chunks/cold-module-TSFWLYWC.mjs +755 -0
  170. package/dist/react-native/chunks/durability-wiring-PZR6KOQK.mjs +4509 -0
  171. package/dist/react-native/chunks/gzip.react-native-7JWCLVOB.mjs +3 -0
  172. package/dist/react-native/chunks/import-VTODLPLO.mjs +22 -0
  173. package/dist/{browser/module-Q6VFGLMG.mjs → react-native/chunks/module-JBBH6PZH.mjs} +609 -144
  174. package/dist/react-native/chunks/module-VEK2KCKU.mjs +25 -0
  175. package/dist/react-native/chunks/phrase-module-76UAPPSG.mjs +329 -0
  176. package/dist/react-native/chunks/pool-CD4ANYUI.mjs +296 -0
  177. package/dist/react-native/chunks/release-D7ZTIANM.mjs +33 -0
  178. package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
  179. package/dist/{browser/restore-queue-SOAHKZSQ.mjs → react-native/chunks/restore-queue-OI5EH7R3.mjs} +2 -2
  180. package/dist/react-native/chunks/runtime-3MCDLJAF.mjs +559 -0
  181. package/dist/react-native/chunks/snapshot-AUZMEV36.mjs +1205 -0
  182. package/dist/react-native/chunks/write-DDC2QBRJ.mjs +197 -0
  183. package/dist/react-native/partial-words.mjs +733 -0
  184. package/dist/react-native/pattern.mjs +2442 -0
  185. package/dist/react-native/synonyms.mjs +460 -0
  186. package/dist/react.d.ts +7 -7
  187. package/dist/react.mjs +2 -0
  188. package/dist/{rerank-ZZ5CHKLW.mjs → rerank-3W54N6P2.mjs} +4 -4
  189. package/dist/rerankers/http.mjs +2 -2
  190. package/dist/restore-queue-BQWWNNQI.mjs +41 -0
  191. package/dist/{runtime-RAOCKSYA.mjs → runtime-L2DZYY6N.mjs} +5 -5
  192. package/dist/server.d.ts +8 -8
  193. package/dist/server.mjs +32 -28
  194. package/dist/{snapshot-DB5ZVCDN.mjs → snapshot-EHXTOTW5.mjs} +39 -33
  195. package/dist/synonyms.mjs +3 -3
  196. package/dist/{tasks-ChWp0wrF.d.ts → tasks-C4ElXr4P.d.ts} +2 -2
  197. package/dist/thread-I7DVHHW7.mjs +31 -0
  198. package/dist/vector/search-worker.mjs +7 -5
  199. package/dist/workers/entry.mjs +9 -9
  200. package/dist/{write-KZVTLGCI.mjs → write-SDJXXQ2G.mjs} +29 -23
  201. package/package.json +88 -7
  202. package/dist/analysis-change-D5APH4Z3.mjs +0 -34
  203. package/dist/browser/import-FTMLTPGQ.mjs +0 -9
  204. package/dist/direct-executor-FDQYQEJQ.mjs +0 -22
  205. package/dist/import-6KXFY6XI.mjs +0 -35
  206. package/dist/thread-OV6T555H.mjs +0 -28
  207. /package/dist/{chunk-22YPZVMG.mjs → chunk-ZXNQEW43.mjs} +0 -0
@@ -0,0 +1,910 @@
1
+ import { MAX_DOCUMENT_ENCODING_DEPTH, EARLIER_RELEASE_MIN_M, MIN_M, MAX_M, DOCUMENT_ENCODER_RETAINED_BUFFER_BYTES } from './chunk-TJMSKBEM.mjs';
2
+ import { DEFAULT_PATTERN_VALUE_LIMIT, isPlainObject, isWordIndexedBase, MAX_TEXT_FIELDS, MAX_VECTOR_DIMENSION, parseFieldType, MAX_DOCUMENT_BYTES, MAX_NESTING_DEPTH, PROTOTYPE_POLLUTION_KEYS, FIELD_NAME_PATTERN, RESERVED_ROOT_FIELDS, MAX_DOCUMENT_NESTING_DEPTH, VECTOR_PATTERN } from './chunk-DKJTZ625.mjs';
3
+ import { visitTokens, visitSurfaceSpellings } from './chunk-2J2727V4.mjs';
4
+ import { compareCodePoints, holdsUnpairedSurrogate, exceedsCodePoints } from './chunk-PPEPTUVV.mjs';
5
+ import { NarsilError, ErrorCodes } from './chunk-YH7W556M.mjs';
6
+ import { Encoder } from '@msgpack/msgpack';
7
+
8
+ // src/schema/validator/schema.ts
9
+ function validateSchemaFields(schema, depth, prefix) {
10
+ if (depth > MAX_NESTING_DEPTH) {
11
+ throw new NarsilError(
12
+ ErrorCodes.SCHEMA_DEPTH_EXCEEDED,
13
+ `Schema nesting exceeds the maximum depth of ${MAX_NESTING_DEPTH} at "${prefix}"`,
14
+ { path: prefix, maxDepth: MAX_NESTING_DEPTH }
15
+ );
16
+ }
17
+ let canonical = null;
18
+ const replace = (field, value) => {
19
+ if (canonical === null) canonical = { ...schema };
20
+ canonical[field] = value;
21
+ };
22
+ for (const [field, type] of Object.entries(schema)) {
23
+ const path = prefix ? `${prefix}.${field}` : field;
24
+ if (PROTOTYPE_POLLUTION_KEYS.has(field)) {
25
+ throw new NarsilError(ErrorCodes.SCHEMA_INVALID_TYPE, `Field name "${field}" is not allowed in a schema`, {
26
+ field,
27
+ path
28
+ });
29
+ }
30
+ if (!FIELD_NAME_PATTERN.test(field)) {
31
+ throw new NarsilError(
32
+ ErrorCodes.SCHEMA_INVALID_TYPE,
33
+ `Field name "${field}" contains characters that are not allowed; use letters, digits, and underscores only`,
34
+ { field, path }
35
+ );
36
+ }
37
+ if (depth === 1 && RESERVED_ROOT_FIELDS.has(field)) {
38
+ throw new NarsilError(
39
+ ErrorCodes.SCHEMA_INVALID_TYPE,
40
+ `Field "${field}" is reserved and cannot be defined in a schema`,
41
+ { field, path }
42
+ );
43
+ }
44
+ if (isPlainObject(type)) {
45
+ const nested = validateSchemaFields(type, depth + 1, path);
46
+ if (nested !== type) replace(field, nested);
47
+ continue;
48
+ }
49
+ if (typeof type !== "string") {
50
+ throw new NarsilError(
51
+ ErrorCodes.SCHEMA_INVALID_TYPE,
52
+ `Field "${path}" has an invalid type definition: ${String(type)}`,
53
+ {
54
+ field: path,
55
+ type: String(type)
56
+ }
57
+ );
58
+ }
59
+ const parsed = parseFieldType(type, path);
60
+ if (parsed.type !== type) replace(field, parsed.type);
61
+ }
62
+ return canonical ?? schema;
63
+ }
64
+ function validateSchema(schema) {
65
+ if (!isPlainObject(schema)) {
66
+ throw new NarsilError(ErrorCodes.SCHEMA_INVALID_TYPE, "Schema must be a plain object", {
67
+ received: typeof schema
68
+ });
69
+ }
70
+ if (Object.keys(schema).length === 0) {
71
+ throw new NarsilError(ErrorCodes.SCHEMA_INVALID_TYPE, "Schema must define at least one field");
72
+ }
73
+ const validated = validateSchemaFields(schema, 1, "");
74
+ requireTextFieldCount(schemaFieldsOf(validated).filter((field) => isWordIndexedBase(field.base)).length);
75
+ return validated;
76
+ }
77
+ function requireTextFieldCount(count) {
78
+ if (count <= MAX_TEXT_FIELDS) return;
79
+ throw new NarsilError(
80
+ ErrorCodes.SCHEMA_INVALID_TYPE,
81
+ `The schema declares ${count} string and string[] fields, but a schema holds at most ${MAX_TEXT_FIELDS}`,
82
+ { textFields: count, maximum: MAX_TEXT_FIELDS }
83
+ );
84
+ }
85
+ function requireCreatableSchema(schema) {
86
+ for (const field of schemaFieldsOf(schema)) {
87
+ if (field.base !== "vector" || field.dimension <= MAX_VECTOR_DIMENSION) continue;
88
+ throw new NarsilError(
89
+ ErrorCodes.SCHEMA_INVALID_VECTOR_DIMENSION,
90
+ `Vector field "${field.path}" has ${field.dimension} dimensions, but a new index takes at most ${MAX_VECTOR_DIMENSION}`,
91
+ { field: field.path, dimension: field.dimension, maximum: MAX_VECTOR_DIMENSION }
92
+ );
93
+ }
94
+ }
95
+ function requireValidFieldTypes(flatSchema) {
96
+ let textFields = 0;
97
+ for (const [path, type] of Object.entries(flatSchema)) {
98
+ if (typeof type !== "string") {
99
+ throw new NarsilError(
100
+ ErrorCodes.SCHEMA_INVALID_TYPE,
101
+ `Field "${path}" has an invalid type definition: ${String(type)}`,
102
+ { field: path, type: String(type) }
103
+ );
104
+ }
105
+ if (isWordIndexedBase(parseFieldType(type, path).base)) textFields += 1;
106
+ }
107
+ requireTextFieldCount(textFields);
108
+ }
109
+ function flattenRecursive(schema, prefix, result) {
110
+ for (const [field, type] of Object.entries(schema)) {
111
+ const path = prefix ? `${prefix}.${field}` : field;
112
+ if (isPlainObject(type)) {
113
+ flattenRecursive(type, path, result);
114
+ } else {
115
+ result[path] = type;
116
+ }
117
+ }
118
+ }
119
+ var flattenedSchemas = /* @__PURE__ */ new WeakMap();
120
+ function flattenSchema(schema) {
121
+ const cached = flattenedSchemas.get(schema);
122
+ if (cached !== void 0) return cached;
123
+ const result = /* @__PURE__ */ Object.create(null);
124
+ flattenRecursive(schema, "", result);
125
+ flattenedSchemas.set(schema, Object.freeze(result));
126
+ return result;
127
+ }
128
+ var parsedSchemas = /* @__PURE__ */ new WeakMap();
129
+ function schemaFieldsOf(schema) {
130
+ const cached = parsedSchemas.get(schema);
131
+ if (cached !== void 0) return cached;
132
+ const fields = [];
133
+ for (const [path, type] of Object.entries(flattenSchema(schema))) {
134
+ fields.push({ ...parseFieldType(type, path), path });
135
+ }
136
+ const frozen = Object.freeze(fields);
137
+ parsedSchemas.set(schema, frozen);
138
+ return frozen;
139
+ }
140
+ var partialFieldsBySchema = /* @__PURE__ */ new WeakMap();
141
+ function partialFieldsOf(schema) {
142
+ const cached = partialFieldsBySchema.get(schema);
143
+ if (cached !== void 0) return cached;
144
+ const paths = [];
145
+ for (const field of schemaFieldsOf(schema)) {
146
+ if (field.partial) paths.push(field.path);
147
+ }
148
+ const frozen = Object.freeze(paths.sort(compareCodePoints));
149
+ partialFieldsBySchema.set(schema, frozen);
150
+ return frozen;
151
+ }
152
+ function extractVectorFieldsFromSchema(schema) {
153
+ const result = /* @__PURE__ */ new Map();
154
+ for (const field of schemaFieldsOf(schema)) {
155
+ if (field.base === "vector") result.set(field.path, field.dimension);
156
+ }
157
+ return result;
158
+ }
159
+
160
+ // src/geo/constants.ts
161
+ var DEG_TO_RAD = Math.PI / 180;
162
+ var MAX_LATITUDE_DEGREES = 90;
163
+ var MAX_LONGITUDE_DEGREES = 180;
164
+ var MIN_POLYGON_POINTS = 3;
165
+ var ANTIMERIDIAN_SPAN_DEGREES = 180;
166
+ var FULL_TURN_DEGREES = 360;
167
+ var EARTH_RADIUS_METERS = 63710088e-1;
168
+ var LOCATION_CODE_BITS_PER_AXIS = 26;
169
+ var MAX_COVERING_CELLS = 16;
170
+ var RADIUS_BOX_MARGIN_RATIO = 1.02;
171
+ var RADIUS_BOX_MARGIN_RADIANS = 1e-9;
172
+ var VINCENTY_MAX_ITERATIONS = 200;
173
+ var VINCENTY_CONVERGENCE_THRESHOLD = 1e-12;
174
+
175
+ // src/geo/coordinates.ts
176
+ function isLatitude(value) {
177
+ return value >= -MAX_LATITUDE_DEGREES && value <= MAX_LATITUDE_DEGREES;
178
+ }
179
+ function isLongitude(value) {
180
+ return value >= -MAX_LONGITUDE_DEGREES && value <= MAX_LONGITUDE_DEGREES;
181
+ }
182
+
183
+ // src/schema/validator/field-values.ts
184
+ function validateGeopoint(path, value) {
185
+ if (!isPlainObject(value)) {
186
+ throw new NarsilError(
187
+ ErrorCodes.SCHEMA_INVALID_GEOPOINT,
188
+ `Field "${path}" expected a geopoint object with lat and lon`,
189
+ {
190
+ field: path,
191
+ received: Array.isArray(value) ? "array" : typeof value
192
+ }
193
+ );
194
+ }
195
+ if (typeof value.lat !== "number" || typeof value.lon !== "number" || Number.isNaN(value.lat) || Number.isNaN(value.lon)) {
196
+ throw new NarsilError(ErrorCodes.SCHEMA_INVALID_GEOPOINT, `Field "${path}" geopoint requires numeric lat and lon`, {
197
+ field: path,
198
+ lat: Number.isNaN(value.lat) ? "NaN" : typeof value.lat,
199
+ lon: Number.isNaN(value.lon) ? "NaN" : typeof value.lon
200
+ });
201
+ }
202
+ if (!isLatitude(value.lat)) {
203
+ throw new NarsilError(
204
+ ErrorCodes.SCHEMA_INVALID_GEOPOINT,
205
+ `Field "${path}" geopoint lat must be between -90 and 90`,
206
+ {
207
+ field: path,
208
+ lat: value.lat
209
+ }
210
+ );
211
+ }
212
+ if (!isLongitude(value.lon)) {
213
+ throw new NarsilError(
214
+ ErrorCodes.SCHEMA_INVALID_GEOPOINT,
215
+ `Field "${path}" geopoint lon must be between -180 and 180`,
216
+ {
217
+ field: path,
218
+ lon: value.lon
219
+ }
220
+ );
221
+ }
222
+ }
223
+ function validateVector(path, value, expectedDimension) {
224
+ const isArray = Array.isArray(value);
225
+ const isFloat32 = value instanceof Float32Array;
226
+ if (!isArray && !isFloat32) {
227
+ throw new NarsilError(
228
+ ErrorCodes.DOC_VALIDATION_FAILED,
229
+ `Field "${path}" expected a vector (Array or Float32Array)`,
230
+ { field: path, received: typeof value }
231
+ );
232
+ }
233
+ const arr = value;
234
+ if (arr.length !== expectedDimension) {
235
+ throw new NarsilError(
236
+ ErrorCodes.DOC_VALIDATION_FAILED,
237
+ `Field "${path}" expected vector of dimension ${expectedDimension}, got ${arr.length}`,
238
+ { field: path, expected: expectedDimension, received: arr.length }
239
+ );
240
+ }
241
+ for (let i = 0; i < arr.length; i++) {
242
+ if (typeof arr[i] !== "number" || !Number.isFinite(arr[i])) {
243
+ throw new NarsilError(
244
+ ErrorCodes.DOC_VALIDATION_FAILED,
245
+ `Field "${path}" vector element at index ${i} is not a valid number`,
246
+ { field: path, index: i, value: arr[i] }
247
+ );
248
+ }
249
+ }
250
+ }
251
+ function validateTypedArray(path, value, elementType) {
252
+ if (!Array.isArray(value)) {
253
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path}" expected an array`, {
254
+ field: path,
255
+ received: typeof value
256
+ });
257
+ }
258
+ for (let i = 0; i < value.length; i++) {
259
+ const el = value[i];
260
+ if (typeof el !== elementType) {
261
+ throw new NarsilError(
262
+ ErrorCodes.DOC_VALIDATION_FAILED,
263
+ `Field "${path}" array element at index ${i} expected ${elementType}, got ${typeof el}`,
264
+ { field: path, index: i, expected: elementType, received: typeof el }
265
+ );
266
+ }
267
+ if (elementType === "number" && !Number.isFinite(el)) {
268
+ throw new NarsilError(
269
+ ErrorCodes.DOC_VALIDATION_FAILED,
270
+ `Field "${path}" array element at index ${i} is ${String(el)}, while a number field holds finite numbers alone`,
271
+ { field: path, index: i }
272
+ );
273
+ }
274
+ }
275
+ }
276
+ function validatePatternValue(path, value, patternValueLimit, index) {
277
+ if (!exceedsCodePoints(value, patternValueLimit)) return;
278
+ const subject = index === void 0 ? `The value of field "${path}"` : `Element ${index} of field "${path}"`;
279
+ throw new NarsilError(
280
+ ErrorCodes.DOC_VALIDATION_FAILED,
281
+ `${subject} is longer than ${patternValueLimit} code points, which is the pattern value limit of this index`,
282
+ { field: path, limit: patternValueLimit, ...index === void 0 ? {} : { index } }
283
+ );
284
+ }
285
+ function validateFieldValue(path, value, type, patternValueLimit) {
286
+ switch (type.base) {
287
+ case "string":
288
+ case "verbatim":
289
+ if (typeof value !== "string") {
290
+ throw new NarsilError(
291
+ ErrorCodes.DOC_VALIDATION_FAILED,
292
+ `Field "${path}" expected string, got ${typeof value}`,
293
+ {
294
+ field: path,
295
+ expected: "string",
296
+ received: typeof value
297
+ }
298
+ );
299
+ }
300
+ if (type.pattern) validatePatternValue(path, value, patternValueLimit);
301
+ return;
302
+ case "number":
303
+ if (typeof value !== "number" || !Number.isFinite(value)) {
304
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path}" expected a finite number`, {
305
+ field: path,
306
+ expected: "number",
307
+ received: typeof value === "number" ? String(value) : typeof value
308
+ });
309
+ }
310
+ return;
311
+ case "boolean":
312
+ if (typeof value !== "boolean") {
313
+ throw new NarsilError(
314
+ ErrorCodes.DOC_VALIDATION_FAILED,
315
+ `Field "${path}" expected boolean, got ${typeof value}`,
316
+ {
317
+ field: path,
318
+ expected: "boolean",
319
+ received: typeof value
320
+ }
321
+ );
322
+ }
323
+ return;
324
+ case "enum":
325
+ if (typeof value !== "string") {
326
+ throw new NarsilError(
327
+ ErrorCodes.DOC_VALIDATION_FAILED,
328
+ `Field "${path}" expected enum (string), got ${typeof value}`,
329
+ {
330
+ field: path,
331
+ expected: "enum",
332
+ received: typeof value
333
+ }
334
+ );
335
+ }
336
+ return;
337
+ case "geopoint":
338
+ validateGeopoint(path, value);
339
+ return;
340
+ case "string[]":
341
+ case "verbatim[]":
342
+ validateTypedArray(path, value, "string");
343
+ if (type.pattern) {
344
+ const elements = value;
345
+ for (let i = 0; i < elements.length; i++) validatePatternValue(path, elements[i], patternValueLimit, i);
346
+ }
347
+ return;
348
+ case "number[]":
349
+ validateTypedArray(path, value, "number");
350
+ return;
351
+ case "boolean[]":
352
+ validateTypedArray(path, value, "boolean");
353
+ return;
354
+ case "enum[]":
355
+ validateTypedArray(path, value, "string");
356
+ return;
357
+ case "vector":
358
+ validateVector(path, value, type.dimension);
359
+ return;
360
+ }
361
+ }
362
+
363
+ // src/schema/validator/storable.ts
364
+ var RESERVED_OBJECT_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
365
+ var UNPAIRED_SURROGATE_REASON = "holds an unpaired surrogate, the half of a character such as an emoji whose other half is missing, so the engine cannot store it as UTF-8";
366
+ function reject(path, reason) {
367
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path === "" ? "(document)" : path}" ${reason}`, {
368
+ field: path
369
+ });
370
+ }
371
+ function isPlainObject2(value) {
372
+ const proto = Object.getPrototypeOf(value);
373
+ return proto === Object.prototype || proto === null;
374
+ }
375
+ function walkStorable(value, path, depth, ancestors, skip) {
376
+ if (value === null || value === void 0) return;
377
+ const kind = typeof value;
378
+ if (kind === "string") {
379
+ if (holdsUnpairedSurrogate(value)) reject(path, UNPAIRED_SURROGATE_REASON);
380
+ return;
381
+ }
382
+ if (kind === "boolean" || kind === "number") return;
383
+ if (kind === "bigint" || kind === "symbol" || kind === "function") {
384
+ reject(path, `holds a ${kind}, which cannot be stored or recovered`);
385
+ }
386
+ const container = value;
387
+ if (container instanceof Date) {
388
+ if (Number.isNaN(container.getTime())) reject(path, "holds an invalid date, which the engine cannot store");
389
+ return;
390
+ }
391
+ if (container instanceof Uint8Array) return;
392
+ const isArray = Array.isArray(container);
393
+ if (!isArray && !isPlainObject2(container)) {
394
+ reject(
395
+ path,
396
+ "holds a value that does not survive storage; use plain objects, arrays, strings, numbers, booleans, dates, or Uint8Array bytes"
397
+ );
398
+ }
399
+ if (depth >= MAX_DOCUMENT_NESTING_DEPTH) {
400
+ reject(path, `nests deeper than ${MAX_DOCUMENT_NESTING_DEPTH} levels`);
401
+ }
402
+ if (ancestors.has(container)) {
403
+ reject(path, "refers back to one of its own ancestors");
404
+ }
405
+ ancestors.add(container);
406
+ if (isArray) {
407
+ const items = container;
408
+ for (let i = 0; i < items.length; i++) {
409
+ walkStorable(items[i], `${path}[${i}]`, depth + 1, ancestors, skip);
410
+ }
411
+ } else {
412
+ const record = container;
413
+ for (const key of Object.keys(record)) {
414
+ const keyPath = path === "" ? key : `${path}.${key}`;
415
+ if (RESERVED_OBJECT_KEYS.has(key)) {
416
+ reject(keyPath, "uses a reserved object key");
417
+ }
418
+ if (holdsUnpairedSurrogate(key)) reject(keyPath, `has a name that ${UNPAIRED_SURROGATE_REASON}`);
419
+ if (skip.has(keyPath)) continue;
420
+ walkStorable(record[key], keyPath, depth + 1, ancestors, skip);
421
+ }
422
+ }
423
+ ancestors.delete(container);
424
+ }
425
+ function assertStorableDocument(document, skipFieldPaths) {
426
+ walkStorable(document, "", 0, /* @__PURE__ */ new Set(), skipFieldPaths ?? /* @__PURE__ */ new Set());
427
+ }
428
+
429
+ // src/schema/validator/document.ts
430
+ var vectorPathCache = /* @__PURE__ */ new WeakMap();
431
+ function vectorFieldPaths(schema) {
432
+ let paths = vectorPathCache.get(schema);
433
+ if (paths !== void 0) return paths;
434
+ paths = /* @__PURE__ */ new Set();
435
+ const collect = (level, prefix) => {
436
+ for (const [field, type] of Object.entries(level)) {
437
+ const path = prefix === "" ? field : `${prefix}.${field}`;
438
+ if (typeof type === "string" && VECTOR_PATTERN.test(type)) {
439
+ paths?.add(path);
440
+ } else if (isPlainObject(type)) {
441
+ collect(type, path);
442
+ }
443
+ }
444
+ };
445
+ collect(schema, "");
446
+ vectorPathCache.set(schema, paths);
447
+ return paths;
448
+ }
449
+ var parsedLevelCache = /* @__PURE__ */ new WeakMap();
450
+ function parsedLevel(schema, prefix) {
451
+ let entries = parsedLevelCache.get(schema);
452
+ if (entries !== void 0) return entries;
453
+ entries = Object.entries(schema).map(([field, type]) => {
454
+ if (isPlainObject(type)) return { field, nested: type, type: null };
455
+ const path = prefix ? `${prefix}.${field}` : field;
456
+ return { field, nested: null, type: parseFieldType(type, path) };
457
+ });
458
+ parsedLevelCache.set(schema, entries);
459
+ return entries;
460
+ }
461
+ function validateDocumentFields(doc, schema, prefix, patternValueLimit) {
462
+ for (const { field, nested, type } of parsedLevel(schema, prefix)) {
463
+ const value = doc[field];
464
+ if (value === void 0 || value === null) continue;
465
+ const path = prefix ? `${prefix}.${field}` : field;
466
+ if (nested !== null) {
467
+ if (!isPlainObject(value)) {
468
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path}" expected an object`, {
469
+ field: path,
470
+ received: Array.isArray(value) ? "array" : typeof value
471
+ });
472
+ }
473
+ validateDocumentFields(value, nested, path, patternValueLimit);
474
+ continue;
475
+ }
476
+ if (type !== null) validateFieldValue(path, value, type, patternValueLimit);
477
+ }
478
+ }
479
+ function validateDocument(document, schema, patternValueLimit = DEFAULT_PATTERN_VALUE_LIMIT) {
480
+ if (!isPlainObject(document)) {
481
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, "Document must be a plain object", {
482
+ received: typeof document
483
+ });
484
+ }
485
+ requireIdFieldKey(document);
486
+ assertStorableDocument(document, vectorFieldPaths(schema));
487
+ validateDocumentFields(document, schema, "", patternValueLimit);
488
+ }
489
+ function requireIdFieldKey(document) {
490
+ if (!Object.hasOwn(document, "id")) return;
491
+ const id = document.id;
492
+ if (typeof id === "string" && id.length > 0) return;
493
+ throw new NarsilError(
494
+ ErrorCodes.DOC_VALIDATION_FAILED,
495
+ `Field "id" holds ${typeof id === "string" ? "an empty string" : id === null ? "null" : `a ${typeof id}`}, while the engine stores a document under its id field, so that field must hold a non-empty string`,
496
+ { field: "id", received: id === null ? "null" : typeof id }
497
+ );
498
+ }
499
+ function collectExtraFields(doc, schema, prefix, extras) {
500
+ for (const key of Object.keys(doc)) {
501
+ if (key === "id") continue;
502
+ const path = prefix ? `${prefix}.${key}` : key;
503
+ if (!Object.hasOwn(schema, key)) {
504
+ extras.push(path);
505
+ continue;
506
+ }
507
+ const schemaType = schema[key];
508
+ if (isPlainObject(schemaType) && isPlainObject(doc[key])) {
509
+ collectExtraFields(doc[key], schemaType, path, extras);
510
+ }
511
+ }
512
+ }
513
+ function validateDocumentStrict(document, schema) {
514
+ const extras = [];
515
+ collectExtraFields(document, schema, "", extras);
516
+ if (extras.length > 0) {
517
+ throw new NarsilError(
518
+ ErrorCodes.DOC_VALIDATION_FAILED,
519
+ `Document contains fields not defined in schema: ${extras.join(", ")}`,
520
+ { extraFields: extras }
521
+ );
522
+ }
523
+ }
524
+ function resolveNestedValue(document, path) {
525
+ if (!path.includes(".")) return document[path];
526
+ const segments = path.split(".");
527
+ let current = document;
528
+ for (const segment of segments) {
529
+ if (current === null || current === void 0 || typeof current !== "object") return void 0;
530
+ current = current[segment];
531
+ }
532
+ return current;
533
+ }
534
+ function validateRequiredFields(document, required) {
535
+ if (required.length === 0) return;
536
+ for (const field of required) {
537
+ const value = resolveNestedValue(document, field);
538
+ if (value === void 0 || value === null) {
539
+ throw new NarsilError(ErrorCodes.DOC_MISSING_REQUIRED_FIELD, `Document is missing required field "${field}"`, {
540
+ field
541
+ });
542
+ }
543
+ }
544
+ }
545
+ var INTEGER_ENCODING = { maxDepth: MAX_DOCUMENT_ENCODING_DEPTH };
546
+ var FLOAT_ENCODING = { maxDepth: MAX_DOCUMENT_ENCODING_DEPTH, forceIntegerToFloat: true };
547
+ var integerEncoder = new Encoder(INTEGER_ENCODING);
548
+ var floatEncoder = new Encoder(FLOAT_ENCODING);
549
+ function encodeExactly(document, forceIntegerToFloat) {
550
+ const bytes = (forceIntegerToFloat ? floatEncoder : integerEncoder).encode(document);
551
+ if (bytes.byteLength > DOCUMENT_ENCODER_RETAINED_BUFFER_BYTES) {
552
+ if (forceIntegerToFloat) floatEncoder = new Encoder(FLOAT_ENCODING);
553
+ else integerEncoder = new Encoder(INTEGER_ENCODING);
554
+ }
555
+ return bytes;
556
+ }
557
+ function holdsNegativeZero(value, depth) {
558
+ if (typeof value === "number") return Object.is(value, -0);
559
+ if (value === null || typeof value !== "object" || ArrayBuffer.isView(value) || depth > MAX_DOCUMENT_ENCODING_DEPTH) {
560
+ return false;
561
+ }
562
+ for (const child of Array.isArray(value) ? value : Object.values(value)) {
563
+ if (holdsNegativeZero(child, depth + 1)) return true;
564
+ }
565
+ return false;
566
+ }
567
+ function encodeDocument(document) {
568
+ try {
569
+ return encodeExactly(document, holdsNegativeZero(document, 1));
570
+ } catch (error) {
571
+ throw new NarsilError(
572
+ ErrorCodes.DOC_VALIDATION_FAILED,
573
+ `The document cannot be encoded as MessagePack: ${error instanceof Error ? error.message : String(error)}`
574
+ );
575
+ }
576
+ }
577
+
578
+ // src/schema/validator/document-size.ts
579
+ var LARGEST_HEADER_BYTES = 5;
580
+ var LARGEST_NUMBER_BYTES = 9;
581
+ var LARGEST_TIMESTAMP_BYTES = 15;
582
+ var LARGEST_UTF8_BYTES_PER_UNIT = 3;
583
+ function upperBoundBytes(value, budget, depth) {
584
+ if (value === null || value === void 0 || typeof value === "boolean") return 1;
585
+ if (typeof value === "number") return LARGEST_NUMBER_BYTES;
586
+ if (typeof value === "string") return LARGEST_HEADER_BYTES + value.length * LARGEST_UTF8_BYTES_PER_UNIT;
587
+ if (value instanceof Date) return LARGEST_TIMESTAMP_BYTES;
588
+ if (ArrayBuffer.isView(value)) return LARGEST_HEADER_BYTES + value.byteLength;
589
+ if (typeof value !== "object" || depth > MAX_DOCUMENT_NESTING_DEPTH) return LARGEST_NUMBER_BYTES;
590
+ let total = LARGEST_HEADER_BYTES;
591
+ if (Array.isArray(value)) {
592
+ for (const item of value) {
593
+ total += upperBoundBytes(item, budget - total, depth + 1);
594
+ if (total > budget) return total;
595
+ }
596
+ return total;
597
+ }
598
+ const record = value;
599
+ for (const key of Object.keys(record)) {
600
+ total += LARGEST_HEADER_BYTES + key.length * LARGEST_UTF8_BYTES_PER_UNIT;
601
+ total += upperBoundBytes(record[key], budget - total, depth + 1);
602
+ if (total > budget) return total;
603
+ }
604
+ return total;
605
+ }
606
+ function messagePackUpperBoundBytes(value) {
607
+ return upperBoundBytes(value, Number.POSITIVE_INFINITY, 0);
608
+ }
609
+ function encodedBytesOf(document) {
610
+ try {
611
+ return encodeDocument(document).byteLength;
612
+ } catch {
613
+ return null;
614
+ }
615
+ }
616
+ function requireDocumentWithinSizeLimit(document) {
617
+ if (upperBoundBytes(document, MAX_DOCUMENT_BYTES, 0) <= MAX_DOCUMENT_BYTES) return;
618
+ const encodedBytes = encodedBytesOf(document);
619
+ if (encodedBytes === null || encodedBytes <= MAX_DOCUMENT_BYTES) return;
620
+ throw new NarsilError(
621
+ ErrorCodes.DOC_VALIDATION_FAILED,
622
+ `The document takes ${encodedBytes} bytes as MessagePack, vector values included, but a document may take at most ${MAX_DOCUMENT_BYTES}`,
623
+ { bytes: encodedBytes, maximum: MAX_DOCUMENT_BYTES }
624
+ );
625
+ }
626
+
627
+ // src/schema/validator/vector-promotion.ts
628
+ var QUANTIZATION_MODES = /* @__PURE__ */ new Set(["osq8", "osq4", "osq2", "osq1", "none"]);
629
+ var STORAGE_MODES = /* @__PURE__ */ new Set(["memory", "disk"]);
630
+ function fail(message, details) {
631
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, message, details);
632
+ }
633
+ function isPositiveInteger(value) {
634
+ return Number.isInteger(value) && value >= 1;
635
+ }
636
+ function isQuantizationMode(value) {
637
+ return typeof value === "string" && QUANTIZATION_MODES.has(value);
638
+ }
639
+ function isStorageMode(value) {
640
+ return typeof value === "string" && STORAGE_MODES.has(value);
641
+ }
642
+ function validateVectorStorage(config, filesystemDurability) {
643
+ if (config?.storage === "disk" && !filesystemDurability) {
644
+ fail("vectorPromotion.storage 'disk' needs filesystem durability, because a checkpoint file holds the vectors", {
645
+ storage: config.storage
646
+ });
647
+ }
648
+ }
649
+ function validateTextStorage(textStorage, filesystemDurability) {
650
+ if (textStorage === void 0 || textStorage === "memory" || textStorage === "disk" && filesystemDurability) return;
651
+ fail("textStorage must be 'memory', or 'disk' with filesystem durability", { textStorage });
652
+ }
653
+ function raiseEarlierReleaseGraphSetting(config) {
654
+ if (typeof config !== "object" || config === null) return config;
655
+ const hnsw = config.hnswConfig;
656
+ if (typeof hnsw !== "object" || hnsw === null) return config;
657
+ if (hnsw.m !== EARLIER_RELEASE_MIN_M) return config;
658
+ return { ...config, hnswConfig: { ...hnsw, m: MIN_M } };
659
+ }
660
+ function validateVectorPromotion(config) {
661
+ if (config === void 0) return;
662
+ if (config.threshold !== void 0 && !isPositiveInteger(config.threshold)) {
663
+ fail("vectorPromotion.threshold must be a positive integer", { threshold: config.threshold });
664
+ }
665
+ const quantization = config.quantization;
666
+ if (quantization !== void 0 && !isQuantizationMode(quantization)) {
667
+ fail("vectorPromotion.quantization must be 'osq8', 'osq4', 'osq2', 'osq1', or 'none'", { quantization });
668
+ }
669
+ const storage = config.storage;
670
+ if (storage !== void 0 && !isStorageMode(storage)) {
671
+ fail("vectorPromotion.storage must be 'memory' or 'disk'", { storage });
672
+ }
673
+ const hnsw = config.hnswConfig;
674
+ if (hnsw === void 0) return;
675
+ if (hnsw.m !== void 0 && (!Number.isInteger(hnsw.m) || hnsw.m < MIN_M || hnsw.m > MAX_M)) {
676
+ fail(`vectorPromotion.hnswConfig.m must be a whole number from ${MIN_M} to ${MAX_M}`, { m: hnsw.m });
677
+ }
678
+ if (hnsw.efConstruction !== void 0 && !isPositiveInteger(hnsw.efConstruction)) {
679
+ fail("vectorPromotion.hnswConfig.efConstruction must be a positive integer", {
680
+ efConstruction: hnsw.efConstruction
681
+ });
682
+ }
683
+ const metric = hnsw.metric;
684
+ if (metric !== void 0 && metric !== "cosine" && metric !== "dotProduct" && metric !== "euclidean") {
685
+ fail("vectorPromotion.hnswConfig.metric must be 'cosine', 'dotProduct', or 'euclidean'", { metric });
686
+ }
687
+ }
688
+
689
+ // src/core/partition/utils.ts
690
+ function getOrCreateFieldNameIndex(table, fieldName) {
691
+ const existing = table.indexMap.get(fieldName);
692
+ if (existing !== void 0) return existing;
693
+ const idx = table.names.length;
694
+ table.names.push(fieldName);
695
+ table.indexMap.set(fieldName, idx);
696
+ return idx;
697
+ }
698
+ var VECTOR_PATTERN2 = /^vector\[(\d+)]$/;
699
+ function getNestedValue(obj, path) {
700
+ const parts = path.split(".");
701
+ let current = obj;
702
+ for (let i = 0; i < parts.length; i++) {
703
+ if (current === null || current === void 0 || typeof current !== "object") return void 0;
704
+ if (!Object.hasOwn(current, parts[i])) return void 0;
705
+ current = current[parts[i]];
706
+ }
707
+ return current;
708
+ }
709
+ function stringArraysEqual(a, b) {
710
+ if (a === b) return true;
711
+ if (!a || !b) return false;
712
+ if (a.length !== b.length) return false;
713
+ for (let i = 0; i < a.length; i++) {
714
+ if (a[i] !== b[i]) return false;
715
+ }
716
+ return true;
717
+ }
718
+ function textFieldsChanged(oldDoc, newDoc, fields) {
719
+ for (const { path, base } of fields) {
720
+ if (!isWordIndexedBase(base)) continue;
721
+ const oldVal = getNestedValue(oldDoc, path);
722
+ const newVal = getNestedValue(newDoc, path);
723
+ if (base === "string") {
724
+ if (oldVal !== newVal) return true;
725
+ } else {
726
+ if (!stringArraysEqual(oldVal, newVal)) return true;
727
+ }
728
+ }
729
+ return false;
730
+ }
731
+ function parseVectorDimension(fieldType) {
732
+ const match = VECTOR_PATTERN2.exec(fieldType);
733
+ return match ? Number.parseInt(match[1], 10) : null;
734
+ }
735
+ function tokenizeOptions(options) {
736
+ return {
737
+ stem: true,
738
+ removeStopWords: true,
739
+ collectSurfaces: options?.collectSurfaces === true,
740
+ stopWordOverride: options?.stopWordOverride,
741
+ customTokenizer: options?.customTokenizer
742
+ };
743
+ }
744
+ function getFlatSchema(state, schema) {
745
+ if (state.lastSchemaRef === schema && state.flatSchemaCache) return state.flatSchemaCache;
746
+ state.flatSchemaCache = flattenSchema(schema);
747
+ state.lastSchemaRef = schema;
748
+ return state.flatSchemaCache;
749
+ }
750
+
751
+ // src/core/partition/partial-words/constants.ts
752
+ var PARTIAL_WORD_RUN_CODE_POINTS = 3;
753
+ var GROWING_PARTIAL_ENTRY_BYTES = 104;
754
+ var GROWING_PARTIAL_PENDING_ENTRY_BYTES = 8;
755
+ var GROWING_PARTIAL_PENDING_ENTRIES_PER_ENTRY = 2;
756
+ var GROWING_PARTIAL_RUN_BYTES = 120;
757
+ var GROWING_PARTIAL_RUN_MEMBER_BYTES = 24;
758
+ var FROZEN_PARTIAL_REMOVED_ENTRY_BYTES = 48;
759
+
760
+ // src/core/partition/partial-words/spellings.ts
761
+ var LAST_BASIC_PLANE_CODE_POINT = 65535;
762
+ function codePointCountOf(text) {
763
+ let count = 0;
764
+ for (let index = 0; index < text.length; count++) {
765
+ index += (text.codePointAt(index) ?? 0) > LAST_BASIC_PLANE_CODE_POINT ? 2 : 1;
766
+ }
767
+ return count;
768
+ }
769
+ function holdsRun(spelling) {
770
+ if (spelling.length > 2 * PARTIAL_WORD_RUN_CODE_POINTS) return true;
771
+ if (spelling.length <= PARTIAL_WORD_RUN_CODE_POINTS) return false;
772
+ return codePointCountOf(spelling) > PARTIAL_WORD_RUN_CODE_POINTS;
773
+ }
774
+ function countSpelling(counts, token, spelling, change) {
775
+ if (!holdsRun(spelling)) return;
776
+ let byToken = counts.get(token);
777
+ if (byToken === void 0) {
778
+ if (change <= 0) return;
779
+ byToken = /* @__PURE__ */ new Map();
780
+ counts.set(token, byToken);
781
+ }
782
+ const next = (byToken.get(spelling) ?? 0) + change;
783
+ if (next > 0) {
784
+ byToken.set(spelling, next);
785
+ return;
786
+ }
787
+ byToken.delete(spelling);
788
+ if (byToken.size === 0) counts.delete(token);
789
+ }
790
+ function textsOf(value) {
791
+ if (typeof value === "string") return [value];
792
+ if (!Array.isArray(value)) return [];
793
+ return value.filter((element) => typeof element === "string");
794
+ }
795
+ function visitFieldTexts(document, fields, visit) {
796
+ for (const field of fields) {
797
+ for (const text of textsOf(getNestedValue(document, field))) visit(text);
798
+ }
799
+ }
800
+ function visitDocumentSpellings(document, analysis, visit) {
801
+ const tokenizing = { ...tokenizeOptions(analysis.options), collectSurfaces: true };
802
+ const visitOccurrence = (token, _position, surface) => {
803
+ visit(token, surface ?? token);
804
+ };
805
+ visitFieldTexts(document, analysis.fields, (text) => visitTokens(text, analysis.language, tokenizing, visitOccurrence));
806
+ }
807
+ function visitDocumentSurfaceSpellings(document, analysis, visit) {
808
+ if (analysis.options?.customTokenizer !== void 0) return;
809
+ visitFieldTexts(document, analysis.fields, (text) => visitSurfaceSpellings(text, analysis.language, visit));
810
+ }
811
+ function countDocumentSpellings(counts, document, analysis, direction) {
812
+ visitDocumentSpellings(document, analysis, (token, spelling) => countSpelling(counts, token, spelling, direction));
813
+ }
814
+ function spellingCountsOf(documents, analysis) {
815
+ const counts = /* @__PURE__ */ new Map();
816
+ for (const document of documents) countDocumentSpellings(counts, document, analysis, 1);
817
+ return counts;
818
+ }
819
+ function encodePartialSpellings(fields, counts) {
820
+ const encoded = { fields: [...fields], tokens: [], spellings: [], counts: [] };
821
+ for (const [token, byToken] of counts) {
822
+ for (const [spelling, count] of byToken) {
823
+ encoded.tokens.push(token);
824
+ encoded.spellings.push(spelling);
825
+ encoded.counts.push(count);
826
+ }
827
+ }
828
+ return encoded;
829
+ }
830
+ function sameFields(left, right) {
831
+ if (left.length !== right.length) return false;
832
+ for (let at = 0; at < left.length; at++) {
833
+ if (left[at] !== right[at]) return false;
834
+ }
835
+ return true;
836
+ }
837
+
838
+ // src/core/partition/suggestions.ts
839
+ function verbatimOccurrences(state, token) {
840
+ const postingList = state.invertedIdx.lookup(token);
841
+ if (!postingList) return 0;
842
+ const derived = postingList.totalTermFrequency - state.surfaceRegistry.stemChangedTotalFor(token);
843
+ return derived > 0 ? derived : 0;
844
+ }
845
+ function suggestDisplayTerms(state, surfacePrefix, stemmedPrefix, limit) {
846
+ if (limit <= 0 || surfacePrefix.length === 0) return [];
847
+ const groups = /* @__PURE__ */ new Map();
848
+ for (const candidate of state.surfaceRegistry.candidatesForPrefix(surfacePrefix)) {
849
+ const postingList = state.invertedIdx.lookup(candidate.token);
850
+ if (!postingList || postingList.docIdSet.size === 0) continue;
851
+ let group = groups.get(candidate.token);
852
+ if (!group) {
853
+ group = { documentFrequency: postingList.docIdSet.size, surfaces: [] };
854
+ groups.set(candidate.token, group);
855
+ }
856
+ group.surfaces.push({ surface: candidate.surface, occurrences: candidate.occurrences });
857
+ }
858
+ const termPrefixes = state.surfaceRegistry.size() === 0 && stemmedPrefix !== surfacePrefix && stemmedPrefix.length > 0 ? [surfacePrefix, stemmedPrefix] : [surfacePrefix];
859
+ for (const prefix of termPrefixes) {
860
+ for (const suggestion of state.invertedIdx.prefixSearch(prefix, Number.MAX_SAFE_INTEGER)) {
861
+ const occurrences = verbatimOccurrences(state, suggestion.term);
862
+ if (occurrences === 0) continue;
863
+ let group = groups.get(suggestion.term);
864
+ if (!group) {
865
+ group = { documentFrequency: suggestion.documentFrequency, surfaces: [] };
866
+ groups.set(suggestion.term, group);
867
+ } else if (group.surfaces.some((s) => s.surface === suggestion.term)) {
868
+ continue;
869
+ }
870
+ group.surfaces.push({ surface: suggestion.term, occurrences });
871
+ }
872
+ }
873
+ const results = [];
874
+ for (const [token, group] of groups) {
875
+ results.push({ token, documentFrequency: group.documentFrequency, surfaces: group.surfaces });
876
+ }
877
+ results.sort((a, b) => b.documentFrequency - a.documentFrequency || compareCodePoints(a.token, b.token));
878
+ if (results.length > limit) results.length = limit;
879
+ return results;
880
+ }
881
+ function partitionDocumentFrequency(subs, token) {
882
+ let documents = 0;
883
+ for (const sub of subs) documents += sub.invertedIdx.documentFrequency(token);
884
+ return documents;
885
+ }
886
+ function expandTermPrefix(subs, surfacePrefix, stemmedToken, maxExpansions) {
887
+ if (maxExpansions <= 0 || surfacePrefix.length === 0) return [];
888
+ const candidates = /* @__PURE__ */ new Set();
889
+ const holdsSurfaceForms = subs.some((sub) => sub.surfaceRegistry.size() > 0);
890
+ const termPrefixes = !holdsSurfaceForms && stemmedToken !== surfacePrefix && stemmedToken.length > 0 ? [surfacePrefix, stemmedToken] : [surfacePrefix];
891
+ for (const sub of subs) {
892
+ for (const candidate of sub.surfaceRegistry.candidatesForPrefix(surfacePrefix)) candidates.add(candidate.token);
893
+ for (const prefix of termPrefixes) {
894
+ for (const suggestion of sub.invertedIdx.prefixSearch(prefix, Number.MAX_SAFE_INTEGER)) {
895
+ if (candidates.has(suggestion.term)) continue;
896
+ if (verbatimOccurrences(sub, suggestion.term) > 0) candidates.add(suggestion.term);
897
+ }
898
+ }
899
+ }
900
+ const ranked = [];
901
+ for (const token of candidates) {
902
+ const documents = partitionDocumentFrequency(subs, token);
903
+ if (documents > 0) ranked.push({ token, documents });
904
+ }
905
+ ranked.sort((a, b) => b.documents - a.documents || compareCodePoints(a.token, b.token));
906
+ if (ranked.length > maxExpansions) ranked.length = maxExpansions;
907
+ return ranked.map((entry) => entry.token);
908
+ }
909
+
910
+ export { ANTIMERIDIAN_SPAN_DEGREES, DEG_TO_RAD, EARTH_RADIUS_METERS, FROZEN_PARTIAL_REMOVED_ENTRY_BYTES, FULL_TURN_DEGREES, GROWING_PARTIAL_ENTRY_BYTES, GROWING_PARTIAL_PENDING_ENTRIES_PER_ENTRY, GROWING_PARTIAL_PENDING_ENTRY_BYTES, GROWING_PARTIAL_RUN_BYTES, GROWING_PARTIAL_RUN_MEMBER_BYTES, LOCATION_CODE_BITS_PER_AXIS, MAX_COVERING_CELLS, MAX_LATITUDE_DEGREES, MAX_LONGITUDE_DEGREES, MIN_POLYGON_POINTS, PARTIAL_WORD_RUN_CODE_POINTS, RADIUS_BOX_MARGIN_RADIANS, RADIUS_BOX_MARGIN_RATIO, VINCENTY_CONVERGENCE_THRESHOLD, VINCENTY_MAX_ITERATIONS, codePointCountOf, countDocumentSpellings, countSpelling, encodeDocument, encodePartialSpellings, expandTermPrefix, extractVectorFieldsFromSchema, flattenSchema, getFlatSchema, getNestedValue, getOrCreateFieldNameIndex, isLatitude, isLongitude, isQuantizationMode, isStorageMode, messagePackUpperBoundBytes, parseVectorDimension, partialFieldsOf, partitionDocumentFrequency, raiseEarlierReleaseGraphSetting, requireCreatableSchema, requireDocumentWithinSizeLimit, requireValidFieldTypes, sameFields, schemaFieldsOf, spellingCountsOf, suggestDisplayTerms, textFieldsChanged, tokenizeOptions, validateDocument, validateDocumentStrict, validateRequiredFields, validateSchema, validateTextStorage, validateVectorPromotion, validateVectorStorage, visitDocumentSpellings, visitDocumentSurfaceSpellings };