@@ -5,18 +5,28 @@ import {
55} from "@copilotkitnext/runtime" ;
66import { handle } from "hono/vercel" ;
77import type { NextRequest } from "next/server" ;
8+ import { BasicAgent } from "@copilotkitnext/agent" ;
89
910type RouteParams = {
10- params : {
11+ params : Promise < {
1112 integrationId : string ;
1213 slug ?: string [ ] ;
13- } ;
14+ } > ;
1415} ;
1516
16- function createHandler ( integrationId : string ) {
17+ const handlerCache = new Map < string , ReturnType < typeof handle > > ( ) ;
18+
19+ function getHandler ( integrationId : string ) {
20+ const cached = handlerCache . get ( integrationId ) ;
21+ if ( cached ) {
22+ return cached ;
23+ }
24+
1725 const runtime = new CopilotRuntime ( {
1826 agents : {
19- default : null as any ,
27+ default : new BasicAgent ( {
28+ model : "openai/gpt-4o" ,
29+ } ) ,
2030 } ,
2131 runner : new InMemoryAgentRunner ( ) ,
2232 } ) ;
@@ -26,16 +36,19 @@ function createHandler(integrationId: string) {
2636 basePath : `/api/copilotkitnext/${ integrationId } ` ,
2737 } ) ;
2838
29- return handle ( app ) ;
39+ const handler = handle ( app ) ;
40+ handlerCache . set ( integrationId , handler ) ;
41+ return handler ;
3042}
3143
32- export function GET ( request : NextRequest , context : RouteParams ) {
33- const handler = createHandler ( context . params . integrationId ) ;
44+ export async function GET ( request : NextRequest , context : RouteParams ) {
45+ const { integrationId } = await context . params ;
46+ const handler = getHandler ( integrationId ) ;
3447 return handler ( request ) ;
3548}
3649
37- export function POST ( request : NextRequest , context : RouteParams ) {
38- const handler = createHandler ( context . params . integrationId ) ;
50+ export async function POST ( request : NextRequest , context : RouteParams ) {
51+ const { integrationId } = await context . params ;
52+ const handler = getHandler ( integrationId ) ;
3953 return handler ( request ) ;
4054}
41-
0 commit comments