session

package
v0.31.4 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages session state and auto-saving functionality. It provides thread-safe operations for managing a conversation session, including automatic persistence to disk after each modification. The Manager ensures that all session operations are synchronized and that the session file is kept up-to-date with any changes.

func NewManager

func NewManager(filePath string) *Manager

NewManager creates a new session manager with a fresh session. The filePath parameter specifies where the session will be auto-saved. If filePath is empty, the session will not be automatically saved to disk. Returns a Manager instance ready to track conversation messages.

func NewManagerWithSession

func NewManagerWithSession(session *Session, filePath string) *Manager

NewManagerWithSession creates a new session manager with an existing session. This is useful when loading a session from a file and wanting to continue managing it with auto-save functionality. The session parameter is the existing session to manage. The filePath parameter specifies where the session will be auto-saved.

func (*Manager) AddMessage

func (m *Manager) AddMessage(msg *schema.Message) error

AddMessage adds a message to the session and auto-saves. The message is converted from schema.Message format to the internal session Message format before being added. If a filePath was specified when creating the Manager, the session is automatically saved to disk. This operation is thread-safe. Returns an error if auto-saving fails, nil otherwise.

func (*Manager) AddMessages

func (m *Manager) AddMessages(msgs []*schema.Message) error

AddMessages adds multiple messages to the session and auto-saves. All messages are added in order and then the session is saved once. This is more efficient than calling AddMessage multiple times when adding several messages at once. The operation is thread-safe. Returns an error if auto-saving fails, nil otherwise.

func (*Manager) GetFilePath

func (m *Manager) GetFilePath() string

GetFilePath returns the file path for this session. Returns the path where the session is being auto-saved, or an empty string if no auto-save path was configured.

func (*Manager) GetMessages

func (m *Manager) GetMessages() []*schema.Message

GetMessages returns all messages as a schema.Message slice. This method converts all stored session messages to the schema format used by LLM providers. The returned slice is a new allocation, so modifications to it won't affect the stored session. This operation is thread-safe for concurrent reads.

func (*Manager) GetSession

func (m *Manager) GetSession() *Session

GetSession returns a copy of the current session. The returned session is a deep copy, including all messages, so modifications to it won't affect the managed session. This is useful for safely inspecting the session state without risk of concurrent modification. This operation is thread-safe for concurrent reads.

func (*Manager) MessageCount

func (m *Manager) MessageCount() int

MessageCount returns the number of messages in the session. This provides a quick way to check the conversation length without retrieving all messages. This operation is thread-safe for concurrent reads.

func (*Manager) ReplaceAllMessages

func (m *Manager) ReplaceAllMessages(msgs []*schema.Message) error

ReplaceAllMessages replaces all messages in the session with the provided messages. This method completely clears the existing message history and replaces it with the new set of messages. Useful for resetting a conversation or loading a different conversation context. The operation is thread-safe and triggers an auto-save if a filePath is configured. Returns an error if auto-saving fails, nil otherwise.

func (*Manager) Save

func (m *Manager) Save() error

Save manually saves the session to file. This forces a save operation even if no changes have been made. Useful for ensuring the session is persisted at specific points. Returns an error if no filePath was specified when creating the Manager, or if the save operation fails.

func (*Manager) SetMetadata

func (m *Manager) SetMetadata(metadata Metadata) error

SetMetadata sets the session metadata. This updates the session's metadata with information about the provider, model, and MCPHost version. The operation is thread-safe and triggers an auto-save if a filePath is configured. Returns an error if auto-saving fails, nil otherwise.

type Message

type Message struct {
	// ID is a unique identifier for this message, auto-generated if not provided
	ID string `json:"id"`
	// Role indicates who sent the message ("user", "assistant", "tool", or "system")
	Role string `json:"role"`
	// Content is the text content of the message
	Content string `json:"content"`
	// Timestamp is when the message was created
	Timestamp time.Time `json:"timestamp"`
	// ToolCalls contains any tool invocations made by the assistant in this message
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
	// ToolCallID links a tool result message to its corresponding tool call
	ToolCallID string `json:"tool_call_id,omitempty"`
}

