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
55 lines (50 loc) · 1.89 KB
/
Copy pathroute.ts
File metadata and controls
55 lines (50 loc) · 1.89 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
// Dedicated runtime for the Agent Config Object demo.
//
// Proxies CopilotKit runtime requests to the MS Agent Framework backend's
// `/agent-config` endpoint, where `agent_config_agent.py` reads forwarded
// props (tone / expertise / responseLength) and rebuilds its system prompt
// per turn. Scoping to its own route + agent name keeps non-demo cells out
// of the dynamic-prompt path.
//
// References:
// - src/agents/agent_config_agent.py (backend graph)
// - src/app/demos/agent-config/page.tsx (provider properties source)
import { NextRequest, NextResponse } from "next/server";
import {
CopilotRuntime,
ExperimentalEmptyAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { AbstractAgent, HttpAgent } from "@ag-ui/client";
const AGENT_URL = process.env.AGENT_URL || "http://localhost:8000";
function createAgent() {
return new HttpAgent({ url: `${AGENT_URL}/agent-config` });
}
const agents: Record<string, AbstractAgent> = {
// The page mounts <CopilotKit agent="agent-config-demo">.
"agent-config-demo": createAgent(),
// useAgent() with no args defaults to "default"; alias so internal lookups
// resolve to the same agent.
default: createAgent(),
};
export const POST = async (req: NextRequest) => {
try {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
endpoint: "/api/copilotkit-agent-config",
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,
}),
});
return await handleRequest(req);
} catch (error: unknown) {
const err = error as Error;
return NextResponse.json(
{ error: err.message, stack: err.stack },
{ status: 500 },
);
}
};