use futures::{Stream, StreamExt};
use serde_json::Value;
use crate::Result;
use crate::bindings::oino::extension::agents as wa;
use crate::error::parse;
pub struct Agent(wa::Agent);
impl Agent {
pub async fn spawn(spec: &Value) -> Result<Self> {
Ok(Self(wa::Agent::spawn(spec.to_string()).await?))
}
#[must_use]
pub fn id(&self) -> String {
self.0.id()
}
#[must_use]
pub fn session_file(&self) -> Option<String> {
self.0.session_file()
}
pub async fn prompt(&self, text: &str) -> Result<String> {
Ok(self.0.prompt(text.to_owned(), None).await?)
}
pub async fn steer(&self, text: &str) -> Result<()> {
Ok(self.0.steer(text.to_owned()).await?)
}
pub async fn follow_up(&self, text: &str) -> Result<()> {
Ok(self.0.follow_up(text.to_owned()).await?)
}
pub fn abort(&self) {
self.0.abort();
}
pub fn set_thinking(&self, level: &str) -> Result<String> {
Ok(self.0.set_thinking(level)?)
}
#[must_use]
pub fn last_assistant_text(&self) -> Option<String> {
self.0.last_assistant_text()
}
pub async fn wait_idle(&self) -> Result<()> {
Ok(self.0.wait_idle().await?)
}
pub fn usage(&self) -> Result<Value> {
parse(&self.0.usage())
}
pub fn events(&self) -> impl Stream<Item = Value> + use<> {
self.0
.events()
.into_stream()
.map(|t: String| serde_json::from_str(&t).unwrap_or(Value::Null))
}
pub async fn close(self, grace_ms: u32) {
self.0.close(grace_ms).await;
}
}
pub fn list() -> Result<Vec<Value>> {
parse(&wa::list())
}