awaken-server 0.4.0

Multi-protocol HTTP server with SSE, mailbox, and protocol adapters for Awaken
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Thread CRUD operations.

use std::collections::HashMap;

use awaken_contract::contract::storage::{ChildThreadDeleteStrategy, StorageError, ThreadStore};
use awaken_contract::thread::Thread;
use serde_json::Value;

/// Parameters for creating a thread.
#[derive(Debug, Clone, Default)]
pub struct CreateThreadOptions {
    pub title: Option<String>,
    pub resource_id: Option<String>,
    pub parent_thread_id: Option<String>,
}

/// Parameters for patching a thread.
#[derive(Debug, Clone, Default)]
pub struct UpdateThreadOptions {
    pub title: Option<String>,
    pub resource_id: Option<Option<String>>,
    pub parent_thread_id: Option<Option<String>>,
    pub custom: Option<HashMap<String, Value>>,
}

/// Parameters for deleting a thread.
#[derive(Debug, Clone, Copy, Default)]
pub struct DeleteThreadOptions {
    pub child_strategy: ChildThreadDeleteStrategy,
}

/// Create a new thread.
pub async fn create_thread(
    store: &dyn ThreadStore,
    title: Option<String>,
) -> Result<Thread, StorageError> {
    create_thread_with_options(
        store,
        CreateThreadOptions {
            title,
            ..CreateThreadOptions::default()
        },
    )
    .await
}

/// Create a new thread with lineage metadata.
pub async fn create_thread_with_options(
    store: &dyn ThreadStore,
    options: CreateThreadOptions,
) -> Result<Thread, StorageError> {
    let now = now_ms();
    let mut thread = Thread::new();
    if let Some(t) = options.title {
        thread.metadata.title = Some(t);
    }
    thread.resource_id = options.resource_id;
    thread.parent_thread_id = options.parent_thread_id;
    thread.normalize_lineage();
    thread.touch(now);
    store.save_thread_validated(&thread).await?;
    Ok(thread)
}

/// Get a thread by ID.
pub async fn get_thread(
    store: &dyn ThreadStore,
    thread_id: &str,
) -> Result<Option<Thread>, StorageError> {
    store.load_thread(thread_id).await
}

/// List thread IDs with pagination.
pub async fn list_threads(
    store: &dyn ThreadStore,
    offset: usize,
    limit: usize,
) -> Result<Vec<String>, StorageError> {
    store.list_threads(offset, limit).await
}

/// Update thread title.
pub async fn update_thread_title(
    store: &dyn ThreadStore,
    thread_id: &str,
    title: String,
) -> Result<Thread, StorageError> {
    update_thread(
        store,
        thread_id,
        UpdateThreadOptions {
            title: Some(title),
            ..UpdateThreadOptions::default()
        },
    )
    .await
}

/// Update thread fields and validate hierarchy changes.
pub async fn update_thread(
    store: &dyn ThreadStore,
    thread_id: &str,
    options: UpdateThreadOptions,
) -> Result<Thread, StorageError> {
    let mut thread = store
        .load_thread(thread_id)
        .await?
        .ok_or_else(|| StorageError::NotFound(thread_id.to_string()))?;

    if let Some(title) = options.title {
        thread.metadata.title = Some(title);
    }
    if let Some(resource_id) = options.resource_id {
        thread.resource_id = resource_id;
    }
    if let Some(parent_thread_id) = options.parent_thread_id {
        thread.parent_thread_id = parent_thread_id;
    }
    thread.normalize_lineage();
    if let Some(custom) = options.custom {
        for (key, value) in custom {
            thread.metadata.custom.insert(key, value);
        }
    }
    thread.touch(now_ms());
    store.save_thread_validated(&thread).await?;
    Ok(thread)
}

/// Delete a thread while managing its child threads.
pub async fn delete_thread(
    store: &dyn ThreadStore,
    thread_id: &str,
    options: DeleteThreadOptions,
) -> Result<(), StorageError> {
    store
        .delete_thread_with_strategy(thread_id, options.child_strategy)
        .await
}

use awaken_contract::now_ms;

#[cfg(test)]
mod tests {
    use super::*;
    use awaken_contract::contract::storage::ThreadStore;

    /// Simple in-memory thread store for testing.
    #[derive(Default)]
    struct MockThreadStore {
        threads: std::sync::RwLock<std::collections::HashMap<String, Thread>>,
        messages: std::sync::RwLock<
            std::collections::HashMap<String, Vec<awaken_contract::contract::message::Message>>,
        >,
    }

    #[async_trait::async_trait]
    impl ThreadStore for MockThreadStore {
        async fn load_thread(&self, thread_id: &str) -> Result<Option<Thread>, StorageError> {
            Ok(self.threads.read().unwrap().get(thread_id).cloned())
        }

