README
¶
example/claude-code
Overview
This directory holds a package main example that connects the github.com/coder/acp-go-sdk client side to the Claude Code ACP server. It starts npx -y @agentclientprotocol/claude-agent-acp@latest (the adapter formerly published as @zed-industries/claude-code-acp) as a subprocess and talks ACP over the subprocess stdin and stdout. It sends Initialize with file system read and write capabilities, creates one session in the current working directory, and then sends each non-empty stdin line as a prompt. The :cancel command sends a cancel notification, and :exit or :quit ends the program.
Interface
- Flag
-yolo(bool, defaultfalse) auto-approves permission prompts [main.go:main] func main()starts the Claude Code ACP subprocess, connects withacp.NewClientSideConnection, and runs the stdin prompt loop [main.go:main]- REPL commands, read only between prompts:
:cancelsendsacp.CancelNotificationfor the session, and:exit/:quitcancel the context and return. 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] type replClient structwith fieldsautoApprove boolandinput <-chan string(the lines of stdin) [main.go:replClient]var _ acp.Client = (*replClient)(nil)compile-time check thatreplClientimplementsacp.Client[main.go:replClient]replClientmethodsRequestPermission,SessionUpdate,WriteTextFile, andReadTextFileimplement theacp.Clientpermission, update, and file system calls [main.go:RequestPermission]replClientterminal methodsCreateTerminal(returns terminal IDterm-1),TerminalOutput,ReleaseTerminal,WaitForTerminalExit, andKillTerminalare stubs that print their parameters [main.go:CreateTerminal]func mustCwd() stringreturns the working directory, or"."on error [main.go:mustCwd]func readLines(r io.Reader) <-chan stringreadsrline by line on one goroutine, sends each line on the returned channel, and closes it at EOF or on a read error [main.go:readLines]
Invariants
WriteTextFilerequires an absolute path and returns an error otherwise [main.go:WriteTextFile]ReadTextFilerequires an absolute path and returns an error otherwise [main.go:ReadTextFile]ReadTextFiletreatsLineas 1-based, clamps the start to the file length, and appliesLimitonly when it is greater than 0 [main.go:ReadTextFile]- With
autoApprove,RequestPermissionpicks the firstallow_onceorallow_alwaysoption, else the first option, else returnsCancelled[main.go:RequestPermission] - Without
autoApprove,RequestPermissionreturnsCancelledwhen stdin ends before a valid option number is entered [main.go:RequestPermission] InitializeandNewSessionerrors kill the subprocess and exit with status 1 [main.go:main]- A
Prompterror prints to stderr and the prompt loop continues [main.go:main]
Side Effects
- Starts an
npxsubprocess, found throughPATHand with the full environment, with stdin and stdout pipes, and forwards its stderr toos.Stderr[main.go:main] - Prints agent messages, thoughts, tool calls, plan updates, and a
[user_message_chunk]notice to stdout [main.go:SessionUpdate] - Starts one goroutine (
readLines) that readsos.Stdin. The prompt 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] - Creates missing parent directories (mode
0o755) and writes files withos.WriteFile: a new file gets mode0o644, and an existing file is truncated and keeps its permissions (modes before umask) [main.go:WriteTextFile] - Reads files from disk [main.go:ReadTextFile]
- Kills the subprocess on
InitializeorNewSessionerror and when stdin ends, and cancels its context on:exit/:quit[main.go:main]
Verification
- Run:
go vet ./example/claude-code
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.