harness-config-manager 0.1.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.md +1547 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +584 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/config.d.ts +7 -0
- package/dist/commands/config.js +49 -0
- package/dist/commands/config.js.map +1 -0
- package/dist/commands/context.d.ts +24 -0
- package/dist/commands/context.js +329 -0
- package/dist/commands/context.js.map +1 -0
- package/dist/commands/export.d.ts +19 -0
- package/dist/commands/export.js +116 -0
- package/dist/commands/export.js.map +1 -0
- package/dist/commands/import.d.ts +33 -0
- package/dist/commands/import.js +81 -0
- package/dist/commands/import.js.map +1 -0
- package/dist/commands/info.d.ts +19 -0
- package/dist/commands/info.js +235 -0
- package/dist/commands/info.js.map +1 -0
- package/dist/commands/init.d.ts +5 -0
- package/dist/commands/init.js +116 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/install.d.ts +119 -0
- package/dist/commands/install.js +533 -0
- package/dist/commands/install.js.map +1 -0
- package/dist/commands/list.d.ts +9 -0
- package/dist/commands/list.js +102 -0
- package/dist/commands/list.js.map +1 -0
- package/dist/commands/params.d.ts +58 -0
- package/dist/commands/params.js +366 -0
- package/dist/commands/params.js.map +1 -0
- package/dist/commands/refs.d.ts +60 -0
- package/dist/commands/refs.js +329 -0
- package/dist/commands/refs.js.map +1 -0
- package/dist/commands/registry.d.ts +18 -0
- package/dist/commands/registry.js +173 -0
- package/dist/commands/registry.js.map +1 -0
- package/dist/commands/status.d.ts +10 -0
- package/dist/commands/status.js +83 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/targets.d.ts +8 -0
- package/dist/commands/targets.js +61 -0
- package/dist/commands/targets.js.map +1 -0
- package/dist/commands/uninstall.d.ts +71 -0
- package/dist/commands/uninstall.js +322 -0
- package/dist/commands/uninstall.js.map +1 -0
- package/dist/commands/update.d.ts +66 -0
- package/dist/commands/update.js +384 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/commands/validate.d.ts +3 -0
- package/dist/commands/validate.js +38 -0
- package/dist/commands/validate.js.map +1 -0
- package/dist/core/bundle.d.ts +49 -0
- package/dist/core/bundle.js +326 -0
- package/dist/core/bundle.js.map +1 -0
- package/dist/core/config.d.ts +65 -0
- package/dist/core/config.js +112 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/conflicts.d.ts +67 -0
- package/dist/core/conflicts.js +300 -0
- package/dist/core/conflicts.js.map +1 -0
- package/dist/core/context.d.ts +139 -0
- package/dist/core/context.js +432 -0
- package/dist/core/context.js.map +1 -0
- package/dist/core/deps.d.ts +62 -0
- package/dist/core/deps.js +339 -0
- package/dist/core/deps.js.map +1 -0
- package/dist/core/errors.d.ts +21 -0
- package/dist/core/errors.js +26 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/executor.d.ts +9 -0
- package/dist/core/executor.js +139 -0
- package/dist/core/executor.js.map +1 -0
- package/dist/core/flavors.d.ts +128 -0
- package/dist/core/flavors.js +386 -0
- package/dist/core/flavors.js.map +1 -0
- package/dist/core/frontmatter.d.ts +6 -0
- package/dist/core/frontmatter.js +20 -0
- package/dist/core/frontmatter.js.map +1 -0
- package/dist/core/fsx.d.ts +40 -0
- package/dist/core/fsx.js +180 -0
- package/dist/core/fsx.js.map +1 -0
- package/dist/core/github.d.ts +37 -0
- package/dist/core/github.js +157 -0
- package/dist/core/github.js.map +1 -0
- package/dist/core/harnesses.d.ts +142 -0
- package/dist/core/harnesses.js +215 -0
- package/dist/core/harnesses.js.map +1 -0
- package/dist/core/hash.d.ts +12 -0
- package/dist/core/hash.js +34 -0
- package/dist/core/hash.js.map +1 -0
- package/dist/core/logger.d.ts +16 -0
- package/dist/core/logger.js +36 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/overlap.d.ts +79 -0
- package/dist/core/overlap.js +155 -0
- package/dist/core/overlap.js.map +1 -0
- package/dist/core/parameters.d.ts +249 -0
- package/dist/core/parameters.js +944 -0
- package/dist/core/parameters.js.map +1 -0
- package/dist/core/paths.d.ts +20 -0
- package/dist/core/paths.js +35 -0
- package/dist/core/paths.js.map +1 -0
- package/dist/core/planner.d.ts +36 -0
- package/dist/core/planner.js +364 -0
- package/dist/core/planner.js.map +1 -0
- package/dist/core/prompt.d.ts +52 -0
- package/dist/core/prompt.js +197 -0
- package/dist/core/prompt.js.map +1 -0
- package/dist/core/refmap.d.ts +91 -0
- package/dist/core/refmap.js +331 -0
- package/dist/core/refmap.js.map +1 -0
- package/dist/core/refs.d.ts +297 -0
- package/dist/core/refs.js +801 -0
- package/dist/core/refs.js.map +1 -0
- package/dist/core/registry.d.ts +119 -0
- package/dist/core/registry.js +363 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/rename.d.ts +36 -0
- package/dist/core/rename.js +74 -0
- package/dist/core/rename.js.map +1 -0
- package/dist/core/rollback.d.ts +38 -0
- package/dist/core/rollback.js +226 -0
- package/dist/core/rollback.js.map +1 -0
- package/dist/core/semver.d.ts +56 -0
- package/dist/core/semver.js +214 -0
- package/dist/core/semver.js.map +1 -0
- package/dist/core/state.d.ts +99 -0
- package/dist/core/state.js +196 -0
- package/dist/core/state.js.map +1 -0
- package/dist/core/store.d.ts +30 -0
- package/dist/core/store.js +54 -0
- package/dist/core/store.js.map +1 -0
- package/dist/core/types.d.ts +515 -0
- package/dist/core/types.js +49 -0
- package/dist/core/types.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/merge/blocks.d.ts +48 -0
- package/dist/merge/blocks.js +0 -0
- package/dist/merge/blocks.js.map +1 -0
- package/dist/merge/json-merge.d.ts +66 -0
- package/dist/merge/json-merge.js +176 -0
- package/dist/merge/json-merge.js.map +1 -0
- package/dist/merge/toml.d.ts +48 -0
- package/dist/merge/toml.js +118 -0
- package/dist/merge/toml.js.map +1 -0
- package/dist/targets/claude-code.d.ts +11 -0
- package/dist/targets/claude-code.js +94 -0
- package/dist/targets/claude-code.js.map +1 -0
- package/dist/targets/copilot.d.ts +13 -0
- package/dist/targets/copilot.js +126 -0
- package/dist/targets/copilot.js.map +1 -0
- package/dist/targets/index.d.ts +20 -0
- package/dist/targets/index.js +60 -0
- package/dist/targets/index.js.map +1 -0
- package/dist/targets/opencode.d.ts +32 -0
- package/dist/targets/opencode.js +172 -0
- package/dist/targets/opencode.js.map +1 -0
- package/dist/targets/pi.d.ts +42 -0
- package/dist/targets/pi.js +134 -0
- package/dist/targets/pi.js.map +1 -0
- package/dist/targets/reasonix.d.ts +27 -0
- package/dist/targets/reasonix.js +192 -0
- package/dist/targets/reasonix.js.map +1 -0
- package/dist/targets/shared.d.ts +33 -0
- package/dist/targets/shared.js +109 -0
- package/dist/targets/shared.js.map +1 -0
- package/dist/targets/types.d.ts +40 -0
- package/dist/targets/types.js +17 -0
- package/dist/targets/types.js.map +1 -0
- package/docs/authoring-bundles.md +828 -0
- package/package.json +74 -0
|
@@ -0,0 +1,944 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parameters: the values a bundle is customised with as it is installed.
|
|
3
|
+
*
|
|
4
|
+
* A bundle is written once and installed into many projects, and some of what
|
|
5
|
+
* it says is different in each of them -- the agent's name, the team's ticket
|
|
6
|
+
* prefix, the model to prefer. Writing those into the bundle means one bundle
|
|
7
|
+
* per project; leaving them out means the instructions are vague. A *parameter*
|
|
8
|
+
* is the third option: the bundle names the hole, and the install fills it.
|
|
9
|
+
*
|
|
10
|
+
* # hcm.yaml
|
|
11
|
+
* parameters:
|
|
12
|
+
* AGENT_NAME:
|
|
13
|
+
* description: What the agent should call itself
|
|
14
|
+
* default: Claude
|
|
15
|
+
*
|
|
16
|
+
* # context/10-identity.md
|
|
17
|
+
* You are a coding agent called <%AGENT_NAME%>.
|
|
18
|
+
*
|
|
19
|
+
* Three rules, and the rest follows from them:
|
|
20
|
+
*
|
|
21
|
+
* 1. A placeholder is `<%NAME%>`, and it is replaced wherever the text of an
|
|
22
|
+
* installed item can hold one -- markdown bodies and their frontmatter,
|
|
23
|
+
* context blocks, the string values inside MCP and settings fragments, and
|
|
24
|
+
* any skill or asset file that is text. Never in a *path*: where a file
|
|
25
|
+
* lands is a fact about the bundle's layout, not about this install.
|
|
26
|
+
* 2. Substitution happens on the *plan*, after the file references have been
|
|
27
|
+
* remapped and before anything is compared against disk. So the hashes,
|
|
28
|
+
* the receipts, `--dry-run`, `hcm info` and the cached context sections
|
|
29
|
+
* all describe the text that will really be there.
|
|
30
|
+
* 3. The values are recorded with the installation. `hcm update` renders the
|
|
31
|
+
* new version with the same ones without being told again -- an update
|
|
32
|
+
* that silently renamed the agent would be worse than no update at all.
|
|
33
|
+
*
|
|
34
|
+
* A parameter can also say *where* it applies, which is what makes one bundle
|
|
35
|
+
* able to hold questions that are not all worth asking every time:
|
|
36
|
+
*
|
|
37
|
+
* parameters:
|
|
38
|
+
* AGENT_NAME: # global: asked for every install
|
|
39
|
+
* default: Claude
|
|
40
|
+
* PYTEST_ARGS: # only when the python flavor is installed
|
|
41
|
+
* flavors: [python]
|
|
42
|
+
* default: -q
|
|
43
|
+
* COPILOT_ORG: # only when installing into Copilot
|
|
44
|
+
* targets: [copilot]
|
|
45
|
+
*
|
|
46
|
+
* Nothing here removes anything, and nothing here is a template *language*.
|
|
47
|
+
* There are no conditionals, no loops and no expressions: a bundle stays
|
|
48
|
+
* readable as the thing it installs, and the diff between what you wrote and
|
|
49
|
+
* what landed is a handful of names.
|
|
50
|
+
*/
|
|
51
|
+
import { readFileSync } from 'node:fs';
|
|
52
|
+
import path from 'node:path';
|
|
53
|
+
import YAML from 'yaml';
|
|
54
|
+
import { HcmError } from './errors.js';
|
|
55
|
+
import { fs, pathExists } from './fsx.js';
|
|
56
|
+
import { color, log } from './logger.js';
|
|
57
|
+
import { isInteractive, select, text } from './prompt.js';
|
|
58
|
+
import { resolveTargetId, TARGET_IDS } from '../targets/index.js';
|
|
59
|
+
/**
|
|
60
|
+
* What a parameter may be called. Deliberately the shape of an environment
|
|
61
|
+
* variable: the name has to survive being typed on a command line, put in
|
|
62
|
+
* `HCM_PARAM_<NAME>`, and read inside `<% %>` without ambiguity.
|
|
63
|
+
*/
|
|
64
|
+
const PARAMETER_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
65
|
+
const DECLARATION_HINT = 'Write "parameters: [AGENT_NAME]", or a mapping of name to description, or a mapping of ' +
|
|
66
|
+
'name to {description, default, required, choices, pattern, secret, flavors, targets}.';
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Reading the manifest
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
/**
|
|
71
|
+
* Accept every way of writing `parameters:` and return the one shape the rest
|
|
72
|
+
* of hcm works with:
|
|
73
|
+
*
|
|
74
|
+
* parameters: [AGENT_NAME, TEAM] # names alone, each one required
|
|
75
|
+
*
|
|
76
|
+
* parameters:
|
|
77
|
+
* AGENT_NAME: What the agent calls itself # name with a description
|
|
78
|
+
*
|
|
79
|
+
* parameters:
|
|
80
|
+
* AGENT_NAME: # name with the full shape
|
|
81
|
+
* description: What the agent calls itself
|
|
82
|
+
* default: Claude
|
|
83
|
+
* choices: [Claude, Codey]
|
|
84
|
+
* flavors: [python]
|
|
85
|
+
* targets: [claude-code]
|
|
86
|
+
*/
|
|
87
|
+
export function normalizeParameters(manifest, where = manifest.name) {
|
|
88
|
+
const declared = manifest.parameters;
|
|
89
|
+
if (declared === undefined)
|
|
90
|
+
return [];
|
|
91
|
+
let entries;
|
|
92
|
+
if (Array.isArray(declared)) {
|
|
93
|
+
for (const name of declared) {
|
|
94
|
+
if (typeof name !== 'string') {
|
|
95
|
+
throw new HcmError(`Malformed parameter in ${where}: ${JSON.stringify(name)}`, DECLARATION_HINT);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
entries = declared.map((name) => [name, undefined]);
|
|
99
|
+
}
|
|
100
|
+
else if (declared && typeof declared === 'object') {
|
|
101
|
+
entries = Object.entries(declared);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
throw new HcmError(`"parameters" in ${where} must be a list of names or a mapping`, DECLARATION_HINT);
|
|
105
|
+
}
|
|
106
|
+
const seen = new Set();
|
|
107
|
+
const parameters = [];
|
|
108
|
+
for (const [rawName, value] of entries) {
|
|
109
|
+
const name = rawName.trim();
|
|
110
|
+
if (!PARAMETER_NAME_PATTERN.test(name)) {
|
|
111
|
+
throw new HcmError(`"${name}" is not a usable parameter name (in ${where})`, 'Start with a letter or underscore, then letters, digits and underscores -- the ' +
|
|
112
|
+
'name goes inside <% %> and into HCM_PARAM_<NAME>.');
|
|
113
|
+
}
|
|
114
|
+
// Case-insensitively unique, because `HCM_PARAM_<NAME>` upper-cases the
|
|
115
|
+
// name and two parameters differing only in case would share one variable.
|
|
116
|
+
const key = name.toLowerCase();
|
|
117
|
+
if (seen.has(key)) {
|
|
118
|
+
throw new HcmError(`"${name}" is listed twice in ${where}'s parameters`);
|
|
119
|
+
}
|
|
120
|
+
seen.add(key);
|
|
121
|
+
parameters.push(readParameterBody(name, value, where));
|
|
122
|
+
}
|
|
123
|
+
return parameters;
|
|
124
|
+
}
|
|
125
|
+
/** The right-hand side of one parameter entry: nothing, a description, or all of it. */
|
|
126
|
+
function readParameterBody(name, value, where) {
|
|
127
|
+
const bare = (extra = {}) => ({
|
|
128
|
+
name,
|
|
129
|
+
required: true,
|
|
130
|
+
flavors: [],
|
|
131
|
+
targets: [],
|
|
132
|
+
...extra,
|
|
133
|
+
});
|
|
134
|
+
if (value === undefined || value === null)
|
|
135
|
+
return bare();
|
|
136
|
+
if (typeof value === 'string') {
|
|
137
|
+
const description = value.trim();
|
|
138
|
+
return bare(description ? { description } : {});
|
|
139
|
+
}
|
|
140
|
+
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
141
|
+
throw new HcmError(`Malformed parameter "${name}" in ${where}`, DECLARATION_HINT);
|
|
142
|
+
}
|
|
143
|
+
const shape = value;
|
|
144
|
+
if (shape.description !== undefined && typeof shape.description !== 'string') {
|
|
145
|
+
throw new HcmError(`"description" for parameter "${name}" in ${where} must be text`);
|
|
146
|
+
}
|
|
147
|
+
const description = typeof shape.description === 'string' ? shape.description.trim() : undefined;
|
|
148
|
+
const fallback = readDefault(shape.default, name, where);
|
|
149
|
+
const choices = readChoices(shape.choices, name, where);
|
|
150
|
+
const pattern = readPattern(shape.pattern, name, where);
|
|
151
|
+
if (shape.required !== undefined && typeof shape.required !== 'boolean') {
|
|
152
|
+
throw new HcmError(`"required" for parameter "${name}" in ${where} must be true or false`);
|
|
153
|
+
}
|
|
154
|
+
if (shape.secret !== undefined && typeof shape.secret !== 'boolean') {
|
|
155
|
+
throw new HcmError(`"secret" for parameter "${name}" in ${where} must be true or false`);
|
|
156
|
+
}
|
|
157
|
+
// A parameter with a default can always be satisfied, so requiredness only
|
|
158
|
+
// decides what happens when there is nothing to fall back on.
|
|
159
|
+
const required = typeof shape.required === 'boolean' ? shape.required : fallback === undefined;
|
|
160
|
+
if (fallback !== undefined && choices && !choices.includes(fallback)) {
|
|
161
|
+
throw new HcmError(`The default for parameter "${name}" in ${where} is not one of its choices`, `default: ${JSON.stringify(fallback)}; choices: ${choices.join(', ')}`);
|
|
162
|
+
}
|
|
163
|
+
if (fallback !== undefined && pattern && !new RegExp(pattern).test(fallback)) {
|
|
164
|
+
throw new HcmError(`The default for parameter "${name}" in ${where} does not match its own pattern`, `default: ${JSON.stringify(fallback)}; pattern: ${pattern}`);
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
name,
|
|
168
|
+
...(description ? { description } : {}),
|
|
169
|
+
...(fallback !== undefined ? { default: fallback } : {}),
|
|
170
|
+
required,
|
|
171
|
+
...(choices ? { choices } : {}),
|
|
172
|
+
...(pattern ? { pattern } : {}),
|
|
173
|
+
...(shape.secret === true ? { secret: true } : {}),
|
|
174
|
+
flavors: readNames(shape.flavors ?? shape.flavor, 'flavors', name, where),
|
|
175
|
+
targets: readTargets(shape.targets ?? shape.target, name, where),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* A default written in YAML need not be a string: `default: 3` and
|
|
180
|
+
* `default: true` are the obvious way to write a number and a flag, and both
|
|
181
|
+
* become text on the way into a file anyway.
|
|
182
|
+
*/
|
|
183
|
+
function readDefault(value, name, where) {
|
|
184
|
+
if (value === undefined || value === null)
|
|
185
|
+
return undefined;
|
|
186
|
+
if (typeof value === 'string')
|
|
187
|
+
return value;
|
|
188
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
189
|
+
return String(value);
|
|
190
|
+
throw new HcmError(`"default" for parameter "${name}" in ${where} must be text, a number or a boolean`);
|
|
191
|
+
}
|
|
192
|
+
function readChoices(value, name, where) {
|
|
193
|
+
if (value === undefined || value === null)
|
|
194
|
+
return undefined;
|
|
195
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
196
|
+
throw new HcmError(`"choices" for parameter "${name}" in ${where} must be a non-empty list`);
|
|
197
|
+
}
|
|
198
|
+
const choices = value.map((entry) => {
|
|
199
|
+
if (typeof entry === 'string')
|
|
200
|
+
return entry;
|
|
201
|
+
if (typeof entry === 'number' || typeof entry === 'boolean')
|
|
202
|
+
return String(entry);
|
|
203
|
+
throw new HcmError(`"choices" for parameter "${name}" in ${where} must hold text, numbers or booleans`);
|
|
204
|
+
});
|
|
205
|
+
if (new Set(choices).size !== choices.length) {
|
|
206
|
+
throw new HcmError(`"choices" for parameter "${name}" in ${where} lists the same value twice`);
|
|
207
|
+
}
|
|
208
|
+
return choices;
|
|
209
|
+
}
|
|
210
|
+
function readPattern(value, name, where) {
|
|
211
|
+
if (value === undefined || value === null)
|
|
212
|
+
return undefined;
|
|
213
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
214
|
+
throw new HcmError(`"pattern" for parameter "${name}" in ${where} must be a regular expression`);
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
new RegExp(value);
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
throw new HcmError(`"pattern" for parameter "${name}" in ${where} is not a valid regular expression`, error.message);
|
|
221
|
+
}
|
|
222
|
+
return value;
|
|
223
|
+
}
|
|
224
|
+
function readNames(value, field, name, where) {
|
|
225
|
+
if (value === undefined || value === null)
|
|
226
|
+
return [];
|
|
227
|
+
const list = Array.isArray(value) ? value : [value];
|
|
228
|
+
const names = [];
|
|
229
|
+
for (const entry of list) {
|
|
230
|
+
if (typeof entry !== 'string' || !entry.trim()) {
|
|
231
|
+
throw new HcmError(`"${field}" for parameter "${name}" in ${where} must be a name or a list of them`, `Got ${JSON.stringify(entry)}.`);
|
|
232
|
+
}
|
|
233
|
+
names.push(entry.trim());
|
|
234
|
+
}
|
|
235
|
+
return names;
|
|
236
|
+
}
|
|
237
|
+
/** Harness names are validated here rather than at install time, and against the real list. */
|
|
238
|
+
function readTargets(value, name, where) {
|
|
239
|
+
const names = readNames(value, 'targets', name, where);
|
|
240
|
+
return names.map((entry) => {
|
|
241
|
+
try {
|
|
242
|
+
return resolveTargetId(entry);
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
throw new HcmError(`"${entry}" is not a harness hcm knows (parameter "${name}" in ${where})`, `Supported: ${TARGET_IDS.join(', ')}`);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
// The placeholder
|
|
251
|
+
// ---------------------------------------------------------------------------
|
|
252
|
+
/**
|
|
253
|
+
* `<%NAME%>`, with optional spaces inside. A doubled `%` escapes it, so
|
|
254
|
+
* `<%%NAME%>` renders as the literal `<%NAME%>` -- which is how a bundle
|
|
255
|
+
* documents this very feature without its own examples being filled in.
|
|
256
|
+
*
|
|
257
|
+
* Anything that is not exactly a name between the delimiters is left alone, so
|
|
258
|
+
* a file carrying some other template language's `<% if x %>` survives intact.
|
|
259
|
+
*/
|
|
260
|
+
const PLACEHOLDER = /<%(%?)\s*([A-Za-z_][A-Za-z0-9_]*)\s*%>/g;
|
|
261
|
+
/** Fill in every placeholder that has a value; leave the rest exactly as they are. */
|
|
262
|
+
export function renderTemplate(input, values) {
|
|
263
|
+
const substituted = new Map();
|
|
264
|
+
const unresolved = new Map();
|
|
265
|
+
const text = input.replace(PLACEHOLDER, (match, escape, name) => {
|
|
266
|
+
if (escape === '%')
|
|
267
|
+
return `<%${name}%>`;
|
|
268
|
+
const value = values[name];
|
|
269
|
+
if (value === undefined) {
|
|
270
|
+
unresolved.set(name, (unresolved.get(name) ?? 0) + 1);
|
|
271
|
+
return match;
|
|
272
|
+
}
|
|
273
|
+
substituted.set(name, (substituted.get(name) ?? 0) + 1);
|
|
274
|
+
return value;
|
|
275
|
+
});
|
|
276
|
+
return { text, substituted, unresolved };
|
|
277
|
+
}
|
|
278
|
+
/** Every parameter name a piece of text refers to, escaped ones excluded. */
|
|
279
|
+
export function placeholderNames(input) {
|
|
280
|
+
const found = new Set();
|
|
281
|
+
for (const match of input.matchAll(PLACEHOLDER)) {
|
|
282
|
+
if (match[1] === '%')
|
|
283
|
+
continue;
|
|
284
|
+
found.add(match[2]);
|
|
285
|
+
}
|
|
286
|
+
return [...found];
|
|
287
|
+
}
|
|
288
|
+
/** True when text anywhere in here refers to a parameter. */
|
|
289
|
+
export function hasPlaceholder(input) {
|
|
290
|
+
return placeholderNames(input).length > 0;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* A buffer read back as text, or undefined when it is not text at all.
|
|
294
|
+
*
|
|
295
|
+
* Skills and assets are copied byte-for-byte, and most of them are markdown or
|
|
296
|
+
* a shell script that should be templated like anything else. An image must not
|
|
297
|
+
* be: decoding it, substituting nothing and re-encoding would still be a
|
|
298
|
+
* needless round trip, and a NUL byte is the cheapest reliable evidence.
|
|
299
|
+
*/
|
|
300
|
+
export function decodeText(buffer) {
|
|
301
|
+
if (buffer.includes(0))
|
|
302
|
+
return undefined;
|
|
303
|
+
const text = buffer.toString('utf8');
|
|
304
|
+
// A lossy decode leaves U+FFFD behind; re-encoding then would corrupt the file.
|
|
305
|
+
return Buffer.from(text, 'utf8').equals(buffer) ? text : undefined;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Render every action's payload with `values`.
|
|
309
|
+
*
|
|
310
|
+
* `only` limits what is rendered without limiting what is reported -- which is
|
|
311
|
+
* what a resolved conflict needs, having regenerated one resource's actions
|
|
312
|
+
* under a new name after the rest were already done.
|
|
313
|
+
*
|
|
314
|
+
* `declared` is used only to explain a placeholder that was left standing: one
|
|
315
|
+
* naming a parameter the bundle never declared is a typo, one naming a
|
|
316
|
+
* parameter that does not apply here is a bundle installed more narrowly than
|
|
317
|
+
* it was written. Both leave the text alone, because a placeholder the user is
|
|
318
|
+
* told about beats a plausible value that is wrong.
|
|
319
|
+
*/
|
|
320
|
+
export function applyParameters(actions, values, declared = [], only) {
|
|
321
|
+
const rendering = new Set(only ?? actions);
|
|
322
|
+
const substituted = [];
|
|
323
|
+
const unresolved = [];
|
|
324
|
+
const known = new Set(declared.map((parameter) => parameter.name));
|
|
325
|
+
const collect = (at, result) => {
|
|
326
|
+
for (const [name, count] of result.substituted)
|
|
327
|
+
substituted.push({ path: at, name, count });
|
|
328
|
+
for (const [name] of result.unresolved) {
|
|
329
|
+
unresolved.push({
|
|
330
|
+
path: at,
|
|
331
|
+
name,
|
|
332
|
+
reason: known.has(name)
|
|
333
|
+
? 'is a parameter of this bundle, but does not apply to this harness or flavor'
|
|
334
|
+
: 'is not a parameter this bundle declares',
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
const rendered = actions.map((action) => {
|
|
339
|
+
if (!rendering.has(action))
|
|
340
|
+
return action;
|
|
341
|
+
const { payload } = action;
|
|
342
|
+
if (payload.kind === 'file') {
|
|
343
|
+
if (typeof payload.contents === 'string') {
|
|
344
|
+
const result = renderTemplate(payload.contents, values);
|
|
345
|
+
collect(action.path, result);
|
|
346
|
+
if (result.text === payload.contents)
|
|
347
|
+
return action;
|
|
348
|
+
return { ...action, payload: { ...payload, contents: result.text } };
|
|
349
|
+
}
|
|
350
|
+
const decoded = decodeText(payload.contents);
|
|
351
|
+
if (decoded === undefined)
|
|
352
|
+
return action;
|
|
353
|
+
const result = renderTemplate(decoded, values);
|
|
354
|
+
collect(action.path, result);
|
|
355
|
+
if (result.text === decoded)
|
|
356
|
+
return action;
|
|
357
|
+
return { ...action, payload: { ...payload, contents: Buffer.from(result.text, 'utf8') } };
|
|
358
|
+
}
|
|
359
|
+
if (payload.kind === 'block') {
|
|
360
|
+
const result = renderTemplate(payload.body, values);
|
|
361
|
+
collect(action.path, result);
|
|
362
|
+
if (result.text === payload.body)
|
|
363
|
+
return action;
|
|
364
|
+
return { ...action, payload: { ...payload, body: result.text } };
|
|
365
|
+
}
|
|
366
|
+
if (payload.kind === 'json-value') {
|
|
367
|
+
const result = renderJson(payload.value, values);
|
|
368
|
+
collect(action.path, result);
|
|
369
|
+
if (!result.changed)
|
|
370
|
+
return action;
|
|
371
|
+
return { ...action, payload: { ...payload, value: result.value } };
|
|
372
|
+
}
|
|
373
|
+
const result = renderJson(payload.items, values);
|
|
374
|
+
collect(action.path, result);
|
|
375
|
+
if (!result.changed)
|
|
376
|
+
return action;
|
|
377
|
+
return { ...action, payload: { ...payload, items: result.value } };
|
|
378
|
+
});
|
|
379
|
+
return { actions: rendered, substituted, unresolved };
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Render the strings inside a JSON payload -- an MCP server's arguments, a
|
|
383
|
+
* settings value, an entry in an allowlist.
|
|
384
|
+
*
|
|
385
|
+
* Values only. A templated *key* would move the JSON pointer the receipt is
|
|
386
|
+
* written against, so the item hcm claims and the item it wrote would be
|
|
387
|
+
* addressed differently; `hcm validate` reports one rather than guessing.
|
|
388
|
+
*/
|
|
389
|
+
function renderJson(value, values) {
|
|
390
|
+
const substituted = new Map();
|
|
391
|
+
const unresolved = new Map();
|
|
392
|
+
let changed = false;
|
|
393
|
+
const add = (into, from) => {
|
|
394
|
+
for (const [name, count] of from)
|
|
395
|
+
into.set(name, (into.get(name) ?? 0) + count);
|
|
396
|
+
};
|
|
397
|
+
const walk = (node) => {
|
|
398
|
+
if (typeof node === 'string') {
|
|
399
|
+
const result = renderTemplate(node, values);
|
|
400
|
+
add(substituted, result.substituted);
|
|
401
|
+
add(unresolved, result.unresolved);
|
|
402
|
+
if (result.text === node)
|
|
403
|
+
return node;
|
|
404
|
+
changed = true;
|
|
405
|
+
return result.text;
|
|
406
|
+
}
|
|
407
|
+
if (Array.isArray(node))
|
|
408
|
+
return node.map(walk);
|
|
409
|
+
if (node && typeof node === 'object') {
|
|
410
|
+
return Object.fromEntries(Object.entries(node).map(([key, item]) => [key, walk(item)]));
|
|
411
|
+
}
|
|
412
|
+
return node;
|
|
413
|
+
};
|
|
414
|
+
return { value: walk(value), changed, substituted, unresolved };
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Does this parameter belong to the install being planned?
|
|
418
|
+
*
|
|
419
|
+
* A parameter that names no flavors and no targets is *global*: it applies
|
|
420
|
+
* always, which is what makes the simplest declaration also the most useful
|
|
421
|
+
* one. Naming either narrows it, and the flavor rule is the same one resources
|
|
422
|
+
* follow -- asking for no flavor asks for all of them.
|
|
423
|
+
*
|
|
424
|
+
* Applicability decides what is *asked for*, not what is substituted. A
|
|
425
|
+
* parameter narrowed to Copilot still has a default, and a file installed into
|
|
426
|
+
* Claude Code that mentions it gets that default rather than a placeholder --
|
|
427
|
+
* see `withDefaults`. Only a narrowed parameter with no default at all leaves a
|
|
428
|
+
* hole elsewhere, and `hcm validate` reports exactly that case.
|
|
429
|
+
*/
|
|
430
|
+
export function appliesTo(parameter, scope) {
|
|
431
|
+
if (parameter.targets.length > 0 && !parameter.targets.includes(scope.target))
|
|
432
|
+
return false;
|
|
433
|
+
if (parameter.flavors.length > 0 && scope.flavors.length > 0) {
|
|
434
|
+
const wanted = new Set(scope.flavors.map((name) => name.toLowerCase()));
|
|
435
|
+
return parameter.flavors.some((name) => wanted.has(name.toLowerCase()));
|
|
436
|
+
}
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
/** The parameters an install into one harness, narrowed to some flavors, has to answer. */
|
|
440
|
+
export function applicableParameters(parameters, scope) {
|
|
441
|
+
return parameters.filter((parameter) => appliesTo(parameter, scope));
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* The resolved values, plus a default for every parameter that was not asked
|
|
445
|
+
* about because it does not apply here.
|
|
446
|
+
*
|
|
447
|
+
* This is what stops a narrowing from putting a hole in a file. A bundle whose
|
|
448
|
+
* `MODEL` is asked for on Claude Code and defaulted everywhere else is an
|
|
449
|
+
* ordinary thing to want; without this, "everywhere else" would read
|
|
450
|
+
* `<%MODEL%>` in the installed text. Nothing here is recorded -- a default is
|
|
451
|
+
* always re-derivable from the manifest, and writing it down would freeze it
|
|
452
|
+
* against the next version.
|
|
453
|
+
*/
|
|
454
|
+
export function withDefaults(values, parameters) {
|
|
455
|
+
const filled = { ...values };
|
|
456
|
+
for (const parameter of parameters) {
|
|
457
|
+
if (filled[parameter.name] === undefined && parameter.default !== undefined) {
|
|
458
|
+
filled[parameter.name] = parameter.default;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return filled;
|
|
462
|
+
}
|
|
463
|
+
export function emptyOverrides() {
|
|
464
|
+
return { global: {}, byBundle: {}, byBundleTarget: {} };
|
|
465
|
+
}
|
|
466
|
+
export function mergeOverrides(...list) {
|
|
467
|
+
const merged = emptyOverrides();
|
|
468
|
+
for (const overrides of list) {
|
|
469
|
+
if (!overrides)
|
|
470
|
+
continue;
|
|
471
|
+
Object.assign(merged.global, overrides.global);
|
|
472
|
+
for (const [bundle, values] of Object.entries(overrides.byBundle)) {
|
|
473
|
+
merged.byBundle[bundle] = { ...merged.byBundle[bundle], ...values };
|
|
474
|
+
}
|
|
475
|
+
for (const [key, values] of Object.entries(overrides.byBundleTarget)) {
|
|
476
|
+
merged.byBundleTarget[key] = { ...merged.byBundleTarget[key], ...values };
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return merged;
|
|
480
|
+
}
|
|
481
|
+
/** The values that apply to one bundle in one harness, narrowest last. */
|
|
482
|
+
export function overridesFor(overrides, bundle, target) {
|
|
483
|
+
return {
|
|
484
|
+
...overrides.global,
|
|
485
|
+
...overrides.byBundle[bundle],
|
|
486
|
+
...overrides.byBundleTarget[`${bundle}@${target}`],
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Read `--param` assignments.
|
|
491
|
+
*
|
|
492
|
+
* --param AGENT_NAME=Ada every bundle that asks for one
|
|
493
|
+
* --param review-kit:AGENT_NAME=Ada that bundle only
|
|
494
|
+
* --param review-kit@copilot:AGENT_NAME=Ada that bundle, in that harness only
|
|
495
|
+
*/
|
|
496
|
+
export function parseAssignments(entries) {
|
|
497
|
+
const overrides = emptyOverrides();
|
|
498
|
+
if (!entries?.length)
|
|
499
|
+
return overrides;
|
|
500
|
+
for (const entry of entries) {
|
|
501
|
+
const equals = entry.indexOf('=');
|
|
502
|
+
if (equals <= 0) {
|
|
503
|
+
throw new HcmError(`"${entry}" is not a parameter assignment`, 'Write NAME=value, or bundle:NAME=value, or bundle@harness:NAME=value.');
|
|
504
|
+
}
|
|
505
|
+
const key = entry.slice(0, equals).trim();
|
|
506
|
+
const value = entry.slice(equals + 1);
|
|
507
|
+
const colon = key.lastIndexOf(':');
|
|
508
|
+
const name = (colon === -1 ? key : key.slice(colon + 1)).trim();
|
|
509
|
+
if (!PARAMETER_NAME_PATTERN.test(name)) {
|
|
510
|
+
throw new HcmError(`"${name}" is not a usable parameter name (in --param ${entry})`, 'Start with a letter or underscore, then letters, digits and underscores.');
|
|
511
|
+
}
|
|
512
|
+
if (colon === -1) {
|
|
513
|
+
overrides.global[name] = value;
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
const where = key.slice(0, colon).trim();
|
|
517
|
+
if (!where) {
|
|
518
|
+
throw new HcmError(`"${entry}" names no bundle before the ":"`, 'Write NAME=value for every bundle, or bundle:NAME=value for one of them.');
|
|
519
|
+
}
|
|
520
|
+
const at = where.indexOf('@');
|
|
521
|
+
if (at === -1) {
|
|
522
|
+
overrides.byBundle[where] = { ...overrides.byBundle[where], [name]: value };
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
const bundle = where.slice(0, at).trim();
|
|
526
|
+
const target = resolveHarness(where.slice(at + 1).trim(), entry);
|
|
527
|
+
const scoped = `${bundle}@${target}`;
|
|
528
|
+
overrides.byBundleTarget[scoped] = { ...overrides.byBundleTarget[scoped], [name]: value };
|
|
529
|
+
}
|
|
530
|
+
return overrides;
|
|
531
|
+
}
|
|
532
|
+
function resolveHarness(value, where) {
|
|
533
|
+
try {
|
|
534
|
+
return resolveTargetId(value);
|
|
535
|
+
}
|
|
536
|
+
catch {
|
|
537
|
+
throw new HcmError(`"${value}" is not a harness hcm knows (in ${where})`, `Supported: ${TARGET_IDS.join(', ')}`);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Read a parameters file -- YAML or JSON, the same two the manifest may be.
|
|
542
|
+
*
|
|
543
|
+
* AGENT_NAME: Ada # every bundle that asks for one
|
|
544
|
+
*
|
|
545
|
+
* bundles:
|
|
546
|
+
* review-kit:
|
|
547
|
+
* REVIEWER: Ada # that bundle
|
|
548
|
+
* targets:
|
|
549
|
+
* copilot:
|
|
550
|
+
* AGENT_NAME: Codey # that bundle, in Copilot
|
|
551
|
+
*
|
|
552
|
+
* This is the answer to "how do I install the same thing on twenty machines":
|
|
553
|
+
* commit the file, pass `--params-file`, and no question is ever asked.
|
|
554
|
+
*/
|
|
555
|
+
export async function readParametersFile(file, cwd) {
|
|
556
|
+
const absolute = path.resolve(cwd, file);
|
|
557
|
+
if (!(await pathExists(absolute))) {
|
|
558
|
+
throw new HcmError(`No parameters file at ${absolute}`);
|
|
559
|
+
}
|
|
560
|
+
const raw = await fs.readFile(absolute, 'utf8');
|
|
561
|
+
let parsed;
|
|
562
|
+
try {
|
|
563
|
+
parsed = YAML.parse(raw);
|
|
564
|
+
}
|
|
565
|
+
catch (error) {
|
|
566
|
+
throw new HcmError(`Failed to parse ${file}: ${error.message}`);
|
|
567
|
+
}
|
|
568
|
+
if (parsed === null || parsed === undefined)
|
|
569
|
+
return emptyOverrides();
|
|
570
|
+
if (typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
571
|
+
throw new HcmError(`${file} must be a mapping of parameter names to values`, 'Optionally with a "bundles:" section for values that apply to one bundle only.');
|
|
572
|
+
}
|
|
573
|
+
const overrides = emptyOverrides();
|
|
574
|
+
const root = parsed;
|
|
575
|
+
for (const [key, value] of Object.entries(root)) {
|
|
576
|
+
if (key === 'bundles')
|
|
577
|
+
continue;
|
|
578
|
+
if (isBlank(value))
|
|
579
|
+
continue;
|
|
580
|
+
overrides.global[key] = readFileValue(value, key, file);
|
|
581
|
+
}
|
|
582
|
+
const bundles = root.bundles;
|
|
583
|
+
if (bundles === undefined || bundles === null)
|
|
584
|
+
return overrides;
|
|
585
|
+
if (typeof bundles !== 'object' || Array.isArray(bundles)) {
|
|
586
|
+
throw new HcmError(`"bundles" in ${file} must be a mapping of bundle name to values`);
|
|
587
|
+
}
|
|
588
|
+
for (const [bundle, section] of Object.entries(bundles)) {
|
|
589
|
+
if (section === null || section === undefined)
|
|
590
|
+
continue;
|
|
591
|
+
if (typeof section !== 'object' || Array.isArray(section)) {
|
|
592
|
+
throw new HcmError(`"bundles.${bundle}" in ${file} must be a mapping of names to values`);
|
|
593
|
+
}
|
|
594
|
+
for (const [key, value] of Object.entries(section)) {
|
|
595
|
+
if (key === 'targets')
|
|
596
|
+
continue;
|
|
597
|
+
if (isBlank(value))
|
|
598
|
+
continue;
|
|
599
|
+
overrides.byBundle[bundle] = {
|
|
600
|
+
...overrides.byBundle[bundle],
|
|
601
|
+
[key]: readFileValue(value, `bundles.${bundle}.${key}`, file),
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
const targets = section.targets;
|
|
605
|
+
if (targets === undefined || targets === null)
|
|
606
|
+
continue;
|
|
607
|
+
if (typeof targets !== 'object' || Array.isArray(targets)) {
|
|
608
|
+
throw new HcmError(`"bundles.${bundle}.targets" in ${file} must be a mapping`);
|
|
609
|
+
}
|
|
610
|
+
for (const [name, values] of Object.entries(targets)) {
|
|
611
|
+
const target = resolveHarness(name, `${file} (bundles.${bundle}.targets)`);
|
|
612
|
+
if (values === null || values === undefined)
|
|
613
|
+
continue;
|
|
614
|
+
if (typeof values !== 'object' || Array.isArray(values)) {
|
|
615
|
+
throw new HcmError(`"bundles.${bundle}.targets.${name}" in ${file} must be a mapping of names to values`);
|
|
616
|
+
}
|
|
617
|
+
const key = `${bundle}@${target}`;
|
|
618
|
+
for (const [parameter, value] of Object.entries(values)) {
|
|
619
|
+
if (isBlank(value))
|
|
620
|
+
continue;
|
|
621
|
+
overrides.byBundleTarget[key] = {
|
|
622
|
+
...overrides.byBundleTarget[key],
|
|
623
|
+
[parameter]: readFileValue(value, `bundles.${bundle}.targets.${name}.${parameter}`, file),
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return overrides;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* A key that is present but says nothing.
|
|
632
|
+
*
|
|
633
|
+
* `hcm params init` writes a template with a blank beside every value it cannot
|
|
634
|
+
* fill in for you, so a half-completed file is the normal state of one. A blank
|
|
635
|
+
* therefore means "I have not said", not "the value is nothing" -- hcm falls
|
|
636
|
+
* back to the default, or asks, exactly as if the key were absent. Saying the
|
|
637
|
+
* value really is empty takes an explicit `KEY: ""`.
|
|
638
|
+
*/
|
|
639
|
+
function isBlank(value) {
|
|
640
|
+
return value === null || value === undefined;
|
|
641
|
+
}
|
|
642
|
+
function readFileValue(value, at, file) {
|
|
643
|
+
if (typeof value === 'string')
|
|
644
|
+
return value;
|
|
645
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
646
|
+
return String(value);
|
|
647
|
+
throw new HcmError(`"${at}" in ${file} must be text, a number or a boolean`, 'Parameter values are substituted into files as they are written.');
|
|
648
|
+
}
|
|
649
|
+
/** The environment variable one parameter answers to, for a run with no terminal. */
|
|
650
|
+
export function parameterEnvName(name) {
|
|
651
|
+
return `HCM_PARAM_${name.toUpperCase()}`;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Work out the value of every parameter this install needs.
|
|
655
|
+
*
|
|
656
|
+
* The order is the point. What the command line says beats the environment,
|
|
657
|
+
* which beats what has already been answered in this run, which beats what the
|
|
658
|
+
* last install recorded -- and only when all of those are silent is anybody
|
|
659
|
+
* asked. So a scripted run never blocks, an interactive one never repeats
|
|
660
|
+
* itself, and `hcm update` never changes an answer behind your back.
|
|
661
|
+
*/
|
|
662
|
+
export async function resolveParameters(input) {
|
|
663
|
+
const values = {};
|
|
664
|
+
const sources = {};
|
|
665
|
+
const session = input.session ?? new Map();
|
|
666
|
+
const overrides = input.overrides ?? {};
|
|
667
|
+
let headerShown = false;
|
|
668
|
+
for (const parameter of input.parameters) {
|
|
669
|
+
const key = sessionKey(input.bundle, input.target, parameter);
|
|
670
|
+
const supplied = firstSupplied(parameter, {
|
|
671
|
+
overrides,
|
|
672
|
+
session,
|
|
673
|
+
key,
|
|
674
|
+
...(input.recorded && !input.reconfigure ? { recorded: input.recorded } : {}),
|
|
675
|
+
});
|
|
676
|
+
if (supplied) {
|
|
677
|
+
values[parameter.name] = supplied.value;
|
|
678
|
+
sources[parameter.name] = supplied.source;
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
if (input.prompt !== false && (input.ask !== undefined || isInteractive())) {
|
|
682
|
+
if (!headerShown && input.ask === undefined) {
|
|
683
|
+
log.plain('');
|
|
684
|
+
log.info(` ${color.bold(input.bundle)} asks for:`);
|
|
685
|
+
headerShown = true;
|
|
686
|
+
}
|
|
687
|
+
const answer = await (input.ask ?? askParameter)(parameter);
|
|
688
|
+
values[parameter.name] = answer;
|
|
689
|
+
sources[parameter.name] = 'prompt';
|
|
690
|
+
session.set(key, answer);
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
if (parameter.default !== undefined) {
|
|
694
|
+
values[parameter.name] = parameter.default;
|
|
695
|
+
sources[parameter.name] = 'default';
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
698
|
+
if (parameter.required)
|
|
699
|
+
throw missingValue(parameter, input.bundle);
|
|
700
|
+
// Optional, unanswered and with nothing to fall back on: the placeholder
|
|
701
|
+
// stands for nothing, so it becomes nothing rather than staying in the text.
|
|
702
|
+
values[parameter.name] = '';
|
|
703
|
+
sources[parameter.name] = 'empty';
|
|
704
|
+
}
|
|
705
|
+
return { values, sources };
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Answers are remembered per bundle, except for a parameter that names the
|
|
709
|
+
* harnesses it applies to -- that one is a fact about a harness, so it is asked
|
|
710
|
+
* again for the next one rather than carried across.
|
|
711
|
+
*/
|
|
712
|
+
function sessionKey(bundle, target, parameter) {
|
|
713
|
+
return parameter.targets.length > 0
|
|
714
|
+
? `${bundle}@${target}::${parameter.name}`
|
|
715
|
+
: `${bundle}::${parameter.name}`;
|
|
716
|
+
}
|
|
717
|
+
/** The first source that has something to say, checked in precedence order. */
|
|
718
|
+
function firstSupplied(parameter, from) {
|
|
719
|
+
const candidates = [
|
|
720
|
+
{ value: from.overrides[parameter.name], source: 'flag' },
|
|
721
|
+
{ value: process.env[parameterEnvName(parameter.name)], source: 'environment' },
|
|
722
|
+
{ value: from.session.get(from.key), source: 'session' },
|
|
723
|
+
{ value: from.recorded?.[parameter.name], source: 'recorded' },
|
|
724
|
+
];
|
|
725
|
+
for (const candidate of candidates) {
|
|
726
|
+
if (candidate.value === undefined)
|
|
727
|
+
continue;
|
|
728
|
+
const problem = valueProblem(parameter, candidate.value);
|
|
729
|
+
if (!problem)
|
|
730
|
+
return { value: candidate.value, source: candidate.source };
|
|
731
|
+
// A value the bundle itself no longer accepts. From the command line or the
|
|
732
|
+
// environment that is a mistake worth stopping for; a recorded one just
|
|
733
|
+
// means the bundle tightened its rules since, so fall through and ask.
|
|
734
|
+
if (candidate.source === 'flag' || candidate.source === 'environment') {
|
|
735
|
+
throw new HcmError(`The value given for "${parameter.name}" ${problem}`, candidate.source === 'environment'
|
|
736
|
+
? `It came from ${parameterEnvName(parameter.name)}.`
|
|
737
|
+
: undefined);
|
|
738
|
+
}
|
|
739
|
+
log.warn(` the value recorded for "${parameter.name}" ${problem}; asking again`);
|
|
740
|
+
}
|
|
741
|
+
return undefined;
|
|
742
|
+
}
|
|
743
|
+
/** What is wrong with a value, if anything -- phrased to follow "the value ...". */
|
|
744
|
+
export function valueProblem(parameter, value) {
|
|
745
|
+
if (parameter.choices && !parameter.choices.includes(value)) {
|
|
746
|
+
return `is not one of its choices (${parameter.choices.join(', ')})`;
|
|
747
|
+
}
|
|
748
|
+
if (parameter.pattern && !new RegExp(parameter.pattern).test(value)) {
|
|
749
|
+
return `does not match the pattern ${parameter.pattern}`;
|
|
750
|
+
}
|
|
751
|
+
if (parameter.required && value === '')
|
|
752
|
+
return 'is empty, and it is required';
|
|
753
|
+
return undefined;
|
|
754
|
+
}
|
|
755
|
+
function missingValue(parameter, bundle) {
|
|
756
|
+
return new HcmError(`"${bundle}" needs a value for the parameter "${parameter.name}"` +
|
|
757
|
+
(parameter.description ? ` (${parameter.description})` : ''), `Pass --param ${parameter.name}=<value>, put it in a --params-file, set ` +
|
|
758
|
+
`${parameterEnvName(parameter.name)}, or run this in a terminal to be asked.`);
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* One question. A menu when the bundle listed the answers, free text otherwise.
|
|
762
|
+
*
|
|
763
|
+
* The name is printed once, by whichever of the two is asking, and the default
|
|
764
|
+
* is offered as the suggestion so that pressing enter takes it.
|
|
765
|
+
*/
|
|
766
|
+
async function askParameter(parameter) {
|
|
767
|
+
const note = (parameter.description ? color.dim(` — ${parameter.description}`) : '') +
|
|
768
|
+
(parameter.secret ? color.dim(' (not stored; supply it again on update)') : '');
|
|
769
|
+
if (parameter.choices) {
|
|
770
|
+
return select(` ${color.bold(parameter.name)}${note}`, parameter.choices.map((choice) => ({ value: choice, label: choice })), parameter.default);
|
|
771
|
+
}
|
|
772
|
+
if (note)
|
|
773
|
+
log.plain(` ${color.dim(note.replace(/^ — /, ''))}`);
|
|
774
|
+
const suggestion = parameter.default;
|
|
775
|
+
const question = ` ${color.bold(parameter.name)}` +
|
|
776
|
+
(suggestion === undefined ? '' : color.dim(` [${suggestion}]`));
|
|
777
|
+
const answer = await text(question, (value) => {
|
|
778
|
+
const candidate = value === '' && suggestion !== undefined ? suggestion : value;
|
|
779
|
+
if (candidate === '' && !parameter.required)
|
|
780
|
+
return undefined;
|
|
781
|
+
const problem = valueProblem(parameter, candidate);
|
|
782
|
+
return problem ? `that value ${problem}` : undefined;
|
|
783
|
+
});
|
|
784
|
+
return answer === '' && suggestion !== undefined ? suggestion : answer;
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* The values worth writing into the ledger. Secrets are not: the project-scope
|
|
788
|
+
* ledger sits in `.hcm/state.json` next to the code, and a token in there is a
|
|
789
|
+
* token in the repository.
|
|
790
|
+
*/
|
|
791
|
+
export function storableValues(values, parameters) {
|
|
792
|
+
const secret = new Set(parameters.filter((one) => one.secret).map((one) => one.name));
|
|
793
|
+
return Object.fromEntries(Object.entries(values).filter(([name]) => !secret.has(name)));
|
|
794
|
+
}
|
|
795
|
+
// ---------------------------------------------------------------------------
|
|
796
|
+
// Saying it out loud
|
|
797
|
+
// ---------------------------------------------------------------------------
|
|
798
|
+
/** What to store in the registry: what will be asked, not how it is validated. */
|
|
799
|
+
export function summarizeParameters(parameters) {
|
|
800
|
+
return parameters.map((parameter) => ({
|
|
801
|
+
name: parameter.name,
|
|
802
|
+
...(parameter.description ? { description: parameter.description } : {}),
|
|
803
|
+
...(parameter.default !== undefined ? { default: parameter.default } : {}),
|
|
804
|
+
...(parameter.required ? { required: true } : {}),
|
|
805
|
+
...(parameter.secret ? { secret: true } : {}),
|
|
806
|
+
...(parameter.flavors.length > 0 ? { flavors: parameter.flavors } : {}),
|
|
807
|
+
...(parameter.targets.length > 0 ? { targets: parameter.targets } : {}),
|
|
808
|
+
}));
|
|
809
|
+
}
|
|
810
|
+
/** `AGENT_NAME, TEAM` -- for the listings. */
|
|
811
|
+
export function parameterNames(parameters) {
|
|
812
|
+
return (parameters ?? []).map((parameter) => parameter.name).join(', ');
|
|
813
|
+
}
|
|
814
|
+
/** Where a parameter applies, in prose. Empty for a global one. */
|
|
815
|
+
export function describeScope(parameter) {
|
|
816
|
+
const parts = [];
|
|
817
|
+
if (parameter.flavors?.length)
|
|
818
|
+
parts.push(`flavor ${parameter.flavors.join('/')}`);
|
|
819
|
+
if (parameter.targets?.length)
|
|
820
|
+
parts.push(`harness ${parameter.targets.join('/')}`);
|
|
821
|
+
return parts.join(', ');
|
|
822
|
+
}
|
|
823
|
+
// ---------------------------------------------------------------------------
|
|
824
|
+
// Validation
|
|
825
|
+
// ---------------------------------------------------------------------------
|
|
826
|
+
/**
|
|
827
|
+
* Text worth scanning for placeholders in a file we would otherwise copy
|
|
828
|
+
* verbatim. Read synchronously because `validateBundle` is, and a bundle's
|
|
829
|
+
* supporting files are small enough that making it async would buy nothing.
|
|
830
|
+
*/
|
|
831
|
+
function readableText(absolutePath) {
|
|
832
|
+
try {
|
|
833
|
+
return decodeText(readFileSync(absolutePath));
|
|
834
|
+
}
|
|
835
|
+
catch {
|
|
836
|
+
return undefined;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
/** Every piece of text one resource would install, with a label for each. */
|
|
840
|
+
function resourceText(resource) {
|
|
841
|
+
const found = [];
|
|
842
|
+
if (resource.body !== undefined)
|
|
843
|
+
found.push({ where: resource.bundlePath, text: resource.body });
|
|
844
|
+
for (const [key, value] of Object.entries(resource.frontmatter)) {
|
|
845
|
+
found.push({ where: `${resource.bundlePath} (${key})`, text: JSON.stringify(value) });
|
|
846
|
+
}
|
|
847
|
+
if (resource.data !== undefined) {
|
|
848
|
+
found.push({ where: resource.bundlePath, text: JSON.stringify(resource.data) });
|
|
849
|
+
}
|
|
850
|
+
// Skills and assets carry files the loader never parsed; they are templated
|
|
851
|
+
// too, so they are scanned too.
|
|
852
|
+
for (const file of resource.files) {
|
|
853
|
+
if (file.absolutePath === resource.primaryFile)
|
|
854
|
+
continue;
|
|
855
|
+
const text = readableText(file.absolutePath);
|
|
856
|
+
if (text !== undefined) {
|
|
857
|
+
found.push({ where: `${resource.bundlePath}/${file.relativePath}`, text });
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
if (resource.kind === 'asset') {
|
|
861
|
+
const text = readableText(resource.primaryFile);
|
|
862
|
+
if (text !== undefined)
|
|
863
|
+
found.push({ where: resource.bundlePath, text });
|
|
864
|
+
}
|
|
865
|
+
return found;
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Parameter mistakes worth reporting, surfaced by `hcm validate` and `hcm info`.
|
|
869
|
+
*
|
|
870
|
+
* The first two are the ones that bite: a placeholder nothing declares installs
|
|
871
|
+
* verbatim, and a parameter narrower than the file that uses it installs
|
|
872
|
+
* verbatim for everyone outside the narrowing. Both look, in the installed
|
|
873
|
+
* file, exactly like a bundle that forgot to finish a sentence.
|
|
874
|
+
*/
|
|
875
|
+
export function parameterProblems(bundle) {
|
|
876
|
+
const problems = [];
|
|
877
|
+
const declared = new Map(bundle.parameters.map((parameter) => [parameter.name, parameter]));
|
|
878
|
+
const flavors = new Set(bundle.flavors.map((flavor) => flavor.name.toLowerCase()));
|
|
879
|
+
const bundleTargets = bundle.manifest.targets?.length ? bundle.manifest.targets : [...TARGET_IDS];
|
|
880
|
+
const used = new Set();
|
|
881
|
+
for (const resource of bundle.resources) {
|
|
882
|
+
// The name decides the installed filename, and a path is never templated.
|
|
883
|
+
if (typeof resource.frontmatter.name === 'string' && hasPlaceholder(resource.frontmatter.name)) {
|
|
884
|
+
problems.push(`${resource.bundlePath}: "name" holds a placeholder, but it decides the installed ` +
|
|
885
|
+
'filename and is never substituted');
|
|
886
|
+
}
|
|
887
|
+
for (const { where, text } of resourceText(resource)) {
|
|
888
|
+
for (const name of placeholderNames(text)) {
|
|
889
|
+
used.add(name);
|
|
890
|
+
const parameter = declared.get(name);
|
|
891
|
+
if (!parameter) {
|
|
892
|
+
problems.push(`${where}: "<%${name}%>" is not a parameter this bundle declares, so it installs verbatim`);
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
// A parameter narrowed to one flavor or one harness is only *asked* for
|
|
896
|
+
// there; everywhere else it falls back to its default. With no default
|
|
897
|
+
// there is nothing to fall back to, and the placeholder installs as it
|
|
898
|
+
// stands -- which is the only version of this worth reporting.
|
|
899
|
+
if (parameter.default !== undefined)
|
|
900
|
+
continue;
|
|
901
|
+
if (parameter.flavors.length > 0) {
|
|
902
|
+
const outside = resource.flavors.length === 0 ||
|
|
903
|
+
!resource.flavors.every((flavor) => parameter.flavors.some((one) => one.toLowerCase() === flavor.toLowerCase()));
|
|
904
|
+
if (outside) {
|
|
905
|
+
problems.push(`${where}: "<%${name}%>" is only asked for with flavor ${parameter.flavors.join('/')} ` +
|
|
906
|
+
`and has no default, but ` +
|
|
907
|
+
(resource.flavors.length === 0
|
|
908
|
+
? 'this resource is common and installs whatever flavor was asked for'
|
|
909
|
+
: `this resource belongs to ${resource.flavors.join('/')}`));
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
if (parameter.targets.length > 0) {
|
|
913
|
+
const elsewhere = bundleTargets.filter((id) => !parameter.targets.includes(id));
|
|
914
|
+
if (elsewhere.length > 0) {
|
|
915
|
+
problems.push(`${where}: "<%${name}%>" is only asked for on ${parameter.targets.join('/')} and has ` +
|
|
916
|
+
`no default, but this bundle also installs into ${elsewhere.join(', ')}`);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
for (const parameter of bundle.parameters) {
|
|
923
|
+
if (!used.has(parameter.name)) {
|
|
924
|
+
problems.push(`Parameter "${parameter.name}" is declared but never used; installing would ask for a ` +
|
|
925
|
+
'value and put it nowhere');
|
|
926
|
+
}
|
|
927
|
+
for (const flavor of parameter.flavors) {
|
|
928
|
+
if (flavors.has(flavor.toLowerCase()))
|
|
929
|
+
continue;
|
|
930
|
+
problems.push(`Parameter "${parameter.name}": flavor "${flavor}" is not one this bundle declares` +
|
|
931
|
+
(bundle.flavors.length > 0
|
|
932
|
+
? ` (it has: ${bundle.flavors.map((one) => one.name).join(', ')})`
|
|
933
|
+
: ' (it has none)'));
|
|
934
|
+
}
|
|
935
|
+
for (const target of parameter.targets) {
|
|
936
|
+
if (bundleTargets.includes(target))
|
|
937
|
+
continue;
|
|
938
|
+
problems.push(`Parameter "${parameter.name}": harness "${target}" is not one this bundle supports ` +
|
|
939
|
+
`(it declares: ${bundleTargets.join(', ')})`);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
return problems;
|
|
943
|
+
}
|
|
944
|
+
//# sourceMappingURL=parameters.js.map
|