use std::{cmp::Ordering, collections::HashMap, num::NonZeroUsize, path::Path};
use rho_protocol::{ExcerptRole, MessageId, SearchExcerpt, SearchLocation, ToolCallId};
use rusqlite::{params, Connection};
use super::{
catalog::{self, CatalogError},
search::{fts_query, LastTerm},
search_window::window,
SessionSummary,
};
pub(crate) const MAX_QUERY_CHARS: usize = 1000;
pub(crate) struct SearchRequest<'a> {
pub(crate) query: &'a str,
pub(crate) cwd: Option<&'a Path>,
pub(crate) cursor: Option<&'a str>,
pub(crate) limit: NonZeroUsize,
}
pub(crate) struct CheckedSearch {
query: String,
after: Option<Cursor<'static>>,
limit: NonZeroUsize,
}
pub(crate) struct Listed(HashMap<String, SessionSummary>);
#[derive(Debug)]
pub(crate) struct SearchPage {
pub(crate) hits: Vec<SearchHit>,
pub(crate) next_cursor: Option<String>,
pub(crate) total: u64,
}
#[derive(Debug)]
pub(crate) struct SearchHit {
pub(crate) summary: SessionSummary,
pub(crate) matching_messages: u64,
pub(crate) excerpts: Vec<SearchExcerpt>,
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum SearchError {
#[error("query must hold a word")]
BlankQuery,
#[error(
"query is over the search's limit (MAX_QUERY_CHARS = {MAX_QUERY_CHARS}): {0} characters"
)]
LongQuery(usize),
#[error("cwd must be an absolute path")]
RelativeCwd,
#[error("unknown session search cursor '{0}'")]
UnknownCursor(String),
#[error(transparent)]
Store(anyhow::Error),
}
pub(crate) fn check(request: SearchRequest<'_>) -> Result<CheckedSearch, SearchError> {
let SearchRequest {
query,
cwd,
cursor,
limit,
} = request;
if query.trim().is_empty() {
return Err(SearchError::BlankQuery);
}
let chars = query.chars().count();
if chars > MAX_QUERY_CHARS {
return Err(SearchError::LongQuery(chars));
}
if cwd.is_some_and(|cwd| !cwd.is_absolute()) {
return Err(SearchError::RelativeCwd);
}
let after = cursor
.map(|cursor| {
Cursor::parse(cursor).ok_or_else(|| SearchError::UnknownCursor(cursor.into()))
})
.transpose()?;
Ok(CheckedSearch {
query: fts_query(query, LastTerm::Prefix),
after,
limit,
})
}
pub(crate) fn listed(root: &Path, cwd: Option<&Path>) -> Result<Listed, SearchError> {
let summaries = catalog::listable_in_root(root, cwd).map_err(|error| match error {
CatalogError::RelativeCwd => SearchError::RelativeCwd,
CatalogError::UnknownCursor(_) | CatalogError::Store(_) => SearchError::Store(error.into()),
})?;
Ok(Listed(
summaries
.into_iter()
.map(|summary| (summary.path.to_string_lossy().into_owned(), summary))
.collect(),
))
}
pub(crate) fn search(
connection: &Connection,
Listed(mut listed): Listed,
request: CheckedSearch,
) -> Result<SearchPage, SearchError> {
let CheckedSearch {
query,
after,
limit,
} = request;
let store = |error: rusqlite::Error| SearchError::Store(error.into());
let connection = &connection.unchecked_transaction().map_err(store)?;
let mut found = Vec::new();
{
let mut statement = connection
.prepare(
"with matches as materialized (
select e.rowid, e.session, bm25(evidence_fts) as score
from evidence_fts join evidence e on e.rowid=evidence_fts.rowid
where evidence_fts match ?1
), ranked as (
select rowid, session, count(*) over (partition by session) as matching,
min(score) over (partition by session) as best,
row_number() over (partition by session order by score, rowid) as n
from matches
) select f.path, r.best, r.matching,
max(case when r.n=1 then r.rowid end),
max(case when r.n=2 then r.rowid end)
from ranked r join files f on f.key=r.session
where r.n<=2 group by r.session",
)
.map_err(store)?;
let mut rows = statement.query([&query]).map_err(store)?;
while let Some(row) = rows.next().map_err(store)? {
let path: String = row.get(0).map_err(store)?;
let Some(summary) = listed.remove(&path) else {
continue;
};
found.push(Found {
summary,
best: row.get(1).map_err(store)?,
matching: row.get(2).map_err(store)?,
rowids: [row.get(3).map_err(store)?, row.get(4).map_err(store)?],
});
}
}
found.sort_by(|left, right| left.place().cmp_order(&right.place()));
let total = found.len() as u64;
let after = after.map(|mut after| {
if let Some(cursor) = found.iter().find(|found| after.names(found)) {
after.best = cursor.best;
}
after
});
let mut page: Vec<Found> = found
.into_iter()
.filter(|found| after.as_ref().is_none_or(|after| after.precedes(found)))
.take(limit.get() + 1)
.collect();
let next_cursor = (page.len() > limit.get()).then(|| {
page.truncate(limit.get());
page.last().expect("a full page").place().encode()
});
let rowids: Vec<i64> = page
.iter()
.flat_map(|found| found.rowids.into_iter().flatten())
.collect();
let mut excerpts: HashMap<i64, SearchExcerpt> = connection
.prepare(
"select e.role, e.text, highlight(evidence_fts,0,char(30),char(31)),
e.at_kind, e.at_id, e.saved_at, e.rowid
from evidence_fts join evidence e on e.rowid=evidence_fts.rowid
where evidence_fts match ?1
and +evidence_fts.rowid in (select value from json_each(?2))",
)
.map_err(store)?
.query_map(
params![query, serde_json::to_string(&rowids).expect("ids encode")],
|row| Ok((row.get(6)?, excerpt(row)?)),
)
.map_err(store)?
.collect::<rusqlite::Result<_>>()
.map_err(store)?;
let mut hits = Vec::with_capacity(page.len());
for found in page {
let mut shown: Vec<SearchExcerpt> = Vec::with_capacity(2);
for excerpt in found
.rowids
.into_iter()
.flatten()
.filter_map(|rowid| excerpts.remove(&rowid))
{
if !shown
.iter()
.any(|previous| previous.role == excerpt.role && previous.text == excerpt.text)
{
shown.push(excerpt);
}
}
let mut summary = found.summary;
summary.added_dirs.retain(|dir| dir.is_dir());
hits.push(SearchHit {
summary,
matching_messages: found.matching,
excerpts: shown,
});
}
Ok(SearchPage {
hits,
next_cursor,
total,
})
}
struct Found {
summary: SessionSummary,
best: f64,
matching: u64,
rowids: [Option<i64>; 2],
}
impl Found {
fn place(&self) -> Cursor<'_> {
Cursor {
updated_at: self.summary.updated_at,
best: self.best,
id: std::borrow::Cow::Borrowed(&self.summary.id),
cwd: self.summary.cwd.to_string_lossy(),
}
}
}
#[derive(Debug)]
struct Cursor<'a> {
updated_at: u64,
best: f64,
id: std::borrow::Cow<'a, str>,
cwd: std::borrow::Cow<'a, str>,
}
impl Cursor<'_> {
fn encode(&self) -> String {
serde_json::json!([self.updated_at, self.best.to_bits(), self.id, self.cwd]).to_string()
}
fn parse(cursor: &str) -> Option<Cursor<'static>> {
serde_json::from_str::<(u64, u64, String, String)>(cursor)
.ok()
.map(|(updated_at, best, id, cwd)| Cursor {
updated_at,
best: f64::from_bits(best),
id: id.into(),
cwd: cwd.into(),
})
}
fn cmp_order(&self, other: &Cursor<'_>) -> Ordering {
other
.updated_at
.cmp(&self.updated_at)
.then_with(|| self.best.total_cmp(&other.best))
.then_with(|| self.id.cmp(&other.id))
.then_with(|| self.cwd.cmp(&other.cwd))
}
fn precedes(&self, found: &Found) -> bool {
self.cmp_order(&found.place()) == Ordering::Less
}
fn names(&self, found: &Found) -> bool {
let place = found.place();
place.id == self.id && place.cwd == self.cwd
}
}
fn excerpt(row: &rusqlite::Row<'_>) -> rusqlite::Result<SearchExcerpt> {
let role = match row.get_ref(0)?.as_str()? {
"user" => ExcerptRole::User,
"assistant" | "assistant_aborted" => ExcerptRole::Agent,
"tool_result" => ExcerptRole::ToolResult,
"tool_error" => ExcerptRole::ToolError,
_ => ExcerptRole::Unknown,
};
let text: String = row.get(1)?;
let highlighted: String = row.get(2)?;
let shown = window(&text, &highlighted);
let at = match (
row.get_ref(3)?.as_str_or_null()?,
row.get::<_, Option<String>>(4)?,
) {
(Some("message"), Some(id)) => Some(SearchLocation::Message {
id: MessageId::new(id),
}),
(Some("tool_call"), Some(id)) => Some(SearchLocation::ToolCall {
id: ToolCallId::new(id),
}),
_ => None,
};
Ok(SearchExcerpt::new(
role,
shown.text,
shown
.highlights
.into_iter()
.map(|range| range.start as u32..range.end as u32)
.collect(),
at,
row.get(5)?,
))
}