Skip to main content

openbutler_protocol/session/
search.rs

1//! Session search over the host's derived index: the request a client sends
2//! and the answer the host composes.
3//!
4//! The index itself never leaves the host. A client asks a question and gets
5//! finished rows plus the screen the host built from them, exactly like
6//! `session.list`: `matches` is for a surface that wants the data (a table it
7//! styles itself, a list it filters), `lines` is for one that prints.
8
9use serde::{Deserialize, Serialize};
10
11use crate::screen::ScreenLine;
12
13/// What to search for, and how much of the answer to return.
14///
15/// The query is keywords, not a sentence: matching is prefix-based and the
16/// host drops the keywords that appear all over the corpus. The answer names
17/// both lists, so a caller can see what was actually searched.
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub struct SessionSearchRequest {
21    /// Two to four distinctive keywords. Empty is refused.
22    pub query: String,
23    /// Maximum matches to return; the host's default when absent.
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub limit: Option<i64>,
26    /// Matches to skip, for paging.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub offset: Option<i64>,
29    /// Include text that came from tool output; off by default because it is
30    /// the noisiest part of a transcript.
31    #[serde(
32        default,
33        skip_serializing_if = "Option::is_none",
34        rename = "includeOutputs"
35    )]
36    pub include_outputs: Option<bool>,
37}
38
39/// One place that matched: the ids a client needs to open it, plus the tier
40/// that produced the row so a weaker hit can be told apart from a stronger
41/// one.
42#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
43#[serde(rename_all = "camelCase")]
44pub struct SessionSearchMatch {
45    /// The session the row belongs to.
46    #[serde(rename = "sessionId")]
47    pub session_id: String,
48    /// 1-based display entry number, the same numbering `session.load` and
49    /// the transcript window use.
50    pub entry: i64,
51    /// Which piece of the entry matched: user, assistant, notice, divider,
52    /// command or output.
53    pub kind: String,
54    /// A window around the match, with the matched words bracketed.
55    pub snippet: String,
56    /// Relevance, higher is better.
57    pub score: f64,
58    /// The tier that produced the row: `phrase`, `strict` or `relaxed`.
59    pub tier: String,
60    /// The session's title, so a row reads on its own.
61    pub title: String,
62    /// The directory the session ran in, when it had one.
63    #[serde(
64        default,
65        skip_serializing_if = "Option::is_none",
66        rename = "workspaceRoot"
67    )]
68    pub workspace_root: Option<String>,
69    /// The session's last update, as the record carries it.
70    #[serde(rename = "updatedAt")]
71    pub updated_at: String,
72}
73
74/// Answer to `session.search`: the rows plus the screen the host composed.
75///
76/// `total` is what the host found and capped, not a count of every possible
77/// hit; `truncated` says the host stopped at its own fetch bound, so the
78/// number is a floor.
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
80#[serde(rename_all = "camelCase")]
81pub struct SessionSearchAnswer {
82    /// The rows, best first.
83    pub matches: Vec<SessionSearchMatch>,
84    /// How many rows the host is reporting.
85    pub total: i64,
86    /// The keywords that were actually searched.
87    pub terms: Vec<String>,
88    /// The keywords the corpus-frequency check threw away.
89    pub dropped: Vec<String>,
90    /// The host stopped at its fetch bound: `total` is a floor.
91    pub truncated: bool,
92    /// The finished screen: a header, one line per row, and a footer.
93    pub lines: Vec<ScreenLine>,
94}