Documentation
¶
Overview ¶
Package chathooks integrates chat lifecycle hooks into chatd: it builds event envelopes, dispatches them, and converts consumer responses into transcript effects and permission decisions.
Index ¶
- Constants
- func ApplyAdmittedToolCalls(content []fantasy.Content, preflight PreToolUseExecutionResult) []fantasy.Content
- func ComposeUserPromptContent(parts []codersdk.ChatMessagePart, result *Result) ([]codersdk.ChatMessagePart, bool, error)
- func DispatchErrorMessage(eventType agenthooks.EventType, dispatchErr error) (string, bool)
- func DispatchFailureFromResults(content []fantasy.Content) error
- func EventMessages(result *Result, modelConfigID uuid.UUID) ([]chatstate.Message, error)
- func EventMessagesForResults(results []*Result, modelConfigID uuid.UUID) ([]chatstate.Message, error)
- func GenerationDispatchError(eventType agenthooks.EventType, dispatchErr error) error
- func PendingToolCalls(content []fantasy.Content) []fantasy.ToolCallContent
- func RejectDuplicateToolUseIDs(toolCalls []fantasy.ToolCallContent) error
- func RestoreToolCallOrder(content []fantasy.Content, calls []fantasy.ToolCallContent)
- func SessionStartSource(messages []database.ChatMessage) string
- func UserPromptDenial(err error) error
- func UserPromptOverride(result *Result) (string, bool, error)
- func UserPromptParts(result *Result) []codersdk.ChatMessagePart
- type Chat
- type Message
- type PreToolUseExecutionResult
- type Result
- type Trigger
- func (t *Trigger) Enabled() bool
- func (t *Trigger) PostToolUseResults(ctx context.Context, chat Chat, content []fantasy.Content) ([]*Result, error)
- func (t *Trigger) PreflightPendingToolCalls(ctx context.Context, chat Chat, toolCalls []fantasy.ToolCallContent) (PreToolUseExecutionResult, error)
- func (t *Trigger) Trigger(ctx context.Context, chat Chat, msg Message, event agenthooks.EventType, ...) (*Result, error)
- type UserPromptDeniedError
Constants ¶
const ( SessionStartSourceStartup = "startup" SessionStartSourceResume = "resume" SessionStartSourceClear = "clear" )
Variables ¶
This section is empty.
Functions ¶
func ApplyAdmittedToolCalls ¶
func ApplyAdmittedToolCalls(content []fantasy.Content, preflight PreToolUseExecutionResult) []fantasy.Content
ApplyAdmittedToolCalls applies admitted inputs before persistence and appends denial results.
func ComposeUserPromptContent ¶
func ComposeUserPromptContent(parts []codersdk.ChatMessagePart, result *Result) ([]codersdk.ChatMessagePart, bool, error)
ComposeUserPromptContent applies a user_prompt_submit result to the submitted parts. The merge order is fixed: override-or-original user parts first, then hook-context, then hook-notice. The composite content then flows through the ordinary send, queue, and edit paths.
func DispatchErrorMessage ¶
func DispatchErrorMessage(eventType agenthooks.EventType, dispatchErr error) (string, bool)
func DispatchFailureFromResults ¶
DispatchFailureFromResults returns the first tool result error whose chain contains a hook dispatch failure. Tools that dispatch lifecycle hooks inside Run (subagent spawn admission) must fail closed, but the tool loop persists Run errors as ordinary tool results the model can ignore, so the turn has to be failed even though the step commits.
func EventMessages ¶
EventMessages converts a turn-time hook result into ordinary transcript rows: model context becomes a user-role, model-visible row and the user message becomes a system-role, user-visible notice row.
func EventMessagesForResults ¶
func GenerationDispatchError ¶
func GenerationDispatchError(eventType agenthooks.EventType, dispatchErr error) error
func PendingToolCalls ¶
func PendingToolCalls(content []fantasy.Content) []fantasy.ToolCallContent
PendingToolCalls returns the calls a step leaves for Coder to run. Hooks never see provider-executed calls because the provider runs them itself.
func RejectDuplicateToolUseIDs ¶
func RejectDuplicateToolUseIDs(toolCalls []fantasy.ToolCallContent) error
RejectDuplicateToolUseIDs fails closed because hook consumers key decisions by tool-use ID; a duplicated ID in one step makes decisions unattributable. Callers must check the complete set of pending calls before removing any, because a filtered-out duplicate still shares its ID with a synthetic result.
func RestoreToolCallOrder ¶
func RestoreToolCallOrder(content []fantasy.Content, calls []fantasy.ToolCallContent)
RestoreToolCallOrder reorders known tool results to match the assistant's call order while preserving slots for unrelated entries.
func SessionStartSource ¶
func SessionStartSource(messages []database.ChatMessage) string
func UserPromptDenial ¶
func UserPromptParts ¶
func UserPromptParts(result *Result) []codersdk.ChatMessagePart
Types ¶
type Chat ¶
type Chat struct {
ID uuid.UUID
OwnerID uuid.UUID
WorkspaceID uuid.NullUUID
ParentChatID uuid.NullUUID
RootChatID uuid.NullUUID
TurnID *uuid.UUID
}
Chat identifies the chat and turn an event belongs to. Admission events for chats that do not exist yet (create, subagent spawn) fill the fields directly instead of loading a row.
type Message ¶
type Message struct {
Source string
Prompt string
Parts json.RawMessage
ToolUseID string
ToolName string
ToolInput json.RawMessage
ToolResponse json.RawMessage
ToolError string
}
func DynamicPostToolUseMessage ¶
func DynamicPostToolUseMessage(result codersdk.ToolResult, toolName string) Message
func UserPromptMessage ¶
func UserPromptMessage(parts []codersdk.ChatMessagePart) (Message, error)
UserPromptMessage builds the user_prompt_submit payload for parts. Invoked skill bodies and directories stay out of the payload sent to the hook consumer; the caller's parts keep them for the stored message.
type PreToolUseExecutionResult ¶
type PreToolUseExecutionResult struct {
Allowed []fantasy.ToolCallContent
Denied []fantasy.ToolResultContent
Results []*Result
Overrides map[string]json.RawMessage
}
PreToolUseExecutionResult preserves hook results in tool-call order for transcript injection.
type Result ¶
type Result struct {
InputOverride json.RawMessage
ModelContext string
UserMessage string
}
Result is a consumer response normalized for callers: a non-empty InputOverride means the permission decision was allow with a replacement input (the wire contract rejects allow without one). Denials surface as *deniedError instead.
func (*Result) GetModelContext ¶
func (*Result) GetUserMessage ¶
type Trigger ¶
type Trigger struct {
// contains filtered or unexported fields
}
Trigger is the only component that talks to the hook dispatcher. Every lifecycle event flows through trigger, which builds the wire envelope, dispatches, and normalizes the outcome.
func NewTrigger ¶
func NewTrigger(dispatcher *dispatch.Dispatcher) *Trigger
func (*Trigger) PostToolUseResults ¶
func (*Trigger) PreflightPendingToolCalls ¶
func (t *Trigger) PreflightPendingToolCalls( ctx context.Context, chat Chat, toolCalls []fantasy.ToolCallContent, ) (PreToolUseExecutionResult, error)
type UserPromptDeniedError ¶
type UserPromptDeniedError struct {
UserMessage string
}
UserPromptDeniedError reports that a lifecycle hook rejected a prompt.
func (*UserPromptDeniedError) Error ¶
func (e *UserPromptDeniedError) Error() string
Error includes UserMessage so callers that only surface the error string, such as subagent tool responses, still expose the user-facing denial message. The HTTP handlers unwrap the typed error instead.