Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

12.5 KB
Raw Blame
"use client";

import { useCallback, useEffect, useRef, useState } from "react";
import { toast, useFeatureState } from "@/features/appshell";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Button } from "@/components/ui/button";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { parseCustomProviderConfig, parseModelList, toggleModelInList } from "./custom-provider-config";
import { useSettingsWindow } from "../lib/window-context";

export type DiscoveredModel = { id: string; name: string };
export type DiscoveryFailure = "invalid-url" | "auth" | "empty";
type Reply = { ok?: boolean; models?: DiscoveredModel[]; error?: string };

export const DISCOVERY_MESSAGES: Record<DiscoveryFailure, string> = {
  "invalid-url": "That endpoint URL isn’t usable. Use a full https:// base URL (e.g. https://host/v1) that the server can reach.",
  auth: "That endpoint rejected the API key (401/403). Check the key, then retry Discover.",
  empty: "The endpoint answered but listed no models. Type the ids by hand or paste JSON instead.",
};

export function baseUrlLooksValid(value: string): boolean {
  try {
    const url = new URL(value.trim());
    return (url.protocol === "http:" || url.protocol === "https:") && Boolean(url.hostname);
  } catch {
    return false;
  }
}

/** Map a discovery response to a failure kind, or null when models came back.
 * The route reports endpoint failures as `{ok:false,error}` with HTTP 200, so the
 * status alone is not enough — the body has to be classified too. */
export function classifyDiscovery(status: number, body: Reply): DiscoveryFailure | null {
  const text = String(body.error ?? "");
  if (status === 401 || status === 403 || /HTTP (401|403)/.test(text)) return "auth";
  if (status >= 400) return "invalid-url"; // rejected body: malformed / unsafe URL
  if (body.ok === false) return /no models|invalid JSON/i.test(text) ? "empty" : "invalid-url";
  return Array.isArray(body.models) && body.models.length ? null : "empty";
}

/** The baseURL+key pair a discovery run is keyed by, or null until it is usable.
 * Keyed by the pair so a re-render cannot re-fire it. */
export function discoveryPairKey(baseURL: string, apiKey: string, protocol = "openai"): string | null {
  return baseUrlLooksValid(baseURL) && apiKey.trim() ? JSON.stringify([baseURL.trim(), apiKey.trim(), protocol]) : null;
}

/** Claim the pair BEFORE fetching; false means "this pair already ran". */
export function claimDiscovery(fired: Set<string>, pair: string | null): boolean {
  if (!pair || fired.has(pair)) return false;
  fired.add(pair);
  return true;
}

/** Every discovered id into the delimited models field. Connecting a provider
 * normally wants all of them; tapping chips one by one was the reported pain. */
export function modelFieldFromDiscovered(list: DiscoveredModel[]): string {
  return list.map((model) => model.id).join(", ");
}

