工具调用 API (已迁移至 AI-Core)
Base URL: http://<host>:8081 | Auth: 无
迁移说明: tool-engine 服务 (8092) 已移除。工具注册与调用功能已整合到 pkg/plugins 共享模块,由 AI-Core (8081) 直接管理。API 路径保持不变。
源码: pkg/plugins/manager/registry.go
工具注册中心管理 15+ 个内置工具,通过内存环形缓冲区 (500 条) 记录调用日志。
目录
- GET /api/v1/health
- GET /api/v1/tools
- GET /api/v1/tools/:name
- POST /api/v1/tools/:name/execute
- POST /api/v1/tools/execute — 批量执行
- GET /api/v1/tools/calls
- GET /api/v1/tools/calls/stats
- 已注册工具参考
数据模型
ToolDefinition
| 字段 |
类型 |
说明 |
name |
string |
工具标识 |
description |
string |
功能描述 |
parameters |
object |
JSON Schema 参数定义 |
CallLogRecord
| 字段 |
类型 |
说明 |
call_id |
string |
调用唯一标识 (纳秒时间戳) |
tool_name |
string |
工具名 |
arguments |
string |
调用参数 (JSON 字符串) |
output |
string |
输出文本 |
error |
string |
错误信息 |
success |
bool |
是否成功 |
duration_ms |
int |
执行耗时 |
timestamp |
int64 |
Unix 毫秒时间戳 |
ToolCallCount
| 字段 |
类型 |
说明 |
tool_name |
string |
工具名 |
count |
int |
调用总数 |
success_count |
int |
成功次数 |
fail_count |
int |
失败次数 |
avg_duration_ms |
float64 |
平均耗时 |
1. GET /api/v1/health
2. GET /api/v1/tools — 工具列表
3. GET /api/v1/tools/:name — 工具详情
404: {"error":"工具 {name} 不存在"}
4. POST /api/v1/tools/:name/execute — 执行工具
请求
Query 参数 (可选,用于日志)
| 参数 |
说明 |
user_id |
调用用户 |
session_id |
调用会话 |
响应 200
- 成功:
output 有值,error 为空字符串
- 失败:
output 可能为空,error 有值 —— 仍然返回 HTTP 200,调用方需检查 error 字段
500: {"error":"执行工具 {name} 失败: ..."} — 工具注册表未找到(非参数错误)
5. POST /api/v1/tools/execute — 批量执行
| 状态码 |
错误 |
| 400 |
{"error":"calls 不能为空"} |
| 400 |
{"error":"请求体格式错误: ..."} |
6. GET /api/v1/tools/calls — 调用日志
内存环形缓冲区 (500 条),无需数据库。
Query 参数
| 参数 |
默认 |
说明 |
tool_name |
全部 |
按工具名过滤 |
page |
1 |
页码 |
limit |
50 (max 500) |
每页条数 |
响应 200
7. GET /api/v1/tools/calls/stats — 调用统计
内存缓冲区,重启后数据清零。
8. 已注册工具参考
calculator — 数学计算
| 参数 |
必填 |
说明 |
expression |
是 |
数学表达式 (如 3 + 4 * 2) |
datetime — 日期时间
| 参数 |
必填 |
说明 |
action |
是 |
now, format, add, diff, timezone_list |
format |
否 |
时间格式 (action=format 时) |
timezone |
否 |
时区 |
date |
否 |
日期 |
duration |
否 |
时长 (action=add 时) |
date2 |
否 |
第二个日期 (action=diff 时) |
text — 文本处理
| 参数 |
必填 |
说明 |
action |
是 |
count, summarize, translate, extract |
text |
是 |
文本内容 |
target_lang |
否 |
目标语言 (translate) |
pattern |
否 |
提取模式 (extract) |
crypto — 加密编码
| 参数 |
必填 |
说明 |
action |
是 |
hash, base64_encode, base64_decode, url_encode, url_decode |
input |
是 |
输入文本 |
algorithm |
否 |
hash 算法: md5, sha1, sha256, sha512 (默认 sha256) |
random — 随机生成
| 参数 |
必填 |
说明 |
action |
是 |
number, uuid, password, pick, shuffle |
min |
否 |
最小值 (number) |
max |
否 |
最大值 (number) |
length |
否 |
长度 (password) |
items |
否 |
选项数组 (pick/shuffle) |
count |
否 |
选取数量 (pick) |
markdown — Markdown 处理
| 参数 |
必填 |
说明 |
action |
是 |
to_html, to_text, extract_links, extract_code, table_of_contents |
markdown |
是 |
Markdown 原文 |
json_ops — JSON 操作
| 参数 |
必填 |
说明 |
action |
是 |
parse, query, validate |
json_string |
是 |
JSON 字符串 |
path |
否 |
查询路径 (query) |
file_ops — 文件操作
| 参数 |
必填 |
说明 |
action |
是 |
read, write, list, exists, delete |
path |
是 |
文件路径 |
content |
否 |
写入内容 (write) |
http_request — HTTP 请求
| 参数 |
必填 |
默认 |
说明 |
url |
是 |
— |
请求 URL |
method |
否 |
GET |
HTTP 方法 |
headers |
否 |
{} |
请求头 |
body |
否 |
— |
请求体 |
timeout |
否 |
30s |
超时秒数 |
web_search — 网页搜索
web_fetch — 网页抓取
iot_query — IoT 设备查询
| 参数 |
必填 |
说明 |
device_id |
否 |
设备 ID (缺省返回全部) |
iot_control — IoT 设备控制
| 参数 |
必填 |
说明 |
device_id |
是 |
设备 ID (别名 entity_id) |
action |
是 |
toggle, turn_on, turn_off, set_temperature, set_brightness, set_position, set_mode, set_color (支持中文名) |
value |
否 |
值 (set_* 动作时需要) |