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";
|
const childId = searchParams.get("childId") || "default";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const sessions = await sql.unsafe(
|
// Fetch sessions
|
||||||
`SELECT cs.id, cs.title, cs.created_at as "createdAt", cs.updated_at as "updatedAt",
|
const sessions = await sql`
|
||||||
COALESCE(
|
SELECT id, title, created_at as "createdAt", updated_at as "updatedAt"
|
||||||
(SELECT json_agg(json_build_object('id', cm.id, 'role', cm.role, 'content', cm.content, 'createdAt', cm.created_at))
|
FROM chat_sessions
|
||||||
FROM chat_messages cm WHERE cm.session_id = cs.id ORDER BY cm.created_at),
|
WHERE child_id = ${childId}
|
||||||
'[]'::json
|
ORDER BY updated_at DESC
|
||||||
) as messages
|
`;
|
||||||
FROM chat_sessions cs
|
|
||||||
WHERE cs.child_id = $1
|
// Fetch messages for each session
|
||||||
ORDER BY cs.updated_at DESC`,
|
const sessionsWithMessages = await Promise.all(
|
||||||
[childId]
|
(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) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
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 });
|
return NextResponse.json({ error: "childId required" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const [session] = await sql.unsafe(
|
const [session] = await sql`
|
||||||
`INSERT INTO chat_sessions (child_id, title)
|
INSERT INTO chat_sessions (child_id, title)
|
||||||
VALUES ($1, $2)
|
VALUES (${childId}, ${title || "New conversation"})
|
||||||
RETURNING id, title, created_at as "createdAt", updated_at as "updatedAt"`,
|
RETURNING id, title, created_at as "createdAt", updated_at as "updatedAt"
|
||||||
[childId, title || "New conversation"]
|
`;
|
||||||
);
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true, session });
|
return NextResponse.json({ success: true, session });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -61,21 +69,12 @@ export async function PATCH(request: Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (title) {
|
if (title) {
|
||||||
await sql.unsafe(
|
await sql`UPDATE chat_sessions SET title = ${title}, updated_at = NOW() WHERE id = ${sessionId}`;
|
||||||
`UPDATE chat_sessions SET title = $1, updated_at = NOW() WHERE id = $2`,
|
|
||||||
[title, sessionId]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (role && content) {
|
if (role && content) {
|
||||||
await sql.unsafe(
|
await sql`INSERT INTO chat_messages (session_id, role, content) VALUES (${sessionId}, ${role}, ${content})`;
|
||||||
`INSERT INTO chat_messages (session_id, role, content) VALUES ($1, $2, $3)`,
|
await sql`UPDATE chat_sessions SET updated_at = NOW() WHERE id = ${sessionId}`;
|
||||||
[sessionId, role, content]
|
|
||||||
);
|
|
||||||
await sql.unsafe(
|
|
||||||
`UPDATE chat_sessions SET updated_at = NOW() WHERE id = $1`,
|
|
||||||
[sessionId]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
return NextResponse.json({ success: true });
|
||||||
|
|
@ -95,7 +94,7 @@ export async function DELETE(request: Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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 });
|
return NextResponse.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue