@suifei/dsh-ai-team 0.4.143 → 0.4.145

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 (44) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +21 -21
  3. package/README.md +4 -0
  4. package/client.js +22 -14
  5. package/cordis.patch.yml +31 -31
  6. package/icon.svg +12 -12
  7. package/index.js +4 -3
  8. package/lib/domain/ids.js +26 -26
  9. package/lib/domain/inbox.js +93 -93
  10. package/lib/domain/member-config.js +168 -168
  11. package/lib/host/decision-report.js +72 -72
  12. package/lib/host/default-workspace.js +69 -69
  13. package/lib/host/fault-adapter.js +50 -50
  14. package/lib/host/people-view.js +120 -120
  15. package/lib/host/permission-guard.js +61 -61
  16. package/lib/host/pmo.js +13 -13
  17. package/lib/host/project-registry.js +104 -104
  18. package/lib/host/risk-view.js +99 -99
  19. package/lib/host/weekly-view.js +86 -86
  20. package/lib/host/workspace-path.js +94 -94
  21. package/lib/ledger/checkpoint.js +415 -415
  22. package/lib/ledger/events.js +398 -398
  23. package/lib/ledger/namespace.js +124 -124
  24. package/lib/ledger/tools.js +118 -118
  25. package/lib/ledger/writer.js +1266 -1266
  26. package/lib/runtime/error-chain.js +31 -31
  27. package/lib/runtime/shell-guard.js +102 -102
  28. package/lib/runtime/skill-catalog.js +41 -41
  29. package/lib/runtime/skill-policy.js +12 -12
  30. package/lib/skillopt/adopt-gate.js +108 -108
  31. package/lib/skillopt/gate-wiring.js +146 -146
  32. package/lib/skillopt/job-runner.js +5 -4
  33. package/lib/skillopt/jobs.js +135 -135
  34. package/lib/skillopt/judge.js +163 -143
  35. package/lib/skillopt/optimizer.js +183 -182
  36. package/lib/skillopt/proposal-store.js +33 -33
  37. package/lib/skillopt/review-runner.js +107 -107
  38. package/lib/skillopt/reviewer.js +87 -85
  39. package/lib/skillopt/rubric.js +189 -189
  40. package/lib/skillopt/subagent-text.js +101 -101
  41. package/lib/skillopt/tool-access.js +56 -56
  42. package/locale/en.json +6 -6
  43. package/locale/zh.json +6 -6
  44. package/package.json +1 -1
