agentic-tools-registry 0.3.0

Unified tool registry aggregating all agentic-tools domain registries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Unified tool registry aggregating all agentic-tools domain registries.
//!
//! This crate provides a single entry point for building a `ToolRegistry` containing
//! all available tools from the various domain crates (coding_agent_tools, pr_comments,
//! linear_tools, gpt5_reasoner, thoughts_mcp_tools, web_retrieval).
//!
//! # Example
//!
//! ```ignore
//! use agentic_tools_registry::{AgenticTools, AgenticToolsConfig};
//!
//! // Build registry with all tools
//! let registry = AgenticTools::new(AgenticToolsConfig::default());
//! assert!(registry.len() >= 19);
//!
//! // Build registry with allowlist
//! let config = AgenticToolsConfig {
//!     allowlist: Some(["cli_ls", "cli_grep"].into_iter().map(String::from).collect()),
//!     ..Default::default()
//! };
//! let filtered = AgenticTools::new(config);
//! assert_eq!(filtered.len(), 2);
//! ```

#[cfg(not(unix))]
compile_error!(
    "agentic-tools-registry only supports Unix-like platforms (Linux/macOS). Windows is not supported."
);

use agentic_config::types::AnthropicServiceConfig;
use agentic_config::types::CliToolsConfig;
use agentic_config::types::ExaServiceConfig;
use agentic_config::types::ReasoningConfig;
use agentic_config::types::SubagentsConfig;
use agentic_config::types::WebRetrievalConfig;
use agentic_tools_core::ToolRegistry;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashSet;
use std::sync::Arc;
use tracing::warn;

/// Configuration for building the unified registry.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct AgenticToolsConfig {
    /// Optional allowlist of tool names (case-insensitive).
    /// Empty or None = enable all tools.
    #[serde(default)]
    pub allowlist: Option<HashSet<String>>,

    /// Tool-specific config for coding-agent-tools subagents.
    #[serde(default)]
    pub subagents: SubagentsConfig,

    /// Tool-specific config for CLI tools (limits, ignore patterns).
    #[serde(default)]
    pub cli_tools: CliToolsConfig,

    /// Tool-specific config for gpt5-reasoner.
    #[serde(default)]
    pub reasoning: ReasoningConfig,

    /// Tool-specific config for web retrieval tools.
    #[serde(default)]
    pub web_retrieval: WebRetrievalConfig,

    /// Anthropic service configuration for web summarization.
    #[serde(default)]
    pub anthropic: AnthropicServiceConfig,

    /// Exa service configuration for web search.
    #[serde(default)]
    pub exa: ExaServiceConfig,

    /// Reserved for future use (e.g., schema strictness, patches).
    #[serde(default)]
    pub extras: serde_json::Value,
}

/// Unified AgenticTools entrypoint.
pub struct AgenticTools;

// Tool name constants for each domain
const CODING_NAMES: &[&str] = &[
    "cli_ls",
    "ask_agent",
    "cli_grep",
    "cli_glob",
    "cli_just_search",
    "cli_just_execute",
];

const PR_COMMENTS_NAMES: &[&str] = &["gh_get_comments", "gh_add_comment_reply", "gh_get_prs"];

const LINEAR_NAMES: &[&str] = &[
    "linear_search_issues",
    "linear_read_issue",
    "linear_create_issue",
    "linear_add_comment",
    "linear_archive_issue",
    "linear_get_metadata",
];

const GPT5_NAMES: &[&str] = &["ask_reasoning_model"];

const THOUGHTS_NAMES: &[&str] = &[
    "thoughts_write_document",
    "thoughts_list_documents",
    "thoughts_list_references",
    "thoughts_get_repo_refs",
    "thoughts_add_reference",
    "thoughts_get_template",
];

const WEB_NAMES: &[&str] = &["web_fetch", "web_search"];

const REVIEW_NAMES: &[&str] = &["diff_snapshot", "diff_page", "run"];

