Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions site/src/pages/AgentsPage/AgentChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const buildAttachmentMediaTypes = (
};

const AgentChatPage: FC = () => {
const { agentId } = useParams<{ agentId: string }>();
const { agentId } = useParams() as { agentId: string };
Comment thread
jakehwll marked this conversation as resolved.
Resolved
const {
chatErrorReasons,
setChatErrorReason,
Expand All @@ -174,19 +174,16 @@ const AgentChatPage: FC = () => {
const isEditReasoningEffortDirtyRef = useRef(false);
const chatInputRef = useRef<ChatMessageInputRef | null>(null);
const inputValueRef = useRef(
agentId
? parseStoredDraft(
localStorage.getItem(`${draftInputStorageKeyPrefix}${agentId}`),
).text
: "",
parseStoredDraft(
localStorage.getItem(`${draftInputStorageKeyPrefix}${agentId}`),
).text,
);

const { showSidebarPanel, handleSetShowSidebarPanel } =
useAgentChatPanelPreference();

const chatQuery = useQuery({
...openChat(agentId ?? ""),
enabled: Boolean(agentId),
...openChat(agentId),
// Poll while the chat runs (this override replaces openChat's
// interval, and queued_for_capacity depends on the poll) or while
// the binding is unresolved: repair happens on chat reads and watch
Expand All @@ -210,15 +207,15 @@ const AgentChatPage: FC = () => {
refetchIntervalInBackground: false,
});
const chatOrganizationId = chatQuery.data?.organization_id ?? "";
const chatMessagesQuery = useInfiniteQuery({
...chatMessagesForInfiniteScroll(agentId ?? ""),
enabled: Boolean(agentId),
});
const chatMessagesQuery = useInfiniteQuery(
chatMessagesForInfiniteScroll(agentId),
);
const parentChatID = getParentChatID(chatQuery.data);
const parentChatQuery = useQuery({
...chatById(parentChatID ?? ""),
enabled: Boolean(parentChatID),
});
const parentChat = parentChatQuery.data;
const workspaceId = chatQuery.data?.workspace_id;
const chatAgentId = chatQuery.data?.agent_id;
const workspaceQuery = useQuery({
Expand Down Expand Up @@ -340,7 +337,6 @@ const AgentChatPage: FC = () => {
if (chat?.mcp_server_ids) {
return chat.mcp_server_ids;
}
// Check for a previously saved selection in localStorage.
const saved = chatOrganizationId
? getSavedMCPSelection(
chatOrganizationId,
Expand Down Expand Up @@ -395,29 +391,29 @@ const AgentChatPage: FC = () => {
// spread). Keeping no intermediate variable prevents future code
// from accidentally closing over the unstable object.
const { isPending: isSendPending, mutateAsync: sendMessage } = useMutation(
createChatMessage(queryClient, agentId ?? ""),
createChatMessage(queryClient, agentId),
);
const { isPending: isEditPending, mutateAsync: editMessage } = useMutation(
editChatMessage(queryClient, agentId ?? ""),
editChatMessage(queryClient, agentId),
);
const { isPending: isInterruptPending, mutateAsync: interrupt } = useMutation(
interruptChat(queryClient, agentId ?? ""),
interruptChat(queryClient, agentId),
);
const { isPending: isCompactPending, mutateAsync: compact } = useMutation(
compactChat(queryClient, agentId ?? ""),
compactChat(queryClient, agentId),
);
const { isPending: isClearPending, mutateAsync: clearChatContext } =
useMutation(clearChat(queryClient, agentId ?? ""));
useMutation(clearChat(queryClient, agentId));
const personalSkillsQuery = useQuery({
...userSkills(),
staleTime: 60_000,
});
const chatWorkspaceSkills = workspaceSkillsFromChat(chatQuery.data);
const { mutateAsync: deleteQueuedMessage } = useMutation(
deleteChatQueuedMessage(queryClient, agentId ?? ""),
deleteChatQueuedMessage(queryClient, agentId),
);
const { mutateAsync: promoteQueuedMessage } = useMutation(
promoteChatQueuedMessage(queryClient, agentId ?? ""),
promoteChatQueuedMessage(queryClient, agentId),
);
const updateChatWorkspaceBase = updateChatWorkspace(queryClient);
const {
Expand Down Expand Up @@ -497,7 +493,7 @@ const AgentChatPage: FC = () => {
const persistedError = getPersistedDetailError({
chatStatus: liveChatStatus,
chatRecord: chat,
cachedError: agentId ? chatErrorReasons[agentId] : undefined,
cachedError: chatErrorReasons[agentId],
});

// Git watcher: runs regardless of sidebar visibility, but only
Expand Down Expand Up @@ -624,7 +620,7 @@ const AgentChatPage: FC = () => {
const isWorkspaceLoading =
workspacesQuery.isLoading || isUpdateChatWorkspacePending;
const handlePlanModeToggle = (enabled: boolean) => {
if (!agentId || enabled === planModeEnabled) {
if (enabled === planModeEnabled) {
return;
}
trackPendingChatSettingSync(
Expand All @@ -637,7 +633,7 @@ const AgentChatPage: FC = () => {
};

const handleRequestError = (error: unknown): void => {
if (!agentId || !isApiError(error)) {
if (!isApiError(error)) {
return;
}
const detail = error.response?.data?.detail?.trim() || undefined;
Expand All @@ -656,14 +652,14 @@ const AgentChatPage: FC = () => {
};

const handleInterrupt = () => {
if (!agentId || isInterruptPending) {
if (isInterruptPending) {
return;
}
void interrupt();
};

const handleWorkspaceChange = (nextWorkspaceId: string | null) => {
if (!agentId || nextWorkspaceId === selectedWorkspaceId) {
if (nextWorkspaceId === selectedWorkspaceId) {
return;
}
trackPendingChatSettingSync(
Expand Down Expand Up @@ -731,7 +727,7 @@ const AgentChatPage: FC = () => {
) {
return;
}
chatReadyFiredRef.current = agentId ?? null;
chatReadyFiredRef.current = agentId;
onChatReady();
}, [
onChatReady,
Expand Down Expand Up @@ -825,7 +821,7 @@ const AgentChatPage: FC = () => {
attachments,
useComposerContent,
});
if (!hasContent || isSubmissionPending || !agentId || !hasModelOptions) {
if (!hasContent || isSubmissionPending || !hasModelOptions) {
return;
}
// Wait for chat-setting mutations to settle before sending so the
Expand Down Expand Up @@ -1148,7 +1144,7 @@ const AgentChatPage: FC = () => {
}}
/>
)
) : !chat || !chatMessagesQuery.data?.pages?.length || !agentId ? (
) : !chat || !chatMessagesQuery.data?.pages?.length ? (
<AgentChatPageNotFoundView />
) : (
<AgentChatPageView
Expand All @@ -1158,7 +1154,7 @@ const AgentChatPage: FC = () => {
preferencesQuery.data?.agent_chat_send_shortcut,
preferencesQuery.isLoading,
)}
parentChat={parentChatQuery.data}
parentChat={parentChat}
persistedError={persistedError}
canShareChat={canShareChat}
workspace={workspace}
Expand Down Expand Up @@ -1231,6 +1227,9 @@ const AgentChatPage: FC = () => {
// editing, queries, scroller) cleanly.
const KeyedAgentChatPage: FC = () => {
const { agentId } = useParams<{ agentId: string }>();
if (!agentId) {
return <AgentChatPageNotFoundView />;
}
return (
<MessageScroller.Provider
key={agentId}
Expand Down
Loading