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    #[serde(default)]
256    pub creation_kind: Option<String>,
257}
258
259#[derive(Debug, Serialize, Deserialize)]
260pub struct SearchPagesRequest {
261    pub query: String,
262    #[serde(default)]
263    pub limit: Option<usize>,
264    #[serde(default, skip_serializing_if = "Option::is_none")]
265    pub page_type: Option<String>,
266}
267
268// ===== Ingest =====
269
270#[derive(Debug, Serialize, Deserialize)]
271pub struct IngestTextRequest {
272    pub source: String,
273    pub source_id: String,
274    pub title: String,
275    pub content: String,
276    pub url: Option<String>,
277    pub metadata: Option<HashMap<String, String>>,
278}
279
280#[doc(hidden)]
281#[derive(Debug, Serialize, Deserialize)]
282pub struct IngestWebpageRequest {
283    pub url: String,
284    pub title: String,
285    pub content: String,
286    pub metadata: Option<HashMap<String, String>>,
287}
288
289#[derive(Debug, Serialize, Deserialize)]
290pub struct IngestMemoryRequest {
291    pub source: String,
292    pub source_id: String,
293    pub title: String,
294    pub content: String,
295    pub url: Option<String>,
296    pub tags: Option<Vec<String>>,
297    pub metadata: Option<HashMap<String, String>>,
298}
299
300// ===== Sources =====
301
302#[doc(hidden)]
303#[derive(Debug, Serialize, Deserialize)]
304pub struct AddSourceRequest {
305    pub source_type: String,
306    /// Filesystem path as a string. Kept as `String` (not `PathBuf`) because
307    /// this is an HTTP wire format — the handler converts it to `PathBuf`.
308    pub path: String,
309}
310
311// ===== Config =====
312
313#[derive(Debug, Serialize, Deserialize)]
314pub struct UpdateConfigRequest {
315    #[serde(default)]
316    pub skip_apps: Option<Vec<String>>,
317    #[serde(default)]
318    pub skip_title_patterns: Option<Vec<String>>,
319    #[serde(default)]
320    pub private_browsing_detection: Option<bool>,
321    #[serde(default)]
322    pub setup_completed: Option<bool>,
323    #[serde(default)]
324    pub clipboard_enabled: Option<bool>,
325    #[serde(default)]
326    pub screen_capture_enabled: Option<bool>,
327    #[serde(default)]
328    pub remote_access_enabled: Option<bool>,
329    /// Anthropic model used for fast/routine tasks (e.g. classification, tagging).
330    #[serde(default)]
331    pub routine_model: Option<String>,
332    /// Anthropic model used for synthesis tasks (e.g. distillation, narrative).
333    #[serde(default)]
334    pub synthesis_model: Option<String>,
335    /// Base URL for an OpenAI-compatible external LLM endpoint.
336    #[serde(default)]
337    pub external_llm_endpoint: Option<String>,
338    /// Model identifier to use with the external LLM endpoint.
339    #[serde(default)]
340    pub external_llm_model: Option<String>,
341}
342
343// ===== Chunks / indexed files =====
344
345#[derive(Debug, Serialize, Deserialize)]
346pub struct DeleteByTimeRangeRequest {
347    pub start: i64,
348    pub end: i64,
349}
350
351#[derive(Debug, Serialize, Deserialize)]
352pub struct BulkDeleteItem {
353    pub source: String,
354    pub source_id: String,
355}
356
357#[derive(Debug, Serialize, Deserialize)]
358pub struct BulkDeleteRequest {
359    pub items: Vec<BulkDeleteItem>,
360}
361
362#[derive(Debug, Serialize, Deserialize)]
363pub struct UpdateChunkRequest {
364    pub content: String,
365}
366
367// ===== Entity / Observation CRUD =====
368
369#[derive(Debug, Serialize, Deserialize)]
370pub struct ConfirmEntityRequest {
371    #[serde(default = "default_confirmed")]
372    pub confirmed: bool,
373}
374
375#[derive(Debug, Serialize, Deserialize)]
376pub struct AddEntityObservationRequest {
377    pub content: String,
378    #[serde(default)]
379    pub source_agent: Option<String>,
380    #[serde(default)]
381    pub confidence: Option<f32>,
382}
383
384#[derive(Debug, Serialize, Deserialize)]
385pub struct UpdateObservationRequest {
386    pub content: String,
387}
388
389#[derive(Debug, Serialize, Deserialize)]
390pub struct ConfirmObservationRequest {
391    #[serde(default = "default_confirmed")]
392    pub confirmed: bool,
393}
394
395// ===== Spaces extended =====
396
397#[derive(Debug, Serialize, Deserialize)]
398pub struct ReorderSpaceRequest {
399    pub name: String,
400    pub new_order: i64,
401}
402
403#[derive(Debug, Serialize, Deserialize)]
404pub struct SetDocumentSpaceRequest {
405    pub space_name: String,
406}
407
408// ===== Tags =====
409
410#[derive(Debug, Serialize, Deserialize)]
411pub struct SetDocumentTagsRequest {
412    pub tags: Vec<String>,
413}
414
415// ===== Memory update =====
416
417#[derive(Debug, Serialize, Deserialize)]
418pub struct UpdateMemoryRequest {
419    #[serde(default)]
420    pub content: Option<String>,
421    #[serde(default, alias = "domain")]
422    pub space: Option<String>,
423    #[serde(default)]
424    pub confirmed: Option<bool>,
425    #[serde(default)]
426    pub memory_type: Option<String>,
427}
428
429#[derive(Debug, Serialize, Deserialize)]
430pub struct SetStabilityRequest {
431    pub stability: String,
432}
433
434#[derive(Debug, Serialize, Deserialize)]
435pub struct CorrectMemoryRequest {
436    pub correction_prompt: String,
437}
438
439// ===== Concepts update =====
440
441#[derive(Debug, Serialize, Deserialize)]
442pub struct UpdatePageRequest {
443    pub content: String,
444    /// Source memory IDs to associate with this page version.
445    /// Omitted or empty by HTTP callers that preserve existing sources;
446    /// always populated by `post_write::update_page`.
447    #[serde(default)]
448    pub source_memory_ids: Vec<String>,
449}
450
451/// Body for `PUT /api/pages/{id}` — agent-side refresh of a stale page.
452///
453/// Distinct from `UpdatePageRequest` (manual content edit via POST) because:
454///  - `source_memory_ids` is replaced, not preserved.
455///  - `summary` is optionally updated.
456///  - The handler clears `stale_reason` in the same transaction.
457///
458/// v1 deliberately excludes title / entity_id / space changes — slug rename
459/// has its own concurrent-read failure mode and lands as a separate route.
460#[derive(Debug, Serialize, Deserialize)]
461pub struct RefreshPageRequest {
462    pub content: String,
463    pub source_memory_ids: Vec<String>,
464    #[serde(default)]
465    pub summary: Option<String>,
466}
467
468// ===== Concept Export =====
469
470/// Request body for `POST /api/pages/export` (bulk export all pages to an Obsidian vault).
471#[derive(Debug, Deserialize, Serialize)]
472pub struct ExportPagesRequest {
473    pub vault_path: Option<String>,
474}
475
476#[derive(Debug, Deserialize, Serialize)]
477pub struct ExportPageRequest {
478    pub vault_path: String,
479}
480
481// ===== LLM test =====
482
483/// `POST /api/llm/test` — probe an OpenAI-compatible LLM endpoint with a 1-shot prompt.
484/// Used by the app settings UI to validate a custom endpoint before saving.
485#[derive(Debug, Clone, Serialize, Deserialize)]
486pub struct TestLlmRequest {
487    pub endpoint: String,
488    pub model: String,
489    /// Optional override prompt. Defaults to "Say 'hello' and nothing else." server-side.
490    #[serde(default, skip_serializing_if = "Option::is_none")]
491    pub prompt: Option<String>,
492}
493
494#[derive(Debug, Clone, Serialize, Deserialize)]
495pub struct TestLlmResponse {
496    pub response: String,
497}
498
499// ===== Default value functions =====
500
501fn default_limit() -> usize {
502    10
503}
504
505fn default_list_limit() -> usize {
506    100
507}
508
509fn default_confirmed() -> bool {
510    true
511}
512
513fn default_max_chunks() -> usize {
514    5
515}
516
517fn default_true() -> bool {
518    true
519}
520
521fn default_entity_search_limit() -> usize {
522    20
523}
524
525#[cfg(test)]
526mod search_pages_page_type_test {
527    use super::*;
528
529    #[test]
530    fn search_pages_request_accepts_page_type() {
531        let json = r#"{"query":"foo","limit":10,"page_type":"recap"}"#;
532        let parsed: SearchPagesRequest = serde_json::from_str(json).unwrap();
533        assert_eq!(parsed.page_type.as_deref(), Some("recap"));
534    }
535
536    #[test]
537    fn search_pages_request_page_type_optional() {
538        let json = r#"{"query":"foo","limit":10}"#;
539        let parsed: SearchPagesRequest = serde_json::from_str(json).unwrap();
540        assert!(parsed.page_type.is_none());
541    }
542}
543
544#[cfg(test)]
545mod list_memories_confirmed_test {
546    use super::*;
547
548    #[test]
549    fn confirmed_false_serializes_to_json() {
550        let req = ListMemoriesRequest {
551            memory_type: None,
552            space: None,
553            limit: 20,
554            confirmed: Some(false),
555        };
556        let s = serde_json::to_string(&req).unwrap();
557        assert!(s.contains("\"confirmed\":false"), "got: {s}");
558    }
559
560    #[test]
561    fn confirmed_none_skips_serialization() {
562        let req = ListMemoriesRequest {
563            memory_type: None,
564            space: None,
565            limit: 20,
566            confirmed: None,
567        };
568        let s = serde_json::to_string(&req).unwrap();
569        assert!(!s.contains("confirmed"), "got: {s}");
570    }
571}