feat: suporte para provedor OpenRouter

This commit is contained in:
2026-06-22 20:06:40 +00:00
parent f41c5eccd2
commit ba10367855
4 changed files with 95 additions and 4 deletions
+55
View File
@@ -349,6 +349,8 @@ export const analyzeCertificate = async (options: AnalyzeOptions): Promise<Repor
return analyzeWithAzure(file, apiKey, endpoint!, model);
case 'ollama':
return analyzeWithOllama(file, endpoint!, model);
case 'openrouter':
return analyzeWithOpenRouter(file, apiKey, model);
default:
throw new Error(`Provedor não suportado: ${provider}`);
}
@@ -421,5 +423,58 @@ export const analyzeWithOllama = async (file: File, endpoint: string, model: str
throw new Error("Resposta vazia do Ollama/OpenWebUI");
}
return cleanAndParseJson(content) as ReportData;
};
export const analyzeWithOpenRouter = async (file: File, apiKey: string, model: string = 'google/gemini-2.5-flash'): Promise<ReportData> => {
if (!apiKey) {
throw new Error("A chave de API do OpenRouter não foi fornecida.");
}
const base64Data = await new Promise<string>((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve((reader.result as string).split(',')[1]);
reader.readAsDataURL(file);
});
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://steelcheck.local',
'X-Title': 'SteelCheck',
},
body: JSON.stringify({
model,
messages: [
{
role: 'user',
content: [
{ type: 'text', text: PROMPT_BASE + "\n\nRetorne apenas JSON válido sem formatação markdown." },
{
type: 'image_url',
image_url: { url: `data:${file.type};base64,${base64Data}` }
}
]
}
],
temperature: 0.1,
response_format: { type: 'json_object' }
})
});
if (!response.ok) {
const error = await response.json();
throw new Error(`Erro do OpenRouter: ${error.error?.message || 'Erro desconhecido'}`);
}
const data = await response.json();
const content = data.choices[0]?.message?.content;
if (!content) {
throw new Error("Resposta vazia do OpenRouter");
}
return cleanAndParseJson(content) as ReportData;
};