fix: migrate AI chat system from Firebase/Firestore to Postgres
Firebase was not configured so every chat request crashed with 'Firebase Admin credentials not configured'. - chat-mode-resolver.ts: read project phase from fs_projects (Postgres) - chat-context.ts: load project data from fs_projects instead of Firestore - /api/ai/conversation: store/retrieve conversations in chat_conversations Postgres table (created automatically on first use) - /api/ai/chat: replace all Firestore reads/writes with Postgres queries - v_ai_chat/page.tsx: replace Firebase client auth with useSession from next-auth/react; remove Firestore listeners, use REST API for project data Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,8 +2,7 @@ import { NextResponse } from 'next/server';
|
||||
import { z } from 'zod';
|
||||
import { GeminiLlmClient } from '@/lib/ai/gemini-client';
|
||||
import type { LlmClient } from '@/lib/ai/llm-client';
|
||||
import { getAdminDb } from '@/lib/firebase/admin';
|
||||
import { FieldValue } from 'firebase-admin/firestore';
|
||||
import { query } from '@/lib/db-postgres';
|
||||
import { MODE_SYSTEM_PROMPTS, ChatMode } from '@/lib/ai/chat-modes';
|
||||
import { resolveChatMode } from '@/lib/server/chat-mode-resolver';
|
||||
import {
|
||||
@@ -48,37 +47,30 @@ interface ChatRequestBody {
|
||||
overrideMode?: ChatMode;
|
||||
}
|
||||
|
||||
const ENSURE_CONV_TABLE = `
|
||||
CREATE TABLE IF NOT EXISTS chat_conversations (
|
||||
project_id text PRIMARY KEY,
|
||||
messages jsonb NOT NULL DEFAULT '[]',
|
||||
updated_at timestamptz NOT NULL DEFAULT NOW()
|
||||
)
|
||||
`;
|
||||
|
||||
async function appendConversation(
|
||||
projectId: string,
|
||||
messages: Array<{ role: 'user' | 'assistant'; content: string }>,
|
||||
newMessages: Array<{ role: 'user' | 'assistant'; content: string }>,
|
||||
) {
|
||||
const adminDb = getAdminDb();
|
||||
const docRef = adminDb.collection('chat_conversations').doc(projectId);
|
||||
await query(ENSURE_CONV_TABLE);
|
||||
const now = new Date().toISOString();
|
||||
const stamped = newMessages.map((m) => ({ ...m, createdAt: now }));
|
||||
|
||||
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 = messages.map((m) => ({
|
||||
role: m.role,
|
||||
content: m.content,
|
||||
// Use a simple ISO string for message timestamps to avoid FieldValue
|
||||
// restrictions inside arrays.
|
||||
createdAt: now,
|
||||
}));
|
||||
|
||||
tx.set(
|
||||
docRef,
|
||||
{
|
||||
projectId,
|
||||
messages: [...existing, ...newMessages],
|
||||
updatedAt: FieldValue.serverTimestamp(),
|
||||
},
|
||||
{ merge: true },
|
||||
);
|
||||
});
|
||||
await query(
|
||||
`INSERT INTO chat_conversations (project_id, messages, updated_at)
|
||||
VALUES ($1, $2::jsonb, NOW())
|
||||
ON CONFLICT (project_id) DO UPDATE
|
||||
SET messages = chat_conversations.messages || $2::jsonb,
|
||||
updated_at = NOW()`,
|
||||
[projectId, JSON.stringify(stamped)]
|
||||
);
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
@@ -91,13 +83,15 @@ export async function POST(request: Request) {
|
||||
return NextResponse.json({ error: 'projectId and message are required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Verify project exists
|
||||
const adminDb = getAdminDb();
|
||||
const projectSnapshot = await adminDb.collection('projects').doc(projectId).get();
|
||||
if (!projectSnapshot.exists) {
|
||||
// Verify project exists in Postgres
|
||||
const projectRows = await query<{ data: any }>(
|
||||
`SELECT data FROM fs_projects WHERE id = $1 LIMIT 1`,
|
||||
[projectId]
|
||||
);
|
||||
if (projectRows.length === 0) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
}
|
||||
const projectData = projectSnapshot.data() ?? {};
|
||||
const projectData = projectRows[0].data ?? {};
|
||||
|
||||
// Resolve chat mode (uses new resolver)
|
||||
const resolvedMode = body.overrideMode ?? await resolveChatMode(projectId);
|
||||
@@ -138,12 +132,13 @@ ${context.sessionHistory.totalSessions > 0 ? `- Complete Cursor session history
|
||||
|
||||
Use this context to provide specific, grounded responses. The session history shows your complete conversation history with the user - use it to understand what has been built and discussed.`;
|
||||
|
||||
// Load existing conversation history
|
||||
const conversationDoc = await adminDb.collection('chat_conversations').doc(projectId).get();
|
||||
const conversationData = conversationDoc.exists ? conversationDoc.data() : null;
|
||||
const conversationHistory = Array.isArray(conversationData?.messages)
|
||||
? conversationData.messages
|
||||
: [];
|
||||
// Load existing conversation history from Postgres
|
||||
await query(ENSURE_CONV_TABLE);
|
||||
const convRows = await query<{ messages: any[] }>(
|
||||
`SELECT messages FROM chat_conversations WHERE project_id = $1`,
|
||||
[projectId]
|
||||
);
|
||||
const conversationHistory: any[] = convRows[0]?.messages ?? [];
|
||||
|
||||
// Build full message context (history + current message)
|
||||
const messages = [
|
||||
@@ -251,10 +246,14 @@ Use this context to provide specific, grounded responses. The session history sh
|
||||
if (Object.keys(updates).length > 0) {
|
||||
updates['visionAnswers.updatedAt'] = new Date().toISOString();
|
||||
|
||||
await adminDb.collection('projects').doc(projectId).set(updates, { merge: true })
|
||||
.catch((error) => {
|
||||
console.error('[ai/chat] Failed to store vision answers', error);
|
||||
});
|
||||
await query(
|
||||
`UPDATE fs_projects
|
||||
SET data = data || $1::jsonb
|
||||
WHERE id = $2`,
|
||||
[JSON.stringify({ visionAnswers: updates }), projectId]
|
||||
).catch((error) => {
|
||||
console.error('[ai/chat] Failed to store vision answers', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,12 +325,17 @@ Use this context to provide specific, grounded responses. The session history sh
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Persist to project phaseData
|
||||
await adminDb.collection('projects').doc(projectId).set(
|
||||
{
|
||||
'phaseData.phaseHandoffs.collector': handoff,
|
||||
},
|
||||
{ merge: true }
|
||||
// Persist to project phaseData in Postgres
|
||||
await query(
|
||||
`UPDATE fs_projects
|
||||
SET data = jsonb_set(
|
||||
data,
|
||||
'{phaseData,phaseHandoffs,collector}',
|
||||
$1::jsonb,
|
||||
true
|
||||
)
|
||||
WHERE id = $2`,
|
||||
[JSON.stringify(handoff), projectId]
|
||||
).catch((error) => {
|
||||
console.error('[ai/chat] Failed to persist collector handoff', error);
|
||||
});
|
||||
@@ -348,9 +352,12 @@ Use this context to provide specific, grounded responses. The session history sh
|
||||
console.log(`[AI Chat] Collector complete - triggering backend extraction`);
|
||||
|
||||
// Mark collector as complete
|
||||
await adminDb.collection('projects').doc(projectId).update({
|
||||
'phaseData.collectorCompletedAt': new Date().toISOString(),
|
||||
}).catch((error) => {
|
||||
await query(
|
||||
`UPDATE fs_projects
|
||||
SET data = jsonb_set(data, '{phaseData,collectorCompletedAt}', $1::jsonb, true)
|
||||
WHERE id = $2`,
|
||||
[JSON.stringify(new Date().toISOString()), projectId]
|
||||
).catch((error) => {
|
||||
console.error('[ai/chat] Failed to mark collector complete', error);
|
||||
});
|
||||
|
||||
@@ -397,44 +404,32 @@ Use this context to provide specific, grounded responses. The session history sh
|
||||
console.log(`[AI Chat] Extraction review complete - transitioning to vision phase`);
|
||||
|
||||
// Mark extraction review as complete and transition to vision
|
||||
await adminDb.collection('projects').doc(projectId).update({
|
||||
currentPhase: 'vision',
|
||||
phaseStatus: 'in_progress',
|
||||
'phaseData.extractionReviewCompletedAt': new Date().toISOString(),
|
||||
}).catch((error) => {
|
||||
await query(
|
||||
`UPDATE fs_projects
|
||||
SET data = data
|
||||
|| '{"currentPhase":"vision","phaseStatus":"in_progress"}'::jsonb
|
||||
|| jsonb_build_object('phaseData',
|
||||
(data->'phaseData') || jsonb_build_object(
|
||||
'extractionReviewCompletedAt', $1::text
|
||||
)
|
||||
)
|
||||
WHERE id = $2`,
|
||||
[new Date().toISOString(), projectId]
|
||||
).catch((error) => {
|
||||
console.error('[ai/chat] Failed to transition to vision phase', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Save conversation history
|
||||
const newConversationHistory = [
|
||||
...conversationHistory,
|
||||
{
|
||||
role: 'user' as const,
|
||||
content: message,
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
role: 'assistant' as const,
|
||||
content: reply.reply,
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
|
||||
await adminDb.collection('chat_conversations').doc(projectId).set(
|
||||
{
|
||||
projectId,
|
||||
userId: projectData.userId,
|
||||
messages: newConversationHistory,
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
{ merge: true }
|
||||
).catch((error) => {
|
||||
// Save conversation history to Postgres
|
||||
await appendConversation(projectId, [
|
||||
{ role: 'user', content: message },
|
||||
{ role: 'assistant', content: reply.reply },
|
||||
]).catch((error) => {
|
||||
console.error('[ai/chat] Failed to save conversation history', error);
|
||||
});
|
||||
|
||||
console.log(`[AI Chat] Conversation history saved (${newConversationHistory.length} total messages)`);
|
||||
console.log(`[AI Chat] Conversation history saved (+2 messages)`);
|
||||
|
||||
// Determine which artifacts were used
|
||||
const artifactsUsed = determineArtifactsUsed(context);
|
||||
|
||||
Reference in New Issue
Block a user