1- const chalk = require ( 'chalk' ) ;
2- const {
3- buildClusterPrefix,
4- getColorForSender,
5- parseDataField,
6- } = require ( './message-formatter-utils' ) ;
7- const { EVENT_COPY , formatMergeStatus } = require ( './event-copy' ) ;
1+ import chalk = require( 'chalk' ) ;
2+ import formatterUtils = require( './message-formatter-utils' ) ;
3+ import eventCopy = require( './event-copy' ) ;
84
9- const DEFAULT_WRITER = Object . freeze ( { printLine : ( text ) => console . log ( text ) } ) ;
5+ const { buildClusterPrefix, getColorForSender, parseDataField } = formatterUtils ;
6+ const { EVENT_COPY , formatMergeStatus } = eventCopy ;
107
11- function formatAgentError ( msg , clusterPrefix , writer ) {
8+ interface WatchMessageData {
9+ issue_number ?: string | number ;
10+ title ?: string ;
11+ prompt ?: string ;
12+ approved ?: boolean | string ;
13+ summary ?: string ;
14+ errors ?: unknown ;
15+ issues ?: unknown ;
16+ pr_number ?: string | number ;
17+ merged ?: unknown ;
18+ }
19+
20+ interface WatchMessage {
21+ sender : string ;
22+ cluster_id : string ;
23+ topic : string ;
24+ content ?: {
25+ text ?: string ;
26+ data ?: WatchMessageData ;
27+ } ;
28+ }
29+
30+ interface LineWriter {
31+ printLine ( text : string ) : void ;
32+ }
33+
34+ type WatchMessageHandler = (
35+ message : WatchMessage ,
36+ clusterPrefix : string ,
37+ writer : LineWriter
38+ ) => void ;
39+
40+ interface CountableData {
41+ readonly length : number ;
42+ }
43+
44+ const DEFAULT_WRITER : Readonly < LineWriter > = Object . freeze ( {
45+ printLine : ( text : string ) => console . log ( text ) ,
46+ } ) ;
47+
48+ function isCountableData ( value : unknown ) : value is CountableData {
49+ if ( typeof value === 'string' ) return true ;
50+ return (
51+ typeof value === 'object' &&
52+ value !== null &&
53+ 'length' in value &&
54+ typeof value . length === 'number'
55+ ) ;
56+ }
57+
58+ function formatAgentError (
59+ msg : WatchMessage ,
60+ clusterPrefix : string ,
61+ writer : LineWriter
62+ ) : void {
1263 writer . printLine ( `${ clusterPrefix } ${ chalk . bold . red ( `Error: ${ msg . sender } ` ) } ` ) ;
1364 if ( msg . content ?. text ) {
1465 writer . printLine ( `${ clusterPrefix } ${ chalk . red ( msg . content . text ) } ` ) ;
1566 }
1667 writer . printLine ( `${ clusterPrefix } Next: zeroshot logs ${ msg . cluster_id } -f` ) ;
1768}
1869
19- function formatIssueOpened ( msg , clusterPrefix , writer ) {
70+ function formatIssueOpened (
71+ msg : WatchMessage ,
72+ clusterPrefix : string ,
73+ writer : LineWriter
74+ ) : void {
2075 const issueNum = msg . content ?. data ?. issue_number || '' ;
2176 const title = msg . content ?. data ?. title || '' ;
2277 const prompt = msg . content ?. data ?. prompt || msg . content ?. text || '' ;
2378 const taskDesc = title === 'Manual Input' && prompt ? prompt : title ;
2479 const truncatedDesc =
2580 taskDesc && taskDesc . length > 60 ? `${ taskDesc . substring ( 0 , 60 ) } ...` : taskDesc ;
26- const eventText = `Started ${ issueNum ? `#${ issueNum } ` : 'task' } ${ truncatedDesc ? chalk . dim ( ` - ${ truncatedDesc } ` ) : '' } ` ;
81+ const issueLabel = issueNum ? `#${ issueNum } ` : 'task' ;
82+ const description = truncatedDesc ? chalk . dim ( ` - ${ truncatedDesc } ` ) : '' ;
83+ const eventText = `Started ${ issueLabel } ${ description } ` ;
2784 writer . printLine ( `${ clusterPrefix } ${ eventText } ` ) ;
2885}
2986
30- function formatImplementationReady ( msg , clusterPrefix , writer = DEFAULT_WRITER ) {
87+ function formatImplementationReady (
88+ msg : WatchMessage ,
89+ clusterPrefix : string ,
90+ writer : LineWriter = DEFAULT_WRITER
91+ ) : void {
3192 const agentName = getColorForSender ( msg . sender ) ( msg . sender ) ;
3293 writer . printLine (
3394 `${ clusterPrefix } ${ agentName } ${ EVENT_COPY . IMPLEMENTATION_READY . toLowerCase ( ) } `
3495 ) ;
3596}
3697
37- function printRejectionDetails ( data , clusterPrefix , writer ) {
98+ function printRejectionDetails (
99+ data : WatchMessageData ,
100+ clusterPrefix : string ,
101+ writer : LineWriter
102+ ) : void {
38103 const errors = parseDataField ( data . errors ) ;
39104 const issues = parseDataField ( data . issues ) ;
40- if ( errors . length > 0 ) {
105+ if ( isCountableData ( errors ) && errors . length > 0 ) {
41106 const count = JSON . stringify ( errors ) . length ;
42107 writer . printLine (
43108 `${ clusterPrefix } ${ chalk . red ( '•' ) } ${ errors . length } error${ errors . length > 1 ? 's' : '' } (${ count } chars)`
44109 ) ;
45110 }
46- if ( issues . length > 0 ) {
111+ if ( isCountableData ( issues ) && issues . length > 0 ) {
47112 const count = JSON . stringify ( issues ) . length ;
48113 writer . printLine (
49114 `${ clusterPrefix } ${ chalk . yellow ( '•' ) } ${ issues . length } issue${ issues . length > 1 ? 's' : '' } (${ count } chars)`
50115 ) ;
51116 }
52117}
53118
54- function formatValidationResult ( msg , clusterPrefix , writer ) {
119+ function formatValidationResult (
120+ msg : WatchMessage ,
121+ clusterPrefix : string ,
122+ writer : LineWriter
123+ ) : void {
55124 const agentName = getColorForSender ( msg . sender ) ( msg . sender ) ;
56125 const data = msg . content ?. data ;
57126 const approved = data ?. approved === 'true' || data ?. approved === true ;
58127 const status = approved ? chalk . green ( 'Approved' ) : chalk . red ( 'Rejected' ) ;
59128 let eventText = `${ agentName } ${ status } ` ;
60129 if ( data ?. summary && ! approved ) eventText += chalk . dim ( ` - ${ data . summary } ` ) ;
61130 writer . printLine ( `${ clusterPrefix } ${ eventText } ` ) ;
62- if ( ! approved ) printRejectionDetails ( data , clusterPrefix , writer ) ;
131+ if ( ! approved && data ) printRejectionDetails ( data , clusterPrefix , writer ) ;
63132}
64133
65- function formatPrCreated ( msg , clusterPrefix , writer = DEFAULT_WRITER ) {
134+ function formatPrCreated (
135+ msg : WatchMessage ,
136+ clusterPrefix : string ,
137+ writer : LineWriter = DEFAULT_WRITER
138+ ) : void {
66139 const agentName = getColorForSender ( msg . sender ) ( msg . sender ) ;
67140 const prNum = msg . content ?. data ?. pr_number || '' ;
68141 let eventText = `${ agentName } ${ EVENT_COPY . PR_CREATED . toLowerCase ( ) } ${ prNum ? ` #${ prNum } ` : '' } ` ;
@@ -71,33 +144,45 @@ function formatPrCreated(msg, clusterPrefix, writer = DEFAULT_WRITER) {
71144 writer . printLine ( `${ clusterPrefix } ${ eventText } ` ) ;
72145}
73146
74- function formatPrMerged ( msg , clusterPrefix , writer ) {
147+ function formatPrMerged (
148+ msg : WatchMessage ,
149+ clusterPrefix : string ,
150+ writer : LineWriter
151+ ) : void {
75152 const agentName = getColorForSender ( msg . sender ) ( msg . sender ) ;
76153 writer . printLine ( `${ clusterPrefix } ${ agentName } merged PR` ) ;
77154}
78155
79- function formatUnknownTopic ( msg , clusterPrefix , writer ) {
156+ function formatUnknownTopic (
157+ msg : WatchMessage ,
158+ clusterPrefix : string ,
159+ writer : LineWriter
160+ ) : void {
80161 const agentName = getColorForSender ( msg . sender ) ( msg . sender ) ;
81162 const eventText = `${ agentName } ${ msg . topic . toLowerCase ( ) . replace ( / _ / g, ' ' ) } ` ;
82163 writer . printLine ( `${ clusterPrefix } ${ eventText } ` ) ;
83164}
84165
85- function formatWatchMode ( msg , isActive , writer = DEFAULT_WRITER ) {
166+ function formatWatchMode (
167+ msg : WatchMessage ,
168+ isActive : boolean ,
169+ writer : LineWriter = DEFAULT_WRITER
170+ ) : true {
86171 if ( msg . topic === 'AGENT_OUTPUT' || msg . topic === 'AGENT_LIFECYCLE' ) return true ;
87172 const clusterPrefix = buildClusterPrefix ( msg . cluster_id , isActive ) ;
88- const handlers = {
173+ const handlers : Readonly < Record < string , WatchMessageHandler > > = {
89174 AGENT_ERROR : formatAgentError ,
90175 ISSUE_OPENED : formatIssueOpened ,
91176 IMPLEMENTATION_READY : formatImplementationReady ,
92177 VALIDATION_RESULT : formatValidationResult ,
93178 PR_CREATED : formatPrCreated ,
94179 PR_MERGED : formatPrMerged ,
95180 } ;
96- ( handlers [ msg . topic ] || formatUnknownTopic ) ( msg , clusterPrefix , writer ) ;
181+ ( handlers [ msg . topic ] ?? formatUnknownTopic ) ( msg , clusterPrefix , writer ) ;
97182 return true ;
98183}
99184
100- module . exports = {
185+ export = {
101186 formatWatchMode,
102187 formatImplementationReady,
103188 formatPrCreated,
0 commit comments