ceulen 0.1.0 → 0.5.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +150 -0
  2. package/README.md +103 -14
  3. package/extensions/index.ts +113 -0
  4. package/extensions/lib/env.ts +75 -0
  5. package/extensions/lib/panel.ts +1720 -0
  6. package/extensions/lib/registry.ts +182 -0
  7. package/extensions/modules/composer/index.ts +128 -0
  8. package/extensions/modules/composer/lib/settings.ts +55 -0
  9. package/extensions/modules/composer/lib/shapes.ts +436 -0
  10. package/extensions/modules/composer/test/editor.test.ts +138 -0
  11. package/extensions/modules/composer/test/module.test.ts +218 -0
  12. package/extensions/modules/composer/test/screen.test.ts +195 -0
  13. package/extensions/modules/composer/test/shapes.test.ts +175 -0
  14. package/extensions/modules/config/index.ts +298 -0
  15. package/extensions/modules/config/piSettings.ts +467 -0
  16. package/extensions/modules/config/plugins.ts +290 -0
  17. package/extensions/modules/config/test/kernel.test.ts +523 -0
  18. package/extensions/modules/config/test/kernelV4.test.ts +196 -0
  19. package/extensions/modules/config/test/menu.test.ts +226 -0
  20. package/extensions/modules/config/test/panel.test.ts +395 -0
  21. package/extensions/modules/config/test/piSettings.test.ts +185 -0
  22. package/extensions/modules/config/test/plugins.test.ts +181 -0
  23. package/extensions/modules/config/test/preview-block.test.ts +88 -0
  24. package/extensions/modules/ponytail/UPSTREAM +26 -0
  25. package/extensions/modules/ponytail/index.ts +336 -0
  26. package/extensions/modules/ponytail/lib/config.ts +149 -0
  27. package/extensions/modules/ponytail/lib/instructions.ts +98 -0
  28. package/extensions/modules/ponytail/lib/subagent.ts +31 -0
  29. package/extensions/modules/ponytail/test/extension.test.ts +422 -0
  30. package/extensions/modules/ponytail/test/helpers.test.ts +163 -0
  31. package/extensions/modules/ponytail/test/subagent.test.ts +33 -0
  32. package/extensions/modules/router/commands/commands.ts +99 -0
  33. package/extensions/modules/router/configPanel.ts +89 -0
  34. package/extensions/modules/router/index.ts +75 -0
  35. package/extensions/modules/router/lib/client.ts +390 -0
  36. package/extensions/modules/router/lib/config.ts +142 -0
  37. package/extensions/modules/router/lib/migrate.ts +92 -0
  38. package/extensions/modules/router/lib/provider.ts +138 -0
  39. package/extensions/modules/router/lib/refresh.ts +19 -0
  40. package/extensions/modules/router/test/guard.test.ts +77 -0
  41. package/extensions/modules/router/test/unit.test.ts +1126 -0
  42. package/extensions/modules/usage/index.ts +1975 -0
  43. package/extensions/modules/usage/test/context.test.ts +387 -0
  44. package/extensions/modules/usage/test/env-gating.test.ts +113 -0
  45. package/extensions/modules/usage/test/opencode-go-usage.test.ts +139 -0
  46. package/extensions/modules/usage/test/parsers.test.ts +244 -0
  47. package/extensions/modules/usage/test/stale-ctx-regression.test.ts +294 -0
  48. package/extensions/modules/usage/test/zai-anthropic-adapter.test.ts +40 -0
  49. package/package.json +15 -3
  50. package/skills/ponytail/SKILL.md +124 -0
  51. package/skills/ponytail-audit/SKILL.md +41 -0
  52. package/skills/ponytail-debt/SKILL.md +44 -0
  53. package/skills/ponytail-gain/SKILL.md +51 -0
  54. package/skills/ponytail-help/SKILL.md +73 -0
  55. package/skills/ponytail-review/SKILL.md +57 -0
  56. package/extensions/index.js +0 -42
