gemini

package
v1.4.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package gemini provides a Go wrapper for the Gemini CLI binary.

The Gemini CLI is Google's AI coding assistant available via npm:

npm install -g @google/gemini-cli

This package implements the provider.Client interface, enabling seamless switching between different AI coding CLI tools while maintaining a consistent API.

Basic Usage

import "github.com/randalmurphal/llmkit/gemini"

client := gemini.NewGeminiCLI(
    gemini.WithModel("gemini-2.5-pro"),
    gemini.WithTimeout(5*time.Minute),
)

resp, err := client.Complete(ctx, gemini.CompletionRequest{
    SystemPrompt: "You are a helpful assistant.",
    Messages: []gemini.Message{
        {Role: gemini.RoleUser, Content: "Hello!"},
    },
})

Provider Registry Usage

import (
    "github.com/randalmurphal/llmkit/provider"
    _ "github.com/randalmurphal/llmkit/gemini" // Register provider
)

client, err := provider.New("gemini", provider.Config{
    Provider: "gemini",
    Model:    "gemini-2.5-pro",
})

Native Tools

Gemini CLI provides the following native tools:

  • read_file: Read file contents
  • write_file: Write content to files
  • run_shell_command: Execute shell commands
  • web_fetch: Fetch content from URLs
  • google_web_search: Search the web
  • save_memory: Persist information across sessions
  • write_todos: Manage todo lists

Context File

Gemini CLI reads project-specific instructions from GEMINI.md files, similar to Claude's CLAUDE.md. Place this file in your project root to provide context to the model.

MCP Support

Gemini CLI supports the Model Context Protocol (MCP) for extending capabilities with custom tools. Configure MCP servers via:

  • WithMCPConfig: Path to MCP configuration file
  • WithMCPServers: Inline server definitions

Sandbox Modes

Gemini supports different execution environments:

  • "host": Direct execution on host machine (default)
  • "docker": Execute commands in Docker container
  • "remote-execution": Execute on remote infrastructure

Use WithSandbox to configure the desired mode.

Non-Interactive Mode

For automation and scripting, use WithYolo to auto-approve all actions without prompting for confirmation. Use with caution in trusted environments.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnavailable indicates the LLM service is unavailable.
	ErrUnavailable = errors.New("LLM service unavailable")

	// ErrContextTooLong indicates the input exceeds the context window.
	ErrContextTooLong = errors.New("context exceeds maximum length")

	// ErrRateLimited indicates the request was rate limited.
	ErrRateLimited = errors.New("rate limited")

	// ErrInvalidRequest indicates the request is malformed.
	ErrInvalidRequest = errors.New("invalid request")

	// ErrTimeout indicates the request timed out.
	ErrTimeout = errors.New("request timed out")

	// ErrQuotaExceeded indicates API quota has been exceeded.
	ErrQuotaExceeded = errors.New("quota exceeded")
)

Sentinel errors for LLM operations.

Functions

This section is empty.

Types

type CLIResponse

type CLIResponse struct {
	// Response contains the model's text response
	Response string `json:"response"`
	// TurnCount is the number of turns in the conversation
	TurnCount int `json:"turnCount"`
	// Usage contains token usage information
	Usage CLIUsage `json:"usage,omitempty"`
	// ExecutedTools lists the tools that were executed
	ExecutedTools []ExecutedTool `json:"executedTools,omitempty"`
}

CLIResponse represents the JSON response from Gemini CLI.

type CLIUsage

type CLIUsage struct {
	InputTokens  int `json:"inputTokens"`
	OutputTokens int `json:"outputTokens"`
}

CLIUsage contains token usage from the CLI response.

type Capabilities

type Capabilities struct {
	// Streaming indicates if the provider supports streaming responses.
	Streaming bool `json:"streaming"`

	// Tools indicates if the provider supports tool/function calling.
	Tools bool `json:"tools"`

	// MCP indicates if the provider supports MCP (Model Context Protocol) servers.
	MCP bool `json:"mcp"`

	// Sessions indicates if the provider supports multi-turn conversation sessions.
	Sessions bool `json:"sessions"`

	// Images indicates if the provider supports image inputs.
	Images bool `json:"images"`

	// NativeTools lists the provider's built-in tools by name.
	NativeTools []string `json:"native_tools"`

	// ContextFile is the filename for project-specific context (e.g., "GEMINI.md").
	ContextFile string `json:"context_file,omitempty"`
}

