Working prototype
This commit is contained in:
1 parent
a1f6dd7b08
commit
5cd6f3c640
20 files changed
+2392
-151
No files matched your search
@@ -0,0 +1,33 @@
|
||||
import { createAnalysisStream, toUserLlmError } from "@/lib/llm";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { analysisSchema } from "@/lib/validation";
|
||||
|
||||
type Event = { type: "token"; value: string } | { type: "done"; summary: string; tags: string[] } | { type: "error"; error: string };
|
||||
const encoder = new TextEncoder();
|
||||
const send = (controller: ReadableStreamDefaultController<Uint8Array>, event: Event) => controller.enqueue(encoder.encode(`${JSON.stringify(event)}\n`));
|
||||
|
||||
export async function POST(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await context.params;
|
||||
const note = await prisma.note.findUnique({ where: { id } });
|
||||
if (!note) return Response.json({ error: "Заметка не найдена." }, { status: 404 });
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
let fullResponse = "";
|
||||
try {
|
||||
const completion = await createAnalysisStream(note.content, request.signal);
|
||||
for await (const chunk of completion) {
|
||||
if (request.signal.aborted) throw new DOMException("Aborted", "AbortError");
|
||||
const delta = chunk.choices[0]?.delta.content ?? "";
|
||||
if (delta) { fullResponse += delta; send(controller, { type: "token", value: delta }); }
|
||||
}
|
||||
const analysis = analysisSchema.safeParse(JSON.parse(fullResponse) as unknown);
|
||||
if (!analysis.success) throw new Error("Некорректный формат ответа LLM.");
|
||||
await prisma.note.update({ where: { id }, data: { summary: analysis.data.summary, tags: JSON.stringify(analysis.data.tags) } });
|
||||
send(controller, { type: "done", ...analysis.data });
|
||||
} catch (error) {
|
||||
send(controller, { type: "error", error: error instanceof Error && error.message === "Некорректный формат ответа LLM." ? error.message : toUserLlmError(error) });
|
||||
} finally { controller.close(); }
|
||||
},
|
||||
});
|
||||
return new Response(stream, { headers: { "Content-Type": "application/x-ndjson; charset=utf-8", "Cache-Control": "no-cache, no-transform", Connection: "keep-alive" } });
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { toNoteDto } from "@/lib/notes";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { createNoteSchema } from "@/lib/validation";
|
||||
|
||||
export async function GET() {
|
||||
const notes = await prisma.note.findMany({ orderBy: { createdAt: "desc" } });
|
||||
return Response.json(notes.map(toNoteDto));
|
||||
}
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const input = createNoteSchema.safeParse(await request.json());
|
||||
if (!input.success) return Response.json({ error: input.error.issues[0]?.message ?? "Некорректные данные." }, { status: 400 });
|
||||
const note = await prisma.note.create({ data: input.data });
|
||||
return Response.json(toNoteDto(note), { status: 201 });
|
||||
} catch { return Response.json({ error: "Некорректный JSON-запрос." }, { status: 400 }); }
|
||||
}
|
||||
Reference in new issue
Block a user