Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

fix: Auto load ollama model & lint errors

Khuram Niaz33782699f93a
diff --git a/__tests__/AiJobMatchSection.spec.tsx b/__tests__/AiJobMatchSection.spec.tsx
index 0a612dd..2764c7b 100644
--- a/__tests__/AiJobMatchSection.spec.tsx
+++ b/__tests__/AiJobMatchSection.spec.tsx
@@ -36,7 +36,7 @@ jest.mock("@/actions/userSettings.actions", () => ({
}));
jest.mock("@/utils/ai.utils", () => ({
- checkIfModelIsRunning: jest.fn().mockResolvedValue({ isRunning: false }),
+ checkOllamaConnection: jest.fn().mockResolvedValue({ isConnected: true }),
}));
const mockToast = jest.fn();
diff --git a/src/components/profile/AiJobMatchSection.tsx b/src/components/profile/AiJobMatchSection.tsx
index 5df3138..c25444c 100644
--- a/src/components/profile/AiJobMatchSection.tsx
+++ b/src/components/profile/AiJobMatchSection.tsx
@@ -31,7 +31,7 @@ import {
TooltipTrigger,
} from "../ui/tooltip";
import { Info, CheckCircle, XCircle } from "lucide-react";
-import { checkIfModelIsRunning } from "@/utils/ai.utils";
+import { checkOllamaConnection } from "@/utils/ai.utils";
import { JobMatchSchema } from "@/models/ai.schemas";
import { getUserSettings } from "@/actions/userSettings.actions";
@@ -49,8 +49,8 @@ export const AiJobMatchSection = ({
onMatchSaved,
}: AiSectionProps) => {
const [selectedResumeId, setSelectedResumeId] = useState<string>();
- const [runningModelName, setRunningModelName] = useState<string>("");
- const [runningModelError, setRunningModelError] = useState<string>("");
+ const [ollamaConnected, setOllamaConnected] = useState<boolean | null>(null);
+ const [connectionError, setConnectionError] = useState<string>("");
const [selectedModel, setSelectedModel] = useState<AiModel>(defaultModel);
const [isLoadingSettings, setIsLoadingSettings] = useState(true);
const resumesRef = useRef<Resume[]>([]);
@@ -147,19 +147,17 @@ export const AiJobMatchSection = ({
submit({ resumeId, jobId, selectedModel });
};
- const checkModelStatus = useCallback(async () => {
- setRunningModelName("");
- setRunningModelError("");
- const result = await checkIfModelIsRunning(
- selectedModel.model,
- selectedModel.provider,
- );
- if (result.isRunning && result.runningModelName) {
- setRunningModelName(result.runningModelName);
- } else if (result.error) {
- setRunningModelError(result.error);
+ const checkConnectionStatus = useCallback(async () => {
+ setOllamaConnected(null);
+ setConnectionError("");
+ const result = await checkOllamaConnection(selectedModel.provider);
+ if (result.isConnected) {
+ setOllamaConnected(true);
+ } else {
+ setOllamaConnected(false);
+ setConnectionError(result.error || "Ollama is not reachable.");
}
- }, [selectedModel.model, selectedModel.provider]);
+ }, [selectedModel.provider]);
const onOpenChange = async (openState: boolean) => {
triggerChange(openState);
@@ -167,10 +165,10 @@ export const AiJobMatchSection = ({
stop();
}
if (openState && selectedModel.provider === "ollama") {
- await checkModelStatus();
+ await checkConnectionStatus();
} else if (!openState) {
- setRunningModelName("");
- setRunningModelError("");
+ setOllamaConnected(null);
+ setConnectionError("");
setSelectedResumeId(undefined);
}
};
@@ -186,9 +184,9 @@ export const AiJobMatchSection = ({
useEffect(() => {
if (aISectionOpen && selectedModel.provider === "ollama") {
- checkModelStatus();
+ checkConnectionStatus();
}
- }, [aISectionOpen, selectedModel.provider, checkModelStatus]);
+ }, [aISectionOpen, selectedModel.provider, checkConnectionStatus]);
// Check if we have any content to show
const hasContent =
@@ -217,16 +215,16 @@ export const AiJobMatchSection = ({
{selectedModel.provider === "ollama" && (
<>
- {runningModelName && (
+ {ollamaConnected === true && (
<div className="flex items-center gap-1 text-green-600 text-sm mt-4">
<CheckCircle className="h-4 w-4 flex-shrink-0" />
- <span>{runningModelName} is running</span>
+ <span>Ollama is connected</span>
</div>
)}
- {runningModelError && (
+ {ollamaConnected === false && (
<div className="flex items-center gap-1 text-red-600 text-sm mt-4">
<XCircle className="h-4 w-4 flex-shrink-0" />
- <span>{runningModelError}</span>
+ <span>{connectionError}</span>
</div>
)}
</>
@@ -240,7 +238,7 @@ export const AiJobMatchSection = ({
disabled={
isLoading ||
isLoadingSettings ||
- (selectedModel.provider === "ollama" && !runningModelName)
+ (selectedModel.provider === "ollama" && ollamaConnected === false)
}
>
<SelectTrigger className="w-[180px]">
diff --git a/src/components/profile/AiResumeReviewSection.tsx b/src/components/profile/AiResumeReviewSection.tsx
index c3972d9..21444b8 100644
--- a/src/components/profile/AiResumeReviewSection.tsx
+++ b/src/components/profile/AiResumeReviewSection.tsx
@@ -23,7 +23,7 @@ import {
TooltipProvider,
TooltipTrigger,
} from "../ui/tooltip";
-import { checkIfModelIsRunning } from "@/utils/ai.utils";
+import { checkOllamaConnection } from "@/utils/ai.utils";
import { ResumeReviewSchema } from "@/models/ai.schemas";
import { getUserSettings } from "@/actions/userSettings.actions";
@@ -33,8 +33,8 @@ interface AiSectionProps {
const AiResumeReviewSection = ({ resume }: AiSectionProps) => {
const [aISectionOpen, setAiSectionOpen] = useState(false);
- const [runningModelName, setRunningModelName] = useState<string>("");
- const [runningModelError, setRunningModelError] = useState<string>("");
+ const [ollamaConnected, setOllamaConnected] = useState<boolean | null>(null);
+ const [connectionError, setConnectionError] = useState<string>("");
const [selectedModel, setSelectedModel] = useState<AiModel>(defaultModel);
const [isLoadingSettings, setIsLoadingSettings] = useState(true);
@@ -89,21 +89,19 @@ const AiResumeReviewSection = ({ resume }: AiSectionProps) => {
if (!openState && isLoading) {
stop();
} else if (openState && selectedModel.provider === "ollama") {
- await checkModelStatus();
+ await checkConnectionStatus();
}
};
- const checkModelStatus = async () => {
- setRunningModelName("");
- setRunningModelError("");
- const result = await checkIfModelIsRunning(
- selectedModel.model,
- selectedModel.provider
- );
- if (result.isRunning && result.runningModelName) {
- setRunningModelName(result.runningModelName);
- } else if (result.error) {
- setRunningModelError(result.error);
+ const checkConnectionStatus = async () => {
+ setOllamaConnected(null);
+ setConnectionError("");
+ const result = await checkOllamaConnection(selectedModel.provider);
+ if (result.isConnected) {
+ setOllamaConnected(true);
+ } else {
+ setOllamaConnected(false);
+ setConnectionError(result.error || "Ollama is not reachable.");
}
};
@@ -149,16 +147,16 @@ const AiResumeReviewSection = ({ resume }: AiSectionProps) => {
{selectedModel.provider === "ollama" && (
<>
- {runningModelName && (
+ {ollamaConnected === true && (
<div className="flex items-center gap-1 text-green-600 text-sm mt-4">
<CheckCircle className="h-4 w-4 flex-shrink-0" />
- <span>{runningModelName} is running</span>
+ <span>Ollama is connected</span>
</div>
)}
- {runningModelError && (
+ {ollamaConnected === false && (
<div className="flex items-center gap-1 text-red-600 text-sm mt-4">
<XCircle className="h-4 w-4 flex-shrink-0" />
- <span>{runningModelError}</span>
+ <span>{connectionError}</span>
</div>
)}
</>
@@ -172,7 +170,7 @@ const AiResumeReviewSection = ({ resume }: AiSectionProps) => {
onClick={getResumeReview}
disabled={
isLoading ||
- (selectedModel.provider === "ollama" && !runningModelName)
+ (selectedModel.provider === "ollama" && ollamaConnected === false)
}
>
<Sparkles className="h-3.5 w-3.5" />
diff --git a/src/components/settings/AiSettings.tsx b/src/components/settings/AiSettings.tsx
index cf3980d..d5ce006 100644
--- a/src/components/settings/AiSettings.tsx
+++ b/src/components/settings/AiSettings.tsx
@@ -23,8 +23,8 @@ import {
import { Label } from "../ui/label";
import { Button } from "../ui/button";
import { toast } from "../ui/use-toast";
-import { XCircle, CheckCircle, Loader2 } from "lucide-react";
-import { checkIfModelIsRunning } from "@/utils/ai.utils";
+import { XCircle, Loader2 } from "lucide-react";
+import { checkOllamaConnection } from "@/utils/ai.utils";
import { getUserSettings, updateAiSettings } from "@/actions/userSettings.actions";
function AiSettings() {
@@ -35,34 +35,18 @@ function AiSettings() {
const [isSaving, setIsSaving] = useState(false);
const [isInitialized, setIsInitialized] = useState(false);
const [fetchError, setFetchError] = useState<string>("");
- const [runningModelError, setRunningModelError] = useState<string>("");
- const [runningModelName, setRunningModelName] = useState<string>("");
+ const [connectionError, setConnectionError] = useState<string>("");
const currentEntry = PROVIDER_REGISTRY[selectedModel.provider];
const setSelectedProvider = (provider: AiProvider) => {
setSelectedModel({ provider, model: undefined });
setFetchError("");
- setRunningModelError("");
- setRunningModelName("");
+ setConnectionError("");
};
- const setSelectedProviderModel = async (model: string) => {
+ const setSelectedProviderModel = (model: string) => {
setSelectedModel({ ...selectedModel, model });
- setRunningModelName("");
- setRunningModelError("");
-
- if (currentEntry?.requiresRunningCheck) {
- const result = await checkIfModelIsRunning(model, selectedModel.provider);
- if (result.isRunning && result.runningModelName) {
- setRunningModelName(result.runningModelName);
- if (currentEntry.supportsKeepAlive) {
- await keepModelAlive(result.runningModelName);
- }
- } else if (result.error) {
- setRunningModelError(result.error);
- }
- }
};
useEffect(() => {
@@ -111,7 +95,6 @@ function AiSettings() {
} else {
setFetchedModels(getFallbackModels(selectedModel.provider));
}
- // eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedModel.provider, isInitialized]);
const fetchModels = async (entry: typeof currentEntry) => {
@@ -119,7 +102,16 @@ function AiSettings() {
const fallback = getFallbackModels(selectedModel.provider);
setIsLoadingModels(true);
setFetchError("");
+ setConnectionError("");
try {
+ if (entry.category === "local") {
+ const connResult = await checkOllamaConnection(selectedModel.provider as AiProvider);
+ if (!connResult.isConnected) {
+ setFetchedModels(fallback);
+ setConnectionError(connResult.error || "Ollama is not reachable.");
+ return;
+ }
+ }
const response = await fetch(`/api/ai/${entry.modelsEndpoint}`);
if (!response.ok) {
setFetchedModels(fallback);
@@ -133,10 +125,6 @@ function AiSettings() {
const data = await response.json();
const models = entry.parseModelsResponse?.(data) ?? [];
setFetchedModels(models.length > 0 ? models : fallback);
-
- if (entry.requiresRunningCheck) {
- await fetchRunningModel();
- }
} catch (error) {
console.error(`Error fetching ${entry.displayName} models:`, error);
setFetchedModels(fallback);
@@ -150,72 +138,6 @@ function AiSettings() {
}
};
- const keepModelAlive = async (modelName: string) => {
- try {
- await fetch("/api/ai/ollama/generate", {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({
- model: modelName,
- prompt: "",
- keep_alive: "1h",
- stream: false,
- }),
- });
- } catch (error) {
- console.error("Error keeping model alive:", error);
- }
- };
-
- const fetchRunningModel = async () => {
- setRunningModelError("");
- setRunningModelName("");
- try {
- const response = await fetch("/api/ai/ollama/ps");
- if (!response.ok) {
- if (currentEntry?.requiresRunningCheck) {
- setRunningModelError(
- "No model is currently running. Please start a model first.",
- );
- }
- return;
- }
- const data = await response.json();
- if (data.models && data.models.length > 0) {
- const runningModel = data.models[0].name;
- setSelectedModel({
- provider: AiProvider.OLLAMA,
- model: runningModel,
- });
- const result = await checkIfModelIsRunning(
- runningModel,
- AiProvider.OLLAMA,
- );
- if (result.isRunning && result.runningModelName) {
- setRunningModelName(result.runningModelName);
- if (currentEntry?.supportsKeepAlive) {
- await keepModelAlive(result.runningModelName);
- }
- } else if (result.error) {
- setRunningModelError(result.error);
- }
- } else {
- if (currentEntry?.requiresRunningCheck) {
- setRunningModelError(
- `No model is currently running. Please run the ${currentEntry.displayName} model first.`,
- );
- }
- }
- } catch (error) {
- console.error("Error fetching running model:", error);
- if (currentEntry?.requiresRunningCheck) {
- setRunningModelError(
- `No model is currently running. Please run the ${currentEntry.displayName} model first.`,
- );
- }
- }
- };
-
const saveModelSettings = async () => {
if (!selectedModel.model) {
toast({
@@ -347,29 +269,18 @@ function AiSettings() {
<span>{fetchError}</span>
</div>
)}
+ {connectionError && (
+ <div className="flex items-center gap-1 text-red-600 text-sm mt-2">
+ <XCircle className="h-4 w-4 flex-shrink-0" />
+ <span>{connectionError}</span>
+ </div>
+ )}
</div>
- {runningModelName && (
- <div className="flex items-center gap-1 text-green-600 text-sm mt-2">
- <CheckCircle className="h-4 w-4 flex-shrink-0" />
- <span>{runningModelName} is running</span>
- </div>
- )}
- {runningModelError && (
- <div className="flex items-center gap-1 text-red-600 text-sm mt-2">
- <XCircle className="h-4 w-4 flex-shrink-0" />
- <span>{runningModelError}</span>
- </div>
- )}
</div>
<Button
className="mt-4"
onClick={saveModelSettings}
- disabled={
- !selectedModel.model ||
- (currentEntry?.requiresRunningCheck && !runningModelName) ||
- isLoadingModels ||
- isSaving
- }
+ disabled={!selectedModel.model || isLoadingModels || isSaving}
>
{isSaving && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Save
diff --git a/src/components/settings/ApiKeySettings.tsx b/src/components/settings/ApiKeySettings.tsx
index ebfcbf4..9bc84d2 100644
--- a/src/components/settings/ApiKeySettings.tsx
+++ b/src/components/settings/ApiKeySettings.tsx
@@ -24,7 +24,7 @@ import {
AlertDialogTitle,
AlertDialogTrigger,
} from "../ui/alert-dialog";
-import { Loader2, Plus, Trash2, CheckCircle } from "lucide-react";
+import { Loader2, Plus, Trash2, CheckCircle, XCircle, RefreshCw } from "lucide-react";
import {
getUserApiKeys,
saveApiKey,
@@ -36,6 +36,8 @@ import type {
ApiKeyProvider,
} from "@/models/apiKey.model";
import { getAiProviders } from "@/lib/ai/provider-registry";
+import { AiProvider } from "@/models/ai.model";
+import { checkOllamaConnection } from "@/utils/ai.utils";
interface ProviderConfig {
id: ApiKeyProvider;
@@ -77,10 +79,20 @@ function ApiKeySettings() {
const [inputValue, setInputValue] = useState("");
const [verifying, setVerifying] = useState(false);
const [deleting, setDeleting] = useState<string | null>(null);
+ const [ollamaConnected, setOllamaConnected] = useState<boolean | null>(null);
+ const [ollamaChecking, setOllamaChecking] = useState(false);
+
+ const recheckOllamaConnection = async () => {
+ setOllamaChecking(true);
+ const result = await checkOllamaConnection(AiProvider.OLLAMA);
+ setOllamaConnected(result.isConnected);
+ setOllamaChecking(false);
+ };
useEffect(() => {
fetchKeys();
getDefaultOllamaBaseUrl().then(setDefaultOllamaUrl);
+ recheckOllamaConnection();
}, []);
const fetchKeys = async () => {
@@ -250,6 +262,37 @@ function ApiKeySettings() {
)}
</div>
</CardHeader>
+ {provider.id === "ollama" && (
+ <div className="px-6 pb-3">
+ <div className="flex items-center gap-2">
+ {ollamaChecking ? (
+ <div className="flex items-center gap-1 text-muted-foreground text-sm">
+ <Loader2 className="h-3.5 w-3.5 animate-spin" />
+ <span>Checking...</span>
+ </div>
+ ) : ollamaConnected === true ? (
+ <div className="flex items-center gap-1 text-green-600 text-sm">
+ <CheckCircle className="h-3.5 w-3.5 flex-shrink-0" />
+ <span>Ollama is running</span>
+ </div>
+ ) : ollamaConnected === false ? (
+ <div className="flex items-center gap-1 text-red-600 text-sm">
+ <XCircle className="h-3.5 w-3.5 flex-shrink-0" />
+ <span>Ollama is not running</span>
+ </div>
+ ) : null}
+ <Button
+ size="sm"
+ variant="ghost"
+ className="h-6 w-6 p-0"
+ onClick={recheckOllamaConnection}
+ disabled={ollamaChecking}
+ >
+ <RefreshCw className={`h-3.5 w-3.5 ${ollamaChecking ? "animate-spin" : ""}`} />
+ </Button>
+ </div>
+ </div>
+ )}
<CardContent>
{isEditing ? (
<div className="space-y-3">
diff --git a/src/lib/ai/provider-registry.ts b/src/lib/ai/provider-registry.ts
index 722a2a9..7d2764e 100644
--- a/src/lib/ai/provider-registry.ts
+++ b/src/lib/ai/provider-registry.ts
@@ -30,7 +30,7 @@ export const PROVIDER_REGISTRY: Record<string, ProviderRegistryEntry> = {
defaultCredential: "http://127.0.0.1:11434",
modelsEndpoint: "ollama/tags",
parseModelsResponse: (data) => data.models?.map((m: any) => m.name) ?? [],
- requiresRunningCheck: true,
+ requiresRunningCheck: false,
supportsKeepAlive: true,
keyConfig: {
placeholder: "http://127.0.0.1:11434",
diff --git a/src/utils/ai.utils.ts b/src/utils/ai.utils.ts
index 7153d1a..3c4d352 100644
--- a/src/utils/ai.utils.ts
+++ b/src/utils/ai.utils.ts
@@ -5,105 +5,40 @@ import { AiProvider } from "@/models/ai.model";
export { convertResumeToText } from "@/lib/ai/tools/preprocessing";
export { convertJobToText } from "@/lib/ai/tools/preprocessing-job";
-export interface ModelCheckResult {
- isRunning: boolean;
+export interface OllamaConnectionResult {
+ isConnected: boolean;
error?: string;
- runningModelName?: string;
}
-/**
- * Check if an Ollama model is currently running
- * @param modelName - The name of the model to check
- * @param provider - The AI provider (only checks for Ollama)
- * @returns ModelCheckResult with isRunning status and optional error message
- */
-export const checkIfModelIsRunning = async (
- modelName: string | undefined,
+// Checks if the Ollama service is reachable
+export const checkOllamaConnection = async (
provider: AiProvider,
-): Promise<ModelCheckResult> => {
- // Only check for Ollama provider
+): Promise<OllamaConnectionResult> => {
if (provider !== AiProvider.OLLAMA) {
- return { isRunning: true };
- }
-
- if (!modelName) {
- return {
- isRunning: false,
- error: "No model selected. Please select an AI model in settings first.",
- };
+ return { isConnected: true };
}
try {
- const response = await fetch("/api/ai/ollama/ps", {
+ const response = await fetch("/api/ai/ollama/tags", {
signal: AbortSignal.timeout(5000),
});
if (!response.ok) {
return {
- isRunning: false,
+ isConnected: false,
error:
"Ollama service is not responding. Please make sure Ollama is running.",
};
}
- const data = await response.json();
-
- if (!data.models || data.models.length === 0) {
- return {
- isRunning: false,
- error: `No Ollama model is currently running. Please start ${modelName} using: ollama run ${modelName}`,
- };
- }
-
- const isRunning = data.models.some((m: any) => m.name === modelName);
-
- if (!isRunning) {
- return {
- isRunning: false,
- error: `${modelName} is not currently running. Please run the model first.`,
- };
- }
-
- return { isRunning: true, runningModelName: modelName };
+ return { isConnected: true };
} catch (error) {
- console.error("Error checking if model is running:", error);
+ console.error("Error checking Ollama connection:", error);
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
return {
- isRunning: false,
+ isConnected: false,
error: `Cannot connect to Ollama service. Please make sure Ollama is running. Error: ${errorMessage}`,
};
}
};
-
-/**
- * Fetch list of all running Ollama models
- * @returns Array of model names currently running
- */
-export const fetchRunningModels = async (): Promise<{
- models: string[];
- error?: string;
-}> => {
- try {
- const response = await fetch("/api/ai/ollama/ps", {
- signal: AbortSignal.timeout(5000),
- });
-
- if (!response.ok) {
- return {
- models: [],
- error: "Failed to fetch running models. Make sure Ollama is running.",
- };
- }
-
- const data = await response.json();
- const models = data.models?.map((m: any) => m.name) || [];
- return { models };
- } catch (error) {
- console.error("Error fetching running models:", error);
- return {
- models: [],
- error: "Cannot connect to Ollama service.",
- };
- }
-};