impl AgenticTools {
    /// Build the unified ToolRegistry using domain registries.
    ///
    /// Lazy domain gating: When an allowlist is provided, only build domains
    /// whose tools intersect the allowlist.
    #[allow(clippy::new_ret_no_self)]
    pub fn new(config: AgenticToolsConfig) -> ToolRegistry {
        let allow = normalize_allowlist(config.allowlist);

        // Helper: decide if a domain should be built
        let domain_wanted = |names: &[&str]| match &allow {
            None => true,
            Some(set) => names.iter().any(|n| set.contains(&n.to_lowercase())),
        };

        // Accumulate selected domain registries
        let mut regs = Vec::new();

        // coding_agent_tools (6 tools)
        if domain_wanted(CODING_NAMES) {
            regs.push(coding_agent_tools::build_registry(
                config.subagents.clone(),
                config.cli_tools.clone(),
            ));
        }

        // pr_comments (3 tools)
        if domain_wanted(PR_COMMENTS_NAMES) {
            // TODO(2): Centralize ambient git repo detection + overrides across tool registries
            // (avoid per-domain fallbacks like this).
            let tool = match pr_comments::PrComments::new() {
                Ok(t) => t,
                Err(e) => {
                    warn!(
                        "pr_comments: ambient repo detection failed ({}); tools will return a clear error until repo context is available",
                        e
                    );
                    pr_comments::PrComments::disabled(format!("{:#}", e))
                }
            };
            regs.push(pr_comments::build_registry(Arc::new(tool)));
        }

        // linear_tools (6 tools)
        if domain_wanted(LINEAR_NAMES) {
            let linear = Arc::new(linear_tools::LinearTools::new());
            regs.push(linear_tools::build_registry(linear));
        }

        // gpt5_reasoner (1 tool)
        if domain_wanted(GPT5_NAMES) {
            regs.push(gpt5_reasoner::build_registry(config.reasoning.clone()));
        }

        // thoughts-mcp-tools (6 tools)
        if domain_wanted(THOUGHTS_NAMES) {
            regs.push(thoughts_mcp_tools::build_registry());
        }

        // web-retrieval (2 tools)
        if domain_wanted(WEB_NAMES) {
            let web = Arc::new(web_retrieval::WebTools::with_config(
                config.web_retrieval.clone(),
                &config.exa,
                config.anthropic.clone(),
            ));
            regs.push(web_retrieval::build_registry(web));
        }

        // review_tools (3 tools)
        if domain_wanted(REVIEW_NAMES) {
            let svc = Arc::new(review_tools::ReviewTools::new());
            regs.push(review_tools::build_registry(svc));
        }

        let merged = ToolRegistry::merge_all(regs);

        // Final allowlist filtering at registry level (authoritative)
        if let Some(set) = allow {
            let names: Vec<&str> = set.iter().map(|s| s.as_str()).collect();
            // Warn about unknown tool names in allowlist
            for name in &names {
                if !merged.contains(name) {
                    warn!("Unknown tool in allowlist: {}", name);
                }
            }
            merged.subset(names)
        } else {
            merged
        }
    }

    /// Get the total count of available tools when no allowlist is applied.
    pub fn total_tool_count() -> usize {
        CODING_NAMES.len()
            + PR_COMMENTS_NAMES.len()
            + LINEAR_NAMES.len()
            + GPT5_NAMES.len()
            + THOUGHTS_NAMES.len()
            + WEB_NAMES.len()
            + REVIEW_NAMES.len()
    }
}

