Documentation
¶
Index ¶
- Constants
- Variables
- func SessionIDFromContext(ctx context.Context) (string, error)
- func WithSessionID(ctx context.Context, sessionID string) context.Context
- type AfterCompleteHook
- type AfterCompleteRequest
- type AfterToolHook
- type AfterToolRequest
- type BeforeGenerateHook
- type BeforeGenerateRequest
- type BeforeStartHook
- type BeforeStartRequest
- type BeforeToolHook
- type BeforeToolRequest
- type Engine
- type HITLHandler
- type History
- type Identity
- type ImagePart
- type Message
- type Part
- type PartType
- type ReasoningPart
- type Receiver
- type Sender
- type SessionHandler
- type StaticEngine
- type TelemetryPart
- type TextPart
- type Tool
- type ToolDefinitionPart
- type ToolExecutionRecord
- type ToolProvider
- type ToolRequestPart
- type ToolResultPart
- type TransitionSignalPart
- type Volatility
Constants ¶
const ContextKeyNamespace contextKey = "tool_namespace"
Variables ¶
var ErrMissingSessionID = errors.New("missing session ID in context")
ErrMissingSessionID is returned when a session ID is not found in the context.
Functions ¶
func SessionIDFromContext ¶
SessionIDFromContext unpacks the session ID bound to the active context execution flow.
Types ¶
type AfterCompleteHook ¶
type AfterCompleteHook func(ctx context.Context, req AfterCompleteRequest) error
AfterCompleteHook fires when the recursive convergence loop terminates (no more pending tool calls).
type AfterCompleteRequest ¶
type AfterCompleteRequest struct {
SessionID string
FinalMessage Message
ToolHistory []ToolExecutionRecord
}
AfterCompleteRequest carries context for the AfterComplete hook.
type AfterToolHook ¶
type AfterToolHook func(ctx context.Context, req AfterToolRequest) error
AfterToolHook fires after each tool execution, regardless of success or failure.
type AfterToolRequest ¶
type AfterToolRequest struct {
SessionID string
ToolCall ToolRequestPart
Result ToolResultPart
Duration time.Duration
Error error // non-nil if the tool execution itself failed
}
AfterToolRequest carries context for the AfterTool hook.
type BeforeGenerateHook ¶
type BeforeGenerateHook func(ctx context.Context, req *BeforeGenerateRequest) error
BeforeGenerateHook fires before each LLM call (including recursive turns). Hooks execute serially and may mutate CurrentMessages. req is passed by pointer so mutations are visible to subsequent hooks.
type BeforeGenerateRequest ¶
type BeforeGenerateRequest struct {
SessionID string
CurrentMessages []Message // accumulated from prior hooks
}
BeforeGenerateRequest carries context for the BeforeGenerate hook. Hooks are executed serially in registration order. Each hook may read, append, reorder, or filter CurrentMessages.
type BeforeStartHook ¶
type BeforeStartHook func(ctx context.Context, req BeforeStartRequest) error
BeforeStartHook fires once per Stream() call, before any LLM interaction.
type BeforeStartRequest ¶
BeforeStartRequest carries context for the BeforeStart hook.
type BeforeToolHook ¶
type BeforeToolHook func(ctx context.Context, req BeforeToolRequest) error
BeforeToolHook fires before each tool execution. Returning an error blocks the tool; the engine converts the error into a synthetic ToolResultPart{IsError:true} and feeds it back to the LLM.
func NewHITLHook ¶
func NewHITLHook(handler HITLHandler, requiresSupervision map[string]interface{}, unsafeAllTools bool) BeforeToolHook
NewHITLHook returns a BeforeToolHook that conditionally requires human approval before a tool is executed.
type BeforeToolRequest ¶
type BeforeToolRequest struct {
SessionID string
ToolCall ToolRequestPart
CallIndex int // 0-based index of this tool call within the current session
}
BeforeToolRequest carries context for the BeforeTool hook.
type Engine ¶
type Engine interface {
// Stream spins up internal resources and returns an unbuffered channel that yields
// intermediate states or final Messages. The Engine is responsible for closing the channel.
Stream(ctx context.Context, inputMessage Message) (<-chan Message, error)
}
Engine represents the core LLM execution block (which wraps internal agentic tool convergence) The engine handles its own back-and-forth reasoning inside its implementation.
type HITLHandler ¶
type HITLHandler interface {
ApproveTool(ctx context.Context, req ToolRequestPart) (bool, error)
}
HITLHandler defines how the system explicitly requests human confirmation to execute a tool.
type History ¶
type History interface {
// Append adds a new message to the session history.
Append(ctx context.Context, sessionID string, msg Message) error
// Read retrieves the full message history for a given session.
Read(ctx context.Context, sessionID string) ([]Message, error)
}
History defines the minimal interface for persisting and retrieving conversational state within an agentic session.
type Identity ¶
type Identity struct {
Role string // e.g., "user", "assistant", "system", "tool"
Name string // The name of the specific tool or agent interacting
}
Identity represents the exact source and nature of a message.
type Message ¶
type Message struct {
ID string
SessionID string
Identity Identity
Parts []Part // Flexible payload list instead of a rigid string
Attributes map[string]string
Volatility Volatility
}
Message is the upgraded payload structure representing a completely isolated event.
func (Message) MarshalJSON ¶
MarshalJSON implements custom JSON serialization to handle the polymorphic Part interface.
func (*Message) UnmarshalJSON ¶
UnmarshalJSON implements custom JSON deserialization to correctly instantiate the polymorphic Part interface.
type Part ¶
type Part interface {
Type() PartType
}
Part defines an abstract section of an LLM generation. The Type() method allows downstream adapters (like Slack) to branch UI logic based on the part's intent, decoupled from how the part physically marshals itself.
type PartType ¶
type PartType string
PartType identifies the structural intent of a Message Part.
const ( TypeText PartType = "text" TypeReasoning PartType = "reasoning" TypeImage PartType = "image" TypeToolCall PartType = "tool_call" TypeToolDefinition PartType = "tool_definition" TypeToolResult PartType = "tool_result" TypeTransitionSignal PartType = "transition_signal" TypeTelemetry PartType = "telemetry" )
type ReasoningPart ¶
type ReasoningPart string
ReasoningPart represents chain-of-thought or provider-supplied reasoning.
func (ReasoningPart) MarshalText ¶
func (r ReasoningPart) MarshalText() ([]byte, error)
MarshalText conforms strictly to the standard Go encoding interfaces.
func (ReasoningPart) Type ¶
func (r ReasoningPart) Type() PartType
type Receiver ¶
Receiver yields incoming messages over a channel. It acts as an asynchronous producer. The provided context should control the lifetime of the receiver.
type SessionHandler ¶
type SessionHandler struct {
// contains filtered or unexported fields
}
SessionHandler wireframes the core Input/Output loop. It enforces strict request queuing and propagates Contexts for graceful cancellation.
func NewSessionHandler ¶
func NewSessionHandler(engine Engine, receiver Receiver, sender Sender) *SessionHandler
NewSessionHandler creates a fully configured session orchestrator.
func (*SessionHandler) ListenAndServe ¶
func (s *SessionHandler) ListenAndServe(ctx context.Context) error
ListenAndServe blocks to process incoming inputs sequentially. Under strict queuing, it ranges over the Engine's Stream until it hits EOF, ensuring the current prompt finishes entirely before dequeuing the next prompt.
type StaticEngine ¶
type StaticEngine struct {
// contains filtered or unexported fields
}
StaticEngine implements Engine and immediately returns a predefined slice of Message objects, completely ignoring the inputMessage. This is primarily useful for testing and providing canned responses.
func NewStaticEngine ¶
func NewStaticEngine(responses ...Message) *StaticEngine
NewStaticEngine creates a new StaticEngine yielding the provided responses in order.
type TelemetryPart ¶
type TelemetryPart struct {
InputTokens int
OutputTokens int
ReasoningTokens int
Duration time.Duration
}
TelemetryPart represents provider execution metrics (token usage, latency).
func (TelemetryPart) Type ¶
func (t TelemetryPart) Type() PartType
type TextPart ¶
type TextPart string
TextPart represents standard conversational text.
func (TextPart) MarshalText ¶
MarshalText conforms strictly to the standard Go encoding interfaces.
type Tool ¶
type Tool interface {
// Name returns the specific identifier for this tool.
Name() string
// Definition returns the JSON Schema outlining arguments.
Definition() ToolDefinitionPart
// Execute performs the underlying tool logic.
Execute(ctx context.Context, args map[string]interface{}) (interface{}, error)
}
Tool defines an atomic executable unit that the Agent can invoke.
type ToolDefinitionPart ¶
type ToolDefinitionPart struct {
Name string
Description string
Parameters json.RawMessage // e.g. JSON Schema
}
ToolDefinitionPart implements Part, allowing tool schemas to be passed dynamically inside the Message structure as structural "specialized context".
func (ToolDefinitionPart) Type ¶
func (t ToolDefinitionPart) Type() PartType
type ToolExecutionRecord ¶
type ToolExecutionRecord struct {
ToolCall ToolRequestPart
Result ToolResultPart
Duration time.Duration
Error error
}
ToolExecutionRecord captures a single tool call and its outcome for AfterComplete.
type ToolProvider ¶
type ToolProvider interface {
Namespace() string
// Tools returns all tools provided by this resolver.
Tools() []Tool
// GetTool allows the engine to resolve an execution callback when the LLM triggers a tool.
GetTool(name string) (Tool, bool)
}
ToolProvider represents an authorized source of executable tools.
type ToolRequestPart ¶
ToolRequestPart carries arguments that evaluate downstream.
func (ToolRequestPart) MarshalJSON ¶
func (t ToolRequestPart) MarshalJSON() ([]byte, error)
MarshalJSON conforms strictly to standard Go encoding libraries, offloading the burden of formatting to standard types.
func (ToolRequestPart) Type ¶
func (t ToolRequestPart) Type() PartType
func (*ToolRequestPart) UnmarshalJSON ¶
func (t *ToolRequestPart) UnmarshalJSON(data []byte) error
UnmarshalJSON for ToolRequestPart counter-acts its custom JSON Marshaler mapping 'Tool' to 'Name'.
type ToolResultPart ¶
type ToolResultPart struct {
ToolID string // Maps back to the original request (if provider supports it)
Name string // Which tool this answers
Result interface{} // Struct/Map that marshals cleanly to JSON
IsError bool // Indicates if the tool threw a business-logic error
}
ToolResultPart carries the output of a locally or remotely executed tool.
func (ToolResultPart) Type ¶
func (t ToolResultPart) Type() PartType
type TransitionSignalPart ¶
TransitionSignalPart represents an internal signal generated by a transition tool. It instructs the adapter Engine to break the recursive turn loop and yield to the context loop to swap the active workflow mode.
func (TransitionSignalPart) Type ¶
func (t TransitionSignalPart) Type() PartType
type Volatility ¶
type Volatility int8
const ( VolatilityStatic Volatility = 0 // Never changes (e.g. System Prompt, Guardrails) VolatilityLow Volatility = 3 // Rarely changes (e.g. Static agent instructions) VolatilityMedium Volatility = 5 // Occasional (e.g. Contextual RAG data) VolatilityHigh Volatility = 10 // Turn-by-turn (e.g. Conversation History, Tool definitions) )