@thegitai/cli 1.0.0-preview.8 → 1.0.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 +1 -1
- package/README.md +301 -15
- package/dist/bin/ai.js +142 -112
- package/dist/bin/browser-host.js +284 -0
- package/dist/src/agent-mode.js +33 -7
- package/dist/src/api/auth.js +8 -2
- package/dist/src/api/browser-login.js +147 -35
- package/dist/src/api/chat.js +703 -166
- package/dist/src/api/contracts.js +55 -1
- package/dist/src/api/default-host.js +1 -0
- package/dist/src/api/http.js +168 -8
- package/dist/src/api/models.js +47 -9
- package/dist/src/api/startup-handshake.js +36 -0
- package/dist/src/api/turn-recovery.js +189 -0
- package/dist/src/background-jobs.js +8 -2
- package/dist/src/browser/bridge.js +268 -0
- package/dist/src/browser/chrome-profiles.js +73 -0
- package/dist/src/browser/framing.js +42 -0
- package/dist/src/browser/native-host.js +219 -0
- package/dist/src/browser/protocol.js +46 -0
- package/dist/src/browser/session-bridge.js +130 -0
- package/dist/src/cli-args.js +19 -5
- package/dist/src/client-environment.js +2 -0
- package/dist/src/core/clipboard.js +8 -14
- package/dist/src/core/image-limits.js +56 -0
- package/dist/src/core/image-path-extractor.js +70 -3
- package/dist/src/core/session-image-store.js +246 -0
- package/dist/src/edit-journal.js +33 -2
- package/dist/src/executor.js +26 -4
- package/dist/src/help-text.js +152 -41
- package/dist/src/install-id.js +32 -0
- package/dist/src/job-follow-up.js +40 -0
- package/dist/src/mcp-client.js +335 -0
- package/dist/src/mcp-command.js +176 -0
- package/dist/src/mcp-config.js +123 -0
- package/dist/src/mcp-sdk.js +3 -0
- package/dist/src/mcp-types.js +26 -0
- package/dist/src/patcher.js +46 -4
- package/dist/src/permissions.js +301 -0
- package/dist/src/preferences.js +39 -0
- package/dist/src/project-key.js +11 -0
- package/dist/src/project-model-preferences.js +74 -0
- package/dist/src/quota-notice.js +62 -0
- package/dist/src/reasoning-effort.js +27 -0
- package/dist/src/session-safety.js +0 -12
- package/dist/src/session-store.js +123 -30
- package/dist/src/session.js +12 -4
- package/dist/src/signin.js +58 -0
- 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/tool-executor.js +41 -51
- package/dist/src/tools/browser.js +928 -0
- package/dist/src/tools/delete-file.js +21 -5
- package/dist/src/tools/index.js +34 -10
- package/dist/src/tools/patch-file.js +14 -29
- package/dist/src/tools/read-file.js +7 -1
- package/dist/src/tools/read-image-file.js +85 -0
- package/dist/src/tools/replace-document-text.js +28 -18
- package/dist/src/tools/restore-checkpoint.js +1 -2
- package/dist/src/tools/run-command.js +14 -71
- package/dist/src/tools/run-node-script.js +12 -81
- package/dist/src/tools/save-generated-image.js +120 -0
- package/dist/src/tools/shell-diagnostics.js +246 -72
- package/dist/src/tools/str-replace.js +14 -29
- package/dist/src/tools/undo-edit.js +2 -7
- package/dist/src/tools/use-skill.js +38 -0
- package/dist/src/tools/write-file.js +69 -14
- package/dist/src/ui/prompt-history-store.js +1 -1
- package/dist/src/ui/repl.js +1155 -301
- package/dist/src/ui/slash-commands.js +140 -0
- package/dist/src/ui/tui/attention.js +25 -0
- package/dist/src/ui/tui/bridge.js +38 -5
- package/dist/src/ui/tui/build-frame.js +903 -304
- package/dist/src/ui/tui/composer-layout.js +76 -0
- package/dist/src/ui/tui/desktop-notification.js +118 -0
- package/dist/src/ui/tui/markdown-render.js +21 -11
- package/dist/src/ui/tui/model-label.js +13 -0
- package/dist/src/ui/tui/shell-input.js +266 -61
- package/dist/src/ui/tui/shortcuts.js +61 -0
- package/dist/src/ui/tui/terminal-theme.js +28 -0
- package/dist/src/ui/tui/text.js +31 -6
- package/dist/src/ui/tui/user-input.js +593 -0
- package/dist/src/update-check.js +205 -0
- package/dist/src/usage-notice.js +52 -0
- package/dist/src/usage.js +25 -2
- package/dist/src/utils.js +13 -0
- package/dist/src/version.js +52 -2
- package/dist/vendor/mcp/LICENSES.txt +414 -0
- package/dist/vendor/mcp/client.cjs +82 -0
- package/package.json +22 -9
- package/dist/src/project-index.js +0 -233
- package/dist/src/tools/code-intel.js +0 -472
- package/dist/src/tools/find-symbol.js +0 -70
- package/dist/src/tools/hover-symbol.js +0 -95
- package/dist/src/tools/list-symbols.js +0 -55
- package/dist/src/tools/search-code.js +0 -37
- package/dist/src/tools/signature-help.js +0 -118
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { chmodSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { interpretSkillFile } from './discovery.js';
|
|
6
|
+
import { parseSkillFile } from './frontmatter.js';
|
|
7
|
+
import { SKILL_FILE_MAX_BYTES, SKILL_FILE_NAME } from './types.js';
|
|
8
|
+
const MAX_FILES = 200;
|
|
9
|
+
const MAX_TOTAL_BYTES = 10 * 1024 * 1024;
|
|
10
|
+
const MAX_FILE_BYTES = 5 * 1024 * 1024;
|
|
11
|
+
const MAX_DEPTH = 8;
|
|
12
|
+
const SKIPPED_DIRS = new Set(['.git', 'node_modules', '__pycache__', '.venv', 'venv']);
|
|
13
|
+
const GITHUB_SEGMENT = /^[A-Za-z0-9_.-]+$/;
|
|
14
|
+
function expandHome(value) {
|
|
15
|
+
if (value === '~')
|
|
16
|
+
return os.homedir();
|
|
17
|
+
if (value.startsWith('~/') || value.startsWith('~\\'))
|
|
18
|
+
return path.join(os.homedir(), value.slice(2));
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function cleanSubpath(value) {
|
|
22
|
+
return value
|
|
23
|
+
.split('/')
|
|
24
|
+
.filter((segment) => segment && segment !== '.')
|
|
25
|
+
.join('/')
|
|
26
|
+
.replace(/\/SKILL\.md$/i, '')
|
|
27
|
+
.replace(/^SKILL\.md$/i, '');
|
|
28
|
+
}
|
|
29
|
+
export function parseSkillSource(raw, cwd) {
|
|
30
|
+
const value = raw.trim().replace(/^["']|["']$/g, '');
|
|
31
|
+
if (!value)
|
|
32
|
+
return null;
|
|
33
|
+
const url = value.match(/^(?:https?:\/\/)?(?:www\.)?github\.com\/([^/\s]+)\/([^/\s]+?)(?:\.git)?(?:\/(?:tree|blob)\/([^/\s]+)((?:\/[^\s]*)?))?\/?$/i);
|
|
34
|
+
if (url) {
|
|
35
|
+
const [, owner, repo, ref, rest] = url;
|
|
36
|
+
if (!GITHUB_SEGMENT.test(owner) || !GITHUB_SEGMENT.test(repo))
|
|
37
|
+
return null;
|
|
38
|
+
return { kind: 'github', owner: owner, repo: repo, ref: ref ?? null, subpath: cleanSubpath(rest ?? '') };
|
|
39
|
+
}
|
|
40
|
+
const local = path.resolve(cwd, expandHome(value));
|
|
41
|
+
if (/^(?:[./~]|[A-Za-z]:[\\/])/.test(value) || existsSync(local)) {
|
|
42
|
+
return { kind: 'local', path: local };
|
|
43
|
+
}
|
|
44
|
+
const short = value.match(/^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)((?:\/[^\s@]+)*)(?:@([^\s]+))?$/);
|
|
45
|
+
if (short) {
|
|
46
|
+
const [, owner, repo, rest, ref] = short;
|
|
47
|
+
return { kind: 'github', owner: owner, repo: repo, ref: ref ?? null, subpath: cleanSubpath(rest ?? '') };
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
function validateSkillText(text, fallbackName) {
|
|
52
|
+
const parsed = parseSkillFile(text);
|
|
53
|
+
if (!parsed.ok)
|
|
54
|
+
return { ok: false, error: parsed.error };
|
|
55
|
+
const declared = typeof parsed.data.name === 'string' && parsed.data.name.trim() ? parsed.data.name.trim() : fallbackName;
|
|
56
|
+
const interpreted = interpretSkillFile(text, declared);
|
|
57
|
+
if (!interpreted.ok)
|
|
58
|
+
return { ok: false, error: interpreted.message };
|
|
59
|
+
return { ok: true, name: interpreted.skill.name, description: interpreted.skill.description };
|
|
60
|
+
}
|
|
61
|
+
function linkOutsideMessage(relPath) {
|
|
62
|
+
return `${relPath} links to a file outside the skill folder. Copy that file into the skill folder, then add the skill again.`;
|
|
63
|
+
}
|
|
64
|
+
function linkMissingMessage(relPath) {
|
|
65
|
+
return `${relPath} links to a file that does not exist.`;
|
|
66
|
+
}
|
|
67
|
+
function isWithin(root, target) {
|
|
68
|
+
const relative = path.relative(root, target);
|
|
69
|
+
return !relative.startsWith('..') && !path.isAbsolute(relative);
|
|
70
|
+
}
|
|
71
|
+
function prepareLocal(sourcePath) {
|
|
72
|
+
let dir = sourcePath;
|
|
73
|
+
try {
|
|
74
|
+
if (statSync(dir).isFile()) {
|
|
75
|
+
if (path.basename(dir) !== SKILL_FILE_NAME) {
|
|
76
|
+
return { ok: false, error: `Point to a skill folder, or to its ${SKILL_FILE_NAME}.` };
|
|
77
|
+
}
|
|
78
|
+
dir = path.dirname(dir);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return { ok: false, error: `Nothing found at ${sourcePath}.` };
|
|
83
|
+
}
|
|
84
|
+
const skillFile = path.join(dir, SKILL_FILE_NAME);
|
|
85
|
+
if (!existsSync(skillFile)) {
|
|
86
|
+
const nested = findNestedSkillDirs(dir);
|
|
87
|
+
if (nested.length) {
|
|
88
|
+
return {
|
|
89
|
+
ok: false,
|
|
90
|
+
error: `${dir} holds several skills. Add one of them:\n${nested.slice(0, 20).map((item) => ` /skills add ${item}`).join('\n')}`,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return { ok: false, error: `No ${SKILL_FILE_NAME} in ${dir}.` };
|
|
94
|
+
}
|
|
95
|
+
const files = [];
|
|
96
|
+
let total = 0;
|
|
97
|
+
let realDir;
|
|
98
|
+
try {
|
|
99
|
+
realDir = realpathSync(dir);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
return { ok: false, error: `Could not read ${dir}: ${error.message}` };
|
|
103
|
+
}
|
|
104
|
+
const walk = (current, depth) => {
|
|
105
|
+
if (depth > MAX_DEPTH) {
|
|
106
|
+
return `${path.relative(dir, current)} is nested more than ${MAX_DEPTH} folders deep. Move its files higher up in the skill folder.`;
|
|
107
|
+
}
|
|
108
|
+
const entries = readdirSync(current, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
109
|
+
for (const entry of entries) {
|
|
110
|
+
if (SKIPPED_DIRS.has(entry.name))
|
|
111
|
+
continue;
|
|
112
|
+
const full = path.join(current, entry.name);
|
|
113
|
+
let stat = lstatSync(full);
|
|
114
|
+
if (stat.isSymbolicLink()) {
|
|
115
|
+
const relPath = path.relative(dir, full).split(path.sep).join('/');
|
|
116
|
+
let target;
|
|
117
|
+
try {
|
|
118
|
+
target = realpathSync(full);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return linkMissingMessage(relPath);
|
|
122
|
+
}
|
|
123
|
+
if (!isWithin(realDir, target))
|
|
124
|
+
return linkOutsideMessage(relPath);
|
|
125
|
+
stat = statSync(full);
|
|
126
|
+
}
|
|
127
|
+
if (stat.isDirectory()) {
|
|
128
|
+
const error = walk(full, depth + 1);
|
|
129
|
+
if (error)
|
|
130
|
+
return error;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (!stat.isFile())
|
|
134
|
+
continue;
|
|
135
|
+
if (stat.size > MAX_FILE_BYTES)
|
|
136
|
+
return `${full} is larger than ${MAX_FILE_BYTES / 1024 / 1024} MB.`;
|
|
137
|
+
total += stat.size;
|
|
138
|
+
if (files.length >= MAX_FILES)
|
|
139
|
+
return `The skill has more than ${MAX_FILES} files.`;
|
|
140
|
+
if (total > MAX_TOTAL_BYTES)
|
|
141
|
+
return `The skill is larger than ${MAX_TOTAL_BYTES / 1024 / 1024} MB.`;
|
|
142
|
+
files.push({
|
|
143
|
+
relPath: path.relative(dir, full).split(path.sep).join('/'),
|
|
144
|
+
content: readFileSync(full),
|
|
145
|
+
executable: (stat.mode & 0o111) !== 0,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
};
|
|
150
|
+
try {
|
|
151
|
+
const error = walk(dir, 0);
|
|
152
|
+
if (error)
|
|
153
|
+
return { ok: false, error };
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
return { ok: false, error: `Could not read ${dir}: ${error.message}` };
|
|
157
|
+
}
|
|
158
|
+
const main = files.find((file) => file.relPath === SKILL_FILE_NAME);
|
|
159
|
+
if (!main || main.content.length > SKILL_FILE_MAX_BYTES) {
|
|
160
|
+
return { ok: false, error: `${SKILL_FILE_NAME} is missing or larger than ${SKILL_FILE_MAX_BYTES / 1024} KB.` };
|
|
161
|
+
}
|
|
162
|
+
const valid = validateSkillText(main.content.toString('utf8'), path.basename(dir));
|
|
163
|
+
if (!valid.ok)
|
|
164
|
+
return { ok: false, error: valid.error };
|
|
165
|
+
return { ok: true, skill: { name: valid.name, description: valid.description, origin: dir, files } };
|
|
166
|
+
}
|
|
167
|
+
function findNestedSkillDirs(dir) {
|
|
168
|
+
const found = [];
|
|
169
|
+
const walk = (current, depth) => {
|
|
170
|
+
if (depth > 3 || found.length >= 50)
|
|
171
|
+
return;
|
|
172
|
+
let entries;
|
|
173
|
+
try {
|
|
174
|
+
entries = readdirSync(current, { withFileTypes: true });
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
for (const entry of entries) {
|
|
180
|
+
if (!entry.isDirectory() || SKIPPED_DIRS.has(entry.name) || entry.name.startsWith('.'))
|
|
181
|
+
continue;
|
|
182
|
+
const full = path.join(current, entry.name);
|
|
183
|
+
if (existsSync(path.join(full, SKILL_FILE_NAME)))
|
|
184
|
+
found.push(full);
|
|
185
|
+
else
|
|
186
|
+
walk(full, depth + 1);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
walk(dir, 0);
|
|
190
|
+
return found.sort();
|
|
191
|
+
}
|
|
192
|
+
function githubHeaders(env) {
|
|
193
|
+
const headers = {
|
|
194
|
+
Accept: 'application/vnd.github+json',
|
|
195
|
+
'User-Agent': 'thegitai-cli',
|
|
196
|
+
};
|
|
197
|
+
const token = String(env.GITHUB_TOKEN ?? env.GH_TOKEN ?? '').trim();
|
|
198
|
+
if (token)
|
|
199
|
+
headers.Authorization = `Bearer ${token}`;
|
|
200
|
+
return headers;
|
|
201
|
+
}
|
|
202
|
+
async function fetchJson(fetchImpl, url, env, signal) {
|
|
203
|
+
const response = await fetchImpl(url, { headers: githubHeaders(env), signal });
|
|
204
|
+
if (response.status === 404)
|
|
205
|
+
throw new Error('Repository or path not found on GitHub (private repositories need GITHUB_TOKEN).');
|
|
206
|
+
if (response.status === 403 || response.status === 429) {
|
|
207
|
+
throw new Error('GitHub refused the request (rate limit). Try again later, or set GITHUB_TOKEN.');
|
|
208
|
+
}
|
|
209
|
+
if (!response.ok)
|
|
210
|
+
throw new Error(`GitHub answered ${response.status}.`);
|
|
211
|
+
return response.json();
|
|
212
|
+
}
|
|
213
|
+
function encodePath(value) {
|
|
214
|
+
return value.split('/').map(encodeURIComponent).join('/');
|
|
215
|
+
}
|
|
216
|
+
function treeEntries(tree) {
|
|
217
|
+
return Array.isArray(tree?.tree) ? tree.tree : [];
|
|
218
|
+
}
|
|
219
|
+
async function fetchGithubTree(source, repoUrl, ref, env, fetchImpl, signal) {
|
|
220
|
+
const tree = await fetchJson(fetchImpl, `${repoUrl}/git/trees/${encodeURIComponent(ref)}?recursive=1`, env, signal);
|
|
221
|
+
if (tree?.truncated !== true)
|
|
222
|
+
return { ok: true, entries: treeEntries(tree) };
|
|
223
|
+
const subpath = source.subpath;
|
|
224
|
+
if (!subpath) {
|
|
225
|
+
return {
|
|
226
|
+
ok: false,
|
|
227
|
+
error: `${source.owner}/${source.repo} is too large to search for skills. Name the skill folder: /skills add ${source.owner}/${source.repo}/path/to/skill`,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const scoped = await fetchJson(fetchImpl, `${repoUrl}/git/trees/${encodeURIComponent(`${ref}:${subpath}`)}?recursive=1`, env, signal);
|
|
231
|
+
if (scoped?.truncated === true) {
|
|
232
|
+
return {
|
|
233
|
+
ok: false,
|
|
234
|
+
error: `${source.owner}/${source.repo}/${subpath} is too large to search for skills. Name the skill folder itself.`,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
ok: true,
|
|
239
|
+
entries: treeEntries(scoped).map((entry) => ({ ...entry, path: `${subpath}/${entry.path}` })),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
async function prepareGithub(source, env, fetchImpl, signal) {
|
|
243
|
+
const repoUrl = `https://api.github.com/repos/${source.owner}/${source.repo}`;
|
|
244
|
+
const ref = source.ref ?? String((await fetchJson(fetchImpl, repoUrl, env, signal))?.default_branch ?? '');
|
|
245
|
+
if (!ref)
|
|
246
|
+
return { ok: false, error: 'Could not find the repository default branch.' };
|
|
247
|
+
const tree = await fetchGithubTree(source, repoUrl, ref, env, fetchImpl, signal);
|
|
248
|
+
if (!tree.ok)
|
|
249
|
+
return tree;
|
|
250
|
+
const blobs = tree.entries.filter((entry) => entry.type === 'blob' && typeof entry.path === 'string');
|
|
251
|
+
const subpath = source.subpath;
|
|
252
|
+
const within = (entryPath) => !subpath || entryPath === subpath || entryPath.startsWith(`${subpath}/`);
|
|
253
|
+
const skillDirs = blobs
|
|
254
|
+
.filter((entry) => path.posix.basename(entry.path) === SKILL_FILE_NAME && within(entry.path))
|
|
255
|
+
.map((entry) => path.posix.dirname(entry.path))
|
|
256
|
+
.map((dir) => (dir === '.' ? '' : dir))
|
|
257
|
+
.sort();
|
|
258
|
+
if (!skillDirs.length) {
|
|
259
|
+
return { ok: false, error: `No ${SKILL_FILE_NAME} found in ${source.owner}/${source.repo}${subpath ? `/${subpath}` : ''}.` };
|
|
260
|
+
}
|
|
261
|
+
const chosen = skillDirs.includes(subpath) ? subpath : skillDirs.length === 1 ? skillDirs[0] : null;
|
|
262
|
+
if (chosen === null) {
|
|
263
|
+
const listed = skillDirs.slice(0, 20).map((dir) => ` /skills add ${source.owner}/${source.repo}/${dir}${source.ref ? `@${source.ref}` : ''}`);
|
|
264
|
+
const more = skillDirs.length > 20 ? `\n ...and ${skillDirs.length - 20} more` : '';
|
|
265
|
+
return { ok: false, error: `Found ${skillDirs.length} skills there. Add one of them:\n${listed.join('\n')}${more}` };
|
|
266
|
+
}
|
|
267
|
+
const prefix = chosen ? `${chosen}/` : '';
|
|
268
|
+
const selected = blobs.filter((entry) => !prefix || entry.path.startsWith(prefix));
|
|
269
|
+
const nestedDirs = skillDirs.filter((dir) => dir !== chosen && (!chosen || dir.startsWith(prefix)));
|
|
270
|
+
const files = selected.filter((entry) => !nestedDirs.some((dir) => entry.path.startsWith(`${dir}/`)));
|
|
271
|
+
const download = async (repoPath) => {
|
|
272
|
+
const rawUrl = `https://raw.githubusercontent.com/${source.owner}/${source.repo}/${encodePath(ref)}/${encodePath(repoPath)}`;
|
|
273
|
+
const { Accept: _accept, ...rawHeaders } = githubHeaders(env);
|
|
274
|
+
const response = await fetchImpl(rawUrl, { headers: rawHeaders, signal });
|
|
275
|
+
if (!response.ok)
|
|
276
|
+
return `Could not download ${repoPath} (${response.status}).`;
|
|
277
|
+
const content = Buffer.from(await response.arrayBuffer());
|
|
278
|
+
if (content.length > MAX_FILE_BYTES)
|
|
279
|
+
return `${repoPath} is larger than ${MAX_FILE_BYTES / 1024 / 1024} MB.`;
|
|
280
|
+
return content;
|
|
281
|
+
};
|
|
282
|
+
const blobsByPath = new Map(blobs.map((blob) => [blob.path, blob]));
|
|
283
|
+
const insideSkill = (repoPath) => chosen ? repoPath === chosen || repoPath.startsWith(prefix) : repoPath !== '..' && !repoPath.startsWith('../');
|
|
284
|
+
const plan = [];
|
|
285
|
+
const relPaths = [];
|
|
286
|
+
const addEntry = async (entry, relPath, chain) => {
|
|
287
|
+
if (entry.mode !== '120000') {
|
|
288
|
+
plan.push(entry);
|
|
289
|
+
relPaths.push(relPath);
|
|
290
|
+
return plan.length > MAX_FILES ? `The skill has more than ${MAX_FILES} files.` : null;
|
|
291
|
+
}
|
|
292
|
+
if (chain.has(entry.path))
|
|
293
|
+
return linkMissingMessage(relPath);
|
|
294
|
+
const link = await download(entry.path);
|
|
295
|
+
if (typeof link === 'string')
|
|
296
|
+
return link;
|
|
297
|
+
const linkTarget = link.toString('utf8').trim();
|
|
298
|
+
if (path.posix.isAbsolute(linkTarget))
|
|
299
|
+
return linkOutsideMessage(relPath);
|
|
300
|
+
const resolved = path.posix.normalize(path.posix.join(path.posix.dirname(entry.path), linkTarget));
|
|
301
|
+
if (!insideSkill(resolved))
|
|
302
|
+
return linkOutsideMessage(relPath);
|
|
303
|
+
const nextChain = new Set(chain).add(entry.path);
|
|
304
|
+
const exact = blobsByPath.get(resolved);
|
|
305
|
+
if (exact)
|
|
306
|
+
return addEntry(exact, relPath, nextChain);
|
|
307
|
+
const under = blobs.filter((blob) => blob.path.startsWith(`${resolved}/`));
|
|
308
|
+
if (!under.length)
|
|
309
|
+
return linkMissingMessage(relPath);
|
|
310
|
+
for (const blob of under) {
|
|
311
|
+
const error = await addEntry(blob, `${relPath}${blob.path.slice(resolved.length)}`, nextChain);
|
|
312
|
+
if (error)
|
|
313
|
+
return error;
|
|
314
|
+
}
|
|
315
|
+
return null;
|
|
316
|
+
};
|
|
317
|
+
for (const entry of files) {
|
|
318
|
+
const error = await addEntry(entry, prefix ? entry.path.slice(prefix.length) : entry.path, new Set());
|
|
319
|
+
if (error)
|
|
320
|
+
return { ok: false, error };
|
|
321
|
+
}
|
|
322
|
+
const declaredTotal = plan.reduce((sum, entry) => sum + (Number(entry.size) || 0), 0);
|
|
323
|
+
if (declaredTotal > MAX_TOTAL_BYTES)
|
|
324
|
+
return { ok: false, error: `The skill is larger than ${MAX_TOTAL_BYTES / 1024 / 1024} MB.` };
|
|
325
|
+
const prepared = [];
|
|
326
|
+
for (const [index, entry] of plan.entries()) {
|
|
327
|
+
const content = await download(entry.path);
|
|
328
|
+
if (typeof content === 'string')
|
|
329
|
+
return { ok: false, error: content };
|
|
330
|
+
prepared.push({ relPath: relPaths[index], content, executable: entry.mode === '100755' });
|
|
331
|
+
}
|
|
332
|
+
const main = prepared.find((file) => file.relPath === SKILL_FILE_NAME);
|
|
333
|
+
if (!main || main.content.length > SKILL_FILE_MAX_BYTES) {
|
|
334
|
+
return { ok: false, error: `${SKILL_FILE_NAME} is missing or larger than ${SKILL_FILE_MAX_BYTES / 1024} KB.` };
|
|
335
|
+
}
|
|
336
|
+
const fallback = chosen ? path.posix.basename(chosen) : source.repo.toLowerCase();
|
|
337
|
+
const valid = validateSkillText(main.content.toString('utf8'), fallback);
|
|
338
|
+
if (!valid.ok)
|
|
339
|
+
return { ok: false, error: valid.error };
|
|
340
|
+
return {
|
|
341
|
+
ok: true,
|
|
342
|
+
skill: {
|
|
343
|
+
name: valid.name,
|
|
344
|
+
description: valid.description,
|
|
345
|
+
origin: `github.com/${source.owner}/${source.repo}${chosen ? `/${chosen}` : ''}@${ref}`,
|
|
346
|
+
files: prepared,
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
export async function prepareSkillInstall(source, env, fetchImpl = globalThis.fetch, signal) {
|
|
351
|
+
if (source.kind === 'local')
|
|
352
|
+
return prepareLocal(source.path);
|
|
353
|
+
try {
|
|
354
|
+
return await prepareGithub(source, env, fetchImpl, signal);
|
|
355
|
+
}
|
|
356
|
+
catch (error) {
|
|
357
|
+
return { ok: false, error: error.message };
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
export function preparedSkillHasScripts(skill) {
|
|
361
|
+
return skill.files.some((file) => file.executable || file.relPath.startsWith('scripts/') || /\.(?:sh|bash|zsh|ps1|py|js|mjs|cjs|ts|rb|pl)$/i.test(file.relPath));
|
|
362
|
+
}
|
|
363
|
+
function safeRelative(relPath) {
|
|
364
|
+
const normalized = path.posix.normalize(relPath);
|
|
365
|
+
if (!normalized || normalized.startsWith('../') || normalized === '..' || path.posix.isAbsolute(normalized))
|
|
366
|
+
return null;
|
|
367
|
+
return normalized;
|
|
368
|
+
}
|
|
369
|
+
export function installPreparedSkill(skill, targetRoot) {
|
|
370
|
+
const dest = path.join(targetRoot, skill.name);
|
|
371
|
+
if (existsSync(dest)) {
|
|
372
|
+
return { ok: false, error: `A skill named "${skill.name}" already exists at ${dest}. Remove that folder first to replace it.` };
|
|
373
|
+
}
|
|
374
|
+
const staging = path.join(targetRoot, `.${skill.name}.installing-${randomUUID().slice(0, 8)}`);
|
|
375
|
+
try {
|
|
376
|
+
mkdirSync(staging, { recursive: true });
|
|
377
|
+
for (const file of skill.files) {
|
|
378
|
+
const relPath = safeRelative(file.relPath);
|
|
379
|
+
if (!relPath)
|
|
380
|
+
throw new Error(`Refusing to write ${file.relPath} outside the skill folder.`);
|
|
381
|
+
const target = path.join(staging, ...relPath.split('/'));
|
|
382
|
+
mkdirSync(path.dirname(target), { recursive: true });
|
|
383
|
+
writeFileSync(target, file.content);
|
|
384
|
+
if (file.executable && process.platform !== 'win32')
|
|
385
|
+
chmodSync(target, 0o755);
|
|
386
|
+
}
|
|
387
|
+
renameSync(staging, dest);
|
|
388
|
+
return { ok: true, dir: dest };
|
|
389
|
+
}
|
|
390
|
+
catch (error) {
|
|
391
|
+
rmSync(staging, { recursive: true, force: true });
|
|
392
|
+
return { ok: false, error: error.message };
|
|
393
|
+
}
|
|
394
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const USE_SKILL_TOOL_NAME = 'use_skill';
|
|
2
|
+
export const SKILL_FILE_NAME = 'SKILL.md';
|
|
3
|
+
export const SKILL_NAME_MAX_CHARS = 64;
|
|
4
|
+
export const SKILL_DESCRIPTION_MAX_CHARS = 1024;
|
|
5
|
+
export const SKILL_ARGUMENT_HINT_MAX_CHARS = 120;
|
|
6
|
+
export const SKILL_MAX_ENTRIES = 100;
|
|
7
|
+
export const SKILL_FILE_MAX_BYTES = 256 * 1024;
|
|
8
|
+
export const SKILL_CONTENT_MAX_CHARS = 60_000;
|
|
9
|
+
export const SKILL_ARGUMENTS_MAX_CHARS = 8_000;
|
|
10
|
+
export const SKILL_DIR_MAX_CHARS = 1024;
|
|
11
|
+
export const SKILL_FILE_LIST_LIMIT = 100;
|
|
12
|
+
export const BUNDLED_SKILLS = [
|
|
13
|
+
{
|
|
14
|
+
name: 'skill-creator',
|
|
15
|
+
description: 'Create a new skill or improve an existing one: a folder with a SKILL.md that teaches TheGitAI a repeatable workflow. Use when the user asks to make, write, or edit a skill.',
|
|
16
|
+
scope: 'bundled',
|
|
17
|
+
argumentHint: '[what the skill should do]',
|
|
18
|
+
},
|
|
19
|
+
];
|
|
20
|
+
export function isValidSkillName(value) {
|
|
21
|
+
return (typeof value === 'string' &&
|
|
22
|
+
value.length > 0 &&
|
|
23
|
+
value.length <= SKILL_NAME_MAX_CHARS &&
|
|
24
|
+
/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value));
|
|
25
|
+
}
|
|
26
|
+
export function cleanSkillText(value, maxChars) {
|
|
27
|
+
if (typeof value !== 'string')
|
|
28
|
+
return '';
|
|
29
|
+
return value
|
|
30
|
+
.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, '')
|
|
31
|
+
.replace(/\s+/g, ' ')
|
|
32
|
+
.trim()
|
|
33
|
+
.slice(0, maxChars);
|
|
34
|
+
}
|
|
35
|
+
export function parseSkillInvocation(input) {
|
|
36
|
+
const match = String(input ?? '').trimStart().match(/^\/([a-z0-9]+(?:-[a-z0-9]+)*)(?:\s+([\s\S]*))?$/);
|
|
37
|
+
if (!match || !isValidSkillName(match[1]))
|
|
38
|
+
return null;
|
|
39
|
+
return {
|
|
40
|
+
name: match[1],
|
|
41
|
+
arguments: String(match[2] ?? '').trim().slice(0, SKILL_ARGUMENTS_MAX_CHARS),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { discoverSkills } from './skills/discovery.js';
|
|
4
|
+
import { BUNDLED_SKILLS } from './skills/types.js';
|
|
5
|
+
export const SKILLS_ADD_USAGE = '/skills add <owner/repo/path, GitHub URL, or folder> [--project]';
|
|
6
|
+
function displayPath(target, rootDir) {
|
|
7
|
+
const relative = path.relative(rootDir, target);
|
|
8
|
+
if (relative && !relative.startsWith('..') && !path.isAbsolute(relative))
|
|
9
|
+
return relative.split(path.sep).join('/');
|
|
10
|
+
const home = os.homedir();
|
|
11
|
+
if (target === home)
|
|
12
|
+
return '~';
|
|
13
|
+
if (target.startsWith(`${home}${path.sep}`))
|
|
14
|
+
return `~/${target.slice(home.length + 1).split(path.sep).join('/')}`;
|
|
15
|
+
return target;
|
|
16
|
+
}
|
|
17
|
+
export const SKILLS_PICKER_GROUP_TITLES = {
|
|
18
|
+
personal: 'Personal',
|
|
19
|
+
project: 'Project',
|
|
20
|
+
bundled: 'Built in',
|
|
21
|
+
'not-loaded': 'Not loaded',
|
|
22
|
+
};
|
|
23
|
+
export const SKILLS_PICKER_ADD_HINT = '/skills add <source> installs one from GitHub or a folder';
|
|
24
|
+
export function buildSkillsPickerItems(rootDir, env, builtinCommands, discovery = discoverSkills(rootDir, env)) {
|
|
25
|
+
const builtins = new Set(builtinCommands);
|
|
26
|
+
const local = (skill) => {
|
|
27
|
+
const clashes = builtins.has(`/${skill.name}`);
|
|
28
|
+
const notes = [];
|
|
29
|
+
if (skill.typedOnly)
|
|
30
|
+
notes.push('runs only when you type it');
|
|
31
|
+
if (clashes)
|
|
32
|
+
notes.push(`/${skill.name} opens the built-in command; the agent can still use it`);
|
|
33
|
+
return {
|
|
34
|
+
name: skill.name,
|
|
35
|
+
group: skill.scope,
|
|
36
|
+
description: skill.description,
|
|
37
|
+
path: displayPath(skill.dir, rootDir),
|
|
38
|
+
notes,
|
|
39
|
+
usable: !clashes,
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const localNames = new Set(discovery.skills.map((skill) => skill.name));
|
|
43
|
+
return [
|
|
44
|
+
...discovery.skills.filter((skill) => skill.scope === 'personal').map(local),
|
|
45
|
+
...discovery.skills.filter((skill) => skill.scope === 'project').map(local),
|
|
46
|
+
...BUNDLED_SKILLS.filter((skill) => !localNames.has(skill.name)).map((skill) => ({
|
|
47
|
+
name: skill.name,
|
|
48
|
+
group: 'bundled',
|
|
49
|
+
description: skill.description,
|
|
50
|
+
path: null,
|
|
51
|
+
notes: [],
|
|
52
|
+
usable: true,
|
|
53
|
+
})),
|
|
54
|
+
...discovery.problems.map((problem) => ({
|
|
55
|
+
name: path.basename(problem.dir),
|
|
56
|
+
group: 'not-loaded',
|
|
57
|
+
description: problem.message,
|
|
58
|
+
path: displayPath(problem.dir, rootDir),
|
|
59
|
+
notes: [],
|
|
60
|
+
usable: false,
|
|
61
|
+
})),
|
|
62
|
+
...discovery.shadowed.map((skill) => ({
|
|
63
|
+
name: skill.name,
|
|
64
|
+
group: 'not-loaded',
|
|
65
|
+
description: 'Hidden by your personal skill with the same name.',
|
|
66
|
+
path: displayPath(skill.dir, rootDir),
|
|
67
|
+
notes: [],
|
|
68
|
+
usable: false,
|
|
69
|
+
})),
|
|
70
|
+
];
|
|
71
|
+
}
|