Skip to content

Commit bfa6708

Browse files
committed
perf: narrow gateway runtime reset imports
1 parent 24bf56c commit bfa6708

4 files changed

Lines changed: 58 additions & 43 deletions

File tree

src/gateway/server-plugin-bootstrap.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { applyPluginAutoEnable } from "../config/plugin-auto-enable.js";
33
import type { OpenClawConfig } from "../config/types.openclaw.js";
44
import type { PluginRegistry } from "../plugins/registry.js";
55
import { pinActivePluginChannelRegistry } from "../plugins/runtime.js";
6-
import { setGatewayNodesRuntime, setGatewaySubagentRuntime } from "../plugins/runtime/index.js";
6+
import {
7+
setGatewayNodesRuntime,
8+
setGatewaySubagentRuntime,
9+
} from "../plugins/runtime/gateway-bindings.js";
710
import type { GatewayRequestHandler } from "./server-methods/types.js";
811
import {
912
createGatewayNodesRuntime,

src/gateway/test-helpers.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
import { drainSystemEvents, peekSystemEvents } from "../infra/system-events.js";
2121
import { rawDataToString } from "../infra/ws.js";
2222
import { resetLogger, setLoggerOverride } from "../logging.js";
23-
import { clearGatewaySubagentRuntime } from "../plugins/runtime/index.js";
23+
import { clearGatewaySubagentRuntime } from "../plugins/runtime/gateway-bindings.js";
2424
import {
2525
DEFAULT_AGENT_ID,
2626
normalizeMainKey,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { resolveGlobalSingleton } from "../../shared/global-singleton.js";
2+
import type { PluginRuntime } from "./types.js";
3+
4+
const GATEWAY_SUBAGENT_SYMBOL: unique symbol = Symbol.for(
5+
"openclaw.plugin.gatewaySubagentRuntime",
6+
) as unknown as typeof GATEWAY_SUBAGENT_SYMBOL;
7+
8+
type GatewaySubagentState = {
9+
subagent: PluginRuntime["subagent"] | undefined;
10+
nodes: PluginRuntime["nodes"] | undefined;
11+
};
12+
13+
export const gatewaySubagentState = resolveGlobalSingleton<GatewaySubagentState>(
14+
GATEWAY_SUBAGENT_SYMBOL,
15+
() => ({
16+
subagent: undefined,
17+
nodes: undefined,
18+
}),
19+
);
20+
21+
/**
22+
* Set the process-global gateway subagent runtime.
23+
* Called during gateway startup so that gateway-bindable plugin runtimes can
24+
* resolve subagent methods dynamically even when their registry was cached
25+
* before the gateway finished loading plugins.
26+
*/
27+
export function setGatewaySubagentRuntime(subagent: PluginRuntime["subagent"]): void {
28+
gatewaySubagentState.subagent = subagent;
29+
}
30+
31+
export function setGatewayNodesRuntime(nodes: PluginRuntime["nodes"]): void {
32+
gatewaySubagentState.nodes = nodes;
33+
}
34+
35+
/**
36+
* Reset the process-global gateway subagent runtime.
37+
* Used by tests to avoid leaking gateway state across module reloads.
38+
*/
39+
export function clearGatewaySubagentRuntime(): void {
40+
gatewaySubagentState.subagent = undefined;
41+
gatewaySubagentState.nodes = undefined;
42+
}

src/plugins/runtime/index.ts

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
listRuntimeMusicGenerationProviders,
99
} from "../../music-generation/runtime.js";
1010
import { RequestScopedSubagentRuntimeError } from "../../plugin-sdk/error-runtime.js";
11-
import { resolveGlobalSingleton } from "../../shared/global-singleton.js";
1211
import {
1312
createLazyRuntimeMethod,
1413
createLazyRuntimeMethodBinder,
@@ -20,6 +19,12 @@ import {
2019
listRuntimeVideoGenerationProviders,
2120
} from "../../video-generation/runtime.js";
2221
import { listWebSearchProviders, runWebSearch } from "../../web-search/runtime.js";
22+
import {
23+
gatewaySubagentState,
24+
setGatewayNodesRuntime,
25+
setGatewaySubagentRuntime,
26+
clearGatewaySubagentRuntime,
27+
} from "./gateway-bindings.js";
2328
import { createRuntimeAgent } from "./runtime-agent.js";
2429
import { defineCachedValue } from "./runtime-cache.js";
2530
import { createRuntimeChannel } from "./runtime-channel.js";
@@ -33,6 +38,11 @@ import { createRuntimeTasks } from "./runtime-tasks.js";
3338
import type { CreatePluginRuntimeOptions, PluginRuntime } from "./types.js";
3439

3540
export type { CreatePluginRuntimeOptions } from "./types.js";
41+
export {
42+
clearGatewaySubagentRuntime,
43+
setGatewayNodesRuntime,
44+
setGatewaySubagentRuntime,
45+
} from "./gateway-bindings.js";
3646

3747
const loadTtsRuntime = createLazyRuntimeModule(() => import("../../tts/tts.js"));
3848
const loadMediaUnderstandingRuntime = createLazyRuntimeModule(
@@ -140,46 +150,6 @@ function createUnavailableSubagentRuntime(): PluginRuntime["subagent"] {
140150
// active gateway subagent dynamically without changing the default behavior for
141151
// ordinary plugin runtimes.
142152

143-
const GATEWAY_SUBAGENT_SYMBOL: unique symbol = Symbol.for(
144-
"openclaw.plugin.gatewaySubagentRuntime",
145-
) as unknown as typeof GATEWAY_SUBAGENT_SYMBOL;
146-
147-
type GatewaySubagentState = {
148-
subagent: PluginRuntime["subagent"] | undefined;
149-
nodes: PluginRuntime["nodes"] | undefined;
150-
};
151-
152-
const gatewaySubagentState = resolveGlobalSingleton<GatewaySubagentState>(
153-
GATEWAY_SUBAGENT_SYMBOL,
154-
() => ({
155-
subagent: undefined,
156-
nodes: undefined,
157-
}),
158-
);
159-
160-
/**
161-
* Set the process-global gateway subagent runtime.
162-
* Called during gateway startup so that gateway-bindable plugin runtimes can
163-
* resolve subagent methods dynamically even when their registry was cached
164-
* before the gateway finished loading plugins.
165-
*/
166-
export function setGatewaySubagentRuntime(subagent: PluginRuntime["subagent"]): void {
167-
gatewaySubagentState.subagent = subagent;
168-
}
169-
170-
export function setGatewayNodesRuntime(nodes: PluginRuntime["nodes"]): void {
171-
gatewaySubagentState.nodes = nodes;
172-
}
173-
174-
/**
175-
* Reset the process-global gateway subagent runtime.
176-
* Used by tests to avoid leaking gateway state across module reloads.
177-
*/
178-
export function clearGatewaySubagentRuntime(): void {
179-
gatewaySubagentState.subagent = undefined;
180-
gatewaySubagentState.nodes = undefined;
181-
}
182-
183153
/**
184154
* Create a late-binding subagent that resolves to:
185155
* 1. An explicitly provided subagent (from runtimeOptions), OR

0 commit comments

Comments
 (0)