forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
74 lines (68 loc) · 2.44 KB
/
Copy pathroute.ts
File metadata and controls
74 lines (68 loc) · 2.44 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Dedicated runtime for the Beautiful Chat flagship showcase cell.
//
// Beautiful Chat simultaneously exercises A2UI (dynamic + fixed schema),
// Open Generative UI, and MCP Apps. The canonical reference
// (examples/integrations/langgraph-python) ships all three flags on a single
// runtime, but the 4085 showcase splits those concerns into per-feature
// endpoints so non-flagship cells keep their per-demo `useFrontendTool` /
// `useComponent` registrations isolated. This route restores the canonical's
// combined runtime for just the one cell that needs it.
//
// References:
// - examples/integrations/langgraph-python/src/app/api/copilotkit/[[...slug]]/route.ts
// - src/app/api/copilotkit-ogui/route.ts (scoping pattern)
// - src/app/api/copilotkit-mcp-apps/route.ts (mcpApps config pattern)
import { NextRequest, NextResponse } from "next/server";
import {
CopilotRuntime,
ExperimentalEmptyAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { LangGraphAgent } from "@copilotkit/runtime/langgraph";
const LANGGRAPH_URL =
process.env.LANGGRAPH_DEPLOYMENT_URL || "http://localhost:8123";
const beautifulChatAgent = new LangGraphAgent({
deploymentUrl: LANGGRAPH_URL,
graphId: "beautiful_chat",
langsmithApiKey: process.env.LANGSMITH_API_KEY || "",
});
const agents: Record<string, LangGraphAgent> = {
"beautiful-chat": beautifulChatAgent,
};
const runtime = new CopilotRuntime({
// @ts-ignore -- see main route.ts
agents,
// Canonical: openGenerativeUI: true, a2ui.injectA2UITool: true, mcpApps.
openGenerativeUI: true,
a2ui: {
// Inject the dynamic `generate_a2ui` tool into the agent
injectA2UITool: true,
},
mcpApps: {
servers: [
{
type: "http",
url: process.env.MCP_SERVER_URL || "https://mcp.excalidraw.com",
// Stable serverId so persisted threads keep restoring the same MCP
// server across URL changes.
serverId: "beautiful_chat_mcp",
},
],
},
});
export const POST = async (req: NextRequest) => {
try {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
endpoint: "/api/copilotkit-beautiful-chat",
serviceAdapter: new ExperimentalEmptyAdapter(),
runtime,
});
return await handleRequest(req);
} catch (error: unknown) {
const e = error as { message?: string; stack?: string };
return NextResponse.json(
{ error: e.message, stack: e.stack },
{ status: 500 },
);
}
};