Files
Cyrene/docs/api/backend-services/tool-engine.md
T
AskaEth 9f3b0f386d docs: 更新 tool-engine 移除后的文档引用
- tool-engine.md: 迁移至 AI-Core (8081),更新为内存环形缓冲区字段
- devtools.md: 移除 tool-engine 服务引用,更新启动顺序和代理路由
- architecture-analysis.md: Section 3.4 重写为 pkg/plugins 工具系统

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-26 21:39:46 +08:00

7.2 KiB

工具调用 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 条) 记录调用日志。


目录

  1. GET /api/v1/health
  2. GET /api/v1/tools
  3. GET /api/v1/tools/:name
  4. POST /api/v1/tools/:name/execute
  5. POST /api/v1/tools/execute — 批量执行
  6. GET /api/v1/tools/calls
  7. GET /api/v1/tools/calls/stats
  8. 已注册工具参考

数据模型

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

{ "status": "ok", "service": "ai-core" }

2. GET /api/v1/tools — 工具列表

{
  "tools": [ ToolDefinition, ... ],
  "total": 13
}

3. GET /api/v1/tools/:name — 工具详情

{ "name": "calculator", "description": "...", "parameters": { ... } }

404: {"error":"工具 {name} 不存在"}


4. POST /api/v1/tools/:name/execute — 执行工具

请求

{ "arguments": { "expression": "3 + 4 * 2" } }

Query 参数 (可选,用于日志)

参数 说明
user_id 调用用户
session_id 调用会话

响应 200

{
  "id": "",
  "output": "11",
  "error": ""
}
  • 成功: output 有值,error 为空字符串
  • 失败: output 可能为空,error 有值 —— 仍然返回 HTTP 200,调用方需检查 error 字段

500: {"error":"执行工具 {name} 失败: ..."} — 工具注册表未找到(非参数错误)


5. POST /api/v1/tools/execute — 批量执行

// 请求
{
  "calls": [
    { "id": "call-1", "name": "calculator", "arguments": { "expression": "3+4" } },
    { "id": "call-2", "name": "datetime",    "arguments": { "action": "now" } }
  ]
}

// 响应 200
{
  "results": [
    { "id": "call-1", "output": "7",  "error": "" },
    { "id": "call-2", "output": "2024-01-01 12:00:00", "error": "" }
  ]
}
状态码 错误
400 {"error":"calls 不能为空"}
400 {"error":"请求体格式错误: ..."}

6. GET /api/v1/tools/calls — 调用日志

内存环形缓冲区 (500 条),无需数据库。

Query 参数

参数 默认 说明
tool_name 全部 按工具名过滤
page 1 页码
limit 50 (max 500) 每页条数

响应 200

{
  "calls": [ CallLogRecord, ... ],
  "total": 150,
  "page": 1,
  "limit": 20,
  "total_pages": 8
}

7. GET /api/v1/tools/calls/stats — 调用统计

{
  "total_calls": 150,
  "success_count": 140,
  "fail_count": 10,
  "success_rate": 93.33,
  "avg_duration_ms": 45.2,
  "by_tool": [
    { "tool_name": "calculator", "count": 80, "success_count": 78, "fail_count": 2, "avg_duration_ms": 12.3 }
  ]
}

内存缓冲区,重启后数据清零。


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 — 网页搜索

参数 必填 说明
query 搜索关键词

web_fetch — 网页抓取

参数 必填 说明
url 目标 URL

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_* 动作时需要)