Capabilities describes what this provider natively supports.

func (Capabilities) HasTool

func (c Capabilities) HasTool(name string) bool

HasTool checks if a native tool is available by name.

type CompletionRequest

type CompletionRequest struct {
	// SystemPrompt sets the system message that guides the model's behavior.
	SystemPrompt string `json:"system_prompt,omitempty"`

	// Messages is the conversation history to send to the model.
	Messages []Message `json:"messages"`

	// Model specifies which model to use (e.g., "gemini-2.5-pro").
	Model string `json:"model,omitempty"`

	// MaxTokens limits the response length.
	MaxTokens int `json:"max_tokens,omitempty"`

	// Temperature controls response randomness (0.0 = deterministic, 1.0 = creative).
	Temperature float64 `json:"temperature,omitempty"`

	// Tools lists available tools the model can invoke.
	Tools []Tool `json:"tools,omitempty"`

	// Options holds provider-specific configuration not covered by standard fields.
	Options map[string]any `json:"options,omitempty"`
}

CompletionRequest configures an LLM completion call.

type CompletionResponse

type CompletionResponse struct {
	Content      string        `json:"content"`
	ToolCalls    []ToolCall    `json:"tool_calls,omitempty"`
	Usage        TokenUsage    `json:"usage"`
	Model        string        `json:"model"`
	FinishReason string        `json:"finish_reason"`
	Duration     time.Duration `json:"duration"`

	// Gemini CLI specific fields
	NumTurns int `json:"num_turns,omitempty"`
}

CompletionResponse is the output of a completion call.

type Config

type Config struct {

	// Model is the primary model to use.
	// Default: "gemini-2.5-pro"
	Model string `json:"model" yaml:"model" mapstructure:"model"`

	// SystemPrompt is the system message prepended to all requests.
	// Optional.
	SystemPrompt string `json:"system_prompt" yaml:"system_prompt" mapstructure:"system_prompt"`

	// MaxTurns limits conversation turns (tool calls + responses).
	// 0 means no limit.
	MaxTurns int `json:"max_turns" yaml:"max_turns" mapstructure:"max_turns"`

	// Timeout is the maximum duration for a completion request.
	// 0 uses the default (5 minutes).
	Timeout time.Duration `json:"timeout" yaml:"timeout" mapstructure:"timeout"`

	// WorkDir is the working directory for file operations.
	// Default: current directory.
	WorkDir string `json:"work_dir" yaml:"work_dir" mapstructure:"work_dir"`

	// AllowedTools limits which tools Gemini can use (comma-separated in CLI).
	// Tools in this list bypass the confirmation dialog.
	// Empty means normal approval flow applies.
	AllowedTools []string `json:"allowed_tools" yaml:"allowed_tools" mapstructure:"allowed_tools"`

	// Yolo enables auto-approval of all actions (no prompts).
	// Use with extreme caution, only in trusted environments.
	Yolo bool `json:"yolo" yaml:"yolo" mapstructure:"yolo"`

	// Env provides additional environment variables.
	Env map[string]string `json:"env" yaml:"env" mapstructure:"env"`

	// OutputFormat controls CLI output format.
	// Default: "json".
	OutputFormat OutputFormat `json:"output_format" yaml:"output_format" mapstructure:"output_format"`

	// IncludeDirs adds directories to Gemini's file access scope.
	IncludeDirs []string `json:"include_dirs" yaml:"include_dirs" mapstructure:"include_dirs"`

	// MCPConfigPath is the path to an MCP configuration JSON file.
	// Maps to the --mcp CLI flag.
	MCPConfigPath string `json:"mcp_config_path" yaml:"mcp_config_path" mapstructure:"mcp_config_path"`

	// MCPServers defines MCP servers inline (alternative to config file).
	// Keys are server names, values are server configurations.
	MCPServers map[string]MCPServerConfig `json:"mcp_servers" yaml:"mcp_servers" mapstructure:"mcp_servers"`

	// Sandbox specifies the execution sandbox mode.
	// Valid values: "host" (default), "docker", "remote-execution"
	Sandbox string `json:"sandbox" yaml:"sandbox" mapstructure:"sandbox"`

	// GeminiPath is the path to the gemini CLI binary.
	// Default: "gemini" (found via PATH).
	GeminiPath string `json:"gemini_path" yaml:"gemini_path" mapstructure:"gemini_path"`
}

