Documentation
¶
Overview ¶
Package graft is a Go framework for building AI agents and LLM-powered applications.
Graft provides type-safe tool definitions, multi-provider LLM support (OpenAI, Anthropic, Google Gemini, AWS Bedrock), agent handoffs, guardrails, MCP integration, graph orchestration, and durable execution — all with zero vendor SDK dependencies.
Quick Start ¶
agent := graft.NewAgent("assistant",
graft.WithInstructions("You are a helpful assistant."),
graft.WithTools(myTool),
)
runner := graft.NewDefaultRunner(model)
result, err := runner.Run(ctx, agent, messages)
See https://github.com/delavalom/graft for full documentation and examples.
Index ¶
- type Agent
- type AgentError
- type AgentOption
- func WithGuardrails(guardrails ...Guardrail) AgentOption
- func WithHandoffs(handoffs ...Handoff) AgentOption
- func WithHooks(hooks *HookRegistry) AgentOption
- func WithInstructions(instructions string) AgentOption
- func WithMaxTokens(max int) AgentOption
- func WithMetadata(meta map[string]any) AgentOption
- func WithModel(model string) AgentOption
- func WithSubAgents(subs ...*SubAgent) AgentOption
- func WithTemperature(temp float64) AgentOption
- func WithToolChoice(tc ToolChoice) AgentOption
- func WithTools(tools ...Tool) AgentOption
- type Cost
- type DefaultRunner
- type ErrorType
- type EventType
- type GenerateParams
- type GenerateResult
- type Guardrail
- type GuardrailType
- type Handoff
- type HookCallback
- type HookEvent
- type HookPayload
- type HookRegistry
- type HookResult
- type LanguageModel
- type Message
- type ProviderError
- type Result
- type Role
- type RunConfig
- type RunOption
- type Runner
- type Span
- type StreamChunk
- type StreamEvent
- type SubAgent
- type SubAgentResult
- type Tool
- type ToolCall
- type ToolChoice
- type ToolDefinition
- type ToolResult
- type Trace
- type Usage
- type ValidationData
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Agent ¶
type Agent struct {
Name string
Instructions string
Tools []Tool
Model string
Temperature *float64
MaxTokens *int
ToolChoice ToolChoice
Guardrails []Guardrail
Handoffs []Handoff
SubAgents []*SubAgent
Hooks *HookRegistry
Metadata map[string]any
}
func NewAgent ¶
func NewAgent(name string, opts ...AgentOption) *Agent
NewAgent creates a new Agent with the given name and options.
type AgentError ¶
func NewAgentError ¶
func NewAgentError(errType ErrorType, message string, cause error) *AgentError
func NewProviderError ¶
func NewProviderError(statusCode int, providerName string, body []byte) *AgentError
NewProviderError builds an *AgentError of type ErrProvider with structured ProviderError context. It maps common HTTP status codes to actionable guidance and attempts to parse provider-specific error messages from body.
func (*AgentError) Error ¶
func (e *AgentError) Error() string
func (*AgentError) Is ¶
func (e *AgentError) Is(target error) bool
func (*AgentError) IsRetryable ¶
func (e *AgentError) IsRetryable() bool
IsRetryable reports whether the error is safe to retry. For non-provider errors it always returns false.
func (*AgentError) StatusCode ¶
func (e *AgentError) StatusCode() int
StatusCode returns the HTTP status code stored inside a provider error, or 0 if this AgentError was not created from a provider HTTP response.
func (*AgentError) Unwrap ¶
func (e *AgentError) Unwrap() error
type AgentOption ¶
type AgentOption func(*Agent)
func WithGuardrails ¶
func WithGuardrails(guardrails ...Guardrail) AgentOption
func WithHandoffs ¶
func WithHandoffs(handoffs ...Handoff) AgentOption
func WithHooks ¶
func WithHooks(hooks *HookRegistry) AgentOption
func WithInstructions ¶
func WithInstructions(instructions string) AgentOption
func WithMaxTokens ¶
func WithMaxTokens(max int) AgentOption
func WithMetadata ¶
func WithMetadata(meta map[string]any) AgentOption
func WithModel ¶
func WithModel(model string) AgentOption
func WithSubAgents ¶
func WithSubAgents(subs ...*SubAgent) AgentOption
func WithTemperature ¶
func WithTemperature(temp float64) AgentOption
func WithToolChoice ¶
func WithToolChoice(tc ToolChoice) AgentOption
func WithTools ¶
func WithTools(tools ...Tool) AgentOption
type Cost ¶
type DefaultRunner ¶
type DefaultRunner struct {
// contains filtered or unexported fields
}
func NewDefaultRunner ¶
func NewDefaultRunner(model LanguageModel) *DefaultRunner
func (*DefaultRunner) RunStream ¶
func (r *DefaultRunner) RunStream(ctx context.Context, agent *Agent, messages []Message, opts ...RunOption) (<-chan StreamEvent, error)
type ErrorType ¶
type ErrorType string
const ( ErrToolExecution ErrorType = "tool_execution" ErrHandoff ErrorType = "handoff" ErrGuardrail ErrorType = "guardrail" ErrTimeout ErrorType = "timeout" ErrContextLength ErrorType = "context_length" ErrInvalidToolCall ErrorType = "invalid_tool_call" ErrRateLimit ErrorType = "rate_limit" ErrProvider ErrorType = "provider" )
type EventType ¶
type EventType string
const ( EventTextDelta EventType = "text_delta" EventToolCallStart EventType = "tool_call_start" EventToolCallDelta EventType = "tool_call_delta" EventToolCallDone EventType = "tool_call_done" EventToolResultDone EventType = "tool_result_done" EventMessageDone EventType = "message_done" EventHandoff EventType = "handoff" EventError EventType = "error" EventDone EventType = "done" )
type GenerateParams ¶
type GenerateParams struct {
Messages []Message `json:"messages"`
Tools []ToolDefinition `json:"tools,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
ToolChoice ToolChoice `json:"tool_choice,omitempty"`
Stop []string `json:"stop,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
GenerateParams holds the parameters for a generation request.
type GenerateResult ¶
type GenerateResult struct {
Message Message `json:"message"`
Usage Usage `json:"usage"`
Cost *Cost `json:"cost,omitempty"`
}
GenerateResult holds the result of a generation request.
type Guardrail ¶
type Guardrail interface {
Name() string
Type() GuardrailType
Validate(ctx context.Context, data *ValidationData) (*ValidationResult, error)
}
type GuardrailType ¶
type GuardrailType string
const ( GuardrailInput GuardrailType = "input" GuardrailOutput GuardrailType = "output" GuardrailTool GuardrailType = "tool" )
type HookCallback ¶
type HookCallback func(ctx context.Context, payload *HookPayload) (*HookResult, error)
type HookEvent ¶
type HookEvent string
const ( HookAgentStart HookEvent = "agent_start" HookAgentEnd HookEvent = "agent_end" HookPreToolCall HookEvent = "pre_tool_call" HookPostToolCall HookEvent = "post_tool_call" HookToolCallError HookEvent = "tool_call_error" HookPreHandoff HookEvent = "pre_handoff" HookPostHandoff HookEvent = "post_handoff" HookPreGenerate HookEvent = "pre_generate" HookPostGenerate HookEvent = "post_generate" HookGuardrailTrip HookEvent = "guardrail_trip" )
type HookPayload ¶
type HookRegistry ¶
type HookRegistry struct {
// contains filtered or unexported fields
}
func NewHookRegistry ¶
func NewHookRegistry() *HookRegistry
func (*HookRegistry) On ¶
func (r *HookRegistry) On(event HookEvent, cb HookCallback)
func (*HookRegistry) Run ¶
func (r *HookRegistry) Run(ctx context.Context, payload *HookPayload) (*HookResult, error)
type HookResult ¶
type LanguageModel ¶
type LanguageModel interface {
Generate(ctx context.Context, params GenerateParams) (*GenerateResult, error)
Stream(ctx context.Context, params GenerateParams) (<-chan StreamChunk, error)
ModelID() string
}
LanguageModel is the interface that all LLM providers must implement.
type ProviderError ¶
type ProviderError struct {
StatusCode int
ProviderCode string
ProviderName string
Retryable bool
RetryAfter time.Duration
Guidance string
}
ProviderError holds structured information about an HTTP error from a provider.
type Result ¶
type Result struct {
Messages []Message `json:"messages"`
Usage Usage `json:"usage"`
Cost *Cost `json:"cost,omitempty"`
Trace *Trace `json:"trace,omitempty"`
}
func (*Result) LastAssistantText ¶
type RunConfig ¶
func DefaultRunConfig ¶
func DefaultRunConfig() RunConfig
type StreamChunk ¶
type StreamChunk struct {
Delta StreamEvent `json:"delta"`
Usage *Usage `json:"usage,omitempty"`
}
StreamChunk holds a single chunk of a streaming response.
type StreamEvent ¶
type SubAgent ¶
type SubAgent struct {
Agent *Agent
Description string
// InputMapper optionally transforms parent messages before passing them to the child agent.
InputMapper func([]Message) []Message
}
SubAgent represents a child agent that can be invoked as a tool by a parent agent.
type SubAgentResult ¶
SubAgentResult holds the result of a subagent execution.
func RunSubAgent ¶
func RunSubAgent(ctx context.Context, runner Runner, sub *SubAgent, messages []Message) (*SubAgentResult, error)
RunSubAgent runs a single subagent with the provided messages. If the SubAgent has an InputMapper, it is applied to the messages before execution. Context isolation is enforced: the child agent receives its own message slice.
func RunSubAgentsParallel ¶
func RunSubAgentsParallel(ctx context.Context, runner Runner, subs []*SubAgent, messages []Message) ([]*SubAgentResult, error)
RunSubAgentsParallel runs multiple subagents concurrently and collects all results. All subagents are started simultaneously using a sync.WaitGroup. Errors from individual subagents are captured in their SubAgentResult and do not abort others.
type Tool ¶
type Tool interface {
Name() string
Description() string
Schema() json.RawMessage
Execute(ctx context.Context, params json.RawMessage) (any, error)
}
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
}
type ToolChoice ¶
type ToolChoice string
const ( ToolChoiceAuto ToolChoice = "auto" ToolChoiceRequired ToolChoice = "required" ToolChoiceNone ToolChoice = "none" )
func ToolChoiceSpecific ¶
func ToolChoiceSpecific(name string) ToolChoice
type ToolDefinition ¶
type ToolDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Schema json.RawMessage `json:"schema"`
}
func ToolDefFromTool ¶
func ToolDefFromTool(t Tool) ToolDefinition
type ToolResult ¶
type Trace ¶
type Usage ¶
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
}
func (Usage) TotalTokens ¶
type ValidationData ¶
type ValidationData struct {
Messages []Message
ToolCall *ToolCall
ToolResult *ToolResult
Agent *Agent
}
type ValidationResult ¶
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
bedrock
command
|
|
|
graph
command
|
|
|
guardrails
command
|
|
|
handoff
command
|
|
|
hatchet
command
|
|
|
mcp-client
command
|
|
|
mcp-server
command
|
|
|
multi-provider
command
|
|
|
state
command
|
|
|
streaming
command
|
|
|
temporal
command
|
|
|
tracing
command
|
|
|
trigger
command
|
|
|
internal
|
|
|
Package provider implements LLM provider abstractions for the graft framework.
|
Package provider implements LLM provider abstractions for the graft framework. |
|
anthropic
Package anthropic implements the Anthropic Messages API provider for the graft framework.
|
Package anthropic implements the Anthropic Messages API provider for the graft framework. |
|
bedrock
Package bedrock implements the AWS Bedrock Converse API provider for the graft framework.
|
Package bedrock implements the AWS Bedrock Converse API provider for the graft framework. |
|
google
Package google implements the Google Generative Language (Gemini) API provider for graft.
|
Package google implements the Google Generative Language (Gemini) API provider for graft. |
|
openai
Package openai implements an OpenAI-compatible LLM provider for the graft framework.
|
Package openai implements an OpenAI-compatible LLM provider for the graft framework. |