@@ -1,168 +1,168 @@
1
- import { ERRORS, fail } from './errors.js';
2
-
3
- /**
4
- * Member model/context configuration. The Project owner (Agent Lead) may
5
- * update it within the Team policy; Human may always update it. Config is
6
- * versioned and takes effect at the next wake, never mid-turn.
7
- */
8
- export const MEMBER_CONFIG_VERSION = 1;
9
-
10
- export const MEMBER_PRESETS = Object.freeze(['standard', 'ptc', 'minimal', 'cordis']);
11
-
12
- export const DEFAULT_MEMBER_CONFIG = Object.freeze({
13
- preset: 'standard',
14
- provider: '',
15
- model: '',
16
- reasoningEffort: null,
17
- maxTokens: null,
18
- contextWindow: null,
19
- compactionThreshold: 0.8,
20
- fallback: [],
21
- retry: { maxAttempts: 3 },
22
- memoryBudgetTokens: 4096,
23
- skillBudgetTokens: 2048,
24
- notesBudgetTokens: 2048,
25
- });
26
-
27
- export const CONFIG_FIELDS = Object.freeze([
28
- 'preset',
29
- 'provider', 'model', 'reasoningEffort', 'maxTokens', 'contextWindow',
30
- 'compactionThreshold', 'fallback', 'retry',
31
- 'memoryBudgetTokens', 'skillBudgetTokens', 'notesBudgetTokens',
32
- ]);
33
-
34
- function isInt(value) {
35
- return Number.isSafeInteger(value);
36
- }
37
-
38
- function positiveInt(value, name, max) {
39
- if (!isInt(value) || value < 1 || value > max) fail(ERRORS.INVALID_ARGUMENT, `${name} must be an integer in 1..${max}`);
40
- }
41
-
42
- function textOrNull(value) {
43
- return typeof value === 'string' && value !== '' ? value : null;
44
- }
45
-
46
- function normalizeFallback(value) {
47
- if (!Array.isArray(value)) fail(ERRORS.INVALID_ARGUMENT, 'fallback must be an array');
48
- if (value.length > 3) fail(ERRORS.INVALID_ARGUMENT, 'fallback supports at most 3 entries');
49
- return value.map((entry, index) => {
50
- if (entry === null || typeof entry !== 'object') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}] must be an object`);
51
- if (typeof entry.provider !== 'string' || entry.provider === '') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}].provider is required`);
52
- if (typeof entry.model !== 'string' || entry.model === '') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}].model is required`);
53
- if (entry.maxTokens !== undefined) positiveInt(entry.maxTokens, `fallback[${index}].maxTokens`, 1_000_000);
54
- return {
55
- provider: entry.provider,
56
- model: entry.model,
57
- ...(textOrNull(entry.reasoningEffort) === null ? {} : { reasoningEffort: entry.reasoningEffort }),
58
- ...(entry.maxTokens === undefined ? {} : { maxTokens: entry.maxTokens }),
59
- };
60
- });
61
- }
62
-
63
- /** Merge + validate one complete config; throws DomainError on invalid fields. */
64
- export function normalizeMemberConfig(input = {}, base = DEFAULT_MEMBER_CONFIG) {
65
- const value = input ?? {};
66
- const preset = value.preset === undefined ? base.preset : value.preset;
67
- if (typeof preset !== 'string' || !MEMBER_PRESETS.includes(preset)) {
68
- fail(ERRORS.INVALID_ARGUMENT, `preset must be one of ${MEMBER_PRESETS.join(', ')}`);
69
- }
70
- const provider = value.provider === undefined ? base.provider : value.provider;
71
- const model = value.model === undefined ? base.model : value.model;
72
- if (typeof provider !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'provider must be a string');
73
- if (typeof model !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'model must be a string');
74
- const reasoningEffort = value.reasoningEffort === undefined ? base.reasoningEffort : textOrNull(value.reasoningEffort);
75
- if (reasoningEffort !== null && typeof reasoningEffort !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'reasoningEffort must be a string or null');
76
- const maxTokens = value.maxTokens === undefined ? base.maxTokens : value.maxTokens;
77
- if (maxTokens !== null) positiveInt(maxTokens, 'maxTokens', 1_000_000);
78
- const contextWindow = value.contextWindow === undefined ? base.contextWindow : value.contextWindow;
79
- if (contextWindow !== null) {
80
- if (!isInt(contextWindow) || contextWindow < 4_096 || contextWindow > 2_000_000) {
81
- fail(ERRORS.INVALID_ARGUMENT, 'contextWindow must be an integer in 4096..2000000 or null');
82
- }
83
- }
84
- const compactionThreshold = value.compactionThreshold === undefined ? base.compactionThreshold : value.compactionThreshold;
85
- if (typeof compactionThreshold !== 'number' || !Number.isFinite(compactionThreshold) || compactionThreshold < 0.5 || compactionThreshold > 0.95) {
86
- fail(ERRORS.INVALID_ARGUMENT, 'compactionThreshold must be in 0.5..0.95');
87
- }
88
- const fallback = value.fallback === undefined ? normalizeFallback(base.fallback) : normalizeFallback(value.fallback);
89
- const retryInput = value.retry === undefined ? base.retry : value.retry;
90
- if (retryInput === null || typeof retryInput !== 'object') fail(ERRORS.INVALID_ARGUMENT, 'retry must be an object');
91
- const maxAttempts = retryInput.maxAttempts === undefined ? base.retry.maxAttempts : retryInput.maxAttempts;
92
- positiveInt(maxAttempts, 'retry.maxAttempts', 5);
93
- const memoryBudgetTokens = value.memoryBudgetTokens === undefined ? base.memoryBudgetTokens : value.memoryBudgetTokens;
94
- const skillBudgetTokens = value.skillBudgetTokens === undefined ? base.skillBudgetTokens : value.skillBudgetTokens;
95
- const notesBudgetTokens = value.notesBudgetTokens === undefined ? base.notesBudgetTokens : value.notesBudgetTokens;
96
- for (const [name, amount] of [['memoryBudgetTokens', memoryBudgetTokens], ['skillBudgetTokens', skillBudgetTokens], ['notesBudgetTokens', notesBudgetTokens]]) {
97
- positiveInt(amount, name, 128_000);
98
- }
99
- return {
100
- preset,
101
- provider,
102
- model,
103
- reasoningEffort,
104
- maxTokens,
105
- contextWindow,
106
- compactionThreshold,
107
- fallback,
108
- retry: { maxAttempts },
109
- memoryBudgetTokens,
110
- skillBudgetTokens,
111
- notesBudgetTokens,
112
- };
113
- }
114
-
115
- /** Team-level allow-list; a Lead can only choose inside this envelope. */
116
- export function validateMemberConfigPolicy(config, policy = {}) {
117
- if (Array.isArray(policy.allowedProviders) && !policy.allowedProviders.includes(config.provider)) {
118
- fail(ERRORS.CONFIG_POLICY, `provider ${config.provider} is not allowed by the Team policy`);
119
- }
120
- if (Array.isArray(policy.allowedModels) && !policy.allowedModels.includes(config.model)) {
121
- fail(ERRORS.CONFIG_POLICY, `model ${config.model} is not allowed by the Team policy`);
122
- }
123
- for (const [index, fallback] of config.fallback.entries()) {
124
- if (Array.isArray(policy.allowedProviders) && !policy.allowedProviders.includes(fallback.provider)) {
125
- fail(ERRORS.CONFIG_POLICY, `fallback[${index}].provider ${fallback.provider} is not allowed`);
126
- }
127
- if (Array.isArray(policy.allowedModels) && !policy.allowedModels.includes(fallback.model)) {
128
- fail(ERRORS.CONFIG_POLICY, `fallback[${index}].model ${fallback.model} is not allowed`);
129
- }
130
- }
131
- if (isInt(policy.maxAttempts) && config.retry.maxAttempts > policy.maxAttempts) {
132
- fail(ERRORS.CONFIG_POLICY, `retry.maxAttempts exceeds Team policy ${policy.maxAttempts}`);
133
- }
134
- if (isInt(policy.maxTokens) && config.maxTokens !== null && config.maxTokens > policy.maxTokens) {
135
- fail(ERRORS.CONFIG_POLICY, `maxTokens exceeds Team policy ${policy.maxTokens}`);
136
- }
137
- if (Array.isArray(policy.allowedPresets) && !policy.allowedPresets.includes(config.preset)) {
138
- fail(ERRORS.CONFIG_POLICY, `preset ${config.preset} is not allowed by the Team policy`);
139
- }
140
- return true;
141
- }
142
-
143
- /**
144
- * Apply a patch. The caller is responsible for the actor check (Lead/Human);
145
- * this function only validates and versions. A no-op patch does not bump.
146
- */
147
- export function applyMemberConfigPatch(current, patch, options = {}) {
148
- const before = normalizeMemberConfig(current);
149
- const merged = normalizeMemberConfig({ ...before, ...patch }, before);
150
- validateMemberConfigPolicy(merged, options.policy ?? {});
151
- const changed = CONFIG_FIELDS.some((field) => JSON.stringify(before[field]) !== JSON.stringify(merged[field]));
152
- if (!changed) return { config: { ...before, configVersion: options.configVersion ?? 0 }, before, changed: false };
153
- return {
154
- config: { ...merged, configVersion: (options.configVersion ?? 0) + 1 },
155
- before,
156
- changed: true,
157
- };
158
- }
159
-
160
- /** Only the fields DSH AgentOptions understands. */
161
- export function buildAgentOptions(config) {
162
- const options = {};
163
- if (config.provider) options.provider = config.provider;
164
- if (config.model) options.model = config.model;
165
- if (typeof config.reasoningEffort === 'string' && config.reasoningEffort !== '') options.reasoningEffort = config.reasoningEffort;
166
- if (Number.isSafeInteger(config.maxTokens)) options.maxTokens = config.maxTokens;
167
- return options;
168
- }
1
+ import { ERRORS, fail } from './errors.js';
2
+
3
+ /**
4
+ * Member model/context configuration. The Project owner (Agent Lead) may
5
+ * update it within the Team policy; Human may always update it. Config is
6
+ * versioned and takes effect at the next wake, never mid-turn.
7
+ */
8
+ export const MEMBER_CONFIG_VERSION = 1;
9
+
10
+ export const MEMBER_PRESETS = Object.freeze(['standard', 'ptc', 'minimal', 'cordis']);
11
+
12
+ export const DEFAULT_MEMBER_CONFIG = Object.freeze({
13
+ preset: 'standard',
14
+ provider: '',
15
+ model: '',
16
+ reasoningEffort: null,
17
+ maxTokens: null,
18
+ contextWindow: null,
19
+ compactionThreshold: 0.8,
20
+ fallback: [],
21
+ retry: { maxAttempts: 3 },
22
+ memoryBudgetTokens: 4096,
23
+ skillBudgetTokens: 2048,
24
+ notesBudgetTokens: 2048,
25
+ });
26
+
27
+ export const CONFIG_FIELDS = Object.freeze([
28
+ 'preset',
29
+ 'provider', 'model', 'reasoningEffort', 'maxTokens', 'contextWindow',
30
+ 'compactionThreshold', 'fallback', 'retry',
31
+ 'memoryBudgetTokens', 'skillBudgetTokens', 'notesBudgetTokens',
32
+ ]);
33
+
34
+ function isInt(value) {
35
+ return Number.isSafeInteger(value);
36
+ }
37
+
38
+ function positiveInt(value, name, max) {
39
+ if (!isInt(value) || value < 1 || value > max) fail(ERRORS.INVALID_ARGUMENT, `${name} must be an integer in 1..${max}`);
40
+ }
41
+
42
+ function textOrNull(value) {
43
+ return typeof value === 'string' && value !== '' ? value : null;
44
+ }
45
+
46
+ function normalizeFallback(value) {
47
+ if (!Array.isArray(value)) fail(ERRORS.INVALID_ARGUMENT, 'fallback must be an array');
48
+ if (value.length > 3) fail(ERRORS.INVALID_ARGUMENT, 'fallback supports at most 3 entries');
49
+ return value.map((entry, index) => {
50
+ if (entry === null || typeof entry !== 'object') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}] must be an object`);
51
+ if (typeof entry.provider !== 'string' || entry.provider === '') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}].provider is required`);
52
+ if (typeof entry.model !== 'string' || entry.model === '') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}].model is required`);
53
+ if (entry.maxTokens !== undefined) positiveInt(entry.maxTokens, `fallback[${index}].maxTokens`, 1_000_000);
54
+ return {
55
+ provider: entry.provider,
56
+ model: entry.model,
57
+ ...(textOrNull(entry.reasoningEffort) === null ? {} : { reasoningEffort: entry.reasoningEffort }),
58
+ ...(entry.maxTokens === undefined ? {} : { maxTokens: entry.maxTokens }),
59
+ };
60
+ });
61
+ }
62
+
63
+ /** Merge + validate one complete config; throws DomainError on invalid fields. */
64
+ export function normalizeMemberConfig(input = {}, base = DEFAULT_MEMBER_CONFIG) {
65
+ const value = input ?? {};
66
+ const preset = value.preset === undefined ? base.preset : value.preset;
67
+ if (typeof preset !== 'string' || !MEMBER_PRESETS.includes(preset)) {
68
+ fail(ERRORS.INVALID_ARGUMENT, `preset must be one of ${MEMBER_PRESETS.join(', ')}`);
69
+ }
70
+ const provider = value.provider === undefined ? base.provider : value.provider;
71
+ const model = value.model === undefined ? base.model : value.model;
72
+ if (typeof provider !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'provider must be a string');
73
+ if (typeof model !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'model must be a string');
74
+ const reasoningEffort = value.reasoningEffort === undefined ? base.reasoningEffort : textOrNull(value.reasoningEffort);
75
+ if (reasoningEffort !== null && typeof reasoningEffort !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'reasoningEffort must be a string or null');
76
+ const maxTokens = value.maxTokens === undefined ? base.maxTokens : value.maxTokens;
77
+ if (maxTokens !== null) positiveInt(maxTokens, 'maxTokens', 1_000_000);
78
+ const contextWindow = value.contextWindow === undefined ? base.contextWindow : value.contextWindow;
79
+ if (contextWindow !== null) {
80
+ if (!isInt(contextWindow) || contextWindow < 4_096 || contextWindow > 2_000_000) {
81
+ fail(ERRORS.INVALID_ARGUMENT, 'contextWindow must be an integer in 4096..2000000 or null');
82
+ }
83
+ }
84
+ const compactionThreshold = value.compactionThreshold === undefined ? base.compactionThreshold : value.compactionThreshold;
85
+ if (typeof compactionThreshold !== 'number' || !Number.isFinite(compactionThreshold) || compactionThreshold < 0.5 || compactionThreshold > 0.95) {
86
+ fail(ERRORS.INVALID_ARGUMENT, 'compactionThreshold must be in 0.5..0.95');
87
+ }
88
+ const fallback = value.fallback === undefined ? normalizeFallback(base.fallback) : normalizeFallback(value.fallback);
89
+ const retryInput = value.retry === undefined ? base.retry : value.retry;
90
+ if (retryInput === null || typeof retryInput !== 'object') fail(ERRORS.INVALID_ARGUMENT, 'retry must be an object');
91
+ const maxAttempts = retryInput.maxAttempts === undefined ? base.retry.maxAttempts : retryInput.maxAttempts;
92
+ positiveInt(maxAttempts, 'retry.maxAttempts', 5);
93
+ const memoryBudgetTokens = value.memoryBudgetTokens === undefined ? base.memoryBudgetTokens : value.memoryBudgetTokens;
94
+ const skillBudgetTokens = value.skillBudgetTokens === undefined ? base.skillBudgetTokens : value.skillBudgetTokens;
95
+ const notesBudgetTokens = value.notesBudgetTokens === undefined ? base.notesBudgetTokens : value.notesBudgetTokens;
96
+ for (const [name, amount] of [['memoryBudgetTokens', memoryBudgetTokens], ['skillBudgetTokens', skillBudgetTokens], ['notesBudgetTokens', notesBudgetTokens]]) {
97
+ positiveInt(amount, name, 128_000);
98
+ }
99
+ return {
100
+ preset,
101
+ provider,
102
+ model,
103
+ reasoningEffort,
104
+ maxTokens,
105
+ contextWindow,
106
+ compactionThreshold,
107
+ fallback,
108
+ retry: { maxAttempts },
109
+ memoryBudgetTokens,
110
+ skillBudgetTokens,
111
+ notesBudgetTokens,
112
+ };
113
+ }
114
+
115
+ /** Team-level allow-list; a Lead can only choose inside this envelope. */
116
+ export function validateMemberConfigPolicy(config, policy = {}) {
117
+ if (Array.isArray(policy.allowedProviders) && !policy.allowedProviders.includes(config.provider)) {
118
+ fail(ERRORS.CONFIG_POLICY, `provider ${config.provider} is not allowed by the Team policy`);
119
+ }
120
+ if (Array.isArray(policy.allowedModels) && !policy.allowedModels.includes(config.model)) {
121
+ fail(ERRORS.CONFIG_POLICY, `model ${config.model} is not allowed by the Team policy`);
122
+ }
123
+ for (const [index, fallback] of config.fallback.entries()) {
124
+ if (Array.isArray(policy.allowedProviders) && !policy.allowedProviders.includes(fallback.provider)) {
125
+ fail(ERRORS.CONFIG_POLICY, `fallback[${index}].provider ${fallback.provider} is not allowed`);
126
+ }
127
+ if (Array.isArray(policy.allowedModels) && !policy.allowedModels.includes(fallback.model)) {
128
+ fail(ERRORS.CONFIG_POLICY, `fallback[${index}].model ${fallback.model} is not allowed`);
129
+ }
130
+ }
131
+ if (isInt(policy.maxAttempts) && config.retry.maxAttempts > policy.maxAttempts) {
132
+ fail(ERRORS.CONFIG_POLICY, `retry.maxAttempts exceeds Team policy ${policy.maxAttempts}`);
133
+ }
134
+ if (isInt(policy.maxTokens) && config.maxTokens !== null && config.maxTokens > policy.maxTokens) {
135
+ fail(ERRORS.CONFIG_POLICY, `maxTokens exceeds Team policy ${policy.maxTokens}`);
136
+ }
137
+ if (Array.isArray(policy.allowedPresets) && !policy.allowedPresets.includes(config.preset)) {
138
+ fail(ERRORS.CONFIG_POLICY, `preset ${config.preset} is not allowed by the Team policy`);
139
+ }
140
+ return true;
141
+ }
142
+
143
+ /**
144
+ * Apply a patch. The caller is responsible for the actor check (Lead/Human);
145
+ * this function only validates and versions. A no-op patch does not bump.
146
+ */
147
+ export function applyMemberConfigPatch(current, patch, options = {}) {
148
+ const before = normalizeMemberConfig(current);
149
+ const merged = normalizeMemberConfig({ ...before, ...patch }, before);
150
+ validateMemberConfigPolicy(merged, options.policy ?? {});
151
+ const changed = CONFIG_FIELDS.some((field) => JSON.stringify(before[field]) !== JSON.stringify(merged[field]));
152
+ if (!changed) return { config: { ...before, configVersion: options.configVersion ?? 0 }, before, changed: false };
153
+ return {
154
+ config: { ...merged, configVersion: (options.configVersion ?? 0) + 1 },
155
+ before,
156
+ changed: true,
157
+ };
158
+ }
159
+
160
+ /** Only the fields DSH AgentOptions understands. */
161
+ export function buildAgentOptions(config) {
162
+ const options = {};
163
+ if (config.provider) options.provider = config.provider;
164
+ if (config.model) options.model = config.model;
165
+ if (typeof config.reasoningEffort === 'string' && config.reasoningEffort !== '') options.reasoningEffort = config.reasoningEffort;
166
+ if (Number.isSafeInteger(config.maxTokens)) options.maxTokens = config.maxTokens;
167
+ return options;
168
+ }
@@ -1,72 +1,72 @@
1
- import { existsSync } from 'node:fs';
2
-
3
- /**
4
- * Read the Human-decision table from a linked Markdown report.
5
- *
6
- * This is deliberately narrow: only a report containing a section titled
7
- * `6.` / `六` with a table gets converted. The Inbox still points back to the
8
- * source Thread; the table is exposed so Human does not have to open the report
9
- * just to discover the questions.
10
- */
11
-
12
- function stripMarkdown(value) {
13
- return String(value ?? '')
14
- .replace(/\*\*/g, '')
15
- .replace(/`/g, '')
16
- .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
17
- .trim();
18
- }
19
-
20
- export function findReportPath(text) {
21
- if (typeof text !== 'string') return null;
22
- // 三种写法都要认:反斜杠绝对路径、正斜杠绝对路径、以及带空格(含引号/代码
23
- // 围栏)的路径。只匹配 `[A-Za-z]:\\...` 会让「E:/works/a b/report.md」这类
24
- // 完全合法、且是本机常见输出的路径永远进不了 Inbox。
25
- const patterns = [
26
- /"[A-Za-z]:[\\/][^"<>|\r\n]+?\.md"/g,
27
- /'[A-Za-z]:[\\/][^'<>|\r\n]+?\.md'/g,
28
- /`[A-Za-z]:[\\/][^`<>|\r\n]+?\.md`/g,
29
- /[A-Za-z]:[\\/][^\s"'<>|`]+?\.md\b/g,
30
- ];
31
- // 命中多个时先看**哪个真的存在**:模型常先给示例路径、再给真实产物路径,文末
32
- // 也可能附一段模板示例——单靠「取第一个/取最后一个」都会错一边。存在的路径
33
- // 优先;都不存在时退回「最后一次提到」,那是模型的最终答复约定。
34
- const hits = [];
35
- for (const pattern of patterns) {
36
- for (const match of text.matchAll(pattern)) {
37
- hits.push({ index: match.index ?? 0, path: match[0].replace(/^["'`]|["'`]$/g, '') });
38
- }
39
- }
40
- if (hits.length === 0) return null;
41
- hits.sort((a, b) => a.index - b.index);
42
- const existing = hits.filter((hit) => {
43
- try { return existsSync(hit.path); } catch { return false; }
44
- });
45
- return (existing.length > 0 ? existing[existing.length - 1] : hits[hits.length - 1]).path;
46
- }
47
-
48
- export function parseDecisionQuestions(markdown) {
49
- const lines = String(markdown ?? '').split(/\r?\n/);
50
- const start = lines.findIndex((line) => /^##\s*(6|六)[.、\s]/.test(line) || /^##\s*(6|六)\s*$/.test(line));
51
- if (start < 0) return [];
52
- let end = lines.length;
53
- for (let index = start + 1; index < lines.length; index += 1) {
54
- if (/^##\s/.test(lines[index]) || /^---\s*$/.test(lines[index])) { end = index; break; }
55
- }
56
- const questions = [];
57
- for (const line of lines.slice(start + 1, end)) {
58
- if (!line.trim().startsWith('|')) continue;
59
- const cells = line.split('|').slice(1, -1).map((cell) => cell.trim());
60
- if (cells.length < 2) continue;
61
- if (!/^\d+$/.test(stripMarkdown(cells[0]))) continue;
62
- const text = stripMarkdown(cells[1]);
63
- if (text === '') continue;
64
- questions.push({
65
- id: `q${questions.length + 1}`,
66
- text,
67
- ...(cells[2] ? { recommendation: stripMarkdown(cells[2]) } : {}),
68
- ...(cells[3] ? { blocking: stripMarkdown(cells[3]) } : {}),
69
- });
70
- }
71
- return questions;
72
- }
1
+ import { existsSync } from 'node:fs';
2
+
3
+ /**
4
+ * Read the Human-decision table from a linked Markdown report.
5
+ *
6
+ * This is deliberately narrow: only a report containing a section titled
7
+ * `6.` / `六` with a table gets converted. The Inbox still points back to the
8
+ * source Thread; the table is exposed so Human does not have to open the report
9
+ * just to discover the questions.
10
+ */
11
+
12
+ function stripMarkdown(value) {
13
+ return String(value ?? '')
14
+ .replace(/\*\*/g, '')
15
+ .replace(/`/g, '')
16
+ .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
17
+ .trim();
18
+ }
19
+
20
+ export function findReportPath(text) {
21
+ if (typeof text !== 'string') return null;
22
+ // 三种写法都要认:反斜杠绝对路径、正斜杠绝对路径、以及带空格(含引号/代码
23
+ // 围栏)的路径。只匹配 `[A-Za-z]:\\...` 会让「E:/works/a b/report.md」这类
24
+ // 完全合法、且是本机常见输出的路径永远进不了 Inbox。
25
+ const patterns = [
26
+ /"[A-Za-z]:[\\/][^"<>|\r\n]+?\.md"/g,
27
+ /'[A-Za-z]:[\\/][^'<>|\r\n]+?\.md'/g,
28
+ /`[A-Za-z]:[\\/][^`<>|\r\n]+?\.md`/g,
29
+ /[A-Za-z]:[\\/][^\s"'<>|`]+?\.md\b/g,
30
+ ];
31
+ // 命中多个时先看**哪个真的存在**:模型常先给示例路径、再给真实产物路径,文末
32
+ // 也可能附一段模板示例——单靠「取第一个/取最后一个」都会错一边。存在的路径
33
+ // 优先;都不存在时退回「最后一次提到」,那是模型的最终答复约定。
34
+ const hits = [];
35
+ for (const pattern of patterns) {
36
+ for (const match of text.matchAll(pattern)) {
37
+ hits.push({ index: match.index ?? 0, path: match[0].replace(/^["'`]|["'`]$/g, '') });
38
+ }
39
+ }
40
+ if (hits.length === 0) return null;
41
+ hits.sort((a, b) => a.index - b.index);
42
+ const existing = hits.filter((hit) => {
43
+ try { return existsSync(hit.path); } catch { return false; }
44
+ });
45
+ return (existing.length > 0 ? existing[existing.length - 1] : hits[hits.length - 1]).path;
46
+ }
47
+
48
+ export function parseDecisionQuestions(markdown) {
49
+ const lines = String(markdown ?? '').split(/\r?\n/);
50
+ const start = lines.findIndex((line) => /^##\s*(6|六)[.、\s]/.test(line) || /^##\s*(6|六)\s*$/.test(line));
51
+ if (start < 0) return [];
52
+ let end = lines.length;
53
+ for (let index = start + 1; index < lines.length; index += 1) {
54
+ if (/^##\s/.test(lines[index]) || /^---\s*$/.test(lines[index])) { end = index; break; }
55
+ }
56
+ const questions = [];
57
+ for (const line of lines.slice(start + 1, end)) {
58
+ if (!line.trim().startsWith('|')) continue;
59
+ const cells = line.split('|').slice(1, -1).map((cell) => cell.trim());
60
+ if (cells.length < 2) continue;
61
+ if (!/^\d+$/.test(stripMarkdown(cells[0]))) continue;
62
+ const text = stripMarkdown(cells[1]);
63
+ if (text === '') continue;
64
+ questions.push({
65
+ id: `q${questions.length + 1}`,
66
+ text,
67
+ ...(cells[2] ? { recommendation: stripMarkdown(cells[2]) } : {}),
68
+ ...(cells[3] ? { blocking: stripMarkdown(cells[3]) } : {}),
69
+ });
70
+ }
71
+ return questions;
72
+ }
@@ -1,69 +1,69 @@
1
- /**
2
- * 初装默认项目 / 新建项目对话框的「默认工作目录」解析。
3
- *
4
- * 规则(用户裁定):
5
- * 1. 只有**没有指定**项目工作目录时才用默认目录;
6
- * 2. 项目一旦切换过工作目录,就按项目自己的目录执行(本模块只负责默认值);
7
- * 3. 默认值不能是插件自己的数据目录,否则成员会退化成「在账本目录里干活」;
8
- * 4. 不能把用户主目录**根**当工作目录——必须是它下面的子目录(`~/works`);
9
- * 5. 系统目录(Windows / Program Files)不当作团队工作目录——桌面启动器的
10
- * cwd 可能就是程序目录。
11
- *
12
- * 跨平台:主目录一律来自调用方的 `homedir()`(Windows 是 `USERPROFILE`,
13
- * Linux/macOS 是 `~`),本模块不自己判断平台,只用 `join` 拼路径。
14
- */
15
-
16
- import { statSync } from 'node:fs';
17
- import { join, sep } from 'node:path';
18
-
19
- const SYSTEM_DIR = /^[a-z]:[\\/](windows|program files|program files \(x86\))([\\/]|$)/i;
20
-
21
- /** 用户主目录下的工作区子目录名(不给主目录根本身)。 */
22
- export const WORKSPACE_SUBDIR = 'works';
23
-
24
- const isDirectory = (value) => {
25
- try { return statSync(value).isDirectory() === true; } catch { return false; }
26
- };
27
-
28
- /**
29
- * @param {{ configured?: string, cwd?: string, home?: string, dataDir?: string, excludeRoots?: string[] }} input
30
- * @returns {string} 绝对目录;找不到任何可用目录时返回空串。
31
- * 注意:返回的路径**可能尚不存在**——那是 `~/works` 建议值,调用方负责创建。
32
- */
33
- export function resolveDefaultWorkspace({ configured = '', cwd = '', home = '', dataDir = '', excludeRoots = [], exists = undefined } = {}) {
34
- const isDir = typeof exists === 'function' ? exists : isDirectory;
35
- const explicit = typeof configured === 'string' ? configured.trim() : '';
36
- const under = (value, root) => {
37
- if (typeof root !== 'string' || root === '') return false;
38
- const resolvedRoot = root.replace(/[\\/]+$/, '');
39
- return value === resolvedRoot || value.startsWith(`${resolvedRoot}${sep}`) || value.startsWith(`${resolvedRoot}/`);
40
- };
41
- const excluded = (value) => {
42
- // 插件数据目录永远不能当工作目录。
43
- if (under(value, dataDir)) return true;
44
- // `~/.dsh` 整棵树都不能当工作目录:桌面启动器的 cwd 就是 profile 目录
45
- // (`…\.dsh\profiles\desktop`),把它当项目工作区是错的。
46
- for (const root of excludeRoots) if (under(value, root)) return true;
47
- return false;
48
- };
49
- // UNC 路径(`\\wsl.localhost\…`、`\\server\share`)不作自动建议:它通常来自启动器
50
- // 的 cwd(从 WSL 目录里启动桌面端时就是这样),并不是用户的项目目录,而且成员
51
- // 工具在 UNC 上读写既慢又容易出权限问题。显式配置不受此限。
52
- const isUnc = (value) => /^\\\\[^\\]/.test(value);
53
- for (const raw of [explicit, cwd]) {
54
- if (typeof raw !== 'string' || raw === '') continue;
55
- if (!isDir(raw)) continue;
56
- if (excluded(raw)) continue;
57
- // 显式配置的目录由用户负责;自动探测到的系统目录/UNC 一律跳过。
58
- if (raw !== explicit && SYSTEM_DIR.test(raw)) continue;
59
- if (raw !== explicit && isUnc(raw)) continue;
60
- return raw;
61
- }
62
- // 兜底:用户主目录下的 `works` 子目录(跨平台由调用方传入 homedir)。
63
- if (typeof home === 'string' && home !== '') {
64
- const suggested = join(home, WORKSPACE_SUBDIR);
65
- // 主目录本身被排除(例如 home 就落在 ~/.dsh 下)时不再兜底。
66
- if (!excluded(suggested)) return suggested;
67
- }
68
- return '';
69
- }
1
+ /**
2
+ * 初装默认项目 / 新建项目对话框的「默认工作目录」解析。
3
+ *
4
+ * 规则(用户裁定):
5
+ * 1. 只有**没有指定**项目工作目录时才用默认目录;
6
+ * 2. 项目一旦切换过工作目录,就按项目自己的目录执行(本模块只负责默认值);
7
+ * 3. 默认值不能是插件自己的数据目录,否则成员会退化成「在账本目录里干活」;
8
+ * 4. 不能把用户主目录**根**当工作目录——必须是它下面的子目录(`~/works`);
9
+ * 5. 系统目录(Windows / Program Files)不当作团队工作目录——桌面启动器的
10
+ * cwd 可能就是程序目录。
11
+ *
12
+ * 跨平台:主目录一律来自调用方的 `homedir()`(Windows 是 `USERPROFILE`,
13
+ * Linux/macOS 是 `~`),本模块不自己判断平台,只用 `join` 拼路径。
14
+ */
15
+
16
+ import { statSync } from 'node:fs';
17
+ import { join, sep } from 'node:path';
18
+
19
+ const SYSTEM_DIR = /^[a-z]:[\\/](windows|program files|program files \(x86\))([\\/]|$)/i;
20
+
21
+ /** 用户主目录下的工作区子目录名(不给主目录根本身)。 */
22
+ export const WORKSPACE_SUBDIR = 'works';
23
+
24
+ const isDirectory = (value) => {
25
+ try { return statSync(value).isDirectory() === true; } catch { return false; }
26
+ };
27
+
28
+ /**
29
+ * @param {{ configured?: string, cwd?: string, home?: string, dataDir?: string, excludeRoots?: string[] }} input
30
+ * @returns {string} 绝对目录;找不到任何可用目录时返回空串。
31
+ * 注意:返回的路径**可能尚不存在**——那是 `~/works` 建议值,调用方负责创建。
32
+ */
33
+ export function resolveDefaultWorkspace({ configured = '', cwd = '', home = '', dataDir = '', excludeRoots = [], exists = undefined } = {}) {
34
+ const isDir = typeof exists === 'function' ? exists : isDirectory;
35
+ const explicit = typeof configured === 'string' ? configured.trim() : '';
36
+ const under = (value, root) => {
37
+ if (typeof root !== 'string' || root === '') return false;
38
+ const resolvedRoot = root.replace(/[\\/]+$/, '');
39
+ return value === resolvedRoot || value.startsWith(`${resolvedRoot}${sep}`) || value.startsWith(`${resolvedRoot}/`);
40
+ };
41
+ const excluded = (value) => {
42
+ // 插件数据目录永远不能当工作目录。
43
+ if (under(value, dataDir)) return true;
44
+ // `~/.dsh` 整棵树都不能当工作目录:桌面启动器的 cwd 就是 profile 目录
45
+ // (`…\.dsh\profiles\desktop`),把它当项目工作区是错的。
46
+ for (const root of excludeRoots) if (under(value, root)) return true;
47
+ return false;
48
+ };
49
+ // UNC 路径(`\\wsl.localhost\…`、`\\server\share`)不作自动建议:它通常来自启动器
50
+ // 的 cwd(从 WSL 目录里启动桌面端时就是这样),并不是用户的项目目录,而且成员
51
+ // 工具在 UNC 上读写既慢又容易出权限问题。显式配置不受此限。
52
+ const isUnc = (value) => /^\\\\[^\\]/.test(value);
53
+ for (const raw of [explicit, cwd]) {
54
+ if (typeof raw !== 'string' || raw === '') continue;
55
+ if (!isDir(raw)) continue;
56
+ if (excluded(raw)) continue;
57
+ // 显式配置的目录由用户负责;自动探测到的系统目录/UNC 一律跳过。
58
+ if (raw !== explicit && SYSTEM_DIR.test(raw)) continue;
59
+ if (raw !== explicit && isUnc(raw)) continue;
60
+ return raw;
61
+ }
62
+ // 兜底:用户主目录下的 `works` 子目录(跨平台由调用方传入 homedir)。
63
+ if (typeof home === 'string' && home !== '') {
64
+ const suggested = join(home, WORKSPACE_SUBDIR);
65
+ // 主目录本身被排除(例如 home 就落在 ~/.dsh 下)时不再兜底。
66
+ if (!excluded(suggested)) return suggested;
67
+ }
68
+ return '';
69
+ }