@@ -4,26 +4,60 @@ import {
44 copilotRuntimeNextJSAppRouterEndpoint ,
55} from "@copilotkit/runtime" ;
66import { agentsIntegrations } from "@/agents" ;
7+ import type { AbstractAgent } from "@ag-ui/client" ;
8+ import { handle } from "hono/vercel" ;
79
8- import { NextRequest } from "next/server" ;
10+ const serviceAdapter = new ExperimentalEmptyAdapter ( ) ;
911
10- export async function POST ( request : NextRequest ) {
11- const integrationId = request . url . split ( "/" ) . pop ( ) ;
12+ function extractIntegrationIdFromUrl ( url : string ) : string | null {
13+ try {
14+ const { pathname } = new URL ( url ) ;
15+ const parts = pathname . split ( "/" ) ; // ["", "api", "copilotkit", "<id>", ...]
16+ const apiIdx = parts . indexOf ( "api" ) ;
17+ if ( apiIdx !== - 1 && parts [ apiIdx + 1 ] === "copilotkit" && parts . length > apiIdx + 2 ) {
18+ return parts [ apiIdx + 2 ] || null ;
19+ }
20+ return parts . filter ( Boolean ) . pop ( ) ?? null ;
21+ } catch {
22+ return null ;
23+ }
24+ }
1225
26+ async function buildAppForIntegration ( integrationId : string ) {
1327 const integration = agentsIntegrations . find ( ( i ) => i . id === integrationId ) ;
14- if ( ! integration ) {
15- return new Response ( "Integration not found" , { status : 404 } ) ;
16- }
17- const agents = await integration . agents ( ) ;
28+ if ( ! integration ) return null ;
29+
30+ const agentsPartial = await integration . agents ( ) ;
31+ const entries = Object . entries ( agentsPartial ) . filter (
32+ ( entry ) : entry is [ string , AbstractAgent ] => Boolean ( entry [ 1 ] )
33+ ) ;
34+ if ( entries . length === 0 ) return null ;
35+ const agents = Object . fromEntries ( entries ) as Record < string , AbstractAgent > ;
36+
1837 const runtime = new CopilotRuntime ( {
1938 // @ts -ignore for now
2039 agents,
2140 } ) ;
22- const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint ( {
41+ const app = copilotRuntimeNextJSAppRouterEndpoint ( {
2342 runtime,
24- serviceAdapter : new ExperimentalEmptyAdapter ( ) ,
43+ serviceAdapter,
2544 endpoint : `/api/copilotkit/${ integrationId } ` ,
2645 } ) ;
27-
28- return handleRequest ( request ) ;
46+ return app ;
2947}
48+
49+ export const GET = async ( request : Request ) => {
50+ const integrationId = extractIntegrationIdFromUrl ( request . url ) ;
51+ if ( ! integrationId ) return new Response ( "Integration not found" , { status : 404 } ) ;
52+ const app = await buildAppForIntegration ( integrationId ) ;
53+ if ( ! app ) return new Response ( "Integration not found" , { status : 404 } ) ;
54+ return handle ( app ) ( request ) ;
55+ } ;
56+
57+ export const POST = async ( request : Request ) => {
58+ const integrationId = extractIntegrationIdFromUrl ( request . url ) ;
59+ if ( ! integrationId ) return new Response ( "Integration not found" , { status : 404 } ) ;
60+ const app = await buildAppForIntegration ( integrationId ) ;
61+ if ( ! app ) return new Response ( "Integration not found" , { status : 404 } ) ;
62+ return handle ( app ) ( request ) ;
63+ } ;
0 commit comments