6b40d5b2ec
Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
224 lines
7.1 KiB
TypeScript
224 lines
7.1 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* Draft state, save/delete actions and store synchronisation for the agent
|
|
* profile editor. Split out of `agent-profile-page.tsx` so that file stays
|
|
* presentation-only and under the 600-line cap.
|
|
*
|
|
* This module holds no JSX, so `i18next/no-literal-string` never inspects it —
|
|
* its toast copy is guarded only by the pseudo-locale.
|
|
*/
|
|
|
|
import { useMemo, useState } from "react";
|
|
import { permissionsToProfilePatch } from "@/lib/agent-permissions";
|
|
import { deleteAgentProfileAction, updateAgentProfileAction } from "@/app/actions/agents";
|
|
import { useAppStore } from "@/components/state-provider";
|
|
import { isProfileDirty } from "@/components/settings/agent-profile-dirty";
|
|
import type { useToast } from "@/components/toast-provider";
|
|
import type { AgentProfileDeleteConflict } from "@/components/settings/agent-profile-delete-dialog";
|
|
import { t as translate } from "@/lib/i18n";
|
|
import { toAgentProfileOption } from "@/lib/state/slices/settings/types";
|
|
import type { Agent, AgentProfile, PermissionSetting } from "@/lib/types/http";
|
|
|
|
export type SaveStatus = "idle" | "loading" | "success" | "error";
|
|
|
|
export function useSyncAgentsToStore() {
|
|
const setSettingsAgents = useAppStore((state) => state.setSettingsAgents);
|
|
const setAgentProfiles = useAppStore((state) => state.setAgentProfiles);
|
|
return (nextAgents: Agent[]) => {
|
|
setSettingsAgents(nextAgents);
|
|
setAgentProfiles(
|
|
nextAgents.flatMap((agentItem) =>
|
|
agentItem.profiles.map((agentProfile) => toAgentProfileOption(agentItem, agentProfile)),
|
|
),
|
|
);
|
|
};
|
|
}
|
|
|
|
export function useProfileEditorState(
|
|
profile: AgentProfile,
|
|
permissionSettings: Record<string, PermissionSetting>,
|
|
) {
|
|
const [draft, setDraft] = useState<AgentProfile>({ ...profile });
|
|
const [savedProfile, setSavedProfile] = useState<AgentProfile>(profile);
|
|
const [saveStatus, setSaveStatus] = useState<"idle" | "loading" | "success" | "error">("idle");
|
|
|
|
const isDirty = useMemo(
|
|
() => isProfileDirty(draft, savedProfile, permissionSettings),
|
|
[draft, savedProfile, permissionSettings],
|
|
);
|
|
|
|
return { draft, setDraft, savedProfile, setSavedProfile, saveStatus, setSaveStatus, isDirty };
|
|
}
|
|
|
|
export function errorMessage(error: unknown): string {
|
|
return error instanceof Error ? error.message : translate("agents:requestFailed");
|
|
}
|
|
|
|
type ProfileEditorActionsOptions = {
|
|
agent: Agent;
|
|
draft: AgentProfile;
|
|
savedProfile: AgentProfile;
|
|
setSavedProfile: (p: AgentProfile) => void;
|
|
setDraft: React.Dispatch<React.SetStateAction<AgentProfile>>;
|
|
setSaveStatus: (s: SaveStatus) => void;
|
|
settingsAgents: Agent[];
|
|
syncAgentsToStore: (agents: Agent[]) => void;
|
|
toast: ReturnType<typeof useToast>["toast"];
|
|
};
|
|
|
|
export function useProfileSave({
|
|
agent,
|
|
draft,
|
|
savedProfile,
|
|
setSavedProfile,
|
|
setDraft,
|
|
setSaveStatus,
|
|
settingsAgents,
|
|
syncAgentsToStore,
|
|
toast,
|
|
}: ProfileEditorActionsOptions) {
|
|
return async () => {
|
|
if (!draft.name.trim()) {
|
|
toast({
|
|
title: translate("agents:profileNameRequiredTitle"),
|
|
description: translate("agents:profileNameRequiredDescription"),
|
|
variant: "error",
|
|
});
|
|
return;
|
|
}
|
|
// Model is optional — an empty profile model means "use the agent's
|
|
// default", which is applied through ACP session model selection at session start.
|
|
setSaveStatus("loading");
|
|
try {
|
|
const updated = await updateAgentProfileAction(draft.id, {
|
|
name: draft.name,
|
|
model: draft.model,
|
|
mode: draft.mode,
|
|
config_options: draft.configOptions ?? {},
|
|
...permissionsToProfilePatch(draft),
|
|
cli_passthrough: draft.cliPassthrough,
|
|
// Omit an unchanged enabled value so a profile editor save cannot
|
|
// resurrect a concurrent list-toggle response from its stale draft.
|
|
enabled:
|
|
(draft.enabled ?? true) !== (savedProfile.enabled ?? true)
|
|
? (draft.enabled ?? true)
|
|
: undefined,
|
|
cli_flags: draft.cliFlags,
|
|
command_prefix: draft.commandPrefix ?? "",
|
|
env_vars: draft.envVars ?? [],
|
|
});
|
|
setSavedProfile(updated);
|
|
setDraft((current) => preserveNewerProfileDraft(current, draft, updated));
|
|
const nextAgents = settingsAgents.map((agentItem: Agent) =>
|
|
agentItem.id === agent.id
|
|
? {
|
|
...agentItem,
|
|
profiles: agentItem.profiles.map((p: AgentProfile) =>
|
|
p.id === updated.id ? updated : p,
|
|
),
|
|
}
|
|
: agentItem,
|
|
);
|
|
syncAgentsToStore(nextAgents);
|
|
setSaveStatus("success");
|
|
} catch (error) {
|
|
setSaveStatus("error");
|
|
toast({
|
|
title: translate("agents:failedToSaveProfile"),
|
|
description: errorMessage(error),
|
|
variant: "error",
|
|
});
|
|
throw error;
|
|
}
|
|
};
|
|
}
|
|
|
|
export function preserveNewerProfileDraft(
|
|
current: AgentProfile,
|
|
submitted: AgentProfile,
|
|
saved: AgentProfile,
|
|
): AgentProfile {
|
|
return current === submitted ? saved : current;
|
|
}
|
|
|
|
export function useProfileDelete(
|
|
agent: Agent,
|
|
draft: AgentProfile,
|
|
settingsAgents: Agent[],
|
|
syncAgentsToStore: (agents: Agent[]) => void,
|
|
toast: ReturnType<typeof useToast>["toast"],
|
|
) {
|
|
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
|
|
const [conflict, setConflict] = useState<AgentProfileDeleteConflict | null>(null);
|
|
|
|
const removeProfileFromStore = () => {
|
|
const nextAgents = settingsAgents.map((agentItem: Agent) =>
|
|
agentItem.id === agent.id
|
|
? {
|
|
...agentItem,
|
|
profiles: agentItem.profiles.filter((p: AgentProfile) => p.id !== draft.id),
|
|
}
|
|
: agentItem,
|
|
);
|
|
syncAgentsToStore(nextAgents);
|
|
window.location.assign("/settings/agents");
|
|
};
|
|
|
|
const requestDelete = () => {
|
|
setShowDeleteConfirm(true);
|
|
};
|
|
|
|
const handleDeleteProfile = async () => {
|
|
setShowDeleteConfirm(false);
|
|
const result = await deleteAgentProfileAction(draft.id);
|
|
if (result.status === "ok") {
|
|
removeProfileFromStore();
|
|
} else if (result.status === "conflict") {
|
|
setConflict({
|
|
activeSessions: result.activeSessions,
|
|
watchers: result.watchers,
|
|
routingTiers: result.routingTiers,
|
|
automations: result.automations,
|
|
});
|
|
} else {
|
|
toast({
|
|
title: translate("agents:failedToDeleteProfile"),
|
|
description: result.message,
|
|
variant: "error",
|
|
});
|
|
}
|
|
};
|
|
|
|
const handleForceDelete = async () => {
|
|
const result = await deleteAgentProfileAction(draft.id, true);
|
|
setConflict(null);
|
|
if (result.status === "ok") {
|
|
removeProfileFromStore();
|
|
} else if (result.status === "conflict") {
|
|
setConflict({
|
|
activeSessions: result.activeSessions,
|
|
watchers: result.watchers,
|
|
routingTiers: result.routingTiers,
|
|
automations: result.automations,
|
|
});
|
|
} else if (result.status === "error") {
|
|
toast({
|
|
title: translate("agents:failedToDeleteProfile"),
|
|
description: result.message,
|
|
variant: "error",
|
|
});
|
|
}
|
|
};
|
|
|
|
return {
|
|
requestDelete,
|
|
showDeleteConfirm,
|
|
setShowDeleteConfirm,
|
|
handleDeleteProfile,
|
|
conflict,
|
|
setConflict,
|
|
handleForceDelete,
|
|
};
|
|
}
|