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

collaboration

package
v1.11.3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AggregateUsage added in v1.6.0

func AggregateUsage(outputs map[string]*agent.Output) (totalTokens int, totalCost float64)

AggregateUsage sums TokensUsed and Cost from a map of outputs.

func MergeMetadata added in v1.6.0

func MergeMetadata(base, extra map[string]any) map[string]any

MergeMetadata non-destructively merges extra key-value pairs into an existing metadata map. If base is nil a new map is allocated.

func RegisterResearchRoles

func RegisterResearchRoles(registry *RoleRegistry) error

RegisterResearchRoles 注册所有预定义的研究角色

func SortedAgentIDs added in v1.6.0

func SortedAgentIDs(agents map[string]agent.Agent) []string

SortedAgentIDs returns deterministic, lexicographically sorted agent IDs.

Types

type BroadcastCoordinator

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

BroadcastCoordinator 广播协调器

func NewBroadcastCoordinator

func NewBroadcastCoordinator(config MultiAgentConfig, hub *MessageHub, logger *zap.Logger) *BroadcastCoordinator

func (*BroadcastCoordinator) Coordinate

func (c *BroadcastCoordinator) Coordinate(ctx context.Context, agents map[string]agent.Agent, input *agent.Input) (*agent.Output, error)

type CollaborationPattern

type CollaborationPattern string

CollaborationPattern 协作模式

const (
	PatternDebate    CollaborationPattern = "debate"    // 辩论模式
	PatternConsensus CollaborationPattern = "consensus" // 共识模式
	PatternPipeline  CollaborationPattern = "pipeline"  // 流水线模式
	PatternBroadcast CollaborationPattern = "broadcast" // 广播模式
	PatternNetwork   CollaborationPattern = "network"   // 网络模式
)

type ConsensusCoordinator

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

ConsensusCoordinator 共识协调器

func NewConsensusCoordinator

func NewConsensusCoordinator(config MultiAgentConfig, hub *MessageHub, logger *zap.Logger) *ConsensusCoordinator

func (*ConsensusCoordinator) Coordinate

func (c *ConsensusCoordinator) Coordinate(ctx context.Context, agents map[string]agent.Agent, input *agent.Input) (*agent.Output, error)

type Coordinator

type Coordinator interface {
	Coordinate(ctx context.Context, agents map[string]agent.Agent, input *agent.Input) (*agent.Output, error)
}

Coordinator 协调器接口

type DebateCoordinator

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

DebateCoordinator 辩论协调器

func NewDebateCoordinator

func NewDebateCoordinator(config MultiAgentConfig, hub *MessageHub, logger *zap.Logger) *DebateCoordinator

func (*DebateCoordinator) Coordinate

func (c *DebateCoordinator) Coordinate(ctx context.Context, agents map[string]agent.Agent, input *agent.Input) (*agent.Output, error)

type InMemorySharedState added in v1.6.0

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

func NewInMemorySharedState added in v1.6.0

func NewInMemorySharedState() *InMemorySharedState

func NewInMemorySharedStateWithLogger added in v1.6.0

func NewInMemorySharedStateWithLogger(logger *zap.Logger) *InMemorySharedState

NewInMemorySharedStateWithLogger 创建带日志的共享状态,用于记录被跳过的 watcher 通知。

func (*InMemorySharedState) Get added in v1.6.0

func (s *InMemorySharedState) Get(ctx context.Context, key string) (any, bool)

func (*InMemorySharedState) Set added in v1.6.0

func (s *InMemorySharedState) Set(ctx context.Context, key string, value any) error

func (*InMemorySharedState) Snapshot added in v1.6.0

func (s *InMemorySharedState) Snapshot(ctx context.Context) map[string]any

func (*InMemorySharedState) Watch added in v1.6.0

func (s *InMemorySharedState) Watch(ctx context.Context, key string) <-chan any

type Message

type Message struct {
	ID        string
	FromID    string
	ToID      string // 空表示广播
	Type      MessageType
	Content   string
	Metadata  map[string]any
	Timestamp time.Time
	Body      types.Message // Canonical message body bridge to Layer-0 types.Message
}

Message Agent 间消息

type MessageHub

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

MessageHub 消息中心 支持持久化存储,防止消息丢失

func NewMessageHub

func NewMessageHub(logger *zap.Logger) *MessageHub

NewMessageHub 创建消息中心

func NewMessageHubWithStore

func NewMessageHubWithStore(logger *zap.Logger, store persistence.MessageStore) *MessageHub

NewMessageHubWithStore 创建带持久化的消息中心

func (*MessageHub) Close

func (h *MessageHub) Close() error

Close 关闭消息中心 使用 sync.Once 保护 channel 关闭,防止重复关闭 panic

func (*MessageHub) CreateChannel

func (h *MessageHub) CreateChannel(agentID string)

CreateChannel 创建通道

func (*MessageHub) Receive

func (h *MessageHub) Receive(agentID string, timeout time.Duration) (*Message, error)

Receive 接收消息

func (*MessageHub) RecoverMessages

func (h *MessageHub) RecoverMessages(ctx context.Context) error

