@pi-chamber/web 1.0.7-rc.1 → 1.0.8-rc.1
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/dist/assets/AgentsSurface-BmHImX8m.js +1 -0
- package/dist/assets/ChoicesArtifact-C71fGOm2.js +1 -0
- package/dist/assets/DiagramView-BZQxoiZu.js +1 -0
- package/dist/assets/{DiffView-C3rA5DfE.js → DiffView-C70pbgk-.js} +6 -6
- package/dist/assets/FileSaveConflictDialog-Cu8c3gBV.js +3 -0
- package/dist/assets/FilesView-nFJYEdRw.js +6 -0
- package/dist/assets/GitHubDetailScaffold-Jvv_tgbl.js +11 -0
- package/dist/assets/GitHubLinkPicker-BXD2ODRi.js +1 -0
- package/dist/assets/GitHubListPrimitives-BWI706F0.js +1 -0
- package/dist/assets/{GitView-OpFCss_4.js → GitView-MIDG5RYW.js} +5 -5
- package/dist/assets/{IssuesSurface-DD6Gz5Ox.js → IssuesSurface-uNlozeu0.js} +2 -2
- package/dist/assets/{JsonTreeView-Dj75jgH6.js → JsonTreeView-CSLsTriz.js} +1 -1
- package/dist/assets/{MarkdownRendererImpl-CnIzmT1p.js → MarkdownRendererImpl-Cs-7BfWi.js} +3 -3
- package/dist/assets/PatchDiff-axbsnbTJ.js +1 -0
- package/dist/assets/PierreDiffViewer-lwn6w_5w.js +147 -0
- package/dist/assets/PullRequestsSurface-o_AXDjbO.js +2 -0
- package/dist/assets/{SessionSwitcherContent-CUz6Cc1-.js → SessionSwitcherContent-C4CMlcbv.js} +1 -1
- package/dist/assets/SessionSwitcherDropdown-DCBVuCoo.js +2 -0
- package/dist/assets/SessionUnreadDot-DyeIrsro.js +1 -0
- package/dist/assets/SettingsView-BHBpt2Um.js +7 -0
- package/dist/assets/{SettingsWindow-J6YOvGcV.js → SettingsWindow-Cb4st5y5.js} +1 -1
- package/dist/assets/{StickyHorizontalScrollbar-DNsbTLS7.js → StickyHorizontalScrollbar-CMbTn5WQ.js} +1 -1
- package/dist/assets/SubagentsArtifact-BzeMEoFP.js +1 -0
- package/dist/assets/ToolOutputDialog-BP2mOWdV.js +8 -0
- package/dist/assets/ToolPartDiffPreview-D49XBR9b.js +1 -0
- package/dist/assets/{VirtualizedFileDiff-Dq8pyTGy.js → VirtualizedFileDiff-CLgDONEQ.js} +4 -4
- package/dist/assets/{Virtualizer-okhy9Aq3.js → Virtualizer-PxblpQn6.js} +1 -1
- package/dist/assets/VisualFrame-CJrAvWSo.js +22 -0
- package/dist/assets/VisualsArtifact-DqLGgU-Q.js +1 -0
- package/dist/assets/{collapsible-D6qdoq7-.js → collapsible-BeyKtQGB.js} +1 -1
- package/dist/assets/{computeVirtualFileMetrics-uXZ9uhI_.js → computeVirtualFileMetrics-DmO6H8u6.js} +2 -2
- package/dist/assets/contextFileOpenGuard-DOK251YW.js +1 -0
- package/dist/assets/{fileTreeStatus-C_7jbeGv.js → fileTreeStatus-CnzR7kxA.js} +2 -2
- package/dist/assets/gitStatusPredicates-4kPg2t6j.js +1 -0
- package/dist/assets/githubLink-QvaLfNNT.js +10 -0
- package/dist/assets/{index-I2QYqABE.js → index-B2v0_mez.js} +8 -8
- package/dist/assets/index-D3F4JrPN.css +1 -0
- package/dist/assets/{index-DD0xW5WE.js → index-DMX9wT1Z.js} +1 -1
- package/dist/assets/{main-BnWf2tm9.js → main-BW9J3Y72.js} +2 -2
- package/dist/assets/main-BirYxac2.js +137 -0
- package/dist/assets/{markdownCore-C-Ulh4n9.js → markdownCore-CxmcPur8.js} +2 -2
- package/dist/assets/miniChat-BRB11eEs.js +2 -0
- package/dist/assets/{mobile-BuAWpKj9.js → mobile-DJZYYCuB.js} +2 -2
- package/dist/assets/{nativeFileSave-Cp9i5QGd.js → nativeFileSave-B6QpoPRn.js} +1 -1
- package/dist/assets/pi-session-focus-preference-PWoh3LNA.js +9 -0
- package/dist/assets/{renderDiffWithHighlighter-DD-iWWus.js → renderDiffWithHighlighter-C48T6YFJ.js} +1 -1
- package/dist/assets/renderElectronMiniChatApp-N-BQdJl0.js +2 -0
- package/dist/assets/{renderFileWithHighlighter-B0pofdV1.js → renderFileWithHighlighter-8Kt2MyTN.js} +1 -1
- package/dist/assets/{renderMobileApp-B3P_bita.js → renderMobileApp-B7cT7m5b.js} +3 -3
- package/dist/assets/runtimeEndpointReset-BC6yGnxN.js +62 -0
- package/dist/assets/runtimeSurface-kIEoYsPP.js +1 -0
- package/dist/assets/selection-BTEdLRVK.js +1 -0
- package/dist/assets/useAppFontEffects-EcTZ_I_I.js +225 -0
- package/dist/assets/useGitHubRemoteSearch-DIBHNu8r.js +1 -0
- package/dist/assets/useSwitcherItems-DfLbfX37.js +1 -0
- package/dist/assets/{vendor-aparajita-capacitor-secure-storage-DVZvgYu4.js → vendor-aparajita-capacitor-secure-storage-DUoR8t1G.js} +1 -1
- package/dist/assets/{vendor-base-ui-BCqPBzDj.js → vendor-base-ui-DE6jnTiC.js} +1 -1
- package/dist/assets/{vendor-boot-3SaHYVb1.js → vendor-boot-D-KEJjsM.js} +29 -29
- package/dist/assets/{vendor-capacitor-app-NbQ-WiPS.js → vendor-capacitor-app-CO5jS31X.js} +1 -1
- package/dist/assets/{vendor-capacitor-keyboard-CgrHt8iN.js → vendor-capacitor-keyboard-335osRpd.js} +1 -1
- package/dist/assets/{vendor-capacitor-status-bar-CWt0OF5t.js → vendor-capacitor-status-bar-lB9y4H9d.js} +1 -1
- package/dist/assets/{vendor-codemirror-autocomplete-WThuuUAz.js → vendor-codemirror-autocomplete-BKvJ8PfM.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-angular-BbLGCxF6.js → vendor-codemirror-lang-angular-wjCUD0bM.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-cpp-Cceks-9O.js → vendor-codemirror-lang-cpp-LJzlG8sJ.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-css-D1wpUg3Z.js → vendor-codemirror-lang-css-DoEEdEpW.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-go-Cv0zXHHM.js → vendor-codemirror-lang-go-DexsLPb8.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-html-DbL8oT9H.js → vendor-codemirror-lang-html-h25V-Hp5.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-java-CNugenuC.js → vendor-codemirror-lang-java-D8xoT8cv.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-javascript-Bx_FIyZF.js → vendor-codemirror-lang-javascript-DF3GDYje.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-jinja-DDGjB3Zq.js → vendor-codemirror-lang-jinja-BboTHFdn.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-json-DIvqf9Pb.js → vendor-codemirror-lang-json-DCqvG1nE.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-less-D8r9dGG1.js → vendor-codemirror-lang-less-B8ZrSKJM.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-liquid-B24mguYi.js → vendor-codemirror-lang-liquid-DF9S5m5F.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-markdown-v3yG2sbf.js → vendor-codemirror-lang-markdown-6BNbCEPe.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-php-BcZGalqy.js → vendor-codemirror-lang-php-CQwAz1Hd.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-python-b-JqEwQy.js → vendor-codemirror-lang-python-DQym-8Tx.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-rust-B1uZxDW-.js → vendor-codemirror-lang-rust-DknzOTkO.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-sass-DqPrngKp.js → vendor-codemirror-lang-sass-dB0Rm5HI.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-sql-UyCnpt-k.js → vendor-codemirror-lang-sql-CCQUnnD-.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-vue-xDwYkovV.js → vendor-codemirror-lang-vue-h1M5bF00.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-wast-CF3169p_.js → vendor-codemirror-lang-wast-DWgtlGfu.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-xml-FCtVhuOb.js → vendor-codemirror-lang-xml-DncwoCUY.js} +1 -1
- package/dist/assets/{vendor-codemirror-lang-yaml-g0sPmAnv.js → vendor-codemirror-lang-yaml-C7QkS5ER.js} +1 -1
- package/dist/assets/{vendor-codemirror-language-data-B03xgzP2.js → vendor-codemirror-language-data-BUZIlm3G.js} +2 -2
- package/dist/assets/{vendor-codemirror-search-Dw3Wpu15.js → vendor-codemirror-search-BlDCaCMK.js} +1 -1
- package/dist/assets/{vendor-lezer-cpp-Cu3E-KUI.js → vendor-lezer-cpp-B4JK1BC1.js} +1 -1
- package/dist/assets/{vendor-lezer-css-Cb8vp67u.js → vendor-lezer-css-DRxDmhcy.js} +1 -1
- package/dist/assets/{vendor-lezer-go-BTsIaOm7.js → vendor-lezer-go-DsUgfmxi.js} +1 -1
- package/dist/assets/{vendor-lezer-html-UQMsUwj-.js → vendor-lezer-html-qr9pUPmM.js} +1 -1
- package/dist/assets/{vendor-lezer-java-BIWrFrAx.js → vendor-lezer-java-6UyNiQiB.js} +1 -1
- package/dist/assets/{vendor-lezer-javascript-BH2Etgoy.js → vendor-lezer-javascript-CUdJ0VGA.js} +1 -1
- package/dist/assets/{vendor-lezer-json-CE-QUCwM.js → vendor-lezer-json-BKYAA8Hu.js} +1 -1
- package/dist/assets/{vendor-lezer-lr-HY6Yc0Vr.js → vendor-lezer-lr-DluApmOX.js} +1 -1
- package/dist/assets/{vendor-lezer-markdown-xFL3Evwf.js → vendor-lezer-markdown-B-uzYzYT.js} +1 -1
- package/dist/assets/{vendor-lezer-php-Cr_mQGrG.js → vendor-lezer-php-DI6PNjlz.js} +1 -1
- package/dist/assets/{vendor-lezer-python-BNRFWCb0.js → vendor-lezer-python-Bj232yb5.js} +1 -1
- package/dist/assets/{vendor-lezer-rust-BvfOK5cU.js → vendor-lezer-rust-DlPG_Yau.js} +1 -1
- package/dist/assets/{vendor-lezer-sass-qoxiLisk.js → vendor-lezer-sass-CBsyWB2A.js} +1 -1
- package/dist/assets/{vendor-lezer-xml--x8SSpqU.js → vendor-lezer-xml-C1FmgtmE.js} +1 -1
- package/dist/assets/{vendor-lezer-yaml-BDo1JaEr.js → vendor-lezer-yaml-DpHmjriG.js} +1 -1
- package/dist/assets/{vendor-replit-codemirror-vim-D1Fe7FiY.js → vendor-replit-codemirror-vim-Cvk64got.js} +1 -1
- package/dist/assets/{web-components-DB-GDG7P.js → web-components-C_DsTJis.js} +4 -4
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/package.json +2 -1
- package/plugins/registry.json +7 -0
- package/plugins/subagents/LICENSE +25 -0
- package/plugins/subagents/agents/planner.md +36 -0
- package/plugins/subagents/agents/reviewer.md +34 -0
- package/plugins/subagents/agents/scout.md +49 -0
- package/plugins/subagents/agents/worker.md +23 -0
- package/plugins/subagents/extension.js +631 -0
- package/plugins/subagents/extension.test.js +295 -0
- package/plugins/subagents/package.json +41 -0
- package/plugins/visuals/choices.js +168 -0
- package/plugins/visuals/extension.js +87 -0
- package/plugins/visuals/files.js +74 -0
- package/plugins/visuals/package.json +32 -0
- package/server/index.js +20 -0
- package/server/lib/github/DOCUMENTATION.md +4 -2
- package/server/lib/github/gh-cli.js +35 -9
- package/server/lib/github/gh-cli.test.js +122 -0
- package/server/lib/notifications/pi-notification-watcher.js +3 -0
- package/server/lib/notifications/pi-notification-watcher.test.js +10 -0
- package/server/lib/open-url/DOCUMENTATION.md +1 -1
- package/server/lib/pi/extension-protocol.js +54 -0
- package/server/lib/pi/fixtures/pichamber-gui-demo.ts +12 -13
- package/server/lib/pi/routes-extension-events.test.js +115 -0
- package/server/lib/pi/routes.js +307 -5
- package/server/lib/pi/routes.test.js +296 -1
- package/server/lib/pi/session-daemon/DOCUMENTATION.md +25 -8
- package/server/lib/pi/session-daemon/extension-bridge.js +89 -29
- package/server/lib/pi/session-daemon/extension-config.js +719 -0
- package/server/lib/pi/session-daemon/extension-config.test.js +618 -0
- package/server/lib/pi/session-daemon/extension-controls.js +177 -0
- package/server/lib/pi/session-daemon/extension-controls.test.js +181 -0
- package/server/lib/pi/session-daemon/extension-name.js +6 -0
- package/server/lib/pi/session-daemon/extension-name.test.js +5 -0
- package/server/lib/pi/session-daemon/extension-palette.js +146 -0
- package/server/lib/pi/session-daemon/extension-palette.test.js +107 -0
- package/server/lib/pi/session-daemon/extension-replacements.js +138 -0
- package/server/lib/pi/session-daemon/extension-replacements.test.js +116 -0
- package/server/lib/pi/session-daemon/extension-ui-mode.js +30 -0
- package/server/lib/pi/session-daemon/extension-ui-mode.test.js +34 -0
- package/server/lib/pi/session-daemon/pichamber-ui.js +44 -0
- package/server/lib/pi/session-daemon/plugin-host.js +245 -0
- package/server/lib/pi/session-daemon/plugin-host.test.js +192 -0
- package/server/lib/pi/session-daemon/session-daemon-controls.e2e.test.js +205 -0
- package/server/lib/pi/session-daemon/session-daemon-drafts.e2e.test.js +409 -0
- package/server/lib/pi/session-daemon/session-daemon-extension-config.e2e.test.js +196 -0
- package/server/lib/pi/session-daemon/session-daemon-extensions.test.js +137 -6
- package/server/lib/pi/session-daemon/session-daemon-plugins.e2e.test.js +272 -0
- package/server/lib/pi/session-daemon/session-daemon-providers.e2e.test.js +153 -0
- package/server/lib/pi/session-daemon/session-daemon.js +568 -61
- package/server/lib/pi/session-daemon/session-daemon.test.js +164 -4
- package/server/lib/pi/session-daemon/session-engine-router.js +7 -0
- package/server/lib/pi/session-daemon/session-engine-router.test.js +20 -0
- package/server/lib/pi/session-daemon/supervisor.js +22 -3
- package/server/lib/pi/settings-store.js +14 -0
- package/server/lib/pi/settings-store.test.js +11 -0
- package/server/lib/plugins/DOCUMENTATION.md +186 -0
- package/server/lib/plugins/daemon-sync.js +78 -0
- package/server/lib/plugins/daemon-sync.supervisor.test.js +83 -0
- package/server/lib/plugins/daemon-sync.test.js +96 -0
- package/server/lib/plugins/impact.js +139 -0
- package/server/lib/plugins/impact.test.js +82 -0
- package/server/lib/plugins/manifest.js +195 -0
- package/server/lib/plugins/manifest.test.js +118 -0
- package/server/lib/plugins/registry.js +59 -0
- package/server/lib/plugins/routes.js +92 -0
- package/server/lib/plugins/routes.test.js +219 -0
- package/server/lib/plugins/service.js +161 -0
- package/server/lib/plugins/store.js +261 -0
- package/server/lib/plugins/store.test.js +188 -0
- package/server/lib/plugins/visuals-extension.test.js +237 -0
- package/server/lib/server-browser/DOCUMENTATION.md +32 -7
- package/server/lib/server-browser/browser-tab.js +4 -1
- package/server/lib/server-browser/downloads.js +261 -34
- package/server/lib/server-browser/downloads.test.js +171 -0
- package/server/lib/server-browser/runtime.integration.test.js +47 -0
- package/server/lib/server-browser/runtime.js +16 -0
- package/server/lib/server-browser/server-browser.js +25 -6
- package/server/lib/tunnels/executable-search.js +5 -1
- package/server/lib/tunnels/executable-search.test.js +26 -0
- package/dist/assets/DiagramView-DyF8zzYx.js +0 -1
- package/dist/assets/FileSaveConflictDialog-CC4F--8H.js +0 -3
- package/dist/assets/FilesView-Dsg-QTJu.js +0 -6
- package/dist/assets/GitHubDetailScaffold-C9WuBMGr.js +0 -11
- package/dist/assets/GitHubLinkPicker-BkVdYxZT.js +0 -1
- package/dist/assets/GitHubListPrimitives-CcVlv_rJ.js +0 -1
- package/dist/assets/PatchDiff-DND5RBnF.js +0 -1
- package/dist/assets/PierreDiffViewer-Cq5CIrmH.js +0 -147
- package/dist/assets/PullRequestsSurface-g8HAvbFg.js +0 -2
- package/dist/assets/SessionSwitcherDropdown-COZ50Y5a.js +0 -2
- package/dist/assets/SessionUnreadDot-DOUcgBw6.js +0 -1
- package/dist/assets/SettingsView-AFymxZYC.js +0 -6
- package/dist/assets/ToolOutputDialog-5Tc7HM4z.js +0 -8
- package/dist/assets/ToolPartDiffPreview-7GiFbVl7.js +0 -1
- package/dist/assets/contextFileOpenGuard-CckKFYjI.js +0 -1
- package/dist/assets/gitStatusPredicates-CNLvdt2V.js +0 -1
- package/dist/assets/githubLink-0EoMGZKd.js +0 -10
- package/dist/assets/index-BaLCg7tL.css +0 -1
- package/dist/assets/main-yQe-k30g.js +0 -137
- package/dist/assets/miniChat-DKK1hotU.js +0 -2
- package/dist/assets/pi-session-focus-preference-DaWQ2rEE.js +0 -9
- package/dist/assets/renderElectronMiniChatApp-CKuFQBRo.js +0 -2
- package/dist/assets/runtimeEndpointReset-DutinXZK.js +0 -62
- package/dist/assets/runtimeSurface-CFwnvks_.js +0 -1
- package/dist/assets/useAppFontEffects-DQlUEhzN.js +0 -213
- package/dist/assets/useGitHubRemoteSearch-BoZvJAk3.js +0 -1
- package/dist/assets/useSwitcherItems-Dy2yumiU.js +0 -1
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
// Owns what `pi config`, `pi list`, and `pi remove` do for Pi extensions:
|
|
2
|
+
// listing what is configured (path-free), turning resources on or off per
|
|
3
|
+
// scope, and removing a configured package. `session-daemon.js` routes the
|
|
4
|
+
// private commands here and supplies the lock, queue, trust, and runtime
|
|
5
|
+
// recreation.
|
|
6
|
+
//
|
|
7
|
+
// Pi keeps the toggle logic private to its terminal screen
|
|
8
|
+
// (`modes/interactive/components/config-selector.js`), so the write functions
|
|
9
|
+
// below reimplement it method by method over a settings-manager-like object.
|
|
10
|
+
// Recheck them against that file on every SDK upgrade.
|
|
11
|
+
|
|
12
|
+
import { createHash } from 'node:crypto';
|
|
13
|
+
import { realpathSync } from 'node:fs';
|
|
14
|
+
import { homedir } from 'node:os';
|
|
15
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, win32 } from 'node:path';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
17
|
+
import {
|
|
18
|
+
CONFIG_DIR_NAME,
|
|
19
|
+
DefaultPackageManager,
|
|
20
|
+
SettingsManager,
|
|
21
|
+
} from '@earendil-works/pi-coding-agent';
|
|
22
|
+
|
|
23
|
+
import { LOADED_BUILTIN_EXTENSIONS } from './builtin-extensions.js';
|
|
24
|
+
|
|
25
|
+
// Pi's `BUILTIN_PATH_PREFIX` (core/source-info.js) is not exported.
|
|
26
|
+
// extension-config.test.js pins this against what Pi resolves.
|
|
27
|
+
export const BUILTIN_PATH_PREFIX = 'builtin:';
|
|
28
|
+
|
|
29
|
+
// Resource types Pi's filters know. Only extensions are listed for now; the
|
|
30
|
+
// writes already handle every type so skills, prompts, and themes are additive.
|
|
31
|
+
const RESOURCE_TYPES = ['extensions', 'skills', 'prompts', 'themes'];
|
|
32
|
+
const LISTED_RESOURCE_TYPES = ['extensions'];
|
|
33
|
+
const OVERRIDE_STATES = new Set(['inherit', 'load', 'unload']);
|
|
34
|
+
const GROUP_ORDER = { package: 0, auto: 1, settings: 2, builtin: 3 };
|
|
35
|
+
const MAX_ERROR_MESSAGE_LENGTH = 300;
|
|
36
|
+
const MAX_NAME_LENGTH = 256;
|
|
37
|
+
const ID_PATTERN = /^[A-Za-z0-9_-]{43}$/;
|
|
38
|
+
|
|
39
|
+
class ExtensionConfigError extends Error {
|
|
40
|
+
constructor(code, message) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.code = code;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const invalid = (message = 'The extension configuration request is invalid.') => new ExtensionConfigError('INVALID_ARGUMENT', message);
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
// Path helpers Pi uses but does not export (utils/paths.js, source-info.js).
|
|
50
|
+
|
|
51
|
+
const isSyntheticPath = (path) => path.startsWith(BUILTIN_PATH_PREFIX) || path.startsWith('<');
|
|
52
|
+
|
|
53
|
+
const canonicalizePath = (path) => {
|
|
54
|
+
try {
|
|
55
|
+
return realpathSync(path);
|
|
56
|
+
} catch {
|
|
57
|
+
return path;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const getPathComparisonKey = (path) => (process.platform === 'win32' ? win32.resolve(path).toLowerCase() : resolve(path));
|
|
62
|
+
|
|
63
|
+
const getResourcePathKey = (path) => (isSyntheticPath(path) ? path : getPathComparisonKey(canonicalizePath(path)));
|
|
64
|
+
|
|
65
|
+
const isLocalPath = (value) => {
|
|
66
|
+
const trimmed = value.trim();
|
|
67
|
+
return !['npm:', 'git:', 'github:', 'http:', 'https:', 'ssh:', 'builtin:'].some((prefix) => trimmed.startsWith(prefix));
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const normalizePath = (input) => {
|
|
71
|
+
const normalized = input.trim();
|
|
72
|
+
if (normalized === '~') return homedir();
|
|
73
|
+
if (normalized.startsWith('~/') || (process.platform === 'win32' && normalized.startsWith('~\\'))) {
|
|
74
|
+
return join(homedir(), normalized.slice(2));
|
|
75
|
+
}
|
|
76
|
+
if (/^file:\/\//.test(normalized)) return fileURLToPath(normalized);
|
|
77
|
+
return normalized;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const resolvePath = (input, baseDir) => {
|
|
81
|
+
const normalized = normalizePath(input);
|
|
82
|
+
return isAbsolute(normalized) ? resolve(normalized) : resolve(normalizePath(baseDir), normalized);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// Toggle logic, reimplemented from Pi's ResourceList (config-selector.js).
|
|
87
|
+
//
|
|
88
|
+
// `ctx` is `{ settingsManager, cwd, agentDir, inheritedEnabledByKey }`.
|
|
89
|
+
// `item` is `{ path, enabled, metadata, resourceType }`, the shape Pi's
|
|
90
|
+
// resolve() returns with the resource type added.
|
|
91
|
+
|
|
92
|
+
const stripPrefix = (entry) => (entry.startsWith('!') || entry.startsWith('+') || entry.startsWith('-') ? entry.slice(1) : entry);
|
|
93
|
+
|
|
94
|
+
const getPatternEntryTarget = stripPrefix;
|
|
95
|
+
|
|
96
|
+
const getItemScope = (item) => (item.metadata.scope === 'project' ? 'project' : 'user');
|
|
97
|
+
|
|
98
|
+
export const getResourceItemKey = (item) => `${item.resourceType}:${getResourcePathKey(item.path)}`;
|
|
99
|
+
|
|
100
|
+
const getTopLevelBaseDir = (ctx, scope) => (scope === 'project' ? join(ctx.cwd, CONFIG_DIR_NAME) : ctx.agentDir);
|
|
101
|
+
|
|
102
|
+
const getResourcePattern = (ctx, item) => {
|
|
103
|
+
if (item.metadata.source === 'builtin') return item.path;
|
|
104
|
+
const baseDir = item.metadata.baseDir ?? getTopLevelBaseDir(ctx, item.metadata.scope);
|
|
105
|
+
return relative(baseDir, item.path);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const getPackageResourcePattern = (item) => {
|
|
109
|
+
const baseDir = item.metadata.baseDir ?? dirname(item.path);
|
|
110
|
+
return relative(baseDir, item.path);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const getResourcePatternForScope = (ctx, item, scope) => {
|
|
114
|
+
const sourceScope = getItemScope(item);
|
|
115
|
+
if (scope !== sourceScope || item.metadata.source === 'builtin') return item.path;
|
|
116
|
+
const baseDir = item.metadata.baseDir ?? getTopLevelBaseDir(ctx, sourceScope);
|
|
117
|
+
return relative(baseDir, item.path);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const getTopLevelOverridePatterns = (ctx, item, scope) => {
|
|
121
|
+
const baseDir = getTopLevelBaseDir(ctx, scope);
|
|
122
|
+
const patterns = new Set([
|
|
123
|
+
getResourcePatternForScope(ctx, item, scope),
|
|
124
|
+
item.path,
|
|
125
|
+
relative(baseDir, item.path),
|
|
126
|
+
]);
|
|
127
|
+
if (item.metadata.baseDir) patterns.add(relative(item.metadata.baseDir, item.path));
|
|
128
|
+
return patterns;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const packageSourceStringMatches = (ctx, leftSource, leftScope, rightSource, rightScope) => {
|
|
132
|
+
if (leftSource === rightSource) return true;
|
|
133
|
+
if (!isLocalPath(leftSource) || !isLocalPath(rightSource)) return false;
|
|
134
|
+
const left = resolvePath(leftSource, getTopLevelBaseDir(ctx, leftScope));
|
|
135
|
+
const right = resolvePath(rightSource, getTopLevelBaseDir(ctx, rightScope));
|
|
136
|
+
return left === right;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const findMatchingPackageSource = (ctx, item, targetScope) => {
|
|
140
|
+
const settings = targetScope === 'project'
|
|
141
|
+
? ctx.settingsManager.getProjectSettings()
|
|
142
|
+
: ctx.settingsManager.getGlobalSettings();
|
|
143
|
+
return (settings.packages ?? []).find((pkg) => packageSourceStringMatches(
|
|
144
|
+
ctx,
|
|
145
|
+
item.metadata.source,
|
|
146
|
+
getItemScope(item),
|
|
147
|
+
typeof pkg === 'string' ? pkg : pkg.source,
|
|
148
|
+
targetScope,
|
|
149
|
+
));
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const createPackageOverrideSource = (ctx, item) => {
|
|
153
|
+
const source = item.metadata.source;
|
|
154
|
+
if (!isLocalPath(source)) return { source, autoload: false };
|
|
155
|
+
const sourcePath = resolvePath(source, getTopLevelBaseDir(ctx, getItemScope(item)));
|
|
156
|
+
return { source: relative(getTopLevelBaseDir(ctx, 'project'), sourcePath) || '.', autoload: false };
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const isInheritedGlobalItem = (ctx, item) => (
|
|
160
|
+
getItemScope(item) === 'user' || ctx.inheritedEnabledByKey.has(getResourceItemKey(item))
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
export const getInheritedEnabled = (ctx, item) => (
|
|
164
|
+
ctx.inheritedEnabledByKey.get(getResourceItemKey(item))
|
|
165
|
+
?? (getItemScope(item) === 'user' ? item.enabled : true)
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
export const getOverrideStateFromEntries = (entries, patterns, emptyArrayIsUnload) => {
|
|
169
|
+
if (entries.length === 0 && emptyArrayIsUnload) return 'unload';
|
|
170
|
+
let state = 'inherit';
|
|
171
|
+
for (const entry of entries) {
|
|
172
|
+
if (!patterns.has(getPatternEntryTarget(entry))) continue;
|
|
173
|
+
state = entry.startsWith('!') || entry.startsWith('-') ? 'unload' : 'load';
|
|
174
|
+
}
|
|
175
|
+
return state;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// The project's own override for an item (Pi reads this only in project mode).
|
|
179
|
+
export const getProjectOverrideState = (ctx, item) => {
|
|
180
|
+
if (item.metadata.origin === 'top-level') {
|
|
181
|
+
return getOverrideStateFromEntries(
|
|
182
|
+
ctx.settingsManager.getProjectSettings()[item.resourceType] ?? [],
|
|
183
|
+
getTopLevelOverridePatterns(ctx, item, 'project'),
|
|
184
|
+
false,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
const pkg = findMatchingPackageSource(ctx, item, 'project');
|
|
188
|
+
if (typeof pkg !== 'object' || pkg === null) return 'inherit';
|
|
189
|
+
const entries = pkg[item.resourceType];
|
|
190
|
+
if (entries === undefined) return 'inherit';
|
|
191
|
+
return getOverrideStateFromEntries(entries, new Set([getPackageResourcePattern(item)]), pkg.autoload !== false);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const setTopLevelPaths = (settingsManager, scope, key, paths) => {
|
|
195
|
+
if (scope === 'project') {
|
|
196
|
+
if (key === 'extensions') settingsManager.setProjectExtensionPaths(paths);
|
|
197
|
+
else if (key === 'skills') settingsManager.setProjectSkillPaths(paths);
|
|
198
|
+
else if (key === 'prompts') settingsManager.setProjectPromptTemplatePaths(paths);
|
|
199
|
+
else settingsManager.setProjectThemePaths(paths);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (key === 'extensions') settingsManager.setExtensionPaths(paths);
|
|
203
|
+
else if (key === 'skills') settingsManager.setSkillPaths(paths);
|
|
204
|
+
else if (key === 'prompts') settingsManager.setPromptTemplatePaths(paths);
|
|
205
|
+
else settingsManager.setThemePaths(paths);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// Global mode, top-level resource: replace any `!`/`+`/`-` entry for the
|
|
209
|
+
// pattern with `+pattern` or `-pattern` in the item's own scope.
|
|
210
|
+
export const toggleTopLevelResource = (ctx, item, enabled) => {
|
|
211
|
+
const scope = item.metadata.scope;
|
|
212
|
+
const settings = scope === 'project' ? ctx.settingsManager.getProjectSettings() : ctx.settingsManager.getGlobalSettings();
|
|
213
|
+
const current = settings[item.resourceType] ?? [];
|
|
214
|
+
const pattern = getResourcePattern(ctx, item);
|
|
215
|
+
const updated = current.filter((entry) => stripPrefix(entry) !== pattern);
|
|
216
|
+
updated.push(`${enabled ? '+' : '-'}${pattern}`);
|
|
217
|
+
setTopLevelPaths(ctx.settingsManager, scope === 'project' ? 'project' : 'user', item.resourceType, updated);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// Global mode, package resource: object form with a per-type filter, back to
|
|
221
|
+
// the plain string when no filter is left.
|
|
222
|
+
export const togglePackageResource = (ctx, item, enabled) => {
|
|
223
|
+
const scope = item.metadata.scope;
|
|
224
|
+
const settings = scope === 'project' ? ctx.settingsManager.getProjectSettings() : ctx.settingsManager.getGlobalSettings();
|
|
225
|
+
const packages = [...(settings.packages ?? [])];
|
|
226
|
+
const pkgIndex = packages.findIndex((pkg) => (typeof pkg === 'string' ? pkg : pkg.source) === item.metadata.source);
|
|
227
|
+
if (pkgIndex === -1) return false;
|
|
228
|
+
let pkg = packages[pkgIndex];
|
|
229
|
+
if (typeof pkg === 'string') {
|
|
230
|
+
pkg = { source: pkg };
|
|
231
|
+
packages[pkgIndex] = pkg;
|
|
232
|
+
}
|
|
233
|
+
const current = pkg[item.resourceType] ?? [];
|
|
234
|
+
const pattern = getPackageResourcePattern(item);
|
|
235
|
+
const updated = current.filter((entry) => stripPrefix(entry) !== pattern);
|
|
236
|
+
updated.push(`${enabled ? '+' : '-'}${pattern}`);
|
|
237
|
+
pkg[item.resourceType] = updated.length > 0 ? updated : undefined;
|
|
238
|
+
if (!RESOURCE_TYPES.some((key) => pkg[key] !== undefined)) packages[pkgIndex] = pkg.source;
|
|
239
|
+
if (scope === 'project') ctx.settingsManager.setProjectPackages(packages);
|
|
240
|
+
else ctx.settingsManager.setPackages(packages);
|
|
241
|
+
return true;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const setProjectTopLevelOverride = (ctx, item, state) => {
|
|
245
|
+
const current = ctx.settingsManager.getProjectSettings()[item.resourceType] ?? [];
|
|
246
|
+
const inherited = isInheritedGlobalItem(ctx, item);
|
|
247
|
+
const pattern = inherited ? item.path : getResourcePatternForScope(ctx, item, 'project');
|
|
248
|
+
const patterns = getTopLevelOverridePatterns(ctx, item, 'project');
|
|
249
|
+
const updated = current.filter((entry) => {
|
|
250
|
+
const target = getPatternEntryTarget(entry);
|
|
251
|
+
if ((entry.startsWith('!') || entry.startsWith('+') || entry.startsWith('-')) && patterns.has(target)) return false;
|
|
252
|
+
return !(state === 'inherit' && inherited && target === pattern);
|
|
253
|
+
});
|
|
254
|
+
if (state !== 'inherit') {
|
|
255
|
+
// Project entries name inherited files to override them. Built-in paths need no entry.
|
|
256
|
+
if (inherited && item.metadata.source !== 'builtin' && !updated.includes(pattern)) updated.push(pattern);
|
|
257
|
+
updated.push(`${state === 'load' ? '+' : '-'}${pattern}`);
|
|
258
|
+
}
|
|
259
|
+
setTopLevelPaths(ctx.settingsManager, 'project', item.resourceType, updated);
|
|
260
|
+
return true;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const setProjectPackageOverride = (ctx, item, state) => {
|
|
264
|
+
const packages = [...(ctx.settingsManager.getProjectSettings().packages ?? [])];
|
|
265
|
+
let pkgIndex = packages.findIndex((pkg) => packageSourceStringMatches(
|
|
266
|
+
ctx,
|
|
267
|
+
item.metadata.source,
|
|
268
|
+
getItemScope(item),
|
|
269
|
+
typeof pkg === 'string' ? pkg : pkg.source,
|
|
270
|
+
'project',
|
|
271
|
+
));
|
|
272
|
+
if (pkgIndex === -1) {
|
|
273
|
+
if (state === 'inherit') return false;
|
|
274
|
+
packages.push(createPackageOverrideSource(ctx, item));
|
|
275
|
+
pkgIndex = packages.length - 1;
|
|
276
|
+
}
|
|
277
|
+
let pkg = packages[pkgIndex];
|
|
278
|
+
if (pkg === undefined) return false;
|
|
279
|
+
if (typeof pkg === 'string') {
|
|
280
|
+
pkg = { source: pkg };
|
|
281
|
+
packages[pkgIndex] = pkg;
|
|
282
|
+
}
|
|
283
|
+
const pattern = getPackageResourcePattern(item);
|
|
284
|
+
const updated = (pkg[item.resourceType] ?? []).filter((entry) => getPatternEntryTarget(entry) !== pattern);
|
|
285
|
+
if (state !== 'inherit') updated.push(`${state === 'load' ? '+' : '-'}${pattern}`);
|
|
286
|
+
pkg[item.resourceType] = updated.length > 0 ? updated : undefined;
|
|
287
|
+
if (!RESOURCE_TYPES.some((key) => pkg[key] !== undefined)) {
|
|
288
|
+
if (pkg.autoload === false) packages.splice(pkgIndex, 1);
|
|
289
|
+
else packages[pkgIndex] = pkg.source;
|
|
290
|
+
}
|
|
291
|
+
ctx.settingsManager.setProjectPackages(packages);
|
|
292
|
+
return true;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
export const setProjectResourceOverride = (ctx, item, state) => (
|
|
296
|
+
item.metadata.origin === 'top-level'
|
|
297
|
+
? setProjectTopLevelOverride(ctx, item, state)
|
|
298
|
+
: setProjectPackageOverride(ctx, item, state)
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
// Global mode write. Like `pi config`, only user-scope items toggle here.
|
|
302
|
+
export const setGlobalResourceEnabled = (ctx, item, enabled) => {
|
|
303
|
+
if (getItemScope(item) !== 'user') return false;
|
|
304
|
+
if (item.metadata.origin === 'top-level') {
|
|
305
|
+
toggleTopLevelResource(ctx, item, enabled);
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
return togglePackageResource(ctx, item, enabled);
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// ---------------------------------------------------------------------------
|
|
312
|
+
// Path-free display forms.
|
|
313
|
+
|
|
314
|
+
const hashId = (...parts) => createHash('sha256').update(parts.join('\0')).digest('base64url');
|
|
315
|
+
|
|
316
|
+
const sanitizeName = (value) => {
|
|
317
|
+
const cleaned = String(value).replace(/\\/g, '/').slice(0, MAX_NAME_LENGTH);
|
|
318
|
+
return cleaned.length > 0 ? cleaned : 'unknown';
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// `pi config`'s display name: the built-in name, `parent/file` for an
|
|
322
|
+
// extension whose parent folder is not `extensions`, else the file name.
|
|
323
|
+
const resourceDisplayName = (item) => {
|
|
324
|
+
if (item.metadata.source === 'builtin') return sanitizeName(item.path.slice(BUILTIN_PATH_PREFIX.length));
|
|
325
|
+
const fileName = basename(item.path);
|
|
326
|
+
const parentFolder = basename(dirname(item.path));
|
|
327
|
+
if (item.resourceType === 'extensions' && parentFolder !== 'extensions' && parentFolder.length > 0) {
|
|
328
|
+
return sanitizeName(`${parentFolder}/${fileName}`);
|
|
329
|
+
}
|
|
330
|
+
if (item.resourceType === 'skills' && fileName === 'SKILL.md' && parentFolder.length > 0) return sanitizeName(parentFolder);
|
|
331
|
+
return sanitizeName(fileName);
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
// Removes URL userinfo (`user:token@`) from any `scheme://` authority.
|
|
335
|
+
export const stripUrlCredentials = (value) => value.replace(
|
|
336
|
+
/([A-Za-z][A-Za-z0-9+.-]*:\/\/)([^/?#\s]*@)/g,
|
|
337
|
+
(_match, scheme) => scheme,
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
const packageKind = (source) => {
|
|
341
|
+
const trimmed = source.trim();
|
|
342
|
+
if (trimmed.startsWith('npm:')) return 'npm';
|
|
343
|
+
if (isLocalPath(trimmed)) return 'local';
|
|
344
|
+
return 'git';
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
// npm and git: as written in settings, credentials stripped. Local: the
|
|
348
|
+
// folder name only.
|
|
349
|
+
export const displayPackageSource = (source, baseDir) => {
|
|
350
|
+
const kind = packageKind(source);
|
|
351
|
+
if (kind !== 'local') return stripUrlCredentials(source.trim()).slice(0, MAX_NAME_LENGTH);
|
|
352
|
+
const resolved = baseDir ? resolvePath(source, baseDir) : source.trim().replace(/[\\/]+$/, '');
|
|
353
|
+
return sanitizeName(basename(resolved) || 'local');
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const parseNpmName = (spec) => spec.match(/^(@?[^@]+(?:\/[^@]+)?)(?:@(.+))?$/)?.[1] ?? spec;
|
|
357
|
+
|
|
358
|
+
// Pi's package identity (npm by name, local by resolved path); git sources
|
|
359
|
+
// keep their written form minus credentials.
|
|
360
|
+
const packageIdentity = (source, baseDir) => {
|
|
361
|
+
const kind = packageKind(source);
|
|
362
|
+
if (kind === 'npm') return `npm:${parseNpmName(source.trim().slice('npm:'.length).trim())}`;
|
|
363
|
+
if (kind === 'git') return `git:${stripUrlCredentials(source.trim())}`;
|
|
364
|
+
return `local:${getPathComparisonKey(resolvePath(source, baseDir))}`;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const packageId = (scope, identity) => hashId('package', scope, identity);
|
|
368
|
+
|
|
369
|
+
const resourceId = (resourceType, scope, item) => hashId(
|
|
370
|
+
resourceType,
|
|
371
|
+
scope,
|
|
372
|
+
item.metadata.source === 'builtin' ? item.path : getResourcePathKey(item.path),
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
// Error text from npm, git, or the SDK: server paths and credentials removed,
|
|
376
|
+
// length capped.
|
|
377
|
+
export const sanitizeErrorMessage = (message, knownPaths = []) => {
|
|
378
|
+
let text = typeof message === 'string' ? message : String(message ?? '');
|
|
379
|
+
for (const path of [...knownPaths].filter((value) => typeof value === 'string' && value.length > 1).sort((a, b) => b.length - a.length)) {
|
|
380
|
+
text = text.split(path).join('<path>');
|
|
381
|
+
}
|
|
382
|
+
text = stripUrlCredentials(text)
|
|
383
|
+
.replace(/(^|[\s'"`(=:])(?:[A-Za-z]:)?~?[\\/](?!\/)[^\s'"`)]*/g, '$1<path>')
|
|
384
|
+
.replace(/\s+/g, ' ')
|
|
385
|
+
.trim();
|
|
386
|
+
if (text.length > MAX_ERROR_MESSAGE_LENGTH) text = `${text.slice(0, MAX_ERROR_MESSAGE_LENGTH - 3)}...`;
|
|
387
|
+
return text.length > 0 ? text : 'The operation failed.';
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
const groupOf = (metadata) => {
|
|
391
|
+
if (metadata.origin === 'package') return 'package';
|
|
392
|
+
if (metadata.source === 'builtin') return 'builtin';
|
|
393
|
+
if (metadata.source === 'auto') return 'auto';
|
|
394
|
+
return 'settings';
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
// ---------------------------------------------------------------------------
|
|
398
|
+
// Service.
|
|
399
|
+
|
|
400
|
+
const defaultCreateSettingsManager = ({ cwd, agentDir, projectTrusted }) => SettingsManager.create(cwd, agentDir, { projectTrusted });
|
|
401
|
+
const defaultCreatePackageManager = (options) => new DefaultPackageManager(options);
|
|
402
|
+
|
|
403
|
+
export function createExtensionConfig({
|
|
404
|
+
agentDir,
|
|
405
|
+
createSettingsManager = defaultCreateSettingsManager,
|
|
406
|
+
createPackageManager = defaultCreatePackageManager,
|
|
407
|
+
builtinExtensions = LOADED_BUILTIN_EXTENSIONS.map((entry) => entry.name),
|
|
408
|
+
} = {}) {
|
|
409
|
+
if (typeof agentDir !== 'string' || agentDir.length === 0) throw new TypeError('agentDir is required.');
|
|
410
|
+
|
|
411
|
+
const baseDirFor = (scope, cwd) => (scope === 'project' ? join(cwd, CONFIG_DIR_NAME) : agentDir);
|
|
412
|
+
|
|
413
|
+
// Never installs: a missing package is skipped and shows `installed: false`.
|
|
414
|
+
const resolveView = async (cwd, projectTrusted) => {
|
|
415
|
+
const settingsManager = createSettingsManager({ cwd, agentDir, projectTrusted });
|
|
416
|
+
const packageManager = createPackageManager({ cwd, agentDir, settingsManager, builtinExtensions: [...builtinExtensions] });
|
|
417
|
+
const resolved = await packageManager.resolve(async () => 'skip');
|
|
418
|
+
return { settingsManager, packageManager, resolved };
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
const itemsOf = (resolved) => LISTED_RESOURCE_TYPES.flatMap((resourceType) => (
|
|
422
|
+
(Array.isArray(resolved?.[resourceType]) ? resolved[resourceType] : [])
|
|
423
|
+
.filter((entry) => entry && typeof entry.path === 'string' && entry.metadata)
|
|
424
|
+
.map((entry) => ({ path: entry.path, enabled: entry.enabled !== false, metadata: entry.metadata, resourceType }))
|
|
425
|
+
));
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Builds both views like `pi config` does. `target` is
|
|
429
|
+
* `{ cwd, directory?, trusted }`; `cwd` is where the global view resolves.
|
|
430
|
+
*/
|
|
431
|
+
const snapshot = async ({ cwd, directory, trusted }) => {
|
|
432
|
+
const globalCwd = directory ?? cwd;
|
|
433
|
+
const global = await resolveView(globalCwd, false);
|
|
434
|
+
const project = directory && trusted ? await resolveView(directory, true) : null;
|
|
435
|
+
const globalItems = itemsOf(global.resolved);
|
|
436
|
+
const inheritedEnabledByKey = new Map(globalItems.map((item) => [getResourceItemKey(item), item.enabled]));
|
|
437
|
+
const ctxFor = (view, viewCwd) => ({ settingsManager: view.settingsManager, cwd: viewCwd, agentDir, inheritedEnabledByKey });
|
|
438
|
+
const globalCtx = ctxFor(global, globalCwd);
|
|
439
|
+
const projectCtx = project ? ctxFor(project, directory) : null;
|
|
440
|
+
|
|
441
|
+
// The same file or built-in keeps one id in both views: an item the
|
|
442
|
+
// global view lists is declared there (user scope).
|
|
443
|
+
const globalById = new Map();
|
|
444
|
+
for (const item of globalItems) globalById.set(resourceId(item.resourceType, 'user', item), item);
|
|
445
|
+
const projectById = new Map();
|
|
446
|
+
if (project) {
|
|
447
|
+
for (const item of itemsOf(project.resolved)) {
|
|
448
|
+
const declared = inheritedEnabledByKey.has(getResourceItemKey(item)) ? 'user' : getItemScope(item);
|
|
449
|
+
projectById.set(resourceId(item.resourceType, declared, item), item);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return { global, project, globalCtx, projectCtx, globalById, projectById, globalCwd };
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
// Turns the daemon's replacement facts (which carry file paths) into the
|
|
456
|
+
// path-free `replaced` list. Item ids come from this response's own
|
|
457
|
+
// `extensions` array; a winner that is not listed gets only its display name.
|
|
458
|
+
const projectReplaced = (replacements, snap, extensions) => {
|
|
459
|
+
const itemEntries = [...snap.globalById, ...(snap.projectById ?? [])];
|
|
460
|
+
const nameById = new Map(extensions.map((entry) => [entry.id, entry.name]));
|
|
461
|
+
const idByPathKey = new Map();
|
|
462
|
+
const idByBuiltinPath = new Map();
|
|
463
|
+
for (const [id, item] of itemEntries) {
|
|
464
|
+
if (!nameById.has(id)) continue;
|
|
465
|
+
if (item.metadata.source === 'builtin') idByBuiltinPath.set(item.path, id);
|
|
466
|
+
else if (!idByPathKey.has(getResourcePathKey(item.path))) idByPathKey.set(getResourcePathKey(item.path), id);
|
|
467
|
+
}
|
|
468
|
+
const winner = (by) => {
|
|
469
|
+
if (!by || typeof by.name !== 'string') return undefined;
|
|
470
|
+
const id = typeof by.path === 'string' ? idByPathKey.get(getResourcePathKey(by.path)) : undefined;
|
|
471
|
+
return id ? { id, name: nameById.get(id) } : { name: sanitizeName(by.name) };
|
|
472
|
+
};
|
|
473
|
+
const detail = (entry) => ({
|
|
474
|
+
...(entry.registers ? { registers: { kind: entry.registers.kind, name: sanitizeName(entry.registers.name) } } : {}),
|
|
475
|
+
...(winner(entry.by) ? { by: winner(entry.by) } : {}),
|
|
476
|
+
});
|
|
477
|
+
const replaced = [];
|
|
478
|
+
for (const plugin of replacements.plugins ?? []) {
|
|
479
|
+
replaced.push({ kind: 'plugin', id: plugin.id, ...detail(plugin) });
|
|
480
|
+
}
|
|
481
|
+
for (const builtin of replacements.builtins ?? []) {
|
|
482
|
+
const id = idByBuiltinPath.get(builtin.path);
|
|
483
|
+
if (id) replaced.push({ kind: 'builtin', id, ...detail(builtin) });
|
|
484
|
+
}
|
|
485
|
+
return replaced;
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
const buildResponse = (snap, { directory, trusted }, replacements) => {
|
|
489
|
+
const { global, project, projectCtx, globalById, projectById, globalCwd } = snap;
|
|
490
|
+
const listing = project ?? global;
|
|
491
|
+
const configured = listing.packageManager.listConfiguredPackages();
|
|
492
|
+
const packages = [];
|
|
493
|
+
const packageIdBySourceScope = new Map();
|
|
494
|
+
const entries = (Array.isArray(configured) ? configured : [])
|
|
495
|
+
.filter((pkg) => pkg && typeof pkg.source === 'string' && (pkg.scope === 'user' || pkg.scope === 'project'))
|
|
496
|
+
.map((pkg) => {
|
|
497
|
+
const baseDir = baseDirFor(pkg.scope, project ? directory : globalCwd);
|
|
498
|
+
return { pkg, baseDir, identity: packageIdentity(pkg.source, baseDir) };
|
|
499
|
+
});
|
|
500
|
+
// A project entry with `autoload: false` for a package the user already
|
|
501
|
+
// has is where Pi keeps that project's overrides (see
|
|
502
|
+
// `createPackageOverrideSource`). It is not a second package: its
|
|
503
|
+
// extensions stay listed under the user's package.
|
|
504
|
+
const userIdentities = new Set(entries.filter((entry) => entry.pkg.scope === 'user').map((entry) => entry.identity));
|
|
505
|
+
const projectPackageSettings = listing.settingsManager.getProjectSettings?.().packages ?? [];
|
|
506
|
+
const isOverrideRecord = ({ pkg, identity }) => pkg.scope === 'project' && userIdentities.has(identity)
|
|
507
|
+
&& projectPackageSettings.some((entry) => entry && typeof entry === 'object' && entry.source === pkg.source && entry.autoload === false);
|
|
508
|
+
for (const entry of entries) {
|
|
509
|
+
const { pkg, baseDir, identity } = entry;
|
|
510
|
+
const id = packageId(pkg.scope, identity);
|
|
511
|
+
if (isOverrideRecord(entry)) continue;
|
|
512
|
+
packageIdBySourceScope.set(`${pkg.scope}\0${pkg.source}`, id);
|
|
513
|
+
packages.push({
|
|
514
|
+
id,
|
|
515
|
+
source: displayPackageSource(pkg.source, baseDir),
|
|
516
|
+
scope: pkg.scope,
|
|
517
|
+
kind: packageKind(pkg.source),
|
|
518
|
+
installed: typeof pkg.installedPath === 'string' && pkg.installedPath.length > 0,
|
|
519
|
+
filtered: pkg.filtered === true,
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
packages.sort((a, b) => (a.scope === b.scope ? a.source.localeCompare(b.source) : a.scope === 'user' ? -1 : 1));
|
|
523
|
+
const packageRank = new Map(packages.map((pkg, index) => [pkg.id, index]));
|
|
524
|
+
|
|
525
|
+
const extensions = [];
|
|
526
|
+
const ids = new Set([...globalById.keys(), ...projectById.keys()]);
|
|
527
|
+
for (const id of ids) {
|
|
528
|
+
const globalItem = globalById.get(id);
|
|
529
|
+
const projectItem = projectById.get(id);
|
|
530
|
+
const item = globalItem ?? projectItem;
|
|
531
|
+
const group = groupOf(item.metadata);
|
|
532
|
+
const scope = globalItem ? 'user' : getItemScope(item);
|
|
533
|
+
const pkgId = group === 'package'
|
|
534
|
+
? packageIdBySourceScope.get(`${item.metadata.scope === 'project' ? 'project' : 'user'}\0${item.metadata.source}`)
|
|
535
|
+
: undefined;
|
|
536
|
+
extensions.push({
|
|
537
|
+
id,
|
|
538
|
+
name: resourceDisplayName(item),
|
|
539
|
+
group,
|
|
540
|
+
...(pkgId ? { packageId: pkgId } : {}),
|
|
541
|
+
scope,
|
|
542
|
+
...(globalItem ? { global: globalItem.enabled } : {}),
|
|
543
|
+
...(projectItem && projectCtx ? {
|
|
544
|
+
project: {
|
|
545
|
+
state: getProjectOverrideState(projectCtx, projectItem),
|
|
546
|
+
inherited: getInheritedEnabled(projectCtx, projectItem),
|
|
547
|
+
enabled: projectItem.enabled,
|
|
548
|
+
},
|
|
549
|
+
} : {}),
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
extensions.sort((a, b) => {
|
|
553
|
+
if (a.group !== b.group) return GROUP_ORDER[a.group] - GROUP_ORDER[b.group];
|
|
554
|
+
if (a.group === 'package' && a.packageId !== b.packageId) {
|
|
555
|
+
return (packageRank.get(a.packageId) ?? Infinity) - (packageRank.get(b.packageId) ?? Infinity);
|
|
556
|
+
}
|
|
557
|
+
if (a.scope !== b.scope) return a.scope === 'user' ? -1 : 1;
|
|
558
|
+
return a.name.localeCompare(b.name);
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
const failures = new Map();
|
|
562
|
+
for (const view of [global, project]) {
|
|
563
|
+
for (const failure of view?.resolved?.installFailures ?? []) {
|
|
564
|
+
if (!failure || typeof failure.source !== 'string') continue;
|
|
565
|
+
const scope = failure.scope === 'project' ? 'project' : 'user';
|
|
566
|
+
const baseDir = baseDirFor(scope, project ? directory : globalCwd);
|
|
567
|
+
const source = displayPackageSource(failure.source, baseDir);
|
|
568
|
+
failures.set(`${scope}\0${source}`, {
|
|
569
|
+
source,
|
|
570
|
+
message: sanitizeErrorMessage(failure.message, [agentDir, directory, globalCwd, homedir()]),
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
return {
|
|
576
|
+
...(directory ? { directory } : {}),
|
|
577
|
+
project: { available: Boolean(directory), trusted: Boolean(directory && trusted) },
|
|
578
|
+
packages,
|
|
579
|
+
extensions,
|
|
580
|
+
installFailures: [...failures.values()],
|
|
581
|
+
// Present only when a live runtime answered; absent means unknown.
|
|
582
|
+
...(replacements ? { replaced: projectReplaced(replacements, snap, extensions) } : {}),
|
|
583
|
+
};
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* `replacements` is optional: `{ plugins: [{ id, registers?, by?: { path, name } }],
|
|
588
|
+
* builtins: [{ path, registers?, by? }] }` from `extension-replacements.js`.
|
|
589
|
+
* Without it the answer has no `replaced` field.
|
|
590
|
+
*/
|
|
591
|
+
const list = async (target, replacements) => buildResponse(await snapshot(target), target, replacements);
|
|
592
|
+
|
|
593
|
+
const assertWritable = (settingsManager) => {
|
|
594
|
+
if (settingsManager.drainErrors().length > 0) {
|
|
595
|
+
throw new ExtensionConfigError('PI_SETTINGS_INVALID', 'Pi settings could not be read.');
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
|
|
599
|
+
const commit = async (settingsManager) => {
|
|
600
|
+
await settingsManager.flush();
|
|
601
|
+
if (settingsManager.drainErrors().length > 0) {
|
|
602
|
+
throw new ExtensionConfigError('PI_SETTINGS_INVALID', 'Pi settings could not be written.');
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Validates a set request. Returns the parsed request; throws
|
|
608
|
+
* `INVALID_ARGUMENT` for a malformed one.
|
|
609
|
+
*/
|
|
610
|
+
const parseSetRequest = (payload) => {
|
|
611
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) throw invalid();
|
|
612
|
+
if (typeof payload.id !== 'string' || !ID_PATTERN.test(payload.id)) throw invalid();
|
|
613
|
+
if (payload.scope === 'global') {
|
|
614
|
+
if (typeof payload.enabled !== 'boolean' || payload.state !== undefined) throw invalid();
|
|
615
|
+
return { id: payload.id, scope: 'global', enabled: payload.enabled };
|
|
616
|
+
}
|
|
617
|
+
if (payload.scope === 'project') {
|
|
618
|
+
// Either the override itself, or just on/off: `set` then picks the
|
|
619
|
+
// override against the settings it reads under the lock.
|
|
620
|
+
if (payload.state === undefined && typeof payload.enabled === 'boolean') {
|
|
621
|
+
return { id: payload.id, scope: 'project', enabled: payload.enabled };
|
|
622
|
+
}
|
|
623
|
+
if (!OVERRIDE_STATES.has(payload.state) || payload.enabled !== undefined) throw invalid();
|
|
624
|
+
return { id: payload.id, scope: 'project', state: payload.state };
|
|
625
|
+
}
|
|
626
|
+
throw invalid();
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Writes one toggle. The caller holds the shared Pi config lock and the
|
|
631
|
+
* resource mutation queue, resolved `target.directory`, and read
|
|
632
|
+
* `target.trusted` inside the lock.
|
|
633
|
+
*/
|
|
634
|
+
const set = async (target, payload) => {
|
|
635
|
+
const request = parseSetRequest(payload);
|
|
636
|
+
if (request.scope === 'project') {
|
|
637
|
+
if (!target.directory) throw invalid('Project scope requires a directory.');
|
|
638
|
+
if (!target.trusted) throw new ExtensionConfigError('PROJECT_UNTRUSTED', 'The project is not trusted.');
|
|
639
|
+
}
|
|
640
|
+
const snap = await snapshot(target);
|
|
641
|
+
if (request.scope === 'global') {
|
|
642
|
+
const item = snap.globalById.get(request.id);
|
|
643
|
+
if (!item) {
|
|
644
|
+
if (snap.projectById.has(request.id)) throw invalid('Only user resources can be changed in global scope.');
|
|
645
|
+
throw new ExtensionConfigError('RESOURCE_NOT_FOUND', 'The requested extension was not found.');
|
|
646
|
+
}
|
|
647
|
+
// Writes go through a fresh manager so nothing the listing cached is
|
|
648
|
+
// written back.
|
|
649
|
+
const settingsManager = createSettingsManager({ cwd: snap.globalCwd, agentDir, projectTrusted: Boolean(target.directory && target.trusted) });
|
|
650
|
+
assertWritable(settingsManager);
|
|
651
|
+
const ctx = { ...snap.globalCtx, settingsManager };
|
|
652
|
+
if (!setGlobalResourceEnabled(ctx, item, request.enabled)) {
|
|
653
|
+
throw new ExtensionConfigError('PACKAGE_NOT_CONFIGURED', 'The package is no longer configured.');
|
|
654
|
+
}
|
|
655
|
+
await commit(settingsManager);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const item = snap.projectById.get(request.id);
|
|
659
|
+
if (!item) throw new ExtensionConfigError('RESOURCE_NOT_FOUND', 'The requested extension was not found.');
|
|
660
|
+
const settingsManager = createSettingsManager({ cwd: target.directory, agentDir, projectTrusted: true });
|
|
661
|
+
assertWritable(settingsManager);
|
|
662
|
+
const ctx = { ...snap.projectCtx, settingsManager };
|
|
663
|
+
// On/off keeps no project entry when the project would get that value
|
|
664
|
+
// anyway. The client cannot decide this: its copy of the global value may
|
|
665
|
+
// be older than a change made in a terminal or another tab.
|
|
666
|
+
const state = request.state
|
|
667
|
+
?? (request.enabled === getInheritedEnabled(snap.projectCtx, item) ? 'inherit' : request.enabled ? 'load' : 'unload');
|
|
668
|
+
// Pi reports no change for "inherit" on a package with no project entry.
|
|
669
|
+
if (setProjectResourceOverride(ctx, item, state)) await commit(settingsManager);
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* Removes one configured package through Pi's `removeAndPersist`. Same
|
|
674
|
+
* locking contract as `set`. PiChamber deletes no path itself.
|
|
675
|
+
*/
|
|
676
|
+
const removePackage = async (target, payload) => {
|
|
677
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)
|
|
678
|
+
|| typeof payload.id !== 'string' || !ID_PATTERN.test(payload.id)) {
|
|
679
|
+
throw invalid();
|
|
680
|
+
}
|
|
681
|
+
const projectTrusted = Boolean(target.directory && target.trusted);
|
|
682
|
+
const globalCwd = target.directory ?? target.cwd;
|
|
683
|
+
const settingsManager = createSettingsManager({ cwd: globalCwd, agentDir, projectTrusted });
|
|
684
|
+
const packageManager = createPackageManager({ cwd: globalCwd, agentDir, settingsManager, builtinExtensions: [...builtinExtensions] });
|
|
685
|
+
const configured = packageManager.listConfiguredPackages();
|
|
686
|
+
const match = (Array.isArray(configured) ? configured : []).find((pkg) => (
|
|
687
|
+
pkg && typeof pkg.source === 'string' && (pkg.scope === 'user' || pkg.scope === 'project')
|
|
688
|
+
&& packageId(pkg.scope, packageIdentity(pkg.source, baseDirFor(pkg.scope, globalCwd))) === payload.id
|
|
689
|
+
));
|
|
690
|
+
if (!match) throw new ExtensionConfigError('RESOURCE_NOT_FOUND', 'The requested package was not found.');
|
|
691
|
+
if (match.scope === 'project' && !projectTrusted) {
|
|
692
|
+
throw new ExtensionConfigError('PROJECT_UNTRUSTED', 'The project is not trusted.');
|
|
693
|
+
}
|
|
694
|
+
assertWritable(settingsManager);
|
|
695
|
+
// Pi matches the source as if it were typed after `pi remove`: a relative
|
|
696
|
+
// local path is read against the working directory. Settings keep it
|
|
697
|
+
// relative to their own folder, so a local package goes in as the
|
|
698
|
+
// absolute path it names there.
|
|
699
|
+
const source = packageKind(match.source) === 'local'
|
|
700
|
+
? resolvePath(match.source, baseDirFor(match.scope, globalCwd))
|
|
701
|
+
: match.source;
|
|
702
|
+
let removed;
|
|
703
|
+
try {
|
|
704
|
+
removed = await packageManager.removeAndPersist(source, { local: match.scope === 'project' });
|
|
705
|
+
} catch (error) {
|
|
706
|
+
throw new ExtensionConfigError(
|
|
707
|
+
'PACKAGE_REMOVE_FAILED',
|
|
708
|
+
sanitizeErrorMessage(error instanceof Error ? error.message : error, [agentDir, globalCwd, homedir()]),
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
// The package files may already be gone here. A settings write that
|
|
712
|
+
// fails now leaves an entry for a missing package: report it, never
|
|
713
|
+
// success.
|
|
714
|
+
await commit(settingsManager);
|
|
715
|
+
if (!removed) throw new ExtensionConfigError('PACKAGE_NOT_CONFIGURED', 'Pi settings had no entry for the package.');
|
|
716
|
+
};
|
|
717
|
+
|
|
718
|
+
return { list, set, removePackage };
|
|
719
|
+
}
|