@thegitai/cli 1.0.0-preview.66 → 1.0.0-preview.68
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 +1 -1
- package/README.md +79 -31
- package/dist/bin/ai.js +1 -1
- package/dist/src/agent-mode.js +1 -0
- package/dist/src/api/browser-login.js +1 -1
- package/dist/src/api/chat.js +2 -0
- package/dist/src/core/clipboard.js +1 -1
- package/dist/src/executor.js +1 -1
- package/dist/src/help-text.js +50 -44
- package/dist/src/patcher.js +3 -0
- package/dist/src/signin.js +1 -1
- package/dist/src/skills/discovery.js +280 -0
- package/dist/src/skills/frontmatter.js +192 -0
- package/dist/src/skills/install.js +394 -0
- package/dist/src/skills/types.js +43 -0
- package/dist/src/skills-command.js +71 -0
- package/dist/src/tools/index.js +2 -0
- package/dist/src/tools/read-file.js +2 -1
- package/dist/src/tools/shell-diagnostics.js +1 -1
- package/dist/src/tools/use-skill.js +38 -0
- package/dist/src/ui/repl.js +147 -13
- package/dist/src/ui/slash-commands.js +19 -1
- package/dist/src/ui/tui/build-frame.js +95 -47
- package/dist/src/ui/tui/shell-input.js +33 -1
- package/dist/src/ui/tui/shortcuts.js +3 -3
- package/dist/src/update-check.js +2 -2
- package/package.json +6 -6
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { parseSkillFile } from './frontmatter.js';
|
|
5
|
+
import { BUNDLED_SKILLS, SKILL_ARGUMENT_HINT_MAX_CHARS, SKILL_CONTENT_MAX_CHARS, SKILL_DESCRIPTION_MAX_CHARS, SKILL_FILE_LIST_LIMIT, SKILL_FILE_MAX_BYTES, SKILL_FILE_NAME, SKILL_MAX_ENTRIES, cleanSkillText, isValidSkillName, parseSkillInvocation, } from './types.js';
|
|
6
|
+
const MAX_SKILL_DIRS_PER_ROOT = 300;
|
|
7
|
+
const SKIPPED_LISTING_DIRS = new Set(['.git', 'node_modules', '__pycache__', '.venv', 'venv']);
|
|
8
|
+
const parsedCache = new Map();
|
|
9
|
+
export function personalSkillsRoot(env = process.env) {
|
|
10
|
+
const home = String(env.THEGITAI_HOME ?? '').trim();
|
|
11
|
+
return path.join(home ? path.resolve(home) : path.join(os.homedir(), '.thegitai'), 'skills');
|
|
12
|
+
}
|
|
13
|
+
export function projectSkillsRoot(rootDir) {
|
|
14
|
+
return path.join(path.resolve(rootDir), '.thegitai', 'skills');
|
|
15
|
+
}
|
|
16
|
+
function safeRealpath(target) {
|
|
17
|
+
try {
|
|
18
|
+
return realpathSync(target);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function isInside(parent, child) {
|
|
25
|
+
const relative = path.relative(parent, child);
|
|
26
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
27
|
+
}
|
|
28
|
+
function parseSkill(file, folderName) {
|
|
29
|
+
let stat;
|
|
30
|
+
try {
|
|
31
|
+
stat = statSync(file);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return { ok: false, message: `No ${SKILL_FILE_NAME} in this folder.` };
|
|
35
|
+
}
|
|
36
|
+
if (!stat.isFile())
|
|
37
|
+
return { ok: false, message: `${SKILL_FILE_NAME} is not a file.` };
|
|
38
|
+
if (stat.size > SKILL_FILE_MAX_BYTES) {
|
|
39
|
+
return { ok: false, message: `${SKILL_FILE_NAME} is larger than ${SKILL_FILE_MAX_BYTES / 1024} KB.` };
|
|
40
|
+
}
|
|
41
|
+
const cached = parsedCache.get(file);
|
|
42
|
+
if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {
|
|
43
|
+
return cached.result;
|
|
44
|
+
}
|
|
45
|
+
let result;
|
|
46
|
+
try {
|
|
47
|
+
result = interpretSkillFile(readFileSync(file, 'utf8'), folderName);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
result = { ok: false, message: `Could not read ${SKILL_FILE_NAME}: ${error.message}` };
|
|
51
|
+
}
|
|
52
|
+
parsedCache.set(file, { mtimeMs: stat.mtimeMs, size: stat.size, result });
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
export function interpretSkillFile(text, folderName) {
|
|
56
|
+
const parsed = parseSkillFile(text);
|
|
57
|
+
if (!parsed.ok)
|
|
58
|
+
return { ok: false, message: parsed.error };
|
|
59
|
+
const rawName = parsed.data.name;
|
|
60
|
+
const name = typeof rawName === 'string' && rawName.trim() ? rawName.trim() : folderName;
|
|
61
|
+
if (!isValidSkillName(name)) {
|
|
62
|
+
return {
|
|
63
|
+
ok: false,
|
|
64
|
+
message: `Invalid name "${String(name).slice(0, 80)}": use 1-64 lowercase letters, digits and single hyphens.`,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (name !== folderName) {
|
|
68
|
+
return { ok: false, message: `The name "${name}" must match its folder name "${folderName}".` };
|
|
69
|
+
}
|
|
70
|
+
const description = cleanSkillText(parsed.data.description, SKILL_DESCRIPTION_MAX_CHARS);
|
|
71
|
+
if (!description)
|
|
72
|
+
return { ok: false, message: 'Missing description in the frontmatter.' };
|
|
73
|
+
const argumentHint = cleanSkillText(parsed.data['argument-hint'], SKILL_ARGUMENT_HINT_MAX_CHARS);
|
|
74
|
+
return {
|
|
75
|
+
ok: true,
|
|
76
|
+
skill: {
|
|
77
|
+
name,
|
|
78
|
+
description,
|
|
79
|
+
typedOnly: parsed.data['disable-model-invocation'] === true,
|
|
80
|
+
...(argumentHint ? { argumentHint } : {}),
|
|
81
|
+
},
|
|
82
|
+
body: parsed.body,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function scanRoot(root, scope, containment) {
|
|
86
|
+
const skills = [];
|
|
87
|
+
const problems = [];
|
|
88
|
+
let entries;
|
|
89
|
+
try {
|
|
90
|
+
entries = readdirSync(root, { withFileTypes: true });
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return { skills, problems };
|
|
94
|
+
}
|
|
95
|
+
const folders = entries
|
|
96
|
+
.filter((entry) => !entry.name.startsWith('.'))
|
|
97
|
+
.filter((entry) => entry.isDirectory() || entry.isSymbolicLink())
|
|
98
|
+
.map((entry) => entry.name)
|
|
99
|
+
.sort((a, b) => a.localeCompare(b))
|
|
100
|
+
.slice(0, MAX_SKILL_DIRS_PER_ROOT);
|
|
101
|
+
for (const folder of folders) {
|
|
102
|
+
const dir = path.join(root, folder);
|
|
103
|
+
let isDirectory = false;
|
|
104
|
+
try {
|
|
105
|
+
isDirectory = statSync(dir).isDirectory();
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
isDirectory = false;
|
|
109
|
+
}
|
|
110
|
+
if (!isDirectory)
|
|
111
|
+
continue;
|
|
112
|
+
const file = path.join(dir, SKILL_FILE_NAME);
|
|
113
|
+
if (containment) {
|
|
114
|
+
const realFile = safeRealpath(file);
|
|
115
|
+
if (realFile && !isInside(containment, realFile)) {
|
|
116
|
+
problems.push({ scope, dir, message: `${SKILL_FILE_NAME} links outside the project, so it was not loaded.` });
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const parsed = parseSkill(file, folder);
|
|
121
|
+
if (!parsed.ok) {
|
|
122
|
+
problems.push({ scope, dir, message: parsed.message });
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
skills.push({ ...parsed.skill, scope, dir, file });
|
|
126
|
+
}
|
|
127
|
+
return { skills, problems };
|
|
128
|
+
}
|
|
129
|
+
export function discoverSkills(rootDir, env = process.env) {
|
|
130
|
+
const personalRoot = personalSkillsRoot(env);
|
|
131
|
+
const projectRootCandidate = projectSkillsRoot(rootDir);
|
|
132
|
+
const samePlace = path.resolve(projectRootCandidate) === path.resolve(personalRoot) ||
|
|
133
|
+
(safeRealpath(projectRootCandidate) ?? '') === (safeRealpath(personalRoot) ?? ' ');
|
|
134
|
+
const projectRoot = samePlace ? null : projectRootCandidate;
|
|
135
|
+
const personal = scanRoot(personalRoot, 'personal', null);
|
|
136
|
+
const projectContainment = safeRealpath(path.resolve(rootDir));
|
|
137
|
+
const project = projectRoot
|
|
138
|
+
? scanRoot(projectRoot, 'project', projectContainment)
|
|
139
|
+
: { skills: [], problems: [] };
|
|
140
|
+
const skills = [];
|
|
141
|
+
const shadowed = [];
|
|
142
|
+
const seen = new Set();
|
|
143
|
+
for (const skill of [...personal.skills, ...project.skills]) {
|
|
144
|
+
if (seen.has(skill.name)) {
|
|
145
|
+
shadowed.push(skill);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
seen.add(skill.name);
|
|
149
|
+
skills.push(skill);
|
|
150
|
+
}
|
|
151
|
+
const problems = [...personal.problems, ...project.problems];
|
|
152
|
+
for (const extra of skills.splice(SKILL_MAX_ENTRIES)) {
|
|
153
|
+
problems.push({ scope: extra.scope, dir: extra.dir, message: `Only the first ${SKILL_MAX_ENTRIES} skills are loaded.` });
|
|
154
|
+
}
|
|
155
|
+
return { personalRoot, projectRoot, skills, shadowed, problems };
|
|
156
|
+
}
|
|
157
|
+
export function skillCatalogue(discovery) {
|
|
158
|
+
return discovery.skills.map((skill) => ({
|
|
159
|
+
name: skill.name,
|
|
160
|
+
description: skill.description,
|
|
161
|
+
scope: skill.scope,
|
|
162
|
+
...(skill.typedOnly ? { typedOnly: true } : {}),
|
|
163
|
+
...(skill.argumentHint ? { argumentHint: skill.argumentHint } : {}),
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
export function bundledSkillNamed(name) {
|
|
167
|
+
return BUNDLED_SKILLS.find((skill) => skill.name === name) ?? null;
|
|
168
|
+
}
|
|
169
|
+
export function findSkill(discovery, name) {
|
|
170
|
+
return discovery.skills.find((skill) => skill.name === name) ?? null;
|
|
171
|
+
}
|
|
172
|
+
function listSkillFiles(dir) {
|
|
173
|
+
const files = [];
|
|
174
|
+
let truncated = false;
|
|
175
|
+
const walk = (current, depth) => {
|
|
176
|
+
if (truncated)
|
|
177
|
+
return;
|
|
178
|
+
if (depth > 4) {
|
|
179
|
+
truncated = true;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
let entries;
|
|
183
|
+
try {
|
|
184
|
+
entries = readdirSync(current, { withFileTypes: true });
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
190
|
+
for (const entry of entries) {
|
|
191
|
+
if (entry.name.startsWith('.') || SKIPPED_LISTING_DIRS.has(entry.name))
|
|
192
|
+
continue;
|
|
193
|
+
const full = path.join(current, entry.name);
|
|
194
|
+
if (entry.isDirectory()) {
|
|
195
|
+
walk(full, depth + 1);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (!entry.isFile())
|
|
199
|
+
continue;
|
|
200
|
+
const relative = path.relative(dir, full).split(path.sep).join('/');
|
|
201
|
+
if (relative === SKILL_FILE_NAME)
|
|
202
|
+
continue;
|
|
203
|
+
if (files.length >= SKILL_FILE_LIST_LIMIT) {
|
|
204
|
+
truncated = true;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
files.push(relative);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
walk(dir, 0);
|
|
211
|
+
return { files, truncated };
|
|
212
|
+
}
|
|
213
|
+
export function loadSkillContent(skill) {
|
|
214
|
+
const parsed = parseSkill(skill.file, path.basename(skill.dir));
|
|
215
|
+
if (!parsed.ok)
|
|
216
|
+
return { ok: false, error: parsed.message };
|
|
217
|
+
const truncated = parsed.body.length > SKILL_CONTENT_MAX_CHARS;
|
|
218
|
+
const listing = listSkillFiles(skill.dir);
|
|
219
|
+
return {
|
|
220
|
+
ok: true,
|
|
221
|
+
content: truncated ? parsed.body.slice(0, SKILL_CONTENT_MAX_CHARS) : parsed.body,
|
|
222
|
+
truncated,
|
|
223
|
+
files: listing.files,
|
|
224
|
+
filesTruncated: listing.truncated,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
export function isSkillsRelativePath(relPath) {
|
|
228
|
+
const normalized = relPath.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
229
|
+
return normalized.startsWith('.thegitai/skills/') && !normalized.split('/').includes('..');
|
|
230
|
+
}
|
|
231
|
+
export function isInsidePersonalSkills(absPath, env = process.env) {
|
|
232
|
+
const root = path.resolve(personalSkillsRoot(env));
|
|
233
|
+
const target = path.resolve(absPath);
|
|
234
|
+
return target !== root && isInside(root, target);
|
|
235
|
+
}
|
|
236
|
+
export function collectSkillRequest(rootDir, env, input) {
|
|
237
|
+
let discovery;
|
|
238
|
+
try {
|
|
239
|
+
discovery = discoverSkills(rootDir, env);
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
return { skills: [] };
|
|
243
|
+
}
|
|
244
|
+
const skills = skillCatalogue(discovery);
|
|
245
|
+
const invocation = parseSkillInvocation(input);
|
|
246
|
+
let invokedSkill;
|
|
247
|
+
if (invocation) {
|
|
248
|
+
const local = findSkill(discovery, invocation.name);
|
|
249
|
+
if (local) {
|
|
250
|
+
const loaded = loadSkillContent(local);
|
|
251
|
+
if (loaded.ok) {
|
|
252
|
+
invokedSkill = {
|
|
253
|
+
name: local.name,
|
|
254
|
+
arguments: invocation.arguments,
|
|
255
|
+
scope: local.scope,
|
|
256
|
+
dir: local.dir,
|
|
257
|
+
content: loaded.content,
|
|
258
|
+
...(loaded.truncated ? { contentTruncated: true } : {}),
|
|
259
|
+
files: loaded.files,
|
|
260
|
+
...(loaded.filesTruncated ? { filesTruncated: true } : {}),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else if (bundledSkillNamed(invocation.name)) {
|
|
265
|
+
invokedSkill = {
|
|
266
|
+
name: invocation.name,
|
|
267
|
+
arguments: invocation.arguments,
|
|
268
|
+
scope: 'bundled',
|
|
269
|
+
...skillFolders(rootDir, env),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return { skills, ...(invokedSkill ? { invokedSkill } : {}) };
|
|
274
|
+
}
|
|
275
|
+
export function skillFolders(rootDir, env) {
|
|
276
|
+
return {
|
|
277
|
+
personalSkillsDir: personalSkillsRoot(env),
|
|
278
|
+
projectSkillsDir: projectSkillsRoot(rootDir),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
function indentOf(text) {
|
|
2
|
+
const match = text.match(/^[ \t]*/);
|
|
3
|
+
return match ? match[0].replace(/\t/g, ' ').length : 0;
|
|
4
|
+
}
|
|
5
|
+
function closingQuoteIndex(value) {
|
|
6
|
+
const quote = value[0];
|
|
7
|
+
for (let index = 1; index < value.length; index += 1) {
|
|
8
|
+
const char = value[index];
|
|
9
|
+
if (quote === '"' && char === '\\') {
|
|
10
|
+
index += 1;
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
if (char !== quote)
|
|
14
|
+
continue;
|
|
15
|
+
if (quote === "'" && value[index + 1] === "'") {
|
|
16
|
+
index += 1;
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
return index;
|
|
20
|
+
}
|
|
21
|
+
return -1;
|
|
22
|
+
}
|
|
23
|
+
function stripComment(value) {
|
|
24
|
+
let start = 0;
|
|
25
|
+
if (value.startsWith('"') || value.startsWith("'")) {
|
|
26
|
+
const close = closingQuoteIndex(value);
|
|
27
|
+
if (close < 0)
|
|
28
|
+
return value;
|
|
29
|
+
start = close + 1;
|
|
30
|
+
}
|
|
31
|
+
for (let index = start; index < value.length; index += 1) {
|
|
32
|
+
if (value[index] === '#' && (index === 0 || /\s/.test(value[index - 1]))) {
|
|
33
|
+
return value.slice(0, index).trimEnd();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function unquote(raw) {
|
|
39
|
+
const value = stripComment(raw.trim()).trim();
|
|
40
|
+
if (value.length >= 2 && value.startsWith('"') && value.endsWith('"')) {
|
|
41
|
+
return value
|
|
42
|
+
.slice(1, -1)
|
|
43
|
+
.replace(/\\(["\\/nrt])/g, (_match, char) => {
|
|
44
|
+
if (char === 'n')
|
|
45
|
+
return '\n';
|
|
46
|
+
if (char === 'r')
|
|
47
|
+
return '\r';
|
|
48
|
+
if (char === 't')
|
|
49
|
+
return '\t';
|
|
50
|
+
return char;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (value.length >= 2 && value.startsWith("'") && value.endsWith("'")) {
|
|
54
|
+
return value.slice(1, -1).replace(/''/g, "'");
|
|
55
|
+
}
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
function scalar(raw) {
|
|
59
|
+
const value = unquote(raw);
|
|
60
|
+
const quoted = /^\s*["']/.test(raw);
|
|
61
|
+
if (!quoted && /^(true|yes|on)$/i.test(value))
|
|
62
|
+
return true;
|
|
63
|
+
if (!quoted && /^(false|no|off)$/i.test(value))
|
|
64
|
+
return false;
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
function blockScalar(lines, start, parentIndent, style) {
|
|
68
|
+
const collected = [];
|
|
69
|
+
let index = start;
|
|
70
|
+
let blockIndent = -1;
|
|
71
|
+
while (index < lines.length) {
|
|
72
|
+
const line = lines[index];
|
|
73
|
+
if (line.text.trim() === '') {
|
|
74
|
+
collected.push('');
|
|
75
|
+
index += 1;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (line.indent <= parentIndent)
|
|
79
|
+
break;
|
|
80
|
+
if (blockIndent < 0)
|
|
81
|
+
blockIndent = line.indent;
|
|
82
|
+
collected.push(line.text.replace(/\t/g, ' ').slice(Math.min(blockIndent, line.indent)));
|
|
83
|
+
index += 1;
|
|
84
|
+
}
|
|
85
|
+
while (collected.length && collected[collected.length - 1] === '')
|
|
86
|
+
collected.pop();
|
|
87
|
+
let value;
|
|
88
|
+
if (style.startsWith('|')) {
|
|
89
|
+
value = collected.join('\n');
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
value = collected
|
|
93
|
+
.join('\n')
|
|
94
|
+
.split(/\n{2,}/)
|
|
95
|
+
.map((paragraph) => paragraph.replace(/\n/g, ' '))
|
|
96
|
+
.join('\n');
|
|
97
|
+
}
|
|
98
|
+
return { value, next: index };
|
|
99
|
+
}
|
|
100
|
+
function nestedBlock(lines, start, parentIndent) {
|
|
101
|
+
const items = [];
|
|
102
|
+
const map = {};
|
|
103
|
+
const plain = [];
|
|
104
|
+
let index = start;
|
|
105
|
+
while (index < lines.length) {
|
|
106
|
+
const line = lines[index];
|
|
107
|
+
const trimmed = line.text.trim();
|
|
108
|
+
if (trimmed === '' || trimmed.startsWith('#')) {
|
|
109
|
+
index += 1;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (line.indent <= parentIndent)
|
|
113
|
+
break;
|
|
114
|
+
const listItem = trimmed.match(/^-\s*(.*)$/);
|
|
115
|
+
const pair = trimmed.match(/^([A-Za-z0-9_.-]+)\s*:\s*(.*)$/);
|
|
116
|
+
if (listItem) {
|
|
117
|
+
items.push(unquote(listItem[1] ?? ''));
|
|
118
|
+
}
|
|
119
|
+
else if (pair) {
|
|
120
|
+
map[pair[1]] = String(scalar(pair[2] ?? ''));
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
plain.push(unquote(trimmed));
|
|
124
|
+
}
|
|
125
|
+
index += 1;
|
|
126
|
+
}
|
|
127
|
+
if (items.length)
|
|
128
|
+
return { value: items, next: index };
|
|
129
|
+
if (Object.keys(map).length)
|
|
130
|
+
return { value: map, next: index };
|
|
131
|
+
return { value: plain.join(' '), next: index };
|
|
132
|
+
}
|
|
133
|
+
export function parseSkillFile(text) {
|
|
134
|
+
const source = String(text ?? '').replace(/^/, '').replace(/\r\n?/g, '\n');
|
|
135
|
+
const rows = source.split('\n');
|
|
136
|
+
if (rows[0]?.trim() !== '---') {
|
|
137
|
+
return { ok: false, error: 'SKILL.md must start with a --- frontmatter block.' };
|
|
138
|
+
}
|
|
139
|
+
const end = rows.findIndex((row, index) => index > 0 && /^(---|\.\.\.)\s*$/.test(row));
|
|
140
|
+
if (end < 0) {
|
|
141
|
+
return { ok: false, error: 'SKILL.md frontmatter is missing its closing ---.' };
|
|
142
|
+
}
|
|
143
|
+
const lines = rows.slice(1, end).map((row) => ({ text: row, indent: indentOf(row) }));
|
|
144
|
+
const data = {};
|
|
145
|
+
let index = 0;
|
|
146
|
+
while (index < lines.length) {
|
|
147
|
+
const line = lines[index];
|
|
148
|
+
const trimmed = line.text.trim();
|
|
149
|
+
if (trimmed === '' || trimmed.startsWith('#') || line.indent > 0) {
|
|
150
|
+
index += 1;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const pair = trimmed.match(/^([A-Za-z0-9_.-]+)\s*:(?:\s+(.*)|\s*)$/);
|
|
154
|
+
if (!pair) {
|
|
155
|
+
return { ok: false, error: `Frontmatter line ${index + 2} is not a "key: value" pair.` };
|
|
156
|
+
}
|
|
157
|
+
const key = pair[1];
|
|
158
|
+
const rest = String(pair[2] ?? '').trim();
|
|
159
|
+
if (/^[|>][+-]?\d*$/.test(rest)) {
|
|
160
|
+
const block = blockScalar(lines, index + 1, line.indent, rest);
|
|
161
|
+
data[key] = block.value;
|
|
162
|
+
index = block.next;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (rest === '') {
|
|
166
|
+
const nested = nestedBlock(lines, index + 1, line.indent);
|
|
167
|
+
data[key] = nested.value;
|
|
168
|
+
index = nested.next;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
let value = rest;
|
|
172
|
+
let next = index + 1;
|
|
173
|
+
if (!/^["']/.test(rest)) {
|
|
174
|
+
while (next < lines.length && lines[next].indent > line.indent && lines[next].text.trim() !== '') {
|
|
175
|
+
value += ` ${lines[next].text.trim()}`;
|
|
176
|
+
next += 1;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else if (!/^(["']).*\1\s*(#.*)?$/.test(rest)) {
|
|
180
|
+
const quote = rest[0];
|
|
181
|
+
while (next < lines.length) {
|
|
182
|
+
value += ` ${lines[next].text.trim()}`;
|
|
183
|
+
next += 1;
|
|
184
|
+
if (value.trimEnd().endsWith(quote))
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
data[key] = scalar(value);
|
|
189
|
+
index = next;
|
|
190
|
+
}
|
|
191
|
+
return { ok: true, data, body: rows.slice(end + 1).join('\n').trim() };
|
|
192
|
+
}
|