doukui 0.0.0-stage → 0.3.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.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 +1443 -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 +1411 -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 +588 -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 +415 -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-lint.js +100 -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 +545 -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/self-rescue-ladder.js +38 -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 +394 -0
|
@@ -0,0 +1,1037 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HarnessBuild - Plugin-to-native format builder for AI Agent Harness
|
|
3
|
+
*
|
|
4
|
+
* Reads plugins from plugin-registry.json, converts them to Claude Code
|
|
5
|
+
* native skill/hook format, and outputs to .claude/skills/ and .claude/hooks/.
|
|
6
|
+
*
|
|
7
|
+
* This module acts as a thin orchestrator that delegates to specialized modules:
|
|
8
|
+
* - yaml-parser.js — YAML configuration parsing
|
|
9
|
+
* - plugin-validator.js — Plugin format validation + capabilities validation
|
|
10
|
+
* - settings-merger.js — Settings.json hook merging
|
|
11
|
+
* - claude-md-injector.js — CLAUDE.md rule injection
|
|
12
|
+
*
|
|
13
|
+
* @module harness-build
|
|
14
|
+
*
|
|
15
|
+
* CLI entry point moved to build-cli.js.
|
|
16
|
+
*
|
|
17
|
+
* Usage (via build-cli.js):
|
|
18
|
+
* node plugins/runtime/build-cli.js # build all plugins
|
|
19
|
+
* node plugins/runtime/build-cli.js --validate # validate only
|
|
20
|
+
*
|
|
21
|
+
* Features:
|
|
22
|
+
* - Builds skill plugins -> .claude/skills/{name}/skill.md
|
|
23
|
+
* - Builds hook plugins -> .claude/hooks/{name}/index.js
|
|
24
|
+
* - Validates plugin.json required fields
|
|
25
|
+
* - Handles empty registries gracefully
|
|
26
|
+
* - Build report with plugin counts and output paths
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
'use strict';
|
|
30
|
+
|
|
31
|
+
var fs = require('fs');
|
|
32
|
+
var path = require('path');
|
|
33
|
+
|
|
34
|
+
// Sub-modules
|
|
35
|
+
var yamlParser = require('./yaml-parser');
|
|
36
|
+
var pluginValidator = require('./plugin-validator');
|
|
37
|
+
var settingsMerger = require('./settings-merger');
|
|
38
|
+
var claudeMdInjector = require('./claude-md-injector');
|
|
39
|
+
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// HarnessBuild class
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* HarnessBuild constructor.
|
|
46
|
+
* @public
|
|
47
|
+
* @param {object} [options]
|
|
48
|
+
* @param {string} [options.rootDir] - project root directory (deprecated, use targetRoot)
|
|
49
|
+
* @param {string} [options.targetRoot] - target project root directory
|
|
50
|
+
* @param {string} [options.packageRoot] - doukui package root directory
|
|
51
|
+
* @param {string} [options.registryPath] - override path to plugin-registry.json
|
|
52
|
+
* @param {string} [options.pluginsDir] - override path to plugins/core/
|
|
53
|
+
* @param {string} [options.outputSkillsDir] - override output .claude/skills/
|
|
54
|
+
* @param {string} [options.outputHooksDir] - override output .claude/hooks/
|
|
55
|
+
* @param {boolean} [options.verbose] - enable verbose logging
|
|
56
|
+
* @param {boolean} [options.globalMode] - global install mode (use default context config)
|
|
57
|
+
* @param {string} [options.cliPath] - absolute path to CLI binary for __DOUKUI_CLI_PATH__ replacement
|
|
58
|
+
*/
|
|
59
|
+
function HarnessBuild(options) {
|
|
60
|
+
options = options || {};
|
|
61
|
+
|
|
62
|
+
// Support both old rootDir and new targetRoot naming
|
|
63
|
+
this._targetRoot = options.targetRoot || options.rootDir || process.cwd();
|
|
64
|
+
this._packageRoot = options.packageRoot || this._targetRoot;
|
|
65
|
+
this._registryPath = options.registryPath || path.join(this._packageRoot, 'plugins', 'plugin-registry.json');
|
|
66
|
+
this._pluginsDir = options.pluginsDir || path.join(this._packageRoot, 'plugins', 'core');
|
|
67
|
+
this._outputSkillsDir = options.outputSkillsDir || path.join(this._targetRoot, '.claude', 'skills');
|
|
68
|
+
this._outputHooksDir = options.outputHooksDir || path.join(this._targetRoot, '.claude', 'hooks');
|
|
69
|
+
this._verbose = options.verbose || false;
|
|
70
|
+
this._globalMode = options.globalMode || false;
|
|
71
|
+
this._cliPath = options.cliPath || null;
|
|
72
|
+
this._logger = options.logger || null;
|
|
73
|
+
|
|
74
|
+
// Legacy alias for backward compatibility
|
|
75
|
+
this._rootDir = this._targetRoot;
|
|
76
|
+
|
|
77
|
+
/** @type {object[]} validation errors collected during --validate */
|
|
78
|
+
this._validationErrors = [];
|
|
79
|
+
/** @type {object[]} validation warnings */
|
|
80
|
+
this._validationWarnings = [];
|
|
81
|
+
/** @type {object[]} build results */
|
|
82
|
+
this._buildResults = [];
|
|
83
|
+
/** @type {object|null} cached harness config */
|
|
84
|
+
this._harnessConfig = null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
// Public API
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Run validation on all plugins listed in the registry.
|
|
93
|
+
* Checks plugin.json format, required fields, and companion files.
|
|
94
|
+
*
|
|
95
|
+
* @public
|
|
96
|
+
* @returns {{ valid: boolean, errors: object[], warnings: object[], summary: string }}
|
|
97
|
+
*/
|
|
98
|
+
HarnessBuild.prototype.validate = function validate() {
|
|
99
|
+
this._validationErrors = [];
|
|
100
|
+
this._validationWarnings = [];
|
|
101
|
+
|
|
102
|
+
var registry = this._readRegistry();
|
|
103
|
+
var pluginEntries = this._getPluginEntries(registry);
|
|
104
|
+
|
|
105
|
+
if (pluginEntries.length === 0) {
|
|
106
|
+
return {
|
|
107
|
+
valid: true,
|
|
108
|
+
errors: [],
|
|
109
|
+
warnings: [],
|
|
110
|
+
summary: 'Registry is empty, nothing to validate.',
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
for (var i = 0; i < pluginEntries.length; i++) {
|
|
115
|
+
this._validatePlugin(pluginEntries[i]);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
var valid = this._validationErrors.length === 0;
|
|
119
|
+
var summary = pluginValidator.formatValidationSummary({
|
|
120
|
+
totalPlugins: pluginEntries.length,
|
|
121
|
+
errors: this._validationErrors,
|
|
122
|
+
warnings: this._validationWarnings,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
valid: valid,
|
|
127
|
+
errors: this._validationErrors,
|
|
128
|
+
warnings: this._validationWarnings,
|
|
129
|
+
summary: summary,
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Build all plugins from the registry into Claude Code native format.
|
|
135
|
+
*
|
|
136
|
+
* @public
|
|
137
|
+
* @returns {{ success: boolean, built: object[], errors: object[], summary: string }}
|
|
138
|
+
*/
|
|
139
|
+
HarnessBuild.prototype.build = function build() {
|
|
140
|
+
this._buildResults = [];
|
|
141
|
+
|
|
142
|
+
// Validate configuration first - degrade to warning on failure
|
|
143
|
+
var configValidation = this.validateConfig();
|
|
144
|
+
if (!configValidation.valid) {
|
|
145
|
+
// Config validation failed - log warnings and continue with defaults
|
|
146
|
+
this._log('warn', 'Configuration file not found or invalid. Using default values.');
|
|
147
|
+
for (var i = 0; i < configValidation.errors.length; i++) {
|
|
148
|
+
this._log('warn', ' - ' + configValidation.errors[i]);
|
|
149
|
+
}
|
|
150
|
+
this._log('warn', 'Run "npm run init" to create a default configuration file.');
|
|
151
|
+
} else if (configValidation.warnings.length > 0) {
|
|
152
|
+
for (var j = 0; j < configValidation.warnings.length; j++) {
|
|
153
|
+
this._log('warn', 'Config warning: ' + configValidation.warnings[j]);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
var registry = this._readRegistry();
|
|
158
|
+
var pluginEntries = this._getPluginEntries(registry);
|
|
159
|
+
|
|
160
|
+
if (pluginEntries.length === 0) {
|
|
161
|
+
return {
|
|
162
|
+
success: true,
|
|
163
|
+
built: [],
|
|
164
|
+
errors: [],
|
|
165
|
+
summary: 'Registry is empty, build produced no output.',
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
var buildErrors = [];
|
|
170
|
+
|
|
171
|
+
for (var k = 0; k < pluginEntries.length; k++) {
|
|
172
|
+
try {
|
|
173
|
+
var result = this._buildPlugin(pluginEntries[k]);
|
|
174
|
+
this._buildResults.push(result);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
buildErrors.push({
|
|
177
|
+
plugin: pluginEntries[k].name || 'unknown',
|
|
178
|
+
error: err.message,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
var success = buildErrors.length === 0;
|
|
184
|
+
var summary = this._formatBuildSummary(this._buildResults, buildErrors);
|
|
185
|
+
|
|
186
|
+
// WP-D2 模型分级路由:把 roles 定义渲染为 Claude Code 自定义 agent 定义
|
|
187
|
+
// (.claude/agents/{roleId}.md,frontmatter 带 model)。降级纪律:任何失败
|
|
188
|
+
// 只 warn 不阻断 build(与 config 校验失败同级)。
|
|
189
|
+
try {
|
|
190
|
+
var agentDefGen = require('./agent-def-generator');
|
|
191
|
+
var rolesCfg = null;
|
|
192
|
+
try {
|
|
193
|
+
var _cm = require('./config-manager');
|
|
194
|
+
var _all = new _cm().getAll();
|
|
195
|
+
rolesCfg = (_all && _all.roles) || null;
|
|
196
|
+
} catch (_e) { /* 无配置 → 默认开 */ }
|
|
197
|
+
var enabled = !(rolesCfg && rolesCfg.generate_agent_defs === false);
|
|
198
|
+
var genResult = agentDefGen.generateAgentDefs({
|
|
199
|
+
projectRoot: this.projectRoot || process.cwd(),
|
|
200
|
+
enabled: enabled,
|
|
201
|
+
rolesDir: (rolesCfg && rolesCfg.roles_dir)
|
|
202
|
+
? (path.isAbsolute(rolesCfg.roles_dir) ? rolesCfg.roles_dir : path.join(this.projectRoot || process.cwd(), rolesCfg.roles_dir))
|
|
203
|
+
: undefined,
|
|
204
|
+
});
|
|
205
|
+
if (genResult.generated.length > 0) {
|
|
206
|
+
this._log('info', 'Agent defs generated: ' + genResult.generated.length +
|
|
207
|
+
' (WP-D2 per-role model routing)');
|
|
208
|
+
}
|
|
209
|
+
for (var gi = 0; gi < genResult.errors.length; gi++) {
|
|
210
|
+
this._log('warn', ' - agent-def ' + genResult.errors[gi].file + ': ' + genResult.errors[gi].error);
|
|
211
|
+
}
|
|
212
|
+
} catch (_genErr) { /* 生成器缺失/异常 → 跳过(零回归) */ }
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
success: success,
|
|
216
|
+
built: this._buildResults,
|
|
217
|
+
errors: buildErrors,
|
|
218
|
+
summary: summary,
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Validate the harness-config.yaml file.
|
|
224
|
+
* Uses ConfigValidator to check structure and report issues.
|
|
225
|
+
*
|
|
226
|
+
* @public
|
|
227
|
+
* @returns {{ valid: boolean, errors: string[], warnings: string[], summary: string }}
|
|
228
|
+
*/
|
|
229
|
+
HarnessBuild.prototype.validateConfig = function validateConfig() {
|
|
230
|
+
var ConfigValidator = require('./config-validator');
|
|
231
|
+
var validator = new ConfigValidator();
|
|
232
|
+
|
|
233
|
+
var configPath = path.join(this._rootDir, '.claude', 'config', 'harness-config.yaml');
|
|
234
|
+
|
|
235
|
+
var result = validator.validateFile(configPath);
|
|
236
|
+
|
|
237
|
+
var summary = '[Config Validation] ';
|
|
238
|
+
if (result.valid) {
|
|
239
|
+
summary += 'OK - Configuration is valid';
|
|
240
|
+
if (result.warnings.length > 0) {
|
|
241
|
+
summary += ' (' + result.warnings.length + ' warning' + (result.warnings.length > 1 ? 's' : '') + ')';
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
summary += 'FAILED - ' + result.errors.length + ' error' + (result.errors.length > 1 ? 's' : '') + ' found';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
valid: result.valid,
|
|
249
|
+
errors: result.errors,
|
|
250
|
+
warnings: result.warnings,
|
|
251
|
+
summary: summary,
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Merge doukui hooks into the target project's .claude/settings.json.
|
|
257
|
+
* Delegates to settings-merger module.
|
|
258
|
+
*
|
|
259
|
+
* @public
|
|
260
|
+
* @param {string} targetRoot - target project root directory
|
|
261
|
+
* @param {string} packageRoot - this package's root directory (node_modules/doukui/)
|
|
262
|
+
*/
|
|
263
|
+
HarnessBuild.prototype.updateSettings = function updateSettings(targetRoot, packageRoot) {
|
|
264
|
+
return settingsMerger.mergeSettings({
|
|
265
|
+
targetRoot: targetRoot,
|
|
266
|
+
packageRoot: packageRoot,
|
|
267
|
+
ensureDir: this._ensureDir.bind(this),
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Inject doukui managed rules into the target project's CLAUDE.md.
|
|
273
|
+
* Delegates to claude-md-injector module.
|
|
274
|
+
*
|
|
275
|
+
* @public
|
|
276
|
+
* @param {string} targetRoot - target project root directory
|
|
277
|
+
*/
|
|
278
|
+
HarnessBuild.prototype.injectClaudeMdRules = function injectClaudeMdRules(targetRoot) {
|
|
279
|
+
var registry = this._readRegistry();
|
|
280
|
+
var pluginEntries = this._getPluginEntries(registry);
|
|
281
|
+
|
|
282
|
+
return claudeMdInjector.injectClaudeMdRules({
|
|
283
|
+
targetRoot: targetRoot,
|
|
284
|
+
pluginEntries: pluginEntries,
|
|
285
|
+
resolvePluginDir: this._resolvePluginDir.bind(this),
|
|
286
|
+
log: this._log.bind(this),
|
|
287
|
+
});
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// ---------------------------------------------------------------------------
|
|
291
|
+
// Registry reading
|
|
292
|
+
// ---------------------------------------------------------------------------
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Read and parse the plugin registry.
|
|
296
|
+
* Uses manifest-resolver to merge global registry with project manifest.
|
|
297
|
+
* @internal
|
|
298
|
+
* @returns {object}
|
|
299
|
+
*/
|
|
300
|
+
HarnessBuild.prototype._readRegistry = function _readRegistry() {
|
|
301
|
+
try {
|
|
302
|
+
var ManifestResolver = require('./manifest-resolver');
|
|
303
|
+
return ManifestResolver.resolveEffectivePlugins(this._packageRoot, this._targetRoot);
|
|
304
|
+
} catch (err) {
|
|
305
|
+
this._log('warn', 'Could not read registry: ' + err.message + '. Using empty registry.');
|
|
306
|
+
return { version: '1.0.0', plugins: [] };
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Extract plugin entries from the registry.
|
|
312
|
+
* Supports array format: [{ name, source, enabled, config }]
|
|
313
|
+
*
|
|
314
|
+
* @internal
|
|
315
|
+
* @param {object} registry
|
|
316
|
+
* @returns {object[]}
|
|
317
|
+
*/
|
|
318
|
+
HarnessBuild.prototype._getPluginEntries = function _getPluginEntries(registry) {
|
|
319
|
+
var plugins = registry.plugins;
|
|
320
|
+
if (!plugins || !Array.isArray(plugins)) {
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
// Filter out disabled plugins
|
|
324
|
+
var entries = [];
|
|
325
|
+
for (var i = 0; i < plugins.length; i++) {
|
|
326
|
+
var entry = plugins[i];
|
|
327
|
+
if (entry && entry.enabled !== false) {
|
|
328
|
+
entries.push(entry);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return entries;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
// ---------------------------------------------------------------------------
|
|
335
|
+
// Validation (delegates to plugin-validator, with capabilities check)
|
|
336
|
+
// ---------------------------------------------------------------------------
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Validate a single plugin entry.
|
|
340
|
+
* Uses plugin-validator for structural checks, then adds capabilities validation.
|
|
341
|
+
* @internal
|
|
342
|
+
* @param {object} entry - registry entry with at least a name field
|
|
343
|
+
*/
|
|
344
|
+
HarnessBuild.prototype._validatePlugin = function _validatePlugin(entry) {
|
|
345
|
+
var pluginDir = this._resolvePluginDir(entry);
|
|
346
|
+
|
|
347
|
+
// Delegate structural validation to plugin-validator
|
|
348
|
+
var result = pluginValidator.validatePlugin(entry, pluginDir);
|
|
349
|
+
this._validationErrors = this._validationErrors.concat(result.errors);
|
|
350
|
+
this._validationWarnings = this._validationWarnings.concat(result.warnings);
|
|
351
|
+
|
|
352
|
+
// Capabilities field validation (v0.2.0 security minimum)
|
|
353
|
+
// Check if plugin directory and meta exist (validator may have returned early)
|
|
354
|
+
if (fs.existsSync(pluginDir)) {
|
|
355
|
+
var metaPath = path.join(pluginDir, 'plugin.json');
|
|
356
|
+
if (fs.existsSync(metaPath)) {
|
|
357
|
+
try {
|
|
358
|
+
var meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
|
|
359
|
+
if (meta.capabilities) {
|
|
360
|
+
var capWarnings = pluginValidator.validateCapabilities(meta.capabilities);
|
|
361
|
+
var pluginName = entry.name || entry.source || 'unknown';
|
|
362
|
+
for (var c = 0; c < capWarnings.length; c++) {
|
|
363
|
+
this._validationWarnings.push({
|
|
364
|
+
plugin: pluginName,
|
|
365
|
+
field: capWarnings[c].field,
|
|
366
|
+
message: capWarnings[c].message,
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
} catch (e) {
|
|
371
|
+
// already caught by validatePlugin
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
// ---------------------------------------------------------------------------
|
|
378
|
+
// Building
|
|
379
|
+
// ---------------------------------------------------------------------------
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Resolve the plugin directory from a registry entry.
|
|
383
|
+
* Uses entry.source if provided, otherwise derives from entry.name.
|
|
384
|
+
*
|
|
385
|
+
* @internal
|
|
386
|
+
* @param {object} entry
|
|
387
|
+
* @returns {string}
|
|
388
|
+
*/
|
|
389
|
+
HarnessBuild.prototype._resolvePluginDir = function _resolvePluginDir(entry) {
|
|
390
|
+
var resolvePluginPath = require('./resolve-plugin-path').resolvePluginPath;
|
|
391
|
+
var registryDir = path.resolve(path.dirname(this._registryPath));
|
|
392
|
+
return resolvePluginPath(entry, this._pluginsDir, registryDir);
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Build a single plugin.
|
|
397
|
+
*
|
|
398
|
+
* @internal
|
|
399
|
+
* @param {object} entry - registry entry
|
|
400
|
+
* @returns {{ name: string, type: string, outputPath: string, files: string[] }}
|
|
401
|
+
*/
|
|
402
|
+
HarnessBuild.prototype._buildPlugin = function _buildPlugin(entry) {
|
|
403
|
+
var pluginDir;
|
|
404
|
+
try {
|
|
405
|
+
pluginDir = this._resolvePluginDir(entry);
|
|
406
|
+
} catch (resolveErr) {
|
|
407
|
+
throw new Error('Path resolution failed for plugin "' + (entry.name || 'unknown') + '": ' + resolveErr.message);
|
|
408
|
+
}
|
|
409
|
+
var metaPath = path.join(pluginDir, 'plugin.json');
|
|
410
|
+
|
|
411
|
+
// Read plugin metadata
|
|
412
|
+
if (!fs.existsSync(metaPath)) {
|
|
413
|
+
throw new Error('plugin.json not found in ' + pluginDir);
|
|
414
|
+
}
|
|
415
|
+
var meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
|
|
416
|
+
|
|
417
|
+
var pluginName = meta.name || entry.name;
|
|
418
|
+
var pluginType = meta.type;
|
|
419
|
+
|
|
420
|
+
this._log('info', 'Building plugin: ' + pluginName + ' (type: ' + pluginType + ')');
|
|
421
|
+
|
|
422
|
+
// External plugin source warning (npm/local sourceType only)
|
|
423
|
+
var sourceType = entry.sourceType || 'core';
|
|
424
|
+
if (sourceType === 'npm' || sourceType === 'local') {
|
|
425
|
+
this._log('warn', 'Building external plugin: ' + pluginName + ' (source: ' + sourceType + ')');
|
|
426
|
+
if (meta.capabilities && Object.keys(meta.capabilities).length > 0) {
|
|
427
|
+
this._log('warn', ' Declared capabilities: ' + Object.keys(meta.capabilities).join(', '));
|
|
428
|
+
} else {
|
|
429
|
+
this._log('warn', ' No capabilities declared (limited to basic services)');
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Dispatch by type
|
|
434
|
+
switch (pluginType) {
|
|
435
|
+
case 'skill':
|
|
436
|
+
return this._buildSkillPlugin(pluginName, pluginDir, meta);
|
|
437
|
+
case 'hook':
|
|
438
|
+
return this._buildHookPlugin(pluginName, pluginDir, meta);
|
|
439
|
+
case 'validator':
|
|
440
|
+
return this._buildValidatorPlugin(pluginName, pluginDir, meta);
|
|
441
|
+
case 'provider':
|
|
442
|
+
return this._buildProviderPlugin(pluginName, pluginDir, meta);
|
|
443
|
+
default:
|
|
444
|
+
throw new Error('Unknown plugin type: ' + pluginType);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Build a skill plugin.
|
|
450
|
+
* Copies skill.md from plugin directory to .claude/skills/{name}/skill.md.
|
|
451
|
+
*
|
|
452
|
+
* @internal
|
|
453
|
+
* @param {string} name
|
|
454
|
+
* @param {string} pluginDir
|
|
455
|
+
* @param {object} meta
|
|
456
|
+
* @returns {{ name: string, type: string, outputPath: string, files: string[] }}
|
|
457
|
+
*/
|
|
458
|
+
HarnessBuild.prototype._buildSkillPlugin = function _buildSkillPlugin(name, pluginDir, meta) {
|
|
459
|
+
var skillMdSrc = path.join(pluginDir, 'skill.md');
|
|
460
|
+
var outputDir = path.join(this._outputSkillsDir, name);
|
|
461
|
+
var skillMdDest = path.join(outputDir, 'skill.md');
|
|
462
|
+
var files = [];
|
|
463
|
+
|
|
464
|
+
// Ensure output directory exists
|
|
465
|
+
this._ensureDir(outputDir);
|
|
466
|
+
|
|
467
|
+
// Copy companion reference files (*.md except skill.md) to output directory
|
|
468
|
+
var companionFiles = fs.readdirSync(pluginDir).filter(function(f) {
|
|
469
|
+
return f !== 'skill.md' && f !== 'plugin.json' && f.endsWith('.md');
|
|
470
|
+
});
|
|
471
|
+
companionFiles.forEach(function(refFile) {
|
|
472
|
+
var src = path.join(pluginDir, refFile);
|
|
473
|
+
var dest = path.join(outputDir, refFile);
|
|
474
|
+
fs.writeFileSync(dest, fs.readFileSync(src, 'utf-8'), 'utf-8');
|
|
475
|
+
files.push(dest);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
// Check if source skill.md exists
|
|
479
|
+
if (fs.existsSync(skillMdSrc)) {
|
|
480
|
+
// Read the source skill content
|
|
481
|
+
var content = fs.readFileSync(skillMdSrc, 'utf-8');
|
|
482
|
+
|
|
483
|
+
// Replace source-relative paths with output-relative paths
|
|
484
|
+
var sourcePath = 'plugins/core/' + name + '/';
|
|
485
|
+
var targetPath = '.claude/skills/' + name + '/';
|
|
486
|
+
content = content.split(sourcePath).join(targetPath);
|
|
487
|
+
|
|
488
|
+
// Replace __DOUKUI_CLI_PATH__ placeholder with actual CLI path
|
|
489
|
+
if (this._cliPath) {
|
|
490
|
+
var cliPath = this._cliPath.replace(/\\/g, '/'); // Normalize to forward slashes
|
|
491
|
+
content = content.split('__DOUKUI_CLI_PATH__').join(cliPath);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// If the skill.md has a front-matter header, keep it.
|
|
495
|
+
// If not, generate one from plugin.json metadata.
|
|
496
|
+
if (!this._hasFrontMatter(content)) {
|
|
497
|
+
content = this._generateSkillFrontMatter(meta) + '\n' + content;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// Inject context window configuration
|
|
501
|
+
content = this._injectContextConfig(content, name);
|
|
502
|
+
|
|
503
|
+
fs.writeFileSync(skillMdDest, content, 'utf-8');
|
|
504
|
+
files.push(skillMdDest);
|
|
505
|
+
this._log('info', ' -> ' + skillMdDest);
|
|
506
|
+
} else {
|
|
507
|
+
// Generate a minimal skill.md from metadata
|
|
508
|
+
var generated = this._generateSkillContent(meta);
|
|
509
|
+
fs.writeFileSync(skillMdDest, generated, 'utf-8');
|
|
510
|
+
files.push(skillMdDest);
|
|
511
|
+
this._log('info', ' -> ' + skillMdDest + ' (generated from metadata)');
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
return {
|
|
515
|
+
name: name,
|
|
516
|
+
type: 'skill',
|
|
517
|
+
outputPath: outputDir,
|
|
518
|
+
files: files,
|
|
519
|
+
};
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Build a hook plugin.
|
|
524
|
+
* Copies index.js from plugin directory to .claude/hooks/{name}/index.js.
|
|
525
|
+
*
|
|
526
|
+
* @internal
|
|
527
|
+
* @param {string} name
|
|
528
|
+
* @param {string} pluginDir
|
|
529
|
+
* @param {object} meta
|
|
530
|
+
* @returns {{ name: string, type: string, outputPath: string, files: string[] }}
|
|
531
|
+
*/
|
|
532
|
+
HarnessBuild.prototype._buildHookPlugin = function _buildHookPlugin(name, pluginDir, meta) {
|
|
533
|
+
var indexJsSrc = path.join(pluginDir, 'index.js');
|
|
534
|
+
var outputDir = path.join(this._outputHooksDir, name);
|
|
535
|
+
var indexJsDest = path.join(outputDir, 'index.js');
|
|
536
|
+
var files = [];
|
|
537
|
+
|
|
538
|
+
this._ensureDir(outputDir);
|
|
539
|
+
|
|
540
|
+
if (fs.existsSync(indexJsSrc)) {
|
|
541
|
+
var content = fs.readFileSync(indexJsSrc, 'utf-8');
|
|
542
|
+
fs.writeFileSync(indexJsDest, content, 'utf-8');
|
|
543
|
+
files.push(indexJsDest);
|
|
544
|
+
this._log('info', ' -> ' + indexJsDest);
|
|
545
|
+
} else {
|
|
546
|
+
// Generate a stub hook
|
|
547
|
+
var stub = this._generateHookStub(meta);
|
|
548
|
+
fs.writeFileSync(indexJsDest, stub, 'utf-8');
|
|
549
|
+
files.push(indexJsDest);
|
|
550
|
+
this._log('info', ' -> ' + indexJsDest + ' (stub generated)');
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
return {
|
|
554
|
+
name: name,
|
|
555
|
+
type: 'hook',
|
|
556
|
+
outputPath: outputDir,
|
|
557
|
+
files: files,
|
|
558
|
+
};
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Build a validator plugin.
|
|
563
|
+
* Validator plugins don't have a native Claude Code output format,
|
|
564
|
+
* so we record them but don't write files to skills/hooks.
|
|
565
|
+
*
|
|
566
|
+
* @internal
|
|
567
|
+
* @param {string} name
|
|
568
|
+
* @param {string} pluginDir
|
|
569
|
+
* @param {object} meta
|
|
570
|
+
* @returns {{ name: string, type: string, outputPath: string, files: string[] }}
|
|
571
|
+
*/
|
|
572
|
+
HarnessBuild.prototype._buildValidatorPlugin = function _buildValidatorPlugin(name, pluginDir, meta) {
|
|
573
|
+
this._log('info', ' -> Validator plugin (no native output, registered internally)');
|
|
574
|
+
|
|
575
|
+
return {
|
|
576
|
+
name: name,
|
|
577
|
+
type: 'validator',
|
|
578
|
+
outputPath: '(internal)',
|
|
579
|
+
files: [],
|
|
580
|
+
};
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Build a provider plugin.
|
|
585
|
+
* Provider plugins are runtime-only, no native Claude Code output.
|
|
586
|
+
* If metadata.deploy_assets is true, copy assets/ directory to .claude/{deploy_path}/.
|
|
587
|
+
*
|
|
588
|
+
* @internal
|
|
589
|
+
* @param {string} name
|
|
590
|
+
* @param {string} pluginDir
|
|
591
|
+
* @param {object} meta
|
|
592
|
+
* @returns {{ name: string, type: string, outputPath: string, files: string[] }}
|
|
593
|
+
*/
|
|
594
|
+
HarnessBuild.prototype._buildProviderPlugin = function _buildProviderPlugin(name, pluginDir, meta) {
|
|
595
|
+
var files = [];
|
|
596
|
+
|
|
597
|
+
// Check for asset deployment
|
|
598
|
+
if (meta.metadata && meta.metadata.deploy_assets) {
|
|
599
|
+
var assetsDir = path.join(pluginDir, 'assets');
|
|
600
|
+
var deployPath = meta.metadata.deploy_path || name;
|
|
601
|
+
var outputDir = path.join(this._rootDir, '.claude', deployPath);
|
|
602
|
+
|
|
603
|
+
if (fs.existsSync(assetsDir)) {
|
|
604
|
+
this._log('info', ' -> Deploying provider assets to ' + outputDir);
|
|
605
|
+
files = this._copyDirectory(assetsDir, outputDir);
|
|
606
|
+
this._log('info', ' -> ' + files.length + ' files deployed');
|
|
607
|
+
} else {
|
|
608
|
+
this._log('warn', ' -> deploy_assets set but no assets/ directory found in ' + pluginDir);
|
|
609
|
+
}
|
|
610
|
+
} else {
|
|
611
|
+
this._log('info', ' -> Provider plugin (no native output, registered internally)');
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
return {
|
|
615
|
+
name: name,
|
|
616
|
+
type: 'provider',
|
|
617
|
+
outputPath: files.length > 0 ? path.join(this._rootDir, '.claude', deployPath) : '(internal)',
|
|
618
|
+
files: files,
|
|
619
|
+
};
|
|
620
|
+
};
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Format build summary for output.
|
|
624
|
+
* Enhanced with installation path and global mode indicator.
|
|
625
|
+
*
|
|
626
|
+
* @internal
|
|
627
|
+
* @param {object[]} results
|
|
628
|
+
* @param {object[]} errors
|
|
629
|
+
* @returns {string}
|
|
630
|
+
*/
|
|
631
|
+
HarnessBuild.prototype._formatBuildSummary = function _formatBuildSummary(results, errors) {
|
|
632
|
+
var lines = [];
|
|
633
|
+
lines.push('');
|
|
634
|
+
lines.push('=== Build Report ===');
|
|
635
|
+
|
|
636
|
+
var skillCount = 0;
|
|
637
|
+
var hookCount = 0;
|
|
638
|
+
var validatorCount = 0;
|
|
639
|
+
var providerCount = 0;
|
|
640
|
+
var totalFiles = 0;
|
|
641
|
+
|
|
642
|
+
for (var i = 0; i < results.length; i++) {
|
|
643
|
+
var r = results[i];
|
|
644
|
+
totalFiles += r.files.length;
|
|
645
|
+
switch (r.type) {
|
|
646
|
+
case 'skill': skillCount++; break;
|
|
647
|
+
case 'hook': hookCount++; break;
|
|
648
|
+
case 'validator': validatorCount++; break;
|
|
649
|
+
case 'provider': providerCount++; break;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// Show installation mode
|
|
654
|
+
if (this._globalMode) {
|
|
655
|
+
lines.push('Installation: Global (home directory)');
|
|
656
|
+
} else {
|
|
657
|
+
lines.push('Installation: Project (local)');
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// Show output paths
|
|
661
|
+
lines.push('Skills output: ' + this._outputSkillsDir);
|
|
662
|
+
lines.push('Hooks output: ' + this._outputHooksDir);
|
|
663
|
+
lines.push('');
|
|
664
|
+
|
|
665
|
+
lines.push('Plugins built: ' + results.length);
|
|
666
|
+
lines.push(' Skills: ' + skillCount);
|
|
667
|
+
lines.push(' Hooks: ' + hookCount);
|
|
668
|
+
lines.push(' Validators: ' + validatorCount);
|
|
669
|
+
lines.push(' Providers: ' + providerCount);
|
|
670
|
+
lines.push('Files written: ' + totalFiles);
|
|
671
|
+
|
|
672
|
+
if (results.length > 0) {
|
|
673
|
+
lines.push('');
|
|
674
|
+
lines.push('--- Output Details ---');
|
|
675
|
+
for (var j = 0; j < results.length; j++) {
|
|
676
|
+
var item = results[j];
|
|
677
|
+
lines.push(' [' + item.type + '] ' + item.name + ' -> ' + item.outputPath);
|
|
678
|
+
for (var k = 0; k < item.files.length; k++) {
|
|
679
|
+
lines.push(' ' + item.files[k]);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
if (errors.length > 0) {
|
|
685
|
+
lines.push('');
|
|
686
|
+
lines.push('--- Build Errors ---');
|
|
687
|
+
for (var m = 0; m < errors.length; m++) {
|
|
688
|
+
lines.push(' [' + errors[m].plugin + '] ' + errors[m].error);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
lines.push('');
|
|
693
|
+
lines.push(errors.length === 0 ? 'Build SUCCEEDED' : 'Build COMPLETED WITH ERRORS');
|
|
694
|
+
lines.push('');
|
|
695
|
+
|
|
696
|
+
return lines.join('\n');
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
// ---------------------------------------------------------------------------
|
|
700
|
+
// Content generation helpers
|
|
701
|
+
// ---------------------------------------------------------------------------
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Check if a skill.md already has YAML front matter.
|
|
705
|
+
* @internal
|
|
706
|
+
* @param {string} content
|
|
707
|
+
* @returns {boolean}
|
|
708
|
+
*/
|
|
709
|
+
HarnessBuild.prototype._hasFrontMatter = function _hasFrontMatter(content) {
|
|
710
|
+
// B13: trimLeft is deprecated (alias of trimStart); use trimStart for
|
|
711
|
+
// forward compatibility with strict mode / future Node versions.
|
|
712
|
+
return String(content).trimStart().indexOf('---') === 0;
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* Generate YAML front matter for a skill from plugin metadata.
|
|
717
|
+
* @internal
|
|
718
|
+
* @param {object} meta
|
|
719
|
+
* @returns {string}
|
|
720
|
+
*/
|
|
721
|
+
HarnessBuild.prototype._generateSkillFrontMatter = function _generateSkillFrontMatter(meta) {
|
|
722
|
+
var lines = ['---'];
|
|
723
|
+
lines.push('name: ' + (meta.name || ''));
|
|
724
|
+
lines.push('description: ' + (meta.description || ''));
|
|
725
|
+
|
|
726
|
+
if (meta.triggers && meta.triggers.length > 0) {
|
|
727
|
+
lines.push('triggers:');
|
|
728
|
+
for (var i = 0; i < meta.triggers.length; i++) {
|
|
729
|
+
lines.push(' - ' + meta.triggers[i]);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
if (meta.config) {
|
|
734
|
+
if (meta.config.plan_mode_required) {
|
|
735
|
+
lines.push('plan_mode_required: true');
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
lines.push('---');
|
|
740
|
+
return lines.join('\n');
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* Generate a full skill.md content from plugin metadata when no skill.md exists.
|
|
745
|
+
* @internal
|
|
746
|
+
* @param {object} meta
|
|
747
|
+
* @returns {string}
|
|
748
|
+
*/
|
|
749
|
+
HarnessBuild.prototype._generateSkillContent = function _generateSkillContent(meta) {
|
|
750
|
+
var frontMatter = this._generateSkillFrontMatter(meta);
|
|
751
|
+
|
|
752
|
+
var body = '\n# ' + (meta.name || 'Unnamed Skill') + '\n';
|
|
753
|
+
body += '\n' + (meta.description || '') + '\n';
|
|
754
|
+
|
|
755
|
+
if (meta.triggers && meta.triggers.length > 0) {
|
|
756
|
+
body += '\n## Triggers\n';
|
|
757
|
+
for (var i = 0; i < meta.triggers.length; i++) {
|
|
758
|
+
body += '- ' + meta.triggers[i] + '\n';
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
body += '\n> Auto-generated by harness-build from plugin.json metadata.\n';
|
|
763
|
+
|
|
764
|
+
return frontMatter + body;
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* Generate a stub hook index.js from plugin metadata.
|
|
769
|
+
* @internal
|
|
770
|
+
* @param {object} meta
|
|
771
|
+
* @returns {string}
|
|
772
|
+
*/
|
|
773
|
+
HarnessBuild.prototype._generateHookStub = function _generateHookStub(meta) {
|
|
774
|
+
var lines = [
|
|
775
|
+
'/**',
|
|
776
|
+
' * Hook plugin: ' + (meta.name || 'unnamed'),
|
|
777
|
+
' *',
|
|
778
|
+
' * Auto-generated stub by harness-build.',
|
|
779
|
+
' * Replace with actual hook implementation.',
|
|
780
|
+
' */',
|
|
781
|
+
'',
|
|
782
|
+
'\'use strict\';',
|
|
783
|
+
'',
|
|
784
|
+
'module.exports = {',
|
|
785
|
+
' name: \'' + (meta.name || 'unnamed-hook') + '\',',
|
|
786
|
+
' version: \'' + (meta.version || '0.0.0') + '\',',
|
|
787
|
+
'',
|
|
788
|
+
' /**',
|
|
789
|
+
' * Handle hook invocation.',
|
|
790
|
+
' * @param {object} context',
|
|
791
|
+
' * @returns {Promise<{ allowed: boolean, reason?: string }>}',
|
|
792
|
+
' */',
|
|
793
|
+
' async handle(context) {',
|
|
794
|
+
' return { allowed: true };',
|
|
795
|
+
' },',
|
|
796
|
+
'};',
|
|
797
|
+
'',
|
|
798
|
+
];
|
|
799
|
+
return lines.join('\n');
|
|
800
|
+
};
|
|
801
|
+
|
|
802
|
+
// ---------------------------------------------------------------------------
|
|
803
|
+
// YAML config reading (delegates to yaml-parser)
|
|
804
|
+
// ---------------------------------------------------------------------------
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* Read and cache the harness config YAML file.
|
|
808
|
+
* Delegates to yaml-parser module.
|
|
809
|
+
* @internal
|
|
810
|
+
* @returns {object}
|
|
811
|
+
*/
|
|
812
|
+
HarnessBuild.prototype._readHarnessConfig = function _readHarnessConfig() {
|
|
813
|
+
if (this._harnessConfig !== null) {
|
|
814
|
+
return this._harnessConfig;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
var configPath = path.join(this._rootDir, '.claude', 'config', 'harness-config.yaml');
|
|
818
|
+
this._harnessConfig = yamlParser.parseYamlFile(configPath);
|
|
819
|
+
return this._harnessConfig;
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
// ---------------------------------------------------------------------------
|
|
823
|
+
// Context injection (uses yaml-parser for serialization)
|
|
824
|
+
// ---------------------------------------------------------------------------
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Inject context window configuration into a skill.md file.
|
|
828
|
+
* Inserts a <!-- CONTEXT-CONFIG --> block after front matter.
|
|
829
|
+
*
|
|
830
|
+
* In global mode, uses default context config instead of reading from project config.
|
|
831
|
+
*
|
|
832
|
+
* @internal
|
|
833
|
+
* @param {string} content - the skill.md content
|
|
834
|
+
* @param {string} pluginName - the plugin name for per-plugin overrides
|
|
835
|
+
* @returns {string} content with injected config block
|
|
836
|
+
*/
|
|
837
|
+
HarnessBuild.prototype._injectContextConfig = function _injectContextConfig(content, pluginName) {
|
|
838
|
+
var relevantConfig = null;
|
|
839
|
+
var configBlockName = '';
|
|
840
|
+
|
|
841
|
+
if (this._globalMode) {
|
|
842
|
+
// Global mode: use default context config
|
|
843
|
+
relevantConfig = {
|
|
844
|
+
enabled: true,
|
|
845
|
+
chunk_size: 120000,
|
|
846
|
+
overlap: 2000,
|
|
847
|
+
thresholds: {
|
|
848
|
+
small: 50000,
|
|
849
|
+
medium: 150000,
|
|
850
|
+
large: 300000
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
configBlockName = 'CONTEXT-CONFIG';
|
|
854
|
+
} else {
|
|
855
|
+
// Project mode: read from harness config
|
|
856
|
+
var config = this._readHarnessConfig();
|
|
857
|
+
if (!config || Object.keys(config).length === 0) {
|
|
858
|
+
return content; // No config, skip injection
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// Determine which config section to inject based on plugin name
|
|
862
|
+
if ((pluginName === 'skill-agent-dispatcher' || pluginName === 'agent-dispatcher') && config.agent_dispatcher) {
|
|
863
|
+
relevantConfig = config.agent_dispatcher;
|
|
864
|
+
configBlockName = 'AGENT-DISPATCHER-CONFIG';
|
|
865
|
+
} else if (config.context_window) {
|
|
866
|
+
relevantConfig = config.context_window;
|
|
867
|
+
configBlockName = 'CONTEXT-CONFIG';
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (!relevantConfig || Object.keys(relevantConfig).length === 0) {
|
|
871
|
+
return content;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// Apply per-plugin override if exists (only for context_window)
|
|
875
|
+
if (config.overrides && config.overrides[pluginName] && relevantConfig === config.context_window) {
|
|
876
|
+
var override = config.overrides[pluginName];
|
|
877
|
+
// B6: shallow-clone the shared config.context_window before applying the
|
|
878
|
+
// override so this plugin's override does NOT leak into subsequent skills
|
|
879
|
+
// (the previous code wrote directly onto the shared reference).
|
|
880
|
+
relevantConfig = {};
|
|
881
|
+
var _ck = Object.keys(config.context_window);
|
|
882
|
+
for (var _ci = 0; _ci < _ck.length; _ci++) {
|
|
883
|
+
relevantConfig[_ck[_ci]] = config.context_window[_ck[_ci]];
|
|
884
|
+
}
|
|
885
|
+
for (var k in override) {
|
|
886
|
+
if (override.hasOwnProperty(k)) {
|
|
887
|
+
relevantConfig[k] = override[k];
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
// Build config block
|
|
894
|
+
var lines = ['\n<!-- ' + configBlockName];
|
|
895
|
+
for (var key in relevantConfig) {
|
|
896
|
+
if (!relevantConfig.hasOwnProperty(key)) continue;
|
|
897
|
+
var val = relevantConfig[key];
|
|
898
|
+
if (Array.isArray(val)) {
|
|
899
|
+
lines.push(key + ': ' + yamlParser.serializeConfigValue(val));
|
|
900
|
+
} else if (typeof val === 'object' && val !== null) {
|
|
901
|
+
// Flatten nested objects (e.g., thresholds)
|
|
902
|
+
var parts = [];
|
|
903
|
+
for (var sk in val) {
|
|
904
|
+
if (val.hasOwnProperty(sk)) {
|
|
905
|
+
parts.push(sk + '=' + yamlParser.serializeConfigValue(val[sk]));
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
lines.push(key + ': ' + parts.join(', '));
|
|
909
|
+
} else {
|
|
910
|
+
lines.push(key + ': ' + val);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
lines.push(configBlockName + ' -->\n');
|
|
914
|
+
|
|
915
|
+
var configBlock = lines.join('\n');
|
|
916
|
+
|
|
917
|
+
// Find insertion point: after front matter closing ---
|
|
918
|
+
var fmCloseIdx = content.indexOf('---', 1); // skip first ---
|
|
919
|
+
if (fmCloseIdx === -1) {
|
|
920
|
+
// No front matter, prepend
|
|
921
|
+
return configBlock + content;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
var afterFm = content.indexOf('\n', fmCloseIdx + 3);
|
|
925
|
+
if (afterFm === -1) {
|
|
926
|
+
afterFm = content.length;
|
|
927
|
+
} else {
|
|
928
|
+
afterFm += 1; // skip the newline itself
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
return content.substring(0, afterFm) + configBlock + content.substring(afterFm);
|
|
932
|
+
};
|
|
933
|
+
|
|
934
|
+
// ---------------------------------------------------------------------------
|
|
935
|
+
// Utilities
|
|
936
|
+
// ---------------------------------------------------------------------------
|
|
937
|
+
|
|
938
|
+
/**
|
|
939
|
+
* Ensure a directory exists, creating it recursively if needed.
|
|
940
|
+
* @internal
|
|
941
|
+
* @param {string} dirPath
|
|
942
|
+
*/
|
|
943
|
+
HarnessBuild.prototype._ensureDir = function _ensureDir(dirPath) {
|
|
944
|
+
if (!fs.existsSync(dirPath)) {
|
|
945
|
+
this._mkdirRecursive(dirPath);
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* Recursively create directories (like mkdir -p).
|
|
951
|
+
* @internal
|
|
952
|
+
* @param {string} dirPath
|
|
953
|
+
*/
|
|
954
|
+
HarnessBuild.prototype._mkdirRecursive = function _mkdirRecursive(dirPath) {
|
|
955
|
+
var parent = path.dirname(dirPath);
|
|
956
|
+
if (!fs.existsSync(parent)) {
|
|
957
|
+
this._mkdirRecursive(parent);
|
|
958
|
+
}
|
|
959
|
+
fs.mkdirSync(dirPath);
|
|
960
|
+
};
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* Recursively copy a directory.
|
|
964
|
+
* @internal
|
|
965
|
+
* @param {string} srcDir - Source directory
|
|
966
|
+
* @param {string} destDir - Destination directory
|
|
967
|
+
* @returns {string[]} List of copied file paths
|
|
968
|
+
*/
|
|
969
|
+
HarnessBuild.prototype._copyDirectory = function _copyDirectory(srcDir, destDir) {
|
|
970
|
+
var copiedFiles = [];
|
|
971
|
+
|
|
972
|
+
if (!fs.existsSync(destDir)) {
|
|
973
|
+
this._mkdirRecursive(destDir);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
var entries = fs.readdirSync(srcDir);
|
|
977
|
+
for (var i = 0; i < entries.length; i++) {
|
|
978
|
+
var entry = entries[i];
|
|
979
|
+
var srcPath = path.join(srcDir, entry);
|
|
980
|
+
var destPath = path.join(destDir, entry);
|
|
981
|
+
var stat = fs.statSync(srcPath);
|
|
982
|
+
|
|
983
|
+
if (stat.isDirectory()) {
|
|
984
|
+
var subFiles = this._copyDirectory(srcPath, destPath);
|
|
985
|
+
copiedFiles = copiedFiles.concat(subFiles);
|
|
986
|
+
} else {
|
|
987
|
+
fs.writeFileSync(destPath, fs.readFileSync(srcPath));
|
|
988
|
+
copiedFiles.push(destPath);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
return copiedFiles;
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* Internal logging.
|
|
997
|
+
* @internal
|
|
998
|
+
* @param {string} level
|
|
999
|
+
* @param {string} message
|
|
1000
|
+
*/
|
|
1001
|
+
HarnessBuild.prototype._log = function _log(level, message) {
|
|
1002
|
+
// Skip info messages unless verbose mode is enabled
|
|
1003
|
+
if (level === 'info' && !this._verbose) {
|
|
1004
|
+
return;
|
|
1005
|
+
}
|
|
1006
|
+
if (this._logger && typeof this._logger[level] === 'function') {
|
|
1007
|
+
this._logger[level]('harness-build', message);
|
|
1008
|
+
} else {
|
|
1009
|
+
var prefix = '[harness-build] [' + level + ']';
|
|
1010
|
+
if (level === 'error') {
|
|
1011
|
+
console.error(prefix, message);
|
|
1012
|
+
} else if (level === 'warn') {
|
|
1013
|
+
console.warn(prefix, message);
|
|
1014
|
+
} else {
|
|
1015
|
+
console.log(prefix, message);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* Detect whether this is a local or global installation.
|
|
1022
|
+
* Delegates to settings-merger module.
|
|
1023
|
+
*
|
|
1024
|
+
* @internal
|
|
1025
|
+
* @param {string} packageRoot - this package's root directory
|
|
1026
|
+
* @param {string} targetRoot - target project root directory
|
|
1027
|
+
* @returns {boolean} true if local install, false if global install
|
|
1028
|
+
*/
|
|
1029
|
+
HarnessBuild.prototype._isLocalInstall = function _isLocalInstall(packageRoot, targetRoot) {
|
|
1030
|
+
return settingsMerger.isLocalInstall(packageRoot, targetRoot);
|
|
1031
|
+
};
|
|
1032
|
+
|
|
1033
|
+
// ---------------------------------------------------------------------------
|
|
1034
|
+
// Module exports
|
|
1035
|
+
// ---------------------------------------------------------------------------
|
|
1036
|
+
|
|
1037
|
+
module.exports = HarnessBuild;
|