ai-agents-tools 1.0.5

Tool system for AI Agents framework
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use ai_agents_core::{LLMProvider, Tool, ToolInfo, ToolSafetyMetadata};

use super::ToolError;
use super::provider::{ProviderHealth, ToolProvider, ToolProviderError};
use super::types::{
    CommandRunner, CommandRunnerSlot, DiagnosticsProvider, DiagnosticsProviderSlot,
    FileVersionStore, QuestionHandler, QuestionHandlerSlot, TodoItem, TodoStore, ToolAliases,
    UnavailableCommandRunner, UnavailableDiagnosticsProvider, UnavailableWebSearchProvider,
    WebSearchProvider, WebSearchProviderSlot,
};

/// Schema rendering mode for tool prompt generation.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolSchemaPromptMode {
    /// Include full JSON schema properties for every granted tool.
    #[default]
    Full,
    /// Include only compact descriptors: name, description, required fields, and property types.
    Compact,
}

/// Canonical identity produced by registry resolution.
#[derive(Debug, Clone)]
pub struct ToolIdentity {
    /// Name, display name, or alias supplied by the caller.
    pub requested_name: String,
    /// Canonical tool ID used for policy and execution.
    pub canonical_id: String,
    /// Display name of the resolved tool.
    pub display_name: String,
    /// Provider ID for provider-backed tools.
    pub provider_id: Option<String>,
}

/// Resolved tool handle plus canonical identity evidence.
#[derive(Clone)]
pub struct ResolvedTool {
    /// Canonical identity returned by lookup.
    pub identity: ToolIdentity,
    /// Executable tool implementation.
    pub tool: Arc<dyn Tool>,
}

#[derive(Clone)]
enum ToolRef {
    Builtin(Arc<dyn Tool>),
    Provider {
        provider_id: String,
        tool: Arc<dyn Tool>,
    },
}

/// Registry for built-in, provider, alias, and localized tool lookup.
pub struct ToolRegistry {
    builtin_tools: RwLock<HashMap<String, Arc<dyn Tool>>>,

    providers: RwLock<HashMap<String, Arc<dyn ToolProvider>>>,

    tool_index: RwLock<HashMap<String, ToolRef>>,

    alias_index: RwLock<HashMap<String, String>>,

    display_name_index: RwLock<HashMap<String, String>>,

    builtin_aliases: RwLock<HashMap<String, ToolAliases>>,

    question_handler: QuestionHandlerSlot,

    diagnostics_provider: DiagnosticsProviderSlot,

    command_runner: CommandRunnerSlot,

    todo_store: TodoStore,

    file_versions: FileVersionStore,

    web_fetch_extractor: Arc<RwLock<Option<Arc<dyn LLMProvider>>>>,

    web_search_provider: WebSearchProviderSlot,

    registry_version: AtomicU64,
}

impl ToolRegistry {
    /// Creates an empty registry with versioned canonical indexes.
    pub fn new() -> Self {
        Self {
            builtin_tools: RwLock::new(HashMap::new()),
            providers: RwLock::new(HashMap::new()),
            tool_index: RwLock::new(HashMap::new()),
            alias_index: RwLock::new(HashMap::new()),
            display_name_index: RwLock::new(HashMap::new()),
            builtin_aliases: RwLock::new(HashMap::new()),
            question_handler: Arc::new(RwLock::new(None)),
            diagnostics_provider: Arc::new(RwLock::new(Arc::new(UnavailableDiagnosticsProvider))),
            command_runner: Arc::new(RwLock::new(Arc::new(UnavailableCommandRunner))),
            todo_store: TodoStore::default(),
            file_versions: FileVersionStore::default(),
            web_fetch_extractor: Arc::new(RwLock::new(None)),
            web_search_provider: Arc::new(RwLock::new(Arc::new(UnavailableWebSearchProvider))),
            registry_version: AtomicU64::new(1),
        }
    }

    fn bump_version(&self) {
        self.registry_version.fetch_add(1, Ordering::SeqCst);
    }

    /// Returns the registry version used in tool execution evidence.
    pub fn version(&self) -> u64 {
        self.registry_version.load(Ordering::SeqCst)
    }

    fn normalize_key(value: &str) -> String {
        value.trim().to_lowercase()
    }

    fn insert_unique_index(index: &mut HashMap<String, String>, key: String, tool_id: &str) {
        match index.get(&key) {
            None => {
                index.insert(key, tool_id.to_string());
            }
            Some(existing) if existing == tool_id => {}
            Some(_) => {
                index.remove(&key);
            }
        }
    }

