flow

package
v1.0.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWorkflowRunning          = errors.New("workflow is already running")
	ErrWorkflowResetRunning     = errors.New("cannot reset a running workflow")
	ErrWorkflowSubmitNodeToPool = errors.New("failed to submit node to pool")
	ErrWorkflowClosed           = errors.New("workflow has been closed and cannot be used")
)
View Source
var (
	ErrLoopCancelled = errors.New("loop cancelled")
	ErrLoopTimeout   = errors.New("loop timeout")
)

Loop 相关的错误

Functions

This section is empty.

Types

type DataSlot

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

DataSlot 数据槽 作用:存储一个值,支持多协程等待、多订阅、重复读取、广播唤醒 一个值生成后,所有节点都能获取

func NewDataSlot

func NewDataSlot() *DataSlot

func (*DataSlot) Done

func (s *DataSlot) Done() <-chan struct{}

Done 返回通知 channel,值被设置时会被 close

func (*DataSlot) Get

func (s *DataSlot) Get(ctx context.Context) (any, error)

Get 等待值就绪

func (*DataSlot) IsReady

func (s *DataSlot) IsReady() bool

IsReady 检查值是否已就绪

func (*DataSlot) Set

func (s *DataSlot) Set(value any)

Set 写入值(唤醒所有等待者) 行为同 SetOnce,保持向后兼容

func (*DataSlot) SetOnce

func (s *DataSlot) SetOnce(value any)

SetOnce 首次写入值(幂等:如果已设置则忽略,不报错) 适用于:节点输出写入上下文,确保只写入一次

func (*DataSlot) SetOrUpdate

func (s *DataSlot) SetOrUpdate(value any)

SetOrUpdate 写入或更新值(始终覆盖,唤醒等待者) 适用于:ReAct 重规划时更新计划、状态变更等场景 注意:更新时会再次 Broadcast,确保新等待者能获取最新值

func (*DataSlot) TryGet

func (s *DataSlot) TryGet() (any, bool)

TryGet 非阻塞获取值 返回值和 true 表示获取成功 返回 nil 和 false 表示值尚未就绪

type FlowContext

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

func NewFlowContext

func NewFlowContext(ctx context.Context) *FlowContext

func (*FlowContext) Cancel

func (c *FlowContext) Cancel(err error)

Cancel 取消工作流并记录首个错误

func (*FlowContext) Done

func (c *FlowContext) Done() <-chan struct{}

Done 返回取消信号 channel

func (*FlowContext) Err

func (c *FlowContext) Err() error

Err 返回工作流级错误

func (*FlowContext) Get

func (c *FlowContext) Get(key string) (any, error)

Get 等待数据就绪并返回

func (*FlowContext) GetContext

func (c *FlowContext) GetContext() *context.Context

func (*FlowContext) GetError

func (c *FlowContext) GetError() error

GetError 获取已记录的首个错误(不触发取消)

func (*FlowContext) IsReady

func (c *FlowContext) IsReady(key string) bool

IsReady 检查是否已就绪

func (*FlowContext) Set

func (c *FlowContext) Set(key string, value any)

Set 放入数据(首次设置,已存在则忽略)

func (*FlowContext) SetError

func (c *FlowContext) SetError(err error) error

SetError 设置错误并触发取消

func (*FlowContext) SetOnce

func (c *FlowContext) SetOnce(key string, value any)

SetOnce 同 Set

func (*FlowContext) SetOrUpdate

func (c *FlowContext) SetOrUpdate(key string, value any)

SetOrUpdate 放入或更新数据(始终覆盖)

func (*FlowContext) TryGet

func (c *FlowContext) TryGet(key string) (any, bool)

TryGet 非阻塞获取

func (*FlowContext) Wait

func (c *FlowContext) Wait(key string) (any, error)

Wait 等待数据就绪

func (*FlowContext) WaitAll

func (c *FlowContext) WaitAll(keys ...string) (map[string]any, error)

WaitAll 等待多个数据全部就绪

func (*FlowContext) WaitForAny

func (c *FlowContext) WaitForAny(keys ...string) (string, any, error)

WaitForAny 等待多个 key 中任意一个就绪 基于 done channel + reflect.Select,零轮询

type LoopResult

type LoopResult struct {
	Status     LoopStatus `json:"status"`     // 结束状态
	Iterations int        `json:"iterations"` // 实际执行次数
	Message    string     `json:"message"`    // 详细描述
}

LoopResult 循环执行结果

func NewLoopCancelledResult

func NewLoopCancelledResult(iterations int, err error) *LoopResult

NewLoopCancelledResult 创建被取消的循环结果

func NewLoopCompletedResult

func NewLoopCompletedResult(iterations int) *LoopResult

NewLoopCompletedResult 创建正常完成的循环结果

func NewLoopMaxIterationsResult

func NewLoopMaxIterationsResult(maxIterations, actualIterations int) *LoopResult

NewLoopMaxIterationsResult 创建达到最大次数的循环结果

func NewLoopResult

func NewLoopResult(status LoopStatus, iterations int, message string) *LoopResult

NewLoopResult 创建循环结果

func NewLoopTimeoutResult

func NewLoopTimeoutResult(iterations int) *LoopResult

NewLoopTimeoutResult 创建超时的循环结果

func (*LoopResult) IsError

func (r *LoopResult) IsError() bool

IsError 判断循环是否因错误而结束

func (*LoopResult) IsSuccess

func (r *LoopResult) IsSuccess() bool

IsSuccess 判断循环是否成功完成

func (*LoopResult) ToMap

func (r *LoopResult) ToMap() map[string]any

ToMap 转换为 map,方便放入 FlowContext

type LoopStatus

type LoopStatus string

LoopStatus 循环执行状态

const (
	LoopStatusCompleted       LoopStatus = "completed"        // 正常完成
	LoopStatusMaxIterations   LoopStatus = "max_iterations"   // 达到最大循环次数
	LoopStatusTimeout         LoopStatus = "timeout"          // 超时
	LoopStatusCancelled       LoopStatus = "cancelled"        // 被取消
	LoopStatusConditionFailed LoopStatus = "condition_failed" // 条件不满足
)

func (LoopStatus) String

func (s LoopStatus) String() string

String 实现 Stringer 接口

type SafeMap

type SafeMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SafeMap 泛型并发安全 Map K: 键类型(必须可比较) V: 值类型(任意)

func (*SafeMap[K, V]) Clear

func (s *SafeMap[K, V]) Clear()

Clear 清空所有数据

func (*SafeMap[K, V]) Delete

func (s *SafeMap[K, V]) Delete(key K)

Delete 删除值

func (*SafeMap[K, V]) Exist

func (s *SafeMap[K, V]) Exist(key K) bool

Exist 判断 key 是否存在

func (*SafeMap[K, V]) Get

func (s *SafeMap[K, V]) Get(key K) (V, bool)

Get 取值

func (*SafeMap[K, V]) GetOrSet

func (s *SafeMap[K, V]) GetOrSet(key K, createFn func() V) V

GetOrSet 原子的「获取或创建」 已存在则返回已有值,不存在则调用 createFn 创建后存入

func (*SafeMap[K, V]) MustGet

func (s *SafeMap[K, V]) MustGet(key K) V

MustGet 没有就panic

func (*SafeMap[K, V]) Range

func (s *SafeMap[K, V]) Range(fn func(key K, value V) bool)

Range 遍历所有键值对

func (*SafeMap[K, V]) Set

func (s *SafeMap[K, V]) Set(key K, value V)

Set 存值

Jump to

Keyboard shortcuts

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