新增聊天会话和消息的API接口,支持会话的创建、更新、删除及消息的分页获取和追加。更新README文档以包含新的API信息,并在server.js中注册聊天路由和索引。引入新的脚本用于导入技能和代理资源到MongoDB。
103 lines
2.7 KiB
Markdown
103 lines
2.7 KiB
Markdown
# Skills Market Server
|
||
|
||
Skills Market API 服务,使用 Express + MongoDB。
|
||
|
||
## 安装 MongoDB
|
||
|
||
### Windows
|
||
1. 下载 MongoDB Community Edition: https://www.mongodb.com/try/download/community
|
||
2. 安装后,MongoDB 会作为 Windows 服务自动启动
|
||
3. 默认端口: 27017
|
||
|
||
### 或使用 Docker
|
||
```bash
|
||
docker run -d -p 27017:27017 --name mongodb mongo:latest
|
||
```
|
||
|
||
## 启动服务
|
||
|
||
```bash
|
||
# 安装依赖
|
||
npm install
|
||
|
||
# 启动服务
|
||
npm start
|
||
|
||
# 开发模式(自动重启)
|
||
npm run dev
|
||
```
|
||
|
||
## API 接口
|
||
|
||
| 接口 | 方法 | 说明 |
|
||
|------|------|------|
|
||
| `/api/skills` | GET | 获取 skills 列表 |
|
||
| `/api/skills/:name` | GET | 获取 skill 详情 |
|
||
| `/api/skills/:name/download` | GET | 下载 skill(增加下载计数) |
|
||
| `/api/skills/:name/publish` | POST | 发布/更新 skill |
|
||
| `/api/skills/:name/versions` | GET | 获取版本历史 |
|
||
| `/api/skills/:name/versions/:version` | GET | 获取特定版本 |
|
||
| `/api/skills/:name` | DELETE | 删除 skill |
|
||
| `/api/health` | GET | 健康检查 |
|
||
| `/api/stats` | GET | 统计信息 |
|
||
|
||
### 云端会话 `/api/chat/*`(需 Bearer JWT,与发布 skill 同源鉴权)
|
||
|
||
| 接口 | 方法 | 说明 |
|
||
|------|------|------|
|
||
| `/api/chat/sessions` | GET | 列出当前用户的会话 |
|
||
| `/api/chat/sessions` | POST | 创建会话(body 含客户端生成的 `id`) |
|
||
| `/api/chat/sessions/:id` | PATCH | 更新会话字段 |
|
||
| `/api/chat/sessions/:id` | DELETE | 删除会话及其消息 |
|
||
| `/api/chat/sessions/:id/messages` | GET | 分页消息,`view=user`(裁剪 tool/thinking 等)或 `view=full` |
|
||
| `/api/chat/sessions/:id/messages` | POST | 追加消息 |
|
||
| `/api/chat/messages/:messageId` | PATCH | 更新单条消息 |
|
||
| `/api/chat/sessions/:id/messages/all` | DELETE | 清空该会话全部消息 |
|
||
| `/api/chat/sessions/:id/messages/from/:fromSort` | DELETE | 删除 `sort_order >= fromSort` 的消息(截断) |
|
||
|
||
## 环境变量
|
||
|
||
创建 `.env` 文件:
|
||
|
||
```
|
||
PORT=3001
|
||
MONGO_URL=mongodb://localhost:27017
|
||
DB_NAME=skills_market
|
||
```
|
||
|
||
## MongoDB 数据结构
|
||
|
||
```javascript
|
||
// skills 集合
|
||
{
|
||
"_id": ObjectId("..."),
|
||
"name": "agent-browser",
|
||
"description": "Browser automation...",
|
||
"owner": "user123",
|
||
"downloads": 150,
|
||
"is_public": true,
|
||
"tags": ["browser", "automation"],
|
||
|
||
// 文件数组
|
||
"files": [
|
||
{ "path": "SKILL.md", "content": "---\nname: ..." },
|
||
{ "path": "references/commands.md", "content": "# Commands..." },
|
||
{ "path": "templates/auth.sh", "content": "#!/bin/bash..." }
|
||
],
|
||
|
||
// 版本历史
|
||
"versions": [
|
||
{
|
||
"version": 1,
|
||
"description": "Initial version",
|
||
"files": [ /* 快照 */ ],
|
||
"created_at": ISODate("..."),
|
||
"created_by": "user123"
|
||
}
|
||
],
|
||
|
||
"created_at": ISODate("..."),
|
||
"updated_at": ISODate("...")
|
||
}
|
||
```
|