mirror of
https://github.com/langchain-ai/docs.git
synced 2026-08-27 02:41:59 -04:00
44 lines
1.1 KiB
Plaintext
44 lines
1.1 KiB
Plaintext
```ts
|
|
import * as z from "zod";
|
|
|
|
import {
|
|
END,
|
|
MemorySaver,
|
|
START,
|
|
StateGraph,
|
|
StateSchema,
|
|
task,
|
|
} from "@langchain/langgraph";
|
|
import type { GraphNode } from "@langchain/langgraph";
|
|
|
|
const State = new StateSchema({
|
|
urls: z.array(z.string()),
|
|
results: z.array(z.string()).optional(),
|
|
});
|
|
|
|
const makeRequest = task("makeRequest", async (url: string) => {
|
|
const response = await fetch(url); // [!code highlight]
|
|
const text = await response.text();
|
|
return text.slice(0, 100);
|
|
});
|
|
|
|
const callApi: GraphNode<typeof State> = async (state) => {
|
|
const pending = state.urls.map((url) => makeRequest(url)); // [!code highlight]
|
|
const results = await Promise.all(pending);
|
|
return { results };
|
|
};
|
|
|
|
const builder = new StateGraph(State)
|
|
.addNode("callApi", callApi)
|
|
.addEdge(START, "callApi")
|
|
.addEdge("callApi", END);
|
|
|
|
const checkpointer = new MemorySaver();
|
|
const graph = builder.compile({ checkpointer });
|
|
|
|
const threadId = crypto.randomUUID();
|
|
const config = { configurable: { thread_id: threadId } };
|
|
|
|
await graph.invoke({ urls: ["https://www.example.com"] }, config);
|
|
```
|