VIBN Frontend for Coolify deployment
This commit is contained in:
73
app/api/sessions/associate-project/route.ts
Normal file
73
app/api/sessions/associate-project/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { adminDb } from '@/lib/firebase/admin';
|
||||
import { FieldValue } from 'firebase-admin/firestore';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { workspacePath, projectId, userId } = body;
|
||||
|
||||
if (!workspacePath || !projectId || !userId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing required fields' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Verify the project belongs to the user
|
||||
const projectDoc = await adminDb.collection('projects').doc(projectId).get();
|
||||
|
||||
if (!projectDoc.exists || projectDoc.data()?.userId !== userId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Project not found or unauthorized' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update all sessions with this workspace path to associate with the project
|
||||
const sessionsSnapshot = await adminDb
|
||||
.collection('sessions')
|
||||
.where('userId', '==', userId)
|
||||
.where('workspacePath', '==', workspacePath)
|
||||
.where('needsProjectAssociation', '==', true)
|
||||
.get();
|
||||
|
||||
const batch = adminDb.batch();
|
||||
let count = 0;
|
||||
|
||||
sessionsSnapshot.docs.forEach((doc: FirebaseFirestore.QueryDocumentSnapshot) => {
|
||||
batch.update(doc.ref, {
|
||||
projectId,
|
||||
needsProjectAssociation: false,
|
||||
updatedAt: FieldValue.serverTimestamp(),
|
||||
});
|
||||
count++;
|
||||
});
|
||||
|
||||
await batch.commit();
|
||||
|
||||
// Update the project's workspace path if not set
|
||||
if (!projectDoc.data()?.workspacePath) {
|
||||
await projectDoc.ref.update({
|
||||
workspacePath,
|
||||
updatedAt: FieldValue.serverTimestamp(),
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
sessionsUpdated: count,
|
||||
message: `Associated ${count} sessions with project`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error associating sessions:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to associate sessions',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
62
app/api/sessions/route.ts
Normal file
62
app/api/sessions/route.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getAdminDb } from '@/lib/firebase/admin';
|
||||
|
||||
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 sessionsSnapshot = await sessionsQuery
|
||||
.orderBy('createdAt', 'desc')
|
||||
.limit(limit)
|
||||
.get();
|
||||
|
||||
const sessions = sessionsSnapshot.docs.map(doc => {
|
||||
const data = doc.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,
|
||||
ide_name: 'Cursor',
|
||||
github_branch: data.githubBranch || null,
|
||||
conversation: data.conversation || [],
|
||||
file_changes: data.fileChanges || [],
|
||||
createdAt: data.createdAt,
|
||||
last_updated: data.updatedAt || data.createdAt,
|
||||
};
|
||||
});
|
||||
|
||||
console.log(`[API] Found ${sessions.length} sessions from Firebase`);
|
||||
return NextResponse.json(sessions);
|
||||
} catch (error) {
|
||||
console.error('[API] Error fetching sessions:', error);
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
}
|
||||
112
app/api/sessions/track/route.ts
Normal file
112
app/api/sessions/track/route.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { adminDb } from '@/lib/firebase/admin';
|
||||
import { FieldValue, Timestamp } from 'firebase-admin/firestore';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { apiKey, sessionData } = body;
|
||||
|
||||
if (!apiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: 'API key is required' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Verify API key and get userId
|
||||
const keyDoc = await adminDb.collection('apiKeys').doc(apiKey).get();
|
||||
|
||||
if (!keyDoc.exists || !keyDoc.data()?.isActive) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid or inactive API key' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const userId = keyDoc.data()!.userId;
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'User not found for API key' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update last used timestamp
|
||||
await keyDoc.ref.update({
|
||||
lastUsed: FieldValue.serverTimestamp(),
|
||||
});
|
||||
|
||||
// Check if workspace has an associated project
|
||||
let projectId = sessionData.projectId || null;
|
||||
let needsProjectAssociation = false;
|
||||
|
||||
if (!projectId && sessionData.workspacePath) {
|
||||
// Try to find a project with this workspace path
|
||||
const projectsSnapshot = await adminDb
|
||||
.collection('projects')
|
||||
.where('userId', '==', userId)
|
||||
.where('workspacePath', '==', sessionData.workspacePath)
|
||||
.limit(1)
|
||||
.get();
|
||||
|
||||
if (!projectsSnapshot.empty) {
|
||||
// Found a matching project, auto-associate
|
||||
projectId = projectsSnapshot.docs[0].id;
|
||||
console.log(`✅ Auto-associated session with project: ${projectId}`);
|
||||
} else {
|
||||
// No matching project found, flag for user action
|
||||
needsProjectAssociation = true;
|
||||
console.log(`⚠️ New workspace detected: ${sessionData.workspacePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Create session document
|
||||
const sessionRef = adminDb.collection('sessions').doc();
|
||||
await sessionRef.set({
|
||||
id: sessionRef.id,
|
||||
userId,
|
||||
projectId,
|
||||
|
||||
// Session data
|
||||
startTime: Timestamp.fromMillis(new Date(sessionData.startTime).getTime()),
|
||||
endTime: sessionData.endTime ? Timestamp.fromMillis(new Date(sessionData.endTime).getTime()) : null,
|
||||
duration: sessionData.duration || null,
|
||||
|
||||
// Project context
|
||||
workspacePath: sessionData.workspacePath || null,
|
||||
workspaceName: sessionData.workspacePath ? sessionData.workspacePath.split('/').pop() : null,
|
||||
needsProjectAssociation,
|
||||
|
||||
// AI usage
|
||||
model: sessionData.model || 'unknown',
|
||||
tokensUsed: sessionData.tokensUsed || 0,
|
||||
cost: sessionData.cost || 0,
|
||||
|
||||
// Context
|
||||
filesModified: sessionData.filesModified || [],
|
||||
conversationSummary: sessionData.conversationSummary || null,
|
||||
conversation: sessionData.conversation || [],
|
||||
messageCount: sessionData.conversation?.length || 0,
|
||||
|
||||
createdAt: FieldValue.serverTimestamp(),
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
sessionId: sessionRef.id,
|
||||
message: 'Session tracked successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error tracking session:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to track session',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user