Files
dsh-enhancements/plugins/dsh-scheduler/README.md
T
江晨 c33fe31f49 fix: 插件最终版(stdin 机制)+ 踩坑经验归档
- dsh-scheduler: shell 请求无 args 字段,改 stdin 传脚本 + JS 字面量持久化(Unicode 安全)
- dsh-usage: 余额查询同样改 stdin 机制
- 记录 CollectedOutput.text / sandboxPolicy danger-full-access / exitCode / workdir 等关键经验
2026-08-17 12:06:47 +08:00

51 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# dsh-scheduler — 定时任务插件
Host 侧定时任务调度器。给 DeepSeek Harness 补上缺失的**定时任务**能力。
## 能力
- **调度表达式**
- cron 5 字段:`分 时 日 月 周`,支持 `*``*/n``a-b``a,b,c`(如 `*/10 * * * *` 每 10 分钟)
- 间隔简写:`every:30s` / `every:5m` / `every:1h`
- **动作**:任意 cmd 命令行(可指定工作目录、超时)
- **持久化**:任务存于 `%DSH_HOME%\storages\dsh-scheduler.json`,重启不丢
- **工具**(注册给 agent 调用):
| 工具 | 作用 |
|---|---|
| `sched_add` | 添加任务(名称/调度/命令) |
| `sched_list` | 列出任务与运行状态 |
| `sched_remove` | 按 id 删除 |
| `sched_run_now` | 立即执行一次 |
| `sched_toggle` | 启用/停用 |
| `sched_diag` | 诊断 shell 执行环境与持久化 |
## 实现要点(踩坑记录)
- 宿主插件环境**没有** `fs`/`process` 全局 → 文件读写走 `shell` 服务 + node 子进程
- **ShellExecRequest 没有 `args` 字段**node 脚本必须用 `stdin` 传入(`command:'node'` + `stdin:脚本`),
`args` 会被静默忽略,导致"裸跑 node"零输出
- `res.stdout``CollectedOutput` 对象(`{text, truncated, spillPath}`),用 `.text` 取值,
`String()` 会得到 `"[object Object]"`
- 宿主 `ctx.shell` 默认按 `workspace-write` 沙箱执行,本机无可用沙箱后端会被拒绝 →
必须显式传 `sandboxPolicy: sandboxPolicyService.resolve({mode:'danger-full-access'})`
- 结果字段是 `res.exitCode`(不是 `res.code`);工作目录字段是 `workdir`
- 数据持久化用 **JS 字符串字面量**直接嵌入脚本(`JSON.stringify(JSON.stringify(data))`),
Unicode 安全(`btoa` base64 曾引发中文乱码)
- 任务执行用完整命令行字符串 `cmd /c <command>`
- 每秒 tick`ctx.timer.interval`),`ticking` 标志防重入
- cron 的 `nextRun` 预计算:从当前时间起逐分钟扫描(最多 7 天)
## 文件
- `host.js` — 完整宿主插件源码
- 部署方式:动态 Cordis 插件(`cordis_define` + `cordis_run`),或归档到 presets/
## 示例
```
sched_add name="每30秒打点" schedule="every:30s" command="echo tick >> C:\Users\a1703\.dsh\logs\sched-demo.log"
sched_add name="每整点提醒" schedule="0 * * * *" command="msg * 整点提醒"
sched_list
```