work-rhythm 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,7 @@ const fs = require('node:fs');
10
10
  const os = require('node:os');
11
11
  const path = require('node:path');
12
12
  const { which } = require('./run');
13
+ const { parseYaml } = require('./config');
13
14
 
14
15
  const BUNDLED_ZCODE = '/Applications/ZCode.app/Contents/Resources/glm/zcode.cjs';
15
16
 
@@ -165,10 +166,13 @@ function installedAgents(env) {
165
166
  });
166
167
  }
167
168
 
168
- // 把选定的 agent 写进 collectors/local.env 的 YZJ_BOT_AGENT(没有这个文件就从样例复制一份)。
169
- // 先确认它在本机找得到。返回 { name, kind, file } 或 { problems }
170
- function bindAgent(root, env, name) {
171
- const problems = createAgent({ root, agent: name, env }).doctor();
169
+ // 把选定的 agent 写进 collectors/local.env 的 YZJ_BOT_AGENT(没有这个文件就从样例复制一份),顺带可以写模型和思考强度。
170
+ // 先确认它在本机找得到、模型和强度它认。返回 { name, kind, file } 或 { problems }
171
+ function bindAgent(root, env, name, { model, effort } = {}) {
172
+ // undefined = 不动原来的;空串 = 清掉
173
+ model = model === undefined ? undefined : String(model).trim();
174
+ effort = effort === undefined ? undefined : String(effort).trim();
175
+ const problems = createAgent({ root, agent: name, env, model: model || '', effort: effort || '' }).doctor();
172
176
  if (problems.length) return { problems };
173
177
  const dir = path.join(root, 'collectors');
174
178
  const file = path.join(dir, 'local.env');
@@ -177,13 +181,24 @@ function bindAgent(root, env, name) {
177
181
  if (fs.existsSync(example)) fs.copyFileSync(example, file);
178
182
  else fs.writeFileSync(file, '');
179
183
  }
180
- const value = /[\s"'$`]/.test(name) ? `'${name.replace(/'/g, `'\\''`)}'` : `"${name}"`;
181
- const lines = fs.readFileSync(file, 'utf8').split('\n');
182
- const at = lines.findIndex((l) => /^#?\s*YZJ_BOT_AGENT=/.test(l));
183
- if (at >= 0) lines[at] = `YZJ_BOT_AGENT=${value}`;
184
- else lines.push(`YZJ_BOT_AGENT=${value}`);
184
+ const value = quote(name);
185
+ let lines = fs.readFileSync(file, 'utf8').split('\n');
186
+ lines = setLocalEnvKey(lines, 'YZJ_BOT_AGENT', value);
187
+ // 模型和思考强度:给了字符串就写(空串是清掉),没给(undefined)不动原来的
188
+ if (model !== undefined) lines = setLocalEnvKey(lines, 'YZJ_BOT_MODEL', quote(model));
189
+ if (effort !== undefined) lines = setLocalEnvKey(lines, 'YZJ_BOT_EFFORT', quote(effort));
185
190
  fs.writeFileSync(file, `${lines.join('\n').replace(/\n*$/, '')}\n`);
186
- return { name, kind: presetKind(name), file };
191
+ return { name, kind: presetKind(name), file, model, effort };
192
+ }
193
+
194
+ const quote = (value) => (value === '' ? '""' : /[\s"'$`]/.test(value) ? `'${value.replace(/'/g, `'\\''`)}'` : `"${value}"`);
195
+
196
+ // 往 local.env 的行里写一个键:有就换那一行(连注释掉的那行一起换),没有就追加
197
+ function setLocalEnvKey(lines, key, value) {
198
+ const at = lines.findIndex((l) => new RegExp(`^#?\\s*${key}=`).test(l));
199
+ if (at >= 0) lines[at] = `${key}=${value}`;
200
+ else lines.push(`${key}=${value}`);
201
+ return lines;
187
202
  }
188
203
 
189
204
  // DSH 的命令行要用它自带的 Node 跑(设 ELECTRON_RUN_AS_NODE 当普通 node 用),入口是 @deepseek-ai/dsh 的 bin.js
@@ -214,8 +229,9 @@ function findDsh(env) {
214
229
  }
215
230
 
216
231
  // headless 默认不带桌面应用里的模型和凭据(会去找 deepseek-official 要 API key)。把它的配置里模型那两条摘出来,
217
- // 外加打开它的凭据存储,写成一份覆盖文件带进去。密钥始终在 DSH 自己的存储里,不经过这里
218
- function readDshPatchEntries(home) {
232
+ // 外加打开它的凭据存储,写成一份覆盖文件带进去。密钥始终在 DSH 自己的存储里,不经过这里。
233
+ // model / effort 给了就把 agent-default-model 换成它们(模型清单和凭据还是桌面应用那份)
234
+ function readDshPatchEntries(home, { model = '', effort = '' } = {}) {
219
235
  const src = path.join(home, 'profiles', 'web', 'cordis.patch.yml');
220
236
  let text;
221
237
  try {
@@ -223,13 +239,77 @@ function readDshPatchEntries(home) {
223
239
  } catch {
224
240
  return { problem: `找不到 DSH 的配置 ${src}:dsh 预置要用桌面应用里配好的模型` };
225
241
  }
226
- const picked = text.split(/^(?=- id: )/m)
227
- .filter((block) => /^- id: (agent-default-model|llm-pi-ai)[ \t]*$/m.test(block))
228
- .map((block) => block.trimEnd());
229
- if (picked.length < 2) return { problem: `${src} 里没找齐 agent-default-model 和 llm-pi-ai 两条` };
230
- return { lines: ['# 值守自动生成,不要手改:让 DSH 的 headless 模式用桌面应用里配置的模型和凭据', '- id: credentials', ' disabled: false', ...picked] };
242
+ const blocks = text.split(/^(?=- id: )/m).filter((block) => /^- id: /.test(block)).map((block) => block.trimEnd());
243
+ const byId = {};
244
+ for (const block of blocks) byId[/^- id: (.*?)[ \t]*$/m.exec(block)[1]] = block;
245
+ if (!byId['agent-default-model'] || !byId['llm-pi-ai']) return { problem: `${src} 里没找齐 agent-default-model 和 llm-pi-ai 两条` };
246
+ let defaultBlock = byId['agent-default-model'];
247
+ if (model || effort) {
248
+ const current = dshDefaultModel(text);
249
+ if (current.problem) return current;
250
+ const provider = model.includes('/') ? model.split('/')[0] : (model ? dshProviderOf(text, model) : current.provider);
251
+ if (!provider) return { problem: `DSH 的配置里没有 ${model} 这个模型(可用 work-rhythm agent models dsh 列)` };
252
+ const picked = model && !model.includes('/') ? model : (model ? model.split('/').slice(1).join('/') : current.model);
253
+ const effortLine = effort && effort !== 'none' ? `\n reasoningEffort: ${effort}` : '';
254
+ defaultBlock = `- id: agent-default-model\n name: "@deepseek-ai/dsh-agent-default-model"\n config:\n provider: ${provider}\n model: ${picked}${effortLine}`;
255
+ }
256
+ return { lines: ['# 值守自动生成,不要手改:让 DSH 的 headless 模式用桌面应用里配置的模型和凭据', '- id: credentials', ' disabled: false', defaultBlock, byId['llm-pi-ai']] };
257
+ }
258
+
259
+ // 桌面应用配置里 agent-default-model 的 provider / model / reasoningEffort。配置是 YAML,用 lib/config 的解析器读
260
+ function dshConfig(text) {
261
+ const doc = parseYaml(text);
262
+ const entries = Array.isArray(doc) ? doc : [];
263
+ const pick = (id) => entries.find((e) => e && e.id === id);
264
+ return { defaultModel: pick('agent-default-model'), llm: pick('llm-pi-ai') };
265
+ }
266
+
267
+ function dshDefaultModel(text) {
268
+ const entry = dshConfig(text).defaultModel;
269
+ const cfg = entry && entry.config;
270
+ if (!cfg || !cfg.provider || !cfg.model) return { problem: 'DSH 的配置里 agent-default-model 没写 provider 或 model' };
271
+ return { provider: String(cfg.provider), model: String(cfg.model), effort: cfg.reasoningEffort ? String(cfg.reasoningEffort) : '' };
272
+ }
273
+
274
+ // DSH 配置里各 provider 的模型清单:[{ provider, id, name, reasoning }]。读的是 llm-pi-ai 那条的 providers
275
+ function dshModels(text) {
276
+ const llm = dshConfig(text).llm;
277
+ const providers = llm && llm.config && llm.config.providers && typeof llm.config.providers === 'object' ? llm.config.providers : {};
278
+ const out = [];
279
+ for (const [provider, p] of Object.entries(providers)) {
280
+ for (const m of (p && Array.isArray(p.models) ? p.models : [])) {
281
+ if (!m || !m.id) continue;
282
+ out.push({ provider, id: String(m.id), name: m.name ? String(m.name) : '', reasoning: Boolean(m.compat && m.compat.supportsReasoningEffort) });
283
+ }
284
+ }
285
+ return out;
231
286
  }
232
287
 
288
+ function dshProviderOf(text, modelId) {
289
+ return dshModels(text).find((m) => m.id === modelId)?.provider || '';
290
+ }
291
+
292
+ // 思考强度的词表(我们的叫法):各家换成自己的,zcode 没有入口
293
+ const EFFORTS = ['none', 'low', 'medium', 'high', 'max'];
294
+
295
+ // 一个预置能不能指定模型和思考强度:modelArgs(model, effort) 返回要加的参数(没有就返回空数组)
296
+ const MODEL_SUPPORT = {
297
+ zcode: { model: false, effort: false },
298
+ dsh: { model: true, effort: true, via: 'patch' },
299
+ qoder: {
300
+ model: true,
301
+ effort: true,
302
+ via: 'args',
303
+ args: (model, effort) => [...(model ? ['--model', model] : []), ...(effort && effort !== 'none' ? ['--reasoning-effort', effort] : [])],
304
+ },
305
+ claude: {
306
+ model: true,
307
+ effort: true,
308
+ via: 'args',
309
+ args: (model, effort) => [...(model ? ['--model', model] : []), ...(EFFORTS.includes(effort) && effort !== 'none' ? ['--effort', effort] : [])],
310
+ },
311
+ };
312
+
233
313
  // DSH 的 --json 是一行一个事件:session 给会话 ID,final 给回答和结束原因
234
314
  function parseDsh(stdout) {
235
315
  let final = null;
@@ -261,22 +341,29 @@ function parseDsh(stdout) {
261
341
  // timeoutS 一次运行最多等多少秒
262
342
  // preamble 随每句话带给 agent 的提示(可以为空)
263
343
  // log 记日志的函数 (event, fields)
344
+ // model 用哪个模型(各家的模型 id;空串用它自己的默认)
345
+ // effort 思考强度:none / low / medium / high / max(空串用它自己的默认)
264
346
  // 返回 { kind, run, doctor }:
265
347
  // run(message, { session, onSessionLost }) → { reply, sessionId } 或 { failed, config? }。
266
348
  // 给了 session 就接着它跑;接不上(过期、被删)时调 onSessionLost(),再开新会话跑一次
267
349
  // doctor() → 配置和依赖的问题,一条一个字符串;没有就是空数组
268
- function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = '', log = () => {}, env = process.env }) {
350
+ function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = '', log = () => {}, env = process.env, model = '', effort = '' }) {
269
351
  const name = String(agent || '').trim();
270
352
  const kind = presetKind(name);
353
+ model = String(model || '').trim();
354
+ effort = String(effort || '').trim();
271
355
 
272
356
  function doctor() {
357
+ if (model && kind !== 'custom' && !MODEL_SUPPORT[kind].model) return [`预置 ${kind} 不支持指定模型(${kind === 'zcode' ? 'ZCode 的命令行没有这个参数,用的是它自己的默认模型' : '见 docs/agents.md'}):把 collectors/local.env 里的 YZJ_BOT_MODEL 去掉,或者换一个支持的 agent`];
358
+ if (effort && kind !== 'custom' && !MODEL_SUPPORT[kind].effort) return [`预置 ${kind} 不支持指定思考强度:把 collectors/local.env 里的 YZJ_BOT_EFFORT 去掉`];
359
+ if (effort && kind !== 'custom' && !EFFORTS.includes(effort)) return [`思考强度写的是「${effort}」,只能是 ${EFFORTS.join(' / ')}`];
273
360
  if (!name) return [`没绑 agent:定时任务和机器人值守都要一个 agent 来跑。先 work-rhythm agent detect 看本机有哪些,再 work-rhythm agent bind <${PRESET_NAMES.join('|')}|命令>(写进 collectors/local.env 的 YZJ_BOT_AGENT)`];
274
361
  if (kind !== 'custom') {
275
362
  const found = PRESETS[kind].find(env);
276
363
  if (found.problem) return [found.problem];
277
- // dsh 还要用桌面应用里配好的模型
364
+ // dsh 还要用桌面应用里配好的模型;指定了模型的话要在它的清单里
278
365
  if (kind === 'dsh') {
279
- const entries = readDshPatchEntries(found.home);
366
+ const entries = readDshPatchEntries(found.home, { model, effort });
280
367
  if (entries.problem) return [entries.problem];
281
368
  }
282
369
  return [];
@@ -328,7 +415,7 @@ function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = ''
328
415
  const q = findQoder(env);
329
416
  if (q.problem) return q;
330
417
  const permission = PRESETS.qoder.modes[mode] || mode;
331
- const base = ['--cwd', root, '--permission-mode', permission];
418
+ const base = ['--cwd', root, '--permission-mode', permission, ...MODEL_SUPPORT.qoder.args(model, effort)];
332
419
  return {
333
420
  command: q.command,
334
421
  env,
@@ -342,7 +429,7 @@ function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = ''
342
429
  const c = findClaude(env);
343
430
  if (c.problem) return c;
344
431
  const permission = PRESETS.claude.modes[mode] || mode;
345
- const base = ['--cwd', root, '--permission-mode', permission];
432
+ const base = ['--cwd', root, '--permission-mode', permission, ...MODEL_SUPPORT.claude.args(model, effort)];
346
433
  return {
347
434
  command: c.command,
348
435
  env,
@@ -356,7 +443,7 @@ function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = ''
356
443
  function planDsh(prompt, stateDir) {
357
444
  const d = findDsh(env);
358
445
  if (d.problem) return d;
359
- const entries = readDshPatchEntries(d.home);
446
+ const entries = readDshPatchEntries(d.home, { model, effort });
360
447
  if (entries.problem) return entries;
361
448
  const patchFile = path.join(stateDir, 'dsh-headless.patch.yml');
362
449
  fs.mkdirSync(stateDir, { recursive: true });
@@ -399,11 +486,38 @@ function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = ''
399
486
  if (r.status !== 0) return { failed: `退出码 ${r.status}:${tail(r.stderr)}` };
400
487
  const out = plan.parse(r.stdout);
401
488
  if (out.failed) return out;
402
- log('agent 完成', `${secs()}s ${kind} mode=${mode} session=${used ? '续' : '新'}`);
489
+ log('agent 完成', `${secs()}s ${kind} mode=${mode}${model ? ` model=${model}` : ''}${effort ? ` effort=${effort}` : ''} session=${used ? '续' : '新'}`);
403
490
  return out.reply.trim() ? { reply: out.reply.trim(), sessionId: out.sessionId || '' } : { failed: '它回了空内容' };
404
491
  }
405
492
 
406
493
  return { kind, run, doctor };
407
494
  }
408
495
 
409
- module.exports = { createAgent, splitCommand, PRESETS, PRESET_NAMES, invokingAgent, installedAgents, bindAgent, findZcode, findDsh, findQoder, findClaude };
496
+ // 一个预置能选哪些模型:[{ provider, id, name, reasoning }]。dsh 读它的配置;qoder 问命令行(--list-models);claude 给常见的几个别名;
497
+ // zcode 不支持。返回 { models } 或 { problem }
498
+ function listModels(kind, env) {
499
+ if (!PRESETS[kind] || !MODEL_SUPPORT[kind].model) return { models: [] };
500
+ const found = PRESETS[kind].find(env);
501
+ if (found.problem) return { problem: found.problem };
502
+ if (kind === 'dsh') {
503
+ let text;
504
+ try {
505
+ text = fs.readFileSync(path.join(found.home, 'profiles', 'web', 'cordis.patch.yml'), 'utf8');
506
+ } catch {
507
+ return { problem: `找不到 DSH 的配置:${path.join(found.home, 'profiles', 'web', 'cordis.patch.yml')}` };
508
+ }
509
+ const current = dshDefaultModel(text);
510
+ return { models: dshModels(text), current: current.problem ? {} : current };
511
+ }
512
+ if (kind === 'qoder') {
513
+ const r = spawnSync(found.command, ['--list-models'], { encoding: 'utf8', env, timeout: 60000 });
514
+ if (r.status !== 0) return { problem: `qoder --list-models 没跑成:${tail(r.stderr || r.stdout, 200)}` };
515
+ return { text: r.stdout.trim() };
516
+ }
517
+ if (kind === 'claude') {
518
+ return { models: ['opus', 'sonnet', 'haiku'].map((id) => ({ id, name: id === 'opus' ? 'Opus(最强)' : id === 'sonnet' ? 'Sonnet(均衡)' : 'Haiku(最快)', reasoning: true })) };
519
+ }
520
+ return { models: [] };
521
+ }
522
+
523
+ module.exports = { createAgent, splitCommand, PRESETS, PRESET_NAMES, MODEL_SUPPORT, EFFORTS, invokingAgent, installedAgents, bindAgent, listModels, findZcode, findDsh, findQoder, findClaude };
@@ -163,7 +163,8 @@ function createBot({ root, env }) {
163
163
  // 收到就先回一句,agent 慢的时候我不用干等
164
164
  const ACK = env.YZJ_BOT_ACK !== '0';
165
165
  const PREAMBLE = env.YZJ_BOT_PREAMBLE !== undefined ? env.YZJ_BOT_PREAMBLE : DEFAULT_PREAMBLE;
166
- const agent = createAgent({ root, agent: AGENT, mode: AGENT_MODE, timeoutS: TIMEOUT_S, preamble: PREAMBLE, log, env });
166
+ // 模型和思考强度(collectors/local.env 的 YZJ_BOT_MODEL / YZJ_BOT_EFFORT),只有支持的预置才认;不写就用 agent 自己的默认
167
+ const agent = createAgent({ root, agent: AGENT, mode: AGENT_MODE, timeoutS: TIMEOUT_S, preamble: PREAMBLE, log, env, model: (env.YZJ_BOT_MODEL || '').trim(), effort: (env.YZJ_BOT_EFFORT || '').trim() });
167
168
 
168
169
  // ---------------------------------------------------------------- 配置
169
170
  // 检查值守的配置和依赖:有问题一条一个字符串;没问题就是空数组。check 配了值守时也调它
@@ -55,6 +55,9 @@ function loadConfig(root) {
55
55
  push: rhythm.push !== false,
56
56
  quiet: asList(rhythm.quiet),
57
57
  agentMode: typeof rhythm['agent-mode'] === 'string' && rhythm['agent-mode'].trim() ? rhythm['agent-mode'].trim() : 'edit',
58
+ // 定时任务用的模型和思考强度(可选项,profile 里不写也行):不配就和值守一样,用 local.env 的 YZJ_BOT_MODEL / YZJ_BOT_EFFORT
59
+ agentModel: typeof rhythm['agent-model'] === 'string' ? rhythm['agent-model'].trim() : '',
60
+ agentEffort: typeof rhythm['agent-effort'] === 'string' ? rhythm['agent-effort'].trim() : '',
58
61
  taskTimeout: positiveInt(rhythm['task-timeout'], 1200),
59
62
  syncTimeout: positiveInt(rhythm['sync-timeout'], 600),
60
63
  notifyChannel: typeof preferences.notify === 'string' ? preferences.notify.trim() : '',
@@ -221,7 +224,7 @@ function createRhythm({ root, env, packageRoot }) {
221
224
  const willPush = wantPush && cfg.push && !cfg.quiet.includes(id);
222
225
  // 服务会替它推送时在指令末尾注明,免得它自己再发一遍
223
226
  const full = willPush ? `${prompt}\n\n(这次运行由 work-rhythm rhythm 发起:你最后写的那段总结会被它推送给用户,你自己不要再发。)` : prompt;
224
- const agent = createAgent({ root, agent: env.YZJ_BOT_AGENT, mode: cfg.agentMode, timeoutS: cfg.taskTimeout, log, env });
227
+ const agent = makeAgent();
225
228
  const t0 = Date.now();
226
229
  const r = agent.run(full, { stateDir: STATE_DIR });
227
230
  const seconds = Math.round((Date.now() - t0) / 1000);
@@ -254,6 +257,11 @@ function createRhythm({ root, env, packageRoot }) {
254
257
  return r.ok ? EXIT.OK : EXIT.FAILED;
255
258
  }
256
259
 
260
+ // 跑任务的 agent:绑定的那个(YZJ_BOT_AGENT),模型和强度先看 profile 的 rhythm.agent-model / agent-effort,没配再看 local.env
261
+ function makeAgent() {
262
+ return createAgent({ root, agent: env.YZJ_BOT_AGENT, mode: cfg.agentMode, timeoutS: cfg.taskTimeout, log, env, model: cfg.agentModel || (env.YZJ_BOT_MODEL || '').trim(), effort: cfg.agentEffort || (env.YZJ_BOT_EFFORT || '').trim() });
263
+ }
264
+
257
265
  // 调度器用它:任务另起一个子进程跑(带着 --push),不堵住计时
258
266
  function startJob(job) {
259
267
  fs.mkdirSync(STATE_DIR, { recursive: true });
@@ -379,7 +387,7 @@ function createRhythm({ root, env, packageRoot }) {
379
387
  if (!(cfg.port >= 1024 && cfg.port <= 65535)) problems.push(`rhythm.port 要是 1024 到 65535 之间的整数(现在是 ${cfg.port})`);
380
388
  // 只有真有任务要跑时,agent 和推送才必须配好;只采集、只看页面的用不着
381
389
  if (jobs().some((job) => job.kind === 'task' && job.hasPrompt)) {
382
- problems.push(...createAgent({ root, agent: env.YZJ_BOT_AGENT, mode: cfg.agentMode, env }).doctor());
390
+ problems.push(...makeAgent().doctor());
383
391
  if (cfg.push && !cfg.notifyChannel) problems.push('rhythm.push 开着,但 preferences.notify 没配消息渠道:要么配一个,要么把 push 写成 false');
384
392
  }
385
393
  return problems;
@@ -89,7 +89,7 @@ test('agent bind:不给名字就绑正在调的这个;没标记就绑装了
89
89
  assert.equal(exists(`${s.box.work}/collectors/local.env`), false);
90
90
  const r = s.run(['agent', 'bind'], { ...env, DSH_SESSION_ID: 'sess-1' });
91
91
  assert.equal(r.code, 0, r.out);
92
- assert.match(r.stdout, /已绑定 dsh(DSH(DeepSeek Harness)的 headless 模式),写进了 collectors\/local\.env 的 YZJ_BOT_AGENT。\n下一步:work-rhythm agent test/);
92
+ assert.match(r.stdout, /已绑定 dsh(DSH(DeepSeek Harness)的 headless 模式),写进了 collectors\/local\.env 的 YZJ_BOT_AGENT。\n 模型用它自己的默认,思考强度用它自己的默认\n 模型和思考强度还没配:work-rhythm agent models dsh 看能用哪些[\s\S]*\n下一步:work-rhythm agent test/);
93
93
  // local.env 原来没有:从样例复制一份,再把样例里注释掉的那行换成真的
94
94
  const text = read(`${s.box.work}/collectors/local.env`);
95
95
  assert.match(text, /^YZJ_BOT_AGENT="dsh"$/m);
@@ -106,7 +106,8 @@ test('agent bind:不给名字就绑正在调的这个;没标记就绑装了
106
106
  assert.equal(s.run(['agent', 'bind', custom], env).code, 0);
107
107
  assert.match(read(`${s.box.work}/collectors/local.env`), /^YZJ_BOT_AGENT='.*\/fake-agent --flag'$/m);
108
108
  const shown = s.run(['agent'], env);
109
- assert.match(shown.stdout, /现在绑定的:.*\/fake-agent --flag(一条自定义命令,zcode\/dsh\/qoder\/claude 以外)/);
109
+ assert.match(shown.stdout, /现在绑定的:.*\/fake-agent --flag(一条自定义命令)\n/);
110
+ assert.equal(shown.stdout.includes('模型用它自己的默认'), false, '自定义命令不谈模型和强度');
110
111
  assert.equal(shown.stdout.includes('建议:'), false, '绑了就不再建议');
111
112
  });
112
113
 
@@ -136,13 +137,13 @@ test('agent test:让绑定的 agent 只读答一句;没绑就退出码 3', (
136
137
  assert.equal(s.run(['agent', 'bind', 'qoder'], env).code, 0);
137
138
  const r = s.run(['agent', 'test', '今天周几'], env);
138
139
  assert.equal(r.code, 0, r.out);
139
- assert.match(r.stdout, /^回复:今天周几\n(qoder,只读模式,用时 \d+ 秒)\n$/);
140
+ assert.match(r.stdout, /^回复:今天周几\n(qoder;只读模式,用时 \d+ 秒)\n$/);
140
141
  // Qoder 的参数:工作目录、只读模式、JSON 输出、--print 后面跟这句话
141
142
  const args = read(s.stubs.STUB_QODER_ARGS);
142
143
  assert.equal(args, `--cwd ${fs.realpathSync(s.box.work)} --permission-mode plan --output-format json --print 今天周几\n`);
143
144
  // 默认那句话
144
145
  s.run(['agent', 'test'], env);
145
- assert.match(read(s.stubs.STUB_QODER_ARGS), /--print 你好,用一句话介绍你自己,并说出你用的是哪个模型。$/m);
146
+ assert.match(read(s.stubs.STUB_QODER_ARGS), /--print 你好。请只用两句话回答:1)你是哪个模型?说模型名。2)1\+1 等于几?$/m);
146
147
  assert.equal(s.run(['agent', 'test', 'a', 'b'], env).code, 2);
147
148
  });
148
149
 
@@ -172,7 +173,7 @@ test('claude 预置:在 PATH 上找到 claude,参数是 Claude Code 那一
172
173
  s.run(['agent', 'bind', 'claude'], s.bindEnv());
173
174
  const r = s.run(['agent', 'test', '在吗'], s.bindEnv());
174
175
  assert.equal(r.code, 0, r.out);
175
- assert.equal(r.stdout, '回复:在吗\n(claude,只读模式,用时 0 秒)\n');
176
+ assert.equal(r.stdout, '回复:在吗\n(claude;只读模式,用时 0 秒)\n');
176
177
  assert.equal(read(s.stubs.STUB_CLAUDE_ARGS).trim(), `--cwd ${fs.realpathSync(s.box.work)} --permission-mode plan --output-format json -p 在吗`);
177
178
  });
178
179
 
@@ -189,7 +190,9 @@ test('agent doctor:绑定的 agent 有没有问题;用法不对退出码为
189
190
  assert.equal(broken.code, 1);
190
191
  assert.match(broken.stdout, /找不到 DSH 的配置/);
191
192
  assert.equal(s.run(['agent', 'nope'], env).code, 2);
192
- assert.match(s.run(['agent', '--help'], env).stderr, /work-rhythm agent: 用法: work-rhythm agent \[子命令\]/);
193
+ const help = s.run(['agent', '--help'], env);
194
+ assert.equal(help.code, 0);
195
+ assert.match(help.stdout, /work-rhythm agent \[子命令\] —— 定时任务和机器人值守用哪个 agent/);
193
196
  });
194
197
 
195
198
  test('installedAgents:本机真有什么就报什么,不编造', () => {
@@ -197,3 +200,126 @@ test('installedAgents:本机真有什么就报什么,不编造', () => {
197
200
  assert.deepEqual(list.map((a) => a.name), ['zcode', 'dsh', 'qoder', 'claude']);
198
201
  for (const a of list) assert.equal(typeof a.found, 'boolean');
199
202
  });
203
+
204
+ // ---------------------------------------------------------------- 模型和思考强度
205
+ const patchOf = (box) => read(`${box.work}/01-inbox/_bot/dsh-headless.patch.yml`);
206
+
207
+ test('agent bind --model --effort:写进 local.env;dsh 把它写进自动生成的覆盖文件,agent 真的按它跑', (t) => {
208
+ const s = setup(t);
209
+ const env = s.bindEnv();
210
+ const r = s.run(['agent', 'bind', 'dsh', '--model', 'opencode-go/glm-5.3-flash', '--effort', 'high'], env);
211
+ assert.equal(r.code, 0, r.out);
212
+ assert.match(r.stdout, /模型 opencode-go\/glm-5\.3-flash,思考强度 high/);
213
+ const local = read(`${s.box.work}/collectors/local.env`);
214
+ assert.match(local, /^YZJ_BOT_AGENT="dsh"$/m);
215
+ assert.match(local, /^YZJ_BOT_MODEL="opencode-go\/glm-5\.3-flash"$/m);
216
+ assert.match(local, /^YZJ_BOT_EFFORT="high"$/m);
217
+ // 样例里注释掉的那两行被换成真的,不留重复
218
+ assert.equal(local.split('\n').filter((l) => /^#?\s*YZJ_BOT_MODEL=/.test(l)).length, 1);
219
+ // 真跑一次(listen handle 要渠道和 openId):覆盖文件里 agent-default-model 换了,llm-pi-ai 原样带着(凭据在它那)
220
+ write(`${s.box.work}/00-config/profile.yaml`, read(`${s.box.work}/00-config/profile.yaml`).replace('notify: ""', 'notify: "yzj-robot"'));
221
+ const h = s.run(['listen', 'handle', '在吗'], { ...env, YZJ_MY_OPEN_ID: 'me-0001', YZJ_BOT_PREAMBLE: '' });
222
+ assert.equal(h.code, 0, h.out);
223
+ const patch = patchOf(s.box);
224
+ assert.match(patch, /- id: agent-default-model\n name: "@deepseek-ai\/dsh-agent-default-model"\n config:\n provider: opencode-go\n model: glm-5\.3-flash\n reasoningEffort: high\n/);
225
+ assert.match(patch, /- id: llm-pi-ai\n/);
226
+ assert.match(h.stderr, /agent 完成 \d+s dsh mode=plan model=opencode-go\/glm-5\.3-flash effort=high/);
227
+ // 只写模型 id 不写 provider:到它的清单里找;找不到就拒
228
+ assert.equal(s.run(['agent', 'model', 'no-such-model'], env).code, 3);
229
+ assert.match(s.run(['agent', 'model', 'no-such-model'], env).stderr, /DSH 的配置里没有 no-such-model 这个模型/);
230
+ const byId = s.run(['agent', 'model', 'deepseek-v4.1-flash'], env);
231
+ assert.equal(byId.code, 0, byId.out);
232
+ s.run(['listen', 'handle', '在吗'], { ...env, YZJ_MY_OPEN_ID: 'me-0001', YZJ_BOT_PREAMBLE: '' });
233
+ assert.match(patchOf(s.box), / provider: opencode-go\n model: deepseek-v4\.1-flash\n reasoningEffort: high\n/);
234
+ // effort none:覆盖文件里不写 reasoningEffort
235
+ s.run(['agent', 'effort', 'none'], env);
236
+ s.run(['listen', 'handle', '在吗'], { ...env, YZJ_MY_OPEN_ID: 'me-0001', YZJ_BOT_PREAMBLE: '' });
237
+ const none = patchOf(s.box);
238
+ assert.match(none, / model: deepseek-v4\.1-flash\n- id: llm-pi-ai/);
239
+ assert.ok(!none.includes('reasoningEffort'), 'effort none 时不写 reasoningEffort');
240
+ });
241
+
242
+ test('agent model / effort:只改那一项,别的不动;--clear 清掉;强度不在词表里退出码 2;没绑时退出码 3', (t) => {
243
+ const s = setup(t);
244
+ const env = s.bindEnv();
245
+ assert.equal(s.run(['agent', 'effort', 'low'], env).code, 3);
246
+ s.run(['agent', 'bind', 'qoder'], env);
247
+ const r = s.run(['agent', 'effort', 'max'], env);
248
+ assert.equal(r.code, 0, r.out);
249
+ assert.match(r.stdout, /思考强度已设为 max,写进了 collectors\/local\.env。\n 模型用它自己的默认,思考强度 max\n/);
250
+ assert.equal(s.run(['agent', 'effort', 'xhigh'], env).code, 2);
251
+ assert.match(s.run(['agent', 'effort', 'xhigh'], env).stderr, /思考强度写的是「xhigh」,只能是 none \/ low \/ medium \/ high \/ max/);
252
+ s.run(['agent', 'model', 'performance'], env);
253
+ const local = read(`${s.box.work}/collectors/local.env`);
254
+ assert.match(local, /^YZJ_BOT_AGENT="qoder"$/m);
255
+ assert.match(local, /^YZJ_BOT_MODEL="performance"$/m);
256
+ assert.match(local, /^YZJ_BOT_EFFORT="max"$/m);
257
+ // qoder / claude 的参数:--model 和 --reasoning-effort / --effort;effort none 不传
258
+ const test = s.run(['agent', 'test', '在吗'], env);
259
+ assert.equal(test.code, 0, test.out);
260
+ assert.match(test.stdout, /(qoder,配置的模型 performance,思考强度 max;只读模式/);
261
+ assert.ok(read(s.stubs.STUB_QODER_ARGS).trim().startsWith(`--cwd ${fs.realpathSync(s.box.work)} --permission-mode plan --model performance --reasoning-effort max --output-format json --print 在吗`), read(s.stubs.STUB_QODER_ARGS));
262
+ s.run(['agent', 'bind', 'claude'], env);
263
+ s.run(['agent', 'model', 'opus'], env);
264
+ s.run(['agent', 'effort', 'none'], env);
265
+ s.run(['agent', 'test', '在吗'], env);
266
+ assert.equal(read(s.stubs.STUB_CLAUDE_ARGS).trim(), `--cwd ${fs.realpathSync(s.box.work)} --permission-mode plan --model opus --output-format json -p 在吗`);
267
+ // 清掉
268
+ const clear = s.run(['agent', 'model', '--clear'], env);
269
+ assert.match(clear.stdout, /模型已清掉,用 claude 自己的默认。\n 模型用它自己的默认,思考强度 none\n/);
270
+ assert.match(read(`${s.box.work}/collectors/local.env`), /^YZJ_BOT_MODEL=""$/m);
271
+ s.run(['agent', 'effort', '--clear'], env);
272
+ assert.equal(s.run(['agent', 'doctor'], env).code, 0);
273
+ });
274
+
275
+ test('zcode 不支持指定模型和强度:绑的时候带上就拒绝,单独设也拒绝,都说清原因', (t) => {
276
+ const s = setup(t);
277
+ const env = s.bindEnv();
278
+ const r = s.run(['agent', 'bind', 'zcode', '--model', 'glm-5.3'], env);
279
+ assert.equal(r.code, 3);
280
+ assert.match(r.stderr, /绑不了 zcode:预置 zcode 不支持指定模型(ZCode 的命令行没有这个参数,用的是它自己的默认模型)/);
281
+ assert.equal(exists(`${s.box.work}/collectors/local.env`), false);
282
+ s.run(['agent', 'bind', 'zcode'], env);
283
+ const m = s.run(['agent', 'effort', 'high'], env);
284
+ assert.equal(m.code, 3);
285
+ assert.match(m.stderr, /zcode 不支持指定思考强度/);
286
+ assert.match(s.run(['agent', 'models', 'zcode'], env).stderr, /zcode 不支持指定模型/);
287
+ assert.match(s.run(['agent'], env).stdout, / zcode +装了[\s\S]*?模型和思考强度:不能配,用它自己的默认/);
288
+ });
289
+
290
+ test('agent models:dsh 从它的配置里列模型,标出当前默认和支持思考强度的;qoder 把 --list-models 的输出带出来', (t) => {
291
+ const s = setup(t);
292
+ const env = s.bindEnv();
293
+ const r = s.run(['agent', 'models', 'dsh'], env);
294
+ assert.equal(r.code, 0, r.out);
295
+ assert.equal(r.stdout, [
296
+ 'dsh 能用的模型:',
297
+ ' opencode-go/deepseek-v4.1-flash DeepSeek V4.1 Flash 当前默认',
298
+ ' opencode-go/glm-5.3-flash GLM-5.3-Flash 支持思考强度',
299
+ '',
300
+ ].join('\n'));
301
+ assert.equal(s.run(['agent', 'models'], env).code, 3, '没绑的时候不给名字就报');
302
+ s.run(['agent', 'bind', 'qoder'], env);
303
+ // 假的 qoder 不认 --list-models,会把最后一个参数回出来——列模型这条路在这里只验「调了它、把输出带出来」
304
+ const q = s.run(['agent', 'models'], env);
305
+ assert.equal(q.code, 0, q.out);
306
+ assert.match(q.stdout, /--list-models/);
307
+ assert.equal(s.run(['agent', 'models', 'a', 'b'], env).code, 2);
308
+ });
309
+
310
+ test('agent --json:带模型、强度,以及这个 agent 支不支持', (t) => {
311
+ const s = setup(t);
312
+ s.run(['agent', 'bind', 'dsh', '--effort', 'low'], s.bindEnv());
313
+ const doc = JSON.parse(s.run(['agent', '--json'], s.bindEnv()).stdout);
314
+ assert.equal(doc.bound, 'dsh');
315
+ assert.equal(doc.model, '');
316
+ assert.equal(doc.effort, 'low');
317
+ assert.equal(doc.modelSupported, true);
318
+ assert.equal(doc.effortSupported, true);
319
+ const zcode = doc.agents.find((a) => a.name === 'zcode');
320
+ assert.equal(zcode.found, true);
321
+ assert.equal(typeof zcode.command, 'string');
322
+ assert.equal(zcode.modelSupported, false);
323
+ assert.equal(zcode.effortSupported, false);
324
+ assert.equal(doc.agents.find((a) => a.name === 'dsh').modelSupported, true);
325
+ });
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+ const { test } = require('node:test');
3
+ const assert = require('node:assert/strict');
4
+ const fs = require('node:fs');
5
+ const path = require('node:path');
6
+ const { makeSandbox, run: rawRun, read, write, addDaysHelper } = require('./harness');
7
+ const { addDays, today } = require('../lib/run');
8
+
9
+ const T = '\t';
10
+ const DAY = (offset) => addDays(today(), offset);
11
+ // 一份每日文件:事件流前后有别的小节,表里有一行格子里带转义的竖线
12
+ function daily(dir, offset, rows, extra = '') {
13
+ const date = DAY(offset);
14
+ const table = rows.length ? `| 时间 | 类型 | 事项 | 来源 | 项目 |\n|------|------|------|------|------|\n${rows.join('\n')}\n` : '| 时间 | 类型 | 事项 | 来源 | 项目 |\n|------|------|------|------|------|\n';
15
+ write(path.join(dir, '02-daily', date.slice(0, 7), `${date}.md`), `# ${date}\n\n## 计划\n\n- **今日重点**:\n\n## 事件流\n\n${table}${extra}\n## 备注\n\n| 09:05 | 沟通 | 备注里的表不算 | x | x |\n\n## 事件流补充\n\n| 10:00 | 其他 | 别的小节里的也不算 | x | x |\n`);
16
+ }
17
+
18
+ test('events:把 7 天内的每日文件里的事件流按日期、时间列成一张表;别的小节里的表不算', (t) => {
19
+ const box = makeSandbox(t);
20
+ const dir = box.workdir();
21
+ daily(dir, 0, ['| 09:05 | 沟通 | 同步接口联调进度 | im-yzj | 导出 |', '| 08:30 | 编码 | 提交 3 次 | git-activity | 导出 |']);
22
+ daily(dir, -1, ['| 14:00 | 会议 | 周会(30min) | calendar-yzj | |']);
23
+ daily(dir, -6, ['| 10:00 | 文档 | 写接口文档 | file-activity | 导出 |']);
24
+ daily(dir, -7, ['| 10:00 | 文档 | 八天前的,不在 7 天里 | file-activity | 旧 |']);
25
+ const r = box.run(['events']);
26
+ assert.equal(r.code, 0, r.out);
27
+ assert.equal(r.stdout, [
28
+ `date${T}time${T}type${T}item${T}source${T}project`,
29
+ `${DAY(-6)}${T}10:00${T}文档${T}写接口文档${T}file-activity${T}导出`,
30
+ `${DAY(-1)}${T}14:00${T}会议${T}周会(30min)${T}calendar-yzj${T}`,
31
+ `${DAY(0)}${T}08:30${T}编码${T}提交 3 次${T}git-activity${T}导出`,
32
+ `${DAY(0)}${T}09:05${T}沟通${T}同步接口联调进度${T}im-yzj${T}导出`,
33
+ '',
34
+ ].join('\n'));
35
+ });
36
+
37
+ test('events:--today、--days、--from/--to 各定一个范围,两头都含', (t) => {
38
+ const box = makeSandbox(t);
39
+ const dir = box.workdir();
40
+ for (const offset of [0, -1, -2, -3]) daily(dir, offset, [`| 09:00 | 其他 | 第 ${offset} 天 | x | |`]);
41
+ const dates = (args) => box.run(['events', ...args]).stdout.trim().split('\n').slice(1).map((l) => l.split(T)[0]);
42
+ assert.deepEqual(dates(['--today']), [DAY(0)]);
43
+ assert.deepEqual(dates(['--days', '3']), [DAY(-2), DAY(-1), DAY(0)]);
44
+ assert.deepEqual(dates(['--from', DAY(-3), '--to', DAY(-2)]), [DAY(-3), DAY(-2)]);
45
+ assert.deepEqual(dates(['--from', DAY(-2)]), [DAY(-2), DAY(-1), DAY(0)]);
46
+ });
47
+
48
+ test('events:按类型、来源、项目筛,可以逗号分隔写多个;--json 每条一个对象', (t) => {
49
+ const box = makeSandbox(t);
50
+ const dir = box.workdir();
51
+ daily(dir, 0, [
52
+ '| 09:05 | 沟通 | 同步进度 | im-yzj | 导出 |',
53
+ '| 10:00 | 会议 | 需求评审 | calendar-yzj | |',
54
+ '| 13:45 | 沟通 | 发材料 | 口述 | demo-goal |',
55
+ ]);
56
+ const items = (args) => box.run(['events', '--today', ...args]).stdout.trim().split('\n').slice(1).map((l) => l.split(T)[3]);
57
+ assert.deepEqual(items(['--type', '沟通,会议']), ['同步进度', '需求评审', '发材料']);
58
+ assert.deepEqual(items(['--type', '会议']), ['需求评审']);
59
+ assert.deepEqual(items(['--source', '口述']), ['发材料']);
60
+ assert.deepEqual(items(['--project', 'demo-goal']), ['发材料']);
61
+ assert.deepEqual(items(['--project', '导出,demo-goal']), ['同步进度', '发材料']);
62
+ const json = JSON.parse(box.run(['events', '--today', '--json']).stdout);
63
+ assert.deepEqual(json[2], { date: DAY(0), time: '13:45', type: '沟通', item: '发材料', source: '口述', project: 'demo-goal' });
64
+ assert.equal(json.length, 3);
65
+ });
66
+
67
+ test('events:没有文件、没有事件流、空表都只有表头;看不懂的行跳过;格子里转义的竖线算一个格子', (t) => {
68
+ const box = makeSandbox(t);
69
+ const dir = box.workdir();
70
+ assert.equal(box.run(['events']).stdout, `date${T}time${T}type${T}item${T}source${T}project\n`);
71
+ const date = DAY(0);
72
+ write(path.join(dir, '02-daily', date.slice(0, 7), `${date}.md`), `# ${date}\n\n## 事件流\n\n| 时间 | 类型 | 事项 | 来源 | 项目 |\n|------|------|------|------|------|\n这行不是表格\n| 10:00 | 文档 | 带 \\| 竖线的事项 | file-activity | |\n| 没有时间的行 |\n\n## 备注\n`);
73
+ assert.equal(box.run(['events', '--today']).stdout, `date${T}time${T}type${T}item${T}source${T}project\n${date}${T}10:00${T}文档${T}带 | 竖线的事项${T}file-activity${T}\n`);
74
+ // 事项里混进制表符时换成空格,表不会串列
75
+ write(path.join(dir, '02-daily', date.slice(0, 7), `${date}.md`), `# ${date}\n\n## 事件流\n\n| 时间 | 类型 | 事项 | 来源 | 项目 |\n|---|---|---|---|---|\n| 11:00 | 其他 | a\tb | x | |\n`);
76
+ assert.equal(box.run(['events', '--today']).stdout.split('\n')[1].split(T).length, 6);
77
+ });
78
+
79
+ test('events:选项不对、日期不对、起始晚于结束时退出码为 2', (t) => {
80
+ const box = makeSandbox(t);
81
+ box.workdir();
82
+ assert.equal(box.run(['events', '--nope']).code, 2);
83
+ assert.equal(box.run(['events', '--days']).code, 2);
84
+ assert.equal(box.run(['events', '--days', '0']).code, 2);
85
+ assert.equal(box.run(['events', '--from', '不是日期']).code, 2);
86
+ assert.equal(box.run(['events', '--from', DAY(1), '--to', DAY(0)]).code, 2);
87
+ assert.match(box.run(['events', '--from', DAY(1), '--to', DAY(0)]).stderr, /起始日期 .* 晚于结束日期/);
88
+ assert.equal(rawRun(['events'], { cwd: box.root }).code, 3);
89
+ });
@@ -133,6 +133,14 @@ function makeSandbox(t) {
133
133
  ' providers:',
134
134
  ' opencode-go:',
135
135
  ' apiKeyEnv: OPENCODE_GO_API_KEY',
136
+ ' models:',
137
+ ' - id: deepseek-v4.1-flash',
138
+ ' name: DeepSeek V4.1 Flash',
139
+ ' - id: glm-5.3-flash',
140
+ ' name: GLM-5.3-Flash',
141
+ ' compat:',
142
+ ' supportsReasoningEffort: true',
143
+ ' thinkingFormat: zai',
136
144
  '',
137
145
  ].join('\n'));
138
146
  const file = (name) => path.join(root, name);