@@ -258,6 +258,8 @@ export async function createMockSdkPackage(rootDir, options = {}) {
258258 const externalDir = path . join ( rootDir , "mock-modules" , "external" ) ;
259259 await mkdir ( pluginSdkDir , { recursive : true } ) ;
260260 await mkdir ( externalDir , { recursive : true } ) ;
261+ const singletonModuleUrl = pathToFileURL ( path . join ( rootDir , "mock-singletons.mjs" ) ) . href ;
262+ await writeFile ( new URL ( singletonModuleUrl ) , mockSingletonSource ( ) , "utf8" ) ;
261263 const imports = options . pluginRoot ? await collectRuntimeImports ( options . pluginRoot ) : emptyRuntimeImports ( ) ;
262264 await writeFile (
263265 path . join ( packageDir , "package.json" ) ,
@@ -280,13 +282,14 @@ export async function createMockSdkPackage(rootDir, options = {}) {
280282 ...mockSdkExportNames ,
281283 ...( imports . bySpecifier . get ( "openclaw/plugin-sdk" ) ?? [ ] ) ,
282284 ] ) ;
283- await writeFile ( path . join ( pluginSdkDir , "index.js" ) , mockSdkSource ( rootExportNames ) , "utf8" ) ;
285+ await writeFile ( path . join ( pluginSdkDir , "index.js" ) , mockSdkSource ( rootExportNames , { singletonModuleUrl } ) , "utf8" ) ;
284286 for ( const [ subpath , exportNames ] of Object . entries ( mockSdkSubpathExports ) ) {
285287 const specifier = `openclaw/plugin-sdk/${ subpath } ` ;
286288 await writeFile (
287289 path . join ( pluginSdkDir , `${ subpath } .js` ) ,
288290 mockSdkSubpathSource ( exportNames , imports . bySpecifier . get ( specifier ) ?? new Set ( ) , {
289291 zod : subpath === "zod" ,
292+ singletonModuleUrl,
290293 } ) ,
291294 "utf8" ,
292295 ) ;
@@ -312,6 +315,7 @@ export async function createMockSdkPackage(rootDir, options = {}) {
312315 ] ) , {
313316 includeSdkRuntime : true ,
314317 zod : relative === "zod" ,
318+ singletonModuleUrl,
315319 } ) ,
316320 "utf8" ,
317321 ) ;
@@ -640,7 +644,7 @@ function dynamicMockModuleSource(exportNames, options = {}) {
640644 addZodExports ( names ) ;
641645 }
642646 return `${ genericMockRuntimeSource ( options ) }
643- ${ [ ...names ] . map ( genericExportStatement ) . join ( "\n" ) }
647+ ${ [ ...names ] . map ( ( name ) => genericExportStatement ( name , options ) ) . join ( "\n" ) }
644648
645649export default ${ options . zod ? "createZNamespace()" : 'createMockValue("default")' } ;
646650` ;
@@ -713,7 +717,45 @@ function isValidExportName(name) {
713717 return name !== "default" && / ^ [ A - Z a - z _ $ ] [ \w $ ] * $ / u. test ( name ) ;
714718}
715719
716- function genericExportStatement ( name ) {
720+ function mockSingletonSource ( ) {
721+ // All SDK barrels share this capture-owned module. Keep factory state local,
722+ // while respecting prepopulated host slots without changing foreign globals.
723+ return `const values = new Map();
724+ const runtimeSlots = new Map();
725+ export function resolveGlobalSingleton(key, create, reset, lifecycle = "close-and-restart") {
726+ if (!values.has(key)) values.set(key, { value: Object.hasOwn(globalThis, key) ? globalThis[key] : create() });
727+ const slot = values.get(key);
728+ if (reset) Object.assign(slot, { reset, lifecycle });
729+ return slot.value;
730+ }
731+ export function resolveGlobalMap(key, reset, lifecycle) {
732+ return resolveGlobalSingleton(key, () => new Map(), typeof reset === "string" ? (value) => value.clear() : reset, typeof reset === "string" ? reset : lifecycle);
733+ }
734+ export async function drainGlobalSingletonLifecycleState() {
735+ throw new Error("SDK mock does not execute global singleton lifecycle resets");
736+ }
737+ export function createPluginRuntimeStore(options) {
738+ const isolated = typeof options === "string";
739+ const pluginId = !isolated && "pluginId" in options ? options.pluginId.trim() : undefined;
740+ if (pluginId === "") throw new Error("createPluginRuntimeStore: pluginId must not be empty");
741+ const key = pluginId === undefined ? options.key : "plugin-runtime:" + pluginId;
742+ const errorMessage = isolated ? options : options.errorMessage;
743+ const slot = isolated ? { runtime: null } : runtimeSlots.get(key) ?? { runtime: null };
744+ if (!isolated) runtimeSlots.set(key, slot);
745+ return {
746+ setRuntime(next) { slot.runtime = next; },
747+ clearRuntime() { slot.runtime = null; },
748+ tryGetRuntime() { return slot.runtime ?? null; },
749+ getRuntime() { if (slot.runtime == null) throw new Error(errorMessage); return slot.runtime; },
750+ };
751+ }
752+ ` ;
753+ }
754+
755+ function genericExportStatement ( name , options = { } ) {
756+ if ( options . singletonModuleUrl && [ "resolveGlobalSingleton" , "resolveGlobalMap" , "drainGlobalSingletonLifecycleState" , "createPluginRuntimeStore" ] . includes ( name ) ) {
757+ return `import { ${ name } } from ${ JSON . stringify ( options . singletonModuleUrl ) } ;\nexport { ${ name } };` ;
758+ }
717759 if ( name === "asObjectRecord" ) {
718760 return "export function asObjectRecord(value) { return isPlainObject(value) ? value : null; }" ;
719761 }
@@ -1169,7 +1211,7 @@ function createTypeNamespace() {
11691211` ;
11701212}
11711213
1172- function mockSdkSource ( exportNames = mockSdkExportNames ) {
1214+ function mockSdkSource ( exportNames = mockSdkExportNames , options = { } ) {
11731215 const dynamicExportNames = [ ...exportNames ] . filter ( ( name ) => ! mockSdkExportNames . includes ( name ) ) ;
11741216 return `function normalizeEntry(entry) {
11751217 return typeof entry === "function" ? { register: entry } : entry;
@@ -2032,7 +2074,7 @@ export const OPENROUTER_THINKING_STREAM_HOOKS = buildProviderStreamFamilyHooks("
20322074export const TOOL_STREAM_DEFAULT_ON_HOOKS = buildProviderStreamFamilyHooks("tool-stream-default");
20332075export const pluginSdkMock = true;
20342076${ dynamicExportNames . length > 0 ? mockValueRuntimeSource ( ) : "" }
2035- ${ dynamicExportNames . map ( genericExportStatement ) . join ( "\n" ) }
2077+ ${ dynamicExportNames . map ( ( name ) => genericExportStatement ( name , options ) ) . join ( "\n" ) }
20362078
20372079export default {
20382080${ [ ...exportNames ] . map ( ( name ) => ` ${ name } ,` ) . join ( "\n" ) }
@@ -2086,7 +2128,7 @@ function mockSdkSubpathSource(staticExportNames, importedExportNames, options =
20862128 const dynamicNames = [ ...importedExportNames ] . filter ( ( name ) => ! staticNames . has ( name ) ) ;
20872129 return `${ [ ...staticNames ] . map ( ( name ) => `export { ${ name } } from "./index.js";` ) . join ( "\n" ) }
20882130${ dynamicNames . length > 0 ? genericMockRuntimeSource ( { includeSdkRuntime : true , zod : options . zod } ) : "" }
2089- ${ dynamicNames . map ( genericExportStatement ) . join ( "\n" ) }
2131+ ${ dynamicNames . map ( ( name ) => genericExportStatement ( name , options ) ) . join ( "\n" ) }
20902132export { default } from "./index.js";
20912133` ;
20922134}
0 commit comments