    /// Registers a built-in or custom tool by canonical ID.
    pub fn register(&mut self, tool: Arc<dyn Tool>) -> Result<(), ToolError> {
        let id = tool.id().to_string();

        let mut builtin_tools = self.builtin_tools.write();
        let mut tool_index = self.tool_index.write();
        let mut display_name_index = self.display_name_index.write();

        if builtin_tools.contains_key(&id) || tool_index.contains_key(&id) {
            return Err(ToolError::Duplicate(id));
        }

        Self::insert_unique_index(
            &mut display_name_index,
            Self::normalize_key(tool.name()),
            &id,
        );
        tool_index.insert(id.clone(), ToolRef::Builtin(tool.clone()));
        builtin_tools.insert(id, tool);
        self.bump_version();
        Ok(())
    }

    pub fn get(&self, id_or_alias: &str) -> Option<Arc<dyn Tool>> {
        self.resolve(id_or_alias).map(|resolved| resolved.tool)
    }

    /// Resolves any accepted name to a canonical tool ID.
    pub fn canonical_id(&self, id_or_alias: &str) -> Option<String> {
        self.resolve(id_or_alias)
            .map(|resolved| resolved.identity.canonical_id)
    }

    /// Resolves safety metadata for a registered tool.
    pub fn safety_metadata(&self, id_or_alias: &str) -> Option<ToolSafetyMetadata> {
        self.resolve(id_or_alias)
            .map(|resolved| resolved.tool.safety_metadata())
    }

    /// Returns the shared question handler slot for host-bound tools.
    pub fn question_handler_slot(&self) -> QuestionHandlerSlot {
        Arc::clone(&self.question_handler)
    }

    /// Installs or clears the question handler used by `ask_user`.
    pub fn set_question_handler(&self, handler: Option<Arc<dyn QuestionHandler>>) {
        *self.question_handler.write() = handler;
    }

    /// Returns the shared diagnostics provider slot for host-bound tools.
    pub fn diagnostics_provider_slot(&self) -> DiagnosticsProviderSlot {
        Arc::clone(&self.diagnostics_provider)
    }

    /// Installs the diagnostics provider used by `diagnostics`.
    pub fn set_diagnostics_provider(&self, provider: Arc<dyn DiagnosticsProvider>) {
        *self.diagnostics_provider.write() = provider;
    }

    /// Returns whether the diagnostics provider can serve requests now.
    pub fn diagnostics_available(&self) -> bool {
        self.diagnostics_provider.read().is_available()
    }

    /// Returns the shared command runner slot for host-bound tools.
    pub fn command_runner_slot(&self) -> CommandRunnerSlot {
        Arc::clone(&self.command_runner)
    }

    /// Installs the command runner used by `command`.
    pub fn set_command_runner(&self, runner: Arc<dyn CommandRunner>) {
        *self.command_runner.write() = runner;
    }

    /// Returns whether the command runner can serve requests now.
    pub fn command_runner_available(&self) -> bool {
        self.command_runner.read().is_available()
    }

    /// Returns the session-local file version store shared with file tools.
    pub fn file_version_store(&self) -> FileVersionStore {
        self.file_versions.clone()
    }

    /// Returns the session-local todo store shared with `todo`.
    pub fn todo_store(&self) -> TodoStore {
        self.todo_store.clone()
    }

    /// Returns a snapshot of session-local todo items.
    pub fn todos(&self) -> Vec<TodoItem> {
        self.todo_store.list()
    }

    /// Returns the shared web-fetch extractor slot.
    pub fn web_fetch_extractor_slot(&self) -> Arc<RwLock<Option<Arc<dyn LLMProvider>>>> {
        Arc::clone(&self.web_fetch_extractor)
    }

    /// Installs or clears the LLM used for `web_fetch` prompt extraction.
    pub fn set_web_fetch_extractor(&self, extractor: Option<Arc<dyn LLMProvider>>) {
        *self.web_fetch_extractor.write() = extractor;
    }

    /// Returns the shared provider slot for `web_search`.
    pub fn web_search_provider_slot(&self) -> WebSearchProviderSlot {
        Arc::clone(&self.web_search_provider)
    }

    /// Installs the provider used by `web_search`.
    pub fn set_web_search_provider(&self, provider: Arc<dyn WebSearchProvider>) {
        *self.web_search_provider.write() = provider;
    }

    /// Returns whether the web search provider can serve requests now.
    pub fn web_search_available(&self) -> bool {
        self.web_search_provider.read().is_available()
    }

