use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use cpa_core::exec::{ExecError, FailureScope};
use super::alias::Reverse;
use super::signals::may_contain;
use crate::rawjson;
const LIMIT: usize = 1024;
pub const NOT_FOUND: &str = "No thread state was found for the requested previous_message_id. Replay the full conversation with thread create to start a new Thread.";
#[derive(Default)]
pub(crate) struct Aliases(Mutex<State>);
#[derive(Default)]
struct State {
entries: HashMap<String, Arc<Reverse>>,
order: VecDeque<String>,
}
impl Aliases {
fn load(&self, key: &str) -> Option<Arc<Reverse>> {
let state = self.0.lock().expect("thread alias store");
state.entries.get(key).cloned()
}
fn save(&self, keys: &[String], aliases: &Arc<Reverse>) {
let mut state = self.0.lock().expect("thread alias store");
for key in keys {
if !state.entries.contains_key(key) {
state.order.push_back(key.clone());
}
state.entries.insert(key.clone(), aliases.clone());
}
while state.order.len() > LIMIT {
if let Some(oldest) = state.order.pop_front() {
state.entries.remove(&oldest);
}
}
}
pub(crate) fn for_continuation(&self, thread: &Thread, body: &str) -> Option<Result<Arc<Reverse>, ExecError>> {
if !thread.continues || thread.previous.is_empty() {
return None;
}
let tools = rawjson::get(body, "tools");
if tools.kind() == gjson::Kind::Array && !tools.array().is_empty() {
return None;
}
Some(self.load(&format!("message:{}", thread.previous)).ok_or_else(not_found))
}
pub(crate) fn remember(&self, thread: &Thread, aliases: &Arc<Reverse>, message_id: &str) {
let keys: Vec<String> = [thread.previous.as_str(), message_id]
.iter()
.filter(|id| !id.is_empty())
.map(|id| format!("message:{id}"))
.collect();
if !keys.is_empty() {
self.save(&keys, aliases);
}
}
}
pub(crate) struct Thread {
continues: bool,
previous: String,
}
impl Thread {
pub(crate) fn of(body: &str) -> Option<Self> {
if !may_contain(body, &["\"thread\""]) {
return None;
}
let kind = rawjson::get(body, "thread.type");
(!kind.str().is_empty()).then(|| Self {
continues: kind.str() == "continue",
previous: rawjson::string(body, "thread.previous_message_id"),
})
}
}
pub(crate) fn present(body: &str) -> bool {
may_contain(body, &["\"thread\""]) && {
let thread = rawjson::get(body, "thread");
thread.exists() && thread.kind() != gjson::Kind::Null
}
}
fn not_found() -> ExecError {
ExecError::local(404, FailureScope::Request, NOT_FOUND)
}
#[cfg(test)]
mod tests {
use super::*;
fn reverse(alias: &str, name: &str) -> Arc<Reverse> {
Arc::new([(alias.to_owned(), name.to_owned())].into_iter().collect())
}
#[test]
fn oldest_turn_is_evicted_past_the_limit() {
let store = Aliases::default();
let create = Thread::of(r#"{"thread":{"type":"create"}}"#).unwrap();
let aliases = reverse("alias", "Read");
store.remember(&create, &aliases, "first-message");
let continuation = r#"{"thread":{"type":"continue","previous_message_id":"first-message"}}"#;
let lookup = |body: &str| store.for_continuation(&Thread::of(body).unwrap(), body);
assert!(Arc::ptr_eq(&lookup(continuation).unwrap().unwrap(), &aliases));
for i in 0..LIMIT {
store.remember(&create, &Arc::default(), &format!("message-{i}"));
}
let error = lookup(continuation).unwrap().unwrap_err();
assert_eq!((error.status, error.scope), (404, FailureScope::Request));
assert_eq!(error.body, NOT_FOUND);
let last = format!(
r#"{{"thread":{{"type":"continue","previous_message_id":"message-{}"}}}}"#,
LIMIT - 1
);
assert!(lookup(&last).unwrap().is_ok());
}
#[test]
fn declared_tools_and_other_threads_take_the_normal_path() {
let store = Aliases::default();
let lookup = |body: &str| Thread::of(body).and_then(|t| store.for_continuation(&t, body));
for body in [
r#"{"thread":{"type":"continue","previous_message_id":"m"},"tools":[{"name":"Read"}]}"#,
r#"{"thread":{"type":"continue","previous_message_id":""}}"#,
r#"{"thread":{"type":"create"}}"#,
r#"{"messages":[{"role":"user","content":"\"thread\""}]}"#,
] {
assert!(lookup(body).is_none(), "{body}");
}
for body in [
r#"{"thread":{"type":"continue","previous_message_id":"m"},"tools":[]}"#,
r#"{"t\u0068read":{"type":"continue","previous_message_id":"m"}}"#,
] {
assert!(lookup(body).is_some(), "{body}");
}
assert!(present(r#"{"t\u0068read":{}}"#) && !present(r#"{"thread":null}"#));
}
}