@koinzhang/paseo-plugin-customize 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/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +58 -0
- package/client/category-visibility.test.ts +26 -0
- package/client/design-tokens.test.ts +39 -0
- package/client/design-tokens.ts +104 -0
- package/client/dropdown.tsx +139 -0
- package/client/entries.test.ts +50 -0
- package/client/entries.ts +47 -0
- package/client/entry-row.tsx +89 -0
- package/client/mechanism-card.tsx +75 -0
- package/client/preview-pane.tsx +101 -0
- package/client/provider-options.test.ts +31 -0
- package/client/provider-options.ts +37 -0
- package/client/query-policy.test.ts +57 -0
- package/client/query-policy.ts +20 -0
- package/client/route.ts +29 -0
- package/client/selection-settings.test.ts +16 -0
- package/client/slash-command.test.ts +94 -0
- package/client/slash-command.ts +36 -0
- package/client/surface.tsx +332 -0
- package/client/ui.tsx +265 -0
- package/client/use-app-language.ts +28 -0
- package/client/web.ts +111 -0
- package/index.client.tsx +57 -0
- package/index.server.ts +13 -0
- package/package.json +37 -0
- package/paseo-plugin.json +6 -0
- package/server/agent-plugins.ts +41 -0
- package/server/compatibility.test.ts +104 -0
- package/server/compatibility.ts +46 -0
- package/server/frontmatter.ts +49 -0
- package/server/glob.ts +46 -0
- package/server/handlers.ts +193 -0
- package/server/jsonc.ts +40 -0
- package/server/mcp.ts +87 -0
- package/server/parsers.test.ts +92 -0
- package/server/providers/acp.ts +186 -0
- package/server/providers/claude.ts +284 -0
- package/server/providers/codex.ts +219 -0
- package/server/providers/copilot.ts +116 -0
- package/server/providers/cursor.ts +194 -0
- package/server/providers/omp.ts +263 -0
- package/server/providers/opencode.ts +215 -0
- package/server/providers/pi.ts +102 -0
- package/server/providers.test.ts +306 -0
- package/server/scan-kit.ts +368 -0
- package/server/scan-snapshot.test.ts +56 -0
- package/server/scan-snapshot.ts +58 -0
- package/server/skill-aliases.ts +60 -0
- package/server/toml.ts +240 -0
- package/server/yaml-lite.ts +172 -0
- package/shared/acp-configs.ts +107 -0
- package/shared/category-visibility.ts +26 -0
- package/shared/compatibility.ts +6 -0
- package/shared/contracts.ts +116 -0
- package/shared/i18n.ts +293 -0
- package/shared/mechanisms.ts +380 -0
- package/shared/providers.ts +27 -0
- package/shared/selection-settings.ts +14 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import { closeSync, lstatSync, openSync, readdirSync, readSync, statSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { Category, Entry, ReasonCode, Scope, Status, Tag } from "../shared/contracts.ts";
|
|
4
|
+
import { firstLine, fmString, parseFrontmatter, type Frontmatter } from "./frontmatter.ts";
|
|
5
|
+
|
|
6
|
+
export interface ScanEnv {
|
|
7
|
+
home: string;
|
|
8
|
+
env: Record<string, string | undefined>;
|
|
9
|
+
platform: NodeJS.Platform;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ScanContext extends ScanEnv {
|
|
13
|
+
projectRoot: string | null;
|
|
14
|
+
nested: NestedIndex | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Filesystem
|
|
19
|
+
|
|
20
|
+
export function isFile(p: string): boolean {
|
|
21
|
+
try {
|
|
22
|
+
return statSync(p).isFile();
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function isDir(p: string): boolean {
|
|
29
|
+
try {
|
|
30
|
+
return statSync(p).isDirectory();
|
|
31
|
+
} catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isSymlink(p: string): boolean {
|
|
37
|
+
try {
|
|
38
|
+
return lstatSync(p).isSymbolicLink();
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function fileSize(p: string): number {
|
|
45
|
+
try {
|
|
46
|
+
return statSync(p).size;
|
|
47
|
+
} catch {
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Reads at most `maxBytes` (UTF-8, may cut a trailing code point). */
|
|
53
|
+
export function readText(p: string, maxBytes = 1024 * 1024): string | null {
|
|
54
|
+
let fd: number | null = null;
|
|
55
|
+
try {
|
|
56
|
+
fd = openSync(p, "r");
|
|
57
|
+
const size = Math.min(statSync(p).size, maxBytes);
|
|
58
|
+
const buf = Buffer.alloc(size);
|
|
59
|
+
const read = readSync(fd, buf, 0, size, 0);
|
|
60
|
+
return buf.subarray(0, read).toString("utf8");
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
} finally {
|
|
64
|
+
if (fd !== null) closeSync(fd);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function readJson(p: string): unknown {
|
|
69
|
+
const text = readText(p);
|
|
70
|
+
if (text == null) return undefined;
|
|
71
|
+
try {
|
|
72
|
+
return JSON.parse(text);
|
|
73
|
+
} catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function listNames(dir: string): string[] {
|
|
79
|
+
try {
|
|
80
|
+
return readdirSync(dir).sort((a, b) => a.localeCompare(b));
|
|
81
|
+
} catch {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
87
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** `dir`, its parent, … up to and including the filesystem root. */
|
|
91
|
+
export function ancestors(dir: string): string[] {
|
|
92
|
+
const out: string[] = [];
|
|
93
|
+
let current = path.resolve(dir);
|
|
94
|
+
for (;;) {
|
|
95
|
+
out.push(current);
|
|
96
|
+
const parent = path.dirname(current);
|
|
97
|
+
if (parent === current) return out;
|
|
98
|
+
current = parent;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Parents of `dir`, excluding itself, up to the filesystem root. */
|
|
103
|
+
export function parentDirs(dir: string): string[] {
|
|
104
|
+
return ancestors(dir).slice(1);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// Nested project index
|
|
109
|
+
|
|
110
|
+
const SKIP_DIRS = new Set([
|
|
111
|
+
"node_modules",
|
|
112
|
+
"dist",
|
|
113
|
+
"build",
|
|
114
|
+
"out",
|
|
115
|
+
"target",
|
|
116
|
+
"vendor",
|
|
117
|
+
"Pods",
|
|
118
|
+
"venv",
|
|
119
|
+
"__pycache__",
|
|
120
|
+
"coverage",
|
|
121
|
+
"DerivedData",
|
|
122
|
+
"bower_components",
|
|
123
|
+
]);
|
|
124
|
+
|
|
125
|
+
export interface NestedIndex {
|
|
126
|
+
root: string;
|
|
127
|
+
/** Directory (absolute, excluding the root) → entry names. */
|
|
128
|
+
dirs: Map<string, Set<string>>;
|
|
129
|
+
truncated: boolean;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const NESTED_MAX_DEPTH = 6;
|
|
133
|
+
const NESTED_MAX_DIRS = 4000;
|
|
134
|
+
|
|
135
|
+
/** Breadth-first directory index below `root`; hidden folders are recorded but not entered. */
|
|
136
|
+
export function buildNestedIndex(root: string): NestedIndex {
|
|
137
|
+
const dirs = new Map<string, Set<string>>();
|
|
138
|
+
let truncated = false;
|
|
139
|
+
let frontier = [root];
|
|
140
|
+
for (let depth = 0; depth <= NESTED_MAX_DEPTH && frontier.length; depth++) {
|
|
141
|
+
const next: string[] = [];
|
|
142
|
+
for (const dir of frontier) {
|
|
143
|
+
let entries;
|
|
144
|
+
try {
|
|
145
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
146
|
+
} catch {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (dir !== root) dirs.set(dir, new Set(entries.map((entry) => entry.name)));
|
|
150
|
+
for (const entry of entries) {
|
|
151
|
+
if (!entry.isDirectory() || entry.name.startsWith(".") || SKIP_DIRS.has(entry.name)) continue;
|
|
152
|
+
if (dirs.size + next.length >= NESTED_MAX_DIRS) {
|
|
153
|
+
truncated = true;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
next.push(path.join(dir, entry.name));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
frontier = next;
|
|
160
|
+
}
|
|
161
|
+
return { root, dirs, truncated };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Nested directories (below the project root) that contain `name`. */
|
|
165
|
+
export function nestedWith(ctx: ScanContext, name: string): string[] {
|
|
166
|
+
if (!ctx.nested) return [];
|
|
167
|
+
const out: string[] = [];
|
|
168
|
+
for (const [dir, names] of ctx.nested.dirs) if (names.has(name)) out.push(dir);
|
|
169
|
+
return out.sort((a, b) => a.localeCompare(b));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
// Display helpers
|
|
174
|
+
|
|
175
|
+
export function displayPath(ctx: ScanEnv & { projectRoot: string | null }, p: string): string {
|
|
176
|
+
const abs = path.resolve(p);
|
|
177
|
+
if (ctx.projectRoot) {
|
|
178
|
+
const rel = path.relative(ctx.projectRoot, abs);
|
|
179
|
+
if (!rel) return "./";
|
|
180
|
+
if (!rel.startsWith("..") && !path.isAbsolute(rel)) return rel;
|
|
181
|
+
}
|
|
182
|
+
const relHome = path.relative(ctx.home, abs);
|
|
183
|
+
if (!relHome) return "~";
|
|
184
|
+
if (!relHome.startsWith("..") && !path.isAbsolute(relHome)) return `~/${relHome}`;
|
|
185
|
+
return abs;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// Entries
|
|
190
|
+
|
|
191
|
+
export interface EntryInput {
|
|
192
|
+
category: Category;
|
|
193
|
+
scope: Scope;
|
|
194
|
+
name: string;
|
|
195
|
+
path: string;
|
|
196
|
+
/** Root the entry was discovered under (display source). Defaults to its directory. */
|
|
197
|
+
root?: string;
|
|
198
|
+
/** Overrides the display source (e.g. `~/.claude.json → mcpServers`). */
|
|
199
|
+
source?: string;
|
|
200
|
+
description?: string;
|
|
201
|
+
status: Status;
|
|
202
|
+
reason?: { code: ReasonCode; value?: string };
|
|
203
|
+
tags?: Tag[];
|
|
204
|
+
agentPlugin?: Entry["agentPlugin"];
|
|
205
|
+
mcp?: Entry["mcp"];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export class EntrySink {
|
|
209
|
+
readonly entries: Entry[] = [];
|
|
210
|
+
private readonly seen = new Set<string>();
|
|
211
|
+
private readonly ctx: ScanContext;
|
|
212
|
+
|
|
213
|
+
constructor(ctx: ScanContext) {
|
|
214
|
+
this.ctx = ctx;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
add(input: EntryInput): Entry | null {
|
|
218
|
+
const id = `${input.category}|${input.scope}|${input.path}|${input.name}`;
|
|
219
|
+
if (this.seen.has(id)) return null;
|
|
220
|
+
this.seen.add(id);
|
|
221
|
+
const dirPath = input.mcp ? input.path : path.dirname(input.path);
|
|
222
|
+
const entry: Entry = {
|
|
223
|
+
id,
|
|
224
|
+
category: input.category,
|
|
225
|
+
scope: input.scope,
|
|
226
|
+
name: input.name,
|
|
227
|
+
...(input.description ? { description: input.description } : {}),
|
|
228
|
+
path: input.path,
|
|
229
|
+
dir: displayPath(this.ctx, dirPath),
|
|
230
|
+
source: input.source ?? displayPath(this.ctx, input.root ?? path.dirname(input.path)),
|
|
231
|
+
status: input.status,
|
|
232
|
+
...(input.reason ? { reason: input.reason } : {}),
|
|
233
|
+
tags: [...new Set(input.tags ?? [])],
|
|
234
|
+
...(input.agentPlugin ? { agentPlugin: input.agentPlugin } : {}),
|
|
235
|
+
...(input.mcp ? { mcp: input.mcp } : {}),
|
|
236
|
+
};
|
|
237
|
+
this.entries.push(entry);
|
|
238
|
+
return entry;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Project scope for files under the project root, user scope otherwise. */
|
|
243
|
+
export function scopeOf(ctx: ScanContext, p: string): Scope {
|
|
244
|
+
if (!ctx.projectRoot) return "user";
|
|
245
|
+
const rel = path.relative(ctx.projectRoot, p);
|
|
246
|
+
return !rel.startsWith("..") && !path.isAbsolute(rel) ? "project" : "user";
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
// Markdown files with frontmatter
|
|
251
|
+
|
|
252
|
+
export interface MarkdownDoc {
|
|
253
|
+
path: string;
|
|
254
|
+
data: Frontmatter;
|
|
255
|
+
body: string;
|
|
256
|
+
description?: string;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function readMarkdown(p: string): MarkdownDoc | null {
|
|
260
|
+
const text = readText(p, 64 * 1024);
|
|
261
|
+
if (text == null) return null;
|
|
262
|
+
const { data, body } = parseFrontmatter(text);
|
|
263
|
+
const description = fmString(data, "description") ?? firstLine(body);
|
|
264
|
+
return { path: p, data, body, ...(description ? { description } : {}) };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Files under `dir` with one of `exts`; `recursive` descends into non-hidden subfolders. */
|
|
268
|
+
export function listFiles(dir: string, exts: readonly string[], recursive: boolean, depth = 0): string[] {
|
|
269
|
+
if (depth > 8) return [];
|
|
270
|
+
const out: string[] = [];
|
|
271
|
+
let entries;
|
|
272
|
+
try {
|
|
273
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
274
|
+
} catch {
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
278
|
+
for (const entry of entries) {
|
|
279
|
+
const full = path.join(dir, entry.name);
|
|
280
|
+
const directory = entry.isDirectory() || (entry.isSymbolicLink() && isDir(full));
|
|
281
|
+
if (directory) {
|
|
282
|
+
if (recursive && !entry.name.startsWith(".")) out.push(...listFiles(full, exts, true, depth + 1));
|
|
283
|
+
} else if (exts.some((ext) => entry.name.toLowerCase().endsWith(ext))) {
|
|
284
|
+
out.push(full);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return out;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ---------------------------------------------------------------------------
|
|
291
|
+
// Skills
|
|
292
|
+
|
|
293
|
+
export interface SkillFile {
|
|
294
|
+
/** `SKILL.md` or a loose `.md` skill. */
|
|
295
|
+
file: string;
|
|
296
|
+
/** Skill folder (or the loose file itself). */
|
|
297
|
+
dir: string;
|
|
298
|
+
doc: MarkdownDoc;
|
|
299
|
+
name: string;
|
|
300
|
+
symlink: boolean;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export interface SkillScanOptions {
|
|
304
|
+
/** `false` = only `<root>/<name>/SKILL.md`; `true` = any depth up to `maxDepth`. */
|
|
305
|
+
recursive: boolean;
|
|
306
|
+
maxDepth?: number;
|
|
307
|
+
/** Pi: loose `<root>/*.md` files are skills too. */
|
|
308
|
+
looseMarkdown?: boolean;
|
|
309
|
+
/** Directory names to skip at the first level (e.g. Claude `synced`, `.trash`). */
|
|
310
|
+
skip?: readonly string[];
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export function findSkills(root: string, options: SkillScanOptions): SkillFile[] {
|
|
314
|
+
const out: SkillFile[] = [];
|
|
315
|
+
const maxDepth = options.recursive ? options.maxDepth ?? 6 : 1;
|
|
316
|
+
const visit = (dir: string, depth: number) => {
|
|
317
|
+
let entries;
|
|
318
|
+
try {
|
|
319
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
320
|
+
} catch {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
324
|
+
for (const entry of entries) {
|
|
325
|
+
if (entry.name.startsWith(".")) continue;
|
|
326
|
+
if (depth === 1 && options.skip?.includes(entry.name)) continue;
|
|
327
|
+
const full = path.join(dir, entry.name);
|
|
328
|
+
const symlink = entry.isSymbolicLink();
|
|
329
|
+
const directory = entry.isDirectory() || (symlink && isDir(full));
|
|
330
|
+
if (directory) {
|
|
331
|
+
const skillFile = path.join(full, "SKILL.md");
|
|
332
|
+
if (isFile(skillFile)) {
|
|
333
|
+
const doc = readMarkdown(skillFile);
|
|
334
|
+
if (doc) out.push({ file: skillFile, dir: full, doc, name: fmString(doc.data, "name") ?? entry.name, symlink });
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (depth < maxDepth) visit(full, depth + 1);
|
|
338
|
+
} else if (depth === 1 && options.looseMarkdown && entry.name.toLowerCase().endsWith(".md")) {
|
|
339
|
+
const doc = readMarkdown(full);
|
|
340
|
+
if (doc) out.push({ file: full, dir: full, doc, name: fmString(doc.data, "name") ?? entry.name.replace(/\.md$/i, ""), symlink });
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
visit(root, 1);
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Standard frontmatter switches shared by Claude / Cursor / Copilot / Pi / OMP skills. */
|
|
349
|
+
export function skillInvocation(doc: MarkdownDoc): { status: Status; reason?: EntryInput["reason"]; tags: Tag[] } {
|
|
350
|
+
const tags: Tag[] = [];
|
|
351
|
+
if (doc.data["user-invocable"] === false) tags.push("userInvocableOff");
|
|
352
|
+
if (doc.data["disable-model-invocation"] === true) {
|
|
353
|
+
return { status: "manual", reason: { code: "frontmatter", value: "disable-model-invocation: true" }, tags };
|
|
354
|
+
}
|
|
355
|
+
return { status: "auto", tags };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function skillEntry(skill: SkillFile, root: string, scope: Scope): Omit<EntryInput, "status"> {
|
|
359
|
+
return {
|
|
360
|
+
category: "skills",
|
|
361
|
+
scope,
|
|
362
|
+
name: skill.name,
|
|
363
|
+
path: skill.file,
|
|
364
|
+
root,
|
|
365
|
+
...(skill.doc.description ? { description: skill.doc.description } : {}),
|
|
366
|
+
tags: skill.symlink ? ["symlink"] : [],
|
|
367
|
+
};
|
|
368
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir, tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { handleCachedScan, handlePreview, scanProvider } from "./handlers.ts";
|
|
7
|
+
import { readSnapshot, SCAN_REFRESH_MS, snapshotFile, writeSnapshot } from "./scan-snapshot.ts";
|
|
8
|
+
|
|
9
|
+
test("saved scans survive a new read, expire after ten minutes, and reject corrupt snapshots", () => {
|
|
10
|
+
const directory = mkdtempSync(path.join(tmpdir(), "customize-snapshot-"));
|
|
11
|
+
const scannedAt = "2026-01-01T00:00:00.000Z";
|
|
12
|
+
const result = { provider: "codex" as const, projectRoot: "/project", home: homedir(), entries: [], compatibility: null, scannedAt };
|
|
13
|
+
const time = Date.parse(scannedAt);
|
|
14
|
+
try {
|
|
15
|
+
writeSnapshot(result, directory);
|
|
16
|
+
assert.equal(statSync(directory).mode & 0o777, 0o700);
|
|
17
|
+
assert.deepEqual(readSnapshot("codex", "/project", { directory, now: time + SCAN_REFRESH_MS - 1 }), { snapshot: result, stale: false });
|
|
18
|
+
assert.deepEqual(readSnapshot("codex", "/project", { directory, now: time + SCAN_REFRESH_MS }), { snapshot: result, stale: true });
|
|
19
|
+
assert.equal(readSnapshot("codex", "/other", { directory }).snapshot, null);
|
|
20
|
+
const file = snapshotFile("codex", "/project", directory);
|
|
21
|
+
assert.equal(statSync(file).mode & 0o777, 0o600);
|
|
22
|
+
assert.equal(JSON.parse(readFileSync(file, "utf8")).version, 2);
|
|
23
|
+
writeFileSync(file, JSON.stringify({ version: 1, result }));
|
|
24
|
+
assert.equal(readSnapshot("codex", "/project", { directory }).snapshot, null);
|
|
25
|
+
writeFileSync(file, "broken json");
|
|
26
|
+
assert.equal(readSnapshot("codex", "/project", { directory }).snapshot, null);
|
|
27
|
+
} finally {
|
|
28
|
+
rmSync(directory, { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("a persisted path must appear in a fresh provider scan before preview", async () => {
|
|
33
|
+
const paseoHome = mkdtempSync(path.join(tmpdir(), "customize-home-"));
|
|
34
|
+
const project = mkdtempSync(path.join(tmpdir(), "customize-project-"));
|
|
35
|
+
const originalHome = process.env.PASEO_HOME;
|
|
36
|
+
try {
|
|
37
|
+
process.env.PASEO_HOME = paseoHome;
|
|
38
|
+
writeFileSync(path.join(project, "AGENTS.md"), "current instructions");
|
|
39
|
+
const entries = scanProvider("codex", project, { home: homedir(), env: process.env, platform: process.platform });
|
|
40
|
+
const valid = entries.find((entry) => entry.path === path.join(project, "AGENTS.md"));
|
|
41
|
+
assert.ok(valid);
|
|
42
|
+
const stale = { ...valid, id: "stale", path: path.join(project, "missing.md") };
|
|
43
|
+
mkdirSync(path.join(paseoHome, "plugin-data"), { recursive: true });
|
|
44
|
+
writeSnapshot({ provider: "codex", projectRoot: project, home: homedir(), entries: [valid, stale], compatibility: null, scannedAt: new Date().toISOString() });
|
|
45
|
+
const cached = handleCachedScan({ provider: "codex", projectRoot: project });
|
|
46
|
+
assert.equal(cached.snapshot?.entries.length, 2);
|
|
47
|
+
await assert.rejects(handlePreview({ path: stale.path }), /Path is not part of a Customize scan/);
|
|
48
|
+
const preview = await handlePreview({ path: valid.path });
|
|
49
|
+
assert.match(preview.content, /current instructions/);
|
|
50
|
+
} finally {
|
|
51
|
+
if (originalHome === undefined) delete process.env.PASEO_HOME;
|
|
52
|
+
else process.env.PASEO_HOME = originalHome;
|
|
53
|
+
rmSync(paseoHome, { recursive: true, force: true });
|
|
54
|
+
rmSync(project, { recursive: true, force: true });
|
|
55
|
+
}
|
|
56
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { chmodSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { scanRpc, type ScanResult } from "../shared/contracts.ts";
|
|
7
|
+
import type { ProviderId } from "../shared/providers.ts";
|
|
8
|
+
|
|
9
|
+
export const SCAN_REFRESH_MS = 10 * 60_000;
|
|
10
|
+
const SNAPSHOT_VERSION = 2;
|
|
11
|
+
const MAX_SNAPSHOT_BYTES = 10 * 1024 * 1024;
|
|
12
|
+
const SnapshotSchema = z.object({ version: z.literal(SNAPSHOT_VERSION), result: scanRpc.output });
|
|
13
|
+
|
|
14
|
+
function pluginDataDir(): string {
|
|
15
|
+
const configured = process.env.PASEO_HOME ?? path.join(homedir(), ".paseo");
|
|
16
|
+
const paseoHome = configured === "~" ? homedir() : configured.startsWith(`~${path.sep}`) ? path.join(homedir(), configured.slice(2)) : configured;
|
|
17
|
+
return path.join(path.resolve(paseoHome), "plugin-data", "customize");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function snapshotFile(provider: ProviderId, projectRoot: string | null, directory = pluginDataDir()): string {
|
|
21
|
+
const key = createHash("sha256").update(JSON.stringify([provider, projectRoot])).digest("hex");
|
|
22
|
+
return path.join(directory, `${key}.json`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function readSnapshot(
|
|
26
|
+
provider: ProviderId,
|
|
27
|
+
projectRoot: string | null,
|
|
28
|
+
options: { directory?: string; now?: number; home?: string } = {},
|
|
29
|
+
): { snapshot: ScanResult | null; stale: boolean } {
|
|
30
|
+
const file = snapshotFile(provider, projectRoot, options.directory);
|
|
31
|
+
try {
|
|
32
|
+
if (statSync(file).size > MAX_SNAPSHOT_BYTES) return { snapshot: null, stale: true };
|
|
33
|
+
const parsed = SnapshotSchema.parse(JSON.parse(readFileSync(file, "utf8")));
|
|
34
|
+
const result = parsed.result;
|
|
35
|
+
const home = options.home ?? homedir();
|
|
36
|
+
if (result.provider !== provider || result.projectRoot !== projectRoot || result.home !== home) return { snapshot: null, stale: true };
|
|
37
|
+
const age = (options.now ?? Date.now()) - Date.parse(result.scannedAt);
|
|
38
|
+
if (!Number.isFinite(age)) return { snapshot: null, stale: true };
|
|
39
|
+
return { snapshot: result, stale: age < 0 || age >= SCAN_REFRESH_MS };
|
|
40
|
+
} catch {
|
|
41
|
+
return { snapshot: null, stale: true };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function writeSnapshot(result: ScanResult, directory = pluginDataDir()): void {
|
|
46
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
47
|
+
chmodSync(directory, 0o700);
|
|
48
|
+
const file = snapshotFile(result.provider, result.projectRoot, directory);
|
|
49
|
+
const serialized = JSON.stringify({ version: SNAPSHOT_VERSION, result });
|
|
50
|
+
if (Buffer.byteLength(serialized) > MAX_SNAPSHOT_BYTES) throw new Error("Customize snapshot exceeds size limit");
|
|
51
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
52
|
+
try {
|
|
53
|
+
writeFileSync(temporary, serialized, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
54
|
+
renameSync(temporary, file);
|
|
55
|
+
} finally {
|
|
56
|
+
rmSync(temporary, { force: true });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type { Entry } from "../shared/contracts.ts";
|
|
4
|
+
import type { ProviderId } from "../shared/providers.ts";
|
|
5
|
+
|
|
6
|
+
const NATIVE_SKILL_ROOTS: Partial<Record<ProviderId, readonly string[]>> = {
|
|
7
|
+
claude: [".claude"], codex: [".agents", ".codex"], cursor: [".cursor"],
|
|
8
|
+
copilot: [".github", ".copilot"], opencode: [".opencode"], pi: [".pi"], omp: [".omp"],
|
|
9
|
+
cline: [".cline"], "codebuddy-code": [".codebuddy"], gemini: [".gemini"],
|
|
10
|
+
goose: [".goose"], grok: [".grok"], kilo: [".kilo"], kiro: [".kiro"],
|
|
11
|
+
kimi: [".kimi-code"], "qwen-code": [".qwen"], traecli: [".traecli", ".trae", ".trae-cn"],
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function directoryRank(provider: ProviderId, file: string): number {
|
|
15
|
+
const parts = path.resolve(file).split(path.sep);
|
|
16
|
+
const native = NATIVE_SKILL_ROOTS[provider] ?? [];
|
|
17
|
+
const isRoot = (part: string) => part === "skills" || part === "managed-skills" || part === "skills-cursor";
|
|
18
|
+
const skillRoot = parts.findLastIndex((part, index) => isRoot(part)
|
|
19
|
+
&& (native.includes(parts[index - 1] ?? "") || native.includes(parts[index - 2] ?? "")
|
|
20
|
+
|| parts[index - 1] === ".agents" || provider === "opencode" && parts[index - 1] === "opencode"));
|
|
21
|
+
const owner = skillRoot > 0 ? parts[skillRoot - 1] : "";
|
|
22
|
+
const grandparent = skillRoot > 1 ? parts[skillRoot - 2] : "";
|
|
23
|
+
if (native.includes(owner) || native.includes(grandparent)
|
|
24
|
+
|| provider === "opencode" && owner === "opencode") return 0;
|
|
25
|
+
if (owner === ".agents") return 1;
|
|
26
|
+
return 2;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const usable = (entry: Entry) => entry.status !== "disabled" && entry.status !== "inactive";
|
|
30
|
+
|
|
31
|
+
/** Collapse only aliases of the same physical skill file; keep same-name distinct files. */
|
|
32
|
+
export function mergeSkillAliases(provider: ProviderId, entries: readonly Entry[]): Entry[] {
|
|
33
|
+
const groups = new Map<string, Entry[]>();
|
|
34
|
+
for (const entry of entries) {
|
|
35
|
+
if (entry.category !== "skills") continue;
|
|
36
|
+
let physical: string;
|
|
37
|
+
try {
|
|
38
|
+
physical = realpathSync(entry.path);
|
|
39
|
+
} catch {
|
|
40
|
+
physical = path.resolve(entry.path);
|
|
41
|
+
}
|
|
42
|
+
const group = groups.get(physical) ?? [];
|
|
43
|
+
group.push(entry);
|
|
44
|
+
groups.set(physical, group);
|
|
45
|
+
}
|
|
46
|
+
const selected = new Map<string, Entry>();
|
|
47
|
+
for (const group of groups.values()) {
|
|
48
|
+
group.sort((a, b) => directoryRank(provider, a.path) - directoryRank(provider, b.path)
|
|
49
|
+
|| Number(usable(b)) - Number(usable(a))
|
|
50
|
+
|| Number(b.scope === "project") - Number(a.scope === "project")
|
|
51
|
+
|| a.path.localeCompare(b.path));
|
|
52
|
+
const display = group[0]!;
|
|
53
|
+
const active = group.find(usable);
|
|
54
|
+
selected.set(display.id, active && !usable(display)
|
|
55
|
+
? { ...display, status: active.status, reason: active.reason, tags: [...new Set([...display.tags, ...active.tags])] }
|
|
56
|
+
: display);
|
|
57
|
+
}
|
|
58
|
+
return entries.filter((entry) => entry.category !== "skills" || selected.has(entry.id))
|
|
59
|
+
.map((entry) => selected.get(entry.id) ?? entry);
|
|
60
|
+
}
|