Documentation
¶
Index ¶
- func StopReason(msg *JsonRpcMessage) string
- type AcpConnection
- func (c *AcpConnection) Alive() bool
- func (c *AcpConnection) GetLastActive() time.Time
- func (c *AcpConnection) Initialize() error
- func (c *AcpConnection) IsBusy() (bool, string)
- func (c *AcpConnection) Kill()
- func (c *AcpConnection) Models() (available []ModelInfo, current string)
- func (c *AcpConnection) Modes() (available []ModeInfo, current string)
- func (c *AcpConnection) PromptDone()
- func (c *AcpConnection) SessionCancel() error
- func (c *AcpConnection) SessionLoad(sessionID string, cwd string) error
- func (c *AcpConnection) SessionNew(cwd string) (string, error)
- func (c *AcpConnection) SessionPrompt(content []ContentBlock, owner string) (<-chan *JsonRpcMessage, uint64, bool, bool, error)
- func (c *AcpConnection) SessionSetMode(modeID string) error
- func (c *AcpConnection) SessionSetModel(modelID string) error
- func (c *AcpConnection) SetCurrentMode(id string)
- func (c *AcpConnection) SetCurrentModel(id string)
- type AcpEvent
- type AcpEventType
- type ContentBlock
- type JsonRpcError
- type JsonRpcMessage
- type JsonRpcNotification
- type JsonRpcRequest
- type JsonRpcResponse
- type ModeInfo
- type ModeSet
- type ModelInfo
- type ModelSet
- type SessionInfo
- type SessionPool
- func (p *SessionPool) CancelSession(threadKey string) error
- func (p *SessionPool) CleanupIdle(ttlSecs int64)
- func (p *SessionPool) Command() string
- func (p *SessionPool) Connection(threadKey string) *AcpConnection
- func (p *SessionPool) GetOrCreate(threadID string) error
- func (p *SessionPool) GetSessionInfo(threadKey string) (*SessionInfo, error)
- func (p *SessionPool) KillSession(threadKey string) error
- func (p *SessionPool) ListSessions() []SessionInfo
- func (p *SessionPool) LoadSessionForThread(threadKey, sessionID, cwd string) (bool, string)
- func (p *SessionPool) ResetSession(threadKey string) error
- func (p *SessionPool) ResumeSession(threadKey string) (bool, string)
- func (p *SessionPool) Shutdown()
- func (p *SessionPool) Stats() (active int, max int)
- func (p *SessionPool) WithConnection(threadID string, fn func(conn *AcpConnection) error) error
- func (p *SessionPool) WorkingDir() string
- type SessionRecord
- type SessionStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StopReason ¶
func StopReason(msg *JsonRpcMessage) string
StopReason returns the stopReason field from a session/prompt response. Returns "" when the message is not a response, has no result, or has no stopReason field. Common values: "end_turn", "cancelled", "max_tokens", "refusal".
Types ¶
type AcpConnection ¶
type AcpConnection struct {
SessionID string
CreatedAt time.Time
MessageCount atomic.Uint64
ThreadKey string
SessionReset bool
SessionResumed bool // true when session was restored via session/load (cleared after first prompt)
WasResumed bool // true when session was restored via session/load (persistent, for /info)
CanLoadSession bool // true when agent advertises loadSession capability
AvailableModes []ModeInfo
CurrentModeID string
AvailableModels []ModelInfo
CurrentModelID string
// contains filtered or unexported fields
}
func SpawnConnection ¶
func (*AcpConnection) Alive ¶
func (c *AcpConnection) Alive() bool
func (*AcpConnection) GetLastActive ¶
func (c *AcpConnection) GetLastActive() time.Time
GetLastActive returns the last activity time (safe for concurrent reads).
func (*AcpConnection) Initialize ¶
func (c *AcpConnection) Initialize() error
func (*AcpConnection) IsBusy ¶ added in v0.30.0
func (c *AcpConnection) IsBusy() (bool, string)
IsBusy returns whether the connection currently has a prompt running and a human-readable description of who started it. Cheap; safe to call from any goroutine. Used by message handlers to render a "queued behind X" placeholder when a new prompt is about to wait on promptMu.
Note: there is a small window between SessionPrompt acquiring promptMu and writing the owner field. During that window IsBusy returns (false, ""). The worst-case effect on UX is a "thinking…" placeholder briefly shown instead of "queued behind X" — benign.
func (*AcpConnection) Kill ¶
func (c *AcpConnection) Kill()
func (*AcpConnection) Models ¶
func (c *AcpConnection) Models() (available []ModelInfo, current string)
Models returns a copy of the session's currently advertised models and the id that is active right now. Safe for concurrent use.
func (*AcpConnection) Modes ¶
func (c *AcpConnection) Modes() (available []ModeInfo, current string)
Modes returns a copy of the session's currently advertised modes and the id that is active right now. Safe for concurrent use.
func (*AcpConnection) PromptDone ¶
func (c *AcpConnection) PromptDone()
PromptDone cleans up the notification subscriber after prompt streaming is done. It releases the prompt lock so the next queued prompt can proceed.
func (*AcpConnection) SessionCancel ¶
func (c *AcpConnection) SessionCancel() error
SessionCancel sends a session/cancel notification to the agent. This is a JSON-RPC notification (no id, no response); the agent stops producing output for the active prompt and the pending session/prompt response returns with stopReason="cancelled".
Must be called from a goroutine distinct from the one blocked inside SessionPrompt — it does NOT attempt to acquire promptMu, since the prompt goroutine already holds it and releases it via PromptDone after the cancelled response arrives.
A watchdog goroutine force-resolves any still-pending session/prompt requests with a synthetic stopReason="cancelled" response after cancelWatchdogTimeout, guarding against agents that ignore session/cancel or have died mid-prompt.
func (*AcpConnection) SessionLoad ¶
func (c *AcpConnection) SessionLoad(sessionID string, cwd string) error
SessionLoad attempts to resume a previous session by ID. The agent replays conversation history as session/update notifications, then responds to signal load is complete. Returns nil on success; the caller can then use SessionPrompt as normal.
func (*AcpConnection) SessionNew ¶
func (c *AcpConnection) SessionNew(cwd string) (string, error)
func (*AcpConnection) SessionPrompt ¶
func (c *AcpConnection) SessionPrompt(content []ContentBlock, owner string) (<-chan *JsonRpcMessage, uint64, bool, bool, error)
SessionPrompt sends a prompt and returns a channel for streaming notifications. The final message on the channel will have ID set (the prompt response). Only one prompt may be active at a time — concurrent callers block until PromptDone. Returns the notification channel, request ID, whether this is a reset, whether this is a resumed session, and any error.
func (*AcpConnection) SessionSetMode ¶
func (c *AcpConnection) SessionSetMode(modeID string) error
SessionSetMode asks the agent to switch the current session to the given mode. The agent is expected to emit a `current_mode_update` session notification on success, which the read loop uses to keep CurrentModeID in sync.
func (*AcpConnection) SessionSetModel ¶
func (c *AcpConnection) SessionSetModel(modelID string) error
SessionSetModel asks the agent to switch the current session to the given model id. Parallels SessionSetMode: same error shape, same optimistic local update, expects a `current_model_update` notification to reconcile.
func (*AcpConnection) SetCurrentMode ¶
func (c *AcpConnection) SetCurrentMode(id string)
SetCurrentMode records a mode change that arrived via a `current_mode_update` notification. The available-modes list is untouched because the notification payload only carries the new id.
func (*AcpConnection) SetCurrentModel ¶
func (c *AcpConnection) SetCurrentModel(id string)
SetCurrentModel records a model change that arrived via a `current_model_update` notification.
type AcpEvent ¶
type AcpEvent struct {
Type AcpEventType
Text string
Title string
Status string
// ModeID is the new current mode id carried by a
// current_mode_update session notification.
ModeID string
// ModelID is the new current model id carried by a
// current_model_update session notification.
ModelID string
}
func ClassifyNotification ¶
func ClassifyNotification(msg *JsonRpcMessage) *AcpEvent
type AcpEventType ¶
type AcpEventType int
const ( AcpEventText AcpEventType = iota AcpEventThinking AcpEventToolStart AcpEventToolDone AcpEventStatus AcpEventModeUpdate AcpEventModelUpdate )
type ContentBlock ¶
type ContentBlock map[string]interface{}
ContentBlock represents a single content element in an ACP prompt. Uses a flexible map representation to support different JSON shapes for text and image blocks.
func ImageBlock ¶
func ImageBlock(base64Data, mimeType string) ContentBlock
ImageBlock creates an ACP image content block with nested source structure. Schema: {"type":"image","source":{"type":"base64","media_type":"...","data":"..."}}
func TextBlock ¶
func TextBlock(text string) ContentBlock
type JsonRpcError ¶
func (*JsonRpcError) Error ¶
func (e *JsonRpcError) Error() string
type JsonRpcMessage ¶
type JsonRpcMessage struct {
ID *uint64 `json:"id,omitempty"`
Method *string `json:"method,omitempty"`
Result *json.RawMessage `json:"result,omitempty"`
Error *JsonRpcError `json:"error,omitempty"`
Params *json.RawMessage `json:"params,omitempty"`
}
type JsonRpcNotification ¶
type JsonRpcNotification struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
JsonRpcNotification is a JSON-RPC 2.0 notification (no id, no response expected).
func NewJsonRpcNotification ¶
func NewJsonRpcNotification(method string, params interface{}) *JsonRpcNotification
type JsonRpcRequest ¶
type JsonRpcRequest struct {
Jsonrpc string `json:"jsonrpc"`
ID uint64 `json:"id"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}
func NewJsonRpcRequest ¶
func NewJsonRpcRequest(id uint64, method string, params interface{}) *JsonRpcRequest
type JsonRpcResponse ¶
type JsonRpcResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID uint64 `json:"id"`
Result interface{} `json:"result"`
}
func NewJsonRpcResponse ¶
func NewJsonRpcResponse(id uint64, result interface{}) *JsonRpcResponse
type ModeInfo ¶
type ModeInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
ModeInfo describes one entry of the `availableModes` array in an ACP session setup response. `Description` is optional per spec.
type ModeSet ¶
type ModeSet struct {
CurrentModeID string `json:"currentModeId"`
AvailableModes []ModeInfo `json:"availableModes"`
}
ModeSet mirrors the `modes` object returned by session/new and session/load: which mode is active now, and what else is available.
type ModelInfo ¶
type ModelInfo struct {
ID string `json:"modelId"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
ModelInfo describes one entry of the `availableModels` array in an ACP session setup response. Shape parallels ModeInfo — but note the asymmetric field naming: per ACP spec (and observed with Kiro), the canonical key is `modelId`, not `id`. The custom UnmarshalJSON accepts either so we stay robust across agents that follow older drafts or that reuse the mode shape.
func (*ModelInfo) UnmarshalJSON ¶
type ModelSet ¶
type ModelSet struct {
CurrentModelID string `json:"currentModelId"`
AvailableModels []ModelInfo `json:"availableModels"`
}
ModelSet mirrors the `models` object returned by session/new and session/load: which model is active now, and what else is available.
type SessionInfo ¶
type SessionPool ¶
type SessionPool struct {
// contains filtered or unexported fields
}
func NewSessionPool ¶
func (*SessionPool) CancelSession ¶
func (p *SessionPool) CancelSession(threadKey string) error
CancelSession sends session/cancel to the agent for a specific thread. Unlike KillSession, this preserves the connection and session ID — the agent stops the active prompt and the pending session/prompt response returns with stopReason="cancelled". Returns an error if no connection exists for the thread.
func (*SessionPool) CleanupIdle ¶
func (p *SessionPool) CleanupIdle(ttlSecs int64)
func (*SessionPool) Command ¶
func (p *SessionPool) Command() string
Command returns the agent binary path configured for this pool. The sessionpicker package uses this to pick the matching backend when listing historical sessions.
func (*SessionPool) Connection ¶
func (p *SessionPool) Connection(threadKey string) *AcpConnection
Connection returns the active connection for a thread, or nil if none exists. The returned pointer is safe to retain — connections are not mutated after creation, and callers must gate use on conn.Alive(). Useful when a handler needs to capture the specific connection that owns a prompt (e.g. for cancel routing) without holding the pool lock across a long-running operation.
Race note: the pool may evict or replace this connection after the RLock is released, but that is by design — the returned pointer pins the original object (GC keeps it alive while the caller holds the pointer), and Kill()/LRU eviction flips alive to false. So a later conn.SessionCancel() on a captured stale pointer cleanly errors out with "connection not alive" rather than mis-cancelling a freshly-spawned connection on the same threadKey.
func (*SessionPool) GetOrCreate ¶
func (p *SessionPool) GetOrCreate(threadID string) error
func (*SessionPool) GetSessionInfo ¶
func (p *SessionPool) GetSessionInfo(threadKey string) (*SessionInfo, error)
GetSessionInfo returns metadata for a specific session.
func (*SessionPool) KillSession ¶
func (p *SessionPool) KillSession(threadKey string) error
KillSession terminates a specific session and removes it from the pool. The session store entry is preserved so /resume can find it later.
func (*SessionPool) ListSessions ¶
func (p *SessionPool) ListSessions() []SessionInfo
ListSessions returns a snapshot of all active sessions.
func (*SessionPool) LoadSessionForThread ¶
func (p *SessionPool) LoadSessionForThread(threadKey, sessionID, cwd string) (bool, string)
LoadSessionForThread kills any existing connection for threadKey, spawns a fresh agent process, and asks the agent to load the given session ID via ACP session/load. The loaded session takes over the thread — subsequent prompts reuse its conversation history.
cwd is the working directory to report in session/load; when empty, the pool's default working_dir is used. Picker callers should pass the session's original cwd so agents that verify session-to-cwd binding (e.g. claude-agent-acp) accept the load.
Behaviour on failure mirrors ResumeSession: if the agent does not advertise loadSession, or session/load returns an error, we fall back to session/new so the thread is not left in a broken state. The returned message tells the caller which branch ran.
func (*SessionPool) ResetSession ¶
func (p *SessionPool) ResetSession(threadKey string) error
ResetSession terminates a session AND clears its store entry (full fresh start).
func (*SessionPool) ResumeSession ¶
func (p *SessionPool) ResumeSession(threadKey string) (bool, string)
ResumeSession explicitly attempts to resume a previous session for the given thread. Returns (resumed bool, message string). - If no stored session exists: returns false with explanation. - If agent doesn't support loadSession: returns false with explanation. - If session/load succeeds: returns true. - If session/load fails: falls back to session/new and returns false with explanation.
func (*SessionPool) Shutdown ¶
func (p *SessionPool) Shutdown()
func (*SessionPool) Stats ¶
func (p *SessionPool) Stats() (active int, max int)
Stats returns pool utilization.
func (*SessionPool) WithConnection ¶
func (p *SessionPool) WithConnection(threadID string, fn func(conn *AcpConnection) error) error
WithConnection provides access to a connection. Caller must have called GetOrCreate first.
func (*SessionPool) WorkingDir ¶
func (p *SessionPool) WorkingDir() string
type SessionRecord ¶
type SessionRecord struct {
SessionID string `json:"session_id"`
CreatedAt time.Time `json:"created_at"`
LastActive time.Time `json:"last_active"`
}
SessionRecord is a single persisted session mapping.
type SessionStore ¶
type SessionStore struct {
// contains filtered or unexported fields
}
SessionStore persists threadKey → sessionId mappings to disk so sessions can be resumed after agent process restarts.
func NewSessionStore ¶
func NewSessionStore(dir string) (*SessionStore, error)
NewSessionStore creates or loads a session store from the given directory. The store file is {dir}/.quill/sessions.json.
func (*SessionStore) Lookup ¶
func (s *SessionStore) Lookup(threadKey string) string
Lookup returns the stored session ID for a thread key, or empty string if not found.
func (*SessionStore) Remove ¶
func (s *SessionStore) Remove(threadKey string)
Remove deletes a thread key from the store.
func (*SessionStore) Save ¶
func (s *SessionStore) Save(threadKey, sessionID string) error
Save persists a threadKey → sessionId mapping.
func (*SessionStore) Touch ¶
func (s *SessionStore) Touch(threadKey string)
Touch updates the last_active timestamp for a thread key.