状态键
Awaken 的状态系统提供类型化、带作用域、可持久化的键值存储。插件和工具在编译期声明状态键,运行时负责快照、持久化和并行合并语义。
StateKey trait
Section titled “StateKey trait”每个状态槽位都由一个实现了 StateKey 的类型来标识。
pub trait StateKey: 'static + Send + Sync { const KEY: &'static str; const MERGE: MergeStrategy = MergeStrategy::Exclusive; const SCOPE: KeyScope = KeyScope::Run;
type Value: Clone + Default + Serialize + DeserializeOwned + Send + Sync + 'static; type Update: Send + 'static;
fn apply(value: &mut Self::Value, update: Self::Update); fn encode(value: &Self::Value) -> Result<JsonValue, StateError>; fn decode(value: JsonValue) -> Result<Self::Value, StateError>;}struct Counter;
impl StateKey for Counter { const KEY: &'static str = "counter"; type Value = usize; type Update = usize;
fn apply(value: &mut Self::Value, update: Self::Update) { *value += update; }}KeyScope
Section titled “KeyScope”控制键值在 run 边界上的生命周期:
pub enum KeyScope { Run, Thread,}MergeStrategy
Section titled “MergeStrategy”决定并行 phase hook 或自定义并行 executor 集成下,多个 MutationBatch 如何合并:
pub enum MergeStrategy { Exclusive, Commutative,}StateMap
Section titled “StateMap”状态值的类型擦除容器。
pub struct StateMap { /* ... */ }fn contains<K: StateKey>(&self) -> boolfn get<K: StateKey>(&self) -> Option<&K::Value>fn get_mut<K: StateKey>(&mut self) -> Option<&mut K::Value>fn insert<K: StateKey>(&mut self, value: K::Value)fn remove<K: StateKey>(&mut self) -> Option<K::Value>fn get_or_insert_default<K: StateKey>(&mut self) -> &mut K::ValueSnapshot
Section titled “Snapshot”传给 hook 和 ToolCallContext 的不可变、带 revision 的状态视图:
pub struct Snapshot { pub revision: u64, pub ext: Arc<StateMap>,}fn new(revision: u64, ext: Arc<StateMap>) -> Selffn revision(&self) -> u64fn get<K: StateKey>(&self) -> Option<&K::Value>fn ext(&self) -> &StateMapStateKeyOptions
Section titled “StateKeyOptions”注册状态键时的选项:
pub struct StateKeyOptions { pub persistent: bool, pub retain_on_uninstall: bool, pub scope: KeyScope,}PersistedState
Section titled “PersistedState”存储后端使用的序列化状态格式:
pub struct PersistedState { pub revision: u64, pub extensions: HashMap<String, JsonValue>,}