memory

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package memory provides memory management for agent conversations and execution history.

This includes step-by-step tracking of agent actions, tool calls, planning steps, and conversation history management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionStep

type ActionStep struct {
	StepNumber         int                    `json:"step_number"`
	Timing             monitoring.Timing      `json:"timing"`
	ModelInputMessages []Message              `json:"model_input_messages,omitempty"`
	ToolCalls          []ToolCall             `json:"tool_calls,omitempty"`
	Error              error                  `json:"error,omitempty"`
	ModelOutputMessage *models.ChatMessage    `json:"model_output_message,omitempty"`
	ModelOutput        string                 `json:"model_output,omitempty"`
	Observations       string                 `json:"observations,omitempty"`
	ObservationImages  []*models.MediaContent `json:"observation_images,omitempty"`
	ActionOutput       interface{}            `json:"action_output,omitempty"`
	TokenUsage         *monitoring.TokenUsage `json:"token_usage,omitempty"`
}

ActionStep represents an action taken by the agent (tool calls, model interactions)

func NewActionStep

func NewActionStep(stepNumber int, startTime ...time.Time) *ActionStep

NewActionStep creates a new action step

func NewActionStepWithImages

func NewActionStepWithImages(stepNumber int, startTime time.Time, images []*models.MediaContent) *ActionStep

NewActionStepWithImages creates a new action step with images

func (*ActionStep) GetActionOutput

func (as *ActionStep) GetActionOutput() interface{}

func (*ActionStep) GetError

func (as *ActionStep) GetError() error

func (*ActionStep) GetModelInputMessages

func (as *ActionStep) GetModelInputMessages() []Message

func (*ActionStep) GetModelOutput

func (as *ActionStep) GetModelOutput() string

func (*ActionStep) GetModelOutputMessage

func (as *ActionStep) GetModelOutputMessage() *models.ChatMessage

func (*ActionStep) GetObservationImages

func (as *ActionStep) GetObservationImages() []*models.MediaContent

func (*ActionStep) GetObservations

func (as *ActionStep) GetObservations() string

func (*ActionStep) GetStepNumber

func (as *ActionStep) GetStepNumber() int

Getter and setter methods for ActionStep

func (*ActionStep) GetTiming

func (as *ActionStep) GetTiming() monitoring.Timing

func (*ActionStep) GetTokenUsage

func (as *ActionStep) GetTokenUsage() *monitoring.TokenUsage

func (*ActionStep) GetToolCalls

func (as *ActionStep) GetToolCalls() []ToolCall

func (*ActionStep) GetType

func (as *ActionStep) GetType() string

GetType implements MemoryStep

func (*ActionStep) SetActionOutput

func (as *ActionStep) SetActionOutput(output interface{})

func (*ActionStep) SetError

func (as *ActionStep) SetError(err error)

func (*ActionStep) SetModelInputMessages

func (as *ActionStep) SetModelInputMessages(msgs []Message)

func (*ActionStep) SetModelOutput

func (as *ActionStep) SetModelOutput(output string)

func (*ActionStep) SetModelOutputMessage

func (as *ActionStep) SetModelOutputMessage(msg *models.ChatMessage)

func (*ActionStep) SetObservationImages

func (as *ActionStep) SetObservationImages(images []*models.MediaContent)

func (*ActionStep) SetObservations

func (as *ActionStep) SetObservations(obs string)

func (*ActionStep) SetStepNumber

func (as *ActionStep) SetStepNumber(n int)

func (*ActionStep) SetTiming

func (as *ActionStep) SetTiming(t monitoring.Timing)

func (*ActionStep) SetTokenUsage

func (as *ActionStep) SetTokenUsage(usage *monitoring.TokenUsage)

func (*ActionStep) SetToolCalls

func (as *ActionStep) SetToolCalls(calls []ToolCall)

func (*ActionStep) ToDict

func (as *ActionStep) ToDict() (map[string]interface{}, error)

ToDict implements MemoryStep

func (*ActionStep) ToMessages

func (as *ActionStep) ToMessages(summaryMode bool) ([]Message, error)

ToMessages implements MemoryStep

type AgentMemory

type AgentMemory struct {
	SystemPrompt *SystemPromptStep `json:"system_prompt,omitempty"`
	Steps        []MemoryStep      `json:"steps"`
}

AgentMemory represents the agent's conversation memory

func NewAgentMemory

func NewAgentMemory(systemPrompt string) *AgentMemory

NewAgentMemory creates a new agent memory instance

func (*AgentMemory) AddStep

func (am *AgentMemory) AddStep(step MemoryStep)

AddStep adds a memory step to the agent's memory

