From 47ab7c3925dd033fed26e1ce7d274306fe2f706b Mon Sep 17 00:00:00 2001 From: kzheart Date: Sat, 10 Oct 2026 14:22:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(sessions):=20=E6=8B=86=E5=88=86?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E8=AF=BB=E5=8F=96=E4=B8=8E=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E8=A7=86=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ferry-engine/src/sessions/agent_read.rs | 237 ++++-------------- .../src/sessions/block_fragment.rs | 77 ++++++ .../ferry-engine/src/sessions/branch_point.rs | 76 +----- crates/ferry-engine/src/sessions/mod.rs | 5 + .../src/sessions/read_metadata.rs | 66 +++++ .../ferry-engine/src/sessions/read_query.rs | 113 +++++++++ .../ferry-engine/src/sessions/session_view.rs | 140 +++++++++++ .../ferry-engine/src/sessions/text_window.rs | 63 +++++ 8 files changed, 519 insertions(+), 258 deletions(-) create mode 100644 crates/ferry-engine/src/sessions/block_fragment.rs create mode 100644 crates/ferry-engine/src/sessions/read_metadata.rs create mode 100644 crates/ferry-engine/src/sessions/read_query.rs create mode 100644 crates/ferry-engine/src/sessions/session_view.rs create mode 100644 crates/ferry-engine/src/sessions/text_window.rs diff --git a/crates/ferry-engine/src/sessions/agent_read.rs b/crates/ferry-engine/src/sessions/agent_read.rs index 0f3f708..d73ecad 100644 --- a/crates/ferry-engine/src/sessions/agent_read.rs +++ b/crates/ferry-engine/src/sessions/agent_read.rs @@ -11,15 +11,19 @@ use crate::errors::{DomainError, DomainResult}; use crate::model::{native_locator, tool_result_text, BlockKind, Message, Session}; use super::index::{AgentSessionIndex, IndexedSession}; -use super::safety::{ - bounded_int, python_json, python_json_len, record_session_id, string_set, truncate_text, - MAX_AGENT_DTO_BYTES, -}; +use super::safety::{python_json, python_json_len, string_set, truncate_text}; -pub const MAX_CONTENT_SEARCH_RESULTS: i64 = 50; -pub const MAX_CONTEXT_MESSAGES: i64 = 50; -pub const MAX_CONTEXT_BYTES: i64 = 64 * 1024; -pub const DEFAULT_CONTEXT_BYTES: i64 = 24 * 1024; +pub const MAX_CONTENT_SEARCH_RESULTS: i64 = super::read_query::MAX_RESULTS; +pub const MAX_CONTEXT_MESSAGES: i64 = super::read_query::MAX_MESSAGES; +pub const MAX_CONTEXT_BYTES: i64 = super::read_query::MAX_BYTES; +pub const DEFAULT_CONTEXT_BYTES: i64 = super::read_query::DEFAULT_BYTES; + +use super::block_fragment::BlockFragment; +use super::read_metadata::ReadMetadata; +use super::read_query::{ContextOptions, ReadScope, SearchOptions}; +use super::session_view::{content_snapshot, message_identity, raw_turns_before}; +pub use super::text_window::{char_find, char_slice}; +use super::text_window::{searchable_text, TextWindow}; /// 读取前后各做一次 `validate_read_scope`,中间夹一次 `resolve` 的钉内容校验。 /// @@ -187,37 +191,31 @@ pub fn get_session_context_through( index: &AgentSessionIndex, through: Option<&str>, ) -> DomainResult> { - let first = bounded_int(from_message, 1, 1, 1_000_000, "from_message")?; - let count = bounded_int(limit, 20, 1, MAX_CONTEXT_MESSAGES, "limit")? as usize; - let budget = bounded_int( - max_bytes, - DEFAULT_CONTEXT_BYTES, - 1024, - MAX_CONTEXT_BYTES, - "max_bytes", - )? as usize; + let options = ContextOptions::parse(from_message, limit, max_bytes)?; + let first = options.first; + let count = options.count; + let budget = options.budget; let supplied = Cursor::decode(cursor)?; let continuing = supplied.is_some(); let record = read_record(index, tool, opaque_ref, continuing)?; let mut session = read_indexed_session(index, &record, true) .map_err(|error| cursor_read_error(error, continuing))?; super::branch_point::restrict(&mut session, through)?; - let binding = read_cursor::digest(&json!([ - "context", + let scope = ReadScope { tool, - opaque_ref, - first, + reference: opaque_ref, inert, - include_tool_outputs, - through - ])); - let snapshot = read_cursor::digest(&json!([record.revision, session])); + outputs: include_tool_outputs, + through, + }; + let binding = scope.context_binding(first); + let snapshot = content_snapshot(&record.revision, &session); let start = Cursor::resume( supplied, &binding, &snapshot, Position { - message: first as usize - 1, + message: first - 1, block: 0, offset: 0, }, @@ -225,25 +223,7 @@ pub fn get_session_context_through( if continuing && start.message >= session.messages.len() { return Err(read_cursor::error("cursor_invalid", "游标消息位置无效")); } - let total_turns = session - .messages - .iter() - .filter(|message| message.role == "user") - .count(); - let title_source = if inert { - super::inert::strip_text(&session.title) - } else { - session.title.clone() - }; - let (title, title_truncated) = truncate_text(&title_source, 200); - let (project, project_truncated) = truncate_text(&session.cwd, 1024); - let base = json!({"tool": tool, "ref": opaque_ref, - "session_id": record_session_id(&record.row, Some(&session.source_id)), - "revision": record.revision, "title": title, "project": project, - "title_source": record.row.get("title_source").and_then(Value::as_str).unwrap_or(""), - "title_truncated": title_truncated, "project_truncated": project_truncated, - "message_count": session.messages.len(), "turn_count": total_turns, - "mode": "context"}); + let base = ReadMetadata::new(tool, opaque_ref, &record, &session).context(inert); context_page( &session, base, @@ -288,12 +268,7 @@ fn context_page( let mut scanned = 0usize; let omissions = std::cell::Cell::new((0usize, 0usize)); let mut byte_limited = false; - let mut current_turn = session - .messages - .iter() - .take(start.message) - .filter(|message| message.role == "user") - .count(); + let mut current_turn = raw_turns_before(session, start.message); if inert { base["inert"] = json!(true); } @@ -367,15 +342,15 @@ fn context_page( let mut item = json!({"message": message_index + 1, "turn": current_turn, "role": message.role, "editable": message_is_rewritable(message), "locator": locator(message, message_index)?, "blocks": [], "complete": false, - "origin": super::inert::message_origin(message), "duplicate_key": super::inert::duplicate_key(message)}); + "origin": message_identity(message)["origin"], "duplicate_key": message_identity(message)["duplicate_key"]}); if inert { item["inert"] = json!(true); } let mut returned_blocks = Vec::::new(); while position.block < blocks.len() { let block = &blocks[position.block]; - let encoded = serde_json::to_string(block).expect("Value 可编码"); - if position.offset >= encoded.len() || !encoded.is_char_boundary(position.offset) { + let fragment = BlockFragment::new(block); + if !fragment.valid_offset(position.offset) { return Err(read_cursor::error("cursor_invalid", "游标字节位置无效")); } let next_block = if position.block + 1 == blocks.len() { @@ -411,43 +386,7 @@ fn context_page( } continue; } - // 按完整 DTO 的实际编码大小二分,而非将正文长度误当成输出预算。 - let fragment = |end: usize| { - json!({"kind": "fragment", "block": block["block"], - "fragment": {"encoding": "json", "offset_bytes": position.offset, - "total_bytes": encoded.len(), "text": &encoded[position.offset..end], "complete": end == encoded.len()}}) - }; - let mut low = position.offset; - let mut high = encoded.len(); - while low < high { - let mut end = low + (high - low).div_ceil(2); - while end > low && !encoded.is_char_boundary(end) { - end -= 1; - } - if end == low { - end = encoded[low..] - .char_indices() - .nth(1) - .map(|(n, _)| low + n) - .unwrap_or(encoded.len()); - if end > high { - break; - } - } - let next = if end == encoded.len() { - next_block - } else { - Position { - offset: end, - ..position - } - }; - if python_json_len(&candidate(fragment(end), next)) <= budget { - low = end; - } else { - high = end - 1; - } - } + let low = fragment.fitting_end(position, budget, next_block, candidate); if low == position.offset { byte_limited = true; if !returned_blocks.is_empty() { @@ -456,15 +395,8 @@ fn context_page( } break 'messages; } - returned_blocks.push(fragment(low)); - position = if low == encoded.len() { - next_block - } else { - Position { - offset: low, - ..position - } - }; + returned_blocks.push(fragment.value(position.offset, low)); + position = fragment.next_position(position, low, next_block); if position.offset > 0 { byte_limited = true; break; @@ -526,51 +458,6 @@ fn status_text(status: crate::model::ToolResultStatus) -> &'static str { } } -/// 检索用文本:调用方要求带工具输出时一并纳入范围。 -/// -/// `inert=true` 时文本先过一遍剥离,检索与 snippet 都不会命中源 agent 的 -/// system prompt——否则 `--inert --terms` 会是个静默无效的组合。 -fn searchable_text(message: &Message, include_tool_outputs: bool, inert: bool) -> String { - let mut parts: Vec = Vec::new(); - for block in &message.blocks { - if block.kind == BlockKind::Text && !block.text.is_empty() { - let text = if inert { - super::inert::strip_text(&block.text) - } else { - block.text.clone() - }; - if !text.is_empty() { - parts.push(text); - } - } else if include_tool_outputs && block.kind == BlockKind::Tool { - let Some(call) = block.tool.as_ref() else { - continue; - }; - parts.push(format!("[tool {}]", call.name)); - let output = tool_result_text(call.result.as_ref()); - if !output.is_empty() { - parts.push(output); - } - } - } - parts.join("\n") -} - -/// 以字符(不是字节)为单位取子串,对齐 Python 的切片语义。 -pub fn char_slice(text: &str, start: usize, end: usize) -> String { - text.chars() - .skip(start) - .take(end.saturating_sub(start)) - .collect() -} - -/// 以字符为单位查找子串位置。 -pub fn char_find(haystack: &str, needle: &str) -> Option { - haystack - .find(needle) - .map(|byte| haystack[..byte].chars().count()) -} - /// `session_read` 的 search 档位。 #[allow(clippy::too_many_arguments)] pub fn search_session_content( @@ -642,7 +529,8 @@ pub fn search_session_content_through( params, )); } - let maximum = bounded_int(limit, 20, 1, MAX_CONTENT_SEARCH_RESULTS, "limit")? as usize; + let options = SearchOptions::parse(limit, max_bytes)?; + let maximum = options.count; let mut sorted_terms = wanted; sorted_terms.sort(); let normalized: Vec<(String, String)> = sorted_terms @@ -653,33 +541,20 @@ pub fn search_session_content_through( let mut session = read_indexed_session(index, &record, true) .map_err(|error| cursor_read_error(error, supplied.is_some()))?; super::branch_point::restrict(&mut session, through)?; - let budget = bounded_int( - max_bytes, - MAX_AGENT_DTO_BYTES as i64, - 1024, - MAX_CONTEXT_BYTES, - "max_bytes", - )? as usize; - let binding = read_cursor::digest(&json!([ - "search", + let budget = options.budget; + let scope = ReadScope { tool, - opaque_ref, - sorted_terms, - allowed_roles, + reference: opaque_ref, inert, - include_tool_outputs, - through - ])); - let snapshot = read_cursor::digest(&json!([record.revision, session])); + outputs: include_tool_outputs, + through, + }; + let binding = scope.search_binding(&sorted_terms, &allowed_roles); + let snapshot = content_snapshot(&record.revision, &session); let start = Cursor::resume(supplied, &binding, &snapshot, Position::default())?; if start.block != 0 || start.offset != 0 || start.message > session.messages.len() { return Err(read_cursor::error("cursor_invalid", "搜索游标位置无效")); } - let total_turns = session - .messages - .iter() - .filter(|message| message.role == "user") - .count() as i64; let mut matches: Vec = Vec::new(); let mut current_turn = 0i64; let mut total_matches = 0i64; @@ -723,15 +598,7 @@ pub fn search_session_content_through( .filter_map(|(_, folded_term)| char_find(&folded, folded_term)) .min() .unwrap_or(0); - let text_chars = text.chars().count(); - let start = first_hit.saturating_sub(240); - let end = text_chars.min(first_hit + 560); - let snippet = format!( - "{}{}{}", - if start > 0 { "…" } else { "" }, - char_slice(&text, start, end), - if end < text_chars { "…" } else { "" } - ); + let window = TextWindow::around(&text, first_hit); let editable = message_is_rewritable(message); let mut item = Map::new(); item.insert( @@ -765,12 +632,9 @@ pub fn search_session_content_through( ); item.insert( "snippet".into(), - Value::from(truncate_text(&snippet, 900).0), - ); - item.insert( - "complete".into(), - Value::Bool(start == 0 && end == text_chars), + Value::from(truncate_text(&window.snippet, 900).0), ); + item.insert("complete".into(), Value::Bool(window.complete)); if inert { item.insert("inert".into(), Value::Bool(true)); } @@ -781,16 +645,7 @@ pub fn search_session_content_through( let has_more = next_message.is_some(); - let mut result = Map::new(); - result.insert("tool".into(), Value::from(tool)); - result.insert("ref".into(), Value::from(opaque_ref)); - result.insert( - "session_id".into(), - Value::from(record_session_id(&record.row, Some(&session.source_id))), - ); - result.insert("revision".into(), Value::from(record.revision.as_str())); - result.insert("message_count".into(), Value::from(session.messages.len())); - result.insert("turn_count".into(), Value::from(total_turns)); + let mut result = ReadMetadata::new(tool, opaque_ref, &record, &session).search(); if inert { result.insert("inert".into(), Value::Bool(true)); } diff --git a/crates/ferry-engine/src/sessions/block_fragment.rs b/crates/ferry-engine/src/sessions/block_fragment.rs new file mode 100644 index 0000000..330b729 --- /dev/null +++ b/crates/ferry-engine/src/sessions/block_fragment.rs @@ -0,0 +1,77 @@ +//! 按完整响应的编码大小选择 UTF-8 block 分片。 +use super::read_cursor::Position; +use super::safety::python_json_len; +use serde_json::{json, Value}; + +pub(super) struct BlockFragment<'a> { + block: &'a Value, + encoded: String, +} + +impl<'a> BlockFragment<'a> { + pub fn new(block: &'a Value) -> Self { + Self { + block, + encoded: serde_json::to_string(block).expect("Value 可编码"), + } + } + + pub fn len(&self) -> usize { + self.encoded.len() + } + + pub fn valid_offset(&self, offset: usize) -> bool { + offset < self.len() && self.encoded.is_char_boundary(offset) + } + + pub fn value(&self, offset: usize, end: usize) -> Value { + json!({"kind": "fragment", "block": self.block["block"], + "fragment": {"encoding": "json", "offset_bytes": offset, + "total_bytes": self.len(), "text": &self.encoded[offset..end], "complete": end == self.len()}}) + } + + pub fn next_position(&self, position: Position, end: usize, next_block: Position) -> Position { + if end == self.len() { + next_block + } else { + Position { + offset: end, + ..position + } + } + } + + pub fn fitting_end( + &self, + position: Position, + budget: usize, + next_block: Position, + candidate: impl Fn(Value, Position) -> Value, + ) -> usize { + let mut low = position.offset; + let mut high = self.len(); + while low < high { + let mut end = low + (high - low).div_ceil(2); + while end > low && !self.encoded.is_char_boundary(end) { + end -= 1; + } + if end == low { + end = self.encoded[low..] + .char_indices() + .nth(1) + .map(|(index, _)| low + index) + .unwrap_or(self.len()); + if end > high { + break; + } + } + let next = self.next_position(position, end, next_block); + if python_json_len(&candidate(self.value(position.offset, end), next)) <= budget { + low = end; + } else { + high = end - 1; + } + } + low + } +} diff --git a/crates/ferry-engine/src/sessions/branch_point.rs b/crates/ferry-engine/src/sessions/branch_point.rs index 70c33de..718c68b 100644 --- a/crates/ferry-engine/src/sessions/branch_point.rs +++ b/crates/ferry-engine/src/sessions/branch_point.rs @@ -4,9 +4,9 @@ use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _}; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; -use super::read_cursor::digest; +use super::session_view::{clear_derived_metadata, SessionView}; use crate::errors::{DomainError, DomainResult}; -use crate::model::{native_locator, BlockKind, Session}; +use crate::model::{native_locator, Session}; #[derive(Debug, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -25,63 +25,14 @@ fn invalid(reason: &str) -> DomainError { error } -fn visible(session: &Session, message: &crate::model::Message) -> bool { - !session - .context_compactions - .iter() - .any(|c| c.summary_message_id.is_some() && c.summary_message_id == message.source_id) -} - /// End is exclusive. Internal compaction messages do not create UI turns. pub fn complete_end(session: &Session, start: usize) -> Option { - let first = session.messages.get(start)?; - if first.role != "user" || !visible(session, first) { - return None; - } - let next = session - .messages - .iter() - .enumerate() - .skip(start + 1) - .find(|(_, m)| m.role == "user" && visible(session, m)) - .map_or(session.messages.len(), |(i, _)| i); - let (last_index, last) = session.messages[..next] - .iter() - .enumerate() - .skip(start + 1) - .rev() - .find(|(_, m)| visible(session, m))?; - (last.role == "assistant" - && last.turn_complete == Some(true) - && last - .blocks - .iter() - .any(|b| b.kind == BlockKind::Text && !b.text.trim().is_empty())) - .then_some(last_index + 1) + SessionView::new(session).complete_end(start) } /// Match show()'s visible-message pagination without issuing locators for unloaded turns. pub fn completed_on_page(session: &Session, from: usize, to: usize) -> Vec { - let mut count = 0; - let positions: Vec = session - .messages - .iter() - .map(|m| { - if visible(session, m) { - count += 1; - } - count - }) - .collect(); - session - .messages - .iter() - .enumerate() - .filter_map(|(i, _)| { - let end = complete_end(session, i)?; - (positions[end - 1] >= from && positions[end - 1] <= to).then_some(i) - }) - .collect() + SessionView::new(session).completed_on_page(from, to) } impl BranchPoint { @@ -127,9 +78,8 @@ impl BranchPoint { { return Err(fail()); } - let start = session.messages[..point.end] - .iter() - .rposition(|m| m.role == "user" && visible(session, m)) + let start = SessionView::new(session) + .latest_user_before(point.end) .ok_or_else(fail)?; if complete_end(session, start) != Some(point.end) { return Err(fail()); @@ -141,16 +91,12 @@ impl BranchPoint { let last = &session.messages[self.end - 1]; json!({"tool": self.tool, "sessionId": self.session, "cwd": session.cwd, "sourceRef": reference, "lastMessageId": last.source_id, - "turn": session.messages[..self.end].iter().filter(|m| m.role == "user" && visible(session, m)).count()}) + "turn": SessionView::new(session).visible_turns(self.end)}) } } fn prefix_hash(session: &Session, end: usize) -> String { - digest(&json!([ - session.source_tool, - session.source_id, - &session.messages[..end] - ])) + SessionView::new(session).prefix_digest(end) } /// Drop every derived whole-session object as well: child transcripts and later summaries @@ -159,11 +105,7 @@ pub fn restrict(session: &mut Session, token: Option<&str>) -> DomainResult<()> if let Some(token) = token { let point = BranchPoint::resolve(token, session)?; session.messages.truncate(point.end); - session.children.clear(); - session.agent_edges.clear(); - session.context_compactions.clear(); - session.loss.clear(); - session.title = "Bounded conversation".into(); + clear_derived_metadata(session); } Ok(()) } diff --git a/crates/ferry-engine/src/sessions/mod.rs b/crates/ferry-engine/src/sessions/mod.rs index 05e12a7..6a52f84 100644 --- a/crates/ferry-engine/src/sessions/mod.rs +++ b/crates/ferry-engine/src/sessions/mod.rs @@ -1,12 +1,15 @@ //! 会话索引与读取能力包。 pub mod agent_read; +mod block_fragment; pub mod content_index; pub mod index; pub mod inert; pub mod live; pub mod read; mod read_cursor; +mod read_metadata; +mod read_query; pub mod reasoning; pub mod regex_search; pub mod safety; @@ -14,6 +17,8 @@ pub mod scan; pub mod scan_cache; pub mod scan_progress; pub mod search; +mod session_view; +mod text_window; pub mod title_evidence; pub mod title_style; pub mod topology; diff --git a/crates/ferry-engine/src/sessions/read_metadata.rs b/crates/ferry-engine/src/sessions/read_metadata.rs new file mode 100644 index 0000000..f2dea3b --- /dev/null +++ b/crates/ferry-engine/src/sessions/read_metadata.rs @@ -0,0 +1,66 @@ +//! 上下文与搜索共用的会话元数据。 +use super::index::IndexedSession; +use super::safety::{record_session_id, truncate_text}; +use super::session_view::raw_turns_before; +use crate::model::Session; +use serde_json::{json, Value}; + +pub(super) struct ReadMetadata<'a> { + tool: &'a str, + reference: &'a str, + record: &'a IndexedSession, + session: &'a Session, +} + +impl<'a> ReadMetadata<'a> { + pub fn new( + tool: &'a str, + reference: &'a str, + record: &'a IndexedSession, + session: &'a Session, + ) -> Self { + Self { + tool, + reference, + record, + session, + } + } + + pub fn context(&self, inert: bool) -> Value { + let title_source = if inert { + super::inert::strip_text(&self.session.title) + } else { + self.session.title.clone() + }; + let (title, title_truncated) = truncate_text(&title_source, 200); + let (project, project_truncated) = truncate_text(&self.session.cwd, 1024); + json!({"tool": self.tool, "ref": self.reference, + "session_id": record_session_id(&self.record.row, Some(&self.session.source_id)), + "revision": self.record.revision, "title": title, "project": project, + "title_source": self.record.row.get("title_source").and_then(Value::as_str).unwrap_or(""), + "title_truncated": title_truncated, "project_truncated": project_truncated, + "message_count": self.session.messages.len(), + "turn_count": raw_turns_before(self.session, self.session.messages.len()), "mode": "context"}) + } + + pub fn search(&self) -> serde_json::Map { + let mut result = serde_json::Map::new(); + result.insert("tool".into(), json!(self.tool)); + result.insert("ref".into(), json!(self.reference)); + result.insert( + "session_id".into(), + json!(record_session_id( + &self.record.row, + Some(&self.session.source_id) + )), + ); + result.insert("revision".into(), json!(self.record.revision)); + result.insert("message_count".into(), json!(self.session.messages.len())); + result.insert( + "turn_count".into(), + json!(raw_turns_before(self.session, self.session.messages.len())), + ); + result + } +} diff --git a/crates/ferry-engine/src/sessions/read_query.rs b/crates/ferry-engine/src/sessions/read_query.rs new file mode 100644 index 0000000..9fc034f --- /dev/null +++ b/crates/ferry-engine/src/sessions/read_query.rs @@ -0,0 +1,113 @@ +//! 会话读取参数及其游标身份。页大小和字节预算允许在续读时调整。 +use super::read_cursor::digest; +use super::safety::{bounded_int, MAX_AGENT_DTO_BYTES}; +use crate::errors::DomainResult; +use serde_json::{json, Value}; + +pub(super) const MAX_RESULTS: i64 = 50; +pub(super) const MAX_MESSAGES: i64 = 50; +pub(super) const MAX_BYTES: i64 = 64 * 1024; +pub(super) const DEFAULT_BYTES: i64 = 24 * 1024; + +pub(super) struct ContextOptions { + pub first: usize, + pub count: usize, + pub budget: usize, +} + +impl ContextOptions { + pub fn parse( + from: Option<&Value>, + limit: Option<&Value>, + bytes: Option<&Value>, + ) -> DomainResult { + Ok(Self { + first: bounded_int(from, 1, 1, 1_000_000, "from_message")? as usize, + count: bounded_int(limit, 20, 1, MAX_MESSAGES, "limit")? as usize, + budget: bounded_int(bytes, DEFAULT_BYTES, 1024, MAX_BYTES, "max_bytes")? as usize, + }) + } +} + +pub(super) struct SearchOptions { + pub count: usize, + pub budget: usize, +} + +impl SearchOptions { + pub fn parse(limit: Option<&Value>, bytes: Option<&Value>) -> DomainResult { + Ok(Self { + count: bounded_int(limit, 20, 1, MAX_RESULTS, "limit")? as usize, + budget: bounded_int( + bytes, + MAX_AGENT_DTO_BYTES as i64, + 1024, + MAX_BYTES, + "max_bytes", + )? as usize, + }) + } +} + +#[derive(Clone, Copy)] +pub(super) struct ReadScope<'a> { + pub tool: &'a str, + pub reference: &'a str, + pub inert: bool, + pub outputs: bool, + pub through: Option<&'a str>, +} + +impl ReadScope<'_> { + pub fn context_binding(&self, first: usize) -> String { + digest(&json!([ + "context", + self.tool, + self.reference, + first, + self.outputs, + self.through + ])) + } + + pub fn search_binding(&self, terms: &[String], roles: &[String]) -> String { + digest(&json!([ + "search", + self.tool, + self.reference, + terms, + roles, + self.inert, + self.outputs, + self.through + ])) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn context_defaults_and_adjustable_page_sizes() { + let defaults = ContextOptions::parse(None, None, None).unwrap(); + assert_eq!( + (defaults.first, defaults.count, defaults.budget), + (1, 20, 24576) + ); + let adjusted = + ContextOptions::parse(Some(&json!(3)), Some(&json!(50)), Some(&json!(4096))).unwrap(); + assert_eq!( + (adjusted.first, adjusted.count, adjusted.budget), + (3, 50, 4096) + ); + } + + #[test] + fn options_reject_invalid_types() { + for value in [json!("4096"), json!(true), json!(-1), json!(65537)] { + assert!(ContextOptions::parse(None, None, Some(&value)).is_err()); + assert!(SearchOptions::parse(None, Some(&value)).is_err()); + } + } +} diff --git a/crates/ferry-engine/src/sessions/session_view.rs b/crates/ferry-engine/src/sessions/session_view.rs new file mode 100644 index 0000000..ffbfe7a --- /dev/null +++ b/crates/ferry-engine/src/sessions/session_view.rs @@ -0,0 +1,140 @@ +//! 原始消息、可见轮次与页面序号之间的映射。 +use super::read_cursor::digest; +use crate::model::{BlockKind, Message, Session}; +use serde_json::{json, Value}; +use std::collections::HashSet; + +pub(super) struct SessionView<'a> { + session: &'a Session, + summaries: HashSet<&'a str>, +} + +impl<'a> SessionView<'a> { + pub fn new(session: &'a Session) -> Self { + let summaries = session + .context_compactions + .iter() + .filter_map(|entry| entry.summary_message_id.as_deref()) + .collect(); + Self { session, summaries } + } + + pub fn visible(&self, message: &Message) -> bool { + message + .source_id + .as_deref() + .is_none_or(|id| !self.summaries.contains(id)) + } + + pub fn complete_end(&self, start: usize) -> Option { + let first = self.session.messages.get(start)?; + if first.role != "user" || !self.visible(first) { + return None; + } + let next = self + .session + .messages + .iter() + .enumerate() + .skip(start + 1) + .find(|(_, message)| message.role == "user" && self.visible(message)) + .map_or(self.session.messages.len(), |(index, _)| index); + let (index, last) = self.session.messages[..next] + .iter() + .enumerate() + .skip(start + 1) + .rev() + .find(|(_, message)| self.visible(message))?; + (last.role == "assistant" + && last + .turn_complete + .unwrap_or_else(|| last.source_id.as_deref().is_some_and(|id| !id.is_empty())) + && last + .blocks + .iter() + .any(|block| block.kind == BlockKind::Text && !block.text.trim().is_empty())) + .then_some(index + 1) + } + + pub fn completed_on_page(&self, from: usize, to: usize) -> Vec { + let mut position = 0; + let positions: Vec<_> = self + .session + .messages + .iter() + .map(|message| { + position += usize::from(self.visible(message)); + position + }) + .collect(); + self.session + .messages + .iter() + .enumerate() + .filter_map(|(index, _)| { + let end = self.complete_end(index)?; + (positions[end - 1] >= from && positions[end - 1] <= to).then_some(index) + }) + .collect() + } + + pub fn latest_user_before(&self, end: usize) -> Option { + self.session.messages[..end] + .iter() + .rposition(|message| message.role == "user" && self.visible(message)) + } + + pub fn visible_turns(&self, end: usize) -> usize { + self.session.messages[..end] + .iter() + .filter(|message| message.role == "user" && self.visible(message)) + .count() + } + + fn checkpoint_messages(&self, end: usize) -> Vec { + self.session.messages[..end] + .iter() + .map(|message| { + let mut projected = message.clone(); + projected + .blocks + .retain(|block| block.kind != BlockKind::Tool); + projected + }) + .collect() + } + + pub fn prefix_digest(&self, end: usize) -> String { + digest(&json!([ + self.session.source_tool, + self.session.source_id, + self.checkpoint_messages(end) + ])) + } +} + +pub(super) fn content_snapshot(revision: &str, session: &Session) -> String { + digest(&json!([revision, session])) +} + +pub(super) fn raw_turns_before(session: &Session, end: usize) -> usize { + session + .messages + .iter() + .take(end) + .filter(|message| message.role == "user") + .count() +} + +pub(super) fn clear_derived_metadata(session: &mut Session) { + session.children.clear(); + session.agent_edges.clear(); + session.context_compactions.clear(); + session.loss.clear(); + session.title = "Bounded conversation".into(); +} + +pub(super) fn message_identity(message: &Message) -> Value { + json!({"origin": super::inert::message_origin(message), + "duplicate_key": super::inert::duplicate_key(message)}) +} diff --git a/crates/ferry-engine/src/sessions/text_window.rs b/crates/ferry-engine/src/sessions/text_window.rs new file mode 100644 index 0000000..47d6dd3 --- /dev/null +++ b/crates/ferry-engine/src/sessions/text_window.rs @@ -0,0 +1,63 @@ +//! 搜索摘要与字符索引,保留原始文本用于展示。 +use crate::model::{tool_result_text, BlockKind, Message}; + +pub(super) fn searchable_text(message: &Message, outputs: bool, inert: bool) -> String { + let mut parts = Vec::new(); + for block in &message.blocks { + if block.kind == BlockKind::Text && !block.text.is_empty() { + let text = if inert { + super::inert::strip_text(&block.text) + } else { + block.text.clone() + }; + if !text.is_empty() { + parts.push(text); + } + } else if outputs && block.kind == BlockKind::Tool { + let Some(call) = block.tool.as_ref() else { + continue; + }; + parts.push(format!("[tool {}]", call.name)); + let output = tool_result_text(call.result.as_ref()); + if !output.is_empty() { + parts.push(output); + } + } + } + parts.join("\n") +} + +pub fn char_slice(text: &str, start: usize, end: usize) -> String { + text.chars() + .skip(start) + .take(end.saturating_sub(start)) + .collect() +} + +pub fn char_find(haystack: &str, needle: &str) -> Option { + haystack + .find(needle) + .map(|byte| haystack[..byte].encode_utf16().count()) +} + +pub(super) struct TextWindow { + pub snippet: String, + pub complete: bool, +} + +impl TextWindow { + pub fn around(text: &str, first_hit: usize) -> Self { + let count = text.chars().count(); + let start = first_hit.saturating_sub(240); + let end = count.min(first_hit + 560); + Self { + snippet: format!( + "{}{}{}", + if start > 0 { "…" } else { "" }, + char_slice(text, start, end), + if end < count { "…" } else { "" } + ), + complete: start == 0 && end == count, + } + } +}