Skip to content

Core Package

Translation in Progress

This page is being translated. Content below is a placeholder.

GeminiClient

The main Agent controller:

typescript
class GeminiClient {
  private chat: GeminiChat
  private tools: ServerTool[]
  private conversation: Message[]

  async run(userMessage: string): AsyncGenerator<GeminiEvent> {
    this.conversation.push({ role: 'user', content: userMessage })

    while (true) {
      const response = await this.chat.send(this.conversation)

      for (const event of this.processResponse(response)) {
        yield event

        if (event.type === 'ToolCallRequest') {
          const result = await this.executeToolCall(event)
          this.conversation.push(/* result */)
        }
      }

      if (/* done */) break
    }
  }
}

Event Types

typescript
type GeminiEventType =
  | 'Content'          // Text output
  | 'ToolCallRequest'  // Tool call request
  | 'ToolCallResponse' // Tool result
  | 'Thought'          // Reasoning
  | 'Error'            // Error
  | 'ChatCompressed'   // Context compressed
  | 'LoopDetected'     // Loop detected

Tool Interface

typescript
interface ServerTool {
  name: string
  schema: FunctionDeclaration
  execute(params: unknown, signal?: AbortSignal): Promise<ToolResult>
  shouldConfirmExecute?(params: unknown): Promise<ConfirmDetails | false>
}

Complete Flow

1. User input

2. GeminiClient.run()

3. Add to conversation

4. GeminiChat.send()

5. Process response → yield events

6. If tool call → execute → add result

7. Repeat until done

Summary

  • GeminiClient is the main controller
  • Uses AsyncGenerator for event streaming
  • Tools follow unified ServerTool interface

Next

Build your own Agent: Mini Agent →

Learn AI Agent development through real source code