pub mod cli_continue;
pub mod cli_search;
pub mod handoff;
pub mod hits;
pub mod ids;
pub mod links;
pub mod message;
pub mod projection;
pub mod rank;
pub mod recall;
pub mod rows;
pub mod search;
pub mod sessions;
pub mod views;
use std::io::{self, Write};
use serde_json::{Value, json};
use self::message::Message;
use crate::repr::repr;
use crate::store::error::{Error as StoreError, MemoryError};
use crate::store::time::Stamp;
pub const FORMAT: u64 = 1;
#[derive(Debug)]
pub struct Turn {
pub conversation_id: String,
pub turn: u64,
pub messages: Vec<Message>,
pub created_at: Stamp,
pub actor: Option<String>,
pub redacted: bool,
}
#[derive(Debug)]
pub struct Projection {
pub turn: u64,
pub kind: String,
pub text: String,
pub span: u64,
}
#[derive(Debug)]
pub struct Redaction {
pub conversation_id: String,
pub turns: Vec<u64>,
pub actor: Option<String>,
pub created_at: Stamp,
}
#[derive(Debug)]
pub struct Refused {
pub code: &'static str,
pub message: String,
pub details: Value,
}
impl Refused {
pub fn id_invalid(conversation_id: &str, reason: &str) -> Self {
Self {
code: "agent_conversation_id_invalid",
message: format!(
"Invalid conversation id {}: {reason}",
repr(conversation_id)
),
details: json!({"conversation_id": conversation_id, "reason": reason}),
}
}
pub fn format_unsupported(conversation_id: &str, reason: &str) -> Self {
Self {
code: "agent_conversation_format_unsupported",
message: format!(
"Unsupported turn format in conversation {}: {reason}",
repr(conversation_id)
),
details: json!({"conversation_id": conversation_id, "reason": reason}),
}
}
pub fn conflict(conversation_id: &str, reason: &str) -> Self {
Self {
code: "agent_conversation_conflict",
message: format!(
"Conversation conflict on {}: {reason}",
repr(conversation_id)
),
details: json!({"conversation_id": conversation_id, "reason": reason}),
}
}
}
#[derive(Debug)]
pub enum Error {
Conversation(Refused),
Memory(MemoryError),
Value(String),
Io(io::Error),
}
impl Error {
pub fn text(&self) -> String {
match self {
Self::Conversation(refused) => format!("[{}] {}", refused.code, refused.message),
Self::Memory(error) => format!("[{}] {}", error.code, error.message),
Self::Value(sentence) => sentence.clone(),
Self::Io(error) => error.to_string(),
}
}
}
impl From<Refused> for Error {
fn from(refused: Refused) -> Self {
Self::Conversation(refused)
}
}
impl From<MemoryError> for Error {
fn from(error: MemoryError) -> Self {
Self::Memory(error)
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Self::Io(error)
}
}
impl From<StoreError> for Error {
fn from(error: StoreError) -> Self {
match error {
StoreError::Memory(error) => Self::Memory(error),
StoreError::Value(sentence) => Self::Value(sentence),
StoreError::Io(error) => Self::Io(error),
}
}
}
pub fn failed(
text: &str,
hint: Option<&str>,
json: bool,
out: &mut impl Write,
err: &mut impl Write,
) -> io::Result<u8> {
if json {
writeln!(out, "{}", json!({"error": text, "hint": hint}))?;
}
writeln!(err, "error: {text}")?;
if let Some(hint) = hint {
writeln!(err, "hint: {hint}")?;
}
Ok(1)
}