    /// Resolves IDs, display names, and aliases to one canonical tool.
    pub fn resolve(&self, id_or_alias: &str) -> Option<ResolvedTool> {
        let tool_index = self.tool_index.read();
        let requested_name = id_or_alias.to_string();
        let normalized = Self::normalize_key(id_or_alias);

        if let Some((canonical_id, tool_ref)) =
            tool_index.get_key_value(id_or_alias).or_else(|| {
                tool_index
                    .iter()
                    .find(|(id, _)| Self::normalize_key(id) == normalized)
            })
        {
            return self.resolved_tool_from_ref(&requested_name, canonical_id, tool_ref);
        }

        if let Some(tool_id) = self.display_name_index.read().get(&normalized).cloned()
            && let Some(tool_ref) = tool_index.get(&tool_id)
        {
            return self.resolved_tool_from_ref(&requested_name, &tool_id, tool_ref);
        }

        let alias_index = self.alias_index.read();
        if let Some(tool_id) = alias_index.get(&normalized).cloned().or_else(|| {
            alias_index.iter().find_map(|(alias_key, tool_id)| {
                alias_key
                    .ends_with(&format!(":{}", normalized))
                    .then(|| tool_id.clone())
            })
        }) && let Some(tool_ref) = tool_index.get(&tool_id)
        {
            return self.resolved_tool_from_ref(&requested_name, &tool_id, tool_ref);
        }

        None
    }

    fn resolved_tool_from_ref(
        &self,
        requested_name: &str,
        canonical_id: &str,
        tool_ref: &ToolRef,
    ) -> Option<ResolvedTool> {
        let tool = self.resolve_tool_ref(tool_ref)?;
        let provider_id = match tool_ref {
            ToolRef::Builtin(_) => None,
            ToolRef::Provider { provider_id, .. } => Some(provider_id.clone()),
        };
        Some(ResolvedTool {
            identity: ToolIdentity {
                requested_name: requested_name.to_string(),
                canonical_id: canonical_id.to_string(),
                display_name: tool.name().to_string(),
                provider_id,
            },
            tool,
        })
    }

    fn resolve_tool_ref(&self, tool_ref: &ToolRef) -> Option<Arc<dyn Tool>> {
        match tool_ref {
            ToolRef::Builtin(tool) => Some(tool.clone()),
            ToolRef::Provider { tool, .. } => Some(tool.clone()),
        }
    }

    pub fn list_ids(&self) -> Vec<String> {
        self.tool_index.read().keys().cloned().collect()
    }

    pub fn list_infos(&self) -> Vec<ToolInfo> {
        let tool_index = self.tool_index.read();
        let mut infos = Vec::with_capacity(tool_index.len());

        for tool_ref in tool_index.values() {
            if let Some(tool) = self.resolve_tool_ref(tool_ref) {
                infos.push(tool.info());
            }
        }

        infos
    }

    pub fn len(&self) -> usize {
        self.tool_index.read().len()
    }

    pub fn is_empty(&self) -> bool {
        self.tool_index.read().is_empty()
    }

    pub fn map_tools<F>(&self, mut f: F) -> ToolRegistry
    where
        F: FnMut(Arc<dyn Tool>) -> Arc<dyn Tool>,
    {
        let mut mapped = ToolRegistry::new();
        mapped.question_handler = Arc::clone(&self.question_handler);
        mapped.diagnostics_provider = Arc::clone(&self.diagnostics_provider);
        mapped.command_runner = Arc::clone(&self.command_runner);
        mapped.todo_store = self.todo_store.clone();
        mapped.file_versions = self.file_versions.clone();
        mapped.web_fetch_extractor = Arc::clone(&self.web_fetch_extractor);
        mapped.web_search_provider = Arc::clone(&self.web_search_provider);

        {
            let providers = self.providers.read();
            let mut mapped_providers = mapped.providers.write();
            for (id, provider) in providers.iter() {
                mapped_providers.insert(id.clone(), provider.clone());
            }
        }

        {
            let aliases = self.alias_index.read();
            let mut mapped_aliases = mapped.alias_index.write();
            for (alias, tool_id) in aliases.iter() {
                mapped_aliases.insert(alias.clone(), tool_id.clone());
            }
        }

        {
            let display_names = self.display_name_index.read();
            let mut mapped_display_names = mapped.display_name_index.write();
            for (name, tool_id) in display_names.iter() {
                mapped_display_names.insert(name.clone(), tool_id.clone());
            }
        }

        {
            let builtin_aliases = self.builtin_aliases.read();
            let mut mapped_builtin_aliases = mapped.builtin_aliases.write();
            for (id, aliases) in builtin_aliases.iter() {
                mapped_builtin_aliases.insert(id.clone(), aliases.clone());
            }
        }

        let tool_index = self.tool_index.read();
        let mut mapped_tool_index = mapped.tool_index.write();
        let mut mapped_builtin_tools = mapped.builtin_tools.write();
        for (id, tool_ref) in tool_index.iter() {
            match tool_ref {
                ToolRef::Builtin(tool) => {
                    let wrapped = f(tool.clone());
                    mapped_tool_index.insert(id.clone(), ToolRef::Builtin(wrapped.clone()));
                    mapped_builtin_tools.insert(id.clone(), wrapped);
                }
                ToolRef::Provider { provider_id, tool } => {
                    let wrapped = f(tool.clone());
                    mapped_tool_index.insert(
                        id.clone(),
                        ToolRef::Provider {
                            provider_id: provider_id.clone(),
                            tool: wrapped,
                        },
                    );
                }
            }
        }

        drop(mapped_builtin_tools);
        drop(mapped_tool_index);
        mapped
            .registry_version
            .store(self.version(), Ordering::SeqCst);
        mapped
    }

