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
188 lines (174 loc) · 6.14 KB
/
Copy pathroute.ts
File metadata and controls
188 lines (174 loc) · 6.14 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import {
CopilotRuntime,
ExperimentalEmptyAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import type { AbstractAgent } from "@ag-ui/client";
import { createClaudeHttpAgent } from "@/app/api/_shared/claude-http-agent";
import crypto from "node:crypto";
// The agent backend runs as a separate process on port 8000.
// This runtime proxies CopilotKit requests to it via AG-UI protocol.
const AGENT_URL = process.env.AGENT_URL || "http://localhost:8000";
console.log("[copilotkit/route] Initializing CopilotKit runtime");
console.log(`[copilotkit/route] AGENT_URL: ${AGENT_URL}`);
// Per-request request/response logging is gated behind this flag (default off).
// Under d6 probe fan-out, unconditional per-request logs flooded Railway's
// 500-logs/sec cap and killed the replica ("Messages dropped" → container stop).
// Set SHOWCASE_ROUTE_DEBUG=1 to re-enable verbose per-request tracing locally.
const ROUTE_DEBUG =
process.env.SHOWCASE_ROUTE_DEBUG === "1" ||
process.env.SHOWCASE_ROUTE_DEBUG === "true";
function createAgent(path = "/") {
return createClaudeHttpAgent(`${AGENT_URL}${path}`);
}
// Register the same agent under all names used by demo pages. Entries that
// need backend-owned tools/state are re-pointed by `dedicatedAgentPaths`.
const agentNames = [
"agentic_chat",
"agentic-chat",
"human_in_the_loop",
"hitl",
"tool-rendering",
"tool-rendering-default-catchall",
"tool-rendering-custom-catchall",
"gen-ui-tool-based",
"gen-ui-agent",
"shared-state-read",
"shared-state-write",
"shared-state-streaming",
"prebuilt-sidebar",
"prebuilt-popup",
"chat-slots",
"chat-customization-css",
"headless-simple",
"frontend_tools",
"frontend-tools",
"threadid-frontend-tool-roundtrip",
"frontend-tools-async",
"hitl-in-chat",
"hitl-in-app",
"readonly-state-agent-context",
"headless-complete",
"beautiful-chat",
"declarative-gen-ui",
"open-gen-ui",
"open-gen-ui-advanced",
"mcp-apps",
"declarative_json_render",
"declarative-hashbrown-demo",
"multimodal-demo",
"voice-demo",
"agent-config-demo",
"auth-demo",
"gen-ui-interrupt",
"interrupt-headless",
"reasoning-default",
"reasoning-custom",
];
// Demos with dedicated FastAPI endpoints (their own state schema, tool
// set, or prompt). The Python backend mounts each at /<name>; mapping
// the agent name to that path keeps the runtime config flat.
const dedicatedAgentPaths: Record<string, string> = {
"gen-ui-agent": "/gen-ui-agent",
"tool-rendering": "/tool-rendering",
"tool-rendering-default-catchall": "/tool-rendering",
"tool-rendering-custom-catchall": "/tool-rendering",
"shared-state-read-write": "/shared-state-read-write",
"shared-state-streaming": "/shared-state-streaming",
subagents: "/subagents",
// Reasoning demos share a single backend that emits AG-UI
// REASONING_MESSAGE_* events (parsed out of <reasoning>...</reasoning>
// blocks the model emits). The two demo cells differ only on the
// frontend slot configuration.
"reasoning-default": "/reasoning",
"reasoning-custom": "/reasoning",
"tool-rendering-reasoning-chain": "/tool-rendering-reasoning-chain",
"headless-complete": "/headless-complete",
"hitl-in-chat": "/hitl-in-chat",
"hitl-in-chat-booking": "/hitl-in-chat",
"gen-ui-interrupt": "/interrupt-adapted",
"interrupt-headless": "/interrupt-adapted",
};
const agents: Record<string, AbstractAgent> = {};
for (const name of agentNames) {
agents[name] = createAgent();
}
for (const [name, path] of Object.entries(dedicatedAgentPaths)) {
agents[name] = createAgent(path);
}
agents["default"] = createAgent();
console.log(
`[copilotkit/route] Registered ${Object.keys(agents).length} agent names: ${Object.keys(agents).join(", ")}`,
);
export const POST = async (req: NextRequest) => {
const url = req.url;
const contentType = req.headers.get("content-type");
if (ROUTE_DEBUG) {
console.log(
`[copilotkit/route] POST ${url} (content-type: ${contentType})`,
);
}
try {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
endpoint: "/api/copilotkit",
serviceAdapter: new ExperimentalEmptyAdapter(),
runtime: new CopilotRuntime({
// @ts-ignore -- Published CopilotRuntime agents type wraps Record in MaybePromise<NonEmptyRecord<...>> which rejects plain Records; fixed in source, pending release
agents,
}),
});
const response = await handleRequest(req);
if (!response.ok) {
console.log(`[copilotkit/route] Response status: ${response.status}`);
} else if (ROUTE_DEBUG) {
console.log(`[copilotkit/route] Response status: ${response.status}`);
}
return response;
} catch (error: unknown) {
// Log the full error server-side with a correlation id, but only return
// a generic message + errorId to the client so we don't leak stack
// traces or internal error messages to untrusted callers. Mirrors the
// pattern used in `mastra/src/app/api/copilotkit/route.ts`.
const err = error instanceof Error ? error : new Error(String(error));
const errorId = crypto.randomUUID();
console.error(
JSON.stringify({
at: new Date().toISOString(),
level: "error",
route: "/api/copilotkit",
errorId,
message: err.message,
stack: err.stack,
}),
);
return NextResponse.json(
{ error: "internal runtime error", errorId },
{ status: 500 },
);
}
};
export const GET = async () => {
if (ROUTE_DEBUG) {
console.log("[copilotkit/route] GET /api/copilotkit (health probe)");
}
let agentStatus = "unknown";
try {
const res = await fetch(`${AGENT_URL}/health`, {
signal: AbortSignal.timeout(3000),
});
agentStatus = res.ok ? "reachable" : `error (${res.status})`;
} catch (e: unknown) {
agentStatus = `unreachable (${(e as Error).message})`;
}
return NextResponse.json({
status: "ok",
agent_url: AGENT_URL,
agent_status: agentStatus,
env: {
ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY ? "set" : "NOT SET",
NODE_ENV: process.env.NODE_ENV,
},
});
};