64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getAdminDb } from '@/lib/firebase/admin';
|
|
import { FieldValue } from 'firebase-admin/firestore';
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const body = await request.json().catch(() => ({}));
|
|
const projectId = (body.projectId ?? '').trim();
|
|
|
|
if (!projectId) {
|
|
return NextResponse.json(
|
|
{ error: 'projectId is required' },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const adminDb = getAdminDb();
|
|
const docRef = adminDb.collection('chat_conversations').doc(projectId);
|
|
|
|
await adminDb.runTransaction(async (tx) => {
|
|
const snapshot = await tx.get(docRef);
|
|
const existing = (snapshot.exists ? (snapshot.data()?.messages as unknown[]) : []) ?? [];
|
|
|
|
const now = new Date().toISOString();
|
|
|
|
const newMessages = [
|
|
{
|
|
role: 'user' as const,
|
|
content: '[debug] test user message',
|
|
createdAt: now,
|
|
},
|
|
{
|
|
role: 'assistant' as const,
|
|
content: '[debug] test assistant reply',
|
|
createdAt: now,
|
|
},
|
|
];
|
|
|
|
tx.set(
|
|
docRef,
|
|
{
|
|
projectId,
|
|
messages: [...existing, ...newMessages],
|
|
updatedAt: FieldValue.serverTimestamp(),
|
|
},
|
|
{ merge: true },
|
|
);
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('[debug/append-conversation] Failed to append messages', error);
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Failed to append debug conversation messages',
|
|
details: error instanceof Error ? error.message : String(error),
|
|
},
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
}
|
|
|
|
|