-
Notifications
You must be signed in to change notification settings - Fork 537
feat: simplify connection status check for Ollama and LM Studio #3804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fdaaad5
18b1f1e
85bfe9d
c13d287
589f3b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||||||||||||||||||||
| import { useQuery } from "@tanstack/react-query"; | ||||||||||||||||||||||||
| import { fetch as tauriFetch } from "@tauri-apps/plugin-http"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import * as settings from "../../../../store/tinybase/store/settings"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export type LocalProviderStatus = "connected" | "disconnected" | "checking"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const LOCAL_PROVIDERS = new Set(["ollama", "lmstudio"]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const DEFAULT_URLS: Record<string, string> = { | ||||||||||||||||||||||||
| ollama: "http://127.0.0.1:11434/v1", | ||||||||||||||||||||||||
| lmstudio: "http://127.0.0.1:1234/v1", | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| async function checkConnection(baseUrl: string): Promise<boolean> { | ||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||
| const timeout = setTimeout(() => controller.abort(), 2000); | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| const origin = new URL(baseUrl).origin; | ||||||||||||||||||||||||
| const res = await tauriFetch(`${baseUrl}/models`, { | ||||||||||||||||||||||||
| signal: controller.signal, | ||||||||||||||||||||||||
| headers: { Origin: origin }, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| return res.ok; | ||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||
| clearTimeout(timeout); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export function useLocalProviderStatus(providerId: string): { | ||||||||||||||||||||||||
| status: LocalProviderStatus | null; | ||||||||||||||||||||||||
| refetch: () => void; | ||||||||||||||||||||||||
| } { | ||||||||||||||||||||||||
| const isLocal = LOCAL_PROVIDERS.has(providerId); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const configuredProviders = settings.UI.useResultTable( | ||||||||||||||||||||||||
| settings.QUERIES.llmProviders, | ||||||||||||||||||||||||
| settings.STORE_ID, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| const baseUrl = String( | ||||||||||||||||||||||||
| configuredProviders[providerId]?.base_url || DEFAULT_URLS[providerId] || "", | ||||||||||||||||||||||||
| ).trim(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const query = useQuery({ | ||||||||||||||||||||||||
| enabled: isLocal && !!baseUrl, | ||||||||||||||||||||||||
| queryKey: ["local-provider-status", providerId, baseUrl], | ||||||||||||||||||||||||
| queryFn: () => checkConnection(baseUrl), | ||||||||||||||||||||||||
| staleTime: 10_000, | ||||||||||||||||||||||||
| refetchInterval: 15_000, | ||||||||||||||||||||||||
| retry: false, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!isLocal) { | ||||||||||||||||||||||||
| return { status: null, refetch: () => {} }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const status: LocalProviderStatus = query.isLoading | ||||||||||||||||||||||||
| ? "checking" | ||||||||||||||||||||||||
| : query.data | ||||||||||||||||||||||||
| ? "connected" | ||||||||||||||||||||||||
| : "disconnected"; | ||||||||||||||||||||||||
|
Comment on lines
+59
to
+63
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 When the user clicks the "Connect" button to recheck a local provider's status, there is no visual feedback because the Root CauseIn React Query, The status derivation at const status: LocalProviderStatus = query.isLoading
? "checking"
: query.data
? "connected"
: "disconnected";After the first failed check, clicking "Connect" triggers
This defeats the intended behavior at Impact: The "Connect" button remains clickable with no loading indicator while the recheck is in progress, giving the user no feedback that anything is happening. Users may click it repeatedly.
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return { status, refetch: () => void query.refetch() }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Origin header construction creates invalid URL
The code constructs an origin from
baseUrl, then sends it to an endpoint derived from the samebaseUrl. This creates a mismatch:http://127.0.0.1:11434(extracted from baseUrl)http://127.0.0.1:11434/v1/modelsThe Origin header should match the origin of the page making the request, not the target server. In Tauri apps, this should typically be the app's origin (e.g.,
tauri://localhost).This could cause CORS preflight failures or unexpected behavior depending on how LM Studio/Ollama validate the Origin header.
Spotted by Graphite Agent

Is this helpful? React 👍 or 👎 to let us know.