core

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DurableRun

func DurableRun[T any](ctx context.Context, executor DurableExecutor, name string, fn func(ctx context.Context) (T, error)) (T, error)

DurableRun is a helper function for type-safe durable execution. Usage: result, err := DurableRun[MyType](ctx, executor, "step-name", func(ctx) (MyType, error) { ... })

func NilCallback

func NilCallback(msg *responses.ResponseChunk)

Types

type BaseTool

type BaseTool struct {
	*responses.ToolUnion
	RequiresApproval bool
}

func (*BaseTool) NeedApproval

func (t *BaseTool) NeedApproval() bool

type ChatHistory

type ChatHistory interface {
	// AddMessages adds new messages into a list in-memory
	AddMessages(ctx context.Context, messages []responses.InputMessageUnion, usage *responses.Usage)

	// GetMessages returns a list of messages from in-memory. It handles summarization if enabled
	GetMessages(ctx context.Context) ([]responses.InputMessageUnion, error)

	// LoadMessages fetches all the messages of the thread from persistent storage into memory
	LoadMessages(ctx context.Context, namespace string, previousMessageID string) ([]responses.InputMessageUnion, error)

	// SaveMessages saves the messages to persistent storage
	SaveMessages(ctx context.Context, meta map[string]any) error

	// GetMeta returns the meta from the most recent message
	GetMeta() map[string]any

	// GetMessageID returns the current run id
	GetMessageID() string
}

type DurableExecutor

type DurableExecutor interface {
	// Run executes a function with durability guarantees.
	// If the process crashes after fn() completes, the result is restored
	// from the checkpoint instead of re-executing fn().
	//
	// The name parameter is used to identify the checkpoint (must be unique per step).
	Run(ctx context.Context, name string, fn func(ctx context.Context) (any, error)) (any, error)

	// Set stores a value in durable state.
	// The value persists across crashes and can be retrieved with Get().
	Set(ctx context.Context, key string, value any) error

	// Get retrieves a value from durable state.
	// Returns the value and true if found, or nil and false if not found.
	Get(ctx context.Context, key string) (any, bool, error)

	// Checkpoint creates an explicit checkpoint marker.
	// Useful for marking progress without storing data.
	Checkpoint(ctx context.Context, name string) error
}

DurableExecutor provides an abstraction for durable/checkpointed execution. Implementations can provide different durability guarantees: - NoOpExecutor: No durability (default, existing behavior) - RestateExecutor: Durable via Restate - TemporalExecutor: Durable via Temporal (future) - RedisExecutor: Durable via Redis Streams (future)

type HistorySummarizer

type HistorySummarizer interface {
	// Summarize takes a list of messages and returns a summary result.
	// If summarization is not needed, returns a result with KeepFromIndex = -1.
	Summarize(ctx context.Context, msgIdToRunId map[string]string, messages []responses.InputMessageUnion, usage *responses.Usage) (*SummaryResult, error)
}

type NoOpExecutor

type NoOpExecutor struct{}

NoOpExecutor is the default executor with no durability. It simply executes functions directly without checkpointing. Use this for existing behavior or when durability is not needed.

func NewNoOpExecutor

func NewNoOpExecutor() *NoOpExecutor

NewNoOpExecutor creates a new no-op executor (existing behavior).

func (*NoOpExecutor) Checkpoint

func (e *NoOpExecutor) Checkpoint(ctx context.Context, name string) error

Checkpoint is a no-op for the NoOpExecutor.

func (*NoOpExecutor) Get

func (e *NoOpExecutor) Get(ctx context.Context, key string) (any, bool, error)

Get always returns not found for the NoOpExecutor.

func (*NoOpExecutor) Run

func (e *NoOpExecutor) Run(ctx context.Context, name string, fn func(ctx context.Context) (any, error)) (any, error)

Run executes the function directly without checkpointing.

func (*NoOpExecutor) Set