export function CustomProviderForm({ onAdded }: { onAdded: () => void }) {
  const { winId, setSensitiveDirty } = useSettingsWindow(), [mode, setMode] = useFeatureState<"fields" | "json">(winId, "provider-mode", "fields");
  const [name, setName] = useFeatureState(winId, "provider-name", "");
  const [baseURL, setBaseURL] = useFeatureState(winId, "provider-url", "");
  const [key, setKey] = useFeatureState(winId, "provider-key", "", { sensitive: true });
  const [protocol, setProtocol] = useFeatureState(winId, "provider-protocol", "openai");
  const [models, setModels] = useFeatureState(winId, "provider-models", "");
  const [json, setJson] = useFeatureState(winId, "provider-json", "", { sensitive: true });
  const [busy, setBusy] = useFeatureState(winId, "provider-submitting", false), [found, setFound] = useState<DiscoveredModel[]>([]);
  const [scanning, setScanning] = useState(false);
  const [failure, setFailure] = useState<DiscoveryFailure | null>(null);
  const autoFired = useRef<Set<string>>(new Set()); // pairs already auto-run
  const abortRef = useRef<AbortController | null>(null);
  useEffect(() => setSensitiveDirty(Boolean(key || json)), [key, json, setSensitiveDirty]);
  const cancelDiscovery = () => { autoFired.current.clear(); abortRef.current?.abort(); setScanning(false); setFound([]); setFailure(null); };
  const run = useCallback(async (signal: AbortSignal, target: string, apiKey: string, proto: string) => {
    if (!baseUrlLooksValid(target) || !apiKey.trim()) {
      setFailure("invalid-url");
      return;
    }
    setScanning(true);
    setFailure(null);
    try {
      const response = await fetch("/api/models/discover", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ baseURL: target, apiKey, protocol: proto }),
        signal,
      });
      const body = (await response.json().catch(() => ({}))) as Reply;
      if (signal.aborted) return;
      const kind = classifyDiscovery(response.status, body);
      if (kind) {
        setFound([]);
        setFailure(kind);
        return;
      }
      const list = body.models ?? [];
      setFound(list);
      setModels(modelFieldFromDiscovered(list));
      toast(`Discovered ${list.length} model${list.length === 1 ? "" : "s"}, all selected`);
    } catch {
      if (!signal.aborted) setFailure("invalid-url");
    } finally {
      if (!signal.aborted) setScanning(false);
    }
  }, [setModels]);

  const start = useCallback((target: string, apiKey: string, proto: string) => {
    abortRef.current?.abort(); // one discovery in flight, cancellable
    const controller = new AbortController();
    abortRef.current = controller;
    void run(controller.signal, target, apiKey, proto);
  }, [run]);

  // Auto-discovery: 700 ms debounce, pair claimed before the fetch, aborted when
  // the pair changes or the form unmounts. Never on every keystroke, never twice.
  useEffect(() => {
    abortRef.current?.abort();
    if (mode !== "fields") return;
    const pair = discoveryPairKey(baseURL, key, protocol);
    if (!pair || autoFired.current.has(pair)) return;
    const timer = setTimeout(() => {
      if (claimDiscovery(autoFired.current, pair)) start(baseURL.trim(), key.trim(), protocol);
    }, 700);
    return () => { window.clearTimeout(timer); abortRef.current?.abort(); };
  }, [baseURL, key, mode, protocol, start]);

  useEffect(() => () => abortRef.current?.abort(), []);
  // The route re-validates everything (SSRF, slug, key); this only reports it.
  async function submit() {
    if (busy) return;
    abortRef.current?.abort(); setScanning(false);
    setBusy(true);
    try {
      const payload = mode === "json"
        ? parseCustomProviderConfig(json) // throws on bad JSON / missing fields
        : { name, baseURL, apiKey: key, protocol, models: parseModelList(models) };
      const response = await fetch("/api/config", {
        method: "POST", headers: { "content-type": "application/json" },
        body: JSON.stringify({ customProvider: payload }),
      });
      const body = (await response.json().catch(() => ({}))) as { slug?: string; error?: string };
      if (!response.ok) return void toast(body.error || "Couldn’t add provider", { tone: "error" });
      setName(""); setBaseURL(""); setKey(""); setModels(""); setJson(""); setFound([]); setFailure(null);
      setSensitiveDirty(false);
      autoFired.current.clear(); // a fresh form starts with a fresh budget
      toast(`Added ${body.slug ?? "provider"}`);
      onAdded();
    } catch (e) {
      toast((e as Error).message || "Invalid config", { tone: "error" });
    } finally { setBusy(false); }
  }

  const selectedIds = parseModelList(models);
  const canSubmit = !busy && (mode === "json" ? !!json.trim() : !!name && !!baseURL && !!key);
  const ghost = "h-auto p-0 text-xs hover:bg-transparent hover:underline [@media(pointer:coarse)]:min-h-[44px]";
  return (
    <div className="space-y-2 rounded-lg border border-border p-3">
      <div className="flex items-center justify-between text-xs text-muted-foreground">
        <span>Custom endpoint: OpenAI-compatible or Anthropic Messages</span>
        <Button type="button" variant="ghost" disabled={busy} className={`${ghost} text-primary`} onClick={() => { cancelDiscovery(); setMode(mode === "fields" ? "json" : "fields"); }}>
          {mode === "fields" ? "paste JSON" : "use fields"}
        </Button>
      </div>
      <p className="text-xs text-muted-foreground">Unsaved credentials stay in this window until you save, discard or close it. Reloading clears them.</p>
      {mode === "fields" ? (
        <div className="space-y-2">
          <div className="flex flex-wrap gap-2">
            <Input disabled={busy} aria-label="Provider name" placeholder="name (e.g. my-llm)" value={name} onChange={(e) => setName(e.target.value)} className="w-40" />
            <Select disabled={busy} value={protocol} onValueChange={(value) => { cancelDiscovery(); setProtocol(value); }}>
              <SelectTrigger aria-label="Endpoint protocol" className="w-52"><SelectValue /></SelectTrigger>
              <SelectContent>
                <SelectItem value="openai">OpenAI /chat/completions</SelectItem>
                <SelectItem value="anthropic">Anthropic /v1/messages</SelectItem>
              </SelectContent>
            </Select>
          </div>
          <Input disabled={busy} aria-label="Base URL" placeholder="https://host/v1" value={baseURL} onChange={(e) => { cancelDiscovery(); setBaseURL(e.target.value); }} />
          <Input disabled={busy} aria-label="API key" type="password" placeholder="API key" value={key} onChange={(e) => { cancelDiscovery(); setKey(e.target.value); setSensitiveDirty(Boolean(e.target.value || json)); }} />
          <div className="flex items-center gap-2">
            <Input disabled={busy} aria-label="Models" placeholder="models: comma/newline, e.g. minimax-m3, gpt-4o" value={models} onChange={(e) => setModels(e.target.value)} className="flex-1" />
            <Button type="button" variant="outline" size="sm" className="[@media(pointer:coarse)]:min-h-[44px]" disabled={busy || scanning || !baseURL || !key} onClick={() => start(baseURL, key, protocol)}>
              {scanning ? "Discovering…" : "Discover models"}
            </Button>
          </div>
          {failure ? <p role="alert" className="text-xs text-destructive">{DISCOVERY_MESSAGES[failure]}</p> : null}
          {found.length > 0 && (
            <div className="space-y-1.5">
              <div className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
                <span>{found.length} found · {selectedIds.length} selected</span>
                <Button type="button" variant="ghost" size="sm" disabled={busy} className={`${ghost} text-primary`} onClick={() => setModels(modelFieldFromDiscovered(found))}>Select all</Button>
                <Button type="button" variant="ghost" size="sm" disabled={busy} className={ghost} onClick={() => setModels("")}>Clear</Button>
              </div>
              <div className="flex flex-wrap gap-1.5">
                {found.map((model) => (
                  <button key={model.id} type="button" disabled={busy} title={model.name} aria-pressed={selectedIds.includes(model.id)}
                    onClick={() => setModels((prev) => toggleModelInList(prev, model.id))}
                    className={`rounded-md border px-2 py-1 font-mono text-xs [@media(pointer:coarse)]:min-h-[44px] ${selectedIds.includes(model.id) ? "border-primary bg-primary/10 text-primary" : "border-border text-muted-foreground"}`}>
                    {model.id}
                  </button>
                ))}
              </div>
            </div>
          )}
        </div>
      ) : (
        <Textarea disabled={busy} rows={7} className="font-mono text-xs" value={json} aria-label="Custom provider JSON"
          onChange={(e) => { setJson(e.target.value); setSensitiveDirty(Boolean(e.target.value || key)); }}
          placeholder={'{\n  "name": "ai-hub",\n  "baseURL": "https://host/v1",\n  "apiKey": "sk-…",\n  "protocol": "openai",\n  "models": ["minimax-m3", "gpt-4o"]\n}'} />
      )}
      <div className="flex justify-end">
        <Button type="button" variant="ghost" size="sm" disabled={busy} onClick={() => { abortRef.current?.abort(); setScanning(false); autoFired.current.clear(); setName(""); setBaseURL(""); setKey(""); setModels(""); setJson(""); setFound([]); setFailure(null); setSensitiveDirty(false); }}>Discard</Button>
        <Button size="sm" className="[@media(pointer:coarse)]:min-h-[44px]" disabled={!canSubmit} onClick={submit}>{busy ? "Adding…" : "Add provider"}</Button>
      </div>
    </div>
  );
}