nx-kv 0.28.0 → 0.32.0

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.
Files changed (214) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/README.md +15 -1
  3. package/assets/kv-get/SKILL.md +3 -1
  4. package/assets/kv-get/references/localget.md +4 -3
  5. package/assets/kv-get/references/timegoal.md +2 -1
  6. package/assets/kv-get/references/wait-workflow.md +44 -14
  7. package/assets/kv-get/references/waitloop.md +6 -4
  8. package/bin/cli.mjs +18 -1
  9. package/package.json +7 -4
  10. package/src/core/atomic-file.js +45 -0
  11. package/src/core/attachmentdb.js +726 -0
  12. package/src/core/attachstore.js +123 -80
  13. package/src/core/config.js +83 -13
  14. package/src/core/db/bootstrap.js +424 -0
  15. package/src/core/db/driver.js +234 -0
  16. package/src/core/db/errors.js +144 -0
  17. package/src/core/db/import-json.js +1224 -0
  18. package/src/core/db/index.js +122 -0
  19. package/src/core/db/migrations/index.js +103 -0
  20. package/src/core/db/migrations/v1.sql +850 -0
  21. package/src/core/db/migrations/v2-data.js +149 -0
  22. package/src/core/db/migrations/v2.sql +89 -0
  23. package/src/core/db/migrator.js +203 -0
  24. package/src/core/db/quiet.js +19 -0
  25. package/src/core/db/seed-templates.js +265 -0
  26. package/src/core/db/settings.js +54 -0
  27. package/src/core/db/time.js +96 -0
  28. package/src/core/db/tx.js +188 -0
  29. package/src/core/db/verify.js +295 -0
  30. package/src/core/db/version.js +59 -0
  31. package/src/core/errors.js +7 -0
  32. package/src/core/instruction-template.js +129 -94
  33. package/src/core/keys.js +21 -20
  34. package/src/core/localatt.js +76 -57
  35. package/src/core/paths.js +39 -2
  36. package/src/core/topic-key.js +43 -0
  37. package/src/core/workflow/project.js +3 -2
  38. package/src/core/workflow/readiness.js +13 -6
  39. package/src/core/workflow/types.js +6 -5
  40. package/src/modules/db/index.js +54 -0
  41. package/src/modules/db/service.js +273 -0
  42. package/src/modules/docs/brief.js +11 -1
  43. package/src/modules/docs/index.js +292 -7
  44. package/src/modules/docs/knowledge.js +8 -1
  45. package/src/modules/docs/paths.js +2 -1
  46. package/src/modules/docs/refs.js +1 -1
  47. package/src/modules/docs/store.js +19 -1
  48. package/src/modules/file/index.js +20 -0
  49. package/src/modules/file/service.js +68 -0
  50. package/src/modules/localtask/deletion.js +14 -10
  51. package/src/modules/localtask/events.js +16 -7
  52. package/src/modules/localtask/index.js +161 -81
  53. package/src/modules/localtask/move-prims.js +68 -38
  54. package/src/modules/localtask/move.js +1 -1
  55. package/src/modules/localtask/panel-presence.js +5 -5
  56. package/src/modules/localtask/paths.js +16 -18
  57. package/src/modules/localtask/repo/goals.js +154 -0
  58. package/src/modules/localtask/repo/index.js +42 -0
  59. package/src/modules/localtask/repo/links.js +136 -0
  60. package/src/modules/localtask/repo/open.js +71 -0
  61. package/src/modules/localtask/repo/presence.js +117 -0
  62. package/src/modules/localtask/repo/rows.js +157 -0
  63. package/src/modules/localtask/repo/settings.js +384 -0
  64. package/src/modules/localtask/repo/snapshot.js +154 -0
  65. package/src/modules/localtask/repo/tasks.js +333 -0
  66. package/src/modules/localtask/repo/topics.js +293 -0
  67. package/src/modules/localtask/service.js +15 -0
  68. package/src/modules/localtask/store/index.js +8 -9
  69. package/src/modules/localtask/store/topic-adapter.js +8 -43
  70. package/src/modules/localtask/store-io.js +19 -161
  71. package/src/modules/localtask/task-lifecycle.js +216 -148
  72. package/src/modules/localtask/task-ops.js +195 -114
  73. package/src/modules/localtask/task-remove.js +151 -79
  74. package/src/modules/localtask/task-templates.js +41 -62
  75. package/src/modules/localtask/task-view.js +165 -151
  76. package/src/modules/localtask/topics.js +89 -80
  77. package/src/modules/localtask/waiters.js +6 -14
  78. package/src/modules/localtask/waiting.js +31 -44
  79. package/src/modules/repos/boards.js +44 -14
  80. package/src/modules/repos/index.js +28 -2
  81. package/src/modules/repos/keys.js +2 -1
  82. package/src/modules/repos/ref.js +6 -6
  83. package/src/modules/repos/repo/index.js +53 -0
  84. package/src/modules/repos/repo/open.js +84 -0
  85. package/src/modules/repos/repo/refs.js +165 -0
  86. package/src/modules/repos/repo/repos.js +177 -0
  87. package/src/modules/repos/repo/rows.js +174 -0
  88. package/src/modules/repos/service.js +375 -130
  89. package/src/modules/repos/transfer.js +43 -14
  90. package/src/modules/tasktool/attach.js +732 -86
  91. package/src/modules/tasktool/delivery-template.js +50 -64
  92. package/src/modules/tasktool/get.js +6 -27
  93. package/src/modules/tasktool/groupctx.js +5 -2
  94. package/src/modules/tasktool/plugins/timegoal-store.js +476 -363
  95. package/src/modules/tasktool/plugins/waitgoal-config.js +1 -1
  96. package/src/modules/tasktool/preview.js +28 -34
  97. package/src/modules/tasktool/system-blocks.js +78 -0
  98. package/src/modules/tasktool/wait.js +113 -125
  99. package/src/modules/templates/index.js +19 -4
  100. package/src/modules/templates/repo/index.js +16 -0
  101. package/src/modules/templates/repo/open.js +65 -0
  102. package/src/modules/templates/repo/templates.js +133 -0
  103. package/src/modules/templates/service.js +130 -119
  104. package/src/runtime/registry.js +2 -1
  105. package/src/shared/prompt-blocks.js +307 -0
  106. package/src/shared/repo-key.js +57 -0
  107. package/src/shared/task-status.js +105 -0
  108. package/src/web/frontend/App.jsx +78 -45
  109. package/src/web/frontend/components/Introduction/docs-modal.jsx +306 -0
  110. package/src/web/frontend/components/Introduction/index.jsx +23 -72
  111. package/src/web/frontend/components/Introduction/style.css +35 -28
  112. package/src/web/frontend/components/board/member-picker.jsx +13 -3
  113. package/src/web/frontend/components/button/index.jsx +91 -0
  114. package/src/web/frontend/components/button/style.css +45 -0
  115. package/src/web/frontend/components/doc/doc-head.jsx +110 -0
  116. package/src/web/frontend/components/layout/breadcrumb.jsx +24 -13
  117. package/src/web/frontend/components/layout/crumbs.js +22 -0
  118. package/src/web/frontend/components/layout/route-crumb.jsx +2 -2
  119. package/src/web/frontend/components/md/attach-area.jsx +307 -0
  120. package/src/web/frontend/components/md/attach-common.js +95 -0
  121. package/src/web/frontend/components/md/attach-modal.jsx +387 -0
  122. package/src/web/frontend/components/md/attach-picker.jsx +274 -0
  123. package/src/web/frontend/components/md/composer-launch.js +68 -0
  124. package/src/web/frontend/components/md/composer-panel.jsx +167 -0
  125. package/src/web/frontend/components/md/md-panes.jsx +337 -46
  126. package/src/web/frontend/components/md/sel-menu.jsx +37 -210
  127. package/src/web/frontend/components/md/task-chain.jsx +2 -5
  128. package/src/web/frontend/components/md/tool-bar.jsx +75 -40
  129. package/src/web/frontend/components/repos/desc-modal.jsx +1 -1
  130. package/src/web/frontend/components/repos/transfer-panel.jsx +4 -0
  131. package/src/web/frontend/components/sidebar/index.jsx +5 -16
  132. package/src/web/frontend/components/sidebar/panel.jsx +2 -0
  133. package/src/web/frontend/components/sidebar/task-children.jsx +11 -1
  134. package/src/web/frontend/modules/board/board-view.js +31 -13
  135. package/src/web/frontend/modules/md/attach-mention.js +48 -0
  136. package/src/web/frontend/modules/md/md-edit-ops.js +24 -0
  137. package/src/web/frontend/modules/md/md-parse.js +40 -10
  138. package/src/web/frontend/modules/md/style.css +283 -39
  139. package/src/web/frontend/modules/md/use-templates.js +3 -1
  140. package/src/web/frontend/modules/prompts/placeholders.js +0 -13
  141. package/src/web/frontend/modules/prompts/prompt-editor.jsx +12 -111
  142. package/src/web/frontend/modules/prompts/style.css +0 -14
  143. package/src/web/frontend/page/dag/index.jsx +3 -1
  144. package/src/web/frontend/page/dir/deleted-panel.jsx +63 -0
  145. package/src/web/frontend/page/dir/index.jsx +137 -4
  146. package/src/web/frontend/page/dir/landing.jsx +136 -8
  147. package/src/web/frontend/page/dir/task.jsx +153 -83
  148. package/src/web/frontend/page/doc/index.jsx +150 -19
  149. package/src/web/frontend/page/group/index.jsx +39 -2
  150. package/src/web/frontend/page/overview/index.jsx +11 -5
  151. package/src/web/frontend/page/refs/index.jsx +122 -0
  152. package/src/web/frontend/page/refs/style.css +41 -0
  153. package/src/web/frontend/page/repos/index.jsx +565 -161
  154. package/src/web/frontend/page/settings/index.jsx +133 -105
  155. package/src/web/frontend/page/settings/prompt-templates-section.jsx +622 -0
  156. package/src/web/frontend/page/settings/style.css +161 -12
  157. package/src/web/frontend/page/settings/task-templates-section.jsx +120 -0
  158. package/src/web/frontend/page/templates/index.jsx +8 -34
  159. package/src/web/frontend/panels.jsx +24 -0
  160. package/src/web/frontend/route-map.js +67 -0
  161. package/src/web/frontend/style-v2.css +45 -19
  162. package/src/web/frontend/style.css +443 -15
  163. package/src/web/frontend/workspace/areas.js +3 -0
  164. package/src/web/frontend/workspace.js +148 -17
  165. package/src/web/public/assets/Introduction-BcPrmROW.css +1 -0
  166. package/src/web/public/assets/Introduction-MAkTZvUX.js +1 -0
  167. package/src/web/public/assets/{attach-v8BxRJ7p.js → attach-Bw0necIf.js} +1 -1
  168. package/src/web/public/assets/{cloud-DMotpyNl.js → cloud-CG_hgVn9.js} +3 -3
  169. package/src/web/public/assets/composer-launch-q1uPzWGo.js +1 -0
  170. package/src/web/public/assets/composer-panel-S2d6A3fG.js +1 -0
  171. package/src/web/public/assets/dag-CwY66RrZ.js +1 -0
  172. package/src/web/public/assets/dir-B_ySdQT1.js +5 -0
  173. package/src/web/public/assets/group-6sWouRDt.js +1 -0
  174. package/src/web/public/assets/index-DKncYqtS.js +54 -0
  175. package/src/web/public/assets/index-DO7b1M3E.css +1 -0
  176. package/src/web/public/assets/{md-edit-ops-BIXUZ5BM.js → md-edit-ops-ZwnwtEax.js} +10 -10
  177. package/src/web/public/assets/md-history-BxbOQtPI.js +1 -0
  178. package/src/web/public/assets/md-panes-Cso2NHbG.js +7 -0
  179. package/src/web/public/assets/member-picker-BpB9ZpWx.js +1 -0
  180. package/src/web/public/assets/overview-Bo8ivqRU.js +1 -0
  181. package/src/web/public/assets/{progress-DRI-n9Gb.js → progress-DPkGjn0P.js} +3 -3
  182. package/src/web/public/assets/refs-CY4i7PoC.css +1 -0
  183. package/src/web/public/assets/refs-CskU6rI7.js +1 -0
  184. package/src/web/public/assets/repos-B_tskNGw.js +9 -0
  185. package/src/web/public/assets/route-crumb-BZ6ffDRj.js +1 -0
  186. package/src/web/public/assets/settings-BMTd_p9Y.js +7 -0
  187. package/src/web/public/assets/settings-qCi-niJC.css +1 -0
  188. package/src/web/public/assets/task-Cs-9RNQn.js +7 -0
  189. package/src/web/public/assets/task-status-Bgo2Nfiw.js +1 -0
  190. package/src/web/public/assets/template-editor-CRxfV218.js +1 -0
  191. package/src/web/public/assets/templates-Apc3JKXa.js +2 -0
  192. package/src/web/public/assets/{upload-F3aoeT7X.js → upload-C83Bwy06.js} +1 -1
  193. package/src/web/public/index.html +4 -3
  194. package/src/core/json-store.js +0 -219
  195. package/src/web/frontend/modules/prompts/template-slots.jsx +0 -87
  196. package/src/web/frontend/page/settings/prompts-section.jsx +0 -531
  197. package/src/web/public/assets/Introduction-9gBNccF6.js +0 -1
  198. package/src/web/public/assets/Introduction-B4k08SH9.css +0 -1
  199. package/src/web/public/assets/dag-B9_B7rnK.js +0 -1
  200. package/src/web/public/assets/dir-DdMpgTHK.js +0 -3
  201. package/src/web/public/assets/group-BF2tfUqG.js +0 -1
  202. package/src/web/public/assets/index-BbAo8n1E.js +0 -50
  203. package/src/web/public/assets/index-DUxHMjZA.css +0 -1
  204. package/src/web/public/assets/md-history-DunAB9Pp.js +0 -5
  205. package/src/web/public/assets/member-picker-CD66NFHl.js +0 -1
  206. package/src/web/public/assets/overview-_Xza9umy.js +0 -1
  207. package/src/web/public/assets/repos-BfbPRMJ0.js +0 -10
  208. package/src/web/public/assets/route-crumb-BHjAXhIY.js +0 -1
  209. package/src/web/public/assets/settings-CVOnFmxp.js +0 -12
  210. package/src/web/public/assets/settings-knWtG9Kc.css +0 -1
  211. package/src/web/public/assets/task-Nw-0PEB4.js +0 -7
  212. package/src/web/public/assets/template-slots-UVa4FnPn.css +0 -1
  213. package/src/web/public/assets/template-slots-bXUEg7EO.js +0 -1
  214. package/src/web/public/assets/templates-cWbR2cCh.js +0 -2