Message represents a single message in the conversation session. Messages can be from different roles (user, assistant, tool) and may include tool calls for assistant messages or tool results for tool messages.

func ConvertFromSchemaMessage

func ConvertFromSchemaMessage(msg *schema.Message) Message

ConvertFromSchemaMessage converts a schema.Message to a session Message. This function bridges between the eino schema message format and the session's internal message format. It preserves role, content, and tool-related information while adding a timestamp. Tool calls from assistant messages and tool call IDs from tool messages are properly converted and preserved.

func (*Message) ConvertToSchemaMessage

func (m *Message) ConvertToSchemaMessage() *schema.Message

ConvertToSchemaMessage converts a session Message to a schema.Message. This method bridges between the session's internal message format and the eino schema message format used by the LLM providers. It properly handles tool calls for assistant messages and tool call IDs for tool result messages. Arguments are converted to string format as required by the schema.

type Metadata

type Metadata struct {
	// MCPHostVersion is the version of MCPHost used for this session
	MCPHostVersion string `json:"mcphost_version"`
	// Provider is the LLM provider used (e.g., "anthropic", "openai", "gemini")
	Provider string `json:"provider"`
	// Model is the specific model identifier used for the conversation
	Model string `json:"model"`
}

Metadata contains session metadata that provides context about the environment and configuration used during the conversation. This helps with debugging and understanding the session's context when reviewing conversation history.

type Session

type Session struct {
	// Version indicates the session format version for compatibility
	Version string `json:"version"`
	// CreatedAt is the timestamp when the session was first created
	CreatedAt time.Time `json:"created_at"`
	// UpdatedAt is the timestamp when the session was last modified
	UpdatedAt time.Time `json:"updated_at"`
	// Metadata contains contextual information about the session
	Metadata Metadata `json:"metadata"`
	// Messages is the ordered list of all messages in this session
	Messages []Message `json:"messages"`
}

Session represents a complete conversation session with metadata. It stores all messages exchanged during a conversation along with contextual information about the session such as the provider, model, and timestamps. Sessions can be saved to and loaded from JSON files for persistence across program runs.

func LoadFromFile

func LoadFromFile(filePath string) (*Session, error)

LoadFromFile loads a session from a JSON file. It reads the file at the specified path and deserializes it into a Session struct. This is useful for resuming previous conversations or reviewing session history. Returns the loaded session on success, or an error if the file cannot be read or the JSON is invalid.

func NewSession

func NewSession() *Session

NewSession creates a new session with default values. It initializes a session with version 1.0, current timestamps, empty message list, and empty metadata. The returned session is ready to receive messages and can be saved to a file.

func (*Session) AddMessage

func (s *Session) AddMessage(msg Message)

AddMessage adds a message to the session. If the message doesn't have an ID, one will be auto-generated. If the message doesn't have a timestamp, the current time will be used. The session's UpdatedAt timestamp is automatically updated.

func (*Session) SaveToFile

func (s *Session) SaveToFile(filePath string) error

SaveToFile saves the session to a JSON file. The session is serialized as indented JSON for readability. The UpdatedAt timestamp is automatically updated before saving. The file is created with 0644 permissions if it doesn't exist, or overwritten if it does exist. Returns an error if marshaling fails or file writing fails.

func (*Session) SetMetadata

func (s *Session) SetMetadata(metadata Metadata)

SetMetadata sets the session metadata. This replaces the existing metadata with the provided metadata and updates the session's UpdatedAt timestamp. Use this to record information about the provider, model, and MCPHost version.

type ToolCall

type ToolCall struct {
	// ID is a unique identifier for this tool call, used to link results
	ID string `json:"id"`
	// Name is the name of the tool being invoked
	Name string `json:"name"`
	// Arguments contains the parameters passed to the tool, typically as JSON
	Arguments any `json:"arguments"`
}

ToolCall represents a tool invocation within an assistant message. When the assistant decides to use a tool, it creates a ToolCall with the necessary information to execute that tool.

Jump to

Keyboard shortcuts

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