func (*AgentMemory) GetActionSteps

func (am *AgentMemory) GetActionSteps() []*ActionStep

GetActionSteps returns only the action steps from memory

func (*AgentMemory) GetFullSteps

func (am *AgentMemory) GetFullSteps() ([]map[string]interface{}, error)

GetFullSteps returns all steps as dictionaries (matching Python API)

func (*AgentMemory) GetLastStep

func (am *AgentMemory) GetLastStep() MemoryStep

GetLastStep returns the last step in memory, or nil if no steps exist

func (*AgentMemory) GetPlanningSteps

func (am *AgentMemory) GetPlanningSteps() []*PlanningStep

GetPlanningSteps returns only the planning steps from memory

func (*AgentMemory) GetStepCount

func (am *AgentMemory) GetStepCount() int

GetStepCount returns the number of steps in memory

func (*AgentMemory) GetSteps

func (am *AgentMemory) GetSteps() []MemoryStep

GetSteps returns all memory steps

func (*AgentMemory) GetSystemPrompt

func (am *AgentMemory) GetSystemPrompt() *SystemPromptStep

GetSystemPrompt returns the system prompt step

func (*AgentMemory) MarshalJSON

func (am *AgentMemory) MarshalJSON() ([]byte, error)

MarshalJSON custom marshaling for AgentMemory

func (*AgentMemory) Replay

func (am *AgentMemory) Replay(logger interface{}, detailed bool) error

Replay prints a replay of the agent's steps (simplified implementation)

func (*AgentMemory) Reset

func (am *AgentMemory) Reset()

Reset clears all memory except system prompt

func (*AgentMemory) SetSystemPrompt

func (am *AgentMemory) SetSystemPrompt(prompt string)

SetSystemPrompt sets the system prompt for the agent

func (*AgentMemory) ToDict

func (am *AgentMemory) ToDict() (map[string]interface{}, error)

ToDict converts the agent memory to a dictionary representation

func (*AgentMemory) WriteMemoryToMessages

func (am *AgentMemory) WriteMemoryToMessages(summaryMode bool) ([]Message, error)

WriteMemoryToMessages converts the agent's memory to a list of messages for model consumption

type FinalAnswerStep

type FinalAnswerStep struct {
	Output interface{} `json:"output"`
}

FinalAnswerStep represents the final answer from the agent

func NewFinalAnswerStep

func NewFinalAnswerStep(output interface{}) *FinalAnswerStep

NewFinalAnswerStep creates a new final answer step

func (*FinalAnswerStep) GetOutput

func (fas *FinalAnswerStep) GetOutput() interface{}

Getter and setter methods for FinalAnswerStep

func (*FinalAnswerStep) GetType

func (fas *FinalAnswerStep) GetType() string

GetType implements MemoryStep

func (*FinalAnswerStep) SetOutput

func (fas *FinalAnswerStep) SetOutput(output interface{})

func (*FinalAnswerStep) ToDict

func (fas *FinalAnswerStep) ToDict() (map[string]interface{}, error)

ToDict implements MemoryStep

func (*FinalAnswerStep) ToMessages

func (fas *FinalAnswerStep) ToMessages(summaryMode bool) ([]Message, error)

ToMessages implements MemoryStep

type MemoryStep

type MemoryStep interface {
	// ToMessages converts the step to a list of messages for model consumption
	ToMessages(summaryMode bool) ([]Message, error)

	// ToDict converts the step to a dictionary representation
	ToDict() (map[string]interface{}, error)

	// GetType returns the type identifier for this step
	GetType() string
}

MemoryStep is the interface that all memory steps must implement

type Message

type Message struct {
	Role       models.MessageRole       `json:"role"`
	Content    []map[string]interface{} `json:"content"`
	Name       string                   `json:"name,omitempty"`
	ToolCalls  []ToolCall               `json:"tool_calls,omitempty"`
	ToolCallID string                   `json:"tool_call_id,omitempty"`
	Images     []image.Image            `json:"images,omitempty"`
	Metadata   map[string]interface{}   `json:"metadata,omitempty"`
}

Message represents a single message in the conversation

func NewMessage

func NewMessage(role models.MessageRole, content string) *Message

NewMessage creates a new message with the specified role and content

func (*Message) ToDict

func (m *Message) ToDict() map[string]interface{}

ToDict converts the message to a dictionary representation

type PlanningStep

type PlanningStep struct {
	ModelInputMessages []Message              `json:"model_input_messages"`
	ModelOutputMessage models.ChatMessage     `json:"model_output_message"`
	Plan               string                 `json:"plan"`
	Timing             monitoring.Timing      `json:"timing"`
	TokenUsage         *monitoring.TokenUsage `json:"token_usage,omitempty"`
}

