@@ -5,6 +5,7 @@ import { resetToolStream } from "./app-tool-stream.ts";
55import type { OpenClawApp } from "./app.ts" ;
66import { abortChatRun , loadChatHistory , sendChatMessage } from "./controllers/chat.ts" ;
77import { loadSessions } from "./controllers/sessions.ts" ;
8+ import type { GatewayBrowserClient } from "./gateway.ts" ;
89import type { GatewayHelloOk } from "./gateway.ts" ;
910import { normalizeBasePath } from "./navigation.ts" ;
1011import type { ChatAttachment , ChatQueueItem } from "./ui-types.ts" ;
@@ -25,11 +26,111 @@ export type ChatHost = {
2526} ;
2627
2728export const CHAT_SESSIONS_ACTIVE_MINUTES = 120 ;
29+ const CHAT_RUN_WATCHDOG_IDLE_MS = 15_000 ;
30+ const CHAT_RUN_WATCHDOG_RETRY_MS = 5_000 ;
31+ const CHAT_RUN_WATCHDOG_WAIT_TIMEOUT_MS = 50 ;
2832
2933export function isChatBusy ( host : ChatHost ) {
3034 return host . chatSending || Boolean ( host . chatRunId ) ;
3135}
3236
37+ type ChatRunWatchdogHost = ChatHost & {
38+ client : GatewayBrowserClient | null ;
39+ chatLoading : boolean ;
40+ chatMessages : unknown [ ] ;
41+ chatThinkingLevel : string | null ;
42+ chatRunLastActivityAt : number | null ;
43+ chatRunWatchdogTimer : number | null ;
44+ chatRunWatchdogProbeInFlight : boolean ;
45+ chatStream : string | null ;
46+ chatStreamStartedAt : number | null ;
47+ lastError : string | null ;
48+ } ;
49+
50+ function clearChatRunWatchdogTimer ( host : ChatRunWatchdogHost ) {
51+ if ( host . chatRunWatchdogTimer !== null ) {
52+ globalThis . clearTimeout ( host . chatRunWatchdogTimer ) ;
53+ host . chatRunWatchdogTimer = null ;
54+ }
55+ }
56+
57+ export function resetChatRunWatchdog ( host : ChatRunWatchdogHost ) {
58+ clearChatRunWatchdogTimer ( host ) ;
59+ host . chatRunWatchdogProbeInFlight = false ;
60+ if ( ! host . chatRunId ) {
61+ host . chatRunLastActivityAt = null ;
62+ }
63+ }
64+
65+ async function runChatRunWatchdog ( host : ChatRunWatchdogHost , runId : string ) {
66+ if ( ! host . connected || ! host . client || ! host . chatRunId || host . chatRunId !== runId ) {
67+ resetChatRunWatchdog ( host ) ;
68+ return ;
69+ }
70+ if ( host . chatRunWatchdogProbeInFlight ) {
71+ scheduleChatRunWatchdog ( host , CHAT_RUN_WATCHDOG_RETRY_MS ) ;
72+ return ;
73+ }
74+
75+ host . chatRunWatchdogProbeInFlight = true ;
76+ try {
77+ const result = await host . client . request < { status ?: string } > ( "agent.wait" , {
78+ runId,
79+ timeoutMs : CHAT_RUN_WATCHDOG_WAIT_TIMEOUT_MS ,
80+ } ) ;
81+ if ( host . chatRunId !== runId ) {
82+ return ;
83+ }
84+ if ( result ?. status === "ok" || result ?. status === "error" ) {
85+ host . chatRunId = null ;
86+ host . chatStream = null ;
87+ host . chatStreamStartedAt = null ;
88+ host . chatRunLastActivityAt = null ;
89+ resetToolStream ( host as unknown as Parameters < typeof resetToolStream > [ 0 ] ) ;
90+ try {
91+ await loadChatHistory ( host as unknown as OpenClawApp ) ;
92+ } finally {
93+ await flushChatQueue ( host ) ;
94+ }
95+ return ;
96+ }
97+ } catch ( err ) {
98+ host . lastError = String ( err ) ;
99+ } finally {
100+ host . chatRunWatchdogProbeInFlight = false ;
101+ }
102+
103+ if ( host . chatRunId === runId ) {
104+ scheduleChatRunWatchdog ( host , CHAT_RUN_WATCHDOG_RETRY_MS ) ;
105+ }
106+ }
107+
108+ export function scheduleChatRunWatchdog ( host : ChatRunWatchdogHost , delayMs ?: number ) {
109+ clearChatRunWatchdogTimer ( host ) ;
110+ if ( ! host . connected || ! host . client || ! host . chatRunId ) {
111+ resetChatRunWatchdog ( host ) ;
112+ return ;
113+ }
114+
115+ const now = Date . now ( ) ;
116+ const lastActivityAt = host . chatRunLastActivityAt ?? now ;
117+ const delay =
118+ delayMs ?? Math . max ( 0 , CHAT_RUN_WATCHDOG_IDLE_MS - Math . max ( 0 , now - lastActivityAt ) ) ;
119+ const runId = host . chatRunId ;
120+ host . chatRunWatchdogTimer = globalThis . setTimeout ( ( ) => {
121+ host . chatRunWatchdogTimer = null ;
122+ void runChatRunWatchdog ( host , runId ) ;
123+ } , delay ) ;
124+ }
125+
126+ export function noteChatRunActivity ( host : ChatRunWatchdogHost , now = Date . now ( ) ) {
127+ if ( ! host . chatRunId ) {
128+ return ;
129+ }
130+ host . chatRunLastActivityAt = now ;
131+ scheduleChatRunWatchdog ( host ) ;
132+ }
133+
33134export function isChatStopCommand ( text : string ) {
34135 const trimmed = text . trim ( ) ;
35136 if ( ! trimmed ) {
@@ -65,7 +166,10 @@ export async function handleAbortChat(host: ChatHost) {
65166 return ;
66167 }
67168 host . chatMessage = "" ;
68- await abortChatRun ( host as unknown as OpenClawApp ) ;
169+ const ok = await abortChatRun ( host as unknown as OpenClawApp ) ;
170+ if ( ok && host . chatRunId ) {
171+ noteChatRunActivity ( host as unknown as ChatRunWatchdogHost ) ;
172+ }
69173}
70174
71175function enqueueChatMessage (
0 commit comments