session: extract Session from memory_search to src/session.rs
Generic session state (session_id, seen set, state directory) doesn't belong in the memory search module. Now at crate root, re-exported from memory_search for backwards compatibility. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
228815d807
commit
a00d52214a
3 changed files with 58 additions and 45 deletions
|
|
@ -8,7 +8,7 @@ use std::collections::HashSet;
|
|||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
|
|
@ -19,49 +19,7 @@ fn now_secs() -> u64 {
|
|||
/// Max bytes per context chunk (hook output limit is ~10K chars)
|
||||
const CHUNK_SIZE: usize = 9000;
|
||||
|
||||
pub struct Session {
|
||||
pub session_id: String,
|
||||
pub transcript_path: String,
|
||||
pub hook_event: String,
|
||||
pub state_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn from_json(input: &str) -> Option<Self> {
|
||||
let state_dir = PathBuf::from("/tmp/claude-memory-search");
|
||||
fs::create_dir_all(&state_dir).ok();
|
||||
|
||||
let json: serde_json::Value = serde_json::from_str(input).ok()?;
|
||||
let session_id = json["session_id"].as_str().unwrap_or("").to_string();
|
||||
if session_id.is_empty() { return None; }
|
||||
let transcript_path = json["transcript_path"].as_str().unwrap_or("").to_string();
|
||||
let hook_event = json["hook_event_name"].as_str().unwrap_or("").to_string();
|
||||
|
||||
Some(Session { session_id, transcript_path, hook_event, state_dir })
|
||||
}
|
||||
|
||||
pub fn path(&self, prefix: &str) -> PathBuf {
|
||||
self.state_dir.join(format!("{}-{}", prefix, self.session_id))
|
||||
}
|
||||
|
||||
/// Load from POC_SESSION_ID environment variable
|
||||
pub fn from_env() -> Option<Self> {
|
||||
let session_id = std::env::var("POC_SESSION_ID").ok()?;
|
||||
if session_id.is_empty() { return None; }
|
||||
let state_dir = PathBuf::from("/tmp/claude-memory-search");
|
||||
Some(Session {
|
||||
session_id,
|
||||
transcript_path: String::new(),
|
||||
hook_event: String::new(),
|
||||
state_dir,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the seen set for this session
|
||||
pub fn seen(&self) -> HashSet<String> {
|
||||
load_seen(&self.state_dir, &self.session_id)
|
||||
}
|
||||
}
|
||||
pub use crate::session::Session;
|
||||
|
||||
/// Run the hook logic on parsed JSON input. Returns output to inject.
|
||||
pub fn run_hook(input: &str) -> String {
|
||||
|
|
@ -148,7 +106,7 @@ fn parse_seen_line(line: &str) -> &str {
|
|||
line.split_once('\t').map(|(_, key)| key).unwrap_or(line)
|
||||
}
|
||||
|
||||
fn load_seen(dir: &Path, session_id: &str) -> HashSet<String> {
|
||||
pub fn load_seen(dir: &Path, session_id: &str) -> HashSet<String> {
|
||||
let path = dir.join(format!("seen-{}", session_id));
|
||||
if path.exists() {
|
||||
fs::read_to_string(&path)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue