use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use tokio_util::codec::LengthDelimitedCodec;
const MAX_FRAME_LENGTH: usize = 256 * 1024 * 1024;
pub fn length_delimited_codec() -> LengthDelimitedCodec {
LengthDelimitedCodec::builder()
.max_frame_length(MAX_FRAME_LENGTH)
.new_codec()
}
pub fn socket_path() -> PathBuf {
dirs::home_dir()
.expect("could not determine home directory")
.join(".infinity")
.join("daemon.sock")
}
pub fn pid_path() -> PathBuf {
dirs::home_dir()
.expect("could not determine home directory")
.join(".infinity")
.join("daemon.pid")
}
pub fn state_dir() -> PathBuf {
dirs::home_dir()
.expect("could not determine home directory")
.join(".infinity")
}
pub fn remotes_config_path() -> PathBuf {
dirs::home_dir()
.expect("could not determine home directory")
.join(".infinity")
.join("remotes.json")
}
fn default_true() -> bool {
true
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClientMessage {
CreateSession {
cwd: PathBuf,
#[serde(default)]
location: Option<String>,
#[serde(default)]
model: Option<ModelRef>,
#[serde(default = "default_true")]
keeps_session_alive: bool,
},
Connect {
session_id: String,
thread_id: Option<String>,
#[serde(default = "default_true")]
keeps_session_alive: bool,
},
UserInput {
session_id: String,
text: String,
},
Disconnect,
SoftDetach {
session_id: String,
},
ShutdownSession {
session_id: String,
},
ArchiveSession {
session_id: String,
},
SwitchModel {
session_id: String,
model: ModelRef,
},
UserChoiceAnswered {
choice_id: String,
selected: usize,
},
TriggerCompaction {
session_id: String,
},
RequestMigrate {
session_id: String,
#[serde(default)]
to: Option<String>,
dest_cwd: PathBuf,
},
Emigrate {
session_id: String,
dest_rap_urls: HashMap<String, String>,
},
EmigrateDone {
session_id: String,
},
ImportSession {
session_id: String,
cwd: PathBuf,
session_data: String,
},
BootRapServers {
cwd: PathBuf,
},
ListDirectory {
path: String,
#[serde(default)]
on: Option<String>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DaemonMessage {
Connected {
session_id: String,
thread_id: String,
model_name: String,
context_window: usize,
title: Option<String>,
total_tokens_used: usize,
#[serde(default)]
provider_id: String,
},
StartOutput {
thread_id: Option<String>,
},
TextChunk {
thread_id: Option<String>,
chunk: String,
},
ToolCall {
name: String,
args: String,
thread_id: Option<String>,
display_as: Option<String>,
},
ToolResult {
segments: Vec<rap_protocol::DisplaySegment>,
thread_id: Option<String>,
},
Info {
thread_id: Option<String>,
text: String,
},
ResponseDone {
thread_id: Option<String>,
token_usage: Option<TokenUsage>,
},
UserInputEcho {
thread_id: Option<String>,
text: String,
},
SubscriptionEvent {
name: String,
text: String,
thread_id: Option<String>,
},
OAuthRequired {
thread_id: Option<String>,
auth_url: String,
},
UserChoiceRequired {
thread_id: Option<String>,
id: String,
prompt: String,
choices: Vec<String>,
default: usize,
},
UserChoiceComplete {
choice_id: String,
},
ThinkingStart {
thread_id: Option<String>,
},
ThinkingEnd {
thread_id: Option<String>,
},
ThinkingChunk {
thread_id: Option<String>,
chunk: String,
},
CompactionApplied {
thread_id: Option<String>,
},
ModelSwitched {
thread_id: String,
model_name: String,
context_window: usize,
provider_id: String,
},
Error {
thread_id: Option<String>,
text: String,
},
ViewUpdate {
thread_id: Option<String>,
view_type: String,
content: serde_json::Value,
},
Replay {
history: Vec<DaemonMessage>,
pending_choices: Vec<DaemonMessage>,
#[serde(default)]
views: HashMap<String, serde_json::Value>,
#[serde(default)]
in_progress: bool,
},
Welcome {
sessions: HashMap<String, SessionInfo>,
available_models: Vec<ModelInfo>,
default_model_name: String,
default_context_window: usize,
provider_name: String,
#[serde(default)]
remotes: Vec<RemoteInfo>,
},
SessionsUpdated {
sessions: HashMap<String, SessionInfo>,
},
RemotesUpdated {
remotes: Vec<RemoteInfo>,
},
DisconnectNotIdle,
DetachedIdle,
EmigrateResult {
session_id: String,
session_data: String,
},
MigrateStarted {
session_id: String,
},
MigrateComplete {
session_id: String,
new_session_id: String,
},
MigrateError {
session_id: String,
error: String,
},
ImportComplete {
session_id: String,
},
RapServersBooted {
server_ports: HashMap<String, u16>,
},
DirectoryListing {
request_path: String,
entries: Vec<String>,
#[serde(default)]
on: Option<String>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SessionStatus {
Running,
Idle,
Stopped,
WaitingForChoice,
Migrating,
Archived,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionInfo {
pub title: Option<String>,
pub last_updated: String,
pub total_tokens_used: usize,
pub status: SessionStatus,
#[serde(default)]
pub threads: Vec<SubthreadInfo>,
#[serde(default)]
pub remote: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubthreadInfo {
pub thread_id: String,
pub parent_thread_id: String,
pub title: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenUsage {
pub input_tokens: Option<u64>,
pub output_tokens: Option<u64>,
#[serde(default)]
pub total_tokens: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfo {
pub display_name: String,
#[serde(default)]
pub provider_id: String,
pub model_id: String,
pub context_window: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ModelRef {
pub provider_id: String,
pub model_id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoteInfo {
pub name: String,
pub status: String,
}