oneiros-model 0.0.8

Shared domain types for oneiros
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
mod actor;
mod agent;
mod brain;
mod cognition;
mod connection;
mod event_ops;
mod experience;
mod failures;
mod level;
mod lifecycle;
mod memory;
mod nature;
mod persona;
mod pressure;
mod search_ops;
mod sensation;
mod serde_stability_tests;
mod storage;
mod tenant;
mod texture;
mod ticket;
mod urge;

use serde::{Deserialize, Serialize};

use crate::*;

pub use actor::*;
pub use agent::*;
pub use brain::*;
pub use cognition::*;
pub use connection::*;
pub use event_ops::*;
pub use experience::*;
pub use failures::*;
pub use level::*;
pub use lifecycle::*;
pub use memory::*;
pub use nature::*;
pub use persona::*;
pub use pressure::*;
pub use search_ops::*;
pub use sensation::*;
pub use storage::*;
pub use tenant::*;
pub use texture::*;
pub use ticket::*;
pub use urge::*;

#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(untagged)]
pub enum Events {
    Actor(ActorEvents),
    Agent(AgentEvents),
    Brain(BrainEvents),
    Cognition(CognitionEvents),
    Connection(ConnectionEvents),
    Dreaming(DreamingEvents),
    Experience(ExperienceEvents),
    Introspecting(IntrospectingEvents),
    Level(LevelEvents),
    Lifecycle(LifecycleEvents),
    Memory(MemoryEvents),
    Nature(NatureEvents),
    Persona(PersonaEvents),
    Reflecting(ReflectingEvents),
    Sensation(SensationEvents),
    Sense(SenseEvents),
    Storage(StorageEvents),
    Tenant(TenantEvents),
    Texture(TextureEvents),
    Ticket(TicketEvents),
    Urge(UrgeEvents),
}

/// Super-enum over all request types. Serde untagged — each inner enum
/// carries its own `{type, data}` tag.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(untagged)]
pub enum Requests {
    Actor(ActorRequests),
    Agent(AgentRequests),
    Brain(BrainRequests),
    Cognition(CognitionRequests),
    Connection(ConnectionRequests),
    Dreaming(DreamingRequests),
    Event(EventRequests),
    Experience(ExperienceRequests),
    Introspecting(IntrospectingRequests),
    Level(LevelRequests),
    Lifecycle(LifecycleRequests),
    Memory(MemoryRequests),
    Nature(NatureRequests),
    Persona(PersonaRequests),
    Pressure(PressureRequests),
    Reflecting(ReflectingRequests),
    Search(SearchRequests),
    Sensation(SensationRequests),
    Sense(SenseRequests),
    Storage(StorageRequests),
    Tenant(TenantRequests),
    Texture(TextureRequests),
    Ticket(TicketRequests),
    Urge(UrgeRequests),
}

impl From<ActorRequests> for Requests {
    fn from(r: ActorRequests) -> Self {
        Self::Actor(r)
    }
}
impl From<AgentRequests> for Requests {
    fn from(r: AgentRequests) -> Self {
        Self::Agent(r)
    }
}
impl From<BrainRequests> for Requests {
    fn from(r: BrainRequests) -> Self {
        Self::Brain(r)
    }
}
impl From<CognitionRequests> for Requests {
    fn from(r: CognitionRequests) -> Self {
        Self::Cognition(r)
    }
}
impl From<ConnectionRequests> for Requests {
    fn from(r: ConnectionRequests) -> Self {
        Self::Connection(r)
    }
}
impl From<DreamingRequests> for Requests {
    fn from(r: DreamingRequests) -> Self {
        Self::Dreaming(r)
    }
}
impl From<EventRequests> for Requests {
    fn from(r: EventRequests) -> Self {
        Self::Event(r)
    }
}
impl From<ExperienceRequests> for Requests {
    fn from(r: ExperienceRequests) -> Self {
        Self::Experience(r)
    }
}
impl From<IntrospectingRequests> for Requests {
    fn from(r: IntrospectingRequests) -> Self {
        Self::Introspecting(r)
    }
}
impl From<LevelRequests> for Requests {
    fn from(r: LevelRequests) -> Self {
        Self::Level(r)
    }
}
impl From<LifecycleRequests> for Requests {
    fn from(r: LifecycleRequests) -> Self {
        Self::Lifecycle(r)
    }
}
impl From<MemoryRequests> for Requests {
    fn from(r: MemoryRequests) -> Self {
        Self::Memory(r)
    }
}
impl From<NatureRequests> for Requests {
    fn from(r: NatureRequests) -> Self {
        Self::Nature(r)
    }
}
impl From<PersonaRequests> for Requests {
    fn from(r: PersonaRequests) -> Self {
        Self::Persona(r)
    }
}
impl From<PressureRequests> for Requests {
    fn from(r: PressureRequests) -> Self {
        Self::Pressure(r)
    }
}
impl From<ReflectingRequests> for Requests {
    fn from(r: ReflectingRequests) -> Self {
        Self::Reflecting(r)
    }
}
impl From<SearchRequests> for Requests {
    fn from(r: SearchRequests) -> Self {
        Self::Search(r)
    }
}
impl From<SensationRequests> for Requests {
    fn from(r: SensationRequests) -> Self {
        Self::Sensation(r)
    }
}
impl From<SenseRequests> for Requests {
    fn from(r: SenseRequests) -> Self {
        Self::Sense(r)
    }
}
impl From<StorageRequests> for Requests {
    fn from(r: StorageRequests) -> Self {
        Self::Storage(r)
    }
}
impl From<TenantRequests> for Requests {
    fn from(r: TenantRequests) -> Self {
        Self::Tenant(r)
    }
}
impl From<TextureRequests> for Requests {
    fn from(r: TextureRequests) -> Self {
        Self::Texture(r)
    }
}
impl From<TicketRequests> for Requests {
    fn from(r: TicketRequests) -> Self {
        Self::Ticket(r)
    }
}
impl From<UrgeRequests> for Requests {
    fn from(r: UrgeRequests) -> Self {
        Self::Urge(r)
    }
}