@@ -15,7 +15,7 @@
15
15
  import { badInput } from '../../../core/errors.js';
16
16
 
17
17
  export const DEFAULT_GET_LOOP_TEMPLATE =
18
- '{{#task}}不要直接结束本轮:完成上面的任务({{task.id}})之后,继续调用 nx-kv localtask get(带 --summary 当前现状)领取下一条任务,直到 get 返回 timeout 为止。{{/task}}';
18
+ '{{#if task}}不要直接结束本轮:完成上面的任务({{task.id}})之后,继续调用 nx-kv localtask get(带 --summary 当前现状)领取下一条任务。队列里有活会立即返回;没活时每次最多阻塞 {{getloop.waitMinutes}} 分钟,直到 get 返回 timeout 为止。{{/if}}';
19
19
 
20
20
  export const GET_LOOP_DEFAULT_MINUTES = 10; // 阻塞窗口默认 10 分钟(上限仍受 wait 的 600s 夹制)
21
21
 
@@ -10,11 +10,11 @@
10
10
  // 2. **不复制渲染逻辑**:整条链路就是 buildTaskDelivery + getTask 的持续 get 叠加,
11
11
  // 与真实领取走同一份代码——复制一份「预览专用渲染」迟早与真实交付漂移,
12
12
  // 那种预览比没有更糟。
