@pi-chamber/web 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/README.md +2 -1
  2. package/bin/cli.js +2 -2
  3. package/bin/cli.test.js +98 -1
  4. package/bin/lib/DOCUMENTATION.md +2 -1
  5. package/bin/lib/cli-args.js +8 -5
  6. package/bin/lib/cli-startup.js +10 -7
  7. package/bin/lib/cli-startup.test.js +28 -0
  8. package/bin/lib/commands-startup.js +152 -6
  9. package/dist/assets/{DiagramEditor-C7xe1Brl.js → DiagramEditor-YPSKccgo.js} +1 -1
  10. package/dist/assets/DiagramView-C-PbBHce.js +1 -0
  11. package/dist/assets/DiffView-DfltyfN2.js +18 -0
  12. package/dist/assets/FilesView-BEjbLiK2.js +6 -0
  13. package/dist/assets/GitView-B_zTaXX4.js +8 -0
  14. package/dist/assets/JsonTreeView-9p4txyB_.js +1 -0
  15. package/dist/assets/MarkdownRendererImpl-CbRPLK2x.js +12 -0
  16. package/dist/assets/PatchDiff-rKjeUBRl.js +1 -0
  17. package/dist/assets/SessionSwitcherContent-C_VxKFJy.js +1 -0
  18. package/dist/assets/SessionSwitcherDropdown-_k4P32o9.js +2 -0
  19. package/dist/assets/SessionUnreadDot-bT_0MixU.js +1 -0
  20. package/dist/assets/SettingsView-CqZ1ZAWG.js +2 -0
  21. package/dist/assets/SettingsWindow-C7vVE35-.js +1 -0
  22. package/dist/assets/ToolOutputDialog-GBbzATa6.js +8 -0
  23. package/dist/assets/ToolPartDiffPreview-Bdb9p5uS.js +1 -0
  24. package/dist/assets/{VirtualizedFileDiff-DFx8xsmh.js → VirtualizedFileDiff-CA8hQyz9.js} +4 -4
  25. package/dist/assets/{Virtualizer-BxW2CJGB.js → Virtualizer-BSHVMKgK.js} +1 -1
  26. package/dist/assets/{appThemeRegistry-BK0dHKa6.js → appThemeRegistry-gegrE2dq.js} +1 -1
  27. package/dist/assets/collapsible-uwSKBy-K.js +1 -0
  28. package/dist/assets/computeVirtualFileMetrics-9LNCLvAG.js +3 -0
  29. package/dist/assets/contextFileOpenGuard-CZSqdEHk.js +1 -0
  30. package/dist/assets/gitStatusPredicates-CRmbLl-x.js +134 -0
  31. package/dist/assets/index-BeJEipxi.js +1 -0
  32. package/dist/assets/index-CaP8oFLB.js +8 -0
  33. package/dist/assets/index-Dq8CnRYM.css +1 -0
  34. package/dist/assets/isDiffPlainText-YYyZNTdb.js +1 -0
  35. package/dist/assets/isFilePlainText-C70Z71bG.js +1 -0
  36. package/dist/assets/main-CsdVAKy_.js +135 -0
  37. package/dist/assets/main-GMQIZjuQ.js +2 -0
  38. package/dist/assets/{markdownCore-CzWR_p3k.js → markdownCore-BgVcDt0q.js} +2 -2
  39. package/dist/assets/miniChat-W6YsdiWR.js +2 -0
  40. package/dist/assets/mobile-BFR9CR5w.js +2 -0
  41. package/dist/assets/{permissionStore-BRcQ9Jgt.js → permissionStore-iKN1K2_x.js} +1 -1
  42. package/dist/assets/registerCustomTheme-DmolaWWZ.js +1 -0
  43. package/dist/assets/renderElectronMiniChatApp-B8ZiW-3E.js +2 -0
  44. package/dist/assets/renderMobileApp-C5CdsYP6.js +11 -0
  45. package/dist/assets/runtimeEndpointReset-D3AZYuoF.js +54 -0
  46. package/dist/assets/{runtimeSurface-OhCUGSKg.js → runtimeSurface-B818u-_c.js} +1 -1
  47. package/dist/assets/themeResolver-yJ9XpNMI.js +1 -0
  48. package/dist/assets/{WindowsWindowControls-DIqnZ2uU.js → useAppFontEffects-CYzYRbLp.js} +126 -499
  49. package/dist/assets/useFilesViewSearch-C--GWtn-.js +7 -0
  50. package/dist/assets/usePushVisibilityBeacon-D9lajdAc.js +10 -0
  51. package/dist/assets/useSwitcherItems-DOoi3x_8.js +1 -0
  52. package/dist/assets/vendor-base-ui-vkqQ2P9o.js +1 -0
  53. package/dist/assets/{vendor-beautiful-mermaid-r4GK_usS.js → vendor-beautiful-mermaid-DneUawHp.js} +1 -1
  54. package/dist/assets/{vendor-cmdk-BCEUzcHE.js → vendor-cmdk-zXx6sGnS.js} +1 -1
  55. package/dist/assets/{vendor-codemirror-autocomplete-BS2SpSAZ.js → vendor-codemirror-autocomplete-wd6iRebq.js} +1 -1
  56. package/dist/assets/{vendor-codemirror-commands-RrgJ8H_6.js → vendor-codemirror-commands-DYppkHEk.js} +1 -1
  57. package/dist/assets/{vendor-codemirror-lang-angular-ADhO_TUK.js → vendor-codemirror-lang-angular-DU_4KiiW.js} +1 -1
  58. package/dist/assets/{vendor-codemirror-lang-cpp-BGCWj7Om.js → vendor-codemirror-lang-cpp-CmIT9Z5_.js} +1 -1
  59. package/dist/assets/{vendor-codemirror-lang-css-Ba8JZ6fp.js → vendor-codemirror-lang-css-djiuoTi8.js} +1 -1
  60. package/dist/assets/{vendor-codemirror-lang-go-DyTiHXo4.js → vendor-codemirror-lang-go-COZSwZYW.js} +1 -1
  61. package/dist/assets/{vendor-codemirror-lang-html-DK27qQri.js → vendor-codemirror-lang-html-CKHqhW5R.js} +1 -1
  62. package/dist/assets/{vendor-codemirror-lang-java-CvFfnhPQ.js → vendor-codemirror-lang-java-CNMP5hyC.js} +1 -1
  63. package/dist/assets/{vendor-codemirror-lang-javascript-BUlYhQ3o.js → vendor-codemirror-lang-javascript-DuOEwJ3f.js} +1 -1
  64. package/dist/assets/{vendor-codemirror-lang-jinja-wBMtAEvT.js → vendor-codemirror-lang-jinja-DaKn_b1k.js} +1 -1
  65. package/dist/assets/{vendor-codemirror-lang-json-CqayksDf.js → vendor-codemirror-lang-json-CZ07HmNC.js} +1 -1
  66. package/dist/assets/{vendor-codemirror-lang-less-Ct3-H1PB.js → vendor-codemirror-lang-less-wJdQiDBf.js} +1 -1
  67. package/dist/assets/{vendor-codemirror-lang-liquid-XspREksq.js → vendor-codemirror-lang-liquid-0z48Roi8.js} +1 -1
  68. package/dist/assets/{vendor-codemirror-lang-markdown-CvO36Y1p.js → vendor-codemirror-lang-markdown-DtcZDGqe.js} +1 -1
  69. package/dist/assets/{vendor-codemirror-lang-php-BlUF9cCe.js → vendor-codemirror-lang-php-W8plS-JQ.js} +1 -1
  70. package/dist/assets/{vendor-codemirror-lang-python-_2bIYTiJ.js → vendor-codemirror-lang-python-DzzokVau.js} +1 -1
  71. package/dist/assets/{vendor-codemirror-lang-rust-BdTYih_U.js → vendor-codemirror-lang-rust-C4ulBVpf.js} +1 -1
  72. package/dist/assets/{vendor-codemirror-lang-sass-E6MhfOFk.js → vendor-codemirror-lang-sass-BkZJYEwS.js} +1 -1
  73. package/dist/assets/{vendor-codemirror-lang-sql-B1BRhQSF.js → vendor-codemirror-lang-sql-HpQWHJjv.js} +1 -1
  74. package/dist/assets/{vendor-codemirror-lang-vue-BYQJRa1Q.js → vendor-codemirror-lang-vue-DiBPljCc.js} +1 -1
  75. package/dist/assets/{vendor-codemirror-lang-wast-D706yuyC.js → vendor-codemirror-lang-wast-DoABsogh.js} +1 -1
  76. package/dist/assets/{vendor-codemirror-lang-xml--MfWU6Dj.js → vendor-codemirror-lang-xml-BhLJhOCH.js} +1 -1
  77. package/dist/assets/{vendor-codemirror-lang-yaml-CRUFIbRX.js → vendor-codemirror-lang-yaml-D95i1Y5R.js} +1 -1
  78. package/dist/assets/{vendor-codemirror-language-E2vgV9K3.js → vendor-codemirror-language-BByA_dGF.js} +2 -2
  79. package/dist/assets/{vendor-codemirror-language-data-Dv4zaHRN.js → vendor-codemirror-language-data-DS_4W5G4.js} +2 -2
  80. package/dist/assets/{vendor-codemirror-search-8kyQdB3f.js → vendor-codemirror-search-BV44140v.js} +1 -1
  81. package/dist/assets/{vendor-codemirror-view-DSzLbZiS.js → vendor-codemirror-view-Ct4T37Ue.js} +1 -1
  82. package/dist/assets/{vendor-dnd-kit-accessibility-Cv52zwWT.js → vendor-dnd-kit-accessibility-DZKKhs9S.js} +1 -1
  83. package/dist/assets/{vendor-dnd-kit-core-GipZW2tY.js → vendor-dnd-kit-core-CP8ck10N.js} +1 -1
  84. package/dist/assets/{vendor-dnd-kit-sortable-Ct2QxIHb.js → vendor-dnd-kit-sortable-B-Icjoj4.js} +1 -1
  85. package/dist/assets/{vendor-dnd-kit-utilities-CrOcp0Bp.js → vendor-dnd-kit-utilities-DTY1C6ST.js} +1 -1
  86. package/dist/assets/{vendor-elkjs-CRCLNXjf.js → vendor-elkjs-mzfkf0ML.js} +1 -1
  87. package/dist/assets/{vendor-floating-ui-dom-Bkw82kL9.js → vendor-floating-ui-dom-WyXykhNi.js} +1 -1
  88. package/dist/assets/{vendor-floating-ui-react-dom-sVFnuruh.js → vendor-floating-ui-react-dom-BZOGXAGe.js} +1 -1
  89. package/dist/assets/{vendor-framer-motion-DJASDzNt.js → vendor-framer-motion-BUygY0oN.js} +1 -1
  90. package/dist/assets/{vendor-heic2any-L8hyfc24.js → vendor-heic2any-BHsbokcA.js} +1 -1
  91. package/dist/assets/{vendor-lru_map-B-cPXHXW.js → vendor-lru_map-Cy8fTs3_.js} +1 -1
  92. package/dist/assets/{vendor-pierre-theming-C6Z4M5_u.js → vendor-pierre-theming-Dgp7B_O4.js} +1 -1
  93. package/dist/assets/{vendor-qrcode-COsmt6jn.js → vendor-qrcode-B0KNSajT.js} +1 -1
  94. package/dist/assets/{vendor-radix-ui-react-compose-refs-D1GnwnP9.js → vendor-radix-ui-react-compose-refs-8xy6TJML.js} +1 -1
  95. package/dist/assets/{vendor-radix-ui-react-context-CM4yFQ2I.js → vendor-radix-ui-react-context-CFsX-AUC.js} +1 -1
  96. package/dist/assets/{vendor-radix-ui-react-dialog-jX3Dld8c.js → vendor-radix-ui-react-dialog-D2yhEu2p.js} +1 -1
  97. package/dist/assets/{vendor-radix-ui-react-dismissable-layer-M7LsH3yL.js → vendor-radix-ui-react-dismissable-layer-DrorBNg9.js} +1 -1
  98. package/dist/assets/{vendor-radix-ui-react-focus-guards-D5MZ0nQs.js → vendor-radix-ui-react-focus-guards-BfdFmL7c.js} +1 -1
  99. package/dist/assets/{vendor-radix-ui-react-focus-scope-C2K7lNPv.js → vendor-radix-ui-react-focus-scope-D2BkImwt.js} +1 -1
  100. package/dist/assets/{vendor-radix-ui-react-id-BcxRjnbX.js → vendor-radix-ui-react-id-D2kS9K1j.js} +1 -1
  101. package/dist/assets/vendor-radix-ui-react-portal-YgCRIyi7.js +1 -0
  102. package/dist/assets/{vendor-radix-ui-react-presence-Btn-Dp7L.js → vendor-radix-ui-react-presence-D432pCMB.js} +1 -1
  103. package/dist/assets/{vendor-radix-ui-react-primitive-CfpW6_IT.js → vendor-radix-ui-react-primitive-DkH8dMOR.js} +1 -1
  104. package/dist/assets/{vendor-radix-ui-react-slot-BxUc3Pvz.js → vendor-radix-ui-react-slot-BIDFZX0-.js} +1 -1
  105. package/dist/assets/{vendor-radix-ui-react-use-callback-ref-UKEWHtoP.js → vendor-radix-ui-react-use-callback-ref-BwDcSoGV.js} +1 -1
  106. package/dist/assets/{vendor-radix-ui-react-use-controllable-state-C_ecoh35.js → vendor-radix-ui-react-use-controllable-state-LiqmKSXO.js} +1 -1
  107. package/dist/assets/vendor-radix-ui-react-use-effect-event-D94va83M.js +1 -0
  108. package/dist/assets/{vendor-radix-ui-react-use-escape-keydown-Co13lHGj.js → vendor-radix-ui-react-use-escape-keydown-DTkqjenV.js} +1 -1
  109. package/dist/assets/vendor-radix-ui-react-use-layout-effect-C8Yrsaph.js +1 -0
  110. package/dist/assets/{vendor-react-DYyjh7Q0.js → vendor-react-B5Z1Mdn3.js} +1 -1
  111. package/dist/assets/{vendor-react-drawio-Boqanm0j.js → vendor-react-drawio-BYDptOsj.js} +1 -1
  112. package/dist/assets/{vendor-react-remove-scroll-bar-C9YRs2nU.js → vendor-react-remove-scroll-bar-CSAHp1YF.js} +1 -1
  113. package/dist/assets/{vendor-react-remove-scroll-CfeMe0DL.js → vendor-react-remove-scroll-vR1dy1aP.js} +1 -1
  114. package/dist/assets/{vendor-react-style-singleton-B1H4bWSG.js → vendor-react-style-singleton-Ctw6HnYx.js} +1 -1
  115. package/dist/assets/{vendor-replit-codemirror-vim-BJY5fGLS.js → vendor-replit-codemirror-vim-CGHHNQxC.js} +1 -1
  116. package/dist/assets/{vendor-sonner-CW8w438f.js → vendor-sonner-BDtm25td.js} +1 -1
  117. package/dist/assets/{vendor-tanstack-react-virtual-IrbCBGJ4.js → vendor-tanstack-react-virtual-2skouoFw.js} +1 -1
  118. package/dist/assets/{vendor-use-callback-ref-CJB0R85I.js → vendor-use-callback-ref-CVVuCyLj.js} +1 -1
  119. package/dist/assets/{vendor-use-sidecar-C-AuTczP.js → vendor-use-sidecar-Vv4V8Nd1.js} +1 -1
  120. package/dist/assets/{vendor-use-sync-external-store-B7OScDIw.js → vendor-use-sync-external-store-D0AXkdQC.js} +1 -1
  121. package/dist/assets/{vendor-zustand-CnsBKjP5.js → vendor-zustand-GVb4H8HE.js} +1 -1
  122. package/dist/assets/{web-components-BOt08OHk.js → web-components-C2cMNPqE.js} +2 -2
  123. package/dist/index.html +6 -6
  124. package/dist/mini-chat.html +5 -5
  125. package/dist/mobile.html +5 -5
  126. package/package.json +1 -1
  127. package/server/lib/git/DOCUMENTATION.md +3 -2
  128. package/server/lib/git/routes.js +27 -0
  129. package/server/lib/git/routes.test.js +41 -0
  130. package/server/lib/git/service.js +7 -1
  131. package/server/lib/git/service.test.js +23 -0
  132. package/server/lib/package-manager.js +32 -1
  133. package/server/lib/package-manager.test.js +21 -0
  134. package/server/lib/pi/attachment-store.js +95 -22
  135. package/server/lib/pi/attachment-store.test.js +23 -0
  136. package/server/lib/pi/routes.js +383 -49
  137. package/server/lib/pi/routes.test.js +377 -3
  138. package/server/lib/pi/session-daemon/DOCUMENTATION.md +3 -3
  139. package/server/lib/pi/session-daemon/prompt-operations.test.js +318 -0
  140. package/server/lib/pi/session-daemon/session-daemon-extensions.test.js +2 -0
  141. package/server/lib/pi/session-daemon/session-daemon.js +316 -25
  142. package/server/lib/pi/session-daemon/session-daemon.test.js +137 -0
  143. package/server/lib/pi/session-daemon/supervisor.test.js +1 -1
  144. package/server/lib/pi/snippets-store.js +400 -0
  145. package/server/lib/pi/snippets-store.test.js +252 -0
  146. package/server/lib/relay/host-client.test.js +79 -15
  147. package/server/lib/server/cors.js +2 -0
  148. package/server/lib/server/cors.test.js +6 -5
  149. package/dist/assets/CommitSection-DuGkZ_bm.js +0 -1
  150. package/dist/assets/DiagramView-BTqROEWP.js +0 -1
  151. package/dist/assets/DiffView-hcqXTjEc.js +0 -18
  152. package/dist/assets/FilesView-GadyKxta.js +0 -14
  153. package/dist/assets/GitView-BKy-hYUN.js +0 -6
  154. package/dist/assets/JsonTreeView-CGc4eTGI.js +0 -1
  155. package/dist/assets/MarkdownRendererImpl-CqfEAhPM.js +0 -12
  156. package/dist/assets/PatchDiff-P17-Byb8.js +0 -1
  157. package/dist/assets/PierreDiffViewer-BbUCO4IF.js +0 -136
  158. package/dist/assets/SessionSwitcherContent-j6KFBSe0.js +0 -1
  159. package/dist/assets/SessionSwitcherDropdown-DSVeuuVL.js +0 -2
  160. package/dist/assets/SessionUnreadDot-BVBBEBTg.js +0 -1
  161. package/dist/assets/SettingsView-FnmtcWZc.js +0 -4
  162. package/dist/assets/SettingsWindow-Bascj0V3.js +0 -1
  163. package/dist/assets/ToolOutputDialog-CPMzJ27S.js +0 -15
  164. package/dist/assets/ToolPartDiffPreview-BZSRKHHM.js +0 -8
  165. package/dist/assets/collapsible-DlqZyxEY.js +0 -1
  166. package/dist/assets/computeVirtualFileMetrics-elCbdk6U.js +0 -3
  167. package/dist/assets/contextFileOpenGuard-BmQ7Zu-L.js +0 -1
  168. package/dist/assets/index-BerHSX92.js +0 -1
  169. package/dist/assets/index-ChFxkws4.js +0 -8
  170. package/dist/assets/index-DZ-f_hCQ.css +0 -1
  171. package/dist/assets/isDiffPlainText-CvfRfc7h.js +0 -1
  172. package/dist/assets/isFilePlainText-_vq9Ghu3.js +0 -1
  173. package/dist/assets/main-DTCOwPr3.js +0 -2
  174. package/dist/assets/main-DYs-nZO6.js +0 -133
  175. package/dist/assets/miniChat-C2XxEA3N.js +0 -2
  176. package/dist/assets/mobile-BQhAOqV7.js +0 -2
  177. package/dist/assets/registerCustomTheme-BBXUCRnG.js +0 -1
  178. package/dist/assets/renderElectronMiniChatApp-M2bY1WKA.js +0 -2
  179. package/dist/assets/renderMobileApp-BnpHUUhY.js +0 -14
  180. package/dist/assets/themeResolver-BmJyTmjJ.js +0 -1
  181. package/dist/assets/useInlineCommentController-DC0iNSYX.js +0 -2
  182. package/dist/assets/usePanelSlide-aeAIo1cp.js +0 -56
  183. package/dist/assets/usePushVisibilityBeacon-om5Qlrt6.js +0 -10
  184. package/dist/assets/useSwitcherItems-Bln2r_Q2.js +0 -1
  185. package/dist/assets/vendor-base-ui-DL8r2fgQ.js +0 -1
  186. package/dist/assets/vendor-radix-ui-react-portal-xlJWBfDg.js +0 -1
  187. package/dist/assets/vendor-radix-ui-react-use-effect-event-DkJl_Rn_.js +0 -1
  188. package/dist/assets/vendor-radix-ui-react-use-layout-effect-0fyGwS2_.js +0 -1
  189. package/dist/assets/vendor-remixicon-react-CzPq62YO.js +0 -1
  190. /package/dist/assets/{usePanelSlide-BV3KOtdA.css → runtimeEndpointReset-BV3KOtdA.css} +0 -0
