|
| 1 | +import type { BaseMessage } from "@langchain/core/messages"; |
| 2 | +import { StateGraph, MessagesZodMeta, START } from "@langchain/langgraph"; |
| 3 | +import { registry } from "@langchain/langgraph/zod"; |
| 4 | +import { ChatOpenAI } from "@langchain/openai"; |
| 5 | +import { z } from "zod/v4"; |
| 6 | + |
| 7 | +import { serve } from "@hono/node-server"; |
| 8 | +import { Hono } from "hono"; |
| 9 | + |
| 10 | +const llm = new ChatOpenAI({ model: "gpt-4o-mini" }); |
| 11 | + |
| 12 | +const schema = z.object({ |
| 13 | + messages: z.custom<BaseMessage[]>().register(registry, MessagesZodMeta), |
| 14 | +}); |
| 15 | + |
| 16 | +const graph = new StateGraph(schema) |
| 17 | + .addNode("agent", async ({ messages }) => ({ |
| 18 | + messages: await llm.invoke(messages), |
| 19 | + })) |
| 20 | + .addEdge(START, "agent") |
| 21 | + .compile(); |
| 22 | + |
| 23 | +export type GraphType = typeof graph; |
| 24 | + |
| 25 | +const app = new Hono(); |
| 26 | + |
| 27 | +app.post("/api/stream", async (c) => { |
| 28 | + const { input } = z.object({ input: schema }).parse(await c.req.json()); |
| 29 | + |
| 30 | + const stream = await graph.stream(input, { |
| 31 | + encoding: "text/event-stream", |
| 32 | + streamMode: ["values", "messages", "updates"], |
| 33 | + }); |
| 34 | + |
| 35 | + return new Response(stream, { |
| 36 | + headers: { "Content-Type": "text/event-stream" }, |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +serve({ fetch: app.fetch, port: 9123 }, (c) => { |
| 41 | + console.log(`Server running at ${c.address}:${c.port}`); |
| 42 | +}); |
0 commit comments