@mailwoman/core 7.2.0 → 7.2.1

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 (129) hide show
  1. package/api/APIClient.ts +175 -0
  2. package/api/headless.ts +23 -0
  3. package/api/index.ts +11 -0
  4. package/api/responses.ts +135 -0
  5. package/coarse-placer/coarse-placer.ts +300 -0
  6. package/coarse-placer/featurize.ts +171 -0
  7. package/coarse-placer/tools/build-dataset.ts +223 -0
  8. package/coarse-placer/tools/build-outlier-exposure.ts +172 -0
  9. package/coarse-placer/tools/build-outlier-latin.ts +200 -0
  10. package/coarse-placer/tools/build-outlier-oa.ts +231 -0
  11. package/coarse-placer/tools/eval-latin-offmap.ts +140 -0
  12. package/coarse-placer/tools/eval-openset.ts +519 -0
  13. package/coarse-placer/tools/eval-quant-compare.ts +151 -0
  14. package/coarse-placer/tools/eval.ts +187 -0
  15. package/coarse-placer/tools/fnv-hash.ts +24 -0
  16. package/coarse-placer/tools/index.ts +23 -0
  17. package/coarse-placer/tools/probe-frontier.ts +216 -0
  18. package/coarse-placer/tools/quantize.ts +116 -0
  19. package/coarse-placer/tools/train.ts +244 -0
  20. package/collections.ts +134 -0
  21. package/db/schema.ts +21 -0
  22. package/decoder/build-tree.ts +228 -0
  23. package/decoder/calibration.ts +101 -0
  24. package/decoder/containment.ts +87 -0
  25. package/decoder/index.ts +17 -0
  26. package/decoder/proposals-to-tree.ts +83 -0
  27. package/decoder/resolve-proposal-overlaps.ts +79 -0
  28. package/decoder/serialize-json.ts +70 -0
  29. package/decoder/serialize-tuples.ts +61 -0
  30. package/decoder/serialize-xml.ts +209 -0
  31. package/decoder/types.ts +159 -0
  32. package/decoder/unknown-spans.ts +103 -0
  33. package/decoder/validate-tree.ts +100 -0
  34. package/env/index.ts +42 -0
  35. package/env/load.ts +14 -0
  36. package/env/paths.ts +40 -0
  37. package/env/schema.ts +125 -0
  38. package/errors/index.ts +9 -0
  39. package/errors/schema.ts +155 -0
  40. package/fs/node.ts +38 -0
  41. package/identifiers.ts +58 -0
  42. package/index.ts +13 -0
  43. package/kysley/adapter.ts +29 -0
  44. package/kysley/client.ts +30 -0
  45. package/kysley/dialect-config.ts +30 -0
  46. package/kysley/dialect.ts +67 -0
  47. package/kysley/driver.ts +175 -0
  48. package/layers/index.ts +12 -0
  49. package/layers/manifest.ts +154 -0
  50. package/layers/schema.ts +105 -0
  51. package/lifecycle/ServiceSymbol.ts +95 -0
  52. package/lifecycle/index.ts +11 -0
  53. package/lifecycle/lru-cache.ts +44 -0
  54. package/lifecycle/services.ts +322 -0
  55. package/locale/index.ts +9 -0
  56. package/locale/locale.ts +49 -0
  57. package/locale/profiles/en-us.ts +69 -0
  58. package/locale/profiles/fr-fr.ts +75 -0
  59. package/locale/profiles/index.ts +9 -0
  60. package/locale/profiles/ja-jp.ts +33 -0
  61. package/locale/registry.ts +68 -0
  62. package/logging/index.ts +8 -0
  63. package/logging/shared.ts +145 -0
  64. package/logging/utils.ts +46 -0
  65. package/objects.ts +211 -0
  66. package/package.json +195 -104
  67. package/pipeline/index.ts +46 -0
  68. package/pipeline/reconcile-lookups.ts +108 -0
  69. package/pipeline/reconcile.ts +542 -0
  70. package/pipeline/runtime-pipeline.ts +732 -0
  71. package/pipeline/span-logit-aggregation.ts +151 -0
  72. package/pipeline/span-proposer.ts +587 -0
  73. package/pipeline/types.ts +348 -0
  74. package/policy/defaults.ts +35 -0
  75. package/policy/index.ts +10 -0
  76. package/policy/policy.ts +60 -0
  77. package/policy/proposal-collection.ts +64 -0
  78. package/policy/registry.ts +149 -0
  79. package/resolver/index.ts +29 -0
  80. package/resolver/types.ts +584 -0
  81. package/resources/LocaleIndex.ts +124 -0
  82. package/resources/ResourceMapCache.ts +126 -0
  83. package/resources/collections.ts +93 -0
  84. package/resources/debugging.ts +19 -0
  85. package/resources/git.ts +54 -0
  86. package/resources/index.ts +17 -0
  87. package/resources/languages/index.ts +49 -0
  88. package/resources/languages/types.gen.ts +1368 -0
  89. package/resources/libaddressinput.ts +105 -0
  90. package/resources/libpostal.ts +182 -0
  91. package/resources/locale.ts +23 -0
  92. package/resources/set.ts +179 -0
  93. package/resources/whosonfirst/DataSourceCache.ts +72 -0
  94. package/resources/whosonfirst/PlacetypeDataSource.ts +199 -0
  95. package/resources/whosonfirst/index.ts +10 -0
  96. package/resources/whosonfirst/loader.ts +157 -0
  97. package/resources/whosonfirst/placetypes/Placetype.ts +438 -0
  98. package/resources/whosonfirst/placetypes/admin.ts +173 -0
  99. package/resources/whosonfirst/placetypes/definition.ts +135 -0
  100. package/resources/whosonfirst/placetypes/graph.ts +0 -0
  101. package/resources/whosonfirst/placetypes/index.ts +12 -0
  102. package/resources/whosonfirst/placetypes/mermaid.ts +168 -0
  103. package/resources/whosonfirst/placetypes/tree.ts +46 -0
  104. package/routing/index.ts +208 -0
  105. package/scripting/index.ts +40 -0
  106. package/scripting/utils/index.ts +106 -0
  107. package/tokenization/Graph.ts +50 -0
  108. package/tokenization/Span.ts +297 -0
  109. package/tokenization/index.ts +9 -0
  110. package/tokenization/normalizer.ts +123 -0
  111. package/tools/download-libpostal-resources.ts +98 -0
  112. package/tools/download-ssl-address.ts +99 -0
  113. package/tools/generate-language-types.ts +194 -0
  114. package/tools/index.ts +13 -0
  115. package/types/Classification.ts +211 -0
  116. package/types/classifier.ts +133 -0
  117. package/types/component.ts +75 -0
  118. package/types/index.ts +10 -0
  119. package/types/mapping.ts +81 -0
  120. package/utils/data-root.ts +56 -0
  121. package/utils/hash.ts +27 -0
  122. package/utils/index.ts +14 -0
  123. package/utils/jsonl.ts +43 -0
  124. package/utils/python-json.ts +105 -0
  125. package/utils/python-random.ts +90 -0
  126. package/utils/repo.ts +118 -0
  127. package/utils/sealed-db.ts +85 -0
  128. package/utils/stats.ts +32 -0
  129. package/vitest.config.ts +53 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mailwoman/core",
