Skip to main content

sqlite_graphrag/
output.rs

1//! Single point of terminal I/O for the CLI (stdout JSON, stderr human).
2//!
3//! All user-visible output must go through this module; direct `println!` in
4//! other modules is forbidden.
5
6use crate::errors::AppError;
7use serde::Serialize;
8
9/// Output format variants accepted by `--format` CLI flags.
10#[derive(Debug, Clone, Copy, clap::ValueEnum, Default)]
11pub enum OutputFormat {
12    #[default]
13    Json,
14    Text,
15    Markdown,
16}
17
18/// Restricted JSON-only format for commands that always emit JSON.
19#[derive(Debug, Clone, Copy, clap::ValueEnum, Default)]
20pub enum JsonOutputFormat {
21    #[default]
22    Json,
23}
24
25/// Serializes `value` as pretty-printed JSON and writes it to stdout with a trailing newline.
26///
27/// Flushes stdout after writing. A `BrokenPipe` error is silenced so that
28/// piping to consumers that close early (e.g. `head`) does not surface an error.
29///
30/// # Errors
31/// Returns `Err` when serialization fails or when a non-`BrokenPipe` I/O error occurs.
32#[inline]
33pub fn emit_json<T: Serialize>(value: &T) -> Result<(), AppError> {
34    let json = serde_json::to_string_pretty(value)?;
35    let mut out = std::io::stdout().lock();
36    if let Err(e) = std::io::Write::write_all(&mut out, json.as_bytes())
37        .and_then(|()| std::io::Write::write_all(&mut out, b"\n"))
38        .and_then(|()| std::io::Write::flush(&mut out))
39    {
40        if e.kind() == std::io::ErrorKind::BrokenPipe {
41            return Ok(());
42        }
43        return Err(AppError::Io(e));
44    }
45    Ok(())
46}
47
48/// Serializes `value` as compact (single-line) JSON and writes it to stdout with a trailing newline.
49///
50/// Flushes stdout after writing. A `BrokenPipe` error is silenced.
51///
52/// # Errors
53/// Returns `Err` when serialization fails or when a non-`BrokenPipe` I/O error occurs.
54#[inline]
55pub fn emit_json_compact<T: Serialize>(value: &T) -> Result<(), AppError> {
56    let json = serde_json::to_string(value)?;
57    let mut out = std::io::stdout().lock();
58    if let Err(e) = std::io::Write::write_all(&mut out, json.as_bytes())
59        .and_then(|()| std::io::Write::write_all(&mut out, b"\n"))
60        .and_then(|()| std::io::Write::flush(&mut out))
61    {
62        if e.kind() == std::io::ErrorKind::BrokenPipe {
63            return Ok(());
64        }
65        return Err(AppError::Io(e));
66    }
67    Ok(())
68}
69
70/// Writes compact JSON to stdout, silently ignoring serialization and I/O errors.
71/// Designed for NDJSON streaming where partial output is acceptable.
72#[inline]
73pub fn emit_json_line<T: Serialize>(value: &T) {
74    if let Ok(json) = serde_json::to_string(value) {
75        let mut out = std::io::stdout().lock();
76        let _ = std::io::Write::write_all(&mut out, json.as_bytes());
77        let _ = std::io::Write::write_all(&mut out, b"\n");
78        let _ = std::io::Write::flush(&mut out);
79    }
80}
81
82/// Writes `msg` followed by a newline to stdout and flushes.
83///
84/// A `BrokenPipe` error is silenced gracefully.
85#[inline]
86pub fn emit_text(msg: &str) {
87    let mut out = std::io::stdout().lock();
88    let _ = std::io::Write::write_all(&mut out, msg.as_bytes())
89        .and_then(|()| std::io::Write::write_all(&mut out, b"\n"))
90        .and_then(|()| std::io::Write::flush(&mut out));
91}
92
93/// Logs `msg` as a structured `tracing::info!` event (does not write to stdout).
94#[inline]
95pub fn emit_progress(msg: &str) {
96    tracing::info!(target: "output", message = msg);
97}
98
99/// Emits a bilingual progress message honouring `--lang` or `SQLITE_GRAPHRAG_LANG`.
100/// Usage: `output::emit_progress_i18n("Computing embedding...", "Calculando embedding...")`.
101pub fn emit_progress_i18n(en: &str, pt: &str) {
102    use crate::i18n::{current, Language};
103    match current() {
104        Language::English => tracing::info!(target: "output", message = en),
105        Language::Portuguese => tracing::info!(target: "output", message = pt),
106    }
107}
108
109/// Emits a JSON error envelope to stdout for machine consumers.
110///
111/// Ensures the stdout JSON contract is honoured even on error paths:
112/// `{"error": true, "code": <exit_code>, "message": "<localized_msg>"}`.
113/// A `BrokenPipe` error is silenced so piping to early-closing consumers
114/// does not surface a secondary error.
115#[cold]
116#[inline(never)]
117pub fn emit_error_json(code: i32, message: &str) {
118    #[derive(serde::Serialize)]
119    struct ErrorEnvelope<'a> {
120        error: bool,
121        code: i32,
122        message: &'a str,
123    }
124    let envelope = ErrorEnvelope {
125        error: true,
126        code,
127        message,
128    };
129    if emit_json(&envelope).is_err() {
130        use std::io::Write;
131        let escaped = message.replace('\\', "\\\\").replace('"', "\\\"");
132        let _ = writeln!(
133            std::io::stdout().lock(),
134            r#"{{"error":true,"code":{code},"message":"{escaped}"}}"#
135        );
136    }
137}
138
139/// Emits a localised error message to stderr with the `Error:`/`Erro:` prefix.
140///
141/// Centralises human-readable error output following Pattern 5 (`output.rs` is the
142/// SOLE I/O point of the CLI). Does not log via `tracing` — call `tracing::error!`
143/// explicitly before this function when structured observability is desired.
144#[cold]
145#[inline(never)]
146pub fn emit_error(localized_msg: &str) {
147    tracing::error!(target: "output", message = localized_msg);
148    eprintln!("{}: {}", crate::i18n::error_prefix(), localized_msg);
149}
150
151/// Emits a bilingual error to stderr honouring `--lang` or `SQLITE_GRAPHRAG_LANG`.
152/// Usage: `output::emit_error_i18n("invariant violated", "invariante violado")`.
153#[cold]
154#[inline(never)]
155pub fn emit_error_i18n(en: &str, pt: &str) {
156    use crate::i18n::{current, Language};
157    let msg = match current() {
158        Language::English => en,
159        Language::Portuguese => pt,
160    };
161    emit_error(msg);
162}
163
164/// JSON payload emitted by the `remember` subcommand.
165///
166/// All fields are required by the JSON contract (see `docs/schemas/remember.schema.json`).
167/// `operation` is an alias of `action` for compatibility with clients using the old field name.
168///
169/// # Examples
170///
171/// ```
172/// use sqlite_graphrag::output::RememberResponse;
173///
174/// let resp = RememberResponse {
175///     memory_id: 1,
176///     name: "nota-inicial".into(),
177///     namespace: "global".into(),
178///     action: "created".into(),
179///     operation: "created".into(),
180///     version: 1,
181///     entities_persisted: 0,
182///     relationships_persisted: 0,
183///     relationships_truncated: false,
184///     chunks_created: 1,
185///     chunks_persisted: 0,
186///     urls_persisted: 0,
187///     extraction_method: None,
188///     merged_into_memory_id: None,
189///     warnings: vec![],
190///     created_at: 1_700_000_000,
191///     created_at_iso: "2023-11-14T22:13:20Z".into(),
192///     elapsed_ms: 42,
193///     name_was_normalized: false,
194///     original_name: None,
195/// };
196///
197/// let json = serde_json::to_string(&resp).unwrap();
198/// assert!(json.contains("\"memory_id\":1"));
199/// assert!(json.contains("\"elapsed_ms\":42"));
200/// assert!(json.contains("\"merged_into_memory_id\":null"));
201/// assert!(json.contains("\"urls_persisted\":0"));
202/// assert!(json.contains("\"relationships_truncated\":false"));
203/// ```
204#[derive(Serialize)]
205pub struct RememberResponse {
206    pub memory_id: i64,
207    pub name: String,
208    pub namespace: String,
209    pub action: String,
210    /// Semantic alias of `action` for compatibility with the contract documented in SKILL.md.
211    pub operation: String,
212    pub version: i64,
213    pub entities_persisted: usize,
214    pub relationships_persisted: usize,
215    /// True when the relationship builder hit the cap before covering all entity pairs.
216    /// Callers can use this to decide whether to increase GRAPHRAG_MAX_RELATIONSHIPS_PER_MEMORY.
217    pub relationships_truncated: bool,
218    /// Total number of chunks the body was split into BEFORE dedup.
219    ///
220    /// For single-chunk bodies this equals 1 even though no row is added to
221    /// the `memory_chunks` table — the memory row itself acts as the chunk.
222    /// Use `chunks_persisted` to know how many rows were actually written.
223    pub chunks_created: usize,
224    /// Number of chunks actually written to chunks/embeddings tables. Always <= chunks_created.
225    ///
226    /// Equal when no chunk had identical normalized text already in DB; less when dedup skipped
227    /// some. Equals zero for single-chunk bodies (the memory row is the chunk) and equals
228    /// `chunks_created` for multi-chunk bodies. Added in v1.0.23 to disambiguate from
229    /// `chunks_created` and reflect database state precisely.
230    pub chunks_persisted: usize,
231    /// Number of unique URLs inserted into `memory_urls` for this memory.
232    /// Added in v1.0.24 — split URLs out of the entity graph (P0-2 fix).
233    #[serde(default)]
234    pub urls_persisted: usize,
235    /// Extraction method used: "gliner-{variant}+regex" or "regex-only". None when NER is not enabled.
236    #[serde(skip_serializing_if = "Option::is_none")]
237    pub extraction_method: Option<String>,
238    pub merged_into_memory_id: Option<i64>,
239    pub warnings: Vec<String>,
240    /// Timestamp Unix epoch seconds.
241    pub created_at: i64,
242    /// RFC 3339 UTC timestamp string parallel to `created_at` for ISO 8601 parsers.
243    pub created_at_iso: String,
244    /// Total execution time in milliseconds from handler start to serialisation.
245    pub elapsed_ms: u64,
246    /// True when the user-supplied `--name` differed from the persisted slug
247    /// (i.e. kebab-case normalization changed the value). Added in v1.0.32 so
248    /// callers can detect normalization without parsing stderr WARN logs.
249    #[serde(default)]
250    pub name_was_normalized: bool,
251    /// Original user-supplied `--name` value before normalization.
252    /// Present only when `name_was_normalized == true`; omitted otherwise to
253    /// keep the common (already-kebab) payload small.
254    #[serde(skip_serializing_if = "Option::is_none")]
255    pub original_name: Option<String>,
256}
257
258/// Individual item returned by the `recall` query.
259///
260/// The `memory_type` field is serialised as `"type"` in JSON to maintain
261/// compatibility with external clients — the Rust name uses `memory_type`
262/// to avoid conflict with the reserved keyword.
263///
264/// # Examples
265///
266/// ```
267/// use sqlite_graphrag::output::RecallItem;
268///
269/// let item = RecallItem {
270///     memory_id: 7,
271///     name: "nota-rust".into(),
272///     namespace: "global".into(),
273///     memory_type: "user".into(),
274///     description: "aprendizado de Rust".into(),
275///     snippet: "ownership e borrowing".into(),
276///     distance: 0.12,
277///     score: 0.88,
278///     source: "direct".into(),
279///     graph_depth: None,
280/// };
281///
282/// let json = serde_json::to_string(&item).unwrap();
283/// // Rust field `memory_type` appears as `"type"` in JSON.
284/// assert!(json.contains("\"type\":\"user\""));
285/// assert!(!json.contains("memory_type"));
286/// assert!(json.contains("\"distance\":0.12"));
287/// ```
288#[derive(Serialize, Clone)]
289pub struct RecallItem {
290    pub memory_id: i64,
291    pub name: String,
292    pub namespace: String,
293    #[serde(rename = "type")]
294    pub memory_type: String,
295    pub description: String,
296    pub snippet: String,
297    pub distance: f32,
298    /// Cosine similarity in `[0.0, 1.0]` derived as `1.0 - distance` and clamped
299    /// to that interval. Always populated to satisfy the documented contract
300    /// (M-A5 in v1.0.40); higher means more similar. For graph hits the value
301    /// reflects the hop-derived distance proxy and should be interpreted
302    /// alongside `graph_depth` rather than as a true cosine score.
303    pub score: f32,
304    pub source: String,
305    /// Number of graph hops between this match and the seed memories.
306    ///
307    /// Set to `None` for direct vector matches (where `distance` is meaningful)
308    /// and to `Some(N)` for traversal results, with `N=0` when the depth could
309    /// not be tracked precisely. Added in v1.0.23 to disambiguate graph results
310    /// from the `distance: 0.0` placeholder previously used for graph entries.
311    /// Field is omitted from JSON output when `None`.
312    #[serde(skip_serializing_if = "Option::is_none")]
313    pub graph_depth: Option<u32>,
314}
315
316impl RecallItem {
317    /// Computes the similarity score from a vector distance, clamped to
318    /// `[0.0, 1.0]`. Cosine distance returned by sqlite-vec lives in `[0, 2]`
319    /// in theory but the embedder produces unit-norm vectors so the practical
320    /// range is `[0, 1]`. Centralized so every constructor keeps the contract.
321    #[inline]
322    pub fn score_from_distance(distance: f32) -> f32 {
323        let raw = 1.0 - distance;
324        if raw.is_nan() {
325            0.0
326        } else {
327            raw.clamp(0.0, 1.0)
328        }
329    }
330}
331
332/// Full response envelope returned by the `recall` subcommand.
333///
334/// Contains both direct vector matches and graph-traversal matches, plus the
335/// aggregated `results` list that merges both for callers that do not need
336/// to distinguish the source.
337#[derive(Serialize)]
338pub struct RecallResponse {
339    pub query: String,
340    pub k: usize,
341    pub direct_matches: Vec<RecallItem>,
342    pub graph_matches: Vec<RecallItem>,
343    /// Aggregated alias of `direct_matches` + `graph_matches` for the contract documented in SKILL.md.
344    pub results: Vec<RecallItem>,
345    /// Total execution time in milliseconds from handler start to serialisation.
346    pub elapsed_ms: u64,
347}
348
349#[cfg(test)]
350mod tests {
351    use super::*;
352    use serde::Serialize;
353
354    #[derive(Serialize)]
355    struct Dummy {
356        val: u32,
357    }
358
359    // Non-serializable type to force a JSON serialization error
360    struct NotSerializable;
361    impl Serialize for NotSerializable {
362        fn serialize<S: serde::Serializer>(&self, _: S) -> Result<S::Ok, S::Error> {
363            Err(serde::ser::Error::custom(
364                "intentional serialization failure",
365            ))
366        }
367    }
368
369    #[test]
370    fn emit_json_returns_ok_for_valid_value() {
371        let v = Dummy { val: 42 };
372        assert!(emit_json(&v).is_ok());
373    }
374
375    #[test]
376    fn emit_json_returns_err_for_non_serializable_value() {
377        let v = NotSerializable;
378        assert!(emit_json(&v).is_err());
379    }
380
381    #[test]
382    fn emit_json_compact_returns_ok_for_valid_value() {
383        let v = Dummy { val: 7 };
384        assert!(emit_json_compact(&v).is_ok());
385    }
386
387    #[test]
388    fn emit_json_compact_returns_err_for_non_serializable_value() {
389        let v = NotSerializable;
390        assert!(emit_json_compact(&v).is_err());
391    }
392
393    #[test]
394    fn emit_text_does_not_panic() {
395        emit_text("mensagem de teste");
396    }
397
398    #[test]
399    fn emit_progress_does_not_panic() {
400        emit_progress("progresso de teste");
401    }
402
403    #[test]
404    fn remember_response_serializes_correctly() {
405        let r = RememberResponse {
406            memory_id: 1,
407            name: "teste".to_string(),
408            namespace: "ns".to_string(),
409            action: "created".to_string(),
410            operation: "created".to_string(),
411            version: 1,
412            entities_persisted: 2,
413            relationships_persisted: 3,
414            relationships_truncated: false,
415            chunks_created: 4,
416            chunks_persisted: 4,
417            urls_persisted: 2,
418            extraction_method: None,
419            merged_into_memory_id: None,
420            warnings: vec!["aviso".to_string()],
421            created_at: 1776569715,
422            created_at_iso: "2026-04-19T03:34:15Z".to_string(),
423            elapsed_ms: 123,
424            name_was_normalized: false,
425            original_name: None,
426        };
427        let json = serde_json::to_string(&r).unwrap();
428        assert!(json.contains("memory_id"));
429        assert!(json.contains("aviso"));
430        assert!(json.contains("\"namespace\""));
431        assert!(json.contains("\"merged_into_memory_id\""));
432        assert!(json.contains("\"operation\""));
433        assert!(json.contains("\"created_at\""));
434        assert!(json.contains("\"created_at_iso\""));
435        assert!(json.contains("\"elapsed_ms\""));
436        assert!(json.contains("\"urls_persisted\""));
437        assert!(json.contains("\"relationships_truncated\":false"));
438    }
439
440    #[test]
441    fn recall_item_serializes_renamed_type_field() {
442        let item = RecallItem {
443            memory_id: 10,
444            name: "entidade".to_string(),
445            namespace: "ns".to_string(),
446            memory_type: "entity".to_string(),
447            description: "desc".to_string(),
448            snippet: "trecho".to_string(),
449            distance: 0.5,
450            score: RecallItem::score_from_distance(0.5),
451            source: "db".to_string(),
452            graph_depth: None,
453        };
454        let json = serde_json::to_string(&item).unwrap();
455        assert!(json.contains("\"type\""));
456        assert!(!json.contains("memory_type"));
457        // Field is omitted from JSON when None.
458        assert!(!json.contains("graph_depth"));
459        assert!(json.contains("\"score\":0.5"));
460    }
461
462    #[test]
463    fn recall_response_serializes_with_lists() {
464        let resp = RecallResponse {
465            query: "busca".to_string(),
466            k: 10,
467            direct_matches: vec![],
468            graph_matches: vec![],
469            results: vec![],
470            elapsed_ms: 42,
471        };
472        let json = serde_json::to_string(&resp).unwrap();
473        assert!(json.contains("direct_matches"));
474        assert!(json.contains("graph_matches"));
475        assert!(json.contains("\"k\":"));
476        assert!(json.contains("\"results\""));
477        assert!(json.contains("\"elapsed_ms\""));
478    }
479
480    #[test]
481    fn error_envelope_serializes_correctly() {
482        #[derive(serde::Serialize)]
483        struct ErrorEnvelope<'a> {
484            error: bool,
485            code: i32,
486            message: &'a str,
487        }
488        let envelope = ErrorEnvelope {
489            error: true,
490            code: 10,
491            message: "database disk image is malformed",
492        };
493        let json = serde_json::to_value(&envelope).unwrap();
494        assert_eq!(json["error"], true);
495        assert_eq!(json["code"], 10);
496        assert_eq!(json["message"], "database disk image is malformed");
497    }
498
499    #[test]
500    fn output_format_default_is_json() {
501        let fmt = OutputFormat::default();
502        assert!(matches!(fmt, OutputFormat::Json));
503    }
504
505    #[test]
506    fn output_format_variants_exist() {
507        let _text = OutputFormat::Text;
508        let _md = OutputFormat::Markdown;
509        let _json = OutputFormat::Json;
510    }
511
512    #[test]
513    fn recall_item_clone_produces_equal_value() {
514        let item = RecallItem {
515            memory_id: 99,
516            name: "clone".to_string(),
517            namespace: "ns".to_string(),
518            memory_type: "relation".to_string(),
519            description: "d".to_string(),
520            snippet: "s".to_string(),
521            distance: 0.1,
522            score: RecallItem::score_from_distance(0.1),
523            source: "src".to_string(),
524            graph_depth: Some(2),
525        };
526        let cloned = item.clone();
527        assert_eq!(cloned.memory_id, item.memory_id);
528        assert_eq!(cloned.name, item.name);
529        assert_eq!(cloned.graph_depth, Some(2));
530    }
531}