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
1,184 changes: 1,080 additions & 104 deletions bun.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
"author": "",
"license": "MIT",
"dependencies": {
"@browseros/tools": "workspace:*",
"@anthropic-ai/claude-agent-sdk": "^0.1.11",
"@browseros/common": "workspace:*",
"@browseros/server": "workspace:*",
"@anthropic-ai/claude-agent-sdk": "^0.1.11",
"@browseros/tools": "workspace:*",
"@google/gemini-cli-core": "^0.16.0",
"zod": "^4.1.12"
},
"devDependencies": {
Expand Down
163 changes: 163 additions & 0 deletions packages/agent/src/agent/GeminiAgent.formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/**
* @license
* Copyright 2025 BrowserOS
*/

import {FormattedEvent} from './types.js';

/**
* Gemini CLI Event Formatter
*
* Maps GeminiEventType to FormattedEvent:
* - Content: Accumulate text chunks, emit as 'response'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Comment says 'emit as response' but formatContent returns null and doesn't emit response events

Suggested change
* - Content: Accumulate text chunks, emit as 'response'
* - Content: Accumulate text chunks (don't emit response events)
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/agent/src/agent/GeminiAgent.formatter.ts
Line: 12:12

Comment:
**syntax:** Comment says 'emit as response' but formatContent returns null and doesn't emit response events

```suggestion
 * - Content: Accumulate text chunks (don't emit response events)
```

How can I resolve this? If you propose a fix, please make it concise.

* - ToolCallRequest: Emit as 'tool_use'
* - Thought: Emit as 'thinking'
* - Finished: Emit as 'completion' with usage metadata
* - Error: Emit as 'error'
*/
export class GeminiEventFormatter {
private textBuffer: string = '';

/**
* Process a Gemini event and return formatted event(s)
*
* @param event - Raw Gemini event from GeminiClient.sendMessageStream()
* @returns FormattedEvent, array of FormattedEvents, or null
*/
format(event: any): FormattedEvent | FormattedEvent[] | null {
const eventType = event.type;

switch (eventType) {
case 'content':
return this.formatContent(event);

case 'tool_call_request':
return this.formatToolCallRequest(event);

case 'thought':
return this.formatThought(event);

case 'finished':
return this.formatFinished(event);

case 'error':
return this.formatError(event);

case 'loop_detected':
return this.formatLoopDetected(event);

case 'max_session_turns':
return this.formatMaxTurns(event);

case 'chat_compressed':
return this.formatChatCompressed();

default:
return null;
}
}

/**
* Format Content event - accumulate text chunks (don't emit response events)
*/
private formatContent(event: any): null {
this.textBuffer += event.value || '';
return null;
}

/**
* Format ToolCallRequest event - emit tool_use only
*/
private formatToolCallRequest(event: any): FormattedEvent {
// Clear text buffer but don't emit response
this.textBuffer = '';

const toolName = event.value?.name || 'unknown';
const toolArgs = event.value?.args || {};
const argsStr = JSON.stringify(toolArgs, null, 2);

return new FormattedEvent('tool_use', `${toolName}\n${argsStr}`);
}

/**
* Format Thought event
*/
private formatThought(event: any): FormattedEvent {
const thought = event.value?.text || 'Thinking...';
return new FormattedEvent('thinking', thought);
}

/**
* Format Finished event - emit accumulated content as thinking event
*/
private formatFinished(event: any): FormattedEvent | null {
if (this.textBuffer.trim()) {
const response = new FormattedEvent('thinking', this.textBuffer);
this.textBuffer = '';
return response;
}
this.textBuffer = '';
return null;
}

/**
* Format Error event
*/
private formatError(event: any): FormattedEvent {
const error = event.value?.error;
const message = error?.message || 'Unknown error occurred';
return new FormattedEvent('error', message, {isError: true});
}

/**
* Format LoopDetected event
*/
private formatLoopDetected(event: any): FormattedEvent {
return new FormattedEvent('error', 'Loop detected - terminating execution', {
isError: true,
});
}

/**
* Format MaxSessionTurns event - treat as error since conversation didn't complete naturally
*/
private formatMaxTurns(event: any): FormattedEvent {
const maxTurns = event.value?.maxTurns || 'unknown';
return new FormattedEvent(
'error',
`Maximum session turns reached (${maxTurns})`,
{isError: true},
);
}

/**
* Format ChatCompressed event
*/
private formatChatCompressed(): FormattedEvent {
return new FormattedEvent('thinking', 'Chat history compressed');
}

/**
* Create a tool result event (called after executeToolCall)
*/
static createToolResultEvent(
toolName: string,
success: boolean,
result?: string,
): FormattedEvent {
const content = success
? `Tool ${toolName} completed successfully${result ? ': ' + result : ''}`
: `Tool ${toolName} failed${result ? ': ' + result : ''}`;

return new FormattedEvent('tool_result', content, {
isError: !success,
});
}

/**
* Reset text buffer (useful between turns)
*/
reset(): void {
this.textBuffer = '';
}
}
Loading