2025-03-03 12:40:24 -08:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2025-03-03 12:31:27 -08:00
|
|
|
import { useEffect, useRef } from "react";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import { useStreamContext } from "@/providers/Stream";
|
|
|
|
|
import { useState, FormEvent } from "react";
|
|
|
|
|
import { Input } from "../ui/input";
|
|
|
|
|
import { Button } from "../ui/button";
|
2025-03-03 13:24:24 -08:00
|
|
|
import { Checkpoint, Message } from "@langchain/langgraph-sdk";
|
2025-03-03 12:31:27 -08:00
|
|
|
import { AssistantMessage, AssistantMessageLoading } from "./messages/ai";
|
|
|
|
|
import { HumanMessage } from "./messages/human";
|
2025-03-03 12:40:24 -08:00
|
|
|
import {
|
|
|
|
|
DO_NOT_RENDER_ID_PREFIX,
|
|
|
|
|
ensureToolCallsHaveResponses,
|
|
|
|
|
} from "@/lib/ensure-tool-responses";
|
2025-03-03 12:51:21 -08:00
|
|
|
import { LangGraphLogoSVG } from "../icons/langgraph";
|
2025-03-03 13:13:57 -08:00
|
|
|
import { TooltipIconButton } from "./tooltip-icon-button";
|
|
|
|
|
import { SquarePen } from "lucide-react";
|
|
|
|
|
import { StringParam, useQueryParam } from "use-query-params";
|
2025-03-03 12:31:27 -08:00
|
|
|
|
2025-03-03 12:51:21 -08:00
|
|
|
function Title({ className }: { className?: string }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn("flex gap-2 items-center", className)}>
|
|
|
|
|
<LangGraphLogoSVG width={32} height={32} />
|
|
|
|
|
<h1 className="text-xl font-medium">LangGraph Chat</h1>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-03 13:51:41 -08:00
|
|
|
function NewThread() {
|
2025-03-04 14:37:39 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2025-03-03 13:51:41 -08:00
|
|
|
const [_, setThreadId] = useQueryParam("threadId", StringParam);
|
2025-03-03 13:13:57 -08:00
|
|
|
|
|
|
|
|
return (
|
2025-03-03 13:51:41 -08:00
|
|
|
<TooltipIconButton
|
|
|
|
|
size="lg"
|
|
|
|
|
className="p-4"
|
|
|
|
|
tooltip="New thread"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={() => setThreadId(null)}
|
|
|
|
|
>
|
|
|
|
|
<SquarePen className="size-5" />
|
2025-03-03 13:13:57 -08:00
|
|
|
</TooltipIconButton>
|
2025-03-03 13:51:41 -08:00
|
|
|
);
|
2025-03-03 13:13:57 -08:00
|
|
|
}
|
|
|
|
|
|
2025-03-03 12:31:27 -08:00
|
|
|
export function Thread() {
|
|
|
|
|
const [input, setInput] = useState("");
|
|
|
|
|
const [firstTokenReceived, setFirstTokenReceived] = useState(false);
|
|
|
|
|
const stream = useStreamContext();
|
|
|
|
|
const messages = stream.messages;
|
|
|
|
|
const isLoading = stream.isLoading;
|
|
|
|
|
|
2025-03-04 14:12:56 +01:00
|
|
|
const prevMessageLength = useRef(0);
|
2025-03-04 14:44:55 +01:00
|
|
|
|
|
|
|
|
// TODO: this should be part of the useStream hook
|
2025-03-03 12:31:27 -08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
messages.length !== prevMessageLength.current &&
|
|
|
|
|
messages?.length &&
|
|
|
|
|
messages[messages.length - 1].type === "ai"
|
|
|
|
|
) {
|
|
|
|
|
setFirstTokenReceived(true);
|
|
|
|
|
prevMessageLength.current = messages.length;
|
|
|
|
|
}
|
|
|
|
|
}, [messages]);
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (e: FormEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
if (!input.trim() || isLoading) return;
|
|
|
|
|
setFirstTokenReceived(false);
|
|
|
|
|
|
2025-03-03 12:40:24 -08:00
|
|
|
const newHumanMessage: Message = {
|
|
|
|
|
id: uuidv4(),
|
|
|
|
|
type: "human",
|
|
|
|
|
content: input,
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-04 13:45:02 +01:00
|
|
|
const toolMessages = ensureToolCallsHaveResponses(stream.messages);
|
2025-03-03 12:31:27 -08:00
|
|
|
stream.submit(
|
2025-03-04 13:45:02 +01:00
|
|
|
{ messages: [...toolMessages, newHumanMessage] },
|
2025-03-04 14:44:55 +01:00
|
|
|
{
|
|
|
|
|
streamMode: ["values"],
|
|
|
|
|
optimisticValues: (prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
messages: [
|
|
|
|
|
...(prev.messages ?? []),
|
|
|
|
|
...toolMessages,
|
|
|
|
|
newHumanMessage,
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
},
|
2025-03-03 12:31:27 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setInput("");
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-03 13:24:24 -08:00
|
|
|
const handleRegenerate = (
|
2025-03-04 14:12:56 +01:00
|
|
|
parentCheckpoint: Checkpoint | null | undefined,
|
2025-03-03 13:24:24 -08:00
|
|
|
) => {
|
|
|
|
|
// Do this so the loading state is correct
|
|
|
|
|
prevMessageLength.current = prevMessageLength.current - 1;
|
|
|
|
|
setFirstTokenReceived(false);
|
|
|
|
|
stream.submit(undefined, {
|
|
|
|
|
checkpoint: parentCheckpoint,
|
|
|
|
|
streamMode: ["values"],
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-03 12:31:27 -08:00
|
|
|
const chatStarted = isLoading || messages.length > 0;
|
2025-03-03 12:40:24 -08:00
|
|
|
const renderMessages = messages.filter(
|
2025-03-04 14:12:56 +01:00
|
|
|
(m) => !m.id?.startsWith(DO_NOT_RENDER_ID_PREFIX),
|
2025-03-03 12:40:24 -08:00
|
|
|
);
|
2025-03-03 12:31:27 -08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2025-03-04 14:12:56 +01:00
|
|
|
className={cn("flex flex-col w-full h-full", chatStarted && "relative")}
|
2025-03-03 12:31:27 -08:00
|
|
|
>
|
|
|
|
|
<div className={cn("flex-1 px-4", chatStarted ? "pb-28" : "mt-64")}>
|
2025-03-03 12:51:21 -08:00
|
|
|
{!chatStarted && (
|
|
|
|
|
<div className="flex justify-center">
|
|
|
|
|
<Title className="mb-12" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{chatStarted && (
|
2025-03-03 13:51:41 -08:00
|
|
|
<div className="hidden md:flex items-center gap-3 absolute top-4 right-4">
|
|
|
|
|
<NewThread />
|
2025-03-03 12:51:21 -08:00
|
|
|
<Title />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-03-03 12:31:27 -08:00
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex flex-col gap-4 max-w-4xl w-full mx-auto mt-12 overflow-y-auto",
|
2025-03-04 14:12:56 +01:00
|
|
|
!chatStarted && "hidden",
|
2025-03-03 12:31:27 -08:00
|
|
|
)}
|
|
|
|
|
>
|
2025-03-03 12:40:24 -08:00
|
|
|
{renderMessages.map((message, index) =>
|
2025-03-03 12:31:27 -08:00
|
|
|
message.type === "human" ? (
|
|
|
|
|
<HumanMessage
|
|
|
|
|
key={"id" in message ? message.id : `${message.type}-${index}`}
|
2025-03-04 13:45:02 +01:00
|
|
|
message={message}
|
2025-03-03 12:31:27 -08:00
|
|
|
isLoading={isLoading}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<AssistantMessage
|
|
|
|
|
key={"id" in message ? message.id : `${message.type}-${index}`}
|
2025-03-04 13:45:02 +01:00
|
|
|
message={message}
|
2025-03-03 12:31:27 -08:00
|
|
|
isLoading={isLoading}
|
2025-03-03 13:24:24 -08:00
|
|
|
handleRegenerate={handleRegenerate}
|
2025-03-03 12:31:27 -08:00
|
|
|
/>
|
2025-03-04 14:12:56 +01:00
|
|
|
),
|
2025-03-03 12:31:27 -08:00
|
|
|
)}
|
|
|
|
|
{isLoading && !firstTokenReceived && <AssistantMessageLoading />}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
2025-03-04 14:12:56 +01:00
|
|
|
"bg-background rounded-2xl border shadow-md mx-auto w-full max-w-4xl",
|
|
|
|
|
chatStarted && "fixed bottom-6 left-0 right-0",
|
2025-03-03 12:31:27 -08:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={handleSubmit}
|
2025-03-04 14:12:56 +01:00
|
|
|
className="grid grid-rows-[1fr,auto] gap-2 max-w-4xl mx-auto"
|
2025-03-03 12:31:27 -08:00
|
|
|
>
|
|
|
|
|
<Input
|
|
|
|
|
type="text"
|
|
|
|
|
value={input}
|
|
|
|
|
onChange={(e) => setInput(e.target.value)}
|
|
|
|
|
placeholder="Type your message..."
|
2025-03-04 14:12:56 +01:00
|
|
|
className="px-4 py-6 border-none bg-transparent shadow-none ring-0 outline-none focus:outline-none focus:ring-0"
|
2025-03-03 12:31:27 -08:00
|
|
|
/>
|
2025-03-04 13:45:02 +01:00
|
|
|
|
2025-03-04 14:12:56 +01:00
|
|
|
<div className="flex items-center justify-end p-2 pt-0">
|
|
|
|
|
<Button type="submit" disabled={isLoading || !input.trim()}>
|
|
|
|
|
Send
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2025-03-03 12:31:27 -08:00
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|