diff --git a/src/outputFormat.ts b/src/outputFormat.ts index e57579de..fe3c30f9 100644 --- a/src/outputFormat.ts +++ b/src/outputFormat.ts @@ -19,7 +19,23 @@ export class OutputFormat { this.quiet = opts.quiet; this.dataArr = []; } - onInit(opts: { + async write(data: string): Promise { + return new Promise((resolve, reject) => { + const result = process.stdout.write(data, (error) => { + if (error) { + reject(error); + } else { + resolve(); + } + }); + // If the buffer is full, wait for the 'drain' event + if (!result) { + process.stdout.once('drain', resolve); + } + }); + } + + async onInit(opts: { text: string; sessionId: string; cwd: string; @@ -39,23 +55,25 @@ export class OutputFormat { tools: opts.tools.map((tool) => tool.name), }; if (this.format === 'stream-json') { - console.log(JSON.stringify(data)); + await this.write(JSON.stringify(data) + '\n'); } else if (this.format === 'json') { this.dataArr.push(data); } } - onMessage(opts: { message: any }) { + + async onMessage(opts: { message: any }) { if (!this.quiet) { return; } const data = { ...opts.message }; if (this.format === 'stream-json') { - console.log(JSON.stringify(data)); + await this.write(JSON.stringify(data) + '\n'); } else if (this.format === 'json') { this.dataArr.push(data); } } - onEnd(opts: { result: LoopResult; sessionId: string }) { + + async onEnd(opts: { result: LoopResult; sessionId: string }) { if (!this.quiet) { return; } @@ -78,15 +96,15 @@ export class OutputFormat { }; } if (this.format === 'stream-json') { - console.log(JSON.stringify(data)); + await this.write(JSON.stringify(data) + '\n'); } else if (this.format === 'json') { this.dataArr.push(data); - console.log(JSON.stringify(this.dataArr)); + await this.write(JSON.stringify(this.dataArr) + '\n'); } else if (this.format === 'text') { - console.log( - opts.result.success + await this.write( + (opts.result.success ? opts.result.data?.text || '' - : opts.result.error.message, + : opts.result.error.message) + '\n', ); } } diff --git a/src/project.ts b/src/project.ts index e95d71d8..57492548 100644 --- a/src/project.ts +++ b/src/project.ts @@ -167,7 +167,6 @@ export class Project { format: this.context.config.outputFormat!, quiet: this.context.config.quiet, }); - const jsonlLogger = new JsonlLogger({ filePath: this.context.paths.getSessionLogPath(this.session.id), }); @@ -280,7 +279,7 @@ export class Project { // Output model info for initial message if (message !== null) { - outputFormat.onInit({ + await outputFormat.onInit({ text: message, sessionId: this.session.id, tools, @@ -306,7 +305,7 @@ export class Project { ...message, sessionId: this.session.id, }; - outputFormat.onMessage({ + await outputFormat.onMessage({ message: normalizedMessage, }); jsonlLogger.addMessage({ @@ -452,14 +451,14 @@ export class Project { ], type: PluginHookType.Series, }); - outputFormat.onEnd({ + + await outputFormat.onEnd({ result, sessionId: this.session.id, }); if (result.success && result.data.history) { this.session.updateHistory(result.data.history); } - return result; } }