Fix chat API to work with database
- Use sql template literals instead of unsafe queries - Handle UUID child_id requirement properly - Fetch messages separately to avoid GROUP BY issues Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
f8df3ce313
commit
2d61ba2afc
1 changed files with 30 additions and 31 deletions
|
|
@ -7,19 +7,28 @@ export async function GET(request: Request) {
|
|||
const childId = searchParams.get("childId") || "default";
|
||||
|
||||
try {
|
||||
const sessions = await sql.unsafe(
|
||||
`SELECT cs.id, cs.title, cs.created_at as "createdAt", cs.updated_at as "updatedAt",
|
||||
COALESCE(
|
||||
(SELECT json_agg(json_build_object('id', cm.id, 'role', cm.role, 'content', cm.content, 'createdAt', cm.created_at))
|
||||
FROM chat_messages cm WHERE cm.session_id = cs.id ORDER BY cm.created_at),
|
||||
'[]'::json
|
||||
) as messages
|
||||
FROM chat_sessions cs
|
||||
WHERE cs.child_id = $1
|
||||
ORDER BY cs.updated_at DESC`,
|
||||
[childId]
|
||||
// Fetch sessions
|
||||
const sessions = await sql`
|
||||
SELECT id, title, created_at as "createdAt", updated_at as "updatedAt"
|
||||
FROM chat_sessions
|
||||
WHERE child_id = ${childId}
|
||||
ORDER BY updated_at DESC
|
||||
`;
|
||||
|
||||
// Fetch messages for each session
|
||||
const sessionsWithMessages = await Promise.all(
|
||||
(sessions || []).map(async (session: any) => {
|
||||
const messages = await sql`
|
||||
SELECT id, role, content, created_at as "createdAt"
|
||||
FROM chat_messages
|
||||
WHERE session_id = ${session.id}
|
||||
ORDER BY created_at
|
||||
`;
|
||||
return { ...session, messages: messages || [] };
|
||||
})
|
||||
);
|
||||
return NextResponse.json({ sessions: sessions || [] });
|
||||
|
||||
return NextResponse.json({ sessions: sessionsWithMessages || [] });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||
|
|
@ -36,12 +45,11 @@ export async function POST(request: Request) {
|
|||
return NextResponse.json({ error: "childId required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const [session] = await sql.unsafe(
|
||||
`INSERT INTO chat_sessions (child_id, title)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, title, created_at as "createdAt", updated_at as "updatedAt"`,
|
||||
[childId, title || "New conversation"]
|
||||
);
|
||||
const [session] = await sql`
|
||||
INSERT INTO chat_sessions (child_id, title)
|
||||
VALUES (${childId}, ${title || "New conversation"})
|
||||
RETURNING id, title, created_at as "createdAt", updated_at as "updatedAt"
|
||||
`;
|
||||
|
||||
return NextResponse.json({ success: true, session });
|
||||
} catch (error) {
|
||||
|
|
@ -61,21 +69,12 @@ export async function PATCH(request: Request) {
|
|||
}
|
||||
|
||||
if (title) {
|
||||
await sql.unsafe(
|
||||
`UPDATE chat_sessions SET title = $1, updated_at = NOW() WHERE id = $2`,
|
||||
[title, sessionId]
|
||||
);
|
||||
await sql`UPDATE chat_sessions SET title = ${title}, updated_at = NOW() WHERE id = ${sessionId}`;
|
||||
}
|
||||
|
||||
if (role && content) {
|
||||
await sql.unsafe(
|
||||
`INSERT INTO chat_messages (session_id, role, content) VALUES ($1, $2, $3)`,
|
||||
[sessionId, role, content]
|
||||
);
|
||||
await sql.unsafe(
|
||||
`UPDATE chat_sessions SET updated_at = NOW() WHERE id = $1`,
|
||||
[sessionId]
|
||||
);
|
||||
await sql`INSERT INTO chat_messages (session_id, role, content) VALUES (${sessionId}, ${role}, ${content})`;
|
||||
await sql`UPDATE chat_sessions SET updated_at = NOW() WHERE id = ${sessionId}`;
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
|
|
@ -95,7 +94,7 @@ export async function DELETE(request: Request) {
|
|||
}
|
||||
|
||||
try {
|
||||
await sql.unsafe(`DELETE FROM chat_sessions WHERE id = $1`, [id]);
|
||||
await sql`DELETE FROM chat_sessions WHERE id = ${id}`;
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue