Skip to content

Commit 5ec4335

Browse files
committed
support langchain api key
1 parent 18d4127 commit 5ec4335

File tree

7 files changed

+82
-9
lines changed

7 files changed

+82
-9
lines changed

.env.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
NEXT_PUBLIC_LANGGRAPH_API_URL=your_langgraph_api_url
1+
LANGCHAIN_API_KEY=your_langchain_api_key
2+
LANGGRAPH_API_URL=your_langgraph_api_url
23
NEXT_PUBLIC_LANGGRAPH_ASSISTANT_ID=your_assistant_id_or_graph_id

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ This is the [assistant-ui](https://github.com/Yonom/assistant-ui) starter projec
55
First, add your langgraph API url and assistant id to `.env.local` file:
66

77
```
8-
NEXT_PUBLIC_LANGGRAPH_API_URL=your_langgraph_api_url
9-
NEXT_PUBLIC_LANGGRAPH_ASSISTANT_ID=your_assistant_id_or_graph_id
8+
LANGCHAIN_API_KEY=your_langchain_api_key
9+
LANGGRAPH_API_URL=your_langgraph_api_url
10+
NEXT_PUBLIC_LANGGRAPH_ASSISTANT_ID=your_assistant_id_or_graph_id
1011
```
1112

1213
Then, run the development server:

app/MyRuntimeProvider.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export function MyRuntimeProvider({
1313
const threadIdRef = useRef<string | undefined>();
1414
const runtime = useLangGraphRuntime({
1515
threadId: threadIdRef.current,
16-
stream: async (message) => {
16+
stream: async (messages) => {
1717
if (!threadIdRef.current) {
1818
const { thread_id } = await createThread();
1919
threadIdRef.current = thread_id;
2020
}
2121
const threadId = threadIdRef.current;
2222
return sendMessage({
2323
threadId,
24-
message,
24+
messages,
2525
});
2626
},
2727
});

app/api/[..._path]/route.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
3+
export const runtime = "edge";
4+
5+
function getCorsHeaders() {
6+
return {
7+
"Access-Control-Allow-Origin": "*",
8+
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
9+
"Access-Control-Allow-Headers": "*",
10+
};
11+
}
12+
13+
async function handleRequest(req: NextRequest, method: string) {
14+
try {
15+
const path = req.nextUrl.pathname.replace(/^\/?api\//, "");
16+
const url = new URL(req.url);
17+
const searchParams = new URLSearchParams(url.search);
18+
searchParams.delete("_path");
19+
searchParams.delete("nxtP_path");
20+
const queryString = searchParams.toString()
21+
? `?${searchParams.toString()}`
22+
: "";
23+
24+
const options: RequestInit = {
25+
method,
26+
headers: {
27+
"x-api-key": process.env["LANGCHAIN_API_KEY"] || "",
28+
},
29+
};
30+
31+
if (["POST", "PUT", "PATCH"].includes(method)) {
32+
options.body = await req.text();
33+
}
34+
35+
console.log({ url, path, queryString, options });
36+
37+
const res = await fetch(
38+
`${process.env["LANGGRAPH_API_URL"]}/${path}${queryString}`,
39+
options
40+
);
41+
42+
return new NextResponse(res.body, {
43+
status: res.status,
44+
statusText: res.statusText,
45+
headers: {
46+
...res.headers,
47+
...getCorsHeaders(),
48+
},
49+
});
50+
} catch (e: any) {
51+
return NextResponse.json({ error: e.message }, { status: e.status ?? 500 });
52+
}
53+
}
54+
55+
export const GET = (req: NextRequest) => handleRequest(req, "GET");
56+
export const POST = (req: NextRequest) => handleRequest(req, "POST");
57+
export const PUT = (req: NextRequest) => handleRequest(req, "PUT");
58+
export const PATCH = (req: NextRequest) => handleRequest(req, "PATCH");
59+
export const DELETE = (req: NextRequest) => handleRequest(req, "DELETE");
60+
61+
// Add a new OPTIONS handler
62+
export const OPTIONS = () => {
63+
return new NextResponse(null, {
64+
status: 204,
65+
headers: {
66+
...getCorsHeaders(),
67+
},
68+
});
69+
};

app/page.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
import { Thread } from "@assistant-ui/react";
24
import { makeMarkdownText } from "@assistant-ui/react-markdown";
35

lib/chatApi.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { LangChainMessage } from "@assistant-ui/react-langgraph";
44
const createClient = () => {
55
const apiUrl =
66
process.env["NEXT_PUBLIC_LANGGRAPH_API_URL"] ||
7-
"https://localhost:8123/api";
7+
new URL("/api", window.location.href).href;
88
return new Client({
99
apiUrl,
1010
});
@@ -17,15 +17,15 @@ export const createThread = async () => {
1717

1818
export const sendMessage = async (params: {
1919
threadId: string;
20-
message: LangChainMessage;
20+
messages: LangChainMessage[];
2121
}) => {
2222
const client = createClient();
2323
return client.runs.stream(
2424
params.threadId,
2525
process.env["NEXT_PUBLIC_LANGGRAPH_ASSISTANT_ID"]!,
2626
{
2727
input: {
28-
messages: [params.message],
28+
messages: params.messages,
2929
},
3030
streamMode: "messages",
3131
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"@ai-sdk/openai": "^0.0.58",
1313
"@assistant-ui/react": "^0.5.51",
14-
"@assistant-ui/react-langgraph": "^0.0.3",
14+
"@assistant-ui/react-langgraph": "^0.0.5",
1515
"@assistant-ui/react-markdown": "^0.2",
1616
"@langchain/langgraph-sdk": "^0.0.8",
1717
"next": "14.2.9",

0 commit comments

Comments
 (0)