Skip to content

Commit 2cf6321

Browse files
committed
fix: handle spinners in npm-app with onChunk
1 parent 5c62bbc commit 2cf6321

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

npm-app/src/client.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export class Client {
205205
private responseComplete: boolean = false
206206
private userInputId: string | undefined
207207
private nonCancelledUserInputIds: string[] = []
208+
private currentOnChunk: ((chunk: string | PrintModeEvent) => void) | undefined
208209

209210
public usageData: UsageData = {
210211
usage: 0,
@@ -959,8 +960,12 @@ export class Client {
959960
// Format the log message for display
960961
const formattedMessage = this.formatLogMessage(level, data, message)
961962

962-
// Display the log message immediately
963-
if (formattedMessage) {
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
964969
process.stdout.write(formattedMessage + '\n')
965970
}
966971
})
@@ -993,6 +998,18 @@ export class Client {
993998
return String(data)
994999
}
9951000

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+
9961013
private showUsageWarning() {
9971014
// Determine user state based on login status and credit balance
9981015
const state = match({
@@ -1304,12 +1321,14 @@ export class Client {
13041321
})
13051322

13061323
this.userInputId = userInputId
1324+
this.currentOnChunk = onChunk
13071325

13081326
const stopResponse = () => {
13091327
responseStopped = true
13101328
unsubscribeChunks()
13111329
unsubscribeComplete()
13121330
this.cancelCurrentInput()
1331+
this.currentOnChunk = undefined
13131332

13141333
const additionalMessages = prompt
13151334
? [
@@ -1531,6 +1550,10 @@ Go to https://www.codebuff.com/config for more information.`) +
15311550
unsubscribeChunks()
15321551
unsubscribeComplete()
15331552
}
1553+
1554+
// Clear the onChunk callback when response is complete
1555+
this.currentOnChunk = undefined
1556+
15341557
resolveResponse({ ...a, wasStoppedByUser: false })
15351558
},
15361559
)

0 commit comments

Comments
 (0)