Owner decision 2026-09-22: 'it doesn't make sense to have any default
model configured anywhere; the application is bring your own keys.'
config.ts: MODELS deleted; ModelRole becomes an explicit 9-role union;
DEFAULT_THINKING_ORDER (medium, low, high, minimal, xhigh, max) bakes
the unset-level default — first entry the model supports; explicitly
set levels keep pi's native nearest-clamp.
core/settings-resolution: the factory layer dissolves — the chain is
role override → slot → NOTHING. resolveSettings(record) takes no
factory. Empty chains resolve to null/undefined; a thinkingLevel-only
slot is inert (stored, never run). resolveConversationSpec: chat.model
override over the strong slot, null when both empty. resolveRoleSpec
spans all nine roles for the job/extraction engines.
Refusal envelope (core/settings.ts): modelsNotConfigured(missing) —
{error, code: 'models_not_configured', missing: [slot tiers]} +
ModelsNotConfiguredError with .envelope. Per-need gates, each honest:
- chat-handler: universal turn gate (new + continuing turns) returns
503 before beginTurn — no liveness, no write-ahead persist, no graph
trigger before the gate fires.
- runner.runJob: unresolvable engine lands the job as error with the
envelope wording; no phantom loop, no result steer.
- extraction: refuseIfStagesUnconfigured throws before any stage
(turn-service info-skips so coverage retries next turn; the import
route surfaces per-file code+missing); labeler-less runs skip
cluster naming with a console line and still merge.
Write side: slot entries must carry a model — level-only slot writes
are 400s (absent slot key = clear). piEngineForSpec resolves unset
thinking levels via DEFAULT_THINKING_ORDER; piEngineFor now throws
honestly; thinkingLevelFor/modelIdFor deleted; teacher-agent, pi-run,
visual-maker, pdf-reader all read engine.level.
model-catalog: defaults/slots/rolePins null when the chain is empty
(contracts nullable) — no phantom ids in payloads. Stores drop
MODEL_PINS entirely.
Tests: pin-leaners converted to explicit injection (per-chat model
pins on scripted chats — exactly what the setup flow writes; faux
engines for agent-construction tests). New coverage: the 503 gate
envelope, the runner refusal, the extraction refusal + labeler skip.
Gates: typecheck, lint, 815/815, build.
66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
/**
|
|
* 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.
|
|
*/
|
|
import { extraction } from "@/src/server";
|
|
import { ModelsNotConfiguredError, type EmissionStats } from "@/src/core";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
interface ImportResult {
|
|
filePath: string;
|
|
ok: boolean;
|
|
stats?: EmissionStats;
|
|
error?: string;
|
|
/** The machine discriminator when ok:false is the BYOK refusal (owner,
|
|
* 2026-09-22) — the client can open the setup flow off it. */
|
|
code?: "models_not_configured";
|
|
missing?: string[];
|
|
}
|
|
|
|
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[] = [];
|
|
const queue = [...files];
|
|
const workers = Array.from({ length: Math.min(3, files.length) }, async () => {
|
|
for (;;) {
|
|
const file = queue.shift();
|
|
if (!file) return;
|
|
try {
|
|
const text = await file.text();
|
|
const stats = await extraction.importNotes({ filePath: file.name, text });
|
|
results.push({ filePath: file.name, ok: true, stats });
|
|
} catch (err) {
|
|
if (err instanceof ModelsNotConfiguredError) {
|
|
results.push({
|
|
filePath: file.name,
|
|
ok: false,
|
|
error: err.envelope.error,
|
|
code: "models_not_configured",
|
|
missing: err.envelope.missing,
|
|
});
|
|
continue;
|
|
}
|
|
results.push({ filePath: file.name, ok: false, error: err instanceof Error ? err.message : String(err) });
|
|
}
|
|
}
|
|
});
|
|
await Promise.all(workers);
|
|
return Response.json({ results });
|
|
} |