Files
agent-chat-ui/src/providers/Stream.tsx

188 lines
5.9 KiB
TypeScript
Raw Normal View History

2025-03-04 17:58:46 +01:00
import React, { createContext, useContext, ReactNode, useState } from "react";
2025-02-27 14:08:24 -08:00
import { useStream } from "@langchain/langgraph-sdk/react";
2025-03-03 13:51:41 -08:00
import { type Message } from "@langchain/langgraph-sdk";
2025-02-27 14:08:24 -08:00
import type {
UIMessage,
RemoveUIMessage,
} from "@langchain/langgraph-sdk/react-ui/types";
2025-03-03 13:51:41 -08:00
import { useQueryParam, StringParam } from "use-query-params";
2025-03-04 17:38:33 +01:00
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { LangGraphLogoSVG } from "@/components/icons/langgraph";
import { Label } from "@/components/ui/label";
import { ArrowRight } from "lucide-react";
2025-03-04 17:58:46 +01:00
import { PasswordInput } from "@/components/ui/password-input";
2025-02-27 14:08:24 -08:00
2025-03-02 20:22:36 +01:00
const useTypedStream = useStream<
{ messages: Message[]; ui: UIMessage[] },
{
UpdateType: {
2025-02-27 14:08:24 -08:00
messages?: Message[] | Message | string;
ui?: (UIMessage | RemoveUIMessage)[] | UIMessage | RemoveUIMessage;
2025-03-02 20:22:36 +01:00
};
CustomUpdateType: UIMessage | RemoveUIMessage;
2025-03-02 20:22:36 +01:00
}
2025-02-27 14:08:24 -08:00
>;
2025-03-02 20:22:36 +01:00
type StreamContextType = ReturnType<typeof useTypedStream>;
2025-02-27 14:08:24 -08:00
const StreamContext = createContext<StreamContextType | undefined>(undefined);
2025-03-04 17:38:33 +01:00
const StreamSession = ({
2025-02-27 14:08:24 -08:00
children,
2025-03-04 17:58:46 +01:00
apiKey,
2025-03-04 17:38:33 +01:00
apiUrl,
assistantId,
}: {
children: ReactNode;
2025-03-04 17:58:46 +01:00
apiKey: string | null;
2025-03-04 17:38:33 +01:00
apiUrl: string;
assistantId: string;
2025-02-27 14:08:24 -08:00
}) => {
2025-03-03 13:51:41 -08:00
const [threadId, setThreadId] = useQueryParam("threadId", StringParam);
2025-03-02 20:22:36 +01:00
const streamValue = useTypedStream({
2025-03-04 17:38:33 +01:00
apiUrl,
2025-03-04 17:58:46 +01:00
apiKey: apiKey ?? undefined,
2025-03-04 17:38:33 +01:00
assistantId,
2025-03-04 13:55:37 +01:00
threadId: threadId ?? null,
2025-03-03 13:13:57 -08:00
onThreadId: setThreadId,
2025-02-27 14:08:24 -08:00
});
return (
<StreamContext.Provider value={streamValue}>
{children}
</StreamContext.Provider>
);
};
2025-03-04 17:38:33 +01:00
export const StreamProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const [apiUrl, setApiUrl] = useQueryParam("apiUrl", StringParam);
2025-03-04 17:58:46 +01:00
const [apiKey, _setApiKey] = useState(() => {
try {
const key = window.localStorage.getItem("lg:chat:apiKey");
return key || null;
} catch {
// pass
}
return null;
});
const setApiKey = (key: string) => {
window.localStorage.setItem("lg:chat:apiKey", key);
_setApiKey(key);
};
2025-03-04 17:38:33 +01:00
const [assistantId, setAssistantId] = useQueryParam(
"assistantId",
StringParam,
);
if (!apiUrl || !assistantId) {
return (
2025-03-04 17:58:46 +01:00
<div className="flex items-center justify-center min-h-screen w-full p-4">
2025-03-04 17:38:33 +01:00
<div className="animate-in fade-in-0 zoom-in-95 flex flex-col border bg-background shadow-lg rounded-lg max-w-2xl">
<div className="flex flex-col gap-2 mt-14 p-6 border-b">
<div className="flex items-start flex-col gap-2">
<LangGraphLogoSVG className="h-7" />
<h1 className="text-xl font-semibold tracking-tight">
LangGraph Chat
</h1>
</div>
<p className="text-muted-foreground">
Welcome to LangGraph Chat! Before you get started, you need to
enter the URL of the deployment and the assistant / graph ID.
</p>
</div>
<form
onSubmit={(e) => {
e.preventDefault();
const form = e.target as HTMLFormElement;
const formData = new FormData(form);
const apiUrl = formData.get("apiUrl") as string;
const assistantId = formData.get("assistantId") as string;
2025-03-04 17:58:46 +01:00
const apiKey = formData.get("apiKey") as string;
2025-03-04 17:38:33 +01:00
setApiUrl(apiUrl);
2025-03-04 17:58:46 +01:00
setApiKey(apiKey);
2025-03-04 17:38:33 +01:00
setAssistantId(assistantId);
form.reset();
}}
className="flex flex-col gap-6 p-6 bg-muted/50"
>
<div className="flex flex-col gap-2">
<Label htmlFor="apiUrl">Deployment URL</Label>
<p className="text-muted-foreground text-sm">
This is the URL of your LangGraph deployment. Can be a local, or
production deployment.
</p>
<Input
id="apiUrl"
name="apiUrl"
className="bg-background"
defaultValue={apiUrl ?? "http://localhost:2024"}
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="assistantId">Assistant / Graph ID</Label>
<p className="text-muted-foreground text-sm">
This is the ID of the graph (can be the graph name), or
assistant to fetch threads from, and invoke when actions are
taken.
</p>
<Input
id="assistantId"
name="assistantId"
className="bg-background"
defaultValue={assistantId ?? "agent"}
2025-03-04 17:58:46 +01:00
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="apiKey">LangSmith API Key</Label>
<p className="text-muted-foreground text-sm">
This value is stored in your browser's local storage and is only
used to authenticate requests sent to your LangGraph server.
</p>
<PasswordInput
id="apiKey"
name="apiKey"
defaultValue={apiKey ?? ""}
className="bg-background"
placeholder="lsv2_pt_..."
2025-03-04 17:38:33 +01:00
/>
</div>
<div className="flex justify-end mt-2">
<Button type="submit" size="lg">
Continue
<ArrowRight className="size-5" />
</Button>
</div>
</form>
</div>
</div>
);
}
return (
2025-03-04 17:58:46 +01:00
<StreamSession apiKey={apiKey} apiUrl={apiUrl} assistantId={assistantId}>
2025-03-04 17:38:33 +01:00
{children}
</StreamSession>
);
};
2025-02-27 14:08:24 -08:00
// Create a custom hook to use the context
export const useStreamContext = (): StreamContextType => {
const context = useContext(StreamContext);
if (context === undefined) {
throw new Error("useStreamContext must be used within a StreamProvider");
}
return context;
};
export default StreamContext;