use crate::session::SessionStore;
use crate::Result;
pub const DECIDED_BY_USER: &str = "user";
pub const DECIDED_BY_JUDGE: &str = "judge";
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct Pending {
pub call_id: String,
pub tool: String,
pub summary: String,
pub args: String,
}
impl Pending {
pub fn new(
call_id: impl Into<String>,
tool: impl Into<String>,
summary: String,
args: String,
) -> Self {
Self {
call_id: call_id.into(),
tool: tool.into(),
summary,
args,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Decision {
Allow,
Deny(String),
Defer,
}
impl Decision {
pub fn store(&self) -> Option<String> {
match self {
Decision::Allow => Some("allow".to_string()),
Decision::Deny(reason) => Some(format!("deny:{reason}")),
Decision::Defer => None,
}
}
pub fn parse(stored: &str) -> Self {
match stored.strip_prefix("deny:") {
Some(reason) => Decision::Deny(reason.to_string()),
None if stored == "allow" => Decision::Allow,
None => Decision::Deny(format!(
"the stored answer {stored:?} is not one this build understands, so the call was \
not run"
)),
}
}
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct Ruling {
pub at_ms: i64,
pub call_id: String,
pub tool: String,
pub summary: String,
pub decision: Decision,
pub by: String,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct PendingCall {
pub seq: i64,
pub ord: usize,
pub call: Pending,
pub decision: Option<Decision>,
pub decided_ms: Option<i64>,
pub by: Option<String>,
}
impl PendingCall {
pub fn is_waiting(&self) -> bool {
self.decision.is_none()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Waiting {
pub session: String,
pub calls: usize,
}
impl Waiting {
pub fn hint(&self) -> String {
format!(
"{} tool call(s) in {} waiting for an answer; run `eclipse approve {} --all` \
(or `--index N`, `--deny <reason>`) to resolve them",
self.calls, self.session, self.session
)
}
}
pub fn record(store: &SessionStore, session: &str, seq: i64, calls: &[Pending]) -> Result<()> {
store.record_pending(session, seq, calls)
}
pub fn record_at(
store: &SessionStore,
session: &str,
seq: i64,
ord: usize,
calls: &[Pending],
) -> Result<()> {
store.record_pending_at(session, seq, ord, calls)
}
pub fn waiting(store: &SessionStore, session: &str) -> Result<Vec<PendingCall>> {
Ok(store
.pending_calls(session)?
.into_iter()
.filter(PendingCall::is_waiting)
.collect())
}
pub fn answer(
store: &SessionStore,
session: &str,
index: usize,
decision: &Decision,
) -> Result<()> {
let waiting = waiting(store, session)?;
let Some(position) = index.checked_sub(1) else {
return Err(crate::Error::Session(
"call numbers start at 1: there is no call 0 to answer".into(),
));
};
let Some(row) = waiting.get(position) else {
return Err(crate::Error::Session(format!(
"there is no waiting call {index} in {session}: {} waiting",
waiting.len()
)));
};
store.decide_pending(session, row.seq, row.ord, decision, DECIDED_BY_USER)
}
pub fn answer_all(store: &SessionStore, session: &str, decision: &Decision) -> Result<usize> {
let waiting = waiting(store, session)?;
for row in &waiting {
store.decide_pending(session, row.seq, row.ord, decision, DECIDED_BY_USER)?;
}
Ok(waiting.len())
}
pub fn decision_for(
store: &SessionStore,
session: &str,
call_id: &str,
) -> Result<Option<Decision>> {
Ok(store
.pending_calls(session)?
.into_iter()
.find(|row| row.call.call_id == call_id)
.and_then(|row| row.decision))
}
pub struct Recorded<'a> {
store: &'a SessionStore,
session: &'a str,
inner: &'a dyn crate::tools::Approver,
}
impl<'a> Recorded<'a> {
pub fn new(
store: &'a SessionStore,
session: &'a str,
inner: &'a dyn crate::tools::Approver,
) -> Self {
Self {
store,
session,
inner,
}
}
}
#[async_trait::async_trait]
impl crate::tools::Approver for Recorded<'_> {
async fn decide(&self, call: &Pending) -> Decision {
match decision_for(self.store, self.session, &call.call_id) {
Ok(Some(decision)) => decision,
Ok(None) => self.inner.decide(call).await,
Err(_) => Decision::Defer,
}
}
}
#[cfg(test)]
mod tests;