9381a6a9f7
* fix(scripts): launch dev-isolated backend via __backend subcommand The kandev binary became a bundle launcher with the restart supervisor (ADR 0019) and exits without KANDEV_BUNDLE_DIR when invoked bare; the isolated dev instance must run the backend entrypoint directly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(mcp): session spawning, sibling messaging, and renameable tabs - spawn_session_kandev spawns an additional agent session on an existing task (default: the caller's own) via LaunchSession(IntentStart), with optional agent_profile_id (defaults to the spawner's profile) and a session name; the initial prompt carries spawner attribution and reply instructions. - message_task_kandev accepts an optional session_id to target a specific session, enabling same-task sibling messaging. Explicit targets are pinned through dispatch so the idle-path primary-session re-resolution cannot reroute a sibling message back to its sender (regression-tested). - task_sessions gains a name column (migration ordered after the agent_execution_id-drop rebuild), a session.rename WS action, and inline tab rename UI; custom names take precedence over derived agent/model labels across the tab, agents dropdown, mobile list, chat header, and dockview panel titles. - Inter-session messages stamp sender_session_name into metadata so the sender badge shows which session sent the message. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(mcp): address review findings on session spawn/rename/messaging - include name in task-session DTOs (list + full, no omitempty) so persisted tab names survive list hydration and clears propagate - sanitize sender task/session ids embedded in <kandev-system> blocks - reject cross-workspace spawn targets; require sender_task_id - truncate session names by runes, not bytes - distinguish not-found from internal errors in message target lookup - guard TabRenameInput blur-commit after Enter/Escape resolution - cap optimistic rename length client-side and toast on rename failure - add tests: spawn launch-failure and cross-workspace paths, renameSession API client Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(mcp): round-two review fixes for spawn/rename lint and hardening - extract spawn validation/authorization helpers (cyclop/nestif limits) - make stripSystemTag replace until stable to block nested tag evasion - load session before rename so a durable rename can never skip its broadcast; surface lookup failures to the caller instead - drop redundant sessionLauncher nil check (guarded registration) - add maxLength to the rename input matching the server cap - add tests: nested sanitizer evasion, spawn rename-failure tolerance Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
140 lines
5.3 KiB
TypeScript
140 lines
5.3 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useMemo } from "react";
|
|
import { IconMessagePlus, IconStar } from "@tabler/icons-react";
|
|
import {
|
|
DropdownMenuItem,
|
|
DropdownMenuLabel,
|
|
DropdownMenuSeparator,
|
|
} from "@kandev/ui/dropdown-menu";
|
|
import { useAppStore } from "@/components/state-provider";
|
|
import { useDockviewStore } from "@/lib/state/dockview-store";
|
|
import { useTaskSessions } from "@/hooks/use-task-sessions";
|
|
import { addSessionPanel } from "@/lib/state/dockview-panel-actions";
|
|
import { getSessionStateIcon } from "@/lib/ui/state-icons";
|
|
import { AgentLogo } from "@/components/agent-logo";
|
|
import { markSessionTabUserActivationIntent } from "@/components/task/session-tab-activation-intent";
|
|
import type { TaskSession } from "@/lib/types/http";
|
|
import type { AgentProfileOption } from "@/lib/state/slices";
|
|
|
|
type AgentInfo = { label: string; agentName: string };
|
|
|
|
function resolveAgentInfo(
|
|
session: TaskSession,
|
|
profilesById: Record<string, AgentProfileOption>,
|
|
): AgentInfo {
|
|
const profile = session.agent_profile_id ? profilesById[session.agent_profile_id] : null;
|
|
const agentName = profile?.agent_name ?? "";
|
|
// A user-supplied session name wins over the derived profile label,
|
|
// matching the session tab title precedence (resolveSessionTabTitle).
|
|
if (session.name) return { label: session.name, agentName };
|
|
if (!profile) return { label: "Unknown agent", agentName: "" };
|
|
const parts = profile.label.split(" \u2022 ");
|
|
return { label: parts[1] || parts[0] || profile.label, agentName };
|
|
}
|
|
|
|
/**
|
|
* Renders session items inside the + dropdown menu.
|
|
* Each item shows session number, agent label, primary star, and state icon.
|
|
* Clicking focuses an existing tab or re-opens a closed one.
|
|
*/
|
|
export function SessionReopenMenuItems({
|
|
taskId,
|
|
groupId,
|
|
onNewSession,
|
|
}: {
|
|
taskId: string;
|
|
groupId?: string;
|
|
/**
|
|
* Click handler for the leading "New Agent" item rendered as the
|
|
* first row under the section label. Omit to hide the row.
|
|
*/
|
|
onNewSession?: () => void;
|
|
}) {
|
|
const { sessions } = useTaskSessions(taskId);
|
|
const api = useDockviewStore((s) => s.api);
|
|
const centerGroupId = useDockviewStore((s) => s.centerGroupId);
|
|
const agentProfiles = useAppStore((s) => s.agentProfiles.items);
|
|
const primarySessionId = useAppStore((s) => {
|
|
const task = s.kanban.tasks.find((t: { id: string }) => t.id === taskId);
|
|
return task?.primarySessionId ?? null;
|
|
});
|
|
|
|
const profilesById = useMemo(
|
|
() => Object.fromEntries(agentProfiles.map((p: AgentProfileOption) => [p.id, p])),
|
|
[agentProfiles],
|
|
);
|
|
|
|
const sortedSessions = useMemo(
|
|
() =>
|
|
[...sessions].sort(
|
|
(a, b) => new Date(a.started_at).getTime() - new Date(b.started_at).getTime(),
|
|
),
|
|
[sessions],
|
|
);
|
|
|
|
const handleClick = useCallback(
|
|
(sessionId: string, label: string, groupId?: string) => {
|
|
if (!api) return;
|
|
// Reopening a session within the same task = same env, so the env switch
|
|
// action no-ops naturally. We just create the chat panel.
|
|
markSessionTabUserActivationIntent(sessionId);
|
|
addSessionPanel(api, groupId ?? centerGroupId, sessionId, label);
|
|
},
|
|
[api, centerGroupId],
|
|
);
|
|
|
|
// Render the section even when there are no sessions yet — the leading
|
|
// "New Agent" row should still be reachable from the menu. Hide the
|
|
// whole block only when neither the create handler nor any sessions
|
|
// are present (e.g. unmounted contexts).
|
|
if (sortedSessions.length === 0 && !onNewSession) return null;
|
|
|
|
return (
|
|
<>
|
|
<DropdownMenuLabel className="text-xs text-muted-foreground">Agents</DropdownMenuLabel>
|
|
{onNewSession && (
|
|
<DropdownMenuItem
|
|
onClick={onNewSession}
|
|
className="cursor-pointer text-xs gap-1.5"
|
|
data-testid="new-session-button"
|
|
>
|
|
<IconMessagePlus className="h-3.5 w-3.5 shrink-0" />
|
|
<span className="flex-1 truncate">New Agent</span>
|
|
</DropdownMenuItem>
|
|
)}
|
|
{sortedSessions.map((session, index) => {
|
|
const info = resolveAgentInfo(session, profilesById);
|
|
const isPrimary = session.id === primarySessionId;
|
|
const isOpen = Boolean(api?.getPanel(`session:${session.id}`));
|
|
return (
|
|
<DropdownMenuItem
|
|
key={session.id}
|
|
onClick={() => handleClick(session.id, info.label, groupId)}
|
|
className={`cursor-pointer text-xs gap-1.5 ${isOpen ? "opacity-50" : ""}`}
|
|
data-testid={`reopen-session-${session.id}`}
|
|
>
|
|
<span
|
|
data-testid={`reopen-session-seq-${index + 1}`}
|
|
className="shrink-0 text-[11px] font-medium leading-none text-muted-foreground bg-foreground/10 rounded px-1.5 py-0.5"
|
|
>
|
|
#{index + 1}
|
|
</span>
|
|
{info.agentName && (
|
|
<AgentLogo agentName={info.agentName} size={14} className="shrink-0" />
|
|
)}
|
|
<span className="flex-1 truncate">{info.label}</span>
|
|
{isPrimary && <IconStar className="h-3 w-3 fill-foreground/50 stroke-0 shrink-0" />}
|
|
{session.state !== "RUNNING" &&
|
|
session.state !== "STARTING" &&
|
|
session.state !== "WAITING_FOR_INPUT" && (
|
|
<span className="shrink-0">{getSessionStateIcon(session.state, "h-3 w-3")}</span>
|
|
)}
|
|
</DropdownMenuItem>
|
|
);
|
|
})}
|
|
<DropdownMenuSeparator />
|
|
</>
|
|
);
|
|
}
|