Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
"use client";
import { useEffect, useMemo, useState } from "react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { ChevronDown, Cpu, Loader2, Plus, Check, Info } from "@multica/ui/icons";
import {
refreshRuntimeModels,
runtimeModelsOptions,
} from "@multica/core/runtimes";
import type { RuntimeModel } from "@multica/core/types";
import {
Popover,
PopoverTrigger,
PopoverContent,
} from "@multica/ui/components/ui/popover";
import { Label } from "@multica/ui/components/ui/label";
import { useT } from "../../i18n";
import { UnavailableModelsNote } from "./unavailable-models-note";
import { ModelSearchHeader } from "./model-search-header";
// ModelDropdown renders a searchable, creatable model picker for an agent.
// It fetches the supported-model catalog from the selected runtime — the
// daemon enumerates models on demand via heartbeat piggyback. Providers
// whose runtime ignores per-agent model selection return supported=false,
// and the dropdown renders disabled with an explanation instead of silently
// accepting a value the backend would ignore. Today that is qwenpaw and mcode
// (agent.ModelSelectionSupported is the single source of truth for the set);
// Antigravity left it when agy 1.0.6 added `--model`.
//
// supported=false is a different state from discovery failing, and the two must
// not be conflated. A failed discovery throws out of resolveRuntimeModels, so
// modelsQuery.isError renders the discovery-failed notice and keeps the
// creatable manual-entry input below — which is exactly the fallback a user
// needs when their runtime could not enumerate anything (MUL-6606).
export function ModelDropdown({
runtimeId,
workspaceSlug,
runtimeOnline,
value,
onChange,
onAutomaticChange = onChange,
disabled,
}: {
runtimeId: string | null;
/** Target workspace when rendered before a workspace route is active. */
workspaceSlug?: string;
runtimeOnline: boolean;
value: string;
onChange: (value: string) => void;
/** Automatic value cleanup, separate from explicit user selections. */
onAutomaticChange?: (value: string) => void;
disabled?: boolean;
}) {
const { t } = useT("agents");
const queryClient = useQueryClient();
const [open, setOpen] = useState(false);
const [search, setSearch] = useState("");
const modelsQuery = useQuery(
runtimeModelsOptions(runtimeOnline ? runtimeId : null, workspaceSlug),
);
const supported = modelsQuery.data?.supported ?? true;
// Stable reference for the model list — `?? []` would mint a fresh
// array each render and force every downstream useMemo to invalidate.
const models = useMemo(
() => modelsQuery.data?.models ?? [],
[modelsQuery.data],
);
const grouped = useMemo(() => groupByProvider(models), [models]);
// Advisory only — never merged into `models`, so nothing below can select one.
const unavailableModels = useMemo(
() => modelsQuery.data?.unavailableModels ?? [],
[modelsQuery.data],
);
// resolveRuntimeModels throws the daemon's reported error text, so this is
// the runtime's own message (plus any hint the daemon appended). It is only
// ever read while isError is true.
const discoveryError =
modelsQuery.error instanceof Error
? modelsQuery.error.message.trim() || null
: null;
// When the selected runtime reports it doesn't support per-agent
// model selection, clear any previously-saved value so we don't
// persist a ghost configuration that never takes effect.
useEffect(() => {
if (!supported && value !== "") {
onAutomaticChange("");
}
}, [supported, value, onAutomaticChange]);
const filtered = useMemo(() => {
if (!search.trim()) return grouped;
const needle = search.toLowerCase();
const out: Record<string, RuntimeModel[]> = {};
for (const [provider, list] of Object.entries(grouped)) {
const matches = list.filter(
(m) =>
m.id.toLowerCase().includes(needle) ||
m.label.toLowerCase().includes(needle),
);
if (matches.length > 0) out[provider] = matches;
}
return out;
}, [grouped, search]);
const trimmedSearch = search.trim();
const exactMatch = models.some(
(m) => m.id === trimmedSearch || m.label === trimmedSearch,
);
const canCreate = trimmedSearch.length > 0 && !exactMatch;
const select = (id: string) => {
onChange(id);
setOpen(false);
setSearch("");
};
const refresh = () => {
if (!runtimeId || !runtimeOnline) return;
void refreshRuntimeModels(queryClient, runtimeId, workspaceSlug).catch(() => {
// React Query owns the error state rendered below. Swallow the returned
// promise rejection so a failed manual refresh is not also unhandled.
});
};
const triggerLabel =
value ||
(disabled
? t(($) => $.model_dropdown.select_runtime_first)
: runtimeOnline
? t(($) => $.model_dropdown.default_provider)
: t(($) => $.model_dropdown.runtime_offline_manual));
if (!supported && !modelsQuery.isLoading) {
return (
<div className="flex flex-col min-w-0">
<div className="flex h-6 items-center">
<Label className="text-caption text-muted-foreground">{t(($) => $.model_dropdown.label)}</Label>
</div>
<div className="mt-1.5 flex items-start gap-2 rounded-lg border border-dashed border-border bg-muted/30 px-3 py-2.5 text-body text-muted-foreground">
<Info className="mt-0.5 h-4 w-4 shrink-0" />
<div className="min-w-0">
<div>{t(($) => $.model_dropdown.managed_by_runtime_title)}</div>
<div className="mt-0.5 text-caption">
{t(($) => $.model_dropdown.managed_by_runtime_hint)}
</div>
</div>
</div>
</div>
);
}
return (
<div className="flex flex-col min-w-0">
<div className="flex h-6 items-center justify-between">
<Label className="text-caption text-muted-foreground">{t(($) => $.model_dropdown.label)}</Label>
{modelsQuery.isError && (
<span
className="text-caption text-muted-foreground"
title={discoveryError ?? undefined}
>
{t(($) => $.model_dropdown.discovery_failed)}
</span>
)}
</div>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger
disabled={disabled}
className="flex w-full min-w-0 items-center gap-3 rounded-lg border border-border bg-background px-3 py-2.5 mt-1.5 text-left text-body transition-colors hover:bg-muted disabled:pointer-events-none disabled:opacity-50"
>
<Cpu className="h-4 w-4 shrink-0 text-muted-foreground" />
<div className="min-w-0 flex-1">
{/* Wrapped in flex to mirror RuntimePicker's trigger DOM. The
two pickers sit side-by-side; inline-in-flex vs block-line-
box height calc would otherwise leave them ~1px misaligned. */}
<div className="flex items-center gap-2">
<span className="min-w-0 truncate font-medium">{triggerLabel}</span>
</div>
{value && (
<div className="truncate text-caption text-muted-foreground">
{modelLabel(models, value)}
</div>
)}
</div>
<ChevronDown
className={`h-4 w-4 shrink-0 text-muted-foreground transition-transform ${open ? "rotate-180" : ""}`}
/>
</PopoverTrigger>
<PopoverContent
align="start"
className="w-[var(--anchor-width)] p-0 overflow-hidden"
>
<div className="border-b border-border">
<ModelSearchHeader
value={search}
onChange={setSearch}
onRefresh={refresh}
refreshing={modelsQuery.isFetching}
refreshDisabled={!runtimeOnline || !runtimeId}
/>
</div>
<div className="max-h-72 overflow-y-auto p-1">
{modelsQuery.isLoading && (
<div className="flex items-center gap-2 px-3 py-6 text-body text-muted-foreground">
<Loader2 className="h-4 w-4 animate-spin" />
{t(($) => $.pickers.model_discovering)}
</div>
)}
{!modelsQuery.isLoading &&
Object.entries(filtered).map(([provider, list]) => (
<div key={provider} className="mb-1">
{provider && (
<div className="px-2 pt-1.5 pb-0.5 text-caption font-medium uppercase tracking-wide text-muted-foreground">
{provider}
</div>
)}
{list.map((m) => (
<button
type="button"
key={m.id}
onClick={() => select(m.id)}
className={`flex w-full items-center gap-2 rounded-md px-3 py-2 text-left text-body transition-colors ${
m.id === value ? "bg-accent" : "hover:bg-accent/50"
}`}
>
<div className="min-w-0 flex-1">
<div className="truncate font-medium">{m.label}</div>
{m.label !== m.id && (
<div className="truncate text-caption text-muted-foreground">
{m.id}
</div>
)}
</div>
{m.id === value && (
<Check className="h-4 w-4 shrink-0 text-primary" />
)}
</button>
))}
</div>
))}
{/* A failed discovery reports WHY here rather than in the label
row's caption, which has no room for a sentence. The runtime's
own words are the actionable part — hermes, for one, names the
exact command to run — so they are rendered verbatim and left
selectable. Paired with the manual-entry prompt, because a
reason with no way forward is just a nicer dead end. */}
{!modelsQuery.isLoading && modelsQuery.isError && (
<div className="px-3 py-4 text-body text-muted-foreground">
<div className="flex items-start gap-2">
<Info className="mt-0.5 h-4 w-4 shrink-0" />
<div className="min-w-0">
<div className="font-medium text-foreground">
{t(($) => $.pickers.model_discovery_failed_title)}
</div>
{discoveryError && (
<div className="mt-1 whitespace-pre-wrap break-words text-caption select-text">
{discoveryError}
</div>
)}
<div className="mt-1.5 text-caption">
{t(($) => $.pickers.model_discovery_failed_hint)}
</div>
</div>
</div>
</div>
)}
{!modelsQuery.isLoading && !modelsQuery.isError && (
<UnavailableModelsNote
models={unavailableModels}
title={t(($) => $.pickers.model_unavailable_heading)}
/>
)}
{!modelsQuery.isLoading &&
!modelsQuery.isError &&
Object.keys(filtered).length === 0 &&
!canCreate && (
<div className="px-3 py-6 text-center text-body text-muted-foreground">
{t(($) => $.pickers.model_empty_with_dot)}
</div>
)}
{canCreate && (
<button
type="button"
onClick={() => select(trimmedSearch)}
className="flex w-full items-center gap-2 rounded-md px-3 py-2 text-left text-body text-primary transition-colors hover:bg-accent/50"
>
<Plus className="h-4 w-4 shrink-0" />
<span className="min-w-0 truncate">
{t(($) => $.pickers.model_custom_use, { value: trimmedSearch })}
</span>
</button>
)}
{value && (
<button
type="button"
onClick={() => select("")}
className="mt-1 flex w-full items-center gap-2 border-t border-border px-3 py-2 text-left text-caption text-muted-foreground transition-colors hover:bg-accent/50"
>
{t(($) => $.model_dropdown.clear_full)}
</button>
)}
</div>
</PopoverContent>
</Popover>
</div>
);
}
function groupByProvider(models: RuntimeModel[]): Record<string, RuntimeModel[]> {
const out: Record<string, RuntimeModel[]> = {};
for (const m of models) {
const key = m.provider ?? "";
if (!out[key]) out[key] = [];
out[key].push(m);
}
return out;
}
function modelLabel(models: RuntimeModel[], id: string): string {
const found = models.find((m) => m.id === id);
if (!found) return "custom";
return found.provider ? found.provider : "model";
}