mirror of
https://github.com/langchain-ai/docs.git
synced 2026-08-27 21:00:00 -04:00
38 lines
884 B
Plaintext
38 lines
884 B
Plaintext
```ts
|
|
import * as z from "zod";
|
|
|
|
import {
|
|
END,
|
|
MemorySaver,
|
|
START,
|
|
StateGraph,
|
|
StateSchema,
|
|
} from "@langchain/langgraph";
|
|
import type { GraphNode } from "@langchain/langgraph";
|
|
|
|
const State = new StateSchema({
|
|
url: z.string(),
|
|
result: z.string().optional(),
|
|
});
|
|
|
|
const callApi: GraphNode<typeof State> = async (state) => {
|
|
const response = await fetch(state.url); // [!code highlight]
|
|
const text = await response.text();
|
|
const result = text.slice(0, 100);
|
|
return { result };
|
|
};
|
|
|
|
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({ url: "https://www.example.com" }, config);
|
|
```
|