feat(lancedb): 批B episodes 时间窗读 op(原文层首次可读)
episodes 此前只有 insert / count_rows,零读接口 ⇒ 事件层孤岛。 - lancedb_ops.rs: query_episodes(since, until, limit) 按 created_at 过滤, 只投影事件层需要列,其余 Default(不复制 memories 25 列投影,避免两处漂移) - main.rs: 新增 op "lancedb_query_episodes" - MemoryRecord 加 Default derive(供最小投影) 验收判据(待验):条数对账 ↔ count_rows、created_at 窗过滤正确、疤行逐字节往返。
This commit is contained in:
+50
-1
@@ -21,7 +21,7 @@ use futures::StreamExt;
|
||||
use lancedb::query::{ExecutableQuery, QueryBase, Select};
|
||||
use lancedb::table::NewColumnTransform;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct MemoryRecord {
|
||||
pub id: String, pub agent_id: String, pub namespace: String,
|
||||
pub content: String, pub category: String, pub vector: Vec<f32>,
|
||||
@@ -419,6 +419,55 @@ impl LanceDBOps {
|
||||
}
|
||||
|
||||
/// 查询记忆(支持 min_recall_count 过滤),供 GetSkillCandidates 使用
|
||||
|
||||
/// 2026-11-28 批 B(episodes 读 op):按 created_at 时间窗读原文层。
|
||||
///
|
||||
/// 背景:episodes 只有 insert / count_rows,**零读接口** ⇒ 事件层是孤岛。
|
||||
/// 本函数只投影事件层需要的列(id/agent_id/namespace/content/category/
|
||||
/// created_at/updated_at/source/is_deleted),其余走 Default 零值——
|
||||
/// 不复制 memories 的 25 列投影,避免两处投影漂移。
|
||||
///
|
||||
/// 参数:since/until 为 ISO 字符串(空串 = 该端不设限);limit 上限 5000。
|
||||
pub fn query_episodes(&self, since: &str, until: &str, limit: usize) -> Result<Vec<MemoryRecord>, Box<dyn std::error::Error>> {
|
||||
let limit = limit.clamp(1, 5000);
|
||||
let db = rt().block_on(lancedb::connect(self.data_dir.to_str().unwrap()).execute())?;
|
||||
let tbl = rt().block_on(db.open_table("episodes").execute())?;
|
||||
|
||||
let mut conds: Vec<String> = Vec::new();
|
||||
if !since.is_empty() {
|
||||
conds.push(format!("created_at >= '{}'", since.replace('\'', "''")));
|
||||
}
|
||||
if !until.is_empty() {
|
||||
conds.push(format!("created_at <= '{}'", until.replace('\'', "''")));
|
||||
}
|
||||
let mut q = tbl.query().limit(limit);
|
||||
if !conds.is_empty() {
|
||||
q = q.only_if(&conds.join(" AND "));
|
||||
}
|
||||
let mut results = Box::pin(rt().block_on(q.execute())?);
|
||||
|
||||
let mut records = Vec::new();
|
||||
while let Some(Ok(batch)) = rt().block_on(results.next()) {
|
||||
for i in 0..batch.num_rows() {
|
||||
records.push(MemoryRecord {
|
||||
id: col_str(&batch, i, "id"),
|
||||
agent_id: col_str(&batch, i, "agent_id"),
|
||||
namespace: col_str(&batch, i, "namespace"),
|
||||
content: col_str(&batch, i, "content"),
|
||||
category: col_str(&batch, i, "category"),
|
||||
created_at: col_str(&batch, i, "created_at"),
|
||||
updated_at: col_str(&batch, i, "updated_at"),
|
||||
source: col_str(&batch, i, "source"),
|
||||
is_deleted: col_bool(&batch, i, "is_deleted"),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
}
|
||||
// created_at 升序(时间窗查询的自然期望顺序;字符串 ISO 可直接排)
|
||||
records.sort_by(|a, b| a.created_at.cmp(&b.created_at));
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn query_memories(&self, min_recall: i64, limit: usize) -> Result<Vec<MemoryRecord>, Box<dyn std::error::Error>> {
|
||||
let db = rt().block_on(lancedb::connect(self.data_dir.to_str().unwrap()).execute())?;
|
||||
let tbl = rt().block_on(db.open_table("memories").execute())?;
|
||||
|
||||
@@ -708,6 +708,18 @@ fn handle_client(mut stream: UnixStream, args: &Args, lancedb: LanceDBOps, state
|
||||
}
|
||||
}
|
||||
}
|
||||
"lancedb_query_episodes" => {
|
||||
// 2026-11-28 批 B:原文层(episodes)时间窗读 —— 此前该表只有 insert/count,
|
||||
// 零读接口,事件层是孤岛。since/until 为 ISO 字符串,空串=不设限。
|
||||
let since = msg["since"].as_str().unwrap_or("");
|
||||
let until = msg["until"].as_str().unwrap_or("");
|
||||
let limit = msg["limit"].as_u64().unwrap_or(200) as usize;
|
||||
eprintln!("[lancedb] query_episodes: since={:?} until={:?} limit={}", since, until, limit);
|
||||
match lancedb.query_episodes(since, until, limit) {
|
||||
Ok(r) => send_ok(&mut stream, &serde_json::to_string(&r).unwrap_or_default()),
|
||||
Err(e) => send_error(&mut stream, "lancedb_query_episodes", &e.to_string()),
|
||||
}
|
||||
}
|
||||
"lancedb_query" => {
|
||||
let min_recall = msg["min_recall"].as_i64().unwrap_or(5) as i64;
|
||||
let limit = msg["limit"].as_u64().unwrap_or(20) as usize;
|
||||
|
||||
Reference in New Issue
Block a user