3
- "version": "7.2.0",
3
+ "version": "7.2.1",
4
4
  "description": "Mailwoman core: tokenization, decoder, formatter, resources, filters, utils.",
5
5
  "license": "AGPL-3.0-only OR LicenseRef-Commercial",
6
6
  "repository": {
@@ -14,174 +14,265 @@
14
14
  "out/**/*.d.ts",
15
15
  "out/**/*.d.ts.map",
16
16
  "data/**/*",
17
- "THIRD_PARTY_NOTICES.md"
17
+ "THIRD_PARTY_NOTICES.md",
18
+ "*.ts",
19
+ "*.tsx",
20
+ "**/*.ts",
21
+ "**/*.tsx",
22
+ "!*.test.ts",
23
+ "!*.test.tsx",
24
+ "!**/*.test.ts",
25
+ "!**/*.test.tsx"
18
26
  ],
19
27
  "type": "module",
20
28
  "exports": {
21
29
  "./package.json": "./package.json",
22
30
  ".": {
23
- "node": "./index.ts",
24
- "default": "./out/index.js",
25
- "types": "./out/index.d.ts"
31
+ "types": "./out/index.d.ts",
32
+ "default": "./out/index.js"
26
33
  },
27
34
  "./coarse-placer": {
28
- "node": "./coarse-placer/coarse-placer.ts",
29
- "default": "./out/coarse-placer/coarse-placer.js",
30
- "types": "./out/coarse-placer/coarse-placer.d.ts"
35
+ "types": "./out/coarse-placer/coarse-placer.d.ts",
36
+ "default": "./out/coarse-placer/coarse-placer.js"
31
37
  },
32
38
  "./coarse-placer/tools": {
33
- "node": "./coarse-placer/tools/index.ts",
34
- "default": "./out/coarse-placer/tools/index.js",
35
- "types": "./out/coarse-placer/tools/index.d.ts"
39
+ "types": "./out/coarse-placer/tools/index.d.ts",
40
+ "default": "./out/coarse-placer/tools/index.js"
36
41
  },
37
42
  "./tokenization": {
38
- "node": "./tokenization/index.ts",
39
- "default": "./out/tokenization/index.js",
40
- "types": "./out/tokenization/index.d.ts"
43
+ "types": "./out/tokenization/index.d.ts",
44
+ "default": "./out/tokenization/index.js"
41
45
  },
42
46
  "./types": {
43
- "node": "./types/index.ts",
44
- "default": "./out/types/index.js",
45
- "types": "./out/types/index.d.ts"
47
+ "types": "./out/types/index.d.ts",
48
+ "default": "./out/types/index.js"
46
49
  },
47
50
  "./decoder": {
48
- "node": "./decoder/index.ts",
49
- "default": "./out/decoder/index.js",
50
- "types": "./out/decoder/index.d.ts"
51
+ "types": "./out/decoder/index.d.ts",
52
+ "default": "./out/decoder/index.js"
51
53
  },
52
54
  "./env": {
53
- "node": "./env/index.ts",
54
- "default": "./out/env/index.js",
55
- "types": "./out/env/index.d.ts"
55
+ "types": "./out/env/index.d.ts",
56
+ "default": "./out/env/index.js"
56
57
  },
57
58
  "./layers": {
58
- "node": "./layers/index.ts",
59
- "default": "./out/layers/index.js",
60
- "types": "./out/layers/index.d.ts"
59
+ "types": "./out/layers/index.d.ts",
60
+ "default": "./out/layers/index.js"
61
61
  },
62
62
  "./resources": {
63
- "node": "./resources/index.ts",
64
- "default": "./out/resources/index.js",
65
- "types": "./out/resources/index.d.ts"
63
+ "types": "./out/resources/index.d.ts",
64
+ "default": "./out/resources/index.js"
66
65
  },
67
66
  "./resources/languages": {
68
- "node": "./resources/languages/index.ts",
69
- "default": "./out/resources/languages/index.js",
70
- "types": "./out/resources/languages/index.d.ts"
67
+ "types": "./out/resources/languages/index.d.ts",
68
+ "default": "./out/resources/languages/index.js"
71
69
  },
72
70
  "./resources/whosonfirst": {
73
- "node": "./resources/whosonfirst/index.ts",
74
- "default": "./out/resources/whosonfirst/index.js",
75
- "types": "./out/resources/whosonfirst/index.d.ts"
76
- },
77
- "./resources/libpostal": {
78
- "node": "./resources/libpostal/index.ts",
79
- "default": "./out/resources/libpostal/index.js",
80
- "types": "./out/resources/libpostal/index.d.ts"
71
+ "types": "./out/resources/whosonfirst/index.d.ts",
72
+ "default": "./out/resources/whosonfirst/index.js"
81
73
  },
82
74
  "./utils": {
83
- "node": "./utils/index.ts",
84
- "default": "./out/utils/index.js",
85
- "types": "./out/utils/index.d.ts"
75
+ "types": "./out/utils/index.d.ts",
76
+ "default": "./out/utils/index.js"
86
77
  },
87
78
  "./fs": {
88
- "node": "./fs/node.ts",
89
- "default": "./out/fs/node.js",
90
- "types": "./out/fs/node.d.ts"
79
+ "types": "./out/fs/node.d.ts",
80
+ "default": "./out/fs/node.js"
91
81
  },
92
82
  "./scripting": {
93
- "node": "./scripting/index.ts",
94
- "default": "./out/scripting/index.js",
95
- "types": "./out/scripting/index.d.ts"
83
+ "types": "./out/scripting/index.d.ts",
84
+ "default": "./out/scripting/index.js"
96
85
  },
97
86
  "./scripting/utils": {
98
- "node": "./scripting/utils/index.ts",
99
- "default": "./out/scripting/utils/index.js",
100
- "types": "./out/scripting/utils/index.d.ts"
87
+ "types": "./out/scripting/utils/index.d.ts",
88
+ "default": "./out/scripting/utils/index.js"
101
89
  },
102
90
  "./lifecycle": {
103
- "node": "./lifecycle/index.ts",
104
- "default": "./out/lifecycle/index.js",
105
- "types": "./out/lifecycle/index.d.ts"
106
- },
107
- "./identifiers": {
108
- "node": "./identifiers/index.ts",
109
- "default": "./out/identifiers/index.js",
110
- "types": "./out/identifiers/index.d.ts"
91
+ "types": "./out/lifecycle/index.d.ts",
92
+ "default": "./out/lifecycle/index.js"
111
93
  },
112
94
  "./objects": {
113
- "node": "./objects.ts",
114
- "default": "./out/objects.js",
115
- "types": "./out/objects.d.ts"
95
+ "types": "./out/objects.d.ts",
96
+ "default": "./out/objects.js"
116
97
  },
117
98
  "./locale": {
118
- "node": "./locale/index.ts",
119
- "default": "./out/locale/index.js",
120
- "types": "./out/locale/index.d.ts"
99
+ "types": "./out/locale/index.d.ts",
100
+ "default": "./out/locale/index.js"
121
101
  },
122
102
  "./api": {
123
- "node": "./api/index.ts",
124
- "default": "./out/api/index.js",
125
- "types": "./out/api/index.d.ts"
103
+ "types": "./out/api/index.d.ts",
104
+ "default": "./out/api/index.js"
126
105
  },
127
106
  "./logging": {
128
- "node": "./logging/index.ts",
129
- "default": "./out/logging/index.js",
130
- "types": "./out/logging/index.d.ts"
107
+ "types": "./out/logging/index.d.ts",
108
+ "default": "./out/logging/index.js"
131
109
  },
132
110
  "./policy": {
133
- "node": "./policy/index.ts",
134
- "default": "./out/policy/index.js",
135
- "types": "./out/policy/index.d.ts"
111
+ "types": "./out/policy/index.d.ts",
112
+ "default": "./out/policy/index.js"
136
113
  },
137
114
  "./pipeline": {
138
- "node": "./pipeline/index.ts",
139
- "default": "./out/pipeline/index.js",
140
- "types": "./out/pipeline/index.d.ts"
115
+ "types": "./out/pipeline/index.d.ts",
116
+ "default": "./out/pipeline/index.js"
141
117
  },
142
118
  "./resolver": {
143
- "node": "./resolver/index.ts",
144
- "default": "./out/resolver/index.js",
145
- "types": "./out/resolver/index.d.ts"
119
+ "types": "./out/resolver/index.d.ts",
120
+ "default": "./out/resolver/index.js"
146
121
  },
147
122
  "./kysley/client": {
148
- "node": "./kysley/client.ts",
149
- "default": "./out/kysley/client.js",
150
- "types": "./out/kysley/client.d.ts"
123
+ "types": "./out/kysley/client.d.ts",
124
+ "default": "./out/kysley/client.js"
151
125
  },
152
126
  "./kysley/dialect": {
153
- "node": "./kysley/dialect.ts",
154
- "default": "./out/kysley/dialect.js",
155
- "types": "./out/kysley/dialect.d.ts"
127
+ "types": "./out/kysley/dialect.d.ts",
128
+ "default": "./out/kysley/dialect.js"
156
129
  },
157
130
  "./kysley/dialect-config": {
158
- "node": "./kysley/dialect-config.ts",
159
- "default": "./out/kysley/dialect-config.js",
160
- "types": "./out/kysley/dialect-config.d.ts"
131
+ "types": "./out/kysley/dialect-config.d.ts",
132
+ "default": "./out/kysley/dialect-config.js"
161
133
  },
162
134
  "./errors": {
163
- "node": "./errors/index.ts",
164
- "default": "./out/errors/index.js",
165
- "types": "./out/errors/index.d.ts"
135
+ "types": "./out/errors/index.d.ts",
136
+ "default": "./out/errors/index.js"
166
137
  },
167
138
  "./routing": {
168
- "node": "./routing/index.ts",
169
- "default": "./out/routing/index.js",
170
- "types": "./out/routing/index.d.ts"
139
+ "types": "./out/routing/index.d.ts",
140
+ "default": "./out/routing/index.js"
171
141
  },
172
142
  "./filters/*": {
173
- "node": "./filters/*.ts",
174
- "default": "./out/filters/*.js",
175
- "types": "./out/filters/*.d.ts"
143
+ "types": "./out/filters/*.d.ts",
144
+ "default": "./out/filters/*.js"
176
145
  },
177
146
  "./tools": {
178
- "node": "./tools/index.ts",
179
- "default": "./out/tools/index.js",
180
- "types": "./out/tools/index.d.ts"
147
+ "types": "./out/tools/index.d.ts",
148
+ "default": "./out/tools/index.js"
181
149
  }
182
150
  },
