Install
openclaw skills install swagger-skill智能 Swagger API 查询和调用工具。通过自然语言指令直接查询接口详情、调用 API,无需繁琐的交互步骤。
openclaw skills install swagger-skill无需手动安装依赖。首次使用时会自动检测并安装所需依赖(axios、form-data),同时自动初始化 package.json(含 "type": "module" 配置)。
如需手动安装,可在 skill 目录下执行:
npm install
import SwaggerAPISkill from './index.js';
const skill = new SwaggerAPISkill();
// 1. 加载 Swagger 规范
await skill.fetchSwaggerSpec('https://api.example.com/swagger.json');
// 2. 获取所有接口
const allAPIs = skill.getAllAPIs();
// 3. 搜索接口
const results = skill.searchAPI('获取用户信息');
// 4. 获取接口详情
const detail = skill.getAPIDetail('/users/{id}', 'GET');
// 5. 调用接口
const response = await skill.callAPI('/users', 'GET', {
query: { page: 1, limit: 10 }
});
// 6. 通过自然语言指令调用
const result = await skill.callAPIByInstruction('获取所有用户', {
query: { page: 1 }
});
import SwaggerAPISkill from './index.js';
const skill = new SwaggerAPISkill();
// 方式 A: 先设置 Token,再加载规范
skill.setAuthToken('your-jwt-token');
await skill.fetchSwaggerSpec('http://localhost:8090/v2/api-docs');
// 方式 B: 在加载规范时直接传入 Token
await skill.fetchSwaggerSpec('http://localhost:8090/v2/api-docs', {
token: 'your-jwt-token',
tokenOptions: {
tokenType: 'Bearer',
headerName: 'Authorization'
}
});
// 调用 API(会自动添加认证头)
const result = await skill.callAPI('/sysUser/list', 'POST', {
body: { pageNum: 1, pageSize: 10 }
});
const skill = new SwaggerAPISkill();
// 方式 A: 先设置 Cookie,再加载规范
skill.setAuthCookies({
token: 'your-token',
JSESSIONID: 'your-session-id'
});
await skill.fetchSwaggerSpec('http://localhost:8090/v2/api-docs');
// 方式 B: 在加载规范时直接传入 Cookie
await skill.fetchSwaggerSpec('http://localhost:8090/v2/api-docs', {
cookies: {
token: 'your-token',
JSESSIONID: 'your-session-id'
}
});
const skill = new SwaggerAPISkill();
// 直接加载规范
await skill.fetchSwaggerSpec('http://localhost:8090/v2/api-docs');
// 调用 API
const result = await skill.callAPI('/users', 'GET', {
query: { page: 1, limit: 10 }
});
node cli.js
交互式 CLI 工具会引导你:
获取并加载 Swagger 规范文件。
参数:
url (string): Swagger JSON URL 或 API 基础 URLoptions (object): 可选配置
token (string): JWT Token 或其他认证 Tokencookies (object): Cookie 对象,如 { token: 'xxx', JSESSIONID: 'xxx' }tokenOptions (object): Token 选项
tokenType (string): Token 类型,默认为 'Bearer'headerName (string): 请求头名称,默认为 'Authorization'返回:
{
success: boolean,
apiCount?: number, // 接口总数
cached?: boolean, // 仅缓存命中时返回 true
error?: string
}
设置认证 Token。
参数:
token (string): JWT Token 或其他认证 Tokenoptions (object): 可选配置
tokenType (string): Token 类型,默认为 'Bearer'headerName (string): 请求头名称,默认为 'Authorization'返回:
{
success: boolean,
message: string
}
设置认证 Cookie。
参数:
cookies (object): Cookie 对象,如 { token: 'xxx', JSESSIONID: 'xxx' }返回:
{
success: boolean,
message: string
}
清除认证信息。
返回:
{
success: boolean,
message: string
}
获取所有接口的基本信息。
返回:
{
success: boolean,
total: number,
apis: Array<{
path: string,
method: string,
summary: string,
description: string,
operationId: string,
tags: string[]
}>
}
根据自然语言查询搜索接口。支持 summary、description、path、operationId 和 tags 匹配。
参数:
query (string): 自然语言查询字符串返回:
{
success: boolean,
query: string,
matchCount: number,
results: Array<{
path: string,
method: string,
summary: string,
description?: string, // 仅非空时返回
score: number
}>
}
获取特定接口的详细信息。使用 Map O(1) 查找。
参数:
path (string): API 路径,如 /users/{id}method (string): HTTP 方法,如 GET, POST 等返回:
{
success: boolean,
detail?: {
path: string,
method: string,
summary: string,
description: string,
parameters: Array,
requestBody: object,
responses: object,
tags: Array
},
error?: string
}
获取完整的接口详情,包括关联的数据模式定义。兼容 OpenAPI 3.0 和 Swagger 2.0。
参数:
path (string): API 路径method (string): HTTP 方法返回:
{
success: boolean,
detail?: {
path: string,
method: string,
summary: string,
description: string,
parameters: Array,
requestBody: object,
responses: object,
tags: Array,
relatedSchemas: object, // 关联的数据模式定义
schemaCount: number
},
error?: string
}
调用 API 接口。支持 JSON 请求和 multipart/form-data 文件上传。
参数:
path (string): API 路径method (string): HTTP 方法params (object): 请求参数
query (object): 查询参数body (object): 请求体(JSON 或 FormData)headers (object): 自定义请求头isFormData (boolean): 是否为 FormData(文件上传)返回:
{
success: boolean,
status?: number,
data?: any,
error?: string
}
示例 - JSON 请求:
const response = await skill.callAPI('/api/users', 'POST', {
body: { name: 'John', email: 'john@example.com' }
});
示例 - 文件上传(使用 FormData):
import FormData from 'form-data';
import fs from 'fs';
const form = new FormData();
form.append('file', fs.createReadStream('./data.jsonl'));
form.append('name', 'My Dataset');
form.append('type', 'train_data');
const response = await skill.callAPI('/api/datasets/', 'POST', {
body: form,
isFormData: true
});
根据自然语言指令调用 API。
参数:
instruction (string): 自然语言指令params (object): 请求参数(同 callAPI)返回:
{
success: boolean,
instruction: string,
matchedAPI?: {
path: string,
method: string,
summary: string,
matchScore: number
},
result: object,
error?: string
}
文件上传方法,支持 multipart/form-data。
参数:
path (string): API 路径formData (object): 表单数据对象
file: 文件内容(Buffer)或文件路径(string)query (object): 查询参数(可选)返回:
{
success: boolean,
status?: number,
data?: any,
error?: string
}
示例:
import SwaggerAPISkill from './index.js';
const skill = new SwaggerAPISkill();
await skill.fetchSwaggerSpec('http://localhost:8000/openapi.json');
// 方式1: 使用文件路径
const result1 = await skill.uploadFile('/api/datasets/', {
file: './test_dataset.jsonl',
name: 'AI知识问答对',
type: 'train_data',
description: '人工智能相关的问答对数据集'
});
// 方式2: 使用 Buffer
import fs from 'fs';
const fileBuffer = fs.readFileSync('./test_dataset.jsonl');
const result2 = await skill.uploadFile('/api/datasets/', {
file: fileBuffer,
name: 'AI知识问答对',
type: 'train_data',
description: '人工智能相关的问答对数据集'
});
获取当前会话ID。
返回:
string // 唯一的会话ID,格式: session_timestamp_randomId
刷新会话,清空所有缓存数据。
返回:
{
success: boolean,
message: string
}
swagger-skill 实现了分层缓存来优化性能和 token 消耗:
getAllAPIs() 和 searchAPI()"METHOD /path" → 完整详情,用于 getAPIDetail() 的 O(1) 查找fetchSwaggerSpec() 时从远程获取规范并构建两层缓存refreshSession() 可清空缓存headers 参数传递认证信息query 参数中提供uploadFile() 方法是最简单的方式,支持文件路径或 BuffercallAPI() 方法配合 FormData 对象进行更灵活的控制components.schemas) 和 Swagger 2.0 (definitions)MIT