dsh-harmonyos-hiboard 0.7.2 → 0.8.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.
- package/README.md +22 -1
- package/package.json +1 -1
- package/src/lib/agents-rule.js +145 -0
- package/src/lib/client.js +41 -0
- package/src/lib/index.js +64 -4
package/README.md
CHANGED
|
@@ -230,6 +230,27 @@ L2 抢救"承诺了却没调用"、L3 按错误类别重试并自动补 `schedul
|
|
|
230
230
|
|
|
231
231
|
这个面板的意义是:以前"保险丝到底有没有干活"只能靠猜,现在直接列给你看。
|
|
232
232
|
|
|
233
|
+
4. **常驻推送规则**(默认开启)—— **保存授权码时,会把「每轮回答都要推送」自动写进
|
|
234
|
+
全局指令文件 `$DSH_HOME/AGENTS.md`**,这样「一键安装 + 粘一次授权码」之后就不必再
|
|
235
|
+
手改任何文件,新会话立刻按规则推送。
|
|
236
|
+
|
|
237
|
+
> ⚠️ **这是本插件唯一会改动你已有文件的行为,且明确声明在此。** 具体约束:
|
|
238
|
+
> - 规则被 **BEGIN/END 标记**包裹,插件**只改标记之间那一段**,标记之外一个字节都不动;
|
|
239
|
+
> - **幂等**:重复保存不会产生第二份规则;
|
|
240
|
+
> - **首次改动前自动备份** `AGENTS.md.bak-hiboard-<时间戳>`(只备份一次);
|
|
241
|
+
> - **检测到你已手写过推送规则就完全跳过**,并如实提示,而不是硬塞第二份;
|
|
242
|
+
> - 卡片上那颗开关可以**一键移除**该段(其余内容原样保留),关掉后再保存授权码也不会重新写入。
|
|
243
|
+
|
|
244
|
+
**为什么必须写这个文件**:dsh 的 `dsh-agent-instructions` 每个会话都加载
|
|
245
|
+
`$DSH_HOME/AGENTS.md`,而且**只认这一个全局文件** —— `.local` 叠加(`AGENTS.local.md`)
|
|
246
|
+
只在**项目目录**的祖先链里生效,全局那段代码写死了只探测 `AGENTS.md`。
|
|
247
|
+
(`agent/pre-step` 注入要拼宿主内部消息形状,在预览版上太脆,所以没有采用。)
|
|
248
|
+
|
|
249
|
+
> **授权码仍然必须手动粘一次**,这一点无法自动化:它是你在手机上
|
|
250
|
+
> 「负一屏 → 我的 → 动态管理 → 关联账号 → Claw 智能体」取到的**个人凭据**,
|
|
251
|
+
> 任何插件都不可能凭空得到它。所以准确说法是:安装后规则自动就位,
|
|
252
|
+
> 你只需**在卡片里粘一次授权码**。
|
|
253
|
+
|
|
233
254
|
配置写在 `<用户目录>/.dsh/hiboard-push/config.json`(0600,先写临时文件再原子替换)。
|
|
234
255
|
该文件属于当前系统用户,本机所有 dsh profile 共用一份。
|
|
235
256
|
|
|
@@ -388,7 +409,7 @@ npm test
|
|
|
388
409
|
|---|---|---|
|
|
389
410
|
| 1 | 装插件:`dsh plugin --profile web add dsh-harmonyos-hiboard` | 没有工具 |
|
|
390
411
|
| 2 | 配授权码(设置卡片,或 profile 补丁层) | 调用时报「未配置授权码」 |
|
|
391
|
-
| 3 |
|
|
412
|
+
| 3 | 保存授权码 —— 插件会把推送规则**自动**写进 `~/.dsh/AGENTS.md`(默认开启、可一键关掉) | 功能在,但**没人调用它** —— 每轮都不会推 |
|
|
392
413
|
| 4 | 重启 dsh | 新装/升级的插件不生效(升级不会热替换已挂载插件) |
|
|
393
414
|
|
|
394
415
|
第 3 步的现成模板见 **[`docs/AGENTS-rule.md`](docs/AGENTS-rule.md)**:dsh 由
|
package/package.json
CHANGED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 常驻推送规则:把「每轮回答都要推送」写进 dsh 的全局指令文件。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么必须动这个文件
|
|
5
|
+
*
|
|
6
|
+
* dsh 的 `dsh-agent-instructions` 在每个会话自动加载 `$DSH_HOME/AGENTS.md`。
|
|
7
|
+
* 它**只认这一个全局文件** —— `.local` 叠加(`AGENTS.local.md`)只在**项目目录**的
|
|
8
|
+
* 祖先链里生效,全局那段代码写死了只探测 `AGENTS.md`(见 dsh-agent-instructions
|
|
9
|
+
* 的 `discoverInstructionFiles`)。所以想让规则对所有会话、所有项目生效,
|
|
10
|
+
* 除了写这个文件没有第二条路(`agent/pre-step` 注入要拼宿主内部消息形状,预览版上太脆)。
|
|
11
|
+
*
|
|
12
|
+
* ## 安全约定(每一条都是必须的)
|
|
13
|
+
*
|
|
14
|
+
* 1. **只改自己那一段**:规则块由 BEGIN/END 标记包裹,标记之外一个字节都不碰。
|
|
15
|
+
* 2. **幂等**:标记已存在就原地替换;重复调用不会产生第二份规则。
|
|
16
|
+
* 3. **不覆盖用户手写的规则**:文件里已有等价规则(非标记块)时**直接跳过**,
|
|
17
|
+
* 并如实报告,而不是硬塞一份重复的。
|
|
18
|
+
* 4. **首次改动前备份**:`AGENTS.md.bak-hiboard-<时间戳>`,且只备份一次,不刷屏。
|
|
19
|
+
* 5. **可一键移除**:`removeRule()` 把标记块整段删掉,其余内容原样保留。
|
|
20
|
+
*/
|
|
21
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync, readdirSync } from "node:fs";
|
|
22
|
+
import { basename, dirname } from "node:path";
|
|
23
|
+
|
|
24
|
+
/** 规则块的起止标记。改这里等于改契约,不要动。 */
|
|
25
|
+
export const RULE_BEGIN = "<!-- BEGIN dsh-harmonyos-hiboard (managed block; the plugin rewrites this region) -->";
|
|
26
|
+
export const RULE_END = "<!-- END dsh-harmonyos-hiboard -->";
|
|
27
|
+
|
|
28
|
+
/** 规则块正文。中英并列:dsh 的全局文件对所有会话生效,读者可能是任意语言。 */
|
|
29
|
+
export function buildRuleBlock() {
|
|
30
|
+
return [
|
|
31
|
+
RULE_BEGIN,
|
|
32
|
+
"## 强制要求:每轮回答都要推送到手机负一屏",
|
|
33
|
+
"",
|
|
34
|
+
"1. 给出最终回答时,调用 `hiboard_push` 推送本轮的详细结论。",
|
|
35
|
+
"2. **必须传 `schedule_id`**(例如 `dsh_worklog`)—— 它为空时卡片只有一行标题、正文不渲染、也点不开。",
|
|
36
|
+
"3. `content` 用 Markdown,**上限 5000 字符**;超长就推精简版,并在开头注明被截断,完整内容留在对话里。",
|
|
37
|
+
"4. `result` 是状态标签,默认「任务已完成」。",
|
|
38
|
+
"5. 本轮确实没有实质输出时,也推一张简短卡片,不要跳过。",
|
|
39
|
+
"6. 推送失败要写明原因(未配置授权码 / 接口不通),不要静默略过。",
|
|
40
|
+
"",
|
|
41
|
+
"> Every turn must push: call `hiboard_push` with a non-empty `schedule_id`; Markdown body is",
|
|
42
|
+
"> capped at 5000 characters; push a short card even on a thin turn; never fail silently.",
|
|
43
|
+
"",
|
|
44
|
+
"(本段由 dsh-harmonyos-hiboard 维护,可在插件的设置卡片里一键关闭与移除。)",
|
|
45
|
+
RULE_END
|
|
46
|
+
].join("\n");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 判定文件里是否已存在「等价规则」(非本插件标记块)。 */
|
|
50
|
+
export function looksLikePushRule(text) {
|
|
51
|
+
if (!/hiboard_push/.test(text)) return false;
|
|
52
|
+
return /(每轮|每一轮|每次|每一条|必须)[^\n]{0,24}(推|发送|推送)/.test(text) || /every\s+(turn|reply|response)/i.test(text);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function readText(file) {
|
|
56
|
+
try {
|
|
57
|
+
return readFileSync(file, "utf8");
|
|
58
|
+
} catch {
|
|
59
|
+
return "";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function hasBackup(file) {
|
|
64
|
+
try {
|
|
65
|
+
return readdirSync(dirname(file)).some((name) => name.startsWith(`${basename(file)}.bak-hiboard-`));
|
|
66
|
+
} catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 原子写入;`backup` 为真且尚无备份时,先留一份原件。 */
|
|
72
|
+
function writeText(file, text, { backup = false } = {}) {
|
|
73
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
74
|
+
if (backup && existsSync(file) && !hasBackup(file)) {
|
|
75
|
+
try {
|
|
76
|
+
copyFileSync(file, `${file}.bak-hiboard-${new Date().toISOString().replace(/[-:T]/g, "").slice(0, 15)}`);
|
|
77
|
+
} catch {
|
|
78
|
+
/* 备份失败不阻止写入:用户要的是规则生效,不是备份成功 */
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
82
|
+
writeFileSync(tmp, text, "utf8");
|
|
83
|
+
renameSync(tmp, file);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 用新块替换旧块(保留前后一切内容)。 */
|
|
87
|
+
function replaceBlock(text, block) {
|
|
88
|
+
const start = text.indexOf(RULE_BEGIN);
|
|
89
|
+
const end = text.indexOf(RULE_END);
|
|
90
|
+
if (start < 0 || end < 0 || end < start) return text;
|
|
91
|
+
return `${text.slice(0, start)}${block}${text.slice(end + RULE_END.length)}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 状态:文件在哪、有没有我们的块、有没有用户自己写的等价规则。 */
|
|
95
|
+
export function ruleStatus(file) {
|
|
96
|
+
const text = readText(file);
|
|
97
|
+
const hasBlock = text.includes(RULE_BEGIN) && text.includes(RULE_END);
|
|
98
|
+
return {
|
|
99
|
+
file,
|
|
100
|
+
exists: existsSync(file),
|
|
101
|
+
block: hasBlock,
|
|
102
|
+
customRule: !hasBlock && looksLikePushRule(text)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 确保规则就位(幂等)。
|
|
108
|
+
* @returns {{action: 'added'|'updated'|'unchanged'|'skipped-custom', status: object}}
|
|
109
|
+
*/
|
|
110
|
+
export function ensureRule(file) {
|
|
111
|
+
const block = buildRuleBlock();
|
|
112
|
+
const before = readText(file);
|
|
113
|
+
const hasBlock = before.includes(RULE_BEGIN) && before.includes(RULE_END);
|
|
114
|
+
|
|
115
|
+
if (hasBlock) {
|
|
116
|
+
const next = replaceBlock(before, block);
|
|
117
|
+
if (next === before) return { action: "unchanged", status: ruleStatus(file) };
|
|
118
|
+
writeText(file, next);
|
|
119
|
+
return { action: "updated", status: ruleStatus(file) };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 用户自己已经写过等价规则 → 不硬塞第二份
|
|
123
|
+
if (looksLikePushRule(before)) return { action: "skipped-custom", status: ruleStatus(file) };
|
|
124
|
+
|
|
125
|
+
const body = before.replace(/\s*$/, "");
|
|
126
|
+
const next = body ? `${body}\n\n${block}\n` : `${block}\n`;
|
|
127
|
+
writeText(file, next, { backup: true });
|
|
128
|
+
return { action: "added", status: ruleStatus(file) };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 移除规则块(只删标记之间那一段)。
|
|
133
|
+
* @returns {{action: 'removed'|'absent', status: object}}
|
|
134
|
+
*/
|
|
135
|
+
export function removeRule(file) {
|
|
136
|
+
const before = readText(file);
|
|
137
|
+
if (!before.includes(RULE_BEGIN)) return { action: "absent", status: ruleStatus(file) };
|
|
138
|
+
const start = before.indexOf(RULE_BEGIN);
|
|
139
|
+
const end = before.indexOf(RULE_END);
|
|
140
|
+
let next = `${before.slice(0, start)}${before.slice(end + RULE_END.length)}`;
|
|
141
|
+
// 清掉移除后留下的空行堆,但不动其它内容
|
|
142
|
+
next = next.replace(/\n{3,}/g, "\n\n").replace(/^\s*\n/, "");
|
|
143
|
+
writeText(file, next);
|
|
144
|
+
return { action: "removed", status: ruleStatus(file) };
|
|
145
|
+
}
|
package/src/lib/client.js
CHANGED
|
@@ -95,6 +95,12 @@ window.__ModuleLoader__.load({
|
|
|
95
95
|
guardBudget: function (g) {
|
|
96
96
|
return '重试预算:确定未送达 ' + g.retryCount + ' 次 / 可能已送达 ' + g.ambiguousRetryCount + ' 次 · schedule_id 兜底:' + g.defaultScheduleId;
|
|
97
97
|
},
|
|
98
|
+
ruleTitle: '常驻推送规则',
|
|
99
|
+
ruleOn: '已启用',
|
|
100
|
+
ruleOff: '未启用',
|
|
101
|
+
ruleHint: '保存授权码时,会自动把「每轮都要推送」写进全局指令文件 AGENTS.md(带标记、可随时移除),新会话即生效。',
|
|
102
|
+
ruleCustom: '检测到你已手写过推送规则 —— 插件不会改动你的 AGENTS.md。',
|
|
103
|
+
ruleBlockPresent: 'AGENTS.md 里已有本插件维护的规则段(带标记;关掉即可移除)。',
|
|
98
104
|
debugTitle: '兜底战绩',
|
|
99
105
|
debugSummary: function (c) {
|
|
100
106
|
return '已救回 ' + (c.retryOk + c.rescueOk) + ' 次(重试 ' + c.retryOk + ' · 补推 ' + c.rescueOk + ')';
|
|
@@ -138,6 +144,12 @@ window.__ModuleLoader__.load({
|
|
|
138
144
|
guardBudget: function (g) {
|
|
139
145
|
return 'Retry budget: definitely-undelivered ' + g.retryCount + ' / possibly-delivered ' + g.ambiguousRetryCount + ' · schedule_id fallback: ' + g.defaultScheduleId;
|
|
140
146
|
},
|
|
147
|
+
ruleTitle: 'Always-push rule',
|
|
148
|
+
ruleOn: 'enabled',
|
|
149
|
+
ruleOff: 'disabled',
|
|
150
|
+
ruleHint: 'Saving the authCode also writes an "every turn must push" rule into your global instruction file AGENTS.md (marker-delimited, removable); new sessions pick it up.',
|
|
151
|
+
ruleCustom: 'You already wrote a push rule by hand, so this plugin leaves AGENTS.md untouched.',
|
|
152
|
+
ruleBlockPresent: "AGENTS.md already carries this plugin's rule block (marker-delimited; switch this off to remove it).",
|
|
141
153
|
debugTitle: 'Rescues so far',
|
|
142
154
|
debugSummary: function (c) {
|
|
143
155
|
return 'Saved ' + (c.retryOk + c.rescueOk) + ' (retries ' + c.retryOk + ', pushes ' + c.rescueOk + ')';
|
|
@@ -264,6 +276,7 @@ window.__ModuleLoader__.load({
|
|
|
264
276
|
var info = summary.data && summary.data.authCode;
|
|
265
277
|
var guard = summary.data && summary.data.guard;
|
|
266
278
|
var audit = summary.data && summary.data.audit;
|
|
279
|
+
var rule = summary.data && summary.data.rule;
|
|
267
280
|
|
|
268
281
|
var onSave = function () {
|
|
269
282
|
if (!draft.trim()) { setNote(t.emptyErr); return; }
|
|
@@ -287,6 +300,14 @@ window.__ModuleLoader__.load({
|
|
|
287
300
|
setNote((data.notes && data.notes[0]) || '');
|
|
288
301
|
});
|
|
289
302
|
};
|
|
303
|
+
var onToggleRule = function (next) {
|
|
304
|
+
setBusy('rule');
|
|
305
|
+
setNote('');
|
|
306
|
+
post({ ruleEnabled: next }).then(function (data) {
|
|
307
|
+
setBusy('');
|
|
308
|
+
if (data && data.notes && data.notes[0]) setNote(data.notes[0]);
|
|
309
|
+
});
|
|
310
|
+
};
|
|
290
311
|
var onToggleGuard = function (next) {
|
|
291
312
|
setBusy('guard');
|
|
292
313
|
setNote('');
|
|
@@ -461,6 +482,26 @@ window.__ModuleLoader__.load({
|
|
|
461
482
|
),
|
|
462
483
|
showDebug ? h('div', { style: { marginTop: '4px' } }, debugBody) : null
|
|
463
484
|
)
|
|
485
|
+
),
|
|
486
|
+
|
|
487
|
+
// ---- 常驻推送规则:保存授权码时自动写进全局指令文件 ----
|
|
488
|
+
h(
|
|
489
|
+
'div',
|
|
490
|
+
{ style: { marginTop: '14px', paddingTop: '12px', borderTop: '1px solid ' + V.border } },
|
|
491
|
+
row([
|
|
492
|
+
h('span', { style: { fontWeight: 600 } }, t.ruleTitle),
|
|
493
|
+
h('span', { style: { color: V.sub } }, rule && rule.enabled ? t.ruleOn : t.ruleOff),
|
|
494
|
+
h(Switch, {
|
|
495
|
+
checked: Boolean(rule && rule.enabled),
|
|
496
|
+
onChange: onToggleRule,
|
|
497
|
+
label: t.ruleTitle,
|
|
498
|
+
disabled: busy === 'rule' || !rule
|
|
499
|
+
})
|
|
500
|
+
]),
|
|
501
|
+
muted(
|
|
502
|
+
rule && rule.customRule ? t.ruleCustom : rule && rule.block ? t.ruleBlockPresent : t.ruleHint,
|
|
503
|
+
{ marginTop: '6px' }
|
|
504
|
+
)
|
|
464
505
|
)
|
|
465
506
|
);
|
|
466
507
|
};
|
package/src/lib/index.js
CHANGED
|
@@ -34,6 +34,7 @@ import { join } from "node:path";
|
|
|
34
34
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
35
35
|
import z from "@deepseek-ai/schemastery";
|
|
36
36
|
import { maskAuthCode, parseAuthCodeInput } from "./authcode.js";
|
|
37
|
+
import { ensureRule, removeRule, ruleStatus } from "./agents-rule.js";
|
|
37
38
|
import { configPath, readStore, writeStore } from "./config-store.js";
|
|
38
39
|
import { apply as applyPushGuard } from "./push-guard.js";
|
|
39
40
|
|
|
@@ -74,6 +75,22 @@ const GUARD_STATE_DIR = join(homedir(), ".dsh", "lan", "push-guard");
|
|
|
74
75
|
/** 调试图最多回溯多少条审计(文件本身有大小上限,读全量没必要)。 */
|
|
75
76
|
const AUDIT_MAX_LINES = 4000;
|
|
76
77
|
|
|
78
|
+
/**
|
|
79
|
+
* dsh 的全局指令文件。`dsh-agent-instructions` 每个会话都加载它 ——
|
|
80
|
+
* 而且**只认这一个全局文件**:`.local` 叠加(`AGENTS.local.md`)只在**项目目录**的
|
|
81
|
+
* 祖先链里生效,全局那段代码写死了只探测 `AGENTS.md`。所以「每轮都推」这条规则
|
|
82
|
+
* 想对所有会话、所有项目生效,只能写在这里。
|
|
83
|
+
*/
|
|
84
|
+
const AGENTS_FILE = join(process.env.DSH_HOME || join(homedir(), ".dsh"), "AGENTS.md");
|
|
85
|
+
|
|
86
|
+
/** 规则写入的结果 → 卡片上的提示语。 */
|
|
87
|
+
const RULE_NOTE = {
|
|
88
|
+
added: "已把「每轮都要推送」写进全局指令文件 AGENTS.md,新会话即生效。",
|
|
89
|
+
updated: "已更新 AGENTS.md 里的推送规则。",
|
|
90
|
+
unchanged: "AGENTS.md 里的推送规则已是最新。",
|
|
91
|
+
"skipped-custom": "检测到你已手写过推送规则,未改动 AGENTS.md。"
|
|
92
|
+
};
|
|
93
|
+
|
|
77
94
|
/** Success codes accepted by the official client (string or numeric zero). */
|
|
78
95
|
const SUCCESS_CODES = new Set(["0000000000", "0"]);
|
|
79
96
|
|
|
@@ -118,7 +135,14 @@ const Config = z.object({
|
|
|
118
135
|
/** hiboard_push 没带有效 schedule_id 时自动补的值(为空则卡片点不开)。 */
|
|
119
136
|
defaultScheduleId: z.string().default("dsh_worklog"),
|
|
120
137
|
/** 设置页路由开关;关掉后 `/hiboard-push/config` 不注册,浏览器半边也停手。 */
|
|
121
|
-
settingsCard: z.boolean().default(true)
|
|
138
|
+
settingsCard: z.boolean().default(true),
|
|
139
|
+
/**
|
|
140
|
+
* 保存授权码时,是否顺带把「每轮都要推送」的规则写进全局指令文件
|
|
141
|
+
* (`$DSH_HOME/AGENTS.md` 里的一个标记块)。**默认开启** —— 这样一键安装后
|
|
142
|
+
* 只要粘一次授权码,规则就自动就位,不必再手改 AGENTS.md。
|
|
143
|
+
* 关掉它(卡片上的按钮)会把已写入的那一段一起移除。
|
|
144
|
+
*/
|
|
145
|
+
autoRule: z.boolean().default(true)
|
|
122
146
|
});
|
|
123
147
|
|
|
124
148
|
const entryDefaults = {
|
|
@@ -136,7 +160,8 @@ const entryDefaults = {
|
|
|
136
160
|
guardAmbiguousRetryCount: 1,
|
|
137
161
|
guardRetryDelayMs: 3000,
|
|
138
162
|
defaultScheduleId: "dsh_worklog",
|
|
139
|
-
settingsCard: true
|
|
163
|
+
settingsCard: true,
|
|
164
|
+
autoRule: true
|
|
140
165
|
};
|
|
141
166
|
|
|
142
167
|
// ---------------------------------------------------------------- payload
|
|
@@ -534,7 +559,9 @@ function buildConfigRoute({ authCodeInfo, settings, storeFile }) {
|
|
|
534
559
|
endpoint: s.pushServiceUrl,
|
|
535
560
|
storeFile,
|
|
536
561
|
// 调试图:兜底到底把多少次「原本失败的推送」救了回来
|
|
537
|
-
audit: auditSummary()
|
|
562
|
+
audit: auditSummary(),
|
|
563
|
+
// 常驻规则:是否开启、有没有已写入的块、有没有用户自己写的等价规则
|
|
564
|
+
rule: Object.assign(ruleStatus(AGENTS_FILE), { enabled: s.autoRule })
|
|
538
565
|
};
|
|
539
566
|
};
|
|
540
567
|
|
|
@@ -574,6 +601,10 @@ function buildConfigRoute({ authCodeInfo, settings, storeFile }) {
|
|
|
574
601
|
const body = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}");
|
|
575
602
|
const patch = {};
|
|
576
603
|
const notes = [];
|
|
604
|
+
/** 本次请求是否真的写入了授权码(清除不算)——用它触发"顺带写规则"。 */
|
|
605
|
+
let authCodeTouched = false;
|
|
606
|
+
/** 卡片上的规则开关:true 写入 / false 移除 / undefined 不表态。 */
|
|
607
|
+
let ruleRequest;
|
|
577
608
|
|
|
578
609
|
// ---- 授权码:完整指令或裸码都行;空串表示"清除覆盖" ----
|
|
579
610
|
if (Object.prototype.hasOwnProperty.call(body, "authCode")) {
|
|
@@ -589,10 +620,17 @@ function buildConfigRoute({ authCodeInfo, settings, storeFile }) {
|
|
|
589
620
|
return;
|
|
590
621
|
}
|
|
591
622
|
patch.authCode = parsed.code;
|
|
623
|
+
authCodeTouched = true;
|
|
592
624
|
notes.push(`${parsed.note}(${maskAuthCode(parsed.code)})`);
|
|
593
625
|
}
|
|
594
626
|
}
|
|
595
627
|
|
|
628
|
+
// ---- 常驻规则开关 ----
|
|
629
|
+
if (Object.prototype.hasOwnProperty.call(body, "ruleEnabled")) {
|
|
630
|
+
patch.autoRule = body.ruleEnabled === true;
|
|
631
|
+
ruleRequest = body.ruleEnabled === true;
|
|
632
|
+
}
|
|
633
|
+
|
|
596
634
|
// ---- 兜底开关:布尔直接落盘,立即生效 ----
|
|
597
635
|
for (const key of ["guardEnabled", "guardRescueEnabled", "guardPromiseRescueEnabled", "guardRetryEnabled"]) {
|
|
598
636
|
if (Object.prototype.hasOwnProperty.call(body, key)) patch[key] = body[key] === true;
|
|
@@ -607,6 +645,27 @@ function buildConfigRoute({ authCodeInfo, settings, storeFile }) {
|
|
|
607
645
|
return;
|
|
608
646
|
}
|
|
609
647
|
writeStore(patch, storeFile);
|
|
648
|
+
|
|
649
|
+
// ---- 常规规则动作:放在落盘之后,读到的才是"最终"的 autoRule ----
|
|
650
|
+
// 保存授权码时**顺带**把「每轮都要推送」写进全局指令文件:用户此刻的意图
|
|
651
|
+
// 就是"让它开始工作",不该再要求他手改 AGENTS.md。默认开启,卡片可关。
|
|
652
|
+
try {
|
|
653
|
+
if (ruleRequest === false) {
|
|
654
|
+
const r = removeRule(AGENTS_FILE);
|
|
655
|
+
notes.push(r.action === "removed" ? "已移除全局指令文件里的推送规则。" : "全局推送规则本来就不存在,未改动。");
|
|
656
|
+
} else if (ruleRequest === true) {
|
|
657
|
+
const r = ensureRule(AGENTS_FILE);
|
|
658
|
+
notes.push(RULE_NOTE[r.action] ?? "已同步全局推送规则。");
|
|
659
|
+
} else if (authCodeTouched && settings().autoRule) {
|
|
660
|
+
const r = ensureRule(AGENTS_FILE);
|
|
661
|
+
// unchanged 不提示:每次保存都重复一句"已是最新"只是噪音
|
|
662
|
+
if (r.action !== "unchanged") notes.push(RULE_NOTE[r.action] ?? "已同步全局推送规则。");
|
|
663
|
+
}
|
|
664
|
+
} catch (error) {
|
|
665
|
+
// 规则写不进去不该让"授权码已保存"这件事看起来失败
|
|
666
|
+
notes.push(`授权码已保存,但写入全局规则失败:${String(error?.message ?? error)}`);
|
|
667
|
+
}
|
|
668
|
+
|
|
610
669
|
send(200, { ...summary(), notes });
|
|
611
670
|
} catch (error) {
|
|
612
671
|
send(400, { error: String(error?.message ?? error) });
|
|
@@ -645,7 +704,8 @@ function apply(ctx, rawConfig) {
|
|
|
645
704
|
guardAmbiguousRetryCount: Number(resolved.guardAmbiguousRetryCount) >= 0 ? Number(resolved.guardAmbiguousRetryCount) : 1,
|
|
646
705
|
guardRetryDelayMs: Number(resolved.guardRetryDelayMs) > 0 ? Number(resolved.guardRetryDelayMs) : 3000,
|
|
647
706
|
defaultScheduleId: String(resolved.defaultScheduleId ?? "").trim() || "dsh_worklog",
|
|
648
|
-
settingsCard: resolved.settingsCard !== false
|
|
707
|
+
settingsCard: resolved.settingsCard !== false,
|
|
708
|
+
autoRule: resolved.autoRule !== false
|
|
649
709
|
};
|
|
650
710
|
};
|
|
651
711
|
|