Skip to main content

origin_types/
requests.rs

1// SPDX-License-Identifier: Apache-2.0
2//! API request types for all HTTP endpoints.
3
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7// ===== Memory CRUD =====
8
9#[derive(Debug, Serialize, Deserialize)]
10pub struct StoreMemoryRequest {
11    pub content: String,
12    #[serde(default)]
13    pub memory_type: Option<String>,
14    #[serde(default, alias = "domain")]
15    pub space: Option<String>,
16    #[serde(default)]
17    pub source_agent: Option<String>,
18    #[serde(default)]
19    pub title: Option<String>,
20    #[serde(default)]
21    pub confidence: Option<f32>,
22    #[serde(default)]
23    pub supersedes: Option<String>,
24    /// Entity name for resolution (e.g. "Alice", "PostgreSQL")
25    #[serde(default)]
26    pub entity: Option<String>,
27    /// Direct entity ID (bypasses name resolution)
28    #[serde(default)]
29    pub entity_id: Option<String>,
30    #[serde(default)]
31    pub structured_fields: Option<serde_json::Value>,
32    #[serde(default)]
33    pub retrieval_cue: Option<String>,
34}
35
36#[derive(Debug, Serialize, Deserialize)]
37pub struct SearchMemoryRequest {
38    pub query: String,
39    #[serde(default = "default_limit")]
40    pub limit: usize,
41    #[serde(default)]
42    pub memory_type: Option<String>,
43    #[serde(default, alias = "domain")]
44    pub space: Option<String>,
45    #[serde(default)]
46    pub source_agent: Option<String>,
47    /// When `true` AND the daemon has a reranker wired (via
48    /// `ORIGIN_RERANKER_ENABLED=1`), results pass through a cross-encoder
49    /// reranker after the embedding+FTS hybrid step. When `true` but no
50    /// reranker is available, the daemon logs a warning and falls back to
51    /// the plain hybrid ordering. Default `false` to preserve current
52    /// behavior for callers that don't opt in.
53    #[serde(default)]
54    pub rerank: bool,
55}
56
57#[derive(Debug, Serialize, Deserialize)]
58pub struct ListMemoriesRequest {
59    #[serde(default)]
60    pub memory_type: Option<String>,
61    #[serde(default, alias = "domain")]
62    pub space: Option<String>,
63    #[serde(default = "default_list_limit")]
64    pub limit: usize,
65    #[serde(default, skip_serializing_if = "Option::is_none")]
66    pub confirmed: Option<bool>,
67}
68
69#[derive(Debug, Serialize, Deserialize)]
70pub struct ConfirmRequest {
71    #[serde(default = "default_confirmed")]
72    pub confirmed: bool,
73}
74
75#[derive(Debug, Serialize, Deserialize)]
76pub struct ReclassifyMemoryRequest {
77    pub memory_type: String,
78}
79
80#[derive(Debug, Serialize, Deserialize)]
81pub struct ImportMemoriesRequest {
82    pub source: String,
83    pub content: String,
84    #[serde(default)]
85    pub label: Option<String>,
86}
87
88// ===== General search/context =====
89
90#[derive(Debug, Serialize, Deserialize)]
91pub struct SearchRequest {
92    pub query: String,
93    #[serde(default = "default_limit")]
94    pub limit: usize,
95    pub source_filter: Option<String>,
96    #[serde(default, alias = "domain")]
97    pub space: Option<String>,
98}
99
100#[doc(hidden)]
101#[derive(Debug, Serialize, Deserialize)]
102pub struct ContextRequest {
103    pub current_file: String,
104    pub cursor_prefix: String,
105    #[serde(default = "default_limit")]
106    pub limit: usize,
107}
108
109#[derive(Debug, Serialize, Deserialize)]
110pub struct ChatContextRequest {
111    #[serde(default)]
112    pub query: Option<String>,
113    #[serde(default)]
114    pub conversation_id: Option<String>,
115    #[serde(default = "default_max_chunks")]
116    pub max_chunks: usize,
117    #[serde(default)]
118    pub relevance_threshold: Option<f64>,
119    /// Deprecated: goal-typed memories were folded into identity by migration 45
120    /// (Phase 0). Daemon ignores this field — no goal-load path exists anymore.
121    /// Field stays for wire backward compat; will be removed in 0.4.
122    #[deprecated(
123        since = "0.3.2",
124        note = "Goal taxonomy folded into Identity by migration 45 (Phase 0). \
125                Daemon ignores this field. Will be removed in 0.4."
126    )]
127    #[serde(default = "default_true")]
128    pub include_goals: bool,
129    #[serde(default, alias = "domain")]
130    pub space: Option<String>,
131}
132
133// ===== Knowledge graph =====
134
135#[derive(Debug, Serialize, Deserialize)]
136pub struct CreateEntityRequest {
137    pub name: String,
138    pub entity_type: String,
139    #[serde(default, alias = "domain")]
140    pub space: Option<String>,
141    #[serde(default)]
142    pub source_agent: Option<String>,
143    #[serde(default)]
144    pub confidence: Option<f32>,
145}
146
147#[doc(hidden)]
148#[derive(Debug, Serialize, Deserialize)]
149pub struct CreateRelationRequest {
150    pub from_entity: String,
151    pub to_entity: String,
152    pub relation_type: String,
153    #[serde(default)]
154    pub source_agent: Option<String>,
155    #[serde(default)]
156    pub confidence: Option<f64>,
157    #[serde(default)]
158    pub explanation: Option<String>,
159    #[serde(default)]
160    pub source_memory_id: Option<String>,
161}
162
163#[derive(Debug, Serialize, Deserialize)]
164pub struct AddObservationRequest {
165    pub entity_id: String,
166    pub content: String,
167    #[serde(default)]
168    pub source_agent: Option<String>,
169    #[serde(default)]
170    pub confidence: Option<f32>,
171}
172
173#[doc(hidden)]
174#[derive(Debug, Serialize, Deserialize)]
175pub struct LinkEntityRequest {
176    pub source_id: String,
177    pub entity_id: String,
178}
179
180#[derive(Debug, Serialize, Deserialize)]
181pub struct ListEntitiesRequest {
182    #[serde(default)]
183    pub entity_type: Option<String>,
184    #[serde(default, alias = "domain")]
185    pub space: Option<String>,
186}
187
188#[derive(Debug, Serialize, Deserialize)]
189pub struct SearchEntitiesRequest {
190    pub query: String,
191    #[serde(default = "default_entity_search_limit")]
192    pub limit: usize,
193}
194
195// ===== Profile & Agents =====
196
197#[derive(Debug, Serialize, Deserialize)]
198pub struct UpdateProfileRequest {
199    #[serde(default)]
200    pub name: Option<String>,
201    #[serde(default)]
202    pub display_name: Option<String>,
203    #[serde(default)]
204    pub email: Option<String>,
205    #[serde(default)]
206    pub bio: Option<String>,
207    #[serde(default)]
208    pub avatar_path: Option<String>,
209}
210
211#[derive(Debug, Serialize, Deserialize)]
212pub struct UpdateAgentRequest {
213    #[serde(default)]
214    pub agent_type: Option<String>,
215    #[serde(default)]
216    pub description: Option<String>,
217    #[serde(default)]
218    pub enabled: Option<bool>,
219    #[serde(default)]
220    pub trust_level: Option<String>,
221    /// Empty string clears the field; None leaves it unchanged.
222    #[serde(default)]
223    pub display_name: Option<String>,
224}
225
226// ===== Spaces =====
227
228#[derive(Debug, Serialize, Deserialize)]
229pub struct CreateSpaceRequest {
230    pub name: String,
231    pub description: Option<String>,
232}
233
234#[derive(Debug, Serialize, Deserialize)]
235pub struct UpdateSpaceRequest {
236    pub new_name: Option<String>,
237    pub description: Option<String>,
238}
239
240// ===== Concepts =====
241
242#[doc(hidden)]
243#[derive(Debug, Serialize, Deserialize)]
244pub struct CreateConceptRequest {
245    pub title: String,
246    pub content: String,
247    #[serde(default)]
248    pub summary: Option<String>,
249    #[serde(default)]
250    pub entity_id: Option<String>,
251    #[serde(default, alias = "domain")]
252    pub space: Option<String>,
253    #[serde(default)]
254    pub source_memory_ids: Vec<String>,
255}
256
257#[derive(Debug, Serialize, Deserialize)]
258pub struct SearchPagesRequest {
259    pub query: String,
260    #[serde(default)]
261    pub limit: Option<usize>,
262    #[serde(default, skip_serializing_if = "Option::is_none")]
263    pub page_type: Option<String>,
264}
265
266// ===== Ingest =====
267
268#[derive(Debug, Serialize, Deserialize)]
269pub struct IngestTextRequest {
270    pub source: String,
271    pub source_id: String,
272    pub title: String,
273    pub content: String,
274    pub url: Option<String>,
275    pub metadata: Option<HashMap<String, String>>,
276}
277
278#[doc(hidden)]
279#[derive(Debug, Serialize, Deserialize)]
280pub struct IngestWebpageRequest {
281    pub url: String,
282    pub title: String,
283    pub content: String,
284    pub metadata: Option<HashMap<String, String>>,
285}
286
287#[derive(Debug, Serialize, Deserialize)]
288pub struct IngestMemoryRequest {
289    pub source: String,
290    pub source_id: String,
291    pub title: String,
292    pub content: String,
293    pub url: Option<String>,
294    pub tags: Option<Vec<String>>,
295    pub metadata: Option<HashMap<String, String>>,
296}
297
298// ===== Sources =====
299
300#[doc(hidden)]
301#[derive(Debug, Serialize, Deserialize)]
302pub struct AddSourceRequest {
303    pub source_type: String,
304    /// Filesystem path as a string. Kept as `String` (not `PathBuf`) because
305    /// this is an HTTP wire format — the handler converts it to `PathBuf`.
306    pub path: String,
307}
308
309// ===== Config =====
310
311#[derive(Debug, Serialize, Deserialize)]
312pub struct UpdateConfigRequest {
313    #[serde(default)]
314    pub skip_apps: Option<Vec<String>>,
315    #[serde(default)]
316    pub skip_title_patterns: Option<Vec<String>>,
317    #[serde(default)]
318    pub private_browsing_detection: Option<bool>,
319    #[serde(default)]
320    pub setup_completed: Option<bool>,
321    #[serde(default)]
322    pub clipboard_enabled: Option<bool>,
323    #[serde(default)]
324    pub screen_capture_enabled: Option<bool>,
325    #[serde(default)]
326    pub remote_access_enabled: Option<bool>,
327    /// Anthropic model used for fast/routine tasks (e.g. classification, tagging).
328    #[serde(default)]
329    pub routine_model: Option<String>,
330    /// Anthropic model used for synthesis tasks (e.g. distillation, narrative).
331    #[serde(default)]
332    pub synthesis_model: Option<String>,
333    /// Base URL for an OpenAI-compatible external LLM endpoint.
334    #[serde(default)]
335    pub external_llm_endpoint: Option<String>,
336    /// Model identifier to use with the external LLM endpoint.
337    #[serde(default)]
338    pub external_llm_model: Option<String>,
339}
340
341// ===== Chunks / indexed files =====
342
343#[derive(Debug, Serialize, Deserialize)]
344pub struct DeleteByTimeRangeRequest {
345    pub start: i64,
346    pub end: i64,
347}
348
349#[derive(Debug, Serialize, Deserialize)]
350pub struct BulkDeleteItem {
351    pub source: String,
352    pub source_id: String,
353}
354
355#[derive(Debug, Serialize, Deserialize)]
356pub struct BulkDeleteRequest {
357    pub items: Vec<BulkDeleteItem>,
358}
359
360#[derive(Debug, Serialize, Deserialize)]
361pub struct UpdateChunkRequest {
362    pub content: String,
363}
364
365// ===== Entity / Observation CRUD =====
366
367#[derive(Debug, Serialize, Deserialize)]
368pub struct ConfirmEntityRequest {
369    #[serde(default = "default_confirmed")]
370    pub confirmed: bool,
371}
372
373#[derive(Debug, Serialize, Deserialize)]
374pub struct AddEntityObservationRequest {
375    pub content: String,
376    #[serde(default)]
377    pub source_agent: Option<String>,
378    #[serde(default)]
379    pub confidence: Option<f32>,
380}
381
382#[derive(Debug, Serialize, Deserialize)]
383pub struct UpdateObservationRequest {
384    pub content: String,
385}
386
387#[derive(Debug, Serialize, Deserialize)]
388pub struct ConfirmObservationRequest {
389    #[serde(default = "default_confirmed")]
390    pub confirmed: bool,
391}
392
393// ===== Spaces extended =====
394
395#[derive(Debug, Serialize, Deserialize)]
396pub struct ReorderSpaceRequest {
397    pub name: String,
398    pub new_order: i64,
399}
400
401#[derive(Debug, Serialize, Deserialize)]
402pub struct SetDocumentSpaceRequest {
403    pub space_name: String,
404}
405
406// ===== Tags =====
407
408#[derive(Debug, Serialize, Deserialize)]
409pub struct SetDocumentTagsRequest {
410    pub tags: Vec<String>,
411}
412
413// ===== Memory update =====
414
415#[derive(Debug, Serialize, Deserialize)]
416pub struct UpdateMemoryRequest {
417    #[serde(default)]
418    pub content: Option<String>,
419    #[serde(default, alias = "domain")]
420    pub space: Option<String>,
421    #[serde(default)]
422    pub confirmed: Option<bool>,
423    #[serde(default)]
424    pub memory_type: Option<String>,
425}
426
427#[derive(Debug, Serialize, Deserialize)]
428pub struct SetStabilityRequest {
429    pub stability: String,
430}
431
432#[derive(Debug, Serialize, Deserialize)]
433pub struct CorrectMemoryRequest {
434    pub correction_prompt: String,
435}
436
437// ===== Concepts update =====
438
439#[derive(Debug, Serialize, Deserialize)]
440pub struct UpdatePageRequest {
441    pub content: String,
442    /// Source memory IDs to associate with this page version.
443    /// Omitted or empty by HTTP callers that preserve existing sources;
444    /// always populated by `post_write::update_page`.
445    #[serde(default)]
446    pub source_memory_ids: Vec<String>,
447}
448
449/// Body for `PUT /api/pages/{id}` — agent-side refresh of a stale page.
450///
451/// Distinct from `UpdatePageRequest` (manual content edit via POST) because:
452///  - `source_memory_ids` is replaced, not preserved.
453///  - `summary` is optionally updated.
454///  - The handler clears `stale_reason` in the same transaction.
455///
456/// v1 deliberately excludes title / entity_id / space changes — slug rename
457/// has its own concurrent-read failure mode and lands as a separate route.
458#[derive(Debug, Serialize, Deserialize)]
459pub struct RefreshPageRequest {
460    pub content: String,
461    pub source_memory_ids: Vec<String>,
462    #[serde(default)]
463    pub summary: Option<String>,
464}
465
466// ===== Concept Export =====
467
468/// Request body for `POST /api/pages/export` (bulk export all pages to an Obsidian vault).
469#[derive(Debug, Deserialize, Serialize)]
470pub struct ExportPagesRequest {
471    pub vault_path: Option<String>,
472}
473
474#[derive(Debug, Deserialize, Serialize)]
475pub struct ExportPageRequest {
476    pub vault_path: String,
477}
478
479// ===== LLM test =====
480
481/// `POST /api/llm/test` — probe an OpenAI-compatible LLM endpoint with a 1-shot prompt.
482/// Used by the app settings UI to validate a custom endpoint before saving.
483#[derive(Debug, Clone, Serialize, Deserialize)]
484pub struct TestLlmRequest {
485    pub endpoint: String,
486    pub model: String,
487    /// Optional override prompt. Defaults to "Say 'hello' and nothing else." server-side.
488    #[serde(default, skip_serializing_if = "Option::is_none")]
489    pub prompt: Option<String>,
490}
491
492#[derive(Debug, Clone, Serialize, Deserialize)]
493pub struct TestLlmResponse {
494    pub response: String,
495}
496
497// ===== Default value functions =====
498
499fn default_limit() -> usize {
500    10
501}
502
503fn default_list_limit() -> usize {
504    100
505}
506
507fn default_confirmed() -> bool {
508    true
509}
510
511fn default_max_chunks() -> usize {
512    5
513}
514
515fn default_true() -> bool {
516    true
517}
518
519fn default_entity_search_limit() -> usize {
520    20
521}
522
523#[cfg(test)]
524mod search_pages_page_type_test {
525    use super::*;
526
527    #[test]
528    fn search_pages_request_accepts_page_type() {
529        let json = r#"{"query":"foo","limit":10,"page_type":"recap"}"#;
530        let parsed: SearchPagesRequest = serde_json::from_str(json).unwrap();
531        assert_eq!(parsed.page_type.as_deref(), Some("recap"));
532    }
533
534    #[test]
535    fn search_pages_request_page_type_optional() {
536        let json = r#"{"query":"foo","limit":10}"#;
537        let parsed: SearchPagesRequest = serde_json::from_str(json).unwrap();
538        assert!(parsed.page_type.is_none());
539    }
540}
541
542#[cfg(test)]
543mod list_memories_confirmed_test {
544    use super::*;
545
546    #[test]
547    fn confirmed_false_serializes_to_json() {
548        let req = ListMemoriesRequest {
549            memory_type: None,
550            space: None,
551            limit: 20,
552            confirmed: Some(false),
553        };
554        let s = serde_json::to_string(&req).unwrap();
555        assert!(s.contains("\"confirmed\":false"), "got: {s}");
556    }
557
558    #[test]
559    fn confirmed_none_skips_serialization() {
560        let req = ListMemoriesRequest {
561            memory_type: None,
562            space: None,
563            limit: 20,
564            confirmed: None,
565        };
566        let s = serde_json::to_string(&req).unwrap();
567        assert!(!s.contains("confirmed"), "got: {s}");
568    }
569}