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
63 lines (58 loc) · 2.55 KB
/
Copy pathroute.ts
File metadata and controls
63 lines (58 loc) · 2.55 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
// Dedicated runtime for the Multimodal Attachments demo (Wave 2b).
//
// Why its own route? The backing graph (`multimodal`, from
// src/agents/multimodal_agent.py) runs a vision-capable model (gpt-4o). Every
// other cell in the showcase uses a text-only, cheaper model. Registering
// `multimodal` under the shared `/api/copilotkit` runtime would silently upgrade
// *all* cells that share that runtime to a vision model whenever the browser
// routed to this one — wasting tokens and blurring the per-demo cost boundary.
// A dedicated route keeps the vision capability — and its cost — scoped to
// exactly the cell that exercises it, matching the pattern used by
// `/api/copilotkit-beautiful-chat`.
//
// The page at src/app/demos/multimodal/page.tsx points its `runtimeUrl` at
// this endpoint and sets `agent="multimodal-demo"` (the slug registered below).
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 multimodalAgent = new LangGraphAgent({
deploymentUrl: LANGGRAPH_URL,
// graphId references the key in langgraph.json — must match the
// "multimodal" entry that resolves to src/agents/multimodal_agent.py:graph.
graphId: "multimodal",
langsmithApiKey: process.env.LANGSMITH_API_KEY || "",
});
const agents: Record<string, LangGraphAgent> = {
// The page's <CopilotKit agent="multimodal-demo"> resolves here.
"multimodal-demo": multimodalAgent,
// Alias for any internal component that calls `useAgent()` without args
// (matches the beautiful-chat route's "default" alias pattern).
default: multimodalAgent,
};
export const POST = async (req: NextRequest) => {
try {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
endpoint: "/api/copilotkit-multimodal",
serviceAdapter: new ExperimentalEmptyAdapter(),
runtime: new CopilotRuntime({
// @ts-ignore -- see main route.ts; published CopilotRuntime's `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 e = error as { message?: string; stack?: string };
return NextResponse.json(
{ error: e.message, stack: e.stack },
{ status: 500 },
);
}
};