@@ -29,70 +29,14 @@ import { resolveGlobalSingleton } from "../shared/global-singleton.js";
2929import { resolveSafeTimeoutDelayMs } from "../utils/timer-delay.js" ;
3030import { ADMIN_SCOPE , APPROVALS_SCOPE , WRITE_SCOPE } from "./method-scopes.js" ;
3131import { normalizeOperatorScopeList , type OperatorScope } from "./operator-scopes.js" ;
32- import type {
33- GatewayRequestContext ,
34- GatewayRequestHandler ,
35- GatewayRequestOptions ,
36- } from "./server-methods/types.js" ;
37-
38- const FALLBACK_GATEWAY_CONTEXT_STATE_KEY : unique symbol = Symbol . for (
39- "openclaw.fallbackGatewayContextState" ,
40- ) ;
41-
42- type FallbackGatewayContextState = {
43- context : GatewayRequestContext | undefined ;
44- resolveContext : ( ( ) => GatewayRequestContext | undefined ) | undefined ;
45- } ;
46-
47- const getFallbackGatewayContextState = ( ) =>
48- resolveGlobalSingleton < FallbackGatewayContextState > ( FALLBACK_GATEWAY_CONTEXT_STATE_KEY , ( ) => ( {
49- context : undefined ,
50- resolveContext : undefined ,
51- } ) ) ;
52-
53- /** Set the process fallback gateway context for channel adapters outside WS requests. */
54- export function setFallbackGatewayContext ( ctx : GatewayRequestContext ) : ( ) => void {
55- const fallbackGatewayContextState = getFallbackGatewayContextState ( ) ;
56- fallbackGatewayContextState . context = ctx ;
57- fallbackGatewayContextState . resolveContext = undefined ;
58- return ( ) => {
59- const currentFallbackGatewayContextState = getFallbackGatewayContextState ( ) ;
60- if (
61- currentFallbackGatewayContextState . context === ctx &&
62- currentFallbackGatewayContextState . resolveContext === undefined
63- ) {
64- currentFallbackGatewayContextState . context = undefined ;
65- }
66- } ;
67- }
68-
69- export function setFallbackGatewayContextResolver (
70- resolveContext : ( ) => GatewayRequestContext | undefined ,
71- ) : ( ) => void {
72- const fallbackGatewayContextState = getFallbackGatewayContextState ( ) ;
73- fallbackGatewayContextState . context = undefined ;
74- fallbackGatewayContextState . resolveContext = resolveContext ;
75- return ( ) => {
76- const currentFallbackGatewayContextState = getFallbackGatewayContextState ( ) ;
77- if ( currentFallbackGatewayContextState . resolveContext === resolveContext ) {
78- currentFallbackGatewayContextState . context = undefined ;
79- currentFallbackGatewayContextState . resolveContext = undefined ;
80- }
81- } ;
82- }
83-
84- /** Clear the fallback gateway context installed for non-WS dispatch paths. */
85- export function clearFallbackGatewayContext ( ) : void {
86- const fallbackGatewayContextState = getFallbackGatewayContextState ( ) ;
87- fallbackGatewayContextState . context = undefined ;
88- fallbackGatewayContextState . resolveContext = undefined ;
89- }
90-
91- function getFallbackGatewayContext ( ) : GatewayRequestContext | undefined {
92- const fallbackGatewayContextState = getFallbackGatewayContextState ( ) ;
93- const resolved = fallbackGatewayContextState . resolveContext ?.( ) ;
94- return resolved ?? fallbackGatewayContextState . context ;
95- }
32+ import type { GatewayRequestHandler , GatewayRequestOptions } from "./server-methods/types.js" ;
33+ import { getFallbackGatewayContext } from "./server-plugin-fallback-context.js" ;
34+
35+ export {
36+ clearFallbackGatewayContext ,
37+ setFallbackGatewayContext ,
38+ setFallbackGatewayContextResolver ,
39+ } from "./server-plugin-fallback-context.js" ;
9640
9741export function hasInProcessGatewayContext ( ) : boolean {
9842 return Boolean ( getPluginRuntimeGatewayRequestScope ( ) ?. context ?? getFallbackGatewayContext ( ) ) ;
0 commit comments