11import { writeFileSync } from "fs"
22
3- /**
4- * Send a Warp notification via OSC 777 escape sequence.
5- * Only emits when Warp declares cli-agent protocol support,
6- * avoiding garbled output in other terminals (and working over SSH).
7- */
3+ const MAX_OSC_LENGTH = 4096
4+
5+ function escapeOSCString ( str : string ) : string {
6+ return str
7+ . replace ( / \x1b / g, "\x1b\x5d" )
8+ . replace ( / \x07 / g, "\x1b\x5c\x07" )
9+ . replace ( / ; / g, "\\;" )
10+ }
11+
812function warpNotify ( title : string , body : string ) : void {
913 if ( ! process . env . WARP_CLI_AGENT_PROTOCOL_VERSION ) return
1014
11- try {
12- // OSC 777 format: \033]777;notify;<title>;<body>\007
13- const sequence = `\x1b]777;notify;${ title } ;${ body } \x07`
14- writeFileSync ( "/dev/tty" , sequence )
15- } catch {
16- // Silently ignore if /dev/tty is not available
15+ const maxBodyLength = MAX_OSC_LENGTH - title . length - 20
16+ const escapedBody = escapeOSCString ( body )
17+ const truncatedBody =
18+ escapedBody . length > maxBodyLength
19+ ? escapedBody . slice ( 0 , maxBodyLength - 3 ) + "..."
20+ : escapedBody
21+
22+ const sequence = `\x1b]777;notify;${ title } ;${ truncatedBody } \x07`
23+
24+ let lastError : Error | null = null
25+ for ( let attempt = 0 ; attempt < 3 ; attempt ++ ) {
26+ try {
27+ writeFileSync ( "/dev/tty" , sequence )
28+ return
29+ } catch ( err ) {
30+ lastError = err as Error
31+ }
1732 }
33+
34+ console . error (
35+ `[opencode-warp] Failed to send Warp notification after 3 attempts: ${ lastError ?. message } ` ,
36+ )
1837}
1938
20- export { warpNotify }
39+ export { warpNotify }
0 commit comments