292 lines
9.2 KiB
TypeScript
292 lines
9.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { Input } from "@kandev/ui/input";
|
|
import { Label } from "@kandev/ui/label";
|
|
import { Badge } from "@kandev/ui/badge";
|
|
import { useAppStore } from "@/components/state-provider";
|
|
import { AgentSelector } from "@/components/task-create-dialog-selectors";
|
|
import type { AgentProfileOption } from "@/lib/state/slices/settings/types";
|
|
import { Combobox, type ComboboxOption } from "@/components/combobox";
|
|
import { getExecutorIcon } from "@/lib/executor-icons";
|
|
import { ToggleGroup, ToggleGroupItem } from "@kandev/ui/toggle-group";
|
|
import type { Tier } from "@/lib/state/slices/office/types";
|
|
import { seedTier } from "./seed-tier-mapping";
|
|
import {
|
|
CreateProfileButton,
|
|
CreateProfilePanel,
|
|
useSelectableProfileOptions,
|
|
} from "./agent-profile-setup-controls";
|
|
|
|
type ProfileSelectOption = ReturnType<typeof useSelectableProfileOptions>["profileOptions"][number];
|
|
|
|
type StepAgentProps = {
|
|
agentName: string;
|
|
agentProfileId: string;
|
|
executorPreference: string;
|
|
defaultTier?: Tier;
|
|
agentProfiles: AgentProfileOption[];
|
|
onChange: (patch: {
|
|
agentName?: string;
|
|
agentProfileId?: string;
|
|
executorPreference?: string;
|
|
defaultTier?: Tier;
|
|
}) => void;
|
|
onAgentProfilesChange?: (profiles: AgentProfileOption[]) => void;
|
|
};
|
|
|
|
// Fallback used only when meta has not been hydrated yet (graceful degradation).
|
|
const FALLBACK_EXECUTOR_OPTIONS = [
|
|
{ id: "local_pc", label: "Local (standalone)", description: "Run on host machine" },
|
|
{ id: "local_docker", label: "Local Docker", description: "Run in a local Docker container" },
|
|
{
|
|
id: "sprites",
|
|
label: "Sprites (remote sandbox)",
|
|
description: "Run in a Sprites cloud environment",
|
|
},
|
|
];
|
|
|
|
export function StepAgent({
|
|
agentName,
|
|
agentProfileId,
|
|
executorPreference,
|
|
defaultTier,
|
|
agentProfiles,
|
|
onChange,
|
|
onAgentProfilesChange,
|
|
}: StepAgentProps) {
|
|
const meta = useAppStore((s) => s.office.meta);
|
|
const executorOptions = meta?.executorTypes ?? FALLBACK_EXECUTOR_OPTIONS;
|
|
const settingsAgents = useAppStore((s) => s.settingsAgents.items);
|
|
const setAgentProfiles = useAppStore((s) => s.setAgentProfiles);
|
|
|
|
const { sortedProfiles, profileOptions } = useSelectableProfileOptions(agentProfiles);
|
|
|
|
const selectedProfile = sortedProfiles.find((p) => p.id === agentProfileId);
|
|
const [showCreate, setShowCreate] = useState(profileOptions.length === 0);
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div>
|
|
<h2 className="text-xl font-semibold">Create your coordinator agent</h2>
|
|
<p className="text-sm text-muted-foreground mt-1">
|
|
The coordinator manages other agents, delegates tasks, and monitors progress.
|
|
</p>
|
|
</div>
|
|
<div className="space-y-4">
|
|
<div>
|
|
<Label htmlFor="agent-name">Agent name</Label>
|
|
<Input
|
|
id="agent-name"
|
|
value={agentName}
|
|
onChange={(e) => onChange({ agentName: e.target.value })}
|
|
placeholder="CEO"
|
|
className="mt-1"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
<div>
|
|
<ProfileSelectorSection
|
|
showCreate={showCreate}
|
|
profileOptions={profileOptions}
|
|
agentProfileId={agentProfileId}
|
|
selectedProfile={selectedProfile}
|
|
onChange={onChange}
|
|
onCreateClick={() => setShowCreate(true)}
|
|
/>
|
|
{showCreate && (
|
|
<CreateProfilePanel
|
|
settingsAgents={settingsAgents}
|
|
wizardProfiles={agentProfiles}
|
|
canCancel={profileOptions.length > 0}
|
|
setAgentProfiles={setAgentProfiles}
|
|
onAgentProfilesChange={onAgentProfilesChange}
|
|
onProfileSaved={(profileId) => onChange({ agentProfileId: profileId })}
|
|
onClose={() => setShowCreate(false)}
|
|
/>
|
|
)}
|
|
</div>
|
|
<ExecutorSelector
|
|
value={executorPreference}
|
|
options={executorOptions}
|
|
onChange={(v) => onChange({ executorPreference: v })}
|
|
/>
|
|
<TierIndicator
|
|
selectedProfile={selectedProfile}
|
|
defaultTier={defaultTier}
|
|
onChange={(t) => onChange({ defaultTier: t })}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function ProfileSelectorSection({
|
|
showCreate,
|
|
profileOptions,
|
|
agentProfileId,
|
|
selectedProfile,
|
|
onChange,
|
|
onCreateClick,
|
|
}: {
|
|
showCreate: boolean;
|
|
profileOptions: ProfileSelectOption[];
|
|
agentProfileId: string;
|
|
selectedProfile: AgentProfileOption | undefined;
|
|
onChange: StepAgentProps["onChange"];
|
|
onCreateClick: () => void;
|
|
}) {
|
|
return (
|
|
<>
|
|
<Label>CLI agent profile</Label>
|
|
{!showCreate && (
|
|
<AgentSelector
|
|
options={profileOptions}
|
|
value={agentProfileId}
|
|
onValueChange={(v) => onChange({ agentProfileId: v })}
|
|
disabled={profileOptions.length === 0}
|
|
placeholder="Select an agent profile..."
|
|
triggerClassName="mt-1 border border-input rounded-md px-3 h-9"
|
|
/>
|
|
)}
|
|
{!showCreate && (
|
|
<ProfilePickerHint
|
|
hasProfiles={profileOptions.length > 0}
|
|
selected={selectedProfile}
|
|
onCreateClick={onCreateClick}
|
|
/>
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
function TierIndicator({
|
|
selectedProfile,
|
|
defaultTier,
|
|
onChange,
|
|
}: {
|
|
selectedProfile: AgentProfileOption | undefined;
|
|
defaultTier?: Tier;
|
|
onChange: (t: Tier) => void;
|
|
}) {
|
|
// The label string in AgentProfileOption is "<agent display> • <profile name>"
|
|
// — fall back to the raw label when we cannot extract a model id, since the
|
|
// seed mapping only matters for the "we'll treat X as the Y tier" hint.
|
|
const modelHint = selectedProfile?.label;
|
|
const seeded = seedTier(selectedProfile?.agent_id, modelHint);
|
|
const value: Tier = defaultTier ?? seeded;
|
|
return (
|
|
<div>
|
|
<Label>Workspace default tier</Label>
|
|
<p className="text-xs text-muted-foreground mb-2">
|
|
We'll treat <span className="font-mono">{modelHint || "your model"}</span> as the{" "}
|
|
{value} tier for {selectedProfile?.agent_name || "this provider"}. Change it later in
|
|
Workspace → Provider routing.
|
|
</p>
|
|
<ToggleGroup
|
|
type="single"
|
|
value={value}
|
|
onValueChange={(v) => v && onChange(v as Tier)}
|
|
className="justify-start"
|
|
>
|
|
<ToggleGroupItem value="frontier" className="cursor-pointer capitalize">
|
|
Frontier
|
|
</ToggleGroupItem>
|
|
<ToggleGroupItem value="balanced" className="cursor-pointer capitalize">
|
|
Balanced
|
|
</ToggleGroupItem>
|
|
<ToggleGroupItem value="economy" className="cursor-pointer capitalize">
|
|
Economy
|
|
</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function ProfilePickerHint({
|
|
hasProfiles,
|
|
selected,
|
|
onCreateClick,
|
|
}: {
|
|
hasProfiles: boolean;
|
|
selected: AgentProfileOption | undefined;
|
|
onCreateClick: () => void;
|
|
}) {
|
|
if (!hasProfiles) {
|
|
return (
|
|
<div className="mt-2 text-xs text-muted-foreground space-y-1">
|
|
<p>No CLI agent profiles available yet.</p>
|
|
<CreateProfileButton hasProfiles={false} onCreateClick={onCreateClick} />
|
|
</div>
|
|
);
|
|
}
|
|
return (
|
|
<div className="mt-2 space-y-2 text-xs text-muted-foreground">
|
|
{selected ? (
|
|
<div className="flex items-center gap-2 flex-wrap">
|
|
<Badge variant="secondary">{selected.agent_name}</Badge>
|
|
{selected.cli_passthrough ? <Badge variant="outline">CLI passthrough</Badge> : null}
|
|
</div>
|
|
) : (
|
|
<p>Picks the CLI client, model, mode, and flags this agent will use.</p>
|
|
)}
|
|
<CreateProfileButton hasProfiles={hasProfiles} onCreateClick={onCreateClick} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// Maps onboarding executor-preference IDs to the icon catalog keys in
|
|
// `lib/executor-icons.ts` (which uses runtime executor type names).
|
|
const EXECUTOR_ICON_TYPE: Record<string, string> = {
|
|
local_pc: "local",
|
|
local_docker: "local_docker",
|
|
remote_docker: "remote_docker",
|
|
sprites: "sprites",
|
|
};
|
|
|
|
function ExecutorSelector({
|
|
value,
|
|
options,
|
|
onChange,
|
|
}: {
|
|
value: string;
|
|
options: { id: string; label: string; description: string }[];
|
|
onChange: (v: string) => void;
|
|
}) {
|
|
const current = value || "local_pc";
|
|
const selected = options.find((o) => o.id === current);
|
|
const comboOptions: ComboboxOption[] = options.map((opt) => {
|
|
const Icon = getExecutorIcon(EXECUTOR_ICON_TYPE[opt.id] ?? "local");
|
|
const disabled = opt.id !== "local_pc";
|
|
return {
|
|
value: opt.id,
|
|
label: opt.label,
|
|
description: opt.description,
|
|
disabled,
|
|
disabledReason: disabled ? "Coming soon — only Local is supported right now." : undefined,
|
|
renderLabel: () => (
|
|
<span className="flex min-w-0 flex-1 items-center gap-2">
|
|
<Icon className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
|
|
<span className="truncate">{opt.label}</span>
|
|
</span>
|
|
),
|
|
};
|
|
});
|
|
return (
|
|
<div>
|
|
<Label>Executor preference</Label>
|
|
<Combobox
|
|
options={comboOptions}
|
|
value={current}
|
|
onValueChange={onChange}
|
|
placeholder="Select executor..."
|
|
showSearch={false}
|
|
triggerClassName="mt-1 border border-input rounded-md px-3 h-9"
|
|
/>
|
|
{selected ? (
|
|
<p className="text-xs text-muted-foreground mt-1">{selected.description}</p>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|