-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathserver.ts
More file actions
52 lines (44 loc) · 1.6 KB
/
Copy pathserver.ts
File metadata and controls
52 lines (44 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { cors } from "hono/cors";
import { CopilotRuntime, createCopilotEndpointSingleRoute } from "@copilotkit/runtime/v2";
import { LangGraphHttpAgent } from "@copilotkit/runtime/langgraph";
import { registerRealtimeSessionRoute } from "@frenchfryai/runtime";
import { isShuttingDown, onShutdown } from "./resilience.js";
const agentHost = process.env.LANGGRAPH_DEPLOYMENT_URL || "http://localhost:8123";
const agentUrl = agentHost.startsWith("http") ? agentHost : `http://${agentHost}`;
const runtime = new CopilotRuntime({
agents: {
default: new LangGraphHttpAgent({
url: agentUrl,
}),
},
});
const copilotApp = createCopilotEndpointSingleRoute({
runtime,
basePath: "/api/copilotkit",
});
const app = new Hono();
app.use("/*", cors());
// Reject new requests during graceful OOM shutdown so in-flight ones can drain
app.use("/*", async (c, next) => {
if (isShuttingDown()) {
c.header("Connection", "close");
c.header("Retry-After", "5");
return c.text("Service restarting", 503);
}
return next();
});
app.route("/", copilotApp as any);
registerRealtimeSessionRoute(app, {
path: "/realtime/session",
openai: {
apiKey: process.env.OPENAI_API_KEY ?? "",
},
});
const port = Number(process.env.PORT || 4000);
let server: ReturnType<typeof serve> | undefined;
server = serve({ fetch: app.fetch, port });
onShutdown(() => server?.close());
console.log(`Runtime server listening at http://localhost:${port}/api/copilotkit`);
console.log(`Realtime session endpoint at http://localhost:${port}/realtime/session`);