conversation

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

包 conversation 提供多智能体对话编排与对话状态管理能力。

概述

conversation 解决两个核心问题:一是如何让多个 Agent 在同一对话中 协作(谁先说、谁后说、何时终止);二是如何管理对话的分支、回滚与 快照,使对话历史具备版本控制能力。

核心接口

  • ConversationAgent:对话参与者接口,定义 ID / Name / SystemPrompt / Reply / ShouldTerminate 五个方法
  • SpeakerSelector:发言人选择器接口,决定下一轮由哪个 Agent 发言
  • LLMClient:LLM 调用接口,供 LLMSelector 使用

主要能力

  • 多种对话模式:通过 ConversationMode 枚举支持 RoundRobin(轮询)、 Selector(LLM 选择)、GroupChat(自由讨论)、Hierarchical(层级委派)、 AutoReply(自动应答链)五种编排模式
  • 对话生命周期:Conversation.Start 驱动完整的对话循环, 支持最大轮次、最大消息数、超时、终止词等多维终止条件
  • 分支管理:ConversationTree 提供 Fork / SwitchBranch / MergeBranch / DeleteBranch 等 Git 风格的对话分支操作
  • 状态回滚:Rollback / RollbackN 支持按状态 ID 或步数 回退到历史节点
  • 快照机制:Snapshot / FindSnapshot / RestoreSnapshot 支持 为关键对话节点打标签并随时恢复
  • 序列化:Export / Import 支持对话树的 JSON 持久化与恢复
  • 群聊管理:GroupChatManager 统一管理多个并行对话实例

内置实现

  • RoundRobinSelector:按 Agent 列表顺序轮流选择发言人
  • LLMSelector:基于 LLM 智能选择下一位发言人(无 LLM 时 回退为轮询)

与其他包协同

conversation 是多 Agent 协作的编排层。它依赖 agent/context 进行上下文窗口管理,与 agent/tools 配合实现对话中的工具调用, 对话产生的中间结果可通过 agent/artifacts 持久化。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Branch

type Branch struct {
	ID          string               `json:"id"`
	Name        string               `json:"name"`
	Description string               `json:"description,omitempty"`
	States      []*ConversationState `json:"states"`
	CreatedAt   time.Time            `json:"created_at"`
	UpdatedAt   time.Time            `json:"updated_at"`
	IsActive    bool                 `json:"is_active"`
}

分会代表谈话分会.

type ChatMessage

type ChatMessage struct {
	ID        string         `json:"id"`
	Role      string         `json:"role"` // user, assistant, system
	SenderID  string         `json:"sender_id"`
	Content   string         `json:"content"`
	Timestamp time.Time      `json:"timestamp"`
	Metadata  map[string]any `json:"metadata,omitempty"`
}

ChatMessage代表谈话中的信息.

type Conversation

type Conversation struct {
	ID       string
	Mode     ConversationMode
	Agents   []ConversationAgent
	Messages []ChatMessage
	Config   ConversationConfig
	Selector SpeakerSelector
	// contains filtered or unexported fields
}

对话管弦乐会多代理对话.

func NewConversation

func NewConversation(mode ConversationMode, agents []ConversationAgent, config ConversationConfig, logger *zap.Logger) *Conversation

新联想创造了一个新的对话。

func (*Conversation) GetMessages

func (c *Conversation) GetMessages() []ChatMessage

GetMessages 返回所有信件 。

func (*Conversation) Start

func (c *Conversation) Start(ctx context.Context, initialMessage string) (*ConversationResult, error)

以初始消息启动对话 。

type ConversationAgent

type ConversationAgent interface {
	ID() string
	Name() string
	SystemPrompt() string
	Reply(ctx context.Context, messages []ChatMessage) (*ChatMessage, error)
	ShouldTerminate(messages []ChatMessage) bool
}

对话代理界面 。

type ConversationConfig

type ConversationConfig struct {
	MaxRounds        int           `json:"max_rounds"`
	MaxMessages      int           `json:"max_messages"`
	Timeout          time.Duration `json:"timeout"`
	AllowInterrupts  bool          `json:"allow_interrupts"`
	TerminationWords []string      `json:"termination_words"`
}

对话 Config 配置对话 。

func DefaultConversationConfig

func DefaultConversationConfig() ConversationConfig

默认 Conversation Config 返回默认配置 。

type ConversationMode

type ConversationMode string

对话 模式定义代理如何互动 。

const (
	ModeRoundRobin   ConversationMode = "round_robin"  // Agents take turns
	ModeSelector     ConversationMode = "selector"     // Selector chooses next speaker
	ModeGroupChat    ConversationMode = "group_chat"   // Free-form group discussion
	ModeHierarchical ConversationMode = "hierarchical" // Manager delegates
	ModeAutoReply    ConversationMode = "auto_reply"   // Automatic response chain
)

type ConversationResult

