use axum::http::StatusCode;
use crate::session_manager::{ManagedSessionId, SessionRecord};
use super::SessionSummary;
pub fn record_to_json(r: &SessionRecord) -> serde_json::Value {
serde_json::json!({
"id": r.id.to_string(),
"name": r.tmux_name,
"state": r.state.to_string(),
"workspace_path": r.workspace_path.as_ref().map(|p| p.to_string_lossy().to_string()),
"cwd": r.cwd.to_string_lossy().to_string(),
"task": r.task,
"repo_url": r.repo_url,
"branch": r.branch,
"created_at": r.created_at.to_rfc3339(),
"last_activity_at": r.last_activity_at.map(|t| t.to_rfc3339()),
"attach_cmd": attach_cmd_for(&r.tmux_name),
"runtime": r.runtime.as_str(),
"pending_decision": r.pending_decision,
"proposed_default": r.proposed_default,
"source_id": r.source_id,
})
}
pub(super) fn record_to_summary(r: &SessionRecord) -> SessionSummary {
SessionSummary {
id: r.id.to_string(),
name: r.tmux_name.clone(),
state: r.state.to_string(),
workspace_path: r
.workspace_path
.as_ref()
.map(|p| p.to_string_lossy().to_string()),
repo_url: r.repo_url.clone(),
branch: r.branch.clone(),
created_at: r.created_at.to_rfc3339(),
last_activity_at: r.last_activity_at.map(|t| t.to_rfc3339()),
pending_decision: r.pending_decision.clone(),
proposed_default: r.proposed_default.clone(),
source_id: r.source_id.clone(),
task: Some(r.task.clone()),
cwd: Some(r.cwd.to_string_lossy().to_string()),
claude_session_id: r.claude_session_id.clone(),
}
}
pub(super) fn attach_cmd_for(tmux_name: &str) -> String {
format!("tmux attach-session -t {tmux_name}")
}
pub(super) fn parse_id(id_str: &str) -> Result<ManagedSessionId, (StatusCode, String)> {
id_str
.parse::<uuid::Uuid>()
.map(ManagedSessionId::from)
.map_err(|_| {
(
StatusCode::BAD_REQUEST,
format!("invalid session id: {id_str}"),
)
})
}