13
- // 3. **分段可追溯**:每段标明来源(交付上下文 / 收尾指令 / check 指令 / 附件 /
14
- // ack / 持续 get),并附带各项开关的当前状态——用户要能看出「这段是哪来的、
13
+ // 3. **分段可追溯**:每段标明来源(交付上下文 / check 指令 / 附件 / ack /
14
+ // 持续 get),并附带各项开关的当前状态——用户要能看出「这段是哪来的、
15
15
  // 想关该关哪个开关」。
16
- import { hasTemplateMarkers, renderInstruction } from '../../core/instruction-template.js';
17
16
  import { getSettings } from '../localtask/service.js';
17
+ import { BLOCK_DEFS, resolveBlockTemplates } from '../../shared/prompt-blocks.js';
18
18
  import { deliveryPlainText } from './delivery-text.js';
19
19
  import { DEFAULT_GET_LOOP_TEMPLATE, getLoopForCwd } from './getloop.js';
20
20
  import { buildTaskDelivery } from './wait.js';
@@ -33,7 +33,11 @@ import { buildTaskDelivery } from './wait.js';
33
33
  export async function previewTaskDelivery({ task, cwd, key, summary, params } = {}) {
34
34
  const settings = (await getSettings().catch(() => null)) || {};
35
35
  const segments = [];
36
- const delivery = await buildTaskDelivery(task, settings.taskSuffix, {
36
+ // 项目级「持续 get」模板:与 tasktool/get.js 同口径(只在开启时传入)。
37
+ // 统一交给 buildTaskDelivery 编排成 getloop 区块,预览与真实领取不会各拼一份。
38
+ const loop = await getLoopForCwd(cwd).catch(() => ({ enabled: false }));
39
+ const getLoopTpl = loop.enabled ? loop.template || DEFAULT_GET_LOOP_TEMPLATE : '';
40
+ const delivery = await buildTaskDelivery(task, {
37
41
  source: 'local',
38
42
  summary: summary || '',
39
43
  cwd,
@@ -42,38 +46,15 @@ export async function previewTaskDelivery({ task, cwd, key, summary, params } =
42
46
  params: params || {},
43
47
  preview: true,
44
48
  trace: segments,
49
+ getLoopTpl,
50
+ getLoopWaitMinutes: loop.enabled ? loop.waitMinutes || 0 : 0,
45
51
  });
46
52
 
47
53
  const previewTask = delivery.task || task;
48
- let instruction = delivery.instruction || '';
49
-
50
- // 项目级「持续 get」模板:与 tasktool/get.js 的叠加口径一致(只在开启时出现)
51
- const loop = await getLoopForCwd(cwd).catch(() => ({ enabled: false }));
52
- let getLoopText = '';
53
- if (loop.enabled) {
54
- const tpl = loop.template || DEFAULT_GET_LOOP_TEMPLATE;
55
- try {
56
- if (hasTemplateMarkers(tpl)) {
57
- getLoopText = renderInstruction(tpl, {
58
- task: previewTask ? { id: previewTask.id, text: String(previewTask.text || '') } : null,
59
- source: 'local',
60
- summary: summary || '',
61
- cwd: cwd || '',
62
- name: '',
63
- goal: null,
64
- });
65
- }
66
- } catch {
67
- getLoopText = '';
68
- }
69
- }
70
- if (getLoopText) {
71
- instruction = instruction ? `${instruction}\n\n${getLoopText}` : getLoopText;
72
- segments.push({ id: 'getloop', label: '持续 get 模板(项目级)', text: getLoopText });
73
- }
54
+ const instruction = delivery.instruction || '';
74
55
 
75
- const suffixList = settings.taskSuffixList || [];
76
56
  const ackList = settings.ackPromptList || [];
57
+ const active = resolveBlockTemplates(settings.blockTemplates);
77
58
  return {
78
59
  task: previewTask,
79
60
  instruction,
@@ -87,10 +68,23 @@ export async function previewTaskDelivery({ task, cwd, key, summary, params } =
87
68
  customTemplate: !!(loop.template && loop.template.trim()),
88
69
  },
89
70
  switches: {
90
- suffix: { on: suffixList.length > 0, count: suffixList.length },
91
71
  ack: { on: ackList.length > 0, count: ackList.length },
92
- deliveryTemplate: { custom: !!(settings.deliveryTemplate || '').trim() },
93
- attachTemplate: { custom: !!(settings.attachTemplate || '').trim() },
72
+ // 兼容旧预览面板:这两个开关表示对应区块当前是否用了自定义模板
73
+ deliveryTemplate: { custom: !active.context.builtin, name: active.context.name },
74
+ attachTemplate: { custom: !active.attachment.builtin, name: active.attachment.name },
75
+ appendMode: settings.appendMode || 'xml',
76
+ // 每个区块各自选中的生效模板(内置 / 自定义)
77
+ blockTemplates: Object.fromEntries(
78
+ BLOCK_DEFS.map((d) => [
79
+ d.id,
80
+ {
81
+ id: active[d.id].id,
82
+ name: active[d.id].name,
83
+ custom: !active[d.id].builtin,
84
+ builtin: active[d.id].builtin,
85
+ },
86
+ ])
87
+ ),
94
88
  attachments: delivery.attachmentsSync ? { synced: delivery.attachmentsSync } : null,
95
89
  },
96
90
  };
@@ -0,0 +1,78 @@
1
+ // tasktool/system-blocks.js —— 系统段落的渲染与薄编排。
2
+ //
3
+ // 模型(v5):所有区块共用**同一个 ctx**(repo / group / task / attach / goal / getloop…),
4
+ // 每个区块只渲染它当前生效的那一条模板(来自各自独立的模板列表,见 shared/prompt-blocks.js)。
5
+ // 这里只负责「按固定顺序渲染 → 包标签(xml)或空行拼接(plain)」,
6
+ // 不生产任何「预渲染正文」,也没有 block 内容包装的中间层。
7
+ //
8
+ // 失败行为(明确、可见、不静默):
9
+ // 某块模板语法出错或使用了已废弃的旧语法 → **该块回落到系统内置模板**,同时把告警
10
+ // 记进 `warnings`(由 wait 带进返回载荷的 renderWarnings,并在预览 trace 里标注)。
11
+ // 选择回落而不是阻断领取:一条坏模板不该让整个队列停摆;但问题必须能被看见
12
+ // ——保存时还会先编译校验(见 localtask/repo/settings.js),正常路径进不来坏模板。
13
+ import { renderInstruction, validateTemplate } from '../../core/instruction-template.js';
14
+ import { BLOCK_DEFS, wrapXmlBlocks } from '../../shared/prompt-blocks.js';
15
+
16
+ /** 渲染单条模板;旧语法 / 语法错误 → 抛出(由 composeSystemBlocks 回落内置模板并记告警)。 */
17
+ function renderOne(template, ctx) {
18
+ const src = String(template ?? '');
19
+ if (!src.trim()) return '';
20
+ const check = validateTemplate(src);
21
+ if (!check.ok) throw new Error(check.error);
22
+ return renderInstruction(src, ctx);
23
+ }
24
+
25
+ /**
26
+ * 组装系统上下文。
27
+ * @param {Array<{id,label,traceId}>} [segments] 段落元数据(只用于 trace 的段名映射)
28
+ * @param {object} [opts]
29
+ * @param {'xml'|'plain'} [opts.mode] 追加模式
30
+ * @param {object} [opts.templates] 每区块当前生效模板
31
+ * `{ [id]: { id, name, template, builtin } }`(见 shared/prompt-blocks.js 的 resolveBlockTemplates)
32
+ * @param {object} [opts.ctx] 共享渲染上下文(所有模板共用这一份)
33
+ * @param {Array} [opts.trace] 预览用分段记录
34
+ * @param {Array} [opts.warnings] 渲染告警收集器(`{ block, label, message }`)
35
+ * @returns {string} instruction 片段(无内容 → '')
36
+ */
37
+ export function composeSystemBlocks(segments, { mode = 'xml', templates, ctx = {}, trace, warnings } = {}) {
38
+ const segOf = new Map((segments || []).map((s) => [s.id, s]));
39
+ const parts = [];
40
+ for (const def of BLOCK_DEFS) {
41
+ const active = templates && templates[def.id];
42
+ const template = active && typeof active.template === 'string' ? active.template : def.defaultTemplate;
43
+ if (!String(template).trim()) continue; // 该块当前模板为空 = 不输出(如 rule 内置)
44
+
45
+ const seg = segOf.get(def.id);
46
+ let text = '';
47
+ let warning = null;
48
+ try {
49
+ text = renderOne(template, ctx).trim();
50
+ } catch (e) {
51
+ // 回落内置模板:系统内容不能因一条坏模板整块消失;同时把原因暴露出来。
52
+ warning = {
53
+ block: def.id,
54
+ label: def.label,
55
+ message: String((e && e.message) || e).replace(/\s+/g, ' ').trim(),
56
+ };
57
+ if (warnings) warnings.push(warning);
58
+ try {
59
+ text = renderOne(def.defaultTemplate, ctx).trim();
60
+ } catch {
61
+ text = '';
62
+ }
63
+ }
64
+ if (!text) continue;
65
+ parts.push({ id: def.id, tag: def.tag, text });
66
+ if (trace) {
67
+ trace.push({
68
+ id: seg?.traceId || def.id,
69
+ label: seg?.label || def.label,
70
+ text,
71
+ templateName: active?.name || '系统内置',
72
+ ...(warning ? { warning: warning.message, fellBack: true } : {}),
73
+ });
74
+ }
75
+ }
76
+ if (mode === 'plain') return parts.map((p) => p.text).join('\n\n').trim();
77
+ return wrapXmlBlocks(parts);
78
+ }
@@ -9,22 +9,24 @@
9
9
  // - 与队列的耦合全部走 localtask/service.js 的导出面;与计时目标的耦合走
10
10
  // 同模块的 timegoal.js(探针只读 + 交付时刻的目标上下文)。
11
11
  import { DEFAULT_PORT } from '../../core/paths.js';
12
- import { formatGoalTiming, hasTemplateMarkers, renderInstruction } from '../../core/instruction-template.js';
13
- import { DEFAULT_DELIVERY_TEMPLATE, buildDeliveryCtx, renderDelivery } from './delivery-template.js';
12
+ import { formatGoalTiming, renderInstruction } from '../../core/instruction-template.js';
13
+ import { buildDeliveryCtx } from './delivery-template.js';
14
+ import { parseFrontmatter } from '../../shared/frontmatter.js';
15
+ import {
16
+ BLOCK_DEFS,
17
+ resolveBlockTemplates,
18
+ } from '../../shared/prompt-blocks.js';
14
19
  import {
15
- clampTaskSuffix,
16
20
  clampTimeoutSec,
17
- joinTaskSuffix,
18
21
  clearWaiting,
19
22
  consumeCancelRequest,
20
23
  consumeFirstPending,
21
24
  getSettings,
22
25
  GLOBAL_KEY,
23
26
  isGlobalKey,
24
- joinAckPrompt,
25
- loadStore,
26
27
  normalizeCwd,
27
28
  normalizeSummary,
29
+ pendingQueueInfo,
28
30
  probeEffectivePresence,
29
31
  registerGlobalWaiter,
30
32
  registerTopicWaiter,
@@ -36,7 +38,8 @@ import {
36
38
  import { ensureMainPanel, deepLinkUrl, escortPanel } from './panel-escort.js';
37
39
  import { ensureTaskGoal, previewTaskGoal } from './taskgoal.js';
38
40
  import { readGroupContext } from './groupctx.js';
39
- import { annotateTaskDelivery, attachInstructionOf } from './attach.js';
41
+ import { annotateTaskDelivery, attachTemplateCtx } from './attach.js';
42
+ import { composeSystemBlocks } from './system-blocks.js';
40
43
  import * as timegoal from './timegoal.js';
41
44
 
42
45
  // 跨进程兜底轮询间隔(只定这里的读取节奏;队列写入节奏与它无关)
@@ -129,21 +132,10 @@ async function readGoalCtx(cwd) {
129
132
 
130
133
  // ─── 阻塞等待:核心入口 ─────────────────────────────────────────────
131
134
 
132
- // 把收尾追加语拼到返回给 agent 的 task.text 末尾(只改返回载荷,绝不回写库存原文)。
133
- // suffix 为空 → 原样返回;已包含同样追加语(幂等)→ 不重复追加。
134
- function withTaskSuffix(task, suffix) {
135
- const s = clampTaskSuffix(suffix);
136
- if (!task || !s) return task;
137
- const base = String(task.text || '');
138
- if (base.includes(s)) return task;
139
- return { ...task, text: `${base.replace(/\s+$/, '')}\n\n${s}` };
140
- }
141
-
142
- // 组装 wait/get 返回的任务载荷 + 收尾指令。分流(见 DEFAULT_TASK_SUFFIX 注释):
143
- // 纯文本(无 {{)→ v0.x 行为:拼到 task.text 末尾(幂等、不回写库存)、无新字段;
144
- // 模板(含 {{) → 渲染进返回载荷的 instruction 字段,task.text 保持库存原文
145
- // (模板原文若追加进正文会把 {{...}} 漏进任务文本);
146
- // ''(关闭) → 只回任务。
135
+ // 组装 wait/get 返回的任务载荷 + 指令。**单一路径**:
136
+ // 所有段落(上下文 / 附件 / 时间目标 / ack / 持续 get)一律进返回载荷的
137
+ // instruction 字段;task.text 始终是库存原文,绝不把系统提示词拼进正文。
138
+ // 旧 rule 区块已删除:done 指令与收尾要求统一由 ack 区块承载。
147
139
  // 任何异常都降级为「只回任务」——wait 绝不能因渲染挂掉(与探针同一条 fail-open 纪律)。
148
140
  //
149
141
  // preview / trace(任务 #65「预览 get 提示词」):
@@ -152,18 +144,14 @@ function withTaskSuffix(task, suffix) {
152
144
  // 完全同一条代码,所以预览出来的字符串就是 agent 会拿到的那一份。
153
145
  // trace=[] → 按顺序记下每一段的来源(谁贡献的、开关是否开着),供面板展示
154
146
  // 「这段是哪来的」。真实领取不传 trace,行为与改造前逐字节一致。
155
- export async function buildTaskDelivery(task, suffix, { source, summary, cwd, name, topicKey, params, preview = false, trace } = {}) {
156
- // 多条收尾指令(settings.taskSuffix 存 string[])先按空行拼成单串再分流;
157
- // 兼容旧单条 string(joinTaskSuffix 对 string 原样算一条)。
158
- const tpl = joinTaskSuffix(suffix);
147
+ export async function buildTaskDelivery(task, { source, summary, cwd, name, topicKey, params, preview = false, trace, getLoopTpl = '', getLoopWaitMinutes = 0 } = {}) {
159
148
  // 附件标注(任务 #15:正文里的 ![名](fileId) 嵌入 → 附件清单 + 本地路径)。
160
149
  // fail-open:标注失败绝不拖垮交付;正文没有引用时 annotateTaskDelivery 返回
161
150
  // null,载荷与改造前**逐字节一致**(与 todo 读任务的同一条纪律)。
162
- // 〔附件〕段本身等 settings 读到后再渲染(模板可配,任务 #82)——标注先行。
163
151
  let att = null;
164
152
  let attSync = null;
165
153
  try {
166
- const a = await annotateTaskDelivery(task, { sync: !preview });
154
+ const a = await annotateTaskDelivery(task, { sync: !preview, topicKey });
167
155
  if (a) {
168
156
  task = a.task;
169
157
  att = a;
@@ -172,10 +160,6 @@ export async function buildTaskDelivery(task, suffix, { source, summary, cwd, na
172
160
  } catch {
173
161
  /* 标注失败照常交付(agent 还能从正文里的嵌入自行处理) */
174
162
  }
175
- // 段落来源记录(预览用):trace 为 undefined 时零开销、零行为变化
176
- const mark = (id, label, text) => {
177
- if (trace && text) trace.push({ id, label, text });
178
- };
179
163
  try {
180
164
  // 任务级 timegoal(发布带 --duration):**领取即计时**——这里起表(幂等),
181
165
  // goal 上下文换成任务级(prompt = check 指令,固化进 timegoal 存储,之后每轮原文回显)。
@@ -200,102 +184,99 @@ export async function buildTaskDelivery(task, suffix, { source, summary, cwd, na
200
184
  }
201
185
  const goal = enrichGoalTiming(taskGoalCtx || (await readGoalCtx(cwd)));
202
186
 
203
- // 项目组上下文(组概念 + 本项目位置 + 兄弟成员 + 参考文件地址):
204
- // 只进 instruction,**task.text 保持库存原文**——todo 的口径是
205
- // 「封装对应的提示词保持在原本的提示此任务下面对上下文环境进行一个封装」。
206
- //
207
- // **模板化**(任务 #62):这段封装是一份模板 + 占位符(默认模板复现旧口径,
208
- // 设置里可改,面板「提示词 / 模板」可见);外部参数(--param k=v)平铺进
209
- // ctx,模板里 {{k}} 直接取——引擎支持平面扩展。
210
- const groupCtx = await readGroupContext(cwd);
211
- const settings = await getSettings().catch(() => null);
212
- // 〔附件〕段:配置了模板(settings.attachTemplate)按模板渲染(ctx 见设置页
213
- // 「变量字典」),空 = 内置默认(历史输出逐字节不变)。
214
- const attSeg = att
215
- ? attachInstructionOf(settings && settings.attachTemplate, att.task.attachments, att.sync, att.task)
216
- : '';
217
- const deliveryTpl = (settings && settings.deliveryTemplate) || DEFAULT_DELIVERY_TEMPLATE;
218
- const groupText = renderDelivery(
219
- deliveryTpl,
220
- buildDeliveryCtx({ task, goal, source, summary, cwd, name, groupCtx, params }),
221
- );
187
+ // 项目组上下文(组概念 + 本项目位置 + 兄弟成员 + 参考文件地址):只进 instruction。
188
+ // #64 作用域:组材料只挂**组任务**(正文 frontmatter 带 board);单 repo 主题里
189
+ // 提交的纯项目任务只带项目介绍,不把所属组的材料挂进来。
190
+ const boardMark = String(parseFrontmatter(String((task && task.text) || '')).board || '').trim();
191
+ const groupCtx = await readGroupContext(cwd, { board: boardMark });
192
+ const settings = (await getSettings().catch(() => null)) || {};
193
+ const deliveryCtx = buildDeliveryCtx({ task, goal, source, summary, cwd, name, groupCtx, params });
222
194
 
223
- // 〔ack 完成总结格式要求〕:settings.ackPrompt(多条,设置面板「提示词 / 模板」
224
- // 一栏可配)——agent 领取时就拿到「完成总结该按什么格式写」的提示词。
225
- // 缺省为空 → 不追加任何东西(默认交付逐字不变);渲染失败也不拖垮交付。
226
- let ackSeg = '';
227
- try {
228
- const ackTpl = joinAckPrompt((await getSettings()).ackPromptList);
229
- if (ackTpl) {
230
- const text = renderInstruction(ackTpl, {
231
- goal,
232
- task: task ? { id: task.id, text: String(task.text || '') } : null,
233
- source: source || '',
234
- summary: summary || '',
235
- cwd: cwd || '',
236
- name: name || '',
195
+ // ── 区块 v3:先备好**一份共享 ctx**,所有区块模板都从这份数据取值 ──
196
+ // 任务级 goal → 完整 check 指令;目录级 goal → 状态行(二者都进 timecheck 区块)。
197
+ const goalCheck = taskGoalCtx ? taskGoalCtx.prompt : null;
198
+ const goalStatus = !taskGoalCtx && goal
199
+ ? `〔时间目标「${goal.name || ''}」${goal.remainingHuman || ''}〕`
200
+ : null;
201
+ const attachCtx = att ? attachTemplateCtx(att.task.attachments, att.sync) : { attach: null };
202
+ const baseCtx = {
203
+ ...deliveryCtx,
204
+ // 共享 ctx 里的 goal 同时暴露 check / statusLine,模板不必区分两种来源。
205
+ goal: goal
206
+ ? { ...goal, check: goalCheck || '', statusLine: goalStatus || '' }
207
+ : null,
208
+ ...attachCtx,
209
+ task: task ? { id: task.id, text: String(task.text || '') } : null,
210
+ // 「持续 get」指令模板自身也能用 {{getloop.waitMinutes}}:先把阻塞窗口放进
211
+ // 同一份 ctx,外层 getloop 区块再补 text(同一个对象口径,前后不分裂)。
212
+ getloop: { text: '', waitMinutes: Number(getLoopWaitMinutes) || 0 },
213
+ };
214
+ // 持续 get 模板本身也是模板(可含 {{task.id}} 等),先在变量上下文里渲染成文本,
215
+ // 再由 getloop 区块的 `{{getloop.text}}` 输出。渲染失败同样记告警(不静默)。
216
+ const renderWarnings = [];
217
+ let loopText = '';
218
+ if (getLoopTpl) {
219
+ try {
220
+ loopText = renderInstruction(getLoopTpl, baseCtx) || '';
221
+ } catch (e) {
222
+ renderWarnings.push({
223
+ block: 'getloop',
224
+ label: '持续 get 指令(项目级)',
225
+ message: String((e && e.message) || e).replace(/\s+/g, ' ').trim(),
237
226
  });
238
- if (text) ackSeg = text;
239
227
  }
240
- } catch {
241
- /* 读不到 / 渲染失败 → 当没配 */
242
228
  }
243
-
244
- const parts = [];
245
- if (groupText) parts.push(groupText);
246
- mark('delivery', '交付上下文模板(组 / 项目关键路径)', groupText);
247
- if (hasTemplateMarkers(tpl)) {
248
- const instruction = renderInstruction(tpl, {
249
- goal,
250
- task: task ? { id: task.id, text: String(task.text || '') } : null,
251
- source: source || '',
252
- summary: summary || '',
253
- cwd: cwd || '',
254
- name: name || '',
229
+ // ext(队列余量):同主题还有多少待领取任务 + 标题,提示优先级。
230
+ // 现算(同主题一次读取),不做缓存;空队列 → null,整段不渲染。
231
+ // 读取失败不阻断领取:按缺失处理,并把原因记进 renderWarnings。
232
+ let extCtx = null;
233
+ try {
234
+ const q = await pendingQueueInfo(topicKey, task && task.id);
235
+ if (q && q.count > 0) extCtx = { topic: q.topic, count: q.count, list: q.list };
236
+ } catch (e) {
237
+ renderWarnings.push({
238
+ block: 'ext',
239
+ label: '待领取队列(ext)',
240
+ message: String((e && e.message) || e).replace(/\s+/g, ' ').trim(),
255
241
  });
256
- if (instruction) parts.push(instruction);
257
- mark('suffix', '回调提示词(收尾指令,模板渲染)', instruction);
258
- } else if (!groupText) {
259
- // 纯文本后缀 + 无组上下文 → 老行为:追加到 task.text 末尾(幂等、不回写库存)
260
- const base = { task: withTaskSuffix(task, tpl) };
261
- // 任务级 goal 必须把 check 指令带到 agent 手里——即使交付模板被关/是纯文本,
262
- // 也要以独立 instruction 交付(这就是「提示词当中写上,让他进行一个 check」)。
263
- if (taskGoalCtx) base.instruction = taskGoalCtx.prompt;
264
- // 〔附件〕段同样必须到达 agent(它带本地路径/拉取方式),与 goal 段同一待遇
265
- if (attSeg) base.instruction = base.instruction ? `${base.instruction}\n\n${attSeg}` : attSeg;
266
- if (ackSeg) base.instruction = base.instruction ? `${base.instruction}\n\n${ackSeg}` : ackSeg;
267
- if (attSync) base.attachmentsSync = attSync;
268
- mark('suffix', '回调提示词(纯文本,追加在任务正文末尾)', tpl);
269
- mark('goal', '任务级 check 指令(时间目标)', taskGoalCtx ? taskGoalCtx.prompt : '');
270
- mark('attach', '附件段(本地路径 / 拉取方式)', attSeg);
271
- mark('ack', 'ack 完成总结提示词', ackSeg);
272
- return base;
273
- } else {
274
- parts.push(tpl); // 纯文本后缀 + 有组上下文 → 进 instruction,不污染 task.text
275
- mark('suffix', '回调提示词(纯文本)', tpl);
276
242
  }
277
- // 任务级 check 指令必须原样到达 agent(默认 suffix 模板不含 {{goal.prompt}},
278
- // 用户自定义模板也可能漏写)——任务级 goal 存在时始终以独立段落追加。
279
- if (taskGoalCtx) parts.push(taskGoalCtx.prompt);
280
- if (attSeg) parts.push(attSeg);
281
- if (ackSeg) parts.push(ackSeg);
282
- mark('goal', '任务级 check 指令(时间目标)', taskGoalCtx ? taskGoalCtx.prompt : '');
283
- mark('attach', '附件段(本地路径 / 拉取方式)', attSeg);
284
- mark('ack', 'ack 完成总结提示词', ackSeg);
243
+ const blockCtx = {
244
+ ...baseCtx,
245
+ getloop: loopText
246
+ ? { text: loopText, waitMinutes: Number(getLoopWaitMinutes) || 0 }
247
+ : null,
248
+ ext: extCtx,
249
+ };
285
250
 
286
- const full = parts.join('\n\n').trim();
287
- if (!full) return attSync ? { task, attachmentsSync: attSync } : { task };
288
- // coversBody = 模板是否包含 {{task.text}}(即 instruction 是否自包含任务正文)。
289
- // 由服务端判定而不是 CLI 端做子串猜测——短任务文本可能碰巧是指令的子串,
290
- // 子串启发会误吞正文块的显示。
291
- // **交付上下文模板也算**:它在正文之外另起一段,自带原文时同样覆盖正文块。
292
- const coversBody =
293
- (hasTemplateMarkers(tpl) && /\{\{\s*task\.text\s*\}\}/.test(tpl)) ||
294
- (hasTemplateMarkers(deliveryTpl) && /\{\{\s*task\.text\s*\}\}/.test(deliveryTpl));
251
+ const mode = settings.appendMode === 'plain' ? 'plain' : 'xml';
252
+ // 每个区块按各自列表中选中的生效模板渲染(内置常驻,找不到就回落内置)
253
+ const blockTemplates = resolveBlockTemplates(settings.blockTemplates);
254
+ const segments = [
255
+ { id: 'context', label: '组 / 项目上下文(模板)', traceId: 'delivery' },
256
+ { id: 'attachment', label: '附件段(清单 / 本地路径)', traceId: 'attach' },
257
+ { id: 'timecheck', label: '时间目标(状态 / check 指令)', traceId: 'goal' },
258
+ { id: 'ext', label: '待领取队列(ext)', traceId: 'ext' },
259
+ { id: 'ack', label: 'ack 完成总结提示词', traceId: 'ack' },
260
+ { id: 'getloop', label: '持续 get 指令(项目级)', traceId: 'getloop' },
261
+ ];
262
+ const full = composeSystemBlocks(segments, {
263
+ mode,
264
+ templates: blockTemplates,
265
+ ctx: blockCtx,
266
+ trace,
267
+ warnings: renderWarnings,
268
+ });
269
+ const warnField = renderWarnings.length ? { renderWarnings } : {};
270
+ if (!full) {
271
+ return { task, ...warnField, ...(attSync ? { attachmentsSync: attSync } : {}) };
272
+ }
273
+ // coversBody = 启用区块的任一模板含 {{task.text}}(instruction 自包含任务正文)。
274
+ // 由服务端判定,避免 CLI 端做子串猜测。
295
275
  return {
296
276
  task,
297
277
  instruction: full,
298
- coversBody,
278
+ coversBody: blocksCoverBody(blockTemplates),
279
+ ...warnField,
299
280
  ...(attSync ? { attachmentsSync: attSync } : {}),
300
281
  };
301
282
  } catch {
@@ -303,6 +284,11 @@ export async function buildTaskDelivery(task, suffix, { source, summary, cwd, na
303
284
  }
304
285
  }
305
286
 
287
+ /** 当前生效模板里是否有模板写了 {{task.text}}(instruction 自包含任务正文)。 */
288
+ function blocksCoverBody(templates) {
289
+ return BLOCK_DEFS.some((d) => /\{\{\s*task\.text\s*\}\}/.test(String(templates?.[d.id]?.template || '')));
290
+ }
291
+
306
292
  /**
307
293
  * 针对某主题(工作目录)等待用户回填下一条任务。
308
294
  * 领取顺序:本目录队列 → 全局主题(共享池)→ 阻塞(两个来源任一到达即唤醒)。
@@ -324,6 +310,8 @@ export async function waitForTask({
324
310
  onReady,
325
311
  params = {}, // 外部参数(平面扩展):进交付模板的 ctx(--param k=v)
326
312
  instant = false, // get 的「直接领」形态:没有 pending 立即空手而回,不开窗不阻塞
313
+ getLoopTpl = '', // 项目级「持续 get」模板(仅 get/preview 传;wait 不传 → 不出现该区块)
314
+ getLoopWaitMinutes = 0, // 项目级阻塞窗口(分钟):进 getloop 模板 ctx,让 agent 知道每轮会等多久
327
315
  } = {}) {
328
316
  const { key, cwd, name } = normalizeCwd(rawCwd);
329
317
  const summary = normalizeSummary(rawSummary); // 现状必填
@@ -335,18 +323,18 @@ export async function waitForTask({
335
323
  const repoCtx = repoCtxOf(repoIndex, cwd);
336
324
 
337
325
  // 超时:显式 --timeout(秒/毫秒)优先;否则用页面配置;统一夹到 1-600
338
- const startStore = await loadStore();
326
+ // T03:设置走 settings 表(repo/settings),不再有 JSON store 对象
327
+ const startSettings = await getSettings();
339
328
  let sec;
340
329
  if (timeoutSec !== undefined && timeoutSec !== null) sec = timeoutSec;
341
330
  else if (timeoutMs !== undefined && timeoutMs !== null) sec = Number(timeoutMs) / 1000;
342
- else sec = startStore.settings.timeoutSec;
331
+ else sec = startSettings.timeoutSec;
343
332
  const tmo = clampTimeoutSec(sec) * 1000;
344
- const suffix = startStore.settings.taskSuffix; // 返回任务时追加的收尾指令(本轮固定;多条列表,交付时拼接)
345
333
 
346
334
  // 1. 主题队列已有 pending → 立即取走,不开窗、不阻塞;记录本轮完成总结
347
335
  const queued = await consumeFirstPending(key, summary, by);
348
336
  if (queued) {
349
- const delivery = await buildTaskDelivery(queued, suffix, { source: 'local', summary, cwd, name, topicKey: key, params });
337
+ const delivery = await buildTaskDelivery(queued, { source: 'local', summary, cwd, name, topicKey: key, params, getLoopTpl, getLoopWaitMinutes });
350
338
  return { status: 'ok', key, cwd, name, summary, ...delivery, queued: true, source: 'local', repo: repoCtx };
351
339
  }
352
340
 
@@ -363,7 +351,7 @@ export async function waitForTask({
363
351
  if (fromGlobal) {
364
352
  // 全局池投递渲染的仍是**等待方目录**的 goal——目标绑定在等待中的 agent 目录上,
365
353
  // 与任务来自哪个队列无关。
366
- const delivery = await buildTaskDelivery(fromGlobal, suffix, { source: 'global', summary, cwd, name, topicKey: key, params });
354
+ const delivery = await buildTaskDelivery(fromGlobal, { source: 'global', summary, cwd, name, topicKey: key, params, getLoopTpl, getLoopWaitMinutes });
367
355
  return {
368
356
  status: 'ok',
369
357
  key,
@@ -477,11 +465,11 @@ export async function waitForTask({
477
465
  let result;
478
466
  try {
479
467
  const r = await outcome;
480
- // 收尾指令在**返回时刻**组装(此刻的 goal 状态才是真实状态;阻塞期间到点会渲染出已到点)。
468
+ // 交付指令在**返回时刻**组装(此刻的 goal 状态才是真实状态;阻塞期间到点会渲染出已到点)。
481
469
  // 只对拿到任务的结果生效——timeout/goal_expired/cancelled 的返回形状是钉死的契约,不动。
482
470
  const delivery =
483
471
  r.kind === 'task'
484
- ? await buildTaskDelivery(r.task, suffix, { source: r.source || 'local', summary, cwd, name, topicKey: key, params })
472
+ ? await buildTaskDelivery(r.task, { source: r.source || 'local', summary, cwd, name, topicKey: key, params, getLoopTpl, getLoopWaitMinutes })
485
473
  : null;
486
474
  result =
487
475
  r.kind === 'timeout'
@@ -74,7 +74,7 @@ export default {
74
74
  id: 'templates.update',
75
75
  cli: ['template', 'update'],
76
76
  http: ['PATCH', '/api/templates/:id'],
77
- summary: '编辑任务模板(--name 改名 / --body 改正文,只改传入项;多行正文用 --body-file;内置模板不可编辑)',
77
+ summary: '编辑任务模板(--name 改名 / --body 改正文,只改传入项;多行正文用 --body-file;内置模板的正文也可改)',
78
78
  args: ['id'],
79
79
  flags: {
80
80
  name: { type: 'string', hint: '新名字(不传保持不变)' },
@@ -83,7 +83,7 @@ export default {
83
83
  'body-file': { type: 'string', hint: '同 --bodyFile(连字符写法)' },
84
84
  values: {
85
85
  type: 'string',
86
- hint: '槽位值 JSON(形如 {"背景":"要做的事"})——正文里的 {{槽位名}} 处填这段文字;内置模板只能改这个(骨架不可改)',
86
+ hint: '槽位值 JSON(形如 {"背景":"要做的事"})——老模板正文里的 {{槽位名}} 处填这段文字(新模板直接写正文即可)',
87
87
  },
88
88
  },
89
89
  run: (ctx, opts) =>
@@ -109,9 +109,24 @@ export default {
109
109
  id: 'templates.default',
110
110
  cli: ['template', 'default'],
111
111
  http: ['PATCH', '/api/templates/default'],
112
- summary: '设置默认模板(新建页的起始正文换成它)',
112
+ // 这条路由同时管两件事——**字面量路由优先于 :id**(runtime/api.js 的路由排序:
113
+ // 字面量段在前),所以 id 恰是 'default' 的模板,正文更新会被这条路由截走:
114
+ // 带 name/body/values → 改**默认模板本身**(id 就是 default)
115
+ // 只带 id → 把默认模板换成它
116
+ // 不分派的话,「默认模板」永远存不进去(面板里改默认模板 400,2026-11-07 任务 #87 实锤)。
117
+ summary: '设置默认模板(新建页的起始正文换成它);带 --body/--name 则改默认模板本身',
113
118
  args: ['id'],
114
- run: (ctx) => service.setDefaultTemplate({ id: ctx.id }),
119
+ run: (ctx) => {
120
+ if (ctx.body !== undefined || ctx.name !== undefined || ctx.values !== undefined) {
121
+ return service.updateTemplate({
122
+ id: ctx.id || 'default',
123
+ name: ctx.name,
124
+ body: ctx.body,
125
+ values: parseValues(ctx.values),
126
+ });
127
+ }
128
+ return service.setDefaultTemplate({ id: ctx.id });
129
+ },
115
130
  render: (d) => `默认模板已设为「${d.template.name}」(${d.template.id})`,
116
131
  },
117
132
  ],
@@ -0,0 +1,16 @@
1
+ // templates 仓储门面:模块内统一从这里拿 repo(不直接 import core/db)。
2
+ import { openTemplatesStore, resetTemplatesStore, db, txw, resolveTemplatesDb } from './open.js';
3
+ import * as templates from './templates.js';
4
+
5
+ export { openTemplatesStore, resetTemplatesStore, db, txw, resolveTemplatesDb, templates };
6
+ export { closeDb, isStoreOpen } from '../../../core/db/index.js';
7
+
8
+ /** 便捷聚合:`repo.templates.getDefaultRow('newtask')`。 */
9
+ export default {
10
+ open: openTemplatesStore,
11
+ reset: resetTemplatesStore,
12
+ db,
13
+ txw,
14
+ resolveTemplatesDb,
15
+ templates,
16
+ };