Skip to content

Commit ee8f10c

Browse files
committed
fix: cleaner logic for logging client-side
1 parent aa389dd commit ee8f10c

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

npm-app/src/client.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -956,32 +956,40 @@ export class Client {
956956
// Handle handleSteps log streaming
957957
this.webSocket.subscribe('handlesteps-log-chunk', (action) => {
958958
const { agentId, level, data, message } = action
959+
const formattedMessage = this.formatLogMessage(
960+
level,
961+
data,
962+
message,
963+
agentId,
964+
)
959965

960-
// Format the log message for display
961-
const formattedMessage = this.formatLogMessage(level, data, message)
962-
963-
// Display the log message using onChunk if we're in an active user input session
964-
if (formattedMessage && this.userInputId) {
965-
// Use the onChunk callback to properly handle spinner state
966-
this.handleLogChunk(formattedMessage + '\n')
967-
} else if (formattedMessage) {
968-
// Fallback to direct stdout for non-user-input scenarios
966+
if (this.currentOnChunk && this.userInputId) {
967+
this.currentOnChunk(formattedMessage + '\n')
968+
} else {
969969
process.stdout.write(formattedMessage + '\n')
970970
}
971971
})
972972
}
973973

974-
private formatLogMessage(level: string, data: any, message?: string): string {
974+
private formatLogMessage(
975+
level: string,
976+
data: any,
977+
message?: string,
978+
agentId?: string,
979+
): string {
975980
const timestamp = new Date().toISOString().substring(11, 23) // HH:MM:SS.mmm
976981
const levelColors = { debug: blue, info: green, warn: yellow, error: red }
977982
const levelColor =
978983
levelColors[level as keyof typeof levelColors] || ((s: string) => s)
979984

980985
const timeTag = `[${timestamp}]`
981986
const levelTag = levelColor(`[${level.toUpperCase()}]`)
987+
const agentTag = agentId ? `[Agent ${agentId}]` : ''
982988
const dataStr = this.serializeLogData(data)
983989

984-
return [timeTag, levelTag, message, dataStr].filter(Boolean).join(' ')
990+
return [timeTag, levelTag, agentTag, message, dataStr]
991+
.filter(Boolean)
992+
.join(' ')
985993
}
986994

987995
private serializeLogData(data: any): string {
@@ -998,18 +1006,6 @@ export class Client {
9981006
return String(data)
9991007
}
10001008

1001-
/**
1002-
* Handle log chunks by using the current onChunk callback if available
1003-
*/
1004-
private handleLogChunk(formattedMessage: string): void {
1005-
if (this.currentOnChunk) {
1006-
this.currentOnChunk(formattedMessage)
1007-
} else {
1008-
// Fallback to direct stdout if no onChunk callback is available
1009-
process.stdout.write(formattedMessage)
1010-
}
1011-
}
1012-
10131009
private showUsageWarning() {
10141010
// Determine user state based on login status and credit balance
10151011
const state = match({

0 commit comments

Comments
 (0)