RecoverMessages 恢复未处理的消息(服务重启后调用)

func (*MessageHub) Send

func (h *MessageHub) Send(msg *Message) error

Send 发送消息 如果配置了持久化存储,消息会先持久化再投递 即使 channel 满了,消息也不会丢失

func (*MessageHub) SendWithContext

func (h *MessageHub) SendWithContext(ctx context.Context, msg *Message) error

SendWithContext 发送消息(带上下文) 修复竞态条件:使用单次锁定确保操作原子性

func (*MessageHub) SetMessageStore

func (h *MessageHub) SetMessageStore(store persistence.MessageStore)

SetMessageStore 设置消息存储(用于依赖注入)

func (*MessageHub) StartRetryLoop

func (h *MessageHub) StartRetryLoop(ctx context.Context, interval time.Duration)

StartRetryLoop 启动消息重试循环

func (*MessageHub) Stats

Stats 获取消息统计信息

type MessageType

type MessageType string

MessageType 消息类型

const (
	MessageTypeProposal  MessageType = "proposal"
	MessageTypeResponse  MessageType = "response"
	MessageTypeVote      MessageType = "vote"
	MessageTypeConsensus MessageType = "consensus"
	MessageTypeBroadcast MessageType = "broadcast"
)

type MultiAgentConfig

type MultiAgentConfig struct {
	Pattern            CollaborationPattern `json:"pattern"`
	MaxRounds          int                  `json:"max_rounds"`
	ConsensusThreshold float64              `json:"consensus_threshold"`
	Timeout            time.Duration        `json:"timeout"`
	EnableVoting       bool                 `json:"enable_voting"`
	SharedState        SharedState          `json:"-"`
}

MultiAgentConfig 多 Agent 配置

func DefaultMultiAgentConfig

func DefaultMultiAgentConfig() MultiAgentConfig

DefaultMultiAgentConfig 默认配置

type MultiAgentSystem

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

MultiAgentSystem 是 legacy 多 Agent 协作 surface。新接入优先使用 agent/team 作为统一官方 facade。

func NewMultiAgentSystem

func NewMultiAgentSystem(agents []agent.Agent, config MultiAgentConfig, logger *zap.Logger) *MultiAgentSystem

NewMultiAgentSystem 创建多 Agent 系统

func (*MultiAgentSystem) Execute

func (m *MultiAgentSystem) Execute(ctx context.Context, input *agent.Input) (*agent.Output, error)

Execute 执行协作任务

type NetworkCoordinator

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

NetworkCoordinator 网络协调器

func NewNetworkCoordinator

func NewNetworkCoordinator(config MultiAgentConfig, hub *MessageHub, logger *zap.Logger) *NetworkCoordinator

func (*NetworkCoordinator) Coordinate

func (c *NetworkCoordinator) Coordinate(ctx context.Context, agents map[string]agent.Agent, input *agent.Input) (*agent.Output, error)

type PipelineConfig

type PipelineConfig struct {
	Name           string        `json:"name"`
	Description    string        `json:"description"`
	MaxConcurrency int           `json:"max_concurrency"` // 最大并行角色数
	Timeout        time.Duration `json:"timeout"`         // 整体超时
	StopOnFailure  bool          `json:"stop_on_failure"` // 失败时停止
}

PipelineConfig 流水线配置

func DefaultPipelineConfig

func DefaultPipelineConfig() PipelineConfig

DefaultPipelineConfig 返回默认流水线配置

type PipelineCoordinator

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

PipelineCoordinator 流水线协调器

func NewPipelineCoordinator

func NewPipelineCoordinator(config MultiAgentConfig, hub *MessageHub, logger *zap.Logger) *PipelineCoordinator

func (*PipelineCoordinator) Coordinate

func (c *PipelineCoordinator) Coordinate(ctx context.Context, agents map[string]agent.Agent, input *agent.Input) (*agent.Output, error)

type RetryPolicy

type RetryPolicy struct {
	MaxRetries int           `json:"max_retries"` // 最大重试次数
	Delay      time.Duration `json:"delay"`       // 重试间隔
	BackoffMul float64       `json:"backoff_mul"` // 退避乘数
}

RetryPolicy 重试策略

type RoleCapability

type RoleCapability struct {
	Name        string   `json:"name"`         // 能力名称
	Description string   `json:"description"`  // 能力描述
	Tools       []string `json:"tools"`        // 可用工具列表
	InputTypes  []string `json:"input_types"`  // 接受的输入类型
	OutputTypes []string `json:"output_types"` // 产出的输出类型
}

RoleCapability 角色能力定义

type RoleDefinition

type RoleDefinition struct {
	Type          RoleType         `json:"type"`
	Name          string           `json:"name"`
	Description   string           `json:"description"`
	SystemPrompt  string           `json:"system_prompt"` // 角色的系统提示词
	Capabilities  []RoleCapability `json:"capabilities"`
	Dependencies  []RoleType       `json:"dependencies"`   // 依赖的前置角色
	MaxConcurrent int              `json:"max_concurrent"` // 最大并发实例数
	Timeout       time.Duration    `json:"timeout"`        // 角色执行超时
	RetryPolicy   *RetryPolicy     `json:"retry_policy"`   // 重试策略
	Priority      int              `json:"priority"`       // 优先级 (越高越优先)
}