        async fn save_thread(&self, thread: &Thread) -> Result<(), StorageError> {
            self.threads
                .write()
                .unwrap()
                .insert(thread.id.clone(), thread.clone());
            Ok(())
        }

        async fn delete_thread(&self, thread_id: &str) -> Result<(), StorageError> {
            self.threads.write().unwrap().remove(thread_id);
            self.messages.write().unwrap().remove(thread_id);
            Ok(())
        }

        async fn list_threads(
            &self,
            offset: usize,
            limit: usize,
        ) -> Result<Vec<String>, StorageError> {
            let guard = self.threads.read().unwrap();
            let mut ids: Vec<String> = guard.keys().cloned().collect();
            ids.sort();
            Ok(ids.into_iter().skip(offset).take(limit).collect())
        }

        async fn load_messages(
            &self,
            thread_id: &str,
        ) -> Result<Option<Vec<awaken_contract::contract::message::Message>>, StorageError>
        {
            Ok(self.messages.read().unwrap().get(thread_id).cloned())
        }

        async fn save_messages(
            &self,
            thread_id: &str,
            messages: &[awaken_contract::contract::message::Message],
        ) -> Result<(), StorageError> {
            self.messages
                .write()
                .unwrap()
                .insert(thread_id.to_owned(), messages.to_vec());
            Ok(())
        }

        async fn delete_messages(&self, thread_id: &str) -> Result<(), StorageError> {
            if !self.threads.read().unwrap().contains_key(thread_id) {
                return Err(StorageError::NotFound(thread_id.to_owned()));
            }
            self.messages.write().unwrap().remove(thread_id);
            Ok(())
        }

        async fn update_thread_metadata(
            &self,
            id: &str,
            metadata: awaken_contract::thread::ThreadMetadata,
        ) -> Result<(), StorageError> {
            let mut guard = self.threads.write().unwrap();
            let thread = guard
                .get_mut(id)
                .ok_or_else(|| StorageError::NotFound(id.to_owned()))?;
            thread.metadata = metadata;
            Ok(())
        }
    }

    #[tokio::test]
    async fn create_thread_assigns_uuid_v7_id() {
        let store = MockThreadStore::default();
        let thread = create_thread(&store, Some("Test".to_string()))
            .await
            .unwrap();
        assert_eq!(thread.id.len(), 36);
        assert_eq!(&thread.id[14..15], "7");
        assert_eq!(thread.metadata.title.as_deref(), Some("Test"));
        assert!(thread.metadata.created_at.is_some());
    }

    #[tokio::test]
    async fn create_thread_without_title() {
        let store = MockThreadStore::default();
        let thread = create_thread(&store, None).await.unwrap();
        assert!(thread.metadata.title.is_none());
    }