    pub async fn register_provider(
        &self,
        provider: Arc<dyn ToolProvider>,
    ) -> Result<(), ToolError> {
        let provider_id = provider.id().to_string();

        {
            let providers = self.providers.read();
            if providers.contains_key(&provider_id) {
                return Err(ToolError::Duplicate(format!("Provider: {}", provider_id)));
            }
        }

        let tools = provider.list_tools().await;

        // Resolve provider tools before taking index locks so provider I/O cannot block registry access.
        let mut resolved_tools = Vec::with_capacity(tools.len());
        for descriptor in &tools {
            resolved_tools.push((descriptor, provider.get_tool(&descriptor.id).await));
        }

        {
            let mut tool_index = self.tool_index.write();
            let mut alias_index = self.alias_index.write();
            let mut display_name_index = self.display_name_index.write();

            for (descriptor, tool) in resolved_tools {
                if tool_index.contains_key(&descriptor.id) {
                    return Err(ToolError::Duplicate(descriptor.id.clone()));
                }

                if let Some(tool) = tool {
                    Self::insert_unique_index(
                        &mut display_name_index,
                        Self::normalize_key(&descriptor.name),
                        &descriptor.id,
                    );
                    tool_index.insert(
                        descriptor.id.clone(),
                        ToolRef::Provider {
                            provider_id: provider_id.clone(),
                            tool,
                        },
                    );

                    if let Some(ref aliases) = descriptor.aliases {
                        for (lang, name) in &aliases.names {
                            let key = format!("{}:{}", lang, Self::normalize_key(name));
                            Self::insert_unique_index(&mut alias_index, key, &descriptor.id);
                            Self::insert_unique_index(
                                &mut alias_index,
                                Self::normalize_key(name),
                                &descriptor.id,
                            );
                        }
                    }
                }
            }
        }

        self.providers.write().insert(provider_id, provider);
        self.bump_version();

        Ok(())
    }

    pub fn unregister_provider(&self, provider_id: &str) -> bool {
        let removed = self.providers.write().remove(provider_id);

        if removed.is_some() {
            let mut tool_index = self.tool_index.write();
            let mut alias_index = self.alias_index.write();
            let mut display_name_index = self.display_name_index.write();

            let tools_to_remove: Vec<String> = tool_index
                .iter()
                .filter_map(|(id, tool_ref)| {
                    if let ToolRef::Provider {
                        provider_id: pid, ..
                    } = tool_ref
                        && pid == provider_id
                    {
                        return Some(id.clone());
                    }
                    None
                })
                .collect();

            for tool_id in &tools_to_remove {
                tool_index.remove(tool_id);
            }

            alias_index.retain(|_, tool_id| !tools_to_remove.contains(tool_id));
            display_name_index.retain(|_, tool_id| !tools_to_remove.contains(tool_id));
            self.bump_version();

            true
        } else {
            false
        }
    }

    pub fn set_tool_aliases(&self, tool_id: &str, aliases: ToolAliases) {
        if !self.tool_index.read().contains_key(tool_id) {
            return;
        }

        {
            let mut alias_index = self.alias_index.write();
            for (lang, name) in &aliases.names {
                let key = format!("{}:{}", lang, Self::normalize_key(name));
                Self::insert_unique_index(&mut alias_index, key, tool_id);
                Self::insert_unique_index(&mut alias_index, Self::normalize_key(name), tool_id);
            }
        }

        self.builtin_aliases
            .write()
            .insert(tool_id.to_string(), aliases);
        self.bump_version();
    }

    pub fn get_by_alias(&self, alias: &str, lang: &str) -> Option<Arc<dyn Tool>> {
        let key = format!("{}:{}", lang, Self::normalize_key(alias));
        let alias_index = self.alias_index.read();

        if let Some(tool_id) = alias_index.get(&key) {
            return self.get(tool_id);
        }

        None
    }

    pub fn list_providers(&self) -> Vec<String> {
        self.providers.read().keys().cloned().collect()
    }

    pub async fn provider_health(&self, provider_id: &str) -> Option<ProviderHealth> {
        let provider = self.providers.read().get(provider_id).cloned();
        if let Some(provider) = provider {
            Some(provider.health_check().await)
        } else {
            None
        }
    }