183
151
  "publishConfig": {
184
- "access": "public"
152
+ "access": "public",
153
+ "exports": {
154
+ "./package.json": "./package.json",
155
+ ".": {
156
+ "types": "./out/index.d.ts",
157
+ "default": "./out/index.js"
158
+ },
159
+ "./coarse-placer": {
160
+ "types": "./out/coarse-placer/coarse-placer.d.ts",
161
+ "default": "./out/coarse-placer/coarse-placer.js"
162
+ },
163
+ "./coarse-placer/tools": {
164
+ "types": "./out/coarse-placer/tools/index.d.ts",
165
+ "default": "./out/coarse-placer/tools/index.js"
166
+ },
167
+ "./tokenization": {
168
+ "types": "./out/tokenization/index.d.ts",
169
+ "default": "./out/tokenization/index.js"
170
+ },
171
+ "./types": {
172
+ "types": "./out/types/index.d.ts",
173
+ "default": "./out/types/index.js"
174
+ },
175
+ "./decoder": {
176
+ "types": "./out/decoder/index.d.ts",
177
+ "default": "./out/decoder/index.js"
178
+ },
179
+ "./env": {
180
+ "types": "./out/env/index.d.ts",
181
+ "default": "./out/env/index.js"
182
+ },
183
+ "./layers": {
184
+ "types": "./out/layers/index.d.ts",
185
+ "default": "./out/layers/index.js"
186
+ },
187
+ "./resources": {
188
+ "types": "./out/resources/index.d.ts",
189
+ "default": "./out/resources/index.js"
190
+ },
191
+ "./resources/languages": {
192
+ "types": "./out/resources/languages/index.d.ts",
193
+ "default": "./out/resources/languages/index.js"
194
+ },
195
+ "./resources/whosonfirst": {
196
+ "types": "./out/resources/whosonfirst/index.d.ts",
197
+ "default": "./out/resources/whosonfirst/index.js"
198
+ },
199
+ "./utils": {
200
+ "types": "./out/utils/index.d.ts",
201
+ "default": "./out/utils/index.js"
202
+ },
203
+ "./fs": {
204
+ "types": "./out/fs/node.d.ts",
205
+ "default": "./out/fs/node.js"
206
+ },
207
+ "./scripting": {
208
+ "types": "./out/scripting/index.d.ts",
209
+ "default": "./out/scripting/index.js"
210
+ },
211
+ "./scripting/utils": {
212
+ "types": "./out/scripting/utils/index.d.ts",
213
+ "default": "./out/scripting/utils/index.js"
214
+ },
215
+ "./lifecycle": {
216
+ "types": "./out/lifecycle/index.d.ts",
217
+ "default": "./out/lifecycle/index.js"
218
+ },
219
+ "./objects": {
220
+ "types": "./out/objects.d.ts",
221
+ "default": "./out/objects.js"
222
+ },
223
+ "./locale": {
224
+ "types": "./out/locale/index.d.ts",
225
+ "default": "./out/locale/index.js"
226
+ },
227
+ "./api": {
228
+ "types": "./out/api/index.d.ts",
229
+ "default": "./out/api/index.js"
230
+ },
231
+ "./logging": {
232
+ "types": "./out/logging/index.d.ts",
233
+ "default": "./out/logging/index.js"
234
+ },
235
+ "./policy": {
236
+ "types": "./out/policy/index.d.ts",
237
+ "default": "./out/policy/index.js"
238
+ },
239
+ "./pipeline": {
240
+ "types": "./out/pipeline/index.d.ts",
241
+ "default": "./out/pipeline/index.js"
242
+ },
243
+ "./resolver": {
244
+ "types": "./out/resolver/index.d.ts",
245
+ "default": "./out/resolver/index.js"
246
+ },
247
+ "./kysley/client": {
248
+ "types": "./out/kysley/client.d.ts",
249
+ "default": "./out/kysley/client.js"
250
+ },
251
+ "./kysley/dialect": {
252
+ "types": "./out/kysley/dialect.d.ts",
253
+ "default": "./out/kysley/dialect.js"
254
+ },
255
+ "./kysley/dialect-config": {
256
+ "types": "./out/kysley/dialect-config.d.ts",
257
+ "default": "./out/kysley/dialect-config.js"
258
+ },
259
+ "./errors": {
260
+ "types": "./out/errors/index.d.ts",
261
+ "default": "./out/errors/index.js"
262
+ },
263
+ "./routing": {
264
+ "types": "./out/routing/index.d.ts",
265
+ "default": "./out/routing/index.js"
266
+ },
267
+ "./filters/*": {
268
+ "types": "./out/filters/*.d.ts",
269
+ "default": "./out/filters/*.js"
270
+ },
271
+ "./tools": {
272
+ "types": "./out/tools/index.d.ts",
273
+ "default": "./out/tools/index.js"
274
+ }
275
+ }
185
276
  },
