@meterapp/vehicle-db 2.51.0 → 2.53.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 +34 -2
- 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 +8 -1
- package/dist/resolve.js +67 -12
- package/package.json +3 -1
package/dist/resolve.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type IdentityGraph, type IdentityKind, type ScopeContext } from "./identity";
|
|
1
|
+
import { type IdentityGraph, type IdentityKind, type ScopeContext, type VisualCompatibility } from "./identity";
|
|
2
2
|
export type { IdentityGraph, VehicleIdentity } from "./identity";
|
|
3
3
|
export interface ResolveVehicleOptions extends ScopeContext {
|
|
4
4
|
makeId?: number;
|
|
@@ -46,8 +46,15 @@ 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;
|
|
52
57
|
/** Exact identity resolution, separate from autocomplete and render availability. */
|
|
53
58
|
export declare function resolveVehicle(query: string, options?: ResolveVehicleOptions): VehicleResolution;
|
|
59
|
+
/** The bundled allow/deny rules between visual identities, with rationale and scope: pass them to evaluateVisualCompatibility(). */
|
|
60
|
+
export declare function getVisualCompatibilityRules(): readonly VisualCompatibility[];
|
package/dist/resolve.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.createVehicleResolver = createVehicleResolver;
|
|
7
7
|
exports.resolveVehicle = resolveVehicle;
|
|
8
|
+
exports.getVisualCompatibilityRules = getVisualCompatibilityRules;
|
|
8
9
|
const data_1 = __importDefault(require("./data"));
|
|
9
10
|
const identity_data_1 = __importDefault(require("./identity-data"));
|
|
10
11
|
const identity_1 = require("./identity");
|
|
@@ -30,6 +31,29 @@ function createVehicleResolver(catalog, identities) {
|
|
|
30
31
|
byMake.set(r[1], list);
|
|
31
32
|
}
|
|
32
33
|
const names = catalog.modelNames.map(identity_1.normalizeIdentityText);
|
|
34
|
+
// A flat row is verified for a market by its reviewed evidence or by a scoped identity linked to it.
|
|
35
|
+
const reviewed = new Map();
|
|
36
|
+
const mask = catalog.reviewedMarkets;
|
|
37
|
+
if (mask)
|
|
38
|
+
for (let i = 0; i + 1 < mask.rows.length; i += 2)
|
|
39
|
+
reviewed.set(catalog.models[mask.rows[i]], mask.sets[mask.rows[i + 1]] ?? []);
|
|
40
|
+
const rowKey = (year, makeId, modelId, vehicleTypeId) => `${year}:${makeId}:${modelId}:${vehicleTypeId}`;
|
|
41
|
+
let linkIndex;
|
|
42
|
+
const linkedTo = (r) => {
|
|
43
|
+
if (!linkIndex) {
|
|
44
|
+
linkIndex = new Map();
|
|
45
|
+
for (const l of identities.catalogLinks) {
|
|
46
|
+
const node = nodes.get(l.identityId);
|
|
47
|
+
if (!node)
|
|
48
|
+
continue;
|
|
49
|
+
const key = rowKey(l.year, l.makeId, l.modelId, l.vehicleTypeId), list = linkIndex.get(key) ?? [];
|
|
50
|
+
if (!list.includes(node))
|
|
51
|
+
list.push(node);
|
|
52
|
+
linkIndex.set(key, list);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return linkIndex.get(rowKey(r[0], r[1], r[2], r[4])) ?? [];
|
|
56
|
+
};
|
|
33
57
|
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
58
|
return function resolve(query, options = {}) {
|
|
35
59
|
const result = (status, candidates = [], unresolvedTokens = []) => status === "MATCHED"
|
|
@@ -65,6 +89,13 @@ function createVehicleResolver(catalog, identities) {
|
|
|
65
89
|
return result("SCOPE_UNVERIFIED");
|
|
66
90
|
const rows = (byMake.get(makeId) ?? []).filter(r => (options.vehicleTypeId === undefined || r[4] === options.vehicleTypeId) && (sourceIndex === undefined || (r[5] & 2 ** sourceIndex) !== 0));
|
|
67
91
|
const scopeOk = (n) => ancestry(n).every(p => (0, identity_1.scopeMatches)(p, context));
|
|
92
|
+
const candidateOf = (n, from) => {
|
|
93
|
+
const chain = ancestry(n), lineage = Object.fromEntries(chain.map(p => [p.kind, p.id]));
|
|
94
|
+
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));
|
|
95
|
+
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);
|
|
96
|
+
const visuals = identities.identities.filter(v => v.kind === "visual" && ancestry(v).some(p => p.id === n.id) && scopeOk(v));
|
|
97
|
+
return { identityId: n.id, depth: n.kind, lineage, catalogSelections, visualIdentityIds: visuals.map(v => v.id), evidenceIds: [...new Set(chain.flatMap(p => p.evidenceIds))] };
|
|
98
|
+
};
|
|
68
99
|
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
100
|
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
101
|
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 +113,7 @@ function createVehicleResolver(catalog, identities) {
|
|
|
82
113
|
matching = [...new Map(matching.map(n => [n.id, n])).values()];
|
|
83
114
|
if (!matching.length)
|
|
84
115
|
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
|
-
});
|
|
116
|
+
const candidates = matching.map(n => candidateOf(n, rows));
|
|
92
117
|
if (sourceIndex !== undefined && candidates.every(c => !c.catalogSelections.length))
|
|
93
118
|
return result("SCOPE_UNVERIFIED", candidates);
|
|
94
119
|
if (candidates.length !== 1)
|
|
@@ -111,12 +136,38 @@ function createVehicleResolver(catalog, identities) {
|
|
|
111
136
|
group.push(r);
|
|
112
137
|
groups.set(k, group);
|
|
113
138
|
}
|
|
114
|
-
const
|
|
115
|
-
|
|
139
|
+
const flat = (rs) => ({ depth: "catalog", lineage: {}, catalogSelections: rs.map(select), visualIdentityIds: [], evidenceIds: [] });
|
|
140
|
+
const candidates = [...groups.values()].map(flat);
|
|
141
|
+
if (options.market === undefined) {
|
|
142
|
+
if (options.depth && options.depth !== "catalog")
|
|
143
|
+
return result("GENERATION_UNRESOLVED", candidates);
|
|
144
|
+
return result(candidates.length === 1 ? "MATCHED" : "AMBIGUOUS", candidates);
|
|
145
|
+
}
|
|
146
|
+
// With a market, only rows evidenced for it answer: reviewed for that market, or linked to an
|
|
147
|
+
// identity whose whole scope (market, year and year basis) fits the request.
|
|
148
|
+
const market = options.market;
|
|
149
|
+
const inScope = (r) => linkedTo(r).filter(scopeOk);
|
|
150
|
+
const verified = yearRows.filter(r => reviewed.get(r)?.includes(market) || inScope(r).length);
|
|
151
|
+
if (!verified.length)
|
|
116
152
|
return result("SCOPE_UNVERIFIED", candidates);
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
153
|
+
const depth = options.depth ?? "catalog";
|
|
154
|
+
if (depth === "catalog") {
|
|
155
|
+
const verifiedGroups = [...groups.values()].map(rs => rs.filter(r => verified.includes(r))).filter(rs => rs.length);
|
|
156
|
+
const scoped = verifiedGroups.map(rs => {
|
|
157
|
+
const linked = [...new Set(rs.flatMap(inScope))];
|
|
158
|
+
const visuals = identities.identities.filter(v => v.kind === "visual" && scopeOk(v) && ancestry(v).some(p => linked.includes(p)));
|
|
159
|
+
return { ...flat(rs), visualIdentityIds: visuals.map(v => v.id), evidenceIds: [...new Set(linked.flatMap(n => ancestry(n)).flatMap(p => p.evidenceIds))] };
|
|
160
|
+
});
|
|
161
|
+
return result(scoped.length === 1 ? "MATCHED" : "AMBIGUOUS", scoped);
|
|
162
|
+
}
|
|
163
|
+
const linked = [...new Set(verified.flatMap(inScope))];
|
|
164
|
+
// A linked identity deeper than the request answers with its ancestor at that depth; a shallower one with its scoped children.
|
|
165
|
+
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))))];
|
|
166
|
+
if (!deep.length)
|
|
167
|
+
return result("GENERATION_UNRESOLVED", [...groups.values()].map(rs => rs.filter(r => verified.includes(r))).filter(rs => rs.length).map(flat));
|
|
168
|
+
// The selections are the verified rows that led to the identity, through itself, a descendant or an ancestor.
|
|
169
|
+
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) }));
|
|
170
|
+
return result(scoped.length === 1 ? "MATCHED" : "AMBIGUOUS", scoped);
|
|
120
171
|
}
|
|
121
172
|
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
173
|
return prefixes.length ? result("UNRESOLVED_TOKENS", [], text.slice(prefixes[0].length).trim().split(" ")) : result("UNKNOWN_MODEL");
|
|
@@ -128,3 +179,7 @@ function resolveVehicle(query, options = {}) {
|
|
|
128
179
|
resolver ?? (resolver = createVehicleResolver(data_1.default, identity_data_1.default));
|
|
129
180
|
return resolver(query, options);
|
|
130
181
|
}
|
|
182
|
+
/** The bundled allow/deny rules between visual identities, with rationale and scope: pass them to evaluateVisualCompatibility(). */
|
|
183
|
+
function getVisualCompatibilityRules() {
|
|
184
|
+
return identity_data_1.default.visualCompatibility;
|
|
185
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meterapp/vehicle-db",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.53.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",
|