use super::Client;
use crate::{
error::{Error, ErrorCode},
shared::{self, IntoArgs},
types::{CallToolRequestParams, CallToolResponse, TaskMetadata},
};
pub struct TaskBuilder<'a> {
pub(super) client: &'a mut Client,
pub(super) metadata: TaskMetadata,
}
impl std::fmt::Debug for TaskBuilder<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TaskBuilder")
.field("metadata", &self.metadata)
.finish_non_exhaustive()
}
}
impl<'a> TaskBuilder<'a> {
pub fn with_ttl(mut self, ttl: usize) -> Self {
self.metadata.ttl = Some(ttl);
self
}
pub async fn call_tool<N, Args>(self, name: N, args: Args) -> Result<CallToolResponse, Error>
where
N: Into<String>,
Args: IntoArgs,
{
if !self.client.is_server_support_call_tool_with_tasks() {
return Err(Error::new(
ErrorCode::InvalidRequest,
"Server does not support call tool with tasks.",
));
}
let params = CallToolRequestParams {
name: name.into(),
meta: None,
args: args.into_args(),
task: Some(self.metadata),
};
let result = self.client.call_tool_raw(params).await?.into_result()?;
shared::wait_to_completion(self.client, result).await
}
}