feat: multi-provider AI support with auto-detection
- Added support for Google Gemini, OpenAI, Anthropic, and Azure OpenAI - Implemented API key validation with auto model detection - Added Error Boundary for better error handling - Migrated PDF generation to native jsPDF (better quality) - Added PWA support with offline capabilities - Implemented tests with Vitest - Fixed language consistency (PT-BR) - Improved accessibility (ARIA)
This commit is contained in:
353
services/aiService.ts
Normal file
353
services/aiService.ts
Normal file
@@ -0,0 +1,353 @@
|
||||
import { GoogleGenAI, Type } from "@google/genai";
|
||||
import type { ReportData, AIProvider } from '../types/providers';
|
||||
|
||||
interface AnalyzeOptions {
|
||||
provider: AIProvider;
|
||||
apiKey: string;
|
||||
model: string;
|
||||
file: File;
|
||||
endpoint?: string;
|
||||
}
|
||||
|
||||
const fileToGenerativePart = async (file: File) => {
|
||||
const base64EncodedDataPromise = new Promise<string>((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 => {
|
||||
const cleanedString = jsonString
|
||||
.replace(/^```json\s*/, '')
|
||||
.replace(/```$/, '')
|
||||
.trim();
|
||||
|
||||
try {
|
||||
return JSON.parse(cleanedString);
|
||||
} 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." },
|
||||
},
|
||||
required: ["property", "norm", "certificate", "status"]
|
||||
}
|
||||
},
|
||||
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 PROMPT_BASE = `Você é o "SteelCheck", um especialista sênior em engenharia de materiais, metalurgia e controle de qualidade, com profundo conhecimento em aços estruturais (ASTM, EN, DIN), parafusos (ASTM, ISO), consumíveis de soldagem (AWS, ISO) e revestimentos industriais (ISO, NACE, SSPC).
|
||||
|
||||
Sua tarefa é analisar o Certificado de Qualidade fornecido e gerar um "Relatório de Análise Técnica" em formato JSON, seguindo rigorosamente o schema fornecido.
|
||||
|
||||
**Instrução Crítica:** Verifique a orientação do documento. Se estiver rotacionado, ajuste para a posição correta antes de extrair dados.
|
||||
|
||||
Execute:
|
||||
1. **Extração de Dados (OCR):** Extraia todos os dados primários do certificado.
|
||||
2. **Identificação de Normas:** Identifique a(s) norma(s) que o certificado alega atender.
|
||||
3. **Análise de Conformidade:** Compare os resultados com os requisitos mínimos e máximos das normas. Determine status como "OK" ou "FALHA" para cada item e "CONFORME" ou "NÃO CONFORME" geral.
|
||||
4. **Análise de Superdesempenho:** Se "CONFORME", calcule o quanto excede os requisitos (ex: "18% acima do mínimo").
|
||||
5. **Análise de Equivalência:** Liste materiais equivalentes de outras normas (EN, DIN, JIS, NBR, etc.).
|
||||
6. **Grau de Confiança:** Forneça um % de confiança baseado na qualidade do documento.
|
||||
|
||||
Retorne APENAS o objeto JSON, sem formatação adicional.`;
|
||||
|
||||
export const analyzeWithGemini = async (file: File, apiKey: string, model: string = 'gemini-2.5-flash'): Promise<ReportData> => {
|
||||
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: reportSchema,
|
||||
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<ReportData> => {
|
||||
if (!apiKey) {
|
||||
throw new Error("A chave de API da OpenAI 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://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<ReportData> => {
|
||||
if (!apiKey) {
|
||||
throw new Error("A chave de API da Anthropic 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://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<ReportData> => {
|
||||
if (!apiKey || !endpoint) {
|
||||
throw new Error("A chave de API e endpoint do Azure são necessários.");
|
||||
}
|
||||
|
||||
const base64Data = await new Promise<string>((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<ReportData> => {
|
||||
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);
|
||||
default:
|
||||
throw new Error(`Provedor não suportado: ${provider}`);
|
||||
}
|
||||
};
|
||||
161
services/apiTestService.ts
Normal file
161
services/apiTestService.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
import { PROVIDERS, type AIProvider } from '../types/providers';
|
||||
|
||||
export interface ModelInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface TestResult {
|
||||
success: boolean;
|
||||
models?: ModelInfo[];
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export const testApiKey = async (provider: AIProvider, apiKey: string, endpoint?: string): Promise<TestResult> => {
|
||||
try {
|
||||
switch (provider) {
|
||||
case 'gemini':
|
||||
return await testGemini(apiKey);
|
||||
case 'openai':
|
||||
return await testOpenAI(apiKey);
|
||||
case 'anthropic':
|
||||
return await testAnthropic(apiKey);
|
||||
case 'azure':
|
||||
return await testAzure(apiKey, endpoint);
|
||||
default:
|
||||
return { success: false, error: 'Provedor não suportado' };
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Erro desconhecido ao testar API'
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const testGemini = async (apiKey: string): Promise<TestResult> => {
|
||||
const { GoogleGenAI } = await import('@google/genai');
|
||||
|
||||
try {
|
||||
const ai = new GoogleGenAI({ apiKey });
|
||||
const response = await ai.models.generateContent({
|
||||
model: 'gemini-2.5-flash',
|
||||
contents: 'Respond with exactly: {"status": "ok"}',
|
||||
config: { temperature: 0 }
|
||||
});
|
||||
|
||||
if (response.text && response.text.includes('ok')) {
|
||||
return {
|
||||
success: true,
|
||||
models: [
|
||||
{ id: 'gemini-2.0-flash', name: 'Gemini 2.0 Flash (Rápido)' },
|
||||
{ id: 'gemini-2.5-flash', name: 'Gemini 2.5 Flash (Equilibrado)' },
|
||||
{ id: 'gemini-2.5-pro', name: 'Gemini 2.5 Pro (Mais potente)' }
|
||||
]
|
||||
};
|
||||
}
|
||||
return { success: false, error: 'Resposta inválida do Gemini' };
|
||||
} catch (error: any) {
|
||||
if (error.message?.includes('API key')) {
|
||||
return { success: false, error: 'Chave de API inválida' };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const testOpenAI = async (apiKey: string): Promise<TestResult> => {
|
||||
try {
|
||||
const response = await fetch('https://api.openai.com/v1/models', {
|
||||
headers: { 'Authorization': `Bearer ${apiKey}` }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
return { success: false, error: error.error?.message || 'Chave de API inválida' };
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const visionModels = data.data
|
||||
.filter((m: any) => m.id.includes('gpt-4o') || m.id.includes('gpt-4') || m.id.includes('gpt-4-turbo'))
|
||||
.map((m: any) => ({ id: m.id, name: m.id }));
|
||||
|
||||
if (visionModels.length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
models: [{ id: 'gpt-4o', name: 'GPT-4o (Padrão)' }]
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true, models: visionModels };
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message || 'Erro ao conectar com OpenAI' };
|
||||
}
|
||||
};
|
||||
|
||||
const testAnthropic = async (apiKey: string): Promise<TestResult> => {
|
||||
try {
|
||||
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: 'claude-3-haiku-20240307',
|
||||
max_tokens: 10,
|
||||
messages: [{ role: 'user', content: 'Hi' }]
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
return { success: false, error: error.error?.message || 'Chave de API inválida' };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
models: [
|
||||
{ id: 'claude-3-opus-20240229', name: 'Claude 3 Opus (Mais potente)' },
|
||||
{ id: 'claude-3-sonnet-20240229', name: 'Claude 3 Sonnet (Equilibrado)' },
|
||||
{ id: 'claude-3-haiku-20240307', name: 'Claude 3 Haiku (Rápido)' }
|
||||
]
|
||||
};
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message || 'Erro ao conectar com Anthropic' };
|
||||
}
|
||||
};
|
||||
|
||||
const testAzure = async (apiKey: string, endpoint: string): Promise<TestResult> => {
|
||||
if (!endpoint) {
|
||||
return { success: false, error: 'Endpoint do Azure é obrigatório' };
|
||||
}
|
||||
|
||||
try {
|
||||
const url = `${endpoint}/openai/deployments?api-version=2024-02-15-preview`;
|
||||
const response = await fetch(url, {
|
||||
headers: { 'api-key': apiKey }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return { success: false, error: 'Endpoint ou chave inválidos' };
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const deployments = data.data?.map((d: any) => ({
|
||||
id: d.id,
|
||||
name: d.id
|
||||
})) || [];
|
||||
|
||||
if (deployments.length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
models: [{ id: 'gpt-4', name: 'GPT-4 (Padrão)' }]
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true, models: deployments };
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message || 'Erro ao conectar com Azure' };
|
||||
}
|
||||
};
|
||||
@@ -1,174 +0,0 @@
|
||||
import { GoogleGenAI, Type } from "@google/genai";
|
||||
import type { ReportData } from '../types';
|
||||
|
||||
const fileToGenerativePart = async (file: File) => {
|
||||
const base64EncodedDataPromise = new Promise<string>((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 },
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Cleans and parses a JSON string, removing potential markdown code blocks.
|
||||
* @param jsonString The raw string from the AI response.
|
||||
* @returns The parsed JSON object.
|
||||
*/
|
||||
const cleanAndParseJson = (jsonString: string): any => {
|
||||
const cleanedString = jsonString
|
||||
.replace(/^```json\s*/, '')
|
||||
.replace(/```$/, '')
|
||||
.trim();
|
||||
|
||||
try {
|
||||
return JSON.parse(cleanedString);
|
||||
} catch (error) {
|
||||
console.error("Failed to parse cleaned JSON. Original string:", jsonString);
|
||||
console.error("Cleaned string:", cleanedString);
|
||||
// Re-throw the parsing error to be caught by the caller
|
||||
throw new Error("A resposta da IA não estava no formato JSON esperado, mesmo após a limpeza.");
|
||||
}
|
||||
}
|
||||
|
||||
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." },
|
||||
},
|
||||
required: ["property", "norm", "certificate", "status"]
|
||||
}
|
||||
},
|
||||
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"]
|
||||
};
|
||||
|
||||
export const analyzeCertificate = async (file: File, apiKey: string): Promise<ReportData> => {
|
||||
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 prompt = `Você é o "SteelCheck", um especialista sênior em engenharia de materiais, metalurgia e controle de qualidade, com profundo conhecimento em aços estruturais (ASTM, EN, DIN), parafusos (ASTM, ISO), consumíveis de soldagem (AWS, ISO) e revestimentos industriais (ISO, NACE, SSPC).
|
||||
|
||||
Sua tarefa é analisar o Certificado de Qualidade fornecido (que pode ser uma imagem ou um PDF) e gerar um "Relatório de Análise Técnica" em formato JSON, seguindo rigorosamente o schema fornecido.
|
||||
|
||||
**Instrução Crítica:** Antes de qualquer outra coisa, verifique a orientação do documento. Se ele estiver rotacionado (de lado ou de cabeça para baixo), ajuste-o mentalmente para a posição de leitura correta antes de iniciar a extração de dados. A análise correta depende da orientação adequada.
|
||||
|
||||
Execute as seguintes etapas:
|
||||
1. **Extração de Dados (OCR):** Extraia todos os dados primários do certificado, já considerando a orientação correta.
|
||||
2. **Identificação de Normas:** Identifique a(s) norma(s) que o certificado alega atender.
|
||||
3. **Análise de Conformidade:** Baseado no seu conhecimento profundo, compare os resultados dos testes (Propriedades Mecânicas, Composição Química, etc.) com os requisitos mínimos e máximos especificados pela(s) norma(s). Determine o status como "OK" ou "FALHA" para cada item e um "status" geral como "CONFORME" ou "NÃO CONFORME".
|
||||
4. **Análise de Superdesempenho:** Se o status geral for "CONFORME", calcule o quanto (em porcentagem) ele excede os requisitos-chave (ex: "18% acima do mínimo"). Se for "NÃO CONFORME", deixe a lista de superdesempenho vazia.
|
||||
5. **Análise de Equivalência:** Pesquise e liste materiais funcionalmente equivalentes de outras normas importantes (EN, DIN, JIS, NBR, etc.).
|
||||
6. **Grau de Confiança:** Forneça um Grau de Confiança (em %) sobre a precisão da sua análise, baseado na qualidade do documento.
|
||||
|
||||
Retorne APENAS o objeto JSON, sem nenhum texto ou formatação adicional.`;
|
||||
|
||||
const response = await ai.models.generateContent({
|
||||
model: 'gemini-2.5-flash',
|
||||
contents: { parts: [imagePart, { text: prompt }] },
|
||||
config: {
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: reportSchema,
|
||||
temperature: 0.1,
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const text = response.text;
|
||||
const data = cleanAndParseJson(text);
|
||||
return data as ReportData;
|
||||
} catch (e) {
|
||||
console.error("Failed to parse JSON response:", e);
|
||||
// Lançar o erro original ou um erro mais específico
|
||||
if (e instanceof Error) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("A resposta da IA não estava no formato JSON esperado.");
|
||||
}
|
||||
};
|
||||
@@ -1,49 +1,291 @@
|
||||
import jsPDF from 'jspdf';
|
||||
import html2canvas from 'html2canvas';
|
||||
import { jsPDF } from 'jspdf';
|
||||
import type { ReportData } from '../types';
|
||||
|
||||
export const exportAsPdf = async (elementId: string, fileName: string, action: 'preview' | 'download'): Promise<void> => {
|
||||
const input = document.getElementById(elementId);
|
||||
if (!input) {
|
||||
const COLORS = {
|
||||
primary: '#1e40af',
|
||||
secondary: '#6366f1',
|
||||
success: '#059669',
|
||||
danger: '#dc2626',
|
||||
dark: '#1e293b',
|
||||
gray: '#64748b',
|
||||
light: '#f8fafc',
|
||||
};
|
||||
|
||||
const formatDate = (dateStr: string): string => {
|
||||
return dateStr || 'N/A';
|
||||
};
|
||||
|
||||
const getStatusColor = (status: string): string => {
|
||||
if (status === 'CONFORME' || status === 'OK') return COLORS.success;
|
||||
if (status === 'NÃO CONFORME' || status === 'FALHA') return COLORS.danger;
|
||||
return COLORS.gray;
|
||||
};
|
||||
|
||||
export const generatePdfReport = (report: ReportData): jsPDF => {
|
||||
const doc = new jsPDF({
|
||||
orientation: 'portrait',
|
||||
unit: 'mm',
|
||||
format: 'a4',
|
||||
});
|
||||
|
||||
const pageWidth = doc.internal.pageSize.getWidth();
|
||||
const pageHeight = doc.internal.pageSize.getHeight();
|
||||
const margin = 15;
|
||||
let y = margin;
|
||||
|
||||
const addHeader = () => {
|
||||
doc.setFillColor(COLORS.primary);
|
||||
doc.rect(0, 0, pageWidth, 30, 'F');
|
||||
|
||||
doc.setTextColor(255, 255, 255);
|
||||
doc.setFontSize(20);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.text('SteelCheck', margin, 18);
|
||||
|
||||
doc.setFontSize(10);
|
||||
doc.setFont('helvetica', 'normal');
|
||||
doc.text('Relatório de Análise Técnica de Qualidade', margin, 25);
|
||||
|
||||
y = 40;
|
||||
};
|
||||
|
||||
const addFooter = (pageNum: number) => {
|
||||
doc.setFillColor(COLORS.light);
|
||||
doc.rect(0, pageHeight - 15, pageWidth, 15, 'F');
|
||||
|
||||
doc.setFontSize(8);
|
||||
doc.setTextColor(COLORS.gray);
|
||||
doc.text(`Página ${pageNum}`, pageWidth / 2, pageHeight - 8, { align: 'center' });
|
||||
doc.text('SteelCheck - Análise assistida por IA', margin, pageHeight - 8);
|
||||
};
|
||||
|
||||
addHeader();
|
||||
|
||||
doc.setTextColor(COLORS.dark);
|
||||
doc.setFontSize(16);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.text('Relatório Técnico de Qualidade', pageWidth / 2, y, { align: 'center' });
|
||||
y += 8;
|
||||
|
||||
doc.setFontSize(10);
|
||||
doc.setFont('helvetica', 'normal');
|
||||
doc.setTextColor(COLORS.gray);
|
||||
doc.text('Análise de conformidade normativa assistida por IA', pageWidth / 2, y, { align: 'center' });
|
||||
y += 12;
|
||||
|
||||
doc.setFillColor(COLORS.light);
|
||||
doc.roundedRect(margin, y, pageWidth - 2 * margin, 25, 3, 3, 'F');
|
||||
y += 5;
|
||||
|
||||
doc.setFontSize(12);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.setTextColor(COLORS.primary);
|
||||
doc.text('Grau de Confiança da Análise', pageWidth / 2, y, { align: 'center' });
|
||||
y += 8;
|
||||
|
||||
doc.setFontSize(36);
|
||||
doc.setTextColor(COLORS.secondary);
|
||||
doc.text(`${report.confidence}%`, pageWidth / 2, y + 5, { align: 'center' });
|
||||
y += 20;
|
||||
|
||||
const sectionTitle = (title: string) => {
|
||||
y += 5;
|
||||
doc.setFillColor(COLORS.primary);
|
||||
doc.rect(margin, y, 3, 10, 'F');
|
||||
|
||||
doc.setFontSize(12);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.setTextColor(COLORS.dark);
|
||||
doc.text(title, margin + 8, y + 7);
|
||||
|
||||
doc.setDrawColor(200, 200, 200);
|
||||
doc.line(margin + 8, y + 12, pageWidth - margin, y + 12);
|
||||
y += 15;
|
||||
};
|
||||
|
||||
const addKeyValue = (key: string, value: string) => {
|
||||
doc.setFontSize(9);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.setTextColor(COLORS.gray);
|
||||
doc.text(key.toUpperCase(), margin, y);
|
||||
|
||||
doc.setFont('helvetica', 'normal');
|
||||
doc.setTextColor(COLORS.dark);
|
||||
const valueLines = doc.splitTextToSize(value || 'N/A', pageWidth - 2 * margin - 40);
|
||||
doc.text(valueLines, margin + 40, y);
|
||||
y += 6 * (valueLines.length || 1);
|
||||
};
|
||||
|
||||
sectionTitle('1. Dados de Identificação');
|
||||
|
||||
const ident = report.identification;
|
||||
addKeyValue('Produto', ident.product);
|
||||
addKeyValue('Norma(s)', ident.standards);
|
||||
addKeyValue('Fabricante', ident.manufacturer);
|
||||
addKeyValue('Nr. Certificado', ident.certificateNumber);
|
||||
addKeyValue('Data', ident.certificateDate);
|
||||
addKeyValue('Lotes', ident.batches);
|
||||
addKeyValue('Corridas (Heats)', ident.heats);
|
||||
addKeyValue('Quantidade', ident.quantity);
|
||||
|
||||
sectionTitle('2. Verificação de Conformidade');
|
||||
|
||||
const complianceStatusColor = report.compliance.status === 'CONFORME' ? COLORS.success : COLORS.danger;
|
||||
doc.setFillColor(complianceStatusColor);
|
||||
doc.roundedRect(pageWidth - margin - 40, y - 12, 35, 10, 2, 2, 'F');
|
||||
doc.setTextColor(255, 255, 255);
|
||||
doc.setFontSize(10);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.text(report.compliance.status, pageWidth - margin - 22, y - 6, { align: 'center' });
|
||||
|
||||
const addTable = (title: string, items: { property?: string; element?: string; test?: string; norm: string; certificate: string; status: string }[]) => {
|
||||
doc.setFontSize(10);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.setTextColor(COLORS.dark);
|
||||
doc.text(title, margin, y);
|
||||
y += 6;
|
||||
|
||||
const colWidths = [45, 40, 40, 25];
|
||||
const colX = [margin, margin + 45, margin + 85, margin + 125];
|
||||
|
||||
doc.setFillColor(240, 240, 240);
|
||||
doc.rect(margin, y, pageWidth - 2 * margin, 8, 'F');
|
||||
|
||||
doc.setFontSize(8);
|
||||
doc.setTextColor(COLORS.gray);
|
||||
doc.text('Propriedade/Norma', colX[0] + 10, y + 5);
|
||||
doc.text('Valor Norma', colX[1] + 5, y + 5);
|
||||
doc.text('Valor Certificado', colX[2] + 5, y + 5);
|
||||
doc.text('Status', colX[3] + 8, y + 5);
|
||||
y += 8;
|
||||
|
||||
items.forEach((item) => {
|
||||
const label = item.property || item.element || item.test || '';
|
||||
|
||||
doc.setFont('helvetica', 'normal');
|
||||
doc.setTextColor(COLORS.dark);
|
||||
doc.setFontSize(8);
|
||||
|
||||
const labelLines = doc.splitTextToSize(label, colWidths[0] - 5);
|
||||
const normLines = doc.splitTextToSize(item.norm, colWidths[1] - 5);
|
||||
const certLines = doc.splitTextToSize(item.certificate, colWidths[2] - 5);
|
||||
|
||||
const rowHeight = Math.max(labelLines.length, normLines.length, certLines.length) * 4 + 4;
|
||||
|
||||
doc.text(labelLines, colX[0], y + 4);
|
||||
doc.text(normLines, colX[1], y + 4);
|
||||
doc.text(certLines, colX[2], y + 4);
|
||||
|
||||
const statusColor = item.status === 'OK' ? COLORS.success : COLORS.danger;
|
||||
doc.setFillColor(statusColor);
|
||||
doc.roundedRect(colX[3], y, 20, 6, 1, 1, 'F');
|
||||
doc.setTextColor(255, 255, 255);
|
||||
doc.setFontSize(7);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.text(item.status, colX[3] + 10, y + 4, { align: 'center' });
|
||||
|
||||
y += rowHeight;
|
||||
});
|
||||
|
||||
y += 5;
|
||||
};
|
||||
|
||||
addTable('Propriedades Mecânicas', report.compliance.mechanical);
|
||||
addTable('Composição Química (%)', report.compliance.chemical);
|
||||
|
||||
if (report.compliance.otherTests.length > 0) {
|
||||
addTable('Outros Testes', report.compliance.otherTests);
|
||||
}
|
||||
|
||||
if (report.compliance.status === 'CONFORME' && report.overPerformance.length > 0) {
|
||||
sectionTitle('3. Destaques de Desempenho');
|
||||
|
||||
doc.setFontSize(9);
|
||||
doc.setTextColor(COLORS.gray);
|
||||
doc.text('Este material excede os requisitos mínimos normativos:', margin, y);
|
||||
y += 6;
|
||||
|
||||
report.overPerformance.forEach((item) => {
|
||||
doc.setFillColor(240, 255, 240);
|
||||
doc.roundedRect(margin, y - 3, pageWidth - 2 * margin, 12, 2, 2, 'F');
|
||||
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.setTextColor(COLORS.dark);
|
||||
doc.text(item.property, margin + 5, y + 2);
|
||||
|
||||
doc.setTextColor(COLORS.success);
|
||||
doc.text(item.value, margin + 5, y + 8);
|
||||
|
||||
y += 15;
|
||||
});
|
||||
}
|
||||
|
||||
if (y > pageHeight - 50) {
|
||||
doc.addPage();
|
||||
y = margin;
|
||||
addHeader();
|
||||
}
|
||||
|
||||
sectionTitle('4. Normas Equivalentes');
|
||||
|
||||
doc.setFontSize(9);
|
||||
doc.setTextColor(COLORS.gray);
|
||||
doc.text(`Equivalências internacionais para ${report.identification.standards}:`, margin, y);
|
||||
y += 8;
|
||||
|
||||
const boxWidth = 40;
|
||||
const boxHeight = 15;
|
||||
const gap = 5;
|
||||
let x = margin;
|
||||
|
||||
report.equivalents.forEach((item) => {
|
||||
if (x + boxWidth > pageWidth - margin) {
|
||||
x = margin;
|
||||
y += boxHeight + gap;
|
||||
}
|
||||
|
||||
doc.setFillColor(248, 248, 255);
|
||||
doc.roundedRect(x, y, boxWidth, boxHeight, 2, 2, 'F');
|
||||
|
||||
doc.setFontSize(7);
|
||||
doc.setTextColor(COLORS.gray);
|
||||
doc.text(item.system, x + 2, y + 4);
|
||||
|
||||
doc.setFontSize(9);
|
||||
doc.setFont('helvetica', 'bold');
|
||||
doc.setTextColor(COLORS.dark);
|
||||
doc.text(item.norm, x + 2, y + 10);
|
||||
|
||||
x += boxWidth + gap;
|
||||
});
|
||||
|
||||
const totalPages = doc.getNumberOfPages();
|
||||
for (let i = 1; i <= totalPages; i++) {
|
||||
doc.setPage(i);
|
||||
addFooter(i);
|
||||
}
|
||||
|
||||
return doc;
|
||||
};
|
||||
|
||||
export const exportAsPdf = (elementId: string, fileName: string, action: 'preview' | 'download'): void => {
|
||||
console.log('Using native jsPDF for better quality PDF generation');
|
||||
|
||||
const hiddenDiv = document.getElementById(elementId);
|
||||
if (!hiddenDiv) {
|
||||
console.error(`Element with id ${elementId} not found.`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const canvas = await html2canvas(input, {
|
||||
scale: 2, // Higher scale improves quality
|
||||
useCORS: true,
|
||||
logging: false,
|
||||
});
|
||||
|
||||
const imgData = canvas.toDataURL('image/png');
|
||||
|
||||
// A4 dimensions in mm: 210 x 297
|
||||
const pdf = new jsPDF({
|
||||
orientation: 'portrait',
|
||||
unit: 'mm',
|
||||
format: 'a4',
|
||||
});
|
||||
|
||||
const pdfWidth = pdf.internal.pageSize.getWidth();
|
||||
const pdfHeight = pdf.internal.pageSize.getHeight();
|
||||
const canvasWidth = canvas.width;
|
||||
const canvasHeight = canvas.height;
|
||||
|
||||
// Calculate the aspect ratio
|
||||
const ratio = canvasWidth / canvasHeight;
|
||||
let imgWidth = pdfWidth;
|
||||
let imgHeight = imgWidth / ratio;
|
||||
|
||||
// If the calculated height is greater than the page height, scale down
|
||||
if (imgHeight > pdfHeight) {
|
||||
imgHeight = pdfHeight;
|
||||
imgWidth = imgHeight * ratio;
|
||||
const jsonData = hiddenDiv.getAttribute('data-report');
|
||||
if (!jsonData) {
|
||||
console.error('Report data not found in element');
|
||||
return;
|
||||
}
|
||||
|
||||
const x = (pdfWidth - imgWidth) / 2;
|
||||
const y = 0;
|
||||
|
||||
pdf.addImage(imgData, 'PNG', x, y, imgWidth, imgHeight);
|
||||
const report: ReportData = JSON.parse(jsonData);
|
||||
const pdf = generatePdfReport(report);
|
||||
|
||||
if (action === 'download') {
|
||||
pdf.save(`${fileName}.pdf`);
|
||||
@@ -54,6 +296,6 @@ export const exportAsPdf = async (elementId: string, fileName: string, action: '
|
||||
URL.revokeObjectURL(pdfUrl);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error generating PDF:", error);
|
||||
console.error('Error generating PDF:', error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user