Documentation
¶
Overview ¶
Package copilotcli implements the Agent interface for GitHub Copilot CLI.
Index ¶
- Constants
- func ExtractModelFromTranscript(ctx context.Context, transcriptPath string) string
- func NewCopilotCLIAgent() agent.Agent
- func ParseTimestamp(raw json.RawMessage) (time.Time, error)
- type CopilotCLIAgent
- func (c *CopilotCLIAgent) AreHooksInstalled(ctx context.Context) (bool, error)
- func (c *CopilotCLIAgent) CalculateTokenUsage(transcriptData []byte, fromOffset int) (*agent.TokenUsage, error)
- func (c *CopilotCLIAgent) CallerSessionEnvVar() string
- func (c *CopilotCLIAgent) CheckHookConfig(ctx context.Context) agent.HookConfigState
- func (c *CopilotCLIAgent) ChunkTranscript(_ context.Context, content []byte, maxSize int) ([][]byte, error)
- func (c *CopilotCLIAgent) Description() string
- func (c *CopilotCLIAgent) DetectPresence(ctx context.Context) (bool, error)
- func (c *CopilotCLIAgent) ExtractModifiedFilesFromOffset(_ context.Context, path string, startOffset int) (files []string, currentPosition int, err error)
- func (c *CopilotCLIAgent) ExtractPrompts(sessionRef string, fromOffset int) ([]string, error)
- func (c *CopilotCLIAgent) ExtractSummary(sessionRef string) (string, error)
- func (c *CopilotCLIAgent) FormatResumeCommand(sessionID string) string
- func (c *CopilotCLIAgent) GenerateText(ctx context.Context, prompt string, model string) (string, error)
- func (c *CopilotCLIAgent) GetSessionDir(_ string) (string, error)
- func (c *CopilotCLIAgent) GetSessionID(input *agent.HookInput) string
- func (c *CopilotCLIAgent) GetSupportedHooks() []agent.HookType
- func (c *CopilotCLIAgent) GetTranscriptPosition(path string) (int, error)
- func (c *CopilotCLIAgent) HookConfigRelPath() string
- func (c *CopilotCLIAgent) HookNames() []string
- func (c *CopilotCLIAgent) InstallHooks(ctx context.Context, force bool) (int, error)
- func (c *CopilotCLIAgent) Name() types.AgentName
- func (c *CopilotCLIAgent) ParseHookEvent(ctx context.Context, hookName string, stdin io.Reader) (*agent.Event, error)
- func (c *CopilotCLIAgent) ProtectedDirs() []string
- func (c *CopilotCLIAgent) ReadSession(input *agent.HookInput) (*agent.AgentSession, error)
- func (c *CopilotCLIAgent) ReadTranscript(sessionRef string) ([]byte, error)
- func (c *CopilotCLIAgent) ReassembleTranscript(chunks [][]byte) ([]byte, error)
- func (c *CopilotCLIAgent) ResolveSessionFile(sessionDir, agentSessionID string) string
- func (c *CopilotCLIAgent) Type() types.AgentType
- func (c *CopilotCLIAgent) UninstallHooks(ctx context.Context) error
- func (c *CopilotCLIAgent) WriteSession(_ context.Context, session *agent.AgentSession) error
- type CopilotHookEntry
- type CopilotHooks
- type CopilotHooksFile
- type HookHost
Constants ¶
const ( VSCodeEventSessionStart = "SessionStart" VSCodeEventUserPromptSubmit = "UserPromptSubmit" VSCodeEventStop = "Stop" VSCodeEventPreToolUse = "PreToolUse" VSCodeEventPostToolUse = "PostToolUse" VSCodeEventPreCompact = "PreCompact" VSCodeEventSubagentStart = "SubagentStart" VSCodeEventSubagentStop = "SubagentStop" )
VS Code hookEventName values (from official VS Code docs). See: https://code.visualstudio.com/docs/copilot/customization/hooks
const ( HookNameUserPromptSubmitted = "user-prompt-submitted" HookNameSessionStart = "session-start" HookNameAgentStop = "agent-stop" HookNameSessionEnd = "session-end" HookNameSubagentStart = "subagent-start" HookNameSubagentStop = "subagent-stop" HookNamePreToolUse = "pre-tool-use" HookNamePostToolUse = "post-tool-use" HookNameErrorOccurred = "error-occurred" )
Copilot CLI hook names - these become subcommands under `entire hooks copilot-cli`
const HooksFileName = "entire.json"
HooksFileName is the hooks file managed by Entire for Copilot CLI.
Variables ¶
This section is empty.
Functions ¶
func ExtractModelFromTranscript ¶
ExtractModelFromTranscript extracts the LLM model name from a Copilot CLI transcript. It prefers session.model_change events (explicit model declarations), but falls back to the model field on tool.execution_complete events for Copilot CLI versions that do not emit session.model_change. Returns the last observed model, or empty string if unavailable.
func NewCopilotCLIAgent ¶
NewCopilotCLIAgent creates a new Copilot CLI agent instance.
func ParseTimestamp ¶ added in v0.7.8
func ParseTimestamp(raw json.RawMessage) (time.Time, error)
ParseTimestamp decodes a Copilot event timestamp, which may be either numeric epoch-millis or an RFC3339(Nano) string. The numeric form may carry a fractional part (Copilot CLI 1.0.71 emits e.g. 1784283185447.0); sub-millisecond precision is truncated. A null/zero value returns the zero time (callers treat that as "missing"). Exported so transcript importers can decode the same dual-format field without re-implementing the logic.
Types ¶
type CopilotCLIAgent ¶
type CopilotCLIAgent struct {
CommandRunner agent.TextCommandRunner
}
CopilotCLIAgent implements the Agent interface for GitHub Copilot CLI.
func (*CopilotCLIAgent) AreHooksInstalled ¶
func (c *CopilotCLIAgent) AreHooksInstalled(ctx context.Context) (bool, error)
AreHooksInstalled checks if Entire hooks are installed in the Copilot CLI config.
A missing config file is an answer — no hooks — while an unreadable or malformed one is an error: "we could not tell" and "there are none" are different things to a caller deciding whether hooks can be left alone.
func (*CopilotCLIAgent) CalculateTokenUsage ¶
func (c *CopilotCLIAgent) CalculateTokenUsage(transcriptData []byte, fromOffset int) (*agent.TokenUsage, error)
CalculateTokenUsage computes token usage from the Copilot CLI JSONL transcript. For full transcripts (fromOffset == 0), it prefers session.shutdown, which contains session-wide aggregates. For sliced transcripts, session.shutdown would overcount because it is not checkpoint-scoped, so we fall back to summing assistant.message outputTokens within the slice.
func (*CopilotCLIAgent) CallerSessionEnvVar ¶ added in v0.11.0
func (c *CopilotCLIAgent) CallerSessionEnvVar() string
CallerSessionEnvVar names the variable holding the session ID Copilot CLI publishes into the environment of the processes it spawns — the same ID that names the session's directory under Copilot's session-state store.
func (*CopilotCLIAgent) CheckHookConfig ¶ added in v0.11.0
func (c *CopilotCLIAgent) CheckHookConfig(ctx context.Context) agent.HookConfigState
CheckHookConfig reports whether every hook Entire manages is present with its current command. This makes repositories enabled by an older CLI surface the newly added subagentStart hook as drift instead of silently staying old.
func (*CopilotCLIAgent) ChunkTranscript ¶
func (c *CopilotCLIAgent) ChunkTranscript(_ context.Context, content []byte, maxSize int) ([][]byte, error)
ChunkTranscript splits a JSONL transcript at line boundaries.
func (*CopilotCLIAgent) Description ¶
func (c *CopilotCLIAgent) Description() string
Description returns a human-readable description.
func (*CopilotCLIAgent) DetectPresence ¶
func (c *CopilotCLIAgent) DetectPresence(ctx context.Context) (bool, error)
DetectPresence checks if Entire hooks are installed in the Copilot CLI config. Delegates to AreHooksInstalled which checks .github/hooks/entire.json for Entire hook entries.
func (*CopilotCLIAgent) ExtractModifiedFilesFromOffset ¶
func (c *CopilotCLIAgent) ExtractModifiedFilesFromOffset(_ context.Context, path string, startOffset int) (files []string, currentPosition int, err error)
ExtractModifiedFilesFromOffset extracts files modified since a given line number. For Copilot CLI (JSONL format), offset is the starting line number. Uses bufio.Reader to handle arbitrarily long lines (no size limit). Returns:
- files: list of file paths modified by Copilot (from tool.execution_complete events)
- currentPosition: total number of lines in the file
- error: any error encountered during reading
func (*CopilotCLIAgent) ExtractPrompts ¶
func (c *CopilotCLIAgent) ExtractPrompts(sessionRef string, fromOffset int) ([]string, error)
ExtractPrompts extracts user prompts from the transcript starting at the given offset.
func (*CopilotCLIAgent) ExtractSummary ¶
func (c *CopilotCLIAgent) ExtractSummary(sessionRef string) (string, error)
ExtractSummary extracts the last assistant message as a session summary.
func (*CopilotCLIAgent) FormatResumeCommand ¶
func (c *CopilotCLIAgent) FormatResumeCommand(sessionID string) string
FormatResumeCommand returns a command to resume a Copilot CLI session.
func (*CopilotCLIAgent) GenerateText ¶ added in v0.5.6
func (c *CopilotCLIAgent) GenerateText(ctx context.Context, prompt string, model string) (string, error)
GenerateText sends a prompt to the Copilot CLI and returns the raw text response.
The prompt is piped via stdin rather than -p to avoid argv size limits. The subprocess runs from os.TempDir with GIT_* stripped and the pinned minimal tool policy above (see agent.RunIsolatedTextGeneratorCLI and generateTextArgs).
func (*CopilotCLIAgent) GetSessionDir ¶
func (c *CopilotCLIAgent) GetSessionDir(_ string) (string, error)
GetSessionDir returns the directory where Copilot CLI stores session transcripts.
func (*CopilotCLIAgent) GetSessionID ¶
func (c *CopilotCLIAgent) GetSessionID(input *agent.HookInput) string
GetSessionID extracts the session ID from hook input.
func (*CopilotCLIAgent) GetSupportedHooks ¶
func (c *CopilotCLIAgent) GetSupportedHooks() []agent.HookType
GetSupportedHooks returns the normalized lifecycle events this agent supports. Note: HookNames() returns 9 hooks but GetSupportedHooks() returns only 6. The three not listed here are:
- subagentStart/subagentStop: native child hooks have no HookType constant (they use EventType instead).
- errorOccurred: pass-through hook with no lifecycle action (ParseHookEvent returns nil).
func (*CopilotCLIAgent) GetTranscriptPosition ¶
func (c *CopilotCLIAgent) GetTranscriptPosition(path string) (int, error)
GetTranscriptPosition returns the current line count of a Copilot CLI transcript. Copilot CLI uses JSONL format, so position is the number of lines. This is a lightweight operation that only counts lines without parsing JSON. Uses bufio.Reader to handle arbitrarily long lines (no size limit). Returns 0 if the file doesn't exist or is empty.
func (*CopilotCLIAgent) HookConfigRelPath ¶ added in v0.10.6
func (c *CopilotCLIAgent) HookConfigRelPath() string
HookConfigRelPath implements agent.HookConfigLocator.
func (*CopilotCLIAgent) HookNames ¶
func (c *CopilotCLIAgent) HookNames() []string
HookNames returns all hook verbs Copilot CLI supports. These become subcommands: entire hooks copilot-cli <verb>
func (*CopilotCLIAgent) InstallHooks ¶
InstallHooks installs Copilot CLI hooks in .github/hooks/entire.json. If force is true, removes existing Entire hooks before installing. Returns the number of hooks installed. Unknown top-level fields and hook types are preserved on round-trip.
func (*CopilotCLIAgent) Name ¶
func (c *CopilotCLIAgent) Name() types.AgentName
Name returns the agent registry key.
func (*CopilotCLIAgent) ParseHookEvent ¶
func (c *CopilotCLIAgent) ParseHookEvent(ctx context.Context, hookName string, stdin io.Reader) (*agent.Event, error)
ParseHookEvent translates a Copilot CLI hook into a normalized lifecycle Event. Returns nil if the hook has no lifecycle significance (pass-through hooks).
For VS Code payloads (detected via hookEventName), the event name is validated against the CLI subcommand. Mismatches are silently skipped to avoid processing a payload that doesn't match the hook being invoked.
func (*CopilotCLIAgent) ProtectedDirs ¶
func (c *CopilotCLIAgent) ProtectedDirs() []string
ProtectedDirs returns directories that Copilot CLI uses for config/state.
func (*CopilotCLIAgent) ReadSession ¶
func (c *CopilotCLIAgent) ReadSession(input *agent.HookInput) (*agent.AgentSession, error)
ReadSession reads a session from Copilot CLI's storage (JSONL transcript file).
func (*CopilotCLIAgent) ReadTranscript ¶
func (c *CopilotCLIAgent) ReadTranscript(sessionRef string) ([]byte, error)
ReadTranscript reads the raw JSONL transcript bytes for a session.
func (*CopilotCLIAgent) ReassembleTranscript ¶
func (c *CopilotCLIAgent) ReassembleTranscript(chunks [][]byte) ([]byte, error)
ReassembleTranscript concatenates JSONL chunks with newlines.
func (*CopilotCLIAgent) ResolveSessionFile ¶
func (c *CopilotCLIAgent) ResolveSessionFile(sessionDir, agentSessionID string) string
ResolveSessionFile returns the path to a Copilot CLI session transcript file. Copilot CLI stores transcripts at <sessionDir>/<sessionId>/events.jsonl.
func (*CopilotCLIAgent) Type ¶
func (c *CopilotCLIAgent) Type() types.AgentType
Type returns the agent type identifier.
func (*CopilotCLIAgent) UninstallHooks ¶
func (c *CopilotCLIAgent) UninstallHooks(ctx context.Context) error
UninstallHooks removes Entire hooks from Copilot CLI's entire.json. Unknown top-level fields and hook types are preserved on round-trip.
func (*CopilotCLIAgent) WriteSession ¶
func (c *CopilotCLIAgent) WriteSession(_ context.Context, session *agent.AgentSession) error
WriteSession writes a session to Copilot CLI's storage (JSONL transcript file).
type CopilotHookEntry ¶
type CopilotHooks ¶
type CopilotHooks struct {
UserPromptSubmitted []CopilotHookEntry `json:"userPromptSubmitted,omitempty"`
SessionStart []CopilotHookEntry `json:"sessionStart,omitempty"`
AgentStop []CopilotHookEntry `json:"agentStop,omitempty"`
SessionEnd []CopilotHookEntry `json:"sessionEnd,omitempty"`
SubagentStart []CopilotHookEntry `json:"subagentStart,omitempty"`
SubagentStop []CopilotHookEntry `json:"subagentStop,omitempty"`
PreToolUse []CopilotHookEntry `json:"preToolUse,omitempty"`
PostToolUse []CopilotHookEntry `json:"postToolUse,omitempty"`
ErrorOccurred []CopilotHookEntry `json:"errorOccurred,omitempty"`
}
type CopilotHooksFile ¶
type CopilotHooksFile struct {
Version int `json:"version"`
Hooks CopilotHooks `json:"hooks"`
}