use std::sync::Arc;
use autoagents_core::tool::ToolT;
use autoagents_llm::LLMProvider;
use temporalio_client::Client;
use temporalio_sdk::{Worker, WorkerOptions};
use temporalio_sdk_core::CoreRuntime;
use tokio::sync::OnceCell;
use crate::activities::AgentActivities;
use crate::error::AgentError;
use crate::state::ToolSchema;
use crate::tool::{ToolRegistry, ToolRegistryBuilder};
use crate::workflow::AgentWorkflow;
pub(crate) static WORKER_TOOL_CATALOG: OnceCell<Vec<ToolSchema>> = OnceCell::const_new();
pub struct AgentWorkerBuilder {
client: Client,
llm: Option<Arc<dyn LLMProvider>>,
tools: ToolRegistryBuilder,
queue: String,
}
impl AgentWorkerBuilder {
#[must_use]
pub fn new(client: Client) -> Self {
Self {
client,
llm: None,
tools: ToolRegistry::builder(),
queue: "agents".to_string(),
}
}
#[must_use]
pub fn llm(mut self, llm: Arc<dyn LLMProvider>) -> Self {
self.llm = Some(llm);
self
}
#[must_use]
pub fn tool(mut self, tool: Arc<dyn ToolT>) -> Self {
self.tools = self.tools.add(tool);
self
}
#[must_use]
pub fn queue(mut self, queue: impl Into<String>) -> Self {
self.queue = queue.into();
self
}
pub fn build_worker(self, runtime: &CoreRuntime) -> Result<Worker, AgentError> {
let llm = self
.llm
.expect("AgentWorkerBuilder::llm(...) must be called before build_worker()");
let registry = self.tools.build();
let catalog = registry.to_schemas();
let _ = WORKER_TOOL_CATALOG.set(catalog);
let activities = AgentActivities::new(llm, registry);
let opts = WorkerOptions::new(&self.queue)
.register_workflow::<AgentWorkflow>()
.register_activities(activities)
.build();
Worker::new(runtime, self.client, opts)
.map_err(|e| AgentError::Other(format!("worker init: {e}")))
}
}