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
91 lines (79 loc) · 2.92 KB
/
Copy pathroute.ts
File metadata and controls
91 lines (79 loc) · 2.92 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
import { NextRequest, NextResponse } from "next/server";
import {
CopilotRuntime,
ExperimentalEmptyAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { AbstractAgent, HttpAgent } from "@ag-ui/client";
// The reasoning backend agent is mounted at `/reasoning` on the FastAPI
// server. Both reasoning demos (custom and default render) point here.
const AGENT_URL = process.env.AGENT_URL || "http://localhost:8000";
console.log("[copilotkit-reasoning/route] Initializing reasoning runtime");
console.log(`[copilotkit-reasoning/route] AGENT_URL: ${AGENT_URL}`);
function createAgent() {
return new HttpAgent({ url: `${AGENT_URL}/reasoning` });
}
// Both demos share the same backend — we just register it under the names
// each demo's `agent` prop uses.
const agentNames = ["agentic-chat-reasoning", "reasoning-default-render"];
const agents: Record<string, AbstractAgent> = {};
for (const name of agentNames) {
agents[name] = createAgent();
}
agents["default"] = createAgent();
console.log(
`[copilotkit-reasoning/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");
console.log(
`[copilotkit-reasoning/route] POST ${url} (content-type: ${contentType})`,
);
try {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
endpoint: "/api/copilotkit-reasoning",
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);
console.log(
`[copilotkit-reasoning/route] Response status: ${response.status}`,
);
return response;
} catch (error: unknown) {
const err = error as Error;
console.error(`[copilotkit-reasoning/route] ERROR: ${err.message}`);
console.error(`[copilotkit-reasoning/route] Stack: ${err.stack}`);
return NextResponse.json(
{ error: err.message, stack: err.stack },
{ status: 500 },
);
}
};
export const GET = async () => {
console.log(
"[copilotkit-reasoning/route] GET /api/copilotkit-reasoning (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}/reasoning`,
agent_status: agentStatus,
env: {
OPENAI_API_KEY: process.env.OPENAI_API_KEY ? "set" : "NOT SET",
NODE_ENV: process.env.NODE_ENV,
},
});
};