VIBN Frontend for Coolify deployment
This commit is contained in:
72
app/api/openai/gpts/route.ts
Normal file
72
app/api/openai/gpts/route.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Retrieve ChatGPT GPT information
|
||||
* Note: This is limited by what's available via OpenAI API
|
||||
* GPTs are primarily accessible through the ChatGPT web interface
|
||||
*/
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getAdminAuth } from '@/lib/firebase/admin';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
// Authenticate user
|
||||
const authHeader = request.headers.get('Authorization');
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const idToken = authHeader.split('Bearer ')[1];
|
||||
const adminAuth = getAdminAuth();
|
||||
|
||||
let userId: string;
|
||||
try {
|
||||
const decodedToken = await adminAuth.verifyIdToken(idToken);
|
||||
userId = decodedToken.uid;
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Invalid token' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { gptUrl } = await request.json();
|
||||
|
||||
if (!gptUrl) {
|
||||
return NextResponse.json({ error: 'GPT URL is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Extract GPT ID from URL
|
||||
// Format: https://chatgpt.com/g/g-p-[id]-[name]/project
|
||||
const gptMatch = gptUrl.match(/\/g\/(g-p-[a-zA-Z0-9]+)/);
|
||||
|
||||
if (!gptMatch) {
|
||||
return NextResponse.json({ error: 'Invalid GPT URL format' }, { status: 400 });
|
||||
}
|
||||
|
||||
const gptId = gptMatch[1];
|
||||
const nameMatch = gptUrl.match(/g-p-[a-zA-Z0-9]+-([^\/]+)/);
|
||||
const gptName = nameMatch ? nameMatch[1].replace(/-/g, ' ') : 'Unknown GPT';
|
||||
|
||||
console.log(`[ChatGPT GPT] Extracted ID: ${gptId}, Name: ${gptName}`);
|
||||
|
||||
// Note: OpenAI API doesn't currently expose GPTs directly
|
||||
// We'll store the reference for now and display in the UI
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
id: gptId,
|
||||
name: gptName,
|
||||
url: gptUrl,
|
||||
type: 'chatgpt-gpt',
|
||||
message: 'GPT reference saved. To capture conversations with this GPT, import individual chat sessions.',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[ChatGPT GPT] Error:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to process GPT',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
82
app/api/openai/projects/route.ts
Normal file
82
app/api/openai/projects/route.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Retrieve OpenAI Projects using the Projects API
|
||||
* https://platform.openai.com/docs/api-reference/projects
|
||||
*/
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getAdminAuth } from '@/lib/firebase/admin';
|
||||
|
||||
const OPENAI_API_URL = 'https://api.openai.com/v1/projects';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
// Authenticate user
|
||||
const authHeader = request.headers.get('Authorization');
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const idToken = authHeader.split('Bearer ')[1];
|
||||
const adminAuth = getAdminAuth();
|
||||
|
||||
let userId: string;
|
||||
try {
|
||||
const decodedToken = await adminAuth.verifyIdToken(idToken);
|
||||
userId = decodedToken.uid;
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Invalid token' }, { status: 401 });
|
||||
}
|
||||
|
||||
const { openaiApiKey, projectId } = await request.json();
|
||||
|
||||
if (!openaiApiKey) {
|
||||
return NextResponse.json({ error: 'OpenAI API key is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// If projectId provided, fetch specific project
|
||||
const url = projectId
|
||||
? `${OPENAI_API_URL}/${projectId}`
|
||||
: OPENAI_API_URL; // List all projects
|
||||
|
||||
console.log(`[OpenAI Projects] Fetching: ${url}`);
|
||||
|
||||
const openaiResponse = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${openaiApiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!openaiResponse.ok) {
|
||||
const errorText = await openaiResponse.text();
|
||||
console.error('[OpenAI Projects] API error:', openaiResponse.status, errorText);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to fetch from OpenAI',
|
||||
details: errorText,
|
||||
status: openaiResponse.status,
|
||||
},
|
||||
{ status: openaiResponse.status }
|
||||
);
|
||||
}
|
||||
|
||||
const projectData = await openaiResponse.json();
|
||||
console.log('[OpenAI Projects] Data fetched successfully');
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: projectData,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[OpenAI Projects] Error:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Failed to fetch OpenAI project',
|
||||
details: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user