post_cortex_daemon/daemon/format_helpers.rs
1// Copyright (c) 2025 Julius ML
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Formatting helpers for MCP tool results
22
23use post_cortex_memory::content_vectorizer::SemanticSearchResult;
24use serde_json::Value;
25
26/// Format semantic search results as JSON array
27///
28/// Converts search results into a standardized JSON format with the following fields:
29/// - `content`: The text content of the result
30/// - `score`: The combined similarity/importance score
31/// - `session_id`: The UUID of the session containing the result
32/// - `type`: The content type (e.g., "qa", "decision_made")
33/// - `timestamp`: RFC3339 timestamp of when the content was created
34///
35/// # Arguments
36///
37/// * `results` - Slice of semantic search results to format
38///
39/// # Returns
40///
41/// Vector of JSON values representing each result
42///
43/// # Example
44///
45/// ```rust,ignore
46/// use post_cortex::daemon::format_helpers::format_search_results;
47///
48/// let formatted = format_search_results(&results);
49/// // Returns: [{"content": "...", "score": 0.85, ...}, ...]
50/// ```
51pub fn format_search_results(results: &[SemanticSearchResult]) -> Vec<Value> {
52 results
53 .iter()
54 .map(|r| {
55 serde_json::json!({
56 "content": r.text_content,
57 "score": r.combined_score,
58 "session_id": r.session_id,
59 "type": format!("{:?}", r.content_type),
60 "timestamp": r.timestamp.to_rfc3339()
61 })
62 })
63 .collect()
64}