doukui 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.en.md +42 -0
- package/README.md +147 -2
- package/bin/commands/build.js +178 -0
- package/bin/commands/config.js +88 -0
- package/bin/commands/help.js +83 -0
- package/bin/commands/init.js +194 -0
- package/bin/commands/install.js +202 -0
- package/bin/commands/interactive.js +352 -0
- package/bin/commands/list.js +108 -0
- package/bin/commands/loop-server.js +357 -0
- package/bin/commands/loop.js +1398 -0
- package/bin/commands/migrate.js +120 -0
- package/bin/commands/setup-global.js +81 -0
- package/bin/commands/status.js +140 -0
- package/bin/commands/team-cleanup.js +190 -0
- package/bin/commands/validate-config.js +35 -0
- package/bin/commands/validate.js +21 -0
- package/bin/commands/version.js +22 -0
- package/bin/context.js +143 -0
- package/bin/doukui.js +216 -0
- package/package.json +47 -4
- package/plugins/contracts/capabilities.js +268 -0
- package/plugins/contracts/checkresult-schema.json +75 -0
- package/plugins/contracts/config-schema.json +179 -0
- package/plugins/contracts/plugin-interface.js +262 -0
- package/plugins/contracts/plugin-schema.json +104 -0
- package/plugins/core/hook-session-start/index.js +281 -0
- package/plugins/core/hook-session-start/plugin.json +7 -0
- package/plugins/core/hook-skill-gate/index.js +661 -0
- package/plugins/core/hook-skill-gate/plugin.json +12 -0
- package/plugins/core/provider-loop-engine/index.js +1397 -0
- package/plugins/core/provider-loop-engine/plugin.json +15 -0
- package/plugins/core/provider-memory-store/index.js +510 -0
- package/plugins/core/provider-memory-store/plugin.json +9 -0
- package/plugins/core/provider-role-registry/index.js +376 -0
- package/plugins/core/provider-role-registry/plugin.json +9 -0
- package/plugins/core/provider-state-store/index.js +244 -0
- package/plugins/core/provider-state-store/plugin.json +9 -0
- package/plugins/core/provider-watchdog/assets/daemon-config.template.json +32 -0
- package/plugins/core/provider-watchdog/assets/lib/watchdog-multi-window.js +676 -0
- package/plugins/core/provider-watchdog/assets/watchdog.js +754 -0
- package/plugins/core/provider-watchdog/index.js +151 -0
- package/plugins/core/provider-watchdog/plugin.json +13 -0
- package/plugins/core/skill-agent-dispatcher/cleanup-reference.md +214 -0
- package/plugins/core/skill-agent-dispatcher/daemon-helpers-reference.md +249 -0
- package/plugins/core/skill-agent-dispatcher/plugin.json +13 -0
- package/plugins/core/skill-agent-dispatcher/roles-reference.md +253 -0
- package/plugins/core/skill-agent-dispatcher/skill.md +1291 -0
- package/plugins/core/skill-agentic-loop/plugin.json +20 -0
- package/plugins/core/skill-agentic-loop/skill.md +572 -0
- package/plugins/core/skill-batch-task-creator/plugin.json +15 -0
- package/plugins/core/skill-batch-task-creator/skill.md +620 -0
- package/plugins/core/skill-checklist/plugin.json +10 -0
- package/plugins/core/skill-checklist/skill.md +171 -0
- package/plugins/core/skill-completion-report/plugin.json +10 -0
- package/plugins/core/skill-completion-report/skill.md +331 -0
- package/plugins/core/skill-doukui-plan/plugin.json +15 -0
- package/plugins/core/skill-doukui-plan/skill.md +233 -0
- package/plugins/core/skill-doukui-sync/plugin.json +10 -0
- package/plugins/core/skill-doukui-sync/skill.md +76 -0
- package/plugins/core/skill-experience-logger/plugin.json +10 -0
- package/plugins/core/skill-experience-logger/skill.md +235 -0
- package/plugins/core/skill-human-checkpoint/plugin.json +10 -0
- package/plugins/core/skill-human-checkpoint/skill.md +194 -0
- package/plugins/core/skill-progress-tracker/plugin.json +10 -0
- package/plugins/core/skill-progress-tracker/skill.md +228 -0
- package/plugins/core/skill-role-manager/plugin.json +10 -0
- package/plugins/core/skill-role-manager/skill.md +252 -0
- package/plugins/core/skill-split-work-package/plugin.json +15 -0
- package/plugins/core/skill-split-work-package/skill.md +441 -0
- package/plugins/core/skill-task-archive/plugin.json +10 -0
- package/plugins/core/skill-task-archive/skill.md +116 -0
- package/plugins/core/skill-task-creator/plugin.json +15 -0
- package/plugins/core/skill-task-creator/skill.md +886 -0
- package/plugins/core/skill-team-cleanup/plugin.json +10 -0
- package/plugins/core/skill-team-cleanup/skill.md +266 -0
- package/plugins/core/skill-watchdog-manager/plugin.json +10 -0
- package/plugins/core/skill-watchdog-manager/skill.md +143 -0
- package/plugins/core/skill-workflow-orchestrator/plugin.json +13 -0
- package/plugins/core/skill-workflow-orchestrator/skill.md +273 -0
- package/plugins/core/validator-doc-sync/index.js +248 -0
- package/plugins/core/validator-doc-sync/plugin.json +12 -0
- package/plugins/core/validator-work-package/index.js +300 -0
- package/plugins/core/validator-work-package/plugin.json +12 -0
- package/plugins/plugin-registry.json +186 -0
- package/plugins/runtime/agent-def-generator.js +150 -0
- package/plugins/runtime/audit-logger.js +311 -0
- package/plugins/runtime/build-cli.js +89 -0
- package/plugins/runtime/checkresult-validator.js +131 -0
- package/plugins/runtime/claude-md-injector.js +159 -0
- package/plugins/runtime/cleanup-utils.js +293 -0
- package/plugins/runtime/config-manager.js +317 -0
- package/plugins/runtime/config-validator.js +320 -0
- package/plugins/runtime/context-window.js +81 -0
- package/plugins/runtime/event-bus.js +207 -0
- package/plugins/runtime/executor-claude.js +828 -0
- package/plugins/runtime/executor-default.js +448 -0
- package/plugins/runtime/executor-local.js +178 -0
- package/plugins/runtime/executor-sdk.js +390 -0
- package/plugins/runtime/harness-build.js +1037 -0
- package/plugins/runtime/hook-dispatcher.js +315 -0
- package/plugins/runtime/logger.js +192 -0
- package/plugins/runtime/loop-actuator.js +292 -0
- package/plugins/runtime/loop-coordinator.js +347 -0
- package/plugins/runtime/loop-defaults.js +29 -0
- package/plugins/runtime/loop-dispatch-batch.js +247 -0
- package/plugins/runtime/loop-executor.js +120 -0
- package/plugins/runtime/loop-report.js +327 -0
- package/plugins/runtime/loop-server-core.js +750 -0
- package/plugins/runtime/loop-snapshot.js +581 -0
- package/plugins/runtime/loop-trace.js +242 -0
- package/plugins/runtime/loop-worktree.js +404 -0
- package/plugins/runtime/manifest-resolver.js +457 -0
- package/plugins/runtime/multi-window-coordinator.js +560 -0
- package/plugins/runtime/objective-verifier.js +136 -0
- package/plugins/runtime/plan-reader.js +763 -0
- package/plugins/runtime/plugin-loader.js +798 -0
- package/plugins/runtime/plugin-trust.js +182 -0
- package/plugins/runtime/plugin-validator.js +491 -0
- package/plugins/runtime/provider-failover.js +114 -0
- package/plugins/runtime/provider-resolver.js +342 -0
- package/plugins/runtime/reflection-evaluator.js +540 -0
- package/plugins/runtime/resolve-plugin-path.js +297 -0
- package/plugins/runtime/safe-path.js +119 -0
- package/plugins/runtime/sandbox-context.js +235 -0
- package/plugins/runtime/sandbox-manager.js +609 -0
- package/plugins/runtime/sandbox-worker.js +215 -0
- package/plugins/runtime/settings-merger.js +172 -0
- package/plugins/runtime/state-store.js +527 -0
- package/plugins/runtime/team-cleanup.js +497 -0
- package/plugins/runtime/text-tokenize.js +25 -0
- package/plugins/runtime/validator-pipeline.js +475 -0
- package/plugins/runtime/yaml-parser.js +692 -0
- package/templates/harness-config.yaml +388 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sandbox-worker.js - Worker Thread script for sandboxed plugin execution
|
|
3
|
+
*
|
|
4
|
+
* This script runs inside a Worker Thread and:
|
|
5
|
+
* 1. Receives plugin metadata via workerData
|
|
6
|
+
* 2. Creates a SandboxContext proxy for the plugin
|
|
7
|
+
* 3. Loads the plugin module
|
|
8
|
+
* 4. Calls plugin.onActivate(sandboxContext)
|
|
9
|
+
* 5. Forwards all context method calls via postMessage RPC to the main thread
|
|
10
|
+
*
|
|
11
|
+
* This file is loaded by SandboxManager as the Worker entry point.
|
|
12
|
+
*
|
|
13
|
+
* @module sandbox-worker
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
var workerThreads = require('worker_threads');
|
|
19
|
+
var parentPort = workerThreads.parentPort;
|
|
20
|
+
var workerData = workerThreads.workerData;
|
|
21
|
+
|
|
22
|
+
if (!workerData || !workerData.pluginName) {
|
|
23
|
+
parentPort.postMessage({
|
|
24
|
+
type: 'activation-error',
|
|
25
|
+
error: 'Missing workerData.pluginName',
|
|
26
|
+
});
|
|
27
|
+
// Exit with error
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var pluginName = workerData.pluginName;
|
|
32
|
+
var pluginPath = workerData.pluginPath;
|
|
33
|
+
var sourceType = workerData.sourceType;
|
|
34
|
+
var declaredCapabilities = workerData.capabilities || {};
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// SandboxContext - RPC proxy layer (runs inside worker)
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* SandboxContext provides a PluginContext-compatible interface inside the worker.
|
|
42
|
+
* All method calls are forwarded to the main thread via postMessage RPC.
|
|
43
|
+
*
|
|
44
|
+
* @internal 此函数仅在 Worker Thread 内部使用
|
|
45
|
+
* @param {string} name - plugin name
|
|
46
|
+
* @param {MessagePort} port - parent port for communication
|
|
47
|
+
*/
|
|
48
|
+
function SandboxContext(name, port) {
|
|
49
|
+
this.pluginName = name;
|
|
50
|
+
this._port = port;
|
|
51
|
+
this._rpcId = 0;
|
|
52
|
+
|
|
53
|
+
// Proxy eventBus: emit only (subscribe requires main-thread coordination)
|
|
54
|
+
this.eventBus = {
|
|
55
|
+
emit: function (event, data) {
|
|
56
|
+
return this._rpc('eventBus.emit', [event, data]);
|
|
57
|
+
}.bind(this),
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Proxy stateStore
|
|
61
|
+
this.stateStore = {
|
|
62
|
+
get: function (key) {
|
|
63
|
+
return this._rpc('stateStore.get', [key]);
|
|
64
|
+
}.bind(this),
|
|
65
|
+
set: function (key, value) {
|
|
66
|
+
return this._rpc('stateStore.set', [key, value]);
|
|
67
|
+
}.bind(this),
|
|
68
|
+
delete: function (key) {
|
|
69
|
+
return this._rpc('stateStore.delete', [key]);
|
|
70
|
+
}.bind(this),
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Proxy logger
|
|
74
|
+
this.logger = {
|
|
75
|
+
info: function (msg) {
|
|
76
|
+
return this._rpc('logger.info', [msg]);
|
|
77
|
+
}.bind(this),
|
|
78
|
+
warn: function (msg) {
|
|
79
|
+
return this._rpc('logger.warn', [msg]);
|
|
80
|
+
}.bind(this),
|
|
81
|
+
error: function (msg) {
|
|
82
|
+
return this._rpc('logger.error', [msg]);
|
|
83
|
+
}.bind(this),
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// config (read-only)
|
|
87
|
+
this.config = {
|
|
88
|
+
get: function (key) {
|
|
89
|
+
return this._rpc('config.get', [key]);
|
|
90
|
+
}.bind(this),
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// Provider cache
|
|
94
|
+
this._providerCache = {};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get a provider by name (via RPC to main thread).
|
|
99
|
+
*
|
|
100
|
+
* @internal
|
|
101
|
+
* @param {string} name - provider name
|
|
102
|
+
* @returns {Promise<object>}
|
|
103
|
+
*/
|
|
104
|
+
SandboxContext.prototype.getProvider = function (name) {
|
|
105
|
+
if (this._providerCache[name]) {
|
|
106
|
+
return Promise.resolve(this._providerCache[name]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
var self = this;
|
|
110
|
+
return this._rpc('getProvider', [name]).then(function (provider) {
|
|
111
|
+
self._providerCache[name] = provider;
|
|
112
|
+
return provider;
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Get another plugin by name (not supported in sandbox — returns undefined).
|
|
118
|
+
*
|
|
119
|
+
* @param {string} _name
|
|
120
|
+
* @returns {undefined}
|
|
121
|
+
*/
|
|
122
|
+
SandboxContext.prototype.getPlugin = function (_name) {
|
|
123
|
+
// Cross-plugin access not supported in sandbox for security
|
|
124
|
+
return undefined;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Send an RPC request to the main thread and wait for the response.
|
|
129
|
+
*
|
|
130
|
+
* @param {string} method - method name
|
|
131
|
+
* @param {Array} args - arguments (must be serializable)
|
|
132
|
+
* @returns {Promise<any>}
|
|
133
|
+
*/
|
|
134
|
+
SandboxContext.prototype._rpc = function (method, args) {
|
|
135
|
+
var id = ++this._rpcId;
|
|
136
|
+
var port = this._port;
|
|
137
|
+
var timeout = 30000;
|
|
138
|
+
|
|
139
|
+
return new Promise(function (resolve, reject) {
|
|
140
|
+
var timer = setTimeout(function () {
|
|
141
|
+
port.off('message', handler);
|
|
142
|
+
reject(new Error('RPC timeout for ' + method + ' after ' + timeout + 'ms'));
|
|
143
|
+
}, timeout);
|
|
144
|
+
|
|
145
|
+
var handler = function (msg) {
|
|
146
|
+
if (msg.type === 'rpc-response' && msg.id === id) {
|
|
147
|
+
clearTimeout(timer);
|
|
148
|
+
port.off('message', handler);
|
|
149
|
+
if (msg.error) {
|
|
150
|
+
reject(new Error(msg.error));
|
|
151
|
+
} else {
|
|
152
|
+
resolve(msg.result);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
port.on('message', handler);
|
|
158
|
+
port.postMessage({
|
|
159
|
+
type: 'rpc-request',
|
|
160
|
+
id: id,
|
|
161
|
+
method: method,
|
|
162
|
+
args: args,
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
// Load and activate the plugin
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
// Require the plugin module
|
|
173
|
+
var indexJsPath = require('path').resolve(pluginPath, 'index.js');
|
|
174
|
+
var PluginClass = require(indexJsPath);
|
|
175
|
+
|
|
176
|
+
if (typeof PluginClass !== 'function') {
|
|
177
|
+
throw new Error('Plugin "' + pluginName + '" does not export a constructor function');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
var pluginInstance = new PluginClass();
|
|
181
|
+
var context = new SandboxContext(pluginName, parentPort);
|
|
182
|
+
|
|
183
|
+
// Call onActivate
|
|
184
|
+
if (typeof pluginInstance.onActivate === 'function') {
|
|
185
|
+
var activateResult = pluginInstance.onActivate(context);
|
|
186
|
+
|
|
187
|
+
// Handle async onActivate
|
|
188
|
+
if (activateResult && typeof activateResult.then === 'function') {
|
|
189
|
+
activateResult.then(function () {
|
|
190
|
+
parentPort.postMessage({ type: 'activated' });
|
|
191
|
+
}).catch(function (err) {
|
|
192
|
+
parentPort.postMessage({
|
|
193
|
+
type: 'activation-error',
|
|
194
|
+
error: err.message,
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
} else {
|
|
198
|
+
parentPort.postMessage({ type: 'activated' });
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
parentPort.postMessage({ type: 'activated' });
|
|
202
|
+
}
|
|
203
|
+
} catch (err) {
|
|
204
|
+
parentPort.postMessage({
|
|
205
|
+
type: 'activation-error',
|
|
206
|
+
error: 'Failed to load plugin "' + pluginName + '": ' + err.message,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Handle terminate signal from main thread
|
|
211
|
+
parentPort.on('message', function (msg) {
|
|
212
|
+
if (msg.type === 'terminate') {
|
|
213
|
+
process.exit(0);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* settings-merger - Merge doukui hooks into project settings
|
|
3
|
+
*
|
|
4
|
+
* Handles the merging of doukui hook configurations into the
|
|
5
|
+
* target project's .claude/settings.json. Supports both global and local
|
|
6
|
+
* install modes with appropriate path resolution.
|
|
7
|
+
*
|
|
8
|
+
* @module settings-merger
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
var fs = require('fs');
|
|
14
|
+
var path = require('path');
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Install mode detection
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Detect whether this is a local or global installation.
|
|
22
|
+
* Checks if packageRoot is an ancestor of targetRoot.
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
* @param {string} packageRoot - this package's root directory (node_modules/doukui/)
|
|
26
|
+
* @param {string} targetRoot - target project root directory
|
|
27
|
+
* @returns {boolean} true if local install, false if global install
|
|
28
|
+
*/
|
|
29
|
+
function isLocalInstall(packageRoot, targetRoot) {
|
|
30
|
+
// Normalize paths for comparison
|
|
31
|
+
var normalizedPackage = path.resolve(packageRoot).replace(/\\/g, '/');
|
|
32
|
+
var normalizedTarget = path.resolve(targetRoot).replace(/\\/g, '/');
|
|
33
|
+
|
|
34
|
+
// Check if packageRoot is an ancestor of targetRoot
|
|
35
|
+
var relative = path.relative(normalizedPackage, normalizedTarget);
|
|
36
|
+
// If relative path doesn't start with '..', packageRoot is an ancestor
|
|
37
|
+
return relative.indexOf('..') !== 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// Hook entry management
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Update or insert a hook entry in a hooks array.
|
|
46
|
+
* If a hook with the same matcher exists, update its command; otherwise add a new entry.
|
|
47
|
+
*
|
|
48
|
+
* @public
|
|
49
|
+
* @param {object[]} hookArray - the hooks array (e.g. settings.hooks.PreToolUse)
|
|
50
|
+
* @param {string} matcher - the matcher string (e.g. 'Edit|Write')
|
|
51
|
+
* @param {string} command - the full command string
|
|
52
|
+
*/
|
|
53
|
+
function upsertHookEntry(hookArray, matcher, command) {
|
|
54
|
+
for (var i = 0; i < hookArray.length; i++) {
|
|
55
|
+
if (hookArray[i].matcher === matcher) {
|
|
56
|
+
hookArray[i].hooks = [{ type: 'command', command: command }];
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
hookArray.push({
|
|
61
|
+
matcher: matcher,
|
|
62
|
+
hooks: [{ type: 'command', command: command }]
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// Public API
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Merge doukui hooks into the target project's .claude/settings.json.
|
|
72
|
+
* Reads existing settings, adds doukui-specific hooks, and writes back.
|
|
73
|
+
* Idempotent: skips hooks that are already registered.
|
|
74
|
+
*
|
|
75
|
+
* Uses absolute paths for global installs, relative paths for local installs.
|
|
76
|
+
*
|
|
77
|
+
* @public
|
|
78
|
+
* @param {object} options
|
|
79
|
+
* @param {string} options.targetRoot - target project root directory
|
|
80
|
+
* @param {string} options.packageRoot - this package's root directory (node_modules/doukui/)
|
|
81
|
+
* @param {Function} [options.ensureDir] - directory creation function (injectable for testing)
|
|
82
|
+
*/
|
|
83
|
+
function mergeSettings(options) {
|
|
84
|
+
var targetRoot = options.targetRoot;
|
|
85
|
+
var packageRoot = options.packageRoot;
|
|
86
|
+
var ensureDir = options.ensureDir || defaultEnsureDir;
|
|
87
|
+
|
|
88
|
+
var settingsPath = path.join(targetRoot, '.claude', 'settings.json');
|
|
89
|
+
var settings = {};
|
|
90
|
+
|
|
91
|
+
// Detect installation mode
|
|
92
|
+
var localInstall = isLocalInstall(packageRoot, targetRoot);
|
|
93
|
+
|
|
94
|
+
// Read existing settings if present
|
|
95
|
+
if (fs.existsSync(settingsPath)) {
|
|
96
|
+
try {
|
|
97
|
+
settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8'));
|
|
98
|
+
} catch (e) {
|
|
99
|
+
// B3:损坏的 settings.json 不能静默重置(会丢失用户的多 matcher/多 command 配置)。
|
|
100
|
+
// 先备份原文件,再抛错让调用方中止——绝不直接覆盖。
|
|
101
|
+
var backupPath = settingsPath + '.corrupt.' + Date.now();
|
|
102
|
+
try {
|
|
103
|
+
fs.copyFileSync(settingsPath, backupPath);
|
|
104
|
+
} catch (_backupErr) {
|
|
105
|
+
// 备份失败也不掩盖问题:仍抛错(备份路径仅用于提示)
|
|
106
|
+
}
|
|
107
|
+
throw new Error(
|
|
108
|
+
'settings.json 解析失败,已停止以避免覆盖用户配置。' +
|
|
109
|
+
'原文件已备份到 ' + backupPath + '。解析错误:' + (e && e.message ? e.message : String(e)) +
|
|
110
|
+
'。请修正后重试。'
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Ensure hooks structure exists
|
|
116
|
+
if (!settings.hooks) settings.hooks = {};
|
|
117
|
+
if (!settings.hooks.SessionStart) settings.hooks.SessionStart = [];
|
|
118
|
+
if (!settings.hooks.PreToolUse) settings.hooks.PreToolUse = [];
|
|
119
|
+
if (!settings.hooks.PostToolUse) settings.hooks.PostToolUse = [];
|
|
120
|
+
|
|
121
|
+
// Resolve hook script paths based on installation mode
|
|
122
|
+
var hookScriptPath = path.join(packageRoot, 'plugins', 'core', 'hook-skill-gate', 'index.js');
|
|
123
|
+
var hookCmd;
|
|
124
|
+
if (localInstall) {
|
|
125
|
+
// Local install: use relative path
|
|
126
|
+
var hookScriptRelative = path.relative(targetRoot, hookScriptPath).replace(/\\/g, '/');
|
|
127
|
+
hookCmd = 'node "' + hookScriptRelative + '"';
|
|
128
|
+
} else {
|
|
129
|
+
// Global install: use absolute path with forward slashes
|
|
130
|
+
hookCmd = 'node "' + hookScriptPath.replace(/\\/g, '/') + '"';
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Update or add PreToolUse hook for Edit|Write
|
|
134
|
+
var preMatcher = 'Edit|Write';
|
|
135
|
+
upsertHookEntry(settings.hooks.PreToolUse, preMatcher, hookCmd + ' --pre-tool');
|
|
136
|
+
|
|
137
|
+
// Update or add PostToolUse hook for Skill
|
|
138
|
+
var postMatcher = 'Skill';
|
|
139
|
+
upsertHookEntry(settings.hooks.PostToolUse, postMatcher, hookCmd + ' --post-skill');
|
|
140
|
+
|
|
141
|
+
// Update or add SessionStart hook for plan-mode rule injection
|
|
142
|
+
var sessionHookScriptPath = path.join(packageRoot, 'plugins', 'core', 'hook-session-start', 'index.js');
|
|
143
|
+
var sessionHookCmd;
|
|
144
|
+
if (localInstall) {
|
|
145
|
+
var sessionHookRelative = path.relative(targetRoot, sessionHookScriptPath).replace(/\\/g, '/');
|
|
146
|
+
sessionHookCmd = 'node "' + sessionHookRelative + '"';
|
|
147
|
+
} else {
|
|
148
|
+
sessionHookCmd = 'node "' + sessionHookScriptPath.replace(/\\/g, '/') + '"';
|
|
149
|
+
}
|
|
150
|
+
var sessionMatcher = 'startup|clear|compact';
|
|
151
|
+
upsertHookEntry(settings.hooks.SessionStart, sessionMatcher, sessionHookCmd);
|
|
152
|
+
|
|
153
|
+
// Write back
|
|
154
|
+
ensureDir(path.dirname(settingsPath));
|
|
155
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf-8');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Default directory creation function.
|
|
160
|
+
* @param {string} dirPath
|
|
161
|
+
*/
|
|
162
|
+
function defaultEnsureDir(dirPath) {
|
|
163
|
+
if (!fs.existsSync(dirPath)) {
|
|
164
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
module.exports = {
|
|
169
|
+
mergeSettings: mergeSettings,
|
|
170
|
+
isLocalInstall: isLocalInstall,
|
|
171
|
+
upsertHookEntry: upsertHookEntry,
|
|
172
|
+
};
|