2025-06-19 11:53:56 -07:00
|
|
|
import { isAIMessageSDK, isHumanMessageSDK } from "@/lib/langchain-messages";
|
2025-06-18 11:03:39 -07:00
|
|
|
import { UseStream, useStream } from "@langchain/langgraph-sdk/react";
|
|
|
|
|
import { AssistantMessage } from "../thread/messages/ai";
|
2025-06-19 11:53:56 -07:00
|
|
|
import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
|
2025-06-18 13:12:16 -07:00
|
|
|
import { ManagerGraphState } from "@open-swe/shared/open-swe/manager/types";
|
2025-07-08 08:04:27 -07:00
|
|
|
import { useCancelStream } from "@/hooks/useCancelStream";
|
2025-06-19 11:53:56 -07:00
|
|
|
import {
|
|
|
|
|
isCustomNodeEvent,
|
|
|
|
|
CustomNodeEvent,
|
|
|
|
|
INITIALIZE_NODE_ID,
|
2025-07-03 20:46:11 +00:00
|
|
|
ACCEPTED_PLAN_NODE_ID,
|
2025-06-19 11:53:56 -07:00
|
|
|
mapCustomEventsToSteps,
|
|
|
|
|
} from "@open-swe/shared/open-swe/custom-node-events";
|
2025-07-03 20:46:11 +00:00
|
|
|
import {
|
|
|
|
|
DO_NOT_RENDER_ID_PREFIX,
|
|
|
|
|
PLANNER_GRAPH_ID,
|
|
|
|
|
} from "@open-swe/shared/constants";
|
2025-06-19 11:53:56 -07:00
|
|
|
import { Message } from "@langchain/langgraph-sdk";
|
|
|
|
|
import { InitializeStep } from "../gen-ui/initialize-step";
|
2025-07-03 20:46:11 +00:00
|
|
|
import { AcceptedPlanStep } from "../gen-ui/accepted-plan-step";
|
2025-06-19 10:56:06 -07:00
|
|
|
import { PlannerGraphState } from "@open-swe/shared/open-swe/planner/types";
|
2025-07-03 20:46:11 +00:00
|
|
|
import { GraphState, PlanItem } from "@open-swe/shared/open-swe/types";
|
|
|
|
|
import { HumanResponse } from "@langchain/langgraph/prebuilt";
|
|
|
|
|
|
|
|
|
|
interface AcceptedPlanEventData {
|
|
|
|
|
planTitle: string;
|
|
|
|
|
planItems: PlanItem[];
|
|
|
|
|
interruptType: HumanResponse["type"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AcceptedPlanEvent = CustomNodeEvent & {
|
|
|
|
|
data: AcceptedPlanEventData;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function isAcceptedPlanEvent(
|
|
|
|
|
event: CustomNodeEvent,
|
|
|
|
|
): event is AcceptedPlanEvent {
|
|
|
|
|
const { data } = event;
|
|
|
|
|
return (
|
|
|
|
|
typeof data === "object" &&
|
|
|
|
|
data !== null &&
|
|
|
|
|
typeof data.planTitle === "string" &&
|
|
|
|
|
Array.isArray(data.planItems) &&
|
|
|
|
|
data.planItems.every(
|
|
|
|
|
(item) =>
|
|
|
|
|
typeof item === "object" &&
|
|
|
|
|
item !== null &&
|
|
|
|
|
typeof item.index === "number" &&
|
|
|
|
|
typeof item.plan === "string" &&
|
|
|
|
|
typeof item.completed === "boolean",
|
|
|
|
|
) &&
|
|
|
|
|
(data.interruptType === "accept" || data.interruptType === "edit")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isAcceptedPlanEvents(
|
|
|
|
|
events: CustomNodeEvent[],
|
|
|
|
|
): events is AcceptedPlanEvent[] {
|
|
|
|
|
return events.every(isAcceptedPlanEvent);
|
|
|
|
|
}
|
2025-06-18 11:03:39 -07:00
|
|
|
|
|
|
|
|
interface ActionsRendererProps {
|
|
|
|
|
graphId: string;
|
|
|
|
|
threadId: string;
|
2025-06-18 13:12:16 -07:00
|
|
|
runId?: string;
|
|
|
|
|
setProgrammerSession?: (
|
|
|
|
|
session: ManagerGraphState["programmerSession"],
|
|
|
|
|
) => void;
|
|
|
|
|
programmerSession?: ManagerGraphState["programmerSession"];
|
2025-06-19 10:56:06 -07:00
|
|
|
setSelectedTab?: Dispatch<SetStateAction<"planner" | "programmer">>;
|
2025-07-08 08:04:27 -07:00
|
|
|
onStreamReady: (cancelFn: (() => void) | undefined) => void;
|
2025-06-18 11:03:39 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 11:53:56 -07:00
|
|
|
const getCustomNodeEventsFromMessages = (
|
|
|
|
|
messages?: Message[],
|
|
|
|
|
nodeId?: string,
|
|
|
|
|
): CustomNodeEvent[] => {
|
|
|
|
|
if (!messages) return [];
|
|
|
|
|
|
|
|
|
|
return messages
|
|
|
|
|
.filter((m) => isAIMessageSDK(m))
|
|
|
|
|
.filter((m) => {
|
|
|
|
|
const events = m.additional_kwargs?.customNodeEvents as
|
|
|
|
|
| CustomNodeEvent[]
|
|
|
|
|
| undefined;
|
|
|
|
|
if (!events?.length) return false;
|
|
|
|
|
if (!nodeId) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return events.some((e) => e.nodeId === nodeId);
|
|
|
|
|
})
|
|
|
|
|
.map((m) => m.additional_kwargs?.customNodeEvents as CustomNodeEvent[])
|
|
|
|
|
.flat();
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-19 10:56:06 -07:00
|
|
|
export function ActionsRenderer<State extends PlannerGraphState | GraphState>({
|
2025-06-18 11:03:39 -07:00
|
|
|
graphId,
|
|
|
|
|
threadId,
|
2025-06-18 13:12:16 -07:00
|
|
|
runId,
|
|
|
|
|
setProgrammerSession,
|
|
|
|
|
programmerSession,
|
2025-06-19 10:56:06 -07:00
|
|
|
setSelectedTab,
|
2025-07-08 08:04:27 -07:00
|
|
|
onStreamReady,
|
2025-06-18 11:03:39 -07:00
|
|
|
}: ActionsRendererProps) {
|
2025-06-19 11:53:56 -07:00
|
|
|
const [customNodeEvents, setCustomNodeEvents] = useState<CustomNodeEvent[]>(
|
|
|
|
|
[],
|
|
|
|
|
);
|
2025-06-18 11:03:39 -07:00
|
|
|
const stream = useStream<State>({
|
|
|
|
|
apiUrl: process.env.NEXT_PUBLIC_API_URL,
|
|
|
|
|
assistantId: graphId,
|
|
|
|
|
reconnectOnMount: true,
|
|
|
|
|
threadId,
|
2025-06-19 11:53:56 -07:00
|
|
|
onCustomEvent: (event) => {
|
|
|
|
|
if (isCustomNodeEvent(event)) {
|
|
|
|
|
setCustomNodeEvents((prev) => [...prev, event]);
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-06-18 11:03:39 -07:00
|
|
|
});
|
|
|
|
|
|
2025-07-08 08:04:27 -07:00
|
|
|
const { cancelRun } = useCancelStream<State>({
|
|
|
|
|
stream,
|
|
|
|
|
threadId,
|
|
|
|
|
runId,
|
|
|
|
|
streamName: graphId === "planner" ? "Planner" : "Programmer",
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-19 11:53:56 -07:00
|
|
|
const initializeEvents = customNodeEvents.filter(
|
|
|
|
|
(e) => e.nodeId === INITIALIZE_NODE_ID,
|
|
|
|
|
);
|
2025-07-03 20:46:11 +00:00
|
|
|
|
|
|
|
|
const acceptedPlanEvents = customNodeEvents.filter(
|
|
|
|
|
(e) => e.nodeId === ACCEPTED_PLAN_NODE_ID,
|
|
|
|
|
);
|
|
|
|
|
|
2025-06-19 11:53:56 -07:00
|
|
|
const steps = mapCustomEventsToSteps(initializeEvents);
|
|
|
|
|
const allSuccess =
|
|
|
|
|
steps.length > 0 && steps.every((s) => s.status === "success");
|
|
|
|
|
|
|
|
|
|
let initStatus: "loading" | "generating" | "done" = "generating";
|
|
|
|
|
if (allSuccess) {
|
|
|
|
|
initStatus = "done";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-07-03 20:46:11 +00:00
|
|
|
const allCustomEvents = getCustomNodeEventsFromMessages(stream.messages);
|
|
|
|
|
if (!allCustomEvents?.length) {
|
2025-06-19 11:53:56 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2025-07-03 20:46:11 +00:00
|
|
|
|
|
|
|
|
setCustomNodeEvents((prev) => {
|
|
|
|
|
// If no existing events, set all new events
|
|
|
|
|
if (prev.length === 0) {
|
|
|
|
|
return allCustomEvents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Merge new events with existing ones, avoiding duplicates
|
|
|
|
|
const existingActionIds = new Set(prev.map((e) => e.actionId));
|
|
|
|
|
const newEvents = allCustomEvents.filter(
|
|
|
|
|
(e) => !existingActionIds.has(e.actionId),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (newEvents.length > 0) {
|
|
|
|
|
return [...prev, ...newEvents];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return prev;
|
|
|
|
|
});
|
2025-06-19 11:53:56 -07:00
|
|
|
}, [stream.messages]);
|
|
|
|
|
|
2025-06-18 13:12:16 -07:00
|
|
|
const streamJoined = useRef(false);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!streamJoined.current && runId) {
|
|
|
|
|
streamJoined.current = true;
|
2025-06-19 11:53:56 -07:00
|
|
|
// TODO: If the SDK changes go in, use this instead:
|
|
|
|
|
// stream.joinStream(runId, undefined, { streamMode: ["values", "messages", "custom"]}).catch(console.error);
|
2025-06-18 13:12:16 -07:00
|
|
|
stream.joinStream(runId).catch(console.error);
|
|
|
|
|
}
|
|
|
|
|
}, [runId]);
|
|
|
|
|
|
2025-07-08 08:04:27 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (stream.isLoading) {
|
|
|
|
|
onStreamReady(cancelRun);
|
|
|
|
|
} else {
|
|
|
|
|
onStreamReady(undefined);
|
|
|
|
|
}
|
|
|
|
|
}, [onStreamReady, runId]); // Depend on runId instead of cancelRun to avoid infinite loops
|
|
|
|
|
|
2025-06-19 11:53:56 -07:00
|
|
|
// Filter out human & do not render messages
|
|
|
|
|
const filteredMessages = stream.messages?.filter(
|
|
|
|
|
(m) =>
|
|
|
|
|
!isHumanMessageSDK(m) &&
|
|
|
|
|
!(m.id && m.id.startsWith(DO_NOT_RENDER_ID_PREFIX)),
|
2025-06-18 11:03:39 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: Need a better way to handle this. Not great like this...
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
2025-06-19 10:56:06 -07:00
|
|
|
"programmerSession" in stream.values &&
|
2025-06-18 13:12:16 -07:00
|
|
|
stream.values.programmerSession &&
|
2025-06-19 10:56:06 -07:00
|
|
|
(stream.values.programmerSession.runId !== programmerSession?.runId ||
|
|
|
|
|
stream.values.programmerSession.threadId !==
|
|
|
|
|
programmerSession?.threadId)
|
2025-06-18 11:03:39 -07:00
|
|
|
) {
|
2025-06-19 10:56:06 -07:00
|
|
|
setProgrammerSession?.(stream.values.programmerSession);
|
2025-07-03 20:46:11 +00:00
|
|
|
|
|
|
|
|
// Only switch tabs from the planner ActionsRenderer to ensure proper timing
|
|
|
|
|
// This allows the accepted plan step to be visible before switching
|
|
|
|
|
if (graphId === PLANNER_GRAPH_ID) {
|
|
|
|
|
// Add a small delay to allow the accepted plan step to render first
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setSelectedTab?.("programmer");
|
|
|
|
|
}, 2000);
|
|
|
|
|
}
|
2025-06-18 11:03:39 -07:00
|
|
|
}
|
2025-07-03 20:46:11 +00:00
|
|
|
}, [stream.values, graphId]);
|
2025-06-18 11:03:39 -07:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex w-full flex-col gap-2">
|
2025-06-19 11:53:56 -07:00
|
|
|
{initializeEvents.length > 0 && steps.length > 0 && (
|
|
|
|
|
<InitializeStep
|
|
|
|
|
status={initStatus}
|
|
|
|
|
steps={steps}
|
|
|
|
|
success={allSuccess}
|
|
|
|
|
collapse={initStatus === "done" && allSuccess}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{filteredMessages?.map((m) => (
|
2025-06-18 11:03:39 -07:00
|
|
|
<AssistantMessage
|
|
|
|
|
key={m.id}
|
|
|
|
|
thread={stream as UseStream<Record<string, unknown>>}
|
|
|
|
|
message={m}
|
|
|
|
|
isLoading={false}
|
|
|
|
|
handleRegenerate={() => {}}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2025-07-03 20:46:11 +00:00
|
|
|
{acceptedPlanEvents.length > 0 &&
|
|
|
|
|
isAcceptedPlanEvents(acceptedPlanEvents) && (
|
|
|
|
|
<AcceptedPlanStep
|
|
|
|
|
planTitle={acceptedPlanEvents[0].data.planTitle}
|
|
|
|
|
planItems={acceptedPlanEvents[0].data.planItems}
|
|
|
|
|
interruptType={acceptedPlanEvents[0].data.interruptType}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-06-18 11:03:39 -07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|