Config holds configuration for a Gemini client. Zero values use sensible defaults where noted.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

func FromEnv

func FromEnv() Config

FromEnv creates a Config from environment variables with defaults.

func (*Config) LoadFromEnv

func (c *Config) LoadFromEnv()

LoadFromEnv populates config fields from environment variables. Environment variables use GEMINI_ prefix and take precedence over existing values.

func (*Config) ToOptions

func (c *Config) ToOptions() []GeminiOption

ToOptions converts the config to functional options. This enables mixing Config with additional options.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid.

type ContentPart

type ContentPart struct {
	// Type indicates the content type: "text", "image", "file"
	Type string `json:"type"`

	// Text content (when Type == "text")
	Text string `json:"text,omitempty"`

	// ImageURL for remote images (when Type == "image")
	ImageURL string `json:"image_url,omitempty"`

	// ImageBase64 for inline images (when Type == "image")
	// Format: base64-encoded image data
	ImageBase64 string `json:"image_base64,omitempty"`

	// MediaType specifies the MIME type (e.g., "image/png", "image/jpeg")
	MediaType string `json:"media_type,omitempty"`

	// FilePath for local file references (when Type == "file")
	FilePath string `json:"file_path,omitempty"`
}

ContentPart represents a piece of multimodal content.

type Error

type Error struct {
	Op        string // Operation that failed ("complete", "stream")
	Err       error  // Underlying error
	Retryable bool   // Whether the error is likely transient
}

Error wraps LLM errors with context.

func NewError

func NewError(op string, err error, retryable bool) *Error

NewError creates a new LLM error.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying error for errors.Is/As support.

type ExecutedTool

type ExecutedTool struct {
	Name   string `json:"name"`
	Result string `json:"result,omitempty"`
}

ExecutedTool represents a tool that was executed by the CLI.

type GeminiCLI

type GeminiCLI struct {
	// contains filtered or unexported fields
}

GeminiCLI implements Client using the Gemini CLI binary.

func NewGeminiCLI

func NewGeminiCLI(opts ...GeminiOption) *GeminiCLI

NewGeminiCLI creates a new Gemini CLI client. Assumes "gemini" is available in PATH unless overridden with WithGeminiPath. By default uses JSON output format for structured responses.

func (*GeminiCLI) Capabilities

func (c *GeminiCLI) Capabilities() Capabilities

Capabilities returns Gemini CLI's native capabilities. Implements provider.Client.

func (*GeminiCLI) Close

func (c *GeminiCLI) Close() error

Close releases any resources held by the client. For GeminiCLI, this is a no-op as each command is independent. Implements provider.Client.

func (*GeminiCLI) Complete

Complete implements Client.

func (*GeminiCLI) Provider

func (c *GeminiCLI) Provider() string

Provider returns the provider name. Implements provider.Client.

func (*GeminiCLI) Stream

func (c *GeminiCLI) Stream(ctx context.Context, req CompletionRequest) (<-chan StreamChunk, error)

Stream implements Client.

type GeminiOption

type GeminiOption func(*GeminiCLI)

GeminiOption configures GeminiCLI.

func WithAllowedTools

func WithAllowedTools(tools []string) GeminiOption

WithAllowedTools sets the allowed tools for gemini (whitelist). These tools bypass the confirmation dialog. Example: []string{"read_file", "write_file"}

func WithEnv

func WithEnv(env map[string]string) GeminiOption

WithEnv adds additional environment variables to the CLI process. These are merged with the parent environment and any other configured env vars.

func WithEnvVar

func WithEnvVar(key, value string) GeminiOption

WithEnvVar adds a single environment variable to the CLI process.

func WithGeminiPath

func WithGeminiPath(path string) GeminiOption

WithGeminiPath sets the path to the gemini binary.

func WithIncludeDirs

func WithIncludeDirs(dirs []string) GeminiOption

WithIncludeDirs adds directories to be included in the context.

func WithMCPConfig

func WithMCPConfig(path string) GeminiOption

