import { useState, useEffect, useRef } from 'react'; import { useConfig } from './ConfigContext'; import { checkOllamaStatus, getOllamaDownloadUrl, pollForOllama, hasModel, pullOllamaModel, getPreferredModel, type PullProgress, } from '../utils/ollamaDetection'; import { toastService } from '../toasts'; import { Ollama } from './icons'; import { errorMessage } from '../utils/conversionUtils'; interface OllamaSetupProps { onSuccess: () => void; onCancel: () => void; } export function OllamaSetup({ onSuccess, onCancel }: OllamaSetupProps) { //const { addExtension, getExtensions, upsert } = useConfig(); const { upsert } = useConfig(); const [isChecking, setIsChecking] = useState(true); const [ollamaDetected, setOllamaDetected] = useState(false); const [isPolling, setIsPolling] = useState(false); const [isConnecting, setIsConnecting] = useState(false); const [modelStatus, setModelStatus] = useState< 'checking' | 'available' | 'not-available' | 'downloading' >('checking'); const [downloadProgress, setDownloadProgress] = useState(null); const stopPollingRef = useRef<(() => void) | null>(null); useEffect(() => { // Check if Ollama is already running const checkInitial = async () => { const status = await checkOllamaStatus(); setOllamaDetected(status.isRunning); // If Ollama is running, check for the preferred model if (status.isRunning) { const modelAvailable = await hasModel(getPreferredModel()); setModelStatus(modelAvailable ? 'available' : 'not-available'); } setIsChecking(false); }; checkInitial(); // Cleanup polling on unmount return () => { if (stopPollingRef.current) { stopPollingRef.current(); } }; }, []); const handleInstallClick = () => { setIsPolling(true); // Start polling for Ollama stopPollingRef.current = pollForOllama( async (status) => { setOllamaDetected(status.isRunning); setIsPolling(false); // Check for the model const modelAvailable = await hasModel(getPreferredModel()); setModelStatus(modelAvailable ? 'available' : 'not-available'); toastService.success({ title: 'Ollama Detected!', msg: 'Ollama is now running. You can connect to it.', }); }, 3000 // Check every 3 seconds ); }; const handleDownloadModel = async () => { setModelStatus('downloading'); setDownloadProgress({ status: 'Starting download...' }); const success = await pullOllamaModel(getPreferredModel(), (progress) => { setDownloadProgress(progress); }); if (success) { setModelStatus('available'); toastService.success({ title: 'Model Downloaded!', msg: `Successfully downloaded ${getPreferredModel()}`, }); } else { setModelStatus('not-available'); toastService.error({ title: 'Download Failed', msg: `Failed to download ${getPreferredModel()}. Please try again.`, traceback: '', }); } setDownloadProgress(null); }; const handleConnectOllama = async () => { setIsConnecting(true); try { // Set up Ollama configuration await upsert('GOOSE_PROVIDER', 'ollama', false); await upsert('GOOSE_MODEL', getPreferredModel(), false); await upsert('OLLAMA_HOST', 'localhost', false); toastService.success({ title: 'Success!', msg: `Connected to Ollama with ${getPreferredModel()} model.`, }); onSuccess(); } catch (error) { console.error('Failed to connect to Ollama:', error); toastService.error({ title: 'Connection Failed', msg: `Failed to connect to Ollama: ${errorMessage(error)}`, traceback: error instanceof Error ? error.stack || '' : '', }); setIsConnecting(false); } }; if (isChecking) { return (

Checking for Ollama...

); } return (
{/* Header with icon above heading - left aligned like onboarding cards */}

Ollama Setup

Ollama lets you run AI models for free, private and locally on your computer.

{ollamaDetected ? (
Ollama is detected and running
{modelStatus === 'checking' ? (
) : modelStatus === 'not-available' ? (

The {getPreferredModel()} model is not installed

This model is recommended for the best experience with Goose

) : modelStatus === 'downloading' ? (

Downloading {getPreferredModel()}...

{downloadProgress && ( <>

{downloadProgress.status}

{downloadProgress.total && downloadProgress.completed && (

{Math.round((downloadProgress.completed / downloadProgress.total) * 100)}%

)} )}
) : ( )}
) : (
Ollama is not detected on your system
{isPolling ? (

Waiting for Ollama to start...

Once Ollama is installed and running, we'll automatically detect it.

) : ( Install Ollama )}
)}
); }