    pub async fn refresh_provider(&self, provider_id: &str) -> Result<(), ToolProviderError> {
        let provider = {
            let providers = self.providers.read();
            providers.get(provider_id).cloned()
        };

        if let Some(provider) = provider {
            if provider.supports_refresh() {
                provider.refresh().await?;

                let tools = provider.list_tools().await;

                // Resolve provider tools before taking index locks so refresh keeps the old snapshot available during provider I/O.
                let mut resolved_tools = Vec::with_capacity(tools.len());
                for descriptor in &tools {
                    if let Some(tool) = provider.get_tool(&descriptor.id).await {
                        resolved_tools.push((descriptor, tool));
                    }
                }

                {
                    let mut tool_index = self.tool_index.write();
                    let mut alias_index = self.alias_index.write();
                    let mut display_name_index = self.display_name_index.write();

                    let old_tools: Vec<String> = tool_index
                        .iter()
                        .filter_map(|(id, tool_ref)| {
                            if let ToolRef::Provider {
                                provider_id: pid, ..
                            } = tool_ref
                                && pid == provider_id
                            {
                                return Some(id.clone());
                            }
                            None
                        })
                        .collect();

                    for tool_id in &old_tools {
                        tool_index.remove(tool_id);
                    }
                    alias_index.retain(|_, tool_id| !old_tools.contains(tool_id));
                    display_name_index.retain(|_, tool_id| !old_tools.contains(tool_id));

                    for (descriptor, tool) in resolved_tools {
                        Self::insert_unique_index(
                            &mut display_name_index,
                            Self::normalize_key(&descriptor.name),
                            &descriptor.id,
                        );
                        tool_index.insert(
                            descriptor.id.clone(),
                            ToolRef::Provider {
                                provider_id: provider_id.to_string(),
                                tool,
                            },
                        );

                        if let Some(ref aliases) = descriptor.aliases {
                            for (lang, name) in &aliases.names {
                                let key = format!("{}:{}", lang, Self::normalize_key(name));
                                Self::insert_unique_index(&mut alias_index, key, &descriptor.id);
                                Self::insert_unique_index(
                                    &mut alias_index,
                                    Self::normalize_key(name),
                                    &descriptor.id,
                                );
                            }
                        }
                    }
                    self.bump_version();
                }
            }
            Ok(())
        } else {
            Err(ToolProviderError::ToolNotFound(format!(
                "Provider not found: {}",
                provider_id
            )))
        }
    }

    pub fn generate_tools_prompt(&self) -> String {
        self.generate_tools_prompt_with_lang(None, false)
    }

    pub fn generate_tools_prompt_with_parallel(&self, parallel: bool) -> String {
        self.generate_tools_prompt_with_lang(None, parallel)
    }

    pub fn generate_tools_prompt_with_lang(
        &self,
        language: Option<&str>,
        parallel: bool,
    ) -> String {
        let tool_index = self.tool_index.read();
        if tool_index.is_empty() {
            return String::new();
        }

        let builtin_aliases = self.builtin_aliases.read();
        let mut prompt = String::from("Available tools:\n");

        for (id, tool_ref) in tool_index.iter() {
            if let Some(tool) = self.resolve_tool_ref(tool_ref) {
                let (name, description) = if let Some(lang) = language {
                    if let Some(aliases) = builtin_aliases.get(id) {
                        let name = aliases
                            .names
                            .get(lang)
                            .map(|s| s.as_str())
                            .unwrap_or_else(|| tool.name());
                        let desc = aliases
                            .descriptions
                            .get(lang)
                            .map(|s| s.as_str())
                            .unwrap_or_else(|| tool.description());
                        (name, desc)
                    } else {
                        (tool.name(), tool.description())
                    }
                } else {
                    (tool.name(), tool.description())
                };

                let schema = tool.input_schema();
                let args_desc = if let Some(props) = schema.get("properties") {
                    serde_json::to_string(props).unwrap_or_default()
                } else {
                    "{}".to_string()
                };

                prompt.push_str(&format!(
                    "- {}: {}. Arguments: {}\n",
                    name, description, args_desc
                ));
            }
        }

        Self::append_tool_format_instructions(&mut prompt, parallel);

        prompt
    }

    pub fn generate_filtered_prompt(&self, tool_ids: &[String]) -> String {
        self.generate_filtered_prompt_with_lang(tool_ids, None, false)
    }

    pub fn generate_filtered_prompt_with_parallel(
        &self,
        tool_ids: &[String],
        parallel: bool,
    ) -> String {
        self.generate_filtered_prompt_with_lang(tool_ids, None, parallel)
    }

    /// Generates a prompt for an explicit tool scope.
    pub fn generate_scoped_prompt_with_parallel(
        &self,
        tool_ids: &[String],
        parallel: bool,
    ) -> String {
        self.generate_scoped_prompt_with_lang(tool_ids, None, parallel)
    }

    pub fn generate_scoped_prompt_with_lang(
        &self,
        tool_ids: &[String],
        language: Option<&str>,
        parallel: bool,
    ) -> String {
        if tool_ids.is_empty() {
            return String::new();
        }
        self.generate_filtered_prompt_inner(tool_ids, language, parallel)
    }

