Skip to content

Build and run agents you can see, understand and trust.

44.2 KB
Raw Blame
import type { PermissionContext } from '@agentscope-ai/agentscope/permission';
import type { TaskContext } from '@agentscope-ai/agentscope/state';
import {
	Activity,
	BookText,
	BrainCircuit,
	ChevronDown,
	Database,
	ListTodo,
	PanelRight,
	RefreshCw,
	ShieldCheck,
	UsersRound,
	X,
} from 'lucide-react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { toast } from 'sonner';

import type {
	ASRModelConfig,
	ChatModelConfig,
	PermissionMode,
	RealtimeModelConfig,
	SessionKnowledgeConfig,
	TTSModelConfig,
	UpdateSessionRequest,
} from '@/api';
import { asrApi, sessionApi } from '@/api';
import MCPSvg from '@/assets/images/mcp.svg?react';
import { ChatContent } from '@/components/chat/ChatContent.tsx';
import { SubagentHitlCard } from '@/components/chat/SubagentHitlCard';
import { CreateCredentialDialog } from '@/components/dialog/CreateCredentialDialog';
import { BackgroundTasksPanel } from '@/components/panel/BackgroundTasksPanel';
import { KnowledgeBasePanel } from '@/components/panel/KnowledgeBasePanel';
import { McpPanel } from '@/components/panel/McpPanel';
import { PanelDock, type PanelDescriptor, type PanelKey } from '@/components/panel/PanelDock.tsx';
import { PermissionPanel } from '@/components/panel/PermissionPanel';
import { SkillPanel } from '@/components/panel/SkillPanel';
import { TaskPanel } from '@/components/panel/TaskPanel';
import { TeamPanel } from '@/components/panel/TeamPanel';
import { KnowledgeBaseParametersPopover } from '@/components/popover/KnowledgeBaseParametersPopover';
import { ModelParametersPopover } from '@/components/popover/ModelParametersPopover';
import { LlmSelect } from '@/components/select/LlmSelect';
import { PermissionModeSelect } from '@/components/select/PermissionModeSelect.tsx';
import { RealtimeModelSelect } from '@/components/select/RealtimeModelSelect';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import {
	DropdownMenu,
	DropdownMenuCheckboxItem,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import {
	ResizableHandle,
	ResizablePanel,
	ResizablePanelGroup,
} from '@/components/ui/resizable.tsx';
import { Sheet, SheetClose, SheetContent, SheetHeader, SheetTitle } from '@/components/ui/sheet';
import { SidebarTrigger } from '@/components/ui/sidebar';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { useAvailableASRModels } from '@/hooks/useAvailableASRModels';
import { useAvailableModels } from '@/hooks/useAvailableModels';
import { useAvailableRealtimeModels } from '@/hooks/useAvailableRealtimeModels';
import { useBackgroundTasks } from '@/hooks/useBackgroundTasks';
import { useKnowledgeBaseMiddlewareSchema } from '@/hooks/useKnowledgeBaseMiddlewareSchema';
import { useKnowledgeBases } from '@/hooks/useKnowledgeBases';
import { useMessages } from '@/hooks/useMessages';
import { useRealtimeVoice } from '@/hooks/useRealtimeVoice';
import { useSessions } from '@/hooks/useSessions';
import { useWorkspace } from '@/hooks/useWorkspace.ts';
import { useWorkspaceStatus } from '@/hooks/useWorkspaceStatus';
import { useTranslation } from '@/i18n/useI18n';
import { getPendingHitlCalls } from '@/lib/pendingToolCalls';

const REALTIME_EXCLUDED_ENUM_VALUES: Record<string, unknown[]> = {
	turn_detection: ['none'],
};

interface ChatViewportProps {
	/**
	 * The agent that owns the session being viewed. May be the
	 * user-facing leader agent or — when drilled into a team member
	 * via the URL's `:memberId` slot — a worker agent.
	 */
	agentId: string | null;
	/**
	 * The session whose messages, model config, permission mode, and
	 * workspace drive every control rendered here.
	 */
	sessionId: string | null;
	/**
	 * Optional hook invoked when a server-side change to this session
	 * or its team arrives on the SSE stream. The outer page owns the
	 * session list that backs the sidebar, so it must be told to
	 * refetch too; passing this callback wires that signal up.
	 */
	onSessionsChanged?: () => void;
}

/** Maximum number of panels stacked in a single dock column. */
const MAX_PANELS_PER_COLUMN = 2;

/** localStorage key holding the dock layout across page navigations. */
const PANEL_LAYOUT_KEY = 'chat_panel_layout';

// Typed as a full Record so adding a PanelKey without listing it here
// is a compile error rather than a silently unrestorable panel.
const KNOWN_PANELS: Record<PanelKey, true> = {
	plan: true,
	mcp: true,
	skill: true,
	permission: true,
	knowledge: true,
	team: true,
	background: true,
};

/**
 * Restore the persisted dock layout, dropping anything that is no
 * longer a known panel (keys get renamed/removed across releases).
 *
 * @returns The stored layout, or an empty one when absent or corrupt.
 */
function loadPanelLayout(): PanelKey[][] {
	try {
		const parsed: unknown = JSON.parse(localStorage.getItem(PANEL_LAYOUT_KEY) ?? '[]');
		if (!Array.isArray(parsed)) return [];
		return parsed
			.map((column: unknown) =>
				Array.isArray(column)
					? column.filter((key): key is PanelKey => key in KNOWN_PANELS)
					: [],
			)
			.filter((column) => column.length > 0);
	} catch {
		return [];
	}
}

/**
 * Insert a panel into the dock layout. Scans columns left to right and
 * appends to the first one with spare room; if every column is full a
 * new rightmost column is created. No-op when the panel is already
 * open.
 *
 * @param layout - The current column/panel arrangement.
 * @param key - The panel to open.
 * @returns A new layout array (the input is never mutated).
 */
function openPanelInLayout(layout: PanelKey[][], key: PanelKey): PanelKey[][] {
	if (layout.some((column) => column.includes(key))) return layout;
	const targetIndex = layout.findIndex((column) => column.length < MAX_PANELS_PER_COLUMN);
	if (targetIndex === -1) return [...layout, [key]];
	return layout.map((column, index) => (index === targetIndex ? [...column, key] : column));
}

/**
 * Remove a panel from the dock layout, dropping its column entirely if
 * it becomes empty.
 *
 * @param layout - The current column/panel arrangement.
 * @param key - The panel to close.
 * @returns A new layout array (the input is never mutated).
 */
function closePanelInLayout(layout: PanelKey[][], key: PanelKey): PanelKey[][] {
	return layout
		.map((column) => column.filter((panelKey) => panelKey !== key))
		.filter((column) => column.length > 0);
}

/**
 * The right-hand main panel of the chat page — every UI element that
 * operates on a single `(agentId, sessionId)` pair lives here:
 * model selector, permission mode select, message stream, workspace
 * drawer, and the team sidebar.
 *
 * Self-contained by design. The outer page passes in the
 * `(agentId, sessionId)` it wants displayed (which may be the leader
 * session or a focused team member's session) and this component
 * does the rest — fetching the session view, syncing local UI state
 * with it, and writing changes back to the same session. Switching
 * between leader and member is just a prop change; no internal
 * branching is needed.
 *
 * @param agentId - The agent to operate on. `null` while no agent is
 *   selected yet (renders an empty / disabled state).
 * @param sessionId - The session to operate on. `null` while no
 *   session is selected yet.
 * @returns The right-side main JSX of the chat page.
 */
export function ChatViewport({ agentId, sessionId, onSessionsChanged }: ChatViewportProps) {
	const { t } = useTranslation();
	const { sessions, refetch: refetchSessions } = useSessions(agentId);
	const { groups } = useAvailableModels();
	const { groups: realtimeGroups } = useAvailableRealtimeModels();
	const { groups: asrGroups, loading: asrModelsLoading } = useAvailableASRModels();

	const [selectedModel, setSelectedModel] = useState<ChatModelConfig | null>(null);
	const [selectedFallbackModel, setSelectedFallbackModel] = useState<ChatModelConfig | null>(
		null,
	);
	const [selectedTTSModel, setSelectedTTSModel] = useState<TTSModelConfig | null>(null);
	const [selectedASRModel, setSelectedASRModel] = useState<ASRModelConfig | null>(null);
	const [selectedRealtimeModel, setSelectedRealtimeModel] = useState<RealtimeModelConfig | null>(
		null,
	);
	const [selectedKnowledgeConfig, setSelectedKnowledgeConfig] =
		useState<SessionKnowledgeConfig | null>(null);
	const [selectedPermissionMode, setSelectedPermissionMode] = useState<string>('default');
	const [credentialOpen, setCredentialOpen] = useState(false);
	const [tasksContext, setTasksContext] = useState<TaskContext | null>(null);
	const [permissionContext, setPermissionContext] = useState<PermissionContext | null>(null);
	const [configPending, setConfigPending] = useState(false);
	// Dock layout: columns laid out left→right, each holding up to 2
	// panels stacked top→bottom. Open order determines placement.
	// Persisted so leaving and returning to /chat keeps the same panels.
	const [panelLayout, setPanelLayout] = useState<PanelKey[][]>(loadPanelLayout);
	const viewportRef = useRef<HTMLElement>(null);
	const [panelsInSheet, setPanelsInSheet] = useState(false);
	// Dismissing the narrow-screen overlay must preserve the desktop layout.
	const [sheetOpen, setSheetOpen] = useState(true);
	const [selectedSheetPanel, setSelectedSheetPanel] = useState<PanelKey | null>(null);

	useEffect(() => {
		const viewport = viewportRef.current;
		if (!viewport) return;
		let frame = 0;
		// Measure available chat space, including changes to the app sidebars.
		const observer = new ResizeObserver(([entry]) => {
			// Change layout in the next frame, outside the observer's delivery loop.
			window.cancelAnimationFrame(frame);
			frame = window.requestAnimationFrame(() => {
				setPanelsInSheet(entry.contentRect.width < 768);
			});
		});
		observer.observe(viewport);
		return () => {
			observer.disconnect();
			window.cancelAnimationFrame(frame);
		};
	}, []);

	const dockLayout = panelsInSheet ? [] : panelLayout;
	const sheetPanelKeys = panelLayout.flat();
	const activeSheetPanel =
		selectedSheetPanel && sheetPanelKeys.includes(selectedSheetPanel)
			? selectedSheetPanel
			: sheetPanelKeys[0];

	useEffect(() => {
		localStorage.setItem(PANEL_LAYOUT_KEY, JSON.stringify(panelLayout));
	}, [panelLayout]);

	// When the viewport agent differs from the outer page's selected
	// agent (i.e. user drilled into a team member), `refetchSessions`
	// only refreshes the member's session list, so we also fire the
	// parent's refetch to keep its copy in sync.
	//
	// Surfacing the team panel here is what makes a team visible at all
	// — `TeamCreate` / `AgentCreate` / `AgentInvite` are agent tools, so
	// the user never opened a dialog that could have opened the panel.
	// `team_updated` also fires on `TeamDelete` and carries no payload,
	// hence checking the refetched list rather than opening blindly.
	const handleTeamUpdated = useCallback(async () => {
		const next = await refetchSessions();
		if (next.some((v) => v.session.id === sessionId && v.team)) {
			// `openPanelInLayout`, not `togglePanel` — the latter would
			// close a panel the user already has open.
			setPanelLayout((layout) => openPanelInLayout(layout, 'team'));
		}
		onSessionsChanged?.();
	}, [refetchSessions, sessionId, onSessionsChanged]);

	// Auto-naming replaced the session's placeholder name. The outer
	// page shares this cached list whenever both are looking at the same
	// agent; when they are not — drilled into a team member — it needs
	// its own nudge.
	const handleSessionUpdated = useCallback(async () => {
		await refetchSessions();
		onSessionsChanged?.();
	}, [refetchSessions, onSessionsChanged]);

	// Surface the plan panel the first time a session's tasks arrive over
	// the stream, and only then — reopening it on every update would undo
	// the user closing it. `state_updated` also fires for permission-only
	// changes and always carries `tasks_context`, hence gating on a
	// non-empty task list rather than the field being present.
	const taskPanelOpenedForRef = useRef<string | null>(null);
	const handleStateUpdated = useCallback(
		(value: Record<string, unknown>) => {
			if (value.tasks_context) {
				const incoming = value.tasks_context as TaskContext;
				setTasksContext(incoming);
				if (incoming.tasks.length > 0 && taskPanelOpenedForRef.current !== sessionId) {
					taskPanelOpenedForRef.current = sessionId;
					setPanelLayout((layout) => openPanelInLayout(layout, 'plan'));
				}
			}
			if (value.permission_context) {
				setPermissionContext(value.permission_context as PermissionContext);
			}
		},
		[sessionId],
	);

	const realtimeVoice = useRealtimeVoice(agentId, sessionId);
	const voiceBusy = realtimeVoice.state === 'active' || realtimeVoice.state === 'connecting';
	const {
		msgs,
		loading: messagesLoading,
		phase,
		activeReplyId,
		queuedCount,
		queuedItems,
		queueReorderDisabled,
		send,
		updateQueued,
		deleteQueued,
		steerQueued,
		moveQueued,
		reorderQueued,
		onUserConfirm,
		onClientExternalToolResult,
		onExternalExecutionResult,
		onSubagentConfirm,
		subagentHitl,
		interrupt,
	} = useMessages(agentId, sessionId, {
		onTeamUpdated: handleTeamUpdated,
		onStateUpdated: handleStateUpdated,
		onSessionUpdated: handleSessionUpdated,
		isRealtimeAudioActive: () => voiceBusy,
		sendRealtimeUserConfirm: realtimeVoice.userConfirm,
		sendRealtimeExternalExecutionResult: realtimeVoice.externalExecutionResult,
	});
	const {
		mcps,
		loading: mcpsLoading,
		addMcps,
		addMcpsFromLibrary,
		removeMcp,
		skills,
		skillsLoading,
		uploadSkill,
		addSkillsFromLibrary,
		removeSkill,
	} = useWorkspace(agentId, sessionId);
	const { knowledgeBases, loading: knowledgeBasesLoading } = useKnowledgeBases();
	const { schema: kbMiddlewareSchema } = useKnowledgeBaseMiddlewareSchema();

	// Toggle a panel open/closed from the top-bar buttons.
	const togglePanel = useCallback((key: PanelKey) => {
		setPanelLayout((layout) =>
			layout.some((column) => column.includes(key))
				? closePanelInLayout(layout, key)
				: openPanelInLayout(layout, key),
		);
	}, []);

	// Close a panel (driven by the panel's own close button).
	const closePanel = useCallback((key: PanelKey) => {
		setPanelLayout((layout) => closePanelInLayout(layout, key));
	}, []);

	const isPanelOpen = useCallback(
		(key: PanelKey) => panelLayout.some((column) => column.includes(key)),
		[panelLayout],
	);
	const backgroundOpen = isPanelOpen('background');
	const {
		data: backgroundData,
		isPending: backgroundLoading,
		error: backgroundError,
		isPaused: backgroundPaused,
		isFetching: backgroundRefreshing,
		dataUpdatedAt: backgroundUpdatedAt,
		refetch: refetchBackground,
		visible: backgroundVisible,
	} = useBackgroundTasks(
		sessionId,
		panelsInSheet ? sheetOpen && activeSheetPanel === 'background' : backgroundOpen,
	);
	const backgroundUnavailable = !!backgroundError || backgroundPaused;

	/**
	 * Persist a knowledge-base attachment change. `null` detaches every
	 * knowledge base from this session, removing the `RAGMiddleware`.
	 *
	 * Declared above `panels` (rather than alongside the other model
	 * handlers below) because `panels` is built inside `useMemo` and
	 * references this handler eagerly — a later `const` would still be
	 * in the temporal dead zone when the memo factory runs on first
	 * render.
	 *
	 * @param config - New attachment, or `null` to detach all.
	 */
	/**
	 * Persist a session config change, applying it locally only once
	 * the server accepts it.
	 *
	 * The backend rejects config writes with 409 while a chat run holds
	 * the session, so an optimistic update would leave the control
	 * showing a value the session does not have. Waiting for the
	 * response keeps the control on its previous value with no rollback
	 * bookkeeping; `client.ts` has already surfaced the error toast by
	 * the time we land in `catch`.
	 *
	 * Declared above `panels` for the same temporal-dead-zone reason as
	 * `handleKnowledgeConfigChange` below.
	 *
	 * @param body - The PATCH body.
	 * @param apply - Mirrors the change into local state on success.
	 */
	const patchConfig = useCallback(
		async (body: UpdateSessionRequest, apply: () => void) => {
			if (!sessionId || !agentId) return;
			setConfigPending(true);
			try {
				await sessionApi.update(sessionId, agentId, body);
				apply();
				await refetchSessions();
			} catch {
				// Toast already shown; local state deliberately untouched.
			} finally {
				setConfigPending(false);
			}
		},
		[sessionId, agentId, refetchSessions],
	);

	const handleKnowledgeConfigChange = useCallback(
		async (config: SessionKnowledgeConfig | null) => {
			await patchConfig({ knowledge_config: config }, () =>
				setSelectedKnowledgeConfig(config),
			);
		},
		[patchConfig],
	);

	// Declared above `panels` — the memo factory reads `view.team`
	// eagerly on first render, so a later `const` would still be in
	// the temporal dead zone.
	const view = sessions.find((v) => v.session.id === sessionId) ?? null;

	const { status: workspaceStatus, refetch: refetchWorkspaceStatus } = useWorkspaceStatus(
		agentId,
		sessionId,
		view?.session.config.cwd ?? null,
	);

	// A finished reply is the one moment the agent may have changed the
	// working tree, and it is why nothing polls for git status. Watching
	// `phase` rather than the REPLY_END event also covers the interrupt
	// timeout, which reaches idle without one.
	const prevPhaseRef = useRef(phase);
	useEffect(() => {
		const wasRunning = prevPhaseRef.current !== 'idle';
		prevPhaseRef.current = phase;
		if (wasRunning && phase === 'idle') void refetchWorkspaceStatus();
	}, [phase, refetchWorkspaceStatus]);

	// Build the panel descriptors with live data. Rebuilt on every
	// data change so the dock always renders the latest state — the
	// dock itself stays free of any data dependency.
	const panels = useMemo<Record<PanelKey, PanelDescriptor>>(
		() => ({
			background: {
				title: (
					<span className="flex items-center gap-2">
						{t('panel.background.title')}
						{backgroundData && !backgroundUnavailable && (
							<Badge variant="outline" className="tabular-nums">
								{t('panel.background.runningCount', {
									count: backgroundData.tasks.filter(
										(task) => task.status === 'running',
									).length,
								})}
							</Badge>
						)}
					</span>
				),
				icon: <Activity className="size-4" />,
				actions: (
					<Button
						variant="ghost"
						size="icon-sm"
						aria-label={t('panel.background.refresh')}
						tooltip={t('panel.background.refresh')}
						disabled={!sessionId || backgroundRefreshing}
						onClick={() => void refetchBackground()}
					>
						<RefreshCw
							className={
								backgroundRefreshing ? 'motion-safe:animate-spin' : undefined
							}
						/>
					</Button>
				),
				content: (
					<BackgroundTasksPanel
						key={sessionId}
						sessionId={sessionId}
						tasks={backgroundData?.tasks}
						loading={backgroundLoading && !backgroundUnavailable}
						error={backgroundUnavailable}
						refreshing={backgroundRefreshing}
						visible={backgroundVisible}
						updatedAt={backgroundUpdatedAt}
						onRefresh={() => void refetchBackground()}
					/>
				),
			},
			plan: {
				title: t('panel.plan.title'),
				icon: <ListTodo className="size-4" />,
				content: <TaskPanel tasksContext={tasksContext} />,
			},
			mcp: {
				title: 'MCP',
				icon: <MCPSvg className="size-4" />,
				content: (
					<McpPanel
						mcps={mcps}
						loading={mcpsLoading}
						onAdd={addMcps}
						onAddFromLibrary={addMcpsFromLibrary}
						onRemove={removeMcp}
					/>
				),
			},
			skill: {
				title: t('panel.skill.title'),
				icon: <BookText className="size-4" />,
				content: (
					<SkillPanel
						skills={skills}
						loading={skillsLoading}
						onUpload={uploadSkill}
						onAddFromLibrary={addSkillsFromLibrary}
						onRemove={removeSkill}
					/>
				),
			},
			permission: {
				title: (
					<span className="flex items-center gap-x-2">
						{t('panel.permission.title')}
						{permissionContext?.mode ? (
							<Badge variant="outline" className="capitalize">
								{t('panel.permission.mode', { mode: permissionContext.mode })}
							</Badge>
						) : null}
					</span>
				),
				icon: <ShieldCheck className="size-4" />,
				content: <PermissionPanel permissionContext={permissionContext} />,
			},
			knowledge: {
				title: (
					<span className="flex items-center gap-x-2">
						{t('panel.knowledge.title')}
						{selectedKnowledgeConfig?.knowledge_base_ids.length ? (
							<Badge variant="outline">
								{selectedKnowledgeConfig.knowledge_base_ids.length}
							</Badge>
						) : null}
					</span>
				),
				icon: <Database className="size-4" />,
				actions: (
					<KnowledgeBaseParametersPopover
						value={selectedKnowledgeConfig}
						schema={kbMiddlewareSchema}
						onChange={handleKnowledgeConfigChange}
						disabled={!sessionId}
					/>
				),
				content: (
					<KnowledgeBasePanel
						knowledgeBases={knowledgeBases}
						loading={knowledgeBasesLoading}
						value={selectedKnowledgeConfig}
						onChange={handleKnowledgeConfigChange}
						disabled={!sessionId}
					/>
				),
			},
			team: {
				title: (
					<span className="flex items-center gap-x-2">
						{t('common.team')}
						{view?.team ? (
							<Badge variant="outline">{view.team.members.length}</Badge>
						) : null}
					</span>
				),
				icon: <UsersRound className="size-4" />,
				content: <TeamPanel team={view?.team ?? null} currentSessionId={sessionId} />,
			},
		}),
		[
			t,
			backgroundData,
			backgroundUnavailable,
			backgroundLoading,
			backgroundRefreshing,
			backgroundUpdatedAt,
			backgroundVisible,
			refetchBackground,
			tasksContext,
			mcps,
			mcpsLoading,
			addMcps,
			addMcpsFromLibrary,
			removeMcp,
			skills,
			skillsLoading,
			uploadSkill,
			addSkillsFromLibrary,
			removeSkill,
			permissionContext,
			knowledgeBases,
			knowledgeBasesLoading,
			selectedKnowledgeConfig,
			kbMiddlewareSchema,
			handleKnowledgeConfigChange,
			sessionId,
			view,
		],
	);

	// Safety net for a `view` that never arrives. A session created from
	// the outer page reaches this list on its own, since both mount the
	// same cached query — but not when the two are looking at different
	// agents (drilled into a team member), and not for a write that
	// happened outside either. Without a `view` every effect below
	// early-returns on `!view`, leaving the model select and friends
	// pinned to whatever the previously-viewed session had configured.
	useEffect(() => {
		if (!sessionId) return;
		if (view) return;
		refetchSessions();
	}, [sessionId, view, refetchSessions]);

	// Reset local UI state when the target session changes. Otherwise
	// the model select (and disabled-state guards on `send`) would
	// show the previous session's model during the in-flight window
	// before `view` repopulates — and an immediate send would post to
	// a session whose backend config doesn't actually have that model.
	useEffect(() => {
		setSelectedModel(null);
		setSelectedFallbackModel(null);
		setSelectedTTSModel(null);
		setSelectedASRModel(null);
		setSelectedRealtimeModel(null);
		setSelectedKnowledgeConfig(null);
	}, [sessionId]);

	const selectedModelCard = useMemo(() => {
		if (!selectedModel) return null;
		const items = groups[selectedModel.type];
		if (!items) return null;
		for (const { models } of items) {
			const card = models.find(
				(m) =>
					m.name === selectedModel.model &&
					(!selectedModel.model_class || m.model_class === selectedModel.model_class),
			);
			if (card) return card;
		}
		return null;
	}, [groups, selectedModel]);

	const selectedRealtimeModelCard = useMemo(() => {
		if (!selectedRealtimeModel) return null;
		for (const items of Object.values(realtimeGroups)) {
			for (const { credential, models } of items) {
				if (credential.id !== selectedRealtimeModel.credential_id) continue;
				const card = models.find(
					(model) =>
						model.model_type === selectedRealtimeModel.type &&
						model.name === selectedRealtimeModel.model,
				);
				if (card) return card;
			}
		}
		return null;
	}, [realtimeGroups, selectedRealtimeModel]);
	const realtimeUsesSessionChat =
		selectedRealtimeModelCard?.delegation_backend === 'session_chat';

	const selectedASRModelCard = useMemo(() => {
		if (!selectedASRModel) return null;
		for (const item of asrGroups[selectedASRModel.type] ?? []) {
			if (item.credential.id !== selectedASRModel.credential_id) continue;
			const card = item.models.find((model) => model.name === selectedASRModel.model);
			if (card) return card;
		}
		return null;
	}, [asrGroups, selectedASRModel]);

	/**
	 * Pick the first model the available-models endpoint surfaces, used
	 * as a sensible default when the current session has no model
	 * configured yet.
	 *
	 * @returns The first available `ChatModelConfig`, or `null` when
	 *   no credentials / models are configured.
	 */
	const getFirstAvailableModel = (): ChatModelConfig | null => {
		const firstType = Object.keys(groups)[0];
		if (!firstType) return null;
		const items = groups[firstType];
		if (!items || items.length === 0) return null;
		const firstItem = items[0];
		const firstModel = firstItem.models[0];
		if (!firstModel) return null;
		return {
			type: firstType,
			credential_id: firstItem.credential.id,
			model: firstModel.name,
			model_class: firstModel.model_class,
			parameters: {},
		};
	};

	// Seed tasks + permission from the session snapshot ONCE per
	// session, then leave them to the CustomEvent(name="state_updated")
	// stream via `handleStateUpdated`.
	//
	// Seeding on every `view` change would be wrong: storage is only
	// written when a run ends, so mid-run the snapshot still holds the
	// run-start values. `view` gets a new identity on every
	// `refetchSessions()` — which `team_updated` triggers — and
	// re-seeding then would silently roll both panels back to where the
	// reply started. Clearing on `!view` still matters so switching
	// sessions cannot leak the previous session's tasks or rules.
	const seededSessionRef = useRef<string | null>(null);
	useEffect(() => {
		if (!view) {
			seededSessionRef.current = null;
			setTasksContext(null);
			setPermissionContext(null);
			return;
		}
		if (seededSessionRef.current === view.session.id) return;
		seededSessionRef.current = view.session.id;
		const state = view.session.state as Record<string, unknown> | undefined;
		setTasksContext((state?.tasks_context as TaskContext) ?? null);
		setPermissionContext((state?.permission_context as PermissionContext) ?? null);
	}, [view]);

	// Sync selectedModel + selectedFallbackModel from the session
	// record. If the session has no model configured yet, auto-pick
	// the first available one and persist it back so subsequent
	// reasoning has a model to call.
	//
	// Important: skip while `view` is still loading. Otherwise the
	// in-flight window between "agentId changed" and "useSessions
	// returned the new list" looks like "session has no model" and
	// we would racily auto-select + persist the first available
	// model, clobbering whatever the user had configured.
	useEffect(() => {
		if (!view) return;
		const sessionModel = view.session.config.chat_model_config;

		if (sessionModel) {
			setSelectedModel(sessionModel);
		} else {
			const firstModel = getFirstAvailableModel();
			if (firstModel) {
				setSelectedModel(firstModel);
				if (sessionId && agentId) {
					// `silent` because the user did not ask for this write —
					// surfacing a toast for a revoked credential or a network
					// blip they never triggered is pure noise.
					sessionApi
						.update(
							sessionId,
							agentId,
							{ chat_model_config: firstModel },
							{ silent: true },
						)
						.then(() => refetchSessions())
						.catch(() => {});
				}
			} else {
				setSelectedModel(null);
			}
		}

		setSelectedFallbackModel(view.session.config.fallback_chat_model_config ?? null);
		setSelectedTTSModel(view.session.config.tts_model_config ?? null);
		setSelectedASRModel(view.session.config.asr_model_config ?? null);
		setSelectedRealtimeModel(view.session.config.realtime_model_config ?? null);
		setSelectedKnowledgeConfig(view.session.config.knowledge_config ?? null);
	}, [view, groups, sessionId, agentId]);

	// Sync selectedPermissionMode when the session changes. Same
	// loading-window guard as above — don't reset the displayed mode
	// to "default" while the new session view is still on the wire.
	useEffect(() => {
		if (!view) return;
		const mode = (view.session.state?.permission_context as Record<string, unknown>)
			?.mode as string;
		setSelectedPermissionMode(mode ?? 'default');
	}, [sessionId, view]);

	/**
	 * Persist a model change to the session and refetch so the local
	 * view picks up the new value.
	 *
	 * @param config - New chat model config; `null` is ignored
	 *   because the primary selector does not allow clearing.
	 */
	const handleLlmChange = async (config: ChatModelConfig | null) => {
		if (!config) return;
		await patchConfig({ chat_model_config: config }, () => setSelectedModel(config));
	};

	/**
	 * Persist a parameter change on the currently selected model.
	 *
	 * @param parameters - New parameter map (model-provider specific).
	 */
	const handleParametersChange = async (parameters: Record<string, unknown>) => {
		if (!selectedModel) return;
		const updated = { ...selectedModel, parameters };
		await patchConfig({ chat_model_config: updated }, () => setSelectedModel(updated));
	};

	/**
	 * Persist a fallback-model change. `null` clears the fallback.
	 *
	 * @param config - New fallback config or `null` to clear.
	 */
	const handleFallbackChange = async (config: ChatModelConfig | null) => {
		await patchConfig({ fallback_chat_model_config: config }, () =>
			setSelectedFallbackModel(config),
		);
	};

	/**
	 * Persist a TTS model change. `null` disables TTS.
	 *
	 * @param config - New TTS config or `null` to disable.
	 */
	const handleTTSChange = async (config: TTSModelConfig | null) => {
		await patchConfig({ tts_model_config: config }, () => setSelectedTTSModel(config));
	};

	/** Persist an ASR model change. `null` disables speech input. */
	const handleASRChange = async (config: ASRModelConfig | null) => {
		await patchConfig({ asr_model_config: config }, () => setSelectedASRModel(config));
	};

	/** Persist the model used by browser realtime voice mode. */
	const handleRealtimeModelChange = async (config: RealtimeModelConfig | null) => {
		await patchConfig({ realtime_model_config: config }, () =>
			setSelectedRealtimeModel(config),
		);
	};

	/** Persist parameters for the browser realtime voice model. */
	const handleRealtimeParametersChange = async (parameters: Record<string, unknown>) => {
		if (!selectedRealtimeModel) return;
		const updated = { ...selectedRealtimeModel, parameters };
		await patchConfig({ realtime_model_config: updated }, () =>
			setSelectedRealtimeModel(updated),
		);
	};

	/** Transcribe one browser recording with the configured batch ASR model. */
	const handleTranscribe = useCallback(
		async (file: File) => {
			if (!selectedASRModel || selectedASRModelCard?.realtime) return '';
			const response = await asrApi.transcribe({
				file,
				credentialId: selectedASRModel.credential_id,
				model: selectedASRModel.model,
				parameters: selectedASRModel.parameters,
			});
			return response.text;
		},
		[selectedASRModel, selectedASRModelCard],
	);

	/**
	 * Persist a permission-mode change.
	 *
	 * @param mode - New permission mode (e.g. `default`, `explore`).
	 */
	/**
	 * Persist a new working directory.
	 *
	 * Nothing local mirrors it — the value is read straight off the
	 * session view, which `patchConfig` refetches on success.
	 *
	 * @param next - Directory relative to the workspace root, or `null`
	 *   for the root itself.
	 */
	const handleCwdChange = async (next: string | null) => {
		// Bypasses `patchConfig`: the dialog shows the failure inline and
		// stays open on it, so the toast would be a duplicate and the
		// swallowed rejection would let the dialog close as if it worked.
		if (!sessionId || !agentId) return;
		setConfigPending(true);
		try {
			await sessionApi.update(sessionId, agentId, { cwd: next }, { silent: true });
			await refetchSessions();
		} finally {
			setConfigPending(false);
		}
	};

	const handlePermissionModeChange = async (mode: string) => {
		await patchConfig({ permission_mode: mode as PermissionMode }, () =>
			setSelectedPermissionMode(mode),
		);
	};

	return (
		<>
			<main ref={viewportRef} className="flex size-full min-w-0">
				<ResizablePanelGroup orientation="horizontal">
					<ResizablePanel
						className="flex flex-1 rounded-[22px] bg-card shadow-panel"
						minSize={panelsInSheet ? '100%' : '24rem'}
					>
						<div className="flex flex-col flex-1 min-h-0 min-w-0 overflow-x-hidden p-2">
							<div className="flex flex-row gap-x-2 justify-between">
								<div
									className={
										panelsInSheet
											? 'flex min-w-0 max-w-24 items-center gap-x-1'
											: 'flex min-w-0 flex-row items-center gap-x-1'
									}
								>
									<SidebarTrigger className="md:hidden" />
									{/* The open session, named opposite its own
									    settings. The sidebar is the only other
									    place the name appears, and it collapses
									    on mobile — so on a narrow screen this is
									    the only thing saying which conversation
									    is on screen.

									    Withheld until the session has something
									    in it: an untouched one is still named
									    after the timestamp it was created at,
									    and a date is worse than no title at all.
									    The first reply replaces that with a real
									    one. */}
									{msgs.length > 0 && (
										<span
											className="truncate px-2 text-sm text-muted-foreground"
											title={view?.session.config.name}
										>
											{view?.session.config.name}
										</span>
									)}
								</div>
								{/* Keep the panel menu reachable when chat space is narrow. */}
								<div
									className={
										panelsInSheet
											? 'flex min-w-0 flex-1 flex-wrap justify-end gap-1'
											: 'flex shrink-0 flex-row gap-x-1'
									}
								>
									<RealtimeModelSelect
										value={selectedRealtimeModel}
										onChange={handleRealtimeModelChange}
										onAddCredential={() => setCredentialOpen(true)}
										disabled={configPending || voiceBusy}
										className="text-muted-foreground hover:text-foreground"
									/>
									<ModelParametersPopover
										selectedModel={selectedRealtimeModel}
										modelCard={selectedRealtimeModelCard}
										onChange={handleRealtimeParametersChange}
										idPrefix="realtime"
										triggerLabel={t('realtime.parameters')}
										disabled={configPending || voiceBusy}
										hideArrayParameters
										excludedEnumValues={REALTIME_EXCLUDED_ENUM_VALUES}
									/>
									<LlmSelect
										id="tour-llm-select"
										variant="ghost"
										className="max-w-full font-mono text-muted-foreground hover:text-foreground"
										value={selectedModel}
										onChange={handleLlmChange}
										onAddCredential={() => setCredentialOpen(true)}
										disabled={configPending || voiceBusy}
									/>
									<ModelParametersPopover
										selectedModel={selectedModel}
										modelCard={selectedModelCard}
										onChange={handleParametersChange}
										selectedFallbackModel={selectedFallbackModel}
										onFallbackChange={handleFallbackChange}
										selectedTTSModel={selectedTTSModel}
										onTTSChange={handleTTSChange}
										selectedASRModel={selectedASRModel}
										onASRChange={handleASRChange}
										disabled={configPending || voiceBusy}
									/>
									<PermissionModeSelect
										id="tour-permission-mode"
										variant={'ghost'}
										className="font-mono text-muted-foreground hover:text-foreground"
										value={selectedPermissionMode}
										disabled={!sessionId || configPending}
										onChange={handlePermissionModeChange}
									/>
									<DropdownMenu>
										<DropdownMenuTrigger asChild>
											<Button
												variant="ghost"
												size="sm"
												className="gap-1 px-2"
											>
												<PanelRight />
												<ChevronDown className="size-3 text-muted-foreground" />
											</Button>
										</DropdownMenuTrigger>
										<DropdownMenuContent align="end" className="w-auto">
											{(
												[
													['background', t('panel.background.title')],
													['plan', t('panel.plan.title')],
													['mcp', 'MCP'],
													['skill', t('panel.skill.title')],
													['permission', t('panel.permission.title')],
													['knowledge', t('panel.knowledge.title')],
													['team', t('common.team')],
												] satisfies [PanelKey, string][]
											).map(([key, title]) =>
												panelsInSheet ? (
													<DropdownMenuItem
														key={key}
														onSelect={() => {
															setPanelLayout((layout) =>
																openPanelInLayout(layout, key),
															);
															setSelectedSheetPanel(key);
															setSheetOpen(true);
														}}
													>
														{panels[key].icon}
														{title}
													</DropdownMenuItem>
												) : (
													<DropdownMenuCheckboxItem
														key={key}
														checked={isPanelOpen(key)}
														onCheckedChange={() => togglePanel(key)}
														onSelect={(e) => e.preventDefault()}
													>
														{panels[key].icon}
														{title}
													</DropdownMenuCheckboxItem>
												),
											)}
										</DropdownMenuContent>
									</DropdownMenu>
								</div>
							</div>
							{realtimeUsesSessionChat && (
								<div
									className={
										'flex min-h-7 items-center justify-end gap-1.5 px-2 ' +
										'text-xs text-muted-foreground'
									}
								>
									<BrainCircuit className="size-3.5 shrink-0" />
									<span className="max-w-full truncate">
										{selectedModel
											? t('realtime.backendFollows', {
													model: selectedModel.model,
												})
											: t('realtime.backendMissing')}
									</span>
								</div>
							)}
							<div className="flex flex-1 justify-center min-h-0 overflow-hidden relative [--chat-content-w:48rem]">
								<ChatContent
									className={'max-w-[var(--chat-content-w)] w-full'}
									msgs={msgs}
									loading={messagesLoading}
									agentId={agentId}
									sessionId={sessionId}
									cwd={view?.session.config.cwd ?? null}
									onCwdChange={handleCwdChange}
									git={workspaceStatus?.git ?? null}
									onRefreshGit={refetchWorkspaceStatus}
									phase={phase}
									activeReplyId={activeReplyId}
									queuedCount={queuedCount}
									queuedItems={queuedItems}
									queueReorderDisabled={queueReorderDisabled}
									disabled={selectedModel === null || voiceBusy}
									onSend={send}
									onUserConfirm={onUserConfirm}
									onClientExternalToolResult={onClientExternalToolResult}
									onExternalExecutionResult={onExternalExecutionResult}
									onInterrupt={interrupt}
									onUpdateQueued={updateQueued}
									onDeleteQueued={deleteQueued}
									onSteerQueued={steerQueued}
									onMoveQueued={moveQueued}
									onReorderQueued={reorderQueued}
									voiceState={realtimeVoice.state}
									onVoiceToggle={() => {
										if (
											!voiceBusy &&
											realtimeUsesSessionChat &&
											getPendingHitlCalls(msgs).length > 0
										) {
											toast.error(t('realtime.pendingChatInteraction'));
											return;
										}
										void realtimeVoice.toggle();
									}}
									voiceDisabled={
										!selectedRealtimeModel ||
										!agentId ||
										!sessionId ||
										(realtimeUsesSessionChat && !selectedModel)
									}
									asrInputTypes={selectedASRModelCard?.input_types ?? []}
									onTranscribe={handleTranscribe}
									asrRealtime={!!selectedASRModelCard?.realtime}
									realtimeASRConfig={
										selectedASRModel && selectedASRModelCard?.realtime
											? {
													credentialId: selectedASRModel.credential_id,
													model: selectedASRModel.model,
													parameters: selectedASRModel.parameters,
												}
											: null
									}
									asrDisabled={
										asrModelsLoading ||
										!selectedASRModel ||
										!selectedASRModelCard ||
										!agentId ||
										!sessionId ||
										voiceBusy
									}
									asrResetKey={`${agentId ?? ''}:${sessionId ?? ''}:${selectedASRModel?.credential_id ?? ''}:${selectedASRModel?.model ?? ''}`}
									footerSlot={
										subagentHitl.length > 0 ? (
											<SubagentHitlCard
												key={`${subagentHitl[0].worker_session_id}:${subagentHitl[0].reply_id}`}
												entry={subagentHitl[0]}
												onConfirm={(toolCall, confirm, rules) =>
													onSubagentConfirm(
														subagentHitl[0],
														toolCall,
														confirm,
														rules,
													)
												}
											/>
										) : null
									}
									allowedInputTypes={(
										selectedModelCard?.input_types ?? []
									).filter(
										(t) =>
											/^(image|video|audio|text)\/.+/.test(t) ||
											t === 'application/pdf' ||
											t.startsWith('application/vnd.') ||
											t.startsWith('application/msword') ||
											t.startsWith('application/vnd.openxmlformats'),
									)}
									fileProcessor={async (file) => {
										const filePath = (file as File & { path?: string }).path;
										if (filePath) {
											return {
												id: crypto.randomUUID(),
												type: 'data' as const,
												source: {
													type: 'url' as const,
													url: `file://${filePath}`,
													media_type:
														file.type || 'application/octet-stream',
												},
												name: file.name,
												created_at: new Date().toISOString(),
											};
										}
										if (file.type === 'text/plain') {
											const text = await file.text();
											return {
												id: crypto.randomUUID(),
												type: 'text' as const,
												text: `[File: ${file.name}]\n${text}`,
												created_at: new Date().toISOString(),
											};
										}
										const buffer = await file.arrayBuffer();
										const bytes = new Uint8Array(buffer);
										let binary = '';
										for (let i = 0; i < bytes.byteLength; i++) {
											binary += String.fromCharCode(bytes[i]);
										}
										const base64 = btoa(binary);
										return {
											id: crypto.randomUUID(),
											type: 'data' as const,
											source: {
												type: 'base64' as const,
												media_type: file.type || 'application/octet-stream',
												data: base64,
											},
											name: file.name,
											created_at: new Date().toISOString(),
										};
									}}
								/>
							</div>
						</div>
					</ResizablePanel>
					{dockLayout.length > 0 && (
						<ResizableHandle withHandle className="bg-transparent w-1.5" />
					)}
					<PanelDock layout={dockLayout} panels={panels} onClosePanel={closePanel} />
				</ResizablePanelGroup>
			</main>
			{panelsInSheet && activeSheetPanel && (
				<Sheet open={sheetOpen} onOpenChange={setSheetOpen}>
					<SheetContent
						className="gap-0 data-[side=right]:w-full sm:max-w-md"
						aria-describedby={undefined}
						showCloseButton={false}
					>
						<SheetHeader className="flex-row items-center justify-between gap-2">
							<SheetTitle className="min-w-0 text-sm">
								{panels[activeSheetPanel].title}
							</SheetTitle>
							<div className="flex shrink-0 items-center gap-1">
								{panels[activeSheetPanel].actions}
								<SheetClose asChild>
									<Button
										variant="ghost"
										size="icon-sm"
										aria-label={t('common.close')}
									>
										<X />
									</Button>
								</SheetClose>
							</div>
						</SheetHeader>
						<Tabs
							value={activeSheetPanel}
							onValueChange={(key) => setSelectedSheetPanel(key as PanelKey)}
							className="min-h-0 flex-1 gap-3 px-4 pb-4"
						>
							<div className="shrink-0 overflow-x-auto">
								<TabsList>
									{sheetPanelKeys.map((key) => (
										<TabsTrigger key={key} value={key}>
											{panels[key].icon}
											{panels[key].title}
										</TabsTrigger>
									))}
								</TabsList>
							</div>
							{sheetPanelKeys.map((key) => (
								<TabsContent
									key={key}
									value={key}
									className="min-h-0 overflow-y-auto"
								>
									{panels[key].content}
								</TabsContent>
							))}
						</Tabs>
					</SheetContent>
				</Sheet>
			)}
			<CreateCredentialDialog open={credentialOpen} onOpenChange={setCredentialOpen} />
		</>
	);
}