func (e *NoOpExecutor) Set(ctx context.Context, key string, value any) error

Set is a no-op for the NoOpExecutor.

type RunState

type RunState struct {
	CurrentStep           Step                            `json:"current_step"`
	LoopIteration         int                             `json:"loop_iteration"`
	Usage                 responses.Usage                 `json:"usage"`
	PendingToolCalls      []responses.FunctionCallMessage `json:"pending_tool_calls,omitempty"`
	ToolsAwaitingApproval []responses.FunctionCallMessage `json:"tools_awaiting_approval,omitempty"`
}

RunState encapsulates the execution state of an agent run

func LoadRunStateFromMeta

func LoadRunStateFromMeta(meta map[string]any) *RunState

LoadRunStateFromMeta loads RunState from messages.meta

func NewRunState

func NewRunState() *RunState

NewRunState creates initial state for a fresh run

func (*RunState) ClearPendingTools

func (s *RunState) ClearPendingTools()

ClearPendingTools clears the pending tool calls

func (*RunState) HasToolsAwaitingApproval

func (s *RunState) HasToolsAwaitingApproval() bool

HasToolsAwaitingApproval returns true if there are tools waiting for approval

func (*RunState) IsComplete

func (s *RunState) IsComplete() bool

IsComplete returns true if the state is complete

func (*RunState) IsPaused

func (s *RunState) IsPaused() bool

IsPaused returns true if the state is awaiting approval

func (*RunState) NextStep

func (s *RunState) NextStep() Step

NextStep returns what the agent should do next

func (*RunState) PromoteAwaitingToApproval

func (s *RunState) PromoteAwaitingToApproval()

PromoteAwaitingToApproval moves tools awaiting approval to pending and transitions to await state

func (*RunState) ToMeta

func (s *RunState) ToMeta(traceid string) map[string]any

ToMeta converts RunState to a map for storage in messages.meta

func (*RunState) TransitionToAwaitApproval

func (s *RunState) TransitionToAwaitApproval(tools []responses.FunctionCallMessage)

TransitionToAwaitApproval moves to await approval step with the given tools

func (*RunState) TransitionToComplete

func (s *RunState) TransitionToComplete()

TransitionToComplete moves to the complete step and clears pending tools

func (*RunState) TransitionToExecuteTools

func (s *RunState) TransitionToExecuteTools(tools []responses.FunctionCallMessage)

TransitionToExecuteTools moves to tool execution step with the given tools

func (*RunState) TransitionToLLM

func (s *RunState) TransitionToLLM()

TransitionToLLM moves to the LLM call step and increments loop iteration

type RunStatus

type RunStatus string

RunStatus represents the overall status of a run

const (
	RunStatusInProgress RunStatus = "running"
	RunStatusPaused     RunStatus = "paused"
	RunStatusCompleted  RunStatus = "completed"
	RunStatusError      RunStatus = "error"
)

type Step

type Step string

Step represents the current step in the agent execution state machine

const (
	StepCallLLM       Step = "call_llm"
	StepExecuteTools  Step = "execute_tools"
	StepAwaitApproval Step = "await_approval"
	StepComplete      Step = "complete"
)

type SummaryResult

type SummaryResult struct {
	Summary                 *responses.InputMessageUnion // The summary message
	MessagesToKeep          []responses.InputMessageUnion
	LastSummarizedMessageID string // ID of the last message that was summarized
	SummaryID               string // Unique ID for the summary (generated if empty)
}

SummaryResult contains the result of summarization including metadata needed for saving

type SystemPromptProvider

type SystemPromptProvider interface {
	GetPrompt(ctx context.Context, data map[string]any) (string, error)
}

type Tool

type Tool interface {
	Execute(ctx context.Context, params *responses.FunctionCallMessage) (*responses.FunctionCallOutputMessage, error)
	Tool(ctx context.Context) *responses.ToolUnion
	NeedApproval() bool
}

Jump to

Keyboard shortcuts

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