use crate::memory::embeddings::{Embedder, HashingEmbedder};
use crate::turn::{new_session_id, TurnSummary};
use crate::types::{AgentPhase, Message, Role, Step, Usage};
use regex::Regex;
use rusqlite::{params, Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex, MutexGuard};
use std::time::{SystemTime, UNIX_EPOCH};
pub const SESSION_TITLE_MAX: usize = 60;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AgentState {
#[serde(default = "new_session_id")]
pub session_id: String,
#[serde(default = "initial_phase")]
pub phase: AgentPhase,
#[serde(default)]
pub messages: Vec<Message>,
#[serde(default)]
pub steps: Vec<Step>,
#[serde(default)]
pub usage: Usage,
#[serde(default)]
pub last_turn: Option<TurnSummary>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub open_turn: Option<OpenTurn>,
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct OpenTurn {
pub goal: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub goal_text: Option<String>,
pub messages: usize,
pub steps: u32,
#[serde(serialize_with = "crate::pyjson::python_float")]
pub cost_usd: f64,
pub consecutive_errors: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub skill: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub agent: Option<String>,
}
fn initial_phase() -> AgentPhase {
AgentPhase::Init
}
impl AgentState {
pub fn new() -> Self {
Self {
session_id: new_session_id(),
phase: AgentPhase::Execute,
messages: Vec::new(),
steps: Vec::new(),
usage: Usage::default(),
last_turn: None,
open_turn: None,
}
}
pub fn step_count(&self) -> usize {
self.steps.len()
}
}
impl Default for AgentState {
fn default() -> Self {
Self::new()
}
}
static MENTION_BLOCK: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?m)^--- @.*---[ \t]*$").expect("valid pattern"));
pub fn typed_prefix(text: &str) -> &str {
match MENTION_BLOCK.find(text) {
None => text,
Some(found) => {
let head = &text[..found.start()];
head.split("\n\n").next().unwrap_or_default()
}
}
}
pub fn session_title(state: &AgentState) -> Option<String> {
let collapse = |text: &str| text.split_whitespace().collect::<Vec<_>>().join(" ");
for message in state
.messages
.iter()
.filter(|message| message.role == Role::User)
{
let content = crate::prompt::without_turn_note(&message.content);
let typed = collapse(typed_prefix(content));
let text = if typed.is_empty() {
collapse(content)
} else {
typed
};
if text.is_empty() {
continue;
}
if text.chars().count() <= SESSION_TITLE_MAX {
return Some(text);
}
let head: String = text.chars().take(SESSION_TITLE_MAX - 3).collect();
return Some(format!("{head}..."));
}
None
}
pub const RENAME_TITLE_MAX: usize = 200;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TitleError {
Empty,
}
pub fn clean_title(raw: &str) -> Result<String, TitleError> {
let first = raw.trim().lines().next().unwrap_or_default();
let kept: String = first
.chars()
.filter(|c| !c.is_control() && !is_bidi_format(*c))
.collect();
let capped: String = kept.trim().chars().take(RENAME_TITLE_MAX).collect();
let title = capped.trim_end().to_owned();
if title.is_empty() {
Err(TitleError::Empty)
} else {
Ok(title)
}
}
fn is_bidi_format(c: char) -> bool {
matches!(
c,
'\u{061C}' | '\u{200E}' | '\u{200F}' | '\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}'
)
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{0}")]
pub struct StoreError(pub String);
#[derive(Clone)]
pub struct SessionStore {
connection: Arc<Mutex<Connection>>,
pub(crate) embedder: Arc<dyn Embedder>,
path: PathBuf,
}
impl std::fmt::Debug for SessionStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SessionStore")
.field("path", &self.path)
.field("embedder", &self.embedder.spec())
.finish()
}
}
impl SessionStore {
pub fn open(path: &Path) -> Result<Self, StoreError> {
Self::open_with(path, Arc::new(HashingEmbedder::default()))
}
pub fn open_with(path: &Path, embedder: Arc<dyn Embedder>) -> Result<Self, StoreError> {
let opened = (|| -> Result<Connection, Box<dyn std::error::Error>> {
if path != Path::new(":memory:") {
if let Some(parent) = path
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
{
std::fs::create_dir_all(parent)?;
}
}
let connection = Connection::open(path)?;
connection.execute_batch(SCHEMA)?;
connection.execute_batch(crate::memory::entries::PINS_SCHEMA)?;
Ok(connection)
})();
opened
.map(|connection| Self {
connection: Arc::new(Mutex::new(connection)),
embedder,
path: path.to_owned(),
})
.map_err(|error| {
StoreError(format!(
"Could not open the memory database at {} - {error}. Another agent86 may be \
using it (close it and retry), or point `[memory] path` at another file - \
`agent86 config path` shows which file to edit. `[memory] enabled = false` \
turns memory off entirely.",
path.display()
))
})
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn embedder_spec(&self) -> String {
self.embedder.spec()
}
pub(crate) fn connection(&self) -> MutexGuard<'_, Connection> {
self.connection
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub fn save(&self, state: &AgentState, title: Option<&str>) -> Result<(), StoreError> {
let state_json =
serde_json::to_string(state).map_err(|error| StoreError(error.to_string()))?;
let now = now();
self.connection()
.execute(
"INSERT INTO sessions (session_id, created_at, updated_at, title, state_json)
VALUES (?1, ?2, ?3, ?4, ?5)
ON CONFLICT(session_id) DO UPDATE SET
updated_at = excluded.updated_at,
title = COALESCE(excluded.title, sessions.title),
state_json = excluded.state_json",
params![state.session_id, now, now, title, state_json],
)
.map(drop)
.map_err(|error| StoreError(error.to_string()))
}
pub fn load(&self, session_id: &str) -> Result<Option<AgentState>, StoreError> {
let raw: Option<String> = self
.connection()
.query_row(
"SELECT state_json FROM sessions WHERE session_id = ?1",
params![session_id],
|row| row.get(0),
)
.optional()
.map_err(|error| StoreError(error.to_string()))?;
raw.map(|json| {
serde_json::from_str(&json).map_err(|error| {
StoreError(format!("session {session_id} could not be read: {error}"))
})
})
.transpose()
}
pub fn title(&self, session_id: &str) -> Result<Option<String>, StoreError> {
self.connection()
.query_row(
"SELECT title FROM sessions WHERE session_id = ?1",
params![session_id],
|row| row.get::<_, Option<String>>(0),
)
.optional()
.map(Option::flatten)
.map_err(|error| StoreError(error.to_string()))
}
pub fn set_title(&self, session_id: &str, title: &str) -> Result<bool, StoreError> {
self.connection()
.execute(
"UPDATE sessions SET title = ?2 WHERE session_id = ?1",
params![session_id, title],
)
.map(|changed| changed > 0)
.map_err(|error| StoreError(error.to_string()))
}
pub fn persist(&self, state: &AgentState) -> Result<(), StoreError> {
let title = if self.title(&state.session_id)?.is_none() {
session_title(state)
} else {
None
};
self.save(state, title.as_deref())
}
pub fn recent_sessions(&self, limit: usize) -> Result<Vec<SessionInfo>, StoreError> {
let error = |error: rusqlite::Error| StoreError(error.to_string());
let connection = self.connection();
let mut statement = connection
.prepare(
"SELECT session_id, updated_at, title FROM sessions \
ORDER BY updated_at DESC LIMIT ?1",
)
.map_err(error)?;
let rows = statement
.query_map(params![limit as i64], |row| {
Ok(SessionInfo {
session_id: row.get(0)?,
updated_at: row.get::<_, Option<f64>>(1)?.unwrap_or(0.0),
title: row.get(2)?,
})
})
.map_err(error)?;
rows.collect::<Result<_, _>>().map_err(error)
}
pub fn counts(&self) -> Result<StoreCounts, StoreError> {
let count = |table: &str| {
self.connection()
.query_row(&format!("SELECT COUNT(*) FROM {table}"), [], |row| {
row.get::<_, i64>(0)
})
.map_err(|error| StoreError(error.to_string()))
};
Ok(StoreCounts {
sessions: count("sessions")?,
episodes: count("episodes")?,
memories: count("memories")?,
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SessionInfo {
pub session_id: String,
pub title: Option<String>,
pub updated_at: f64,
}
impl SessionInfo {
pub fn label(&self) -> &str {
self.title.as_deref().unwrap_or("(untitled)")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StoreCounts {
pub sessions: i64,
pub episodes: i64,
pub memories: i64,
}
const SCHEMA: &str = "
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
created_at REAL NOT NULL,
updated_at REAL NOT NULL,
title TEXT,
state_json TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS episodes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT,
ts REAL NOT NULL,
task TEXT NOT NULL,
outcome TEXT NOT NULL,
embedding BLOB,
metadata_json TEXT
);
CREATE TABLE IF NOT EXISTS memories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts REAL NOT NULL,
kind TEXT NOT NULL DEFAULT 'fact',
text TEXT NOT NULL,
embedding BLOB,
metadata_json TEXT
);
";
fn now() -> f64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0.0, |elapsed| elapsed.as_secs_f64())
}
pub fn expand_home(path: &str) -> PathBuf {
let home = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
match (path.strip_prefix('~'), std::env::var_os(home)) {
(Some(rest), Some(home)) if rest.is_empty() || rest.starts_with(['/', '\\']) => {
let mut expanded = PathBuf::from(home);
for part in rest.split(['/', '\\']).filter(|part| !part.is_empty()) {
expanded.push(part);
}
expanded
}
_ => PathBuf::from(path),
}
}