eventstream

package
v0.5.13 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package eventstream 提供 Agent 执行事件流

为 Agent 运行过程提供实时事件推送,支持:

  • 多订阅者并发接收
  • 非阻塞发布(缓冲满时丢弃)
  • 同步发布(等待所有订阅者消费)
  • 便捷的事件发射方法

使用示例:

stream := eventstream.New(eventstream.WithBufferSize(200))
ch, unsub := stream.Subscribe()
defer unsub()

stream.Emit(eventstream.EventAgentStart, "agent-1", map[string]any{"model": "gpt-4"})
event := <-ch

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// Type 事件类型
	Type EventType `json:"type"`

	// AgentID Agent 标识
	AgentID string `json:"agent_id"`

	// Timestamp 事件时间
	Timestamp time.Time `json:"timestamp"`

	// Data 事件数据
	Data map[string]any `json:"data,omitempty"`

	// TraceID 追踪 ID
	TraceID string `json:"trace_id,omitempty"`

	// SpanID 跨度 ID
	SpanID string `json:"span_id,omitempty"`
}

Event Agent 执行事件

type EventType

type EventType string

EventType 事件类型

const (
	// EventAgentStart Agent 开始执行
	EventAgentStart EventType = "agent.start"

	// EventAgentEnd Agent 执行完成
	EventAgentEnd EventType = "agent.end"

	// EventAgentError Agent 执行出错
	EventAgentError EventType = "agent.error"

	// EventToolCall 工具调用开始
	EventToolCall EventType = "tool.call"

	// EventToolResult 工具调用结果
	EventToolResult EventType = "tool.result"

	// EventLLMRequest LLM 请求发出
	EventLLMRequest EventType = "llm.request"

	// EventLLMResponse LLM 响应接收
	EventLLMResponse EventType = "llm.response"

	// EventStateChange 状态变更
	EventStateChange EventType = "state.change"

	// EventCheckpoint 检查点保存
	EventCheckpoint EventType = "checkpoint"

	// EventMessage 消息事件
	EventMessage EventType = "message"
)

type Option

type Option func(*Stream)

Option 配置选项

func WithBufferSize

func WithBufferSize(size int) Option

WithBufferSize 设置订阅者通道缓冲大小

默认 100。缓冲满时非阻塞发布会丢弃事件。

type Stream

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

Stream Agent 事件流

支持多个订阅者同时接收事件。 线程安全。

func New

func New(opts ...Option) *Stream

New 创建事件流

func (*Stream) BridgeTo

func (s *Stream) BridgeTo(bus *event.Bus) func()

BridgeTo 将事件流桥接到 toolkit 事件总线

所有通过 Stream 发布的事件会同时发送到指定的 toolkit event.Bus。 事件类型映射为 string(EventType),Payload 为 Event 结构体。 返回取消桥接的函数。

func (*Stream) Close

func (s *Stream) Close()

Close 关闭事件流,关闭所有订阅者通道

func (*Stream) Emit

func (s *Stream) Emit(eventType EventType, agentID string, data map[string]any)

Emit 便捷方法:创建事件并发布

func (*Stream) EmitWithTrace

func (s *Stream) EmitWithTrace(eventType EventType, agentID, traceID, spanID string, data map[string]any)

EmitWithTrace 带追踪信息的便捷发布

func (*Stream) Publish

func (s *Stream) Publish(event Event)

Publish 非阻塞发布事件

向所有订阅者发送事件。若某个订阅者的缓冲区满,该订阅者会丢失此事件。 若订阅者 channel 被外部意外关闭,会安全地移除该订阅者而非 panic。

func (*Stream) PublishSync

func (s *Stream) PublishSync(ctx context.Context, event Event) error

PublishSync 同步发布事件(阻塞直到所有订阅者消费或 ctx 取消)

func (*Stream) Subscribe

func (s *Stream) Subscribe() (<-chan Event, func())

Subscribe 订阅事件流

返回事件接收通道和取消订阅函数。 调用方必须在不再需要时调用 unsubscribe。

func (*Stream) SubscriberCount

func (s *Stream) SubscriberCount() int

SubscriberCount 返回当前订阅者数量

Jump to

Keyboard shortcuts

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