@@ -20,9 +20,6 @@ import { logger } from "@copilotkitnext/shared";
2020 * Public types
2121 * --------------------------------------------------------------------------------------------- */
2222
23- /** A string beginning with http:// or https:// that points to a webhook endpoint. */
24- export type MiddlewareURL = `${"http" | "https" } ://${string } `;
25-
2623export interface BeforeRequestMiddlewareParameters {
2724 runtime : CopilotRuntime ;
2825 request : Request ;
@@ -44,8 +41,8 @@ export type AfterRequestMiddlewareFn = (
4441/**
4542 * A middleware value can be either a callback function or a webhook URL.
4643 */
47- export type BeforeRequestMiddleware = BeforeRequestMiddlewareFn | MiddlewareURL ;
48- export type AfterRequestMiddleware = AfterRequestMiddlewareFn | MiddlewareURL ;
44+ export type BeforeRequestMiddleware = BeforeRequestMiddlewareFn ;
45+ export type AfterRequestMiddleware = AfterRequestMiddlewareFn ;
4946
5047/** Lifecycle events emitted to webhook middleware. */
5148export enum CopilotKitMiddlewareEvent {
@@ -64,10 +61,6 @@ export enum WebhookStage {
6461 * Internal helpers – (de)serialisation
6562 * --------------------------------------------------------------------------------------------- */
6663
67- function isMiddlewareURL ( value : unknown ) : value is MiddlewareURL {
68- return typeof value === "string" && / ^ h t t p s ? : \/ \/ / . test ( value ) ;
69- }
70-
7164export async function callBeforeRequestMiddleware ( {
7265 runtime,
7366 request,
@@ -81,93 +74,6 @@ export async function callBeforeRequestMiddleware({
8174 return ( mw as BeforeRequestMiddlewareFn ) ( { runtime, request, path } ) ;
8275 }
8376
84- // Webhook middleware
85- if ( isMiddlewareURL ( mw ) ) {
86- const clone = request . clone ( ) ;
87- const url = new URL ( request . url ) ;
88- const headersObj : Record < string , string > = { } ;
89- clone . headers . forEach ( ( v , k ) => {
90- headersObj [ k ] = v ;
91- } ) ;
92- let bodyJson : unknown = undefined ;
93- try {
94- bodyJson = await clone . json ( ) ;
95- } catch {
96- /* ignore */
97- }
98-
99- const payload = {
100- method : request . method ,
101- path : url . pathname ,
102- query : url . search . startsWith ( "?" ) ? url . search . slice ( 1 ) : url . search ,
103- headers : headersObj ,
104- body : bodyJson ,
105- } ;
106-
107- const ac = new AbortController ( ) ;
108- const to = setTimeout ( ( ) => ac . abort ( ) , 2000 ) ;
109- let res : Response ;
110- try {
111- res = await fetch ( mw , {
112- method : "POST" ,
113- headers : {
114- "content-type" : "application/json" ,
115- "X-CopilotKit-Webhook-Stage" : WebhookStage . BeforeRequest ,
116- } ,
117- body : JSON . stringify ( payload ) ,
118- signal : ac . signal ,
119- } ) ;
120- } catch {
121- clearTimeout ( to ) ;
122- throw new Response ( undefined , { status : 502 } ) ;
123- }
124- clearTimeout ( to ) ;
125-
126- if ( res . status >= 500 ) {
127- throw new Response ( undefined , { status : 502 } ) ;
128- }
129- if ( res . status >= 400 ) {
130- const errBody = await res . text ( ) ;
131- throw new Response ( errBody || null , {
132- status : res . status ,
133- headers : {
134- "content-type" : res . headers . get ( "content-type" ) || "application/json" ,
135- } ,
136- } ) ;
137- }
138- if ( res . status === 204 ) return ;
139-
140- let json : unknown ;
141- try {
142- json = await res . json ( ) ;
143- } catch {
144- return ;
145- }
146-
147- if ( json && typeof json === "object" ) {
148- const { headers, body } = json as {
149- headers ?: Record < string , string > ;
150- body ?: unknown ;
151- } ;
152- const init : RequestInit = {
153- method : request . method ,
154- } ;
155- if ( headers ) {
156- init . headers = headers ;
157- }
158- // Only add body for non-GET/HEAD requests
159- if (
160- body !== undefined &&
161- request . method !== "GET" &&
162- request . method !== "HEAD"
163- ) {
164- init . body = JSON . stringify ( body ) ;
165- }
166- return new Request ( request . url , init ) ;
167- }
168- return ;
169- }
170-
17177 logger . warn ( { mw } , "Unsupported beforeRequestMiddleware value – skipped" ) ;
17278 return ;
17379}
@@ -184,49 +90,5 @@ export async function callAfterRequestMiddleware({
18490 return ( mw as AfterRequestMiddlewareFn ) ( { runtime, response, path } ) ;
18591 }
18692
187- if ( isMiddlewareURL ( mw ) ) {
188- const clone = response . clone ( ) ;
189- const headersObj : Record < string , string > = { } ;
190- clone . headers . forEach ( ( v , k ) => {
191- headersObj [ k ] = v ;
192- } ) ;
193- let body = "" ;
194- try {
195- body = await clone . text ( ) ;
196- } catch {
197- /* ignore */
198- }
199-
200- const payload = {
201- status : clone . status ,
202- headers : headersObj ,
203- body,
204- } ;
205-
206- const ac = new AbortController ( ) ;
207- const to = setTimeout ( ( ) => ac . abort ( ) , 2000 ) ;
208- let res : Response ;
209- try {
210- res = await fetch ( mw , {
211- method : "POST" ,
212- headers : {
213- "content-type" : "application/json" ,
214- "X-CopilotKit-Webhook-Stage" : WebhookStage . AfterRequest ,
215- } ,
216- body : JSON . stringify ( payload ) ,
217- signal : ac . signal ,
218- } ) ;
219- } finally {
220- clearTimeout ( to ) ;
221- }
222-
223- if ( ! res . ok ) {
224- throw new Error (
225- `after_request webhook ${ mw } responded with ${ res . status } `
226- ) ;
227- }
228- return ;
229- }
230-
23193 logger . warn ( { mw } , "Unsupported afterRequestMiddleware value – skipped" ) ;
23294}
0 commit comments