import React, { useEffect, useState } from "react"; import type { AgentPersona, AgentProfile, AgentPrototype, LlmProvider } from "../types"; import { api } from "../api/client"; import { useAvatarPrefs } from "../avatarPrefs"; import { AvatarColorPicker } from "./AvatarColorPicker"; import { ModelSelectField } from "./ModelSelectField"; import { ProviderModelSelect } from "./ProviderModelSelect"; import { inputSolidStyle, modalPanelStyle, modalOverlayStyle, personaColumnStyle, personaGridStyle, sectionBoxStyle, sectionTitleStyle, wideModalWidthStyle, } from "./modalStyles"; interface Props { mode: "create" | "edit"; agent?: AgentProfile | null; prototypes: AgentPrototype[]; agents?: AgentProfile[]; embedded?: boolean; onClose: () => void; onSaved: () => void; onDeleted?: () => void; } export function AgentProfileModal({ mode, agent, prototypes, agents = [], embedded = false, onClose, onSaved, onDeleted }: Props) { // Clone source can be any built-in prototype OR any installed profile. Show // prototypes first, then the rest of the installed profiles (deduped). const protoIds = new Set(prototypes.map((p) => p.id)); const cloneSources = [ ...prototypes.map((p) => ({ id: p.id, label: `${p.name} — ${p.tagline}`, group: "Templates" })), ...agents .filter((a) => !protoIds.has(a.id) && a.available !== false) .map((a) => ({ id: a.id, label: a.name ? `${a.name} (${a.id})` : a.id, group: "Existing profiles" })), ]; const [profileId, setProfileId] = useState(""); const [cloneFrom, setCloneFrom] = useState(cloneSources[0]?.id ?? prototypes[0]?.id ?? "coder"); const [displayName, setDisplayName] = useState(""); const [tagline, setTagline] = useState(""); const [soul, setSoul] = useState(""); const [memory, setMemory] = useState(""); const [profileModel, setProfileModel] = useState(""); const [baseUrl, setBaseUrl] = useState(""); const [providerId, setProviderId] = useState(""); const [providers, setProviders] = useState([]); const [providersLoading, setProvidersLoading] = useState(false); const [availableModels, setAvailableModels] = useState([]); const [modelsLoading, setModelsLoading] = useState(false); const [loading, setLoading] = useState(mode === "edit"); const [saving, setSaving] = useState(false); const [deleting, setDeleting] = useState(false); const [error, setError] = useState(null); const canDelete = mode === "edit" && !!agent; const avatars = useAvatarPrefs(); useEffect(() => { if (mode !== "edit" || !agent) return; let cancelled = false; setLoading(true); setError(null); api.agents.persona(agent.id) .then((p: AgentPersona) => { if (cancelled) return; setDisplayName(p.name ?? agent.name); setTagline(agent.tagline ?? ""); setSoul(p.soul ?? ""); setMemory(p.memory ?? ""); const model = p.model ?? agent.model ?? ""; const url = p.base_url ?? agent.base_url ?? ""; setProfileModel(model); setBaseUrl(url); }) .catch((e: Error) => { if (!cancelled) setError(e.message); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [mode, agent]); useEffect(() => { if (mode !== "edit" || !agent?.id || !baseUrl.trim()) { setAvailableModels([]); return; } let cancelled = false; setModelsLoading(true); api.llm.models({ agentId: agent.id, baseUrl }) .then((r) => { if (!cancelled) setAvailableModels(r.models ?? []); }) .catch(() => { if (!cancelled) setAvailableModels([]); }) .finally(() => { if (!cancelled) setModelsLoading(false); }); return () => { cancelled = true; }; }, [mode, agent?.id, baseUrl]); useEffect(() => { // Edit → that profile's providers; create → global (~/.hermes) catalog, since // prototypes ship with an empty `providers:` block. let cancelled = false; setProvidersLoading(true); const req = mode === "edit" ? (agent?.id ? api.llm.providers({ agentId: agent.id }) : null) : api.llm.providers(); if (!req) { setProviders([]); setProvidersLoading(false); return; } req .then((r) => { if (cancelled) return; setProviders(r.providers ?? []); // Create: default to the first provider's model so a backend is always chosen. if (mode === "create") { const p = (r.providers ?? [])[0]; if (p) { setProviderId(p.id); setProfileModel(p.default_model || p.models[0] || ""); setBaseUrl(p.base_url); } } else { setProviderId(r.active ?? ""); } }) .catch(() => { if (!cancelled) setProviders([]); }) .finally(() => { if (!cancelled) setProvidersLoading(false); }); return () => { cancelled = true; }; }, [mode, agent?.id]); useEffect(() => { if (mode !== "create" || !cloneFrom) return; let cancelled = false; api.agents.persona(cloneFrom) .then((p: AgentPersona) => { if (cancelled) return; setSoul(p.soul ?? ""); setMemory(p.memory ?? ""); }) .catch(() => {}); return () => { cancelled = true; }; }, [mode, cloneFrom]); async function handleSave() { setSaving(true); setError(null); try { if (mode === "create") { const id = profileId.trim().toLowerCase(); if (!id) throw new Error("Profile id is required"); const selected = providers.length > 0 && providerId ? providers.find((p) => p.id === providerId) : undefined; await api.agents.create({ id, clone_from: cloneFrom, name: displayName.trim() || undefined, tagline: tagline.trim() || undefined, soul, memory, ...(selected && profileModel.trim() ? { model_default: profileModel, base_url: selected.base_url, provider: selected.id } : {}), }); } else if (agent) { const usingProviders = providers.length > 0 && !!providerId; const selected = usingProviders ? providers.find((p) => p.id === providerId) : undefined; await api.agents.savePersona(agent.id, { soul, memory, ...(baseUrl.trim() || selected ? { model_default: profileModel } : {}), ...(selected ? { base_url: selected.base_url, provider: selected.id } : {}), }); } onSaved(); onClose(); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setSaving(false); } } async function handleDelete() { if (!agent) return; const label = agent.name || agent.id; if (!window.confirm(`Delete agent profile "${label}"? This cannot be undone.`)) return; setDeleting(true); setError(null); try { await api.agents.delete(agent.id); onDeleted?.(); onClose(); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setDeleting(false); } } const title = mode === "create" ? "New agent profile" : `Edit ${agent?.name ?? "agent"}`; const body = ( <> {!embedded && (

{title}

)} {loading ? (
Loading…
) : ( <> {mode === "create" && (
setProfileId(e.target.value)} placeholder="my-coder" style={inputStyle} autoFocus /> Lowercase letters, numbers, hyphens. Used by Hermes as the profile name. Copies the source profile's config, SOUL & MEMORY as a starting point. setDisplayName(e.target.value)} style={inputStyle} /> setTagline(e.target.value)} style={inputStyle} /> {providers.length > 0 && ( { setProviderId(pid); setProfileModel(model); setBaseUrl(url); }} /> )}
)} {mode === "edit" && agent && (
Profile {agent.id} {agent.is_prototype && " · prototype"} {agent.clone_from && !agent.is_prototype && ( <> · clone of {agent.clone_from} )}
)} {mode === "edit" && providers.length > 0 ? ( { setProviderId(pid); setProfileModel(model); setBaseUrl(url); }} /> ) : mode === "edit" && baseUrl.trim() && ( )} {mode === "edit" && agent && (
Avatar & color
avatars.set(agent.id, patch)} />
)}
Persona