Fix stale closure bug in agent-input-area message handlers
Replace direct `agentId` prop usage with `agentIdRef.current` to prevent messages from being sent to the wrong agent during navigation. Fixed 3 instances: - handleSendMessage: Line 383 - handleSendQueuedNow: Line 869 - handleCancelAgent: Line 834 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -101,12 +101,12 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) {
|
|||||||
}),
|
}),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
const noopSendAgentMessage = useCallback<SessionContextValue["sendAgentMessage"]>(async () => {}, []);
|
const noopSendAgentMessage = useCallback(async () => {}, []);
|
||||||
const noopSendAgentAudio = useCallback<SessionContextValue["sendAgentAudio"]>(async () => {}, []);
|
const noopSendAgentAudio = useCallback(async () => {}, []);
|
||||||
const noopCancelAgentRun = useCallback<SessionContextValue["cancelAgentRun"]>(() => {}, []);
|
const noopCancelAgentRun = useCallback(() => {}, []);
|
||||||
const noopGetDraftInput = useCallback<SessionContextValue["getDraftInput"]>(() => undefined, []);
|
const noopGetDraftInput = useCallback(() => undefined, []);
|
||||||
const noopSaveDraftInput = useCallback<SessionContextValue["saveDraftInput"]>(() => {}, []);
|
const noopSaveDraftInput = useCallback(() => {}, []);
|
||||||
const noopSetQueuedMessages = useCallback<SessionContextValue["setQueuedMessages"]>(() => {}, []);
|
const noopSetQueuedMessages = useCallback(() => {}, []);
|
||||||
const ws = session?.ws ?? inertWebSocket;
|
const ws = session?.ws ?? inertWebSocket;
|
||||||
const sendAgentMessage = session?.sendAgentMessage ?? noopSendAgentMessage;
|
const sendAgentMessage = session?.sendAgentMessage ?? noopSendAgentMessage;
|
||||||
const sendAgentAudio = session?.sendAgentAudio ?? noopSendAgentAudio;
|
const sendAgentAudio = session?.sendAgentAudio ?? noopSendAgentAudio;
|
||||||
@@ -380,7 +380,7 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) {
|
|||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sendAgentMessage(agentId, message, imageAttachments);
|
await sendAgentMessage(agentIdRef.current, message, imageAttachments);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[AgentInput] Failed to send message:", error);
|
console.error("[AgentInput] Failed to send message:", error);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -662,7 +662,7 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) {
|
|||||||
|
|
||||||
const updateQueue = useCallback(
|
const updateQueue = useCallback(
|
||||||
(updater: (current: QueuedMessage[]) => QueuedMessage[]) => {
|
(updater: (current: QueuedMessage[]) => QueuedMessage[]) => {
|
||||||
setQueuedMessagesByAgent((prev) => {
|
setQueuedMessagesByAgent((prev: Map<string, QueuedMessage[]>) => {
|
||||||
const next = new Map(prev);
|
const next = new Map(prev);
|
||||||
next.set(agentId, updater(prev.get(agentId) ?? []));
|
next.set(agentId, updater(prev.get(agentId) ?? []));
|
||||||
return next;
|
return next;
|
||||||
@@ -831,7 +831,7 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsCancellingAgent(true);
|
setIsCancellingAgent(true);
|
||||||
cancelAgentRun(agentId);
|
cancelAgentRun(agentIdRef.current);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleQueueCurrentInput() {
|
function handleQueueCurrentInput() {
|
||||||
@@ -866,7 +866,7 @@ export function AgentInputArea({ agentId, serverId }: AgentInputAreaProps) {
|
|||||||
|
|
||||||
// Cancels current agent run before sending queued prompt
|
// Cancels current agent run before sending queued prompt
|
||||||
handleCancelAgent();
|
handleCancelAgent();
|
||||||
await sendAgentMessage(agentId, item.text, item.images);
|
await sendAgentMessage(agentIdRef.current, item.text, item.images);
|
||||||
}
|
}
|
||||||
|
|
||||||
const realtimeButton = (
|
const realtimeButton = (
|
||||||
|
|||||||
Reference in New Issue
Block a user