/// Super-enum over all response types. Serde untagged — each inner enum
/// carries its own `{type, data}` tag.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(untagged)]
pub enum Responses {
    Actor(ActorResponses),
    Agent(AgentResponses),
    Brain(BrainResponses),
    Cognition(CognitionResponses),
    Connection(ConnectionResponses),
    Dreaming(DreamingResponses),
    Event(EventResponses),
    Experience(ExperienceResponses),
    Introspecting(IntrospectingResponses),
    Level(LevelResponses),
    Lifecycle(LifecycleResponses),
    Memory(MemoryResponses),
    Nature(NatureResponses),
    Persona(PersonaResponses),
    Pressure(PressureResponses),
    Reflecting(ReflectingResponses),
    Search(SearchResponses),
    Sensation(SensationResponses),
    Sense(SenseResponses),
    Storage(StorageResponses),
    Tenant(TenantResponses),
    Texture(TextureResponses),
    Ticket(TicketResponses),
    Urge(UrgeResponses),
}

impl From<ActorResponses> for Responses {
    fn from(r: ActorResponses) -> Self {
        Self::Actor(r)
    }
}
impl From<AgentResponses> for Responses {
    fn from(r: AgentResponses) -> Self {
        Self::Agent(r)
    }
}
impl From<BrainResponses> for Responses {
    fn from(r: BrainResponses) -> Self {
        Self::Brain(r)
    }
}
impl From<CognitionResponses> for Responses {
    fn from(r: CognitionResponses) -> Self {
        Self::Cognition(r)
    }
}
impl From<ConnectionResponses> for Responses {
    fn from(r: ConnectionResponses) -> Self {
        Self::Connection(r)
    }
}
impl From<DreamingResponses> for Responses {
    fn from(r: DreamingResponses) -> Self {
        Self::Dreaming(r)
    }
}
impl From<EventResponses> for Responses {
    fn from(r: EventResponses) -> Self {
        Self::Event(r)
    }
}
impl From<ExperienceResponses> for Responses {
    fn from(r: ExperienceResponses) -> Self {
        Self::Experience(r)
    }
}
impl From<IntrospectingResponses> for Responses {
    fn from(r: IntrospectingResponses) -> Self {
        Self::Introspecting(r)
    }
}
impl From<LevelResponses> for Responses {
    fn from(r: LevelResponses) -> Self {
        Self::Level(r)
    }
}
impl From<LifecycleResponses> for Responses {
    fn from(r: LifecycleResponses) -> Self {
        Self::Lifecycle(r)
    }
}
impl From<MemoryResponses> for Responses {
    fn from(r: MemoryResponses) -> Self {
        Self::Memory(r)
    }
}
impl From<NatureResponses> for Responses {
    fn from(r: NatureResponses) -> Self {
        Self::Nature(r)
    }
}
impl From<PersonaResponses> for Responses {
    fn from(r: PersonaResponses) -> Self {
        Self::Persona(r)
    }
}
impl From<PressureResponses> for Responses {
    fn from(r: PressureResponses) -> Self {
        Self::Pressure(r)
    }
}
impl From<ReflectingResponses> for Responses {
    fn from(r: ReflectingResponses) -> Self {
        Self::Reflecting(r)
    }
}
impl From<SearchResponses> for Responses {
    fn from(r: SearchResponses) -> Self {
        Self::Search(r)
    }
}
impl From<SensationResponses> for Responses {
    fn from(r: SensationResponses) -> Self {
        Self::Sensation(r)
    }
}
impl From<SenseResponses> for Responses {
    fn from(r: SenseResponses) -> Self {
        Self::Sense(r)
    }
}
impl From<StorageResponses> for Responses {
    fn from(r: StorageResponses) -> Self {
        Self::Storage(r)
    }
}
impl From<TenantResponses> for Responses {
    fn from(r: TenantResponses) -> Self {
        Self::Tenant(r)
    }
}
impl From<TextureResponses> for Responses {
    fn from(r: TextureResponses) -> Self {
        Self::Texture(r)
    }
}
impl From<TicketResponses> for Responses {
    fn from(r: TicketResponses) -> Self {
        Self::Ticket(r)
    }
}
impl From<UrgeResponses> for Responses {
    fn from(r: UrgeResponses) -> Self {
        Self::Urge(r)
    }
}

