chathooks

package
v2.37.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func DispatchFailureFromResults(content []fantasy.Content) error

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

func EventMessages(result *Result, modelConfigID uuid.UUID) ([]chatstate.Message, error)

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 EventMessagesForResults(
	results []*Result,
	modelConfigID uuid.UUID,
) ([]chatstate.Message, error)

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 UserPromptDenial(err error) error

func UserPromptOverride

func UserPromptOverride(result *Result) (string, bool, error)

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.

func ChatFor

func ChatFor(chat database.Chat, turnID *uuid.UUID) Chat

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)

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 (r *Result) GetModelContext() string

func (*Result) GetUserMessage

func (r *Result) GetUserMessage() string

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) Enabled

func (t *Trigger) Enabled() bool

func (*Trigger) PostToolUseResults

func (t *Trigger) PostToolUseResults(
	ctx context.Context,
	chat Chat,
	content []fantasy.Content,
) ([]*Result, error)

func (*Trigger) PreflightPendingToolCalls

func (t *Trigger) PreflightPendingToolCalls(
	ctx context.Context,
	chat Chat,
	toolCalls []fantasy.ToolCallContent,
) (PreToolUseExecutionResult, error)

func (*Trigger) Trigger

func (t *Trigger) Trigger(
	ctx context.Context,
	chat Chat,
	msg Message,
	event agenthooks.EventType,
	capacity dispatch.CapacityClass,
) (*Result, 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL