Файли
openidea/src/features/agents/ConfigPickers.tsx
T
Mr0Bread 9984e40b83 fix(agents): refresh available models before starting tasks (#3366)
New Task showed no models until an agent's first task. It now shows the cached catalog at once and discovers the agent's models without starting a task (no prompt, no project MCP servers): one shared lookup per provider and project, fresh for 15 minutes, with a Refresh models action and a 30-second cooldown. Catalogs persist per effective launch configuration (a digest, never the arguments or credentials); a selected model the latest catalog dropped blocks Start until another is chosen. Discovery requires a trusted project.

Co-authored-by: Mr0Bread <[email protected]>
2026-11-14 23:22:14 +08:00

480 рядки
17 KiB
TypeScript

/**
* Model, mode, approvals and thinking-effort controls for a composer's bottom row.
* With both a model and an effort option, one button shows both and opens the model
* list and an effort slider; with a mode and an on/off permissions option (Allow All),
* the mode menu carries the switch. Names and descriptions are the agent's own.
*/
import { useEffect, useRef, useState, type CSSProperties } from "react";
import clsx from "clsx";
import type { AgentConfigOption } from "@/bindings";
import { useTx } from "@/platform/i18n";
import { Icon, IconButton, Popup, Select, T, Toggle } from "@/ui";
import { SelectListbox, type SelectOption } from "@/ui/Select";
import { mainOptions, modelConfigOptions, modeIcon } from "./composerModel";
import styles from "./Composer.module.css";
export function configPickerOptions(option: AgentConfigOption): SelectOption[] {
if (option.category !== "model") {
return option.values.map((v) => ({ value: v.value, label: v.name, detail: v.description ?? undefined }));
}
const groups = new Map<string, SelectOption[]>();
for (const v of option.values) {
const slash = v.value.indexOf("/");
const provider = slash >= 0 ? v.value.slice(0, slash) : "";
const path = slash >= 0 ? v.value.slice(slash + 1) : "";
const label = path ? path.slice(path.lastIndexOf("/") + 1) : v.name || v.value;
const detail = path ? [path, v.description].filter(Boolean).join(" · ") : (v.description ?? undefined);
const bucket = groups.get(provider) ?? [];
bucket.push({ value: v.value, label, detail, keywords: provider });
groups.set(provider, bucket);
}
const plain = groups.get("") ?? [];
groups.delete("");
return [
...plain,
...[...groups].flatMap(([provider, items]) => [{ value: provider, label: provider, section: true }, ...items]),
];
}
/** The on and off values of a two-state permissions option, or null when it has other values. */
function switchValues(option: AgentConfigOption): { on: string; off: string } | null {
const isFast = option.id === "fast" || option.category === "model_config";
if (option.category !== "permissions" && !isFast) return null;
const on = option.values.find((v) => /^(on|true)$/i.test(v.value))?.value;
const off = option.values.find((v) => /^(off|false)$/i.test(v.value))?.value;
return on && off ? { on, off } : null;
}
const selected = (option: AgentConfigOption, value: string | null) =>
option.values.find((v) => v.value === value) ?? null;
interface ModelRefresh {
refreshing: boolean;
retryAtMs: number | null;
error: string | null;
onRefresh: () => void;
}
export function ConfigPickers({
config,
values,
onChange,
modelRefresh,
}: {
config: readonly AgentConfigOption[];
/** Chosen values that override `current`, by option id. */
values?: Record<string, string>;
onChange: (option: string, value: string) => void;
/** Only New Task supplies catalog discovery; session pickers retain their own settings. */
modelRefresh?: ModelRefresh;
}) {
const tx = useTx();
const [open, setOpen] = useState(false);
const anchor = useRef<HTMLButtonElement>(null);
const options = mainOptions(config);
if (modelRefresh && !options.some((o) => o.category === "model"))
options.unshift({
id: "model",
name: "Model",
category: "model",
description: null,
current: null,
values: [],
via: "option",
});
const valueOf = (o: AgentConfigOption) => values?.[o.id] ?? o.current ?? null;
const model = options.find((o) => o.category === "model");
const effort = config.find((o) => o.category === "thought_level" && o.values.length > 0);
const modelConfigs = modelConfigOptions(config);
const combinedModel = model && (modelRefresh || effort || modelConfigs.length > 0);
const tuckedModelConfig = new Set(modelConfigs.map((o) => o.id));
const mode = options.find((o) => o.category === "mode");
const allowAll = mode && options.find((o) => switchValues(o));
const inline = options.filter(
(o) => o !== effort && o !== allowAll && !(combinedModel && tuckedModelConfig.has(o.id)),
);
// The compact menu holds everything but the model button (effort + model parameters).
const secondary = options.filter(
(o) => o.category !== "model" && o !== effort && !(combinedModel && tuckedModelConfig.has(o.id)),
);
const approval = secondary.find((o) => o.category === "approval" || o.category === "mode");
const summary = secondary.map((o) => `${tx(o.name)}: ${selected(o, valueOf(o))?.name ?? tx("Default")}`).join(" · ");
return (
<>
{inline.map((o) => (
<span key={o.id} className={clsx(styles.configPicker, o.category !== "model" && styles.inlineSetting)}>
{o === model && combinedModel ? (
<ModelEffortPicker
model={o}
modelValue={valueOf(o)}
effort={effort}
effortValue={effort ? valueOf(effort) : null}
modelConfigs={modelConfigs}
valueOf={valueOf}
onChange={onChange}
modelRefresh={modelRefresh}
/>
) : o === mode && allowAll ? (
<ModePicker
mode={o}
modeValue={valueOf(o)}
allowAll={allowAll}
allowAllValue={valueOf(allowAll)}
onChange={onChange}
/>
) : (
<ConfigPicker option={o} value={valueOf(o)} onChange={onChange} />
)}
</span>
))}
{secondary.length > 0 && (
<div className={styles.settingsMenu}>
<IconButton
ref={anchor}
icon={approval ? modeIcon(valueOf(approval) ?? "") : "settings"}
title={`${tx("Agent settings")}: ${summary}`}
aria-label={tx("Agent settings")}
aria-haspopup="dialog"
aria-expanded={open}
size="small"
onClick={() => setOpen((v) => !v)}
/>
<Popup
open={open}
anchor={anchor}
onClose={() => setOpen(false)}
aria-label={tx("Agent settings")}
className={styles.settingsPopup}
>
<div className={styles.settingsTitle}>
<T>Agent settings</T>
</div>
{secondary.map((o) => (
<div key={o.id} className={styles.settingsRow}>
<span className={styles.settingsLabel}>{tx(o.name)}</span>
<ConfigPicker option={o} value={valueOf(o)} onChange={onChange} framed />
</div>
))}
</Popup>
</div>
)}
</>
);
}
function ConfigPicker({
option,
value,
onChange,
framed = false,
}: {
option: AgentConfigOption;
value: string | null;
onChange: (option: string, value: string) => void;
framed?: boolean;
}) {
const tx = useTx();
const toggle = switchValues(option);
if (toggle) {
return (
<Toggle
checked={value === toggle.on}
aria-label={tx(option.name)}
label={framed ? undefined : tx(option.name)}
onChange={(checked) => onChange(option.id, checked ? toggle.on : toggle.off)}
/>
);
}
return (
<Select
variant={framed ? "framed" : "ghost"}
aria-label={option.name}
value={value}
placeholder={tx(option.name)}
options={configPickerOptions(option).map((opt) => ({
...opt,
icon:
option.category === "approval"
? opt.value === "bypass"
? "unlock"
: "lock"
: option.category === "mode"
? modeIcon(opt.value)
: undefined,
}))}
searchable={option.category === "model"}
onChange={(v) => onChange(option.id, v)}
renderValue={
option.category === "thought_level" && !framed
? (current) => (
<span className={styles.pickerValue}>
<span className={styles.pickerName}>{option.name}</span>
{current?.label ?? tx("Default")}
</span>
)
: undefined
}
/>
);
}
/** One button for the model and its effort; the popup has the model list and an effort slider. */
function ModelEffortPicker({
model,
modelValue,
effort,
effortValue,
modelConfigs,
valueOf,
onChange,
modelRefresh,
}: {
model: AgentConfigOption;
modelValue: string | null;
effort?: AgentConfigOption;
effortValue: string | null;
modelConfigs: AgentConfigOption[];
valueOf: (o: AgentConfigOption) => string | null;
onChange: (option: string, value: string) => void;
modelRefresh?: ModelRefresh;
}) {
const tx = useTx();
const [open, setOpen] = useState(false);
const anchor = useRef<HTMLButtonElement>(null);
const unavailable = modelRefresh && modelValue !== null && !selected(model, modelValue);
const modelName = unavailable
? tx`${modelValue} (unavailable)`
: (selected(model, modelValue)?.name ?? tx("Default"));
const effortName = effort ? (selected(effort, effortValue)?.name ?? tx("Default")) : null;
const modelConfigSummary = modelConfigs
.map((o) => {
const current = selected(o, valueOf(o));
return current ? `${tx(o.name)}: ${current.name}` : null;
})
.filter(Boolean)
.join(" · ");
const summary = [effortName ? `${tx(effort!.name)}: ${effortName}` : null, modelConfigSummary]
.filter(Boolean)
.join(" · ");
return (
<>
<button
ref={anchor}
type="button"
className={styles.chip}
aria-label={tx(model.name)}
aria-haspopup="dialog"
aria-expanded={open}
title={`${tx(model.name)}: ${modelName}${summary ? ` · ${summary}` : ""}`}
onClick={() => setOpen((v) => !v)}
>
<span className={styles.chipValue}>{modelName}</span>
{/* Every level's name in one cell, so the button keeps the widest one's width. */}
{effort && (
<span className={styles.chipDetail}>
{[...new Set([effortName, ...effort.values.map((v) => v.name)])].map((name) => (
<span
key={name}
className={name === effortName ? undefined : styles.chipSizer}
aria-hidden={name !== effortName}
>
{name}
</span>
))}
</span>
)}
<Icon name="chevron-down" size={14} className={styles.chipChevron} />
</button>
<Popup
open={open}
anchor={anchor}
onClose={() => setOpen(false)}
aria-label={tx("Model & effort settings")}
className={styles.modelEffortPopup}
>
{unavailable && (
<p className={styles.catalogError} role="alert">
<T>Choose another model before starting.</T>
</p>
)}
{effort && (
<div className={styles.effortBlock}>
<div className={styles.effortHeader}>
<span className={styles.settingsLabel}>{tx(effort.name)}</span>
<span className={styles.effortValue}>{effortName}</span>
</div>
<EffortSlider option={effort} value={effortValue} onChange={(v) => onChange(effort.id, v)} />
</div>
)}
{modelConfigs.map((o) => (
<div key={o.id} className={styles.modeFooter}>
<span title={o.description ?? undefined}>{tx(o.name)}</span>
<ConfigPicker option={o} value={valueOf(o)} onChange={onChange} framed />
</div>
))}
{model.values.length > 0 || !modelRefresh ? (
<SelectListbox
aria-label={tx(model.name)}
value={modelValue}
options={configPickerOptions(model)}
searchable
listClassName={styles.modelList}
onChoose={(v) => onChange(model.id, v)}
/>
) : (
<p className={styles.catalogNote} role="status">
{modelRefresh?.refreshing ? <T>Loading models…</T> : <T>No models reported by this agent.</T>}
</p>
)}
{modelRefresh && <ModelRefreshFooter key={modelRefresh.retryAtMs} {...modelRefresh} />}
</Popup>
</>
);
}
function ModelRefreshFooter({ refreshing, retryAtMs, error, onRefresh }: ModelRefresh) {
const tx = useTx();
const [now, setNow] = useState(Date.now);
const remaining = retryAtMs == null ? 0 : Math.max(0, Math.ceil((retryAtMs - now) / 1000));
const coolingDown = remaining > 0;
useEffect(() => {
if (!coolingDown) return;
const timer = window.setInterval(() => setNow(Date.now()), 1000);
return () => window.clearInterval(timer);
}, [retryAtMs, coolingDown]);
return (
<div className={styles.catalogFooter}>
{error && (
<p className={styles.catalogError} role="alert">
{tx`Could not refresh models: ${error}`}
</p>
)}
<button type="button" className={styles.refreshModels} disabled={refreshing || coolingDown} onClick={onRefresh}>
{refreshing ? tx("Refreshing…") : coolingDown ? tx`Refresh in ${remaining}s` : tx("Refresh models")}
</button>
</div>
);
}
/** A stepped slider over the agent's effort levels, in the order the agent lists them. */
function EffortSlider({
option,
value,
onChange,
}: {
option: AgentConfigOption;
value: string | null;
onChange: (value: string) => void;
}) {
const tx = useTx();
const steps = option.values;
const index = Math.max(
0,
steps.findIndex((v) => v.value === value),
);
const last = steps.length - 1;
const fill = last > 0 ? (index / last) * 100 : 100;
return (
<div className={styles.effortSlider}>
<input
type="range"
min={0}
max={last}
step={1}
value={index}
aria-label={tx(option.name)}
aria-valuetext={steps[index]?.name}
title={steps[index]?.description ?? undefined}
style={{ "--fill": `${fill}%` } as CSSProperties}
onChange={(e) => {
const next = steps[Number(e.target.value)];
if (next && next.value !== value) onChange(next.value);
}}
/>
<div className={styles.effortTicks} aria-hidden>
{steps.map((v) => (
<span key={v.value} className={clsx(v.value === steps[index]?.value && styles.effortTickActive)} />
))}
</div>
</div>
);
}
/** The mode menu, with the agent's on/off permissions option (Allow All) as a switch under it. */
function ModePicker({
mode,
modeValue,
allowAll,
allowAllValue,
onChange,
}: {
mode: AgentConfigOption;
modeValue: string | null;
allowAll: AgentConfigOption;
allowAllValue: string | null;
onChange: (option: string, value: string) => void;
}) {
const tx = useTx();
const [open, setOpen] = useState(false);
const anchor = useRef<HTMLButtonElement>(null);
const toggle = switchValues(allowAll)!;
const current = selected(mode, modeValue);
const on = allowAllValue === toggle.on;
const summary = `${tx(mode.name)}: ${current?.name ?? tx("Default")} · ${tx(allowAll.name)}: ${on ? tx("On") : tx("Off")}`;
return (
<>
<button
ref={anchor}
type="button"
className={styles.chip}
aria-label={tx(mode.name)}
aria-haspopup="dialog"
aria-expanded={open}
title={summary}
onClick={() => setOpen((v) => !v)}
>
<Icon name={on ? "unlock" : modeIcon(current?.value ?? "")} size={14} />
<span className={styles.chipValue}>{current?.name ?? tx("Default")}</span>
{on && <span className={styles.chipDetail}>{tx(allowAll.name)}</span>}
<Icon name="chevron-down" size={14} className={styles.chipChevron} />
</button>
<Popup
open={open}
anchor={anchor}
onClose={() => setOpen(false)}
aria-label={tx(mode.name)}
className={styles.modePopup}
>
<div role="listbox" aria-label={tx(mode.name)} className={styles.modeList}>
{mode.values.map((v) => (
<button
key={v.value}
type="button"
role="option"
aria-selected={v.value === current?.value}
title={v.description ?? undefined}
className={styles.modeOption}
onClick={() => {
onChange(mode.id, v.value);
setOpen(false);
}}
>
<span className={styles.modeCheck}>{v.value === current?.value && <Icon name="check" size={12} />}</span>
<Icon name={modeIcon(v.value)} size={14} />
<span className={styles.chipValue}>{v.name}</span>
</button>
))}
</div>
<div className={styles.modeFooter}>
<span title={allowAll.description ?? undefined}>{tx(allowAll.name)}</span>
<Toggle
checked={on}
aria-label={tx(allowAll.name)}
onChange={(checked) => onChange(allowAll.id, checked ? toggle.on : toggle.off)}
/>
</div>
</Popup>
</>
);
}