Files
2025-02-11 02:09:51 -06:00

47 lines
1.2 KiB
TypeScript

import { NextRequest } from "next/server";
import {
CopilotRuntime,
OpenAIAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
langGraphPlatformEndpoint,
copilotKitEndpoint,
} from "@copilotkit/runtime";
import OpenAI from "openai";
const openai = new OpenAI();
const llmAdapter = new OpenAIAdapter({ openai } as any);
const langsmithApiKey = process.env.LANGSMITH_API_KEY as string;
export const POST = async (req: NextRequest) => {
const searchParams = req.nextUrl.searchParams;
const deploymentUrl = searchParams.get("lgcDeploymentUrl");
const remoteEndpoint = deploymentUrl
? langGraphPlatformEndpoint({
deploymentUrl,
langsmithApiKey,
agents: [
{
name: "email_agent",
description: "This agent sends emails",
},
],
})
: copilotKitEndpoint({
url:
process.env.REMOTE_ACTION_URL || "http://localhost:8000/copilotkit",
});
const runtime = new CopilotRuntime({
remoteEndpoints: [remoteEndpoint],
});
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
runtime,
serviceAdapter: llmAdapter,
endpoint: "/api/copilotkit",
});
return handleRequest(req);
};