@kangyishuai/dsh-projects 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +73 -2
- package/README.zh-CN.md +73 -0
- package/cordis.patch.yml +3 -0
- package/lib/attach.js +89 -0
- package/lib/autofix.js +111 -0
- package/lib/autopr.js +115 -0
- package/lib/budget.js +95 -0
- package/lib/client.js +1841 -0
- package/lib/context.js +99 -0
- package/lib/exec.js +36 -0
- package/lib/executor.js +167 -0
- package/lib/index.js +5 -0
- package/lib/memory.js +53 -0
- package/lib/notify.js +50 -0
- package/lib/overview.js +199 -0
- package/lib/paths.js +28 -0
- package/lib/persona.js +49 -0
- package/lib/project-files.js +37 -0
- package/lib/routing.js +155 -0
- package/lib/service.js +1698 -0
- package/lib/store.js +547 -0
- package/lib/thread.js +245 -0
- package/lib/tools.js +277 -0
- package/lib/trash.js +71 -0
- package/lib/untrusted.js +98 -0
- package/lib/wake.js +195 -0
- package/lib/worktree.js +161 -0
- package/locale/en.json +94 -0
- package/locale/zh.json +94 -0
- package/package.json +61 -4
package/lib/persona.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 协调器人格:纯函数、零 I/O。
|
|
3
|
+
// 人格只影响「怎么说、先做什么」,**不影响权限**——权限由自治级别 + 危险开关 + 元规则决定
|
|
4
|
+
// (见 spec §7.4 / §7.6)。这一点必须在代码里可读,避免以后有人从人格里塞权限。
|
|
5
|
+
|
|
6
|
+
/** 内置默认人格的核心三条(规格 §7 要求写死这三件事)。 */
|
|
7
|
+
export const DEFAULT_PERSONA_RULES = Object.freeze([
|
|
8
|
+
"先读再改:动手前先看现状(线程状态、分支、已有改动)。",
|
|
9
|
+
"不确定就问:宁可用一句话问清,也不要猜着做。",
|
|
10
|
+
"只在有真结论时播报:不要为「我还在跑」发消息。",
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
/** 系统提示词里的段落顺序(人格排在项目上下文之后)。 */
|
|
14
|
+
export const PERSONA_ORDER = 140;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 组装人格段落文本。
|
|
18
|
+
* @param {{ projectName?: string, persona?: string }} input
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
export function buildPersona(input) {
|
|
22
|
+
const name = String(input?.projectName ?? "").trim();
|
|
23
|
+
const custom = String(input?.persona ?? "").trim();
|
|
24
|
+
const lines = [`# 你是「${name === "" ? "这个项目" : name}」的协调器`, ""];
|
|
25
|
+
if (custom !== "") {
|
|
26
|
+
lines.push("## 用户设定", custom, "");
|
|
27
|
+
}
|
|
28
|
+
lines.push("## 默认准则", ...DEFAULT_PERSONA_RULES.map((rule) => `- ${rule}`), "");
|
|
29
|
+
lines.push(
|
|
30
|
+
"## 边界",
|
|
31
|
+
"- 人格不改变权限:能做什么由自治级别与危险开关决定。",
|
|
32
|
+
"- 需要不可逆动作时,只能发起「申请」,不能自己执行。",
|
|
33
|
+
"- 不要声称自己做了没做的事。",
|
|
34
|
+
);
|
|
35
|
+
return lines.join("\n");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 协调器的人格段落是否与项目上下文重复(防止把 AGENTS.md 再贴一遍)。
|
|
40
|
+
* @param {string} text
|
|
41
|
+
* @param {{ instructions?: string }} [context]
|
|
42
|
+
* @returns {boolean}
|
|
43
|
+
*/
|
|
44
|
+
export function personaRepeatsContext(text, context) {
|
|
45
|
+
const instructions = String(context?.instructions ?? "").trim();
|
|
46
|
+
if (instructions === "") return false;
|
|
47
|
+
const head = instructions.slice(0, 120);
|
|
48
|
+
return head !== "" && String(text ?? "").includes(head);
|
|
49
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 项目内容文件(人可读、可 git 管理)的读取。
|
|
3
|
+
// 布局:<dataRoot>/<projectId>/instructions.md 与 memory/MEMORY.md。
|
|
4
|
+
// 只读,不创建;缺文件返回空串(不当作错误)。
|
|
5
|
+
|
|
6
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
/** 单个文件最多读这么多字符:防手滑把大文件塞进每一步的提示词。 */
|
|
10
|
+
export const MAX_FILE_CHARS = 64 * 1024;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} file
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
function readCapped(file) {
|
|
17
|
+
if (!existsSync(file)) return "";
|
|
18
|
+
const text = readFileSync(file, "utf8");
|
|
19
|
+
return text.length <= MAX_FILE_CHARS
|
|
20
|
+
? text
|
|
21
|
+
: `${text.slice(0, MAX_FILE_CHARS)}\n…(文件过大,已截断)`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 读一个项目的常设规则与记忆索引。
|
|
26
|
+
* @param {string} dataRoot
|
|
27
|
+
* @param {string} projectId
|
|
28
|
+
* @returns {{instructions: string, memoryIndex: string, dir: string}}
|
|
29
|
+
*/
|
|
30
|
+
export function readProjectFiles(dataRoot, projectId) {
|
|
31
|
+
const dir = join(dataRoot, projectId);
|
|
32
|
+
return {
|
|
33
|
+
dir,
|
|
34
|
+
instructions: readCapped(join(dir, "instructions.md")).trim(),
|
|
35
|
+
memoryIndex: readCapped(join(dir, "memory", "MEMORY.md")).trim(),
|
|
36
|
+
};
|
|
37
|
+
}
|
package/lib/routing.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 路由:规则优先、模型兜底。纯函数、零 I/O。
|
|
3
|
+
// 设计依据 notes/projects/m3-design.md §2:
|
|
4
|
+
// 规则命中即确定性执行(不调模型判断),未命中才走兜底;每条决策都要能审计。
|
|
5
|
+
|
|
6
|
+
/** 规则能被放行的动作(模型只能在兜底时选这些)。 */
|
|
7
|
+
export const ROUTE_ACTIONS = Object.freeze([
|
|
8
|
+
"answer-inline",
|
|
9
|
+
"new-thread",
|
|
10
|
+
"reuse-thread",
|
|
11
|
+
"propose",
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 自治级别允许的动作矩阵(级别不改变危险开关默认值,见 spec §7.4)。
|
|
16
|
+
* @type {Readonly<Record<string, readonly string[]>>}
|
|
17
|
+
*/
|
|
18
|
+
export const AUTONOMY_ALLOWED = Object.freeze({
|
|
19
|
+
L0: ["answer-inline", "propose"],
|
|
20
|
+
L1: ["answer-inline", "propose"],
|
|
21
|
+
L2: ["answer-inline", "propose", "new-thread", "reuse-thread"],
|
|
22
|
+
L3: ["answer-inline", "propose", "new-thread", "reuse-thread"],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 一条规则是否命中。
|
|
27
|
+
* 不支持的条件一律视为「不命中」——宁可落到兜底或问用户,也不瞎猜。
|
|
28
|
+
* @param {any} rule
|
|
29
|
+
* @param {{ text?: string, threadState?: string, isQuestion?: boolean }} context
|
|
30
|
+
* @returns {boolean}
|
|
31
|
+
*/
|
|
32
|
+
export function matchRule(rule, context) {
|
|
33
|
+
const when = rule?.when;
|
|
34
|
+
if (!when || typeof when !== "object") return false;
|
|
35
|
+
const text = String(context?.text ?? "");
|
|
36
|
+
|
|
37
|
+
if (typeof when.textMatches === "string" && when.textMatches !== "") {
|
|
38
|
+
let regex;
|
|
39
|
+
try {
|
|
40
|
+
regex = new RegExp(when.textMatches, "iu");
|
|
41
|
+
} catch {
|
|
42
|
+
return false; // 用户写坏的正则不能让整个路由崩掉
|
|
43
|
+
}
|
|
44
|
+
if (!regex.test(text)) return false;
|
|
45
|
+
}
|
|
46
|
+
if (Number.isFinite(when.textMaxChars) && text.length > Number(when.textMaxChars)) return false;
|
|
47
|
+
if (typeof when.isQuestion === "boolean" && Boolean(context?.isQuestion) !== when.isQuestion)
|
|
48
|
+
return false;
|
|
49
|
+
if (
|
|
50
|
+
typeof when.threadState === "string" &&
|
|
51
|
+
String(context?.threadState ?? "") !== when.threadState
|
|
52
|
+
) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 按顺序找第一条命中的规则。
|
|
60
|
+
* @param {any[]} rules
|
|
61
|
+
* @param {{ text?: string, threadState?: string, isQuestion?: boolean }} context
|
|
62
|
+
* @returns {{ ruleId: string, action: string, titleTemplate?: string } | null}
|
|
63
|
+
*/
|
|
64
|
+
export function matchRules(rules, context) {
|
|
65
|
+
for (const rule of Array.isArray(rules) ? rules : []) {
|
|
66
|
+
if (!matchRule(rule, context)) continue;
|
|
67
|
+
// 规则是扁平结构:{ id, when, action, titleTemplate? }
|
|
68
|
+
const action = String(rule?.action ?? "");
|
|
69
|
+
if (!ROUTE_ACTIONS.includes(action)) continue;
|
|
70
|
+
return {
|
|
71
|
+
ruleId: String(rule?.id ?? ""),
|
|
72
|
+
action,
|
|
73
|
+
...(typeof rule?.titleTemplate === "string" ? { titleTemplate: rule.titleTemplate } : {}),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 决定怎么处理一条输入。
|
|
81
|
+
* @param {object} input
|
|
82
|
+
* @param {string} input.text
|
|
83
|
+
* @param {any[]} [input.rules]
|
|
84
|
+
* @param {string} [input.autonomy]
|
|
85
|
+
* @param {boolean} [input.isQuestion]
|
|
86
|
+
* @param {string} [input.threadState]
|
|
87
|
+
* @param {{ action: string, titleTemplate?: string } | null} [input.fallback] 模型给的候选(可空)
|
|
88
|
+
* @returns {{
|
|
89
|
+
* by: "rule"|"fallback"|"blocked",
|
|
90
|
+
* action: string,
|
|
91
|
+
* ruleId?: string,
|
|
92
|
+
* blockedReason?: string,
|
|
93
|
+
* titleTemplate?: string,
|
|
94
|
+
* }}
|
|
95
|
+
*/
|
|
96
|
+
export function decideRoute(input) {
|
|
97
|
+
const autonomy = String(input?.autonomy ?? "L1");
|
|
98
|
+
const allowed = AUTONOMY_ALLOWED[autonomy] ?? AUTONOMY_ALLOWED.L1;
|
|
99
|
+
const context = {
|
|
100
|
+
text: String(input?.text ?? ""),
|
|
101
|
+
isQuestion: input?.isQuestion,
|
|
102
|
+
threadState: input?.threadState,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const hit = matchRules(input?.rules ?? [], context);
|
|
106
|
+
if (hit) {
|
|
107
|
+
if (!allowed.includes(hit.action)) {
|
|
108
|
+
return {
|
|
109
|
+
by: "blocked",
|
|
110
|
+
action: hit.action,
|
|
111
|
+
ruleId: hit.ruleId,
|
|
112
|
+
blockedReason: `autonomy/${autonomy}`,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
by: "rule",
|
|
117
|
+
action: hit.action,
|
|
118
|
+
ruleId: hit.ruleId,
|
|
119
|
+
...(hit.titleTemplate ? { titleTemplate: hit.titleTemplate } : {}),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const fallbackAction = String(input?.fallback?.action ?? "");
|
|
124
|
+
if (fallbackAction === "")
|
|
125
|
+
return { by: "blocked", action: "propose", blockedReason: "no-rule-no-fallback" };
|
|
126
|
+
if (!ROUTE_ACTIONS.includes(fallbackAction)) {
|
|
127
|
+
return { by: "blocked", action: "propose", blockedReason: "invalid-fallback" };
|
|
128
|
+
}
|
|
129
|
+
if (!allowed.includes(fallbackAction)) {
|
|
130
|
+
return { by: "blocked", action: fallbackAction, blockedReason: `autonomy/${autonomy}` };
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
by: "fallback",
|
|
134
|
+
action: fallbackAction,
|
|
135
|
+
...(typeof input?.fallback?.titleTemplate === "string"
|
|
136
|
+
? { titleTemplate: input.fallback.titleTemplate }
|
|
137
|
+
: {}),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 默认规则集合(用户可编辑;这里只给最常见的三条)。
|
|
143
|
+
* @returns {any[]}
|
|
144
|
+
*/
|
|
145
|
+
export function defaultRules() {
|
|
146
|
+
return [
|
|
147
|
+
{ id: "question", when: { isQuestion: true, textMaxChars: 200 }, action: "answer-inline" },
|
|
148
|
+
{
|
|
149
|
+
id: "long-task",
|
|
150
|
+
when: { textMaxChars: 2000 },
|
|
151
|
+
action: "new-thread",
|
|
152
|
+
titleTemplate: "{{firstLine}}",
|
|
153
|
+
},
|
|
154
|
+
];
|
|
155
|
+
}
|