-
Notifications
You must be signed in to change notification settings - Fork 4
gemini client integrated #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shivammittal274
wants to merge
2
commits into
main
Choose a base branch
from
gemini-agent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | ||
| * - 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 = ''; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Prompt To Fix With AI