// ── Response envelope ─────────────────────────────────────────────

/// Ambient metadata carried alongside every response.
/// Extensible for future cross-cutting concerns.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct ResponseMeta {
    pub pressure: Vec<PressureReading>,
}

/// Unified protocol response. Every transport returns this shape.
///
/// Flattening `Responses` produces `{ type, data, meta }` — the domain
/// response's tag and content merge with the optional meta field.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Response {
    #[serde(flatten)]
    pub data: Responses,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub meta: Option<ResponseMeta>,
}

impl Response {
    pub fn new(data: impl Into<Responses>) -> Self {
        Self {
            data: data.into(),
            meta: None,
        }
    }

    pub fn with_meta(mut self, meta: ResponseMeta) -> Self {
        self.meta = Some(meta);
        self
    }

    /// Extract the inner domain data, consuming the response.
    ///
    /// Serializes `Responses` to a JSON value, extracts the `data` field
    /// from the adjacently-tagged inner enum, and deserializes as `T`.
    ///
    /// ```ignore
    /// let agent: Agent = response.data()?;
    /// ```
    pub fn data<T: serde::de::DeserializeOwned>(self) -> Result<T, serde_json::Error> {
        let value = serde_json::to_value(self.data)?;
        if let Some(inner) = value.get("data") {
            serde_json::from_value(inner.clone())
        } else {
            serde_json::from_value(value)
        }
    }

    /// Get pressure readings from the response meta, if present.
    pub fn pressure_readings(&self) -> Vec<PressureReading> {
        self.meta
            .as_ref()
            .map(|m| m.pressure.clone())
            .unwrap_or_default()
    }
}

// ── Agent scope extraction ────────────────────────────────────────

impl Requests {
    /// Returns the agent scope for this request, if one is specified.
    ///
    /// Used by Response assembly to determine which agent's pressure
    /// readings to include in ResponseMeta.
    pub fn agent_scope(&self) -> Option<&AgentName> {
        match self {
            // Agent-scoped: required agent field
            Self::Agent(AgentRequests::CreateAgent(r)) => Some(&r.name),
            Self::Agent(AgentRequests::UpdateAgent(r)) => Some(&r.name),
            Self::Agent(AgentRequests::GetAgent(r)) => Some(&r.name),
            Self::Agent(AgentRequests::RemoveAgent(r)) => Some(&r.name),
            Self::Cognition(CognitionRequests::AddCognition(r)) => Some(&r.agent),
            Self::Memory(MemoryRequests::AddMemory(r)) => Some(&r.agent),
            Self::Experience(ExperienceRequests::CreateExperience(r)) => Some(&r.agent),
            Self::Lifecycle(LifecycleRequests::Wake(r)) => Some(&r.agent),
            Self::Lifecycle(LifecycleRequests::Sleep(r)) => Some(&r.agent),
            Self::Lifecycle(LifecycleRequests::Emerge(r)) => Some(&r.name),
            Self::Lifecycle(LifecycleRequests::Recede(r)) => Some(&r.agent),
            Self::Dreaming(DreamingRequests::Dream(r)) => Some(&r.agent),
            Self::Introspecting(IntrospectingRequests::Introspect(r)) => Some(&r.agent),
            Self::Reflecting(ReflectingRequests::Reflect(r)) => Some(&r.agent),
            Self::Sense(SenseRequests::Sense(r)) => Some(&r.agent),
            Self::Pressure(PressureRequests::GetPressure(r)) => Some(&r.agent),

            // Optionally scoped: use filter if provided
            Self::Cognition(CognitionRequests::ListCognitions(r)) => r.agent.as_ref(),
            Self::Memory(MemoryRequests::ListMemories(r)) => r.agent.as_ref(),
            Self::Experience(ExperienceRequests::ListExperiences(r)) => r.agent.as_ref(),
            Self::Search(SearchRequests::Search(r)) => r.agent.as_ref(),

            // Everything else: brain-scoped, no agent context
            _ => None,
        }
    }
}