2025-03-03 12:40:24 -08:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2025-03-04 15:37:40 +01:00
|
|
|
import { ReactNode, useEffect, useRef } from "react";
|
2025-03-03 12:31:27 -08:00
|
|
|
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-04 15:37:40 +01:00
|
|
|
import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
|
2025-03-03 12:31:27 -08:00
|
|
|
|
2025-03-03 13:51:41 -08:00
|
|
|
function NewThread() {
|
2025-03-04 15:37:40 +01:00
|
|
|
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
|
|
|
|
|
if (!threadId) return null;
|
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-04 15:37:40 +01:00
|
|
|
function StickyToBottomContent(props: {
|
|
|
|
|
content: ReactNode;
|
|
|
|
|
footer?: ReactNode;
|
|
|
|
|
className?: string;
|
|
|
|
|
contentClassName?: string;
|
|
|
|
|
}) {
|
|
|
|
|
const context = useStickToBottomContext();
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
ref={context.scrollRef}
|
|
|
|
|
style={{ width: "100%", height: "100%" }}
|
|
|
|
|
className={props.className}
|
|
|
|
|
>
|
|
|
|
|
<div ref={context.contentRef} className={props.contentClassName}>
|
|
|
|
|
{props.content}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{props.footer}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
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 (
|
2025-03-04 15:37:40 +01:00
|
|
|
<div className="flex flex-col w-full h-screen overflow-hidden">
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"grow grid grid-rows-[auto_1fr]",
|
|
|
|
|
!chatStarted && "grid-rows-[1fr]",
|
2025-03-03 12:51:21 -08:00
|
|
|
)}
|
2025-03-04 15:37:40 +01:00
|
|
|
>
|
2025-03-03 12:51:21 -08:00
|
|
|
{chatStarted && (
|
2025-03-04 15:37:40 +01:00
|
|
|
<div className="flex items-center justify-between gap-3 p-2 pl-4 z-10 relative">
|
2025-03-04 15:43:35 +01:00
|
|
|
<div className="flex gap-2 items-center">
|
|
|
|
|
<LangGraphLogoSVG width={32} height={32} />
|
|
|
|
|
<h1 className="text-xl font-medium">LangGraph Chat</h1>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-03-04 15:37:40 +01:00
|
|
|
<NewThread />
|
|
|
|
|
|
|
|
|
|
<div className="absolute inset-x-0 top-full h-5 bg-gradient-to-b from-background to-background/0" />
|
2025-03-03 12:51:21 -08:00
|
|
|
</div>
|
|
|
|
|
)}
|
2025-03-03 12:31:27 -08:00
|
|
|
|
2025-03-04 15:37:40 +01:00
|
|
|
<StickToBottom className="relative">
|
|
|
|
|
<StickyToBottomContent
|
|
|
|
|
className={cn(
|
|
|
|
|
"absolute inset-0",
|
|
|
|
|
!chatStarted && "flex flex-col items-stretch mt-[25vh]",
|
|
|
|
|
chatStarted && "grid grid-rows-[1fr_auto]",
|
|
|
|
|
)}
|
|
|
|
|
contentClassName="pt-8 pb-16 px-4 max-w-4xl mx-auto flex flex-col gap-4 w-full empty:hidden"
|
|
|
|
|
content={
|
|
|
|
|
<>
|
|
|
|
|
{renderMessages.map((message, index) =>
|
|
|
|
|
message.type === "human" ? (
|
|
|
|
|
<HumanMessage
|
|
|
|
|
key={
|
|
|
|
|
"id" in message
|
|
|
|
|
? message.id
|
|
|
|
|
: `${message.type}-${index}`
|
|
|
|
|
}
|
|
|
|
|
message={message}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<AssistantMessage
|
|
|
|
|
key={
|
|
|
|
|
"id" in message
|
|
|
|
|
? message.id
|
|
|
|
|
: `${message.type}-${index}`
|
|
|
|
|
}
|
|
|
|
|
message={message}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
handleRegenerate={handleRegenerate}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
)}
|
|
|
|
|
{isLoading && !firstTokenReceived && (
|
|
|
|
|
<AssistantMessageLoading />
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
footer={
|
|
|
|
|
<div className="sticky flex flex-col items-center gap-8 bottom-8 px-4">
|
2025-03-04 15:43:35 +01:00
|
|
|
{!chatStarted && (
|
|
|
|
|
<div className="flex gap-3 items-center">
|
|
|
|
|
<LangGraphLogoSVG className="flex-shrink-0 h-8" />
|
|
|
|
|
<h1 className="text-2xl font-medium">LangGraph Chat</h1>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div className="bg-background rounded-2xl border shadow-md mx-auto w-full max-w-4xl">
|
2025-03-04 15:37:40 +01:00
|
|
|
<form
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
className="grid grid-rows-[1fr_auto] gap-2 max-w-4xl mx-auto"
|
|
|
|
|
>
|
|
|
|
|
<Input
|
|
|
|
|
type="text"
|
|
|
|
|
value={input}
|
|
|
|
|
onChange={(e) => setInput(e.target.value)}
|
|
|
|
|
placeholder="Type your message..."
|
|
|
|
|
className="px-4 py-6 border-none bg-transparent shadow-none ring-0 outline-none focus:outline-none focus:ring-0"
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center justify-end p-2 pt-0">
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={isLoading || !input.trim()}
|
|
|
|
|
>
|
|
|
|
|
Send
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
2025-03-03 12:31:27 -08:00
|
|
|
/>
|
2025-03-04 15:37:40 +01:00
|
|
|
</StickToBottom>
|
2025-03-03 12:31:27 -08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|