Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package session 实现会话管理功能,用于存储和检索用户与 Agent 之间的聊天历史记录。

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 负责管理内存中的活跃会话,并将其持久化到磁盘。

func NewManager

func NewManager(baseDir string) *Manager

NewManager 在指定的基础目录下创建一个新的会话管理器。

func (*Manager) Append added in v0.6.4

func (m *Manager) Append(key string, msgs ...*Message) error

Append 将一组新消息增量追加到指定的会话持久化文件中。

func (*Manager) GetOrCreate

func (m *Manager) GetOrCreate(key string) *Session

GetOrCreate 获取指定键值的会话实例。如果内存中不存在,则尝试从磁盘加载或创建一个新会话。

func (*Manager) Reset added in v0.6.0

func (m *Manager) Reset(key string)

Reset 清除会话在内存中的历史记录,并从磁盘中永久删除对应的会话文件。

func (*Manager) Save

func (m *Manager) Save(sess *Session) error

Save 将指定的会话内容完整覆盖写入到磁盘文件中。

type Message

type Message struct {
	Role      string    `json:"role"`      // 角色:user 或 assistant
	Content   string    `json:"content"`   // 消息文本内容
	Timestamp time.Time `json:"timestamp"` // 消息产生的时间戳
}

Message 表示会话中的单条消息记录。

type Session

type Session struct {
	Key      string     // 会话的唯一键值
	Messages []*Message // 消息历史列表
	// contains filtered or unexported fields
}

Session 表示一个完整的对话会话,包含唯一的标识符和消息序列。

func (*Session) AddMessage

func (s *Session) AddMessage(role, content string) *Message

AddMessage 向会话中追加一条新消息。

func (*Session) GetHistory

func (s *Session) GetHistory(limit int) []*Message

GetHistory 返回会话中最近的 n 条消息。

Jump to

Keyboard shortcuts

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