2026-09-13 02:08:55 +03:00
|
|
|
/**
|
|
|
|
|
* POST /api/graph/import — the format-agnostic front door's transport
|
|
|
|
|
* (vision §1, 2026-09-13): raw personal-notes files in, one extraction run
|
|
|
|
|
* per file through the SAME pipeline the sessions use. Multipart form data
|
|
|
|
|
* (field "files", any text format — the extractor is format-agnostic); the
|
|
|
|
|
* pipeline mints a deterministic source id per file path, persists the
|
|
|
|
|
* source record (the file's provenance + the run's usage), and runs
|
|
|
|
|
* extractor → auditor → merger over the raw text.
|
|
|
|
|
*
|
|
|
|
|
* Small concurrency cap: the model calls are the slow part, and a burst of
|
|
|
|
|
* 25 simultaneous runs would trip provider rate limits — a 3-wide worker
|
|
|
|
|
* pool keeps the run registry (the dock's status chips) and the ledgers
|
|
|
|
|
* healthy. The dock's chips + graph refetch track progress live through
|
|
|
|
|
* /api/graph/status, as with session runs.
|
|
|
|
|
*/
|
2026-10-23 04:43:56 +03:00
|
|
|
import { extraction, NotesImportError } from "@/src/server";
|
2026-09-24 22:51:12 +03:00
|
|
|
import {
|
|
|
|
|
ModelsNotConfiguredError,
|
|
|
|
|
PROVIDER_KEY_MISSING_CODE,
|
|
|
|
|
ProviderKeyMissingError,
|
|
|
|
|
type EmissionStats,
|
|
|
|
|
} from "@/src/core";
|
2026-09-13 02:08:55 +03:00
|
|
|
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
|
|
|
|
interface ImportResult {
|
2026-10-23 04:43:56 +03:00
|
|
|
requestIndex: number;
|
|
|
|
|
outcome: "completed" | "failed" | "partial" | "aborted" | "busy";
|
2026-09-13 02:08:55 +03:00
|
|
|
filePath: string;
|
|
|
|
|
ok: boolean;
|
|
|
|
|
stats?: EmissionStats;
|
|
|
|
|
error?: string;
|
2026-09-24 22:51:12 +03:00
|
|
|
/** The machine discriminator when ok:false is a config refusal — the
|
|
|
|
|
* BYOK models refusal (owner, 2026-09-22) or the provider-key refusal
|
|
|
|
|
* (2026-09-25); the client can open the setup/keys flow off it. */
|
|
|
|
|
code?: "models_not_configured" | (typeof PROVIDER_KEY_MISSING_CODE);
|
2026-09-22 16:58:25 +03:00
|
|
|
missing?: string[];
|
2026-09-24 22:51:12 +03:00
|
|
|
/** The key-refusal specifics (provider_key_missing only). */
|
|
|
|
|
provider?: string;
|
|
|
|
|
slots?: string[];
|
2026-09-13 02:08:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function POST(request: Request) {
|
|
|
|
|
const form = await request.formData();
|
|
|
|
|
const files = form.getAll("files").filter((f): f is File => f instanceof File);
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
|
return Response.json({ results: [] as ImportResult[], error: "no files" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const results: ImportResult[] = [];
|
2026-10-23 04:43:56 +03:00
|
|
|
const queue = files.map((file, requestIndex) => ({ file, requestIndex }));
|
2026-09-13 02:08:55 +03:00
|
|
|
const workers = Array.from({ length: Math.min(3, files.length) }, async () => {
|
|
|
|
|
for (;;) {
|
2026-10-23 04:43:56 +03:00
|
|
|
const next = queue.shift();
|
|
|
|
|
if (!next) return;
|
|
|
|
|
const { file, requestIndex } = next;
|
2026-09-13 02:08:55 +03:00
|
|
|
try {
|
|
|
|
|
const text = await file.text();
|
|
|
|
|
const stats = await extraction.importNotes({ filePath: file.name, text });
|
2026-10-23 04:43:56 +03:00
|
|
|
results.push({ filePath: file.name, requestIndex, outcome: "completed", ok: true, stats });
|
2026-09-13 02:08:55 +03:00
|
|
|
} catch (err) {
|
2026-09-22 16:58:25 +03:00
|
|
|
if (err instanceof ModelsNotConfiguredError) {
|
|
|
|
|
results.push({
|
|
|
|
|
filePath: file.name,
|
|
|
|
|
ok: false,
|
2026-10-23 04:43:56 +03:00
|
|
|
requestIndex, outcome: "failed",
|
2026-09-22 16:58:25 +03:00
|
|
|
error: err.envelope.error,
|
|
|
|
|
code: "models_not_configured",
|
|
|
|
|
missing: err.envelope.missing,
|
|
|
|
|
});
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-09-24 22:51:12 +03:00
|
|
|
if (err instanceof ProviderKeyMissingError) {
|
|
|
|
|
results.push({
|
|
|
|
|
filePath: file.name,
|
|
|
|
|
ok: false,
|
2026-10-23 04:43:56 +03:00
|
|
|
requestIndex, outcome: "failed",
|
2026-09-24 22:51:12 +03:00
|
|
|
error: err.envelope.error,
|
|
|
|
|
code: PROVIDER_KEY_MISSING_CODE,
|
|
|
|
|
provider: err.envelope.provider,
|
|
|
|
|
slots: err.envelope.slots,
|
|
|
|
|
});
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-10-23 04:43:56 +03:00
|
|
|
results.push({
|
|
|
|
|
filePath: file.name, requestIndex, ok: false,
|
|
|
|
|
outcome: err instanceof NotesImportError ? err.outcome : "failed",
|
|
|
|
|
...(err instanceof NotesImportError ? { stats: err.partialStats } : {}),
|
|
|
|
|
error: err instanceof Error ? err.message : String(err),
|
|
|
|
|
});
|
2026-09-13 02:08:55 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
await Promise.all(workers);
|
|
|
|
|
return Response.json({ results });
|
|
|
|
|
}
|