/// Normalize allowlist: lowercase, trim, filter empty strings.
/// Returns None if the resulting set is empty (empty allowlist = all tools).
fn normalize_allowlist(allowlist: Option<HashSet<String>>) -> Option<HashSet<String>> {
    allowlist.and_then(|s| {
        let normalized: HashSet<String> = s
            .into_iter()
            .map(|n| n.trim().to_lowercase())
            .filter(|n| !n.is_empty())
            .collect();
        if normalized.is_empty() {
            None // Empty allowlist = enable all tools
        } else {
            Some(normalized)
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn total_tool_count_is_27() {
        assert_eq!(AgenticTools::total_tool_count(), 27);
    }

    #[test]
    fn normalize_allowlist_lowercases() {
        let mut set = HashSet::new();
        set.insert("CLI_LS".to_string());
        set.insert("Ask_Reasoning_Model".to_string());
        let normalized = normalize_allowlist(Some(set)).unwrap();
        assert!(normalized.contains("cli_ls"));
        assert!(normalized.contains("ask_reasoning_model"));
        assert!(!normalized.contains("CLI_LS"));
    }

    #[test]
    fn normalize_allowlist_filters_empty() {
        let mut set = HashSet::new();
        set.insert("".to_string());
        set.insert("   ".to_string());
        set.insert("cli_ls".to_string());
        let normalized = normalize_allowlist(Some(set)).unwrap();
        assert_eq!(normalized.len(), 1);
        assert!(normalized.contains("cli_ls"));
    }

    #[test]
    fn normalize_allowlist_none_returns_none() {
        assert!(normalize_allowlist(None).is_none());
    }

    // Integration tests for AgenticTools::new
    // Note: These tests actually build the full registries, which may have
    // side effects (e.g., pr_comments tries git detection, linear reads env var).
    // The fallbacks ensure they don't fail in test environments.

    #[test]
    fn allowlist_none_builds_all_tools() {
        let reg = AgenticTools::new(AgenticToolsConfig::default());
        let names = reg.list_names();

        // Should have all 27 tools
        assert!(
            names.len() >= 27,
            "expected at least 27 tools, got {}",
            names.len()
        );

        // Check some known tools from each domain
        assert!(
            reg.contains("cli_ls"),
            "missing cli_ls from coding_agent_tools"
        );
        assert!(
            reg.contains("gh_get_comments"),
            "missing gh_get_comments from pr_comments"
        );
        assert!(
            reg.contains("linear_search_issues"),
            "missing linear_search_issues from linear_tools"
        );
        assert!(
            reg.contains("ask_reasoning_model"),
            "missing ask_reasoning_model from gpt5_reasoner"
        );
        assert!(
            reg.contains("thoughts_add_reference"),
            "missing thoughts_add_reference from thoughts_mcp_tools"
        );
        assert!(
            reg.contains("thoughts_get_repo_refs"),
            "missing thoughts_get_repo_refs from thoughts_mcp_tools"
        );
        assert!(
            reg.contains("web_fetch"),
            "missing web_fetch from web_retrieval"
        );
        assert!(
            reg.contains("web_search"),
            "missing web_search from web_retrieval"
        );
    }

    #[test]
    fn allowlist_filters_to_specific_tools() {
        let mut set = HashSet::new();
        set.insert("cli_ls".to_string());
        set.insert("ask_reasoning_model".to_string());
        let config = AgenticToolsConfig {
            allowlist: Some(set),
            ..Default::default()
        };

        let reg = AgenticTools::new(config);
        let names = reg.list_names();

        assert_eq!(names.len(), 2);
        assert!(reg.contains("cli_ls"));
        assert!(reg.contains("ask_reasoning_model"));
        assert!(!reg.contains("cli_grep"));
    }

    #[test]
    fn allowlist_is_case_insensitive() {
        let mut set = HashSet::new();
        set.insert("CLI_LS".to_string());
        set.insert("ASK_REASONING_MODEL".to_string());
        let config = AgenticToolsConfig {
            allowlist: Some(set),
            ..Default::default()
        };

        let reg = AgenticTools::new(config);

        // Should find tools despite uppercase allowlist
        assert!(reg.contains("cli_ls"));
        assert!(reg.contains("ask_reasoning_model"));
    }

    #[test]
    fn empty_allowlist_enables_all_tools() {
        let config = AgenticToolsConfig {
            allowlist: Some(HashSet::new()),
            ..Default::default()
        };

        let reg = AgenticTools::new(config);

        // Empty allowlist normalizes to None, enabling all tools
        assert!(reg.len() >= 27);
    }

    #[test]
    fn allowlist_web_search_only() {
        let mut set = HashSet::new();
        set.insert("web_search".to_string());
        let config = AgenticToolsConfig {
            allowlist: Some(set),
            ..Default::default()
        };

        let reg = AgenticTools::new(config);
        assert_eq!(reg.len(), 1);
        assert!(reg.contains("web_search"));
        assert!(!reg.contains("web_fetch"));
    }

    #[test]
    fn unknown_allowlist_names_are_ignored() {
        let mut set = HashSet::new();
        set.insert("cli_ls".to_string());
        set.insert("nonexistent_tool".to_string());
        let config = AgenticToolsConfig {
            allowlist: Some(set),
            ..Default::default()
        };

        let reg = AgenticTools::new(config);

        // Should only have "cli_ls", ignoring "nonexistent_tool"
        assert_eq!(reg.len(), 1);
        assert!(reg.contains("cli_ls"));
    }

    #[test]
    fn builds_with_non_default_tool_configs() {
        // Verify that AgenticTools::new() builds successfully with non-default
        // SubagentsConfig and ReasoningConfig values
        let config = AgenticToolsConfig {
            allowlist: None,
            subagents: SubagentsConfig {
                locator_model: "custom-haiku".into(),
                analyzer_model: "custom-sonnet".into(),
            },
            reasoning: ReasoningConfig {
                optimizer_model: "anthropic/custom-optimizer".into(),
                executor_model: "openai/custom-executor".into(),
                reasoning_effort: Some("high".into()),
                api_base_url: None,
                token_limit: None,
            },
            ..Default::default()
        };

        let reg = AgenticTools::new(config);

        // Should build successfully with all tools
        assert!(
            reg.len() >= 23,
            "expected at least 23 tools, got {}",
            reg.len()
        );

        // Verify tools from domains that use the configs are present
        assert!(
            reg.contains("ask_agent"),
            "missing ask_agent (uses subagents config)"
        );
        assert!(
            reg.contains("ask_reasoning_model"),
            "missing ask_reasoning_model (uses reasoning config)"
        );
    }
}