























@@ -299,17 +299,20 @@ function mergeGatewayClientInternal(
299299300300type DispatchGatewayMethodInProcessOptions = {
301301allowSyntheticModelOverride?: boolean;
302+disableSyntheticClient?: boolean;
302303expectFinal?: boolean;
303304forceSyntheticClient?: boolean;
304305pluginRuntimeOwnerId?: string;
306+requireScopedClient?: boolean;
305307syntheticScopes?: string[];
306308timeoutMs?: number;
307309};
308310309-type GatewayMethodDispatchResponse = {
311+export type GatewayMethodDispatchResponse = {
310312ok: boolean;
311313payload?: unknown;
312314error?: ErrorShape;
315+meta?: Record<string, unknown>;
313316};
314317315318function unwrapGatewayMethodDispatchResponse(
@@ -322,11 +325,11 @@ function unwrapGatewayMethodDispatchResponse(
322325return response.payload;
323326}
324327325-async function dispatchGatewayMethod<T>(
328+export async function dispatchGatewayMethodInProcessRaw(
326329method: string,
327-params: Record<string, unknown>,
330+params: unknown,
328331options?: DispatchGatewayMethodInProcessOptions,
329-): Promise<T> {
332+): Promise<GatewayMethodDispatchResponse> {
330333const scope = getPluginRuntimeGatewayRequestScope();
331334const context = scope?.context ?? getFallbackGatewayContext();
332335const 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+}
338346339347let firstResponse: GatewayMethodDispatchResponse | undefined;
340348let finalResponse: GatewayMethodDispatchResponse | undefined;
@@ -353,6 +361,9 @@ async function dispatchGatewayMethod<T>(
353361scope?.client,
354362pluginRuntimeOwnerId ? { pluginRuntimeOwnerId } : undefined,
355363);
364+if (options?.disableSyntheticClient === true && !scopedClient) {
365+throw new Error(`In-process gateway dispatch requires a scoped client (method: ${method}).`);
366+}
356367await handleGatewayRequest({
357368req: {
358369type: "req",
@@ -361,10 +372,12 @@ async function dispatchGatewayMethod<T>(
361372 params,
362373},
363374client:
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 } : {}) };
368381if (!firstResponse) {
369382firstResponse = response;
370383return;
@@ -382,7 +395,7 @@ async function dispatchGatewayMethod<T>(
382395}
383396const firstPayload = firstResponse.payload as { status?: unknown } | undefined;
384397if (options?.expectFinal !== true || firstPayload?.status !== "accepted") {
385-return unwrapGatewayMethodDispatchResponse(method, firstResponse) as T;
398+return firstResponse;
386399}
387400const final =
388401finalResponse ??
@@ -412,7 +425,16 @@ async function dispatchGatewayMethod<T>(
412425resolve(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}
417439418440export async function dispatchGatewayMethodInProcess<T>(
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。