arbritage/services/geminiService.ts

89 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

2026-01-26 14:20:25 +00:00
import { Product, AuctionLot, BiddingTender } from "../types";
import { supabase } from "./supabase";
2026-01-26 14:20:25 +00:00
// Removed API_KEY check for frontend as we now use Edge Functions
2026-01-26 14:20:25 +00:00
/**
* Helper: converte File em Part (inlineData) para o Gemini
* @deprecated Moved to Backend
2026-01-26 14:20:25 +00:00
*/
async function fileToPart(file: File) {
throw new Error("File processing must be moved to Backend Edge Functions.");
2026-01-26 14:20:25 +00:00
}
/**
* Sourcing Otimizado: Via Supabase Edge Function
2026-01-26 14:20:25 +00:00
*/
export async function searchProducts(
query: string,
currentExchangeRate?: number
2026-01-26 14:20:25 +00:00
): Promise<{ products: Product[]; sources: any[] }> {
try {
console.log("🚀 Calling Edge Function: search-products for:", query);
// Call Supabase Edge Function
const { data, error } = await supabase.functions.invoke('search-products', {
body: {
query,
kind: "products"
}
});
2026-01-26 14:20:25 +00:00
if (error) throw error;
2026-01-26 14:20:25 +00:00
console.log(`📦 Edge Function Result: ${data.cache}`);
2026-01-26 14:20:25 +00:00
let products = data.products || [];
const sources = data.sources || [];
2026-01-26 14:20:25 +00:00
// Recalculate BRL if exchange rate provided
if (currentExchangeRate && Array.isArray(products)) {
products = products.map((p: any) => ({
...p,
priceBRL: p.priceUSD * currentExchangeRate
}));
}
2026-01-26 14:20:25 +00:00
return { products, sources };
2026-01-26 14:20:25 +00:00
} catch (error) {
console.error("Erro na busca de produtos (Edge Function):", error);
throw error;
}
2026-01-26 14:20:25 +00:00
}
/**
* Busca Oportunidades (>25% Margem) por Categoria
* @todo Migrate to Edge Function
2026-01-26 14:20:25 +00:00
*/
export async function searchOpportunities(
category: string,
includeOverhead: boolean = true,
currentExchangeRate?: number
): Promise<{ products: Product[]; sources: any[] }> {
console.warn("searchOpportunities not yet migrated to Edge Function.");
return { products: [], sources: [] };
2026-01-26 14:20:25 +00:00
}
/**
* Leilões: Analisa texto, link ou ARQUIVO PDF
* @todo Migrate to Edge Function
2026-01-26 14:20:25 +00:00
*/
export async function analyzeAuctionData(
input: string | File
): Promise<{ lot: AuctionLot | null; sources: any[] }> {
console.warn("analyzeAuctionData not yet migrated to Edge Function.");
return { lot: null, sources: [] };
2026-01-26 14:20:25 +00:00
}
/**
* Licitações Otimizadas
* @todo Migrate to Edge Function
2026-01-26 14:20:25 +00:00
*/
export async function searchBiddings(
query: string
): Promise<{ tenders: BiddingTender[]; sources: any[] }> {
console.warn("searchBiddings not yet migrated to Edge Function.");
return { tenders: [], sources: [] };
2026-01-26 14:20:25 +00:00
}