Skip to content
Merged
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
26 changes: 15 additions & 11 deletions packages/core/src/evaluation/providers/copilot-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,7 @@ export class CopilotStreamLogger {
}

handleEvent(eventType: string, data: unknown): void {
if (this.format === 'json') {
const elapsed = formatElapsed(this.startedAt);
this.stream.write(`${JSON.stringify({ time: elapsed, event: eventType, data })}\n`);
return;
}

// In summary mode, buffer chunk events and emit a single consolidated line.
// Buffer chunk events into a single consolidated entry (both formats).
if (this.chunkExtractor) {
const chunkText = this.chunkExtractor(eventType, data);
if (chunkText === null) {
Expand All @@ -348,6 +342,12 @@ export class CopilotStreamLogger {
this.flushPendingText();
}

if (this.format === 'json') {
const elapsed = formatElapsed(this.startedAt);
this.stream.write(`${JSON.stringify({ time: elapsed, event: eventType, data })}\n`);
return;
}

const elapsed = formatElapsed(this.startedAt);
const summary = this.summarize(eventType, data);
if (summary) {
Expand All @@ -358,14 +358,18 @@ export class CopilotStreamLogger {
private flushPendingText(): void {
if (!this.pendingText) return;
const elapsed = formatElapsed(this.startedAt);
this.stream.write(`[+${elapsed}] [assistant_message] ${this.pendingText}\n`);
if (this.format === 'json') {
this.stream.write(
`${JSON.stringify({ time: elapsed, event: 'assistant_message', data: { content: this.pendingText } })}\n`,
);
} else {
this.stream.write(`[+${elapsed}] [assistant_message] ${this.pendingText}\n`);
}
this.pendingText = '';
}

async close(): Promise<void> {
if (this.format !== 'json') {
this.flushPendingText();
}
this.flushPendingText();
await new Promise<void>((resolve, reject) => {
this.stream.once('error', reject);
this.stream.end(() => resolve());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('CopilotStreamLogger', () => {
expect(content).toMatch(/\[assistant_message\] Final answer/);
});

it('does not buffer in json format (keeps per-event for full fidelity)', async () => {
it('consolidates chunk events in json format as single assistant_message entry', async () => {
const filePath = path.join(tempDir, 'test.log');
const chunkExtractor = (type: string, data: unknown): string | null | undefined => {
if (type !== 'agent_message_chunk') return undefined;
Expand All @@ -118,15 +118,21 @@ describe('CopilotStreamLogger', () => {

logger.handleEvent('agent_message_chunk', { content: { type: 'text', text: 'chunk1' } });
logger.handleEvent('agent_message_chunk', { content: { type: 'text', text: 'chunk2' } });
logger.handleEvent('tool_call', { title: 'read_file' });
await logger.close();

const content = await readFile(filePath, 'utf8');
const jsonLines = content
.split('\n')
.filter((l) => l.trim().startsWith('{'))
.map((l) => JSON.parse(l));
// Both chunks emitted individually as JSON
expect(jsonLines.filter((e) => e.event === 'agent_message_chunk')).toHaveLength(2);
// No raw chunk events — consolidated into one assistant_message
expect(jsonLines.filter((e) => e.event === 'agent_message_chunk')).toHaveLength(0);
const msg = jsonLines.find((e) => e.event === 'assistant_message');
expect(msg).toBeDefined();
expect(msg.data.content).toBe('chunk1chunk2');
// Non-chunk event still emitted
expect(jsonLines.find((e) => e.event === 'tool_call')).toBeDefined();
});

it('handles chunk events with no extractable text gracefully', async () => {
Expand Down
Loading