@@ -18,6 +18,7 @@ type Adapter = {
1818 start ( ) : Promise < void > ;
1919 stop ( ) : Promise < void > ;
2020 sendText ( peerId : string , text : string ) : Promise < void > ;
21+ sendTyping ?: ( peerId : string ) => Promise < void > ;
2122} ;
2223
2324type OutboundKind = "reply" | "system" | "tool" ;
@@ -36,12 +37,19 @@ type InboundMessage = {
3637 fromMe ?: boolean ;
3738} ;
3839
40+ type ModelRef = {
41+ providerID : string ;
42+ modelID : string ;
43+ } ;
44+
3945type RunState = {
4046 sessionID : string ;
4147 channel : ChannelName ;
4248 peerId : string ;
4349 toolUpdatesEnabled : boolean ;
4450 seenToolStates : Map < string , string > ;
51+ thinkingLabel ?: string ;
52+ thinkingActive ?: boolean ;
4553} ;
4654
4755const TOOL_LABELS : Record < string , string > = {
@@ -57,6 +65,13 @@ const TOOL_LABELS: Record<string, string> = {
5765 webfetch : "webfetch" ,
5866} ;
5967
68+ const CHANNEL_LABELS : Record < ChannelName , string > = {
69+ whatsapp : "WhatsApp" ,
70+ telegram : "Telegram" ,
71+ } ;
72+
73+ const TYPING_INTERVAL_MS = 6000 ;
74+
6075export async function startBridge ( config : Config , logger : Logger , reporter ?: BridgeReporter ) {
6176 const reportStatus = reporter ?. onStatus ;
6277 const client = createClient ( config ) ;
@@ -88,6 +103,65 @@ export async function startBridge(config: Config, logger: Logger, reporter?: Bri
88103
89104 const sessionQueue = new Map < string , Promise < void > > ( ) ;
90105 const activeRuns = new Map < string , RunState > ( ) ;
106+ const sessionModels = new Map < string , ModelRef > ( ) ;
107+ const typingLoops = new Map < string , NodeJS . Timeout > ( ) ;
108+
109+ const formatPeer = ( channel : ChannelName , peerId : string ) =>
110+ channel === "whatsapp" ? normalizeWhatsAppId ( peerId ) : peerId ;
111+
112+ const formatModelLabel = ( model ?: ModelRef ) =>
113+ model ? `${ model . providerID } /${ model . modelID } ` : null ;
114+
115+ const extractModelRef = ( info : unknown ) : ModelRef | null => {
116+ if ( ! info || typeof info !== "object" ) return null ;
117+ const record = info as { role ?: unknown ; model ?: unknown } ;
118+ if ( record . role !== "user" ) return null ;
119+ if ( ! record . model || typeof record . model !== "object" ) return null ;
120+ const model = record . model as { providerID ?: unknown ; modelID ?: unknown } ;
121+ if ( typeof model . providerID !== "string" || typeof model . modelID !== "string" ) return null ;
122+ return { providerID : model . providerID , modelID : model . modelID } ;
123+ } ;
124+
125+ const reportThinking = ( run : RunState ) => {
126+ if ( ! reportStatus ) return ;
127+ const modelLabel = formatModelLabel ( sessionModels . get ( run . sessionID ) ) ;
128+ const nextLabel = modelLabel ? `Thinking (${ modelLabel } )` : "Thinking..." ;
129+ if ( run . thinkingLabel === nextLabel && run . thinkingActive ) return ;
130+ run . thinkingLabel = nextLabel ;
131+ run . thinkingActive = true ;
132+ reportStatus ( `[${ CHANNEL_LABELS [ run . channel ] } ] ${ formatPeer ( run . channel , run . peerId ) } ${ nextLabel } ` ) ;
133+ } ;
134+
135+ const reportDone = ( run : RunState ) => {
136+ if ( ! reportStatus || ! run . thinkingActive ) return ;
137+ const modelLabel = formatModelLabel ( sessionModels . get ( run . sessionID ) ) ;
138+ const suffix = modelLabel ? ` (${ modelLabel } )` : "" ;
139+ reportStatus ( `[${ CHANNEL_LABELS [ run . channel ] } ] ${ formatPeer ( run . channel , run . peerId ) } Done${ suffix } ` ) ;
140+ run . thinkingActive = false ;
141+ } ;
142+
143+ const startTyping = ( run : RunState ) => {
144+ const adapter = adapters . get ( run . channel ) ;
145+ if ( ! adapter ?. sendTyping ) return ;
146+ if ( typingLoops . has ( run . sessionID ) ) return ;
147+ const sendTyping = async ( ) => {
148+ try {
149+ await adapter . sendTyping ?.( run . peerId ) ;
150+ } catch ( error ) {
151+ logger . warn ( { error, channel : run . channel } , "typing update failed" ) ;
152+ }
153+ } ;
154+ void sendTyping ( ) ;
155+ const timer = setInterval ( sendTyping , TYPING_INTERVAL_MS ) ;
156+ typingLoops . set ( run . sessionID , timer ) ;
157+ } ;
158+
159+ const stopTyping = ( sessionID : string ) => {
160+ const timer = typingLoops . get ( sessionID ) ;
161+ if ( ! timer ) return ;
162+ clearInterval ( timer ) ;
163+ typingLoops . delete ( sessionID ) ;
164+ } ;
91165
92166 let opencodeHealthy = false ;
93167 let opencodeVersion : string | undefined ;
@@ -133,6 +207,47 @@ export async function startBridge(config: Config, logger: Logger, reporter?: Bri
133207 const event = normalizeEvent ( raw as any ) ;
134208 if ( ! event ) continue ;
135209
210+ if ( event . type === "message.updated" ) {
211+ if ( event . properties && typeof event . properties === "object" ) {
212+ const record = event . properties as Record < string , unknown > ;
213+ const info = record . info as Record < string , unknown > | undefined ;
214+ const sessionID = typeof info ?. sessionID === "string" ? ( info . sessionID as string ) : null ;
215+ const model = extractModelRef ( info ) ;
216+ if ( sessionID && model ) {
217+ sessionModels . set ( sessionID , model ) ;
218+ const run = activeRuns . get ( sessionID ) ;
219+ if ( run ) reportThinking ( run ) ;
220+ }
221+ }
222+ }
223+
224+ if ( event . type === "session.status" ) {
225+ if ( event . properties && typeof event . properties === "object" ) {
226+ const record = event . properties as Record < string , unknown > ;
227+ const sessionID = typeof record . sessionID === "string" ? record . sessionID : null ;
228+ const status = record . status as { type ?: unknown } | undefined ;
229+ if ( sessionID && ( status ?. type === "busy" || status ?. type === "retry" ) ) {
230+ const run = activeRuns . get ( sessionID ) ;
231+ if ( run ) {
232+ reportThinking ( run ) ;
233+ startTyping ( run ) ;
234+ }
235+ }
236+ }
237+ }
238+
239+ if ( event . type === "session.idle" ) {
240+ if ( event . properties && typeof event . properties === "object" ) {
241+ const record = event . properties as Record < string , unknown > ;
242+ const sessionID = typeof record . sessionID === "string" ? record . sessionID : null ;
243+ if ( sessionID ) {
244+ stopTyping ( sessionID ) ;
245+ const run = activeRuns . get ( sessionID ) ;
246+ if ( run ) reportDone ( run ) ;
247+ }
248+ }
249+ }
250+
136251 if ( event . type === "message.part.updated" ) {
137252 const part = ( event . properties as { part ?: any } ) ?. part ;
138253 if ( ! part ?. sessionID ) continue ;
@@ -280,6 +395,8 @@ export async function startBridge(config: Config, logger: Logger, reporter?: Bri
280395 seenToolStates : new Map ( ) ,
281396 } ;
282397 activeRuns . set ( sessionID , runState ) ;
398+ reportThinking ( runState ) ;
399+ startTyping ( runState ) ;
283400 try {
284401 const response = await client . session . prompt ( {
285402 sessionID,
@@ -305,6 +422,8 @@ export async function startBridge(config: Config, logger: Logger, reporter?: Bri
305422 kind : "system" ,
306423 } ) ;
307424 } finally {
425+ stopTyping ( sessionID ) ;
426+ reportDone ( runState ) ;
308427 activeRuns . delete ( sessionID ) ;
309428 }
310429 } ) ;
@@ -320,6 +439,10 @@ export async function startBridge(config: Config, logger: Logger, reporter?: Bri
320439 if ( ! sessionID ) throw new Error ( "Failed to create session" ) ;
321440 store . upsertSession ( message . channel , message . peerId , sessionID ) ;
322441 logger . info ( { sessionID, channel : message . channel , peerId : message . peerId } , "session created" ) ;
442+ reportStatus ?.(
443+ `${ CHANNEL_LABELS [ message . channel ] } session created for ${ formatPeer ( message . channel , message . peerId ) } (ID: ${ sessionID } ).` ,
444+ ) ;
445+ await sendText ( message . channel , message . peerId , "🧭 Session started." , { kind : "system" } ) ;
323446 return sessionID ;
324447 }
325448
@@ -351,6 +474,10 @@ export async function startBridge(config: Config, logger: Logger, reporter?: Bri
351474 eventAbort . abort ( ) ;
352475 clearInterval ( healthTimer ) ;
353476 if ( stopHealthServer ) stopHealthServer ( ) ;
477+ for ( const timer of typingLoops . values ( ) ) {
478+ clearInterval ( timer ) ;
479+ }
480+ typingLoops . clear ( ) ;
354481 for ( const adapter of adapters . values ( ) ) {
355482 await adapter . stop ( ) ;
356483 }
0 commit comments