"use client";
import { useMemo, useState } from "react";
import { AgentLogo } from "@/components/agent-logo";
import {
ContextMenuItem,
ContextMenuSub,
ContextMenuSubContent,
ContextMenuSubTrigger,
} from "@kandev/ui/context-menu";
import {
DropdownMenuItem,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
} from "@kandev/ui/dropdown-menu";
import { useAppStore } from "@/components/state-provider";
import { useRemoteAuthSpecs } from "@/hooks/domains/settings/use-remote-auth-specs";
import { useTaskExecutorProfile } from "@/hooks/domains/session/use-task-executor-profile";
import { isAgentConfiguredOnExecutor } from "@/lib/agent-executor-compat";
import type { AgentProfileOption } from "@/lib/state/slices";
export type HandoffProfile = {
id: string;
label: string;
agentName?: string;
disabled: boolean;
};
function profileDisplayLabel(profile: AgentProfileOption): { label: string; agentName: string } {
const parts = profile.label.split(" \u2022 ");
const agentLabel =
parts.length > 1 ? parts.slice(1).join(" \u2022 ") : (parts[0] ?? profile.label);
return {
label: agentLabel,
agentName: profile.agent_name,
};
}
export function useHandoffProfiles(taskId: string, enabled = true): HandoffProfile[] {
const agentProfiles = useAppStore((s) => s.agentProfiles.items);
const executorProfile = useTaskExecutorProfile(taskId, enabled);
const { specs: authSpecs, loaded: authLoaded } = useRemoteAuthSpecs();
return useMemo(() => {
return agentProfiles.map((profile) => {
const { label, agentName } = profileDisplayLabel(profile);
let disabled = false;
if (executorProfile && authLoaded) {
disabled = !isAgentConfiguredOnExecutor(profile, executorProfile, authSpecs);
}
return { id: profile.id, label, agentName, disabled };
});
}, [agentProfiles, executorProfile, authSpecs, authLoaded]);
}
function HandoffProfileList({
profiles,
onSelectProfile,
Item,
}: {
profiles: HandoffProfile[];
onSelectProfile: (profileId: string) => void;
Item: typeof ContextMenuItem | typeof DropdownMenuItem;
}) {
if (profiles.length === 0) {
return (
-
No agent profiles configured
);
}
return profiles.map((profile) => (
- onSelectProfile(profile.id)}
>
{profile.agentName && (
)}
{profile.label}
));
}
type HandoffMenuProps = {
taskId: string;
disabled?: boolean;
onSelectProfile: (profileId: string) => void;
};
export function HandoffContextMenuSub({ taskId, disabled, onSelectProfile }: HandoffMenuProps) {
const [submenuOpen, setSubmenuOpen] = useState(false);
const profiles = useHandoffProfiles(taskId, submenuOpen);
const submenuDisabled = disabled || (profiles.length > 0 && profiles.every((p) => p.disabled));
return (
Handoff
);
}
export function HandoffDropdownMenuSub({ taskId, disabled, onSelectProfile }: HandoffMenuProps) {
const [submenuOpen, setSubmenuOpen] = useState(false);
const profiles = useHandoffProfiles(taskId, submenuOpen);
const submenuDisabled = disabled || (profiles.length > 0 && profiles.every((p) => p.disabled));
return (
Handoff
);
}