RoleDefinition 角色定义

func NewResearchCollectorRole

func NewResearchCollectorRole() *RoleDefinition

NewResearchCollectorRole 创建研究资源收集者角色

func NewResearchFilterRole

func NewResearchFilterRole() *RoleDefinition

NewResearchFilterRole 创建研究质量过滤者角色

func NewResearchGeneratorRole

func NewResearchGeneratorRole() *RoleDefinition

NewResearchGeneratorRole 创建研究想法生成者角色

func NewResearchValidatorRole

func NewResearchValidatorRole() *RoleDefinition

NewResearchValidatorRole 创建研究验证者角色

func NewResearchWriterRole

func NewResearchWriterRole() *RoleDefinition

NewResearchWriterRole 创建研究报告撰写者角色

type RoleExecuteFunc

type RoleExecuteFunc func(ctx context.Context, role *RoleDefinition, input any) (any, error)

RoleExecuteFunc 角色执行函数签名 接收角色定义和输入,返回输出

type RoleInstance

type RoleInstance struct {
	ID          string         `json:"id"`
	Definition  RoleDefinition `json:"definition"`
	AgentID     string         `json:"agent_id"` // 绑定的 Agent ID
	Status      RoleStatus     `json:"status"`
	Input       any            `json:"input,omitempty"`
	Output      any            `json:"output,omitempty"`
	Error       string         `json:"error,omitempty"`
	StartedAt   time.Time      `json:"started_at"`
	CompletedAt *time.Time     `json:"completed_at,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty"`
}

RoleInstance 角色实例(运行时状态)

type RolePipeline

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

RolePipeline 角色流水线编排器 按照依赖关系自动编排角色执行顺序

func NewRolePipeline

func NewRolePipeline(config PipelineConfig, registry *RoleRegistry, executeFn RoleExecuteFunc, logger *zap.Logger) *RolePipeline

NewRolePipeline 创建角色流水线

func (*RolePipeline) AddStage

func (p *RolePipeline) AddStage(roles ...RoleType) *RolePipeline

AddStage 添加执行阶段(阶段内角色可并行执行)

func (*RolePipeline) Execute

func (p *RolePipeline) Execute(ctx context.Context, initialInput any) (map[RoleType]any, error)

Execute 执行流水线

func (*RolePipeline) GetInstances

func (p *RolePipeline) GetInstances() []*RoleInstance

GetInstances 获取所有角色实例

func (*RolePipeline) GetTransitions

func (p *RolePipeline) GetTransitions() []RoleTransition

GetTransitions 获取所有角色转换记录

type RoleRegistry

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

RoleRegistry 角色注册表

func NewRoleRegistry

func NewRoleRegistry(logger *zap.Logger) *RoleRegistry

NewRoleRegistry 创建角色注册表

func (*RoleRegistry) Get

func (r *RoleRegistry) Get(roleType RoleType) (*RoleDefinition, bool)

Get 获取角色定义

func (*RoleRegistry) List

func (r *RoleRegistry) List() []*RoleDefinition

List 列出所有角色定义

func (*RoleRegistry) Register

func (r *RoleRegistry) Register(def *RoleDefinition) error

Register 注册角色定义

func (*RoleRegistry) Unregister

func (r *RoleRegistry) Unregister(roleType RoleType) error

Unregister 注销角色定义

type RoleStatus

type RoleStatus string

RoleStatus 角色状态

const (
	RoleStatusIdle    RoleStatus = "idle"    // 空闲
	RoleStatusActive  RoleStatus = "active"  // 活跃
	RoleStatusBlocked RoleStatus = "blocked" // 阻塞
	RoleStatusDone    RoleStatus = "done"    // 完成
	RoleStatusFailed  RoleStatus = "failed"  // 失败
)

type RoleTransition

type RoleTransition struct {
	FromRole  RoleType  `json:"from_role"`
	ToRole    RoleType  `json:"to_role"`
	Data      any       `json:"data"`
	Timestamp time.Time `json:"timestamp"`
}

RoleTransition 角色间的数据传递

type RoleType

type RoleType string

RoleType 角色类型

const (
	RoleCollector   RoleType = "collector"   // 资源收集者
	RoleFilter      RoleType = "filter"      // 质量过滤者
	RoleGenerator   RoleType = "generator"   // 想法生成者
	RoleDesigner    RoleType = "designer"    // 方案设计者
	RoleImplementer RoleType = "implementer" // 实现者
	RoleValidator   RoleType = "validator"   // 验证者
	RoleWriter      RoleType = "writer"      // 报告撰写者
	RoleCoordinator RoleType = "coordinator" // 协调者
	RoleCustom      RoleType = "custom"      // 自定义角色
)

type SharedState added in v1.6.0

type SharedState interface {
	Get(ctx context.Context, key string) (any, bool)
	Set(ctx context.Context, key string, value any) error
	Watch(ctx context.Context, key string) <-chan any
	Snapshot(ctx context.Context) map[string]any
}

Jump to

Keyboard shortcuts

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