@@ -0,0 +1,290 @@
1
+ /**
2
+ * Pi-package management for the /config panel ("Plugins" group).
3
+ *
4
+ * Pi stores installed packages in the `packages` array of settings.json
5
+ * (global agent dir, plus the trusted project's .pi/settings.json). The load
6
+ * contract in Pi's package manager:
7
+ *
8
+ * - string entry → load everything the package declares
9
+ * - object entry, resource array [] → "Empty array explicitly disables all
10
+ * resources of this type" (package-manager.js applyPackageFilter)
11
+ * - object entry with patterns → per-resource narrowing (pi config)
12
+ * - object entry, autoload: false → project-scope FILTER DELTA over a
13
+ * personal entry, not an install
14
+ *
15
+ * So whole-package on/off is: string ⇄ object with all four resource arrays
16
+ * empty. Entries carrying patterns or autoload:false are read-only here —
17
+ * re-shaping them would silently drop a user's granular `pi config` setup.
18
+ *
19
+ * Writes go to the SAME file the entry lives in (project entries stay in the
20
+ * project file), atomic tmp+rename, corrupt file refuses to clobber — the
21
+ * same data-loss guard as writeDisabled.
22
+ */
23
+
24
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
25
+ import path from "node:path";
26
+ import { agentDirs, isProjectTrusted } from "../../lib/registry.js";
27
+ import { row, type PanelGroup } from "../../lib/panel.js";
28
+
29
+ /** Pi's resource types (package-manager.js RESOURCE_TYPES). */
30
+ const RESOURCE_KEYS = ["extensions", "skills", "prompts", "themes"] as const;
31
+
32
+ export type PackageScope = "global" | "project";
33
+ /** on = string entry; off = all-empty-array object; filtered = patterns or
34
+ * autoload:false — managed by `pi config`, not toggleable here. */
35
+ export type PackageState = "on" | "off" | "filtered";
36
+
37
+ export interface PackageEntry {
38
+ scope: PackageScope;
39
+ /** Index into that file's `packages` array (write-back position). */
40
+ index: number;
41
+ source: string;
42
+ state: PackageState;
43
+ /** True when the entry is a project filter delta (autoload: false). */
44
+ delta: boolean;
45
+ }
46
+
47
+ /** One settings file's package array (raw, for read-modify-write). */
48
+ export interface PackageFile {
49
+ path: string;
50
+ scope: PackageScope;
51
+ packages: unknown[];
52
+ }
53
+
54
+ export const PLUGINS_PREFIX = "ceulen.plugins.";
55
+
56
+ function globalSettingsPath(): string {
57
+ return path.join(agentDirs()[0]!, "settings.json");
58
+ }
59
+
60
+ function projectSettingsPath(cwd: string): string {
61
+ return path.join(cwd, ".pi", "settings.json");
62
+ }
63
+
64
+ /** Package arrays in Pi's load order: project first (it can shadow/delta
65
+ * global entries), then global. The project file is only eligible when
66
+ * trusted — an untrusted checkout's packages never load, so showing them
67
+ * would misrepresent the session. */
68
+ export function readPackageFiles(cwd = process.cwd()): PackageFile[] {
69
+ const files: PackageFile[] = [];
70
+ const projectFile = projectSettingsPath(cwd);
71
+ if (isProjectTrusted(cwd) && existsSync(projectFile)) {
72
+ const pkgs = readPackagesFrom(projectFile);
73
+ if (pkgs.length > 0) files.push({ path: projectFile, scope: "project", packages: pkgs });
74
+ }
75
+ const globalFile = globalSettingsPath();
76
+ if (existsSync(globalFile)) {
77
+ const pkgs = readPackagesFrom(globalFile);
78
+ if (pkgs.length > 0) files.push({ path: globalFile, scope: "global", packages: pkgs });
79
+ }
80
+ return files;
81
+ }
82
+
83
+ /** The raw `packages` array of one settings file ([] on missing/corrupt —
84
+ * reads never throw; the WRITE path refuses a corrupt file instead). */
85
+ export function readPackagesFrom(file: string): unknown[] {
86
+ try {
87
+ const json = JSON.parse(readFileSync(file, "utf8")) as Record<string, unknown>;
88
+ return Array.isArray(json.packages) ? json.packages : [];
89
+ } catch {
90
+ return [];
91
+ }
92
+ }
93
+
94
+ /** Entry state per the load contract above:
95
+ * - string → on
96
+ * - object, autoload:false → filtered (project delta — pi config owns it)
97
+ * - object, no resource arrays → on (loads package defaults)
98
+ * - object, ALL four arrays empty → off (Pi: "empty array disables all")
99
+ * - object, patterns or partial [] → filtered (granular pi config setup) */
100
+ export function packageState(pkg: unknown): PackageState {
101
+ if (typeof pkg === "string") return "on";
102
+ if (pkg && typeof pkg === "object") {
103
+ const o = pkg as Record<string, unknown>;
104
+ if (o.autoload === false) return "filtered";
105
+ const arrays = RESOURCE_KEYS.filter((k) => Array.isArray(o[k]));
106
+ if (arrays.length === 0) return "on";
107
+ const allEmpty = arrays.every((k) => (o[k] as unknown[]).length === 0);
108
+ return allEmpty && arrays.length === RESOURCE_KEYS.length ? "off" : "filtered";
109
+ }
110
+ return "filtered";
111
+ }
112
+
113
+ /** The source string of an entry, whatever its shape. */
114
+ export function packageSource(pkg: unknown): string {
115
+ if (typeof pkg === "string") return pkg;
116
+ if (pkg && typeof pkg === "object") {
117
+ const source = (pkg as Record<string, unknown>).source;
118
+ if (typeof source === "string") return source;
119
+ }
120
+ return "";
121
+ }
122
+
123
+ /** Flat list of toggleable/visible entries across both scopes, project first. */
124
+ export function readPackageEntries(cwd = process.cwd()): PackageEntry[] {
125
+ const out: PackageEntry[] = [];
126
+ for (const file of readPackageFiles(cwd)) {
127
+ for (let i = 0; i < file.packages.length; i++) {
128
+ const pkg = file.packages[i];
129
+ const source = packageSource(pkg);
130
+ if (!source) continue; // malformed entry — not ours to represent
131
+ const isObj = typeof pkg === "object" && pkg !== null;
132
+ out.push({
133
+ scope: file.scope,
134
+ index: i,
135
+ source,
136
+ state: packageState(pkg),
137
+ delta: isObj && (pkg as Record<string, unknown>).autoload === false,
138
+ });
139
+ }
140
+ }
141
+ return out;
142
+ }
143
+
144
+ /** Display label: npm scope+name without version/prefix; path basename for
145
+ * local/git sources. */
146
+ export function packageLabel(source: string): string {
147
+ let s = source;
148
+ if (s.startsWith("npm:")) {
149
+ s = s.slice(4);
150
+ // Strip a trailing @version — an @ AFTER the last "/" is a version; one
151
+ // before it is the scope, which stays: @a-fig/accordion reads better than
152
+ // a bare name that collides across registries.
153
+ const at = s.lastIndexOf("@");
154
+ if (at > s.lastIndexOf("/")) s = s.slice(0, at);
155
+ return s;
156
+ }
157
+ else if (s.startsWith("git:")) s = s.slice(4);
158
+ else if (s.startsWith("http://")) s = s.slice(7);
159
+ else if (s.startsWith("https://")) s = s.slice(8);
160
+ else if (s.startsWith("./") || s.startsWith("../") || s.startsWith("/")) {
161
+ const base = s.replace(/\/+$/, "").split("/").pop();
162
+ return base || s;
163
+ }
164
+ s = s.replace(/\.git$/, "").replace(/\/+$/, "");
165
+ // Strip a trailing @version/@ref — but NOT a leading npm scope: an @ that
166
+ // sits after the last "/" is a version/ref, one before it is the scope.
167
+ const at = s.lastIndexOf("@");
168
+ if (at > s.lastIndexOf("/")) s = s.slice(0, at);
169
+ return s.split("/").pop() ?? s;
170
+ }
171
+
172
+ /** The replacement value that flips an entry to `enabled` (string form) or
173
+ * `disabled` (object with every resource list empty — Pi's whole-off form).
174
+ * Unknown entry shapes pass through untouched (filtered entries are not
175
+ * editable). */
176
+ export function flipPackage(pkg: unknown, enabled: boolean): unknown {
177
+ if (packageState(pkg) === "filtered") return pkg;
178
+ const source = packageSource(pkg);
179
+ if (!source) return pkg;
180
+ if (enabled) return source;
181
+ return { source, extensions: [], skills: [], prompts: [], themes: [] };
182
+ }
183
+
184
+ /** Persist a changed packages array back into its settings file (merge, atomic,
185
+ * corrupt-refusal). Returns the written path. */
186
+ export function writePackages(file: string, packages: unknown[]): string {
187
+ let settings: Record<string, unknown> = {};
188
+ if (existsSync(file)) {
189
+ try {
190
+ settings = JSON.parse(readFileSync(file, "utf8")) as Record<string, unknown>;
191
+ } catch {
192
+ throw new Error(`${file} is not valid JSON — fix or remove it before saving.`);
193
+ }
194
+ }
195
+ settings.packages = packages;
196
+ mkdirSync(path.dirname(file), { recursive: true });
197
+ const tmp = file + ".tmp";
198
+ writeFileSync(tmp, JSON.stringify(settings, null, 2) + "\n", { mode: 0o600 });
199
+ renameSync(tmp, file);
200
+ return file;
201
+ }
202
+
203
+ /** Working state for the panel: the packages arrays cloned at open, mutated
204
+ * in place by row setters, then diffed on save. */
205
+ export interface PluginsWorking {
206
+ files: { path: string; scope: PackageScope; packages: unknown[] }[];
207
+ }
208
+
209
+ export function openPluginsWorking(cwd = process.cwd()): PluginsWorking {
210
+ return {
211
+ files: readPackageFiles(cwd).map((f) => ({
212
+ path: f.path,
213
+ scope: f.scope,
214
+ packages: structuredClone(f.packages),
215
+ })),
216
+ };
217
+ }
218
+
219
+ function stateOf(state: PackageState): { value: boolean | string; kind: "toggle" | "info" } {
220
+ if (state === "filtered") return { value: "custom filters", kind: "info" };
221
+ return { value: state === "on", kind: "toggle" };
222
+ }
223
+
224
+ /** Build the PLUGINS contribution: one group per scope — project packages and
225
+ * global packages are separate SECTIONS (same `tab`, so the panel shows one
226
+ * Plugins tab with a Global/Project sidebar). A discoverability hint row
227
+ * appears when nothing is installed. Row setters mutate the working copy;
228
+ * save() diffs it against disk. */
229
+ export function buildPluginsGroups(working: PluginsWorking): PanelGroup[] {
230
+ const groups: PanelGroup[] = working.files.map((file) => ({
231
+ key: `plugins-${file.scope}`,
232
+ label: file.scope === "project" ? "Project" : "Global",
233
+ tab: "Plugins",
234
+ icon: "📦",
235
+ rows: file.packages.flatMap((pkg, i) => {
236
+ const source = packageSource(pkg);
237
+ if (!source) return [];
238
+ const state = packageState(pkg);
239
+ const isObj = typeof pkg === "object" && pkg !== null;
240
+ const delta = isObj && (pkg as Record<string, unknown>).autoload === false;
241
+ const { value, kind } = stateOf(state);
242
+ const scopeNote = file.scope === "project" ? "project" : "global";
243
+ const detail = delta
244
+ ? "Project filter delta over a personal package (managed by pi config)."
245
+ : state === "filtered"
246
+ ? "Per-resource filters set (managed by pi config)."
247
+ : "Takes effect after /reload (or restart).";
248
+ return [
249
+ row(`${PLUGINS_PREFIX}${scopeNote}.${i}`, packageLabel(source), kind, value, (v) => {
250
+ if (state === "filtered") return; // read-only
251
+ file.packages[i] = flipPackage(file.packages[i], Boolean(v));
252
+ }, {
253
+ description: `${source} · ${scopeNote}. ${detail}`,
254
+ }),
255
+ ];
256
+ }),
257
+ }));
258
+
259
+ if (groups.length === 0) {
260
+ groups.push({
261
+ key: "plugins-none",
262
+ label: "Plugins",
263
+ tab: "Plugins",
264
+ icon: "📦",
265
+ rows: [
266
+ row(`${PLUGINS_PREFIX}none`, "(no packages installed)", "info", "", () => {}, {
267
+ description: "Install with `pi install npm:<package>` or `pi install ./path`.",
268
+ }),
269
+ ],
270
+ });
271
+ }
272
+ return groups;
273
+ }
274
+
275
+ /** Keys this group owns (for save routing). */
276
+ export function isPluginsKey(key: string): boolean {
277
+ return key.startsWith(PLUGINS_PREFIX);
278
+ }
279
+
280
+ /** Write every file whose working array differs from disk. Returns written
281
+ * paths. Throws before writing anything if a file is corrupt. */
282
+ export function savePlugins(working: PluginsWorking): string[] {
283
+ const written: string[] = [];
284
+ for (const file of working.files) {
285
+ const before = readPackagesFrom(file.path);
286
+ if (JSON.stringify(before) === JSON.stringify(file.packages)) continue;
287
+ written.push(writePackages(file.path, file.packages));
288
+ }
289
+ return written;
290
+ }