PlanningStep represents a planning step by the agent

func NewPlanningStep

func NewPlanningStep(inputMessages []Message, outputMessage models.ChatMessage, plan string, timing monitoring.Timing, tokenUsage *monitoring.TokenUsage) *PlanningStep

NewPlanningStep creates a new planning step

func (*PlanningStep) GetModelInputMessages

func (ps *PlanningStep) GetModelInputMessages() []Message

Getter methods for PlanningStep

func (*PlanningStep) GetModelOutputMessage

func (ps *PlanningStep) GetModelOutputMessage() models.ChatMessage

func (*PlanningStep) GetPlan

func (ps *PlanningStep) GetPlan() string

func (*PlanningStep) GetTiming

func (ps *PlanningStep) GetTiming() monitoring.Timing

func (*PlanningStep) GetTokenUsage

func (ps *PlanningStep) GetTokenUsage() *monitoring.TokenUsage

func (*PlanningStep) GetType

func (ps *PlanningStep) GetType() string

GetType implements MemoryStep

func (*PlanningStep) SetModelInputMessages

func (ps *PlanningStep) SetModelInputMessages(msgs []Message)

func (*PlanningStep) SetModelOutputMessage

func (ps *PlanningStep) SetModelOutputMessage(msg models.ChatMessage)

func (*PlanningStep) SetPlan

func (ps *PlanningStep) SetPlan(plan string)

func (*PlanningStep) SetTiming

func (ps *PlanningStep) SetTiming(timing monitoring.Timing)

func (*PlanningStep) SetTokenUsage

func (ps *PlanningStep) SetTokenUsage(usage *monitoring.TokenUsage)

func (*PlanningStep) ToDict

func (ps *PlanningStep) ToDict() (map[string]interface{}, error)

ToDict implements MemoryStep

func (*PlanningStep) ToMessages

func (ps *PlanningStep) ToMessages(summaryMode bool) ([]Message, error)

ToMessages implements MemoryStep

type SystemPromptStep

type SystemPromptStep struct {
	SystemPrompt string `json:"system_prompt"`
}

SystemPromptStep represents the system prompt

func NewSystemPromptStep

func NewSystemPromptStep(systemPrompt string) *SystemPromptStep

NewSystemPromptStep creates a new system prompt step

func (*SystemPromptStep) GetType

func (sps *SystemPromptStep) GetType() string

GetType implements MemoryStep

func (*SystemPromptStep) ToDict

func (sps *SystemPromptStep) ToDict() (map[string]interface{}, error)

ToDict implements MemoryStep

func (*SystemPromptStep) ToMessages

func (sps *SystemPromptStep) ToMessages(summaryMode bool) ([]Message, error)

ToMessages implements MemoryStep

type TaskStep

type TaskStep struct {
	Task       string                 `json:"task"`
	TaskImages []*models.MediaContent `json:"task_images,omitempty"`
}

TaskStep represents the initial task given to the agent

func NewTaskStep

func NewTaskStep(task string, images ...[]*models.MediaContent) *TaskStep

NewTaskStep creates a new task step

func (*TaskStep) GetType

func (ts *TaskStep) GetType() string

GetType implements MemoryStep

func (*TaskStep) ToDict

func (ts *TaskStep) ToDict() (map[string]interface{}, error)

ToDict implements MemoryStep

func (*TaskStep) ToMessages

func (ts *TaskStep) ToMessages(summaryMode bool) ([]Message, error)

ToMessages implements MemoryStep

type Timing

type Timing struct {
	StartTime time.Time      `json:"start_time"`
	EndTime   *time.Time     `json:"end_time,omitempty"`
	Duration  *time.Duration `json:"duration,omitempty"`
}

Timing represents timing information for operations

func NewTiming

func NewTiming() *Timing

NewTiming creates a new timing instance with start time set to now

func (*Timing) End

func (t *Timing) End()

End marks the end time and calculates duration

type TokenUsage

type TokenUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
	TotalTokens  int `json:"total_tokens"`
}

TokenUsage represents token usage statistics

func (*TokenUsage) Add

func (tu *TokenUsage) Add(other *TokenUsage)

Add combines this token usage with another

type ToolCall

type ToolCall struct {
	ID        string                 `json:"id"`
	Name      string                 `json:"name"`
	Arguments map[string]interface{} `json:"arguments"`
}

ToolCall represents a tool call made by an agent

func (*ToolCall) ToDict

func (tc *ToolCall) ToDict() map[string]interface{}

ToDict converts the tool call to a dictionary representation

Jump to

Keyboard shortcuts

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