    pub fn generate_filtered_prompt_with_lang(
        &self,
        tool_ids: &[String],
        language: Option<&str>,
        parallel: bool,
    ) -> String {
        if tool_ids.is_empty() {
            return self.generate_tools_prompt_with_lang(language, parallel);
        }

        self.generate_filtered_prompt_inner(tool_ids, language, parallel)
    }

    fn generate_filtered_prompt_inner(
        &self,
        tool_ids: &[String],
        language: Option<&str>,
        parallel: bool,
    ) -> String {
        let tool_index = self.tool_index.read();
        let builtin_aliases = self.builtin_aliases.read();
        let mut prompt = String::from("Available tools:\n");
        let mut found_any = false;

        for id in tool_ids {
            if let Some(tool_ref) = tool_index.get(id)
                && let Some(tool) = self.resolve_tool_ref(tool_ref)
            {
                found_any = true;

                let (name, description) = if let Some(lang) = language {
                    if let Some(aliases) = builtin_aliases.get(id) {
                        let name = aliases
                            .names
                            .get(lang)
                            .map(|s| s.as_str())
                            .unwrap_or_else(|| tool.name());
                        let desc = aliases
                            .descriptions
                            .get(lang)
                            .map(|s| s.as_str())
                            .unwrap_or_else(|| tool.description());
                        (name, desc)
                    } else {
                        (tool.name(), tool.description())
                    }
                } else {
                    (tool.name(), tool.description())
                };

                let schema = tool.input_schema();
                let args_desc = if let Some(props) = schema.get("properties") {
                    serde_json::to_string(props).unwrap_or_default()
                } else {
                    "{}".to_string()
                };

                prompt.push_str(&format!(
                    "- {}: {}. Arguments: {}\n",
                    name, description, args_desc
                ));
            }
        }

        if !found_any {
            return String::new();
        }

        Self::append_tool_format_instructions(&mut prompt, parallel);

        prompt
    }

    /// Generate a scoped prompt with a configurable schema rendering mode.
    pub fn generate_scoped_prompt_with_mode(
        &self,
        tool_ids: &[impl AsRef<str>],
        language: Option<&str>,
        parallel: bool,
        mode: ToolSchemaPromptMode,
    ) -> String {
        if tool_ids.is_empty() {
            return String::new();
        }
        match mode {
            ToolSchemaPromptMode::Full => self.generate_scoped_prompt_with_lang(
                &tool_ids
                    .iter()
                    .map(|s| s.as_ref().to_string())
                    .collect::<Vec<_>>(),
                language,
                parallel,
            ),
            ToolSchemaPromptMode::Compact => {
                self.generate_compact_prompt_inner(tool_ids, language, parallel)
            }
        }
    }

    /// Generate a compact tool prompt with stable ordering and reduced schema.
    fn generate_compact_prompt_inner(
        &self,
        tool_ids: &[impl AsRef<str>],
        language: Option<&str>,
        parallel: bool,
    ) -> String {
        let tool_index = self.tool_index.read();
        let builtin_aliases = self.builtin_aliases.read();
        let mut prompt = String::from("Available tools:\n");
        let mut found_any = false;

        for id in tool_ids {
            let id = id.as_ref();
            if let Some(tool_ref) = tool_index.get(id)
                && let Some(tool) = self.resolve_tool_ref(tool_ref)
            {
                found_any = true;

                let (name, description) = if let Some(lang) = language {
                    if let Some(aliases) = builtin_aliases.get(id) {
                        let n = aliases
                            .names
                            .get(lang)
                            .map(|s| s.as_str())
                            .unwrap_or_else(|| tool.name());
                        let d = aliases
                            .descriptions
                            .get(lang)
                            .map(|s| s.as_str())
                            .unwrap_or_else(|| tool.description());
                        (n, d)
                    } else {
                        (tool.name(), tool.description())
                    }
                } else {
                    (tool.name(), tool.description())
                };

                let schema = tool.input_schema();
                let compact = compact_schema_descriptor(&schema);
                prompt.push_str(&format!("- {}: {}. {}\n", name, description, compact));
            }
        }

        if !found_any {
            return String::new();
        }

        Self::append_tool_format_instructions(&mut prompt, parallel);
        prompt
    }

    /// Append tool call format instructions to a prompt.
    /// When `parallel` is true, also instructs the LLM to use a JSON array
    /// for multiple simultaneous tool calls.
    fn append_tool_format_instructions(prompt: &mut String, parallel: bool) {
        prompt.push_str(
            "\nWhen you need to use a tool, respond ONLY with valid JSON in this exact format:\n",
        );
        prompt.push_str("{\"tool\": \"tool_name\", \"arguments\": {...}}\n");
        prompt.push_str("The \"tool\" value MUST be one of the exact tool names listed above. Do not invent tool names.\n");
        if parallel {
            prompt.push_str(
                "\nWhen you need to call multiple tools at once, respond with a JSON array:\n",
            );
            prompt.push_str(
                "[{\"tool\": \"tool_name1\", \"arguments\": {...}}, {\"tool\": \"tool_name2\", \"arguments\": {...}}]\n",
            );
        }
        prompt.push_str("\nWhen you receive a tool result, summarize it naturally for the user.\n");
        prompt.push_str("If no tool is needed, respond normally.");
    }
}

