PicoBot/src/gateway/scheduled_agent_task_service.rs

60 lines
1.9 KiB
Rust

use crate::agent::AgentError;
use crate::bus::OutboundMessage;
use crate::scheduler::ScheduledAgentTaskOptions;
use super::execution::{AgentExecutionService, ScheduledExecutionRequest};
use super::provider_config_service::ProviderConfigService;
use super::session_lifecycle::SessionLifecycleService;
#[derive(Clone)]
pub(crate) struct ScheduledAgentTaskService {
lifecycle: SessionLifecycleService,
provider_configs: ProviderConfigService,
show_tool_results: bool,
}
impl ScheduledAgentTaskService {
pub(crate) fn new(
lifecycle: SessionLifecycleService,
provider_configs: ProviderConfigService,
show_tool_results: bool,
) -> Self {
Self {
lifecycle,
provider_configs,
show_tool_results,
}
}
pub(crate) async fn run(
&self,
channel_name: &str,
chat_id: &str,
notification_chat_id: Option<&str>,
prompt: &str,
options: ScheduledAgentTaskOptions,
) -> Result<Vec<OutboundMessage>, AgentError> {
let session = self.lifecycle.active_session(channel_name).await?;
let sender_id = options
.sender_id
.clone()
.unwrap_or_else(|| "scheduler".to_string());
let provider_config = self.provider_configs.select(options.agent.as_deref())?;
AgentExecutionService::new(self.show_tool_results)
.prepare_and_execute_scheduled_task(ScheduledExecutionRequest {
session,
channel_name,
chat_id,
notification_chat_id,
prompt,
sender_id: &sender_id,
provider_config,
system_prompt: options.system_prompt.as_deref(),
metadata: &options.metadata,
fresh_session: options.fresh_session,
})
.await
}
}