check ollama status every 3s

This commit is contained in:
Test 2024-09-22 20:03:51 -07:00
parent cd93dbf156
commit 7eed894d71
3 changed files with 32 additions and 64 deletions

View file

@ -1,2 +1,2 @@
export * from "./useCheckOllamaModels";
export * from "./useSubmitOnboarding";
export * from "./useOnboardingCard";

View file

@ -1,58 +0,0 @@
import { useContext, useEffect, useRef } from "react";
import { IdeMessengerContext } from "../../../context/IdeMessenger";
import { ONBOARDING_LOCAL_MODEL_TITLE } from "core/config/onboarding";
const MAX_RETRIES = 10;
const INITIAL_DELAY = 1000;
export const useCheckOllamaModels = (
onDownloadedModels: (models: string[]) => void,
) => {
const ideMessenger = useContext(IdeMessengerContext);
const retryCountRef = useRef(0);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
const fetchDownloadedModels = async () => {
try {
const result = (await ideMessenger.request("llm/listModels", {
title: ONBOARDING_LOCAL_MODEL_TITLE,
})) as any;
if (result.status === "success") {
const models = result.content;
if (Array.isArray(models)) {
onDownloadedModels(models);
return;
}
}
throw new Error("Failed to fetch models");
} catch (error) {
if (retryCountRef.current < MAX_RETRIES) {
const delay = INITIAL_DELAY * Math.pow(2, retryCountRef.current);
console.debug(
`Failed to connect to Ollama - retrying in ${delay}ms...`,
);
timeoutRef.current = setTimeout(() => {
retryCountRef.current += 1;
fetchDownloadedModels();
}, delay);
} else {
console.debug(
`Max retries (${MAX_RETRIES}) reached. Unable to fetch Ollama models.`,
);
}
}
};
fetchDownloadedModels();
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, [ideMessenger, onDownloadedModels]);
};

View file

@ -13,7 +13,9 @@ import { hasPassedFTL } from "../../../util/freeTrial";
import AddModelButtonSubtext from "../../AddModelButtonSubtext";
import OllamaModelDownload from "../components/OllamaModelDownload";
import { OllamaStatus } from "../components/OllamaStatus";
import { useCheckOllamaModels, useSubmitOnboarding } from "../hooks";
import { useSubmitOnboarding } from "../hooks";
const OLLAMA_CHECK_INTERVAL_MS = 3000;
function OnboardingLocalTab() {
const dispatch = useDispatch();
@ -52,10 +54,34 @@ function OnboardingLocalTab() {
}
}, [downloadedOllamaModels, isOllamaConnected]);
useCheckOllamaModels((models) => {
setDownloadedOllamaModels(models);
setIsOllamaConnected(true);
});
useEffect(() => {
const fetchDownloadedModels = async () => {
try {
const result = (await ideMessenger.request("llm/listModels", {
title: ONBOARDING_LOCAL_MODEL_TITLE,
})) as any;
if (result.status === "success" && Array.isArray(result.content)) {
setDownloadedOllamaModels(result.content);
setIsOllamaConnected(true);
} else {
throw new Error("Failed to fetch models");
}
} catch (error) {
console.error("Error fetching models:", error);
setIsOllamaConnected(false);
}
};
const intervalId = setInterval(
fetchDownloadedModels,
OLLAMA_CHECK_INTERVAL_MS,
);
fetchDownloadedModels();
return () => clearInterval(intervalId);
}, []);
return (
<div className="flex flex-col gap-2">