@meterapp/vehicle-db 2.50.0 → 2.52.0
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.
- package/README.md +24 -6
- package/dist/data.d.ts +5 -0
- package/dist/data.js +1 -1
- package/dist/identity-data.js +1 -1
- package/dist/resolve.d.ts +5 -0
- package/dist/resolve.js +62 -12
- package/package.json +3 -1
package/dist/resolve.d.ts
CHANGED
|
@@ -46,6 +46,11 @@ interface Catalog {
|
|
|
46
46
|
sources: {
|
|
47
47
|
source_id: string;
|
|
48
48
|
}[];
|
|
49
|
+
/** Markets the reviewed manufacturer rows were evidenced for: `rows` holds (index into `models`, index into `sets`) pairs. */
|
|
50
|
+
reviewedMarkets?: {
|
|
51
|
+
sets: readonly (readonly string[])[];
|
|
52
|
+
rows: readonly number[];
|
|
53
|
+
};
|
|
49
54
|
}
|
|
50
55
|
/** Create an isolated resolver for a validated graph and catalog, including test fixtures. */
|
|
51
56
|
export declare function createVehicleResolver(catalog: Catalog, identities: IdentityGraph): (query: string, options?: ResolveVehicleOptions) => VehicleResolution;
|
package/dist/resolve.js
CHANGED
|
@@ -30,6 +30,29 @@ function createVehicleResolver(catalog, identities) {
|
|
|
30
30
|
byMake.set(r[1], list);
|
|
31
31
|
}
|
|
32
32
|
const names = catalog.modelNames.map(identity_1.normalizeIdentityText);
|
|
33
|
+
// A flat row is verified for a market by its reviewed evidence or by a scoped identity linked to it.
|
|
34
|
+
const reviewed = new Map();
|
|
35
|
+
const mask = catalog.reviewedMarkets;
|
|
36
|
+
if (mask)
|
|
37
|
+
for (let i = 0; i + 1 < mask.rows.length; i += 2)
|
|
38
|
+
reviewed.set(catalog.models[mask.rows[i]], mask.sets[mask.rows[i + 1]] ?? []);
|
|
39
|
+
const rowKey = (year, makeId, modelId, vehicleTypeId) => `${year}:${makeId}:${modelId}:${vehicleTypeId}`;
|
|
40
|
+
let linkIndex;
|
|
41
|
+
const linkedTo = (r) => {
|
|
42
|
+
if (!linkIndex) {
|
|
43
|
+
linkIndex = new Map();
|
|
44
|
+
for (const l of identities.catalogLinks) {
|
|
45
|
+
const node = nodes.get(l.identityId);
|
|
46
|
+
if (!node)
|
|
47
|
+
continue;
|
|
48
|
+
const key = rowKey(l.year, l.makeId, l.modelId, l.vehicleTypeId), list = linkIndex.get(key) ?? [];
|
|
49
|
+
if (!list.includes(node))
|
|
50
|
+
list.push(node);
|
|
51
|
+
linkIndex.set(key, list);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return linkIndex.get(rowKey(r[0], r[1], r[2], r[4])) ?? [];
|
|
55
|
+
};
|
|
33
56
|
const select = (r) => ({ year: r[0], makeId: r[1], modelId: r[2], modelName: catalog.modelNames[r[3]], vehicleTypeId: r[4], sourceIds: catalog.sources.filter((_, i) => (r[5] & 2 ** i) !== 0).map(s => s.source_id) });
|
|
34
57
|
return function resolve(query, options = {}) {
|
|
35
58
|
const result = (status, candidates = [], unresolvedTokens = []) => status === "MATCHED"
|
|
@@ -65,6 +88,13 @@ function createVehicleResolver(catalog, identities) {
|
|
|
65
88
|
return result("SCOPE_UNVERIFIED");
|
|
66
89
|
const rows = (byMake.get(makeId) ?? []).filter(r => (options.vehicleTypeId === undefined || r[4] === options.vehicleTypeId) && (sourceIndex === undefined || (r[5] & 2 ** sourceIndex) !== 0));
|
|
67
90
|
const scopeOk = (n) => ancestry(n).every(p => (0, identity_1.scopeMatches)(p, context));
|
|
91
|
+
const candidateOf = (n, from) => {
|
|
92
|
+
const chain = ancestry(n), lineage = Object.fromEntries(chain.map(p => [p.kind, p.id]));
|
|
93
|
+
const links = identities.catalogLinks.filter(l => (l.identityId === n.id || (n.kind === "visual" && chain.some(p => p.id === l.identityId))) && (year === undefined || l.year === year));
|
|
94
|
+
const catalogSelections = from.filter(r => links.some(l => l.year === r[0] && l.makeId === r[1] && l.modelId === r[2] && l.vehicleTypeId === r[4])).map(select);
|
|
95
|
+
const visuals = identities.identities.filter(v => v.kind === "visual" && ancestry(v).some(p => p.id === n.id) && scopeOk(v));
|
|
96
|
+
return { identityId: n.id, depth: n.kind, lineage, catalogSelections, visualIdentityIds: visuals.map(v => v.id), evidenceIds: [...new Set(chain.flatMap(p => p.evidenceIds))] };
|
|
97
|
+
};
|
|
68
98
|
const allNamed = identities.identities.filter(n => n.makeId === makeId && (options.vehicleTypeId === undefined || options.vehicleTypeId === n.vehicleTypeId) && n.names.some(s => (0, identity_1.normalizeIdentityText)(s) === text));
|
|
69
99
|
const aliases = identities.aliases.filter(a => (0, identity_1.normalizeIdentityText)(a.text) === text && nodes.get(a.targetId)?.makeId === makeId && (options.vehicleTypeId === undefined || nodes.get(a.targetId)?.vehicleTypeId === options.vehicleTypeId));
|
|
70
100
|
const named = [...new Map([...allNamed, ...aliases.filter(a => (0, identity_1.scopeMatches)(a, context)).map(a => nodes.get(a.targetId))].map(n => [n.id, n])).values()];
|
|
@@ -82,13 +112,7 @@ function createVehicleResolver(catalog, identities) {
|
|
|
82
112
|
matching = [...new Map(matching.map(n => [n.id, n])).values()];
|
|
83
113
|
if (!matching.length)
|
|
84
114
|
return result("GENERATION_UNRESOLVED");
|
|
85
|
-
const candidates = matching.map(n =>
|
|
86
|
-
const chain = ancestry(n), lineage = Object.fromEntries(chain.map(p => [p.kind, p.id]));
|
|
87
|
-
const links = identities.catalogLinks.filter(l => (l.identityId === n.id || (n.kind === "visual" && chain.some(p => p.id === l.identityId))) && (year === undefined || l.year === year));
|
|
88
|
-
const catalogSelections = rows.filter(r => links.some(l => l.year === r[0] && l.makeId === r[1] && l.modelId === r[2] && l.vehicleTypeId === r[4])).map(select);
|
|
89
|
-
const visuals = identities.identities.filter(v => v.kind === "visual" && ancestry(v).some(p => p.id === n.id) && scopeOk(v));
|
|
90
|
-
return { identityId: n.id, depth: n.kind, lineage, catalogSelections, visualIdentityIds: visuals.map(v => v.id), evidenceIds: [...new Set(chain.flatMap(p => p.evidenceIds))] };
|
|
91
|
-
});
|
|
115
|
+
const candidates = matching.map(n => candidateOf(n, rows));
|
|
92
116
|
if (sourceIndex !== undefined && candidates.every(c => !c.catalogSelections.length))
|
|
93
117
|
return result("SCOPE_UNVERIFIED", candidates);
|
|
94
118
|
if (candidates.length !== 1)
|
|
@@ -111,12 +135,38 @@ function createVehicleResolver(catalog, identities) {
|
|
|
111
135
|
group.push(r);
|
|
112
136
|
groups.set(k, group);
|
|
113
137
|
}
|
|
114
|
-
const
|
|
115
|
-
|
|
138
|
+
const flat = (rs) => ({ depth: "catalog", lineage: {}, catalogSelections: rs.map(select), visualIdentityIds: [], evidenceIds: [] });
|
|
139
|
+
const candidates = [...groups.values()].map(flat);
|
|
140
|
+
if (options.market === undefined) {
|
|
141
|
+
if (options.depth && options.depth !== "catalog")
|
|
142
|
+
return result("GENERATION_UNRESOLVED", candidates);
|
|
143
|
+
return result(candidates.length === 1 ? "MATCHED" : "AMBIGUOUS", candidates);
|
|
144
|
+
}
|
|
145
|
+
// With a market, only rows evidenced for it answer: reviewed for that market, or linked to an
|
|
146
|
+
// identity whose whole scope (market, year and year basis) fits the request.
|
|
147
|
+
const market = options.market;
|
|
148
|
+
const inScope = (r) => linkedTo(r).filter(scopeOk);
|
|
149
|
+
const verified = yearRows.filter(r => reviewed.get(r)?.includes(market) || inScope(r).length);
|
|
150
|
+
if (!verified.length)
|
|
116
151
|
return result("SCOPE_UNVERIFIED", candidates);
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
152
|
+
const depth = options.depth ?? "catalog";
|
|
153
|
+
if (depth === "catalog") {
|
|
154
|
+
const verifiedGroups = [...groups.values()].map(rs => rs.filter(r => verified.includes(r))).filter(rs => rs.length);
|
|
155
|
+
const scoped = verifiedGroups.map(rs => {
|
|
156
|
+
const linked = [...new Set(rs.flatMap(inScope))];
|
|
157
|
+
const visuals = identities.identities.filter(v => v.kind === "visual" && scopeOk(v) && ancestry(v).some(p => linked.includes(p)));
|
|
158
|
+
return { ...flat(rs), visualIdentityIds: visuals.map(v => v.id), evidenceIds: [...new Set(linked.flatMap(n => ancestry(n)).flatMap(p => p.evidenceIds))] };
|
|
159
|
+
});
|
|
160
|
+
return result(scoped.length === 1 ? "MATCHED" : "AMBIGUOUS", scoped);
|
|
161
|
+
}
|
|
162
|
+
const linked = [...new Set(verified.flatMap(inScope))];
|
|
163
|
+
// A linked identity deeper than the request answers with its ancestor at that depth; a shallower one with its scoped children.
|
|
164
|
+
const deep = [...new Set(linked.flatMap(n => rank[n.kind] >= rank[depth] ? [ancestry(n).find(p => p.kind === depth) ?? n] : identities.identities.filter(child => rank[child.kind] === rank[depth] && ancestry(child).some(p => p.id === n.id) && scopeOk(child))))];
|
|
165
|
+
if (!deep.length)
|
|
166
|
+
return result("GENERATION_UNRESOLVED", [...groups.values()].map(rs => rs.filter(r => verified.includes(r))).filter(rs => rs.length).map(flat));
|
|
167
|
+
// The selections are the verified rows that led to the identity, through itself, a descendant or an ancestor.
|
|
168
|
+
const scoped = deep.map(n => ({ ...candidateOf(n, verified), catalogSelections: verified.filter(r => inScope(r).some(x => ancestry(x).some(p => p.id === n.id) || ancestry(n).some(p => p.id === x.id))).map(select) }));
|
|
169
|
+
return result(scoped.length === 1 ? "MATCHED" : "AMBIGUOUS", scoped);
|
|
120
170
|
}
|
|
121
171
|
const prefixes = [...rows.map(r => names[r[3]]), ...identities.identities.filter(n => n.makeId === makeId).flatMap(n => n.names.map(identity_1.normalizeIdentityText))].filter(n => text.startsWith(n + " ")).sort((a, b) => b.length - a.length);
|
|
122
172
|
return prefixes.length ? result("UNRESOLVED_TOKENS", [], text.slice(prefixes[0].length).trim().split(" ")) : result("UNKNOWN_MODEL");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meterapp/vehicle-db",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.52.0",
|
|
4
4
|
"description": "Offline international database for cars, motorcycles, trucks, buses, MPVs, and more",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -72,6 +72,8 @@
|
|
|
72
72
|
"refresh:fueleconomy-us": "tsx scripts/import-fueleconomy-us.ts",
|
|
73
73
|
"refresh:reviewed": "tsx scripts/import-reviewed-models.ts data/evidence/manufacturer-reviewed.json",
|
|
74
74
|
"replay": "tsx scripts/replay-catalog.ts",
|
|
75
|
+
"release:diff": "tsx scripts/release-diff.ts",
|
|
76
|
+
"changelog": "tsx scripts/release-diff.ts --worktree --changelog",
|
|
75
77
|
"refresh:manufacturer-catalogs": "tsx scripts/import-manufacturer-catalogs.ts",
|
|
76
78
|
"refresh:india": "tsx scripts/import-indian-catalogs.ts",
|
|
77
79
|
"refresh:india-history": "tsx scripts/import-indian-history.ts",
|