@@ -4,7 +4,14 @@ import path from "node:path";
44import { fileURLToPath } from "node:url" ;
55import qrcode from "qrcode" ;
66import { createServer , type Plugin , type ViteDevServer } from "vite" ;
7- import type { UserProfile } from "../packages/gateway-protocol/src/index.js" ;
7+ import type {
8+ RequestFrame ,
9+ SystemAgentChatHistoryResult ,
10+ SystemAgentChatQuestion ,
11+ SystemAgentChatResult ,
12+ SystemChangesListResult ,
13+ UserProfile ,
14+ } from "../packages/gateway-protocol/src/index.js" ;
815import { expectDefined } from "../packages/normalization-core/src/expect.js" ;
916import { applySharedChannelFieldHelp } from "../src/config/schema.channel-field-help.js" ;
1017import { applyConfigTierHints , applyResolvedConfigTierHints } from "../src/config/schema.tiers.js" ;
@@ -65,6 +72,7 @@ const NARRATION_DEMO_SESSION_KEY = "agent:main:sidebar-narration-demo";
6572const NARRATION_DEMO_RUN_ID = "mock-sidebar-narration-run" ;
6673const OBSERVER_DEMO_SESSION_KEY = "agent:main:session-observer-demo" ;
6774const OBSERVER_DEMO_RUN_ID = "mock-session-observer-run" ;
75+ const CUSTODIAN_CHAT_REPLY_DELAY_MS = 600 ;
6876
6977const repoRoot = path . resolve ( path . dirname ( fileURLToPath ( import . meta. url ) ) , ".." ) ;
7078const uiRoot = path . join ( repoRoot , "ui" ) ;
@@ -1343,6 +1351,53 @@ async function createChatPickerScenario(
13431351 const cronMocks = buildCronMocks ( Date . now ( ) ) ;
13441352 const channelWizard = buildChannelWizardMocks ( ) ;
13451353 const configMocks = buildConfigMocks ( { swarmEnabled : fixture === "swarm" } ) ;
1354+ const custodianHistory = {
1355+ turns : [
1356+ {
1357+ role : "user" ,
1358+ text : "Can you check whether this system is ready?" ,
1359+ at : baseTime - 18 * 60_000 ,
1360+ } ,
1361+ {
1362+ role : "assistant" ,
1363+ text : "Everything important is connected. I’ll keep watching for changes." ,
1364+ at : baseTime - 17 * 60_000 ,
1365+ } ,
1366+ {
1367+ role : "user" ,
1368+ text : "Please remember that I prefer concise updates." ,
1369+ at : baseTime - 16 * 60_000 ,
1370+ } ,
1371+ ] ,
1372+ } satisfies SystemAgentChatHistoryResult ;
1373+ const custodianChanges = {
1374+ entries : [
1375+ {
1376+ id : "mock-system-agent-model" ,
1377+ at : baseTime - 6 * 60_000 ,
1378+ kind : "operation" ,
1379+ source : "system-agent" ,
1380+ summary : "Updated the default model for the main agent" ,
1381+ changedPaths : [ "agents.defaults.model" ] ,
1382+ } ,
1383+ {
1384+ id : "mock-plugin-install" ,
1385+ at : baseTime - 42 * 60_000 ,
1386+ kind : "config-write" ,
1387+ source : "plugin-install" ,
1388+ summary : "Enabled the Telegram plugin" ,
1389+ changedPaths : [ "plugins.entries.telegram.enabled" ] ,
1390+ } ,
1391+ {
1392+ id : "mock-doctor-repair" ,
1393+ at : baseTime - 2 * 60 * 60_000 ,
1394+ kind : "config-write" ,
1395+ source : "doctor" ,
1396+ summary : "Repaired a stale channel account reference" ,
1397+ changedPaths : [ "channels.whatsapp.defaultAccount" ] ,
1398+ } ,
1399+ ] ,
1400+ } satisfies SystemChangesListResult ;
13461401 return {
13471402 assistantAgentId : "main" ,
13481403 assistantName : "Molty" ,
@@ -1351,6 +1406,9 @@ async function createChatPickerScenario(
13511406 "chat.metadata" ,
13521407 "chat.startup" ,
13531408 "question.list" ,
1409+ "openclaw.changes.list" ,
1410+ "openclaw.chat" ,
1411+ "openclaw.chat.history" ,
13541412 "sessions.diff" ,
13551413 "sessions.files.set" ,
13561414 "system.info" ,
@@ -1473,6 +1531,8 @@ async function createChatPickerScenario(
14731531 // (raw persists, hash advances) because config.get ships a raw fixture.
14741532 "config.get" : configMocks . get ,
14751533 "config.schema" : configMocks . schema ,
1534+ "openclaw.chat.history" : custodianHistory ,
1535+ "openclaw.changes.list" : custodianChanges ,
14761536 // The sidebar recovers pending questions through question.list after the
14771537 // hello handshake, so this remains visible after a mock-page refresh.
14781538 "question.list" : {
@@ -1997,8 +2057,117 @@ function escapeScriptContent(script: string): string {
19972057 return script . replaceAll ( "</script" , "<\\/script" ) ;
19982058}
19992059
2060+ /** Adds the one stateful mock surface the generic scenario fixture cannot express. */
2061+ function installControlUiCustodianMock ( replyDelayMs : number ) : void {
2062+ type MockGatewayControls = {
2063+ deferNext : ( method : string ) => void ;
2064+ resolveDeferred : ( method : string , payload : unknown ) => void ;
2065+ } ;
2066+ type MockWindow = Window & {
2067+ openclawControlUiE2eGateway ?: MockGatewayControls ;
2068+ } ;
2069+
2070+ const gateway = ( window as MockWindow ) . openclawControlUiE2eGateway ;
2071+ const MockWebSocket = window . WebSocket ;
2072+ if ( ! gateway || ! MockWebSocket ) {
2073+ return ;
2074+ }
2075+ const sendDescriptor = Object . getOwnPropertyDescriptor ( MockWebSocket . prototype , "send" ) ;
2076+ const originalSend = sendDescriptor ?. value as WebSocket [ "send" ] | undefined ;
2077+ if ( typeof originalSend !== "function" ) {
2078+ return ;
2079+ }
2080+ const sendOriginal = ( socket : WebSocket , data : Parameters < WebSocket [ "send" ] > [ 0 ] ) : void => {
2081+ Reflect . apply ( originalSend , socket , [ data ] ) ;
2082+ } ;
2083+ const sessionTurns = new Map < string , number > ( ) ;
2084+ MockWebSocket . prototype . send = function ( data ) : void {
2085+ let frame : RequestFrame | null = null ;
2086+ if ( typeof data === "string" ) {
2087+ try {
2088+ frame = JSON . parse ( data ) as RequestFrame ;
2089+ } catch {
2090+ frame = null ;
2091+ }
2092+ }
2093+ if ( frame ?. type !== "req" || frame . method !== "openclaw.chat" ) {
2094+ sendOriginal ( this , data ) ;
2095+ return ;
2096+ }
2097+
2098+ const params =
2099+ frame . params && typeof frame . params === "object"
2100+ ? ( frame . params as { message ?: unknown ; sessionId ?: unknown } )
2101+ : { } ;
2102+ const sessionId =
2103+ typeof params . sessionId === "string" && params . sessionId . trim ( )
2104+ ? params . sessionId
2105+ : "control-ui-custodian-mock" ;
2106+ const message = typeof params . message === "string" ? params . message : undefined ;
2107+ const turn = sessionTurns . get ( sessionId ) ?? 0 ;
2108+ sessionTurns . set ( sessionId , turn + 1 ) ;
2109+ const channelQuestion = {
2110+ id : "mock-channel-choice" ,
2111+ header : "Channel setup" ,
2112+ question : "Which channel would you like to work on?" ,
2113+ options : [
2114+ {
2115+ label : "WhatsApp" ,
2116+ reply : "help me connect WhatsApp" ,
2117+ description : "Review linking and account status." ,
2118+ recommended : true ,
2119+ } ,
2120+ {
2121+ label : "Telegram" ,
2122+ reply : "help me connect Telegram" ,
2123+ description : "Review the bot token and delivery status." ,
2124+ } ,
2125+ {
2126+ label : "Discord" ,
2127+ reply : "help me connect Discord" ,
2128+ description : "Review the bot and server connection." ,
2129+ } ,
2130+ ] ,
2131+ isOther : true ,
2132+ } satisfies SystemAgentChatQuestion ;
2133+ const response : SystemAgentChatResult = message
2134+ ? message . toLowerCase ( ) . includes ( "channel" )
2135+ ? {
2136+ sessionId,
2137+ reply : "I can help with that.\n\nChoose a channel to continue." ,
2138+ action : "none" ,
2139+ question : channelQuestion ,
2140+ }
2141+ : {
2142+ sessionId,
2143+ reply : `I checked the mock system. Everything looks healthy.\n\nThat was demo turn ${ turn } .` ,
2144+ action : "none" ,
2145+ }
2146+ : {
2147+ sessionId,
2148+ reply :
2149+ "Hi — I’m OpenClaw, your system caretaker.\n\nAsk me about setup, channels, or recent changes." ,
2150+ action : "none" ,
2151+ } ;
2152+
2153+ // Defer before forwarding so the generic gateway records the request but
2154+ // cannot race its normal immediate response against this stateful reply.
2155+ gateway . deferNext ( "openclaw.chat" ) ;
2156+ sendOriginal ( this , data ) ;
2157+ window . setTimeout (
2158+ ( ) => gateway . resolveDeferred ( "openclaw.chat" , response ) ,
2159+ message === undefined ? 0 : replyDelayMs ,
2160+ ) ;
2161+ } ;
2162+ }
2163+
2164+ function createCustodianMockInitScript ( ) : string {
2165+ return `(() => { const __name = (target) => target; (${ installControlUiCustodianMock . toString ( ) } )(${ CUSTODIAN_CHAT_REPLY_DELAY_MS } ); })();` ;
2166+ }
2167+
20002168function createMockGatewayPlugin ( scenario : ControlUiMockGatewayScenario ) : Plugin {
20012169 const initScript = escapeScriptContent ( createControlUiMockGatewayInitScript ( scenario ) ) ;
2170+ const custodianInitScript = escapeScriptContent ( createCustodianMockInitScript ( ) ) ;
20022171 const bootstrapBody = JSON . stringify ( createControlUiMockBootstrapConfig ( scenario ) ) ;
20032172 return {
20042173 configureServer ( server ) {
@@ -2012,7 +2181,7 @@ function createMockGatewayPlugin(scenario: ControlUiMockGatewayScenario): Plugin
20122181 transformIndexHtml ( html ) {
20132182 return html . replace (
20142183 "</head>" ,
2015- ` <script data-openclaw-control-ui-mock-gateway>\n${ initScript } \n </script>\n </head>` ,
2184+ ` <script data-openclaw-control-ui-mock-gateway>\n${ initScript } \n${ custodianInitScript } \n </script>\n </head>` ,
20162185 ) ;
20172186 } ,
20182187 } ;
0 commit comments