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
//! Tool(工具)接口定义
//!
//! # 概述
//!
//! 本模块定义了 [`Tool`] trait,这是所有工具必须实现的接口。
//!
//! # 设计原则
//!
//! - **统一输入输出**: 所有工具使用 `serde_json::Value` 作为输入输出类型
//! - **自描述**: 每个工具提供自己的名称、描述和输入 schema
//! - **分类管理**: 支持工具分类,便于按类别加载和过滤
//! - **异步执行**: 所有工具异步执行,支持 IO 密集型操作
//!
//! # 实现示例
//!
//! ## 简单工具
//!
//! ```rust
//! use rucora_core::tool::{Tool, ToolCategory};
//! use rucora_core::error::ToolError;
//! use async_trait::async_trait;
//! use serde_json::{Value, json};
//!
//! /// 简单的回显工具
//! struct EchoTool;
//!
//! #[async_trait]
//! impl Tool for EchoTool {
//! fn name(&self) -> &str {
//! "echo"
//! }
//!
//! fn description(&self) -> Option<&str> {
//! Some("回显输入内容")
//! }
//!
//! fn categories(&self) -> &'static [ToolCategory] {
//! &[ToolCategory::Basic]
//! }
//!
//! fn input_schema(&self) -> Value {
//! json!({
//! "type": "object",
//! "properties": {
//! "text": {
//! "type": "string",
//! "description": "要回显的文本"
//! }
//! },
//! "required": ["text"]
//! })
//! }
//!
//! async fn call(&self, input: Value) -> Result<Value, ToolError> {
//! let text = input.get("text")
//! .and_then(|v| v.as_str())
//! .ok_or_else(|| ToolError::Message("缺少 'text' 字段".to_string()))?;
//!
//! Ok(json!({"echo": text}))
//! }
//! }
//! ```
//!
//! ## 带状态的工具
//!
//! ```rust
//! use std::sync::Arc;
//! use tokio::sync::RwLock;
//! use rucora_core::tool::{Tool, ToolCategory};
//! use rucora_core::error::ToolError;
//! use async_trait::async_trait;
//! use serde_json::{Value, json};
//!
//! /// 带状态的计数器工具
//! struct CounterTool {
//! count: Arc<RwLock<i32>>,
//! }
//!
//! impl CounterTool {
//! fn new() -> Self {
//! Self {
//! count: Arc::new(RwLock::new(0)),
//! }
//! }
//! }
//!
//! #[async_trait]
//! impl Tool for CounterTool {
//! fn name(&self) -> &str {
//! "counter"
//! }
//!
//! fn description(&self) -> Option<&str> {
//! Some("计数器工具,每次调用递增")
//! }
//!
//! fn categories(&self) -> &'static [ToolCategory] {
//! &[ToolCategory::Basic]
//! }
//!
//! fn input_schema(&self) -> Value {
//! json!({
//! "type": "object",
//! "properties": {
//! "increment": {
//! "type": "integer",
//! "description": "递增的值,默认为 1"
//! }
//! }
//! })
//! }
//!
//! async fn call(&self, input: Value) -> Result<Value, ToolError> {
//! let increment = input.get("increment")
//! .and_then(|v| v.as_i64())
//! .unwrap_or(1) as i32;
//!
//! let mut count = self.count.write().await;
//! *count += increment;
//!
//! Ok(json!({"count": *count}))
//! }
//! }
//! ```
//!
//! # 最佳实践
//!
//! ## 1. 提供清晰的描述
//!
//! ```rust
//! fn description(&self) -> Option<&str> {
//! Some("读取文件内容。支持 txt、md、json 等文本文件格式。")
//! }
//! ```
//!
//! ## 2. 定义明确的输入 schema
//!
//! ```rust
//! fn input_schema(&self) -> Value {
//! json!({
//! "type": "object",
//! "properties": {
//! "path": {
//! "type": "string",
//! "description": "文件路径"
//! },
//! "max_size": {
//! "type": "integer",
//! "description": "最大文件大小(字节),默认 1MB",
//! "default": 1048576
//! }
//! },
//! "required": ["path"]
//! })
//! }
//! ```
//!
//! ## 3. 完善的错误处理
//!
//! ```rust
//! async fn call(&self, input: Value) -> Result<Value, ToolError> {
//! let path = input.get("path")
//! .and_then(|v| v.as_str())
//! .ok_or_else(|| ToolError::Message("缺少必需的 'path' 字段".to_string()))?;
//!
//! // 执行操作...
//! Ok(json!({"result": "success"}))
//! }
//! ```
//!
//! ## 4. 合理的分类
//!
//! ```rust
//! fn categories(&self) -> &'static [ToolCategory] {
//! &[ToolCategory::File, ToolCategory::System]
//! }
//! ```
use async_trait;
use Value;
use crateToolError;
use crateToolDefinition; // 引入 ToolDefinition
/// 工具分类枚举
///
/// 用于对工具进行分类,以便按类别加载和管理工具。
///
/// # 变体说明
///
/// - `Basic`: 基础工具,用于测试、调试等通用功能
/// - `File`: 文件操作,读取、写入、编辑文件
/// - `Network`: 网络请求,HTTP、网页获取等网络操作
/// - `System`: 系统命令,执行 shell 命令、Git 操作等
/// - `Browser`: 浏览器操作,打开浏览器、网页自动化等
/// - `Memory`: 记忆存储,存储和检索长期记忆
/// - `External`: 外部服务,与第三方 API 交互
/// - `Custom(&'static str)`: 自定义分类
///
/// # 示例
///
/// ```rust
/// use rucora_core::tool::ToolCategory;
///
/// // 使用预定义分类
/// let category = ToolCategory::File;
/// assert_eq!(category.name(), "file");
///
/// // 使用自定义分类
/// let custom = ToolCategory::Custom("ai_tool");
/// assert_eq!(custom.name(), "ai_tool");
/// ```
/// Tool(工具)接口
///
/// 所有工具必须实现此 trait。
///
/// # 设计要求
///
/// - **输入输出统一使用 JSON**: 便于跨 provider、跨 runtime 复用
/// - **自描述**: 提供名称、描述和输入 schema
/// - **异步执行**: 支持 IO 密集型操作
/// - **线程安全**: 实现 `Send + Sync`
///
/// # 字段说明
///
/// - `name()`: 工具名称,必须唯一
/// - `description()`: 工具描述,帮助 LLM 理解工具用途
/// - `categories()`: 工具分类,支持多标签
/// - `input_schema()`: 输入参数的 JSON Schema
/// - `call()`: 执行工具的异步方法
///
/// # 示例
///
/// ```rust,no_run
/// use rucora_core::tool::{Tool, ToolCategory};
/// use rucora_core::error::ToolError;
/// use async_trait::async_trait;
/// use serde_json::{Value, json};
///
/// struct MyTool;
///
/// #[async_trait]
/// impl Tool for MyTool {
/// fn name(&self) -> &str {
/// "my_tool"
/// }
///
/// fn description(&self) -> Option<&str> {
/// Some("我的自定义工具")
/// }
///
/// fn categories(&self) -> &'static [ToolCategory] {
/// &[ToolCategory::Basic]
/// }
///
/// fn input_schema(&self) -> Value {
/// json!({
/// "type": "object",
/// "properties": {
/// "param": {"type": "string"}
/// }
/// })
/// }
///
/// async fn call(&self, input: Value) -> Result<Value, ToolError> {
/// // 实现工具逻辑
/// Ok(json!({"result": "success"}))
/// }
/// }
/// ```