186
277
  "dependencies": {
187
278
  "axios": "^1.18.1",
@@ -205,7 +296,7 @@
205
296
  },
206
297
  "devDependencies": {
207
298
  "@duckdb/node-api": "1.5.3-r.3",
208
- "@mailwoman/codex": "7.2.0",
299
+ "@mailwoman/codex": "7.2.1",
209
300
  "@types/d3-color": "^3.1.3",
210
301
  "@types/d3-scale-chromatic": "^3.1.0"
211
302
  }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Runtime pipeline coordinator — see `STAGES.md` for the full contract.
7
+ */
8
+
9
+ export { reconcileSpans } from "./reconcile.ts"
10
+ export type {
11
+ ClassifierCandidate,
12
+ ParentChainLookup,
13
+ ParseTree,
14
+ ReconcileInputs,
15
+ ReconcileOpts,
16
+ ResolverCandidatesLookup,
17
+ ScoreBreakdown,
18
+ } from "./reconcile.ts"
19
+ export { HARD_PLACE_COUNTRY_SAFELIST, hardCountryFor, isBareLocalityTree, runPipeline } from "./runtime-pipeline.ts"
20
+ export { aggregateSpanLogits } from "./span-logit-aggregation.ts"
21
+ export type { SpanBounds, TokenPiece } from "./span-logit-aggregation.ts"
22
+ export { EMPTY_SPAN_PROPOSER_LEXICON, proposeSpans } from "./span-proposer.ts"
23
+ export type { ProposedSpan, ProposedSpanKind, SpanProposerLexicon } from "./span-proposer.ts"
24
+ export { WORD_CONSISTENCY_SHIP_DEFAULT } from "./types.ts"
25
+ export type {
26
+ AddressClassifier,
27
+ ClassifierOpts,
28
+ FSTMatcherLike,
29
+ LocaleHint,
30
+ LocaleTag,
31
+ NormalizedInputLite,
32
+ PhraseGrouper,
33
+ PhraseKind,
34
+ PhraseProposal,
35
+ PipelineOpts,
36
+ PipelineResult,
37
+ PipelineTiming,
38
+ POIIntent,
39
+ POIIntentOutcome,
40
+ POIResult,
41
+ QueryKind,
42
+ QueryKindResult,
43
+ QueryShapeLite,
44
+ RuntimePipelineStages,
45
+ UserLocation,
46
+ } from "./types.ts"
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Pre-fetch adapters that wire a `ResolverBackend` into the reconciler's concordance axes (#478
7
+ * step 2, re-scoped): `reconcileSpans` already implements resolver-candidate and parent-chain
8
+ * scoring but the runtime pipeline never passed the lookups — the axes were dormant, exercised
9
+ * only by Map-backed test fixtures.
10
+ *
11
+ * The reconciler's beam search is SYNCHRONOUS; the backend is async. So this module does one
12
+ * bounded async pre-fetch pass — `findPlace` per (phrase-span × top-k tag) pair, `ancestors` per
13
+ * fetched place — and returns Map-backed lookups with the contract the reconciler expects. Query
14
+ * count is capped (`maxLookups`, default 12) the same way `resolveTree` caps its walk; pairs are
15
+ * visited in classifier-score order so the budget goes to the likeliest spans.
16
+ */
17
+
18
+ import type { ResolvedPlace, ResolverBackend } from "../resolver/types.ts"
19
+ import type { ClassifierCandidate, ParentChainLookup, ResolverCandidatesLookup } from "./reconcile.ts"
20
+
21
+ const RESOLVABLE_TAGS = new Set(["country", "region", "locality", "dependent_locality", "county", "subregion"])
22
+
23
+ export interface ReconcileLookups {
24
+ resolverCandidates: ResolverCandidatesLookup
25
+ parentChain: ParentChainLookup
26
+ }
27
+
28
+ /**
29
+ * Build the reconciler's concordance lookups from one bounded pre-fetch pass over the backend.
30
+ *
31
+ * @param backend The gazetteer backend (Stage 6's `WOFSqlitePlaceLookup.asResolverBackend()` in production; any
32
+ * structural match in tests).
33
+ * @param raw The normalized input text — span bounds index into this.
34
+ * @param classifierTopK The aggregated per-span tag candidates (score-descending preferred; the budget follows this
35
+ * order).
36
+ * @param opts.maxLookups Hard cap on `findPlace` calls (default 12).
37
+ * @param opts.candidatesPerPair Max places retained per (span, tag) pair (default 3).
38
+ */
39
+ export async function prefetchReconcileLookups(
40
+ backend: ResolverBackend,
41
+ raw: string,
42
+ classifierTopK: ReadonlyArray<ClassifierCandidate>,
43
+ opts?: { maxLookups?: number; candidatesPerPair?: number; defaultCountry?: string }
44
+ ): Promise<ReconcileLookups> {
45
+ const maxLookups = opts?.maxLookups ?? 12
46
+ const perPair = opts?.candidatesPerPair ?? 3
47
+
48
+ const candidateTable = new Map<string, ResolvedPlace[]>()
49
+ const chainTable = new Map<number | string, ResolvedPlace[]>()
50
+ const pairKey = (span: { start: number; end: number }, tag: string) => `${span.start}:${span.end}:${tag}`
51
+
52
+ let budget = maxLookups
53
+
54
+ for (const candidate of classifierTopK) {
55
+ if (budget <= 0) break
56
+
57
+ if (!RESOLVABLE_TAGS.has(candidate.tag)) continue
58
+ const key = pairKey(candidate.span, candidate.tag)
59
+
60
+ if (candidateTable.has(key)) continue
61
+ const text = raw.slice(candidate.span.start, candidate.span.end).trim()
62
+
63
+ if (!text) continue
64
+ budget--
65
+ let places: ResolvedPlace[] = []
66
+
67
+ try {
68
+ places = await backend.findPlace({
69
+ text,
70
+ placetype: candidate.tag,
71
+ ...(opts?.defaultCountry ? { country: opts.defaultCountry } : {}),
72
+ limit: perPair,
73
+ })
74
+ } catch {
75
+ // A backend failure degrades to "no concordance evidence for this pair" — the
76
+ // reconciler scores without it, identical to today's behavior.
77
+ }
78
+ candidateTable.set(key, places.slice(0, perPair))
79
+
80
+ // Ancestors are synchronous + memoized backend-side; chain them now so parentsOf is a
81
+ // pure Map read during the beam search.
82
+ if (backend.ancestors) {
83
+ for (const place of places) {
84
+ if (chainTable.has(place.id)) continue
85
+ const chain = backend.ancestors(place.id) ?? []
86
+ // The reconciler's concordance check is MEMBERSHIP-ONLY (by id) — Ancestor carries no
87
+ // coordinates, and none are needed; zeroes satisfy the ResolvedPlace shape.
88
+ chainTable.set(
89
+ place.id,
90
+ chain.map((a) => ({ id: a.id, name: a.name, placetype: a.placetype, lat: 0, lon: 0 }) as ResolvedPlace)
91
+ )
92
+ }
93
+ }
94
+ }
95
+
96
+ return {
97
+ resolverCandidates: {
98
+ candidatesFor(span, tag) {
99
+ return candidateTable.get(pairKey(span, tag)) ?? []
100
+ },
101
+ },
102
+ parentChain: {
103
+ parentsOf(place) {
104
+ return chainTable.get(place.id) ?? []
105
+ },
106
+ },
107
+ }
108
+ }