migrate: replace Firebase with PostgreSQL across core routes
- chat-context.ts: session history now from fs_sessions - /api/sessions: reads from fs_sessions (NextAuth session auth) - /api/github/connect: NextAuth session + stores in fs_users.data - /api/user/api-key: NextAuth session + stores in fs_users.data - /api/projects/[id]/vision: PATCH to fs_projects JSONB - /api/projects/[id]/knowledge/items: reads from fs_knowledge_items - /api/projects/[id]/knowledge/import-ai-chat: uses pg createKnowledgeItem - lib/server/knowledge.ts: fully rewritten to use PostgreSQL - entrypoint.sh: add fs_knowledge_items and chat_conversations tables Made-with: Cursor
This commit is contained in:
@@ -205,17 +205,15 @@ export async function buildProjectContextForChat(
|
||||
};
|
||||
|
||||
try {
|
||||
// Query sessions linked to this project
|
||||
const sessionsSnapshot = await adminDb
|
||||
.collection('sessions')
|
||||
.where('projectId', '==', projectId)
|
||||
.orderBy('startTime', 'asc')
|
||||
.get();
|
||||
// Query sessions linked to this project from PostgreSQL
|
||||
const sessionRows = await query<{ id: string; data: any }>(
|
||||
`SELECT id, data FROM fs_sessions WHERE data->>'projectId' = $1 ORDER BY created_at ASC`,
|
||||
[projectId]
|
||||
);
|
||||
|
||||
if (sessionRows.length > 0) {
|
||||
sessionHistory.totalSessions = sessionRows.length;
|
||||
|
||||
if (!sessionsSnapshot.empty) {
|
||||
sessionHistory.totalSessions = sessionsSnapshot.size;
|
||||
|
||||
// Extract all messages from all sessions in chronological order
|
||||
const allMessages: Array<{
|
||||
role: string;
|
||||
content: string;
|
||||
@@ -223,32 +221,27 @@ export async function buildProjectContextForChat(
|
||||
sessionId: string;
|
||||
}> = [];
|
||||
|
||||
for (const sessionDoc of sessionsSnapshot.docs) {
|
||||
const sessionData = sessionDoc.data();
|
||||
const conversation = sessionData.conversation || [];
|
||||
|
||||
// Add messages from this session
|
||||
for (const row of sessionRows) {
|
||||
const conversation = row.data?.conversation || [];
|
||||
for (const msg of conversation) {
|
||||
if (msg.content && msg.content.trim()) {
|
||||
allMessages.push({
|
||||
role: msg.role || 'unknown',
|
||||
content: msg.content,
|
||||
timestamp: msg.timestamp instanceof Date
|
||||
? msg.timestamp.toISOString()
|
||||
: (typeof msg.timestamp === 'string' ? msg.timestamp : new Date().toISOString()),
|
||||
sessionId: sessionDoc.id,
|
||||
timestamp: typeof msg.timestamp === 'string'
|
||||
? msg.timestamp
|
||||
: new Date().toISOString(),
|
||||
sessionId: row.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort all messages by timestamp (chronological order)
|
||||
allMessages.sort((a, b) =>
|
||||
allMessages.sort((a, b) =>
|
||||
new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime()
|
||||
);
|
||||
|
||||
sessionHistory.messages = allMessages;
|
||||
|
||||
console.log(
|
||||
`[Chat Context] Loaded ${sessionHistory.totalSessions} sessions with ${allMessages.length} total messages for project ${projectId}`
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user