spool-memory 0.1.1

Local-first developer memory system — persistent, structured knowledge for AI coding tools
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use crate::desktop::{
    DesktopContextResponse, DesktopDaemonRequest, DesktopErrorEnvelope, DesktopHistoryResponse,
    DesktopImportSessionRequest, DesktopImportSessionResponse, DesktopLifecycleActionDto,
    DesktopMemoryActionRequest, DesktopMemoryDraftRequest, DesktopMetadataDto,
    DesktopPromptOptimizeRequest, DesktopPromptOptimizeResponse, DesktopRecordLookupRequest,
    DesktopRecordResponse, DesktopRouteRequest, DesktopService, DesktopSessionActionRequest,
    DesktopSessionActionResponse, DesktopSessionBrowserRequest, DesktopSessionBrowserResponse,
    DesktopSessionDetailRequest, DesktopSessionDetailResponse, DesktopStatusRequest,
    DesktopStatusResponse, DesktopWakeupRequest, DesktopWakeupResponse, DesktopWikiIndexRequest,
    DesktopWikiIndexResponse, DesktopWikiLintRequest, DesktopWikiLintResponse,
    DesktopWorkbenchRequest, DesktopWorkbenchResponse, DesktopWriteResponse,
};
use crate::domain::{MemoryScope, OutputFormat, TargetTool, WakeupProfile};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

