use serde_json::Value;
use super::{OrchestratorBackend, required_str};
const DEFAULT_ACTIVITY_LINES: u32 = 60;
pub async fn try_dispatch<B: OrchestratorBackend>(
backend: &B,
name: &str,
args: &Value,
) -> Option<Result<Value, String>> {
let result = match name {
"session_new" => session_new(backend, args).await,
"session_stop" => match required_str(args, "session_id") {
Ok(id) => backend.session_stop(&id).await,
Err(e) => Err(e),
},
"session_resume" => match required_str(args, "session_id") {
Ok(id) => backend.session_resume(&id).await,
Err(e) => Err(e),
},
"session_decommission" => match required_str(args, "session_id") {
Ok(id) => backend.session_decommission(&id).await,
Err(e) => Err(e),
},
"session_activity" => match required_str(args, "session_id") {
Ok(id) => {
let lines = args
.get("lines")
.and_then(Value::as_u64)
.map(|n| n.min(u32::MAX as u64) as u32)
.unwrap_or(DEFAULT_ACTIVITY_LINES);
backend.session_activity(&id, lines).await
}
Err(e) => Err(e),
},
"session_send" => match (required_str(args, "session_id"), required_str(args, "text")) {
(Ok(id), Ok(text)) => backend.session_send(&id, &text).await,
(Err(e), _) | (_, Err(e)) => Err(e),
},
_ => return None,
};
Some(result)
}
async fn session_new<B: OrchestratorBackend>(backend: &B, args: &Value) -> Result<Value, String> {
let repo_url = required_str(args, "repo_url")?;
let git_ref = required_str(args, "ref")?;
let task = required_str(args, "task")?;
let name_hint = args.get("name_hint").and_then(Value::as_str);
let runtime = args.get("runtime").and_then(Value::as_str);
backend
.session_new(&repo_url, &git_ref, &task, name_hint, runtime)
.await
}