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
62 lines (59 loc) · 2.45 KB
/
Copy pathroute.ts
File metadata and controls
62 lines (59 loc) · 2.45 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
import { NextRequest, NextResponse } from "next/server";
import {
CopilotRuntime,
ExperimentalEmptyAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { AbstractAgent, HttpAgent } from "@ag-ui/client";
// Dedicated runtime for the Open Generative UI demos.
//
// Isolated from the main `/api/copilotkit` runtime because the
// `openGenerativeUI` flag flips `openGenerativeUIEnabled: true` globally
// on the runtime info probe, which causes the CopilotKit provider's
// setTools effect to wipe per-demo `useFrontendTool` / `useComponent`
// registrations in the default runtime.
//
// Each OGUI agent is backed by a dedicated sub-path on the MS Agent
// Framework FastAPI server (see `src/agent_server.py`):
// - /open-gen-ui -> the minimal OGUI agent
// - /open-gen-ui-advanced -> the advanced OGUI agent (sandbox functions)
const AGENT_URL = process.env.AGENT_URL || "http://localhost:8000";
const agents: Record<string, AbstractAgent> = {
"open-gen-ui": new HttpAgent({ url: `${AGENT_URL}/open-gen-ui` }),
"open-gen-ui-advanced": new HttpAgent({
url: `${AGENT_URL}/open-gen-ui-advanced`,
}),
};
export const POST = async (req: NextRequest) => {
try {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
endpoint: "/api/copilotkit-ogui",
serviceAdapter: new ExperimentalEmptyAdapter(),
// Server-side config is identical for the minimal and advanced cells --
// the advanced behaviour (sandbox -> host function calls) is wired
// entirely on the frontend via `openGenerativeUI.sandboxFunctions` on
// the provider. The single `openGenerativeUI` flag below turns on
// Open Generative UI for the listed agent(s); the runtime middleware
// converts each agent's streamed `generateSandboxedUi` tool call into
// `open-generative-ui` activity events.
// @region[minimal-runtime-flag]
// @region[advanced-runtime-config]
runtime: new CopilotRuntime({
// @ts-ignore -- see main route.ts for type-comment rationale
agents,
openGenerativeUI: {
agents: ["open-gen-ui", "open-gen-ui-advanced"],
},
}),
// @endregion[advanced-runtime-config]
// @endregion[minimal-runtime-flag]
});
return await handleRequest(req);
} catch (error: unknown) {
const err = error as Error;
return NextResponse.json(
{ error: err.message, stack: err.stack },
{ status: 500 },
);
}
};