/// Build a compact schema descriptor from a full JSON schema.
/// Includes required fields and property types only, with stable key ordering.
fn compact_schema_descriptor(schema: &serde_json::Value) -> String {
    let props = schema.get("properties").and_then(|p| p.as_object());
    let required = schema.get("required").and_then(|r| r.as_array());
    let mut parts = Vec::new();

    if let Some(req) = required {
        let req_fields: Vec<String> = req
            .iter()
            .filter_map(|v| v.as_str().map(str::to_string))
            .collect();
        if !req_fields.is_empty() {
            parts.push(format!("required: [{}]", req_fields.join(", ")));
        }
    }

    if let Some(props) = props {
        let mut prop_parts = Vec::new();
        for (key, value) in props.iter() {
            let prop_type = value.get("type").and_then(|t| t.as_str()).unwrap_or("?");
            let prop_desc = value
                .get("description")
                .and_then(|d| d.as_str())
                .unwrap_or("");
            let short_desc = if prop_desc.len() > 40 {
                format!("{}...", &prop_desc[..40])
            } else if !prop_desc.is_empty() {
                prop_desc.to_string()
            } else {
                String::new()
            };
            if short_desc.is_empty() {
                prop_parts.push(format!("{}({})", key, prop_type));
            } else {
                prop_parts.push(format!("{}({}): {}", key, prop_type, short_desc));
            }
        }
        if !prop_parts.is_empty() {
            parts.push(format!("args: {}", prop_parts.join("; ")));
        }
    }

    if parts.is_empty() {
        "Args: none".to_string()
    } else {
        parts.join(". ")
    }
}

impl Default for ToolRegistry {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ToolResult;
    use async_trait::async_trait;
    use serde_json::Value;

    struct TestTool {
        id: String,
    }

    #[async_trait]
    impl Tool for TestTool {
        fn id(&self) -> &str {
            &self.id
        }
        fn name(&self) -> &str {
            "Test"
        }
        fn description(&self) -> &str {
            "A test tool"
        }
        fn input_schema(&self) -> Value {
            serde_json::json!({"type": "object"})
        }
        async fn execute(
            &self,
            _args: Value,
            _ctx: ai_agents_core::ToolExecutionContext,
        ) -> ToolResult {
            ToolResult::ok("test")
        }
    }

    #[test]
    fn test_register_and_get() {
        let mut registry = ToolRegistry::new();
        let tool = Arc::new(TestTool {
            id: "test".to_string(),
        });

        registry.register(tool).unwrap();
        assert!(registry.get("test").is_some());
        assert_eq!(registry.len(), 1);
    }

    #[test]
    fn test_duplicate_registration() {
        let mut registry = ToolRegistry::new();
        let tool1 = Arc::new(TestTool {
            id: "test".to_string(),
        });
        let tool2 = Arc::new(TestTool {
            id: "test".to_string(),
        });

        registry.register(tool1).unwrap();
        assert!(registry.register(tool2).is_err());
    }

