Files
openchamber/packages/ui/src/components/multirun/AgentSelector.tsx
T

114 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-01-04 15:53:44 +01:00
import React from 'react';
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { cn } from '@/lib/utils';
2026-01-04 15:53:44 +01:00
import { useConfigStore } from '@/stores/useConfigStore';
export interface AgentSelectorProps {
/** Currently selected agent name (empty string for no agent) */
value: string;
/** Called when agent selection changes */
onChange: (agentName: string) => void;
/** Optional className for the trigger */
className?: string;
/** Whether the selector is disabled */
disabled?: boolean;
/** ID for accessibility */
id?: string;
}
/**
* Agent selector dropdown for selecting an agent for multi-run sessions.
* Uses getVisibleAgents from useConfigStore to show available agents.
*/
export const AgentSelector: React.FC<AgentSelectorProps> = ({
value,
onChange,
className,
disabled,
id,
}) => {
const getVisibleAgents = useConfigStore((state) => state.getVisibleAgents);
const loadAgents = useConfigStore((state) => state.loadAgents);
2026-01-06 21:31:04 +02:00
const defaultAgentName = useConfigStore((state) => state.currentAgentName);
2026-01-04 15:53:44 +01:00
const agents = getVisibleAgents();
2026-01-06 21:31:04 +02:00
const selectableAgents = React.useMemo(
() => agents.filter((agent) => agent.mode !== 'subagent'),
[agents]
);
2026-01-04 15:53:44 +01:00
// Load agents on mount
React.useEffect(() => {
loadAgents();
}, [loadAgents]);
2026-01-06 21:31:04 +02:00
// Ensure we always have a valid selection (defaults to current default agent, then first selectable agent).
React.useEffect(() => {
if (disabled) {
return;
}
const trimmedValue = value.trim();
if (trimmedValue.length > 0 && selectableAgents.some((agent) => agent.name === trimmedValue)) {
return;
}
const candidateDefault =
typeof defaultAgentName === 'string' && defaultAgentName.trim().length > 0
? defaultAgentName.trim()
: null;
2026-01-04 15:53:44 +01:00
2026-01-06 21:31:04 +02:00
if (candidateDefault && selectableAgents.some((agent) => agent.name === candidateDefault)) {
onChange(candidateDefault);
return;
}
const firstAgent = selectableAgents[0]?.name;
if (firstAgent) {
onChange(firstAgent);
}
}, [defaultAgentName, disabled, onChange, selectableAgents, value]);
const selectValue = value.trim().length > 0 ? value : undefined;
2026-01-04 15:53:44 +01:00
return (
<Select
value={selectValue}
2026-01-06 21:31:04 +02:00
onValueChange={onChange}
2026-01-04 15:53:44 +01:00
disabled={disabled}
>
<SelectTrigger
id={id}
size="lg"
className={cn(
'max-w-full typography-meta text-foreground !border-border/80 !bg-[var(--surface-subtle)] hover:!bg-[var(--interactive-hover)]/70 data-[state=open]:!bg-[var(--interactive-active)]/70',
className,
)}
2026-01-04 15:53:44 +01:00
>
2026-01-06 21:31:04 +02:00
<SelectValue placeholder="Select an agent" />
2026-01-04 15:53:44 +01:00
</SelectTrigger>
<SelectContent fitContent>
2026-01-06 21:31:04 +02:00
{selectableAgents.length > 0 && (
2026-01-04 15:53:44 +01:00
<SelectGroup>
2026-01-06 21:31:04 +02:00
{selectableAgents.map((agent) => (
2026-01-04 15:53:44 +01:00
<SelectItem
key={agent.name}
value={agent.name}
className="w-auto whitespace-nowrap"
>
{agent.name}
</SelectItem>
))}
</SelectGroup>
)}
</SelectContent>
</Select>
);
};