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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Box, ContextMenu, Flex } from "@radix-ui/themes";
import {
type AcpMessage,
isJsonRpcNotification,
isJsonRpcRequest,
isJsonRpcResponse,
} from "@shared/types/session-events";
import { useCallback, useMemo, useState } from "react";
import { useHotkeys } from "react-hotkeys-hook";
Expand Down Expand Up @@ -58,28 +58,38 @@ export function SessionView({
]);

const latestPlan = useMemo((): Plan | null => {
// Find the index of the last user prompt - only show plans after that
let lastPromptIndex = -1;
let planIndex = -1;
let plan: Plan | null = null;
let responseIndex = -1;

// Find the most recent plan and response in one pass
for (let i = events.length - 1; i >= 0; i--) {
const msg = events[i].message;
if (isJsonRpcRequest(msg) && msg.method === "session/prompt") {
lastPromptIndex = i;
break;

if (responseIndex === -1 && isJsonRpcResponse(msg)) {
responseIndex = i;
}
}

// Search for plans only after the last user prompt
for (let i = events.length - 1; i > lastPromptIndex; i--) {
const msg = events[i].message;
if (isJsonRpcNotification(msg) && msg.method === "session/update") {
if (
planIndex === -1 &&
isJsonRpcNotification(msg) &&
msg.method === "session/update"
) {
const update = (msg.params as { update?: { sessionUpdate?: string } })
?.update;
if (update?.sessionUpdate === "plan") {
return update as Plan;
planIndex = i;
plan = update as Plan;
}
}

if (planIndex !== -1 && responseIndex !== -1) break;
}
return null;

// Plan is stale if the most recent response came after it (turn completed)
if (responseIndex > planIndex) return null;

return plan;
}, [events]);

const handleSubmit = useCallback(
Expand Down