agentmux_srv_core/backend/wcore/
block.rs1#![allow(dead_code)]
2use uuid::Uuid;
8
9use crate::backend::storage::store::Store;
10use crate::backend::storage::StoreError;
11use crate::backend::obj::*;
12
13pub fn create_block(
15 store: &Store,
16 tab_id: &str,
17 meta: MetaMapType,
18) -> Result<Block, StoreError> {
19 let mut tab = store.must_get::<Tab>(tab_id)?;
20
21 let mut block = Block {
22 oid: Uuid::new_v4().to_string(),
23 parentoref: format!("tab:{}", tab_id),
24 meta,
25 ..Default::default()
26 };
27 store.insert(&mut block)?;
28
29 tab.blockids.push(block.oid.clone());
30 store.update(&mut tab)?;
31
32 Ok(block)
33}
34
35pub fn delete_block(
48 store: &Store,
49 tab_id: &str,
50 block_id: &str,
51) -> Result<(), StoreError> {
52 let mut tab = store.must_get::<Tab>(tab_id)?;
53 tab.blockids.retain(|id| id != block_id);
54 store.update(&mut tab)?;
55 store.delete::<Block>(block_id)?;
56 forget_block_state(block_id);
57 Ok(())
58}
59
60pub fn delete_parked_block(store: &Store, block_id: &str) -> Result<(), StoreError> {
62 store.delete::<Block>(block_id)?;
63 forget_block_state(block_id);
64 Ok(())
65}
66
67static BLOCK_GONE_HOOK: std::sync::OnceLock<fn(&str)> = std::sync::OnceLock::new();
68
69pub fn set_block_gone_hook(hook: fn(&str)) {
72 let _ = BLOCK_GONE_HOOK.set(hook);
73}
74
75fn forget_block_state(block_id: &str) {
77 if let Some(hook) = BLOCK_GONE_HOOK.get() {
78 hook(block_id);
79 }
80}
81
82pub fn resolve_block_id_from_prefix(
84 store: &Store,
85 tab_id: &str,
86 prefix: &str,
87) -> Result<String, StoreError> {
88 if prefix.len() != 8 {
89 return Err(StoreError::Other(
90 "block_id prefix must be 8 characters".to_string(),
91 ));
92 }
93 let tab = store.must_get::<Tab>(tab_id)?;
94 for block_id in &tab.blockids {
95 if block_id.starts_with(prefix) {
96 return Ok(block_id.clone());
97 }
98 }
99 Err(StoreError::NotFound)
100}