Compare commits
49
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3df103cd45 | ||
|
|
c9dd5e48bd | ||
|
|
47a0b9e50d | ||
|
|
490afd6977 | ||
|
|
7dd6e5d12d | ||
|
|
e0cba62a0c | ||
|
|
ba20a4f39d | ||
|
|
02c154ae24 | ||
|
|
2f0b056a63 | ||
|
|
7212e0dfee | ||
|
|
29e4690982 | ||
|
|
1eeaf38009 | ||
|
|
1fd04e2607 | ||
|
|
92f0f16070 | ||
|
|
0e2f7c219f | ||
|
|
1e7f3e76ee | ||
|
|
938a99cabe | ||
|
|
3c9f9d2bdb | ||
|
|
4a36cc0266 | ||
|
|
af2e4a1c20 | ||
|
|
4333a53dd3 | ||
|
|
7d855454a8 | ||
|
|
1859fe0d5b | ||
|
|
925cf18da9 | ||
|
|
54c33dd7f1 | ||
|
|
4ff8b93025 | ||
|
|
ae9b78c661 | ||
|
|
ca8099b158 | ||
|
|
e55d209f59 | ||
|
|
8096d42926 | ||
|
|
4aa8f01610 | ||
|
|
488734641d | ||
|
|
85984e1ee2 | ||
|
|
71b83bb523 | ||
|
|
45e4720453 | ||
|
|
424bdcbd52 | ||
|
|
bba42ae2ca | ||
|
|
05d03f0836 | ||
|
|
e928fd7cb0 | ||
|
|
3139108838 | ||
|
|
c750393c81 | ||
|
|
6bd1fa48f8 | ||
|
|
6a0aed2611 | ||
|
|
af0575dddd | ||
|
|
25133f90ed | ||
|
|
ac052d6997 | ||
|
|
67d3355f44 | ||
|
|
60d8278610 | ||
|
|
772408fb6e |
@@ -0,0 +1,53 @@
|
||||
name: agent-device release
|
||||
|
||||
# 只认设备端标签(agent-device-v0.1.0),不碰主项目的 v2.5.0 这类版本号。
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'agent-device-v*'
|
||||
workflow_dispatch: # 兜底:页面上手动跑,填一个已存在的标签
|
||||
inputs:
|
||||
tag:
|
||||
description: '要发布的标签(已存在),如 agent-device-v0.1.0'
|
||||
required: true
|
||||
|
||||
permissions:
|
||||
contents: write # action-gh-release 需要建 release、传附件
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: device/go.mod
|
||||
cache-dependency-path: device/go.sum
|
||||
|
||||
# 复用仓库里的脚本,本地和 CI 一套目标列表
|
||||
- name: 编译各平台
|
||||
working-directory: device
|
||||
run: sh scripts/build.sh all
|
||||
|
||||
- name: 发布 Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
name: ${{ inputs.tag || github.ref_name }}
|
||||
tag_name: ${{ inputs.tag || github.ref_name }}
|
||||
files: device/bin/agent-device-*
|
||||
fail_on_unmatched_files: true
|
||||
body: |
|
||||
设备执行端 agent-device(`device/`)。
|
||||
|
||||
- 设备主动出站连 core 的 `/dev/ws`,不需要公网 IP、不需要开端口
|
||||
- 二进制不含服务器地址与 token,启动时用 `-server` 或同目录 `config.json` 指定
|
||||
- 模型侧可用 `device` 工具跑命令、看负载、收发文件
|
||||
|
||||
| 平台 | 文件 |
|
||||
| --- | --- |
|
||||
| Linux amd64 / arm64 / mips | `agent-device-linux-*` |
|
||||
| macOS amd64 / arm64 | `agent-device-darwin-*` |
|
||||
| Windows amd64 | `agent-device-windows-amd64.exe` |
|
||||
|
||||
`linux-mips` 是软浮点(`GOMIPS=softfloat`),给 AR9331 这类没 FPU 的 OpenWrt 设备用。
|
||||
@@ -14,8 +14,14 @@ local/config.local.yml
|
||||
skills/office/bin/
|
||||
skills/**/__pycache__/
|
||||
*.log
|
||||
# 读图压缩缓存(会话工作目录 = 仓库根时落在仓库里)
|
||||
.imgcache/
|
||||
.DS_Store
|
||||
app/server/version.json
|
||||
*.apk
|
||||
# 部署配置真身(含密钥,模板见 scripts/deploy.conf.example);
|
||||
deploy.conf
|
||||
relay/config.json
|
||||
|
||||
# 设备端编译产物(交叉编译后本地生成,不入库)
|
||||
device/bin/
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
`/chat/completions`)→ 模型调用内置工具 / MCP / 技能 → 命令在 `agent-executor` 隔离容器里执行 →
|
||||
产物落在会话工作目录并回传。每个会话 = 一个真实 Linux 工作目录 + 一条 SQLite 会话记录。
|
||||
|
||||
另有一个可选的**设备执行端** `device/`:设备主动出站连 core 的 `/dev/ws`,模型用 `device`
|
||||
工具在远端机器(Mac / Windows / OpenWrt 盒子)上跑命令、拉推文件。
|
||||
|
||||
设计基调:**单进程内存模型 + SQLite 同步 API**(JS 单线程,不存在跨进程竞争);**配置冷加载**
|
||||
(改 `config.yml` 需重启,少数写盘接口例外);**单租户**(登录只把门,不做资源级授权)。
|
||||
|
||||
@@ -21,10 +24,12 @@
|
||||
| `app/server/` | 后端全部代码(入口 `server.js`) | 见 `app/AGENTS.md` |
|
||||
| `app/server/public/` | 前端静态资源,经典脚本按序加载,共享全局作用域 | 无构建步骤 |
|
||||
| `executor/` | **agent-executor**:`/exec` 隔离命令执行 + Office 预览网关 | 零依赖 Node,独立容器 |
|
||||
| `device/` | **agent-device**:设备执行端,出站连 core 的 `/dev/ws`,提供 exec / 文件传输 | Go 单二进制,交叉编译(amd64/arm64/mips/mipsle/windows),见 `device/README.md` |
|
||||
| `wxbot/` | 微信 iLink 协议调制解调器(多实例) | 独立容器 `agent-wxbot` |
|
||||
| `mcp/` | 同栈 MCP 服务:`web_search` / `image_task` / `video_task` / `asr` | 四个容器,仅内网 |
|
||||
| `skills/` | 目录式技能(`SKILL.md` + `run.py`/`run.sh`) | 运行时挂载,`skills/deps` 为依赖层 |
|
||||
| `local/` | **本地端启动器**:同一份代码跑在本机(不起 Docker / executor),`start.sh` 负责探测、装依赖、守护 | 独立于 compose,见 `docs/local-mode.md` |
|
||||
| `tunnel/` | **反向隧道**:服务器端 `server.js` 按 Host / 固定端口 / 通配路由转发,本机 `client.js` 承接 | 独立 compose,见 `tunnel/README.md` |
|
||||
| `scripts/` | `init.sh` 生成 `.env` + 首次创建 `config.yml`(仅初始管理员);`init_host.sh` 裸机初始化 | 幂等,**不覆盖已有配置** |
|
||||
| `docs/` | 专题文档:`tool-design.md`(工具接口规范)、`OFFICE.md` | — |
|
||||
| `tasks/` | **运行时数据**:每个会话一个工作目录 | gitignore |
|
||||
@@ -38,13 +43,17 @@
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
CORE["agent-core(8090,唯一对外端口)"]
|
||||
B["浏览器 / PWA"] -->|"HTTP + SSE"| CORE
|
||||
M["手机 / 外网"] -->|"HTTPS"| TUN["tunnel server(19000,仅回环)"]
|
||||
TUN -->|"WebSocket"| TC["tunnel client(本机)"]
|
||||
TC -->|"HTTP"| CORE
|
||||
W["微信客户端"] --> WXBOT["agent-wxbot(8093,仅内网)"]
|
||||
WXBOT -->|"回调 /wxhook/*(共享 Bearer token)"| CORE
|
||||
CORE["agent-core(8090,唯一对外端口)"]
|
||||
CORE --> EXEC["agent-executor(9000 exec / 3100 预览,仅内网)"]
|
||||
CORE --> MCP["mcp_web_search / mcp_image_task / mcp_video_task / mcp_asr(3000,仅内网)"]
|
||||
CORE -->|"分段投递回复"| WXBOT
|
||||
DEV["设备:Mac / Windows / OpenWrt"] -->|"wss 出站 /dev/ws"| CORE
|
||||
```
|
||||
|
||||
- **对外只暴露 `agent-core` 一个端口**(宿主 `AGENT_PORT`,默认 8090)。executor、wxbot、MCP
|
||||
@@ -53,12 +62,15 @@ flowchart TD
|
||||
- 容器间按容器名访问(`http://agent-executor:9000`、`http://mcp_web_search:3000/`);
|
||||
所有业务容器同在 `agent-mcp-net`(compose 里固定网络名,避免被加上项目名前缀)。
|
||||
- `agent-executor-init` 是一次性容器:把技能依赖装进 `./skills/deps` 后退出,属正常现象。
|
||||
- 设备(`device/`)**不需要任何入站端口**:设备主动连 `wss://<PUBLIC_BASE_URL 的 host>/dev/ws`,
|
||||
core 按 `DEVICE_TOKEN` 校验。本地部署没有公网前缀,端点不挂,`device` 工具直接回“未启用”。
|
||||
|
||||
### 端口与网络(要改先看这里)
|
||||
|
||||
| 服务 | 端口 | 可见性 |
|
||||
|---|---|---|
|
||||
| `agent-core` | 8090 | 宿主机映射,**唯一对外** |
|
||||
| `tunnel` server | 19000 | 仅 `127.0.0.1`,对外经 nginx 反代 |
|
||||
| `agent-executor` | 9000(exec)、3100(预览网关) | 仅 `expose`,内网 |
|
||||
| `agent-wxbot` | 8093 | 仅内网 |
|
||||
| `mcp_*` | 3000 | 仅内网 |
|
||||
@@ -72,6 +84,7 @@ flowchart TD
|
||||
| `./skills/` | `/workspace/skills` | `/workspace/skills:ro` | 技能与共享依赖 |
|
||||
| `./config.yml` | `/app/config.yml` | 不挂、不读 | 平台密钥、登录密钥 |
|
||||
| `./app/server/` | `/app/server` | — | 后端代码(**热挂载**) |
|
||||
| `./tunnel/data/` | — | — | 隧道服务端配置 `server.yml`(首次启动自动生成,含 token) |
|
||||
| `./wxbot/data/` | — | `/data`(wxbot) | 微信登录凭证 |
|
||||
|
||||
推论:
|
||||
@@ -84,12 +97,12 @@ flowchart TD
|
||||
### 环境变量(接线全靠根 `.env` + compose)
|
||||
|
||||
`AGENT_CONFIG`、`PORT`、`EXECUTOR_URL`、`EXECUTOR_TOKEN`、`WXBOT_URL`、`WXBOT_TOKEN`、
|
||||
`PUBLIC_BASE_URL`、`ASR_URL`、`PREVIEW_PROXIES`、`EXECUTOR_LOG_LEVEL`、`WXBOT_LOG_LEVEL`、`ASR_LOG_LEVEL`。
|
||||
`PUBLIC_BASE_URL`、`DEVICE_TOKEN`、`ASR_URL`、`PREVIEW_PROXIES`、`EXECUTOR_LOG_LEVEL`、`WXBOT_LOG_LEVEL`、`ASR_LOG_LEVEL`。
|
||||
密钥生成:`./scripts/init.sh`(幂等,重跑不掉登录态;**从不覆盖已有 config.yml**,只在其缺失时创建
|
||||
最小骨架)。语音识别的上游 key/base/model 不在 env,
|
||||
写在 `config.yml` 的 `mcp_servers[name=asr].headers`(容器不存平台密钥)。
|
||||
|
||||
## 三条主链路
|
||||
## 四条主链路
|
||||
|
||||
1. **Web / HTTP**:`app/server/server.js` 装配 → 路由 → `engine.js` 起回合 →
|
||||
`llm.js` 组装并流式调用上游 → `tools/executor.js` 执行工具(`terminal` / `skill` 走
|
||||
@@ -97,7 +110,10 @@ flowchart TD
|
||||
2. **微信**:`agent-wxbot` 收消息 → 回调 core `/wxhook/inbox` → `wechat.js` 落媒体、注入上下文 →
|
||||
`routes/channel.js` 的命令层(`/new` `/switch` `/think` …)→ 同一套 `engine`;回合结束由
|
||||
SSE 内部订阅者(`bus.onInternal`)分段推回微信。
|
||||
3. **定时任务**:`scheduler.js` 每 30s tick → 到期则先 `archiveHistory`(历史整体 `archived`,
|
||||
3. **远端设备**:设备上的 `agent-device` 出站连 core `/dev/ws` → `devices.js` 维护连接表 →
|
||||
模型调 `device` 工具(`list` / `health` / `exec` / `pull` / `push`)→ 命令经 WS 下发、输出分片
|
||||
回传;`pull` 落到会话目录后,`read_file` / 技能可直接消费。
|
||||
4. **定时任务**:`scheduler.js` 每 30s tick → 到期则先 `archiveHistory`(历史整体 `archived`,
|
||||
模型等价新会话)→ `engine.postUserMessage` 投递 → 结束后 `wechat.notifyTask` 通知。
|
||||
|
||||
## 数据与状态
|
||||
@@ -127,7 +143,9 @@ flowchart TD
|
||||
「回合中会话数 → 在用会话数 → 最久未用」择优(详见 `app/AGENTS.md`)。
|
||||
7. **运行时目录不入库**。`tasks/`、`data/`、`skills/deps/`、`wxbot/data/` 已被 gitignore;
|
||||
不要把样例任务、数据库、密钥当成源码提交。
|
||||
8. **工具 schema 是每轮全量重发的上下文成本**。新增工具前先读 `docs/tool-design.md`:能用已有工具
|
||||
8. **设备是远端 shell**。`device exec` 在设备上就是 `sh -c` / `cmd /c`;`root` 只约束**起始
|
||||
cwd**,命令里 `cd /etc` 照样能跑。`DEVICE_TOKEN` 泄漏等价于那台机器的 shell 泄漏,按这个等级管理。
|
||||
9. **工具 schema 是每轮全量重发的上下文成本**。新增工具前先读 `docs/tool-design.md`:能用已有工具
|
||||
的 `op` 扩展就不要新开工具。
|
||||
|
||||
## 开发与验证
|
||||
@@ -163,6 +181,7 @@ curl -s http://127.0.0.1:8090/api/health
|
||||
| 配置默认值 | `app/server/config.js` 的 `DEFAULTS` ↔ `app/config.example.yml` |
|
||||
| 内置工具 | `app/server/tools/definitions.json` ↔ `app/AGENTS.md` / README 工具表 ↔ `docs/tool-design.md` 规范 |
|
||||
| 新增服务 / 端口 / 卷 | `docker-compose.yml` ↔ `README.md` 的部署与安全章节 |
|
||||
| 隧道 token | `tunnel/data/server.yml`(自动生成)↔ `tunnel/client.yml`(手写),两边必须一致 |
|
||||
| 新增环境变量 | compose ↔ README「环境变量」 |
|
||||
| 发版号 | `app/package.json` 的 `version` ↔ `app/server/routes/system.js` 的 `VERSION` 字面量(两者不自动同步) |
|
||||
| 技能格式 | `skills/README.md` ↔ `app/server/tools/skills.js` 的解析 |
|
||||
@@ -197,5 +216,8 @@ curl -s http://127.0.0.1:8090/api/health
|
||||
| 改微信投递 | `app/server/wechat.js`(业务)+ `app/server/routes/wechat.js`(HTTP/鉴权) |
|
||||
| 改语音输入 | `app/server/public/js/voice.js`(录音与插入)+ `app/server/routes/asr.js`(代理)+ `mcp/asr/`(转写);客户端桥契约见 `docs/voice-input.md` |
|
||||
| 改隔离执行 | `executor/server.js`(先读顶部安全模型注释) |
|
||||
| 改设备执行 / 文件传输 | `app/server/devices.js`(连接表 + exec/health/pull/push)+ `app/server/tools/executor.js` 的 `toolDevice` |
|
||||
| 改设备端行为 | `device/cmd/agent/`(协议 `device/pkg/proto`,连接基座 `device/pkg/wsnode`) |
|
||||
| 改隧道路由 / 转发 | `tunnel/server.js`(按 Host / 端口匹配)+ `tunnel/client.js`(本地服务映射) |
|
||||
| 加同栈 MCP 服务 | `mcp/<name>/` + `docker-compose.yml` + `config.yml` 的 `mcp_servers` |
|
||||
| 加技能 | `skills/<name>/`(`SKILL.md` + `run.py`),见 `skills/README.md` |
|
||||
|
||||
@@ -28,6 +28,8 @@ macOS / Linux,Node 20+,不需要 Docker:
|
||||
|
||||
首次运行会让你选一个工作目录(技能、会话、数据库都在里面)。命令直接在本机执行,没有沙箱隔离,只监听 `127.0.0.1`。
|
||||
|
||||
需要从外网访问时,配好 [`tunnel/client.yml`](tunnel/README.md) 后 `start.sh` 会一并拉起隧道 client;未配置则跳过。
|
||||
|
||||
## 组成
|
||||
|
||||
| 目录 | 说明 |
|
||||
@@ -38,6 +40,30 @@ macOS / Linux,Node 20+,不需要 Docker:
|
||||
| [`mcp/`](mcp/README.md) | 四个 MCP 服务:网页搜索、图片生成、视频生成、语音转写 |
|
||||
| [`skills/`](skills/README.md) | 内置技能 |
|
||||
| [`local/`](local/README.md) | 本机模式启动器 |
|
||||
| [`tunnel/`](tunnel/README.md) | 反向隧道:把本机服务经服务器中转,供外网访问 |
|
||||
| [`device/`](device/README.md) | 设备执行端:任意机器装上就连回来,模型可在上面跑命令、传文件 |
|
||||
|
||||
## 远端设备
|
||||
|
||||
把任意一台机器(Mac / Linux / Windows / OpenWrt 盒子)接进来,让模型在它上面执行命令、读写文件:
|
||||
|
||||
1. 编译或下载 `device/` 的二进制,和 `config.json` 放在同一个目录:
|
||||
|
||||
```json
|
||||
{ "server": "wss://你的域名/dev/ws", "token": "<DEVICE_TOKEN>", "root": "/Users/you", "name": "mac-01" }
|
||||
```
|
||||
|
||||
2. 直接运行(默认读同目录的 `config.json`,token 不会出现在 `ps` 里):
|
||||
|
||||
```bash
|
||||
./agent-device-darwin-amd64
|
||||
```
|
||||
|
||||
3. 服务器 `.env` 里配同一个 `DEVICE_TOKEN` 并重启 core。
|
||||
|
||||
设备**主动出站**连接,不需要公网 IP、不需要开端口。连上后模型侧多出 `device` 工具:
|
||||
`list` 看在线设备、`health` 看负载、`exec` 跑命令、`pull`/`push` 收发文件(单个 2 GiB 以内)。
|
||||
盒子上的常驻用系统自带的服务管理器(systemd / procd / schtasks),细节见 [`device/README.md`](device/README.md)。
|
||||
|
||||
## 配置
|
||||
|
||||
@@ -68,6 +94,7 @@ docker compose up -d --build
|
||||
## 安全
|
||||
|
||||
- 对外只开 agent-core 一个端口,其余服务只在 Docker 内网
|
||||
- 隧道只经服务器 nginx(TLS)对外,宿主机端口仅绑 `127.0.0.1`;`server.yml` 与 `client.yml` 的 token 必须一致
|
||||
- 命令在 executor 容器里执行,只挂当前会话目录,根文件系统只读
|
||||
- 单租户设计:除管理员接口外不做用户级资源隔离,不要开放给互不信任的人
|
||||
- `config.yml`、`.env`、`wxbot/data/` 存密钥,已加入 `.gitignore`
|
||||
|
||||
+8
-1
@@ -51,7 +51,7 @@ AGENT_CONFIG=./config.yml node server/server.js
|
||||
| `server/wechat.js` | 微信绑定、媒体落盘、上下文注入、SSE→微信分段投递、任务通知 |
|
||||
| `server/routes/channel.js` | 文本命令层(`/new` `/switch` `/think` …),微信与外部接入端共用 |
|
||||
| `server/routes/*.js` | HTTP API,按资源分文件 |
|
||||
| `server/sse.js` | 按会话分组的 SSE 总线(`addClient`/`emit`/`onInternal`) |
|
||||
| `server/sse.js` | 按会话分组的 SSE 总线(`addClient`/`emit`/`onInternal`);`assistant_delta` / `reasoning_delta` / `tool_call_args` 攒 50ms 或 8 条合并写入 |
|
||||
| `server/auth.js` | HMAC cookie 登录、Basic 兜底、`requireAdmin`、免认证路径 |
|
||||
| `server/memory.js` | 长期记忆(日记碎片)读写与检索,仅依赖 `time.js` |
|
||||
| `server/time.js` | 业务时区单一入口:`TZ`、`dayString`、`stampString`、`fmtTime`、`fmtIso` |
|
||||
@@ -211,6 +211,11 @@ IM 用户在实时对话,回合中插话应当“当前这步跑完就插”
|
||||
`{content, reasoning, tool_calls, finish_reason, usage, aborted, sseFrames}`;正文开头混入的
|
||||
`<think>` 块会被 `splitInlineThinking` 剥离到 reasoning。
|
||||
|
||||
增量上下文(模型配了 `store: true` 且带 `opts.context_id`):只发上次之后新增的消息,带
|
||||
`X-Context-Id` / `X-Context-Hash`;relay 回 409 说明缓存对不上(压缩、relay 重启、TTL 过期),
|
||||
改发全量重试**一次**。指针存 `ctxState`(进程内存,只存 hash + 条数),进程重启即失效 →
|
||||
下一轮自动全量。上游仍然是全量,语义不变。
|
||||
|
||||
## 工具层
|
||||
|
||||
- 工具全集 = `definitions.json`(14 个内置)+ MCP 发现结果;再依次叠 profile 的 `tools_allow`
|
||||
@@ -354,6 +359,8 @@ preview-proxy.mount(末尾兜底)
|
||||
JS/CSS 改动不需要。
|
||||
- 另有 `login.html`、`memory.html`(日记调试页)。
|
||||
- API 调用统一走 `core.js` 的 `req()`;SSE 用 `EventSource` + 前端看门狗。
|
||||
- 文件面板(`files.js`)按目录懒加载:只请求 `GET /api/sessions/:id/tree?dir=<rel>` 的当前层,
|
||||
进入子目录才拉下一层;会话详情接口不返回全量 `files`。
|
||||
|
||||
## 扩展指南
|
||||
|
||||
|
||||
Generated
+22
@@ -14,6 +14,7 @@
|
||||
"multer": "1.4.5-lts.1",
|
||||
"sharp": "0.35.4",
|
||||
"turndown": "7.2.0",
|
||||
"ws": "^8.23.0",
|
||||
"yaml": "2.6.1"
|
||||
}
|
||||
},
|
||||
@@ -2002,6 +2003,27 @@
|
||||
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
||||
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "8.23.0",
|
||||
"resolved": "https://registry.npmmirror.com/ws/-/ws-8.23.0.tgz",
|
||||
"integrity": "sha512-wCkmX8QMOrKjgO8HW2WyDbTSGeqkocxgDzS9iR79rEAexty6/2qzilx3Ag8Tqf9ykop3irLKle0MtVn5qjVnoQ==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": ">=5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/xtend": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
"multer": "1.4.5-lts.1",
|
||||
"sharp": "0.35.4",
|
||||
"turndown": "7.2.0",
|
||||
"ws": "^8.23.0",
|
||||
"yaml": "2.6.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +102,10 @@ const DEFAULTS = {
|
||||
{ tool: 'fetch', side: 'result', rounds: 2, turns: 1, status_regex: '^HTTP (\\d{3})' },
|
||||
{ tool: 'read_file', side: 'result', rounds: 2, turns: 1 },
|
||||
{ tool: 'web_search', side: 'result', rounds: 2, turns: 1 },
|
||||
// terminal 输出占了会话上下文的大头(实测 69%),但单条都不大(最大 10KB),
|
||||
// 所以阈值降到 1500(实测拐点:>1500 裁 43%,>2000 只裁 33%)。
|
||||
// rounds:5 比其它规则更晚动手——terminal 输出经常还要回头看;turns:2 保证当前回合绝不裁。
|
||||
{ tool: 'terminal', side: 'result', rounds: 5, turns: 2, min_chars: 1500 },
|
||||
],
|
||||
} },
|
||||
// 大图上传预处理(浏览器端 canvas 压缩,参数经 /api/system/status 下发前端)。
|
||||
@@ -236,6 +240,9 @@ function normalizePlatforms(rawPlatforms, profileNames) {
|
||||
// 非空时发请求前给历史 assistant 行挂该字段;空(缺省)= 不回传。纯发送侧旋钮,
|
||||
// 与接收解析无关(接收早已兼容 reasoning/reasoning_content 双名)。
|
||||
reasoning_ack_field: typeof m.reasoning_ack_field === 'string' ? m.reasoning_ack_field.trim() : '',
|
||||
// 选填:该模型的请求走增量上下文(context relay),只有配合 relay 生效。
|
||||
// 不配 = 每轮重传全量,和以前完全一样。
|
||||
store: m.store === true,
|
||||
// 选填:自动压缩阈值(用户自定义的「上下文用量线」,**不是**模型真实窗口)。
|
||||
// 回合内 In token 峰值达到它即自动压缩上下文(见 engine.runLoopInner 循环顶部);0/未配 = 不启用。
|
||||
max_context: Math.max(0, parseInt(m.max_context, 10) || 0),
|
||||
@@ -363,6 +370,7 @@ function parseAndValidate(rawText, configPath) {
|
||||
extra_params: m.extra_params, thinking_params: m.thinking_params,
|
||||
reasoning_ack_field: m.reasoning_ack_field,
|
||||
max_context: m.max_context,
|
||||
store: m.store,
|
||||
profile: m.profile,
|
||||
url: p.url, api_key: k.api_key, platform: p.name, type: p.type, concurrency: k.concurrency,
|
||||
platform_index: pi, key_index: ki, model_index: mi,
|
||||
|
||||
@@ -0,0 +1,448 @@
|
||||
// 设备(远端执行节点)接入层。
|
||||
//
|
||||
// 设备主动出站连 wss://<公网前缀>/dev/ws,core 维护连接表;工具调用时把命令推下去、
|
||||
// 把 stdout/stderr/退出码收回来。设备侧不需要任何入站端口,所以家里的盒子、公司的
|
||||
// Windows 都能直接在 NAT 后面接进来。
|
||||
//
|
||||
// 启用条件:有公网前缀(config.yml public_link.base_url 或 env PUBLIC_BASE_URL)
|
||||
// 且配了 DEVICE_TOKEN。本地部署两者都没有,设备也连不进来,端点干脆不挂。
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
const WebSocket = require('ws');
|
||||
const log = require('./log');
|
||||
|
||||
const PATH = '/dev/ws';
|
||||
const PING_MS = 20000; // nginx 默认 60s 空闲断,20s 足够安全
|
||||
const DEAD_SEC = 61; // 超过这么久没任何消息 → 判假死
|
||||
const HELLO_MS = 10000; // 连上后多久没 hello 就踢
|
||||
const DEFAULT_TIMEOUT_MS = 120000;
|
||||
const MAX_TIMEOUT_MS = 600000;
|
||||
const MAX_OUT_CHARS = 256 * 1024; // 单流回传上限,超出截断(免得一次把上下文塞爆)
|
||||
|
||||
const state = {
|
||||
enabled: false,
|
||||
token: '',
|
||||
wss: null,
|
||||
byName: new Map(), // name -> device
|
||||
pending: new Map(), // 请求 id -> { onChunk, onExit, onHealth }
|
||||
};
|
||||
|
||||
function init(cfg) {
|
||||
const base = String((cfg.public_link && cfg.public_link.base_url) || '').trim();
|
||||
state.token = String(process.env.DEVICE_TOKEN || '').trim();
|
||||
state.enabled = !!(base && state.token);
|
||||
if (state.enabled) {
|
||||
let host = base;
|
||||
try { host = new URL(base).host; } catch (_) { /* 保留原文 */ }
|
||||
log.info('devices', '设备接入已启用:wss://' + host + PATH);
|
||||
} else {
|
||||
log.info('devices', '设备接入未启用(' + (base ? '缺 DEVICE_TOKEN' : '无公网前缀') + ')');
|
||||
}
|
||||
return state.enabled;
|
||||
}
|
||||
|
||||
function enabled() {
|
||||
return state.enabled;
|
||||
}
|
||||
|
||||
function tokenOK(given) {
|
||||
if (!state.token) return false;
|
||||
const a = Buffer.from(String(given || ''));
|
||||
const b = Buffer.from(state.token);
|
||||
return a.length === b.length && crypto.timingSafeEqual(a, b);
|
||||
}
|
||||
|
||||
function attach(server) {
|
||||
if (!state.enabled) return;
|
||||
state.wss = new WebSocket.Server({ noServer: true, maxPayload: 8 * 1024 * 1024 });
|
||||
server.on('upgrade', (req, socket, head) => {
|
||||
let url;
|
||||
try { url = new URL(req.url, 'http://x'); } catch (_) { return; }
|
||||
if (url.pathname !== PATH) return; // 不认识的 upgrade 不归这层管
|
||||
const hdr = String(req.headers.authorization || '');
|
||||
const bearer = hdr.toLowerCase().startsWith('bearer ') ? hdr.slice(7).trim() : '';
|
||||
if (!tokenOK(bearer || url.searchParams.get('token'))) {
|
||||
log.warn('devices', '设备接入被拒:token 不匹配(来自 ' + (req.socket.remoteAddress || '?') + ')');
|
||||
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
|
||||
socket.destroy();
|
||||
return;
|
||||
}
|
||||
state.wss.handleUpgrade(req, socket, head, ws => state.wss.emit('connection', ws, req));
|
||||
});
|
||||
state.wss.on('connection', onConnection);
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const dev of state.byName.values()) {
|
||||
if (now - dev.lastSeen > DEAD_SEC * 1000) {
|
||||
log.warn('devices', '设备无消息超时,断开:' + dev.name);
|
||||
try { dev.ws.terminate(); } catch (_) {}
|
||||
continue;
|
||||
}
|
||||
try { dev.ws.send(JSON.stringify({ t: 'ping' })); } catch (_) {}
|
||||
}
|
||||
}, PING_MS).unref();
|
||||
}
|
||||
|
||||
function onConnection(ws) {
|
||||
const dev = {
|
||||
ws, name: null, os: '', arch: '', shell: '', shellPath: '', shellVer: '', root: '', ver: '',
|
||||
connectedAt: Date.now(), lastSeen: Date.now(), helloTimer: null,
|
||||
};
|
||||
dev.helloTimer = setTimeout(() => {
|
||||
if (!dev.name) { try { ws.close(4001, 'hello timeout'); } catch (_) {} }
|
||||
}, HELLO_MS);
|
||||
|
||||
ws.on('message', raw => {
|
||||
let m;
|
||||
try { m = JSON.parse(String(raw)); } catch (_) { return; }
|
||||
dev.lastSeen = Date.now();
|
||||
if (m.t === 'hello') return onHello(dev, m);
|
||||
if (m.t === 'chunk') {
|
||||
const p = state.pending.get(m.id);
|
||||
if (p && p.onChunk) p.onChunk(m);
|
||||
return;
|
||||
}
|
||||
if (m.t === 'exit') {
|
||||
const p = state.pending.get(m.id);
|
||||
if (p && p.onExit) p.onExit(m);
|
||||
return;
|
||||
}
|
||||
if (m.t === 'file.chunk') {
|
||||
const p = state.pending.get(m.id);
|
||||
if (p && p.onFileChunk) p.onFileChunk(m);
|
||||
return;
|
||||
}
|
||||
if (m.t === 'file.done') {
|
||||
const p = state.pending.get(m.id);
|
||||
if (p && p.onFileDone) p.onFileDone(m);
|
||||
return;
|
||||
}
|
||||
if (m.t === 'health') {
|
||||
const p = state.pending.get(m.id);
|
||||
if (p && p.onHealth) p.onHealth(m);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
clearTimeout(dev.helloTimer);
|
||||
if (dev.name && state.byName.get(dev.name) === dev) {
|
||||
state.byName.delete(dev.name);
|
||||
log.info('devices', '设备下线:' + dev.name);
|
||||
}
|
||||
});
|
||||
ws.on('error', () => {});
|
||||
}
|
||||
|
||||
function onHello(dev, m) {
|
||||
clearTimeout(dev.helloTimer);
|
||||
dev.name = String(m.name || '').trim() || ('dev-' + crypto.randomBytes(3).toString('hex'));
|
||||
dev.os = String(m.os || '');
|
||||
dev.arch = String(m.arch || '');
|
||||
dev.shell = String(m.shell || '');
|
||||
dev.shellPath = String(m.shell_path || '');
|
||||
dev.shellVer = String(m.shell_ver || '');
|
||||
dev.root = String(m.root || '');
|
||||
dev.ver = String(m.ver || '');
|
||||
|
||||
const old = state.byName.get(dev.name);
|
||||
if (old && old !== dev) {
|
||||
log.warn('devices', '同名设备重新接入,踢掉旧连接:' + dev.name);
|
||||
try { old.ws.close(4000, 'replaced'); } catch (_) {}
|
||||
}
|
||||
state.byName.set(dev.name, dev);
|
||||
log.info('devices', `设备上线:${dev.name}(${dev.os}/${dev.arch} shell=${dev.shellPath || dev.shell} ${dev.shellVer} root=${dev.root})`);
|
||||
}
|
||||
|
||||
function pick(name) {
|
||||
if (name) return state.byName.get(String(name).trim()) || null;
|
||||
const first = state.byName.values().next();
|
||||
return first.done ? null : first.value;
|
||||
}
|
||||
|
||||
function list() {
|
||||
const now = Date.now();
|
||||
return [...state.byName.values()].map(d => ({
|
||||
device: d.name,
|
||||
os: d.os,
|
||||
arch: d.arch,
|
||||
// shell 是模型的命令入口:kind 决定调用形式(sh -c / cmd /c),path 决定实际解释器
|
||||
//(/bin/sh 可能是 dash、bash 或 busybox),ver 拿得到就给。
|
||||
shell: d.shell,
|
||||
shell_path: d.shellPath,
|
||||
shell_ver: d.shellVer,
|
||||
root: d.root,
|
||||
ver: d.ver,
|
||||
connected_sec: Math.round((now - d.connectedAt) / 1000),
|
||||
idle_sec: Math.round((now - d.lastSeen) / 1000),
|
||||
}));
|
||||
}
|
||||
|
||||
function health(name) {
|
||||
const dev = pick(name);
|
||||
if (!dev) return Promise.reject(new Error(name ? `设备 ${name} 不在线` : '当前没有设备在线'));
|
||||
const id = newJobId();
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
state.pending.delete(id);
|
||||
reject(new Error(`设备 ${dev.name} 未在 5s 内回应 health`));
|
||||
}, 5000);
|
||||
state.pending.set(id, {
|
||||
onHealth(m) {
|
||||
clearTimeout(timer);
|
||||
state.pending.delete(id);
|
||||
resolve({
|
||||
device: dev.name, os: dev.os, arch: dev.arch,
|
||||
uptime_sec: m.uptime_sec, host_uptime_sec: m.host_uptime_sec,
|
||||
load1: m.load1, load5: m.load5, load15: m.load15,
|
||||
mem_total_mb: m.mem_total_mb, mem_free_mb: m.mem_free_mb,
|
||||
disk_total_gb: m.disk_total_gb, disk_free_gb: m.disk_free_gb, temp_c: m.temp_c,
|
||||
});
|
||||
},
|
||||
});
|
||||
try {
|
||||
dev.ws.send(JSON.stringify({ t: 'health', id }));
|
||||
} catch (e) {
|
||||
clearTimeout(timer);
|
||||
state.pending.delete(id);
|
||||
reject(new Error('下发 health 失败:' + e.message));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function newJobId() {
|
||||
return crypto.randomBytes(6).toString('hex');
|
||||
}
|
||||
|
||||
function exec({ device, command, cwd, timeout_ms }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dev = pick(device);
|
||||
if (!dev) return reject(new Error(device ? `设备 ${device} 不在线` : '当前没有设备在线'));
|
||||
const cmd = String(command || '');
|
||||
if (!cmd.trim()) return reject(new Error('command 不能为空'));
|
||||
const tmo = Math.min(Math.max(Number(timeout_ms) || DEFAULT_TIMEOUT_MS, 1000), MAX_TIMEOUT_MS);
|
||||
const id = newJobId();
|
||||
const started = Date.now();
|
||||
let out = '';
|
||||
let err = '';
|
||||
let truncated = false;
|
||||
let settled = false;
|
||||
|
||||
const done = payload => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(watchdog);
|
||||
state.pending.delete(id);
|
||||
resolve(payload);
|
||||
};
|
||||
|
||||
// 兜底看门狗:设备侧本来就会按 timeout 杀进程,这里再晚 10s 收尸,防止设备掉线后永远挂着
|
||||
const watchdog = setTimeout(() => {
|
||||
done({
|
||||
device: dev.name, code: -1, killed_by: 'watchdog', ms: Date.now() - started,
|
||||
stdout: out, stderr: err, truncated,
|
||||
note: '设备未在超时后回传结果(可能已掉线)',
|
||||
});
|
||||
}, tmo + 10000);
|
||||
|
||||
state.pending.set(id, {
|
||||
onChunk(m) {
|
||||
const text = Buffer.from(String(m.chunk || ''), 'base64').toString('utf8');
|
||||
if (m.out === 'err') {
|
||||
if (err.length + text.length > MAX_OUT_CHARS) { truncated = true; err += text.slice(0, Math.max(0, MAX_OUT_CHARS - err.length)); }
|
||||
else err += text;
|
||||
} else {
|
||||
if (out.length + text.length > MAX_OUT_CHARS) { truncated = true; out += text.slice(0, Math.max(0, MAX_OUT_CHARS - out.length)); }
|
||||
else out += text;
|
||||
}
|
||||
},
|
||||
onExit(m) {
|
||||
done({
|
||||
device: dev.name,
|
||||
code: typeof m.code === 'number' ? m.code : -1,
|
||||
killed_by: m.killed_by || '',
|
||||
ms: Number(m.ms) || Date.now() - started,
|
||||
stdout: out,
|
||||
stderr: err + (m.err ? String(m.err) : ''),
|
||||
truncated: truncated || !!m.truncated,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
dev.ws.send(JSON.stringify({
|
||||
t: 'exec', id, cmd, cwd: cwd ? String(cwd) : '', timeout_ms: tmo,
|
||||
}));
|
||||
} catch (e) {
|
||||
state.pending.delete(id);
|
||||
clearTimeout(watchdog);
|
||||
reject(new Error('下发命令失败:' + e.message));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// ---------- 文件通道 ----------
|
||||
// core 与设备之间拉/推单个文件,复用同一条 WS:256 KiB 一块,base64 传输。
|
||||
// 写盘一律先落 .part,size 与 sha256 都对上才 rename——不留半截文件。
|
||||
const FILE_CHUNK = 256 * 1024; // 每块 256 KiB
|
||||
const MAX_FILE = 2 * 1024 * 1024 * 1024; // 单文件上限 2 GiB
|
||||
const FIRST_CHUNK_MS = 30000; // 请求发出后等第一块(覆盖设备端 open/stat)
|
||||
const IDLE_MS = 30000; // 块与块之间的空闲超时:只看卡不卡,不看总共多久
|
||||
const DONE_WAIT_MS = 60000; // push 发完最后一包后等设备确认
|
||||
const WS_HIGH = 4 * 1024 * 1024; // 发送缓冲高水位,超了就歇一会
|
||||
|
||||
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
||||
|
||||
// 流式算 sha256:2 GiB 不能整块读进内存
|
||||
function hashFile(p) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const h = crypto.createHash('sha256');
|
||||
fs.createReadStream(p)
|
||||
.on('data', c => h.update(c))
|
||||
.on('end', () => resolve(h.digest('hex')))
|
||||
.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
// pull:设备 → 会话目录
|
||||
// 超时按「空闲」算:首块 30s,之后每收到一块重置 30s。稳定但慢的链路不会被误杀。
|
||||
function pull({ device, remotePath, localPath }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dev = pick(device);
|
||||
if (!dev) return reject(new Error(device ? `设备 ${device} 不在线` : '当前没有设备在线'));
|
||||
if (!remotePath) return reject(new Error('remote_path 不能为空'));
|
||||
|
||||
const id = newJobId();
|
||||
const tmp = localPath + '.part';
|
||||
const out = fs.createWriteStream(tmp);
|
||||
const hash = crypto.createHash('sha256');
|
||||
let got = 0;
|
||||
let settled = false;
|
||||
let paused = false;
|
||||
let idle = null;
|
||||
|
||||
const finish = () => {
|
||||
if (settled) return false;
|
||||
settled = true;
|
||||
clearTimeout(idle);
|
||||
state.pending.delete(id);
|
||||
// 背压可能把连接停在 pause 上:结束时必须恢复,否则后续所有消息都收不到
|
||||
if (paused) {
|
||||
paused = false;
|
||||
try { dev.ws.resume(); } catch (_) {}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
const fail = msg => {
|
||||
if (!finish()) return;
|
||||
try { out.destroy(); } catch (_) {}
|
||||
try { fs.unlinkSync(tmp); } catch (_) {}
|
||||
reject(new Error(msg));
|
||||
};
|
||||
const bump = ms => {
|
||||
clearTimeout(idle);
|
||||
idle = setTimeout(() => fail(`${ms / 1000}s 没有收到数据,判定卡住`), ms);
|
||||
};
|
||||
bump(FIRST_CHUNK_MS);
|
||||
|
||||
state.pending.set(id, {
|
||||
onFileChunk(m) {
|
||||
if (settled) return;
|
||||
bump(IDLE_MS);
|
||||
let b;
|
||||
try { b = Buffer.from(String(m.data || ''), 'base64'); } catch (_) { return fail('数据块解码失败'); }
|
||||
got += b.length;
|
||||
if (got > MAX_FILE) return fail('文件超过 2GiB 上限');
|
||||
hash.update(b);
|
||||
// 磁盘比网络慢时暂停从连接读,等 drain 再恢复——否则缓冲会在内存里堆到 OOM
|
||||
if (!out.write(b) && !paused) {
|
||||
paused = true;
|
||||
try { dev.ws.pause(); } catch (_) {}
|
||||
out.once('drain', () => {
|
||||
paused = false;
|
||||
try { dev.ws.resume(); } catch (_) {}
|
||||
});
|
||||
}
|
||||
},
|
||||
onFileDone(m) {
|
||||
if (settled) return;
|
||||
clearTimeout(idle);
|
||||
if (m.err) return fail(String(m.err));
|
||||
out.end(() => {
|
||||
if (!finish()) return;
|
||||
const sum = hash.digest('hex');
|
||||
if (typeof m.size === 'number' && m.size !== got) {
|
||||
try { fs.unlinkSync(tmp); } catch (_) {}
|
||||
return reject(new Error(`大小不符:收到 ${got},设备报 ${m.size}`));
|
||||
}
|
||||
if (m.sha256 && String(m.sha256).toLowerCase() !== sum) {
|
||||
try { fs.unlinkSync(tmp); } catch (_) {}
|
||||
return reject(new Error('sha256 不符'));
|
||||
}
|
||||
try { fs.renameSync(tmp, localPath); } catch (e) { return reject(e); }
|
||||
resolve({ device: dev.name, bytes: got, sha256: sum, path: localPath });
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
dev.ws.send(JSON.stringify({ t: 'file', id, op: 'read', path: remotePath }));
|
||||
});
|
||||
}
|
||||
|
||||
// push:会话目录 → 设备
|
||||
// 发送阶段不设总超时(core 自己知道在发,靠 bufferedAmount 控节奏),
|
||||
// 发完最后一包才开始等设备确认(60s)。
|
||||
async function push({ device, localPath, remotePath }) {
|
||||
const dev = pick(device);
|
||||
if (!dev) throw new Error(device ? `设备 ${device} 不在线` : '当前没有设备在线');
|
||||
if (!remotePath) throw new Error('remote_path 不能为空');
|
||||
|
||||
const st = fs.statSync(localPath);
|
||||
if (!st.isFile()) throw new Error('只能推文件,不能推目录:' + localPath);
|
||||
if (st.size > MAX_FILE) throw new Error('文件超过 2GiB 上限');
|
||||
const size = st.size;
|
||||
const sum = await hashFile(localPath);
|
||||
const id = newJobId();
|
||||
|
||||
let settled = false;
|
||||
let doneTimer = null;
|
||||
let rejectDone = null;
|
||||
const done = new Promise((resolve, reject) => {
|
||||
rejectDone = reject;
|
||||
state.pending.set(id, {
|
||||
onFileDone(m) {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(doneTimer);
|
||||
state.pending.delete(id);
|
||||
if (m.err) return reject(new Error(String(m.err)));
|
||||
resolve({ device: dev.name, bytes: size, sha256: sum, path: remotePath });
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
dev.ws.send(JSON.stringify({ t: 'file', id, op: 'write', path: remotePath, size, sha256: sum }));
|
||||
for await (const chunk of fs.createReadStream(localPath, { highWaterMark: FILE_CHUNK })) {
|
||||
dev.ws.send(JSON.stringify({ t: 'file.chunk', id, data: chunk.toString('base64') }));
|
||||
while (dev.ws.bufferedAmount > WS_HIGH) await sleep(10);
|
||||
}
|
||||
dev.ws.send(JSON.stringify({ t: 'file.done', id }));
|
||||
} catch (e) {
|
||||
settled = true;
|
||||
state.pending.delete(id);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// 发完最后一包才开始看门狗:设备还要算 sha、rename,慢盘上会停几秒
|
||||
doneTimer = setTimeout(() => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
state.pending.delete(id);
|
||||
rejectDone(new Error(`发完最后一包后 ${DONE_WAIT_MS / 1000}s 仍未收到设备确认`));
|
||||
}, DONE_WAIT_MS);
|
||||
|
||||
return done;
|
||||
}
|
||||
|
||||
module.exports = { init, attach, enabled, list, health, exec, pull, push, PATH };
|
||||
+78
-12
@@ -2,7 +2,8 @@
|
||||
// 单进程内存模型:JS 单线程 + better-sqlite3 同步 API,不存在跨进程竞争。
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { buildMessages, callModel, encodeBlocks, decodeBlocks, compressMessages, titleMessages, setProfilePrompts, patchImageReceipt } = require('./llm');
|
||||
const { buildMessages, callModel, encodeBlocks, decodeBlocks, compressMessages, titleMessages, setProfilePrompts, patchImageReceipt, invalidateContext } = require('./llm');
|
||||
const { instanceId } = require('./instance');
|
||||
const { executeTool, resolveInTask } = require('./tools/executor');
|
||||
const publicFiles = require('./public-files');
|
||||
const mcp = require('./mcp');
|
||||
@@ -109,6 +110,29 @@ function loadHistory(sessionId) {
|
||||
return db.prepare('SELECT * FROM messages WHERE session_id = ? ORDER BY id').all(sessionId);
|
||||
}
|
||||
|
||||
// 已被压缩掉的行数 = 「压缩代数」。压缩时会把摘要之前的所有行标成 archived,
|
||||
// 所以这个数单调递增;用它做增量上下文的 id 后缀,压缩后 id 自变,relay 缓存自然失效。
|
||||
function archivedCount(history) {
|
||||
let n = 0;
|
||||
for (const r of history || []) if (r.type === 'archived') n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
// 同上,但按 sessionId 现查:编辑/删除入口拿到的是裸 sessions 行(不含 history),
|
||||
// 用 archivedCount(session.history) 会恒得 0,作废的 cid 就打到别的 key 上(压缩过的会话必踩)。
|
||||
function archivedCountOf(sessionId) {
|
||||
const r = db.prepare("SELECT COUNT(*) AS n FROM messages WHERE session_id = ? AND type = 'archived'").get(sessionId);
|
||||
return r ? r.n : 0;
|
||||
}
|
||||
|
||||
// 增量上下文 id 的拼法(与 llm.js 的 X-Context-Id 一致):<core_id>:<user>:<session_id>:<archived_cnt>。
|
||||
// 删除消息后要把这个 key 作废,让下一轮走全量重建。
|
||||
// runLoop 里 session.history 已就绪,走内存计数;其余入口现查库,两条路结果一致。
|
||||
function contextIdOf(session) {
|
||||
const n = session.history ? archivedCount(session.history) : archivedCountOf(session.id);
|
||||
return instanceId(cfg.data_root) + ':' + (session.user || '-') + ':' + session.id + ':' + n;
|
||||
}
|
||||
|
||||
function saveMessage(sessionId, msg) {
|
||||
const info = db.prepare(
|
||||
'INSERT INTO messages (session_id, role, content, reasoning, tool_calls, tool_call_id, name, type, is_error, usage, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?)'
|
||||
@@ -857,15 +881,26 @@ async function runLoopInner(sessionId, run) {
|
||||
break;
|
||||
} catch (err) {
|
||||
lastErr = err;
|
||||
// 无 upstream_status = 网络层失败或中断(含「停止」「立即发送」的 abort):relay 在收到
|
||||
// 上游 200 头时就已 put,而这边没拿到响应头、指针还停在上一轮,不作废就会在下一轮把
|
||||
// 重复的增量再拼一遍 → tool_call_mismatch。直接清指针,下一轮走全量 + Reset 覆盖。
|
||||
// 有 upstream_status 的 429/5xx 则确定没写缓存,保留指针,重试发空增量最省。
|
||||
if (!err.upstream_status) invalidateContext(contextIdOf(session));
|
||||
const retriable = !err.upstream_status || err.upstream_status === 429 || err.upstream_status >= 500;
|
||||
if (!retriable || attempt === cfg.agent.max_retries) break;
|
||||
// 重试用的是同一份 messages(数组一字未变),所以增量上下文会发空 body,
|
||||
// 只花百来字节就能让 relay 用缓存重发。这行日志是用来对上那笔账的。
|
||||
log.warn('model', sessionId + ' 第 ' + (attempt + 1) + '/' + cfg.agent.max_retries +
|
||||
' 次重试:' + String(err.message || '').slice(0, 100));
|
||||
await sleep(2000 * (attempt + 1));
|
||||
}
|
||||
}
|
||||
if (run.cancelled) break;
|
||||
if (!result) {
|
||||
const text = '⚠️ 模型调用失败:' + (lastErr && lastErr.message || '未知错误');
|
||||
const errId = saveMessage(sessionId, { role: 'assistant', content: text });
|
||||
// type='error_note':留痕给 UI,但不进模型上下文(buildMessages 跳过),
|
||||
// 否则失败文案会当成模型的真实回复一直带下去。
|
||||
const errId = saveMessage(sessionId, { role: 'assistant', content: text, type: 'error_note' });
|
||||
bus.emit(sessionId, 'assistant_message', { id: errId, content: text, error: true, created_at: now() });
|
||||
break;
|
||||
}
|
||||
@@ -936,10 +971,10 @@ async function runLoopInner(sessionId, run) {
|
||||
recordModelUsage(sessionId, session.model_id, result.usage);
|
||||
// 回合级 In token 峰值:工具迭代只会越滚越大,取各轮 prompt_tokens 最大值作自动压缩判据
|
||||
if (msgRow.usage && msgRow.usage.pt > (run.turnMaxPt || 0)) run.turnMaxPt = msgRow.usage.pt;
|
||||
// 「压缩」回合的总结判定:压缩时不传 tools,因此本回合第一条「无 tool_calls 的收尾回复」
|
||||
// 就是摘要(compressing 取自本回合的 run.summarizing 标记);回合中间若有异常产生的
|
||||
// 工具轮(理论上不会)都是普通消息,前端照常渲染。
|
||||
const isSummary = !hasTools && compressing;
|
||||
// 「压缩」回合的总结判定:压缩指令发出去,模型回来的第一段非空文本就是摘要——带不带
|
||||
// 工具都算(模型常为了把摘要写准先调一次工具查证,这一轮的文字同样是摘要,不能当普通
|
||||
// 回复落库)。空文本(纯工具调用、一个字没写)不算,run.summarizing 保持,下一轮继续压。
|
||||
const isSummary = compressing && !!String(msgRow.content || '').trim();
|
||||
bus.emit(sessionId, 'assistant_message', {
|
||||
id: assistantId, content: msgRow.content, reasoning: msgRow.reasoning,
|
||||
tool_calls: msgRow.tool_calls, type: msgRow.type, created_at: msgRow.created_at,
|
||||
@@ -964,14 +999,17 @@ async function runLoopInner(sessionId, run) {
|
||||
log.info('msg', sessionId + ' 压缩摘要 ' + assistantId + ' 落库,存档之前 ' + n + ' 条消息(不再发给模型)');
|
||||
}
|
||||
// 回合内自动压缩把正在跑的任务切成了摘要,摘要一落地就地续跑:
|
||||
// 补一条 user 指令接回原任务,否则会直接走到下面的 !hasTools 收尾,
|
||||
// 任务停在半路,要用户再喊一声才动。
|
||||
// 没有工具回执可用时补一条 user 指令接回原任务,否则会直接走到下面的 !hasTools 收尾,
|
||||
// 任务停在半路,要用户再喊一声才动。有工具回执时不插消息也不 continue——落到下面
|
||||
// 执行工具,回执紧跟在摘要后面,本身就是续跑输入。
|
||||
if (run.compactAuto) {
|
||||
run.compactAuto = false;
|
||||
const resumeText = '上下文已压缩,请继续完成原任务。';
|
||||
const resumeId = saveMessage(sessionId, { role: 'user', content: resumeText });
|
||||
bus.emit(sessionId, 'user_message', { id: resumeId, content: resumeText, created_at: now() });
|
||||
continue;
|
||||
if (!hasTools) {
|
||||
const resumeText = '上下文已压缩,请继续完成原任务。';
|
||||
const resumeId = saveMessage(sessionId, { role: 'user', content: resumeText });
|
||||
bus.emit(sessionId, 'user_message', { id: resumeId, content: resumeText, created_at: now() });
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1018,9 +1056,20 @@ async function runLoopInner(sessionId, run) {
|
||||
output = chk.error;
|
||||
isError = true;
|
||||
} else {
|
||||
// 工具执行中的增量输出(terminal 长命令):50ms 或 16KB 合并成一帧 SSE,避免逐行刷包
|
||||
let liveBuf = '';
|
||||
let liveTimer = null;
|
||||
const flushLive = () => {
|
||||
if (liveTimer) { clearTimeout(liveTimer); liveTimer = null; }
|
||||
if (!liveBuf) return;
|
||||
const chunk = liveBuf;
|
||||
liveBuf = '';
|
||||
bus.emit(sessionId, 'tool_output', { id, chunk });
|
||||
};
|
||||
try {
|
||||
output = await executeTool({ sessionId, taskDir: taskDirOf(sessionId), workDir: sessionWorkDir(sessionId), run, cfg, db, vision,
|
||||
onTaskCreated: () => { run.taskWrap = true; }, onCard: (card) => bus.emit(sessionId, 'ui_card', card),
|
||||
onOutput: (s) => { liveBuf += s; if (liveBuf.length >= 16384) flushLive(); else if (!liveTimer) liveTimer = setTimeout(flushLive, 50); },
|
||||
onDeepThink: (info) => {
|
||||
const m = (cfg.models || []).find(x => x.id === session.model_id);
|
||||
const supported = !!(m && m.thinking_params && Object.keys(m.thinking_params).length);
|
||||
@@ -1033,6 +1082,7 @@ async function runLoopInner(sessionId, run) {
|
||||
output = 'Error: ' + (err && err.message || String(err));
|
||||
isError = true;
|
||||
}
|
||||
flushLive();
|
||||
}
|
||||
// 工具可返回 {text, image_rel}(read_file 读图且模型支持 vision,压缩产物在 .imgcache/,
|
||||
// text 是 imageReceipt 生成的结构化 JSON 回执):落库时以 image_ref 内容块拼进 tool 消息
|
||||
@@ -1159,6 +1209,11 @@ async function streamAssistant(sessionId, run, session, messages, tools) {
|
||||
// key_slot 定位会话占用的具体 key 副本(model_id 只到「平台/模型」)
|
||||
const result = await callModel(cfg, session.model_id, messages, tools, {
|
||||
key_slot: session.model_slot,
|
||||
// 增量上下文用:<core_id>:<user>:<session_id>:<archived_cnt>。
|
||||
// archived_cnt = 已被压缩掉的行数,它就是「压缩代数」:压缩后自增 → id 自变 →
|
||||
// relay 找不到缓存并回 409 → 发全量重建。裁剪不改它(只换内容不删行),
|
||||
// 所以裁剪不会打断增量。只有带 store: true 的模型才真的用它(llm.js 判断)。
|
||||
context_id: contextIdOf(session),
|
||||
signal: run.abortController.signal,
|
||||
shouldCancel: () => run.cancelled,
|
||||
onText: delta => bus.emit(sessionId, 'assistant_delta', { text: delta }),
|
||||
@@ -1835,7 +1890,9 @@ function clearSessionMessages(sessionId) {
|
||||
}
|
||||
runs.delete(sessionId);
|
||||
clearPendings(sessionId); // 历史清零:队列里的消息也没了着落,一并作废
|
||||
const cid = contextIdOf(session); // 删行不影响 archived_cnt,先算后算等价
|
||||
db.prepare('DELETE FROM messages WHERE session_id = ?').run(sessionId);
|
||||
invalidateContext(cid); // 历史清零:增量指针也得跟着作废
|
||||
wipeSessionTmp(sessionId); // 会话级 /tmp 随清空一并抹掉(尽力而为,失败只记日志不阻断)
|
||||
autoCompressProposed.delete(sessionId); // 历史清零,恢复自动压缩提议额度
|
||||
// 标题随历史一并清零:旧标题描述的是已删除的对话,留着会让新对话文不对题;
|
||||
@@ -1871,6 +1928,7 @@ function deleteMessage(sessionId, messageId) {
|
||||
const row = db.prepare('SELECT * FROM messages WHERE id = ? AND session_id = ?').get(messageId, sessionId);
|
||||
if (!row) throw Object.assign(new Error('消息不存在'), { status_code: 404 });
|
||||
if (row.role === 'tool') return deleteToolMessage(sessionId, row);
|
||||
const cid = contextIdOf(session); // 删之前先算:archived_cnt 不受删行影响,两者等价,但先算更直观
|
||||
let removed = 0;
|
||||
if (row.tool_calls) {
|
||||
let calls = [];
|
||||
@@ -1882,6 +1940,9 @@ function deleteMessage(sessionId, messageId) {
|
||||
}
|
||||
}
|
||||
db.prepare('DELETE FROM messages WHERE id = ?').run(messageId);
|
||||
// 删中间一条:后面的行位置全变,但末尾行 id 没变,锚点查不出来——主动作废增量指针,
|
||||
// 下一轮改发 X-Context-Reset 全量重建,否则 relay 缓存里还留着被删那行,增量拼出来会错位。
|
||||
invalidateContext(cid);
|
||||
return { ok: true, id: messageId, removed: removed + 1 };
|
||||
}
|
||||
|
||||
@@ -1899,6 +1960,8 @@ function editUserMessage(sessionId, messageId, content, opts) {
|
||||
// 先确保能开跑:无可用模型时抛 503,避免删完历史却发不出去
|
||||
ensureBinding(session);
|
||||
const removed = db.prepare('DELETE FROM messages WHERE session_id = ? AND id >= ?').run(sessionId, messageId).changes;
|
||||
// 尾部截断:锚点本就能查出,但这里紧接着就重新发消息,一并作废指针更直接
|
||||
invalidateContext(contextIdOf(session));
|
||||
const r = postUserMessage(sessionId, content, opts);
|
||||
log.info('msg', sessionId + ' 编辑消息 ' + messageId + ',丢弃其后 ' + Math.max(0, removed - 1) + ' 条历史');
|
||||
return Object.assign({ removed: removed }, r);
|
||||
@@ -1929,6 +1992,9 @@ function deleteToolMessage(sessionId, row) {
|
||||
}
|
||||
break; // id 唯一,命中即止
|
||||
}
|
||||
// 同上:回执被删、父调用也改过 tool_calls,历史已被控——作废增量指针
|
||||
const s = getSession(sessionId);
|
||||
if (s) invalidateContext(contextIdOf(s));
|
||||
return { ok: true, id: row.id, removed };
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
// 实例标识:跨 core 隔离用,作 relay 的 X-Context-Id 前缀。
|
||||
// 首次启动生成并落盘到 <data_root>/instance-id,之后只读同一个值。
|
||||
// 删了数据目录会换新 id —— 旧缓存成孤儿,由 relay 的 TTL 自行清理,不影响正确性。
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const crypto = require('node:crypto');
|
||||
|
||||
const cache = new Map(); // dataRoot -> id
|
||||
|
||||
function instanceId(dataRoot) {
|
||||
const root = path.resolve(String(dataRoot || '.'));
|
||||
if (cache.has(root)) return cache.get(root);
|
||||
const file = path.join(root, 'instance-id');
|
||||
let id = '';
|
||||
try {
|
||||
id = fs.readFileSync(file, 'utf8').trim();
|
||||
} catch (_) {}
|
||||
if (!/^[0-9a-f]{12}$/.test(id)) {
|
||||
id = crypto.randomBytes(6).toString('hex');
|
||||
try {
|
||||
fs.writeFileSync(file, id + '\n');
|
||||
} catch (_) {}
|
||||
}
|
||||
cache.set(root, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
module.exports = { instanceId };
|
||||
+97
-10
@@ -3,6 +3,11 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// 增量上下文:context_id -> { len }(本进程认为 relay 已存到第几条)。
|
||||
// 只存指针不存正文;进程重启即清空 → 下一轮发全量,relay 整段替换,行为正确。
|
||||
// 不存 hash:id 里已经带 archived_cnt(压缩代数),压缩后 id 自变、缓存自然对不上。
|
||||
const ctxState = new Map();
|
||||
|
||||
const SYSTEM_PROMPT_TEMPLATE = fs.readFileSync(path.join(__dirname, 'templates', 'profile-default.txt'), 'utf8');
|
||||
|
||||
// 上下文压缩提示词(templates/prompt-compress.txt):点「压缩」时不插入指令消息。
|
||||
@@ -305,10 +310,12 @@ function buildMessages(session, language, mintUrl, profile, withProfile, trimOn)
|
||||
history.forEach((row, i) => {
|
||||
// archived:UI 可见,但不进入模型上下文(节省 token)
|
||||
if (row.type === 'archived') return;
|
||||
// error_note:失败文案,只给 UI 留痕,不进模型(否则被当成模型的真实回复带下去)
|
||||
if (row.type === 'error_note') return;
|
||||
// summarize_request(旧版遗留行):仅在未收尾的旧压缩回合内发给模型,其余一律跳过
|
||||
if (row.type === 'summarize_request') {
|
||||
if (i > lastClosingAssistant && (row.content || '').trim()) {
|
||||
messages.push({ role: 'user', content: row.content });
|
||||
messages.push({ role: 'user', content: row.content, __rowId: row.id });
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -338,7 +345,7 @@ function buildMessages(session, language, mintUrl, profile, withProfile, trimOn)
|
||||
});
|
||||
return;
|
||||
}
|
||||
const m = { role: 'assistant', content: row.content || '' };
|
||||
const m = { role: 'assistant', content: row.content || '', __rowId: row.id };
|
||||
// 思维链回传原料:存库 reasoning 随原样带给 callModel(中性内部键,按模型
|
||||
// reasoning_ack_field 改名注入后此键必被剥除,不会泄往上游)。
|
||||
if (row.reasoning) m.__reasoning = row.reasoning;
|
||||
@@ -419,9 +426,9 @@ function buildMessages(session, language, mintUrl, profile, withProfile, trimOn)
|
||||
}
|
||||
if (row.role === 'tool') {
|
||||
if (trimActive && typeof content === 'string') content = trimToolContent(content, row, callById, roundsAfter, turnsAfter);
|
||||
messages.push({ role: 'tool', tool_call_id: row.tool_call_id, name: row.name, content });
|
||||
messages.push({ role: 'tool', tool_call_id: row.tool_call_id, name: row.name, content, __rowId: row.id });
|
||||
} else {
|
||||
messages.push({ role: row.role, content });
|
||||
messages.push({ role: row.role, content, __rowId: row.id });
|
||||
}
|
||||
});
|
||||
// 未消费图片合并发送:图上文字置顶、图块紧随(同序)。
|
||||
@@ -531,7 +538,8 @@ function trimToolContent(content, row, callById, roundsAfter, turnsAfter) {
|
||||
// 拼压缩回合的 messages(参考 titleMessages 的拼法):默认 system prompt + 正常历史
|
||||
//(与 buildMessages 同取舍:archived 不发、旧摘要/工具轮照常),末尾追加一条 user
|
||||
//(prompt-compress.txt 原文,历史就在它前面的 messages 里,不再填占位符)。
|
||||
// 调用方(engine.runLoopInner 的 compressing 分支)需同时不传 tools。
|
||||
// 调用方(engine.runLoopInner 的 compressing 分支)照常带 tools:工具段在最前,删掉等于断整段
|
||||
// 前缀缓存;模型偶尔会为核对事实调一次,那一轮的文字仍按摘要处理(见 isSummary 判定)。
|
||||
// 【缓存铁律】压缩请求必须与上一次常规请求字节同构、只在尾部追加指令:画像快照与
|
||||
// 占位裁剪一律照常(不传 withProfile/trim = 同一拼法),也不裁历史中间任何注入行——
|
||||
// 从首个被删/被改动行起前缀缓存全断,等于全价重读(旧代码 withProfile=false 把坐在
|
||||
@@ -649,6 +657,14 @@ function splitInlineThinking(raw) {
|
||||
return { content: text.trim(), reasoning: think.trim() };
|
||||
}
|
||||
|
||||
// 剥离内部键 __rowId(锚点用):有才拷贝,没就原样返回,避免每行都新建对象
|
||||
function stripRowId(m) {
|
||||
if (!m || m.__rowId == null) return m;
|
||||
const c = Object.assign({}, m);
|
||||
delete c.__rowId;
|
||||
return c;
|
||||
}
|
||||
|
||||
// 调模型一次。onText(textDelta) 回调流式文本。
|
||||
// 返回 { content, tool_calls: [{id,name,arguments(string)}], finish_reason, usage, aborted }
|
||||
// 可选回调 opts.onToolCallStart({id,name}):某 tool_call 首次凑齐 id+name 时触发一次;
|
||||
@@ -686,20 +702,23 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
// 思维链回传(逐模型 opt-in):开思考时把 buildMessages 挂的内部键 __reasoning 按
|
||||
// reasoning_ack_field 写回 assistant 行;关思考时整段不发。带 tool_calls 的行缺思维链
|
||||
// 时用空串占位(缺失会被上游 400,空串实测通过)。__reasoning 是内部键,任何情况都剥。
|
||||
// __rowId(锚点用的库行 id)同样是内部键:没想发也不该发给上游,任何行有就剥。
|
||||
if (body.messages && body.messages.length) {
|
||||
const ack = modelCfg.reasoning_ack_field;
|
||||
const send = !!(ack && opts.deepThink);
|
||||
body.messages = body.messages.map(mm => {
|
||||
if (!mm || mm.role !== 'assistant') return mm;
|
||||
if (!mm || mm.role !== 'assistant') return stripRowId(mm);
|
||||
if (!mm.__reasoning) {
|
||||
if (!send || !mm.tool_calls) return mm;
|
||||
if (!send || !mm.tool_calls) return stripRowId(mm);
|
||||
const c = Object.assign({}, mm);
|
||||
c[ack] = '';
|
||||
delete c.__rowId; // 内部键,不发给上游
|
||||
return c;
|
||||
}
|
||||
const c = Object.assign({}, mm);
|
||||
if (send) c[ack] = mm.__reasoning;
|
||||
delete c.__reasoning;
|
||||
delete c.__rowId;
|
||||
return c;
|
||||
});
|
||||
}
|
||||
@@ -714,18 +733,78 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
opts.onFetchParams(logBody);
|
||||
}
|
||||
|
||||
const res = await fetch(modelCfg.url + '/chat/completions', {
|
||||
// ---- 增量上下文(context relay)----
|
||||
// 仅对显式 store: true 的模型生效,且必须带 opts.context_id。
|
||||
// id 尾部的 archived_cnt 就是「压缩代数」:压缩会把老行标 archived,代数一变 id 就变,
|
||||
// relay 自然找不到缓存并回 409,不需要额外传 hash。
|
||||
// 裁剪不改 id(只换内容不删行),所以它不会打断增量:relay 里留着完本文,模型读到的信息更多。
|
||||
//
|
||||
// 能不能只发新增,靠「锚点」判:上一轮发出去的最后一条库行(其 messages.id)还在不在
|
||||
// 本轮数组的同一位置上。历史被改只有两种形态——尾部截断(编辑/清空/压缩后)与中间挖洞
|
||||
//(删单条,由 invalidateContext 另行作废)——前者必然让锚点对不上。
|
||||
// 只比长度是不行的:编辑台丢弃历史后新内容又长出来,长度可能反而更大,切点就错位了,
|
||||
// relay 忠实 concat 出一份错位数组,上游按 tool_call_mismatch 拒收。
|
||||
const cid = opts.context_id;
|
||||
const storeOn = !!(modelCfg.store && cid);
|
||||
const fullBody = JSON.stringify(body);
|
||||
// 已剥内部键的全量数组(上面 map 的产物,保序保长)。增量分支会把它替换成尾部切片,
|
||||
// 所以先存一份:409 重试要拿它重发全量,不能拿原始 messages(那上面挂着 __rowId)。
|
||||
const strippedFull = body.messages;
|
||||
if (storeOn) {
|
||||
const st = ctxState.get(cid);
|
||||
headers['X-Context-Id'] = cid;
|
||||
// 锚点:上一轮发出去的数组里最后一条库行的 messages.id。
|
||||
const anchor = st && st.len > 0 ? messages[st.len - 1] : null;
|
||||
const anchorOk = !!(st && anchor && anchor.__rowId === st.lastRowId);
|
||||
if (anchorOk && messages.length === st.len) {
|
||||
// 数组与上次一模一样(错误重试 / 切模型但没新增消息):发空增量,
|
||||
// relay 用缓存原样转发即可。缓存不在时它会回 409,下面重试一次全量。
|
||||
body.messages = [];
|
||||
} else if (anchorOk && messages.length > st.len) {
|
||||
// 前缀没动:只发新增。strippedFull 与 messages 同序同长,索引可直接沿用。
|
||||
body.messages = strippedFull.slice(st.len);
|
||||
} else {
|
||||
// 全量:首轮 / 进程重启 / 压缩后新 id / 历史被改
|
||||
headers['X-Context-Reset'] = '1';
|
||||
}
|
||||
}
|
||||
|
||||
let res = await fetch(modelCfg.url + '/chat/completions', {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
body: storeOn ? JSON.stringify(body) : fullBody,
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
// 409 = relay 那边没有这个 id 的缓存(重启 / TTL / LRU / 压缩换了 id):改发全量重试一次。
|
||||
// 只重试一次:重发的是全量(带 Reset 头),relay 会整段存下,不可能再 409。
|
||||
if (storeOn && res.status === 409) {
|
||||
body.messages = strippedFull; // 已剥内部键的全量,不能拿原始 messages
|
||||
headers['X-Context-Reset'] = '1';
|
||||
res = await fetch(modelCfg.url + '/chat/completions', {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
signal: controller.signal,
|
||||
});
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
const err = new Error('上游返回 ' + res.status + ': ' + text.slice(0, 500));
|
||||
err.upstream_status = res.status;
|
||||
throw err;
|
||||
}
|
||||
// 成功才更新“relay 已存到几条”:失败下一轮自然重发全量。
|
||||
// lastRowId 取本轮发出去的数组里最后一个「来自数据库的行」的 id(末尾可能是合成的
|
||||
// 图片提示行,没有 __rowId,要往前找):它就是下一轮比对的锚点。
|
||||
if (storeOn) {
|
||||
let lastRowId = null;
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
if (messages[i] && messages[i].__rowId != null) { lastRowId = messages[i].__rowId; break; }
|
||||
}
|
||||
ctxState.set(cid, { len: messages.length, lastRowId: lastRowId });
|
||||
}
|
||||
|
||||
let content = '';
|
||||
let reasoning = '';
|
||||
@@ -815,4 +894,12 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
return { content, reasoning, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted, sseFrames };
|
||||
}
|
||||
|
||||
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, setEnvMode, setPayloadTrim, setProfilePrompts, encodeBlocks, decodeBlocks, compressMessages, titleMessages, patchImageReceipt };
|
||||
// 作废某会话的增量上下文指针:下一轮直接走 X-Context-Reset 全量重建。
|
||||
// 用在「历史被挖了一个洞」的地方——删中间某条消息时,后面的行位置没变、
|
||||
// 锚点(末尾行 id)查不出异常,但 relay 缓存里还留着被删那行,增量拼出来就错位了。
|
||||
// 压缩不用调:压缩会改 cid(archived_cnt +1),自然回落。
|
||||
function invalidateContext(cid) {
|
||||
if (cid) ctxState.delete(cid);
|
||||
}
|
||||
|
||||
module.exports = { callModel, buildMessages, buildSystemPrompt, systemPromptText, setSkillListText, setEnvMode, setPayloadTrim, setProfilePrompts, encodeBlocks, decodeBlocks, compressMessages, titleMessages, patchImageReceipt, invalidateContext };
|
||||
|
||||
+11
-3
@@ -95,7 +95,7 @@
|
||||
{
|
||||
"key": "video_task",
|
||||
"label": "视频生成",
|
||||
"desc": "智谱 BigModel 异步视频接口",
|
||||
"desc": "文生视频 / 图生视频",
|
||||
"url": "http://mcp_video_task:3000/mcp",
|
||||
"mode": "skill",
|
||||
"fields": [
|
||||
@@ -105,12 +105,20 @@
|
||||
"secret": true,
|
||||
"prefix": "Bearer ",
|
||||
"required": true,
|
||||
"placeholder": "智谱 API Key"
|
||||
"placeholder": "API Key"
|
||||
},
|
||||
{
|
||||
"header": "X-Video-Task-Base-Url",
|
||||
"label": "地址",
|
||||
"required": true,
|
||||
"placeholder": "https://<host>/v1",
|
||||
"hint": "OpenAI 兼容 videos / generations"
|
||||
},
|
||||
{
|
||||
"header": "X-Video-Task-Model",
|
||||
"label": "模型",
|
||||
"default": "cogvideox-flash"
|
||||
"required": true,
|
||||
"placeholder": "上游的模型 id"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -432,6 +432,14 @@ main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
|
||||
border-top: 1px solid var(--border);
|
||||
background: var(--bg-fafbfd);
|
||||
}
|
||||
/* 流式思考:面板保持折叠,只在标题行挂一段动态文字(.msg-think-live),
|
||||
正文开始后清空。不占额外高度,也不会大段刷屏;要看全文点开就是落库的那份。 */
|
||||
.msg-think.streaming { pointer-events: none; }
|
||||
.msg-think-live {
|
||||
flex: 1; min-width: 0; margin-left: 8px;
|
||||
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
font-size: 12px; font-weight: 400; color: var(--gray-light);
|
||||
}
|
||||
/* 流式阶段给一点视觉提示:边色稍亮,收尾(assistant_message)后去除 */
|
||||
.msg-think.streaming { border-color: var(--bd-d8e0f5); }
|
||||
|
||||
@@ -483,6 +491,12 @@ main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
|
||||
|
||||
/* markdown 排版(v1 同款;仅当 marked 渲染成功时加 .md 类,否则保留 pre-wrap 原文) */
|
||||
.bubble-ai.md, .bubble-user.md { white-space: normal; }
|
||||
/* KaTeX 公式:块级单独占行、超宽可横滚;行内不折行避免断在半截 */
|
||||
.math-block { display: block; margin: 12px 0; overflow-x: auto; overflow-y: hidden; }
|
||||
.math-block .katex-display { margin: 0; }
|
||||
.math-inline { display: inline; white-space: nowrap; }
|
||||
.math-fallback, .math-error { font-family: ui-monospace, Menlo, Consolas, monospace; color: #b45309; }
|
||||
.math-error { text-decoration: underline dotted; }
|
||||
.bubble-ai.md > :first-child, .bubble-user.md > :first-child { margin-top: 0; }
|
||||
.bubble-ai.md > :last-child, .bubble-user.md > :last-child { margin-bottom: 0; }
|
||||
.bubble-ai.md p, .bubble-user.md p { margin: 6px 0; }
|
||||
@@ -600,8 +614,7 @@ footer { position: relative; flex: none; padding: 8px 12px calc(10px + var(--sab
|
||||
.attach-chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 140px; }
|
||||
.attach-chip .rm { color: var(--gray); display: flex; }
|
||||
/* 工具卡参数流式生成中:◌ 呼吸提示(收尾后随 .tool-generating 移除) */
|
||||
.tool-card.tool-generating .spin { display: inline-block; color: var(--active); animation: tool-gen-pulse 1s ease-in-out infinite; }
|
||||
@keyframes tool-gen-pulse { 0%, 100% { opacity: 1 } 50% { opacity: .2 } }
|
||||
.tool-card.tool-generating .spin { animation: tool-spin 0.9s linear infinite; }
|
||||
|
||||
/* 上传文件卡片行(嵌在输入框内部顶端,由 .input-shell 的 gap 提供间距;逐卡进度遮罩;卡多时横向滚动) */
|
||||
.up-cards {
|
||||
@@ -997,7 +1010,8 @@ body.voice-active .scroll-bottom-btn { display: none; }
|
||||
flex: 1; min-width: 0; font-weight: 600; color: var(--text, var(--fg-223));
|
||||
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
}
|
||||
.files-panel .file-dir-count { flex: none; font-size: 12px; color: var(--gray); }
|
||||
.files-panel .file-dir-count,
|
||||
.files-panel .file-dir-time { flex: none; font-size: 12px; color: var(--gray); }
|
||||
.files-panel .file-dir-go {
|
||||
width: 14px; height: 14px; flex: none;
|
||||
fill: none; stroke: var(--gray-light); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round;
|
||||
@@ -1158,7 +1172,6 @@ body.voice-active .scroll-bottom-btn { display: none; }
|
||||
font-size: 13px; color: var(--ink); white-space: nowrap;
|
||||
}
|
||||
.tree-row:hover { background: var(--surface); }
|
||||
.tree-row.is-ignored { color: var(--gray-light); cursor: default; }
|
||||
.tree-row.is-link { color: var(--gray); }
|
||||
.tree-ico { flex: none; width: 16px; height: 16px; display: inline-flex; color: var(--gray); }
|
||||
.tree-ico svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
|
||||
@@ -1376,7 +1389,12 @@ body.voice-active .scroll-bottom-btn { display: none; }
|
||||
.tool-card .tool-time .act svg { width: 12px; height: 12px; }
|
||||
.tool-card .tool-time .act.copied { color: var(--fg-2e7d32); }
|
||||
.tool-card.error { background: var(--bg-fdecea); border-color: var(--bd-f2c4bd); color: var(--fg-a3352a); }
|
||||
.tool-card .spin { display: inline-block; width: 1em; text-align: center; }
|
||||
.tool-card .spin { display: inline-flex; align-items: center; justify-content: center; width: 1em; height: 1em; }
|
||||
.tool-card .spin svg { width: 100%; height: 100%; }
|
||||
/* 执行中:摘要行左侧那段弧转起来。动画挂在 .spin 容器上——直接转 SVG 元素时
|
||||
CSS transform 的默认原点在左上角(0 0),会绕着角转,看着像没动 */
|
||||
.tool-card.running .spin { animation: tool-spin 0.9s linear infinite; }
|
||||
@keyframes tool-spin { to { transform: rotate(360deg); } }
|
||||
.tool-card .tool-args-full { color: var(--fg-6b5b2a); }
|
||||
/* 工具结果里的图片(read_file 读图回传;点击放大查看) */
|
||||
.tool-img {
|
||||
@@ -1533,6 +1551,16 @@ label.icon-btn { cursor: pointer; }
|
||||
.cfg-input:focus { border-color: var(--active); background: var(--bg-fff); }
|
||||
.cfg-input::-webkit-outer-spin-button, .cfg-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
|
||||
.cfg-input.cfg-dirty { color: var(--fg-c0392b); border-color: var(--bd-e8b4b0); background: var(--bg-fdf3f2); }
|
||||
/* 多行条目输入(如画像偏好):标签在上、输入框占整行,便于一行一条 */
|
||||
.cfg-kv.stack { flex-direction: column; align-items: stretch; gap: 6px; }
|
||||
.cfg-kv.stack label { max-width: none; }
|
||||
.cfg-textarea {
|
||||
width: 100%; min-height: 96px; box-sizing: border-box; resize: vertical;
|
||||
font-size: 13px; line-height: 1.7; color: var(--ink); font-family: inherit;
|
||||
text-align: left; background: var(--surface); border: 1px solid transparent;
|
||||
border-radius: 8px; padding: 8px 10px; -webkit-appearance: none; appearance: none;
|
||||
}
|
||||
.cfg-textarea:focus { border-color: var(--active); background: var(--bg-fff); outline: none; }
|
||||
.cfg-err {
|
||||
margin: 0 0 8px; padding: 8px 12px; border-radius: 8px;
|
||||
background: var(--bg-fdecea); color: var(--fg-a3352a); font-size: 12.5px;
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover, interactive-widget=resizes-content">
|
||||
<title>Agent 任务台</title>
|
||||
<script src="/vendor/marked.min.js"></script>
|
||||
<script src="/vendor/katex/katex.min.js"></script>
|
||||
<script src="/js/math.js"></script>
|
||||
<script src="/vendor/prism.min.js"></script>
|
||||
<meta name="theme-color" content="#3964FE">
|
||||
<!-- 主题首屏:先落 data-theme,避开刷新时的白屏闪一下。默认浅色,手动切过后存 localStorage -->
|
||||
@@ -29,6 +31,7 @@
|
||||
<link rel="manifest" href="/manifest.json">
|
||||
<link rel="apple-touch-icon" href="/icons/apple-touch-icon.png">
|
||||
<link rel="icon" type="image/png" sizes="192x192" href="/icons/icon-192.png">
|
||||
<link rel="stylesheet" href="/vendor/katex/katex.min.css">
|
||||
<link rel="stylesheet" href="/css/style.css">
|
||||
<style>
|
||||
/* 「压缩」摘要卡片:与 .msg-ctx(画像快照/渠道/记忆)同一套视觉——淡蓝底、蓝边蓝字、
|
||||
|
||||
@@ -192,7 +192,7 @@
|
||||
const gen = !!(opts && opts.generating);
|
||||
if (gen) div.classList.add('tool-generating');
|
||||
div.innerHTML =
|
||||
'<details' + (open ? ' open' : '') + '><summary><span class="spin">◌</span>' +
|
||||
'<details' + (open ? ' open' : '') + '><summary><span class="spin"><svg class="ic sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"><path d="M21 12a9 9 0 1 1-6.2-8.6"/></svg></span>' +
|
||||
'<span class="tool-name">' + esc(name) + '</span>' +
|
||||
'<span class="tool-args">' + (gen ? '生成参数中…' : esc(toolArgsSummary(name, argsStr))) + '</span></summary>' +
|
||||
'<pre class="tool-args-full">' + (gen ? '' : esc(toolArgsFull(argsStr))) + '</pre>' +
|
||||
@@ -249,7 +249,10 @@
|
||||
const card = document.getElementById('tool-' + id);
|
||||
if (!card) return;
|
||||
if (isError) card.classList.add('error');
|
||||
card.querySelector('.spin').textContent = isError ? '✕' : '✓';
|
||||
card.classList.remove('running'); // 停止转圈,换成结果标记
|
||||
card.querySelector('.spin').innerHTML = isError
|
||||
? '<svg class="ic sm" viewBox="0 0 24 24"><use href="#i-x-line"/></svg>'
|
||||
: '<svg class="ic sm" viewBox="0 0 24 24"><use href="#i-check"/></svg>';
|
||||
const parsed = parseToolContent(content);
|
||||
const pre = card.querySelector('.tool-result');
|
||||
if (!pre) return;
|
||||
@@ -274,6 +277,20 @@
|
||||
pre.insertAdjacentElement('afterend', el);
|
||||
}
|
||||
}
|
||||
// 工具执行中的增量输出(terminal 长命令):边跑边填;只留尾部 2000 行,避免长跑命令撑爆 DOM
|
||||
function appendToolOutput(id, chunk) {
|
||||
const card = document.getElementById('tool-' + id);
|
||||
if (!card) return;
|
||||
const pre = card.querySelector('.tool-result');
|
||||
if (!pre) return;
|
||||
if (!pre.dataset.streaming) { pre.textContent = ''; pre.dataset.streaming = '1'; } // 首块到达:清掉“执行中…”占位
|
||||
const follow = pre.scrollHeight - pre.scrollTop - pre.clientHeight < 8; // 用户往上翻时不抢滚动
|
||||
pre.appendChild(document.createTextNode(chunk));
|
||||
const lines = pre.textContent.split('\n');
|
||||
if (lines.length > 2000) pre.textContent = '…(更早输出已折叠)\n' + lines.slice(-2000).join('\n');
|
||||
// 追加后滚到底:读 scrollHeight 会强制布局拿到新高度;再补一帧,避免大 chunk 一次没贴到底
|
||||
if (follow) { pre.scrollTop = pre.scrollHeight; requestAnimationFrame(() => { pre.scrollTop = pre.scrollHeight; }); }
|
||||
}
|
||||
// 把 assistant 消息里的 tool_calls 建成工具卡片(历史渲染与 assistant_message 事件共用)
|
||||
function appendToolCardsFromAssistant(m, container) {
|
||||
if (!m.tool_calls) return;
|
||||
|
||||
@@ -48,7 +48,13 @@
|
||||
if (typeof marked !== 'undefined') marked.setOptions({ breaks: true, gfm: true });
|
||||
function mdHtml(text) {
|
||||
if (typeof marked === 'undefined') return { html: esc(text), fmt: false };
|
||||
try { return { html: marked.parse(text || ''), fmt: true }; }
|
||||
try {
|
||||
// 公式先抠成占位、marked 解析完再填回 KaTeX 输出(见 math.js)
|
||||
const html = typeof mdParseWithMath === 'function'
|
||||
? mdParseWithMath(text || '', marked)
|
||||
: marked.parse(text || '');
|
||||
return { html: html, fmt: true };
|
||||
}
|
||||
catch (_) { return { html: esc(text), fmt: false }; }
|
||||
}
|
||||
function fmtTime(ts) {
|
||||
|
||||
@@ -98,13 +98,12 @@
|
||||
if (expanded.has(ent.rel)) row.classList.add('open');
|
||||
}
|
||||
if (ent.type === 'link') row.classList.add('is-link');
|
||||
if (ent.ignored) row.classList.add('is-ignored');
|
||||
|
||||
// 不画展开箭头:文件夹图标已经说明可以点,点行任意位置都能展开/收起。
|
||||
|
||||
// 勾选框:hover 或已选中时淡入,但占位始终在,避免 hover 时文件名左右跳。
|
||||
// 忽略的大目录和软链不参与批量操作(后端也不让删),所以不给勾选框。
|
||||
if (!ent.ignored && ent.type !== 'link') {
|
||||
// 软链不参与批量操作(后端也不让删),所以不给勾选框。
|
||||
if (ent.type !== 'link') {
|
||||
const chk = document.createElement('span');
|
||||
chk.className = 'tree-check';
|
||||
chk.setAttribute('role', 'checkbox');
|
||||
@@ -125,10 +124,6 @@
|
||||
name.textContent = ent.name;
|
||||
row.appendChild(name);
|
||||
|
||||
if (ent.ignored) {
|
||||
row.title = ent.rel + '(已忽略的大目录/构建产物)';
|
||||
return row;
|
||||
}
|
||||
// 行内动作:服务器形态给「下载 / 删除」;local 本就跑在同一台机器上,
|
||||
// 「下载」到本地是多余的(用 Finder 直接看原文件更顺),所以换成「在 Finder 中显示」。
|
||||
// @ 引用排最前:它是「往输入框送东西」的动作,和后面的下载 / Finder / 删除(对文件本身动手)分开
|
||||
@@ -149,7 +144,7 @@
|
||||
if (!list.length && !rel) tip(frag, '(空目录)');
|
||||
for (const ent of list) {
|
||||
frag.appendChild(rowFor(ent, depth));
|
||||
if (ent.type === 'dir' && !ent.ignored && expanded.has(ent.rel)) {
|
||||
if (ent.type === 'dir' && expanded.has(ent.rel)) {
|
||||
const kids = document.createElement('div');
|
||||
kids.className = 'tree-children';
|
||||
frag.appendChild(kids);
|
||||
@@ -246,7 +241,7 @@
|
||||
const body = $('tree-body');
|
||||
if (!body) return [];
|
||||
return Array.from(body.querySelectorAll('.tree-row')).filter(function (r) {
|
||||
return !r.classList.contains('is-ignored') && r.dataset.type !== 'link';
|
||||
return r.dataset.type !== 'link';
|
||||
});
|
||||
}
|
||||
|
||||
@@ -386,7 +381,7 @@
|
||||
else if (a === 'at') insertPathRef(rel, isDir);
|
||||
return;
|
||||
}
|
||||
if (row.classList.contains('is-ignored') || row.dataset.type === 'link') return;
|
||||
if (row.dataset.type === 'link') return;
|
||||
if (isDir) {
|
||||
if (expanded.has(rel)) expanded.delete(rel); else expanded.add(rel);
|
||||
renderBody();
|
||||
@@ -500,7 +495,10 @@
|
||||
// 侧栏底部「文件树」图标 → 切到这个面板(由 files.js 的 setDrawerPane 调用)。
|
||||
window.showFileTree = function () {
|
||||
ensureShell();
|
||||
invalidate(true); // 切回面板一律重拉已展开的目录,不拿上次的缓存糊弄(后台只标脏的也在这里补上)
|
||||
// 缓存还在就直接用:同一会话里反复开关面板,不该把已展开的目录全部重拉一遍
|
||||
// (展开 N 层就是 N 轮请求,移动网络下能到 1-2 秒)。文件真变了有 reloadFileTree 兜底,
|
||||
// 它在一轮跑完 / 回前台 / 断线重连时会把已展开的层重新拉一遍。
|
||||
if (!cache.size) invalidate(true);
|
||||
renderBody();
|
||||
};
|
||||
|
||||
|
||||
@@ -158,10 +158,10 @@
|
||||
return fileSelected.has(p) || isPathInDirSel(p);
|
||||
}
|
||||
// 目录节点内所有文件的相对路径(递归)
|
||||
// 当前层能拿到的文件(懒加载只拉这一层,子目录内容没有;
|
||||
// 目录整体勾选走 dirSelKey,zip 打包由后端 collectZipEntries 递归)
|
||||
function filesUnderDir(node) {
|
||||
let out = node.files.map(f => f.filename);
|
||||
for (const d of node.dirs.values()) out = out.concat(filesUnderDir(d));
|
||||
return out;
|
||||
return node ? node.files.map(f => f.filename) : [];
|
||||
}
|
||||
// 全选的作用域 = 当前浏览目录下的全部文件(递归含其子文件夹,等价于逐个勾选这些文件夹行)。
|
||||
// 不能再用全任务树的扁平列表:在子文件夹里点全选会把根下其它文件也选上,批量删除会清空整个任务目录。
|
||||
@@ -297,31 +297,28 @@
|
||||
// 扁平文件列表 → 树节点 { name, path, dirs: Map, files: [] }
|
||||
// 目录标记行(?dirs=1:filename 以 '/' 结尾 + dir:true)只沿途铺设路径节点、不入任何 files[],
|
||||
// 不含文件的空夹子因此也能渲染;与同名文件路径铺出的节点经 dirs Map 天然去重。
|
||||
function buildFilesTree(files) {
|
||||
const root = { name: '', path: '', dirs: new Map(), files: [] };
|
||||
for (const f of files) {
|
||||
const isDir = f.dir === true || /\/$/.test(String(f.filename));
|
||||
const name = isDir ? String(f.filename).replace(/\/+$/, '') : String(f.filename);
|
||||
if (!name) continue;
|
||||
const segs = name.split('/');
|
||||
let node = root, rel = '';
|
||||
const dirDepth = isDir ? segs.length : segs.length - 1; // 文件:末段是文件名;目录:全是路径段
|
||||
for (let i = 0; i < dirDepth; i++) {
|
||||
const seg = segs[i];
|
||||
rel = rel ? rel + '/' + seg : seg;
|
||||
if (!node.dirs.has(seg)) node.dirs.set(seg, { name: seg, path: rel, dirs: new Map(), files: [] });
|
||||
node = node.dirs.get(seg);
|
||||
// 把 /tree 返回的单层 entries 变成渲染用节点(子目录只留空 Map,进入时才拉)
|
||||
function buildNodeFromEntries(dir, entries) {
|
||||
const node = { name: dir.split('/').pop() || '', path: dir, dirs: new Map(), files: [] };
|
||||
for (const e of entries) {
|
||||
if (e.type === 'dir') {
|
||||
node.dirs.set(e.name, { name: e.name, path: e.rel, dirs: new Map(), files: [], mtime: e.mtimeMs });
|
||||
} else if (e.type === 'file') {
|
||||
node.files.push({ filename: e.rel, size: e.size, mtime: e.mtimeMs, ctime: e.mtimeMs });
|
||||
}
|
||||
if (!isDir) node.files.push(f);
|
||||
}
|
||||
return root;
|
||||
return node;
|
||||
}
|
||||
|
||||
// 目录(含子目录)内文件总数,用于目录头计数
|
||||
function countDirFiles(node) {
|
||||
let n = node.files.length;
|
||||
for (const d of node.dirs.values()) n += countDirFiles(d);
|
||||
return n;
|
||||
// 文件/目录时间:统一「年/月/日 时:分」。全局 fmtTime 对今天只给时分(聊天消息那种场景),
|
||||
// 文件列表要能一眼看出新旧,所以固定带日期。
|
||||
function fmtFileTime(ts) {
|
||||
if (!ts) return '';
|
||||
const d = new Date(ts);
|
||||
if (isNaN(d.getTime())) return '';
|
||||
const p = n => String(n).padStart(2, '0');
|
||||
return d.getFullYear() + '/' + p(d.getMonth() + 1) + '/' + p(d.getDate())
|
||||
+ ' ' + p(d.getHours()) + ':' + p(d.getMinutes());
|
||||
}
|
||||
|
||||
function fileRowHtml(f) {
|
||||
@@ -332,7 +329,7 @@
|
||||
? '<svg class="file-ico eye" viewBox="0 0 24 24" aria-label="可预览"><use href="#i-eye"/></svg>'
|
||||
: '<svg class="file-ico doc" viewBox="0 0 24 24" aria-label="可下载"><use href="#i-file-doc"/></svg>';
|
||||
const name = esc(String(f.filename).split('/').pop() || f.filename);
|
||||
const ftime = fmtTime(f.mtime || f.ctime);
|
||||
const ftime = fmtFileTime(f.mtime || f.ctime);
|
||||
const sel = isFileEffectivelySelected(f.filename);
|
||||
return '<div class="file-row clickable' + (sel ? ' selected' : '') + '" data-path="' + esc(f.filename) + '" data-preview="' + (fsPv ? '1' : '0') + '" data-size="' + esc(fmtSize(f.size)) + '">' +
|
||||
'<span class="file-check" role="checkbox" aria-checked="' + (sel ? 'true' : 'false') + '" aria-label="选择"></span>' +
|
||||
@@ -346,29 +343,23 @@
|
||||
|
||||
// 目录行(带勾选框):整个文件夹作为一个选中项(下载时后端打包成 zip);
|
||||
// 点行其余区域进入该目录(单层列表,不再有折叠/展开)。
|
||||
// 懒加载下不知道子目录内容,勾选态只看这一行自己是否被整体勾选;右侧显示修改时间,和文件行一致。
|
||||
function dirRowHtml(node) {
|
||||
const under = filesUnderDir(node);
|
||||
const checked = fileSelected.has(dirSelKey(node.path)) ||
|
||||
(under.length > 0 && under.every(isFileEffectivelySelected));
|
||||
const checked = fileSelected.has(dirSelKey(node.path));
|
||||
const dtime = fmtFileTime(node.mtime);
|
||||
return '<div class="file-dir-head' + (checked ? ' selected' : '') + '" data-dir="' + esc(node.path) + '" role="button">' +
|
||||
'<span class="file-check" role="checkbox" aria-checked="' + (checked ? 'true' : 'false') + '" aria-label="选择文件夹"></span>' +
|
||||
'<svg class="file-dir-ico" viewBox="0 0 24 24"><use href="#i-folder-c"/></svg>' +
|
||||
'<span class="file-dir-name">' + esc(node.name) + '</span>' +
|
||||
'<span class="file-dir-count">' + countDirFiles(node) + ' 个文件</span>' +
|
||||
(dtime ? '<span class="file-dir-time">' + dtime + '</span>' : '') +
|
||||
'<svg class="file-dir-go" viewBox="0 0 24 24"><use href="#i-chevron-right"/></svg>' +
|
||||
'</div>';
|
||||
}
|
||||
|
||||
// 按相对路径取树节点('' = 根);路径不存在(如删除后重拉)返回 null
|
||||
// 懒加载下只有当前浏览目录的节点('' = 根);其它路径没有数据
|
||||
function filesNodeAt(path) {
|
||||
let node = filesTreeRoot;
|
||||
if (!node) return null;
|
||||
if (!path) return node;
|
||||
for (const seg of String(path).split('/')) {
|
||||
node = node.dirs.get(seg);
|
||||
if (!node) return null;
|
||||
}
|
||||
return node;
|
||||
if (!filesTreeRoot) return null;
|
||||
return (path || '') === filesCurDir ? filesTreeRoot : null;
|
||||
}
|
||||
|
||||
// 渲染当前目录:顶部面包屑(根目录(home) › a › b,点任意节点回跳)+ 单层列表(目录在前、文件在后)。
|
||||
@@ -380,7 +371,7 @@
|
||||
if (!node) { filesCurDir = ''; node = filesNodeAt(''); } // 当前目录已被删除 → 退回根
|
||||
const sizeEl = $('files-panel-size');
|
||||
if (!node) { el.innerHTML = ''; if (sizeEl) sizeEl.textContent = ''; return; }
|
||||
// 标题旁总大小:按未过滤的聚合表取当前目录递归总量(关彩蛋时服务端不返回 dot 项;开彩蛋时含 .imgcache/.tmp/.venv 全计)
|
||||
// 标题旁总大小:只算当前层文件(子目录的递归大小要额外扫全树,懒加载拿不到)
|
||||
if (sizeEl) {
|
||||
const total = filesDirSizes.get(filesCurDir) || 0;
|
||||
sizeEl.textContent = total > 0 ? fmtSize(total) : '';
|
||||
@@ -419,28 +410,18 @@
|
||||
filesTreeRoot = null;
|
||||
updateFilesFooter();
|
||||
try {
|
||||
// 彩蛋开启时后端连 dot 项一起列,且不再滤 .tmp/.venv(全量放出)
|
||||
// 按目录懒加载:只拉当前这一层(/tree),不再全量递归。
|
||||
// 之前一次拉全树,仓库级工作目录能到 1.4MB(10544 个文件,81% 在 node_modules)。
|
||||
const hiddenQ = filesShowHidden ? '&hidden=1' : '';
|
||||
const files = await req('GET', '/api/sessions/' + state.sessionId + '/files?dirs=1' + hiddenQ);
|
||||
// 会话级 .tmp(executor 的共享 /tmp 落盘处)与 .venv(Python 虚拟环境)不属于任务产物:默认隐藏(老后端兼容兑底;新版关彩蛋时服务端已不返回 dot 项)
|
||||
const shown = filesShowHidden ? files : files.filter(f => !/^(\.tmp|\.venv)(\/|$)/.test(String(f.filename)));
|
||||
// 重建目录树后只渲染当前目录这一层(后端已按时间倒序,组内再排一次防旧排序缓存)
|
||||
filesTreeRoot = buildFilesTree(shown);
|
||||
// 总大小按原始列表(含 .tmp/.venv)聚合:目录标记行无字节量跳过;文件把 size 记到每一级祖先目录(根 = '')
|
||||
filesDirSizes = new Map([['', 0]]);
|
||||
for (const f of files) {
|
||||
if (f.dir === true || /\/$/.test(String(f.filename))) continue;
|
||||
const sz = Number(f.size) || 0;
|
||||
if (!sz) continue;
|
||||
filesDirSizes.set('', (filesDirSizes.get('') || 0) + sz);
|
||||
const segs = String(f.filename).split('/');
|
||||
segs.pop(); // 末段是文件名,其余是祖先目录
|
||||
let acc = '';
|
||||
for (const seg of segs) {
|
||||
acc = acc ? acc + '/' + seg : seg;
|
||||
filesDirSizes.set(acc, (filesDirSizes.get(acc) || 0) + sz);
|
||||
}
|
||||
}
|
||||
const data = await req('GET', '/api/sessions/' + state.sessionId + '/tree?dir='
|
||||
+ encodeURIComponent(filesCurDir) + hiddenQ);
|
||||
const entries = (data && data.entries) || [];
|
||||
filesTreeRoot = buildNodeFromEntries(filesCurDir, entries);
|
||||
// 标题旁总大小:只统计当前层文件(子目录的递归大小要额外扫全树,懒加载拿不到)
|
||||
let total = 0;
|
||||
for (const f of filesTreeRoot.files) total += Number(f.size) || 0;
|
||||
filesDirSizes = new Map([[filesCurDir, total]]);
|
||||
if (data && data.capped) toast('目录条目过多,仅显示前 ' + entries.length + ' 项');
|
||||
renderFilesDir(); // filesCurDir 失效时自动退回根目录
|
||||
// 重新渲染后刷新底部计数与按钮状态
|
||||
updateFilesFooter();
|
||||
@@ -543,8 +524,7 @@
|
||||
if (!dp || !state.sessionId) return;
|
||||
if (e.target.closest('.file-check')) { toggleDirSelect(dp); return; }
|
||||
filesCurDir = dp;
|
||||
renderFilesDir();
|
||||
updateFilesFooter(); // 全选作用域随浏览目录变化,刷新底部勾选态
|
||||
loadFiles(); // 懒加载:进哪层拉哪层(renderFilesDir / updateFilesFooter 在它内部调)
|
||||
return;
|
||||
}
|
||||
const row = e.target.closest('.file-row');
|
||||
@@ -594,8 +574,7 @@
|
||||
const c = e.target.closest('.crumb[data-path]');
|
||||
if (!c) return;
|
||||
filesCurDir = c.dataset.path;
|
||||
renderFilesDir();
|
||||
updateFilesFooter(); // 同上:面包屑回跳也重算全选态
|
||||
loadFiles(); // 同上:面包屑回跳也按目录重拉
|
||||
});
|
||||
|
||||
// 彩蛋:点面板标题旁的总大小文本 = 切换隐藏文件(dot 项含 .tmp/.venv)显隐。
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
// 数学公式渲染(KaTeX 本地化,vendor/katex/)。
|
||||
// 所有 markdown 入口(聊天气泡 / md 文件预览 / 流式增量)都走 core.js 的 mdHtml(),
|
||||
// 公式处理收在这一个文件里,不散落到各处。
|
||||
//
|
||||
// 支持:
|
||||
// $$...$$ 块级(可换行)
|
||||
// $...$ 行内($ 后不能是空白、前不能是数字/字母,避免「$5 ... $6」被当公式)
|
||||
//
|
||||
// 实现方式:占位符往返,而不是 marked 扩展。
|
||||
// 1) markdown 解析前,把公式抠成 @@MATHn@@ 占位(跳过围栏代码与行内代码)
|
||||
// 2) 交给 marked 正常解析
|
||||
// 3) 输出 HTML 里把占位换回 KaTeX 渲染结果
|
||||
// 这样公式里的 * _ \ { } 不会被 marked 当成强调/转义吃掉。
|
||||
|
||||
const MATH_PH = /@@MATH(\d+)@@/g;
|
||||
|
||||
// 从 markdown 源里抠出公式,返回 { md, store }
|
||||
function mathExtract(src) {
|
||||
const text = String(src || '');
|
||||
const store = [];
|
||||
let out = '';
|
||||
let i = 0;
|
||||
while (i < text.length) {
|
||||
// 围栏代码块整段跳过(三个反引号)
|
||||
if (text.startsWith('```', i)) {
|
||||
const end = text.indexOf('```', i + 3);
|
||||
const stop = end < 0 ? text.length : end + 3;
|
||||
out += text.slice(i, stop);
|
||||
i = stop;
|
||||
continue;
|
||||
}
|
||||
// 行内代码跳过
|
||||
if (text[i] === '`') {
|
||||
const end = text.indexOf('`', i + 1);
|
||||
if (end > i) {
|
||||
out += text.slice(i, end + 1);
|
||||
i = end + 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// 块级 $$...$$
|
||||
if (text.startsWith('$$', i)) {
|
||||
const end = text.indexOf('$$', i + 2);
|
||||
if (end > i + 2) {
|
||||
const latex = text.slice(i + 2, end).trim();
|
||||
if (latex) {
|
||||
store.push({ latex: latex, display: true });
|
||||
out += '@@MATH' + (store.length - 1) + '@@';
|
||||
i = end + 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 行内 $...$
|
||||
if (text[i] === '$') {
|
||||
const m = /^\$(?!\s)((?:\\.|[^$\\\n])+?)(?<!\s)\$/.exec(text.slice(i));
|
||||
if (m) {
|
||||
const before = i > 0 ? text[i - 1] : '';
|
||||
const after = text[i + m[0].length] || '';
|
||||
// 前后紧邻字母/数字时不当公式:$5 和 $6、a$b$c 之类
|
||||
if (!/[0-9A-Za-z]/.test(before) && !/[0-9A-Za-z]/.test(after)) {
|
||||
store.push({ latex: m[1], display: false });
|
||||
out += '@@MATH' + (store.length - 1) + '@@';
|
||||
i += m[0].length;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
out += text[i];
|
||||
i++;
|
||||
}
|
||||
return { md: out, store: store };
|
||||
}
|
||||
|
||||
function mathEsc(s) {
|
||||
return String(s == null ? '' : s).replace(/[&<>"]/g, function (c) {
|
||||
return { '&': '&', '<': '<', '>': '>', '"': '"' }[c];
|
||||
});
|
||||
}
|
||||
|
||||
// 单个公式 → HTML。KaTeX 不存在或 LaTeX 有错时原样吐回,用户能看见哪里写错了。
|
||||
function mathRenderOne(latex, display) {
|
||||
const wrap = display ? '$$' : '$';
|
||||
if (typeof katex === 'undefined') {
|
||||
return '<span class="math-fallback">' + wrap + mathEsc(latex) + wrap + '</span>';
|
||||
}
|
||||
try {
|
||||
const html = katex.renderToString(latex, {
|
||||
displayMode: display,
|
||||
throwOnError: false,
|
||||
output: 'html',
|
||||
strict: false,
|
||||
});
|
||||
// 块级公式自己占一行;行内公式保持内联
|
||||
return display ? '<span class="math-block">' + html + '</span>' : '<span class="math-inline">' + html + '</span>';
|
||||
} catch (e) {
|
||||
return '<span class="math-error" title="' + mathEsc(String((e && e.message) || e)) + '">' +
|
||||
wrap + mathEsc(latex) + wrap + '</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// 把 HTML 里的占位换回渲染结果
|
||||
function mathExpand(html, store) {
|
||||
if (!store || !store.length) return html;
|
||||
return String(html || '').replace(MATH_PH, function (all, idx) {
|
||||
const item = store[Number(idx)];
|
||||
return item ? mathRenderOne(item.latex, item.display) : all;
|
||||
});
|
||||
}
|
||||
|
||||
// 一步到位:markdown → 含公式的 HTML。markedRef 由调用方传入(core.js 里的全局 marked)
|
||||
function mdParseWithMath(text, markedRef) {
|
||||
const r = mathExtract(text);
|
||||
const html = markedRef.parse(r.md);
|
||||
return mathExpand(html, r.store);
|
||||
}
|
||||
@@ -497,17 +497,22 @@ function pvCloseOverlayQuiet() { const fs = document.getElementById('fs-viewer')
|
||||
// 其余按 text/plain 原样打开——服务端文件响应一律 attachment,直接导航只会触发下载。
|
||||
// md 里原样透传的 <script> 在面板(innerHTML)本就不执行,但 blob 文档会执行,故先剔除以保持一致。
|
||||
function pvMdDoc(html, name) {
|
||||
// KaTeX 样式表用根绝对路径引入:blob 文档的 base 是 blob:,相对路径找不到字体
|
||||
const css =
|
||||
'body{margin:0;padding:24px 32px 48px;max-width:900px;font:15px/1.7 -apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;color:#1f2328;background:#fff}' +
|
||||
'img{max-width:100%;height:auto}pre{background:#f6f8fa;padding:12px 14px;border-radius:8px;overflow:auto}' +
|
||||
'code{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;font-size:.92em}' +
|
||||
'table{border-collapse:collapse}th,td{border:1px solid #d8dee4;padding:6px 10px}' +
|
||||
'blockquote{margin:0;padding:0 14px;color:#57606a;border-left:3px solid #d8dee4}' +
|
||||
'a{color:#0969da}';
|
||||
'a{color:#0969da}' +
|
||||
'.math-block{display:block;text-align:center;margin:14px 0;overflow-x:auto;overflow-y:hidden}';
|
||||
const title = esc(String(name || '').split('/').pop() || '预览');
|
||||
const origin = (typeof location !== 'undefined' && location.origin) ? location.origin : '';
|
||||
return '<!DOCTYPE html><html><head><meta charset="utf-8">' +
|
||||
'<meta name="viewport" content="width=device-width,initial-scale=1">' +
|
||||
'<title>' + title + '</title><style>' + css + '</style></head><body>' +
|
||||
'<title>' + title + '</title>' +
|
||||
'<link rel="stylesheet" href="' + origin + '/vendor/katex/katex.min.css">' +
|
||||
'<style>' + css + '</style></head><body>' +
|
||||
html.replace(/<script\b[\s\S]*?(?:<\/script\s*>|$)/gi, '') + '</body></html>';
|
||||
}
|
||||
function pvTextUrl(text) { return URL.createObjectURL(new Blob([text], { type: 'text/plain;charset=utf-8' })); }
|
||||
|
||||
@@ -242,6 +242,9 @@
|
||||
renderAttachChips();
|
||||
clearUploadCards(); // 切会话清掉旧会话的上传卡片
|
||||
renderDrawerList(); // 会话切换后立刻重画抽屉高亮,避免下次打开才看到更新
|
||||
// 文件树同理,而且更早:它的缓存按路径存、不含会话,晚一步清就会在抽屉里
|
||||
// 先看到上个会话的文件。紧跟 sessionId 变更就清,不等 detail 回来。
|
||||
if (window.refreshFileTree) window.refreshFileTree();
|
||||
setView('chat');
|
||||
$('chat-view').innerHTML = '<div class="drawer-empty">加载中…</div>';
|
||||
try {
|
||||
@@ -252,7 +255,6 @@
|
||||
state.deepThink = !!(detail.session && detail.session.deep_think);
|
||||
$('deep-think-btn').classList.toggle('on', state.deepThink);
|
||||
connectStream();
|
||||
if (window.refreshFileTree) window.refreshFileTree();
|
||||
syncUrl(); // 详情拿到才写地址栏:坏链接不会把刷新后的页面也钉死在坏会话上
|
||||
} catch (e) {
|
||||
if (state.sessionId === id) {
|
||||
|
||||
@@ -362,29 +362,55 @@
|
||||
} catch (e) { toast('修改失败:' + e.message); }
|
||||
}
|
||||
// ---------- 页:账户——用户画像编辑卡直接渲染在账户页(无二级页;保存后 renderSetPage 重拉刷新停用徽标) ----------
|
||||
const PREF_MAX = 1000; // 与后端 user-profile.js 的 PREF_MAX 对齐
|
||||
const PROFILE_FIELDS = [
|
||||
['honorific', '称谓', '希望被怎么称呼', 80],
|
||||
['industry', '行业', '影响术语与默认假设', 80],
|
||||
['preference', '偏好', '顿号分隔标签(AI 会在沟通中自动补充,可手动增删)', 300],
|
||||
['preference', '偏好', '一行一条', 0, 'textarea'],
|
||||
];
|
||||
function prefCountText(n) {
|
||||
return '已用 ' + n + ' / ' + PREF_MAX + ' 字' + (n > PREF_MAX ? ',超出部分保存时截断' : '');
|
||||
}
|
||||
function prefCount() {
|
||||
const el = $('prof-preference'), out = $('prof-count-preference');
|
||||
if (!el || !out) return;
|
||||
const n = el.value.trim().length;
|
||||
out.textContent = prefCountText(n);
|
||||
out.style.color = n > PREF_MAX ? 'var(--fg-c0392b)' : '';
|
||||
}
|
||||
function profileCardHtml(p) {
|
||||
let html = '<div class="settings-card"><h4>用户画像' +
|
||||
(p.enabled === false ? ' <span class="badge held">已停用</span>' : '') + '</h4>' +
|
||||
'<div class="cfg-kv"><label>启用</label><input type="checkbox" id="prof-enabled"' + (p.enabled === false ? '' : ' checked') + '></div>';
|
||||
for (const [k, name, ph, maxLen] of PROFILE_FIELDS) {
|
||||
for (const [k, name, ph, maxLen, kind] of PROFILE_FIELDS) {
|
||||
const v = String(p[k] || '');
|
||||
html += '<div class="cfg-kv"><label>' + esc(name) + '</label>' +
|
||||
'<input class="cfg-input" id="prof-' + k + '" type="text" maxlength="' + (maxLen || 80) + '" data-orig="' + att(v) + '" value="' + att(v) + '" placeholder="' + esc(ph) + '"></div>';
|
||||
if (kind === 'textarea') {
|
||||
// 用户输入什么就存什么、注入什么:不拆不拼、不规范化分隔符,换行原样保留
|
||||
html += '<div class="cfg-kv stack"><label>' + esc(name) + '</label>' +
|
||||
'<textarea class="cfg-textarea" id="prof-' + k + '" rows="8" placeholder="' + esc(ph) + '" oninput="sUI.prefCount()">' + esc(v) + '</textarea>' +
|
||||
'<div class="cfg-note" id="prof-count-' + k + '">' + prefCountText(v.length) + '</div></div>';
|
||||
} else {
|
||||
html += '<div class="cfg-kv"><label>' + esc(name) + '</label>' +
|
||||
'<input class="cfg-input" id="prof-' + k + '" type="text" maxlength="' + (maxLen || 80) + '" data-orig="' + att(v) + '" value="' + att(v) + '" placeholder="' + esc(ph) + '"></div>';
|
||||
}
|
||||
}
|
||||
html += '<button class="primary-btn" style="margin-top:8px" onclick="sUI.saveProfile()">保存</button>' +
|
||||
'<div class="cfg-note">画像只记「不知道就会做错」的信息,新会话开始时作为背景注入(会话内不更新;清空字段保存即抹除)。' +
|
||||
'AI 会在沟通中持续维护(尤其偏好标签,只记明确表达过的,写入自下个会话生效),这里手动改随时可覆盖。' +
|
||||
'停用后不再注入,AI 的记录请求也会被拒。近期关注(最近会话标题)自动带入背景,无需维护。</div></div>';
|
||||
'<div class="cfg-note">新会话生效</div></div>';
|
||||
return html;
|
||||
}
|
||||
async function saveProfile() {
|
||||
const body = { enabled: $('prof-enabled').checked };
|
||||
for (const [k] of PROFILE_FIELDS) { const el = $('prof-' + k); if (el) body[k] = el.value.trim(); }
|
||||
for (const [k, , , , kind] of PROFILE_FIELDS) {
|
||||
const el = $('prof-' + k);
|
||||
if (!el) continue;
|
||||
const v = el.value.trim();
|
||||
if (kind === 'textarea' && v.length > PREF_MAX) {
|
||||
body[k] = v.slice(0, PREF_MAX);
|
||||
toast('偏好超过 ' + PREF_MAX + ' 字,已截断保存');
|
||||
} else {
|
||||
body[k] = v;
|
||||
}
|
||||
}
|
||||
try { await req('PUT', '/api/profile', body); toast('已保存,新会话生效'); renderSetPage(); }
|
||||
catch (e) { toast('保存失败:' + e.message); }
|
||||
}
|
||||
@@ -1260,7 +1286,7 @@
|
||||
}
|
||||
function pfNewModel() {
|
||||
const r = { ref: null, id: '', vision: true, enable: true, display_name: '', profile: '',
|
||||
extra_params: '', thinking_params: '', reasoning_ack_field: '', max_context: 0, adv: false };
|
||||
extra_params: '', thinking_params: '', reasoning_ack_field: '', max_context: 0, store: false, adv: false };
|
||||
if (pfEdit) applyModelDefaults(r, pfEdit.preset);
|
||||
return r;
|
||||
}
|
||||
@@ -1284,6 +1310,7 @@
|
||||
thinking_params: pfJsonText(m.thinking_params),
|
||||
reasoning_ack_field: m.reasoning_ack_field || '',
|
||||
max_context: Number(m.max_context) || 0,
|
||||
store: m.store === true,
|
||||
adv: false,
|
||||
})),
|
||||
};
|
||||
@@ -1374,6 +1401,7 @@
|
||||
'<div class="mpe-cks">' +
|
||||
'<label class="mpe-ck"><input type="checkbox"' + (r.vision ? ' checked' : '') + ' onchange="sUI.mModVision(' + i + ',this.checked)">图片输入</label>' +
|
||||
'<label class="mpe-ck"><input type="checkbox"' + (r.enable !== false ? ' checked' : '') + ' onchange="sUI.mModEnable(' + i + ',this.checked)">启用</label>' +
|
||||
'<label class="mpe-ck" title="只发新增消息,需上游有 context relay"><input type="checkbox"' + (r.store ? ' checked' : '') + ' onchange="sUI.mModField(' + i + ',\'store\',this.checked)">增量上下文</label>' +
|
||||
'<button type="button" class="mpe-adv-btn" onclick="sUI.mAdvToggle(' + i + ')">' + (r.adv ? '收起' : '高级') + '</button>' +
|
||||
'</div>' + adv +
|
||||
'</div>';
|
||||
@@ -1430,6 +1458,7 @@
|
||||
if (ra) nv.reasoning_ack_field = ra;
|
||||
const mc = Math.max(0, parseInt(r.max_context, 10) || 0);
|
||||
if (mc) nv.max_context = mc;
|
||||
if (r.store === true) nv.store = true; // 缺省 false,只在勾选时写
|
||||
return nv;
|
||||
}
|
||||
// 根据工作态算批量编辑:原地改(值变更项) → 删行(降序) → 新增行(append)。
|
||||
@@ -1512,6 +1541,12 @@
|
||||
if (mc !== curMc) {
|
||||
edits.push(mc ? { path: mp.concat('max_context'), value: mc } : { path: mp.concat('max_context'), op: 'delete' });
|
||||
}
|
||||
// 增量上下文:缺省不写,只在勾选时落键,取消勾选则删键
|
||||
const curSt = om.store === true;
|
||||
const st = r.store === true;
|
||||
if (st !== curSt) {
|
||||
edits.push(st ? { path: mp.concat('store'), value: true } : { path: mp.concat('store'), op: 'delete' });
|
||||
}
|
||||
} else {
|
||||
edits.push({ op: 'append', path: ['platforms', pfEdit.idx, 'models'], value: pfModelValue(r) });
|
||||
}
|
||||
@@ -1989,6 +2024,7 @@
|
||||
// 账户
|
||||
changePassword: changePassword,
|
||||
saveProfile: saveProfile,
|
||||
prefCount: prefCount,
|
||||
// 用户管理(仅管理员)
|
||||
openUserAdd: openUserAdd,
|
||||
openUserEdit: openUserEditor,
|
||||
|
||||
@@ -182,15 +182,16 @@
|
||||
state.streamEl = bubble;
|
||||
return wrap;
|
||||
}
|
||||
// 思考面板:首次思考 delta 到达时创建,带 open 状态(流式中默认展开)
|
||||
// 思考面板:首次思考 delta 到达时创建。**不展开**,只在 summary 里挂一段动态文字,
|
||||
// 让人感觉到在干活;正文开始后清空那段文字,需要看全文再点开。
|
||||
function ensureStreamThinkPanel() {
|
||||
if (state.streamThinkEl) return state.streamThinkEl;
|
||||
if (!state.streamWrap) ensureStreamWrap();
|
||||
const det = document.createElement('details');
|
||||
det.className = 'msg-think streaming';
|
||||
det.open = true; // 流式阶段默认展开,看思考增量
|
||||
det.open = false; // 流式阶段也不展开:展开的大段思考会把下面的内容一路顶下去
|
||||
const sum = document.createElement('summary');
|
||||
sum.innerHTML = '思考过程' + THINK_COPY_BTN;
|
||||
sum.innerHTML = '思考过程<span class="msg-think-live"></span>' + THINK_COPY_BTN;
|
||||
const body = document.createElement('div');
|
||||
body.className = 'msg-think-body';
|
||||
det.appendChild(sum);
|
||||
@@ -198,10 +199,12 @@
|
||||
// 插在气泡之前(思考 → 结论 的顺序)
|
||||
state.streamWrap.insertBefore(det, state.streamEl);
|
||||
state.streamThinkEl = body;
|
||||
state.streamThinkLive = sum.querySelector('.msg-think-live');
|
||||
return body;
|
||||
}
|
||||
// 思考结束(正文开始流式或本轮收尾):自动折叠思考面板并去掉流式高亮边框;用户仍可手动点开回看
|
||||
// 思考结束(正文开始流式或本轮收尾):清空标题行那段动态文字、折叠面板、去掉流式标记
|
||||
function collapseStreamThink() {
|
||||
if (state.streamThinkLive) state.streamThinkLive.textContent = '';
|
||||
if (!state.streamThinkEl) return;
|
||||
const det = state.streamThinkEl.closest('details');
|
||||
if (det) { det.open = false; det.classList.remove('streaming'); }
|
||||
@@ -223,13 +226,22 @@
|
||||
el.textContent = full; // 结构不符(收尾后重建/罕见回退)才整段重写
|
||||
}
|
||||
|
||||
// 标题行里的动态文字:只取最后一行(单行截断),持续换内容形成“在动”的感觉。
|
||||
// 不能像正文那样 appendData:这里是一行标题,追加只会无限变长。
|
||||
function paintThinkLive(el, full) {
|
||||
if (!el) return;
|
||||
const lines = String(full || '').split('\n').filter(Boolean);
|
||||
const tail = lines.length ? lines[lines.length - 1] : '';
|
||||
if (el.textContent !== tail) el.textContent = tail;
|
||||
}
|
||||
|
||||
function scheduleStreamPaint() {
|
||||
if (streamPaintQueued) return;
|
||||
streamPaintQueued = true;
|
||||
requestAnimationFrame(() => {
|
||||
streamPaintQueued = false;
|
||||
paintStreamingText(state.streamEl, state.streamText);
|
||||
paintStreamingText(state.streamThinkEl, state.streamReasoning);
|
||||
paintThinkLive(state.streamThinkLive, state.streamReasoning);
|
||||
maybeAutoScroll();
|
||||
updateScrollBottomBtn();
|
||||
});
|
||||
@@ -243,7 +255,7 @@
|
||||
state.lastSseAt = Date.now(); // 建连即重置静默计时
|
||||
// 任何具名事件都算连接存活:静默看门狗与回前台宽限共用(error 不列,原生连接错误也走 error)
|
||||
['ping', 'status', 'assistant_delta', 'reasoning_delta', 'assistant_message', 'tool_call_start',
|
||||
'tool_call_args', 'tool_call', 'tool_result', 'ui_card', 'auto_compress', 'done', 'title', 'think_auto',
|
||||
'tool_call_args', 'tool_call', 'tool_output', 'tool_result', 'ui_card', 'auto_compress', 'done', 'title', 'think_auto',
|
||||
'pending_added', 'pending_removed', 'user_message']
|
||||
.forEach(function (n) { es.addEventListener(n, function () { markSseAlive(n); }); });
|
||||
es.addEventListener('status', ev => {
|
||||
@@ -416,14 +428,20 @@
|
||||
if (state.toolCards[d.id]) {
|
||||
// 执行时的完整参数事件:若为流式早卡则回填收尾(unwrap 后的干净参数),普通卡不动
|
||||
finalizeToolCardArgs(d.id, d.name, d.arguments);
|
||||
state.toolCards[d.id].classList.add('running'); // 执行中:摘要行圆点转起来
|
||||
return;
|
||||
}
|
||||
// 无早卡(旧行为/刷新后重连):直接建展开卡
|
||||
const card = renderToolCard(d.id, d.name, d.arguments, Date.now(), { open: true });
|
||||
card.classList.add('running');
|
||||
$('chat-view').appendChild(card);
|
||||
state.toolCards[d.id] = card;
|
||||
maybeAutoScroll();
|
||||
});
|
||||
es.addEventListener('tool_output', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
appendToolOutput(d.id, d.chunk);
|
||||
});
|
||||
es.addEventListener('tool_result', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
fillToolResult(d.tool_call_id, d.content, d.is_error, d.image_url, d.msg_id);
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
+1
File diff suppressed because one or more lines are too long
@@ -19,7 +19,7 @@ function reqHeaders(cfg) {
|
||||
}
|
||||
|
||||
// 执行一条命令。返回 { code, killed_by, out };传输层失败抛 Error(由调用方决定如何呈现)。
|
||||
// opts: { command, cwd?, timeout_ms?, env?, run? }(run = engine 的 {cancelled, procs},可省略)
|
||||
// opts: { command, cwd?, timeout_ms?, env?, run?, onChunk? }(run = engine 的 {cancelled, procs},可省略)
|
||||
async function run(cfg, opts) {
|
||||
const base = cfg.executor.url;
|
||||
const timeoutMs = opts.timeout_ms && opts.timeout_ms > 0 ? opts.timeout_ms : 300000;
|
||||
@@ -73,7 +73,11 @@ async function run(cfg, opts) {
|
||||
let ev;
|
||||
try { ev = JSON.parse(line.slice(6)); } catch (_) { continue; }
|
||||
if (ev.type === 'start') id = ev.id;
|
||||
else if (ev.type === 'out' || ev.type === 'err') out += ev.chunk;
|
||||
else if (ev.type === 'out' || ev.type === 'err') {
|
||||
out += ev.chunk;
|
||||
// 增量回调(UI 实时显示用):失败不影响主流程
|
||||
if (opts.onChunk) { try { opts.onChunk(ev.chunk); } catch (_) {} }
|
||||
}
|
||||
else if (ev.type === 'exit') { exit = ev; break outer; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,10 +81,6 @@ router.post('/:id/files', upload.array('files', 10), function (req, res) {
|
||||
// 为什么不复用 GET /:id/files:那个接口同步递归整棵目录,会话里一旦有大项目 /
|
||||
// node_modules,单线程 core 会被卡死(连 SSE 一起挂)。这里一律「一次一层」、
|
||||
// 忽略大目录、限量返回,客户端展开才请求下一层。
|
||||
const TREE_IGNORE_DIRS = new Set([
|
||||
'node_modules', '.git', 'dist', 'build', 'out', 'target', '.venv', 'venv',
|
||||
'__pycache__', '.next', '.cache', 'coverage', '.idea', '.vscode',
|
||||
]);
|
||||
const TREE_MAX = 500;
|
||||
|
||||
// 相对路径 → 会话根内的绝对路径。字面 + realpath 双重校验,防符号链接跳出会话目录。
|
||||
@@ -122,19 +118,18 @@ router.get('/:id/tree', function (req, res) {
|
||||
// mtimeMs 给前端判断空目录是否还是空的:目录内增删子项会更新它
|
||||
let mtimeMs = null;
|
||||
try { mtimeMs = fs.statSync(path.join(abs, e.name)).mtimeMs; } catch (_) {}
|
||||
entries.push({ name: e.name, rel: childRel, type: 'dir', ignored: TREE_IGNORE_DIRS.has(e.name), mtimeMs: mtimeMs });
|
||||
entries.push({ name: e.name, rel: childRel, type: 'dir', mtimeMs: mtimeMs });
|
||||
} else if (e.isFile()) {
|
||||
let size = null;
|
||||
try { size = fs.statSync(path.join(abs, e.name)).size; } catch (_) {}
|
||||
entries.push({ name: e.name, rel: childRel, type: 'file', size: size });
|
||||
let size = null, mtimeMs = null;
|
||||
try { const st = fs.statSync(path.join(abs, e.name)); size = st.size; mtimeMs = st.mtimeMs; } catch (_) {}
|
||||
entries.push({ name: e.name, rel: childRel, type: 'file', size: size, mtimeMs: mtimeMs });
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: String((err && err.message) || err) });
|
||||
}
|
||||
// 排序:正常目录 → 正常文件 → 被忽略的大目录(灰显沉底);同类按名称
|
||||
// 排序:目录在前、文件在后;同类按名称
|
||||
entries.sort((a, b) =>
|
||||
(a.ignored ? 1 : 0) - (b.ignored ? 1 : 0) ||
|
||||
(a.type === 'dir' ? 0 : 1) - (b.type === 'dir' ? 0 : 1) ||
|
||||
(a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
||||
res.json({
|
||||
|
||||
@@ -14,6 +14,25 @@ const engine = require('../engine');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// 这些接口都是弹宿主机的原生对话框 / 开 Finder / 开终端,只有坐在这台机器前才有意义。
|
||||
// 远程访问(经隧道 / 反代 / 局域网)一律当作不存在,返回 404 —— 行为和服务器形态完全一致
|
||||
// (那边 server.js 根本不挂这个路由),前端原有的 catch 就会把入口整条隐藏。
|
||||
// 判断依据:反向代理一定会加 X-Forwarded-* 头(nginx 设的,tunnel client 原样转发),
|
||||
// 本机直连 127.0.0.1:8090 不会有。
|
||||
// 不能看 Host:tunnel client 用 fetch 转发时 Host 是禁止头、会被重写成 127.0.0.1:8090。
|
||||
// 也不能看 X-Forwarded-For 的 IP 值:stream 分流后源地址永远是 127.0.0.1。
|
||||
function isLocalHost(req) {
|
||||
if (req.headers['x-forwarded-for'] || req.headers['x-forwarded-host']
|
||||
|| req.headers['x-real-ip'] || req.headers['x-forwarded-proto']) return false;
|
||||
const host = String(req.headers.host || '').replace(/:\d+$/, '').toLowerCase().replace(/^\[|\]$/g, '');
|
||||
return host === '127.0.0.1' || host === 'localhost' || host === '::1';
|
||||
}
|
||||
|
||||
router.use(function (req, res, next) {
|
||||
if (isLocalHost(req)) return next();
|
||||
res.status(404).json({ error: 'Not Found' });
|
||||
});
|
||||
|
||||
function sendErr(res, err) {
|
||||
const code = (err && err.status_code) || 500;
|
||||
res.status(code).json({ error: String((err && err.message) || err) });
|
||||
|
||||
@@ -13,6 +13,7 @@ const scheduler = require('./scheduler');
|
||||
const wechat = require('./wechat');
|
||||
const llm = require('./llm');
|
||||
const mcp = require('./mcp');
|
||||
const devices = require('./devices');
|
||||
const skills = require('./tools/skills');
|
||||
const log = require('./log');
|
||||
|
||||
@@ -48,6 +49,11 @@ wechat.init(cfg, db, bus);
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
||||
// 设备接入(devices.js):设备主动出站连 wss://<公网前缀>/dev/ws。
|
||||
// 只在「有公网前缀 + 配了 DEVICE_TOKEN」时真正挂升级处理器,本地部署静默跳过。
|
||||
devices.init(cfg);
|
||||
devices.attach(server);
|
||||
|
||||
// wxbot 内网回调(/wxhook/inbox|hook):凭共享服务 token(Bearer)把门,微信用户永远不该直接碰
|
||||
// 这两个端点(wxbot 只挂内网)。必须挂在全局 express.json(2mb) 之前:媒体 base64 转发体远超 2mb,
|
||||
// 由 hookRouter 自带的 32mb 解析器先读掉并置 req._body,下游全局 json 解析器自动跳过(同 executor 不冲突)。
|
||||
|
||||
+43
-3
@@ -5,6 +5,31 @@ function createBus() {
|
||||
const clients = new Map(); // session_id -> Set<res>
|
||||
const internal = new Set(); // fn(sessionId, event, data)
|
||||
|
||||
// 高频事件的合并缓冲:LLM 每吐一个 token 就是一条 assistant_delta,一次 res.write 就是一个
|
||||
// TCP 包(Node 默认 TCP_NODELAY)。手机热点 / 跨境链路上包数比字节数更伤——每包都要射频调度,
|
||||
// 拥塞窗口也容易被小包拖住。摍 50ms 或 8 条合并成一次 write。
|
||||
// 其他事件立即发,但发前先 flush,保证前端看到的顺序和产生顺序一致。
|
||||
const MERGE_EVENTS = new Set(['assistant_delta', 'reasoning_delta', 'tool_call_args']);
|
||||
const FLUSH_MS = 50;
|
||||
const FLUSH_COUNT = 8;
|
||||
const pending = new Map(); // session_id -> { parts: string[], timer }
|
||||
|
||||
function writeAll(sessionId, payload) {
|
||||
const set = clients.get(sessionId);
|
||||
if (!set || set.size === 0) return;
|
||||
for (const res of Array.from(set)) {
|
||||
try { res.write(payload); } catch (_) { removeClient(sessionId, res); }
|
||||
}
|
||||
}
|
||||
|
||||
function flush(sessionId) {
|
||||
const p = pending.get(sessionId);
|
||||
if (!p) return;
|
||||
pending.delete(sessionId);
|
||||
if (p.timer) { clearTimeout(p.timer); p.timer = null; }
|
||||
if (p.parts.length) writeAll(sessionId, p.parts.join(''));
|
||||
}
|
||||
|
||||
function onInternal(fn) {
|
||||
internal.add(fn);
|
||||
return () => internal.delete(fn);
|
||||
@@ -19,7 +44,10 @@ function createBus() {
|
||||
const set = clients.get(sessionId);
|
||||
if (!set) return;
|
||||
set.delete(res);
|
||||
if (set.size === 0) clients.delete(sessionId);
|
||||
if (set.size === 0) {
|
||||
clients.delete(sessionId);
|
||||
flush(sessionId); // 没人看了,把还摍着的清掉,避免 pending 常驻
|
||||
}
|
||||
}
|
||||
|
||||
// event 名称 + 任意 JSON data。无客户端时静默丢弃(消息本身已持久化,重连可回放)。
|
||||
@@ -32,13 +60,25 @@ function createBus() {
|
||||
}
|
||||
const set = clients.get(sessionId);
|
||||
if (!set || set.size === 0) return;
|
||||
|
||||
const payload = 'event: ' + event + '\ndata: ' + JSON.stringify(data) + '\n\n';
|
||||
for (const res of Array.from(set)) {
|
||||
try { res.write(payload); } catch (_) { removeClient(sessionId, res); }
|
||||
|
||||
if (MERGE_EVENTS.has(event)) {
|
||||
let p = pending.get(sessionId);
|
||||
if (!p) { p = { parts: [], timer: null }; pending.set(sessionId, p); }
|
||||
p.parts.push(payload);
|
||||
if (p.parts.length >= FLUSH_COUNT) { flush(sessionId); return; }
|
||||
if (!p.timer) p.timer = setTimeout(() => flush(sessionId), FLUSH_MS);
|
||||
return;
|
||||
}
|
||||
|
||||
// 非合并事件:先把摍着的发掉,保证顺序不乱
|
||||
flush(sessionId);
|
||||
writeAll(sessionId, payload);
|
||||
}
|
||||
|
||||
function closeAll(sessionId) {
|
||||
flush(sessionId);
|
||||
const set = clients.get(sessionId);
|
||||
if (!set) return;
|
||||
for (const res of Array.from(set)) {
|
||||
|
||||
@@ -198,23 +198,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "profile",
|
||||
"description": "Maintain the current user's profile — stable facts that persist across conversations and get injected into the context of NEW sessions (the current session's injected copy is frozen). Ops: list (read the live profile), set (honorific or industry, single short value), add (append preference tags), remove (delete exact preference tags). Rules: only record what the user has clearly stated or asked to be remembered (form of address, domain, stable output/work preferences); never guess personality, never store one-time task parameters; brief tags beat essays. Preference is a 、-separated tag list (each tag ≤15 chars, ≤24 tags, ≤300 chars total; overflow is rejected — consolidate or remove first). When unsure what is already recorded, op=list returns live state — trust it over memory.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["op"],
|
||||
"properties": {
|
||||
"op": { "type": "string", "enum": ["list", "set", "add", "remove"], "description": "list = read live profile; set = single-value honorific/industry; add/remove = preference tags." },
|
||||
"honorific": { "type": "string", "description": "op=set: how to address the user (≤20 chars, e.g. 王工). Pass empty string to clear. Exactly one of honorific/industry per call." },
|
||||
"industry": { "type": "string", "description": "op=set: the user's industry/domain (≤20 chars, e.g. 金融). Pass empty string to clear." },
|
||||
"tags": { "type": "array", "items": { "type": "string" }, "description": "op=add/remove: preference tag list, e.g. [\"偏好简洁回复\", \"图表中文标注\"]. Matching is exact text." }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
@@ -340,11 +323,17 @@
|
||||
"description": "Run or inspect the skills installed on this server. Each skill describes its purpose and input_schema.\n\n{\"op\":\"list\"} list every skill with a one-line description\n{\"op\":\"get\",\"name\":\"<skill>\"} return the skill's full details; call it before the first run\n{\"op\":\"run\",\"name\":\"<skill>\",\"args\":{...}} execute it; args follow its input_schema\n\nop is required. op=get and op=run require name. Put skill arguments inside args, never at the top level.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": ["op"],
|
||||
"required": [
|
||||
"op"
|
||||
],
|
||||
"properties": {
|
||||
"op": {
|
||||
"type": "string",
|
||||
"enum": ["list", "get", "run"],
|
||||
"enum": [
|
||||
"list",
|
||||
"get",
|
||||
"run"
|
||||
],
|
||||
"description": "Operation to perform on skills."
|
||||
},
|
||||
"name": {
|
||||
@@ -403,6 +392,56 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "device",
|
||||
"description": "Operate the remote devices (other machines) connected to this server. Devices dial out to this server, so they need no inbound port; they stay connected until closed.\n\n- action=list: no other params. Returns every online device with os/arch/shell/root. Always call this first when you don't know device names.\n- action=health: requires device. Returns host uptime, load, memory, disk and temperature (fields the platform can't provide are -1).\n- action=exec: requires device and command. Runs the command through a shell on that device (sh -c on Unix, cmd /c on Windows) and returns stdout, stderr and the exit code. `cwd` must be inside that device's work root; omit it to use the root.\n\n- action=pull: requires device and remote_path; copies a file from the device into the session directory so read_file / skills can use it.\n- action=push: requires device, local_path and remote_path; sends a session file to the device.\n\nUse it when the work has to happen on the user's own machine (laptop, box, Windows PC). For the machine running this server use `terminal` instead. If a device is offline, say so explicitly — do not silently run the command elsewhere.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"action"
|
||||
],
|
||||
"properties": {
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"list",
|
||||
"health",
|
||||
"exec",
|
||||
"pull",
|
||||
"push"
|
||||
],
|
||||
"description": "Operation to perform."
|
||||
},
|
||||
"device": {
|
||||
"type": "string",
|
||||
"description": "Device name from action=list."
|
||||
},
|
||||
"command": {
|
||||
"type": "string",
|
||||
"description": "Shell command line (action=exec). Avoid interactive commands."
|
||||
},
|
||||
"cwd": {
|
||||
"type": "string",
|
||||
"description": "Working directory on the device; must be inside its work root (action=exec)."
|
||||
},
|
||||
"timeout_ms": {
|
||||
"type": "integer",
|
||||
"description": "Default 120000, max 600000 (action=exec)."
|
||||
},
|
||||
"remote_path": {
|
||||
"type": "string",
|
||||
"description": "设备上的绝对路径,必须在设备工作根内(pull/push)。"
|
||||
},
|
||||
"local_path": {
|
||||
"type": "string",
|
||||
"description": "会话目录内的相对路径。pull 省略则用远端文件名;push 必填(源文件)。"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
|
||||
+119
-37
@@ -11,9 +11,10 @@ const skills = require('./skills');
|
||||
const Database = require('better-sqlite3'); // db 工具:会话目录下的 data.db(同步 API,与单进程模型契合)
|
||||
const TurndownService = require('turndown'); // fetch 工具:HTML→Markdown 用真解析器(内置 htmlparser2,容错截断/畸形文档)
|
||||
const rexec = require('../remote-exec');
|
||||
const devices = require('../devices'); // device 工具:远端执行节点(设备主动出站连 core,见 devices.js)
|
||||
const log = require('../log'); // device 工具调用日志(info 记动作+设备+耗时+命令截断)
|
||||
const publicFiles = require('../public-files');
|
||||
const scheduler = require('../scheduler'); // 只依赖 log/cron-parser/time.js,不成环(engine→executor→scheduler 单向)
|
||||
const profile = require('../user-profile'); // profile 工具的数据面(仅 llm/log 依赖,无环)
|
||||
const memory = require('../memory'); // recall 工具的数据面(日记翻阅;仅依赖 time.js,无环)
|
||||
// lab 的 help 伪工具要读全量定义:与 engine 同路径 require → node 缓存同一对象,单一事实源。
|
||||
const BUILTIN_TOOL_DEFS = require('./definitions.json');
|
||||
@@ -784,6 +785,116 @@ function drainUiCards(text, ctx) {
|
||||
return keep.join('\n');
|
||||
}
|
||||
|
||||
// device 工具:把命令送到远端设备执行(设备主动出站连 core,连接表在 devices.js)。
|
||||
// 与 terminal 的分工:terminal 只在 core 所在机器/容器里跑,需要落在用户自己的电脑、盒子、
|
||||
// Windows 上时用 device。设备不在线就明确报错,不自动降级到本机执行。
|
||||
// device 日志用:命令压成一行并截断,避免长命令刷屏
|
||||
function devCmd(s, n) {
|
||||
const t = String(s || '').replace(/\s+/g, ' ').trim();
|
||||
return t.length > (n || 120) ? t.slice(0, n || 120) + '…' : t;
|
||||
}
|
||||
|
||||
// device 日志用:字节数human化
|
||||
function devBytes(n) {
|
||||
if (!n) return '0B';
|
||||
if (n < 1024) return n + 'B';
|
||||
if (n < 1024 * 1024) return (n / 1024).toFixed(1) + 'KB';
|
||||
return (n / 1024 / 1024).toFixed(2) + 'MB';
|
||||
}
|
||||
|
||||
async function toolDevice(ctx, args) {
|
||||
if (!devices.enabled()) {
|
||||
return '设备接入未启用:需要配置公网前缀(PUBLIC_BASE_URL 或设置页「公网地址」)和 DEVICE_TOKEN,重启 core 后生效。';
|
||||
}
|
||||
const action = String(args.action || '').trim();
|
||||
|
||||
if (action === 'list') {
|
||||
const list = devices.list();
|
||||
log.debug('device', 'list → ' + list.length + ' 台');
|
||||
if (!list.length) return '当前没有设备在线(设备需主动连 wss://<公网前缀>/dev/ws)。';
|
||||
return JSON.stringify(list, null, 2);
|
||||
}
|
||||
|
||||
if (action === 'health') {
|
||||
const name = String(args.device || '').trim();
|
||||
if (!name) return 'Error: action=health 需要 device 参数(先调 action=list 看设备名)';
|
||||
try {
|
||||
const h = await devices.health(name);
|
||||
log.debug('device', 'health ' + name + ' ok');
|
||||
return JSON.stringify(h, null, 2);
|
||||
} catch (e) {
|
||||
log.warn('device', 'health ' + name + ' 失败:' + e.message);
|
||||
return 'Error: ' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
if (action === 'exec') {
|
||||
const name = String(args.device || '').trim();
|
||||
if (!name) return 'Error: action=exec 需要 device 参数(先调 action=list 看设备名)';
|
||||
if (!String(args.command || '').trim()) return 'Error: action=exec 需要 command 参数';
|
||||
try {
|
||||
const r = await devices.exec({
|
||||
device: name,
|
||||
command: args.command,
|
||||
cwd: args.cwd,
|
||||
timeout_ms: args.timeout_ms,
|
||||
});
|
||||
log.info('device', `exec ${r.device} code=${r.code} ${r.ms}ms cmd="${devCmd(args.command)}"`);
|
||||
const head = `device=${r.device} exit=${r.code}`
|
||||
+ (r.killed_by ? ` killed_by=${r.killed_by}` : '')
|
||||
+ ` ${r.ms}ms` + (r.truncated ? '(输出已截断)' : '');
|
||||
const parts = [head];
|
||||
if (r.note) parts.push('note: ' + r.note);
|
||||
if (r.stdout && r.stdout.trim()) parts.push('--- stdout ---\n' + r.stdout.replace(/\s+$/, ''));
|
||||
if (r.stderr && r.stderr.trim()) parts.push('--- stderr ---\n' + r.stderr.replace(/\s+$/, ''));
|
||||
return parts.join('\n');
|
||||
} catch (e) {
|
||||
log.warn('device', `exec ${name} 失败:${e.message} cmd="${devCmd(args.command)}"`);
|
||||
return 'Error: ' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
if (action === 'pull') {
|
||||
const name = String(args.device || '').trim();
|
||||
const remote = String(args.remote_path || '').trim();
|
||||
if (!name) return 'Error: action=pull 需要 device 参数(先调 action=list 看设备名)';
|
||||
if (!remote) return 'Error: action=pull 需要 remote_path 参数';
|
||||
try {
|
||||
const rel = String(args.local_path || '').trim() || path.basename(remote);
|
||||
const abs = resolveInTask(ctx.taskDir, rel, fsRoot(ctx));
|
||||
const t0 = Date.now();
|
||||
const r = await devices.pull({ device: name, remotePath: remote, localPath: abs });
|
||||
recordFile(ctx, abs);
|
||||
log.info('device', `pull ${name} ${remote} → ${path.relative(ctx.taskDir, abs) || path.basename(abs)} ${devBytes(r.bytes)} ${Date.now() - t0}ms`);
|
||||
return `已拉到 ${path.relative(ctx.taskDir, abs) || path.basename(abs)}(${r.bytes} 字节,sha256 ${r.sha256.slice(0, 16)}…)`;
|
||||
} catch (e) {
|
||||
log.warn('device', `pull ${name} ${remote} 失败:${e.message}`);
|
||||
return 'Error: ' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
if (action === 'push') {
|
||||
const name = String(args.device || '').trim();
|
||||
const local = String(args.local_path || '').trim();
|
||||
const remote = String(args.remote_path || '').trim();
|
||||
if (!name) return 'Error: action=push 需要 device 参数(先调 action=list 看设备名)';
|
||||
if (!local) return 'Error: action=push 需要 local_path 参数(会话目录内的相对路径)';
|
||||
if (!remote) return 'Error: action=push 需要 remote_path 参数(设备上的绝对路径,须在工作根内)';
|
||||
try {
|
||||
const abs = resolveInTask(ctx.taskDir, local, fsRoot(ctx));
|
||||
const t0 = Date.now();
|
||||
const r = await devices.push({ device: name, localPath: abs, remotePath: remote });
|
||||
log.info('device', `push ${name} ${remote} ← ${path.relative(ctx.taskDir, abs) || path.basename(abs)} ${devBytes(r.bytes)} ${Date.now() - t0}ms`);
|
||||
return `已推到 ${name}:${remote}(${r.bytes} 字节,sha256 ${r.sha256.slice(0, 16)}…)`;
|
||||
} catch (e) {
|
||||
log.warn('device', `push ${name} ${remote} 失败:${e.message}`);
|
||||
return 'Error: ' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
return 'Error: action 必须是 list / health / exec / pull / push';
|
||||
}
|
||||
|
||||
async function toolTerminal(ctx, args) {
|
||||
const cfg = ctx.cfg;
|
||||
const timeout = args.timeout_ms || cfg.terminal.default_timeout_ms;
|
||||
@@ -794,7 +905,7 @@ async function toolTerminal(ctx, args) {
|
||||
|
||||
// 配了 executor.url(或 env EXECUTOR_URL):命令交给远端 agent-executor 执行,文本拼回与本地完全一致。
|
||||
if (rexec.isRemote(cfg)) {
|
||||
const r = await rexec.run(cfg, { command: args.command, cwd, timeout_ms: timeout, run: ctx.run });
|
||||
const r = await rexec.run(cfg, { command: args.command, cwd, timeout_ms: timeout, run: ctx.run, onChunk: ctx.onOutput });
|
||||
r.out = drainUiCards(r.out, ctx);
|
||||
let text = selectOutput(r.out.replace(/\n+$/, ''), args.head_lines, args.tail_lines);
|
||||
if (r.killed_by === 'timeout') {
|
||||
@@ -820,7 +931,11 @@ async function toolTerminal(ctx, args) {
|
||||
killGroup(child);
|
||||
}, timeout);
|
||||
|
||||
const onData = (d) => { out += d.toString(); };
|
||||
const onData = (d) => {
|
||||
const s = d.toString();
|
||||
out += s;
|
||||
if (ctx.onOutput) { try { ctx.onOutput(s); } catch (_) {} }
|
||||
};
|
||||
child.stdout.on('data', onData);
|
||||
child.stderr.on('data', onData);
|
||||
|
||||
@@ -1346,39 +1461,6 @@ function toolScheduleTask(ctx, args) {
|
||||
}
|
||||
}
|
||||
|
||||
// profile:用户画像增量维护(op 分派同 schedule 风格)。user 取自会话行,AI 不可指定他人;
|
||||
// 条件必填/帽子校验全在 user-profile.js(抛错由引擎包成 is_error 回执),停用拒写不拒读。
|
||||
function toolProfile(ctx, args) {
|
||||
const op = String(args.op || '').trim().toLowerCase();
|
||||
const s = ctx.db.prepare('SELECT user FROM sessions WHERE id = ?').get(ctx.sessionId);
|
||||
if (!s) throw new Error('会话不存在');
|
||||
const user = s.user;
|
||||
switch (op) {
|
||||
case 'list': {
|
||||
const p = profile.getProfile(ctx.db, user);
|
||||
const lines = [
|
||||
'称谓:' + (p.honorific || '(未记录)'),
|
||||
'行业:' + (p.industry || '(未记录)'),
|
||||
'偏好:' + (p.preference || '(未记录)'),
|
||||
];
|
||||
if (!p.enabled) lines.push('(该用户已停用画像注入;set/add/remove 会被拒绝)');
|
||||
lines.push('本会话已注入的快照不会因写入而变;新会话自动携带最新画像。');
|
||||
return lines.join('\n');
|
||||
}
|
||||
case 'set': {
|
||||
const keys = ['honorific', 'industry'].filter(k => typeof args[k] === 'string');
|
||||
if (keys.length !== 1) throw new Error('op=set 需要 honorific 或 industry 恰一个字符串参数(偏好标签用 add/remove)');
|
||||
return profile.setProfileField(ctx.db, user, keys[0], args[keys[0]]);
|
||||
}
|
||||
case 'add':
|
||||
return profile.addProfileTags(ctx.db, user, args.tags);
|
||||
case 'remove':
|
||||
return profile.removeProfileTags(ctx.db, user, args.tags);
|
||||
default:
|
||||
throw new Error('未知的 op:"' + op + '"。可用:list / set(honorific|industry) / add(tags) / remove(tags)');
|
||||
}
|
||||
}
|
||||
|
||||
// db:会话本地 SQLite(taskDir/data.db,随会话目录隔离,首次使用自动建库建文件)。
|
||||
// 连接策略:每次调用 open→用→close,不缓存句柄(会话删目录后不留脏连接);
|
||||
// 单进程 + better-sqlite3 同步 API,同进程内不存在交叠。busy_timeout 兼容
|
||||
@@ -1786,9 +1868,9 @@ const HANDLERS = {
|
||||
terminal: toolTerminal,
|
||||
fetch: toolFetch,
|
||||
schedule: toolScheduleTask,
|
||||
profile: toolProfile,
|
||||
recall: toolRecall,
|
||||
db: toolDb,
|
||||
device: toolDevice,
|
||||
skill: toolSkill,
|
||||
lab: toolLab,
|
||||
};
|
||||
|
||||
@@ -7,19 +7,15 @@
|
||||
// - 注入:会话创建/清空时把渲染文本冻结进 sessions.profile_snapshot,
|
||||
// buildMessages 每轮以 system 后首条 user 消息注入——内容字节恒定不破坏前缀缓存,
|
||||
// 且不落 messages 表,阶段切换归档/压缩都动不到它。
|
||||
// - 更新(原 handoff 落槌一次性提取链路已移除):AI 沟通中经 profile 工具增量维护
|
||||
// (list/set/add/remove,见 executor.js;写入只影响下个会话——快照冻结语义不变),
|
||||
// 用户也可在设置页手动编辑。preference 按顿号标签列表管理,工具只做增量增删、
|
||||
// 不做整段覆写——防模型重写时吃掉旧标签;停用(enabled=0)时拒写不拒读。
|
||||
// - 更新:只由用户在设置页手动编辑。原 AI 侧的 profile 工具(list/set/add/remove)已移除:
|
||||
// 模型自主记录容易把错的东西冻结成长期上下文,记什么由用户把关。
|
||||
// - preference 是用户自由输入的多行文本(一行一条即可):原样入库、原样注入,不拆不拼、
|
||||
// 不把分隔符规范化;只卡一个总长帽(PREF_MAX)。
|
||||
const log = require('./log');
|
||||
|
||||
const FIELDS = ['honorific', 'industry', 'preference'];
|
||||
const MAX_FIELD = 80; // honorific/industry 长度帽(Web 手改与工具共用)
|
||||
const PREF_MAX = 300; // preference 总长帽
|
||||
const SET_MAX = 20; // AI 写 honorific/industry 的单值帽(称谓应当短小)
|
||||
const TAG_MAX = 15; // 单个偏好标签长度帽
|
||||
const TAGS_MAX = 24; // 偏好标签数量帽
|
||||
const TAG_SPLIT = /[、,,;;\n]+/;
|
||||
const MAX_FIELD = 80; // honorific/industry 长度帽
|
||||
const PREF_MAX = 1000; // preference 总长帽(用户自由输入的多行文本,只卡总量)
|
||||
// 画像快照头:AI 友好的分块标记(与 channel_note/memory_note 同款「# [CTX] <名>」体例,
|
||||
// buildMessages 把快照作 system 后首条 user 注入,标题即自描述来源)。
|
||||
const SNAPSHOT_HEAD = '# [CTX] user_profile\n\n以下为历史会话沉淀的粗粒度认知,非任务约束;与当前请求冲突时,一律以当前请求为准。';
|
||||
@@ -54,7 +50,9 @@ function sanitizePatch(body) {
|
||||
const patch = { fields: {}, enabled: undefined };
|
||||
for (const k of FIELDS) {
|
||||
if (typeof b[k] === 'string') {
|
||||
patch.fields[k] = b[k].replace(/\s+/g, ' ').trim().slice(0, k === 'preference' ? PREF_MAX : MAX_FIELD);
|
||||
// preference 是用户按条目输入的自由文本:保留换行,只去首尾空白;另两字段压成单行
|
||||
const v = k === 'preference' ? b[k].trim() : b[k].replace(/\s+/g, ' ').trim();
|
||||
patch.fields[k] = v.slice(0, k === 'preference' ? PREF_MAX : MAX_FIELD);
|
||||
}
|
||||
}
|
||||
if (typeof b.enabled === 'boolean' || b.enabled === 0 || b.enabled === 1) patch.enabled = !!b.enabled;
|
||||
@@ -70,77 +68,6 @@ function saveProfile(db, user, patch) {
|
||||
writeRow(db, user, data, enabled);
|
||||
}
|
||||
|
||||
// ---------- profile 工具(AI 增量维护入口;op 语义与 executor.js toolProfile 对应) ----------
|
||||
// preference 存顿号连接的标签串;读取按分隔符拆 token(人工输入的整句会被拆为 token 存留,可接受)。
|
||||
function profileTokens(preference) {
|
||||
return String(preference || '').split(TAG_SPLIT).map(t => t.trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
// 所有写 op 的统一后缀:快照「新会话冻结注入」语义不因工具改变,明说免 AI 在本会话里困惑等待。
|
||||
const WRITE_NOTE = '(新会话开始时注入,本会话内不变;可用 op=list 查看实时状态)';
|
||||
|
||||
function requireEnabled(db, user) {
|
||||
if (!getProfile(db, user).enabled) {
|
||||
throw new Error('该用户已停用画像(设置页·账户·用户画像可重新启用),本次未写入。');
|
||||
}
|
||||
}
|
||||
|
||||
// 单值字段(称谓/行业)设置;空串 = 清除。
|
||||
function setProfileField(db, user, field, value) {
|
||||
if (!FIELDS.includes(field) || field === 'preference') {
|
||||
throw new Error('op=set 仅支持 honorific / industry(偏好标签请用 op=add / op=remove)');
|
||||
}
|
||||
requireEnabled(db, user);
|
||||
const v = String(value == null ? '' : value).replace(/\s+/g, ' ').trim().slice(0, SET_MAX);
|
||||
const cur = getProfile(db, user)[field];
|
||||
if (cur === v) return FIELD_LABELS[field] + '已是「' + v + '」,未改动。';
|
||||
saveProfile(db, user, { fields: { [field]: v } });
|
||||
return v
|
||||
? FIELD_LABELS[field] + '已设为「' + v + '」' + WRITE_NOTE
|
||||
: FIELD_LABELS[field] + '已清除(下次冻结时将不再出现)。';
|
||||
}
|
||||
|
||||
// 追加偏好标签:清洗 → 逐条校验长度 → 去重 → 数量/总长帽(整批原子:任一带帽违例则不落库)
|
||||
function addProfileTags(db, user, tags) {
|
||||
const list = (Array.isArray(tags) ? tags : [tags])
|
||||
.map(t => String(t == null ? '' : t).replace(TAG_SPLIT, ' ').replace(/\s+/g, ' ').trim())
|
||||
.filter(Boolean);
|
||||
if (!list.length) throw new Error('op=add 需要非空的 tags 字符串数组(单个标签也用数组传)');
|
||||
requireEnabled(db, user);
|
||||
const cur = profileTokens(getProfile(db, user).preference);
|
||||
const added = [], dup = [];
|
||||
for (const t of list) {
|
||||
if (t.length > TAG_MAX) throw new Error('标签「' + t + '」超过 ' + TAG_MAX + ' 字上限,请换成更概括的说法');
|
||||
if (cur.includes(t)) { dup.push(t); continue; }
|
||||
cur.push(t); added.push(t);
|
||||
}
|
||||
if (cur.length > TAGS_MAX) throw new Error('偏好标签已达 ' + TAGS_MAX + ' 个上限(本次将到 ' + cur.length + '),先用 op=remove 合并精简');
|
||||
const joined = cur.join('、');
|
||||
if (joined.length > PREF_MAX) throw new Error('偏好总长将超 ' + PREF_MAX + ' 字(现 ' + joined.length + '),先用 op=remove 精简');
|
||||
if (!added.length) {
|
||||
return '均已存在,未新增(' + dup.join('、') + ');当前偏好 ' + cur.length + ' 条。';
|
||||
}
|
||||
saveProfile(db, user, { fields: { preference: joined } });
|
||||
return '已记录:' + added.join('、')
|
||||
+ (dup.length ? '(已存在未重复:' + dup.join('、') + ')' : '')
|
||||
+ ';偏好现共 ' + cur.length + ' 条' + WRITE_NOTE;
|
||||
}
|
||||
|
||||
// 删除偏好标签:按 token 精确匹配;未命中的回报(引导先 list 核对写法)。
|
||||
function removeProfileTags(db, user, tags) {
|
||||
const list = (Array.isArray(tags) ? tags : [tags]).map(t => String(t == null ? '' : t).trim()).filter(Boolean);
|
||||
if (!list.length) throw new Error('op=remove 需要非空的 tags 字符串数组');
|
||||
requireEnabled(db, user);
|
||||
const cur = profileTokens(getProfile(db, user).preference);
|
||||
const removed = list.filter(t => cur.includes(t));
|
||||
const missing = list.filter(t => !cur.includes(t));
|
||||
if (!removed.length) return '未找到要删除的标签:' + missing.join('、') + '(op=list 可查看当前精确标签)';
|
||||
saveProfile(db, user, { fields: { preference: cur.filter(t => !removed.includes(t)).join('、') } });
|
||||
return '已移除:' + removed.join('、')
|
||||
+ (missing.length ? ';未找到:' + missing.join('、') : '')
|
||||
+ ';偏好现共 ' + (cur.length - removed.length) + ' 条' + WRITE_NOTE;
|
||||
}
|
||||
|
||||
// 渲染注入文本:三字段(非空才成行);停用或全空 → 空串(不注入)。
|
||||
function snapshotText(db, user) {
|
||||
const p = getProfile(db, user);
|
||||
@@ -161,5 +88,4 @@ function freezeSnapshot(db, sessionId) {
|
||||
|
||||
module.exports = {
|
||||
getProfile, sanitizePatch, saveProfile, snapshotText, freezeSnapshot,
|
||||
setProfileField, addProfileTags, removeProfileTags,
|
||||
};
|
||||
|
||||
@@ -489,7 +489,7 @@ function getTurn(bid) { return turnState.get(bid) || resetTurn(bid); }
|
||||
|
||||
// 投递前的内容归一(语义同旧 deliverNow):errAck / 空文本 → 不可投;富文本 blocks → 占位文案。
|
||||
function normalizeContent(m) {
|
||||
if (!m || m.type === 'errAck') return '';
|
||||
if (!m || m.type === 'errAck' || m.type === 'error_note') return '';
|
||||
let content = String(m.content || '').trim();
|
||||
if (!content) return '';
|
||||
if (content.startsWith('{"__blocks__"')) content = '【富文本消息,请到网页端查看】';
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
# agent-device:远端设备执行端
|
||||
|
||||
把一台机器(Linux 盒子、Windows、macOS)变成 agent 的一双“手”:**主动出站**连到 core 的
|
||||
WebSocket 端点,接收 shell 命令、流式回传输出与退出码、上报主机指标。
|
||||
|
||||
设备不需要公网 IP、不需要开任何入站端口,NAT、公司网络、随身热点都能用。
|
||||
|
||||
> 这不是通用 agent,只是执行端。会话、模型、记忆都在服务端;这里只负责“在那台机器上跑东西”。
|
||||
|
||||
## 二进制里没有服务器地址
|
||||
|
||||
`-server` 与 token 都**没有默认值**,不显式给就启动失败。所以拿到的二进制不会连到别人的服务器上——
|
||||
它只连你指定的那一台。
|
||||
|
||||
## 安装
|
||||
|
||||
### 自己编译
|
||||
|
||||
需要 Go 1.22+:
|
||||
|
||||
```sh
|
||||
git clone <本仓库> && cd device
|
||||
go build -trimpath -ldflags "-s -w" -o bin/agent-device ./cmd/agent
|
||||
```
|
||||
|
||||
交叉编译(一次出六个平台):
|
||||
|
||||
```sh
|
||||
sh scripts/build.sh all
|
||||
```
|
||||
|
||||
脚本只做构建:不联网、不推送、不依赖任何特定服务器。本机没有 Go 但装了 docker 时,
|
||||
会自动用 `golang:1.23-alpine` 容器编译。
|
||||
|
||||
### 下载预编译
|
||||
|
||||
从本仓库的 Releases 页面取对应平台的文件(`agent-device-linux-arm64`、`agent-device-windows-amd64.exe` 等),
|
||||
`chmod +x` 即可,静态链接、无运行时依赖,一个文件约 5.5 MB。
|
||||
|
||||
## 运行
|
||||
|
||||
```sh
|
||||
# 推荐:二进制旁边放一个 config.json,直接启动(token 不出现在 ps 里)
|
||||
./agent-device
|
||||
|
||||
# 或者全走命令行
|
||||
./agent-device -server wss://your-host.example/dev/ws -token <TOKEN> -root /var/agent-work -name box-01
|
||||
|
||||
# 配置文件不在默认位置时显式指定
|
||||
./agent-device -config /etc/agent-device.json
|
||||
```
|
||||
|
||||
**默认会自动读可执行文件同目录的 `config.json`**(存在才读,不存在就当没配),所以部署时把二进制和配置放一个目录、直接跑就行:
|
||||
|
||||
```json
|
||||
{
|
||||
"server": "wss://your-host.example/dev/ws",
|
||||
"token": "…",
|
||||
"root": "/var/agent-work",
|
||||
"name": "box-01"
|
||||
}
|
||||
```
|
||||
|
||||
优先级:**命令行显式参数 > 环境变量 `NODE_TOKEN`(仅 token)> 配置文件 > 默认值**。
|
||||
|
||||
### shell 用哪个
|
||||
|
||||
默认:macOS / Linux 用 `/bin/sh`(固定,不读 `$SHELL`——用户把默认 shell 设成 fish 时,
|
||||
模型写的 POSIX 语法会全废);Windows 用 `cmd`。
|
||||
|
||||
想换就在 `config.json` 里写 `shell`,**不做自动探测**:装了 Git Bash / busybox 就写路径,
|
||||
没装就保持默认。
|
||||
|
||||
```json
|
||||
{ "shell": "C:\\Program Files\\Git\\bin\\bash.exe" }
|
||||
```
|
||||
|
||||
报给 core 的是短标签(`sh` / `bash` / `busybox` / `cmd` / `powershell`),同时带上解释器路径与版本。
|
||||
|
||||
| 参数 | 说明 |
|
||||
|---|---|
|
||||
| `-server` | core 的接入地址,`wss://…/dev/ws` |
|
||||
| `-token` | Bearer token,须与服务端 `DEVICE_TOKEN` 一致 |
|
||||
| `-root` | 工作根目录,命令的 `cwd` 必须落在它之内(默认 `~/agent-work`,会自动建)。写相对路径时按**二进制所在目录**解析,如 `workspace` / `.`,不能越出该目录 |
|
||||
| `-name` | 设备名,服务端用它区分设备(默认 hostname) |
|
||||
| `-shell` | `sh` / `cmd` / `powershell`,默认按平台 |
|
||||
| `-config` | 配置文件路径,默认 `<二进制所在目录>/config.json` |
|
||||
| `-insecure` | 跳过证书校验(自签证书调试用) |
|
||||
| `-fix-clock` | 证书报错时用服务器 `Date` 头校准本机时间(小设备没有 RTC 时用,Linux 需 root) |
|
||||
|
||||
设备名重复时后接入的会顶掉前一个连接(换机、重装不留僵尸连接)。
|
||||
|
||||
## 协议
|
||||
|
||||
一条 WebSocket 上全是 JSON 文本帧,`t` 区分类型:
|
||||
|
||||
| 方向 | 消息 |
|
||||
|---|---|
|
||||
| 设备 → core | `{t:"hello", ver, name, os, arch, shell, shell_path, shell_ver, root}` |
|
||||
| core → 设备 | `{t:"exec", id, cmd, cwd, timeout_ms}` · `{t:"kill", id}` · `{t:"ping"}` · `{t:"health", id}` |
|
||||
| 文件通道 | `{t:"file", id, op:"read"\|"write", path, size?, sha256?}` · `{t:"file.chunk", id, data:<base64>}` ×N · `{t:"file.done", id, size?, sha256?, err?}` |
|
||||
| 设备 → core | `{t:"chunk", id, out:"out"\|"err", chunk:<base64>}` · `{t:"exit", id, code, killed_by, ms, truncated}` · `{t:"pong"}` · `{t:"health", …}` |
|
||||
|
||||
- 保活:core 每 20 秒发 `ping`,设备回 `pong`;90 秒无任何消息即断开重连。
|
||||
- 断线:指数退避 1s → 2s → 4s → … 上限 30s,连上后重置。
|
||||
- 超时:命令超过 `timeout_ms` 由设备侧杀**进程组**,回 `killed_by:"timeout"`。
|
||||
- 输出:单流超过上限截断并置 `truncated`。
|
||||
- `shell` 报的是实际解释器(`/bin/sh` 可能是 dash / bash / busybox),连同版本,供模型决定命令语法。
|
||||
|
||||
## 文件通道
|
||||
|
||||
core 通过同一条连接搬文件,**设备不需要任何额外端口**:
|
||||
|
||||
| 方向 | 流程 |
|
||||
|---|---|
|
||||
| 拉取(设备 → 会话目录) | `file{op:read}` → 设备分块回 `file.chunk` → `file.done{size,sha256}` |
|
||||
| 推送(会话目录 → 设备) | `file{op:write,size,sha256}` → 分块 `file.chunk` → `file.done` |
|
||||
|
||||
- 每块 256 KiB(base64 后约 341 KiB),单文件上限 2 GiB;
|
||||
- 超时按**空闲**算:首块 30s,之后每收到一块重置 30s——链路慢但不卡就不会被误杀;
|
||||
- 写盘先落 `<path>.part`,`size` 与 `sha256` 都对上才 `rename`;
|
||||
- 设备侧路径必须在工作根内(与 `exec` 的 cwd 校验同一套)。
|
||||
|
||||
## 安全边界
|
||||
|
||||
- 设备主动出站,服务端不需要知道设备 IP;
|
||||
- token 走 `Authorization: Bearer`,与业务 token 分开;
|
||||
- `cwd` 只约束**起始目录**,命令内部仍可 `cd` 到别处——给了 shell 就是这个语义,需要强隔离请用容器/namespaces;
|
||||
- 同一个 token 可被多台设备共用(当前设计),吊销需换 token 并重启所有设备。
|
||||
|
||||
## 实测
|
||||
|
||||
| 用例 | 结果 |
|
||||
|---|---|
|
||||
| exec 基本 | cwd 自动创建,stdout/stderr 分流,退出码透传 |
|
||||
| 超时 | 杀进程组,`killed_by=timeout` |
|
||||
| 起始 cwd 越界 | 拒绝 |
|
||||
| 断线重连 | 1s→2s→4s 退避,恢复后自动注册 |
|
||||
|
||||
在 Linux / macOS / Windows 三平台编译验证(amd64 + arm64);另出 `linux-mips` 软浮点版给 OpenWrt 设备。
|
||||
@@ -0,0 +1,237 @@
|
||||
package main
|
||||
|
||||
// 文件通道:core 通过同一条 WS 拉取 / 推送文件。
|
||||
//
|
||||
// 协议(与 core 的 devices.js 一一对应):
|
||||
//
|
||||
// core → 节点 {t:"file", id, op:"read", path}
|
||||
// 节点 → core {t:"file.chunk", id, data:<base64>} ×N → {t:"file.done", id, size, sha256}
|
||||
//
|
||||
// core → 节点 {t:"file", id, op:"write", path, size, sha256}
|
||||
// core → 节点 {t:"file.chunk", id, data} ×N → {t:"file.done", id}
|
||||
// 节点 → core {t:"file.done", id, size} 或 {t:"file.done", id, err}
|
||||
//
|
||||
// 写盘一律先落 <path>.part,校验通过再 rename——半截文件不能留在原地。
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"hash"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"agent-device/pkg/wsnode"
|
||||
"agent-device/pkg/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
fileChunkSize = 256 << 10 // 每块 256 KiB(base64 后约 341 KiB)
|
||||
maxFileBytes = 2 << 30 // 单文件上限 2 GiB
|
||||
)
|
||||
|
||||
type fileWriter struct {
|
||||
path string
|
||||
tmp *os.File
|
||||
tmpPath string
|
||||
hash hash.Hash
|
||||
got int64
|
||||
// 期望值(core 下发时给的,用于对账)
|
||||
size int64
|
||||
sha string
|
||||
}
|
||||
|
||||
type fileMgr struct {
|
||||
mu sync.Mutex
|
||||
writers map[string]*fileWriter
|
||||
}
|
||||
|
||||
func newFileMgr() *fileMgr {
|
||||
return &fileMgr{writers: map[string]*fileWriter{}}
|
||||
}
|
||||
|
||||
// safePath 把设备侧路径限制在 root 之内(与 exec 的 cwd 校验同一套语义)。
|
||||
func (r *runner) safePath(raw string) (string, error) {
|
||||
if raw == "" {
|
||||
return "", errors.New("path 不能为空")
|
||||
}
|
||||
p := raw
|
||||
if !filepath.IsAbs(p) {
|
||||
p = filepath.Join(r.root, p)
|
||||
}
|
||||
p = filepath.Clean(p)
|
||||
rel, err := filepath.Rel(r.root, p)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return "", errors.New("path 超出工作根:" + raw)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (r *runner) fileStart(c *wsnode.Conn, m proto.Msg) {
|
||||
switch m.Op {
|
||||
case "read":
|
||||
go r.fileRead(c, m)
|
||||
case "write":
|
||||
r.fileBegin(c, m)
|
||||
default:
|
||||
r.fileFail(c, m.ID, "未知的 file.op:"+m.Op)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *runner) fileFail(c *wsnode.Conn, id, msg string) {
|
||||
_ = c.Send(proto.Msg{T: proto.TKindFileDone, ID: id, Err: msg})
|
||||
}
|
||||
|
||||
// fileRead 把设备上的文件分块推回 core。
|
||||
func (r *runner) fileRead(c *wsnode.Conn, m proto.Msg) {
|
||||
p, err := r.safePath(m.Path)
|
||||
if err != nil {
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
f, err := os.Open(p)
|
||||
if err != nil {
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
st, err := f.Stat()
|
||||
if err != nil {
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
if st.IsDir() {
|
||||
r.fileFail(c, m.ID, "是目录,不是文件:"+p)
|
||||
return
|
||||
}
|
||||
if st.Size() > maxFileBytes {
|
||||
r.fileFail(c, m.ID, "文件超过 2GiB 上限")
|
||||
return
|
||||
}
|
||||
|
||||
h := sha256.New()
|
||||
buf := make([]byte, fileChunkSize)
|
||||
var sent int64
|
||||
for {
|
||||
n, rerr := f.Read(buf)
|
||||
if n > 0 {
|
||||
h.Write(buf[:n])
|
||||
sent += int64(n)
|
||||
if err := c.Send(proto.Msg{
|
||||
T: proto.TKindFileChunk, ID: m.ID,
|
||||
Data: append([]byte(nil), buf[:n]...),
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if rerr == io.EOF {
|
||||
break
|
||||
}
|
||||
if rerr != nil {
|
||||
r.fileFail(c, m.ID, rerr.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
_ = c.Send(proto.Msg{
|
||||
T: proto.TKindFileDone, ID: m.ID,
|
||||
Size: sent, SHA: hex.EncodeToString(h.Sum(nil)),
|
||||
})
|
||||
}
|
||||
|
||||
// fileBegin 为一次 write 建立临时文件。
|
||||
func (r *runner) fileBegin(c *wsnode.Conn, m proto.Msg) {
|
||||
p, err := r.safePath(m.Path)
|
||||
if err != nil {
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
if m.Size > maxFileBytes {
|
||||
r.fileFail(c, m.ID, "文件超过 2GiB 上限")
|
||||
return
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
tmpPath := p + ".part"
|
||||
f, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
r.files.mu.Lock()
|
||||
r.files.writers[m.ID] = &fileWriter{
|
||||
path: p, tmp: f, tmpPath: tmpPath, hash: sha256.New(),
|
||||
size: m.Size, sha: m.SHA,
|
||||
}
|
||||
r.files.mu.Unlock()
|
||||
}
|
||||
|
||||
func (r *runner) fileChunk(c *wsnode.Conn, m proto.Msg) {
|
||||
r.files.mu.Lock()
|
||||
w := r.files.writers[m.ID]
|
||||
r.files.mu.Unlock()
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
if w.got+int64(len(m.Data)) > maxFileBytes {
|
||||
r.abortWriter(m.ID, w, "超过 2GiB 上限")
|
||||
r.fileFail(c, m.ID, "超过 2GiB 上限")
|
||||
return
|
||||
}
|
||||
if _, err := w.tmp.Write(m.Data); err != nil {
|
||||
r.abortWriter(m.ID, w, err.Error())
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
w.hash.Write(m.Data)
|
||||
w.got += int64(len(m.Data))
|
||||
}
|
||||
|
||||
// fileEnd 收尾写入:校验大小与 sha256,通过才改名到正式路径。
|
||||
func (r *runner) fileEnd(c *wsnode.Conn, m proto.Msg) {
|
||||
r.files.mu.Lock()
|
||||
w := r.files.writers[m.ID]
|
||||
delete(r.files.writers, m.ID)
|
||||
r.files.mu.Unlock()
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = w.tmp.Close() }()
|
||||
|
||||
sum := hex.EncodeToString(w.hash.Sum(nil))
|
||||
if w.size > 0 && w.got != w.size {
|
||||
_ = os.Remove(w.tmpPath)
|
||||
r.fileFail(c, m.ID, "大小不符:收到 "+strconv.FormatInt(w.got, 10)+",应为 "+strconv.FormatInt(w.size, 10))
|
||||
return
|
||||
}
|
||||
if w.sha != "" && !strings.EqualFold(w.sha, sum) {
|
||||
_ = os.Remove(w.tmpPath)
|
||||
r.fileFail(c, m.ID, "sha256 不符")
|
||||
return
|
||||
}
|
||||
if err := w.tmp.Close(); err != nil {
|
||||
_ = os.Remove(w.tmpPath)
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
if err := os.Rename(w.tmpPath, w.path); err != nil {
|
||||
_ = os.Remove(w.tmpPath)
|
||||
r.fileFail(c, m.ID, err.Error())
|
||||
return
|
||||
}
|
||||
_ = c.Send(proto.Msg{T: proto.TKindFileDone, ID: m.ID, Size: w.got, SHA: sum})
|
||||
}
|
||||
|
||||
func (r *runner) abortWriter(id string, w *fileWriter, why string) {
|
||||
r.files.mu.Lock()
|
||||
delete(r.files.writers, id)
|
||||
r.files.mu.Unlock()
|
||||
_ = w.tmp.Close()
|
||||
_ = os.Remove(w.tmpPath)
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
//go:build !windows
|
||||
|
||||
package main
|
||||
|
||||
// 主机指标:Linux 读 /proc,macOS 调 sysctl / vm_stat(health 调用不频繁,允许 fork)。
|
||||
// 拿不到的指标给 -1,不编数据。
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type healthInfo struct {
|
||||
HostUptimeSec int64
|
||||
Load1, Load5, Load15 float64
|
||||
MemTotalMB, MemFreeMB int64
|
||||
DiskTotalGB, DiskFreeGB float64
|
||||
TempC float64
|
||||
}
|
||||
|
||||
func hostHealth() healthInfo {
|
||||
h := healthInfo{
|
||||
HostUptimeSec: -1, Load1: -1, Load5: -1, Load15: -1,
|
||||
MemTotalMB: -1, MemFreeMB: -1, DiskTotalGB: -1, DiskFreeGB: -1, TempC: -1,
|
||||
}
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
if b, err := os.ReadFile("/proc/uptime"); err == nil {
|
||||
if f := strings.Fields(string(b)); len(f) > 0 {
|
||||
if v, err := strconv.ParseFloat(f[0], 64); err == nil {
|
||||
h.HostUptimeSec = int64(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
if b, err := os.ReadFile("/proc/loadavg"); err == nil {
|
||||
if f := strings.Fields(string(b)); len(f) >= 3 {
|
||||
h.Load1, _ = strconv.ParseFloat(f[0], 64)
|
||||
h.Load5, _ = strconv.ParseFloat(f[1], 64)
|
||||
h.Load15, _ = strconv.ParseFloat(f[2], 64)
|
||||
}
|
||||
}
|
||||
if b, err := os.ReadFile("/proc/meminfo"); err == nil {
|
||||
for _, line := range strings.Split(string(b), "\n") {
|
||||
f := strings.Fields(line)
|
||||
if len(f) < 2 {
|
||||
continue
|
||||
}
|
||||
kb, _ := strconv.ParseInt(f[1], 10, 64)
|
||||
switch f[0] {
|
||||
case "MemTotal:":
|
||||
h.MemTotalMB = kb / 1024
|
||||
case "MemAvailable:":
|
||||
h.MemFreeMB = kb / 1024
|
||||
}
|
||||
}
|
||||
}
|
||||
if b, err := os.ReadFile("/sys/class/thermal/thermal_zone0/temp"); err == nil {
|
||||
if v, err := strconv.ParseFloat(strings.TrimSpace(string(b)), 64); err == nil {
|
||||
h.TempC = v / 1000
|
||||
}
|
||||
}
|
||||
case "darwin":
|
||||
// macOS 没有 /proc,走 sysctl / vm_stat;每次 health 只 fork 三四个进程,频率极低
|
||||
if v, err := sysctlValue("hw.memsize"); err == nil {
|
||||
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
|
||||
h.MemTotalMB = n >> 20
|
||||
}
|
||||
}
|
||||
// 开机时长:kern.boottime 形如 { sec = 1794298276, usec = 155984 } Tue Nov 10 …
|
||||
if v, err := sysctlValue("kern.boottime"); err == nil {
|
||||
if i := strings.Index(v, "sec = "); i >= 0 {
|
||||
rest := v[i+6:]
|
||||
if j := strings.IndexAny(rest, ",}"); j > 0 {
|
||||
if sec, err := strconv.ParseInt(strings.TrimSpace(rest[:j]), 10, 64); err == nil {
|
||||
h.HostUptimeSec = time.Now().Unix() - sec
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 负载:vm.loadavg 形如 { 1.11 1.40 1.59 }
|
||||
if v, err := sysctlValue("vm.loadavg"); err == nil {
|
||||
f := strings.FieldsFunc(v, func(r rune) bool { return r == '{' || r == '}' || r == ' ' })
|
||||
if len(f) >= 3 {
|
||||
h.Load1, _ = strconv.ParseFloat(f[0], 64)
|
||||
h.Load5, _ = strconv.ParseFloat(f[1], 64)
|
||||
h.Load15, _ = strconv.ParseFloat(f[2], 64)
|
||||
}
|
||||
}
|
||||
// 可用内存 ≈ free + inactive + speculative:macOS 上只看 free 会小得吓人(大半是缓存)
|
||||
if mb := darwinAvailableMB(); mb >= 0 {
|
||||
h.MemFreeMB = mb
|
||||
}
|
||||
}
|
||||
|
||||
var st syscall.Statfs_t
|
||||
if err := syscall.Statfs("/", &st); err == nil {
|
||||
h.DiskTotalGB = float64(st.Blocks) * float64(st.Bsize) / (1 << 30)
|
||||
h.DiskFreeGB = float64(st.Bavail) * float64(st.Bsize) / (1 << 30)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// sysctlValue 读一个 sysctl 键,1s 超时兜底。
|
||||
func sysctlValue(key string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
out, err := exec.CommandContext(ctx, "sysctl", "-n", key).Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(out)), nil
|
||||
}
|
||||
|
||||
// darwinAvailableMB 解析 vm_stat,返回 free+inactive+speculative 的 MB 数;解析失败给 -1。
|
||||
func darwinAvailableMB() int64 {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Millisecond)
|
||||
defer cancel()
|
||||
out, err := exec.CommandContext(ctx, "vm_stat").Output()
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
pageSize := int64(4096)
|
||||
free, inactive, speculative := int64(-1), int64(0), int64(0)
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
if i := strings.Index(line, "page size of "); i >= 0 {
|
||||
rest := line[i+len("page size of "):]
|
||||
if j := strings.Index(rest, " bytes"); j > 0 {
|
||||
if v, err := strconv.ParseInt(rest[:j], 10, 64); err == nil {
|
||||
pageSize = v
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
num := func(prefix string) (int64, bool) {
|
||||
if !strings.HasPrefix(line, prefix) {
|
||||
return 0, false
|
||||
}
|
||||
v, err := strconv.ParseInt(strings.TrimSuffix(strings.TrimSpace(line[len(prefix):]), "."), 10, 64)
|
||||
return v, err == nil
|
||||
}
|
||||
if v, ok := num("Pages free:"); ok {
|
||||
free = v
|
||||
}
|
||||
if v, ok := num("Pages inactive:"); ok {
|
||||
inactive = v
|
||||
}
|
||||
if v, ok := num("Pages speculative:"); ok {
|
||||
speculative = v
|
||||
}
|
||||
}
|
||||
if free < 0 {
|
||||
return -1
|
||||
}
|
||||
return (free + inactive + speculative) * pageSize / (1 << 20)
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
// Windows 指标:GetTickCount64(开机时长)+ GlobalMemoryStatusEx + GetDiskFreeSpaceExW。
|
||||
// 负载/温度在 Windows 上没有等价指标,返回 -1。
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type healthInfo struct {
|
||||
HostUptimeSec int64
|
||||
Load1, Load5, Load15 float64
|
||||
MemTotalMB, MemFreeMB int64
|
||||
DiskTotalGB, DiskFreeGB float64
|
||||
TempC float64
|
||||
}
|
||||
|
||||
var (
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
procTick = kernel32.NewProc("GetTickCount64")
|
||||
procMem = kernel32.NewProc("GlobalMemoryStatusEx")
|
||||
procDiskF = kernel32.NewProc("GetDiskFreeSpaceExW")
|
||||
)
|
||||
|
||||
type memStatusEx struct {
|
||||
Length uint32
|
||||
MemoryLoad uint32
|
||||
TotalPhys uint64
|
||||
AvailPhys uint64
|
||||
TotalPageFile uint64
|
||||
AvailPageFile uint64
|
||||
TotalVirtual uint64
|
||||
AvailVirtual uint64
|
||||
AvailExtendedVirtual uint64
|
||||
}
|
||||
|
||||
func hostHealth() healthInfo {
|
||||
h := healthInfo{
|
||||
HostUptimeSec: -1, Load1: -1, Load5: -1, Load15: -1,
|
||||
MemTotalMB: -1, MemFreeMB: -1, DiskTotalGB: -1, DiskFreeGB: -1, TempC: -1,
|
||||
}
|
||||
if r, _, _ := procTick.Call(); r != 0 {
|
||||
h.HostUptimeSec = int64(r / 1000)
|
||||
}
|
||||
var m memStatusEx
|
||||
m.Length = uint32(unsafe.Sizeof(m))
|
||||
if r, _, _ := procMem.Call(uintptr(unsafe.Pointer(&m))); r != 0 {
|
||||
h.MemTotalMB = int64(m.TotalPhys >> 20)
|
||||
h.MemFreeMB = int64(m.AvailPhys >> 20)
|
||||
}
|
||||
if p, err := syscall.UTF16PtrFromString("C:\\"); err == nil {
|
||||
var free, total, totalFree uint64
|
||||
if r, _, _ := procDiskF.Call(
|
||||
uintptr(unsafe.Pointer(p)),
|
||||
uintptr(unsafe.Pointer(&free)),
|
||||
uintptr(unsafe.Pointer(&total)),
|
||||
uintptr(unsafe.Pointer(&totalFree)),
|
||||
); r != 0 {
|
||||
h.DiskTotalGB = float64(total) / (1 << 30)
|
||||
h.DiskFreeGB = float64(free) / (1 << 30)
|
||||
}
|
||||
}
|
||||
return h
|
||||
}
|
||||
@@ -0,0 +1,522 @@
|
||||
// agent-device:跑在盒子 / Windows / 任意 Linux 上,主动出站连 core 的 wss,接收并执行命令。
|
||||
//
|
||||
// agent-device -server wss://your-host.example/dev/ws -token <TOKEN> -root /var/agent/work
|
||||
// agent-device # 自动读同目录下的 config.json(token 不进 ps)
|
||||
//
|
||||
// 设计约束(对齐 files/print/agent):
|
||||
// - 只出站,服务器不需要任何入站端口
|
||||
// - 断线指数退避重连,半开连接靠读超时兜底
|
||||
// - 单机不依赖 Node / 不装运行时,一个二进制跑起来
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"agent-device/pkg/proto"
|
||||
"agent-device/pkg/wsnode"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
)
|
||||
|
||||
const version = "0.1.1"
|
||||
|
||||
var (
|
||||
maxOutBytes = int64(4 << 20) // 单条命令回传上限,超出截断并标记
|
||||
maxParallel = 4 // 同机并发执行数
|
||||
)
|
||||
|
||||
type proc struct {
|
||||
cmd *exec.Cmd
|
||||
mu sync.Mutex
|
||||
kln string // 终止原因:cancelled / timeout
|
||||
}
|
||||
|
||||
func (p *proc) kill(reason string) {
|
||||
p.mu.Lock()
|
||||
if p.kln == "" {
|
||||
p.kln = reason
|
||||
}
|
||||
p.mu.Unlock()
|
||||
killProc(p.cmd)
|
||||
}
|
||||
|
||||
func (p *proc) killed() string {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
return p.kln
|
||||
}
|
||||
|
||||
// fileConfig 是 -config 读的 JSON,字段全部可选:没写的落到命令行/默认值。
|
||||
// 之所以需要它:token 写在命令行会被 ps 看见。
|
||||
type fileConfig struct {
|
||||
Server string `json:"server"`
|
||||
Token string `json:"token"`
|
||||
Root string `json:"root"`
|
||||
Name string `json:"name"`
|
||||
Shell string `json:"shell"`
|
||||
Insecure *bool `json:"insecure"`
|
||||
}
|
||||
|
||||
var startedAt = time.Now()
|
||||
|
||||
type runner struct {
|
||||
conn *wsnode.Conn
|
||||
root string
|
||||
shell string
|
||||
sem chan struct{}
|
||||
|
||||
mu sync.Mutex
|
||||
procs map[string]*proc
|
||||
|
||||
files *fileMgr // 文件通道:core 拉取/推送文件时的在途写入
|
||||
}
|
||||
|
||||
func main() {
|
||||
server := flag.String("server", "", "core 的接入地址,如 wss://your-host.example/dev/ws")
|
||||
token := flag.String("token", "", "Bearer token(或环境变量 NODE_TOKEN,或写在 -config 里)")
|
||||
root := flag.String("root", "", "工作根目录(默认 ~/agent-work),命令的 cwd 必须落在它之内")
|
||||
shellName := flag.String("shell", "", "sh | cmd | powershell(默认按平台)")
|
||||
name := flag.String("name", "", "设备名(默认 hostname)")
|
||||
configPath := flag.String("config", "", "JSON 配置文件;不给时自动找可执行文件同目录的 config.json")
|
||||
insecure := flag.Bool("insecure", false, "跳过证书校验")
|
||||
fixClock := flag.Bool("fix-clock", false, "证书失败时用服务器时间校准本机(Linux 需 root)")
|
||||
flag.Parse()
|
||||
|
||||
// 优先级:命令行显式参数 > 环境变量(仅 token)> 配置文件 > 默认值。
|
||||
// 配置文件的意义是把 token 从命令行拿掉——ps aux 能看见所有参数。
|
||||
set := map[string]bool{}
|
||||
flag.Visit(func(f *flag.Flag) { set[f.Name] = true })
|
||||
// 配置文件默认位置:可执行文件同目录的 config.json。放二进制旁边就不用带参数,
|
||||
// token 也不会出现在 ps 里。命令行显式给的 -config 优先。
|
||||
effectiveCfg := *configPath
|
||||
if effectiveCfg == "" {
|
||||
if dir := exeDir(); dir != "" {
|
||||
p := filepath.Join(dir, "config.json")
|
||||
if _, err := os.Stat(p); err == nil {
|
||||
effectiveCfg = p
|
||||
}
|
||||
}
|
||||
}
|
||||
if effectiveCfg != "" {
|
||||
var fc fileConfig
|
||||
b, err := os.ReadFile(effectiveCfg)
|
||||
if err != nil {
|
||||
log.Fatalf("读配置失败:%v", err)
|
||||
}
|
||||
if err := json.Unmarshal(b, &fc); err != nil {
|
||||
log.Fatalf("配置 %s 不是合法 JSON:%v", effectiveCfg, err)
|
||||
}
|
||||
log.Printf("已加载配置 %s", effectiveCfg)
|
||||
if !set["server"] && fc.Server != "" {
|
||||
*server = fc.Server
|
||||
}
|
||||
if !set["token"] && fc.Token != "" {
|
||||
*token = fc.Token
|
||||
}
|
||||
if !set["root"] && fc.Root != "" {
|
||||
*root = fc.Root
|
||||
}
|
||||
if !set["name"] && fc.Name != "" {
|
||||
*name = fc.Name
|
||||
}
|
||||
if !set["shell"] && fc.Shell != "" {
|
||||
*shellName = fc.Shell
|
||||
}
|
||||
if !set["insecure"] && fc.Insecure != nil {
|
||||
*insecure = *fc.Insecure
|
||||
}
|
||||
}
|
||||
if *token == "" {
|
||||
*token = os.Getenv("NODE_TOKEN")
|
||||
}
|
||||
|
||||
if *server == "" {
|
||||
log.Fatal("缺少 -server(或配置文件里的 server)")
|
||||
}
|
||||
if *token == "" {
|
||||
log.Fatal("缺少 token:用 -token / NODE_TOKEN / 配置文件")
|
||||
}
|
||||
|
||||
r := &runner{
|
||||
root: *root,
|
||||
shell: *shellName,
|
||||
sem: make(chan struct{}, maxParallel),
|
||||
procs: map[string]*proc{},
|
||||
files: newFileMgr(),
|
||||
}
|
||||
if r.root == "" {
|
||||
home, _ := os.UserHomeDir()
|
||||
r.root = filepath.Join(home, "agent-work")
|
||||
}
|
||||
r.root = filepath.Clean(r.root)
|
||||
// 相对路径按可执行文件所在目录解析:双击、服务、命令行启动都得到同一个工作区,
|
||||
// 不受进程当前目录影响;同时不允许用 ".." 跑到 exe 目录之外。
|
||||
if !filepath.IsAbs(r.root) {
|
||||
dir := exeDir()
|
||||
if dir == "" {
|
||||
log.Fatalf("工作根 %q 是相对路径,但取不到可执行文件位置", r.root)
|
||||
}
|
||||
abs := filepath.Join(dir, r.root)
|
||||
rel, err := filepath.Rel(dir, abs)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
log.Fatalf("工作根 %q 超出可执行文件目录:%s", r.root, abs)
|
||||
}
|
||||
r.root = abs
|
||||
}
|
||||
if err := os.MkdirAll(r.root, 0o755); err != nil {
|
||||
log.Fatalf("工作根不可用:%v", err)
|
||||
}
|
||||
// 进程 cwd 失效时(快捷方式的「起始位置」指向已删目录、启动目录被改名等),
|
||||
// Windows 上 os/exec 给 nil Stdin 打开空设备 "NUL" 是个相对路径调用,会直接报
|
||||
// 「找不到文件」,任何命令都起不来(含下面的 shellInfo 探测)。工作根刚 MkdirAll 过,
|
||||
// 钉在这里;失败也不致命,后面 run() 还有一层不依赖 NUL 的兵底。
|
||||
if err := os.Chdir(r.root); err != nil {
|
||||
log.Printf("切换工作目录失败(不影响命令执行):%v", err)
|
||||
}
|
||||
if r.shell == "" {
|
||||
r.shell = defaultShell()
|
||||
}
|
||||
host, _ := os.Hostname()
|
||||
if *name != "" {
|
||||
host = *name
|
||||
}
|
||||
|
||||
log.Printf("agent-device %s 启动:root=%s shell=%s → %s", version, r.root, shellKind(r.shell), *server)
|
||||
shPath, shVer := shellInfo(r.shell)
|
||||
log.Printf("shell:%s(%s %s)", r.shell, shPath, shVer)
|
||||
cfg := wsnode.Config{URL: *server, Token: *token, Insecure: *insecure, FixClock: *fixClock}
|
||||
err := wsnode.Run(context.Background(), cfg,
|
||||
func(c *wsnode.Conn) error {
|
||||
r.conn = c
|
||||
if err := c.Send(proto.Msg{
|
||||
T: proto.TKindHello, Ver: version, Name: host,
|
||||
OS: runtime.GOOS, Arch: runtime.GOARCH,
|
||||
Shell: shellKind(r.shell), ShellPath: shPath, ShellVer: shVer, Root: r.root,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("已连上 hub(%s/%s shell=%s %s)", runtime.GOOS, runtime.GOARCH, shPath, shVer)
|
||||
return nil
|
||||
},
|
||||
func(c *wsnode.Conn, t string, raw []byte) {
|
||||
var m proto.Msg
|
||||
if json.Unmarshal(raw, &m) != nil {
|
||||
return
|
||||
}
|
||||
switch t {
|
||||
case proto.TKindExec:
|
||||
go r.run(m)
|
||||
case proto.TKindKill:
|
||||
if p := r.lookup(m.ID); p != nil {
|
||||
p.kill("cancelled")
|
||||
}
|
||||
case proto.TKindPing:
|
||||
_ = c.Send(proto.Msg{T: proto.TKindPong})
|
||||
case proto.TKindFile:
|
||||
// write 的建栅必须同步完成:core 紧接着就发 chunk,异步会先到后建、丢块
|
||||
r.fileStart(c, m)
|
||||
case proto.TKindFileChunk:
|
||||
r.fileChunk(c, m)
|
||||
case proto.TKindFileDone:
|
||||
r.fileEnd(c, m)
|
||||
case proto.TKindHealth:
|
||||
h := hostHealth()
|
||||
m := proto.Msg{
|
||||
T: proto.TKindHealth, ID: m.ID,
|
||||
UptimeSec: int64(time.Since(startedAt).Seconds()), HostUptimeSec: h.HostUptimeSec,
|
||||
Load1: h.Load1, Load5: h.Load5, Load15: h.Load15,
|
||||
MemTotalMB: h.MemTotalMB, MemFreeMB: h.MemFreeMB,
|
||||
DiskTotalGB: h.DiskTotalGB, DiskFreeGB: h.DiskFreeGB, TempC: h.TempC,
|
||||
OS: runtime.GOOS, Arch: runtime.GOARCH, Name: host,
|
||||
}
|
||||
_ = c.Send(m)
|
||||
}
|
||||
})
|
||||
log.Printf("退出:%v", err)
|
||||
}
|
||||
|
||||
func (r *runner) lookup(id string) *proc {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.procs[id]
|
||||
}
|
||||
|
||||
func (r *runner) send(m proto.Msg) { _ = r.conn.Send(m) }
|
||||
|
||||
func (r *runner) exit(id string, code int, killed, errMsg string, truncated bool, ms int64) {
|
||||
m := proto.Msg{T: proto.TKindExit, ID: id, Code: &code, Killed: killed, Err: errMsg, MS: ms, Truncated: truncated}
|
||||
r.send(m)
|
||||
}
|
||||
|
||||
// run 执行一条命令:并发闸门 → cwd 校验 → 起进程组 → 流式回传 → 退出码。
|
||||
func (r *runner) run(m proto.Msg) {
|
||||
select {
|
||||
case r.sem <- struct{}{}:
|
||||
case <-time.After(30 * time.Second):
|
||||
r.exit(m.ID, -1, "", "节点忙:排队超过 30 秒", false, 0)
|
||||
return
|
||||
}
|
||||
defer func() { <-r.sem }()
|
||||
|
||||
dir, err := r.resolveCwd(m.Cwd)
|
||||
if err != nil {
|
||||
r.exit(m.ID, -1, "", err.Error(), false, 0)
|
||||
return
|
||||
}
|
||||
tmo := time.Duration(m.TimeoutMS) * time.Millisecond
|
||||
if tmo <= 0 {
|
||||
tmo = 5 * time.Minute
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), tmo)
|
||||
defer cancel()
|
||||
|
||||
sh, shArgs := shellCmd(r.shell)
|
||||
cmd := newCmd(ctx, sh, append(shArgs, m.Cmd)...)
|
||||
cmd.Dir = dir
|
||||
if len(m.Env) > 0 {
|
||||
cmd.Env = append(os.Environ(), flatten(m.Env)...)
|
||||
}
|
||||
cmd.Cancel = func() error { killProc(cmd); return nil } // 超时/取消时整组杀,不只杀 sh
|
||||
setupProc(cmd)
|
||||
|
||||
outPipe, err1 := cmd.StdoutPipe()
|
||||
errPipe, err2 := cmd.StderrPipe()
|
||||
if err1 != nil || err2 != nil {
|
||||
r.exit(m.ID, 127, "", "取管道失败", false, 0)
|
||||
return
|
||||
}
|
||||
|
||||
p := &proc{cmd: cmd}
|
||||
r.mu.Lock()
|
||||
r.procs[m.ID] = p
|
||||
r.mu.Unlock()
|
||||
defer func() {
|
||||
r.mu.Lock()
|
||||
delete(r.procs, m.ID)
|
||||
r.mu.Unlock()
|
||||
}()
|
||||
|
||||
started := time.Now()
|
||||
if err := cmd.Start(); err != nil {
|
||||
r.exit(m.ID, 127, "", "启动失败:"+err.Error(), false, 0)
|
||||
return
|
||||
}
|
||||
|
||||
var sent atomic.Int64
|
||||
var truncated atomic.Bool
|
||||
var wg sync.WaitGroup
|
||||
pump := func(rd io.Reader, stream string) {
|
||||
defer wg.Done()
|
||||
buf := make([]byte, 32<<10)
|
||||
for {
|
||||
n, rerr := rd.Read(buf)
|
||||
if n > 0 {
|
||||
if sent.Load() >= maxOutBytes {
|
||||
truncated.Store(true)
|
||||
} else {
|
||||
chunk := make([]byte, n)
|
||||
copy(chunk, buf[:n])
|
||||
sent.Add(int64(n))
|
||||
r.send(proto.Msg{T: proto.TKindChunk, ID: m.ID, Out: stream, Stream: chunk})
|
||||
}
|
||||
}
|
||||
if rerr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
wg.Add(2)
|
||||
go pump(outPipe, "out")
|
||||
go pump(errPipe, "err")
|
||||
wg.Wait()
|
||||
werr := cmd.Wait()
|
||||
|
||||
code := 0
|
||||
if werr != nil {
|
||||
var ee *exec.ExitError
|
||||
if errors.As(werr, &ee) {
|
||||
code = ee.ExitCode()
|
||||
} else {
|
||||
code = 127
|
||||
}
|
||||
}
|
||||
killed := p.killed()
|
||||
if killed == "" && ctx.Err() == context.DeadlineExceeded {
|
||||
killed = "timeout"
|
||||
}
|
||||
if killed != "" && code == 0 {
|
||||
code = -1
|
||||
}
|
||||
r.exit(m.ID, code, killed, "", truncated.Load(), time.Since(started).Milliseconds())
|
||||
}
|
||||
|
||||
// resolveCwd:hub 已把服务器路径重写成节点路径,这里只保证落在 root 之内并自动建目录。
|
||||
func (r *runner) resolveCwd(raw string) (string, error) {
|
||||
if raw == "" {
|
||||
return r.root, nil
|
||||
}
|
||||
if !filepath.IsAbs(raw) {
|
||||
return "", errors.New("cwd 必须是绝对路径:" + raw)
|
||||
}
|
||||
cwd := filepath.Clean(raw)
|
||||
rel, err := filepath.Rel(r.root, cwd)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
return "", errors.New("cwd 超出工作根:" + cwd)
|
||||
}
|
||||
if err := os.MkdirAll(cwd, 0o755); err != nil {
|
||||
return "", errors.New("建目录失败:" + err.Error())
|
||||
}
|
||||
return cwd, nil
|
||||
}
|
||||
|
||||
func flatten(env map[string]string) []string {
|
||||
out := make([]string, 0, len(env))
|
||||
for k, v := range env {
|
||||
out = append(out, k+"="+v)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// exeDir 返回可执行文件所在目录(解开软链接)。相对的工作根、默认配置都基于它。
|
||||
func exeDir() string {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if real, err := filepath.EvalSymlinks(exe); err == nil {
|
||||
exe = real
|
||||
}
|
||||
return filepath.Dir(exe)
|
||||
}
|
||||
|
||||
// defaultShell 选默认解释器。POSIX 上固定 /bin/sh(不读 $SHELL:用户把 shell 设成 fish
|
||||
// 之后模型写的 POSIX 语法全废);Windows 默认 cmd,想用 git-bash / busybox 就在
|
||||
// config.json 里写 "shell": "C:\\Program Files\\Git\\bin\\bash.exe"(或 PATH 里的名字)。
|
||||
func defaultShell() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return "cmd"
|
||||
}
|
||||
return "sh"
|
||||
}
|
||||
|
||||
// shellKind 把路径/命令名归成一个短标签给 core 看(hello 的 shell 字段)。
|
||||
func shellKind(p string) string {
|
||||
b := strings.ToLower(filepath.Base(p))
|
||||
switch {
|
||||
case strings.Contains(b, "busybox"):
|
||||
return "busybox"
|
||||
case strings.Contains(b, "bash"):
|
||||
return "bash"
|
||||
case b == "sh" || b == "sh.exe" || b == "/bin/sh":
|
||||
return "sh"
|
||||
case strings.Contains(b, "powershell"), b == "pwsh", b == "pwsh.exe":
|
||||
return "powershell"
|
||||
case b == "cmd" || b == "cmd.exe":
|
||||
return "cmd"
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// newCmd 统一构造要执行的命令:Stdin 显式给一个空 reader。
|
||||
// 不给(nil)时 os/exec 会自己 os.Open(os.DevNull),Windows 上那是相对路径的设备名
|
||||
// "NUL"——有的机器上 CreateFile 直接报 ERROR_FILE_NOT_FOUND,命令还没到 shell 就死了
|
||||
//(实测 pc-gd:所有 exec 都 exit 127 "open NUL: The system cannot find the file specified")。
|
||||
// 空 reader 与 NUL 语义一致(子进程读到立即 EOF),且不依赖进程 cwd。
|
||||
func newCmd(ctx context.Context, name string, args ...string) *exec.Cmd {
|
||||
c := exec.CommandContext(ctx, name, args...)
|
||||
c.Stdin = strings.NewReader("")
|
||||
return c
|
||||
}
|
||||
|
||||
func shellCmd(name string) (string, []string) {
|
||||
switch name {
|
||||
case "cmd":
|
||||
return "cmd", []string{"/c"}
|
||||
case "powershell", "pwsh":
|
||||
return name, []string{"-NoProfile", "-NonInteractive", "-Command"}
|
||||
}
|
||||
// busybox 的子命令形式:busybox sh -c "…"
|
||||
if strings.Contains(strings.ToLower(filepath.Base(name)), "busybox") {
|
||||
return name, []string{"sh", "-c"}
|
||||
}
|
||||
// 带路径的(git-bash 绝对路径、/bin/sh)直接当 POSIX shell 用
|
||||
if strings.ContainsAny(name, `\/`) {
|
||||
return name, []string{"-c"}
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
return "cmd", []string{"/c"}
|
||||
}
|
||||
if _, err := os.Stat("/bin/sh"); err == nil {
|
||||
return "/bin/sh", []string{"-c"}
|
||||
}
|
||||
return "sh", []string{"-c"}
|
||||
}
|
||||
|
||||
// toUTF8 把 Windows 中文控制台吐出的 GBK 字节转成 UTF-8。
|
||||
// 先过 utf8.Valid:纯 ASCII / 真 UTF-8(英文版 Windows、开了 UTF-8 的系统)一律原样返回,
|
||||
// 不会被误转;只有不是合法 UTF-8 时才按 GBK 解,解不出来就保留原样。
|
||||
func toUTF8(b []byte) []byte {
|
||||
if utf8.Valid(b) {
|
||||
return b
|
||||
}
|
||||
if d, err := simplifiedchinese.GBK.NewDecoder().Bytes(b); err == nil {
|
||||
return d
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// shellInfo 探测实际解释器与版本。这是模型写命令的唯一入口,所以路径要解开符号链接
|
||||
// (/bin/sh 可能是 dash / bash / busybox),版本拿不到就留空(dash、busybox 不支持 --version)。
|
||||
func shellInfo(name string) (string, string) {
|
||||
bin, _ := shellCmd(name)
|
||||
p, err := exec.LookPath(bin)
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
if real, err := filepath.EvalSymlinks(p); err == nil {
|
||||
p = real
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 800*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
var c *exec.Cmd
|
||||
switch name {
|
||||
case "cmd":
|
||||
c = newCmd(ctx, "cmd", "/c", "ver")
|
||||
case "powershell", "pwsh":
|
||||
c = newCmd(ctx, bin, "-NoProfile", "-NonInteractive", "-Command",
|
||||
"$PSVersionTable.PSVersion.ToString()")
|
||||
default:
|
||||
c = newCmd(ctx, p, "--version")
|
||||
}
|
||||
out, _ := c.Output()
|
||||
out = toUTF8(out) // 中文 Windows 的 ver 是 GBK;本来就是 UTF-8 的原样返回
|
||||
|
||||
ver := ""
|
||||
if s := strings.TrimSpace(string(out)); s != "" {
|
||||
if i := strings.IndexByte(s, '\n'); i > 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
s = strings.TrimSpace(s)
|
||||
if len(s) > 60 {
|
||||
s = s[:60] + "…"
|
||||
}
|
||||
ver = s
|
||||
}
|
||||
return p, ver
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//go:build !windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// setupProc 让命令自成一个进程组,后面才能整组杀(sh 拉起的子进程一起收)。
|
||||
func setupProc(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
}
|
||||
|
||||
// killProc 负号 = 整个进程组。忽略错误:进程可能已自行退出。
|
||||
func killProc(cmd *exec.Cmd) {
|
||||
if cmd == nil || cmd.Process == nil {
|
||||
return
|
||||
}
|
||||
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
const createNewProcessGroup = 0x00000200
|
||||
|
||||
// setupProc 让命令自成一组,taskkill /T 才能连子进程一起收。
|
||||
func setupProc(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: createNewProcessGroup}
|
||||
}
|
||||
|
||||
func killProc(cmd *exec.Cmd) {
|
||||
if cmd == nil || cmd.Process == nil {
|
||||
return
|
||||
}
|
||||
_ = newCmd(context.Background(), "taskkill", "/T", "/F", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()
|
||||
_ = cmd.Process.Kill()
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
module agent-device
|
||||
|
||||
go 1.22
|
||||
|
||||
require github.com/gorilla/websocket v1.5.3
|
||||
|
||||
require golang.org/x/text v0.21.0
|
||||
@@ -0,0 +1,4 @@
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
@@ -0,0 +1,68 @@
|
||||
// Package proto 定义 hub 与节点之间的消息。一条连接上全是 JSON 文本帧,
|
||||
// 用 t 字段区分类型;二进制数据(命令输出)走 chunk 字段,encoding/json 自动 base64。
|
||||
package proto
|
||||
|
||||
// 消息类型
|
||||
const (
|
||||
TKindHello = "hello" // 节点 → hub:上线自报家门
|
||||
TKindExec = "exec" // hub → 节点:执行一条命令
|
||||
TKindKill = "kill" // hub → 节点:终止某条命令
|
||||
TKindChunk = "chunk" // 节点 → hub:输出片段
|
||||
TKindExit = "exit" // 节点 → hub:命令结束
|
||||
TKindPing = "ping" // hub → 节点:保活
|
||||
TKindPong = "pong" // 节点 → hub:保活回应
|
||||
TKindHealth = "health" // 双向:hub 要指标 / 节点回指标
|
||||
TKindFile = "file" // 双向:core 发起一次 read/write
|
||||
TKindFileChunk = "file.chunk" // 双向:文件数据块(base64)
|
||||
TKindFileDone = "file.done" // 双向:结束,带 size / sha256 / err
|
||||
)
|
||||
|
||||
// Msg 是双向通用信封:不同 t 只用其中一部分字段,其余 omitempty。
|
||||
type Msg struct {
|
||||
T string `json:"t"`
|
||||
|
||||
// hello
|
||||
Ver string `json:"ver,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
OS string `json:"os,omitempty"`
|
||||
Arch string `json:"arch,omitempty"`
|
||||
Shell string `json:"shell,omitempty"` // 逻辑名:sh / cmd / powershell
|
||||
ShellPath string `json:"shell_path,omitempty"` // 实际解释器路径(符号链接已解开)
|
||||
ShellVer string `json:"shell_ver,omitempty"` // 版本首行(拿不到就留空)
|
||||
Root string `json:"root,omitempty"`
|
||||
|
||||
// exec / kill
|
||||
ID string `json:"id,omitempty"`
|
||||
Cmd string `json:"cmd,omitempty"`
|
||||
Cwd string `json:"cwd,omitempty"`
|
||||
TimeoutMS int `json:"timeout_ms,omitempty"`
|
||||
Env map[string]string `json:"env,omitempty"`
|
||||
|
||||
// chunk / exit
|
||||
Stream []byte `json:"chunk,omitempty"` // 原始字节,JSON 里是 base64
|
||||
Out string `json:"out,omitempty"` // "out" | "err"
|
||||
Code *int `json:"code,omitempty"`
|
||||
Killed string `json:"killed_by,omitempty"` // cancelled | timeout
|
||||
MS int64 `json:"ms,omitempty"`
|
||||
Err string `json:"err,omitempty"`
|
||||
Truncated bool `json:"truncated,omitempty"`
|
||||
|
||||
// health(hub 请求 / 节点回报共用)
|
||||
UptimeSec int64 `json:"uptime_sec,omitempty"` // 本进程已运行秒数
|
||||
HostUptimeSec int64 `json:"host_uptime_sec,omitempty"` // 主机开机秒数(不支持为 -1)
|
||||
Load1 float64 `json:"load1,omitempty"`
|
||||
Load5 float64 `json:"load5,omitempty"`
|
||||
Load15 float64 `json:"load15,omitempty"`
|
||||
MemTotalMB int64 `json:"mem_total_mb,omitempty"`
|
||||
MemFreeMB int64 `json:"mem_free_mb,omitempty"`
|
||||
DiskTotalGB float64 `json:"disk_total_gb,omitempty"`
|
||||
DiskFreeGB float64 `json:"disk_free_gb,omitempty"`
|
||||
TempC float64 `json:"temp_c,omitempty"`
|
||||
|
||||
// file(read/write 共用一个 信封)
|
||||
Op string `json:"op,omitempty"` // read | write
|
||||
Path string `json:"path,omitempty"` // 设备侧绝对路径,必须在 root 内
|
||||
Size int64 `json:"size,omitempty"` // 文件字节数
|
||||
SHA string `json:"sha256,omitempty"` // 文件内容的 sha256(传完对账)
|
||||
Data []byte `json:"data,omitempty"` // 数据块(JSON 里是 base64)
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
// Package wsnode 是「节点主动出站 + 断线重连 + 心跳保活」的最小 WebSocket 基座。
|
||||
// 语义与 files/print/agent 一致:节点在 NAT 后面也能被驱动,服务器不需要任何入站端口。
|
||||
package wsnode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// ReadTimeout 多久没收到服务器任何消息(含 ping)就认定连接已死。
|
||||
// hub 每 20s ping 一次,正常情况下永远触发不到;半开连接会在这里超时并触发重连。
|
||||
const ReadTimeout = 90 * time.Second
|
||||
|
||||
type Config struct {
|
||||
URL string // wss://host/ws
|
||||
Token string // Authorization: Bearer <token>
|
||||
Insecure bool // 跳过证书校验(自签环境)
|
||||
CAFile string // 额外信任的 CA
|
||||
FixClock bool // 证书校验失败时用服务器 Date 头校时(盒子无 RTC 时有用)
|
||||
}
|
||||
|
||||
type Conn struct {
|
||||
ws *websocket.Conn
|
||||
mu chan struct{} // 容量 1 的信号量:gorilla 不允许并发写
|
||||
cfg Config
|
||||
}
|
||||
|
||||
func (c *Conn) Send(v any) error {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.mu <- struct{}{}
|
||||
defer func() { <-c.mu }()
|
||||
return c.ws.WriteMessage(websocket.TextMessage, b)
|
||||
}
|
||||
|
||||
func (c *Conn) Close() { _ = c.ws.Close() }
|
||||
|
||||
// Wrap 把已升级的底层连接包成 Conn(hub 服务端用;客户端走 Run/Dial)。
|
||||
func Wrap(ws *websocket.Conn) *Conn {
|
||||
ws.SetReadLimit(4 << 20)
|
||||
return &Conn{ws: ws, mu: make(chan struct{}, 1)}
|
||||
}
|
||||
|
||||
// Run 持续维持连接:拨号 → onOpen → 读循环 → 断开 → 退避重连,直到 ctx 结束。
|
||||
func Run(ctx context.Context, cfg Config, onOpen func(*Conn) error, onMsg func(*Conn, string, []byte)) error {
|
||||
backoff := time.Second
|
||||
for {
|
||||
start := time.Now()
|
||||
err := once(ctx, cfg, onOpen, onMsg)
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
// 稳定连过 60s 才把退避重置,避免「连上就断」时疯狂重试
|
||||
if time.Since(start) > 60*time.Second {
|
||||
backoff = time.Second
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("连接中断:%v;%s 后重连", err, backoff)
|
||||
}
|
||||
select {
|
||||
case <-time.After(backoff):
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
if backoff < 30*time.Second {
|
||||
backoff *= 2
|
||||
if backoff > 30*time.Second {
|
||||
backoff = 30 * time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func once(ctx context.Context, cfg Config, onOpen func(*Conn) error, onMsg func(*Conn, string, []byte)) error {
|
||||
d := websocket.Dialer{
|
||||
HandshakeTimeout: 15 * time.Second,
|
||||
TLSClientConfig: tlsConf(cfg),
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
NetDial: (&net.Dialer{
|
||||
Timeout: 15 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).Dial,
|
||||
}
|
||||
hdr := http.Header{}
|
||||
if cfg.Token != "" {
|
||||
hdr.Set("Authorization", "Bearer "+cfg.Token)
|
||||
}
|
||||
ws, resp, err := d.DialContext(ctx, cfg.URL, hdr)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
return fmt.Errorf("%s 拒绝连接:%s", cfg.URL, resp.Status)
|
||||
}
|
||||
if cfg.FixClock && !cfg.Insecure && isCertErr(err) {
|
||||
log.Printf("证书校验失败,可能是系统时间不对(%v),尝试用服务器时间校准", err)
|
||||
syncClock(cfg.URL)
|
||||
}
|
||||
return err
|
||||
}
|
||||
defer ws.Close()
|
||||
ws.SetReadLimit(4 << 20)
|
||||
|
||||
c := &Conn{ws: ws, mu: make(chan struct{}, 1), cfg: cfg}
|
||||
if err := onOpen(c); err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
_ = ws.SetReadDeadline(time.Now().Add(ReadTimeout))
|
||||
_, data, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var head struct {
|
||||
T string `json:"t"`
|
||||
}
|
||||
if json.Unmarshal(data, &head) != nil {
|
||||
continue
|
||||
}
|
||||
onMsg(c, head.T, data)
|
||||
}
|
||||
}
|
||||
|
||||
func isCertErr(err error) bool {
|
||||
for _, k := range []string{"certificate", "x509", "tls:"} {
|
||||
if contains(err.Error(), k) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func contains(s, sub string) bool {
|
||||
for i := 0; i+len(sub) <= len(s); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func tlsConf(cfg Config) *tls.Config {
|
||||
t := &tls.Config{InsecureSkipVerify: cfg.Insecure}
|
||||
if cfg.CAFile != "" {
|
||||
if pem, err := os.ReadFile(cfg.CAFile); err == nil {
|
||||
pool := x509.NewCertPool()
|
||||
if pool.AppendCertsFromPEM(pem) {
|
||||
t.RootCAs = pool
|
||||
}
|
||||
} else {
|
||||
log.Printf("读 CA 失败:%v(忽略)", err)
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// syncClock 用服务器 Date 头把本机时间拨回来(只对 Linux 的 date 生效,Windows 一般有 RTC 不需要)。
|
||||
// 证书本身可能是「因为时间错才失败」,所以这里刻意不校验证书。
|
||||
func syncClock(rawURL string) {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
scheme := "http"
|
||||
if u.Scheme == "wss" || u.Scheme == "https" {
|
||||
scheme = "https"
|
||||
}
|
||||
req, err := http.NewRequest("HEAD", scheme+"://"+u.Host+"/", nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Printf("校时请求失败:%v", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
t, err := http.ParseTime(resp.Header.Get("Date"))
|
||||
if err != nil {
|
||||
log.Printf("服务器 Date 头不可解析:%v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("本地时间偏差约 %s(服务器 %s)", time.Since(t).Truncate(time.Second), t.Format(time.RFC3339))
|
||||
if runtime.GOOS == "windows" {
|
||||
return
|
||||
}
|
||||
out, err := exec.Command("date", "-u", "-s", t.UTC().Format("2006-01-02 15:04:05")).CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("设置系统时间失败(需要 root):%v %s", err, out)
|
||||
return
|
||||
}
|
||||
log.Printf("已校时到 %s", t.UTC().Format(time.RFC3339))
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#!/bin/sh
|
||||
# 交叉编译设备端二进制到 ./bin(纯本地动作:不联网、不推送、不依赖任何特定服务器)。
|
||||
#
|
||||
# sh scripts/build.sh # 编当前平台
|
||||
# sh scripts/build.sh all # 编 linux-arm64 / linux-amd64 / linux-mips / windows-amd64 / darwin-amd64 / darwin-arm64
|
||||
#
|
||||
# 有本机 Go 就用本机;没有但有 docker 就用 golang 镜像;都没有就报错。
|
||||
set -e
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
TARGETS="$(uname -s | tr 'A-Z' 'a-z')/$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')"
|
||||
[ "${1:-}" = "all" ] && TARGETS="linux/arm64 linux/amd64 linux/mips windows/amd64 darwin/amd64 darwin/arm64"
|
||||
|
||||
build_with_go() {
|
||||
mkdir -p bin
|
||||
for t in $TARGETS; do
|
||||
os=${t%/*}; arch=${t#*/}; ext=""; gomips=""
|
||||
[ "$os" = windows ] && ext=.exe
|
||||
# mips 设备(OpenWrt / 老路由)基本都没有 FPU,硬浮点二进制一跑就是非法指令
|
||||
[ "$arch" = mips ] && gomips=softfloat
|
||||
echo "build $os/$arch"
|
||||
GOOS=$os GOARCH=$arch GOMIPS=$gomips CGO_ENABLED=0 go build -trimpath -ldflags "-s -w" \
|
||||
-o "bin/agent-device-$os-$arch$ext" ./cmd/agent
|
||||
done
|
||||
}
|
||||
|
||||
if command -v go >/dev/null 2>&1; then
|
||||
build_with_go
|
||||
elif command -v docker >/dev/null 2>&1; then
|
||||
echo "本机没有 Go,改用 golang:1.23-alpine 容器编译"
|
||||
docker run --rm -v "$PWD":/w -w /w \
|
||||
-e CGO_ENABLED=0 -e TARGETS="$TARGETS" \
|
||||
golang:1.23-alpine sh -c '
|
||||
mkdir -p bin
|
||||
for t in $TARGETS; do
|
||||
os=${t%/*}; arch=${t#*/}; ext=""; gomips=""
|
||||
[ "$os" = windows ] && ext=.exe
|
||||
[ "$arch" = mips ] && gomips=softfloat
|
||||
echo "build $os/$arch"
|
||||
GOOS=$os GOARCH=$arch GOMIPS=$gomips go build -trimpath -ldflags "-s -w" \
|
||||
-o "bin/agent-device-$os-$arch$ext" ./cmd/agent
|
||||
done'
|
||||
else
|
||||
echo "需要 Go 或 docker 才能编译" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ls -lh bin
|
||||
@@ -3,6 +3,12 @@ services:
|
||||
build: ./app
|
||||
container_name: agent-core
|
||||
restart: unless-stopped
|
||||
# 日志轮转:默认 json-file 无上限,长期跑会把磁盘吃穿。保留最近 100MB。
|
||||
logging:
|
||||
driver: json-file
|
||||
options:
|
||||
max-size: "20m"
|
||||
max-file: "5"
|
||||
ports:
|
||||
# 直接把容器 8090 映射到宿主机端口,用 http://<服务器IP>:<AGENT_PORT> 访问。
|
||||
- "${AGENT_PORT:-8090}:8090"
|
||||
@@ -28,6 +34,9 @@ services:
|
||||
# 对外地址:public_link 的默认值(scripts/init.sh 探测公网 IP 后写进 .env)。
|
||||
# 设置页里填过 public_link.base_url 时以设置页为准。
|
||||
- PUBLIC_BASE_URL=${PUBLIC_BASE_URL:-}
|
||||
# 设备接入(可选):设备主动连 wss://<公网前缀>/dev/ws 时的 Bearer token。
|
||||
# 与 PUBLIC_BASE_URL 同时具备才会挂端点;留空 = 不启用,本地部署就是这种。
|
||||
- DEVICE_TOKEN=${DEVICE_TOKEN:-}
|
||||
networks:
|
||||
- agent-mcp-net
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ cd agent
|
||||
| Office 文档(docx/xlsx/pptx) | 首次用到时下载一次 officecli 二进制(约 34MB) | `office` 报 `binary missing` 并给出该技能 `AGENTS.md` 的路径,按它下载即可(不需 root、不需重启);预览面板里直接显示同一份安装说明 |
|
||||
| 网页搜索 | 无 | 装了 Node 就有(内置免费后端) |
|
||||
| 图片 / 视频生成 | 设置页给对应 MCP 填上游 key | 调用时报明确的缺参错误 |
|
||||
| 反向隧道 | `tunnel/client.yml`(服务器地址 + token) | `start.sh` 跳过,不占端口;配置说明见 `tunnel/README.md` |
|
||||
|
||||
装完 `ffmpeg` 之后**重跑一次 `./local/start.sh`** 即可生效。`python3` 稍特殊:技能清单本来就在,
|
||||
只要新装的解释器落在 core 启动时 `PATH` 已包含的目录里(如 brew 的 `/opt/homebrew/bin`)就无需重启;
|
||||
@@ -144,6 +145,9 @@ git pull
|
||||
|
||||
另外,`core` 只监听 `127.0.0.1`,同一局域网的人访问不到。
|
||||
|
||||
配了隧道后,core 会经隧道 client 暴露到公网域名。访问控制靠隧道 token + 上游 nginx 的 TLS;
|
||||
入口仍是 `127.0.0.1:8090`,不额外监听端口。
|
||||
|
||||
## 已知限制
|
||||
|
||||
- **没 ffmpeg 时 core 日志会有一条 `asr 工具发现失败`**:此时 `mcp_asr` 不启动(它转码要靠
|
||||
|
||||
+10
-8
@@ -6,7 +6,7 @@
|
||||
|---|---|---|---|---|
|
||||
| `web_search/` | `mcp_web_search` | 网页搜索(默认免费 Parallel / Exa,可换成自建 `/search` 上游) | `3000` | `POST /` |
|
||||
| `image_task/` | `mcp_image_task` | 图片生成 / 编辑(文生图走 `/images/generations`,图生图走 `/images/edits`,单工具 op=create/get) | `3000` | `POST /mcp` |
|
||||
| `video_task/` | `mcp_video_task` | 智谱 CogVideo 视频生成(异步,单工具 op=create/get) | `3000` | `POST /mcp` |
|
||||
| `video_task/` | `mcp_video_task` | 视频生成(任意 OpenAI 兼容 `/videos` 上游,异步,单工具 op=create/get) | `3000` | `POST /mcp` |
|
||||
| `asr/` | `mcp_asr` | 语音转文字(上游 OpenAI 兼容 `/audio/transcriptions`),**额外带一个给输入框用的 `POST /transcribe`** | `3000` | `POST /mcp` |
|
||||
|
||||
**这些服务一个宿主机端口都不占**——它们只挂在 `agent-mcp-net` 上,由 `agent-core` / `agent-executor`
|
||||
@@ -50,7 +50,9 @@ mcp_servers:
|
||||
> 从旧的「agent + mcp 双仓库」部署升级:先在旧 mcp 仓库目录 `docker compose down`(只需一次,否则同名容器与网络会冲突),再 `up` 本栈。
|
||||
|
||||
`image_task` / `video_task` 的 token **不由服务端保存**——每个请求从 `Authorization` header 读取
|
||||
(agent 侧配在 `config.yml` 的 `mcp_servers[].headers`)。`video_task` 用智谱 token;
|
||||
(agent 侧配在 `config.yml` 的 `mcp_servers[].headers`)。`video_task` 走任意 OpenAI 兼容的 videos 上游:
|
||||
提交 `/videos/generations`、查询 `/videos/{task_id}`,地址与模型由 `X-Video-Task-Base-Url` /
|
||||
`X-Video-Task-Model` 指定(两者都必填);
|
||||
`image_task` 走任意 OpenAI 兼容的 images 上游:文生图 `/images/generations`、图生图 `/images/edits`,
|
||||
上游地址与模型分别由 `X-Image-Task-Base-Url` / `X-Image-Task-Model` 指定(两者都必填)。
|
||||
所以这两个服务不用配任何密钥。
|
||||
@@ -71,23 +73,23 @@ mcp_servers:
|
||||
本栈默认不给 MCP 服务映射宿主机端口(它们只在 `agent-mcp-net` 内网可达)。要从宿主机直连,
|
||||
自行在 `docker-compose.yml` 给对应服务加 `ports` 即可。
|
||||
|
||||
## 图片是怎么到智谱手上的(只有 video_task 关心)
|
||||
## 图片是怎么到上游的(只有 video_task 关心)
|
||||
|
||||
智谱服务器生成视频前要**自己来拉参考图**,它不带 cookie、不带 `Authorization`。这套链路里**没有任何人上传文件**:
|
||||
上游生成视频前要**自己来拉参考图**,它不带 cookie、不带 `Authorization`。这套链路里**没有任何人上传文件**:
|
||||
|
||||
```
|
||||
AI ──调用 agent 内置的「公开临时 URL」工具(直接传文件路径)──► 拿到一条短命公网 URL
|
||||
│
|
||||
▼
|
||||
AI 把拿到的 URL 当 image_url 交给 video_task:3000/mcp
|
||||
AI 把拿到的 URL 当 image_url 交给 video_task:3000/mcp(转发为上游的 image 字段)
|
||||
│
|
||||
▼
|
||||
智谱 ──匿名 GET──► agent 侧提供的公开临时 URL
|
||||
那个文件始终躺在 agent 的任务目录里,零拷贝
|
||||
上游 ──匿名 GET──► agent 侧提供的公开临时 URL
|
||||
那个文件始终躺在 agent 的任务目录里,零拷贝
|
||||
```
|
||||
|
||||
- 签发与托管全在 agent 侧(它已集成为一个 tool,AI 直接调用即可,不需要 curl 任何内网入口)。
|
||||
- 链接短命:到期或 agent 重启后失效;智谱只在提交任务那一刻拉一次图,完全够用。
|
||||
- 链接短命:到期或 agent 重启后失效;上游只在提交任务那一刻拉一次图,完全够用。
|
||||
- mcp 这三家一个端口都不露,唯一公网可达的东西只有 agent 自己。
|
||||
|
||||
## 端口与安全
|
||||
|
||||
+101
-228
@@ -1,13 +1,15 @@
|
||||
# MCP Video Task
|
||||
|
||||
基于 [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) 的视频生成服务,对接智谱 BigModel 的视频生成**异步**接口,支持**文生视频**和**图生视频**。只有一个工具 `video_task`,用 `op` 区分动作:`op=create` 提交任务、`op=get` 单次查询结果。Server **不内部轮询**——AI 拿到 `task_id` 后自己决定什么时候用 `op="get"` 复查。
|
||||
对接**任意 OpenAI 兼容的视频生成端点**:提交走 `POST {base}/videos/generations`,查询走
|
||||
`GET {base}/videos/{task_id}`。对外只有一个工具 `video_task`,用 `op` 区分动作:
|
||||
|
||||
- 协议:MCP **Streamable HTTP**(单端点 `/mcp`,支持 SSE 流)
|
||||
- 上游:[智谱 BigModel 视频生成(异步)](https://docs.bigmodel.cn/api-reference/模型-api/视频生成异步) + [查询异步结果](https://docs.bigmodel.cn/api-reference/模型-api/查询异步结果)
|
||||
- 默认模型:`cogvideox-flash`
|
||||
- 端口:容器内 `3000`(MCP `/mcp` + `/health`,**容器外不映射**,只在 `agent-mcp-net` 内被 agent 访问)
|
||||
- 鉴权:**无服务端密钥**,每次请求必须从 header 携带智谱 token
|
||||
- 本地文件需要公网 URL 时:不传字节,AI 直接调用 agent 内置的「公开临时 URL」工具拿到短命链接(见下节)
|
||||
- `op=create` 提交任务,立刻回 `task_id`
|
||||
- `op=get` 按 `task_id` 单次查询,**Server 不内部轮询**
|
||||
|
||||
- 协议:MCP **Streamable HTTP**(单端点 `/mcp`)
|
||||
- 端口:容器内 `3000`(`/mcp` + `/health`)
|
||||
- 鉴权:**服务端不存密钥**,每个请求从 header 读
|
||||
- 上游:**不预设任何厂商**,Base-Url / 模型 / 凭据全部由调用方带入
|
||||
|
||||
---
|
||||
|
||||
@@ -22,287 +24,158 @@ cd /root/workspace/tools/agent
|
||||
docker compose up -d --build mcp_video_task
|
||||
```
|
||||
|
||||
服务容器名 `mcp_video_task`,**默认不占宿主机端口**:由 agent 走 `agent-mcp-net` 按容器名访问
|
||||
(`http://mcp_video_task:3000/mcp`)。要从宿主机 curl 调试,自行给该服务加 `ports` 映射。
|
||||
容器名 `mcp_video_task`,不占宿主机端口,由 agent 走 `agent-mcp-net` 按容器名访问。
|
||||
|
||||
```bash
|
||||
# 容器内健康检查(无需占端口)
|
||||
# 容器内健康检查
|
||||
docker exec mcp_video_task node -e "fetch('http://localhost:3000/health').then(r=>r.text()).then(console.log)"
|
||||
# {"status":"ok","service":"video_task","sessions":0}
|
||||
```
|
||||
|
||||
> 下文 curl 示例里的 `localhost:8083` 均指“临时打开了调试端口”的情形。
|
||||
|
||||
### MCP 客户端配置
|
||||
|
||||
#### Claude Desktop / Cursor 等支持 `headers` 配置的客户端
|
||||
|
||||
```jsonc
|
||||
// Claude Desktop: ~/Library/Application Support/Claude/claude_desktop_config.json
|
||||
// Cursor: ~/.cursor/mcp.json
|
||||
{
|
||||
"mcpServers": {
|
||||
"video_task": {
|
||||
"url": "http://localhost:8083/mcp",
|
||||
"type": "http",
|
||||
"url": "http://mcp_video_task:3000/mcp",
|
||||
"headers": {
|
||||
"Authorization": "Bearer <your-zhipu-api-key>"
|
||||
"Authorization": "Bearer <上游 API Key>",
|
||||
"X-Video-Task-Base-Url": "https://<host>/v1",
|
||||
"X-Video-Task-Model": "<上游模型 id>"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> 上面的 `localhost:8083` 只是示例:本栈默认不给 MCP 服务映射宿主端口,要直连需自行加 `ports`。
|
||||
---
|
||||
|
||||
> **重要**:客户端必须在 **每一个 HTTP 请求**(`initialize` / `notifications/initialized` / `tools/call`)都带上 `Authorization` header。
|
||||
## 三个 header
|
||||
|
||||
| header | 必填 | 说明 |
|
||||
|---|---|---|
|
||||
| `Authorization` | 是 | `Bearer <token>`,原样转给上游 |
|
||||
| `X-Video-Task-Base-Url` | 是 | 完整 http(s) 地址,填到 `/v1` 即可;直接填到 `/videos/generations` 或 `/videos` 也会被剥回 base |
|
||||
| `X-Video-Task-Model` | 是 | 上游模型 id,原样透传;不猜缺省值 |
|
||||
|
||||
配置缺失或非法时,工具直接回 `isError` 并说明缺哪个 header,不会去打上游。
|
||||
|
||||
---
|
||||
|
||||
## 工具:video_task
|
||||
|
||||
单个工具,`op` 必填,只有两个取值:
|
||||
|
||||
| op | 作用 | 同步/异步 |
|
||||
|---|---|---|
|
||||
| `create` | 提交一个视频生成任务,立刻返回 `task_id` | 同步(仅提交) |
|
||||
| `get` | 用 `task_id` 拉一次当前结果 | 同步(单次查询,不轮询) |
|
||||
|
||||
### 参数(AI 可见)
|
||||
|
||||
| 名称 | 类型 | 必填 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `op` | `"create"` \| `"get"` | ✅ | 要执行的操作 |
|
||||
| `prompt` | string | op=create 时与 `image_url` 二选一 | 视频文本描述,最大 512 字符;与 `image_url` 同传时作为运动/动作描述 |
|
||||
| `image_url` | string (URL) | ❌ | 参考图 URL(**单张**,不支持数组 / 多张 / 首尾帧)。图片 ≤ 5MB,格式 png/jpeg/jpg。本地文件请先用 agent 内置的公开临时 URL 工具换成公网链接(上一节) |
|
||||
| `with_audio` | boolean | ❌ | 是否生成 AI 音效,默认 `false`(仅 op=create 使用) |
|
||||
| `task_id` | string | op=get 时必填 | `op=create` 返回的 `id` |
|
||||
|
||||
### 服务端 Header 覆盖项(AI 看不到)
|
||||
|
||||
| Header | 类型 | 默认 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `X-Video-Task-Model` / `X-Mcp-Model` / `X-Model` | string | `cogvideox-flash` | 覆盖模型 |
|
||||
| `X-Video-Task-Quality` | string | `speed` | `speed`(速度优先)或 `quality`(质量优先) |
|
||||
| `X-Video-Task-Fps` | int | `30` | `30` 或 `60` |
|
||||
| `X-Video-Task-Watermark` | bool | `true` | 是否添加 AI 水印;关闭需先在智谱后台签署免责声明 |
|
||||
|
||||
模型名串识别顺序(首个非空值生效):`X-Video-Task-Model` → `X-Mcp-Model` → `X-Model` → 默认。
|
||||
| 参数 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `op` | `create` \| `get` | 必填 |
|
||||
| `prompt` | string | 视频描述,`create` 时与 `image_url` 至少给一个 |
|
||||
| `image_url` | string | 参考图(图生视频),公开可访问的 png/jpeg URL |
|
||||
| `seconds` | int | 时长(秒),留空用上游默认 |
|
||||
| `aspect_ratio` | string | 画幅,如 `16:9` / `9:16` / `1:1` |
|
||||
| `resolution` | string | 分辨率,如 `768P` / `1080P` |
|
||||
| `negative_prompt` | string | 不希望出现的画面 |
|
||||
| `enable_audio` | bool | 是否生成音频,默认 false(旧名 `with_audio` 同样接受) |
|
||||
| `task_id` | string | `create` 返回的 id,`get` 时必填 |
|
||||
|
||||
### 返回(op=create)
|
||||
|
||||
立刻返回智谱提交结果:
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"content": [
|
||||
{ "type": "text", "text": "Model: cogvideox-flash\ntask_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\nrequest_id: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy\ntask_status: PROCESSING\n\nVideo generation is asynchronous. Call this tool again with op=\"get\" and task_id=\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\" to fetch the result." }
|
||||
]
|
||||
}
|
||||
```
|
||||
task_id: <id>
|
||||
model: <上游模型 id>
|
||||
status: queued
|
||||
|
||||
`task_status` 通常为 `PROCESSING`,极少数情况下可能直接 `SUCCESS`(极快完成的短任务)。
|
||||
Video generation is asynchronous. Call this tool again with op="get" and task_id="<id>" to fetch the result.
|
||||
```
|
||||
|
||||
### 返回(op=get)
|
||||
|
||||
- **`SUCCESS`**:单个 `text` 块,含 `Video URL`(智谱签名 URL,**24h 左右有效**,需及时转存),以及 `Cover` 封面图:
|
||||
```jsonc
|
||||
{
|
||||
"content": [
|
||||
{ "type": "text", "text": "task_id: ...\nmodel: cogvideox-flash\ntask_status: SUCCESS\nVideo URL: https://...\nCover: https://..." }
|
||||
]
|
||||
}
|
||||
```
|
||||
- **`PROCESSING`**:`isError: false`,正文给出当前状态,建议稍候再查。
|
||||
- **`FAIL`**:`isError: true`,正文含 `task_status` 和智谱返回的 `error` 详情。
|
||||
任务完成:
|
||||
|
||||
```
|
||||
task_id: <id>
|
||||
model: <上游模型 id>
|
||||
status: completed
|
||||
Video URL: https://...
|
||||
credit: 62.29
|
||||
note: <上游附带的提示,如直链有效期>
|
||||
```
|
||||
|
||||
未完成 / 失败:回 `status` 与 `progress`,失败时附上游 `error` 原文,并置 `isError=true`。
|
||||
|
||||
---
|
||||
|
||||
## 上游要求
|
||||
|
||||
只要满足这两条,任何实现都能接(自建网关、云厂商、本地服务):
|
||||
|
||||
1. `POST {base}/videos/generations` 返回 `task_id` 或 `id`
|
||||
2. `GET {base}/videos/{task_id}` 返回 `status`;完成时带 `videos[].url` 或 `data[].url`
|
||||
|
||||
`status` 按小写比较:`completed` / `succeeded` / `success` 视为完成,`failed` / `error` /
|
||||
`cancelled` 视为失败,其余按进行中处理。
|
||||
|
||||
---
|
||||
|
||||
## 本地文件 → 公网 URL(本服务不搬字节)
|
||||
|
||||
`image_url` 必须是一个**公网可达**的 URL(智谱服务器会自己去拉)。图片在本地盘上时,本服务不负责上传:
|
||||
agent 侧已集成为一个 **tool**,AI 直接传文件路径就能拿到一条公开临时 URL,拿来填 `image_url` 即可,
|
||||
不需要 curl 任何地址、不需要任何配置。
|
||||
|
||||
- 文件始终躺在 agent 的任务目录里,零拷贝;链接短命、到期或 agent 重启后失效。
|
||||
- 智谱只在提交任务那一刻拉一次图,短命链接完全够用。
|
||||
|
||||
---
|
||||
|
||||
## 鉴权细节
|
||||
|
||||
每个 HTTP 请求必须携带智谱 API Token,server 从以下 header 字段按顺序识别,**首个非空值生效**:
|
||||
|
||||
| 优先级 | Header | 说明 |
|
||||
|---|---|---|
|
||||
| 1 | `X-Video-Task-Token: <token>` | 直传,避免与客户端自身的 Authorization 冲突 |
|
||||
| 2 | `X-Mcp-Token: <token>` | 通用兜底 |
|
||||
| 3 | `Authorization: Bearer <token>` | 标准 OAuth 风格(排最后是为避免误用) |
|
||||
|
||||
Token 仅在每次 tool call 时从 header 读取,不写入任何存储、不进 session。
|
||||
|
||||
### 错误响应
|
||||
|
||||
| 场景 | HTTP 状态 | 表现 |
|
||||
|---|---|---|
|
||||
| initialize 时缺 token | `401` | JSON-RPC `error.message` 提示补充 header |
|
||||
| 智谱 API 4xx/5xx | `200`(MCP 工具返回) | `isError: true`,正文含智谱返回的状态码和 body |
|
||||
| 网络失败 | `200`(MCP 工具返回) | `isError: true`,正文含 `fetch` 错误信息 |
|
||||
| 上游挂住不回(超时 >30s) | `200`(MCP 工具返回) | `isError: true`,正文提示 timed out;`fetch` 带 `AbortSignal.timeout`,不会无限卡住工具调用 |
|
||||
|
||||
---
|
||||
|
||||
## 推荐用法(AI 视角)
|
||||
图生视频要的是**公网可访问的 URL**,不是字节流。本地文件这样走:
|
||||
|
||||
```
|
||||
1. 如果手头是【本地文件】:先调用 agent 内置的「公开临时 URL」工具拿到一条公网链接(上一节),再进第 2 步
|
||||
2. 调用 video_task({ op: "create", prompt, image_url?, with_audio? })
|
||||
3. 拿到 task_id → 等待若干秒(建议 2~5 秒)
|
||||
4. 调用 video_task({ op: "get", task_id })
|
||||
- 若 PROCESSING → 等一会再调
|
||||
- 若 SUCCESS → 拿到 video URL,贴给用户 / 下载 / 二次编辑
|
||||
- 若 FAIL → 展示错误信息,结束
|
||||
AI ──调用 agent 内置的「公开临时 URL」工具(直接传文件路径)──► 拿到一条短命公网 URL
|
||||
│
|
||||
▼
|
||||
AI 把 URL 当 image_url 交给 video_task:3000/mcp(转成上游的 image 字段)
|
||||
│
|
||||
▼
|
||||
上游 ──匿名 GET──► agent 侧提供的公开临时 URL(文件始终躺在任务目录里,零拷贝)
|
||||
```
|
||||
|
||||
> `image_url` 必须是公网可达 URL;本地文件走 agent 内置工具,本服务不参与上传。
|
||||
链接短命:到期或 agent 重启后失效;上游只在提交任务那一刻拉一次图,够用。
|
||||
|
||||
---
|
||||
|
||||
## 用 curl 手工调试
|
||||
|
||||
MCP Streamable HTTP 不是普通 REST,**必须按顺序**:`initialize` → `notifications/initialized` → `tools/call`。下面用一行版串起来:
|
||||
|
||||
```bash
|
||||
# 把 <your-zhipu-api-key> 替换成你智谱的 API Key
|
||||
TOKEN="<your-zhipu-api-key>"
|
||||
URL=http://127.0.0.1:3000/mcp # 容器内;宿主机调试需自行映射端口
|
||||
H=(-H 'Content-Type: application/json' -H 'Accept: application/json, text/event-stream'
|
||||
-H 'Authorization: Bearer <token>'
|
||||
-H 'X-Video-Task-Base-Url: https://<host>/v1'
|
||||
-H 'X-Video-Task-Model: <model>')
|
||||
|
||||
# 1. initialize 拿 sessionId
|
||||
SESSION=$(curl -s -D - -X POST http://localhost:8083/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "initialize",
|
||||
"params": {
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": {},
|
||||
"clientInfo": { "name": "curl", "version": "1.0" }
|
||||
}
|
||||
}' | grep -i '^mcp-session-id:' | awk '{print $2}' | tr -d '\r\n')
|
||||
# 1. initialize,从响应头取 mcp-session-id
|
||||
curl -s -D - "${H[@]}" -X POST $URL \
|
||||
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"c","version":"0"}}}'
|
||||
|
||||
echo "Session: $SESSION"
|
||||
# 2. 提交任务
|
||||
curl -s "${H[@]}" -H 'mcp-session-id: <sid>' -X POST $URL \
|
||||
-d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"video_task","arguments":{"op":"create","prompt":"一只猫在草地上跑"}}}'
|
||||
|
||||
# 2. notifications/initialized
|
||||
curl -s -X POST http://localhost:8083/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Mcp-Session-Id: $SESSION" \
|
||||
-d '{"jsonrpc": "2.0", "method": "notifications/initialized"}'
|
||||
|
||||
# 3. 列出工具(可选验证)
|
||||
curl -s -X POST http://localhost:8083/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Mcp-Session-Id: $SESSION" \
|
||||
-d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}'
|
||||
|
||||
# 4. 提交文生视频任务(op=create)
|
||||
curl -s -X POST http://localhost:8083/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Mcp-Session-Id: $SESSION" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 3,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": "video_task",
|
||||
"arguments": {
|
||||
"op": "create",
|
||||
"prompt": "A cat is playing with a ball.",
|
||||
"with_audio": true
|
||||
}
|
||||
}
|
||||
}'
|
||||
|
||||
# 5. 查结果(op=get,替换下面的 <task_id>)
|
||||
curl -s -X POST http://localhost:8083/mcp \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Accept: application/json, text/event-stream" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Mcp-Session-Id: $SESSION" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 4,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": "video_task",
|
||||
"arguments": { "op": "get", "task_id": "<task_id>" }
|
||||
}
|
||||
}'
|
||||
# 3. 查结果
|
||||
curl -s "${H[@]}" -H 'mcp-session-id: <sid>' -X POST $URL \
|
||||
-d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"video_task","arguments":{"op":"get","task_id":"<id>"}}}'
|
||||
```
|
||||
|
||||
响应是 SSE 格式(`event: message` + `data: {...}`),用 `grep '^data:'` 抽 JSON 即可。
|
||||
响应是 SSE(`event: message` + `data: {...}`),取 `data:` 那行解析。
|
||||
|
||||
---
|
||||
|
||||
## 架构与文件
|
||||
|
||||
```
|
||||
video_task/
|
||||
├── Dockerfile # node:22-slim,安装依赖后启动 MCP HTTP server
|
||||
├── .dockerignore # 排除 node_modules
|
||||
package.json # @modelcontextprotocol/sdk + express
|
||||
├── README.md # 本文件
|
||||
└── src/
|
||||
└── mcp-server.js # 单文件实现:HTTP server + MCP transport + 单工具(op 分发)
|
||||
mcp/video_task/
|
||||
├── Dockerfile
|
||||
├── package.json
|
||||
├── README.md
|
||||
└── src/mcp-server.js # 全部逻辑:header 解析 / 上游调用 / MCP 服务
|
||||
```
|
||||
|
||||
### 关键设计
|
||||
|
||||
- **单工具 + `op` 分发**:`video_task(op=create|get)` 替代原来的 `video_gen` + `query_video` 双工具,AI 面对的工具面更小,参数按 op 在 handler 侧校验
|
||||
- **每 session 一个 transport + server**:通过 `Mcp-Session-Id` 复用,避免每请求重建
|
||||
- **Token 闭包注入**:在 Express 入口处从 header 提取 token,闭包传给 tool 回调;不依赖 SDK 内部字段
|
||||
- **AI 可见 vs AI 不可见**:
|
||||
- AI 只看到 `op / prompt / image_url / with_audio / task_id`
|
||||
- `model / quality / fps / watermark_enabled` 全部由 HTTP header 注入,避免 AI 误传或被诱导调高价档
|
||||
- **异步语义暴露给 AI**:服务端不做轮询,`op=create` 只提交、`op=get` 只查一次。AI 自主决定节奏
|
||||
- **服务端自动 `request_id`**:每次提交服务端生成一个 UUID 作为智谱 `request_id`,便于日志关联,但不暴露给 AI
|
||||
- **不搬字节**:本地文件 -> 公网 URL 完全由 agent 侧集成的「公开临时 URL」工具完成,本服务不参与;所以本体不加任何写入口、不落盘、不需要额外端口
|
||||
- **链接短命**:公开临时 URL 到期或 agent 重启后失效;智谱只需在提交任务那一刻拉一次图
|
||||
|
||||
### 默认模型
|
||||
|
||||
```js
|
||||
// src/mcp-server.js
|
||||
const DEFAULT_MODEL = "cogvideox-flash";
|
||||
```
|
||||
|
||||
修改后 `docker compose up -d --build mcp_video_task` 即可生效。
|
||||
|
||||
---
|
||||
|
||||
## 常见问题
|
||||
|
||||
**Q: AI 拿到 task_id 后立刻 op=get 一直 PROCESSING,怎么办?**
|
||||
A: AI 自己 sleep 几秒再调(建议 2~5 秒间隔)。这是设计取舍:把节奏控制权交给 AI 模型,而不是服务端硬编码超时。
|
||||
|
||||
**Q: 视频 URL 多久过期?**
|
||||
A: 智谱签发 URL 默认 24h 左右。需要在收到后及时转存到自己的 OSS/S3。
|
||||
|
||||
**Q: 为什么 quality / fps 不让 AI 直接传?**
|
||||
A: 这些是技术档位,与成本/单价挂钩。让 AI 自主挑 `quality=quality + fps=60` 会爆账号预算。改成 header 由部署方注入更可控。
|
||||
|
||||
**Q: 报错 `Missing valid Mcp-Session-Id`?**
|
||||
A: 漏发了 `Mcp-Session-Id` header,或调 `tools/call` 前没先 `initialize`。
|
||||
|
||||
**Q: 同一 task_id 能并发查吗?**
|
||||
A: 可以。`/async-result/{id}` 是幂等的读接口,并发无副作用。
|
||||
|
||||
**Q: 换模型需要重连 MCP session 吗?**
|
||||
A: 不需要。Model header 在每次 `tools/call` 都从当前请求读取。
|
||||
|
||||
**Q: 上传的图片多久过期?**
|
||||
A: 本服务不存图,不存在“上传的图片”:要用的图在 agent 的任务目录里,AI 用 agent 内置的公开临时 URL 工具现取一条短命链接给智谱拉。链接过期或 agent 重启后该 URL 失效,但智谱只在提交任务时拉一次,不受影响。
|
||||
- **无状态**:不存密钥、不存任务,重启即忘。任务状态由上游持有,`task_id` 就是全部上下文。
|
||||
- **不轮询**:`op=get` 只拉一次。轮询节奏由 AI 决定,避免服务端挂着长任务占资源。
|
||||
- **配置错误不打上游**:缺 header 时同步返回 `isError`,省一次无意义的 400。
|
||||
- **`wait=false` 提交**:立刻拿 `task_id`,不让工具调用被上游排队拖死。
|
||||
|
||||
+166
-248
@@ -6,26 +6,16 @@ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/
|
||||
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
||||
import { z } from "zod";
|
||||
|
||||
const ZHIPU_SUBMIT_URL =
|
||||
"https://open.bigmodel.cn/api/paas/v4/videos/generations";
|
||||
const ZHIPU_QUERY_URL = (id) =>
|
||||
`https://open.bigmodel.cn/api/paas/v4/async-result/${encodeURIComponent(id)}`;
|
||||
// 上游必须是 OpenAI 兼容的视频端点:提交 POST /videos/generations,查询 GET /videos/{task_id}。
|
||||
// Base-Url / 模型 / 凭据全部由调用方随每个请求用 header 带入(X-Video-Task-Base-Url /
|
||||
// X-Video-Task-Model / Authorization),服务自己不存、也不预设任何厂商。
|
||||
|
||||
// 默认模型:cogvideox-flash
|
||||
const DEFAULT_MODEL = "cogvideox-flash";
|
||||
|
||||
// AI 不可见的服务端默认(可被同名 header 覆盖)
|
||||
const DEFAULT_QUALITY = "speed"; // speed | quality
|
||||
const DEFAULT_FPS = 30; // 30 | 60
|
||||
const DEFAULT_WATERMARK_ENABLED = true; // 平台政策默认 true
|
||||
|
||||
// submit/query 都是快接口,卡住基本等于上游挂了;限时避免工具调用被拖死
|
||||
const UPSTREAM_TIMEOUT_MS = 30_000;
|
||||
// 提交与查询都是快接口(wait=false 立刻回 task_id),卡住基本等于上游挂了。
|
||||
const UPSTREAM_TIMEOUT_MS = 60_000;
|
||||
|
||||
// ── 优雅退出 ──────────────────────────────────────────────────────────────
|
||||
// 容器里 node 以 PID 1 运行且未注册 SIGTERM 处理器时,内核不会对其执行默认
|
||||
// 终止动作,docker stop/restart 会白等满 10s 宽限期才被 SIGKILL。
|
||||
// 显式注册处理器,关闭 MCP session 与长连接后立即退出;3s 定时器为兜底。
|
||||
let httpServer = null;
|
||||
let closeAllSessions = () => {};
|
||||
let shuttingDown = false;
|
||||
@@ -43,11 +33,7 @@ process.on("SIGTERM", () => shutdown("SIGTERM"));
|
||||
process.on("SIGINT", () => shutdown("SIGINT"));
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {
|
||||
http: false,
|
||||
port: 3000,
|
||||
host: "0.0.0.0",
|
||||
};
|
||||
const args = { http: false, port: 3000, host: "0.0.0.0" };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--http") args.http = true;
|
||||
@@ -57,74 +43,84 @@ function parseArgs(argv) {
|
||||
return args;
|
||||
}
|
||||
|
||||
function pickToken(headers) {
|
||||
// 对外只有三个 header:Authorization / X-Video-Task-Base-Url / X-Video-Task-Model。
|
||||
function pickAuthToken(headers) {
|
||||
if (!headers) return null;
|
||||
const candidates = [
|
||||
headers["x-video-task-token"],
|
||||
headers["x-mcp-token"],
|
||||
headers["authorization"],
|
||||
];
|
||||
for (const v of candidates) {
|
||||
if (!v) continue;
|
||||
const s = String(v).trim();
|
||||
if (!s) continue;
|
||||
if (/^Bearer\s+/i.test(s)) {
|
||||
const t = s.replace(/^Bearer\s+/i, "").trim();
|
||||
if (t) return t;
|
||||
} else {
|
||||
return s;
|
||||
}
|
||||
const s = String(headers["authorization"] || "").trim();
|
||||
if (!s) return null;
|
||||
if (/^Bearer\s+/i.test(s)) {
|
||||
const t = s.replace(/^Bearer\s+/i, "").trim();
|
||||
return t || null;
|
||||
}
|
||||
return null;
|
||||
return s;
|
||||
}
|
||||
|
||||
function pickModel(headers) {
|
||||
function pickHeaderString(headers, name) {
|
||||
if (!headers) return null;
|
||||
const v =
|
||||
headers["x-video-task-model"] ||
|
||||
headers["x-mcp-model"] ||
|
||||
headers["x-model"];
|
||||
if (!v) return null;
|
||||
const v = headers[name];
|
||||
if (v === undefined || v === null) return null;
|
||||
const s = String(v).trim();
|
||||
return s || null;
|
||||
}
|
||||
|
||||
function pickHeaderString(headers, name, fallback) {
|
||||
if (!headers) return fallback;
|
||||
const v = headers[name];
|
||||
if (v === undefined || v === null) return fallback;
|
||||
const s = String(v).trim();
|
||||
return s ? s : fallback;
|
||||
function pickBaseUrl(headers) {
|
||||
return pickHeaderString(headers, "x-video-task-base-url");
|
||||
}
|
||||
|
||||
function pickHeaderInt(headers, name, fallback) {
|
||||
if (!headers) return fallback;
|
||||
const v = headers[name];
|
||||
if (v === undefined || v === null) return fallback;
|
||||
const n = parseInt(String(v).trim(), 10);
|
||||
return Number.isFinite(n) ? n : fallback;
|
||||
function pickModel(headers) {
|
||||
return pickHeaderString(headers, "x-video-task-model");
|
||||
}
|
||||
|
||||
function pickHeaderBool(headers, name, fallback) {
|
||||
if (!headers) return fallback;
|
||||
const v = headers[name];
|
||||
if (v === undefined || v === null) return fallback;
|
||||
const s = String(v).trim().toLowerCase();
|
||||
if (s === "true" || s === "1" || s === "yes") return true;
|
||||
if (s === "false" || s === "0" || s === "no") return false;
|
||||
return fallback;
|
||||
// 解析 Base-Url:必须是完整 http(s) URL。允许调用方直接填到 /videos/generations
|
||||
// 或 /videos(照抄文档的写法),统一剥回 base——否则查询会被拼成两条路径。
|
||||
function resolveBaseUrl(rawBase) {
|
||||
const base = String(rawBase || "").trim().replace(/\/+$/, "");
|
||||
if (!base) {
|
||||
throw new Error(
|
||||
"Missing X-Video-Task-Base-Url: pass the upstream base URL (e.g. https://<host>/v1)."
|
||||
);
|
||||
}
|
||||
if (!/^https?:\/\//i.test(base)) {
|
||||
throw new Error(
|
||||
`Invalid X-Video-Task-Base-Url "${base}": must be a full http(s) URL.`
|
||||
);
|
||||
}
|
||||
return base.replace(/\/videos(\/generations)?$/i, "");
|
||||
}
|
||||
|
||||
async function callZhipu({ url, token, method = "POST", body }) {
|
||||
// 模型只由 header 决定,AI 看不到也填不了。不猜缺省值:猜错只会换来含义不明的 400。
|
||||
function resolveModel(headers) {
|
||||
const model = pickModel(headers);
|
||||
if (!model) {
|
||||
throw new Error("Missing X-Video-Task-Model: pass the upstream model name.");
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
// 一次解析出本次请求的上游、凭据、模型。配置问题同步抛错,直接回给调用方。
|
||||
function resolveTarget(headers) {
|
||||
const token = pickAuthToken(headers);
|
||||
if (!token) {
|
||||
throw new Error('Missing Authorization: use "Authorization: Bearer <token>".');
|
||||
}
|
||||
return {
|
||||
base: resolveBaseUrl(pickBaseUrl(headers)),
|
||||
token,
|
||||
model: resolveModel(headers),
|
||||
};
|
||||
}
|
||||
|
||||
// 统一的上游请求:超时、错误体解析都在这层,提交与查询共用。
|
||||
async function callUpstream({ url, token, method = "POST", body }) {
|
||||
const init = {
|
||||
method,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
|
||||
};
|
||||
if (body !== undefined) init.body = JSON.stringify(body);
|
||||
if (body !== undefined) {
|
||||
init.headers["Content-Type"] = "application/json";
|
||||
init.body = JSON.stringify(body);
|
||||
}
|
||||
let resp;
|
||||
try {
|
||||
resp = await fetch(url, init);
|
||||
@@ -143,9 +139,7 @@ async function callZhipu({ url, token, method = "POST", body }) {
|
||||
data = { raw: text };
|
||||
}
|
||||
if (!resp.ok) {
|
||||
const err = new Error(
|
||||
`Zhipu API error: ${resp.status} ${resp.statusText} - ${text}`
|
||||
);
|
||||
const err = new Error(`Upstream error: ${resp.status} ${resp.statusText} - ${text}`);
|
||||
err.status = resp.status;
|
||||
err.detail = data;
|
||||
throw err;
|
||||
@@ -153,96 +147,74 @@ async function callZhipu({ url, token, method = "POST", body }) {
|
||||
return data;
|
||||
}
|
||||
|
||||
// ===== op=create:提交视频生成(同步只提交,立刻返回)=====
|
||||
async function submitVideo({
|
||||
token,
|
||||
model,
|
||||
prompt,
|
||||
image_url,
|
||||
quality,
|
||||
with_audio,
|
||||
fps,
|
||||
watermark_enabled,
|
||||
}) {
|
||||
// 平台要求 image_url 和 prompt 至少传一个(handler 侧已校验)
|
||||
const body = {
|
||||
model,
|
||||
quality,
|
||||
with_audio: Boolean(with_audio),
|
||||
watermark_enabled: Boolean(watermark_enabled),
|
||||
fps,
|
||||
// 由服务端生成唯一 request_id,便于日志关联;不暴露给 AI
|
||||
request_id: randomUUID(),
|
||||
};
|
||||
// ===== op=create:提交视频生成(wait=false,立刻返回 task_id)=====
|
||||
async function submitVideo({ base, token, model, prompt, image_url, seconds, aspect_ratio, resolution, negative_prompt, enable_audio }) {
|
||||
const body = { model, wait: false };
|
||||
if (prompt) body.prompt = prompt;
|
||||
if (image_url) body.image_url = image_url;
|
||||
if (image_url) body.image = image_url; // OpenAI Videos 口径的参考图字段
|
||||
if (seconds !== undefined) body.seconds = seconds;
|
||||
if (aspect_ratio) body.aspect_ratio = aspect_ratio;
|
||||
if (resolution) body.resolution = resolution;
|
||||
if (negative_prompt) body.negative_prompt = negative_prompt;
|
||||
if (enable_audio !== undefined) body.enable_audio = Boolean(enable_audio);
|
||||
|
||||
return await callZhipu({ url: ZHIPU_SUBMIT_URL, token, body });
|
||||
// { model, id, request_id, task_status }
|
||||
return await callUpstream({ url: base + "/videos/generations", token, body });
|
||||
}
|
||||
|
||||
// ===== op=get:查询异步结果(单次拉取,不内部轮询)=====
|
||||
async function queryAsyncResult({ token, id }) {
|
||||
return await callZhipu({
|
||||
url: ZHIPU_QUERY_URL(id),
|
||||
// ===== op=get:查询任务(单次拉取,不内部轮询)=====
|
||||
async function queryVideo({ base, token, taskId }) {
|
||||
return await callUpstream({
|
||||
url: base + "/videos/" + encodeURIComponent(taskId),
|
||||
token,
|
||||
method: "GET",
|
||||
});
|
||||
// { id, request_id, model, task_status, video_result?, ... }
|
||||
}
|
||||
|
||||
const DONE = new Set(["completed", "succeeded", "success"]);
|
||||
const FAILED = new Set(["failed", "error", "cancelled", "canceled"]);
|
||||
|
||||
function normalizeStatus(s) {
|
||||
return String(s || "").toLowerCase();
|
||||
}
|
||||
|
||||
// ---------- op=create 的业务处理 ----------
|
||||
async function handleCreate({ args, headers, token }) {
|
||||
async function handleCreate({ args, headers }) {
|
||||
// 纯空白 prompt 等同没给:schema 只校验类型/长度,不校验内容。
|
||||
// 不 trim 的话,“ ”会被当成合法 prompt 提交(text-to-video 拿到空白提示词,
|
||||
// image-to-video 则多传一个无意义字段)。
|
||||
const prompt = typeof args.prompt === "string" ? args.prompt.trim() : "";
|
||||
if (!prompt && !args.image_url) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: 'op=create requires at least one of "prompt" or "image_url".',
|
||||
},
|
||||
],
|
||||
content: [{ type: "text", text: 'op=create requires at least one of "prompt" or "image_url".' }],
|
||||
};
|
||||
}
|
||||
|
||||
// AI 不可见:全部走 header
|
||||
const model = pickModel(headers) || DEFAULT_MODEL;
|
||||
const quality = pickHeaderString(
|
||||
headers,
|
||||
"x-video-task-quality",
|
||||
DEFAULT_QUALITY
|
||||
);
|
||||
const fps = pickHeaderInt(headers, "x-video-task-fps", DEFAULT_FPS);
|
||||
const watermark_enabled = pickHeaderBool(
|
||||
headers,
|
||||
"x-video-task-watermark",
|
||||
DEFAULT_WATERMARK_ENABLED
|
||||
);
|
||||
let target;
|
||||
try {
|
||||
target = resolveTarget(headers);
|
||||
} catch (e) {
|
||||
return { isError: true, content: [{ type: "text", text: e.message }] };
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await submitVideo({
|
||||
token,
|
||||
model,
|
||||
...target,
|
||||
prompt,
|
||||
image_url: args.image_url,
|
||||
quality,
|
||||
with_audio: args.with_audio ?? false,
|
||||
fps,
|
||||
watermark_enabled,
|
||||
seconds: args.seconds,
|
||||
aspect_ratio: args.aspect_ratio,
|
||||
resolution: args.resolution,
|
||||
negative_prompt: args.negative_prompt,
|
||||
enable_audio: args.enable_audio ?? args.with_audio,
|
||||
});
|
||||
|
||||
const taskId = data.task_id || data.id;
|
||||
const summary =
|
||||
`Model: ${model}\n` +
|
||||
`task_id: ${data.id}\n` +
|
||||
`request_id: ${data.request_id}\n` +
|
||||
`task_status: ${data.task_status}\n` +
|
||||
`task_id: ${taskId}\n` +
|
||||
`model: ${target.model}\n` +
|
||||
`status: ${data.status || "queued"}\n` +
|
||||
`\n` +
|
||||
`Video generation is asynchronous. ` +
|
||||
`Call this tool again with op="get" and task_id="${data.id}" to fetch the result.`;
|
||||
`Call this tool again with op="get" and task_id="${taskId}" to fetch the result.`;
|
||||
|
||||
return { content: [{ type: "text", text: summary }] };
|
||||
} catch (e) {
|
||||
@@ -251,9 +223,7 @@ async function handleCreate({ args, headers, token }) {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Failed to submit video task: ${e.message}${
|
||||
e.detail ? `\nDetails: ${JSON.stringify(e.detail)}` : ""
|
||||
}`,
|
||||
text: `提交视频任务失败:${e.message}${e.detail ? `\nDetails: ${JSON.stringify(e.detail)}` : ""}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -261,59 +231,51 @@ async function handleCreate({ args, headers, token }) {
|
||||
}
|
||||
|
||||
// ---------- op=get 的业务处理 ----------
|
||||
async function handleGet({ args, token }) {
|
||||
// 与 handleCreate 同理:纯空白 task_id 不能直接拼进上游路径,
|
||||
// 否则会得到一条含编码后空白的上游报错,既无从归因也误导模型。
|
||||
async function handleGet({ args, headers }) {
|
||||
// 纯空白 task_id 不能直接拼进上游路径,否则得到一条含编码后空白的报错。
|
||||
const taskId = typeof args.task_id === "string" ? args.task_id.trim() : "";
|
||||
if (!taskId) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: 'op=get requires "task_id".' }],
|
||||
};
|
||||
return { isError: true, content: [{ type: "text", text: 'op=get requires "task_id".' }] };
|
||||
}
|
||||
|
||||
let target;
|
||||
try {
|
||||
target = resolveTarget(headers);
|
||||
} catch (e) {
|
||||
return { isError: true, content: [{ type: "text", text: e.message }] };
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await queryAsyncResult({ token, id: taskId });
|
||||
const status = data.task_status || "UNKNOWN";
|
||||
const data = await queryVideo({ ...target, taskId });
|
||||
const status = normalizeStatus(data.status);
|
||||
|
||||
if (status === "SUCCESS") {
|
||||
const v = (data.video_result || [])[0];
|
||||
if (!v || !v.url) {
|
||||
if (DONE.has(status)) {
|
||||
const url = (data.videos || [])[0]?.url || (data.data || [])[0]?.url;
|
||||
if (!url) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Task succeeded but no video URL returned. Raw: ${JSON.stringify(
|
||||
data
|
||||
)}`,
|
||||
},
|
||||
],
|
||||
content: [{ type: "text", text: `任务已完成但没有视频地址。Raw: ${JSON.stringify(data).slice(0, 500)}` }],
|
||||
};
|
||||
}
|
||||
const summary =
|
||||
`task_id: ${data.id}\n` +
|
||||
`model: ${data.model}\n` +
|
||||
`task_status: SUCCESS\n` +
|
||||
`Video URL: ${v.url}\n` +
|
||||
(v.cover_image_url ? `Cover: ${v.cover_image_url}\n` : "");
|
||||
// 单块输出:URL 已经写在摘要里,旧版额外回一个裸 URL 块会让模型
|
||||
// (core 会把 text 块 join 成一段)看到同一个链接两遍。
|
||||
return { content: [{ type: "text", text: summary }] };
|
||||
const credit = data.usage?.credit;
|
||||
const text =
|
||||
`task_id: ${taskId}\n` +
|
||||
`model: ${data.model || target.model}\n` +
|
||||
`status: completed\n` +
|
||||
`Video URL: ${url}\n` +
|
||||
(credit !== undefined ? `credit: ${credit}\n` : "") +
|
||||
(data.hint ? `note: ${data.hint}\n` : "");
|
||||
return { content: [{ type: "text", text }] };
|
||||
}
|
||||
|
||||
// PROCESSING / FAIL / 其他:原样返回关键字段
|
||||
const errBlock = data.error ? `\nerror: ${JSON.stringify(data.error)}` : "";
|
||||
const progress = data.progress !== undefined ? `\nprogress: ${data.progress}` : "";
|
||||
return {
|
||||
isError: status === "FAIL",
|
||||
isError: FAILED.has(status),
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text:
|
||||
`task_id: ${data.id}\n` +
|
||||
`model: ${data.model}\n` +
|
||||
`task_status: ${status}` +
|
||||
errBlock,
|
||||
text: `task_id: ${taskId}\nmodel: ${data.model || target.model}\nstatus: ${data.status || "unknown"}${progress}${errBlock}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -323,9 +285,7 @@ async function handleGet({ args, token }) {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Failed to query video task: ${e.message}${
|
||||
e.detail ? `\nDetails: ${JSON.stringify(e.detail)}` : ""
|
||||
}`,
|
||||
text: `查询视频任务失败:${e.message}${e.detail ? `\nDetails: ${JSON.stringify(e.detail)}` : ""}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -333,81 +293,61 @@ async function handleGet({ args, token }) {
|
||||
}
|
||||
|
||||
function buildServer() {
|
||||
const server = new McpServer(
|
||||
{ name: "video_task", version: "1.0.0" },
|
||||
{ capabilities: { tools: {} } }
|
||||
);
|
||||
const server = new McpServer({ name: "video_task", version: "2.0.0" }, { capabilities: { tools: {} } });
|
||||
|
||||
server.tool(
|
||||
"video_task",
|
||||
`AI video generation task.
|
||||
`AI video generation task (async).
|
||||
|
||||
Operations:
|
||||
- \`create\`: Start a new video generation task. Requires at least one of \`prompt\` or \`image_url\`. Returns \`task_id\` immediately; generation runs in background.
|
||||
- \`get\`: Check an existing task. Requires \`task_id\`. Returns status and, on SUCCESS, the video URL.
|
||||
- \`create\`: Start a new task. Requires at least one of \`prompt\` or \`image_url\`. Returns \`task_id\` immediately.
|
||||
- \`get\`: Check a task by \`task_id\`. Returns status and, when completed, the video URL.
|
||||
|
||||
Examples:
|
||||
- Text-to-video → \`{"op":"create","prompt":"A cat running in a garden"}\`
|
||||
- Image-to-video → \`{"op":"create","image_url":"https://example.com/cat.jpg","prompt":"The cat runs forward"}\`
|
||||
- Check task → \`{"op":"get","task_id":"<task_id from create>"}\`
|
||||
|
||||
Workflow: call \`create\` once, then call \`get\` with the returned \`task_id\` until \`task_status\` is \`SUCCESS\` or \`FAIL\`. Never create duplicate tasks just to check status.`,
|
||||
Workflow: call \`create\` once, then call \`get\` with the returned \`task_id\` until status is \`completed\` or \`failed\`. Never create duplicate tasks just to check status.`,
|
||||
{
|
||||
op: z
|
||||
.enum(["create", "get"])
|
||||
.describe(
|
||||
"Use create to start video generation; use get to check a task by its task_id."
|
||||
),
|
||||
op: z.enum(["create", "get"]).describe("create 提交生成;get 按 task_id 查结果。"),
|
||||
prompt: z
|
||||
.string()
|
||||
.max(512)
|
||||
.max(2000)
|
||||
.optional()
|
||||
.describe(
|
||||
"Video description, max 512 characters. Required for text-to-video; when combined with image_url it describes the motion."
|
||||
),
|
||||
.describe("视频描述,create 时 prompt 与 image_url 至少给一个。图生视频时描述画面如何运动。"),
|
||||
image_url: z
|
||||
.string()
|
||||
.url()
|
||||
.optional()
|
||||
.describe(
|
||||
"Reference image for image-to-video: publicly reachable png/jpeg/jpg URL <= 5 MB. For local files, first use the built-in mint_public_link tool to get a public temporary URL."
|
||||
"参考图(图生视频):公开可访问的 png/jpeg URL。本地文件先用 agent 内置的公开临时 URL 工具换成短链。"
|
||||
),
|
||||
with_audio: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe(
|
||||
"Whether to generate AI audio. Used with create; defaults to false."
|
||||
),
|
||||
task_id: z
|
||||
.string()
|
||||
seconds: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(60)
|
||||
.optional()
|
||||
.describe("task_id returned by create. Required for get; omit for create."),
|
||||
.describe("视频时长(秒)。留空用上游默认;越长越慢越贵。"),
|
||||
aspect_ratio: z.string().optional().describe("画幅,如 16:9 / 9:16 / 1:1。"),
|
||||
resolution: z.string().optional().describe("分辨率,如 768P / 1080P。留空用上游默认。"),
|
||||
negative_prompt: z.string().optional().describe("不希望出现的画面描述。"),
|
||||
enable_audio: z.boolean().optional().describe("是否生成音频,默认 false。"),
|
||||
with_audio: z.boolean().optional().describe("旧字段名,等同 enable_audio。"),
|
||||
task_id: z.string().min(1).optional().describe("create 返回的 task_id,get 时必填。"),
|
||||
},
|
||||
async (args, extra) => {
|
||||
const headers = extra?.requestInfo?.headers;
|
||||
const token = pickToken(headers);
|
||||
if (!token) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Missing authentication token." }],
|
||||
};
|
||||
}
|
||||
|
||||
switch (args.op) {
|
||||
case "create":
|
||||
return await handleCreate({ args, headers, token });
|
||||
return await handleCreate({ args, headers });
|
||||
case "get":
|
||||
return await handleGet({ args, token });
|
||||
return await handleGet({ args, headers });
|
||||
default:
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Unknown op "${args.op}" (available: create, get).`,
|
||||
},
|
||||
],
|
||||
content: [{ type: "text", text: `Unknown op "${args.op}" (available: create, get).` }],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -420,20 +360,14 @@ async function startHttp({ port, host }) {
|
||||
const app = express();
|
||||
app.use(express.json({ limit: "2mb" }));
|
||||
|
||||
// session -> { server, transport } 映射
|
||||
const sessions = new Map();
|
||||
|
||||
closeAllSessions = () => {
|
||||
for (const { server, transport } of sessions.values()) {
|
||||
try {
|
||||
transport.close();
|
||||
} catch {}
|
||||
try {
|
||||
server.close();
|
||||
} catch {}
|
||||
try { transport.close(); } catch {}
|
||||
try { server.close(); } catch {}
|
||||
}
|
||||
sessions.clear();
|
||||
// 掰掉未关闭的 SSE / keep-alive 连接,否则 httpServer.close() 回调不会触发
|
||||
httpServer?.closeAllConnections?.();
|
||||
};
|
||||
|
||||
@@ -444,22 +378,16 @@ async function startHttp({ port, host }) {
|
||||
async function handleSessionRequest(req, res) {
|
||||
const sessionId = req.headers["mcp-session-id"];
|
||||
|
||||
// 已有 session:复用 transport
|
||||
if (sessionId && sessions.has(sessionId)) {
|
||||
const { transport } = sessions.get(sessionId);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return;
|
||||
}
|
||||
|
||||
// 新 session:必须是 initialize 请求
|
||||
if (!isInitializeRequest(req.body)) {
|
||||
res.status(400).json({
|
||||
jsonrpc: "2.0",
|
||||
error: {
|
||||
code: -32000,
|
||||
message:
|
||||
"Bad Request: missing valid Mcp-Session-Id for non-initialize request",
|
||||
},
|
||||
error: { code: -32000, message: "Bad Request: missing valid Mcp-Session-Id for non-initialize request" },
|
||||
id: req.body?.id ?? null,
|
||||
});
|
||||
return;
|
||||
@@ -469,25 +397,19 @@ async function startHttp({ port, host }) {
|
||||
const server = buildServer();
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => newId,
|
||||
onsessioninitialized: (id) => {
|
||||
sessions.set(id, { server, transport });
|
||||
},
|
||||
onsessioninitialized: (id) => { sessions.set(id, { server, transport }); },
|
||||
});
|
||||
|
||||
transport.onclose = () => {
|
||||
if (transport.sessionId) {
|
||||
sessions.delete(transport.sessionId);
|
||||
}
|
||||
if (transport.sessionId) sessions.delete(transport.sessionId);
|
||||
};
|
||||
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
}
|
||||
|
||||
// POST /mcp: 主入口
|
||||
app.post("/mcp", handleSessionRequest);
|
||||
|
||||
// GET /mcp & DELETE /mcp: 用于 SSE 流 / 会话关闭
|
||||
const handleOther = async (req, res) => {
|
||||
const sessionId = req.headers["mcp-session-id"];
|
||||
if (!sessionId || !sessions.has(sessionId)) {
|
||||
@@ -501,17 +423,13 @@ async function startHttp({ port, host }) {
|
||||
app.delete("/mcp", handleOther);
|
||||
|
||||
httpServer = app.listen(port, host, () => {
|
||||
console.log(
|
||||
`[video] MCP HTTP server listening on http://${host}:${port}/mcp`
|
||||
);
|
||||
console.log(`[video] MCP HTTP server listening on http://${host}:${port}/mcp`);
|
||||
});
|
||||
}
|
||||
|
||||
async function startStdio() {
|
||||
const server = buildServer();
|
||||
const { StdioServerTransport } = await import(
|
||||
"@modelcontextprotocol/sdk/server/stdio.js"
|
||||
);
|
||||
const { StdioServerTransport } = await import("@modelcontextprotocol/sdk/server/stdio.js");
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error("[video] MCP stdio server started");
|
||||
|
||||
+8
-3
@@ -4,7 +4,7 @@
|
||||
# ./scripts/init.sh # 已有 .env / config.yml 均不覆盖
|
||||
#
|
||||
# 可选变量:PUBLIC_IP、AGENT_PORT、AUTH_USER / AUTH_PASSWORD(默认 admin / admin)、
|
||||
# EXECUTOR_TOKEN / WXBOT_TOKEN / AUTH_SECRET(不给则生成并复用)。
|
||||
# EXECUTOR_TOKEN / WXBOT_TOKEN / DEVICE_TOKEN / AUTH_SECRET(不给则生成并复用)。
|
||||
set -euo pipefail
|
||||
|
||||
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
@@ -55,6 +55,7 @@ title "生成 $ENV_FILE"
|
||||
|
||||
EXECUTOR_TOKEN="${EXECUTOR_TOKEN:-$(env_get EXECUTOR_TOKEN)}"
|
||||
WXBOT_TOKEN="${WXBOT_TOKEN:-$(env_get WXBOT_TOKEN)}"
|
||||
DEVICE_TOKEN="${DEVICE_TOKEN:-$(env_get DEVICE_TOKEN)}"
|
||||
AUTH_SECRET="${AUTH_SECRET:-$(env_get AUTH_SECRET)}"
|
||||
AGENT_PORT="${PORT_INPUT:-$(env_get AGENT_PORT)}"
|
||||
AGENT_PORT="${AGENT_PORT:-8090}"
|
||||
@@ -62,6 +63,9 @@ OLD_URL="$(env_get PUBLIC_BASE_URL)"
|
||||
|
||||
EXECUTOR_TOKEN="${EXECUTOR_TOKEN:-$(gen_hex)}"
|
||||
WXBOT_TOKEN="${WXBOT_TOKEN:-$(gen_hex)}"
|
||||
# 设备接入(devices.js):设备连 wss://<公网前缀>/dev/ws 时出示的 Bearer token。
|
||||
# 与 PUBLIC_BASE_URL 同时具备才启用;没公网地址的部署生成了也无害。
|
||||
DEVICE_TOKEN="${DEVICE_TOKEN:-$(gen_hex)}"
|
||||
AUTH_SECRET="${AUTH_SECRET:-$(gen_hex)}"
|
||||
|
||||
if [ -n "$PUBLIC_IP" ]; then
|
||||
@@ -84,7 +88,7 @@ fi
|
||||
EXTRAS=""
|
||||
if [ -f "$ENV_FILE" ]; then
|
||||
EXTRAS="$(awk -F= '/^[A-Za-z_][A-Za-z0-9_]*=/ &&
|
||||
$1 !~ /^(EXECUTOR_TOKEN|WXBOT_TOKEN|AUTH_SECRET|AGENT_PORT|PUBLIC_BASE_URL)$/ { print }' \
|
||||
$1 !~ /^(EXECUTOR_TOKEN|WXBOT_TOKEN|DEVICE_TOKEN|AUTH_SECRET|AGENT_PORT|PUBLIC_BASE_URL)$/ { print }' \
|
||||
"$ENV_FILE" || true)"
|
||||
fi
|
||||
|
||||
@@ -92,6 +96,7 @@ fi
|
||||
echo "# 由 scripts/init.sh 生成;重跑复用已有密钥,并保留其它变量。"
|
||||
echo "EXECUTOR_TOKEN=$EXECUTOR_TOKEN"
|
||||
echo "WXBOT_TOKEN=$WXBOT_TOKEN"
|
||||
echo "DEVICE_TOKEN=$DEVICE_TOKEN"
|
||||
echo "AUTH_SECRET=$AUTH_SECRET"
|
||||
echo "AGENT_PORT=$AGENT_PORT"
|
||||
echo "PUBLIC_BASE_URL=$PUBLIC_BASE_URL"
|
||||
@@ -101,7 +106,7 @@ fi
|
||||
fi
|
||||
} > "$ENV_FILE"
|
||||
|
||||
info "EXECUTOR_TOKEN / WXBOT_TOKEN / AUTH_SECRET 已就位"
|
||||
info "EXECUTOR_TOKEN / WXBOT_TOKEN / DEVICE_TOKEN / AUTH_SECRET 已就位"
|
||||
|
||||
# ---------- config.yml(只写初始管理员)----------
|
||||
title "检查 $CONFIG_YML"
|
||||
|
||||
+1
-1
@@ -141,7 +141,7 @@ services:
|
||||
- `name`:固定端口路由用的服务标识(有域名时可省略)
|
||||
- `host`:`域名` / `*.example.com` / `"*"`(兜底)
|
||||
- `userAgent`:可选,握手时伪装浏览器 UA,降低被 DPI 盯上的概率
|
||||
- 断线自动重连,启动时自检每个端口可达性(不可达只告警不退出)
|
||||
- 断线自动重连(含半死链路:75 秒收不到任何帧即主动断开重连),启动时自检每个端口可达性(不可达只告警不退出)
|
||||
|
||||
## 隧道协议
|
||||
|
||||
|
||||
+19
-3
@@ -4,7 +4,8 @@
|
||||
// 主动连服务器 → 收请求 → 按 Host 找本地端口 → 转发 → 流式回传
|
||||
//
|
||||
// 用法: node client.js [config.yml] 环境变量 TUNNEL_SERVER / TUNNEL_TOKEN 覆盖配置
|
||||
// 断线自动重连(指数退避 1s → 30s),心跳由服务端 ping 驱动,本端 ws 库自动回 pong。
|
||||
// 断线自动重连(指数退避 1s → 30s)。心跳由服务端 ping 驱动、本端 ws 库自动回 pong;
|
||||
// 但半死链路(Mac 睡眠/切网后 TCP 还在、实际不通)不会触发 close,所以本端另设看门狗。
|
||||
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
@@ -42,7 +43,9 @@ function loadConfig() {
|
||||
}
|
||||
const CFG = loadConfig();
|
||||
|
||||
function log(...a) { console.log(new Date().toISOString().slice(11, 19), '[client]', ...a); }
|
||||
// 日志时间固定东八区:本机时区可能变,跨时区看日志不该换算
|
||||
const CST_MS = 8 * 3600 * 1000;
|
||||
function log(...a) { console.log(new Date(Date.now() + CST_MS).toISOString().slice(11, 19), '[client]', ...a); }
|
||||
|
||||
// ---------- 服务路由:Host → 本地端口 ----------
|
||||
// 精确匹配优先;再退到「忽略端口」;最后允许 *.xxx 通配一项
|
||||
@@ -72,6 +75,7 @@ const inflight = new Map();
|
||||
let ws = null;
|
||||
let backoff = 1000;
|
||||
let stopping = false;
|
||||
let lastSeen = 0; // 最近一次收到服务端任何帧的时刻
|
||||
|
||||
function send(obj) {
|
||||
if (ws && ws.readyState === 1) {
|
||||
@@ -192,10 +196,12 @@ function connect() {
|
||||
ws.on('open', () => {
|
||||
backoff = 1000;
|
||||
linked = true;
|
||||
lastSeen = Date.now();
|
||||
log(everLinked ? '已重新连接' : '已连接 ' + CFG.server);
|
||||
everLinked = true;
|
||||
});
|
||||
ws.on('message', onMessage);
|
||||
ws.on('message', (data) => { lastSeen = Date.now(); onMessage(data); });
|
||||
ws.on('ping', () => { lastSeen = Date.now(); });
|
||||
ws.on('close', (code, reason) => {
|
||||
if (stopping) return;
|
||||
if (linked) { // 只在「连着 → 断开」这个跳变上说一次
|
||||
@@ -208,6 +214,16 @@ function connect() {
|
||||
ws.on('error', () => {}); // close 紧接着会来,不重复报
|
||||
}
|
||||
|
||||
// 看门狗:服务端每 25s ping 一次,超过 75s(连丢 3 次)一个帧都没收到就认定链路半死。
|
||||
// 这种连接 close 事件永远不来,只能本端主动 terminate,由 close 走既有重连。
|
||||
setInterval(() => {
|
||||
if (stopping || !ws || ws.readyState !== 1) return;
|
||||
if (Date.now() - lastSeen > 75000) {
|
||||
log('链路 ' + Math.round((Date.now() - lastSeen) / 1000) + 's 无响应,主动断开重连');
|
||||
try { ws.terminate(); } catch (_) {}
|
||||
}
|
||||
}, 20000).unref();
|
||||
|
||||
process.on('SIGINT', () => { stopping = true; log('退出'); try { ws && ws.close(1000, 'client shutdown'); } catch (_) {} process.exit(0); });
|
||||
process.on('SIGTERM', () => { stopping = true; process.exit(0); });
|
||||
|
||||
|
||||
+6
-3
@@ -123,9 +123,12 @@ function handleRequest(req, res, fixedService) {
|
||||
client.send(JSON.stringify({ id, op: 'body', b64: chunk.toString('base64') }));
|
||||
});
|
||||
req.on('end', () => { if (!overflow) client.send(JSON.stringify({ id, op: 'end' })); });
|
||||
// 手机/浏览器提前断开:让家里把上游请求也停掉,别白跑
|
||||
req.on('aborted', () => {
|
||||
if (!pending.has(id)) return;
|
||||
// 客户端提前断开:让家里把上游请求也停掉,别白跑。
|
||||
// 必须挂在 res 上:SSE 是 GET 无 body,req 的 'aborted' 只在传 body 时触发(且已废弃)。
|
||||
// 漏掉这条时,前端关掉 SSE 后 client 会继续读流、继续往 WS 灌数据,切会话时
|
||||
// 新旧两条流同时推,把单条 WS 占满,后发的 detail 请求只能排队。
|
||||
res.on('close', () => {
|
||||
if (!pending.has(id)) return; // 正常结束时 pending 已删
|
||||
client.send(JSON.stringify({ id, op: 'abort' }));
|
||||
pending.delete(id);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user