Skip to content

Commit dee6897

Browse files
committed
Changed: ToolContext::NAME from associated const to method
- Replace `const NAME: &'static str` with `fn name(&self) -> &'static str` in the ToolContext trait - Makes the trait object-safe, enabling `Box<dyn ToolContext>` for custom-tools feature - Update all tool implementations: read, write, edit, glob, grep, bash, webfetch, todo read/write, task - Update SystemPromptBuilder::track() to use tool.name() instead of T::NAME - Update all doc examples and mock tools - Add object-safety test proving Box<dyn ToolContext> construction works - Silence doc lint warning on api_type field in provider-config
1 parent fab32c8 commit dee6897

15 files changed

Lines changed: 125 additions & 36 deletions

File tree

docs/src/guides/custom-framework.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ use reloaded_code_core::{
7373

7474
// Implement ToolContext for your tool
7575
impl<R: PathResolver> ToolContext for MyReadTool<R> {
76-
const NAME: &'static str = tool_metadata::read::NAME;
76+
fn name(&self) -> &'static str {
77+
tool_metadata::read::NAME
78+
}
7779

7880
fn context(&self) -> ToolPrompt {
7981
ToolPrompt::Read {

src/reloaded-code-core/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ impl ReadTool {
162162
}
163163
164164
impl ToolContext for ReadTool {
165-
const NAME: &'static str = tool_metadata::read::NAME;
165+
fn name(&self) -> &'static str {
166+
tool_metadata::read::NAME
167+
}
166168
167169
fn context(&self) -> ToolPrompt {
168170
ToolPrompt::Read {
@@ -196,7 +198,9 @@ impl ReadTool {
196198
}
197199
198200
impl ToolContext for ReadTool {
199-
const NAME: &'static str = tool_metadata::read::NAME;
201+
fn name(&self) -> &'static str {
202+
tool_metadata::read::NAME
203+
}
200204
201205
fn context(&self) -> ToolPrompt {
202206
ToolPrompt::Read {

src/reloaded-code-core/examples/system_prompt/mock_tools.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ macro_rules! path_tool_with_line_numbers {
121121
impl<const ALLOWED: bool, const LINE_NUMBERS: bool> ToolContext
122122
for $tool<ALLOWED, LINE_NUMBERS>
123123
{
124-
const NAME: &'static str = $name;
124+
fn name(&self) -> &'static str {
125+
$name
126+
}
125127

126128
fn context(&self) -> ToolPrompt {
127129
ToolPrompt::$variant {
@@ -138,7 +140,9 @@ macro_rules! path_tool {
138140
struct $tool<const ALLOWED: bool>;
139141

140142
impl<const ALLOWED: bool> ToolContext for $tool<ALLOWED> {
141-
const NAME: &'static str = $name;
143+
fn name(&self) -> &'static str {
144+
$name
145+
}
142146

143147
fn context(&self) -> ToolPrompt {
144148
ToolPrompt::$variant {
@@ -158,7 +162,9 @@ path_tool_with_line_numbers!(MockGrepTool, tool_metadata::grep::NAME, Grep);
158162
struct MockBashTool;
159163

160164
impl ToolContext for MockBashTool {
161-
const NAME: &'static str = tool_metadata::bash::NAME;
165+
fn name(&self) -> &'static str {
166+
tool_metadata::bash::NAME
167+
}
162168

163169
fn context(&self) -> ToolPrompt {
164170
ToolPrompt::Bash {
@@ -171,7 +177,9 @@ impl ToolContext for MockBashTool {
171177
struct MockWebFetchTool;
172178

173179
impl ToolContext for MockWebFetchTool {
174-
const NAME: &'static str = tool_metadata::webfetch::NAME;
180+
fn name(&self) -> &'static str {
181+
tool_metadata::webfetch::NAME
182+
}
175183

176184
fn context(&self) -> ToolPrompt {
177185
ToolPrompt::WebFetch
@@ -181,7 +189,9 @@ impl ToolContext for MockWebFetchTool {
181189
struct MockTodoWriteTool;
182190

183191
impl ToolContext for MockTodoWriteTool {
184-
const NAME: &'static str = tool_metadata::todo_write::NAME;
192+
fn name(&self) -> &'static str {
193+
tool_metadata::todo_write::NAME
194+
}
185195

186196
fn context(&self) -> ToolPrompt {
187197
ToolPrompt::TodoWrite
@@ -191,7 +201,9 @@ impl ToolContext for MockTodoWriteTool {
191201
struct MockTodoReadTool;
192202

193203
impl ToolContext for MockTodoReadTool {
194-
const NAME: &'static str = tool_metadata::todo_read::NAME;
204+
fn name(&self) -> &'static str {
205+
tool_metadata::todo_read::NAME
206+
}
195207

196208
fn context(&self) -> ToolPrompt {
197209
ToolPrompt::TodoRead
@@ -201,7 +213,9 @@ impl ToolContext for MockTodoReadTool {
201213
struct MockTaskTool;
202214

203215
impl ToolContext for MockTaskTool {
204-
const NAME: &'static str = tool_metadata::task::NAME;
216+
fn name(&self) -> &'static str {
217+
tool_metadata::task::NAME
218+
}
205219

206220
fn context(&self) -> ToolPrompt {
207221
ToolPrompt::Task

src/reloaded-code-core/src/context/mod.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
//! struct NotesTool;
1919
//!
2020
//! impl ToolContext for ReadTool {
21-
//! const NAME: &'static str = "read";
21+
//! fn name(&self) -> &'static str {
22+
//! "read"
23+
//! }
2224
//!
2325
//! fn context(&self) -> ToolPrompt {
2426
//! ToolPrompt::Read {
@@ -29,7 +31,9 @@
2931
//! }
3032
//!
3133
//! impl ToolContext for NotesTool {
32-
//! const NAME: &'static str = "notes";
34+
//! fn name(&self) -> &'static str {
35+
//! "notes"
36+
//! }
3337
//!
3438
//! fn context(&self) -> ToolPrompt {
3539
//! ToolPrompt::Static("Use this tool for short project notes.")
@@ -68,19 +72,22 @@ pub const GITHUB_CLI: &str = include_str!("github_cli.txt");
6872
/// struct MyTool;
6973
///
7074
/// impl ToolContext for MyTool {
71-
/// const NAME: &'static str = "mytool";
75+
/// fn name(&self) -> &'static str {
76+
/// "mytool"
77+
/// }
7278
///
7379
/// fn context(&self) -> ToolPrompt {
7480
/// ToolPrompt::Static("Instructions for using MyTool...")
7581
/// }
7682
/// }
7783
/// ```
7884
pub trait ToolContext {
79-
/// Tool name used for section headers in generated system prompt.
85+
/// Returns the tool name for section headers in generated system prompt.
8086
///
8187
/// Should be lowercase (e.g., "read", "bash", "glob").
8288
/// SystemPromptBuilder capitalizes this for display.
83-
const NAME: &'static str;
89+
#[must_use]
90+
fn name(&self) -> &'static str;
8491

8592
/// Returns the guidance for this tool.
8693
#[must_use]
@@ -91,6 +98,24 @@ pub trait ToolContext {
9198
mod tests {
9299
use super::*;
93100

101+
#[test]
102+
fn trait_is_object_safe() {
103+
// Verify that Box<dyn ToolContext> can be constructed.
104+
// This proves the trait is object-safe (no associated constants,
105+
// no methods requiring Self: Sized).
106+
struct DummyTool;
107+
impl ToolContext for DummyTool {
108+
fn name(&self) -> &'static str {
109+
"dummy"
110+
}
111+
fn context(&self) -> ToolPrompt {
112+
ToolPrompt::Static("Dummy context")
113+
}
114+
}
115+
116+
let _: Box<dyn ToolContext> = Box::new(DummyTool);
117+
}
118+
94119
#[test]
95120
fn context_strings_are_not_empty() {
96121
assert!(

src/reloaded-code-core/src/system_prompt.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ struct ContextEntry {
2727
/// struct ReadTool;
2828
///
2929
/// impl ToolContext for ReadTool {
30-
/// const NAME: &'static str = "read";
30+
/// fn name(&self) -> &'static str {
31+
/// "read"
32+
/// }
3133
///
3234
/// fn context(&self) -> ToolPrompt {
3335
/// ToolPrompt::Read {
@@ -87,7 +89,9 @@ impl SystemPromptBuilder {
8789
/// struct MyTool;
8890
///
8991
/// impl ToolContext for MyTool {
90-
/// const NAME: &'static str = "read";
92+
/// fn name(&self) -> &'static str {
93+
/// "read"
94+
/// }
9195
///
9296
/// fn context(&self) -> ToolPrompt {
9397
/// ToolPrompt::Read {
@@ -113,7 +117,7 @@ impl SystemPromptBuilder {
113117
/// ```
114118
pub fn track<T: ToolContext>(&mut self, tool: T) -> T {
115119
self.entries.push(ContextEntry {
116-
name: T::NAME,
120+
name: tool.name(),
117121
prompt: tool.context(),
118122
});
119123
tool
@@ -388,7 +392,9 @@ mod tests {
388392
}
389393

390394
impl ToolContext for MockTool {
391-
const NAME: &'static str = "mock";
395+
fn name(&self) -> &'static str {
396+
"mock"
397+
}
392398
fn context(&self) -> ToolPrompt {
393399
ToolPrompt::Static("Mock tool context.")
394400
}
@@ -397,7 +403,9 @@ mod tests {
397403
struct OtherTool;
398404

399405
impl ToolContext for OtherTool {
400-
const NAME: &'static str = "other";
406+
fn name(&self) -> &'static str {
407+
"other"
408+
}
401409
fn context(&self) -> ToolPrompt {
402410
ToolPrompt::Static("Other context.")
403411
}
@@ -418,7 +426,9 @@ mod tests {
418426
impl<const ALLOWED: bool, const LINE_NUMBERS: bool> ToolContext
419427
for $tool<ALLOWED, LINE_NUMBERS>
420428
{
421-
const NAME: &'static str = $name;
429+
fn name(&self) -> &'static str {
430+
$name
431+
}
422432

423433
fn context(&self) -> ToolPrompt {
424434
ToolPrompt::$variant {
@@ -435,7 +445,9 @@ mod tests {
435445
struct $tool<const ALLOWED: bool>;
436446

437447
impl<const ALLOWED: bool> ToolContext for $tool<ALLOWED> {
438-
const NAME: &'static str = $name;
448+
fn name(&self) -> &'static str {
449+
$name
450+
}
439451

440452
fn context(&self) -> ToolPrompt {
441453
ToolPrompt::$variant {
@@ -451,7 +463,9 @@ mod tests {
451463
struct $tool;
452464

453465
impl ToolContext for $tool {
454-
const NAME: &'static str = $name;
466+
fn name(&self) -> &'static str {
467+
$name
468+
}
455469

456470
fn context(&self) -> ToolPrompt {
457471
$prompt
@@ -1200,7 +1214,9 @@ mod tests {
12001214
struct SandboxedBashTool;
12011215

12021216
impl ToolContext for SandboxedBashTool {
1203-
const NAME: &'static str = bash::NAME;
1217+
fn name(&self) -> &'static str {
1218+
bash::NAME
1219+
}
12041220
fn context(&self) -> ToolPrompt {
12051221
ToolPrompt::Bash {
12061222
network_disabled: true,
@@ -1212,7 +1228,9 @@ mod tests {
12121228
struct HostBashTool;
12131229

12141230
impl ToolContext for HostBashTool {
1215-
const NAME: &'static str = bash::NAME;
1231+
fn name(&self) -> &'static str {
1232+
bash::NAME
1233+
}
12161234
fn context(&self) -> ToolPrompt {
12171235
ToolPrompt::Bash {
12181236
network_disabled: false,

src/reloaded-code-provider-config/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct ProviderConfig {
3939
pub api_url: Option<String>,
4040
/// API type string, mapped via [`crate::api_type::api_type_from_str`].
4141
/// Defaults to `"openai-compatible"` when omitted.
42+
#[allow(rustdoc::private_intra_doc_links)]
4243
pub api_type: Option<String>,
4344
/// Environment variable names checked by `CredentialResolver`, in order.
4445
pub env: Option<Vec<String>>,

src/reloaded-code-serdesai/src/task/tool.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ impl<C> ToolContext for TaskTool<C>
8383
where
8484
C: CredentialLookup + Send + Sync + 'static,
8585
{
86-
const NAME: &'static str = task_meta::NAME;
86+
fn name(&self) -> &'static str {
87+
task_meta::NAME
88+
}
8789

8890
fn context(&self) -> ToolPrompt {
8991
ToolPrompt::Task
@@ -117,6 +119,11 @@ mod tests {
117119

118120
#[test]
119121
fn task_tool_name_matches_metadata() {
120-
assert_eq!(TaskTool::<CredentialResolver>::NAME, task_meta::NAME);
122+
let targets = vec![
123+
summary("alpha", "Alpha agent"),
124+
summary("beta", "Beta agent"),
125+
];
126+
let definition = task_tool_definition(&targets);
127+
assert_eq!(definition.name(), task_meta::NAME);
121128
}
122129
}

src/reloaded-code-serdesai/src/tools/bash.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ fn bash_prompt_sandboxed(mode: &BashExecutionMode) -> bool {
259259
}
260260

261261
impl ToolContext for BashTool {
262-
const NAME: &'static str = bash_meta::NAME;
262+
fn name(&self) -> &'static str {
263+
bash_meta::NAME
264+
}
263265

264266
fn context(&self) -> ToolPrompt {
265267
ToolPrompt::Bash {

src/reloaded-code-serdesai/src/tools/edit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ impl<R: PathResolver + Clone + Send + Sync, Deps: Send + Sync> Tool<Deps> for Ed
8181
}
8282

8383
impl<R: PathResolver + Clone> ToolContext for EditTool<R> {
84-
const NAME: &'static str = edit_meta::NAME;
84+
fn name(&self) -> &'static str {
85+
edit_meta::NAME
86+
}
8587

8688
fn context(&self) -> ToolPrompt {
8789
ToolPrompt::Edit {

src/reloaded-code-serdesai/src/tools/glob.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ fn glob_output_to_return(output: GlobOutput) -> ToolReturn {
116116
}
117117

118118
impl<R: PathResolver + Clone> ToolContext for GlobTool<R> {
119-
const NAME: &'static str = glob_meta::NAME;
119+
fn name(&self) -> &'static str {
120+
glob_meta::NAME
121+
}
120122

121123
fn context(&self) -> ToolPrompt {
122124
ToolPrompt::Glob {

0 commit comments

Comments
 (0)