geminicli

package
v0.3.13 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package geminicli implements the Agent interface for Gemini CLI.

Index

Constants

View Source
const (
	HookNameSessionStart        = "session-start"
	HookNameSessionEnd          = "session-end"
	HookNameBeforeAgent         = "before-agent"
	HookNameAfterAgent          = "after-agent"
	HookNameBeforeModel         = "before-model"
	HookNameAfterModel          = "after-model"
	HookNameBeforeToolSelection = "before-tool-selection"
	HookNameBeforeTool          = "before-tool"
	HookNameAfterTool           = "after-tool"
	HookNamePreCompress         = "pre-compress"
	HookNameNotification        = "notification"
)

Gemini CLI hook names - these become subcommands under `entire hooks gemini`

View Source
const (
	MessageTypeUser   = "user"
	MessageTypeGemini = "gemini"
)

Message type constants for Gemini transcripts

View Source
const (
	ToolWriteFile = "write_file"
	ToolEditFile  = "edit_file"
	ToolSaveFile  = "save_file"
	ToolReplace   = "replace"
)

Tool names used in Gemini CLI that modify files Note: Gemini CLI uses different names in different contexts: - Internal/transcript names: write_file, replace - Display names: WriteFile, Edit

View Source
const GeminiSettingsFileName = "settings.json"

GeminiSettingsFileName is the settings file used by Gemini CLI.

Variables

View Source
var FileModificationTools = []string{
	ToolWriteFile,
	ToolEditFile,
	ToolSaveFile,
	ToolReplace,
}

FileModificationTools lists tools that create or modify files in Gemini CLI

Functions

func CalculateTokenUsage

func CalculateTokenUsage(data []byte, startMessageIndex int) *agent.TokenUsage

CalculateTokenUsage calculates token usage from a Gemini transcript. This is specific to Gemini's API format where each message may have a tokens object with input, output, cached, thoughts, tool, and total counts. Only processes messages from startMessageIndex onwards (0-indexed).

func CalculateTokenUsageFromFile

func CalculateTokenUsageFromFile(path string, startMessageIndex int) (*agent.TokenUsage, error)

CalculateTokenUsageFromFile calculates token usage from a Gemini transcript file. If startMessageIndex > 0, only considers messages from that index onwards.

func ExtractAllUserPrompts

func ExtractAllUserPrompts(data []byte) ([]string, error)

ExtractAllUserPrompts extracts all user messages from transcript data

func ExtractAllUserPromptsFromTranscript

func ExtractAllUserPromptsFromTranscript(transcript *GeminiTranscript) []string

ExtractAllUserPromptsFromTranscript extracts all user prompts from a parsed transcript

func ExtractLastAssistantMessage

func ExtractLastAssistantMessage(data []byte) (string, error)

ExtractLastAssistantMessage extracts the last gemini response from transcript data

func ExtractLastAssistantMessageFromTranscript

func ExtractLastAssistantMessageFromTranscript(transcript *GeminiTranscript) string

ExtractLastAssistantMessageFromTranscript extracts the last gemini response from a parsed transcript

func ExtractLastUserPrompt

func ExtractLastUserPrompt(data []byte) (string, error)

ExtractLastUserPrompt extracts the last user message from transcript data

func ExtractLastUserPromptFromTranscript

func ExtractLastUserPromptFromTranscript(transcript *GeminiTranscript) string

ExtractLastUserPromptFromTranscript extracts the last user prompt from a parsed transcript

func ExtractModifiedFiles

func ExtractModifiedFiles(data []byte) ([]string, error)

ExtractModifiedFiles extracts files modified by tool calls from transcript data

func ExtractModifiedFilesFromTranscript

func ExtractModifiedFilesFromTranscript(transcript *GeminiTranscript) []string

ExtractModifiedFilesFromTranscript extracts files from a parsed transcript

func GetLastMessageID

func GetLastMessageID(data []byte) (string, error)

GetLastMessageID returns the ID of the last message in the transcript. Returns empty string if the transcript is empty or the last message has no ID.

func GetLastMessageIDFromFile

func GetLastMessageIDFromFile(path string) (string, error)

GetLastMessageIDFromFile reads a transcript file and returns the last message's ID. Returns empty string if the file doesn't exist, is empty, or has no messages with IDs.

func GetLastMessageIDFromTranscript

func GetLastMessageIDFromTranscript(transcript *GeminiTranscript) string

GetLastMessageIDFromTranscript returns the ID of the last message in a parsed transcript. Returns empty string if the transcript is empty or the last message has no ID.

func NewGeminiCLIAgent

