Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/outputFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@ export class OutputFormat {
this.quiet = opts.quiet;
this.dataArr = [];
}
onInit(opts: {
async write(data: string): Promise<void> {
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;
Expand All @@ -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;
}
Expand All @@ -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',
);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand Down Expand Up @@ -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,
Expand All @@ -306,7 +305,7 @@ export class Project {
...message,
sessionId: this.session.id,
};
outputFormat.onMessage({
await outputFormat.onMessage({
message: normalizedMessage,
});
jsonlLogger.addMessage({
Expand Down Expand Up @@ -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;
}
}
Loading