import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Progress } from "@/components/ui/progress"; import { Badge } from "@/components/ui/badge"; import { Check, Download, Loader2, Play } from "lucide-react"; import { RECOMMENDED_MODELS, ModelRecommendation } from "@/lib/recommendation"; import { useState, useEffect } from "react"; import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; interface ModelSelectorProps { recommendation: ModelRecommendation; onComplete: () => void; } export function ModelSelector({ recommendation, onComplete }: ModelSelectorProps) { const [selectedModelId, setSelectedModelId] = useState(recommendation.modelId); const [isInstalled, setIsInstalled] = useState(false); const [isDownloading, setIsDownloading] = useState(false); const [downloadProgress, setDownloadProgress] = useState(0); const [downloadStatus, setDownloadStatus] = useState(""); const selectedModel = RECOMMENDED_MODELS.find(m => m.id === selectedModelId) || RECOMMENDED_MODELS[0]; useEffect(() => { checkInstallation(selectedModelId); }, [selectedModelId]); useEffect(() => { const unlisten = listen('download-progress', (event) => { const line = event.payload; setDownloadStatus(line); // Simple heuristic parsing for progress // Ollama output example: "pulling manifest", "downloading 10%", "verifying sha256 digest" if (line.includes('%')) { const match = line.match(/(\d+)%/); if (match) { setDownloadProgress(parseInt(match[1], 10)); } } else if (line.includes('success')) { setDownloadProgress(100); } }); return () => { unlisten.then(f => f()); }; }, []); const checkInstallation = async (modelId: string) => { try { const installed = await invoke('check_if_model_installed', { name: modelId }); setIsInstalled(installed); } catch (e) { console.error("Check failed", e); } }; const handleDownload = async () => { setIsDownloading(true); setDownloadProgress(0); try { await invoke('pull_model', { name: selectedModelId }); setIsInstalled(true); setIsDownloading(false); } catch (error) { console.error("Download failed", error); setDownloadStatus("Falha no download. Tente novamente."); setIsDownloading(false); } }; return (
Escolha seu Modelo {selectedModelId === recommendation.modelId ? "Baseado no seu hardware, recomendamos:" : "Você selecionou um modelo personalizado:"}
{selectedModelId === recommendation.modelId && ( Recomendado )}

Sobre este modelo:

{selectedModelId === recommendation.modelId ? recommendation.reason : "Modelo alternativo selecionado. Certifique-se de ter memória suficiente."}

{isDownloading && (
{downloadStatus || "Iniciando..."} {downloadProgress}%
)}
{isInstalled ? ( ) : ( )}
); }