import React, { useEffect, useMemo, useState } from "react";
import type { AgentCapabilities, AgentProfile, ToolsetMeta } from "../types";
import type { PendingAssignment, ToolPresetId } from "../types";
import { api } from "../api/client";
import { AgentFigure } from "./AgentFigure";
import {
modalPanelStyle,
modalOverlayStyle,
personaColumnStyle,
personaGridStyle,
presetTabStyle,
sectionBoxStyle,
sectionTitleStyle,
skillChipStyle,
toolChipStyle,
wideModalWidthStyle,
} from "./modalStyles";
import { ModelSelectField } from "./ModelSelectField";
const PRESET_TABS: ToolPresetId[] = ["chat", "lean", "full"];
interface Props {
deskId: string;
agent: AgentProfile;
toolsets: ToolsetMeta[];
onAssign: (deskId: string, assignment: PendingAssignment) => void;
onClose: () => void;
}
function PersonaColumn({ label, text }: { label: string; text: string }) {
const empty = !text.trim();
return (
{label}
{empty ? (empty) : text}
);
}
export function AgentAssignModal({ deskId, agent, toolsets, onAssign, onClose }: Props) {
const [caps, setCaps] = useState(null);
const [soul, setSoul] = useState("");
const [memory, setMemory] = useState("");
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [tab, setTab] = useState("lean");
const backendUrl = (agent.base_url || "").trim();
const [availableModels, setAvailableModels] = useState([]);
const [modelOverride, setModelOverride] = useState(agent.model || "");
useEffect(() => {
let cancelled = false;
setLoading(true);
setError(null);
Promise.all([
api.agents.capabilities(agent.id),
api.agents.persona(agent.id),
...(backendUrl ? [api.llm.models({ baseUrl: backendUrl, agentId: agent.id })] : []),
])
.then((results) => {
if (cancelled) return;
const capData = results[0] as AgentCapabilities;
const persona = results[1] as { soul?: string; memory?: string };
setCaps(capData);
setSoul(persona.soul ?? "");
setMemory(persona.memory ?? "");
const def = capData.default_preset;
if (def === "chat" || def === "lean" || def === "full") setTab(def);
if (backendUrl && results[2]) {
const listed = results[2] as { models: string[]; current: string };
setAvailableModels(listed.models ?? []);
setModelOverride((prev) => prev || agent.model || listed.current || "");
}
})
.catch((e: Error) => { if (!cancelled) setError(e.message); })
.finally(() => { if (!cancelled) setLoading(false); });
return () => { cancelled = true; };
}, [agent.id, agent.model, backendUrl]);
const labels = useMemo(
() => Object.fromEntries(toolsets.map((t) => [t.name, t.label])),
[toolsets],
);
const enabledSet = new Set(caps?.presets[tab] ?? []);
const skillBundles = useMemo(() => {
if (!caps) return [];
return [...caps.skill_bundles].sort((a, b) => {
if (a.count > 0 && b.count === 0) return -1;
if (a.count === 0 && b.count > 0) return 1;
return a.bundle.localeCompare(b.bundle);
});
}, [caps]);
function handleAssign() {
if (!caps) return;
const pickedModel = modelOverride.trim();
onAssign(deskId, {
agentId: agent.id,
agentName: agent.name,
agentColor: agent.color,
toolPreset: tab,
toolsEnabled: caps.presets[tab] ?? [],
...(pickedModel ? { modelOverride: pickedModel } : {}),
});
onClose();
}
return (
{ if (e.target === e.currentTarget) onClose(); }}
>
{agent.name}
{agent.tagline}
{loading ? (
Loading profile…
) : error ? (
{error}
) : caps && (
<>
{/* Persona — SOUL | MEMORY side by side */}
{backendUrl && (
)}
{/* Tools + skills — full width below */}
Tools
{caps.source === "profile" ? "Profile presets" : "Global presets"}
{PRESET_TABS.map((id) => (
))}
{toolsets.map((t) => (
{labels[t.name] ?? t.label}
))}
{skillBundles.length > 0 && (
Skill categories
({caps.skill_count} installed)
{skillBundles.map((b) => (
0)} title={
b.count > 0 ? b.skills.slice(0, 8).join(", ") : "No skills in this category"
}>
{b.bundle}
0 ? 0.85 : 0.55 }}>
({b.count})
))}
)}
>
)}
);
}
const ghostBtn: React.CSSProperties = {
background: "transparent", border: "none", color: "var(--text-dim)", cursor: "pointer", fontSize: 14,
};
const primaryBtn: React.CSSProperties = {
padding: "8px 14px", borderRadius: 6, border: "none", cursor: "pointer",
background: "var(--accent)", color: "#fff", fontSize: 12, fontWeight: 600,
};