    #[tokio::test]
    async fn get_thread_returns_none_for_missing() {
        let store = MockThreadStore::default();
        let result = get_thread(&store, "missing").await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn get_thread_returns_existing() {
        let store = MockThreadStore::default();
        let created = create_thread(&store, Some("T".to_string())).await.unwrap();
        let loaded = get_thread(&store, &created.id).await.unwrap().unwrap();
        assert_eq!(loaded.id, created.id);
        assert_eq!(loaded.metadata.title.as_deref(), Some("T"));
    }

    #[tokio::test]
    async fn list_threads_paginated() {
        let store = MockThreadStore::default();
        for _ in 0..5 {
            create_thread(&store, None).await.unwrap();
        }
        let page1 = list_threads(&store, 0, 3).await.unwrap();
        assert_eq!(page1.len(), 3);
        let page2 = list_threads(&store, 3, 3).await.unwrap();
        assert_eq!(page2.len(), 2);
    }

    #[tokio::test]
    async fn update_thread_title_works() {
        let store = MockThreadStore::default();
        let created = create_thread(&store, Some("Old".to_string()))
            .await
            .unwrap();
        let updated = update_thread_title(&store, &created.id, "New".to_string())
            .await
            .unwrap();
        assert_eq!(updated.metadata.title.as_deref(), Some("New"));
    }

    #[tokio::test]
    async fn update_thread_title_not_found() {
        let store = MockThreadStore::default();
        let result = update_thread_title(&store, "missing", "Title".to_string()).await;
        assert!(matches!(result, Err(StorageError::NotFound(_))));
    }

    #[tokio::test]
    async fn create_thread_rejects_missing_parent_thread() {
        let store = MockThreadStore::default();

        let error = create_thread_with_options(
            &store,
            CreateThreadOptions {
                title: Some("Child".to_string()),
                resource_id: None,
                parent_thread_id: Some("missing-parent".to_string()),
            },
        )
        .await
        .expect_err("missing parent should fail");

        assert!(
            matches!(error, StorageError::Validation(message) if message == "parent thread not found: missing-parent")
        );
    }

    #[tokio::test]
    async fn create_thread_normalizes_lineage_fields() {
        let store = MockThreadStore::default();
        store.save_thread(&Thread::with_id("parent")).await.unwrap();

        let thread = create_thread_with_options(
            &store,
            CreateThreadOptions {
                title: Some("Child".to_string()),
                resource_id: Some(" resource-a ".to_string()),
                parent_thread_id: Some(" parent ".to_string()),
            },
        )
        .await
        .expect("create child");

        assert_eq!(thread.resource_id.as_deref(), Some("resource-a"));
        assert_eq!(thread.parent_thread_id.as_deref(), Some("parent"));
    }

    #[tokio::test]
    async fn update_thread_supports_detaching_parent() {
        let store = MockThreadStore::default();
        store
            .save_thread(&Thread::with_id("parent"))
            .await
            .expect("seed parent");
        let created = create_thread_with_options(
            &store,
            CreateThreadOptions {
                title: Some("Child".to_string()),
                resource_id: Some("resource-a".to_string()),
                parent_thread_id: Some("parent".to_string()),
            },
        )
        .await
        .expect("create child");

        let updated = update_thread(
            &store,
            &created.id,
            UpdateThreadOptions {
                resource_id: Some(None),
                parent_thread_id: Some(None),
                ..UpdateThreadOptions::default()
            },
        )
        .await
        .expect("detach child");

        assert_eq!(updated.resource_id, None);
        assert_eq!(updated.parent_thread_id, None);
    }

    #[tokio::test]
    async fn update_thread_normalizes_lineage_fields() {
        let store = MockThreadStore::default();
        let created = create_thread_with_options(
            &store,
            CreateThreadOptions {
                title: Some("Child".to_string()),
                resource_id: None,
                parent_thread_id: None,
            },
        )
        .await
        .expect("create child");

        let updated = update_thread(
            &store,
            &created.id,
            UpdateThreadOptions {
                resource_id: Some(Some(" resource-a ".to_string())),
                parent_thread_id: Some(Some("   ".to_string())),
                ..UpdateThreadOptions::default()
            },
        )
        .await
        .expect("normalize lineage");

        assert_eq!(updated.resource_id.as_deref(), Some("resource-a"));
        assert_eq!(updated.parent_thread_id, None);
    }

    #[tokio::test]
    async fn update_thread_rejects_cycle() {
        let store = MockThreadStore::default();
        store.save_thread(&Thread::with_id("root")).await.unwrap();
        store
            .save_thread(&Thread::with_id("child").with_parent_thread_id("root"))
            .await
            .unwrap();

        let error = update_thread(
            &store,
            "root",
            UpdateThreadOptions {
                parent_thread_id: Some(Some("child".to_string())),
                ..UpdateThreadOptions::default()
            },
        )
        .await
        .expect_err("cycle should be rejected");

        assert!(
            matches!(error, StorageError::Validation(message) if message.contains("cycle detected"))
        );
    }

    #[tokio::test]
    async fn delete_thread_default_detaches_children() {
        let store = MockThreadStore::default();
        store.save_thread(&Thread::with_id("root")).await.unwrap();
        store
            .save_thread(&Thread::with_id("child").with_parent_thread_id("root"))
            .await
            .unwrap();

        delete_thread(&store, "root", DeleteThreadOptions::default())
            .await
            .unwrap();

        assert!(store.load_thread("root").await.unwrap().is_none());
        assert_eq!(
            store
                .load_thread("child")
                .await
                .unwrap()
                .and_then(|thread| thread.parent_thread_id),
            None
        );
    }

    #[tokio::test]
    async fn delete_thread_supports_reject_strategy() {
        let store = MockThreadStore::default();
        store.save_thread(&Thread::with_id("root")).await.unwrap();
        store
            .save_thread(&Thread::with_id("child").with_parent_thread_id("root"))
            .await
            .unwrap();

        let error = delete_thread(
            &store,
            "root",
            DeleteThreadOptions {
                child_strategy: ChildThreadDeleteStrategy::Reject,
            },
        )
        .await
        .expect_err("reject strategy should fail");

        assert!(
            matches!(error, StorageError::Validation(message) if message.contains("child threads"))
        );
    }

    #[tokio::test]
    async fn delete_thread_supports_cascade_strategy() {
        let store = MockThreadStore::default();
        store.save_thread(&Thread::with_id("root")).await.unwrap();
        store
            .save_thread(&Thread::with_id("child").with_parent_thread_id("root"))
            .await
            .unwrap();
        store
            .save_thread(&Thread::with_id("grandchild").with_parent_thread_id("child"))
            .await
            .unwrap();

        delete_thread(
            &store,
            "root",
            DeleteThreadOptions {
                child_strategy: ChildThreadDeleteStrategy::Cascade,
            },
        )
        .await
        .unwrap();

        assert!(store.load_thread("root").await.unwrap().is_none());
        assert!(store.load_thread("child").await.unwrap().is_none());
        assert!(store.load_thread("grandchild").await.unwrap().is_none());
    }
}