@@ -299,17 +299,20 @@ function mergeGatewayClientInternal(
299299
300300type DispatchGatewayMethodInProcessOptions = {
301301 allowSyntheticModelOverride ?: boolean ;
302+ disableSyntheticClient ?: boolean ;
302303 expectFinal ?: boolean ;
303304 forceSyntheticClient ?: boolean ;
304305 pluginRuntimeOwnerId ?: string ;
306+ requireScopedClient ?: boolean ;
305307 syntheticScopes ?: string [ ] ;
306308 timeoutMs ?: number ;
307309} ;
308310
309- type GatewayMethodDispatchResponse = {
311+ export type GatewayMethodDispatchResponse = {
310312 ok : boolean ;
311313 payload ?: unknown ;
312314 error ?: ErrorShape ;
315+ meta ?: Record < string , unknown > ;
313316} ;
314317
315318function unwrapGatewayMethodDispatchResponse (
@@ -322,11 +325,11 @@ function unwrapGatewayMethodDispatchResponse(
322325 return response . payload ;
323326}
324327
325- async function dispatchGatewayMethod < T > (
328+ export async function dispatchGatewayMethodInProcessRaw (
326329 method : string ,
327- params : Record < string , unknown > ,
330+ params : unknown ,
328331 options ?: DispatchGatewayMethodInProcessOptions ,
329- ) : Promise < T > {
332+ ) : Promise < GatewayMethodDispatchResponse > {
330333 const scope = getPluginRuntimeGatewayRequestScope ( ) ;
331334 const context = scope ?. context ?? getFallbackGatewayContext ( ) ;
332335 const isWebchatConnect = scope ?. isWebchatConnect ?? ( ( ) => false ) ;
@@ -335,6 +338,11 @@ async function dispatchGatewayMethod<T>(
335338 `In-process gateway dispatch requires a gateway request scope (method: ${ method } ). No scope set and no fallback context available.` ,
336339 ) ;
337340 }
341+ if ( options ?. requireScopedClient === true && ! scope ?. client ) {
342+ throw new Error (
343+ `In-process gateway dispatch requires an authenticated plugin request scope (method: ${ method } ).` ,
344+ ) ;
345+ }
338346
339347 let firstResponse : GatewayMethodDispatchResponse | undefined ;
340348 let finalResponse : GatewayMethodDispatchResponse | undefined ;
@@ -353,6 +361,9 @@ async function dispatchGatewayMethod<T>(
353361 scope ?. client ,
354362 pluginRuntimeOwnerId ? { pluginRuntimeOwnerId } : undefined ,
355363 ) ;
364+ if ( options ?. disableSyntheticClient === true && ! scopedClient ) {
365+ throw new Error ( `In-process gateway dispatch requires a scoped client (method: ${ method } ).` ) ;
366+ }
356367 await handleGatewayRequest ( {
357368 req : {
358369 type : "req" ,
@@ -361,10 +372,12 @@ async function dispatchGatewayMethod<T>(
361372 params,
362373 } ,
363374 client :
364- options ?. forceSyntheticClient === true ? syntheticClient : ( scopedClient ?? syntheticClient ) ,
375+ options ?. forceSyntheticClient === true
376+ ? syntheticClient
377+ : ( scopedClient ?? ( options ?. disableSyntheticClient === true ? null : syntheticClient ) ) ,
365378 isWebchatConnect,
366- respond : ( ok , payload , error ) => {
367- const response = { ok, payload, error } ;
379+ respond : ( ok , payload , error , meta ) => {
380+ const response = { ok, payload, error, ... ( meta ? { meta } : { } ) } ;
368381 if ( ! firstResponse ) {
369382 firstResponse = response ;
370383 return ;
@@ -382,7 +395,7 @@ async function dispatchGatewayMethod<T>(
382395 }
383396 const firstPayload = firstResponse . payload as { status ?: unknown } | undefined ;
384397 if ( options ?. expectFinal !== true || firstPayload ?. status !== "accepted" ) {
385- return unwrapGatewayMethodDispatchResponse ( method , firstResponse ) as T ;
398+ return firstResponse ;
386399 }
387400 const final =
388401 finalResponse ??
@@ -412,7 +425,16 @@ async function dispatchGatewayMethod<T>(
412425 resolve ( response ) ;
413426 } ;
414427 } ) ) ;
415- return unwrapGatewayMethodDispatchResponse ( method , final ) as T ;
428+ return final ;
429+ }
430+
431+ async function dispatchGatewayMethod < T > (
432+ method : string ,
433+ params : unknown ,
434+ options ?: DispatchGatewayMethodInProcessOptions ,
435+ ) : Promise < T > {
436+ const response = await dispatchGatewayMethodInProcessRaw ( method , params , options ) ;
437+ return unwrapGatewayMethodDispatchResponse ( method , response ) as T ;
416438}
417439
418440export async function dispatchGatewayMethodInProcess < T > (
0 commit comments