Files
Netcatty/components/AIChatSidePanel.tsx
T

2504 lines
97 KiB
TypeScript

import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Loader2 } from 'lucide-react';
import { useI18n } from '../application/i18n/I18nProvider';
import { useWindowControls } from '../application/state/useWindowControls';
import type {
AIDraft,
AIPanelView,
AgentModelPreset,
AISessionScope,
UploadedFile,
DiscoveredAgent,
ExternalAgentConfig,
} from '../infrastructure/ai/types';
import type { VaultNote } from '../domain/models';
import type { ExecutorContext } from '../infrastructure/ai/cattyAgent/executor';
import {
filterAgentModelPresetsForCliVersion,
getAgentModelPresets,
resolveAgentCliVersion,
resolveAgentModelSelection,
} from '../infrastructure/ai/types';
import { getExternalAgentSdkBackend, getManualAgentCommand, matchesManagedAgentConfig } from '../infrastructure/ai/managedAgents';
import {
appendComposerCustomModelPresets,
resolveComposerCustomModelIds,
} from '../infrastructure/ai/composerPicker';
import {
readComposerModelPrefs,
subscribeComposerModelPrefs,
} from '../infrastructure/ai/composerModelPrefs';
import { toast } from './ui/toast';
import { useAgentDiscovery } from '../application/state/useAgentDiscovery';
import {
getReadyUserSkillOptions,
getNextSelectedUserSkillSlugsMap,
type UserSkillOption,
} from './ai/userSkillsState';
import { subscribeUserSkillsStatusChanged } from './ai/userSkillsStatusEvents';
import {
applyDraftEntrySelection,
applyHistorySessionSelection,
resolveDisplayedPanelView,
resolveDisplayedSession,
shouldForceDraftViewSync,
} from './ai/aiPanelViewState';
import {
endSendForKey,
tryBeginSendForKey,
} from './ai/draftSendGate';
import { draftsByScopeEqualIgnoringComposerText, selectDraftForAgentSwitch } from '../application/state/aiDraftState';
import { latestAIActiveSessionMapSnapshot, latestAISessionsSnapshot } from '../application/state/aiStateSnapshots';
import { sanitizeContextWindow } from '../infrastructure/ai/contextCompaction';
import {
buildPromptWithTerminalSelectionAttachments,
isInlineTextAttachment,
} from '../application/state/terminalSelectionAttachment';
import { createVaultNoteAttachment, isVaultNoteAttachment, vaultNoteReferencesFit } from '../application/state/vaultNoteAttachment';
import { useCodexConfigModel } from '../application/state/useCodexConfigModel';
import {
useAIChatStreaming,
getNetcattyBridge,
isAIChatSessionStreaming,
type DefaultTargetSessionHint,
} from '../application/state/useAIChatStreaming';
import { getScopedHistorySessions } from './ai/scopedHistorySessions';
import { buildAiChatsListingEntries, canRebindChat, formatAiChatsListingNote, resolveRebindTargetFromArg, type AiChatsListingEntry } from './ai/aiChatsListing';
import { resolveInheritedAIActiveSessionId } from '../domain/aiWorkspaceScopeInherit';
import { resolveUndoLastTurnBoundary } from '../domain/aiUndoLastTurn';
import { aiSessionIdSetEqual, exactScopeAISessionsEqual } from '../domain/aiSessionsForScope';
import { buildExternalAgentHistoryMessagesForBridge } from './ai/externalAgentHistory';
import { canSendWithAgent, findEnabledExternalAgent } from './ai/agentSendEligibility';
import { registerGrantPersister } from '../infrastructure/ai/shared/approvalGate';
import { stopAgentTurn } from '../infrastructure/ai/harness/agentStop';
import { getAgentRuntime } from '../infrastructure/ai/harness/globalAgentRuntime';
import { useAIPermissionGrantsState } from '../application/state/useAIPermissionGrantsState';
import { useConversationExport } from './ai/hooks/useConversationExport';
import { useAgentContextUsage } from '../application/state/useAgentCompactionUi';
import type { AIChatSidePanelProps } from './AIChatSidePanel.types';
import {
buildCursorListModelsAgentEnv,
buildSdkRuntimeModelCacheKey,
sdkRuntimeModelCache,
reconcileRuntimeModelWarning,
retainStoredModelPresetForCatalog,
toSdkRuntimeModelCatalog,
generateId,
agentModelPresetsShallowEqual,
mergeFallbackThinkingLevels,
normalizeStoredAgentModelSelection,
normalizeSdkRuntimeModelPresets,
resolveSdkRuntimeCatalogDefaultModelId,
shouldAdoptSdkCurrentModel,
appendRetainedStoredModelPreset,
shouldLoadSdkRuntimeModels,
shouldUseStoredAgentModel,
type SdkRuntimeModelCatalog,
type SdkRuntimeModelCatalogSource,
} from './AIChatSidePanelHelpers';
import { AIChatPanelContent } from './AIChatPanelContent';
import { TERMINAL_SIDE_PANEL_INNER_HEADER_CLASS } from './terminalLayer/terminalSidePanelChrome';
import {
getAIPanelProfilerProps,
profileAIPanelCalculation,
} from './ai/aiPanelDiagnostics';
import { scheduleWhenAiComposerIdle, warmAiMarkdownRenderer } from './ai/aiMarkdownWarmup';
type UserSkillsStatusResult = { ok: boolean; skills?: Array<{
id: string;
slug: string;
name: string;
description: string;
status: 'ready' | 'warning';
}> } | null;
type UserSkillsStatusLoadResult = UserSkillsStatusResult | undefined;
type SdkRuntimeModelTarget = {
agentId: string;
cacheKey: string;
sdkBackend: string;
agentEnv?: Record<string, string>;
agentCommand?: string;
codexRuntime?: 'sdk' | 'app-server';
};
type SteerWarning = {
reason: 'not-steerable' | 'busy' | 'inactive' | 'unsupported' | 'cancelled' | 'failed';
turnKind?: 'review' | 'compact';
};
const USER_SKILLS_STATUS_CACHE_TTL_MS = 60_000;
let userSkillsStatusCache: {
version: number;
result: UserSkillsStatusResult;
updatedAt: number;
} | null = null;
let userSkillsStatusPromise: {
version: number;
promise: Promise<UserSkillsStatusLoadResult>;
} | null = null;
let userSkillsStatusCacheVersion = 0;
function invalidateUserSkillsStatusCache() {
userSkillsStatusCacheVersion += 1;
userSkillsStatusCache = null;
userSkillsStatusPromise = null;
}
if (typeof window !== 'undefined') {
subscribeUserSkillsStatusChanged(invalidateUserSkillsStatusCache);
}
function loadUserSkillsStatus(
bridge: ReturnType<typeof getNetcattyBridge>,
): Promise<UserSkillsStatusLoadResult> {
const requestVersion = userSkillsStatusCacheVersion;
if (!bridge?.aiUserSkillsGetStatus) {
userSkillsStatusCache = { version: requestVersion, result: null, updatedAt: Date.now() };
return Promise.resolve(null);
}
if (
userSkillsStatusCache
&& userSkillsStatusCache.version === requestVersion
&& Date.now() - userSkillsStatusCache.updatedAt < USER_SKILLS_STATUS_CACHE_TTL_MS
) {
return Promise.resolve(userSkillsStatusCache.result);
}
if (!userSkillsStatusPromise || userSkillsStatusPromise.version !== requestVersion) {
const promise = bridge.aiUserSkillsGetStatus()
.then((result) => {
if (userSkillsStatusCacheVersion !== requestVersion) return undefined;
userSkillsStatusCache = { version: requestVersion, result, updatedAt: Date.now() };
return result;
})
.catch(() => {
if (userSkillsStatusCacheVersion !== requestVersion) return undefined;
userSkillsStatusCache = { version: requestVersion, result: null, updatedAt: Date.now() };
return null;
})
.finally(() => {
if (userSkillsStatusPromise?.version === requestVersion) {
userSkillsStatusPromise = null;
}
});
userSkillsStatusPromise = { version: requestVersion, promise };
}
return userSkillsStatusPromise.promise;
}
export function hasAIChatSidePanelRetainedContent(props: Pick<
AIChatSidePanelProps,
'activeSessionIdMap' | 'draftsByScope' | 'sessions' | 'scopeTargetId' | 'scopeType'
>): boolean {
const scopeKey = `${props.scopeType}:${props.scopeTargetId ?? ''}`;
const sessionId = props.activeSessionIdMap[scopeKey] ?? null;
const activeSession = sessionId
? props.sessions.find((session) => session.id === sessionId)
: null;
if (activeSession && activeSession.messages.length > 0) {
return true;
}
const draft = props.draftsByScope[scopeKey] ?? null;
return Boolean(
draft
&& (
draft.text.trim().length > 0
|| draft.attachments.length > 0
|| draft.selectedUserSkillSlugs.length > 0
),
);
}
export function shouldKeepAIChatSidePanelMounted(props: AIChatSidePanelProps): boolean {
if (props.isVisible ?? true) {
return true;
}
const scopeKey = `${props.scopeType}:${props.scopeTargetId ?? ''}`;
const sessionId = props.activeSessionIdMap[scopeKey] ?? null;
if (hasAIChatSidePanelRetainedContent(props)) {
return true;
}
return isAIChatSessionStreaming(sessionId);
}
function shouldDelayAIChatSidePanelActivation(_props: AIChatSidePanelProps): boolean {
// Empty-panel activation delay used to land in the expand → first-type window.
return false;
}
function schedulePanelActivation(callback: () => void): () => void {
let timeoutId: number | null = null;
if (typeof requestAnimationFrame === 'function') {
const rafId = requestAnimationFrame(() => {
timeoutId = window.setTimeout(callback, 0);
});
return () => {
cancelAnimationFrame(rafId);
if (timeoutId !== null) window.clearTimeout(timeoutId);
};
}
timeoutId = window.setTimeout(callback, 0);
return () => {
if (timeoutId !== null) window.clearTimeout(timeoutId);
};
}
const AIChatSidePanelPreparing = React.memo(function AIChatSidePanelPreparing() {
const { t } = useI18n();
return (
<div className="flex h-full flex-col bg-background" data-section="ai-chat-panel-preparing">
<div className={`${TERMINAL_SIDE_PANEL_INNER_HEADER_CLASS} border-b border-border/50 px-2 flex items-center`}>
<div className="h-6 w-32 rounded-md bg-muted/45" />
</div>
<div className="flex flex-1 items-center justify-center text-xs text-muted-foreground">
<div className="flex items-center gap-2">
<Loader2 size={14} className="animate-spin" />
{t('ai.chat.preparing')}
</div>
</div>
</div>
);
});
const AIChatSidePanelActive: React.FC<AIChatSidePanelProps> = ({
sessions,
activeSessionIdMap,
draftsByScope,
panelViewByScope,
setActiveSessionId: setActiveSessionIdForScope,
ensureDraftForScope,
updateDraft,
showDraftView,
showSessionView,
clearDraftForScope,
addDraftFiles,
removeDraftFile,
createSession,
undoLastTurnInSession,
deleteSession,
updateSessionTitle,
rebindSessionScope,
exportSessionsBackup,
importSessionsBackup,
updateSessionExternalSessionId,
addMessageToSession,
updateLastMessage,
updateMessageById,
persistContextCompaction,
forkSessionFromMessage,
providers,
activeProviderId,
activeModelId,
defaultAgentId,
toolIntegrationMode,
externalAgents,
setExternalAgents,
agentModelMap,
setAgentModel,
agentProviderMap,
setAgentProvider,
agentThinkingMap,
setAgentThinking,
updateProvider,
globalPermissionMode,
setGlobalPermissionMode,
commandBlocklist,
commandTimeout,
responseIdleTimeout,
maxIterations = 20,
webSearchConfig,
quickMessages = [],
scopeType,
scopeTargetId,
scopeHostIds,
scopeLabel,
focusedSessionId,
onSetWorkspaceAnchorChat,
workspaces = [],
terminalSessions = [],
liveScopeTargetIds,
resolveExecutorContext,
isVisible = true,
notes = [],
hosts = [],
snippets = [],
onOpenVaultNote,
onOpenVaultHost,
onOpenVaultSnippet,
onOpenVaultSection,
}) => {
const { t } = useI18n();
const scopeKey = `${scopeType}:${scopeTargetId ?? ''}`;
const [showHistory, setShowHistory] = useState(false);
// Inline system-style note above the composer (e.g. `/chats` listing).
// Panel-command output, never agent content.
const [panelCommandNote, setPanelCommandNote] = useState<string | null>(null);
// Entries of the last `/chats` listing; `/rebind <n>` resolves against them.
const lastChatsListingRef = useRef<AiChatsListingEntry[]>([]);
const [isSending, setIsSending] = useState(false);
// Undo-last-turn is still
// running. Unlike the send flow this is not reflected in `isSending`, so it
// is tracked separately to block sends until the branch settles: a turn
// submitted meanwhile would run against the (soon hidden) source session.
const [isUndoingLastTurn, setIsUndoingLastTurn] = useState(false);
const [runtimeAgentModelPresets, setRuntimeAgentModelPresets] = useState<Record<string, { cacheKey: string; models: AgentModelPreset[]; catalogUnavailable: boolean }>>({});
const [runtimeModelWarnings, setRuntimeModelWarnings] = useState<Record<string, { cacheKey: string; message: string }>>({});
// Tracks SDK runtime model catalogs currently loading (per agent scope);
// the composer shows the loading row alongside the build-time presets and
// the live catalog replaces them when it arrives.
const [runtimeModelLoading, setRuntimeModelLoading] = useState<Record<string, { cacheKey: string }>>({});
// Provenance of the last applied SDK runtime catalog, per agent scope. The
// picker renders a uniform status chip from it; when no runtime catalog is
// active the composer falls back to build-time presets ('presets').
const [runtimeModelCatalogMeta, setRuntimeModelCatalogMeta] = useState<Record<string, {
cacheKey: string;
source: SdkRuntimeModelCatalogSource;
fetchedAt?: string;
}>>({});
const [steerWarnings, setSteerWarnings] = useState<Record<string, SteerWarning>>({});
const [steeringSessionId, setSteeringSessionId] = useState<string | null>(null);
const [userSkillOptions, setUserSkillOptions] = useState<UserSkillOption[]>([]);
const [userSkillsStatusVersion, setUserSkillsStatusVersion] = useState(0);
const { openSettingsWindow } = useWindowControls();
const terminalSessionsRef = useRef(terminalSessions);
terminalSessionsRef.current = terminalSessions;
const resolveExecutorContextRef = useRef(resolveExecutorContext);
resolveExecutorContextRef.current = resolveExecutorContext;
const {
streamingSessionIds,
setStreamingForScope,
abortControllersRef,
sendToCattyAgent,
sendToExternalAgent,
steerExternalAgent,
reportStreamError,
activeCompaction,
} = useAIChatStreaming({
maxIterations,
addMessageToSession,
updateLastMessage,
updateMessageById,
persistContextCompaction,
});
const setActiveSessionId = useCallback((id: string | null) => {
setActiveSessionIdForScope(scopeKey, id);
}, [scopeKey, setActiveSessionIdForScope]);
const activeTerminalSessionIds = useMemo(() => {
const sessionIds = new Set<string>();
const entries = Object.entries(activeSessionIdMap) as Array<[string, string | null]>;
for (const [sessionScopeKey, sessionId] of entries) {
if (!sessionScopeKey.startsWith('terminal:') || !sessionId) continue;
if (sessionScopeKey === scopeKey) continue;
sessionIds.add(sessionId);
}
return sessionIds;
}, [activeSessionIdMap, scopeKey]);
const workspaceMemberTerminalIds = useMemo(() => {
if (scopeType !== 'workspace') return undefined;
return new Set(
terminalSessions
.map((session) => session.sessionId)
.filter((sessionId): sessionId is string => Boolean(sessionId)),
);
}, [scopeType, terminalSessions]);
// Use live sessions for history + view resolution. Deferring the list used to
// lag one paint behind createSession, so normalizePanelView treated the new
// chat as missing and the draft-sync effect forced showDraftView — which
// parked the just-sent session into history under StrictMode.
const historySessions = useMemo(
() => profileAIPanelCalculation(
'AIChatSidePanel.historySessions',
() => getScopedHistorySessions(
sessions,
scopeType,
scopeTargetId,
scopeHostIds,
activeTerminalSessionIds,
workspaceMemberTerminalIds,
activeSessionIdMap,
),
),
[sessions, scopeType, scopeTargetId, scopeHostIds, activeTerminalSessionIds, workspaceMemberTerminalIds, activeSessionIdMap],
);
const explicitPanelView = panelViewByScope[scopeKey];
const currentDraft = draftsByScope[scopeKey] ?? null;
const pendingComposerTextRef = useRef<string | null>(null);
const visibleHistorySessionIds = useMemo(
() => new Set(historySessions.map((session) => session.id)),
[historySessions],
);
const persistedSessionId = resolveInheritedAIActiveSessionId({
scopeType,
scopeTargetId,
activeSessionIdMap,
memberTerminalIds: terminalSessions.map((session) => session.sessionId).filter(Boolean),
preferredTerminalId: focusedSessionId,
visibleSessionIds: visibleHistorySessionIds,
});
// Issue #4098 phase 2: this workspace's anchored chat — used by the panel
// view resolver before any positional fallback could apply.
const workspaceAnchorChatId = scopeType === 'workspace'
? workspaces.find((workspace) => workspace.id === scopeTargetId)?.anchorChatId
: undefined;
// getScopedHistorySessions orders by scope match first, recency second.
const preferredHistorySessionId = historySessions[0]?.id ?? null;
const normalizedPanelView = useMemo<AIPanelView>(
() => resolveDisplayedPanelView(explicitPanelView, currentDraft != null, historySessions, persistedSessionId, scopeType, workspaceAnchorChatId, preferredHistorySessionId),
[explicitPanelView, currentDraft, historySessions, persistedSessionId, scopeType, workspaceAnchorChatId, preferredHistorySessionId],
);
const activeSession = useMemo(
() => resolveDisplayedSession(normalizedPanelView, historySessions),
[normalizedPanelView, historySessions],
);
const activeSessionId = normalizedPanelView.mode === 'session' ? normalizedPanelView.sessionId : null;
const isStreaming = activeSessionId ? streamingSessionIds.has(activeSessionId) : false;
const isSteering = activeSessionId != null && steeringSessionId === activeSessionId;
const currentAgentId = activeSession?.agentId ?? currentDraft?.agentId ?? defaultAgentId;
const observedContextUsage = useAgentContextUsage(activeSessionId);
const inputValue = pendingComposerTextRef.current ?? currentDraft?.text ?? '';
const files = currentDraft?.attachments ?? [];
const panelViewRef = useRef(normalizedPanelView);
panelViewRef.current = normalizedPanelView;
const permissionModeRef = useRef(globalPermissionMode);
permissionModeRef.current = globalPermissionMode;
const sendEpochRef = useRef(0);
useEffect(() => () => {
sendEpochRef.current += 1;
}, [scopeKey]);
const currentDraftRef = useRef(currentDraft);
if (pendingComposerTextRef.current != null) {
const pending = pendingComposerTextRef.current;
if (currentDraft && currentDraft.text === pending) {
pendingComposerTextRef.current = null;
currentDraftRef.current = currentDraft;
} else if (currentDraftRef.current?.text === pending) {
if (currentDraft && currentDraftRef.current) {
currentDraftRef.current.attachments = currentDraft.attachments;
currentDraftRef.current.selectedUserSkillSlugs = currentDraft.selectedUserSkillSlugs;
currentDraftRef.current.agentId = currentDraft.agentId;
}
} else {
const base = currentDraft ?? currentDraftRef.current ?? {
text: '',
agentId: currentAgentId,
attachments: [],
selectedUserSkillSlugs: [],
updatedAt: Date.now(),
};
currentDraftRef.current = { ...base, text: pending };
}
} else {
currentDraftRef.current = currentDraft;
}
const activeSessionRef = useRef(activeSession);
activeSessionRef.current = activeSession;
/** Rename a chat (slash command + history drawer affordance). Panel action: nothing is sent to the agent. */
const handleRenameSession = useCallback((sessionId: string, rawTitle: string) => {
const title = rawTitle.trim();
if (!title) {
toast.warning(t('ai.chat.renameUsage'));
return;
}
if (!sessionId) {
toast.warning(t('ai.chat.renameNoActive'));
return;
}
updateSessionTitle(sessionId, title);
toast.success(t('ai.chat.renameDone').replace('{title}', title));
}, [t, updateSessionTitle]);
const handleRenameSlashCommand = useCallback((title: string) => {
handleRenameSession(activeSessionRef.current?.id ?? null, title);
}, [handleRenameSession]);
const handleListChats = useCallback(() => {
// Prefer the app-wide live target set so chats bound to other still-open
// terminals are not mislabelled dangling / offered for /rebind.
const liveTerminalIds = liveScopeTargetIds
?? (scopeType === 'workspace' && workspaceMemberTerminalIds
? workspaceMemberTerminalIds
: new Set(scopeTargetId ? [scopeTargetId] : []));
// Dangling chats no longer match any live scope (rank 0), so they must be
// appended explicitly for the listing — that is where they become visible
// and rebindable again (issue #4015 R2).
const listedSessionIds = new Set(historySessions.map((session) => session.id));
const danglingSessions = sessions.filter((session) => session.scopeDangling && !listedSessionIds.has(session.id));
const entries = buildAiChatsListingEntries({
sessions: [...historySessions, ...danglingSessions],
activeSessionId,
externalAgents,
workspaces,
liveTerminalIds,
currentScopeLabel: scopeLabel,
scopeTargetId,
t,
});
lastChatsListingRef.current = entries;
setPanelCommandNote(formatAiChatsListingNote(entries, t));
}, [activeSessionId, externalAgents, historySessions, liveScopeTargetIds, scopeLabel, scopeTargetId, scopeType, sessions, t, workspaces, workspaceMemberTerminalIds]);
// `/rebind <n>`: rebind the nth dangling chat from the last /chats listing
// to this panel's current scope. Panel action — no agent round-trip.
const handleRebindChat = useCallback((arg: string) => {
if (!scopeTargetId) {
toast.warning(t('ai.chat.rebindUnavailable'));
return;
}
const target = resolveRebindTargetFromArg(arg, lastChatsListingRef.current);
if (!target) {
toast.warning(t('ai.chat.rebindUsage'));
return;
}
const session = sessions.find((item) => item.id === target.sessionId);
const liveTargets = new Set(liveScopeTargetIds ?? workspaceMemberTerminalIds ?? []);
for (const workspace of workspaces) liveTargets.add(workspace.id);
liveTargets.add(scopeTargetId);
if (canRebindChat(session, liveTargets)) {
rebindSessionScope?.(target.sessionId, { type: scopeType, targetId: scopeTargetId, hostIds: scopeHostIds });
toast.success(t('ai.chat.rebindDone').replace('{title}', target.title));
} else {
toast.warning(t('ai.chat.rebindUsage'));
}
// Do not rebuild /chats from this render's stale sessions (still dangling).
// Drop the resolved entry from the stored listing so a repeat /rebind <n>
// cannot target it; the next explicit /chats refreshes from live state.
const remaining = lastChatsListingRef.current.filter(
(entry) => entry.sessionId !== target.sessionId,
);
let danglingCounter = 0;
for (const entry of remaining) {
if (!entry.isDangling) {
entry.rebindNumber = undefined;
continue;
}
danglingCounter += 1;
entry.rebindNumber = danglingCounter;
}
lastChatsListingRef.current = remaining;
setPanelCommandNote(formatAiChatsListingNote(remaining, t));
}, [liveScopeTargetIds, rebindSessionScope, scopeHostIds, scopeTargetId, scopeType, sessions, t, workspaces, workspaceMemberTerminalIds]);
// Manual durability actions (history drawer header). Export writes the
// whole store to a user-chosen JSON file; import merges by id.
const handleExportSessions = useCallback(() => {
void exportSessionsBackup?.().then((filePath) => {
if (filePath == null) {
toast.warning(t('ai.chat.exportUnavailable'));
return;
}
toast.success(
filePath
? t('ai.chat.exportDone').replace('{path}', filePath)
: t('ai.chat.exportDoneNoPath'),
);
});
}, [exportSessionsBackup, t]);
const handleImportSessions = useCallback(() => {
void importSessionsBackup?.().then((count) => {
if (count <= 0) {
toast.warning(t('ai.chat.importNone'));
return;
}
toast.success(t('ai.chat.importDone').replace('{n}', String(count)));
});
}, [importSessionsBackup, t]);
const defaultTargetSession = useMemo<DefaultTargetSessionHint | undefined>(() => {
const connectedSessions = terminalSessions.filter((session) => session.connected !== false);
if (scopeType === 'terminal' && scopeTargetId) {
const target = terminalSessions.find((session) => session.sessionId === scopeTargetId);
if (target) {
return {
...target,
source: 'scope-target',
};
}
}
if (connectedSessions.length === 1) {
return {
...connectedSessions[0],
source: 'only-connected-in-scope',
};
}
return undefined;
}, [terminalSessions, scopeType, scopeTargetId]);
useEffect(() => {
if (!isVisible) return;
const bridge = getNetcattyBridge();
if (!bridge?.aiMcpUpdateSessions) return;
return scheduleWhenAiComposerIdle(() => {
void bridge.aiMcpUpdateSessions(terminalSessions, activeSessionId ?? undefined);
}, { initialDelayMs: 250 });
}, [isVisible, terminalSessions, activeSessionId]);
// Workspace maintenance can retarget an inherited chat earlier in this same
// passive-effect flush. Before clearing a selection, recheck its membership
// against the synchronous snapshot instead of this render's old history.
const isSessionVisibleInCurrentScope = useCallback((sessionId: string) => (
getScopedHistorySessions(
latestAISessionsSnapshot ?? sessions,
scopeType,
scopeTargetId,
scopeHostIds,
activeTerminalSessionIds,
workspaceMemberTerminalIds,
latestAIActiveSessionMapSnapshot ?? activeSessionIdMap,
).some((session) => session.id === sessionId)
), [sessions, scopeType, scopeTargetId, scopeHostIds, activeTerminalSessionIds, workspaceMemberTerminalIds, activeSessionIdMap]);
useEffect(() => {
if (!isVisible) return;
// Predicate must match normalizePanelView's list (scoped history), not the
// global store — out-of-scope sessions must still demote to draft.
if (!shouldForceDraftViewSync(
explicitPanelView,
normalizedPanelView,
isSessionVisibleInCurrentScope,
)) {
return;
}
showDraftView(scopeKey);
}, [isVisible, normalizedPanelView, explicitPanelView, scopeKey, isSessionVisibleInCurrentScope, showDraftView]);
useEffect(() => {
if (!activeSession) return;
// Issue #4098 phase 2: a chat becoming active in the workspace scope pins
// the workspace's anchorChatId so member loss / restart resolve by id,
// never by position.
if (scopeType === 'workspace' && scopeTargetId && isVisible) {
onSetWorkspaceAnchorChat?.(scopeTargetId, activeSession.id);
}
if (isVisible && activeSessionIdMap[scopeKey] !== activeSession.id) {
setActiveSessionId(activeSession.id);
}
}, [
activeSession,
activeSessionIdMap,
scopeKey,
scopeType,
scopeTargetId,
isVisible,
onSetWorkspaceAnchorChat,
setActiveSessionId,
]);
useEffect(() => {
if (!isVisible) return;
if (normalizedPanelView.mode !== 'draft') return;
// Keep a selection that maintenance just made displayable, but always
// honor an explicit New Chat draft. Out-of-scope / missing ids still clear.
const selectedSessionId = explicitPanelView?.mode === 'session'
? explicitPanelView.sessionId : persistedSessionId;
if (
explicitPanelView?.mode !== 'draft'
&& selectedSessionId
&& isSessionVisibleInCurrentScope(selectedSessionId)
) {
return;
}
if (persistedSessionId == null) return;
setActiveSessionId(null);
}, [
isVisible,
normalizedPanelView.mode,
explicitPanelView,
isSessionVisibleInCurrentScope,
persistedSessionId,
setActiveSessionId,
]);
const ensureScopeDraft = useCallback((agentId: string) => {
ensureDraftForScope(scopeKey, agentId);
}, [ensureDraftForScope, scopeKey]);
const updateScopeDraft = useCallback((
fallbackAgentId: string,
updater: (draft: AIDraft) => AIDraft,
) => {
updateDraft(scopeKey, fallbackAgentId, updater);
}, [scopeKey, updateDraft]);
const showScopeDraftView = useCallback(() => {
showDraftView(scopeKey);
}, [scopeKey, showDraftView]);
const showScopeSessionView = useCallback((sessionId: string) => {
showSessionView(scopeKey, sessionId);
}, [scopeKey, showSessionView]);
const discardPendingComposerText = useCallback(() => {
pendingComposerTextRef.current = null;
}, []);
const clearScopeDraft = useCallback((options?: { keepPendingText?: boolean }) => {
if (!options?.keepPendingText) discardPendingComposerText();
clearDraftForScope(scopeKey);
}, [clearDraftForScope, discardPendingComposerText, scopeKey]);
const enterScopeDraftMode = useCallback((agentId: string, preserveSessionView = false) => {
applyDraftEntrySelection({
ensureDraft: () => ensureScopeDraft(agentId),
showDraftView: showScopeDraftView,
preserveSessionView,
});
}, [ensureScopeDraft, showScopeDraftView]);
const flushDraftText = useCallback(() => {
const pending = pendingComposerTextRef.current;
if (pending == null) return;
if (panelViewRef.current.mode !== 'draft') {
enterScopeDraftMode(currentAgentId, panelViewRef.current.mode === 'session');
}
updateScopeDraft(currentAgentId, (current) => (
current.text === pending ? current : { ...current, text: pending }
));
}, [currentAgentId, enterScopeDraftMode, updateScopeDraft]);
const setInputValue = useCallback((value: string) => {
const base = currentDraftRef.current ?? {
text: '',
agentId: currentAgentId,
attachments: [],
selectedUserSkillSlugs: [],
updatedAt: Date.now(),
};
pendingComposerTextRef.current = value;
currentDraftRef.current = { ...base, text: value, updatedAt: Date.now() };
}, [currentAgentId]);
const addFiles = useCallback(async (inputFiles: File[]) => {
enterScopeDraftMode(currentAgentId, panelViewRef.current.mode === 'session');
await addDraftFiles(scopeKey, currentAgentId, inputFiles);
}, [addDraftFiles, currentAgentId, enterScopeDraftMode, scopeKey]);
const removeFile = useCallback((fileId: string) => {
removeDraftFile(scopeKey, currentAgentId, fileId);
}, [removeDraftFile, scopeKey, currentAgentId]);
// External turns keep the tools from launch; changed settings cannot enable note reads mid-turn.
const canMentionNotes = currentAgentId === 'catty' || (toolIntegrationMode === 'mcp' && !isStreaming);
const validateNoteMentions = useCallback((attachments: UploadedFile[]) => {
if (!canMentionNotes && attachments.some(isVaultNoteAttachment)) {
toast.warning(t('ai.chat.mentionNoteUnavailable'));
return false;
}
if (!vaultNoteReferencesFit(attachments)) {
toast.warning(t('ai.chat.mentionNoteTooMany'));
return false;
}
return true;
}, [canMentionNotes, t]);
/** Mention Note: attach a Vault → Notes entry as inline context for the next send. */
const mentionNote = useCallback((note: VaultNote) => {
if (!canMentionNotes) return;
const upload = createVaultNoteAttachment({ id: note.id, title: note.title.trim() || t('ai.chat.untitledNote') });
if (!upload) {
toast.error(t('ai.chat.mentionNoteInvalid', {
title: String(note.title || '').trim() || t('ai.chat.untitledNote'),
}));
return;
}
const existing = currentDraftRef.current?.attachments ?? [];
if (!validateNoteMentions([...existing.filter((file) => file.vaultNoteId !== upload.vaultNoteId), upload])) return;
enterScopeDraftMode(currentAgentId, panelViewRef.current.mode === 'session');
updateDraft(scopeKey, currentAgentId, (current) => ({
...current,
attachments: [
...current.attachments.filter((file) => file.vaultNoteId !== upload.vaultNoteId),
upload,
],
}));
}, [canMentionNotes, validateNoteMentions, updateDraft, currentAgentId, enterScopeDraftMode, scopeKey, t]);
useEffect(() => {
if (isVisible) return undefined;
flushDraftText();
return undefined;
}, [flushDraftText, isVisible]);
useEffect(() => () => {
flushDraftText();
}, [flushDraftText, scopeKey]);
useEffect(() => {
flushDraftText();
}, [activeSessionId, flushDraftText]);
useEffect(() => {
if (!isVisible) return;
let cancelled = false;
const applyUserSkillsStatus = (result: { ok: boolean; skills?: Array<{
id: string;
slug: string;
name: string;
description: string;
status: 'ready' | 'warning';
}> } | null | undefined) => {
const nextOptions = getReadyUserSkillOptions(result);
setUserSkillOptions(nextOptions);
const draft = currentDraftRef.current;
if (!draft) {
return;
}
const nextSelectedUserSkillSlugs =
getNextSelectedUserSkillSlugsMap(
{ [scopeKey]: draft.selectedUserSkillSlugs },
result,
)[scopeKey] ?? [];
const selectedUserSkillsChanged =
nextSelectedUserSkillSlugs.length !== draft.selectedUserSkillSlugs.length
|| nextSelectedUserSkillSlugs.some((slug, index) => slug !== draft.selectedUserSkillSlugs[index]);
if (!selectedUserSkillsChanged) {
return;
}
updateScopeDraft(draft.agentId, (currentScopeDraft) => ({
...currentScopeDraft,
selectedUserSkillSlugs: nextSelectedUserSkillSlugs,
}));
};
const bridge = getNetcattyBridge();
const cancelIdle = scheduleWhenAiComposerIdle(() => {
void loadUserSkillsStatus(bridge)
.then((result) => {
if (cancelled) return;
if (result === undefined) return;
applyUserSkillsStatus(result);
})
.catch(() => {});
});
return () => {
cancelled = true;
cancelIdle();
};
}, [isVisible, scopeKey, toolIntegrationMode, updateScopeDraft, userSkillsStatusVersion]);
useEffect(() => {
const handleUserSkillsChanged = () => {
setUserSkillsStatusVersion((version) => version + 1);
};
return subscribeUserSkillsStatusChanged(handleUserSkillsChanged);
}, []);
useEffect(() => {
if (!isVisible) return;
const bridge = getNetcattyBridge();
if (!bridge?.aiSyncProviders || providers.length === 0) return;
void bridge.aiSyncProviders(providers);
}, [isVisible, providers]);
useEffect(() => {
if (!isVisible) return;
const bridge = getNetcattyBridge();
if (!bridge?.aiSyncWebSearch) return;
void bridge.aiSyncWebSearch(webSearchConfig?.apiHost || null, webSearchConfig?.apiKey || null);
}, [isVisible, webSearchConfig?.apiHost, webSearchConfig?.apiKey, webSearchConfig?.enabled]);
const {
discoveredAgents,
isDiscovering,
rediscover,
enableAgent,
} = useAgentDiscovery(externalAgents, setExternalAgents, {
enabled: isVisible,
schedule: scheduleWhenAiComposerIdle,
});
const handleEnableDiscoveredAgent = useCallback(
(agent: DiscoveredAgent) => {
const config = enableAgent(agent);
setExternalAgents?.((prev) => [...prev, config]);
},
[enableAgent, setExternalAgents],
);
const messages = activeSession?.messages ?? [];
const selectedUserSkillSlugs = useMemo(
() => currentDraft?.selectedUserSkillSlugs ?? [],
[currentDraft],
);
const selectedUserSkills = useMemo(
() =>
selectedUserSkillSlugs.map((slug) => {
const option = userSkillOptions.find((skill) => skill.slug === slug);
return option ?? { id: slug, slug, name: slug, description: '' };
}),
[selectedUserSkillSlugs, userSkillOptions],
);
const { handleExport } = useConversationExport(activeSession);
const activeProvider = useMemo(
() => providers.find((p) => p.id === activeProviderId),
[providers, activeProviderId],
);
const cattyAgentProvider = useMemo(() => {
const overrideId = agentProviderMap['catty'];
if (overrideId) {
const p = providers.find((cfg) => cfg.id === overrideId);
if (p) return p;
}
return activeProvider;
}, [agentProviderMap, providers, activeProvider]);
const cattyAgentModelId = useMemo(() => {
const trim = (s: string | undefined | null): string => (s ?? '').trim();
const overrideId = agentProviderMap['catty'];
const overrideProvider = overrideId
? providers.find((cfg) => cfg.id === overrideId)
: undefined;
if (overrideProvider) {
return trim(agentModelMap['catty']) || trim(overrideProvider.defaultModel);
}
return trim(cattyAgentProvider?.defaultModel) || trim(activeModelId);
}, [agentModelMap, agentProviderMap, providers, cattyAgentProvider, activeModelId]);
const effectiveActiveProvider = currentAgentId === 'catty' ? cattyAgentProvider : activeProvider;
const effectiveActiveModelId = currentAgentId === 'catty' ? cattyAgentModelId : activeModelId;
const cattyConfiguredProviders = useMemo(
() => (currentAgentId === 'catty' ? providers : []),
[currentAgentId, providers],
);
// Hide the ring until a real context_snapshot arrives. A synthetic 0% fallback
// misleads users when reopening long sessions (looks empty until the next turn).
const contextUsage = currentAgentId === 'catty' ? observedContextUsage : null;
// Catty-only manual compact. External agents own their own compaction.
// No minimum message count: short sessions may still run /compact (even if
// there is little to summarize).
const canCompact = currentAgentId === 'catty'
&& Boolean(activeSessionId)
&& !isStreaming
&& Boolean(effectiveActiveProvider)
&& Boolean(effectiveActiveModelId.trim());
// Undo last turn: offered only at a safe boundary (never inside a compacted
// prefix, never between an assistant tool call and its result).
const canUndoLastTurn = useMemo(() => Boolean(
activeSession
&& !isStreaming
&& resolveUndoLastTurnBoundary(
activeSession.messages,
activeSession.contextCompaction?.compactedMessageCount ?? 0,
) != null,
), [activeSession, isStreaming]);
const undoInFlightRef = useRef(false);
const handleUndoLastTurn = useCallback(async () => {
const sessionId = activeSessionRef.current?.id;
if (!sessionId || !undoLastTurnInSession) return;
if (isStreaming || isAIChatSessionStreaming(sessionId)) return;
// Serialize: while a branch is still being created, `canUndoLastTurn` stays stale, so a quick second
// click must not start a parallel undo of the same session — that would
// publish duplicate branches and restore the same prompt twice.
if (undoInFlightRef.current) return;
undoInFlightRef.current = true;
setIsUndoingLastTurn(true);
try {
const result = await undoLastTurnInSession(sessionId, {
type: scopeType,
targetId: scopeTargetId,
});
if (!result) return;
ensureScopeDraft(currentAgentId);
// Text typed since the last flush lives only in the live composer buffer
// (pendingComposerTextRef/currentDraftRef), not in the persisted draft;
// merge from the live buffer so it is not lost, then drop the pending
// buffer so the later flushDraftText() cannot overwrite the merged text.
const liveDraft = currentDraftRef.current;
updateScopeDraft(currentAgentId, (draft) => {
const liveText = liveDraft?.text ?? draft.text;
const liveSkillSlugs = liveDraft?.selectedUserSkillSlugs ?? draft.selectedUserSkillSlugs;
return {
...draft,
// Keep anything the user is still typing; the undone prompt goes first.
text: liveText.trim()
? `${result.restored.text}\n\n${liveText}`
: result.restored.text,
attachments: [
...result.restored.attachments,
...(liveDraft?.attachments ?? draft.attachments),
],
// The undone turn was selected with these pills; keep any pills the
// user picked since the send on top of them.
selectedUserSkillSlugs: [
...result.restored.selectedUserSkillSlugs,
...liveSkillSlugs.filter((slug) => !result.restored.selectedUserSkillSlugs.includes(slug)),
],
};
});
discardPendingComposerText();
showScopeSessionView(result.sessionId);
// Tool side effects live outside the conversation and cannot be rolled back.
toast.info(t('ai.chat.undoLastTurnNotice'));
} finally {
undoInFlightRef.current = false;
setIsUndoingLastTurn(false);
}
}, [
discardPendingComposerText,
ensureScopeDraft,
isStreaming,
showScopeSessionView,
t,
undoLastTurnInSession,
updateScopeDraft,
currentAgentId,
scopeType,
scopeTargetId,
]);
const providersRef = useRef(providers);
providersRef.current = providers;
const handleAgentProviderModelSelect = useCallback(
(providerId: string, modelId: string, contextWindow?: number) => {
setAgentProvider(currentAgentId, providerId);
setAgentModel(currentAgentId, modelId);
const sanitized = sanitizeContextWindow(contextWindow);
if (!updateProvider || sanitized == null) return;
const provider = providersRef.current.find((item) => item.id === providerId);
if (!provider) return;
if (provider.modelContextWindows?.[modelId] === sanitized) return;
updateProvider(providerId, {
modelContextWindows: {
...(provider.modelContextWindows ?? {}),
[modelId]: sanitized,
},
});
},
[currentAgentId, setAgentProvider, setAgentModel, updateProvider],
);
const providerDisplayName = effectiveActiveProvider?.name ?? '';
const modelDisplayName = effectiveActiveModelId || effectiveActiveProvider?.defaultModel || '';
const currentAgentConfig = useMemo(
() => currentAgentId !== 'catty' ? externalAgents.find(a => a.id === currentAgentId) : undefined,
[currentAgentId, externalAgents],
);
const isCodexManagedAgent = useMemo(
() => currentAgentConfig ? matchesManagedAgentConfig(currentAgentConfig, 'codex') : false,
[currentAgentConfig],
);
const {
model: codexConfigModel,
loadModel: loadCodexConfigModel,
isPending: isCodexConfigModelPending,
} = useCodexConfigModel(
currentAgentConfig, isVisible,
);
const agentModelMapRef = useRef(agentModelMap);
agentModelMapRef.current = agentModelMap;
// Manual model ids typed in the composer picker are recorded in composer
// prefs (per agent scope). Re-render when they change so custom picks stay
// visible and accepted as stored selections (#3534).
const [composerCustomModelsVersion, setComposerCustomModelsVersion] = useState(0);
useEffect(() => subscribeComposerModelPrefs(() => {
setComposerCustomModelsVersion((version) => version + 1);
}), []);
const collectCustomModelIds = useCallback((agentId: string, presets: AgentModelPreset[]) => {
// Touch the version so pref writes recreate this callback and invalidate
// memos that read custom ids.
void composerCustomModelsVersion;
return resolveComposerCustomModelIds({
prefs: readComposerModelPrefs(agentId),
presets,
});
}, [composerCustomModelsVersion]);
const buildExternalAgentRuntimeModelTarget = useCallback((agent: ExternalAgentConfig | undefined): SdkRuntimeModelTarget | null => {
if (!agent) return null;
const sdkBackend = getExternalAgentSdkBackend(agent);
if (!sdkBackend) return null;
// Cursor: re-inject auth mode for list-models (same as run-turn). Persisted
// agent.env strips NETCATTY_CURSOR_*; without this, main defaults to api-key
// and the UI falls back to curated CURSOR_MODEL_PRESETS (#2562).
const agentEnv = sdkBackend === 'cursor'
? buildCursorListModelsAgentEnv(agent)
: agent.env;
return {
agentId: agent.id,
cacheKey: buildSdkRuntimeModelCacheKey({ ...agent, env: agentEnv }),
sdkBackend,
agentEnv,
agentCommand: getManualAgentCommand(agent),
codexRuntime: sdkBackend === 'codex' ? (agent.codexRuntime ?? 'sdk') : undefined,
};
}, []);
const externalAgentsRef = useRef(externalAgents);
externalAgentsRef.current = externalAgents;
const applySdkRuntimeModelCatalog = useCallback((
target: SdkRuntimeModelTarget,
catalog: SdkRuntimeModelCatalog,
options: { adoptCurrentModel?: boolean } = {},
) => {
const agent = externalAgentsRef.current.find((item) => item.id === target.agentId);
if (buildExternalAgentRuntimeModelTarget(agent)?.cacheKey !== target.cacheKey) return;
const agentId = target.agentId;
// Record the catalog provenance for the picker's status chip. An absent
// source (older main without the marker) displays as a live catalog.
setRuntimeModelCatalogMeta((prev) => ({
...prev,
[agentId]: {
cacheKey: target.cacheKey,
source: catalog.source ?? 'live',
...(catalog.fetchedAt ? { fetchedAt: catalog.fetchedAt } : {}),
},
}));
const catalogPresets = mergeFallbackThinkingLevels(
normalizeSdkRuntimeModelPresets(catalog.models, catalog.currentModelId),
getAgentModelPresets(agent?.command, getExternalAgentSdkBackend(agent)),
);
const runtimePresets = retainStoredModelPresetForCatalog(
catalogPresets,
appendComposerCustomModelPresets(
catalogPresets,
collectCustomModelIds(agentId, catalogPresets),
),
agentModelMapRef.current[agentId],
agent,
Boolean(catalog.warning),
);
setRuntimeAgentModelPresets((prev) => (
prev[agentId]?.cacheKey === target.cacheKey
&& prev[agentId].catalogUnavailable === Boolean(catalog.warning)
&& agentModelPresetsShallowEqual(prev[agentId].models, catalogPresets)
? prev
: { ...prev, [agentId]: { cacheKey: target.cacheKey, models: catalogPresets, catalogUnavailable: Boolean(catalog.warning) } }
));
// Another path (e.g. send preflight) applied the catalog before the
// deferred idle callback scheduled in the visibility effect: clear its
// stale pending flag so the composer stops claiming models are loading.
setRuntimeModelLoading((prev) => {
if (!prev[agentId] || prev[agentId].cacheKey !== target.cacheKey) return prev;
const { [agentId]: _removed, ...rest } = prev;
return rest;
});
const normalizedStoredModelId = normalizeStoredAgentModelSelection(
agentModelMapRef.current[agentId],
runtimePresets,
);
if (normalizedStoredModelId && normalizedStoredModelId !== agentModelMapRef.current[agentId]) {
setAgentModel(agentId, normalizedStoredModelId);
return;
}
const defaultModelId = resolveSdkRuntimeCatalogDefaultModelId(catalog, agent);
if (
options.adoptCurrentModel
&& defaultModelId
&& shouldAdoptSdkCurrentModel(defaultModelId, agentModelMapRef.current[agentId], runtimePresets, agent)
) {
setAgentModel(agentId, defaultModelId);
}
}, [setAgentModel, setRuntimeModelLoading, buildExternalAgentRuntimeModelTarget, collectCustomModelIds]);
const loadSdkRuntimeModelCatalog = useCallback((
target: SdkRuntimeModelTarget,
options: { force?: boolean; logErrors?: boolean } = {},
): Promise<SdkRuntimeModelCatalog | null> => {
const bridge = getNetcattyBridge();
if (!bridge?.aiSdkAgentListModels) return Promise.resolve(null);
return sdkRuntimeModelCache.refresh(
target.cacheKey,
async () => {
const result = await bridge.aiSdkAgentListModels!(
target.sdkBackend,
undefined,
undefined,
`models_${target.agentId}`,
target.agentEnv,
target.agentCommand,
target.codexRuntime,
// Manual refresh (retry banner / picker button) must bypass the
// main-side LKG-first policy and go live.
options.force === true,
);
if (!result?.ok || !Array.isArray(result.models)) {
throw new Error(result?.error || 'Failed to load SDK agent models');
}
setRuntimeModelWarnings((current) => {
const agent = externalAgentsRef.current.find((item) => item.id === target.agentId);
if (buildExternalAgentRuntimeModelTarget(agent)?.cacheKey !== target.cacheKey) return current;
const next = { ...current };
if (result.warning) {
next[target.agentId] = {
cacheKey: target.cacheKey,
message: t('ai.chat.modelCatalogWarning'),
};
console.warn('[AIChatSidePanel] SDK model catalog unavailable:', result.warning);
} else {
delete next[target.agentId];
}
return next;
});
return toSdkRuntimeModelCatalog(result);
},
{ force: options.force },
).then((catalog) => {
// A refresh that joined another panel's in-flight load never ran the
// loader above, so sync this panel's banner from the shared result.
setRuntimeModelWarnings((current) => {
const agent = externalAgentsRef.current.find((item) => item.id === target.agentId);
if (buildExternalAgentRuntimeModelTarget(agent)?.cacheKey !== target.cacheKey) return current;
return reconcileRuntimeModelWarning(
current,
target.agentId,
target.cacheKey,
Boolean(catalog.warning),
t('ai.chat.modelCatalogWarning'),
);
});
return catalog;
}).catch((err) => {
setRuntimeModelWarnings((current) => {
const agent = externalAgentsRef.current.find((item) => item.id === target.agentId);
if (buildExternalAgentRuntimeModelTarget(agent)?.cacheKey !== target.cacheKey) return current;
return {
...current,
[target.agentId]: { cacheKey: target.cacheKey, message: t('ai.chat.modelCatalogWarning') },
};
});
if (options.logErrors !== false) {
console.warn('[AIChatSidePanel] Failed to load SDK agent models:', err);
}
return null;
});
}, [t, buildExternalAgentRuntimeModelTarget]);
useEffect(() => {
if (!isVisible) return;
if (!currentAgentConfig) return;
if (!shouldLoadSdkRuntimeModels(currentAgentConfig) && !isCodexManagedAgent) return;
const target = buildExternalAgentRuntimeModelTarget(currentAgentConfig);
if (!target) return;
const cached = sdkRuntimeModelCache.read(target.cacheKey);
if (cached) {
applySdkRuntimeModelCatalog(target, cached);
// The cache stores warned (last-known-good) responses too: sync the
// banner so another panel or a remount within the TTL does not present
// degraded models as live.
setRuntimeModelWarnings((current) => reconcileRuntimeModelWarning(
current,
target.agentId,
target.cacheKey,
Boolean(cached.warning),
t('ai.chat.modelCatalogWarning'),
));
}
// Respect renderer TTL / in-flight coalescing for all SDK agents including
// OpenCode. Forced refresh used to re-spawn opencode on every effect re-run
// even when the user never selected OpenCode (#2184). Manual refresh still
// passes force via the model selector path. Defer the network refresh so
// expand → first type does not wait on CLI model listing.
const clearRuntimeModelLoading = () => {
setRuntimeModelLoading((prev) => {
if (!prev[target.agentId] || prev[target.agentId].cacheKey !== target.cacheKey) return prev;
const { [target.agentId]: _removed, ...rest } = prev;
return rest;
});
};
let cancelled = false;
let idleFired = false;
// Mark the catalog pending as soon as the refresh is scheduled so the
// composer can show its loading affordance for the whole idle delay (and
// longer while the composer stays focused) instead of silently keeping
// only the build-time presets (#3584). Only do this on a cache miss:
// when a cached catalog was already applied above, the runtime presets are
// available, so a pending flag would keep the composer banner/picker in the
// "loading" state indefinitely while the idle callback is deferred.
if (!cached) {
setRuntimeModelLoading((prev) => (
prev[target.agentId]?.cacheKey === target.cacheKey ? prev : { ...prev, [target.agentId]: { cacheKey: target.cacheKey } }
));
}
const cancelIdle = scheduleWhenAiComposerIdle(() => {
idleFired = true;
void loadSdkRuntimeModelCatalog(target).then((catalog) => {
if (cancelled || !catalog) return;
applySdkRuntimeModelCatalog(target, catalog, { adoptCurrentModel: true });
}).finally(() => {
// Always clear, even after cancellation: a stale flag would keep the
// composer in the loading state forever.
clearRuntimeModelLoading();
});
});
return () => {
cancelled = true;
cancelIdle();
// If the idle callback never fired, the load never started and nothing
// would clear the pending flag — do it here. In-flight loads clear their
// own flag in `.finally`, so leave them alone.
if (!idleFired) clearRuntimeModelLoading();
};
}, [
isVisible,
currentAgentConfig,
isCodexManagedAgent,
buildExternalAgentRuntimeModelTarget,
loadSdkRuntimeModelCatalog,
applySdkRuntimeModelCatalog,
t,
]);
// Uniform status chip inputs: when a runtime catalog is active for the
// current agent its recorded provenance wins; otherwise the composer is
// showing build-time presets (the renderer-side preset fallback derives
// 'presets' here). While a load is pending the picker replaces the chip
// with the spinner row.
const modelCatalogSource = useMemo((): SdkRuntimeModelCatalogSource | undefined => {
// A config-locked Codex picker shows only that configured model, not the
// discovered catalog whose provenance is tracked below.
if (isCodexManagedAgent && codexConfigModel) return undefined;
const target = buildExternalAgentRuntimeModelTarget(currentAgentConfig);
const meta = runtimeModelCatalogMeta[currentAgentId];
const runtimeEntry = runtimeAgentModelPresets[currentAgentId];
if (meta && target && meta.cacheKey === target.cacheKey
&& runtimeEntry?.cacheKey === target.cacheKey && runtimeEntry.models.length > 0) {
return meta.source;
}
return 'presets';
}, [currentAgentConfig, currentAgentId, runtimeModelCatalogMeta, runtimeAgentModelPresets, buildExternalAgentRuntimeModelTarget, isCodexManagedAgent, codexConfigModel]);
const modelCatalogFetchedAt = useMemo((): string | undefined => {
const target = buildExternalAgentRuntimeModelTarget(currentAgentConfig);
const meta = runtimeModelCatalogMeta[currentAgentId];
if (meta && target && meta.cacheKey === target.cacheKey) return meta.fetchedAt;
return undefined;
}, [currentAgentConfig, currentAgentId, runtimeModelCatalogMeta, buildExternalAgentRuntimeModelTarget]);
const isCodexAppServer = isCodexManagedAgent && currentAgentConfig?.codexRuntime === 'app-server';
const canSteerCurrentTurn = Boolean(activeSessionId && isStreaming && isCodexAppServer);
const hasCodexCustomConfig = Boolean(codexConfigModel) && isCodexManagedAgent;
// `codexConfigModel` is null both while the config probe is pending and when
// the managed Codex config has no locked model. Keep the manual-entry action
// hidden until the probe resolves so a pick made in the interim can't be
// silently overridden by the config model on send (#3535).
const allowCustomModelEntry = !isCodexManagedAgent
|| (!isCodexConfigModelPending && !hasCodexCustomConfig);
const agentModelPresets = useMemo(() => {
const target = buildExternalAgentRuntimeModelTarget(currentAgentConfig);
const runtimeEntry = runtimeAgentModelPresets[currentAgentId];
const runtimePresets = runtimeEntry && target && runtimeEntry.cacheKey === target.cacheKey
? runtimeEntry.models
: undefined;
if (hasCodexCustomConfig && codexConfigModel) {
return [{ id: codexConfigModel, name: codexConfigModel }];
}
if (runtimePresets?.length) {
return appendRetainedStoredModelPreset(
appendComposerCustomModelPresets(
runtimePresets,
collectCustomModelIds(currentAgentId, runtimePresets),
),
agentModelMap[currentAgentId],
currentAgentConfig,
Boolean(runtimeEntry?.catalogUnavailable),
);
}
const presets = getAgentModelPresets(
currentAgentConfig?.command,
getExternalAgentSdkBackend(currentAgentConfig),
);
// BYO Codex CLI: hide GPT-5.6 when CLI < 0.144.0 (stored probe or discovery).
const cliVersion = resolveAgentCliVersion(currentAgentConfig, discoveredAgents);
const filteredPresets = filterAgentModelPresetsForCliVersion(presets, cliVersion);
// Manual model ids typed in the composer (#3534) stay selectable even when
// the CLI catalog does not list them yet.
return appendRetainedStoredModelPreset(
appendComposerCustomModelPresets(
filteredPresets,
collectCustomModelIds(currentAgentId, filteredPresets),
),
agentModelMap[currentAgentId],
currentAgentConfig,
// Until the initial catalog settles, show the choice a failed
// preflight would actually send instead of flashing Auto.
!runtimeEntry || !target || runtimeEntry.cacheKey !== target.cacheKey || runtimeEntry.catalogUnavailable,
);
}, [
currentAgentConfig,
currentAgentId,
runtimeAgentModelPresets,
agentModelMap,
buildExternalAgentRuntimeModelTarget,
hasCodexCustomConfig,
codexConfigModel,
discoveredAgents,
collectCustomModelIds,
]);
const selectedAgentModel = useMemo(() => {
const stored = agentModelMap[currentAgentId];
if (shouldUseStoredAgentModel(stored, agentModelPresets, currentAgentConfig)) {
return normalizeStoredAgentModelSelection(stored, agentModelPresets) ?? stored;
}
if (agentModelPresets.length > 0) {
// Cursor CLI login defaults to `auto` (subscription quota routing).
if (currentAgentConfig?.cursorAuthMode === 'cli-login') {
const autoPreset = agentModelPresets.find((preset) => preset.id === 'auto');
if (autoPreset) return resolveAgentModelSelection(autoPreset);
}
// Use catalog defaultThinkingLevel — do not pick last array entry
// (that made GPT-5.6 Sol default to ultra).
return resolveAgentModelSelection(agentModelPresets[0]);
}
return undefined;
}, [currentAgentConfig, currentAgentId, agentModelMap, agentModelPresets]);
const inputAgentId = activeSession?.agentId ?? currentDraft?.agentId ?? currentAgentId;
const canSendCurrentAgent = useMemo(
() => !isSending && !isUndoingLastTurn && canSendWithAgent(inputAgentId, externalAgents),
[inputAgentId, externalAgents, isSending, isUndoingLastTurn],
);
const handleAgentModelSelect = useCallback((modelId: string) => {
setAgentModel(currentAgentId, modelId);
}, [currentAgentId, setAgentModel]);
// Retry a failed SDK runtime model catalog load (composer warning banner).
// Failures are never cached, so a forced refresh re-queries the CLI/daemon.
const handleRetrySdkModelCatalog = useCallback(() => {
const target = buildExternalAgentRuntimeModelTarget(currentAgentConfig);
if (!target) return;
setRuntimeModelLoading((prev) => (
prev[target.agentId]?.cacheKey === target.cacheKey ? prev : { ...prev, [target.agentId]: { cacheKey: target.cacheKey } }
));
void loadSdkRuntimeModelCatalog(target, { force: true }).then((catalog) => {
if (!catalog) return;
applySdkRuntimeModelCatalog(target, catalog, { adoptCurrentModel: true });
}).finally(() => {
setRuntimeModelLoading((prev) => {
if (!prev[target.agentId] || prev[target.agentId].cacheKey !== target.cacheKey) return prev;
const { [target.agentId]: _removed, ...rest } = prev;
return rest;
});
});
}, [
currentAgentConfig,
buildExternalAgentRuntimeModelTarget,
loadSdkRuntimeModelCatalog,
applySdkRuntimeModelCatalog,
]);
const selectedCattyThinking = agentThinkingMap.catty;
const handleCattyThinkingSelect = useCallback((level: string) => {
setAgentThinking('catty', level);
}, [setAgentThinking]);
const handleNewChat = useCallback(() => {
clearScopeDraft();
updateScopeDraft(currentAgentId, () => ({
text: '',
agentId: currentAgentId,
attachments: [],
selectedUserSkillSlugs: [],
updatedAt: Date.now(),
}));
showScopeDraftView();
setShowHistory(false);
}, [clearScopeDraft, currentAgentId, showScopeDraftView, updateScopeDraft]);
const handleOpenSettings = useCallback(() => {
void openSettingsWindow();
}, [openSettingsWindow]);
/** Ref to always access latest sessions (avoids stale closure in autoTitleSession). */
const sessionsRef = useRef(sessions);
sessionsRef.current = sessions;
/** Auto-title a session from the first user message if untitled. */
const autoTitleSession = useCallback((sessionId: string, text: string) => {
const s = sessionsRef.current.find(x => x.id === sessionId);
if (s && (!s.title || s.title === 'New Chat')) {
updateSessionTitle(sessionId, text.length > 50 ? text.slice(0, 50) + '...' : text);
}
}, [updateSessionTitle]);
const buildExecutorContextForScope = useCallback((scope: {
type: 'terminal' | 'workspace';
targetId?: string;
label?: string;
}): ExecutorContext => {
const resolved = resolveExecutorContextRef.current?.(scope);
if (resolved) return resolved;
return {
sessions: terminalSessionsRef.current,
workspaceId: scope.type === 'workspace' ? scope.targetId : undefined,
workspaceName: scope.type === 'workspace' ? scope.label : undefined,
};
}, []);
const addSelectedUserSkill = useCallback((slug: string) => {
const normalizedSlug = String(slug || '').trim().toLowerCase();
if (!normalizedSlug) return;
enterScopeDraftMode(currentAgentId, panelViewRef.current.mode === 'session');
updateScopeDraft(currentAgentId, (draft) => {
if (draft.selectedUserSkillSlugs.includes(normalizedSlug)) {
return draft;
}
return {
...draft,
selectedUserSkillSlugs: [...draft.selectedUserSkillSlugs, normalizedSlug],
};
});
}, [currentAgentId, enterScopeDraftMode, updateScopeDraft]);
const removeSelectedUserSkill = useCallback((slug: string) => {
const normalizedSlug = String(slug || '').trim().toLowerCase();
if (!normalizedSlug) return;
enterScopeDraftMode(currentAgentId, panelViewRef.current.mode === 'session');
updateScopeDraft(currentAgentId, (draft) => {
const nextSelectedUserSkillSlugs = draft.selectedUserSkillSlugs.filter(
(entry) => entry !== normalizedSlug,
);
if (nextSelectedUserSkillSlugs.length === draft.selectedUserSkillSlugs.length) {
return draft;
}
return {
...draft,
selectedUserSkillSlugs: nextSelectedUserSkillSlugs,
};
});
}, [currentAgentId, enterScopeDraftMode, updateScopeDraft]);
const handleSend = useCallback(async () => {
const draft = currentDraftRef.current;
const currentPanelView = panelViewRef.current;
const currentSessionView = activeSessionRef.current;
if (!validateNoteMentions(draft?.attachments ?? [])) return;
const trimmed = draft?.text.trim() ?? '';
const sendScopeKey = scopeKey;
const attachments = (draft?.attachments ?? []).map((file) => ({
base64Data: file.base64Data,
mediaType: file.mediaType,
filename: file.filename,
filePath: file.filePath,
terminalSelection: file.terminalSelection,
vaultNoteId: file.vaultNoteId,
vaultNoteTitle: file.vaultNoteTitle,
previewText: file.previewText,
lineCount: file.lineCount,
}));
const hasInlineTextAttachments = attachments.some(isInlineTextAttachment);
if ((!trimmed && !hasInlineTextAttachments) || isStreaming) return;
// While undo-last-turn is creating the branch, the target is still the source; a
// turn submitted now would run in the soon-hidden source session, so
// block it until the branch settles.
if (isUndoingLastTurn) return;
// Note-only sends (empty text + a mentioned note) still need a usable
// session title: fall back to the first mentioned note's title so these
// conversations don't all show up as "Untitled" in history.
const noteOnlyTitle = attachments.find(
(attachment) => isVaultNoteAttachment(attachment) && attachment.vaultNoteTitle,
)?.vaultNoteTitle ?? '';
const titleText = trimmed || noteOnlyTitle.trim();
const sendAgentId = currentSessionView?.agentId ?? draft?.agentId ?? currentAgentId;
const agentConfig = sendAgentId !== 'catty' ? findEnabledExternalAgent(externalAgents, sendAgentId) : undefined;
if (sendAgentId !== 'catty' && !agentConfig) return;
const selectedSkillSlugs = draft?.selectedUserSkillSlugs ?? [];
const modelPrompt = buildPromptWithTerminalSelectionAttachments(trimmed, attachments);
const modelAttachments = attachments.filter((attachment) => !isInlineTextAttachment(attachment));
const isDraftMode = currentPanelView.mode === 'draft';
flushDraftText();
const submittedText = draft?.text ?? '';
const keepPendingAfterSend = () => {
const pending = pendingComposerTextRef.current;
return pending != null && pending !== submittedText;
};
const sendGateKey = currentSessionView?.id ?? `draft:${scopeKey}`;
if (!tryBeginSendForKey(sendGateKey)) {
return;
}
let sessionSendGateKey: string | null = null;
const sendEpoch = sendEpochRef.current;
const isSendStale = () => sendEpochRef.current !== sendEpoch;
setIsSending(true);
try {
const sendBridge = getNetcattyBridge();
if (sendBridge?.aiSyncProviders && providers.length > 0) {
await sendBridge.aiSyncProviders(providers);
}
if (sendBridge?.aiSyncWebSearch) {
await sendBridge.aiSyncWebSearch(webSearchConfig?.apiHost || null, webSearchConfig?.apiKey || null);
}
// A quick send must await the same config probe as the picker. Its model
// also takes precedence over App Server's built-in model catalog.
const configModel = await loadCodexConfigModel();
let sendSelectedAgentModel = configModel ?? selectedAgentModel;
if (!configModel && currentAgentConfig && shouldLoadSdkRuntimeModels(currentAgentConfig)) {
const runtimeTarget = buildExternalAgentRuntimeModelTarget(currentAgentConfig);
if (runtimeTarget) {
const catalog = await loadSdkRuntimeModelCatalog(runtimeTarget);
if (catalog) {
applySdkRuntimeModelCatalog(runtimeTarget, catalog, { adoptCurrentModel: true });
const catalogPresets = mergeFallbackThinkingLevels(
normalizeSdkRuntimeModelPresets(catalog.models, catalog.currentModelId),
getAgentModelPresets(
currentAgentConfig.command,
getExternalAgentSdkBackend(currentAgentConfig),
),
);
const runtimePresets = retainStoredModelPresetForCatalog(
catalogPresets,
appendComposerCustomModelPresets(
catalogPresets,
collectCustomModelIds(sendAgentId, catalogPresets),
),
agentModelMapRef.current[sendAgentId],
currentAgentConfig,
Boolean(catalog.warning),
);
const storedModelId = agentModelMapRef.current[sendAgentId];
const defaultModelId = resolveSdkRuntimeCatalogDefaultModelId(catalog, currentAgentConfig);
if (
defaultModelId
&& shouldAdoptSdkCurrentModel(defaultModelId, storedModelId, runtimePresets, currentAgentConfig)
) {
sendSelectedAgentModel = defaultModelId;
} else if (shouldUseStoredAgentModel(storedModelId, runtimePresets, currentAgentConfig)) {
sendSelectedAgentModel = normalizeStoredAgentModelSelection(
storedModelId,
runtimePresets,
) ?? storedModelId ?? sendSelectedAgentModel;
} else if (runtimePresets[0]) {
sendSelectedAgentModel = resolveAgentModelSelection(runtimePresets[0]);
}
}
}
}
setIsSending(false);
const sendPermissionMode = permissionModeRef.current;
if (isSendStale()) return;
if (currentAgentId !== sendAgentId) return;
const liveView = panelViewRef.current;
if (liveView.mode !== currentPanelView.mode) return;
if (
currentPanelView.mode === 'session'
&& liveView.mode === 'session'
&& liveView.sessionId !== currentPanelView.sessionId
) {
return;
}
void warmAiMarkdownRenderer();
let sessionId = currentSessionView?.id ?? null;
let currentSession = currentSessionView ?? null;
if (isDraftMode) {
const scope: AISessionScope = { type: scopeType, targetId: scopeTargetId, hostIds: scopeHostIds };
const createdSession = createSession(scope, sendAgentId);
sessionId = createdSession.id;
currentSession = createdSession;
clearScopeDraft({ keepPendingText: keepPendingAfterSend() });
showScopeSessionView(createdSession.id);
setActiveSessionId(createdSession.id);
sessionSendGateKey = sessionId;
if (!tryBeginSendForKey(sessionSendGateKey)) {
return;
}
}
if (!sessionId) {
return;
}
if (isAIChatSessionStreaming(sessionId)) {
return;
}
const isExternalAgent = sendAgentId !== 'catty';
const sendActiveProvider = isExternalAgent ? activeProvider : effectiveActiveProvider;
const sendActiveModelId = isExternalAgent ? activeModelId : effectiveActiveModelId;
if (!isExternalAgent && !sendActiveProvider) {
addMessageToSession(sessionId, {
id: generateId(), role: 'user', content: trimmed,
...(attachments.length > 0 ? { attachments } : {}),
...(selectedSkillSlugs.length > 0 ? { selectedUserSkillSlugs: selectedSkillSlugs } : {}),
timestamp: Date.now(),
});
addMessageToSession(sessionId, { id: generateId(), role: 'assistant', content: t('ai.chat.noProvider'), timestamp: Date.now() });
if (currentPanelView.mode === 'session') {
clearScopeDraft({ keepPendingText: keepPendingAfterSend() });
showScopeSessionView(sessionId);
}
return;
}
if (!isExternalAgent && !sendActiveModelId.trim()) {
addMessageToSession(sessionId, {
id: generateId(), role: 'user', content: trimmed,
...(attachments.length > 0 ? { attachments } : {}),
...(selectedSkillSlugs.length > 0 ? { selectedUserSkillSlugs: selectedSkillSlugs } : {}),
timestamp: Date.now(),
});
addMessageToSession(sessionId, { id: generateId(), role: 'assistant', content: t('ai.chat.noProviderModel'), timestamp: Date.now() });
if (currentPanelView.mode === 'session') {
clearScopeDraft({ keepPendingText: keepPendingAfterSend() });
showScopeSessionView(sessionId);
}
return;
}
addMessageToSession(sessionId, {
id: generateId(), role: 'user', content: trimmed,
...(attachments.length > 0 ? { attachments } : {}),
// Kept on the user message so undo last turn can restore the pills.
...(selectedSkillSlugs.length > 0 ? { selectedUserSkillSlugs: selectedSkillSlugs } : {}),
timestamp: Date.now(),
});
clearScopeDraft({ keepPendingText: keepPendingAfterSend() });
showScopeSessionView(sessionId);
setActiveSessionId(sessionId);
setStreamingForScope(sessionId, true);
const assistantMsgId = generateId();
addMessageToSession(sessionId, {
id: assistantMsgId, role: 'assistant', content: '', timestamp: Date.now(),
model: isExternalAgent
? (sendSelectedAgentModel || agentConfig?.name || 'external')
: (sendActiveModelId || sendActiveProvider?.defaultModel || ''),
providerId: isExternalAgent ? undefined : sendActiveProvider?.providerId,
});
const abortController = new AbortController();
abortControllersRef.current.set(sessionId, abortController);
currentSession = currentSession ?? sessionsRef.current.find((session) => session.id === sessionId) ?? null;
if (isExternalAgent) {
if (!agentConfig) {
updateMessageById(sessionId, assistantMsgId, msg => ({ ...msg, content: 'External agent not found. Please check settings.', executionStatus: 'failed' }));
setStreamingForScope(sessionId, false);
return;
}
try {
const existingExternalSessionId = currentSession?.externalSessionId;
await sendToExternalAgent(sessionId, assistantMsgId, modelPrompt, agentConfig, abortController, modelAttachments, {
scopeType,
scopeTargetId,
existingSessionId: existingExternalSessionId,
updateExternalSessionId: updateSessionExternalSessionId,
historyMessages: buildExternalAgentHistoryMessagesForBridge(currentSession?.messages ?? [], existingExternalSessionId),
terminalSessions,
defaultTargetSession,
providers,
selectedAgentModel: sendSelectedAgentModel,
toolIntegrationMode,
selectedUserSkillSlugs: selectedSkillSlugs,
permissionMode: sendPermissionMode,
});
} catch (err) {
reportStreamError(sessionId, abortController.signal, err);
}
updateLastMessage(sessionId, msg => msg.statusText ? { ...msg, statusText: '' } : msg);
setStreamingForScope(sessionId, false);
abortControllersRef.current.delete(sessionId);
autoTitleSession(sessionId, titleText);
} else {
const toolScope = {
type: scopeType,
targetId: scopeTargetId,
label: scopeLabel,
} as const;
await sendToCattyAgent(sessionId, sendScopeKey, modelPrompt, abortController, currentSession ?? undefined, assistantMsgId, {
activeProvider: sendActiveProvider,
activeModelId: sendActiveModelId,
reasoningEffort: selectedCattyThinking,
scopeType,
scopeTargetId,
scopeLabel,
globalPermissionMode: sendPermissionMode,
commandBlocklist,
commandTimeout,
responseIdleTimeout,
terminalSessions,
webSearchConfig,
getExecutorContext: () => buildExecutorContextForScope(toolScope),
autoTitleSession,
selectedUserSkillSlugs: selectedSkillSlugs,
titleText,
}, modelAttachments.length > 0 ? modelAttachments : undefined);
}
} finally {
setIsSending(false);
endSendForKey(sendGateKey);
if (sessionSendGateKey && sessionSendGateKey !== sendGateKey) {
endSendForKey(sessionSendGateKey);
}
}
}, [
validateNoteMentions, loadCodexConfigModel,
isStreaming, isUndoingLastTurn, activeProvider, effectiveActiveProvider, effectiveActiveModelId, selectedCattyThinking, scopeKey, currentAgentId,
activeModelId, externalAgents,
createSession, addMessageToSession, updateMessageById, updateLastMessage,
setStreamingForScope,
sendToExternalAgent, sendToCattyAgent, reportStreamError, autoTitleSession, t,
abortControllersRef, terminalSessions, defaultTargetSession, providers, selectedAgentModel, updateSessionExternalSessionId,
scopeType, scopeTargetId, scopeHostIds, scopeLabel, commandBlocklist, commandTimeout, responseIdleTimeout, webSearchConfig, buildExecutorContextForScope,
toolIntegrationMode,
clearScopeDraft, showScopeSessionView, setActiveSessionId,
flushDraftText, currentAgentConfig, buildExternalAgentRuntimeModelTarget,
loadSdkRuntimeModelCatalog, applySdkRuntimeModelCatalog, collectCustomModelIds,
]);
const handleCompact = useCallback(async () => {
const session = activeSessionRef.current;
if (
currentAgentId !== 'catty'
|| !session
|| isStreaming
|| isAIChatSessionStreaming(session.id)
|| !effectiveActiveProvider
|| !effectiveActiveModelId.trim()
) {
return;
}
if (!tryBeginSendForKey(session.id)) return;
const controller = new AbortController();
abortControllersRef.current.set(session.id, controller);
setStreamingForScope(session.id, true);
try {
await sendToCattyAgent(
session.id,
scopeKey,
'',
controller,
session,
'',
{
activeProvider: effectiveActiveProvider,
activeModelId: effectiveActiveModelId,
reasoningEffort: selectedCattyThinking,
scopeType,
scopeTargetId,
scopeLabel,
globalPermissionMode,
commandBlocklist,
commandTimeout,
responseIdleTimeout,
terminalSessions,
webSearchConfig,
getExecutorContext: () => buildExecutorContextForScope({
type: scopeType,
targetId: scopeTargetId,
label: scopeLabel,
}),
autoTitleSession: () => {},
selectedUserSkillSlugs: [],
forceCompaction: true,
},
);
} finally {
setStreamingForScope(session.id, false);
if (abortControllersRef.current.get(session.id) === controller) {
abortControllersRef.current.delete(session.id);
}
endSendForKey(session.id);
}
}, [
abortControllersRef,
buildExecutorContextForScope,
commandBlocklist,
commandTimeout,
responseIdleTimeout,
currentAgentId,
effectiveActiveModelId,
effectiveActiveProvider,
selectedCattyThinking,
globalPermissionMode,
isStreaming,
scopeKey,
scopeLabel,
scopeTargetId,
scopeType,
sendToCattyAgent,
setStreamingForScope,
terminalSessions,
webSearchConfig,
]);
const handleSteer = useCallback(async () => {
const sessionId = activeSessionRef.current?.id;
const draft = currentDraftRef.current;
if (!sessionId || !draft || steeringSessionId || !canSteerCurrentTurn) return;
if (!validateNoteMentions(draft.attachments)) return;
const trimmed = draft.text.trim();
const attachments = draft.attachments.map((file) => ({
base64Data: file.base64Data,
mediaType: file.mediaType,
filename: file.filename,
filePath: file.filePath,
terminalSelection: file.terminalSelection,
vaultNoteId: file.vaultNoteId,
vaultNoteTitle: file.vaultNoteTitle,
previewText: file.previewText,
lineCount: file.lineCount,
}));
const hasInlineTextAttachments = attachments.some(isInlineTextAttachment);
if (!trimmed && !hasInlineTextAttachments) return;
const userMessageId = generateId();
const modelPrompt = buildPromptWithTerminalSelectionAttachments(trimmed, attachments);
const modelAttachments = attachments.filter((attachment) => !isInlineTextAttachment(attachment));
setSteerWarnings(current => {
const next = { ...current };
delete next[sessionId];
return next;
});
setSteeringSessionId(sessionId);
try {
const result = await steerExternalAgent({
chatSessionId: sessionId,
userMessageId,
userText: trimmed,
prompt: modelPrompt,
attachments,
attachedImages: modelAttachments,
});
if (result.status === 'accepted') {
if (currentDraftRef.current === draft) clearScopeDraft();
return;
}
if (result.status !== 'cancelled') {
setSteerWarnings(current => ({
...current,
[sessionId]: { reason: result.status, turnKind: result.turnKind },
}));
}
} finally {
setSteeringSessionId(current => current === sessionId ? null : current);
}
}, [
validateNoteMentions, canSteerCurrentTurn, clearScopeDraft, steerExternalAgent, steeringSessionId]);
const stopStreamingForSession = useCallback(async (sessionId: string) => {
const controller = abortControllersRef.current.get(sessionId);
setStreamingForScope(sessionId, false);
updateLastMessage(sessionId, (msg) => ({
...msg,
statusText: '',
executionStatus: msg.executionStatus === 'running' ? 'cancelled' : msg.executionStatus,
}));
await stopAgentTurn({
chatSessionId: sessionId,
abortController: controller,
bridge: getNetcattyBridge(),
reason: 'user',
});
await getAgentRuntime().waitForActiveTurn(sessionId);
if (controller && abortControllersRef.current.get(sessionId) === controller) {
abortControllersRef.current.delete(sessionId);
}
}, [setStreamingForScope, updateLastMessage, abortControllersRef]);
const { addGrant } = useAIPermissionGrantsState();
useEffect(() => {
return registerGrantPersister((rule) => { addGrant(rule); });
}, [addGrant]);
const handleStop = useCallback(() => {
if (!activeSessionId) return;
setSteeringSessionId(current => current === activeSessionId ? null : current);
setSteerWarnings(current => {
const next = { ...current };
delete next[activeSessionId];
return next;
});
stopStreamingForSession(activeSessionId);
}, [activeSessionId, stopStreamingForSession]);
useEffect(() => {
if (!activeSessionId || isStreaming) return;
setSteeringSessionId(current => current === activeSessionId ? null : current);
setSteerWarnings(current => {
if (!current[activeSessionId]) return current;
const next = { ...current };
delete next[activeSessionId];
return next;
});
}, [activeSessionId, isStreaming]);
const steerWarning = useMemo(() => {
if (!activeSessionId) return undefined;
const warning = steerWarnings[activeSessionId];
if (!warning) return undefined;
if (warning.reason === 'not-steerable' && warning.turnKind === 'review') {
return t('ai.codex.steer.notSteerableReview');
}
if (warning.reason === 'not-steerable' && warning.turnKind === 'compact') {
return t('ai.codex.steer.notSteerableCompact');
}
if (warning.reason === 'busy') return t('ai.codex.steer.busy');
if (warning.reason === 'inactive') return t('ai.codex.steer.inactive');
if (warning.reason === 'unsupported') return t('ai.codex.steer.unsupported');
return t('ai.codex.steer.failed');
}, [activeSessionId, steerWarnings, t]);
const handleSelectSession = useCallback(
(sessionId: string) => {
applyHistorySessionSelection(sessionId, {
showSessionView: showScopeSessionView,
setActiveSessionId,
closeHistory: () => setShowHistory(false),
});
},
[setActiveSessionId, showScopeSessionView],
);
// "Fork from here": copy the viewed conversation up to a completed assistant
// response into a new session, switch to it, and confirm via toast. The
// original stays in history. Boundary validation is re-run by the state
// hook; a null result means the fork was refused (already hidden in the UI).
// This panel's scope is passed so the branch is created in the scope viewing
// it, even when the source session's own scope went stale (e.g. a terminal
// chat resumed after reconnecting under a new terminal id).
const handleForkFromMessage = useCallback(async (messageId: string) => {
const sourceSessionId = activeSessionRef.current?.id ?? activeSessionId;
if (!sourceSessionId) return;
// Reject a double-click while the new session is being selected so one
// interaction cannot create two forks.
if (!tryBeginSendForKey(`fork:${sourceSessionId}`)) return;
try {
const fork = await forkSessionFromMessage?.(sourceSessionId, messageId, {
type: scopeType,
targetId: scopeTargetId,
});
if (!fork) return;
applyHistorySessionSelection(fork.id, {
showSessionView: showScopeSessionView,
setActiveSessionId,
closeHistory: () => setShowHistory(false),
});
toast.success(t('ai.chat.forkCreated'));
} finally {
endSendForKey(`fork:${sourceSessionId}`);
}
}, [activeSessionId, forkSessionFromMessage, scopeTargetId, scopeType, setActiveSessionId, showScopeSessionView, t]);
const handleDeleteSession = useCallback(
async (e: React.MouseEvent, sessionId: string) => {
e.stopPropagation();
const deletingActiveSession =
activeSessionId === sessionId
|| persistedSessionId === sessionId
|| (
explicitPanelView?.mode === 'session'
&& explicitPanelView.sessionId === sessionId
);
const deletingLastScopedSession =
historySessions.length === 1 && historySessions[0]?.id === sessionId;
const deletedSessionAgentId =
historySessions.find((session) => session.id === sessionId)?.agentId
?? currentAgentId;
if (abortControllersRef.current.has(sessionId) || streamingSessionIds.has(sessionId)) {
await stopStreamingForSession(sessionId);
}
deleteSession(sessionId, scopeKey);
if (deletingActiveSession || deletingLastScopedSession) {
setShowHistory(false);
ensureScopeDraft(deletedSessionAgentId);
}
},
[
activeSessionId,
abortControllersRef,
currentAgentId,
deleteSession,
ensureScopeDraft,
explicitPanelView,
historySessions,
persistedSessionId,
scopeKey,
stopStreamingForSession,
streamingSessionIds,
],
);
const handleAgentChange = useCallback((agentId: string) => {
showScopeDraftView();
ensureScopeDraft(agentId);
updateScopeDraft(agentId, (draft) => ({
...selectDraftForAgentSwitch(
draft,
agentId,
Boolean(activeSessionRef.current?.messages.length),
),
}));
setShowHistory(false);
}, [ensureScopeDraft, showScopeDraftView, updateScopeDraft]);
// Hidden retained panels keep the composer mounted (parent is `hidden` +
// inert) so reopen does not remount ChatInput. Skip the message list.
return (
<React.Profiler {...getAIPanelProfilerProps('AIChatSidePanel.Active')}>
<div
className="h-full min-h-0"
data-section={isVisible ? undefined : 'ai-chat-panel-retained'}
aria-hidden={isVisible ? undefined : true}
inert={isVisible ? undefined : true}
>
<AIChatPanelContent
parked={!isVisible}
sending={isSending || isUndoingLastTurn}
t={t}
currentAgentId={currentAgentId}
externalAgents={externalAgents}
discoveredAgents={discoveredAgents}
isDiscovering={isDiscovering}
handleAgentChange={handleAgentChange}
handleEnableDiscoveredAgent={handleEnableDiscoveredAgent}
rediscover={rediscover}
handleOpenSettings={handleOpenSettings}
activeSession={activeSession}
handleExport={handleExport}
showHistory={showHistory}
setShowHistory={setShowHistory}
handleNewChat={handleNewChat}
historySessions={historySessions}
activeSessionId={activeSessionId}
handleSelectSession={handleSelectSession}
handleDeleteSession={handleDeleteSession}
onForkFromMessage={handleForkFromMessage}
messages={messages}
isStreaming={isStreaming}
activeCompaction={
activeCompaction?.sessionId === activeSessionId ? activeCompaction : null
}
contextUsage={contextUsage}
canCompact={canCompact}
inputValue={inputValue}
setInputValue={setInputValue}
handleSend={handleSend}
handleCompact={handleCompact}
onRenameSession={handleRenameSession}
onRenameActiveChat={handleRenameSlashCommand}
onListChats={handleListChats}
onRebindChat={handleRebindChat}
panelCommandNote={panelCommandNote}
onDismissPanelCommandNote={() => setPanelCommandNote(null)}
onExportSessions={handleExportSessions}
onImportSessions={handleImportSessions}
canUndoLastTurn={canUndoLastTurn}
handleUndoLastTurn={undoLastTurnInSession ? handleUndoLastTurn : undefined}
handleSteer={handleSteer}
handleStop={handleStop}
canSteer={canSteerCurrentTurn}
isSteering={isSteering}
steerWarning={steerWarning}
lockTurnConfiguration={Boolean(isSending || (isStreaming && isCodexAppServer))}
canSendCurrentAgent={canSendCurrentAgent}
providerDisplayName={providerDisplayName}
modelDisplayName={modelDisplayName}
modelCatalogWarning={!hasCodexCustomConfig
&& runtimeModelWarnings[currentAgentId]
&& runtimeModelWarnings[currentAgentId].cacheKey === buildExternalAgentRuntimeModelTarget(currentAgentConfig)?.cacheKey
? runtimeModelWarnings[currentAgentId].message
: undefined}
modelCatalogLoading={
!hasCodexCustomConfig
&& Boolean(runtimeModelLoading[currentAgentId])
&& runtimeModelLoading[currentAgentId]?.cacheKey === buildExternalAgentRuntimeModelTarget(currentAgentConfig)?.cacheKey
}
modelCatalogSource={modelCatalogSource}
modelCatalogFetchedAt={modelCatalogFetchedAt}
handleModelCatalogRetry={handleRetrySdkModelCatalog}
agentModelPresets={agentModelPresets}
// A managed Codex config's `model` field overrides every selection on
// send, so the manual-entry action would be a silent no-op.
allowCustomModelEntry={allowCustomModelEntry}
selectedAgentModel={selectedAgentModel}
handleAgentModelSelect={handleAgentModelSelect}
cattyConfiguredProviders={cattyConfiguredProviders}
effectiveActiveProvider={effectiveActiveProvider}
effectiveActiveModelId={effectiveActiveModelId}
handleAgentProviderModelSelect={handleAgentProviderModelSelect}
selectedCattyThinking={selectedCattyThinking}
handleCattyThinkingSelect={handleCattyThinkingSelect}
files={files}
addFiles={addFiles}
removeFile={removeFile}
onMentionNote={canMentionNotes ? mentionNote : undefined}
terminalSessions={terminalSessions}
selectedUserSkills={selectedUserSkills}
userSkillOptions={userSkillOptions}
quickMessages={quickMessages}
addSelectedUserSkill={addSelectedUserSkill}
removeSelectedUserSkill={removeSelectedUserSkill}
globalPermissionMode={globalPermissionMode}
setGlobalPermissionMode={setGlobalPermissionMode}
notes={notes}
hosts={hosts}
snippets={snippets}
onOpenVaultNote={onOpenVaultNote}
onOpenVaultHost={onOpenVaultHost}
onOpenVaultSnippet={onOpenVaultSnippet}
onOpenVaultSection={onOpenVaultSection}
/>
</div>
</React.Profiler>
);
};
const AI_CHAT_SIDE_PANEL_AI_STATE_KEYS = [
'sessions',
'activeSessionIdMap',
'draftsByScope',
'panelViewByScope',
'setActiveSessionId',
'ensureDraftForScope',
'updateDraft',
'showDraftView',
'showSessionView',
'clearDraftForScope',
'addDraftFiles',
'removeDraftFile',
'createSession',
'deleteSession',
'updateSessionTitle',
'rebindSessionScope',
'exportSessionsBackup',
'importSessionsBackup',
'updateSessionExternalSessionId',
'addMessageToSession',
'updateLastMessage',
'updateMessageById',
'persistContextCompaction',
'forkSessionFromMessage',
'providers',
'activeProviderId',
'activeModelId',
'defaultAgentId',
'toolIntegrationMode',
'externalAgents',
'setExternalAgents',
'agentModelMap',
'setAgentModel',
'agentProviderMap',
'setAgentProvider',
'agentThinkingMap',
'setAgentThinking',
'updateProvider',
'globalPermissionMode',
'setGlobalPermissionMode',
'commandBlocklist',
'commandTimeout',
'responseIdleTimeout',
'maxIterations',
'webSearchConfig',
'quickMessages',
] as const satisfies readonly (keyof AIChatSidePanelProps)[];
export function aiChatSidePanelPropsAreEqual(
prev: AIChatSidePanelProps,
next: AIChatSidePanelProps,
): boolean {
const prevKeep = shouldKeepAIChatSidePanelMounted(prev);
const nextKeep = shouldKeepAIChatSidePanelMounted(next);
if (!prevKeep && !nextKeep) {
return true;
}
if (prevKeep !== nextKeep) {
return false;
}
if (prev.scopeType !== next.scopeType) return false;
if (prev.scopeTargetId !== next.scopeTargetId) return false;
if (prev.scopeLabel !== next.scopeLabel) return false;
if ((prev.focusedSessionId ?? '') !== (next.focusedSessionId ?? '')) return false;
if ((prev.isVisible ?? true) !== (next.isVisible ?? true)) return false;
if (prev.scopeHostIds !== next.scopeHostIds) return false;
if (prev.terminalSessions !== next.terminalSessions) return false;
if (prev.liveScopeTargetIds !== next.liveScopeTargetIds) return false;
if (prev.workspaces !== next.workspaces) return false;
if (prev.resolveExecutorContext !== next.resolveExecutorContext) return false;
if (prev.notes !== next.notes) return false;
if (prev.hosts !== next.hosts) return false;
if (prev.snippets !== next.snippets) return false;
if (prev.onOpenVaultNote !== next.onOpenVaultNote) return false;
if (prev.onOpenVaultHost !== next.onOpenVaultHost) return false;
if (prev.onOpenVaultSnippet !== next.onOpenVaultSnippet) return false;
if (prev.onOpenVaultSection !== next.onOpenVaultSection) return false;
const scopeKey = `${prev.scopeType}:${prev.scopeTargetId ?? ''}`;
if (
prev.sessions === next.sessions
&& draftsByScopeEqualIgnoringComposerText(prev.draftsByScope, next.draftsByScope, scopeKey)
) {
let restEqual = true;
for (const key of AI_CHAT_SIDE_PANEL_AI_STATE_KEYS) {
if (key === 'sessions' || key === 'draftsByScope') continue;
if (prev[key] !== next[key]) {
restEqual = false;
break;
}
}
if (restEqual) return true;
}
// Sibling stream thrash: full sessions array identity always changes. Only
// exact-scope session object refs matter for this panel's active chat —
// plus the currently selected session, which may be a host-matched history
// resume whose stored scope.targetId is an older terminal.
// Fuzzy history still receives the full list; drawer open forces re-render
// via isVisible / other prop paths when the user actually needs it.
// Keep visibleSessionIds in sync with the live panel so inheritance that
// skips non-history chats does not leave memo pinned to a different id.
const resolveSelectedSessionId = (
props: AIChatSidePanelProps,
): string | null => {
const scopeKey = `${props.scopeType}:${props.scopeTargetId ?? ''}`;
const memberTerminalIds = (props.terminalSessions ?? [])
.map((session) => session.sessionId)
.filter((sessionId): sessionId is string => Boolean(sessionId));
const workspaceMemberTerminalIds = props.scopeType === 'workspace'
? new Set(memberTerminalIds)
: undefined;
const activeTerminalSessionIds = new Set<string>();
for (const [sessionScopeKey, sessionId] of Object.entries(props.activeSessionIdMap) as Array<[string, string | null]>) {
if (!sessionScopeKey.startsWith('terminal:') || !sessionId) continue;
if (sessionScopeKey === scopeKey) continue;
activeTerminalSessionIds.add(sessionId);
}
const visibleSessionIds = new Set(
getScopedHistorySessions(
props.sessions,
props.scopeType,
props.scopeTargetId,
props.scopeHostIds,
activeTerminalSessionIds,
workspaceMemberTerminalIds,
props.activeSessionIdMap,
).map((session) => session.id),
);
return resolveInheritedAIActiveSessionId({
scopeType: props.scopeType,
scopeTargetId: props.scopeTargetId,
activeSessionIdMap: props.activeSessionIdMap,
memberTerminalIds,
preferredTerminalId: props.focusedSessionId,
visibleSessionIds,
});
};
const selectedSessionId = resolveSelectedSessionId(prev)
?? resolveSelectedSessionId(next);
if (!exactScopeAISessionsEqual(
prev.sessions,
next.sessions,
prev.scopeType,
prev.scopeTargetId,
selectedSessionId,
)) {
return false;
}
// History drawer / recent list need create/delete/title/updatedAt chrome.
// Only when the panel is (or becomes) visible so hidden retained panels do
// not re-render on every sibling stream that bumps updatedAt.
const prevVisible = prev.isVisible ?? true;
const nextVisible = next.isVisible ?? true;
if ((prevVisible || nextVisible) && !aiSessionIdSetEqual(prev.sessions, next.sessions)) {
return false;
}
for (const key of AI_CHAT_SIDE_PANEL_AI_STATE_KEYS) {
if (key === 'sessions') continue;
if (key === 'draftsByScope') {
if (!draftsByScopeEqualIgnoringComposerText(prev.draftsByScope, next.draftsByScope, scopeKey)) {
return false;
}
continue;
}
if (prev[key] !== next[key]) return false;
}
return true;
}
const AIChatSidePanel = React.memo(function AIChatSidePanel(props: AIChatSidePanelProps) {
const shouldKeepMounted = shouldKeepAIChatSidePanelMounted(props);
const shouldDelayActivation = shouldKeepMounted && shouldDelayAIChatSidePanelActivation(props);
const activationKey = `${props.scopeType}:${props.scopeTargetId ?? ''}`;
const [activationReady, setActivationReady] = useState(!shouldDelayActivation);
useEffect(() => {
if (!shouldDelayActivation) {
setActivationReady(true);
return undefined;
}
setActivationReady(false);
return schedulePanelActivation(() => setActivationReady(true));
}, [activationKey, shouldDelayActivation]);
if (!shouldKeepMounted) return null;
if (shouldDelayActivation && !activationReady) {
return <AIChatSidePanelPreparing />;
}
// Keep hidden panels alive only when they contain real work (messages, draft
// content, or an active stream). Empty hidden panels can drop their heavy
// input/agent-picker subtree and remount cheaply when shown again.
return <AIChatSidePanelActive {...props} />;
}, aiChatSidePanelPropsAreEqual);
AIChatSidePanel.displayName = 'AIChatSidePanel';
export default AIChatSidePanel;
export { AIChatSidePanel };
export type { AIChatSidePanelProps };