@pi-chamber/web 1.0.8-rc.1 → 1.0.8-rc.2

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 (142) hide show
  1. package/dist/assets/{AgentsSurface-BmHImX8m.js → AgentsSurface-CcWBqAWV.js} +1 -1
  2. package/dist/assets/{ChoicesArtifact-C71fGOm2.js → ChoicesArtifact-Kk-qvrSB.js} +1 -1
  3. package/dist/assets/{DiagramView-BZQxoiZu.js → DiagramView-DZUBo1uP.js} +1 -1
  4. package/dist/assets/{DiffView-C70pbgk-.js → DiffView-CiZy4jFA.js} +1 -1
  5. package/dist/assets/{FileSaveConflictDialog-Cu8c3gBV.js → FileSaveConflictDialog-DZ9NDwOO.js} +1 -1
  6. package/dist/assets/{FilesView-nFJYEdRw.js → FilesView-DqiZ9kw9.js} +1 -1
  7. package/dist/assets/{GitHubDetailScaffold-Jvv_tgbl.js → GitHubDetailScaffold-DM2DH7o8.js} +1 -1
  8. package/dist/assets/{GitHubLinkPicker-BXD2ODRi.js → GitHubLinkPicker-ChB8NZ1h.js} +1 -1
  9. package/dist/assets/{GitHubListPrimitives-BWI706F0.js → GitHubListPrimitives-CibDiJGq.js} +1 -1
  10. package/dist/assets/{GitView-MIDG5RYW.js → GitView--EaZQrBr.js} +5 -5
  11. package/dist/assets/{IssuesSurface-uNlozeu0.js → IssuesSurface-CySTpaD1.js} +3 -3
  12. package/dist/assets/{JsonTreeView-CSLsTriz.js → JsonTreeView-_yMHKKQZ.js} +1 -1
  13. package/dist/assets/{MarkdownRendererImpl-Cs-7BfWi.js → MarkdownRendererImpl-D_-2fQw0.js} +1 -1
  14. package/dist/assets/{PatchDiff-axbsnbTJ.js → PatchDiff-bHObDlhD.js} +1 -1
  15. package/dist/assets/{PierreDiffViewer-lwn6w_5w.js → PierreDiffViewer-UY7Cse-s.js} +1 -1
  16. package/dist/assets/{PullRequestsSurface-o_AXDjbO.js → PullRequestsSurface-iXy6chwc.js} +2 -2
  17. package/dist/assets/{SessionSwitcherContent-C4CMlcbv.js → SessionSwitcherContent-DJGCa_wN.js} +1 -1
  18. package/dist/assets/{SessionSwitcherDropdown-DCBVuCoo.js → SessionSwitcherDropdown-CDOX-5Y2.js} +2 -2
  19. package/dist/assets/{SessionUnreadDot-DyeIrsro.js → SessionUnreadDot-Dwc3MdvA.js} +1 -1
  20. package/dist/assets/SettingsView-DA-U9lsj.js +11 -0
  21. package/dist/assets/{SettingsWindow-Cb4st5y5.js → SettingsWindow-CUg49Mfh.js} +1 -1
  22. package/dist/assets/{SubagentsArtifact-BzeMEoFP.js → SubagentsArtifact-DUZX9tDe.js} +1 -1
  23. package/dist/assets/{ToolOutputDialog-BP2mOWdV.js → ToolOutputDialog-BuZZaRw4.js} +1 -1
  24. package/dist/assets/{ToolPartDiffPreview-D49XBR9b.js → ToolPartDiffPreview-CKncG6i2.js} +1 -1
  25. package/dist/assets/{VirtualizedFileDiff-CLgDONEQ.js → VirtualizedFileDiff-BGZiLPe_.js} +1 -1
  26. package/dist/assets/VisualFrame-DBZkHFnG.js +22 -0
  27. package/dist/assets/VisualsArtifact-Bq0vMO-i.js +1 -0
  28. package/dist/assets/{collapsible-BeyKtQGB.js → collapsible-Bro9KrfM.js} +1 -1
  29. package/dist/assets/{computeVirtualFileMetrics-DmO6H8u6.js → computeVirtualFileMetrics-DJQqK49u.js} +1 -1
  30. package/dist/assets/{contextFileOpenGuard-DOK251YW.js → contextFileOpenGuard-ByWK3Sru.js} +1 -1
  31. package/dist/assets/{fileTreeStatus-CnzR7kxA.js → fileTreeStatus-CyJ3TOYD.js} +1 -1
  32. package/dist/assets/{gitStatusPredicates-4kPg2t6j.js → gitStatusPredicates-BsXrwngT.js} +1 -1
  33. package/dist/assets/{githubLink-QvaLfNNT.js → githubLink-g2bmC0qi.js} +1 -1
  34. package/dist/assets/index-B_L9GVYw.css +1 -0
  35. package/dist/assets/{index-DMX9wT1Z.js → index-C9rUUQ_Y.js} +1 -1
  36. package/dist/assets/index-CKKWVoND.js +11 -0
  37. package/dist/assets/{main-BW9J3Y72.js → main-DT-4_fhe.js} +2 -2
  38. package/dist/assets/{main-BirYxac2.js → main-DsLMU9CT.js} +4 -4
  39. package/dist/assets/{markdownCore-CxmcPur8.js → markdownCore-QmM_aOnZ.js} +1 -1
  40. package/dist/assets/{miniChat-BRB11eEs.js → miniChat-BqGslD2z.js} +2 -2
  41. package/dist/assets/{mobile-DJZYYCuB.js → mobile-C5JOcDIC.js} +2 -2
  42. package/dist/assets/{nativeFileSave-B6QpoPRn.js → nativeFileSave-DSMsJqXM.js} +1 -1
  43. package/dist/assets/{pi-session-focus-preference-PWoh3LNA.js → pi-session-focus-preference-Cc_S4zi1.js} +1 -1
  44. package/dist/assets/{renderDiffWithHighlighter-C48T6YFJ.js → renderDiffWithHighlighter-BXhnIAHg.js} +1 -1
  45. package/dist/assets/{renderElectronMiniChatApp-N-BQdJl0.js → renderElectronMiniChatApp-CiSkecEE.js} +2 -2
  46. package/dist/assets/{renderFileWithHighlighter-8Kt2MyTN.js → renderFileWithHighlighter-CK29YZtC.js} +1 -1
  47. package/dist/assets/renderMobileApp-C4JuoxsZ.js +11 -0
  48. package/dist/assets/runtimeEndpointReset-Dwj-jCDS.js +66 -0
  49. package/dist/assets/{runtimeSurface-kIEoYsPP.js → runtimeSurface-0K7vuUpd.js} +1 -1
  50. package/dist/assets/{selection-BTEdLRVK.js → selection-CnYkEsu_.js} +1 -1
  51. package/dist/assets/useAppFontEffects-CV5UyTRw.js +226 -0
  52. package/dist/assets/{useGitHubRemoteSearch-DIBHNu8r.js → useGitHubRemoteSearch-CgP-zEBj.js} +1 -1
  53. package/dist/assets/{useSwitcherItems-DfLbfX37.js → useSwitcherItems-C8G5F0hs.js} +1 -1
  54. package/dist/assets/{web-components-C_DsTJis.js → web-components-BpncHjnV.js} +1 -1
  55. package/dist/index.html +4 -4
  56. package/dist/mini-chat.html +3 -3
  57. package/dist/mobile.html +3 -3
  58. package/package.json +1 -1
  59. package/plugins/featured.json +12 -0
  60. package/plugins/subagents/package.json +1 -1
  61. package/plugins/visuals/extension.js +7 -4
  62. package/plugins/visuals/package.json +1 -1
  63. package/server/index.js +86 -27
  64. package/server/lib/git/DOCUMENTATION.md +4 -0
  65. package/server/lib/git/service.js +70 -5
  66. package/server/lib/git/service.test.js +179 -0
  67. package/server/lib/github/DOCUMENTATION.md +2 -0
  68. package/server/lib/marketplace/DOCUMENTATION.md +95 -0
  69. package/server/lib/marketplace/featured.js +176 -0
  70. package/server/lib/marketplace/featured.test.js +207 -0
  71. package/server/lib/marketplace/npm-registry.js +255 -0
  72. package/server/lib/marketplace/npm-registry.test.js +227 -0
  73. package/server/lib/marketplace/routes.js +64 -0
  74. package/server/lib/marketplace/routes.test.js +119 -0
  75. package/server/lib/marketplace/service.js +225 -0
  76. package/server/lib/marketplace/service.test.js +174 -0
  77. package/server/lib/notifications/DOCUMENTATION.md +10 -4
  78. package/server/lib/notifications/pi-notification-watcher.js +92 -33
  79. package/server/lib/notifications/pi-notification-watcher.test.js +79 -0
  80. package/server/lib/open-url/DOCUMENTATION.md +1 -0
  81. package/server/lib/pi/merged-event-stream.js +316 -0
  82. package/server/lib/pi/merged-event-stream.test.js +95 -0
  83. package/server/lib/pi/routes-daemon-pool.test.js +348 -0
  84. package/server/lib/pi/routes-merged-events.test.js +327 -0
  85. package/server/lib/pi/routes-sandbox.test.js +1684 -0
  86. package/server/lib/pi/routes.js +491 -90
  87. package/server/lib/pi/routes.test.js +87 -2
  88. package/server/lib/pi/sandbox/DOCUMENTATION.md +68 -0
  89. package/server/lib/pi/sandbox/launcher.js +122 -0
  90. package/server/lib/pi/sandbox/launcher.test.js +91 -0
  91. package/server/lib/pi/sandbox/nono-launcher.js +455 -0
  92. package/server/lib/pi/sandbox/nono-launcher.test.js +371 -0
  93. package/server/lib/pi/sandbox/nono-profile-macos.json +20 -0
  94. package/server/lib/pi/sandbox/nono-profile-wsl2.json +21 -0
  95. package/server/lib/pi/sandbox/nono-profile.json +22 -0
  96. package/server/lib/pi/sandbox/project-sandbox-manager.js +1461 -0
  97. package/server/lib/pi/sandbox/project-sandbox-setting.js +166 -0
  98. package/server/lib/pi/sandbox/project-sandbox.js +207 -0
  99. package/server/lib/pi/sandbox/sandbox-routes.js +110 -0
  100. package/server/lib/pi/sandbox/sandboxed-folders.js +100 -0
  101. package/server/lib/pi/sandbox/sandboxed-folders.test.js +65 -0
  102. package/server/lib/pi/sandbox/trust-ask.js +135 -0
  103. package/server/lib/pi/sandbox/trust-ask.test.js +54 -0
  104. package/server/lib/pi/sandbox/worktree-ids.js +96 -0
  105. package/server/lib/pi/sandbox/worktree-ids.test.js +73 -0
  106. package/server/lib/pi/session-daemon/DOCUMENTATION.md +26 -6
  107. package/server/lib/pi/session-daemon/daemon-pool.js +157 -0
  108. package/server/lib/pi/session-daemon/daemon-process.js +10 -1
  109. package/server/lib/pi/session-daemon/extension-config.js +206 -23
  110. package/server/lib/pi/session-daemon/extension-config.test.js +180 -0
  111. package/server/lib/pi/session-daemon/session-daemon-read-persisted.test.js +167 -0
  112. package/server/lib/pi/session-daemon/session-daemon.js +368 -56
  113. package/server/lib/pi/session-daemon/session-daemon.test.js +227 -0
  114. package/server/lib/pi/session-daemon/session-idle-residency.test.js +98 -1
  115. package/server/lib/pi/session-daemon/session-jsonl.js +49 -10
  116. package/server/lib/pi/session-daemon/session-jsonl.test.js +11 -0
  117. package/server/lib/pi/session-daemon/session-lease.js +93 -15
  118. package/server/lib/pi/session-daemon/session-lease.test.js +107 -1
  119. package/server/lib/pi/session-daemon/supervisor-launcher-stop.test.js +295 -0
  120. package/server/lib/pi/session-daemon/supervisor-sandbox-nono.e2e.test.js +301 -0
  121. package/server/lib/pi/session-daemon/supervisor-sandbox.test.js +444 -0
  122. package/server/lib/pi/session-daemon/supervisor.js +386 -35
  123. package/server/lib/pi/session-daemon/supervisor.test.js +28 -0
  124. package/server/lib/pi/ui-settings-store.js +88 -2
  125. package/server/lib/pi/ui-settings-store.test.js +118 -0
  126. package/server/lib/plugins/DOCUMENTATION.md +17 -5
  127. package/server/lib/plugins/daemon-sync.js +16 -6
  128. package/server/lib/plugins/daemon-sync.test.js +12 -0
  129. package/server/lib/plugins/impact.test.js +2 -1
  130. package/server/lib/plugins/manifest.js +25 -0
  131. package/server/lib/plugins/manifest.test.js +9 -0
  132. package/server/lib/plugins/routes.test.js +28 -0
  133. package/server/lib/plugins/service.js +24 -2
  134. package/server/lib/server/cross-process-lock.js +28 -3
  135. package/dist/assets/SettingsView-BHBpt2Um.js +0 -7
  136. package/dist/assets/VisualFrame-CJrAvWSo.js +0 -22
  137. package/dist/assets/VisualsArtifact-DqLGgU-Q.js +0 -1
  138. package/dist/assets/index-B2v0_mez.js +0 -11
  139. package/dist/assets/index-D3F4JrPN.css +0 -1
  140. package/dist/assets/renderMobileApp-B7cT7m5b.js +0 -11
  141. package/dist/assets/runtimeEndpointReset-BC6yGnxN.js +0 -62
  142. package/dist/assets/useAppFontEffects-EcTZ_I_I.js +0 -225
