README
¶
example/gemini
Overview
This directory holds one package main program, main.go. It starts the Gemini CLI as a child process with --experimental-acp and connects to it with acp.NewClientSideConnection from github.com/coder/acp-go-sdk. It runs the ACP initialize and session/new handshake, then sends each stdin line as a prompt and prints the streamed session updates. The replClient type implements acp.Client with permission prompts, file read and write for absolute paths, and stub terminal methods.
Interface
- Entry point
main, run withgo run ./example/gemini[main.go:main] - Flag
-gemini(string, defaultgemini): path of the Gemini CLI binary [main.go:-gemini] - Flag
-model(string, default empty): when set, passes--model <value>to Gemini [main.go:-model] - Flag
-sandbox(bool): passes--sandboxto Gemini [main.go:-sandbox] - Flag
-yolo(bool): auto-approves permission prompts [main.go:-yolo] - Flag
-debug(bool): passes--debugto Gemini [main.go:-debug] - REPL commands, read only between prompts:
:exitand:quitend the program,:cancelsendssession/cancel, and any other non-empty line is sent as a text prompt. During a turn the loop is blocked inconn.Prompt, so:cancelcannot stop the running turn, and a line typed at a permission prompt goes to that prompt [main.go:main] - Unexported type
replClient(fieldsautoApproveandinput, the channel of stdin lines), checked at compile time byvar _ acp.Client = (*replClient)(nil)[main.go:replClient] replClientmethods:RequestPermission,SessionUpdate,WriteTextFile,ReadTextFile,CreateTerminal,TerminalOutput,ReleaseTerminal,WaitForTerminalExit,KillTerminal[main.go:replClient]- Unexported helper
mustCwdreturns the working directory, or.on error [main.go:mustCwd] - Unexported helper
readLinesreads anio.Readerline by line on one goroutine and sends each line on a channel, which it closes at EOF or on a read error [main.go:readLines]
Invariants
- The child argv always starts with
--experimental-acp[main.go:main] initializesendsacp.ProtocolVersionNumberand advertisesfs.readTextFile,fs.writeTextFile, andterminalastrue[main.go:main]session/newusesmustCwd()ascwdand an empty MCP server list [main.go:main]- With
-yolo,RequestPermissionselects the firstallow_onceorallow_alwaysoption, else the first option, else returnscancelled[main.go:RequestPermission] - Without
-yolo,RequestPermissionasks again until the user enters a 1-based option number in range. When stdin ends first, it returnscancelled[main.go:RequestPermission] WriteTextFileandReadTextFilereturn an error for a path that is not absolute [main.go:WriteTextFile, main.go:ReadTextFile]ReadTextFileappliesline(1-based, clamped to the line count) andlimitonly when they are greater than 0 [main.go:ReadTextFile]SessionUpdatealways returnsnil[main.go:SessionUpdate]- A failed
initializeorsession/newprints the error, kills the child, and exits with status 1 [main.go:main] - A failed prompt prints the error to stderr and the REPL continues. A
*acp.RequestErroris printed as indented JSON [main.go:main] - Empty input lines are skipped [main.go:main]
Side Effects
- Starts the Gemini CLI with
exec.CommandContext(looked up throughPATHunless-gemininames a path, and with the full environment), pipes its stdin and stdout to the ACP connection, and connects its stderr toos.Stderr[main.go:main] - Reads user input from
os.Stdinon one goroutine (readLines). The REPL loop and the interactive permission prompts both take lines from its channel, so a permission answer typed or piped after a prompt reaches the permission prompt [main.go:readLines, main.go:main, main.go:RequestPermission] - Prints connection status, permission prompts, agent message and thought chunks, a
[user_message_chunk]notice, tool call and plan notices, and file and terminal calls to stdout [main.go:main, main.go:SessionUpdate, main.go:RequestPermission] - Prints pipe, start,
initialize,session/new, and prompt errors to stderr [main.go:main] WriteTextFilecreates missing parent directories withos.MkdirAll(mode0755) and writes the file withos.WriteFile: a new file gets mode0644, and an existing file is truncated and keeps its permissions (modes before umask) [main.go:WriteTextFile]ReadTextFilereads the file withos.ReadFile[main.go:ReadTextFile]- Terminal methods only print their parameters and return stub values (
CreateTerminalreturns terminal IDterm-1). They start no process [main.go:CreateTerminal, main.go:TerminalOutput, main.go:ReleaseTerminal, main.go:WaitForTerminalExit, main.go:KillTerminal] - Sets the connection logger to
slog.Default()[main.go:main] - Calls
os.Exit(1)on a pipe, start,initialize, orsession/newfailure [main.go:main] :exitand:quitcancel the context passed toexec.CommandContext. End of stdin kills the child withcmd.Process.Kill[main.go:main]- Calls
os.Getwdfor the session working directory [main.go:mustCwd]
Verification
- Run:
go vet ./example/gemini
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.