pub type TauriCommandResult<T> = Result<T, DesktopErrorEnvelope>;

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriContextCommand {
    pub config_path: PathBuf,
    pub vault_root_override: Option<PathBuf>,
    pub cwd: PathBuf,
    pub task: String,
    #[serde(default)]
    pub files: Vec<String>,
    pub target: TargetTool,
    pub format: OutputFormat,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriWakeupCommand {
    pub config_path: PathBuf,
    pub vault_root_override: Option<PathBuf>,
    pub cwd: PathBuf,
    pub task: String,
    #[serde(default)]
    pub files: Vec<String>,
    pub target: TargetTool,
    pub profile: WakeupProfile,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriPromptOptimizeCommand {
    pub config_path: PathBuf,
    pub vault_root_override: Option<PathBuf>,
    pub cwd: PathBuf,
    pub task: String,
    #[serde(default)]
    pub files: Vec<String>,
    pub target: TargetTool,
    pub profile: WakeupProfile,
    pub provider: Option<String>,
    pub session_id: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriDaemonCommand {
    pub enabled: bool,
    pub daemon_bin: Option<PathBuf>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriWorkbenchCommand {
    pub config_path: PathBuf,
    pub daemon: Option<TauriDaemonCommand>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriRecordCommand {
    pub config_path: PathBuf,
    pub record_id: String,
    pub daemon: Option<TauriDaemonCommand>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriSessionBrowserCommand {
    pub config_path: PathBuf,
    pub page: Option<usize>,
    pub per_page: Option<usize>,
    pub provider: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriSessionDetailCommand {
    pub config_path: PathBuf,
    pub session_id: String,
    pub message_offset: Option<usize>,
    pub message_limit: Option<usize>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriSessionActionCommand {
    pub config_path: PathBuf,
    pub session_id: String,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriStatusCommand {
    pub config_path: PathBuf,
    pub vault_root_override: Option<PathBuf>,
    pub cwd: PathBuf,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriImportSessionCommand {
    pub config_path: PathBuf,
    pub provider: String,
    pub session_id: String,
    #[serde(default)]
    pub apply: bool,
    #[serde(default)]
    pub actor: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriMemoryDraftCommand {
    pub config_path: PathBuf,
    pub title: String,
    pub summary: String,
    pub memory_type: String,
    pub scope: MemoryScope,
    pub source_ref: String,
    pub project_id: Option<String>,
    pub user_id: Option<String>,
    pub sensitivity: Option<String>,
    #[serde(default)]
    pub metadata: DesktopMetadataDto,
    // Structured retrieval signals
    #[serde(default)]
    pub entities: Vec<String>,
    #[serde(default)]
    pub tags: Vec<String>,
    #[serde(default)]
    pub triggers: Vec<String>,
    #[serde(default)]
    pub related_files: Vec<String>,
    #[serde(default)]
    pub related_records: Vec<String>,
    #[serde(default)]
    pub supersedes: Option<String>,
    #[serde(default)]
    pub applies_to: Vec<String>,
    #[serde(default)]
    pub valid_until: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct TauriMemoryActionCommand {
    pub config_path: PathBuf,
    pub record_id: String,
    pub action: DesktopLifecycleActionDto,
    #[serde(default)]
    pub metadata: DesktopMetadataDto,
}

pub fn desktop_run_context(
    command: TauriContextCommand,
) -> TauriCommandResult<DesktopContextResponse> {
    DesktopService::new().run_context(command.into_request())
}

pub fn desktop_build_wakeup(
    command: TauriWakeupCommand,
) -> TauriCommandResult<DesktopWakeupResponse> {
    DesktopService::new().build_wakeup(command.into_request())
}

pub fn desktop_optimize_prompt(
    command: TauriPromptOptimizeCommand,
) -> TauriCommandResult<DesktopPromptOptimizeResponse> {
    DesktopService::new().optimize_prompt(command.into_request())
}

pub fn desktop_load_workbench(
    command: TauriWorkbenchCommand,
) -> TauriCommandResult<DesktopWorkbenchResponse> {
    DesktopService::new().load_workbench(command.into_request())
}

pub fn desktop_get_record(
    command: TauriRecordCommand,
) -> TauriCommandResult<Option<DesktopRecordResponse>> {
    DesktopService::new().get_record(command.into_request())
}

pub fn desktop_get_history(
    command: TauriRecordCommand,
) -> TauriCommandResult<DesktopHistoryResponse> {
    DesktopService::new().get_history(command.into_request())
}

pub fn desktop_browse_sessions(
    command: TauriSessionBrowserCommand,
) -> TauriCommandResult<DesktopSessionBrowserResponse> {
    DesktopService::new().browse_sessions(command.into_request())
}

pub fn desktop_get_session(
    command: TauriSessionDetailCommand,
) -> TauriCommandResult<Option<DesktopSessionDetailResponse>> {
    DesktopService::new().get_session(command.into_request())
}

pub fn desktop_collect_status(
    command: TauriStatusCommand,
) -> TauriCommandResult<DesktopStatusResponse> {
    DesktopService::new().collect_status(command.into_request())
}

pub fn desktop_continue_session(
    command: TauriSessionActionCommand,
) -> TauriCommandResult<DesktopSessionActionResponse> {
    DesktopService::new().continue_session(command.into_request())
}

pub fn desktop_delete_session(
    command: TauriSessionActionCommand,
) -> TauriCommandResult<DesktopSessionActionResponse> {
    DesktopService::new().delete_session(command.into_request())
}

pub fn desktop_record_manual(
    command: TauriMemoryDraftCommand,
) -> TauriCommandResult<DesktopWriteResponse> {
    DesktopService::new().record_manual(command.into_request())
}

pub fn desktop_propose_memory(
    command: TauriMemoryDraftCommand,
) -> TauriCommandResult<DesktopWriteResponse> {
    DesktopService::new().propose_memory(command.into_request())
}

pub fn desktop_apply_memory_action(
    command: TauriMemoryActionCommand,
) -> TauriCommandResult<DesktopWriteResponse> {
    DesktopService::new().apply_memory_action(command.into_request())
}

pub fn desktop_import_session(
    command: TauriImportSessionCommand,
) -> TauriCommandResult<DesktopImportSessionResponse> {
    DesktopService::new().import_session(command.into_request())
}

pub fn desktop_wiki_lint(
    request: DesktopWikiLintRequest,
) -> TauriCommandResult<DesktopWikiLintResponse> {
    DesktopService::new().wiki_lint(request)
}

pub fn desktop_read_wiki_index(
    request: DesktopWikiIndexRequest,
) -> TauriCommandResult<DesktopWikiIndexResponse> {
    DesktopService::new().read_wiki_index(request)
}

impl TauriContextCommand {
    fn into_request(self) -> DesktopRouteRequest {
        DesktopRouteRequest {
            config_path: self.config_path,
            vault_root_override: self.vault_root_override,
            cwd: self.cwd,
            task: self.task,
            files: self.files,
            target: self.target,
            format: self.format,
        }
    }
}

impl TauriWakeupCommand {
    fn into_request(self) -> DesktopWakeupRequest {
        DesktopWakeupRequest {
            config_path: self.config_path,
            vault_root_override: self.vault_root_override,
            cwd: self.cwd,
            task: self.task,
            files: self.files,
            target: self.target,
            profile: self.profile,
        }
    }
}

impl TauriPromptOptimizeCommand {
    fn into_request(self) -> DesktopPromptOptimizeRequest {
        DesktopPromptOptimizeRequest {
            config_path: self.config_path,
            vault_root_override: self.vault_root_override,
            cwd: self.cwd,
            task: self.task,
            files: self.files,
            target: self.target,
            profile: self.profile,
            provider: self.provider,
            session_id: self.session_id,
        }
    }
}

impl TauriWorkbenchCommand {
    fn into_request(self) -> DesktopWorkbenchRequest {
        DesktopWorkbenchRequest {
            config_path: self.config_path,
            daemon: self.daemon.map(TauriDaemonCommand::into_request),
        }
    }
}

impl TauriRecordCommand {
    fn into_request(self) -> DesktopRecordLookupRequest {
        DesktopRecordLookupRequest {
            config_path: self.config_path,
            record_id: self.record_id,
            daemon: self.daemon.map(TauriDaemonCommand::into_request),
        }
    }
}

impl TauriDaemonCommand {
    fn into_request(self) -> DesktopDaemonRequest {
        DesktopDaemonRequest {
            enabled: self.enabled,
            daemon_bin: self.daemon_bin,
        }
    }
}

impl TauriSessionBrowserCommand {
    fn into_request(self) -> DesktopSessionBrowserRequest {
        DesktopSessionBrowserRequest {
            config_path: self.config_path,
            page: self.page.unwrap_or(1),
            per_page: self.per_page.unwrap_or(10),
            provider: self.provider,
        }
    }
}

impl TauriSessionDetailCommand {
    fn into_request(self) -> DesktopSessionDetailRequest {
        DesktopSessionDetailRequest {
            config_path: self.config_path,
            session_id: self.session_id,
            message_offset: self.message_offset,
            message_limit: self.message_limit,
        }
    }
}

impl TauriStatusCommand {
    fn into_request(self) -> DesktopStatusRequest {
        DesktopStatusRequest {
            config_path: self.config_path,
            vault_root_override: self.vault_root_override,
            cwd: self.cwd,
        }
    }
}

impl TauriSessionActionCommand {
    fn into_request(self) -> DesktopSessionActionRequest {
        DesktopSessionActionRequest {
            config_path: self.config_path,
            session_id: self.session_id,
        }
    }
}

impl TauriMemoryDraftCommand {
    fn into_request(self) -> DesktopMemoryDraftRequest {
        DesktopMemoryDraftRequest {
            config_path: self.config_path,
            title: self.title,
            summary: self.summary,
            memory_type: self.memory_type,
            scope: self.scope,
            source_ref: self.source_ref,
            project_id: self.project_id,
            user_id: self.user_id,
            sensitivity: self.sensitivity,
            metadata: self.metadata,
            entities: self.entities,
            tags: self.tags,
            triggers: self.triggers,
            related_files: self.related_files,
            related_records: self.related_records,
            supersedes: self.supersedes,
            applies_to: self.applies_to,
            valid_until: self.valid_until,
        }
    }
}

impl TauriMemoryActionCommand {
    fn into_request(self) -> DesktopMemoryActionRequest {
        DesktopMemoryActionRequest {
            config_path: self.config_path,
            record_id: self.record_id,
            action: self.action,
            metadata: self.metadata,
        }
    }
}

impl TauriImportSessionCommand {
    fn into_request(self) -> DesktopImportSessionRequest {
        DesktopImportSessionRequest {
            config_path: self.config_path,
            provider: self.provider,
            session_id: self.session_id,
            apply: self.apply,
            actor: self.actor,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{
        TauriContextCommand, TauriDaemonCommand, TauriMemoryActionCommand, TauriMemoryDraftCommand,
        TauriRecordCommand, TauriWakeupCommand, TauriWorkbenchCommand, desktop_apply_memory_action,
        desktop_build_wakeup, desktop_get_history, desktop_get_record, desktop_load_workbench,
        desktop_propose_memory, desktop_run_context,
    };
    use crate::desktop::{DesktopLifecycleActionDto, DesktopMetadataDto};
    use crate::domain::{
        MemoryLifecycleState, MemoryScope, OutputFormat, TargetTool, WakeupProfile,
    };
    use std::fs;
    use tempfile::tempdir;

    fn setup_workspace() -> (tempfile::TempDir, std::path::PathBuf, std::path::PathBuf) {
        let temp = tempdir().unwrap();
        let vault_root = temp.path().join("vault");
        let repo_root = temp.path().join("repo");
        fs::create_dir_all(vault_root.join("10-Projects")).unwrap();
        fs::create_dir_all(&repo_root).unwrap();
        fs::write(
            vault_root.join("10-Projects/spool.md"),
            "---\ntitle: spool\nmemory_type: project\n---\n\nDesktop tauri command test note.\n",
        )
        .unwrap();
        let config_path = temp.path().join("spool.toml");
        fs::write(
            &config_path,
            format!(
                "[vault]\nroot = \"{}\"\n\n[output]\ndefault_format = \"markdown\"\nmax_chars = 4000\nmax_notes = 8\n\n[[projects]]\nid = \"spool\"\nname = \"spool\"\nrepo_paths = [\"{}\"]\nnote_roots = [\"10-Projects\"]\n",
                vault_root.display(),
                repo_root.display(),
            ),
        )
        .unwrap();
        (temp, config_path, repo_root)
    }

    #[test]
    fn tauri_commands_should_parse_inputs_and_run_context_and_wakeup() {
        let (_temp, config_path, repo_root) = setup_workspace();

        let context = desktop_run_context(TauriContextCommand {
            config_path: config_path.clone(),
            vault_root_override: None,
            cwd: repo_root.clone(),
            task: "summarize current project context".to_string(),
            files: vec!["src/lib.rs".to_string(), "src/main.rs".to_string()],
            target: TargetTool::Claude,
            format: OutputFormat::Markdown,
        })
        .unwrap();
        assert_eq!(context.bundle.route.debug.note_count, 1);

        let wakeup = desktop_build_wakeup(TauriWakeupCommand {
            config_path,
            vault_root_override: None,
            cwd: repo_root,
            task: "prepare restart packet".to_string(),
            files: Vec::new(),
            target: TargetTool::Claude,
            profile: WakeupProfile::Project,
        })
        .unwrap();
        assert_eq!(wakeup.packet.profile, WakeupProfile::Project);
    }

    #[test]
    fn tauri_commands_should_cover_lifecycle_read_and_write_flows() {
        let (_temp, config_path, _repo_root) = setup_workspace();

        let created = desktop_propose_memory(TauriMemoryDraftCommand {
            config_path: config_path.clone(),
            title: "测试偏好".to_string(),
            summary: "先 smoke 再收口".to_string(),
            memory_type: "workflow".to_string(),
            scope: MemoryScope::User,
            source_ref: "session:1".to_string(),
            project_id: None,
            user_id: Some("long".to_string()),
            sensitivity: None,
            metadata: DesktopMetadataDto {
                actor: Some("desktop".to_string()),
                reason: Some("captured from tauri command".to_string()),
                evidence_refs: vec!["session:1".to_string(), "obsidian://workflow".to_string()],
            },
            entities: Vec::new(),
            tags: Vec::new(),
            triggers: Vec::new(),
            related_files: Vec::new(),
            related_records: Vec::new(),
            supersedes: None,
            applies_to: Vec::new(),
            valid_until: None,
        })
        .unwrap();
        assert_eq!(created.entry.record.state, MemoryLifecycleState::Candidate);

        let workbench = desktop_load_workbench(TauriWorkbenchCommand {
            config_path: config_path.clone(),
            daemon: Some(TauriDaemonCommand {
                enabled: false,
                daemon_bin: None,
            }),
        })
        .unwrap();
        assert_eq!(workbench.snapshot.pending_review.len(), 1);

        let record = desktop_get_record(TauriRecordCommand {
            config_path: config_path.clone(),
            record_id: created.entry.record_id.clone(),
            daemon: None,
        })
        .unwrap()
        .unwrap();
        assert_eq!(record.record.record.title, "测试偏好");

        let action = desktop_apply_memory_action(TauriMemoryActionCommand {
            config_path: config_path.clone(),
            record_id: created.entry.record_id.clone(),
            action: DesktopLifecycleActionDto::Accept,
            metadata: DesktopMetadataDto::default(),
        })
        .unwrap();
        assert_eq!(action.entry.record.state, MemoryLifecycleState::Accepted);

        let history = desktop_get_history(TauriRecordCommand {
            config_path,
            record_id: created.entry.record_id,
            daemon: None,
        })
        .unwrap();
        assert_eq!(history.history.len(), 2);
    }
}