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
691 changes: 603 additions & 88 deletions bun.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
"commander": "^14.0.1",
"core-js": "3.45.1",
"debug": "4.4.3",
"hono": "^4.10.6",
"mitt": "^3.0.1",
"proxy-agent": "^6.5.0",
"puppeteer-core": "24.23.0",
"semver": "^7.7.3",
"smol-toml": "^1.4.2"
},
"devDependencies": {
Expand Down
13 changes: 11 additions & 2 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@
"author": "",
"license": "MIT",
"dependencies": {
"@browseros/tools": "workspace:*",
"@ai-sdk/amazon-bedrock": "^3.0.59",
"@ai-sdk/anthropic": "^2.0.47",
"@ai-sdk/azure": "^2.0.74",
"@ai-sdk/google": "^2.0.43",
"@ai-sdk/openai": "^2.0.72",
"@ai-sdk/openai-compatible": "^1.0.27",
"@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",
"@openrouter/ai-sdk-provider": "~1.2.5",
"ai": "^5.0.101",
"zod": "^4.1.12"
},
"devDependencies": {
Expand Down
163 changes: 163 additions & 0 deletions packages/agent/src/agent/GeminiAgent.formatter.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

This event formatter would only be required temporarily until we continue using our old UI, right?

For new UI, we don't need this event formatter, is that right? @shivammittal274

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 (don't emit response events)
* - 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