state

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package state provides type-safe state management with schema validation.

This file addresses the state serialization bottleneck problem:

  • Replaces map[string]interface{} with typed, schema-validated state
  • Provides contract enforcement for distributed systems
  • Enables efficient serialization with known schemas

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgentState

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

AgentState is a thread-safe implementation of the State interface.

It uses sync.RWMutex to ensure safe concurrent access from multiple goroutines. This is critical for Agent systems where multiple tools or middleware may access state simultaneously.

func NewAgentState

func NewAgentState() *AgentState

NewAgentState creates a new empty AgentState.

func NewAgentStateWithData

func NewAgentStateWithData(data map[string]interface{}) *AgentState

NewAgentStateWithData creates a new AgentState initialized with the given data.

func (*AgentState) Clear

func (s *AgentState) Clear()

Clear removes all values from state.

func (*AgentState) Clone

func (s *AgentState) Clone() State

Clone creates a deep copy of the state. 使用 JSON 序列化实现真正的深拷贝,确保嵌套的 map 和 slice 也被复制。

func (*AgentState) Delete

func (s *AgentState) Delete(key string)

Delete removes a value from state by key.

func (*AgentState) Get

func (s *AgentState) Get(key string) (interface{}, bool)

Get retrieves a value from state by key.

func (*AgentState) GetBool

func (s *AgentState) GetBool(key string) (bool, bool)

GetBool retrieves a bool value from state by key. Returns false and false if the key doesn't exist or value is not a bool.

func (*AgentState) GetFloat64

func (s *AgentState) GetFloat64(key string) (float64, bool)

GetFloat64 retrieves a float64 value from state by key. Returns 0.0 and false if the key doesn't exist or value is not a float64.

func (*AgentState) GetInt

func (s *AgentState) GetInt(key string) (int, bool)

GetInt retrieves an int value from state by key. Returns 0 and false if the key doesn't exist or value is not an int.

func (*AgentState) GetMap

func (s *AgentState) GetMap(key string) (map[string]interface{}, bool)

GetMap retrieves a map value from state by key. Returns nil and false if the key doesn't exist or value is not a map.

func (*AgentState) GetSlice

func (s *AgentState) GetSlice(key string) ([]interface{}, bool)

GetSlice retrieves a slice value from state by key. Returns nil and false if the key doesn't exist or value is not a slice.

func (*AgentState) GetString

func (s *AgentState) GetString(key string) (string, bool)

GetString retrieves a string value from state by key. Returns empty string and false if the key doesn't exist or value is not a string.

func (*AgentState) Keys

func (s *AgentState) Keys() []string

Keys returns all keys currently in state.

func (*AgentState) Set

func (s *AgentState) Set(key string, value interface{})

Set sets a value in state by key.

func (*AgentState) Size

func (s *AgentState) Size() int

Size returns the number of values in state.

func (*AgentState) Snapshot

func (s *AgentState) Snapshot() map[string]interface{}

Snapshot returns a copy of all state values at this moment.

func (*AgentState) String

func (s *AgentState) String() string

String returns a string representation of the state for debugging.

func (*AgentState) Update

func (s *AgentState) Update(updates map[string]interface{})

Update performs a batch update of multiple state values.

type FieldSchema

type FieldSchema struct {
	// Name 字段名称
	Name string `json:"name"`

	// Type 字段类型
	Type FieldType `json:"type"`

	// Required 是否必需
	Required bool `json:"required"`

	// Description 字段描述
	Description string `json:"description,omitempty"`

	// Default 默认值
	Default interface{} `json:"default,omitempty"`

	// ItemType 数组元素类型(仅当 Type == FieldTypeArray 时有效)
	ItemType FieldType `json:"item_type,omitempty"`

	// ObjectSchema 嵌套对象的 Schema(仅当 Type == FieldTypeObject 时有效)
	ObjectSchema *StateSchema `json:"object_schema,omitempty"`

	// Validator 自定义验证函数
	Validator func(value interface{}) error `json:"-"`
}

FieldSchema 定义单个字段的 Schema

type FieldType

type FieldType string

FieldType 定义字段的数据类型

const (
	FieldTypeString FieldType = "string"
	FieldTypeInt    FieldType = "int"
	FieldTypeFloat  FieldType = "float"
	FieldTypeBool   FieldType = "bool"
	FieldTypeObject FieldType = "object"
	FieldTypeArray  FieldType = "array"
	FieldTypeAny    FieldType = "any" // 向后兼容
	FieldTypeBytes  FieldType = "bytes"
	FieldTypeTime   FieldType = "time"
)

type SchemaRegistry

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

SchemaRegistry 管理状态 Schema 定义

func GlobalSchemaRegistry

func GlobalSchemaRegistry() *SchemaRegistry

GlobalSchemaRegistry 获取全局 Schema 注册中心

func NewSchemaRegistry

func NewSchemaRegistry() *SchemaRegistry

NewSchemaRegistry 创建 Schema 注册中心

func (*SchemaRegistry) CreateState

func (r *SchemaRegistry) CreateState(schemaName, version string) (*TypedState, error)

CreateState 使用注册的 Schema 创建状态

func (*SchemaRegistry) Get