    #[test]
    fn test_list_ids() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "a".to_string(),
            }))
            .unwrap();
        registry
            .register(Arc::new(TestTool {
                id: "b".to_string(),
            }))
            .unwrap();

        let ids = registry.list_ids();
        assert_eq!(ids.len(), 2);
        assert!(ids.contains(&"a".to_string()));
        assert!(ids.contains(&"b".to_string()));
    }

    #[test]
    fn test_generate_tools_prompt() {
        let empty_registry = ToolRegistry::new();
        let empty_prompt = empty_registry.generate_tools_prompt();
        assert!(empty_prompt.is_empty());

        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "test".to_string(),
            }))
            .unwrap();

        let prompt = registry.generate_tools_prompt();
        assert!(prompt.contains("Available tools:"));
        assert!(prompt.contains("Test:"));
        assert!(prompt.contains("A test tool"));
        assert!(prompt.contains("tool_name"));
    }

    #[test]
    fn test_generate_filtered_prompt_with_filter() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "tool_a".to_string(),
            }))
            .unwrap();
        registry
            .register(Arc::new(TestTool {
                id: "tool_b".to_string(),
            }))
            .unwrap();
        registry
            .register(Arc::new(TestTool {
                id: "tool_c".to_string(),
            }))
            .unwrap();

        let prompt =
            registry.generate_filtered_prompt(&["tool_a".to_string(), "tool_c".to_string()]);

        assert!(prompt.contains("tool_a") || prompt.contains("Test"));
        assert!(!prompt.contains("tool_b"));
    }

    #[test]
    fn test_generate_filtered_prompt_empty_filter() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "tool_a".to_string(),
            }))
            .unwrap();
        registry
            .register(Arc::new(TestTool {
                id: "tool_b".to_string(),
            }))
            .unwrap();

        let prompt = registry.generate_filtered_prompt(&[]);
        assert!(prompt.contains("Test"));
    }

    #[test]
    fn test_generate_filtered_prompt_nonexistent_tools() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "tool_a".to_string(),
            }))
            .unwrap();

        let prompt = registry.generate_filtered_prompt(&["nonexistent".to_string()]);
        assert!(prompt.is_empty());

        let prompt2 =
            registry.generate_filtered_prompt(&["tool_a".to_string(), "nonexistent".to_string()]);
        assert!(prompt2.contains("Test"));
    }

    #[test]
    fn test_set_tool_aliases() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "calculator".to_string(),
            }))
            .unwrap();

        let aliases = ToolAliases::new()
            .with_name("ko", "계산기")
            .with_name("ja", "計算機")
            .with_description("ko", "수학 계산을 합니다");

        registry.set_tool_aliases("calculator", aliases);

        assert!(registry.get_by_alias("계산기", "ko").is_some());
        assert!(registry.get_by_alias("計算機", "ja").is_some());
        assert!(registry.get("calculator").is_some());
    }

    #[test]
    fn test_get_by_alias_case_insensitive() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "search".to_string(),
            }))
            .unwrap();

        let aliases = ToolAliases::new().with_name("ko", "검색");
        registry.set_tool_aliases("search", aliases);

        assert!(registry.get_by_alias("검색", "ko").is_some());
    }

    #[test]
    fn test_generate_prompt_with_language() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "calculator".to_string(),
            }))
            .unwrap();

        let aliases = ToolAliases::new()
            .with_name("ko", "계산기")
            .with_description("ko", "수학 계산");

        registry.set_tool_aliases("calculator", aliases);

        let prompt_en = registry.generate_tools_prompt_with_lang(None, false);
        assert!(prompt_en.contains("Test"));

        let prompt_ko = registry.generate_tools_prompt_with_lang(Some("ko"), false);
        assert!(prompt_ko.contains("계산기"));
        assert!(prompt_ko.contains("수학 계산"));
    }

    #[test]
    fn test_generate_tools_prompt_parallel() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "tool_a".to_string(),
            }))
            .unwrap();
        registry
            .register(Arc::new(TestTool {
                id: "tool_b".to_string(),
            }))
            .unwrap();

        // Without parallel: no array instruction
        let prompt_seq = registry.generate_tools_prompt();
        assert!(prompt_seq.contains("\"tool\": \"tool_name\""));
        assert!(!prompt_seq.contains("JSON array"));
        assert!(!prompt_seq.contains("tool_name1"));

        // With parallel: array instruction present
        let prompt_par = registry.generate_tools_prompt_with_parallel(true);
        assert!(prompt_par.contains("\"tool\": \"tool_name\""));
        assert!(prompt_par.contains("JSON array"));
        assert!(prompt_par.contains("tool_name1"));
        assert!(prompt_par.contains("tool_name2"));
    }

    #[test]
    fn test_canonical_resolution_and_scoped_empty_prompt() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "calculator".to_string(),
            }))
            .unwrap();
        let aliases = ToolAliases::new().with_name("ko", "계산기");
        registry.set_tool_aliases("calculator", aliases);

        let by_id = registry.resolve("calculator").unwrap();
        assert_eq!(by_id.identity.canonical_id, "calculator");

        let by_alias = registry.resolve("계산기").unwrap();
        assert_eq!(by_alias.identity.canonical_id, "calculator");

        let scoped = registry.generate_scoped_prompt_with_parallel(&[], false);
        assert!(scoped.is_empty());
    }

    #[test]
    fn test_generate_filtered_prompt_parallel() {
        let mut registry = ToolRegistry::new();
        registry
            .register(Arc::new(TestTool {
                id: "tool_a".to_string(),
            }))
            .unwrap();
        registry
            .register(Arc::new(TestTool {
                id: "tool_b".to_string(),
            }))
            .unwrap();

        // Filtered without parallel
        let prompt_seq =
            registry.generate_filtered_prompt(&["tool_a".to_string(), "tool_b".to_string()]);
        assert!(!prompt_seq.contains("JSON array"));

        // Filtered with parallel
        let prompt_par = registry.generate_filtered_prompt_with_parallel(
            &["tool_a".to_string(), "tool_b".to_string()],
            true,
        );
        assert!(prompt_par.contains("JSON array"));
        assert!(prompt_par.contains("tool_name1"));
    }
}