@@ -0,0 +1,176 @@
1
+ // The marketplace's curated list: packages shown first, and packages never
2
+ // shown. It ships with the app (`packages/web/plugins/featured.json`) and is
3
+ // refreshed from the PiChamber repository, so picks change without a release.
4
+ // The last good remote copy is kept in the data folder. See DOCUMENTATION.md.
5
+
6
+ import { randomUUID } from 'node:crypto';
7
+ import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
8
+ import { dirname, join } from 'node:path';
9
+ import { fileURLToPath } from 'node:url';
10
+
11
+ import { isValidNpmName } from './npm-registry.js';
12
+
13
+ const BUILTIN_FEATURED_PATH = fileURLToPath(new URL('../../../plugins/featured.json', import.meta.url));
14
+ export const DEFAULT_FEATURED_URL = 'https://raw.githubusercontent.com/RyderAsKing/PiChamber/main/packages/web/plugins/featured.json';
15
+
16
+ const REFRESH_MS = 6 * 60 * 60 * 1000;
17
+ /** After a failed refresh, wait this long before trying again. */
18
+ const RETRY_MS = 10 * 60 * 1000;
19
+ const TIMEOUT_MS = 5_000;
20
+ const MAX_BYTES = 64 * 1024;
21
+ const MAX_FEATURED = 24;
22
+ const MAX_HIDDEN = 500;
23
+ const MAX_NOTE_LENGTH = 120;
24
+ const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
25
+
26
+ const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
27
+
28
+ /**
29
+ * Parse a featured file. Returns `undefined` when the file is unusable as a
30
+ * whole (wrong version, bad date, not an object); a bad entry is skipped.
31
+ * `{ version: 1, updated: 'YYYY-MM-DD', featured: [{ name, note? }], hidden: [name] }`.
32
+ */
33
+ export const parseFeaturedList = (value) => {
34
+ if (!isRecord(value) || value.version !== 1) return undefined;
35
+ if (typeof value.updated !== 'string' || !DATE_PATTERN.test(value.updated) || !Number.isFinite(Date.parse(value.updated))) {
36
+ return undefined;
37
+ }
38
+ const hidden = [];
39
+ for (const name of Array.isArray(value.hidden) ? value.hidden : []) {
40
+ if (isValidNpmName(name) && !hidden.includes(name)) hidden.push(name);
41
+ if (hidden.length === MAX_HIDDEN) break;
42
+ }
43
+ const featured = [];
44
+ for (const entry of Array.isArray(value.featured) ? value.featured : []) {
45
+ if (!isRecord(entry) || !isValidNpmName(entry.name) || hidden.includes(entry.name)) continue;
46
+ if (featured.some((item) => item.name === entry.name)) continue;
47
+ // eslint-disable-next-line no-control-regex
48
+ const note = typeof entry.note === 'string' ? entry.note.replace(/[\u0000-\u001f\u007f]+/g, ' ').trim() : '';
49
+ featured.push({ name: entry.name, ...(note.length > 0 && note.length <= MAX_NOTE_LENGTH ? { note } : {}) });
50
+ if (featured.length === MAX_FEATURED) break;
51
+ }
52
+ return { updated: value.updated, featured, hidden };
53
+ };
54
+
55
+ /** The newer of two lists by their `updated` date; `a` wins a tie. */
56
+ const newer = (a, b) => {
57
+ if (!a) return b;
58
+ if (!b) return a;
59
+ return b.updated > a.updated ? b : a;
60
+ };
61
+
62
+ /**
63
+ * `remoteUrl`: `null` turns refreshing off. Reads never wait for the network:
64
+ * they answer from the newest of the built-in and cached lists and start a
65
+ * refresh in the background when the cache is old.
66
+ */
67
+ export const createFeaturedSource = ({
68
+ builtinPath = BUILTIN_FEATURED_PATH,
69
+ cachePath,
70
+ remoteUrl = DEFAULT_FEATURED_URL,
71
+ fetchImpl = globalThis.fetch,
72
+ now = () => Date.now(),
73
+ logger = console,
74
+ } = {}) => {
75
+ let builtin;
76
+ let cached;
77
+ let cacheLoaded = false;
78
+ let fetchedAt = 0;
79
+ let lastAttempt = 0;
80
+ let refreshing = null;
81
+
82
+ const loadBuiltin = async () => {
83
+ if (builtin !== undefined) return builtin;
84
+ try {
85
+ builtin = parseFeaturedList(JSON.parse(await readFile(builtinPath, 'utf8'))) ?? null;
86
+ } catch {
87
+ builtin = null;
88
+ }
89
+ if (!builtin) logger.warn('[marketplace] the built-in featured list is unreadable');
90
+ return builtin;
91
+ };
92
+
93
+ const loadCache = async () => {
94
+ if (cacheLoaded || !cachePath) return;
95
+ cacheLoaded = true;
96
+ try {
97
+ const stored = JSON.parse(await readFile(cachePath, 'utf8'));
98
+ const list = parseFeaturedList(stored?.list);
99
+ if (list && Number.isFinite(stored.fetchedAt)) {
100
+ cached = list;
101
+ fetchedAt = stored.fetchedAt;
102
+ }
103
+ } catch {
104
+ // Missing or unreadable: the built-in list answers until a refresh lands.
105
+ }
106
+ };
107
+
108
+ const writeCache = async (list, at) => {
109
+ if (!cachePath) return;
110
+ const temporary = join(dirname(cachePath), `.featured.${randomUUID()}.tmp`);
111
+ try {
112
+ await mkdir(dirname(cachePath), { recursive: true, mode: 0o700 });
113
+ await writeFile(temporary, JSON.stringify({ fetchedAt: at, list: { version: 1, ...list } }), { mode: 0o600 });
114
+ await rename(temporary, cachePath);
115
+ } catch (error) {
116
+ await rm(temporary, { force: true }).catch(() => {});
117
+ logger.warn('[marketplace] could not keep the featured list', error?.code ?? error?.message ?? error);
118
+ }
119
+ };
120
+
121
+ const fetchRemote = async () => {
122
+ const controller = new AbortController();
123
+ const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
124
+ timer.unref?.();
125
+ try {
126
+ const response = await fetchImpl(remoteUrl, {
127
+ headers: { accept: 'application/json', 'user-agent': 'PiChamber' },
128
+ signal: controller.signal,
129
+ });
130
+ if (!response.ok) throw new Error(`status ${response.status}`);
131
+ const text = await response.text();
132
+ if (text.length > MAX_BYTES) throw new Error('too large');
133
+ const list = parseFeaturedList(JSON.parse(text));
134
+ if (!list) throw new Error('malformed');
135
+ return list;
136
+ } finally {
137
+ clearTimeout(timer);
138
+ }
139
+ };
140
+
141
+ /** Fetch the remote list once; concurrent callers share the attempt. */
142
+ const refresh = () => {
143
+ if (!remoteUrl) return Promise.resolve(false);
144
+ if (refreshing) return refreshing;
145
+ lastAttempt = now();
146
+ refreshing = (async () => {
147
+ try {
148
+ const list = await fetchRemote();
149
+ const at = now();
150
+ cached = list;
151
+ fetchedAt = at;
152
+ await writeCache(list, at);
153
+ return true;
154
+ } catch (error) {
155
+ logger.warn('[marketplace] could not refresh the featured list', error?.message ?? error);
156
+ return false;
157
+ } finally {
158
+ refreshing = null;
159
+ }
160
+ })();
161
+ return refreshing;
162
+ };
163
+
164
+ /** `{ updated, featured, hidden, source: 'remote' | 'builtin' }`, never throws. */
165
+ const get = async () => {
166
+ const [base] = await Promise.all([loadBuiltin(), loadCache()]);
167
+ const time = now();
168
+ if (remoteUrl && time - fetchedAt > REFRESH_MS && time - lastAttempt > RETRY_MS) void refresh();
169
+ // On the same date the fetched copy wins: it is what the repository says now.
170
+ const list = newer(cached, base);
171
+ if (!list) return { updated: null, featured: [], hidden: [], source: 'builtin' };
172
+ return { ...list, source: list === base ? 'builtin' : 'remote' };
173
+ };
174
+
175
+ return { get, refresh };
176
+ };
@@ -0,0 +1,207 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { mkdtemp, readFile, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+
6
+ import { createFeaturedSource, parseFeaturedList } from './featured.js';
7
+
8
+ const tempDir = () => mkdtemp(join(tmpdir(), 'pichamber-featured-'));
9
+
10
+ const writeBuiltin = async (dir, list) => {
11
+ const path = join(dir, 'featured.json');
12
+ await writeFile(path, JSON.stringify(list));
13
+ return path;
14
+ };
15
+
16
+ const builtinList = (overrides = {}) => ({
17
+ version: 1,
18
+ updated: '2026-01-01',
19
+ featured: [{ name: 'pi-lens', note: 'Local pick' }],
20
+ hidden: [],
21
+ ...overrides,
22
+ });
23
+
24
+ const remoteResponse = (list) => ({
25
+ ok: true,
26
+ status: 200,
27
+ text: async () => JSON.stringify(list),
28
+ });
29
+
30
+ describe('parseFeaturedList', () => {
31
+ it('requires version 1 and a parseable date', () => {
32
+ expect(parseFeaturedList({ version: 2, updated: '2026-01-01', featured: [] })).toBeUndefined();
33
+ expect(parseFeaturedList({ version: 1, updated: 'not-a-date', featured: [] })).toBeUndefined();
34
+ expect(parseFeaturedList({ version: 1, updated: '2026/01/01', featured: [] })).toBeUndefined();
35
+ expect(parseFeaturedList(null)).toBeUndefined();
36
+ expect(parseFeaturedList(builtinList())).toMatchObject({ updated: '2026-01-01' });
37
+ });
38
+
39
+ it('skips bad names and drops duplicates', () => {
40
+ const list = parseFeaturedList(builtinList({
41
+ featured: [{ name: 'pi-lens' }, { name: 'Bad Name' }, { name: 'pi-lens' }, { name: '../x' }],
42
+ }));
43
+ expect(list.featured).toEqual([{ name: 'pi-lens' }]);
44
+ });
45
+
46
+ it('removes hidden names from featured', () => {
47
+ const list = parseFeaturedList(builtinList({
48
+ featured: [{ name: 'pi-lens' }, { name: 'evil-pkg' }],
49
+ hidden: ['evil-pkg'],
50
+ }));
51
+ expect(list.featured).toEqual([{ name: 'pi-lens' }]);
52
+ expect(list.hidden).toEqual(['evil-pkg']);
53
+ });
54
+
55
+ it('caps notes at 120 chars', () => {
56
+ const list = parseFeaturedList(builtinList({
57
+ featured: [{ name: 'pi-lens', note: 'x'.repeat(121) }, { name: 'pi-other', note: 'kept' }],
58
+ }));
59
+ expect(list.featured).toEqual([{ name: 'pi-lens' }, { name: 'pi-other', note: 'kept' }]);
60
+ });
61
+
62
+ it('caps featured at 24 entries', () => {
63
+ const featured = Array.from({ length: 30 }, (_, index) => ({ name: `pi-pkg-${index}` }));
64
+ const list = parseFeaturedList(builtinList({ featured }));
65
+ expect(list.featured).toHaveLength(24);
66
+ });
67
+ });
68
+
69
+ describe('createFeaturedSource', () => {
70
+ it('answers built-in when there is no cache and does not await the network', async () => {
71
+ const dir = await tempDir();
72
+ const builtinPath = await writeBuiltin(dir, builtinList());
73
+ const fetchImpl = vi.fn(() => new Promise(() => {}));
74
+ const source = createFeaturedSource({
75
+ builtinPath,
76
+ cachePath: join(dir, 'cache', 'featured.json'),
77
+ remoteUrl: 'https://example.com/featured.json',
78
+ fetchImpl,
79
+ logger: { warn: vi.fn() },
80
+ });
81
+ const result = await source.get();
82
+ expect(result).toMatchObject({ updated: '2026-01-01', source: 'builtin' });
83
+ expect(result.featured).toEqual([{ name: 'pi-lens', note: 'Local pick' }]);
84
+ expect(fetchImpl).toHaveBeenCalledTimes(1);
85
+ });
86
+
87
+ it('writes the cache on refresh and later returns the remote list', async () => {
88
+ const dir = await tempDir();
89
+ const builtinPath = await writeBuiltin(dir, builtinList());
90
+ const cachePath = join(dir, 'cache', 'featured.json');
91
+ const remote = builtinList({ updated: '2026-02-01', featured: [{ name: 'pi-new', note: 'Fresh' }] });
92
+ const fetchImpl = vi.fn(async () => remoteResponse(remote));
93
+ let time = 1_000_000;
94
+ const source = createFeaturedSource({
95
+ builtinPath, cachePath, remoteUrl: 'https://example.com/featured.json', fetchImpl, now: () => time, logger: { warn: vi.fn() },
96
+ });
97
+ expect(await source.refresh()).toBe(true);
98
+ const stored = JSON.parse(await readFile(cachePath, 'utf8'));
99
+ expect(stored.list.featured).toEqual([{ name: 'pi-new', note: 'Fresh' }]);
100
+ time += 1000;
101
+ const result = await source.get();
102
+ expect(result).toMatchObject({ updated: '2026-02-01', source: 'remote' });
103
+ expect(result.featured).toEqual([{ name: 'pi-new', note: 'Fresh' }]);
104
+ });
105
+
106
+ it('prefers the remote copy on the same date but keeps a newer built-in', async () => {
107
+ const dir = await tempDir();
108
+ const sameDayRemote = builtinList({ updated: '2026-01-01', featured: [{ name: 'pi-remote' }] });
109
+ const source = createFeaturedSource({
110
+ builtinPath: await writeBuiltin(dir, builtinList()),
111
+ cachePath: join(dir, 'cache.json'),
112
+ remoteUrl: 'https://example.com/featured.json',
113
+ fetchImpl: async () => remoteResponse(sameDayRemote),
114
+ logger: { warn: vi.fn() },
115
+ });
116
+ expect(await source.refresh()).toBe(true);
117
+ expect((await source.get()).featured).toEqual([{ name: 'pi-remote' }]);
118
+
119
+ const dir2 = await tempDir();
120
+ const olderRemote = builtinList({ updated: '2025-01-01', featured: [{ name: 'pi-old' }] });
121
+ const newer = createFeaturedSource({
122
+ builtinPath: await writeBuiltin(dir2, builtinList({ updated: '2026-03-01' })),
123
+ cachePath: join(dir2, 'cache.json'),
124
+ remoteUrl: 'https://example.com/featured.json',
125
+ fetchImpl: async () => remoteResponse(olderRemote),
126
+ logger: { warn: vi.fn() },
127
+ });
128
+ expect(await newer.refresh()).toBe(true);
129
+ const result = await newer.get();
130
+ expect(result).toMatchObject({ updated: '2026-03-01', source: 'builtin' });
131
+ });
132
+
133
+ it('logs a failed refresh and does not retry within 10 minutes', async () => {
134
+ const dir = await tempDir();
135
+ const logger = { warn: vi.fn() };
136
+ const fetchImpl = vi.fn(async () => { throw new Error('down'); });
137
+ let time = 100_000_000_000;
138
+ const source = createFeaturedSource({
139
+ builtinPath: await writeBuiltin(dir, builtinList()),
140
+ cachePath: join(dir, 'cache.json'),
141
+ remoteUrl: 'https://example.com/featured.json',
142
+ fetchImpl,
143
+ now: () => time,
144
+ logger,
145
+ });
146
+ expect(await source.refresh()).toBe(false);
147
+ expect(logger.warn).toHaveBeenCalled();
148
+ const calls = fetchImpl.mock.calls.length;
149
+ const result = await source.get();
150
+ expect(result.source).toBe('builtin');
151
+ expect(fetchImpl.mock.calls.length).toBe(calls);
152
+ time += 11 * 60 * 1000;
153
+ await source.get();
154
+ expect(fetchImpl.mock.calls.length).toBe(calls + 1);
155
+ });
156
+
157
+ it('never fetches when remoteUrl is null', async () => {
158
+ const dir = await tempDir();
159
+ const fetchImpl = vi.fn(async () => remoteResponse(builtinList()));
160
+ const source = createFeaturedSource({
161
+ builtinPath: await writeBuiltin(dir, builtinList()),
162
+ cachePath: join(dir, 'cache.json'),
163
+ remoteUrl: null,
164
+ fetchImpl,
165
+ logger: { warn: vi.fn() },
166
+ });
167
+ expect(await source.refresh()).toBe(false);
168
+ expect((await source.get()).source).toBe('builtin');
169
+ expect(fetchImpl).not.toHaveBeenCalled();
170
+ });
171
+
172
+ it('ignores a malformed cache file', async () => {
173
+ const dir = await tempDir();
174
+ const cachePath = join(dir, 'cache.json');
175
+ await writeFile(cachePath, '{broken');
176
+ const source = createFeaturedSource({
177
+ builtinPath: await writeBuiltin(dir, builtinList()),
178
+ cachePath,
179
+ remoteUrl: null,
180
+ logger: { warn: vi.fn() },
181
+ });
182
+ const result = await source.get();
183
+ expect(result).toMatchObject({ updated: '2026-01-01', source: 'builtin' });
184
+ });
185
+
186
+ it('shares one fetch across concurrent refresh calls', async () => {
187
+ const dir = await tempDir();
188
+ const remote = builtinList({ updated: '2026-02-01' });
189
+ let release;
190
+ const gate = new Promise((resolve) => { release = resolve; });
191
+ const fetchImpl = vi.fn(() => gate.then(() => remoteResponse(remote)));
192
+ const source = createFeaturedSource({
193
+ builtinPath: await writeBuiltin(dir, builtinList()),
194
+ cachePath: join(dir, 'cache.json'),
195
+ remoteUrl: 'https://example.com/featured.json',
196
+ fetchImpl,
197
+ logger: { warn: vi.fn() },
198
+ });
199
+ const first = source.refresh();
200
+ const second = source.refresh();
201
+ expect(fetchImpl).toHaveBeenCalledTimes(1);
202
+ release();
203
+ expect(await first).toBe(true);
204
+ expect(await second).toBe(true);
205
+ expect(fetchImpl).toHaveBeenCalledTimes(1);
206
+ });
207
+ });
@@ -0,0 +1,255 @@
1
+ // Reads npm's public registry for the marketplace: search, a package's latest
2
+ // manifest, and weekly downloads. Every answer is reduced to a small,
3
+ // validated shape here, so nothing npm sends reaches the browser unchecked.
4
+ // See DOCUMENTATION.md.
5
+
6
+ const DEFAULT_REGISTRY_URL = 'https://registry.npmjs.org';
7
+ const DEFAULT_DOWNLOADS_URL = 'https://api.npmjs.org';
8
+
9
+ /** Pi's gallery lists npm packages with this keyword (Pi docs/packages.md). */
10
+ export const PI_PACKAGE_KEYWORD = 'pi-package';
11
+ /** Authors add this keyword to say the package has PiChamber support. */
12
+ export const PICHAMBER_KEYWORD = 'pichamber-plugin';
13
+
14
+ const USER_AGENT = 'PiChamber';
15
+ const DEFAULT_TIMEOUT_MS = 8_000;
16
+ const MAX_SEARCH_BYTES = 2 * 1024 * 1024;
17
+ const MAX_MANIFEST_BYTES = 512 * 1024;
18
+ const MAX_DOWNLOADS_BYTES = 16 * 1024;
19
+ const MAX_NAME_LENGTH = 214;
20
+ const MAX_DESCRIPTION_LENGTH = 300;
21
+ const MAX_KEYWORDS = 20;
22
+ const MAX_KEYWORD_LENGTH = 50;
23
+ const MAX_URL_LENGTH = 500;
24
+ const PI_RESOURCE_TYPES = ['extensions', 'skills', 'prompts', 'themes'];
25
+
26
+ const NPM_NAME_PATTERN = /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/;
27
+ const VERSION_PATTERN = /^[0-9A-Za-z.+-]{1,64}$/;
28
+ const USERNAME_PATTERN = /^[A-Za-z0-9._~ -]{1,64}$/;
29
+ const LICENSE_PATTERN = /^[A-Za-z0-9.+() -]{1,64}$/;
30
+
31
+ export class MarketplaceError extends Error {
32
+ constructor(code, message = code) {
33
+ super(message);
34
+ this.code = code;
35
+ }
36
+ }
37
+
38
+ const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
39
+
40
+ /** A name npm would accept for a new package, which is what `pi install npm:<name>` needs. */
41
+ export const isValidNpmName = (value) => (
42
+ typeof value === 'string' && value.length > 0 && value.length <= MAX_NAME_LENGTH && NPM_NAME_PATTERN.test(value)
43
+ );
44
+
45
+ /** One line of plain text: control characters removed, spaces collapsed, length capped. */
46
+ const plainText = (value, max) => {
47
+ if (typeof value !== 'string') return undefined;
48
+ // eslint-disable-next-line no-control-regex
49
+ const cleaned = value.replace(/[\u0000-\u001f\u007f]+/g, ' ').replace(/\s+/g, ' ').trim();
50
+ if (cleaned.length === 0) return undefined;
51
+ return cleaned.length > max ? `${cleaned.slice(0, max - 1).trimEnd()}…` : cleaned;
52
+ };
53
+
54
+ const keywordsOf = (value) => {
55
+ if (!Array.isArray(value)) return [];
56
+ const keywords = [];
57
+ for (const entry of value) {
58
+ const keyword = typeof entry === 'string' ? entry.trim().toLowerCase() : '';
59
+ if (keyword.length === 0 || keyword.length > MAX_KEYWORD_LENGTH || keywords.includes(keyword)) continue;
60
+ keywords.push(keyword);
61
+ if (keywords.length === MAX_KEYWORDS) break;
62
+ }
63
+ return keywords;
64
+ };
65
+
66
+ /** An http(s) link the UI may open, credentials removed. Anything else is dropped. */
67
+ export const safeLink = (value) => {
68
+ if (typeof value !== 'string' || value.length > MAX_URL_LENGTH) return undefined;
69
+ let text = value.trim();
70
+ // npm's repository field: `git+https://host/x.git`, `git://host/x`, `github:user/repo`.
71
+ if (/^github:/i.test(text)) text = `https://github.com/${text.slice('github:'.length)}`;
72
+ text = text.replace(/^git\+/i, '').replace(/^git:\/\//i, 'https://');
73
+ let url;
74
+ try {
75
+ url = new URL(text);
76
+ } catch {
77
+ return undefined;
78
+ }
79
+ if (url.protocol !== 'https:' && url.protocol !== 'http:') return undefined;
80
+ url.username = '';
81
+ url.password = '';
82
+ if (url.hostname === 'github.com' || url.hostname === 'gitlab.com') url.pathname = url.pathname.replace(/\.git$/, '');
83
+ return url.toString();
84
+ };
85
+
86
+ const repositoryUrl = (repository) => safeLink(isRecord(repository) ? repository.url : repository);
87
+
88
+ const npmPageUrl = (name) => `https://www.npmjs.com/package/${name}`;
89
+
90
+ const isoDate = (value) => {
91
+ if (typeof value !== 'string') return undefined;
92
+ const time = Date.parse(value);
93
+ return Number.isFinite(time) ? new Date(time).toISOString() : undefined;
94
+ };
95
+
96
+ /** What a package's `pi` block says it adds, in Pi's resource order. */
97
+ const piContents = (pi) => (isRecord(pi)
98
+ ? PI_RESOURCE_TYPES.filter((type) => Array.isArray(pi[type]) && pi[type].length > 0)
99
+ : []);
100
+
101
+ /**
102
+ * Read a JSON body without trusting its size: the declared length is checked
103
+ * first, then the stream is cut off at `maxBytes`.
104
+ */
105
+ const readJson = async (response, maxBytes) => {
106
+ const declared = Number(response.headers?.get?.('content-length'));
107
+ if (Number.isFinite(declared) && declared > maxBytes) throw new MarketplaceError('MARKETPLACE_UNAVAILABLE', 'npm answer too large');
108
+ const reader = response.body?.getReader?.();
109
+ if (!reader) {
110
+ const text = await response.text();
111
+ if (text.length > maxBytes) throw new MarketplaceError('MARKETPLACE_UNAVAILABLE', 'npm answer too large');
112
+ return JSON.parse(text);
113
+ }
114
+ const chunks = [];
115
+ let size = 0;
116
+ for (;;) {
117
+ const { done, value } = await reader.read();
118
+ if (done) break;
119
+ size += value.byteLength;
120
+ if (size > maxBytes) {
121
+ await reader.cancel().catch(() => {});
122
+ throw new MarketplaceError('MARKETPLACE_UNAVAILABLE', 'npm answer too large');
123
+ }
124
+ chunks.push(value);
125
+ }
126
+ return JSON.parse(Buffer.concat(chunks.map((chunk) => Buffer.from(chunk))).toString('utf8'));
127
+ };
128
+
129
+ /**
130
+ * One search hit or one manifest, reduced to what the marketplace shows.
131
+ * Returns `undefined` for an entry without a usable name.
132
+ */
133
+ export const summarizePackage = (pkg, extra = {}) => {
134
+ if (!isRecord(pkg) || !isValidNpmName(pkg.name)) return undefined;
135
+ const keywords = keywordsOf(pkg.keywords);
136
+ const version = typeof pkg.version === 'string' && VERSION_PATTERN.test(pkg.version) ? pkg.version : undefined;
137
+ const description = plainText(pkg.description, MAX_DESCRIPTION_LENGTH);
138
+ // Who maintains it, not who published: CI publishes as "GitHub Actions".
139
+ const maintainer = Array.isArray(pkg.maintainers) && isRecord(pkg.maintainers[0]) ? pkg.maintainers[0] : undefined;
140
+ const publisher = [maintainer?.username, maintainer?.name, isRecord(pkg.publisher) ? pkg.publisher.username : undefined]
141
+ .find((name) => typeof name === 'string' && USERNAME_PATTERN.test(name) && name !== 'GitHub Actions');
142
+ const date = isoDate(pkg.date);
143
+ const links = isRecord(pkg.links) ? pkg.links : {};
144
+ const repository = safeLink(links.repository) ?? repositoryUrl(pkg.repository);
145
+ const homepage = safeLink(links.homepage) ?? safeLink(pkg.homepage);
146
+ return {
147
+ name: pkg.name,
148
+ ...(version ? { version } : {}),
149
+ ...(description ? { description } : {}),
150
+ keywords,
151
+ ...(publisher ? { publisher } : {}),
152
+ ...(date ? { date } : {}),
153
+ ...(Number.isFinite(extra.weeklyDownloads) ? { weeklyDownloads: Math.max(0, Math.floor(extra.weeklyDownloads)) } : {}),
154
+ // From the keyword alone. The package page reads the manifest to confirm it.
155
+ pichamberKeyword: keywords.includes(PICHAMBER_KEYWORD),
156
+ links: {
157
+ npm: npmPageUrl(pkg.name),
158
+ ...(repository ? { repository } : {}),
159
+ ...(homepage && homepage !== repository ? { homepage } : {}),
160
+ },
161
+ };
162
+ };
163
+
164
+ /**
165
+ * The latest manifest, with what only the manifest knows: the license, what
166
+ * the `pi` block adds, and whether a `pichamber` block is really there.
167
+ */
168
+ export const summarizeManifest = (manifest) => {
169
+ const summary = summarizePackage(manifest);
170
+ if (!summary) return undefined;
171
+ const license = typeof manifest.license === 'string' && LICENSE_PATTERN.test(manifest.license) ? manifest.license : undefined;
172
+ return {
173
+ ...summary,
174
+ ...(license ? { license } : {}),
175
+ contains: piContents(manifest.pi),
176
+ pichamber: isRecord(manifest.pichamber) && typeof manifest.pichamber.apiVersion === 'number',
177
+ isPiPackage: summary.keywords.includes(PI_PACKAGE_KEYWORD),
178
+ };
179
+ };
180
+
181
+ /**
182
+ * `fetchImpl` is the global `fetch` by default; tests pass a stub. Every
183
+ * request has a timeout and a size cap. Failures throw `MarketplaceError`:
184
+ * `MARKETPLACE_UNAVAILABLE` (network, status, malformed answer) or
185
+ * `MARKETPLACE_PACKAGE_NOT_FOUND` (404 for a package).
186
+ */
187
+ export const createNpmRegistry = ({
188
+ fetchImpl = globalThis.fetch,
189
+ registryUrl = DEFAULT_REGISTRY_URL,
190
+ downloadsUrl = DEFAULT_DOWNLOADS_URL,
191
+ timeoutMs = DEFAULT_TIMEOUT_MS,
192
+ } = {}) => {
193
+ const getJson = async (url, maxBytes, { notFound } = {}) => {
194
+ const controller = new AbortController();
195
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
196
+ timer.unref?.();
197
+ try {
198
+ let response;
199
+ try {
200
+ response = await fetchImpl(url, {
201
+ headers: { accept: 'application/json', 'user-agent': USER_AGENT },
202
+ redirect: 'follow',
203
+ signal: controller.signal,
204
+ });
205
+ } catch {
206
+ throw new MarketplaceError('MARKETPLACE_UNAVAILABLE', 'npm could not be reached');
207
+ }
208
+ if (response.status === 404 && notFound) throw new MarketplaceError(notFound);
209
+ if (!response.ok) throw new MarketplaceError('MARKETPLACE_UNAVAILABLE', `npm answered ${response.status}`);
210
+ try {
211
+ return await readJson(response, maxBytes);
212
+ } catch (error) {
213
+ if (error instanceof MarketplaceError) throw error;
214
+ throw new MarketplaceError('MARKETPLACE_UNAVAILABLE', 'npm answer unreadable');
215
+ }
216
+ } finally {
217
+ clearTimeout(timer);
218
+ }
219
+ };
220
+
221
+ /**
222
+ * One page of npm's search. `text` ranks results; `keywords` filter them
223
+ * (npm's `keywords:` qualifier). Answers `{ total, objects }` with raw
224
+ * entries for `searchPackages` to reduce.
225
+ */
226
+ const searchRaw = async ({ text = '', keywords = [], from = 0, size = 20 }) => {
227
+ const query = [text, ...keywords.map((keyword) => `keywords:${keyword}`)].filter(Boolean).join(' ');
228
+ const url = new URL('/-/v1/search', registryUrl);
229
+ url.searchParams.set('text', query);
230
+ url.searchParams.set('size', String(size));
231
+ url.searchParams.set('from', String(from));
232
+ const body = await getJson(url, MAX_SEARCH_BYTES);
233
+ if (!isRecord(body) || !Array.isArray(body.objects)) throw new MarketplaceError('MARKETPLACE_UNAVAILABLE', 'npm answer malformed');
234
+ return { total: Number.isFinite(body.total) ? body.total : undefined, objects: body.objects };
235
+ };
236
+
237
+ /** The `latest` manifest of one package. */
238
+ const latest = async (name) => {
239
+ if (!isValidNpmName(name)) throw new MarketplaceError('INVALID_MARKETPLACE_REQUEST');
240
+ const url = new URL(`/${name.replace('/', '%2F')}/latest`, registryUrl);
241
+ const body = await getJson(url, MAX_MANIFEST_BYTES, { notFound: 'MARKETPLACE_PACKAGE_NOT_FOUND' });
242
+ if (!isRecord(body) || body.name !== name) throw new MarketplaceError('MARKETPLACE_UNAVAILABLE', 'npm answer malformed');
243
+ return body;
244
+ };
245
+
246
+ /** Downloads in the last week, or `undefined` when npm does not say. */
247
+ const weeklyDownloads = async (name) => {
248
+ if (!isValidNpmName(name)) throw new MarketplaceError('INVALID_MARKETPLACE_REQUEST');
249
+ const url = new URL(`/downloads/point/last-week/${name}`, downloadsUrl);
250
+ const body = await getJson(url, MAX_DOWNLOADS_BYTES, { notFound: 'MARKETPLACE_PACKAGE_NOT_FOUND' });
251
+ return isRecord(body) && Number.isFinite(body.downloads) ? body.downloads : undefined;
252
+ };
253
+
254
+ return { searchRaw, latest, weeklyDownloads };
255
+ };