func NewGeminiCLIAgent() agent.Agent

func SanitizePathForGemini

func SanitizePathForGemini(path string) string

Types

type GeminiCLIAgent

type GeminiCLIAgent struct{}

GeminiCLIAgent implements the Agent interface for Gemini CLI.

func (*GeminiCLIAgent) AreHooksInstalled

func (g *GeminiCLIAgent) AreHooksInstalled() bool

AreHooksInstalled checks if Entire hooks are installed.

func (*GeminiCLIAgent) ChunkTranscript

func (g *GeminiCLIAgent) ChunkTranscript(content []byte, maxSize int) ([][]byte, error)

ChunkTranscript splits a Gemini JSON transcript by distributing messages across chunks. Gemini uses JSON format with a {"messages": [...]} structure, so chunking splits the messages array while preserving the JSON structure in each chunk.

func (*GeminiCLIAgent) Description

func (g *GeminiCLIAgent) Description() string

Description returns a human-readable description.

func (*GeminiCLIAgent) DetectPresence

func (g *GeminiCLIAgent) DetectPresence() (bool, error)

DetectPresence checks if Gemini CLI is configured in the repository.

func (*GeminiCLIAgent) ExtractAgentSessionID

func (g *GeminiCLIAgent) ExtractAgentSessionID(entireSessionID string) string

ExtractAgentSessionID extracts the Gemini session ID from an Entire session ID. Since Entire session ID = agent session ID (identity), this returns the input unchanged. For backwards compatibility with legacy date-prefixed IDs, it strips the prefix if present.

func (*GeminiCLIAgent) ExtractModifiedFilesFromOffset

func (g *GeminiCLIAgent) ExtractModifiedFilesFromOffset(path string, startOffset int) (files []string, currentPosition int, err error)

ExtractModifiedFilesFromOffset extracts files modified since a given message index. For Gemini (JSON format), offset is the starting message index. Returns:

  • files: list of file paths modified by Gemini (from Write/Edit tools)
  • currentPosition: total number of messages in the transcript
  • error: any error encountered during reading

func (*GeminiCLIAgent) FormatResumeCommand

func (g *GeminiCLIAgent) FormatResumeCommand(sessionID string) string

FormatResumeCommand returns the command to resume a Gemini CLI session.

func (*GeminiCLIAgent) GetHookConfigPath

func (g *GeminiCLIAgent) GetHookConfigPath() string

GetHookConfigPath returns the path to Gemini's hook config file.

func (*GeminiCLIAgent) GetHookNames

func (g *GeminiCLIAgent) GetHookNames() []string

GetHookNames returns the hook verbs Gemini CLI supports. These become subcommands: entire hooks gemini <verb>

func (*GeminiCLIAgent) GetSessionDir

func (g *GeminiCLIAgent) GetSessionDir(repoPath string) (string, error)

GetSessionDir returns the directory where Gemini stores session transcripts. Gemini stores sessions in ~/.gemini/tmp/<project-hash>/chats/

func (*GeminiCLIAgent) GetSessionID

func (g *GeminiCLIAgent) GetSessionID(input *agent.HookInput) string

GetSessionID extracts the session ID from hook input.

func (*GeminiCLIAgent) GetSupportedHooks

func (g *GeminiCLIAgent) GetSupportedHooks() []agent.HookType

GetSupportedHooks returns the hook types Gemini CLI supports.

func (*GeminiCLIAgent) GetTranscriptPosition

func (g *GeminiCLIAgent) GetTranscriptPosition(path string) (int, error)

GetTranscriptPosition returns the current message count of a Gemini transcript. Gemini uses JSON format with a messages array, so position is the message count. Returns 0 if the file doesn't exist or is empty.

func (*GeminiCLIAgent) InstallHooks

func (g *GeminiCLIAgent) InstallHooks(localDev bool, force bool) (int, error)

InstallHooks installs Gemini CLI hooks in .gemini/settings.json. If force is true, removes existing Entire hooks before installing. Returns the number of hooks installed.

func (*GeminiCLIAgent) Name

func (g *GeminiCLIAgent) Name() agent.AgentName

Name returns the agent registry key.

func (*GeminiCLIAgent) ParseHookInput

func (g *GeminiCLIAgent) ParseHookInput(hookType agent.HookType, reader io.Reader) (*agent.HookInput, error)

ParseHookInput parses Gemini CLI hook input from stdin.

func (*GeminiCLIAgent) ReadSession

func (g *GeminiCLIAgent) ReadSession(input *agent.HookInput) (*agent.AgentSession, error)

