@diagonal-hq/cli 0.38.2 → 0.38.4

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.
@@ -0,0 +1,262 @@
1
+ import { createRequire as __diagonalCreateRequire } from 'node:module';
2
+ const require = __diagonalCreateRequire(import.meta.url);
3
+ import {
4
+ frontMatterUidInsertion,
5
+ readDefinitionUid,
6
+ require_dist
7
+ } from "./cli-chunk-T6442V5V.mjs";
8
+ import {
9
+ SourceError,
10
+ characterCount,
11
+ diagnosticFromError
12
+ } from "./cli-chunk-F2J44UIS.mjs";
13
+ import {
14
+ diagnostic,
15
+ diagnosticAt,
16
+ withoutDevInfix
17
+ } from "./cli-chunk-A7Q2M4ZU.mjs";
18
+ import {
19
+ __toESM
20
+ } from "./cli-chunk-LRS5IEI3.mjs";
21
+
22
+ // scripts/lib/runtime-build/skill-file.mjs
23
+ var import_yaml = __toESM(require_dist(), 1);
24
+ import { readFileSync } from "node:fs";
25
+ import { join } from "node:path";
26
+ var SKILL_NAME_MAX_LENGTH = 64;
27
+ var SKILL_DESCRIPTION_MAX_LENGTH = 1024;
28
+ var SKILL_INSTRUCTIONS_MAX_LENGTH = 2e4;
29
+ var FRONT_MATTER = /^---[ \t]*\r?\n(?:([\s\S]*?)\r?\n)?---[ \t]*(?:\r?\n|$)/;
30
+ var FRONT_MATTER_FIRST_LINE = 2;
31
+ var PROPERTIES = {
32
+ name: { type: "string", required: true, maxLength: SKILL_NAME_MAX_LENGTH },
33
+ description: { type: "string", required: true, maxLength: SKILL_DESCRIPTION_MAX_LENGTH },
34
+ enabled: { type: "boolean", required: false, fallback: true },
35
+ manualOnly: { type: "boolean", required: false, fallback: false }
36
+ };
37
+ var KEYS = ["uid", ...Object.keys(PROPERTIES)];
38
+ var EXAMPLE = ["---", "uid: 019a4d6e-3c2f-7b8a-9d1e-5f6a7b8c9d0e", "name: membership-support", "description: Help members with membership questions.", "---"].join("\n");
39
+ function lineOf(source, offset) {
40
+ let line = 1;
41
+ for (let index = 0; index < offset; index++) {
42
+ if (source[index] === "\n") {
43
+ line++;
44
+ }
45
+ }
46
+ return line;
47
+ }
48
+ function readProperties(path, document, lineCounter, diagnostics) {
49
+ const at = (node, message) => {
50
+ const position = node?.range ? lineCounter.linePos(node.range[0]) : { line: 1, col: 1 };
51
+ diagnostics.push(diagnostic(path, position.line + FRONT_MATTER_FIRST_LINE - 1, position.col, message));
52
+ };
53
+ if (!(0, import_yaml.isMap)(document.contents)) {
54
+ at(document.contents, `The front matter must be a map of properties, for example:
55
+ ${EXAMPLE}`);
56
+ return null;
57
+ }
58
+ const values = {};
59
+ const seen = /* @__PURE__ */ new Set();
60
+ for (const pair of document.contents.items) {
61
+ const key = (0, import_yaml.isScalar)(pair.key) ? pair.key.value : null;
62
+ if (typeof key !== "string") {
63
+ at(pair.key, "A front matter key must be a plain name");
64
+ continue;
65
+ }
66
+ if (seen.has(key)) {
67
+ at(pair.key, `Duplicate skill property "${key}"`);
68
+ continue;
69
+ }
70
+ seen.add(key);
71
+ if (key === "uid") {
72
+ continue;
73
+ }
74
+ const property = PROPERTIES[key];
75
+ if (property === void 0) {
76
+ at(pair.key, `Unknown skill property "${key}". Use ${KEYS.join(", ")}`);
77
+ continue;
78
+ }
79
+ const value = (0, import_yaml.isScalar)(pair.value) ? pair.value.value : void 0;
80
+ if (typeof value !== property.type) {
81
+ at(
82
+ pair.value ?? pair.key,
83
+ property.type === "boolean" ? `"${key}" must be true or false without quotes` : `"${key}" must be a string`
84
+ );
85
+ continue;
86
+ }
87
+ if (property.type === "string" && value.trim() === "") {
88
+ at(pair.value, `"${key}" must not be empty`);
89
+ continue;
90
+ }
91
+ if (property.maxLength !== void 0 && characterCount(value) > property.maxLength) {
92
+ at(pair.value, `"${key}" is longer than ${property.maxLength} characters`);
93
+ continue;
94
+ }
95
+ values[key] = value;
96
+ }
97
+ for (const [key, property] of Object.entries(PROPERTIES)) {
98
+ if (property.required && !seen.has(key)) {
99
+ at(document.contents, `The front matter needs "${key}"`);
100
+ }
101
+ }
102
+ return values;
103
+ }
104
+ function buildSkill(path, source) {
105
+ const diagnostics = [];
106
+ const match = FRONT_MATTER.exec(source);
107
+ if (match === null) {
108
+ diagnostics.push(
109
+ diagnostic(path, 1, 1, `A skill file must start with front matter, for example:
110
+ ${EXAMPLE}`)
111
+ );
112
+ return { skill: null, diagnostics };
113
+ }
114
+ const lineCounter = new import_yaml.LineCounter();
115
+ const document = (0, import_yaml.parseDocument)(match[1] ?? "", {
116
+ lineCounter,
117
+ schema: "core",
118
+ // Duplicate keys are reported at the repeated key, not at the map.
119
+ uniqueKeys: false,
120
+ prettyErrors: false
121
+ });
122
+ for (const error of [...document.errors, ...document.warnings]) {
123
+ const position = error.linePos?.[0] ?? { line: 1, col: 1 };
124
+ diagnostics.push(
125
+ diagnostic(path, position.line + FRONT_MATTER_FIRST_LINE - 1, position.col, error.message)
126
+ );
127
+ }
128
+ const values = diagnostics.length === 0 ? readProperties(path, document, lineCounter, diagnostics) : null;
129
+ const instructions = source.slice(match[0].length).trim();
130
+ const bodyLine = lineOf(source, match[0].length);
131
+ if (instructions === "") {
132
+ diagnostics.push(diagnostic(path, bodyLine, 1, "The skill has no instructions. Write them below the front matter"));
133
+ } else if (characterCount(instructions) > SKILL_INSTRUCTIONS_MAX_LENGTH) {
134
+ diagnostics.push(
135
+ diagnostic(path, bodyLine, 1, `The instructions are longer than ${SKILL_INSTRUCTIONS_MAX_LENGTH} characters`)
136
+ );
137
+ }
138
+ if (diagnostics.length > 0 || values === null) {
139
+ return { skill: null, diagnostics };
140
+ }
141
+ return {
142
+ skill: {
143
+ path,
144
+ uid: null,
145
+ name: values.name,
146
+ description: values.description,
147
+ enabled: values.enabled ?? PROPERTIES.enabled.fallback,
148
+ manual_only: values.manualOnly ?? PROPERTIES.manualOnly.fallback,
149
+ instructions
150
+ },
151
+ diagnostics,
152
+ frontMatter: { map: document.contents, offset: source.indexOf("\n") + 1 }
153
+ };
154
+ }
155
+ function skillUidDefinition(path, source) {
156
+ const built = buildSkill(path, source);
157
+ return built.skill === null ? null : { type: "skill", path, source, frontMatter: built.frontMatter };
158
+ }
159
+ function isSkillPath(path) {
160
+ return /^skills\/[^/]+\.md$/.test(withoutDevInfix(path));
161
+ }
162
+ var PLACEHOLDER_UID = "00000000-0000-7000-8000-000000000000";
163
+ function skillFrontMatter(root, paths) {
164
+ return Object.fromEntries(
165
+ paths.map((path) => {
166
+ const answer = skillFrontMatterOf(path, readFileSync(join(root, path), "utf8"));
167
+ if (!wellFormed(answer)) {
168
+ throw new Error(
169
+ `The workspace check cannot read a string with a lone surrogate in ${path}. Write the character as a pair, and report the file to the Diagonal platform team.`
170
+ );
171
+ }
172
+ return [path, answer];
173
+ })
174
+ );
175
+ }
176
+ function wellFormed(value) {
177
+ if (typeof value === "string") {
178
+ return value.isWellFormed();
179
+ }
180
+ if (value === null || typeof value !== "object") {
181
+ return true;
182
+ }
183
+ return Object.entries(value).every(([key, item]) => key.isWellFormed() && wellFormed(item));
184
+ }
185
+ function skillFrontMatterOf(path, source) {
186
+ const { skill, diagnostics, frontMatter } = buildSkill(path, source);
187
+ if (skill === null) {
188
+ return { skill, diagnostics };
189
+ }
190
+ const definition = { type: "skill", path, source, frontMatter };
191
+ let read;
192
+ try {
193
+ read = readDefinitionUid(definition);
194
+ } catch (error) {
195
+ if (!(error instanceof SourceError)) {
196
+ throw error;
197
+ }
198
+ return { skill, diagnostics, uid: { diagnostic: diagnosticFromError(path, error) } };
199
+ }
200
+ const { line, column } = diagnosticAt(path, read.at, "");
201
+ if (read.uid !== null) {
202
+ return { skill, diagnostics, uid: { uid: read.uid, line, column } };
203
+ }
204
+ const insertion = frontMatterUidInsertion(source, frontMatter, PLACEHOLDER_UID);
205
+ const at = insertion === null ? -1 : insertion.text.indexOf(PLACEHOLDER_UID);
206
+ return {
207
+ skill,
208
+ diagnostics,
209
+ uid: { uid: null, line, column },
210
+ insertion: insertion === null ? null : { offset: insertion.start, before: insertion.text.slice(0, at), after: insertion.text.slice(at + PLACEHOLDER_UID.length) }
211
+ };
212
+ }
213
+ var SkillFileError = class extends Error {
214
+ };
215
+ var WRITABLE = [
216
+ ["name", "name", null],
217
+ ["description", "description", null],
218
+ ["enabled", "enabled", true],
219
+ ["manualOnly", "manual_only", false]
220
+ ];
221
+ function writeSkillFile(source, skill) {
222
+ let document = new import_yaml.Document({});
223
+ let body = "";
224
+ if (source !== null) {
225
+ const match = FRONT_MATTER.exec(source);
226
+ if (match === null) {
227
+ throw new SkillFileError("The skill file has no front matter to change");
228
+ }
229
+ document = (0, import_yaml.parseDocument)(match[1] ?? "", { schema: "core" });
230
+ if (document.errors.length > 0 || !(0, import_yaml.isMap)(document.contents)) {
231
+ throw new SkillFileError("The skill file front matter is not a valid YAML map");
232
+ }
233
+ body = source.slice(match[0].length).replace(/^(\r?\n)+/, "");
234
+ }
235
+ if (source === null && skill.uid !== void 0) document.set("uid", skill.uid);
236
+ for (const [key, field, fallback] of WRITABLE) {
237
+ const value = skill[field];
238
+ if (value === void 0 || value === fallback && !document.has(key)) {
239
+ continue;
240
+ }
241
+ document.set(key, value);
242
+ }
243
+ if (skill.instructions !== void 0) {
244
+ body = `${skill.instructions.trim()}
245
+ `;
246
+ }
247
+ return `---
248
+ ${String(document)}---
249
+
250
+ ${body}`;
251
+ }
252
+ export {
253
+ SKILL_DESCRIPTION_MAX_LENGTH,
254
+ SKILL_INSTRUCTIONS_MAX_LENGTH,
255
+ SKILL_NAME_MAX_LENGTH,
256
+ SkillFileError,
257
+ buildSkill,
258
+ isSkillPath,
259
+ skillFrontMatter,
260
+ skillUidDefinition,
261
+ writeSkillFile
262
+ };