86 lines
4.1 KiB
JavaScript
86 lines
4.1 KiB
JavaScript
/**
|
|
* Orchestrates what happens after /api/process responds with
|
|
* needs_mapping: true - tier 1 (the keyword/geometry detector) AND
|
|
* tier 2 (layout_quirks) have both failed on every page (see
|
|
* backend/services/extraction_service.py's own LayoutMappingRequired).
|
|
* Entirely client-side: the exact/near-match lookup and comparison both
|
|
* run here, never duplicated server-side - see layoutMappingsService
|
|
* .js's own scoreNearMatch/findBestMatch for why.
|
|
*
|
|
* Reused as-is (no changes needed here) for the value-pattern mapping
|
|
* work's own needs_value_confirmation response too - resolveNeedsMapping
|
|
* only ever cares about a fingerprint, never WHY a statement needed a
|
|
* mapping in the first place, so the same exact/near-match lookup
|
|
* correctly covers a value-pattern-only mapping too. useBatchExtraction.js
|
|
* is what tells the two cases apart (it calls this the same way either
|
|
* time, differing only in what it does on `reason: 'no_match'`).
|
|
*
|
|
* `confirmFn` - an injected `(mapping, {kind, changedLabels}) =>
|
|
* Promise<boolean>` callback for the "layout changed, please confirm"
|
|
* step (`kind` is "stale" or "near") - the real caller
|
|
* (useBatchExtraction.js, via ExtractorView.vue) wires this to a real
|
|
* ConfirmModal; a test can inject a scripted callback instead. Omitting
|
|
* it treats every stale/near case as declined, never silently applying
|
|
* a mapping without confirmation.
|
|
*/
|
|
import * as layoutMappingsService from '../services/layoutMappingsService'
|
|
|
|
function forcedMappingFor(mapping) {
|
|
return {
|
|
columnMap: mapping.columnMap,
|
|
signConvention: mapping.signConvention,
|
|
ignoredRowRules: mapping.ignoredRowRules,
|
|
dateFormat: mapping.dateFormat,
|
|
startPage: mapping.startPage,
|
|
// The real header-row-text-on-the-same-page gap found while
|
|
// building the Layouts page (L.7) - see table_extract.py's own
|
|
// extract_pdf docstring for the full story.
|
|
headerBottom: mapping.headerBottom,
|
|
// Value-pattern mapping - composes with auto-detection rather than
|
|
// replacing it, so this is just as meaningful on a mapping that
|
|
// also has a real columnMap as on one that doesn't (see
|
|
// table_extract.extract_pdf's own docstring for why either way
|
|
// behaves correctly).
|
|
valuePatterns: mapping.valuePatterns,
|
|
}
|
|
}
|
|
|
|
export function useLayoutMapping(confirmFn) {
|
|
/**
|
|
* Returns {applied: true, mapping, forcedMapping} once a mapping is
|
|
* ready to retry extraction with, or {applied: false, reason:
|
|
* "declined" | "no_match", fingerprint, fingerprintHash} otherwise -
|
|
* "no_match" is the manual mapping screen's own entry point (layout
|
|
* mapper L.6, not built yet); the caller surfaces that honestly
|
|
* rather than pretending it was handled.
|
|
*/
|
|
async function resolveNeedsMapping({ fingerprint, fingerprintHash }) {
|
|
const exact = await layoutMappingsService.findByFingerprintHash(fingerprintHash)
|
|
if (exact) {
|
|
if (exact.status !== 'stale') {
|
|
return { applied: true, mapping: exact, forcedMapping: forcedMappingFor(exact) }
|
|
}
|
|
// An exact fingerprint match that's already flagged stale (a
|
|
// prior use failed its own checks) is never silently reapplied -
|
|
// treated the same as a near match, confirmation required.
|
|
const confirmed = confirmFn ? await confirmFn(exact, { kind: 'stale', changedLabels: [] }) : false
|
|
if (confirmed) return { applied: true, mapping: exact, forcedMapping: forcedMappingFor(exact) }
|
|
return { applied: false, reason: 'declined', fingerprint, fingerprintHash }
|
|
}
|
|
|
|
const candidates = await layoutMappingsService.listSharedAndMine()
|
|
const best = layoutMappingsService.findBestMatch(fingerprint, candidates)
|
|
if (best) {
|
|
const confirmed = confirmFn
|
|
? await confirmFn(best.mapping, { kind: 'near', changedLabels: best.changedLabels })
|
|
: false
|
|
if (confirmed) return { applied: true, mapping: best.mapping, forcedMapping: forcedMappingFor(best.mapping) }
|
|
return { applied: false, reason: 'declined', fingerprint, fingerprintHash }
|
|
}
|
|
|
|
return { applied: false, reason: 'no_match', fingerprint, fingerprintHash }
|
|
}
|
|
|
|
return { resolveNeedsMapping }
|
|
}
|