Skip to main content

agentic_reality/engine/write/
resource.rs

1//! Resource write operations.
2
3use crate::engine::write::WriteEngine;
4use crate::types::error::{RealityError, RealityResult};
5use crate::types::resource::*;
6
7impl<'a> WriteEngine<'a> {
8    /// Sense resources — set the full resource body.
9    pub fn sense_resources(&mut self, body: ResourceBody) -> RealityResult<()> {
10        self.engine.resource_store.body = Some(body);
11        self.engine.mark_dirty();
12        Ok(())
13    }
14
15    /// Update mind (memory) capacity.
16    pub fn update_mind(&mut self, mind: MindCapacity) -> RealityResult<()> {
17        let body = self
18            .engine
19            .resource_store
20            .body
21            .as_mut()
22            .ok_or_else(|| RealityError::NotInitialized("resource body".into()))?;
23        body.mind = mind;
24        self.engine.mark_dirty();
25        Ok(())
26    }
27
28    /// Update processing energy (CPU).
29    pub fn update_energy(&mut self, energy: ProcessingEnergy) -> RealityResult<()> {
30        let body = self
31            .engine
32            .resource_store
33            .body
34            .as_mut()
35            .ok_or_else(|| RealityError::NotInitialized("resource body".into()))?;
36        body.energy = energy;
37        self.engine.mark_dirty();
38        Ok(())
39    }
40
41    /// Update network reach.
42    pub fn update_reach(&mut self, reach: NetworkReach) -> RealityResult<()> {
43        let body = self
44            .engine
45            .resource_store
46            .body
47            .as_mut()
48            .ok_or_else(|| RealityError::NotInitialized("resource body".into()))?;
49        body.reach = reach;
50        self.engine.mark_dirty();
51        Ok(())
52    }
53
54    /// Update storage capacity.
55    pub fn update_storage(&mut self, storage: StorageCapacity) -> RealityResult<()> {
56        let body = self
57            .engine
58            .resource_store
59            .body
60            .as_mut()
61            .ok_or_else(|| RealityError::NotInitialized("resource body".into()))?;
62        body.storage = storage;
63        self.engine.mark_dirty();
64        Ok(())
65    }
66
67    /// Update GPU capacity.
68    pub fn update_visual(&mut self, visual: Option<GpuCapacity>) -> RealityResult<()> {
69        let body = self
70            .engine
71            .resource_store
72            .body
73            .as_mut()
74            .ok_or_else(|| RealityError::NotInitialized("resource body".into()))?;
75        body.visual = visual;
76        self.engine.mark_dirty();
77        Ok(())
78    }
79
80    /// Add a resource sensation.
81    pub fn add_sensation(&mut self, sensation: ResourceSensation) -> RealityResult<()> {
82        let body = self
83            .engine
84            .resource_store
85            .body
86            .as_mut()
87            .ok_or_else(|| RealityError::NotInitialized("resource body".into()))?;
88        body.sensations.push(sensation);
89        self.engine.mark_dirty();
90        Ok(())
91    }
92
93    /// Clear sensations for a resource type.
94    pub fn clear_sensation(&mut self, resource: ResourceType) -> RealityResult<()> {
95        let body = self
96            .engine
97            .resource_store
98            .body
99            .as_mut()
100            .ok_or_else(|| RealityError::NotInitialized("resource body".into()))?;
101        body.sensations.retain(|s| s.resource != resource);
102        self.engine.mark_dirty();
103        Ok(())
104    }
105
106    /// Update the resource pressure gradient.
107    pub fn update_pressure_gradient(
108        &mut self,
109        gradient: ResourcePressureGradient,
110    ) -> RealityResult<()> {
111        self.engine.resource_store.pressure_gradient = Some(gradient);
112        self.engine.mark_dirty();
113        Ok(())
114    }
115
116    /// Discover a new capability.
117    pub fn discover_capability(&mut self, capability: Capability) -> RealityResult<()> {
118        let map = self
119            .engine
120            .resource_store
121            .capabilities
122            .get_or_insert_with(|| CapabilityMap {
123                capabilities: vec![],
124                discovered_at: crate::types::now_micros() as i64,
125                stale_after_secs: 3600,
126            });
127        map.capabilities.push(capability);
128        self.engine.mark_dirty();
129        Ok(())
130    }
131
132    /// Lose a capability by name.
133    pub fn lose_capability(&mut self, name: &str) -> RealityResult<()> {
134        if let Some(map) = &mut self.engine.resource_store.capabilities {
135            map.capabilities.retain(|c| c.name != name);
136            self.engine.mark_dirty();
137        }
138        Ok(())
139    }
140
141    /// Update cost consciousness.
142    pub fn update_cost(&mut self, cost: CostConsciousness) -> RealityResult<()> {
143        self.engine.resource_store.cost = Some(cost);
144        self.engine.mark_dirty();
145        Ok(())
146    }
147
148    /// Update capacity planning intuition.
149    pub fn update_capacity_intuition(&mut self, intuition: CapacityIntuition) -> RealityResult<()> {
150        self.engine.resource_store.capacity_intuition = Some(intuition);
151        self.engine.mark_dirty();
152        Ok(())
153    }
154
155    /// Set budget constraints.
156    pub fn set_budget(&mut self, budget: BudgetConstraints) -> RealityResult<()> {
157        let cost = self
158            .engine
159            .resource_store
160            .cost
161            .as_mut()
162            .ok_or_else(|| RealityError::NotInitialized("cost consciousness".into()))?;
163        cost.budget = Some(budget);
164        self.engine.mark_dirty();
165        Ok(())
166    }
167}