ReadSession reads a session from Gemini's storage (JSON transcript file). The session data is stored in NativeData as raw JSON bytes.

func (*GeminiCLIAgent) ReassembleTranscript

func (g *GeminiCLIAgent) ReassembleTranscript(chunks [][]byte) ([]byte, error)

ReassembleTranscript merges Gemini JSON chunks by combining their message arrays.

func (*GeminiCLIAgent) SupportsHooks

func (g *GeminiCLIAgent) SupportsHooks() bool

SupportsHooks returns true as Gemini CLI supports lifecycle hooks.

func (*GeminiCLIAgent) TransformSessionID

func (g *GeminiCLIAgent) TransformSessionID(agentSessionID string) string

TransformSessionID converts a Gemini session ID to an Entire session ID. This is now an identity function - the agent session ID IS the Entire session ID.

func (*GeminiCLIAgent) Type

func (g *GeminiCLIAgent) Type() agent.AgentType

Type returns the agent type identifier.

func (*GeminiCLIAgent) UninstallHooks

func (g *GeminiCLIAgent) UninstallHooks() error

UninstallHooks removes Entire hooks from Gemini CLI settings.

func (*GeminiCLIAgent) WriteSession

func (g *GeminiCLIAgent) WriteSession(session *agent.AgentSession) error

WriteSession writes a session to Gemini's storage (JSON transcript file). Uses the NativeData field which contains raw JSON bytes.

type GeminiHookEntry

type GeminiHookEntry struct {
	Name    string `json:"name"`
	Type    string `json:"type"`
	Command string `json:"command"`
}

GeminiHookEntry represents a single hook command. Unlike Claude Code, Gemini CLI requires a "name" field for each hook entry.

type GeminiHookMatcher

type GeminiHookMatcher struct {
	Matcher string            `json:"matcher,omitempty"`
	Hooks   []GeminiHookEntry `json:"hooks"`
}

GeminiHookMatcher matches hooks to specific patterns

type GeminiHooks

type GeminiHooks struct {
	// Hooks are only executed when hooksConfig.enabled is true in .gemini/settings.json.
	SessionStart        []GeminiHookMatcher `json:"SessionStart,omitempty"`
	SessionEnd          []GeminiHookMatcher `json:"SessionEnd,omitempty"`
	BeforeAgent         []GeminiHookMatcher `json:"BeforeAgent,omitempty"`
	AfterAgent          []GeminiHookMatcher `json:"AfterAgent,omitempty"`
	BeforeModel         []GeminiHookMatcher `json:"BeforeModel,omitempty"`
	AfterModel          []GeminiHookMatcher `json:"AfterModel,omitempty"`
	BeforeToolSelection []GeminiHookMatcher `json:"BeforeToolSelection,omitempty"`
	BeforeTool          []GeminiHookMatcher `json:"BeforeTool,omitempty"`
	AfterTool           []GeminiHookMatcher `json:"AfterTool,omitempty"`
	PreCompress         []GeminiHookMatcher `json:"PreCompress,omitempty"`
	Notification        []GeminiHookMatcher `json:"Notification,omitempty"`
}

GeminiHooks contains all hook configurations

type GeminiHooksConfig

type GeminiHooksConfig struct {
	Enabled bool `json:"enabled,omitempty"`
}

GeminiHooksConfig contains tool-related settings

type GeminiMessage

type GeminiMessage struct {
	ID        string           `json:"id,omitempty"` // UUID for the message
	Type      string           `json:"type"`         // MessageTypeUser or MessageTypeGemini
	Content   string           `json:"content,omitempty"`
	ToolCalls []GeminiToolCall `json:"toolCalls,omitempty"`
}

GeminiMessage represents a single message in the transcript

type GeminiSettings

type GeminiSettings struct {
	HooksConfig GeminiHooksConfig `json:"hooksConfig,omitempty"`
	Hooks       GeminiHooks       `json:"hooks,omitempty"`
}

GeminiSettings represents the .gemini/settings.json structure

type GeminiToolCall

type GeminiToolCall struct {
	ID     string                 `json:"id"`
	Name   string                 `json:"name"`
	Args   map[string]interface{} `json:"args"`
	Status string                 `json:"status,omitempty"`
}

GeminiToolCall represents a tool call in a gemini message

type GeminiTranscript

type GeminiTranscript struct {
	Messages []GeminiMessage `json:"messages"`
}

GeminiTranscript represents the top-level structure of a Gemini session file

func ParseTranscript

func ParseTranscript(data []byte) (*GeminiTranscript, error)

ParseTranscript parses raw JSON content into a transcript structure

Jump to

Keyboard shortcuts

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