WithMCPConfig sets the path to an MCP configuration file.

func WithMCPServers

func WithMCPServers(servers map[string]MCPServerConfig) GeminiOption

WithMCPServers sets inline MCP server definitions. The servers are converted to JSON and passed via temp file.

func WithMaxTurns

func WithMaxTurns(n int) GeminiOption

WithMaxTurns limits the number of agentic turns in a conversation. A value of 0 means no limit.

func WithModel

func WithModel(model string) GeminiOption

WithModel sets the default model.

func WithOutputFormat

func WithOutputFormat(format OutputFormat) GeminiOption

WithOutputFormat sets the output format (text, json, stream-json). Default is json for structured responses.

func WithSandbox

func WithSandbox(mode string) GeminiOption

WithSandbox sets the sandbox mode for execution. Valid values: "docker", "podman", or a custom container command. Set via GEMINI_SANDBOX environment variable.

func WithSystemPrompt

func WithSystemPrompt(prompt string) GeminiOption

WithSystemPrompt sets a custom system prompt.

func WithTimeout

func WithTimeout(d time.Duration) GeminiOption

WithTimeout sets the default timeout for commands.

func WithWorkdir

func WithWorkdir(dir string) GeminiOption

WithWorkdir sets the working directory for gemini commands.

func WithYolo

func WithYolo() GeminiOption

WithYolo enables auto-approval of all actions (no prompts). Use this for non-interactive execution in trusted environments. WARNING: This allows Gemini to execute any tools without confirmation.

type MCPServerConfig

type MCPServerConfig struct {
	// Type specifies the transport type: "stdio", "http", or "sse".
	// If empty, defaults to "stdio" for servers with Command set.
	Type string `json:"type,omitempty"`

	// Command is the command to run the MCP server (for stdio transport).
	Command string `json:"command,omitempty"`

	// Args are the arguments to pass to the command (for stdio transport).
	Args []string `json:"args,omitempty"`

	// Env provides environment variables for the server process.
	Env map[string]string `json:"env,omitempty"`

	// URL is the server endpoint (for http/sse transport).
	URL string `json:"url,omitempty"`

	// Headers are HTTP headers (for http/sse transport).
	Headers []string `json:"headers,omitempty"`
}

MCPServerConfig defines an MCP server for the Gemini CLI. Supports stdio, http, and sse transport types.

type Message

type Message struct {
	Role    Role   `json:"role"`
	Content string `json:"content"`
	Name    string `json:"name,omitempty"` // For tool results

	// ContentParts enables multimodal content (text + images).
	// If set, takes precedence over Content.
	ContentParts []ContentPart `json:"content_parts,omitempty"`
}

Message is a conversation turn.

type OutputFormat

type OutputFormat string

OutputFormat specifies the CLI output format.

const (
	OutputFormatText       OutputFormat = "text"
	OutputFormatJSON       OutputFormat = "json"
	OutputFormatStreamJSON OutputFormat = "stream-json"
)

Output format constants.

type Role

type Role string

Role identifies the message sender.

const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
	RoleSystem    Role = "system"
)

Standard message roles.

type StreamChunk

type StreamChunk struct {
	Content   string      `json:"content,omitempty"`
	ToolCalls []ToolCall  `json:"tool_calls,omitempty"`
	Usage     *TokenUsage `json:"usage,omitempty"` // Only set in final chunk
	Done      bool        `json:"done"`
	Error     error       `json:"-"` // Non-nil if streaming failed
}

StreamChunk is a piece of a streaming response.

type TokenUsage

type TokenUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
	TotalTokens  int `json:"total_tokens"`
}

TokenUsage tracks token consumption.

func (*TokenUsage) Add

func (u *TokenUsage) Add(other TokenUsage)

Add calculates total tokens and adds to existing usage.

type Tool

type Tool struct {
	Name        string          `json:"name"`
	Description string          `json:"description"`
	Parameters  json.RawMessage `json:"parameters"` // JSON Schema
}

Tool defines an available tool for the LLM.

type ToolCall

type ToolCall struct {
	ID        string          `json:"id"`
	Name      string          `json:"name"`
	Arguments json.RawMessage `json:"arguments"`
}

ToolCall represents a tool invocation request from the LLM.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL