Documentation
¶
Overview ¶
Package claudecode runs the Claude Code CLI on a sidecar and explains how the run ended.
Index ¶
- Constants
- Variables
- func CredentialRejected(stdout, stderr string) bool
- func Env(cred Credential, baseURL string) map[string]string
- func RunError(ctx context.Context, timeout time.Duration, code int, execErr error, ...) error
- func Script(repoPath, prompt string, args []string) string
- type Activity
- type Credential
- type Options
- type Result
- type Stream
- type StreamResult
- type Turn
Constants ¶
const ExitMissing = 97
ExitMissing is the exit code a script running claude on a sidecar uses for claude not being on PATH, so every caller reports it the same way. Not the shell's own 127, which claude also exits with when something it shelled out to is missing: that is one broken run, not a missing binary.
Variables ¶
var ErrCredentialRejected = errors.New("anthropic rejected the credential")
ErrCredentialRejected is returned when Anthropic rejects the credential.
var ErrMissing = errors.New("claude is not installed on the sidecar")
ErrMissing is returned when a sidecar has no claude binary.
Functions ¶
func CredentialRejected ¶
CredentialRejected reports whether claude failed to authenticate. Both streams are checked: the 401 lands on stdout, while stderr can carry unrelated warnings.
func Env ¶
func Env(cred Credential, baseURL string) map[string]string
Env is the environment claude runs with on a sidecar: only the credential, and the base URL when it points somewhere other than Anthropic.
func RunError ¶
func RunError(ctx context.Context, timeout time.Duration, code int, execErr error, stdout, stderr string) error
RunError explains how a claude run on a sidecar ended, or returns nil when it exited 0. ctx is the run's own context, bounded by timeout when it is positive; code and execErr are what the Execer returned, and stdout and stderr what claude wrote. Every caller running claude classifies its end here, so a missing binary or a rejected credential reads the same whatever ran.
func Script ¶
Script builds the shell script that runs claude in repoPath on a sidecar with prompt on stdin. args is the whole command line, starting with "claude". The prompt is piped in base64-encoded, so no quoting in it can reach the shell. Claude Code's native installer puts claude in ~/.local/bin, which a non-login sh does not have on PATH.
Types ¶
type Activity ¶
type Activity struct {
// Tool is the tool used, or "" for text claude wrote.
Tool string
// Detail is the tool's target (a file, a command) or the text.
Detail string
}
Activity is one thing claude did during a run, for display.
type Credential ¶
Credential is the Claude credential a run authenticates with. EnvVar is the variable claude reads it from, so only one of the two is ever sent and a stale one cannot shadow the other.
type Options ¶
type Options struct {
Credential Credential
// BaseURL is forwarded to claude when it is not Anthropic's own, so a
// credential issued by a gateway is sent to that gateway.
BaseURL string
Model string // optional; claude's default when empty
Timeout time.Duration // optional; no limit beyond ctx when zero
// Tools is claude's --allowedTools list.
Tools []string
// Schema, when set, is passed as --json-schema and the output is claude's
// JSON result, which ParseResult reads. Empty, the output is the answer as
// plain text.
Schema string
// OnSubmitted is called with the remote command ID as soon as the exec is
// accepted and before its output is streamed.
OnSubmitted func(commandID string)
// OnActivity, when set, runs claude with --output-format stream-json and
// is called with each tool call and piece of text as claude makes it, so a
// long run can show what it is doing. Turn.Output is the same either way.
OnActivity func(Activity)
}
Options configures one run of claude -p.
type Result ¶
type Result struct {
IsError bool `json:"is_error"`
Subtype string `json:"subtype"`
Result string `json:"result"`
StructuredOutput json.RawMessage `json:"structured_output"`
}
Result is the part of claude's --output-format json result a caller reads. StructuredOutput is set only when the answer satisfied the schema.
func ParseResult ¶
ParseResult reads claude's JSON result. A null structured_output reads as none.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream reads claude's --output-format stream-json output as it arrives, which can be mid-line, and reports what claude does.
func NewStream ¶
NewStream returns a Stream that reports each tool call and piece of text to onActivity, which may be nil.
func (*Stream) Flush ¶
func (s *Stream) Flush()
Flush handles a last line that had no trailing newline.
func (*Stream) Result ¶
func (s *Stream) Result() (StreamResult, bool)
Result is the run's result event, and whether one arrived.
type StreamResult ¶
type StreamResult struct {
IsError bool
// Result is claude's final text.
Result string
CostUSD float64
// Raw is the whole event, the same shape as --output-format json's
// result, so ParseResult reads it.
Raw string
}
StreamResult is the result event that ends a stream-json run.
type Turn ¶
type Turn struct {
// Output is claude's stdout, trimmed: the answer, or the JSON result when
// Options.Schema is set.
Output string
Duration time.Duration
// CostUSD is what the run cost, as claude reported it. It is known only
// when Options.OnActivity is set, since only the streamed result is read
// for it.
CostUSD float64
}
Turn is the outcome of one run. Output is kept when the run fails, so a caller can show what it produced.