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:
@@ -1,59 +1,72 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getAdminDb } from '@/lib/firebase/admin';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '@/lib/auth/authOptions';
|
||||
import { query } from '@/lib/db-postgres';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const projectId = searchParams.get('projectId');
|
||||
const limit = parseInt(searchParams.get('limit') || '10');
|
||||
|
||||
console.log(`[API] Fetching sessions for project ${projectId}, limit ${limit}`);
|
||||
|
||||
const adminDb = getAdminDb();
|
||||
let sessionsQuery = adminDb.collection('sessions');
|
||||
|
||||
// Filter by projectId if provided
|
||||
if (projectId) {
|
||||
sessionsQuery = sessionsQuery.where('projectId', '==', projectId) as any;
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user?.email) {
|
||||
return NextResponse.json([], { status: 200 });
|
||||
}
|
||||
|
||||
const sessionsSnapshot = await sessionsQuery
|
||||
.orderBy('createdAt', 'desc')
|
||||
.limit(limit)
|
||||
.get();
|
||||
const { searchParams } = new URL(request.url);
|
||||
const projectId = searchParams.get('projectId');
|
||||
const limit = parseInt(searchParams.get('limit') || '50');
|
||||
|
||||
const sessions = sessionsSnapshot.docs.map(doc => {
|
||||
const data = doc.data();
|
||||
let rows: any[];
|
||||
if (projectId) {
|
||||
rows = await query<any>(
|
||||
`SELECT s.id, s.data, s.created_at
|
||||
FROM fs_sessions s
|
||||
JOIN fs_users u ON u.id = s.user_id
|
||||
WHERE u.data->>'email' = $1 AND s.data->>'projectId' = $2
|
||||
ORDER BY s.created_at DESC LIMIT $3`,
|
||||
[session.user.email, projectId, limit]
|
||||
);
|
||||
} else {
|
||||
rows = await query<any>(
|
||||
`SELECT s.id, s.data, s.created_at
|
||||
FROM fs_sessions s
|
||||
JOIN fs_users u ON u.id = s.user_id
|
||||
WHERE u.data->>'email' = $1
|
||||
ORDER BY s.created_at DESC LIMIT $2`,
|
||||
[session.user.email, limit]
|
||||
);
|
||||
}
|
||||
|
||||
const sessions = rows.map((row: any) => {
|
||||
const d = row.data ?? {};
|
||||
return {
|
||||
id: doc.id,
|
||||
session_id: doc.id,
|
||||
projectId: data.projectId,
|
||||
userId: data.userId,
|
||||
workspacePath: data.workspacePath,
|
||||
workspaceName: data.workspaceName,
|
||||
startTime: data.startTime,
|
||||
endTime: data.endTime,
|
||||
duration: data.duration,
|
||||
duration_minutes: data.duration ? Math.round(data.duration / 60) : 0,
|
||||
tokensUsed: data.tokensUsed || 0,
|
||||
total_tokens: data.tokensUsed || 0,
|
||||
cost: data.cost || 0,
|
||||
estimated_cost_usd: data.cost || 0,
|
||||
model: data.model || 'unknown',
|
||||
primary_ai_model: data.model || 'unknown',
|
||||
filesModified: data.filesModified || [],
|
||||
summary: data.conversationSummary || null,
|
||||
message_count: data.messageCount || 0,
|
||||
id: row.id,
|
||||
session_id: row.id,
|
||||
projectId: d.projectId,
|
||||
userId: d.userId,
|
||||
workspacePath: d.workspacePath,
|
||||
workspaceName: d.workspaceName,
|
||||
startTime: d.startTime,
|
||||
endTime: d.endTime,
|
||||
duration: d.duration,
|
||||
duration_minutes: d.duration ? Math.round(d.duration / 60) : 0,
|
||||
tokensUsed: d.tokensUsed || 0,
|
||||
total_tokens: d.tokensUsed || 0,
|
||||
cost: d.cost || 0,
|
||||
estimated_cost_usd: d.cost || 0,
|
||||
model: d.model || 'unknown',
|
||||
primary_ai_model: d.model || 'unknown',
|
||||
filesModified: d.filesModified || [],
|
||||
summary: d.conversationSummary || null,
|
||||
message_count: d.messageCount || 0,
|
||||
ide_name: 'Cursor',
|
||||
github_branch: data.githubBranch || null,
|
||||
conversation: data.conversation || [],
|
||||
file_changes: data.fileChanges || [],
|
||||
createdAt: data.createdAt,
|
||||
last_updated: data.updatedAt || data.createdAt,
|
||||
github_branch: d.githubBranch || null,
|
||||
conversation: d.conversation || [],
|
||||
file_changes: d.fileChanges || [],
|
||||
createdAt: row.created_at,
|
||||
last_updated: d.updatedAt || row.created_at,
|
||||
};
|
||||
});
|
||||
|
||||
console.log(`[API] Found ${sessions.length} sessions from Firebase`);
|
||||
console.log(`[API] Found ${sessions.length} sessions from PostgreSQL`);
|
||||
return NextResponse.json(sessions);
|
||||
} catch (error) {
|
||||
console.error('[API] Error fetching sessions:', error);
|
||||
|
||||
Reference in New Issue
Block a user