@@ -5,7 +5,11 @@
5
5
 
6
6
  import { createPiArchiveStore } from './archive-store.js';
7
7
  import { createPiAttachmentStore } from './attachment-store.js';
8
- import { checkForUpdates } from '../package-manager.js';
8
+ import {
9
+ checkForUpdates,
10
+ launchUpdateCommand,
11
+ resolveTrustedUpdatePackageManager,
12
+ } from '../package-manager.js';
9
13
  import { listPiCustomThemes } from './custom-themes.js';
10
14
  import { createPiSettingsStore } from './settings-store.js';
11
15
  import { isPiThinkingLevel } from './thinking-levels.js';
@@ -15,6 +19,7 @@ import {
15
19
  sanitizeExtensionFormFields,
16
20
  } from './extension-protocol.js';
17
21
  import { createPiUiSettingsStore } from './ui-settings-store.js';
22
+ import { createPiSnippetsStore } from './snippets-store.js';
18
23
 
19
24
  const UNAVAILABLE_CODES = new Set([
20
25
  'DAEMON_UNAVAILABLE',
@@ -39,7 +44,17 @@ const UNAVAILABLE_CODES = new Set([
39
44
 
40
45
  const writeDaemonError = (res, error) => {
41
46
  const code = typeof error?.code === 'string' ? error.code : 'DAEMON_REQUEST_FAILED';
42
- const status = UNAVAILABLE_CODES.has(code) ? 503 : code === 'INVALID_SESSION' ? 404 : 400;
47
+ const status = UNAVAILABLE_CODES.has(code)
48
+ ? 503
49
+ : code === 'INVALID_SESSION'
50
+ ? 404
51
+ : code === 'ATTACHMENT_TOO_LARGE'
52
+ ? 413
53
+ : code === 'ATTACHMENT_LIMIT_REACHED'
54
+ ? 429
55
+ : code === 'ATTACHMENT_MISSING'
56
+ ? 410
57
+ : 400;
43
58
  res.status(status).json({ error: { code } });
44
59
  };
45
60
 
@@ -181,6 +196,16 @@ const sanitizeNavigation = (value) => {
181
196
  };
182
197
  };
183
198
 
199
+ const projectFilePart = (part) => {
200
+ // Optional fields degrade instead of failing the whole message. The daemon
201
+ // only emits inline data URLs; never pass a filesystem or remote URL on.
202
+ const file = {};
203
+ if (typeof part.mime === 'string' && part.mime.length > 0 && part.mime.length <= 200) file.mime = part.mime.slice(0, 200);
204
+ if (typeof part.filename === 'string' && part.filename.length > 0 && part.filename.length <= 2048) file.filename = part.filename.slice(0, 2048);
205
+ if (typeof part.url === 'string' && part.url.length > 0 && part.url.length <= 8000000 && part.url.startsWith('data:')) file.url = part.url;
206
+ return { type: 'file', id: part.id, index: part.index, ...file };
207
+ };
208
+
184
209
  const projectSessionDetail = (value) => {
185
210
  if (!value || typeof value !== 'object' || !Array.isArray(value.messages) || !Number.isSafeInteger(value.lastSequence)) throw protocolMismatch();
186
211
  const messages = value.messages.map((item) => {
@@ -226,6 +251,7 @@ const projectSessionDetail = (value) => {
226
251
  state: ['pending', 'running', 'completed', 'error', 'cancelled'].includes(part.state) ? part.state : 'completed',
227
252
  };
228
253
  }
254
+ if (part.type === 'file') return projectFilePart(part);
229
255
  throw protocolMismatch();
230
256
  });
231
257
  return { message: projected, parts };
@@ -246,6 +272,7 @@ const projectSessionDetail = (value) => {
246
272
  ...(compaction ? { compaction } : {}),
247
273
  ...(Number.isFinite(value.runStartedAt) ? { runStartedAt: Math.floor(value.runStartedAt) } : {}),
248
274
  ...(Number.isFinite(value.serverNow) ? { serverNow: Math.floor(value.serverNow) } : {}),
275
+ ...projectExtensionSnapshotState(value),
249
276
  };
250
277
  };
251
278
 
@@ -439,6 +466,48 @@ export const projectExtensionList = (result) => {
439
466
  return { directory: result.directory, extensions, commands };
440
467
  };
441
468
 
469
+ function projectExtensionSnapshotState(snapshot) {
470
+ const extensionStatuses = Array.isArray(snapshot.extensionStatuses)
471
+ ? snapshot.extensionStatuses.filter((entry) => entry && typeof entry.key === 'string' && typeof entry.text === 'string' && entry.key.length > 0 && entry.key.length <= 128 && entry.text.length <= 1000).slice(0, 50).map((entry) => ({ key: entry.key.slice(0, 128), text: entry.text.slice(0, 1000) }))
472
+ : undefined;
473
+ const extensionWidgets = Array.isArray(snapshot.extensionWidgets)
474
+ ? snapshot.extensionWidgets.filter((entry) => entry && typeof entry.key === 'string' && Array.isArray(entry.lines) && entry.key.length > 0 && entry.key.length <= 128 && entry.lines.length <= 100).slice(0, 50).map((entry) => ({
475
+ key: entry.key.slice(0, 128),
476
+ lines: entry.lines.filter((line) => typeof line === 'string').map((line) => line.slice(0, 2000)).slice(0, 100),
477
+ ...(entry.placement === 'belowEditor' ? { placement: 'belowEditor' } : { placement: 'aboveEditor' }),
478
+ }))
479
+ : undefined;
480
+ const extensionDialogs = Array.isArray(snapshot.extensionDialogs)
481
+ ? snapshot.extensionDialogs.filter((entry) => entry && typeof entry.requestId === 'string' && typeof entry.method === 'string' && typeof entry.title === 'string').slice(0, 20).map((entry) => ({
482
+ requestId: entry.requestId.slice(0, 512),
483
+ method: ['select', 'confirm', 'input', 'editor', 'form'].includes(entry.method) ? entry.method : 'confirm',
484
+ title: entry.title.slice(0, 512),
485
+ ...(typeof entry.message === 'string' ? { message: entry.message.slice(0, 2000) } : {}),
486
+ ...(Array.isArray(entry.options) ? { options: entry.options.filter((option) => typeof option === 'string').map((option) => option.slice(0, 256)).slice(0, 20) } : {}),
487
+ ...(typeof entry.placeholder === 'string' ? { placeholder: entry.placeholder.slice(0, 512) } : {}),
488
+ ...(typeof entry.prefill === 'string' ? { prefill: entry.prefill.slice(0, 10000) } : {}),
489
+ ...(Array.isArray(entry.fields) ? { fields: sanitizeExtensionFormFields(entry.fields) } : {}),
490
+ ...(Number.isFinite(entry.timeoutMs) ? { timeoutMs: Math.floor(entry.timeoutMs) } : {}),
491
+ }))
492
+ : undefined;
493
+ const extensionPanels = Array.isArray(snapshot.extensionPanels)
494
+ ? snapshot.extensionPanels.filter((entry) => entry && typeof entry.id === 'string' && entry.id.length > 0).slice(0, 24).map(projectExtensionPanelPayload)
495
+ : undefined;
496
+ const extensionApps = Array.isArray(snapshot.extensionApps)
497
+ ? snapshot.extensionApps.filter((entry) => entry && typeof entry.appId === 'string' && entry.appId.length > 0 && typeof entry.html === 'string' && entry.html.length > 0).slice(0, 8).map(projectExtensionAppPayload)
498
+ : undefined;
499
+ return {
500
+ ...(extensionStatuses ? { extensionStatuses } : {}),
501
+ ...(extensionWidgets ? { extensionWidgets } : {}),
502
+ ...(extensionDialogs ? { extensionDialogs } : {}),
503
+ ...(extensionPanels ? { extensionPanels } : {}),
504
+ ...(extensionApps ? { extensionApps } : {}),
505
+ ...(typeof snapshot.extensionTitle === 'string' && snapshot.extensionTitle.length > 0
506
+ ? { extensionTitle: snapshot.extensionTitle.replace(/[\u0000-\u001f\u007f]/g, ' ').trim().slice(0, 256) }
507
+ : {}),
508
+ };
509
+ }
510
+
442
511
  export const projectEventFrame = (frame) => {
443
512
  if (!frame || frame.kind !== 'event' || typeof frame.event !== 'string' || !Number.isSafeInteger(frame.sequence)
444
513
  || !frame.payload || typeof frame.payload.sessionId !== 'string' || typeof frame.payload.directory !== 'string') return null;
@@ -447,35 +516,7 @@ export const projectEventFrame = (frame) => {
447
516
  switch (frame.event) {
448
517
  case 'session.snapshot': {
449
518
  const snapshot = frame.payload;
450
- const snapshotExtensionStatuses = Array.isArray(snapshot.extensionStatuses)
451
- ? snapshot.extensionStatuses.filter((entry) => entry && typeof entry.key === 'string' && typeof entry.text === 'string' && entry.key.length > 0 && entry.key.length <= 128 && entry.text.length <= 1000).slice(0, 50).map((entry) => ({ key: entry.key.slice(0, 128), text: entry.text.slice(0, 1000) }))
452
- : undefined;
453
- const snapshotExtensionWidgets = Array.isArray(snapshot.extensionWidgets)
454
- ? snapshot.extensionWidgets.filter((entry) => entry && typeof entry.key === 'string' && Array.isArray(entry.lines) && entry.key.length > 0 && entry.key.length <= 128 && entry.lines.length <= 100).slice(0, 50).map((entry) => ({
455
- key: entry.key.slice(0, 128),
456
- lines: entry.lines.filter((line) => typeof line === 'string').map((line) => line.slice(0, 2000)).slice(0, 100),
457
- ...(entry.placement === 'belowEditor' ? { placement: 'belowEditor' } : { placement: 'aboveEditor' }),
458
- }))
459
- : undefined;
460
- const snapshotExtensionDialogs = Array.isArray(snapshot.extensionDialogs)
461
- ? snapshot.extensionDialogs.filter((entry) => entry && typeof entry.requestId === 'string' && typeof entry.method === 'string' && typeof entry.title === 'string').slice(0, 20).map((entry) => ({
462
- requestId: entry.requestId.slice(0, 512),
463
- method: ['select', 'confirm', 'input', 'editor', 'form'].includes(entry.method) ? entry.method : 'confirm',
464
- title: entry.title.slice(0, 512),
465
- ...(typeof entry.message === 'string' ? { message: entry.message.slice(0, 2000) } : {}),
466
- ...(Array.isArray(entry.options) ? { options: entry.options.filter((option) => typeof option === 'string').map((option) => option.slice(0, 256)).slice(0, 20) } : {}),
467
- ...(typeof entry.placeholder === 'string' ? { placeholder: entry.placeholder.slice(0, 512) } : {}),
468
- ...(typeof entry.prefill === 'string' ? { prefill: entry.prefill.slice(0, 10000) } : {}),
469
- ...(Array.isArray(entry.fields) ? { fields: sanitizeExtensionFormFields(entry.fields) } : {}),
470
- ...(Number.isFinite(entry.timeoutMs) ? { timeoutMs: Math.floor(entry.timeoutMs) } : {}),
471
- }))
472
- : undefined;
473
- const snapshotExtensionPanels = Array.isArray(snapshot.extensionPanels)
474
- ? snapshot.extensionPanels.filter((entry) => entry && typeof entry.id === 'string' && entry.id.length > 0).slice(0, 24).map(projectExtensionPanelPayload)
475
- : undefined;
476
- const snapshotExtensionApps = Array.isArray(snapshot.extensionApps)
477
- ? snapshot.extensionApps.filter((entry) => entry && typeof entry.appId === 'string' && entry.appId.length > 0 && typeof entry.html === 'string' && entry.html.length > 0).slice(0, 8).map(projectExtensionAppPayload)
478
- : undefined;
519
+ const extensionSnapshot = projectExtensionSnapshotState(snapshot);
479
520
  const lifecycle = ['idle', 'busy', 'retry', 'error', 'interrupted'].includes(snapshot.lifecycle) ? snapshot.lifecycle : 'idle';
480
521
  const retry = lifecycle === 'retry' ? projectRetryInfo(snapshot.retry) : null;
481
522
  const compaction = projectCompactionInfo(snapshot.compaction);
@@ -492,14 +533,7 @@ export const projectEventFrame = (frame) => {
492
533
  ...(Number.isFinite(snapshot.runStartedAt) ? { runStartedAt: Math.floor(snapshot.runStartedAt) } : {}),
493
534
  ...(Number.isFinite(snapshot.serverNow) ? { serverNow: Math.floor(snapshot.serverNow) } : {}),
494
535
  lastSequence: Number.isSafeInteger(snapshot.lastSequence) ? snapshot.lastSequence : frame.sequence,
495
- ...(snapshotExtensionStatuses ? { extensionStatuses: snapshotExtensionStatuses } : {}),
496
- ...(snapshotExtensionWidgets ? { extensionWidgets: snapshotExtensionWidgets } : {}),
497
- ...(snapshotExtensionDialogs ? { extensionDialogs: snapshotExtensionDialogs } : {}),
498
- ...(snapshotExtensionPanels ? { extensionPanels: snapshotExtensionPanels } : {}),
499
- ...(snapshotExtensionApps ? { extensionApps: snapshotExtensionApps } : {}),
500
- ...(typeof snapshot.extensionTitle === 'string' && snapshot.extensionTitle.length > 0
501
- ? { extensionTitle: snapshot.extensionTitle.replace(/[\u0000-\u001f\u007f]/g, ' ').trim().slice(0, 256) }
502
- : {}),
536
+ ...extensionSnapshot,
503
537
  } } };
504
538
  }
505
539
  case 'session.lifecycle': {
@@ -513,7 +547,15 @@ export const projectEventFrame = (frame) => {
513
547
  return { ...common, payload: { title } };
514
548
  }
515
549
  case 'session.tree.updated': return { ...common, payload: {} };
516
- case 'assistant.message.start': return { ...common, payload: { messageId: frame.payload.messageId, role: frame.payload.role, startedAt: frame.payload.startedAt, ...(typeof frame.payload.parentId === 'string' ? { parentId: frame.payload.parentId } : {}), ...(typeof frame.payload.text === 'string' ? { text: frame.payload.text } : {}), ...(frame.payload.model ? { model: frame.payload.model } : {}) } };
550
+ case 'assistant.message.start': {
551
+ const files = frame.payload.role === 'user' && Array.isArray(frame.payload.files)
552
+ ? frame.payload.files
553
+ .filter((part) => part && part.type === 'file' && typeof part.id === 'string' && Number.isSafeInteger(part.index))
554
+ .slice(0, 32)
555
+ .map(projectFilePart)
556
+ : [];
557
+ return { ...common, payload: { messageId: frame.payload.messageId, role: frame.payload.role, startedAt: frame.payload.startedAt, ...(typeof frame.payload.parentId === 'string' ? { parentId: frame.payload.parentId } : {}), ...(typeof frame.payload.text === 'string' ? { text: frame.payload.text } : {}), ...(files.length > 0 ? { files } : {}), ...(frame.payload.model ? { model: frame.payload.model } : {}) } };
558
+ }
517
559
  case 'assistant.message.delta':
518
560
  case 'assistant.thinking.delta': return { ...common, payload: { messageId: frame.payload.messageId, contentIndex: frame.payload.contentIndex, delta: frame.payload.delta, ...(typeof frame.payload.partId === 'string' ? { partId: frame.payload.partId } : {}) } };
519
561
  case 'assistant.message.end': return {
@@ -724,8 +766,11 @@ export const registerPiRuntimeRoutes = (app, {
724
766
  settingsStore = createPiSettingsStore(),
725
767
  sessionFoldersStore = createPiSessionFoldersStore(),
726
768
  uiSettingsStore = createPiUiSettingsStore(),
769
+ snippetsStore = createPiSnippetsStore(),
727
770
  listCustomThemes = listPiCustomThemes,
728
771
  updateChecker = checkForUpdates,
772
+ updateLauncher = launchUpdateCommand,
773
+ resolveUpdatePackageManager = resolveTrustedUpdatePackageManager,
729
774
  smallModelGenerator = async (input) => (await import('./small-model-generation.js')).generateWithSmallModel(input),
730
775
  eventHeartbeatMs = 15_000,
731
776
  }) => {
@@ -775,12 +820,31 @@ export const registerPiRuntimeRoutes = (app, {
775
820
  currentVersion: typeof req.query.currentVersion === 'string' ? req.query.currentVersion : undefined,
776
821
  appType: typeof req.query.appType === 'string' ? req.query.appType : undefined,
777
822
  platform: typeof req.query.platform === 'string' ? req.query.platform : undefined,
823
+ instanceMode: typeof req.query.instanceMode === 'string' ? req.query.instanceMode : undefined,
778
824
  }));
779
825
  } catch {
780
826
  res.status(503).json({ error: 'Update check unavailable' });
781
827
  }
782
828
  });
783
829
 
830
+ app.post('/api/pi/update-install', (_req, res) => {
831
+ const packageManager = resolveUpdatePackageManager();
832
+ if (!packageManager) {
833
+ res.status(409).json({
834
+ success: false,
835
+ error: 'This PiChamber copy is not a supported global package-manager install. Run: pichamber update',
836
+ });
837
+ return;
838
+ }
839
+
840
+ const result = updateLauncher();
841
+ if (!result.success) {
842
+ res.status(409).json({ success: false, error: result.error });
843
+ return;
844
+ }
845
+ res.json({ success: true, autoRestart: true });
846
+ });
847
+
784
848
  app.get('/api/pi/runtime', async (_req, res) => {
785
849
  const runtime = getPiSessionDaemonRuntime();
786
850
  if (!runtime) {
@@ -1022,14 +1086,206 @@ export const registerPiRuntimeRoutes = (app, {
1022
1086
  }
1023
1087
  });
1024
1088
 
1025
- app.get('/api/pi/resources', async (_req, res) => {
1089
+ app.get('/api/pi/resources', async (req, res) => {
1090
+ const directory = typeof req.query?.directory === 'string' && req.query.directory.length > 0
1091
+ ? req.query.directory
1092
+ : undefined;
1093
+ try {
1094
+ res.json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request(
1095
+ 'resources.list',
1096
+ directory ? { directory } : undefined,
1097
+ )));
1098
+ } catch (error) {
1099
+ writeDaemonError(res, error);
1100
+ }
1101
+ });
1102
+
1103
+ app.get('/api/pi/commands', async (req, res) => {
1104
+ const directory = typeof req.query?.directory === 'string' && req.query.directory.length > 0
1105
+ ? req.query.directory
1106
+ : undefined;
1026
1107
  try {
1027
- res.json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request('resources.list')));
1108
+ const runtime = getDaemonRuntime(getPiSessionDaemonRuntime);
1109
+ const [resources, extensionList] = await Promise.all([
1110
+ runtime.request('resources.list', { ...(directory ? { directory } : {}) }),
1111
+ runtime.request('extensions.list', { ...(directory ? { directory } : {}) }),
1112
+ ]);
1113
+ const projectedResources = projectResources(resources);
1114
+ const projectedExtensions = projectExtensionList(extensionList);
1115
+ // Pi SDK 0.84.1 `getCommands()` exposes skills as `skill:name`, prompts
1116
+ // as `name`, and extensions under their registered invocation name
1117
+ // (including Pi-generated `:suffix` disambiguation). Mirror that
1118
+ // executable identity here so `/` autocomplete inserts what Pi executes.
1119
+ // Precedence at execution time is extension > skill/prompt expansion,
1120
+ // with PiChamber-local system commands intercepting before Pi.
1121
+ const commands = [
1122
+ ...projectedExtensions.commands,
1123
+ ...projectedResources.prompts.map((prompt) => ({
1124
+ name: prompt.name,
1125
+ ...(typeof prompt.description === 'string' ? { description: prompt.description } : {}),
1126
+ source: 'prompt',
1127
+ ...(typeof prompt.location === 'string' ? { scope: prompt.location } : {}),
1128
+ })),
1129
+ ...projectedResources.skills.map((skill) => ({
1130
+ name: `skill:${skill.name}`,
1131
+ ...(typeof skill.description === 'string' ? { description: skill.description } : {}),
1132
+ source: 'skill',
1133
+ ...(typeof skill.location === 'string' ? { scope: skill.location } : {}),
1134
+ })),
1135
+ ];
1136
+ res.json({ directory: projectedExtensions.directory, commands });
1028
1137
  } catch (error) {
1029
1138
  writeDaemonError(res, error);
1030
1139
  }
1031
1140
  });
1032
1141
 
1142
+ const SNIPPET_NAME_PATTERN = /^[a-z0-9][a-z0-9_-]{0,63}$/i;
1143
+ const validSnippetAliases = (aliases) => aliases === undefined
1144
+ || (Array.isArray(aliases)
1145
+ && aliases.length <= 10
1146
+ && aliases.every((alias) => typeof alias === 'string' && SNIPPET_NAME_PATTERN.test(alias.trim())));
1147
+
1148
+ const projectSnippets = (snippets) => {
1149
+ if (!Array.isArray(snippets)) throw protocolMismatch();
1150
+ return {
1151
+ snippets: snippets.map((snippet) => {
1152
+ if (!snippet || typeof snippet !== 'object'
1153
+ || typeof snippet.id !== 'string' || snippet.id.length === 0
1154
+ || typeof snippet.name !== 'string' || snippet.name.length === 0
1155
+ || typeof snippet.content !== 'string'
1156
+ || (snippet.description !== undefined && typeof snippet.description !== 'string')
1157
+ || !Array.isArray(snippet.aliases)
1158
+ || (snippet.scope !== 'global' && snippet.scope !== 'project')) throw protocolMismatch();
1159
+ return {
1160
+ id: snippet.id,
1161
+ name: snippet.name,
1162
+ content: snippet.content,
1163
+ ...(typeof snippet.description === 'string' && snippet.description.length > 0 ? { description: snippet.description } : {}),
1164
+ aliases: snippet.aliases.filter((alias) => typeof alias === 'string'),
1165
+ scope: snippet.scope,
1166
+ ...(snippet.scope === 'project' && typeof snippet.directory === 'string' ? { directory: snippet.directory } : {}),
1167
+ ...(Number.isSafeInteger(snippet.createdAt) ? { createdAt: snippet.createdAt } : {}),
1168
+ ...(Number.isSafeInteger(snippet.updatedAt) ? { updatedAt: snippet.updatedAt } : {}),
1169
+ };
1170
+ }),
1171
+ };
1172
+ };
1173
+
1174
+ const snippetDirectoryFrom = (req) => {
1175
+ const queryDirectory = typeof req.query?.directory === 'string' && req.query.directory.length > 0
1176
+ ? req.query.directory
1177
+ : undefined;
1178
+ const bodyDirectory = typeof req.body?.directory === 'string' && req.body.directory.length > 0
1179
+ ? req.body.directory
1180
+ : undefined;
1181
+ return queryDirectory ?? bodyDirectory;
1182
+ };
1183
+
1184
+ const writeSnippetError = (res, error) => {
1185
+ const code = typeof error?.code === 'string' ? error.code : 'SNIPPETS_INVALID';
1186
+ if (code === 'SNIPPET_NOT_FOUND') {
1187
+ res.status(404).json({ error: { code } });
1188
+ return;
1189
+ }
1190
+ if (code === 'SNIPPETS_INVALID') {
1191
+ res.status(500).json({ error: { code } });
1192
+ return;
1193
+ }
1194
+ res.status(400).json({ error: { code } });
1195
+ };
1196
+
1197
+ app.get('/api/pi/snippets', async (req, res) => {
1198
+ try {
1199
+ res.json(projectSnippets(await snippetsStore.list(snippetDirectoryFrom(req))));
1200
+ } catch {
1201
+ res.status(500).json({ error: { code: 'SNIPPETS_INVALID' } });
1202
+ }
1203
+ });
1204
+
1205
+ app.post('/api/pi/snippets', async (req, res) => {
1206
+ const { name, content, description, aliases, scope, directory } = req.body ?? {};
1207
+ if (typeof name !== 'string' || typeof content !== 'string'
1208
+ || !SNIPPET_NAME_PATTERN.test(name.trim())
1209
+ || content.length === 0 || content.length > 200_000
1210
+ || (description !== undefined && (typeof description !== 'string' || description.length > 4_000))
1211
+ || !validSnippetAliases(aliases)
1212
+ || (scope !== undefined && scope !== 'global' && scope !== 'project')
1213
+ || (directory !== undefined && typeof directory !== 'string')) {
1214
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1215
+ return;
1216
+ }
1217
+ try {
1218
+ const snippets = await snippetsStore.create({
1219
+ name,
1220
+ content,
1221
+ ...(description !== undefined ? { description } : {}),
1222
+ ...(aliases !== undefined ? { aliases } : {}),
1223
+ scope: scope ?? 'global',
1224
+ ...(directory !== undefined ? { directory } : {}),
1225
+ });
1226
+ res.status(201).json(projectSnippets(snippets));
1227
+ } catch (error) {
1228
+ writeSnippetError(res, error);
1229
+ }
1230
+ });
1231
+
1232
+ app.put('/api/pi/snippets/:snippetId', async (req, res) => {
1233
+ const snippetId = req.params.snippetId;
1234
+ if (typeof snippetId !== 'string' || snippetId.length === 0 || snippetId.length > 128) {
1235
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1236
+ return;
1237
+ }
1238
+ const { content, description, aliases, name, scope, directory } = req.body ?? {};
1239
+ if ((content !== undefined && (typeof content !== 'string' || content.length === 0 || content.length > 200_000))
1240
+ || (description !== undefined && (typeof description !== 'string' || description.length > 4_000))
1241
+ || !validSnippetAliases(aliases)
1242
+ || (name !== undefined && typeof name !== 'string')
1243
+ || (scope !== undefined && scope !== 'global' && scope !== 'project')
1244
+ || (directory !== undefined && typeof directory !== 'string')) {
1245
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1246
+ return;
1247
+ }
1248
+ if (name !== undefined && !SNIPPET_NAME_PATTERN.test(name.trim())) {
1249
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1250
+ return;
1251
+ }
1252
+ try {
1253
+ res.json(projectSnippets(await snippetsStore.update(snippetId, {
1254
+ ...(name !== undefined ? { name } : {}),
1255
+ ...(content !== undefined ? { content } : {}),
1256
+ ...(description !== undefined ? { description } : {}),
1257
+ ...(aliases !== undefined ? { aliases } : {}),
1258
+ ...(scope !== undefined ? { scope } : {}),
1259
+ ...(directory !== undefined ? { directory } : {}),
1260
+ }, snippetDirectoryFrom(req))));
1261
+ } catch (error) {
1262
+ writeSnippetError(res, error);
1263
+ }
1264
+ });
1265
+
1266
+ app.delete('/api/pi/snippets/:snippetId', async (req, res) => {
1267
+ const snippetId = req.params.snippetId;
1268
+ if (typeof snippetId !== 'string' || snippetId.length === 0) {
1269
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1270
+ return;
1271
+ }
1272
+ try {
1273
+ res.json(projectSnippets(await snippetsStore.remove(snippetId, snippetDirectoryFrom(req))));
1274
+ } catch (error) {
1275
+ writeSnippetError(res, error);
1276
+ }
1277
+ });
1278
+
1279
+ const resourceDirectoryFrom = (req) => {
1280
+ const queryDirectory = typeof req.query?.directory === 'string' && req.query.directory.length > 0
1281
+ ? req.query.directory
1282
+ : undefined;
1283
+ const bodyDirectory = typeof req.body?.directory === 'string' && req.body.directory.length > 0
1284
+ ? req.body.directory
1285
+ : undefined;
1286
+ return queryDirectory ?? bodyDirectory;
1287
+ };
1288
+
1033
1289
  app.put('/api/pi/resources/:resourceId', async (req, res) => {
1034
1290
  const resourceId = req.params.resourceId;
1035
1291
  const content = req.body?.content;
@@ -1037,8 +1293,9 @@ export const registerPiRuntimeRoutes = (app, {
1037
1293
  res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1038
1294
  return;
1039
1295
  }
1296
+ const directory = resourceDirectoryFrom(req);
1040
1297
  try {
1041
- res.json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request('resources.update', { resourceId, content })));
1298
+ res.json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request('resources.update', { resourceId, content, ...(directory ? { directory } : {}) })));
1042
1299
  } catch (error) {
1043
1300
  writeDaemonError(res, error);
1044
1301
  }
@@ -1050,8 +1307,50 @@ export const registerPiRuntimeRoutes = (app, {
1050
1307
  res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1051
1308
  return;
1052
1309
  }
1310
+ if (!/^[a-z0-9][a-z0-9_-]{0,63}$/i.test(name)) {
1311
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1312
+ return;
1313
+ }
1314
+ const directory = resourceDirectoryFrom(req);
1053
1315
  try {
1054
- res.status(201).json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request('resources.prompts.create', { name, description, content, location })));
1316
+ res.status(201).json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request('resources.prompts.create', { name, description, content, location, ...(directory ? { directory } : {}) })));
1317
+ } catch (error) {
1318
+ writeDaemonError(res, error);
1319
+ }
1320
+ });
1321
+
1322
+ app.put('/api/pi/resources/prompts/:resourceId', async (req, res) => {
1323
+ const resourceId = req.params.resourceId;
1324
+ if (typeof resourceId !== 'string' || resourceId.length === 0) {
1325
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1326
+ return;
1327
+ }
1328
+ const { name, description, content, location } = req.body ?? {};
1329
+ if ((name !== undefined && typeof name !== 'string')
1330
+ || (description !== undefined && typeof description !== 'string')
1331
+ || (content !== undefined && typeof content !== 'string')
1332
+ || (location !== undefined && location !== 'global' && location !== 'project')) {
1333
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1334
+ return;
1335
+ }
1336
+ if (name !== undefined && !/^[a-z0-9][a-z0-9_-]{0,63}$/i.test(name)) {
1337
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1338
+ return;
1339
+ }
1340
+ if (name === undefined && description === undefined && content === undefined && location === undefined) {
1341
+ res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1342
+ return;
1343
+ }
1344
+ const directory = resourceDirectoryFrom(req);
1345
+ try {
1346
+ res.json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request('resources.prompts.update', {
1347
+ resourceId,
1348
+ ...(directory ? { directory } : {}),
1349
+ ...(name !== undefined ? { name } : {}),
1350
+ ...(description !== undefined ? { description } : {}),
1351
+ ...(content !== undefined ? { content } : {}),
1352
+ ...(location !== undefined ? { location } : {}),
1353
+ })));
1055
1354
  } catch (error) {
1056
1355
  writeDaemonError(res, error);
1057
1356
  }
