Skip to main content

starweaver_runtime/agent/
overrides.rs

1//! Scoped agent override builder.
2
3use std::sync::Arc;
4
5use starweaver_model::{ModelAdapter, ModelRequestParameters, ModelSettings};
6use starweaver_tools::{DynTool, DynToolset, ToolRegistry};
7
8use starweaver_usage::UsageLimits;
9
10use crate::{
11    agent::{Agent, AgentRuntimePolicy},
12    capability::{AgentCapability, CapabilityBundle},
13    executor::DynAgentExecutor,
14    instructions::DynDynamicInstruction,
15    output::{DynOutputFunction, OutputPolicy, OutputSchema, OutputValidator},
16};
17
18/// Scoped agent override builder.
19pub struct AgentOverride {
20    agent: Agent,
21}
22
23impl AgentOverride {
24    pub(super) const fn new(agent: Agent) -> Self {
25        Self { agent }
26    }
27
28    /// Override the model adapter.
29    #[must_use]
30    pub fn model(mut self, model: Arc<dyn ModelAdapter>) -> Self {
31        self.agent.model = model;
32        self
33    }
34
35    /// Override model settings.
36    #[must_use]
37    pub fn model_settings(mut self, settings: Option<ModelSettings>) -> Self {
38        self.agent.model_settings = settings;
39        self
40    }
41
42    /// Override request parameters.
43    #[must_use]
44    pub fn request_params(mut self, params: ModelRequestParameters) -> Self {
45        self.agent.request_params = params;
46        self
47    }
48
49    /// Override runtime tools.
50    #[must_use]
51    pub fn with_tools(mut self, tools: ToolRegistry) -> Self {
52        self.agent.tools = tools;
53        self.agent.toolsets.clear();
54        self
55    }
56
57    /// Add one runtime tool to the overridden agent clone.
58    #[must_use]
59    pub fn tool(mut self, tool: DynTool) -> Self {
60        self.agent.tools.insert(tool);
61        self
62    }
63
64    /// Add one runtime toolset to the overridden agent clone.
65    #[must_use]
66    pub fn toolset(mut self, toolset: &DynToolset) -> Self {
67        self.agent.toolsets.push(toolset.clone());
68        self
69    }
70
71    /// Merge runtime tools from another registry into the overridden agent clone.
72    #[must_use]
73    pub fn append_tools(mut self, tools: &ToolRegistry) -> Self {
74        self.agent.tools.insert_registry(tools);
75        self
76    }
77
78    /// Override usage limits.
79    #[must_use]
80    pub const fn usage_limits(mut self, limits: Option<UsageLimits>) -> Self {
81        self.agent.usage_limits = limits;
82        self
83    }
84
85    /// Override static instructions.
86    #[must_use]
87    pub fn with_instructions(mut self, instructions: Vec<String>) -> Self {
88        self.agent.instructions = instructions;
89        self
90    }
91
92    /// Append static instructions to the overridden agent clone.
93    #[must_use]
94    pub fn append_instructions(mut self, instructions: impl IntoIterator<Item = String>) -> Self {
95        self.agent.instructions.extend(instructions);
96        self
97    }
98
99    /// Override dynamic instructions.
100    #[must_use]
101    pub fn dynamic_instructions(mut self, instructions: Vec<DynDynamicInstruction>) -> Self {
102        self.agent.dynamic_instructions = instructions;
103        self
104    }
105
106    /// Override structured output schema.
107    #[must_use]
108    pub fn output_schema(mut self, schema: Option<OutputSchema>) -> Self {
109        self.agent.output_schema = schema;
110        self
111    }
112
113    /// Apply a complete output policy to the overridden agent clone.
114    #[must_use]
115    pub fn output_policy(mut self, policy: OutputPolicy) -> Self {
116        self.agent = self.agent.with_output_policy(policy);
117        self
118    }
119
120    /// Override output validators.
121    #[must_use]
122    pub fn output_validators(mut self, validators: Vec<Arc<dyn OutputValidator>>) -> Self {
123        self.agent.output_validators = validators;
124        self
125    }
126
127    /// Override output functions.
128    #[must_use]
129    pub fn output_functions(mut self, functions: Vec<DynOutputFunction>) -> Self {
130        self.agent.output_functions = functions;
131        self
132    }
133
134    /// Override capabilities.
135    #[must_use]
136    pub fn capabilities(mut self, capabilities: Vec<Arc<dyn AgentCapability>>) -> Self {
137        self.agent.capabilities = capabilities;
138        self
139    }
140
141    /// Override durable executor.
142    #[must_use]
143    pub fn executor(mut self, executor: DynAgentExecutor) -> Self {
144        self.agent.executor = executor;
145        self
146    }
147
148    /// Apply a capability bundle to the overridden agent clone.
149    #[must_use]
150    pub fn capability_bundle(mut self, bundle: &dyn CapabilityBundle) -> Self {
151        self.agent = self.agent.with_capability_bundle(bundle);
152        self
153    }
154
155    /// Override runtime policy.
156    #[must_use]
157    pub const fn policy(mut self, policy: AgentRuntimePolicy) -> Self {
158        self.agent.policy = policy;
159        self
160    }
161
162    /// Build the overridden agent clone.
163    #[must_use]
164    pub fn build(self) -> Agent {
165        self.agent
166    }
167}