dwijverma2's picture
Add API: apply-changes route
bda1702 verified
/**
* POST /api/apply-changes
*
* Lets the user manually override the LLM's heading classifications.
* Accepts the user's corrected heading indices and regenerates the enriched docx.
*
* Body: {
* sessionId: string,
* headingIndices: number[], // paragraph indices to mark as headings
* headingFontSize?: number // font size in pt, default 18
* }
*/
import { NextResponse } from "next/server";
import { applyHeadingChanges } from "@/lib/docx-service";
import fileStore from "@/lib/file-store";
export async function POST(request) {
try {
const body = await request.json();
const { sessionId, headingIndices, headingFontSize = 18 } = body;
if (!sessionId) {
return NextResponse.json(
{ error: "sessionId is required" },
{ status: 400 }
);
}
if (!Array.isArray(headingIndices)) {
return NextResponse.json(
{ error: "headingIndices must be an array of paragraph indices" },
{ status: 400 }
);
}
const session = fileStore.get(sessionId);
if (!session) {
return NextResponse.json(
{ error: "Session not found or expired. Please re-upload the file." },
{ status: 404 }
);
}
// Build changes from user-specified heading indices
const headingChanges = headingIndices.map((index) => ({
index,
headingFontSize,
bold: true,
}));
// Apply formatting changes
const enrichedBuffer = applyHeadingChanges(
session.originalBuffer,
headingChanges
);
// Update the session
const updatedClassifications = (session.paragraphs || []).map((p) => ({
index: p.index,
text: p.text,
isHeading: headingIndices.includes(p.index),
}));
fileStore.update(sessionId, {
enrichedBuffer: enrichedBuffer,
headingIndices: headingIndices,
classifications: updatedClassifications,
headingFontSize: headingFontSize,
});
return NextResponse.json({
sessionId,
classifications: updatedClassifications,
headingsApplied: headingIndices.length,
headingFontSize,
});
} catch (err) {
console.error("Apply changes error:", err);
return NextResponse.json(
{ error: "Failed to apply changes: " + err.message },
{ status: 500 }
);
}
}