@@ -1063,8 +1362,9 @@ export const registerPiRuntimeRoutes = (app, {
1063
1362
  res.status(400).json({ error: { code: 'INVALID_ARGUMENT' } });
1064
1363
  return;
1065
1364
  }
1365
+ const directory = resourceDirectoryFrom(req);
1066
1366
  try {
1067
- res.json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request('resources.prompts.delete', { resourceId })));
1367
+ res.json(projectResources(await getDaemonRuntime(getPiSessionDaemonRuntime).request('resources.prompts.delete', { resourceId, ...(directory ? { directory } : {}) })));
1068
1368
  } catch (error) {
1069
1369
  writeDaemonError(res, error);
1070
1370
  }
@@ -1292,10 +1592,12 @@ export const registerPiRuntimeRoutes = (app, {
1292
1592
  for (const [suffix, command] of [['prompt', 'sessions.prompt'], ['steer', 'sessions.steer'], ['follow-up', 'sessions.followUp']]) {
1293
1593
  app.post(`/api/pi/sessions/:sessionId/${suffix}`, async (req, res) => {
1294
1594
  let payload = req.body && typeof req.body === 'object' ? req.body : {};
1595
+ let attachmentIds = [];
1295
1596
  try {
1296
1597
  if (payload.attachments !== undefined) {
1297
1598
  if (!Array.isArray(payload.attachments) || payload.attachments.some((attachment) => !attachment || typeof attachment.id !== 'string')) throw protocolMismatch();
1298
- const attachments = await attachmentStore.resolve(payload.attachments.map((attachment) => attachment.id));
1599
+ attachmentIds = payload.attachments.map((attachment) => attachment.id);
1600
+ const attachments = await attachmentStore.resolve(attachmentIds);
1299
1601
  payload = { ...payload, attachments };
1300
1602
  }
1301
1603
  } catch (error) {
@@ -1308,6 +1610,7 @@ export const registerPiRuntimeRoutes = (app, {
1308
1610
  writeDaemonError(res, protocolMismatch());
1309
1611
  return;
1310
1612
  }
1613
+ await attachmentStore.consume?.(attachmentIds);
1311
1614
  res.status(202).json({ accepted: true, messageId: result.messageId });
1312
1615
  }
1313
1616
  });
@@ -1332,10 +1635,41 @@ export const registerPiRuntimeRoutes = (app, {
1332
1635
 
1333
1636
  app.post('/api/pi/attachments', async (req, res) => {
1334
1637
  try {
1335
- const attachment = await attachmentStore.create(req.body ?? {});
1638
+ const isBinary = req.is('application/octet-stream');
1639
+ let attachment;
1640
+ if (isBinary) {
1641
+ const encodedFilename = req.get('x-pichamber-filename');
1642
+ const mime = req.get('x-pichamber-mime');
1643
+ if (!encodedFilename || encodedFilename.length > 2_048 || !mime || mime.length > 200) throw Object.assign(new Error('Invalid attachment metadata'), { code: 'ATTACHMENT_FAILED' });
1644
+ let filename;
1645
+ try {
1646
+ filename = decodeURIComponent(encodedFilename);
1647
+ } catch {
1648
+ throw Object.assign(new Error('Invalid attachment metadata'), { code: 'ATTACHMENT_FAILED' });
1649
+ }
1650
+ attachment = await attachmentStore.createFromStream({ filename, mime, stream: req });
1651
+ } else {
1652
+ attachment = await attachmentStore.create(req.body ?? {});
1653
+ }
1336
1654
  if (!attachment || typeof attachment.id !== 'string' || typeof attachment.name !== 'string'
1337
- || typeof attachment.mime !== 'string' || !Number.isSafeInteger(attachment.size)) throw protocolMismatch();
1338
- res.status(201).json({ attachment: { id: attachment.id, name: attachment.name, mime: attachment.mime, size: attachment.size } });
1655
+ || typeof attachment.mime !== 'string' || !Number.isSafeInteger(attachment.size)
1656
+ || !Number.isFinite(attachment.expiresAt)) throw protocolMismatch();
1657
+ res.status(201).json({ attachment: {
1658
+ id: attachment.id,
1659
+ name: attachment.name,
1660
+ mime: attachment.mime,
1661
+ size: attachment.size,
1662
+ expiresAt: attachment.expiresAt,
1663
+ } });
1664
+ } catch (error) {
1665
+ writeDaemonError(res, error);
1666
+ }
1667
+ });
1668
+
1669
+ app.delete('/api/pi/attachments/:attachmentId', async (req, res) => {
1670
+ try {
1671
+ await attachmentStore.remove(req.params.attachmentId);
1672
+ res.status(204).end();
1339
1673
  } catch (error) {
1340
1674
  writeDaemonError(res, error);
1341
1675
  }