@@ -20,6 +20,14 @@ type Adapter = {
2020 sendText ( peerId : string , text : string ) : Promise < void > ;
2121} ;
2222
23+ type OutboundKind = "reply" | "system" | "tool" ;
24+
25+ export type BridgeReporter = {
26+ onStatus ?: ( message : string ) => void ;
27+ onInbound ?: ( message : { channel : ChannelName ; peerId : string ; text : string ; fromMe ?: boolean } ) => void ;
28+ onOutbound ?: ( message : { channel : ChannelName ; peerId : string ; text : string ; kind : OutboundKind } ) => void ;
29+ } ;
30+
2331type InboundMessage = {
2432 channel : ChannelName ;
2533 peerId : string ;
@@ -49,7 +57,8 @@ const TOOL_LABELS: Record<string, string> = {
4957 webfetch : "webfetch" ,
5058} ;
5159
52- export async function startBridge ( config : Config , logger : Logger ) {
60+ export async function startBridge ( config : Config , logger : Logger , reporter ?: BridgeReporter ) {
61+ const reportStatus = reporter ?. onStatus ;
5362 const client = createClient ( config ) ;
5463 const store = new BridgeStore ( config . dbPath ) ;
5564 store . seedAllowlist ( "telegram" , config . allowlist . telegram ) ;
@@ -64,12 +73,17 @@ export async function startBridge(config: Config, logger: Logger) {
6473 adapters . set ( "telegram" , createTelegramAdapter ( config , logger , handleInbound ) ) ;
6574 } else {
6675 logger . info ( "telegram adapter disabled" ) ;
76+ reportStatus ?.( "Telegram adapter disabled." ) ;
6777 }
6878
6979 if ( config . whatsappEnabled ) {
70- adapters . set ( "whatsapp" , createWhatsAppAdapter ( config , logger , handleInbound , { printQr : true } ) ) ;
80+ adapters . set (
81+ "whatsapp" ,
82+ createWhatsAppAdapter ( config , logger , handleInbound , { printQr : true , onStatus : reportStatus } ) ,
83+ ) ;
7184 } else {
7285 logger . info ( "whatsapp adapter disabled" ) ;
86+ reportStatus ?.( "WhatsApp adapter disabled." ) ;
7387 }
7488
7589 const sessionQueue = new Map < string , Promise < void > > ( ) ;
@@ -142,7 +156,7 @@ export async function startBridge(config: Config, logger: Logger) {
142156 if ( output ) message += `\n${ output } ` ;
143157 }
144158
145- await sendText ( run . channel , run . peerId , message ) ;
159+ await sendText ( run . channel , run . peerId , message , { kind : "tool" } ) ;
146160 }
147161
148162 if ( event . type === "permission.asked" ) {
@@ -157,7 +171,9 @@ export async function startBridge(config: Config, logger: Logger) {
157171 if ( response === "reject" ) {
158172 const run = activeRuns . get ( permission . sessionID ) ;
159173 if ( run ) {
160- await sendText ( run . channel , run . peerId , "Permission denied. Update configuration to allow tools." ) ;
174+ await sendText ( run . channel , run . peerId , "Permission denied. Update configuration to allow tools." , {
175+ kind : "system" ,
176+ } ) ;
161177 }
162178 }
163179 }
@@ -166,9 +182,18 @@ export async function startBridge(config: Config, logger: Logger) {
166182 logger . error ( { error } , "event stream closed" ) ;
167183 } ) ;
168184
169- async function sendText ( channel : ChannelName , peerId : string , text : string ) {
185+ async function sendText (
186+ channel : ChannelName ,
187+ peerId : string ,
188+ text : string ,
189+ options : { kind ?: OutboundKind ; display ?: boolean } = { } ,
190+ ) {
170191 const adapter = adapters . get ( channel ) ;
171192 if ( ! adapter ) return ;
193+ const kind = options . kind ?? "system" ;
194+ if ( options . display !== false ) {
195+ reporter ?. onOutbound ?.( { channel, peerId, text, kind } ) ;
196+ }
172197 const chunks = chunkText ( text , adapter . maxTextLength ) ;
173198 for ( const chunk of chunks ) {
174199 logger . info ( { channel, peerId, length : chunk . length } , "sending message" ) ;
@@ -199,6 +224,7 @@ export async function startBridge(config: Config, logger: Logger) {
199224 inbound . channel ,
200225 inbound . peerId ,
201226 "Access denied. Ask the owner to allowlist your number." ,
227+ { kind : "system" } ,
202228 ) ;
203229 return ;
204230 }
@@ -211,6 +237,7 @@ export async function startBridge(config: Config, logger: Logger) {
211237 inbound . channel ,
212238 inbound . peerId ,
213239 "Pairing queue full. Ask the owner to approve pending requests." ,
240+ { kind : "system" } ,
214241 ) ;
215242 return ;
216243 }
@@ -223,16 +250,24 @@ export async function startBridge(config: Config, logger: Logger) {
223250 inbound . channel ,
224251 inbound . peerId ,
225252 `Pairing required. Ask the owner to approve code: ${ code } ` ,
253+ { kind : "system" } ,
226254 ) ;
227255 return ;
228256 }
229257 } else if ( config . allowlist [ inbound . channel ] . size > 0 ) {
230258 if ( ! store . isAllowed ( inbound . channel , peerKey ) ) {
231- await sendText ( inbound . channel , inbound . peerId , "Access denied." ) ;
259+ await sendText ( inbound . channel , inbound . peerId , "Access denied." , { kind : "system" } ) ;
232260 return ;
233261 }
234262 }
235263
264+ reporter ?. onInbound ?.( {
265+ channel : inbound . channel ,
266+ peerId : inbound . peerId ,
267+ text : inbound . text ,
268+ fromMe : inbound . fromMe ,
269+ } ) ;
270+
236271 const session = store . getSession ( inbound . channel , peerKey ) ;
237272 const sessionID = session ?. session_id ?? ( await createSession ( { ...inbound , peerId : peerKey } ) ) ;
238273
@@ -258,13 +293,17 @@ export async function startBridge(config: Config, logger: Logger) {
258293 . trim ( ) ;
259294
260295 if ( reply ) {
261- await sendText ( inbound . channel , inbound . peerId , reply ) ;
296+ await sendText ( inbound . channel , inbound . peerId , reply , { kind : "reply" } ) ;
262297 } else {
263- await sendText ( inbound . channel , inbound . peerId , "No response generated. Try again." ) ;
298+ await sendText ( inbound . channel , inbound . peerId , "No response generated. Try again." , {
299+ kind : "system" ,
300+ } ) ;
264301 }
265302 } catch ( error ) {
266303 logger . error ( { error } , "prompt failed" ) ;
267- await sendText ( inbound . channel , inbound . peerId , "Error: failed to reach OpenCode." ) ;
304+ await sendText ( inbound . channel , inbound . peerId , "Error: failed to reach OpenCode." , {
305+ kind : "system" ,
306+ } ) ;
268307 } finally {
269308 activeRuns . delete ( sessionID ) ;
270309 }
@@ -301,9 +340,11 @@ export async function startBridge(config: Config, logger: Logger) {
301340
302341 for ( const adapter of adapters . values ( ) ) {
303342 await adapter . start ( ) ;
343+ reportStatus ?.( `${ adapter . name === "whatsapp" ? "WhatsApp" : "Telegram" } adapter started.` ) ;
304344 }
305345
306346 logger . info ( { channels : Array . from ( adapters . keys ( ) ) } , "bridge started" ) ;
347+ reportStatus ?.( `Bridge running. Logs: ${ config . logFile } ` ) ;
307348
308349 return {
309350 async stop ( ) {
0 commit comments