func (r *SchemaRegistry) Get(name, version string) (*StateSchema, error)

Get 获取 Schema

func (*SchemaRegistry) Register

func (r *SchemaRegistry) Register(schema *StateSchema) error

Register 注册 Schema

type State

type State interface {
	// Get retrieves a value from state by key.
	// Returns the value and true if found, nil and false if not found.
	Get(key string) (interface{}, bool)

	// Set sets a value in state by key.
	Set(key string, value interface{})

	// Update performs a batch update of multiple state values.
	Update(updates map[string]interface{})

	// Snapshot returns a copy of all state values at this moment.
	// The returned map is a new copy and can be safely modified.
	Snapshot() map[string]interface{}

	// Clone creates a deep copy of the state.
	Clone() State

	// Delete removes a value from state by key.
	Delete(key string)

	// Clear removes all values from state.
	Clear()

	// Keys returns all keys currently in state.
	Keys() []string

	// Size returns the number of values in state.
	Size() int
}

State defines the interface for Agent state management.

Inspired by LangChain's AgentState design, providing:

  • Thread-safe state access
  • State persistence support
  • State update tracking
  • State snapshots and cloning

type StateEnvelope

type StateEnvelope struct {
	// SchemaName Schema 名称
	SchemaName string `json:"schema_name,omitempty"`

	// SchemaVersion Schema 版本
	SchemaVersion string `json:"schema_version,omitempty"`

	// Data 实际状态数据
	Data map[string]interface{} `json:"data"`

	// Checksum 数据校验和(可选)
	Checksum string `json:"checksum,omitempty"`
}

StateEnvelope 用于序列化的状态信封

包含元数据以支持版本兼容性和 Schema 验证

type StateSchema

type StateSchema struct {
	// Name Schema 名称
	Name string `json:"name"`

	// Version Schema 版本(用于兼容性检查)
	Version string `json:"version"`

	// Description Schema 描述
	Description string `json:"description,omitempty"`

	// Fields 字段定义
	Fields map[string]*FieldSchema `json:"fields"`

	// StrictMode 严格模式:不允许未定义的字段
	StrictMode bool `json:"strict_mode"`
}

StateSchema 定义状态的完整 Schema

func NewStateSchema

func NewStateSchema(name, version string) *StateSchema

NewStateSchema 创建新的 Schema

func (*StateSchema) AddField

func (s *StateSchema) AddField(field *FieldSchema) *StateSchema

AddField 添加字段定义

func (*StateSchema) Validate

func (s *StateSchema) Validate(data map[string]interface{}) error

Validate 验证数据是否符合 Schema

func (*StateSchema) WithStrictMode

func (s *StateSchema) WithStrictMode() *StateSchema

WithStrictMode 启用严格模式

type TypedState

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

TypedState 是带有 Schema 验证的类型安全状态

func NewTypedState

func NewTypedState(schema *StateSchema) *TypedState

NewTypedState 创建带有 Schema 的类型安全状态

func (*TypedState) Clone

func (ts *TypedState) Clone() *TypedState

Clone 克隆状态

func (*TypedState) Delete

func (ts *TypedState) Delete(key string) error

Delete 删除字段

func (*TypedState) FromMap

func (ts *TypedState) FromMap(data map[string]interface{}) error

FromMap 从 map 导入(带验证)

func (*TypedState) Get

func (ts *TypedState) Get(key string) (interface{}, bool)

Get 获取字段值

func (*TypedState) GetBool

func (ts *TypedState) GetBool(key string) (bool, error)

GetBool 获取布尔字段

func (*TypedState) GetFloat

func (ts *TypedState) GetFloat(key string) (float64, error)

GetFloat 获取浮点数字段

func (*TypedState) GetInt

func (ts *TypedState) GetInt(key string) (int64, error)

GetInt 获取整数字段

func (*TypedState) GetString

func (ts *TypedState) GetString(key string) (string, error)

GetString 获取字符串字段

func (*TypedState) IsDirty

func (ts *TypedState) IsDirty() bool

IsDirty 检查是否有未保存的更改

func (*TypedState) Keys

func (ts *TypedState) Keys() []string

Keys 返回所有键

func (*TypedState) MarkClean

func (ts *TypedState) MarkClean()

MarkClean 标记为已保存

func (*TypedState) MarshalJSON

func (ts *TypedState) MarshalJSON() ([]byte, error)

MarshalJSON 实现 json.Marshaler

func (*TypedState) Schema

func (ts *TypedState) Schema() *StateSchema

Schema 返回关联的 Schema

func (*TypedState) Set

func (ts *TypedState) Set(key string, value interface{}) error

Set 设置字段值(带验证)

func (*TypedState) SetBatch

func (ts *TypedState) SetBatch(values map[string]interface{}) error

SetBatch 批量设置字段值

func (*TypedState) ToMap

func (ts *TypedState) ToMap() map[string]interface{}

ToMap 导出为普通 map(用于序列化)

func (*TypedState) UnmarshalJSON

func (ts *TypedState) UnmarshalJSON(data []byte) error

UnmarshalJSON 实现 json.Unmarshaler

Jump to

Keyboard shortcuts

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