type ConversationResult struct {
	ConversationID    string        `json:"conversation_id"`
	Messages          []ChatMessage `json:"messages"`
	TotalRounds       int           `json:"total_rounds"`
	StartTime         time.Time     `json:"start_time"`
	EndTime           time.Time     `json:"end_time"`
	TerminationReason string        `json:"termination_reason"`
}

对话Result包含对话结果.

type ConversationState

type ConversationState struct {
	ID        string         `json:"id"`
	ParentID  string         `json:"parent_id,omitempty"`
	Messages  []llm.Message  `json:"messages"`
	Metadata  map[string]any `json:"metadata,omitempty"`
	CreatedAt time.Time      `json:"created_at"`
	Label     string         `json:"label,omitempty"`
}

对话状态(Conversation State)代表时间点的对话状态.

type ConversationTree

type ConversationTree struct {
	ID           string             `json:"id"`
	RootState    *ConversationState `json:"root_state"`
	Branches     map[string]*Branch `json:"branches"`
	ActiveBranch string             `json:"active_branch"`
	// contains filtered or unexported fields
}

对话 树用分支管理对话历史.

func Import

func Import(data []byte) (*ConversationTree, error)

导入 JSON 的谈话树 。

func NewConversationTree

func NewConversationTree(id string) *ConversationTree

新建组合 树创造出一棵新的对话树.

func (*ConversationTree) AddMessage

func (t *ConversationTree) AddMessage(msg llm.Message) *ConversationState

添加 Message 为活动分支添加了消息 。

func (*ConversationTree) DeleteBranch

func (t *ConversationTree) DeleteBranch(branchName string) error

删除Branch删除一个分支(不能删除活动分支或主分支).

func (*ConversationTree) Export

func (t *ConversationTree) Export() ([]byte, error)

导出对话树给 JSON 。

func (*ConversationTree) FindSnapshot

func (t *ConversationTree) FindSnapshot(label string) *ConversationState

FindSnapshot通过标签找到快照.

func (*ConversationTree) Fork

func (t *ConversationTree) Fork(branchName string) (*Branch, error)

叉从当前状态创建出一个新的分支.

func (*ConversationTree) GetCurrentState

func (t *ConversationTree) GetCurrentState() *ConversationState

GetCurentState 返回活动分支当前状态 。

func (*ConversationTree) GetHistory

func (t *ConversationTree) GetHistory() []*ConversationState

GetHistory还原了目前分行的州史.

func (*ConversationTree) GetMessages

func (t *ConversationTree) GetMessages() []llm.Message

GetMessages 返回当前状态下的所有信件 。

func (*ConversationTree) ListBranches

func (t *ConversationTree) ListBranches() []string

ListBranches 返回所有分支名称 。

func (*ConversationTree) MergeBranch

func (t *ConversationTree) MergeBranch(sourceBranch string) error

合并Branch将一个分支合并到活动分支.

func (*ConversationTree) RestoreSnapshot

func (t *ConversationTree) RestoreSnapshot(label string) error

还原Snapshot恢复到标签快照.

func (*ConversationTree) Rollback

func (t *ConversationTree) Rollback(stateID string) error

后滚回当前分行的上个状态.

func (*ConversationTree) RollbackN

func (t *ConversationTree) RollbackN(n int) error

滚回N 滚回N州。

func (*ConversationTree) Snapshot

func (t *ConversationTree) Snapshot(label string) *ConversationState

抓图创建当前状态的标签快照.

func (*ConversationTree) SwitchBranch

func (t *ConversationTree) SwitchBranch(branchName string) error

切换Branch切换到不同的分支.

type GroupChatManager

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

GroupChatManager管理分组聊天对话.

func NewGroupChatManager

func NewGroupChatManager(logger *zap.Logger) *GroupChatManager

NewGroupChatManager创建了新的分组聊天管理器.

func (*GroupChatManager) CreateChat

func (m *GroupChatManager) CreateChat(agents []ConversationAgent, config ConversationConfig) *Conversation

CreateChat 创建了新的分组聊天.

func (*GroupChatManager) GetChat

func (m *GroupChatManager) GetChat(id string) (*Conversation, bool)

GetChat通过身份证检索谈话内容.

type LLMClient

type LLMClient interface {
	Complete(ctx context.Context, prompt string) (string, error)
}

LLM电话的LLMClient接口.

type LLMSelector

type LLMSelector struct {
	LLM LLMClient
}

LLMSelector使用LLM来选择下一位扬声器.

func (*LLMSelector) SelectNext

func (s *LLMSelector) SelectNext(ctx context.Context, agents []ConversationAgent, messages []ChatMessage) (ConversationAgent, error)

type RoundRobinSelector

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

roundRobinSelector按顺序选择代理.

func (*RoundRobinSelector) SelectNext

func (s *RoundRobinSelector) SelectNext(ctx context.Context, agents []ConversationAgent, messages []ChatMessage) (ConversationAgent, error)

type SpeakerSelector

type SpeakerSelector interface {
	SelectNext(ctx context.Context, agents []ConversationAgent, messages []ChatMessage) (ConversationAgent, error)
}

发言人选择下一位发言人.

Jump to

Keyboard shortcuts

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