Skip to main content

starweaver_runtime/agent/
overrides.rs

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