Files
IPSExtractor/services/desktop_app/src/renderer/modules/extraction/JobExtractionWizard.tsx
T

565 lines
21 KiB
TypeScript

/**
* Wrapper del ExtractionWizard original para el contexto de Job.
* Contiene toda la lógica de estado que antes vivía en App.tsx,
* preservando el diseño paso a paso completo.
*/
import { ArrowRight, CheckCircle2, FileText } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import type {
BackgroundProgress,
CountJobFile,
DocumentType,
ExtractionFormState,
FieldSelection,
ManifestEntry,
SchemaResponse,
Task,
} from "../../../shared/types";
import { ExtractionWizard } from "../../components/ExtractionWizard";
import type { ExtractionStep } from "../../components/ExtractionStepper";
import {
buildDesktopBatchPayload,
buildEstimateUploadForm,
buildIngestFormBulk,
buildSchemaUploadForm,
flattenSchema,
mergeSchemaResponses,
validDocTypes,
} from "../../lib/payloads";
import { defaultAlias } from "../../lib/schemaDisplay";
import { loadApiBaseUrl } from "../../lib/apiDefaults";
import { splitRadicados } from "../../lib/pathMapping";
import type { ModuleWizardProps } from "../registry";
import { ExtractionResults } from "./ExtractionResults";
declare const desktopApi: import("../../../shared/types").DesktopBridge;
interface Props extends ModuleWizardProps {
/** ID de tarea de conteo previa (desde contexto de JobView) */
countingTaskId?: string;
/** Conteos exactos válidos para filtrar desde el conteo */
validCounts?: number[];
/** Número de archivos válidos del conteo (para mostrar en banner) */
validCount?: number;
/** Callback de progreso en segundo plano, para mostrarlo desde otras secciones. */
onProgress?: (progress: BackgroundProgress) => void;
}
function makeDefaultForm(job: { metadata?: Record<string, unknown> }, fromCount = false): ExtractionFormState {
const folder = String(job.metadata?.folder_path ?? "");
return {
apiBaseUrl: loadApiBaseUrl(),
localRoot: folder || localStorage.getItem("localRoot") || "",
containerRoot: localStorage.getItem("containerRoot") || "/data",
selectedLocalPath: folder,
selectedContainerPath: folder,
// Cuando viene de conteo usamos "general" porque los archivos están en carpeta plana.
// En extracción directa, se puede cambiar en el wizard.
mode: fromCount ? "general" : "radicados",
docTypes: [{ name: "Factura", keyword: "Factura_O_Documento_Equivalente" }],
keywordsText: "factura",
retryOnEmpty: false,
filterEnabled: false,
radicadoFilterText: "",
tiffEnabled: false,
tiffStart: "0",
tiffEnd: "0",
model: localStorage.getItem("extractorModel") || "gpt-5.4",
};
}
export function JobExtractionWizard({
client,
job,
onTaskCreated,
onActivateModule,
context,
countingTaskId: countingTaskIdProp,
validCounts: validCountsProp,
validCount: validCountProp,
onProgress,
}: Props) {
const countingTaskId = countingTaskIdProp ?? (context?.countingTaskId as string | undefined);
const validCounts = validCountsProp ?? (context?.validCounts as number[] | undefined);
const validCount = validCountProp ?? (context?.validCount as number | undefined);
const [form, setForm] = useState<ExtractionFormState>(() => makeDefaultForm(job, !!countingTaskId));
const [schemaResponse, setSchemaResponse] = useState<SchemaResponse | null>(null);
const [schemaSearch, setSchemaSearch] = useState("");
const [selection, setSelection] = useState<FieldSelection>({ fields: [], aliasMap: {} });
const [estimateJson, setEstimateJson] = useState<unknown>(null);
const [manifest, setManifest] = useState<ManifestEntry[]>([]);
const [ingestProgress, setIngestProgress] = useState<{ processed: number; total: number } | null>(null);
const [statusText, setStatusText] = useState("Listo");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
// Si viene del conteo, empezamos en paso 2 (los archivos ya están definidos)
const [extractionStep, setExtractionStep] = useState<ExtractionStep>(() => countingTaskId ? 2 : 1);
const [maxReachedStep, setMaxReachedStep] = useState<ExtractionStep>(() => countingTaskId ? 2 : 1);
const [taskCreated, setTaskCreated] = useState<Task | null>(null);
/** Rutas de muestras ya usadas en descubrimiento (para no repetir al reintentar). */
const [usedDiscoveryPaths, setUsedDiscoveryPaths] = useState<string[]>([]);
const [discoverySampleLabels, setDiscoverySampleLabels] = useState<string[]>([]);
const flattened = useMemo(
() => (schemaResponse ? flattenSchema(schemaResponse) : { schema: {}, sample: null }),
[schemaResponse],
);
const fieldCount = Object.keys(flattened.schema).length;
const canGoNext =
// Paso 1: necesita documentos en carpeta (o saltarse si viene de conteo)
(extractionStep === 1 && (manifest.length > 0 || !!countingTaskId)) ||
// Paso 2: necesita campos descubiertos
(extractionStep === 2 && fieldCount > 0) ||
// Paso 3: necesita al menos un campo seleccionado
(extractionStep === 3 && selection.fields.length > 0);
// Auto-indexar la carpeta del job al montar (usada como muestra para descubrir campos)
useEffect(() => {
const folder = form.localRoot || form.selectedLocalPath;
if (!folder || manifest.length > 0) return;
indexFolderAt(folder, form.mode, form.docTypes)
.then((entries) => {
setManifest(entries);
// Asegurar que selectedLocalPath esté sincronizado
setForm((cur) => ({ ...cur, selectedLocalPath: folder, selectedContainerPath: folder }));
})
.catch(() => undefined);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
function updateForm(patch: Partial<ExtractionFormState>) {
setForm((cur) => {
const next = { ...cur, ...patch };
if (next.localRoot) localStorage.setItem("localRoot", next.localRoot);
if (next.containerRoot) localStorage.setItem("containerRoot", next.containerRoot);
if (next.model) localStorage.setItem("extractorModel", next.model);
return next;
});
}
async function indexFolderAt(
path: string,
mode: ExtractionFormState["mode"],
docTypes: DocumentType[],
): Promise<ManifestEntry[]> {
if (!window.desktopApi) throw new Error("La selección de carpetas requiere Electron.");
const indexed = await window.desktopApi.indexFolder(path, mode, validDocTypes(docTypes), []);
if (indexed.length === 0) {
throw new Error("No se encontraron documentos compatibles en la carpeta.");
}
return indexed;
}
function resetSchemaState() {
setSchemaResponse(null);
setSelection({ fields: [], aliasMap: {} });
setEstimateJson(null);
setUsedDiscoveryPaths([]);
setDiscoverySampleLabels([]);
setMaxReachedStep(1);
if (extractionStep > 1) setExtractionStep(1);
}
async function selectRoot() {
if (!window.desktopApi) { setError("La selección de carpetas requiere Electron."); return; }
setError(null);
const selected = await window.desktopApi.selectDirectory();
if (!selected) return;
try {
const indexed = await indexFolderAt(selected, form.mode, form.docTypes);
setManifest(indexed);
updateForm({ localRoot: selected, selectedLocalPath: selected, selectedContainerPath: selected });
resetSchemaState();
setMaxReachedStep(1);
setStatusText(`${indexed.length} documento(s) listos`);
try { await client.updateJob(job.id, { folder_path: selected }); } catch { /* ignore */ }
} catch (err) {
setManifest([]);
setError(err instanceof Error ? err.message : "No se pudo indexar la carpeta");
}
}
async function handleModeChange(mode: ExtractionFormState["mode"]) {
if (mode === form.mode) return;
updateForm({ mode });
if (!form.selectedLocalPath) return;
try {
const indexed = await indexFolderAt(form.selectedLocalPath, mode, form.docTypes);
setManifest(indexed);
resetSchemaState();
setStatusText(`${indexed.length} documento(s) indexados`);
} catch (err) {
setManifest([]);
setError(err instanceof Error ? err.message : "No se pudo reindexar la carpeta");
}
}
function pickSampleEntry(
candidates: ManifestEntry[],
exclude: Set<string>,
preferRandom: boolean,
): ManifestEntry {
if (candidates.length === 0) {
throw new Error("No hay documentos candidatos para la muestra.");
}
const unused = candidates.filter((e) => !exclude.has(e.localPath));
const pool = unused.length > 0 ? unused : candidates;
if (!preferRandom && unused.length === candidates.length) {
return candidates[0];
}
return pool[Math.floor(Math.random() * pool.length)]!;
}
async function discoverSchema(options?: { randomOther?: boolean }) {
const randomOther = Boolean(options?.randomOther);
await runTask(
randomOther ? "Probando con otro archivo…" : "Descubriendo campos...",
async () => {
if (!window.desktopApi) throw new Error("Requiere Electron.");
if (manifest.length === 0) throw new Error("Selecciona una carpeta con documentos.");
const exclude = new Set(randomOther ? usedDiscoveryPaths : []);
const responses: SchemaResponse[] = [];
const picked: ManifestEntry[] = [];
if (form.mode === "general") {
const entry = pickSampleEntry(manifest, exclude, randomOther);
picked.push(entry);
const fileData = await window.desktopApi.readFile(entry.localPath);
const blob = new Blob([fileData.data]);
responses.push(
await client.discoverSchemaUpload(
buildSchemaUploadForm(form, blob, fileData.filename),
),
);
} else {
for (const docType of validDocTypes(form.docTypes)) {
const typed = manifest.filter((item) => item.docType === docType.name);
if (typed.length === 0) {
throw new Error(`No se encontró muestra para ${docType.name}.`);
}
const entry = pickSampleEntry(typed, exclude, randomOther);
picked.push(entry);
const fileData = await window.desktopApi.readFile(entry.localPath);
const blob = new Blob([fileData.data]);
responses.push(
await client.discoverSchemaUpload(
buildSchemaUploadForm(form, blob, fileData.filename, docType.name),
),
);
}
}
const schema = mergeSchemaResponses(responses);
setSchemaResponse(schema);
const flat = flattenSchema(schema);
setSelection({
fields: [],
aliasMap: Object.fromEntries(Object.keys(flat.schema).map((p) => [p, defaultAlias(p)])),
});
setEstimateJson(null);
const labels = picked.map((e) => e.filename);
setDiscoverySampleLabels(labels);
setUsedDiscoveryPaths((prev) => {
const next = new Set(randomOther ? prev : []);
for (const e of picked) next.add(e.localPath);
return [...next];
});
setStatusText(
`${Object.keys(flat.schema).length} campo(s) descubiertos` +
(labels.length ? ` · muestra: ${labels.join(", ")}` : ""),
);
setMaxReachedStep((cur) => (cur < 3 ? 3 : cur));
if (!randomOther) {
setExtractionStep(3);
}
},
);
}
async function rediscoverWithOtherSample() {
await discoverSchema({ randomOther: true });
}
async function estimate() {
await runTask("Estimando costo...", async () => {
if (!window.desktopApi) throw new Error("Requiere Electron.");
if (!selection.fields.length) throw new Error("Selecciona al menos un campo.");
if (manifest.length === 0) throw new Error("Selecciona una carpeta.");
const sampleEntry = manifest[0];
const fileData = await window.desktopApi.readFile(sampleEntry.localPath);
const blob = new Blob([fileData.data]);
const uniqueRadicados = new Set(manifest.map((e) => e.radicado)).size;
const formData = buildEstimateUploadForm(form, blob, fileData.filename, uniqueRadicados, selection.fields, form.model);
const result = await client.estimateUpload(formData);
setEstimateJson(result);
setStatusText("Estimación completada");
});
}
/** Determina si un archivo del conteo es válido según los filtros activos */
function isCountFileValid(f: CountJobFile): boolean {
if (f.status !== "ok" || f.is_valid === false) return false;
if (f.is_valid === true) return true;
if (!validCounts || validCounts.length === 0) return true;
return validCounts.includes(f.service_count ?? 0);
}
async function runExtraction() {
await runTask("Iniciando extracción...", async () => {
if (selection.fields.length === 0) throw new Error("Selecciona al menos un campo.");
if (!window.desktopApi) throw new Error("Requiere Electron.");
if (manifest.length === 0) throw new Error("Selecciona una carpeta.");
let workManifest: ManifestEntry[];
// ── Caso 1: desde conteo previo — filtrar manifest a los archivos válidos ──
if (countingTaskId) {
const countFiles = await client.countJobFiles(countingTaskId);
const validFilenames = new Set(
countFiles.filter(isCountFileValid).map((f) => f.filename),
);
workManifest = manifest.filter((e) => validFilenames.has(e.filename));
if (workManifest.length === 0) {
throw new Error(
"No se encontraron archivos válidos del conteo en la carpeta. " +
"Asegúrate de que los PDFs válidos están en la carpeta seleccionada.",
);
}
} else {
// ── Caso 2: extracción desktop normal ──
workManifest = [...manifest];
if (form.filterEnabled) {
const allowed = new Set(splitRadicados(form.radicadoFilterText));
workManifest = workManifest.filter((e) => allowed.has(e.radicado));
}
if (workManifest.length === 0) throw new Error("El filtro no dejó documentos.");
}
const payload = buildDesktopBatchPayload(form, selection, workManifest, form.model);
const payloadWithJob = { ...payload, job_id: job.id };
const task = await client.createExtractionTask(
payloadWithJob as Parameters<typeof client.createExtractionTask>[0],
);
setTaskCreated(task);
const total = workManifest.length;
setIngestProgress({ processed: 0, total });
onProgress?.({
module: "extraction",
total,
processed: 0,
failed: 0,
currentFile: "",
running: true,
label: "Preparando envío de documentos…",
});
const BATCH_SIZE = 20;
const BATCH_CONCURRENCY = 3;
const batches: ManifestEntry[][] = [];
for (let i = 0; i < workManifest.length; i += BATCH_SIZE) {
batches.push(workManifest.slice(i, i + BATCH_SIZE));
}
let queued = 0;
let nextBatchIdx = 0;
async function uploadBatch(batch: ManifestEntry[], batchIdx: number) {
const files = [];
for (let fi = 0; fi < batch.length; fi++) {
onProgress?.({
module: "extraction",
total,
processed: queued + fi,
failed: 0,
currentFile: batch[fi].filename,
running: true,
label: `Subiendo archivos (${queued + fi}/${total})…`,
});
files.push(await window.desktopApi!.readFile(batch[fi].localPath));
}
const blobs = files.map((f) => new Blob([f.data]));
onProgress?.({
module: "extraction",
total,
processed: queued,
failed: 0,
currentFile: batch[0]?.filename ?? "",
running: true,
label: `Enviando lote ${batchIdx + 1}/${batches.length} al servidor…`,
});
await client.ingestTaskDocumentsBulk(task.id, buildIngestFormBulk(batch, blobs));
queued += batch.length;
setIngestProgress({ processed: queued, total });
setStatusText(`Enviando ${queued}/${total} documentos…`);
onProgress?.({
module: "extraction",
total,
processed: queued,
failed: 0,
currentFile: batch[batch.length - 1]?.filename ?? "",
running: true,
label: `Subiendo archivos (${queued}/${total})…`,
});
}
async function uploadWorker() {
while (nextBatchIdx < batches.length) {
const batchIdx = nextBatchIdx++;
await uploadBatch(batches[batchIdx], batchIdx);
}
}
await Promise.all(
Array.from({ length: Math.min(BATCH_CONCURRENCY, batches.length) }, () => uploadWorker()),
);
setIngestProgress(null);
setStatusText(`${total} documentos enviados. Procesando en segundo plano…`);
onProgress?.({
module: "extraction",
total,
processed: total,
failed: 0,
currentFile: "",
running: false,
label: "Subida completada — extrayendo datos…",
});
onTaskCreated(task);
});
}
async function runTask(label: string, fn: () => Promise<void>) {
setBusy(true);
setError(null);
setStatusText(label);
try {
await fn();
} catch (err) {
setError(err instanceof Error ? err.message : "Error desconocido");
setStatusText("Error");
} finally {
setBusy(false);
}
}
function goToStep(step: ExtractionStep) {
setExtractionStep(step);
setMaxReachedStep((cur) => (step > cur ? step : cur));
}
function setDocType(index: number, patch: Partial<DocumentType>) {
updateForm({
docTypes: form.docTypes.map((item, idx) => (idx === index ? { ...item, ...patch } : item)),
});
}
function toggleField(path: string, checked: boolean) {
setSelection((cur) => {
const fields = checked
? Array.from(new Set([...cur.fields, path]))
: cur.fields.filter((f) => f !== path);
const aliasMap = { ...cur.aliasMap };
if (checked && !aliasMap[path]) aliasMap[path] = defaultAlias(path);
return { fields, aliasMap };
});
}
function selectAllFields() {
setSelection({
fields: Object.keys(flattened.schema),
aliasMap: Object.fromEntries(
Object.keys(flattened.schema).map((p) => [p, selection.aliasMap[p] || defaultAlias(p)]),
),
});
}
// Tras crear la tarea: mostrar resultados (con polling) en esta misma pestaña
if (taskCreated) {
return (
<div className="job-extraction-results-pane">
{(ingestProgress || busy) && (
<div className="wiz-toast" style={{ margin: "0 0 12px" }}>
<CheckCircle2 size={14} />
{statusText || "Extracción en curso…"}
</div>
)}
<ExtractionResults
client={client}
job={job}
task={taskCreated}
onTaskUpdated={(t) => {
setTaskCreated(t);
onTaskCreated(t);
}}
onActivateModule={onActivateModule}
/>
</div>
);
}
return (
<div className="job-extraction-wrapper">
{/* ── Banner "desde conteo" — solo cuando viene de archivos válidos del conteo ── */}
{countingTaskId && (
<div className="from-count-info-banner">
<CheckCircle2 size={15} />
<span>
<strong>Extracción desde conteo</strong> — solo se procesarán los{" "}
<strong>{validCount ?? "…"} archivos válidos</strong> seleccionados. La carpeta se usa
únicamente para descubrir los campos del documento.
</span>
<ArrowRight size={14} style={{ flexShrink: 0, opacity: 0.5 }} />
<span className="from-count-info-files">
<FileText size={13} />
{validCount ?? "…"} archivos
</span>
</div>
)}
<ExtractionWizard
step={extractionStep}
maxReached={maxReachedStep}
onStepChange={goToStep}
form={form}
updateForm={updateForm}
onModeChange={handleModeChange}
busy={busy}
error={error}
statusText={statusText}
selectRoot={selectRoot}
discoverSchema={() => discoverSchema()}
rediscoverWithOtherSample={() => rediscoverWithOtherSample()}
discoverySampleLabels={discoverySampleLabels}
canRediscoverWithOtherSample={
form.mode === "general"
? manifest.length > 1
: validDocTypes(form.docTypes).some(
(dt) => manifest.filter((m) => m.docType === dt.name).length > 1,
)
}
estimate={estimate}
runExtraction={runExtraction}
selection={selection}
setSelection={setSelection}
schemaSearch={schemaSearch}
setSchemaSearch={setSchemaSearch}
flattened={flattened}
schemaResponse={schemaResponse}
estimateJson={estimateJson}
manifest={manifest}
ingestProgress={ingestProgress}
setDocType={setDocType}
toggleField={toggleField}
selectAllFields={selectAllFields}
canGoNext={canGoNext}
onBack={() => goToStep((extractionStep > 1 ? extractionStep - 1 : 1) as ExtractionStep)}
onNext={() => { if (canGoNext && extractionStep < 4) goToStep((extractionStep + 1) as ExtractionStep); }}
effectiveDocumentCount={countingTaskId ? validCount : undefined}
documentSourceLabel={countingTaskId ? "válidos del conteo" : undefined}
/>
</div>
);
}