| 1 | import type { ServerView } from "./types"; |
| 2 | |
| 3 | function isEnabled(s: ServerView): boolean { |
| 4 | if (typeof s.enabled === "boolean") return s.enabled; |
| 5 | if (s.startIntent === "off" || s.status === "disabled") return false; |
| 6 | if (s.configured && s.autoStart === false) return false; |
| 7 | return true; |
| 8 | } |
| 9 | |
| 10 | function runtimeState(s: ServerView): "idle" | "connecting" | "ready" | "issue" { |
| 11 | if (s.runtimeState === "idle" || s.runtimeState === "connecting" || s.runtimeState === "ready" || s.runtimeState === "issue") { |
| 12 | return s.runtimeState; |
| 13 | } |
| 14 | if (s.status === "connected") return "ready"; |
| 15 | if (s.status === "initializing") return "connecting"; |
| 16 | if (s.status === "failed") return "issue"; |
| 17 | return "idle"; |
| 18 | } |
| 19 | |
| 20 | export function mcpServerLifecycleActions(s: ServerView): { |
| 21 | enabled: boolean; |
| 22 | showRetryInRow: boolean; |
| 23 | canConnectNow: boolean; |
| 24 | canReconnect: boolean; |
| 25 | } { |
| 26 | const enabled = isEnabled(s); |
| 27 | const state = runtimeState(s); |
| 28 | return { |
| 29 | enabled: state === "ready" || enabled, |
| 30 | showRetryInRow: state === "issue" || s.action === "retry", |
| 31 | canConnectNow: !enabled && state !== "ready", |
| 32 | canReconnect: state === "ready" || state === "issue", |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | export function mcpServerRetryableFromAvailableList(s: ServerView): boolean { |
| 37 | if (s.status === "connected" || s.status === "disabled" || s.status === "failed") return false; |
| 38 | if (!isEnabled(s)) return false; |
| 39 | return runtimeState(s) === "connecting" || s.action === "retry"; |
| 40 | } |
| 41 | |
| 42 | /** Prefer product availability labels over legacy status strings. */ |
| 43 | export function mcpServerAvailability(s: ServerView): string { |
| 44 | if (s.availability) return s.availability; |
| 45 | if (!isEnabled(s)) return "disabled"; |
| 46 | switch (runtimeState(s)) { |
| 47 | case "ready": |
| 48 | return "connected"; |
| 49 | case "connecting": |
| 50 | return "starting"; |
| 51 | case "issue": |
| 52 | if (s.requiresLaunchApproval) return "project_auth_changed"; |
| 53 | if (s.authStatus === "required" || s.authStatus === "possible") return "auth_required"; |
| 54 | return "start_failed"; |
| 55 | default: |
| 56 | return "available_on_demand"; |
| 57 | } |
| 58 | } |
| 59 |