"use client";
import { useState, useEffect, useMemo } from "react";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@kandev/ui/dialog";
import { Button } from "@kandev/ui/button";
import { Input } from "@kandev/ui/input";
import { Label } from "@kandev/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@kandev/ui/select";
import { ModelCombobox } from "@/components/settings/model-combobox";
import {
type UtilityAgent,
createUtilityAgent,
updateUtilityAgent,
getTemplateVariables,
listInferenceAgents,
type TemplateVariable,
type InferenceAgent,
type InferenceModel,
} from "@/lib/api/domains/utility-api";
import { ScriptEditor } from "./profile-edit/script-editor";
import type { ScriptPlaceholder } from "./profile-edit/script-editor-completions";
type Props = {
open: boolean;
onOpenChange: (open: boolean) => void;
agent: UtilityAgent | null;
onSuccess: () => void;
};
type FormState = {
name: string;
description: string;
prompt: string;
agent_id: string;
model: string;
};
const defaultFormState: FormState = {
name: "",
description: "",
prompt: "",
agent_id: "claude-acp",
model: "",
};
function toScriptPlaceholders(variables: TemplateVariable[]): ScriptPlaceholder[] {
return variables.map((v) => ({
key: v.name,
description: v.description,
example: v.example,
executor_types: [],
}));
}
type AgentModelSelectProps = {
agentId: string;
model: string;
inferenceAgents: InferenceAgent[];
availableModels: InferenceModel[];
onAgentChange: (agentId: string) => void;
onModelChange: (model: string) => void;
};
function AgentModelSelect({
agentId,
model,
inferenceAgents,
availableModels,
onAgentChange,
onModelChange,
}: AgentModelSelectProps) {
return (
m.is_default)?.id}
placeholder="Select model..."
disabled={availableModels.length === 0}
/>
);
}
type UtilityAgentFormProps = {
form: FormState;
setForm: React.Dispatch>;
isBuiltin: boolean;
inferenceAgents: InferenceAgent[];
availableModels: InferenceModel[];
placeholders: ScriptPlaceholder[];
};
function UtilityAgentForm({
form,
setForm,
isBuiltin,
inferenceAgents,
availableModels,
placeholders,
}: UtilityAgentFormProps) {
return (
);
}
export function UtilityAgentDialog({ open, onOpenChange, agent, onSuccess }: Props) {
const [form, setForm] = useState(defaultFormState);
const [saving, setSaving] = useState(false);
const [placeholders, setPlaceholders] = useState([]);
const [inferenceAgents, setInferenceAgents] = useState([]);
const isEdit = Boolean(agent);
// Fetch template variables and inference agents
useEffect(() => {
getTemplateVariables()
.then(({ variables }) => setPlaceholders(toScriptPlaceholders(variables)))
.catch(() => setPlaceholders([]));
listInferenceAgents()
.then(({ agents }) => setInferenceAgents(agents))
.catch(() => setInferenceAgents([]));
}, []);
// Get models for the selected agent
const selectedAgent = useMemo(
() => inferenceAgents.find((a) => a.id === form.agent_id),
[inferenceAgents, form.agent_id],
);
const availableModels = selectedAgent?.models ?? [];
// Auto-select default model when agent changes
useEffect(() => {
if (selectedAgent && !form.model) {
const defaultModel = (selectedAgent.models ?? []).find((m) => m.is_default);
if (defaultModel) {
setForm((f) => ({ ...f, model: defaultModel.id }));
}
}
}, [selectedAgent, form.model]);
useEffect(() => {
if (agent) {
setForm({
name: agent.name,
description: agent.description,
prompt: agent.prompt,
agent_id: agent.agent_id || "claude-acp",
model: agent.model || "",
});
} else {
setForm(defaultFormState);
}
}, [agent, open]);
const handleSubmit = async () => {
setSaving(true);
try {
const data = {
name: form.name,
description: form.description,
prompt: form.prompt,
agent_id: form.agent_id,
model: form.model,
};
if (isEdit && agent) {
await updateUtilityAgent(agent.id, data);
} else {
await createUtilityAgent(data);
}
onSuccess();
} catch (error) {
console.error("Failed to save agent:", error);
} finally {
setSaving(false);
}
};
const dialogTitle = isEdit ? "Edit Utility Agent" : "Create Utility Agent";
const getSubmitLabel = () => {
if (saving) return "Saving...";
return isEdit ? "Save Changes" : "Create Agent";
};
return (
);
}