Merge pull request #2 from langchain-ai/brace/new-thread
feat: Allow for creating new threads
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -13,23 +13,9 @@ import {
|
||||
ensureToolCallsHaveResponses,
|
||||
} from "@/lib/ensure-tool-responses";
|
||||
import { LangGraphLogoSVG } from "../icons/langgraph";
|
||||
|
||||
// const dummyMessages = [
|
||||
// { type: "human", content: "Hi! What can you do?" },
|
||||
// {
|
||||
// type: "ai",
|
||||
// content: `Hello! I can assist you with a variety of tasks, including:
|
||||
|
||||
// 1. **Answering Questions**: I can provide information on a wide range of topics, from science and history to technology and culture.
|
||||
// 2. **Writing Assistance**: I can help you draft emails, essays, reports, and creative writing pieces.
|
||||
// 3. **Learning Support**: I can explain concepts, help with homework, and provide study tips.
|
||||
// 4. **Language Help**: I can assist with translations, grammar, and vocabulary in multiple languages.
|
||||
// 5. **Recommendations**: I can suggest books, movies, recipes, and more based on your interests.
|
||||
// 6. **General Advice**: I can offer tips on various subjects, including productivity, wellness, and personal development.
|
||||
|
||||
// If you have something specific in mind, feel free to ask!`,
|
||||
// },
|
||||
// ];
|
||||
import { TooltipIconButton } from "./tooltip-icon-button";
|
||||
import { SquarePen } from "lucide-react";
|
||||
import { StringParam, useQueryParam } from "use-query-params";
|
||||
|
||||
function Title({ className }: { className?: string }) {
|
||||
return (
|
||||
@@ -40,11 +26,26 @@ function Title({ className }: { className?: string }) {
|
||||
);
|
||||
}
|
||||
|
||||
function NewThread() {
|
||||
const [_, setThreadId] = useQueryParam("threadId", StringParam);
|
||||
|
||||
return (
|
||||
<TooltipIconButton
|
||||
size="lg"
|
||||
className="p-4"
|
||||
tooltip="New thread"
|
||||
variant="ghost"
|
||||
onClick={() => setThreadId(null)}
|
||||
>
|
||||
<SquarePen className="size-5" />
|
||||
</TooltipIconButton>
|
||||
);
|
||||
}
|
||||
|
||||
export function Thread() {
|
||||
const [input, setInput] = useState("");
|
||||
const [firstTokenReceived, setFirstTokenReceived] = useState(false);
|
||||
const stream = useStreamContext();
|
||||
// const messages = [...dummyMessages, ...stream.messages];
|
||||
const messages = stream.messages;
|
||||
const isLoading = stream.isLoading;
|
||||
const prevMessageLength = useRef(0);
|
||||
@@ -105,7 +106,8 @@ export function Thread() {
|
||||
</div>
|
||||
)}
|
||||
{chatStarted && (
|
||||
<div className="hidden md:flex absolute top-4 right-4">
|
||||
<div className="hidden md:flex items-center gap-3 absolute top-4 right-4">
|
||||
<NewThread />
|
||||
<Title />
|
||||
</div>
|
||||
)}
|
||||
|
||||
13
src/main.tsx
13
src/main.tsx
@@ -2,9 +2,16 @@ import { createRoot } from "react-dom/client";
|
||||
import "./index.css";
|
||||
import App from "./App.tsx";
|
||||
import { StreamProvider } from "./providers/Stream.tsx";
|
||||
import { QueryParamProvider } from "use-query-params";
|
||||
import { ReactRouter6Adapter } from "use-query-params/adapters/react-router-6";
|
||||
import { BrowserRouter } from "react-router-dom";
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StreamProvider>
|
||||
<App />
|
||||
</StreamProvider>,
|
||||
<BrowserRouter>
|
||||
<QueryParamProvider adapter={ReactRouter6Adapter}>
|
||||
<StreamProvider>
|
||||
<App />
|
||||
</StreamProvider>
|
||||
</QueryParamProvider>
|
||||
</BrowserRouter>,
|
||||
);
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import React, { createContext, useContext, ReactNode } from "react";
|
||||
import { useStream } from "@langchain/langgraph-sdk/react";
|
||||
import type { Message } from "@langchain/langgraph-sdk";
|
||||
import { type Message } from "@langchain/langgraph-sdk";
|
||||
import type {
|
||||
UIMessage,
|
||||
RemoveUIMessage,
|
||||
} from "@langchain/langgraph-sdk/react-ui/types";
|
||||
import { useQueryParam, StringParam } from "use-query-params";
|
||||
|
||||
const useTypedStream = useStream<
|
||||
{ messages: Message[]; ui: UIMessage[] },
|
||||
@@ -23,11 +24,18 @@ const StreamContext = createContext<StreamContextType | undefined>(undefined);
|
||||
export const StreamProvider: React.FC<{ children: ReactNode }> = ({
|
||||
children,
|
||||
}) => {
|
||||
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
|
||||
|
||||
const streamValue = useTypedStream({
|
||||
apiUrl: "http://localhost:2024",
|
||||
assistantId: "agent",
|
||||
threadId: threadId ?? null,
|
||||
onThreadId: setThreadId,
|
||||
});
|
||||
|
||||
console.log("threadId", threadId);
|
||||
console.log("streamValue", streamValue.values);
|
||||
|
||||
return (
|
||||
<StreamContext.Provider value={streamValue}>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user