System AI

This commit is contained in:
Joey Yakimowich-Payne 2026-01-15 19:39:38 -07:00
commit a7ad1e9bba
No known key found for this signature in database
GPG key ID: 6BFE655FA5ABD1E1
10 changed files with 271 additions and 13 deletions

View file

@ -406,9 +406,61 @@ async function generateQuizWithOpenAI(options: GenerateQuizOptions): Promise<Qui
return transformToQuiz(data);
}
const API_URL = import.meta.env.VITE_API_URL || '';
async function generateQuizWithServer(options: GenerateQuizOptions): Promise<Quiz> {
if (!options.accessToken) {
throw new Error("Authentication required for system AI");
}
const docs = options.documents || [];
const documentsPayload = docs.map(doc => ({
type: doc.type,
content: doc.type === 'native' && doc.content instanceof ArrayBuffer
? btoa(String.fromCharCode(...new Uint8Array(doc.content)))
: doc.content,
mimeType: doc.mimeType
}));
const response = await fetch(`${API_URL}/api/generate`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${options.accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
topic: options.topic,
questionCount: options.questionCount || 10,
documents: documentsPayload
})
});
if (!response.ok) {
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
throw new Error(error.error || `Server error: ${response.status}`);
}
return response.json();
}
export async function checkSystemAIAvailable(): Promise<boolean> {
try {
const response = await fetch(`${API_URL}/api/generate/status`);
if (!response.ok) return false;
const data = await response.json();
return data.available === true;
} catch {
return false;
}
}
export const generateQuiz = async (options: GenerateQuizOptions): Promise<Quiz> => {
const provider = options.aiProvider || 'gemini';
if (provider === 'system') {
return generateQuizWithServer(options);
}
if (provider === 'openrouter') {
return generateQuizWithOpenRouter(options);
}