import { GoogleGenAI, Type } from "@google/genai"; import type { AIProvider } from '../types/providers'; import type { ReportData } from '../types'; interface AnalyzeOptions { provider: AIProvider; apiKey: string; model: string; file: File; endpoint?: string; } const fileToGenerativePart = async (file: File) => { const base64EncodedDataPromise = new Promise((resolve) => { const reader = new FileReader(); reader.onloadend = () => resolve((reader.result as string).split(',')[1]); reader.readAsDataURL(file); }); return { inlineData: { data: await base64EncodedDataPromise, mimeType: file.type }, }; }; const cleanAndParseJson = (jsonString: string): any => { let cleanedString = jsonString.trim(); // Tenta encontrar um bloco markdown com json const jsonMatch = cleanedString.match(/```(?:json)?\s*([\s\S]*?)\s*```/i); if (jsonMatch) { cleanedString = jsonMatch[1].trim(); } else { // Se não tiver crases, tenta encontrar o primeiro { e o último } const startIndex = cleanedString.indexOf('{'); const endIndex = cleanedString.lastIndexOf('}'); if (startIndex !== -1 && endIndex !== -1) { cleanedString = cleanedString.substring(startIndex, endIndex + 1); } } try { const parsed = JSON.parse(cleanedString); let finalData = parsed; // Se o modelo aninhou os dados dentro de uma chave "report" ou "reports" if (parsed.reports) { finalData = parsed.reports; } else if (parsed.report && !parsed.identification) { finalData = parsed.report; } return Array.isArray(finalData) ? finalData : [finalData]; } catch (error) { console.error("Failed to parse cleaned JSON:", jsonString); throw new Error("A resposta da IA não estava no formato JSON esperado."); } }; const reportSchema = { type: Type.OBJECT, properties: { confidence: { type: Type.NUMBER, description: "Grau de Confiança da Análise em percentual (ex: 98)." }, identification: { type: Type.OBJECT, properties: { product: { type: Type.STRING, description: "Nome do produto/material (ex: Chapa de Aço)." }, standards: { type: Type.STRING, description: "Norma(s) principal(is) citada(s) no certificado." }, manufacturer: { type: Type.STRING, description: "Nome do fabricante ou revendedor." }, certificateNumber: { type: Type.STRING, description: "Número do certificado." }, certificateDate: { type: Type.STRING, description: "Data do certificado." }, batches: { type: Type.STRING, description: "Lista de lotes (batches)." }, heats: { type: Type.STRING, description: "Lista de corridas (heats)." }, quantity: { type: Type.STRING, description: "Volume, peso ou medida do material." }, }, required: ["product", "standards", "manufacturer", "certificateNumber", "certificateDate", "batches", "heats", "quantity"] }, compliance: { type: Type.OBJECT, properties: { status: { type: Type.STRING, enum: ["CONFORME", "NÃO CONFORME"], description: "Status geral de conformidade." }, mechanical: { type: Type.ARRAY, items: { type: Type.OBJECT, properties: { property: { type: Type.STRING, description: "Propriedade mecânica (ex: Limite de Escoamento)." }, norm: { type: Type.STRING, description: "Valor exigido pela norma (ex: Mín: 350 MPa)." }, certificate: { type: Type.STRING, description: "Valor encontrado no certificado." }, status: { type: Type.STRING, enum: ["OK", "FALHA"], description: "Status da propriedade." }, differencePercentage: { type: Type.STRING, description: "Calculated percentage difference compared to theoretical normative material (e.g., '+15%' or '-5%'). Only for mechanical properties." } }, required: ["property", "norm", "certificate", "status", "differencePercentage"] } }, chemical: { type: Type.ARRAY, items: { type: Type.OBJECT, properties: { element: { type: Type.STRING, description: "Elemento químico (ex: Carbono (C))." }, norm: { type: Type.STRING, description: "Valor exigido pela norma (ex: Máx: 0.25%)." }, certificate: { type: Type.STRING, description: "Valor encontrado no certificado." }, status: { type: Type.STRING, enum: ["OK", "FALHA"], description: "Status do elemento." }, }, required: ["element", "norm", "certificate", "status"] } }, otherTests: { type: Type.ARRAY, items: { type: Type.OBJECT, properties: { test: { type: Type.STRING, description: "Outro teste relevante (ex: Teste de Impacto Charpy)." }, norm: { type: Type.STRING, description: "Valor exigido pela norma." }, certificate: { type: Type.STRING, description: "Valor encontrado no certificado." }, status: { type: Type.STRING, enum: ["OK", "FALHA"], description: "Status do teste." }, }, required: ["test", "norm", "certificate", "status"] } } }, required: ["status", "mechanical", "chemical", "otherTests"] }, overPerformance: { type: Type.ARRAY, items: { type: Type.OBJECT, properties: { property: { type: Type.STRING, description: "Ponto-chave de superdesempenho." }, value: { type: Type.STRING, description: "Percentual acima do mínimo normativo (ex: 12.5%)." }, }, required: ["property", "value"] }, description: "Lista de pontos onde o material excede os requisitos mínimos. Preencher apenas se o status for 'CONFORME'." }, equivalents: { type: Type.ARRAY, items: { type: Type.OBJECT, properties: { system: { type: Type.STRING, description: "Sistema da norma (ex: EN (Europa))." }, norm: { type: Type.STRING, description: "Norma equivalente (ex: S355J2)." }, }, required: ["system", "norm"] }, description: "Lista de normas internacionais equivalentes." } }, required: ["confidence", "identification", "compliance", "overPerformance", "equivalents"] }; const arraySchema = { type: Type.ARRAY, items: reportSchema }; const PROMPT_BASE = `Você é o "SteelCheck", um especialista sênior em engenharia de materiais, metalurgia e controle de qualidade. Sua tarefa é analisar o Certificado de Qualidade fornecido e gerar um "Relatório de Análise Técnica" em formato JSON. **Instrução Crítica 1 (Orientação):** A imagem PODE estar rotacionada (deitada ou de cabeça para baixo). Compense mentalmente a orientação correta antes de ler as tabelas. **Instrução Crítica 2 (Múltiplos Lotes/Corridas):** Se o certificado contiver MÚLTIPLOS LOTES (Batches) ou MÚLTIPLAS CORRIDAS (Heats) com resultados INDEPENDENTES, gere múltiplos relatórios! O JSON final DEVE SER UM ARRAY contendo 1 objeto para cada lote/corrida independente encontrado. Execute: 1. Extração de Dados: Leia a imagem atentamente, respeitando as colunas. 2. Identificação de Normas. 3. Análise de Conformidade: Compare resultados x requisitos. 4. Propriedades Mecânicas (mechanical): Calcule o "differencePercentage" (+X% ou -X%) em relação ao teórico. 5. Análise de Equivalência e Grau de Confiança. Retorne APENAS o JSON, sem markdown. DEVE SER UM ARRAY DE OBJETOS: [ { "confidence": 95, "identification": { "product": "", "standards": "", "manufacturer": "", "certificateNumber": "", "certificateDate": "", "batches": "LOTE X", "heats": "CORRIDA Y", "quantity": "" }, "compliance": { "status": "CONFORME ou NÃO CONFORME", "mechanical": [ { "property": "", "norm": "", "certificate": "", "status": "OK ou FALHA", "differencePercentage": "+X% ou -X%" } ], "chemical": [ { "element": "", "norm": "", "certificate": "", "status": "OK ou FALHA" } ], "otherTests": [ { "test": "", "norm": "", "certificate": "", "status": "OK ou FALHA" } ] }, "overPerformance": [ { "property": "", "value": "" } ], "equivalents": [ { "system": "", "norm": "" } ] } ]`; export const analyzeWithGemini = async (file: File, apiKey: string, model: string = 'gemini-2.5-flash'): Promise => { if (!apiKey) { throw new Error("A chave de API do Gemini não foi fornecida."); } const ai = new GoogleGenAI({ apiKey: apiKey }); const imagePart = await fileToGenerativePart(file); const response = await ai.models.generateContent({ model, contents: { parts: [imagePart, { text: PROMPT_BASE }] }, config: { responseMimeType: "application/json", responseSchema: arraySchema, temperature: 0.1, } }); try { const text = response.text; const data = cleanAndParseJson(text); return data as ReportData[]; } catch (e) { if (e instanceof Error) { throw e; } throw new Error("A resposta da IA não estava no formato JSON esperado."); } }; export const analyzeWithOpenAI = async (file: File, apiKey: string, model: string = 'gpt-4o'): Promise => { if (!apiKey) { throw new Error("A chave de API da OpenAI não foi fornecida."); } const base64Data = await new Promise((resolve) => { const reader = new FileReader(); reader.onloadend = () => resolve((reader.result as string).split(',')[1]); reader.readAsDataURL(file); }); const response = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ model, messages: [ { role: 'user', content: [ { type: 'text', text: PROMPT_BASE }, { type: 'image_url', image_url: { url: `data:${file.type};base64,${base64Data}` } } ] } ], max_tokens: 4096, temperature: 0.1, response_format: { type: 'json_object' } }) }); if (!response.ok) { const error = await response.json(); throw new Error(`Erro da OpenAI: ${error.error?.message || 'Erro desconhecido'}`); } const data = await response.json(); const content = data.choices[0]?.message?.content; if (!content) { throw new Error("Resposta vazia da OpenAI"); } return cleanAndParseJson(content) as ReportData[]; }; export const analyzeWithAnthropic = async (file: File, apiKey: string, model: string = 'claude-3-sonnet-20240229'): Promise => { if (!apiKey) { throw new Error("A chave de API da Anthropic não foi fornecida."); } const base64Data = await new Promise((resolve) => { const reader = new FileReader(); reader.onloadend = () => resolve((reader.result as string).split(',')[1]); reader.readAsDataURL(file); }); const response = await fetch('https://api.anthropic.com/v1/messages', { method: 'POST', headers: { 'x-api-key': apiKey, 'anthropic-version': '2023-06-01', 'Content-Type': 'application/json', }, body: JSON.stringify({ model, max_tokens: 4096, temperature: 0.1, messages: [ { role: 'user', content: [ { type: 'image', source: { type: 'base64', media_type: file.type, data: base64Data } }, { type: 'text', text: PROMPT_BASE + "\n\nRetorne apenas JSON válido sem formatação markdown." } ] } ] }) }); if (!response.ok) { const error = await response.json(); throw new Error(`Erro da Anthropic: ${error.error?.message || 'Erro desconhecido'}`); } const data = await response.json(); const content = data.content[0]?.text; if (!content) { throw new Error("Resposta vazia da Anthropic"); } return cleanAndParseJson(content) as ReportData[]; }; export const analyzeWithAzure = async (file: File, apiKey: string, endpoint: string, deployment: string): Promise => { if (!apiKey || !endpoint) { throw new Error("A chave de API e endpoint do Azure são necessários."); } const base64Data = await new Promise((resolve) => { const reader = new FileReader(); reader.onloadend = () => resolve((reader.result as string).split(',')[1]); reader.readAsDataURL(file); }); const url = `${endpoint}/openai/deployments/${deployment}/chat/completions?api-version=2024-02-15-preview`; const response = await fetch(url, { method: 'POST', headers: { 'api-key': apiKey, 'Content-Type': 'application/json', }, body: JSON.stringify({ messages: [ { role: 'user', content: [ { type: 'text', text: PROMPT_BASE }, { type: 'image_url', image_url: { url: `data:${file.type};base64,${base64Data}` } } ] } ], max_tokens: 4096, temperature: 0.1 }) }); if (!response.ok) { const error = await response.text(); throw new Error(`Erro do Azure: ${error}`); } const data = await response.json(); const content = data.choices[0]?.message?.content; if (!content) { throw new Error("Resposta vazia do Azure"); } return cleanAndParseJson(content) as ReportData[]; }; export const analyzeCertificate = async (options: AnalyzeOptions): Promise => { const { provider, apiKey, model, file, endpoint } = options; switch (provider) { case 'gemini': return analyzeWithGemini(file, apiKey, model); case 'openai': return analyzeWithOpenAI(file, apiKey, model); case 'anthropic': return analyzeWithAnthropic(file, apiKey, model); case 'azure': return analyzeWithAzure(file, apiKey, endpoint!, model); case 'ollama': return analyzeWithOllama(file, endpoint!, model); case 'openrouter': return analyzeWithOpenRouter(file, apiKey, model); case 'minimax': return analyzeWithMinimax(file, apiKey, model); default: throw new Error(`Provedor não suportado: ${provider}`); } }; export const analyzeWithOllama = async (file: File, endpoint: string, model: string = 'llama3.2-vision'): Promise => { if (!endpoint) { throw new Error("O endpoint do Ollama é necessário. Configure o endereço da sua VPS."); } const base64Data = await new Promise((resolve) => { const reader = new FileReader(); reader.onloadend = () => resolve((reader.result as string).split(',')[1]); reader.readAsDataURL(file); }); let url = `${endpoint}/api/chat`; // Check if using OpenWebUI (has /api in path but not direct Ollama) const useOpenWebUI = endpoint.includes('/api') || endpoint.includes('llm.reifonas.cloud'); if (useOpenWebUI) { // Use OpenWebUI API format url = `${endpoint}/api/v1/chat/completions`; } const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(useOpenWebUI ? { 'Authorization': 'Bearer no-key-required' } : {}) }, body: JSON.stringify({ model, messages: [ { role: 'user', content: [ { type: 'image_url', image_url: { url: `data:${file.type};base64,${base64Data}` } }, { type: 'text', text: PROMPT_BASE + "\n\nRetorne apenas JSON válido sem formatação markdown." } ] } ], ...(useOpenWebUI ? {} : { format: 'json' }), options: { temperature: 0.1, num_predict: 4096 } }) }); if (!response.ok) { const error = await response.text(); throw new Error(`Erro do Ollama/OpenWebUI: ${error}`); } const data = await response.json(); // OpenWebUI format: data.choices[0].message.content // Ollama native format: data.message.content const content = data.choices?.[0]?.message?.content || data.message?.content; if (!content) { 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 => { if (!apiKey) { throw new Error("A chave de API do OpenRouter não foi fornecida."); } const base64Data = await new Promise((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 }) }); 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[]; }; export const analyzeWithMinimax = async (file: File, apiKey: string, model: string = 'minimax-2.7'): Promise => { if (!apiKey) { throw new Error("A chave de API do MiniMax não foi fornecida."); } const base64Data = await new Promise((resolve) => { const reader = new FileReader(); reader.onloadend = () => resolve((reader.result as string).split(',')[1]); reader.readAsDataURL(file); }); const response = await fetch('https://api.minimaxi.com/v1/chat/completions', { method: 'POST', headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' }, 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 }) }); if (!response.ok) { const error = await response.json().catch(() => ({})); throw new Error(`Erro do MiniMax: ${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 MiniMax"); } return cleanAndParseJson(content) as ReportData[]; };