@@ -4,55 +4,44 @@ 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" ;
7+ import { handle } from 'hono/vercel'
98
10- const serviceAdapter = new ExperimentalEmptyAdapter ( ) ;
9+ import { NextRequest } from "next/server" ;
1110
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- }
25-
26- async function buildAppForIntegration ( integrationId : string ) {
11+ async function createApp ( integrationId : string ) {
2712 const integration = agentsIntegrations . find ( ( i ) => i . id === integrationId ) ;
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 > ;
13+ if ( ! integration ) {
14+ throw new Error ( `Integration not found: ${ integrationId } ` ) ;
15+ }
16+ const agents = await integration . agents ( ) ;
3617
3718 const runtime = new CopilotRuntime ( {
3819 // @ts -ignore for now
3920 agents,
4021 } ) ;
41- const app = copilotRuntimeNextJSAppRouterEndpoint ( {
22+
23+ return copilotRuntimeNextJSAppRouterEndpoint ( {
4224 runtime,
43- serviceAdapter,
25+ serviceAdapter : new ExperimentalEmptyAdapter ( ) ,
4426 endpoint : `/api/copilotkit/${ integrationId } ` ,
4527 } ) ;
46- return app ;
4728}
4829
49- const requestHandler = 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- } ;
30+ async function routeHandler (
31+ request : NextRequest ,
32+ { params } : { params : Promise < { integrationId : string } > }
33+ ) {
34+ const { integrationId } = await params ;
35+ try {
36+ const app = await createApp ( integrationId ) ;
37+ return handle ( app ) ( request ) ;
38+ } catch ( error ) {
39+ return new Response (
40+ error instanceof Error ? error . message : "Integration not found" ,
41+ { status : 404 }
42+ ) ;
43+ }
44+ }
5645
57- export const GET = requestHandler ;
58- export const POST = requestHandler ;
46+ export const GET = routeHandler ;
47+ export const POST = routeHandler ;
0 commit comments