@jadelclemens/pi-forge 0.0.0-stage → 0.6.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 (440) hide show
  1. package/CHANGELOG.md +486 -0
  2. package/LICENSE +21 -0
  3. package/PUBLIC_API.md +5 -0
  4. package/README.md +204 -2
  5. package/README.zh-CN.md +128 -0
  6. package/SUBAGENT_ADAPTER_CONTRACT.md +7 -0
  7. package/dist/active-state.d.ts +148 -0
  8. package/dist/active-state.d.ts.map +1 -0
  9. package/dist/active-state.js +374 -0
  10. package/dist/active-state.js.map +1 -0
  11. package/dist/agent-profile.d.ts +51 -0
  12. package/dist/agent-profile.d.ts.map +1 -0
  13. package/dist/agent-profile.js +197 -0
  14. package/dist/agent-profile.js.map +1 -0
  15. package/dist/capabilities.d.ts +39 -0
  16. package/dist/capabilities.d.ts.map +1 -0
  17. package/dist/capabilities.js +160 -0
  18. package/dist/capabilities.js.map +1 -0
  19. package/dist/capability-anchors.d.ts +45 -0
  20. package/dist/capability-anchors.d.ts.map +1 -0
  21. package/dist/capability-anchors.js +263 -0
  22. package/dist/capability-anchors.js.map +1 -0
  23. package/dist/capability-command.d.ts +4 -0
  24. package/dist/capability-command.d.ts.map +1 -0
  25. package/dist/capability-command.js +164 -0
  26. package/dist/capability-command.js.map +1 -0
  27. package/dist/capability-events.d.ts +79 -0
  28. package/dist/capability-events.d.ts.map +1 -0
  29. package/dist/capability-events.js +478 -0
  30. package/dist/capability-events.js.map +1 -0
  31. package/dist/capability-projection.d.ts +22 -0
  32. package/dist/capability-projection.d.ts.map +1 -0
  33. package/dist/capability-projection.js +273 -0
  34. package/dist/capability-projection.js.map +1 -0
  35. package/dist/capability-protocol.d.ts +21 -0
  36. package/dist/capability-protocol.d.ts.map +1 -0
  37. package/dist/capability-protocol.js +15 -0
  38. package/dist/capability-protocol.js.map +1 -0
  39. package/dist/capability-state.d.ts +74 -0
  40. package/dist/capability-state.d.ts.map +1 -0
  41. package/dist/capability-state.js +43 -0
  42. package/dist/capability-state.js.map +1 -0
  43. package/dist/capability-tool.d.ts +11 -0
  44. package/dist/capability-tool.d.ts.map +1 -0
  45. package/dist/capability-tool.js +25 -0
  46. package/dist/capability-tool.js.map +1 -0
  47. package/dist/capability-web-host.d.ts +11 -0
  48. package/dist/capability-web-host.d.ts.map +1 -0
  49. package/dist/capability-web-host.js +105 -0
  50. package/dist/capability-web-host.js.map +1 -0
  51. package/dist/catalog.d.ts +27 -0
  52. package/dist/catalog.d.ts.map +1 -0
  53. package/dist/catalog.js +59 -0
  54. package/dist/catalog.js.map +1 -0
  55. package/dist/codecs/agent-profile.d.ts +53 -0
  56. package/dist/codecs/agent-profile.d.ts.map +1 -0
  57. package/dist/codecs/agent-profile.js +176 -0
  58. package/dist/codecs/agent-profile.js.map +1 -0
  59. package/dist/codecs/capability.d.ts +71 -0
  60. package/dist/codecs/capability.d.ts.map +1 -0
  61. package/dist/codecs/capability.js +363 -0
  62. package/dist/codecs/capability.js.map +1 -0
  63. package/dist/codecs/index.d.ts +5 -0
  64. package/dist/codecs/index.d.ts.map +1 -0
  65. package/dist/codecs/index.js +3 -0
  66. package/dist/codecs/index.js.map +1 -0
  67. package/dist/codecs/prompt-stack.d.ts +18 -0
  68. package/dist/codecs/prompt-stack.d.ts.map +1 -0
  69. package/dist/codecs/prompt-stack.js +623 -0
  70. package/dist/codecs/prompt-stack.js.map +1 -0
  71. package/dist/command-contribution/index.d.ts +21 -0
  72. package/dist/command-contribution/index.d.ts.map +1 -0
  73. package/dist/command-contribution/index.js +14 -0
  74. package/dist/command-contribution/index.js.map +1 -0
  75. package/dist/compile-cycle.d.ts +26 -0
  76. package/dist/compile-cycle.d.ts.map +1 -0
  77. package/dist/compile-cycle.js +16 -0
  78. package/dist/compile-cycle.js.map +1 -0
  79. package/dist/compiler.d.ts +22 -0
  80. package/dist/compiler.d.ts.map +1 -0
  81. package/dist/compiler.js +737 -0
  82. package/dist/compiler.js.map +1 -0
  83. package/dist/context-diff-history.d.ts +69 -0
  84. package/dist/context-diff-history.d.ts.map +1 -0
  85. package/dist/context-diff-history.js +107 -0
  86. package/dist/context-diff-history.js.map +1 -0
  87. package/dist/context-diff-snapshot.d.ts +19 -0
  88. package/dist/context-diff-snapshot.d.ts.map +1 -0
  89. package/dist/context-diff-snapshot.js +146 -0
  90. package/dist/context-diff-snapshot.js.map +1 -0
  91. package/dist/context-diff.d.ts +70 -0
  92. package/dist/context-diff.d.ts.map +1 -0
  93. package/dist/context-diff.js +259 -0
  94. package/dist/context-diff.js.map +1 -0
  95. package/dist/extension-registry.d.ts +25 -0
  96. package/dist/extension-registry.d.ts.map +1 -0
  97. package/dist/extension-registry.js +9 -0
  98. package/dist/extension-registry.js.map +1 -0
  99. package/dist/forge-command.d.ts +10 -0
  100. package/dist/forge-command.d.ts.map +1 -0
  101. package/dist/forge-command.js +106 -0
  102. package/dist/forge-command.js.map +1 -0
  103. package/dist/forge-config.d.ts +9 -0
  104. package/dist/forge-config.d.ts.map +1 -0
  105. package/dist/forge-config.js +15 -0
  106. package/dist/forge-config.js.map +1 -0
  107. package/dist/forge-extensions.d.ts +28 -0
  108. package/dist/forge-extensions.d.ts.map +1 -0
  109. package/dist/forge-extensions.js +198 -0
  110. package/dist/forge-extensions.js.map +1 -0
  111. package/dist/forge-v1/analyzer.d.ts +3 -0
  112. package/dist/forge-v1/analyzer.d.ts.map +1 -0
  113. package/dist/forge-v1/analyzer.js +50 -0
  114. package/dist/forge-v1/analyzer.js.map +1 -0
  115. package/dist/forge-v1/index.d.ts +6 -0
  116. package/dist/forge-v1/index.d.ts.map +1 -0
  117. package/dist/forge-v1/index.js +13 -0
  118. package/dist/forge-v1/index.js.map +1 -0
  119. package/dist/forge-v1/parser.d.ts +3 -0
  120. package/dist/forge-v1/parser.d.ts.map +1 -0
  121. package/dist/forge-v1/parser.js +186 -0
  122. package/dist/forge-v1/parser.js.map +1 -0
  123. package/dist/forge-v1/renderer.d.ts +9 -0
  124. package/dist/forge-v1/renderer.d.ts.map +1 -0
  125. package/dist/forge-v1/renderer.js +181 -0
  126. package/dist/forge-v1/renderer.js.map +1 -0
  127. package/dist/forge-v1/types.d.ts +87 -0
  128. package/dist/forge-v1/types.d.ts.map +1 -0
  129. package/dist/forge-v1/types.js +4 -0
  130. package/dist/forge-v1/types.js.map +1 -0
  131. package/dist/host-compatibility.d.ts +13 -0
  132. package/dist/host-compatibility.d.ts.map +1 -0
  133. package/dist/host-compatibility.js +41 -0
  134. package/dist/host-compatibility.js.map +1 -0
  135. package/dist/index.d.ts +15 -0
  136. package/dist/index.d.ts.map +1 -0
  137. package/dist/index.js +183 -0
  138. package/dist/index.js.map +1 -0
  139. package/dist/json-fingerprint.d.ts +11 -0
  140. package/dist/json-fingerprint.d.ts.map +1 -0
  141. package/dist/json-fingerprint.js +66 -0
  142. package/dist/json-fingerprint.js.map +1 -0
  143. package/dist/lifecycle.d.ts +46 -0
  144. package/dist/lifecycle.d.ts.map +1 -0
  145. package/dist/lifecycle.js +330 -0
  146. package/dist/lifecycle.js.map +1 -0
  147. package/dist/loader.d.ts +21 -0
  148. package/dist/loader.d.ts.map +1 -0
  149. package/dist/loader.js +58 -0
  150. package/dist/loader.js.map +1 -0
  151. package/dist/macro-engine.d.ts +18 -0
  152. package/dist/macro-engine.d.ts.map +1 -0
  153. package/dist/macro-engine.js +29 -0
  154. package/dist/macro-engine.js.map +1 -0
  155. package/dist/payload-capture.d.ts +31 -0
  156. package/dist/payload-capture.d.ts.map +1 -0
  157. package/dist/payload-capture.js +253 -0
  158. package/dist/payload-capture.js.map +1 -0
  159. package/dist/payload-command.d.ts +12 -0
  160. package/dist/payload-command.d.ts.map +1 -0
  161. package/dist/payload-command.js +197 -0
  162. package/dist/payload-command.js.map +1 -0
  163. package/dist/payload-state.d.ts +20 -0
  164. package/dist/payload-state.d.ts.map +1 -0
  165. package/dist/payload-state.js +20 -0
  166. package/dist/payload-state.js.map +1 -0
  167. package/dist/policy.d.ts +7 -0
  168. package/dist/policy.d.ts.map +1 -0
  169. package/dist/policy.js +41 -0
  170. package/dist/policy.js.map +1 -0
  171. package/dist/preset-command.d.ts +19 -0
  172. package/dist/preset-command.d.ts.map +1 -0
  173. package/dist/preset-command.js +261 -0
  174. package/dist/preset-command.js.map +1 -0
  175. package/dist/preview-inspection.d.ts +68 -0
  176. package/dist/preview-inspection.d.ts.map +1 -0
  177. package/dist/preview-inspection.js +293 -0
  178. package/dist/preview-inspection.js.map +1 -0
  179. package/dist/preview-text.d.ts +5 -0
  180. package/dist/preview-text.d.ts.map +1 -0
  181. package/dist/preview-text.js +27 -0
  182. package/dist/preview-text.js.map +1 -0
  183. package/dist/preview.d.ts +32 -0
  184. package/dist/preview.d.ts.map +1 -0
  185. package/dist/preview.js +272 -0
  186. package/dist/preview.js.map +1 -0
  187. package/dist/profile-command.d.ts +17 -0
  188. package/dist/profile-command.d.ts.map +1 -0
  189. package/dist/profile-command.js +385 -0
  190. package/dist/profile-command.js.map +1 -0
  191. package/dist/profile-service.d.ts +107 -0
  192. package/dist/profile-service.d.ts.map +1 -0
  193. package/dist/profile-service.js +248 -0
  194. package/dist/profile-service.js.map +1 -0
  195. package/dist/prompt-analysis.d.ts +24 -0
  196. package/dist/prompt-analysis.d.ts.map +1 -0
  197. package/dist/prompt-analysis.js +84 -0
  198. package/dist/prompt-analysis.js.map +1 -0
  199. package/dist/prompt-cache-warning.d.ts +23 -0
  200. package/dist/prompt-cache-warning.d.ts.map +1 -0
  201. package/dist/prompt-cache-warning.js +74 -0
  202. package/dist/prompt-cache-warning.js.map +1 -0
  203. package/dist/prompt-runtime.d.ts +19 -0
  204. package/dist/prompt-runtime.d.ts.map +1 -0
  205. package/dist/prompt-runtime.js +34 -0
  206. package/dist/prompt-runtime.js.map +1 -0
  207. package/dist/regex.d.ts +19 -0
  208. package/dist/regex.d.ts.map +1 -0
  209. package/dist/regex.js +507 -0
  210. package/dist/regex.js.map +1 -0
  211. package/dist/render-helpers.d.ts +38 -0
  212. package/dist/render-helpers.d.ts.map +1 -0
  213. package/dist/render-helpers.js +85 -0
  214. package/dist/render-helpers.js.map +1 -0
  215. package/dist/repositories/agent-profile.d.ts +36 -0
  216. package/dist/repositories/agent-profile.d.ts.map +1 -0
  217. package/dist/repositories/agent-profile.js +153 -0
  218. package/dist/repositories/agent-profile.js.map +1 -0
  219. package/dist/repositories/capability.d.ts +53 -0
  220. package/dist/repositories/capability.d.ts.map +1 -0
  221. package/dist/repositories/capability.js +294 -0
  222. package/dist/repositories/capability.js.map +1 -0
  223. package/dist/repositories/index.d.ts +3 -0
  224. package/dist/repositories/index.d.ts.map +1 -0
  225. package/dist/repositories/index.js +3 -0
  226. package/dist/repositories/index.js.map +1 -0
  227. package/dist/repositories/prompt-stack.d.ts +57 -0
  228. package/dist/repositories/prompt-stack.d.ts.map +1 -0
  229. package/dist/repositories/prompt-stack.js +175 -0
  230. package/dist/repositories/prompt-stack.js.map +1 -0
  231. package/dist/resource-identity.d.ts +33 -0
  232. package/dist/resource-identity.d.ts.map +1 -0
  233. package/dist/resource-identity.js +56 -0
  234. package/dist/resource-identity.js.map +1 -0
  235. package/dist/runtime/capability-runtime.d.ts +95 -0
  236. package/dist/runtime/capability-runtime.d.ts.map +1 -0
  237. package/dist/runtime/capability-runtime.js +991 -0
  238. package/dist/runtime/capability-runtime.js.map +1 -0
  239. package/dist/runtime/profile-runtime.d.ts +14 -0
  240. package/dist/runtime/profile-runtime.d.ts.map +1 -0
  241. package/dist/runtime/profile-runtime.js +56 -0
  242. package/dist/runtime/profile-runtime.js.map +1 -0
  243. package/dist/runtime/prompt-stack-runtime.d.ts +27 -0
  244. package/dist/runtime/prompt-stack-runtime.d.ts.map +1 -0
  245. package/dist/runtime/prompt-stack-runtime.js +149 -0
  246. package/dist/runtime/prompt-stack-runtime.js.map +1 -0
  247. package/dist/runtime/tool-policy-runtime.d.ts +25 -0
  248. package/dist/runtime/tool-policy-runtime.d.ts.map +1 -0
  249. package/dist/runtime/tool-policy-runtime.js +386 -0
  250. package/dist/runtime/tool-policy-runtime.js.map +1 -0
  251. package/dist/runtime/web-editor-runtime.d.ts +11 -0
  252. package/dist/runtime/web-editor-runtime.d.ts.map +1 -0
  253. package/dist/runtime/web-editor-runtime.js +141 -0
  254. package/dist/runtime/web-editor-runtime.js.map +1 -0
  255. package/dist/session-adapter.d.ts +30 -0
  256. package/dist/session-adapter.d.ts.map +1 -0
  257. package/dist/session-adapter.js +167 -0
  258. package/dist/session-adapter.js.map +1 -0
  259. package/dist/session-state.d.ts +23 -0
  260. package/dist/session-state.d.ts.map +1 -0
  261. package/dist/session-state.js +44 -0
  262. package/dist/session-state.js.map +1 -0
  263. package/dist/session-usage.d.ts +65 -0
  264. package/dist/session-usage.d.ts.map +1 -0
  265. package/dist/session-usage.js +134 -0
  266. package/dist/session-usage.js.map +1 -0
  267. package/dist/slot-renderers.d.ts +38 -0
  268. package/dist/slot-renderers.d.ts.map +1 -0
  269. package/dist/slot-renderers.js +285 -0
  270. package/dist/slot-renderers.js.map +1 -0
  271. package/dist/stack-migration.d.ts +35 -0
  272. package/dist/stack-migration.d.ts.map +1 -0
  273. package/dist/stack-migration.js +123 -0
  274. package/dist/stack-migration.js.map +1 -0
  275. package/dist/status-text.d.ts +6 -0
  276. package/dist/status-text.d.ts.map +1 -0
  277. package/dist/status-text.js +11 -0
  278. package/dist/status-text.js.map +1 -0
  279. package/dist/storage.d.ts +24 -0
  280. package/dist/storage.d.ts.map +1 -0
  281. package/dist/storage.js +94 -0
  282. package/dist/storage.js.map +1 -0
  283. package/dist/subagent/fingerprints.d.ts +12 -0
  284. package/dist/subagent/fingerprints.d.ts.map +1 -0
  285. package/dist/subagent/fingerprints.js +17 -0
  286. package/dist/subagent/fingerprints.js.map +1 -0
  287. package/dist/subagent/host-port.d.ts +296 -0
  288. package/dist/subagent/host-port.d.ts.map +1 -0
  289. package/dist/subagent/host-port.js +560 -0
  290. package/dist/subagent/host-port.js.map +1 -0
  291. package/dist/subagent/index.d.ts +16 -0
  292. package/dist/subagent/index.d.ts.map +1 -0
  293. package/dist/subagent/index.js +15 -0
  294. package/dist/subagent/index.js.map +1 -0
  295. package/dist/subagent-host.d.ts +125 -0
  296. package/dist/subagent-host.d.ts.map +1 -0
  297. package/dist/subagent-host.js +327 -0
  298. package/dist/subagent-host.js.map +1 -0
  299. package/dist/template-render.d.ts +18 -0
  300. package/dist/template-render.d.ts.map +1 -0
  301. package/dist/template-render.js +232 -0
  302. package/dist/template-render.js.map +1 -0
  303. package/dist/types.d.ts +213 -0
  304. package/dist/types.d.ts.map +1 -0
  305. package/dist/types.js +2 -0
  306. package/dist/types.js.map +1 -0
  307. package/dist/ui-contribution/contrib-port.d.ts +170 -0
  308. package/dist/ui-contribution/contrib-port.d.ts.map +1 -0
  309. package/dist/ui-contribution/contrib-port.js +640 -0
  310. package/dist/ui-contribution/contrib-port.js.map +1 -0
  311. package/dist/ui-contribution/index.d.ts +3 -0
  312. package/dist/ui-contribution/index.d.ts.map +1 -0
  313. package/dist/ui-contribution/index.js +2 -0
  314. package/dist/ui-contribution/index.js.map +1 -0
  315. package/dist/web-editor/client-script.d.ts +2 -0
  316. package/dist/web-editor/client-script.d.ts.map +1 -0
  317. package/dist/web-editor/client-script.generated.d.ts +2 -0
  318. package/dist/web-editor/client-script.generated.d.ts.map +1 -0
  319. package/dist/web-editor/client-script.generated.js +3 -0
  320. package/dist/web-editor/client-script.generated.js.map +1 -0
  321. package/dist/web-editor/client-script.js +2 -0
  322. package/dist/web-editor/client-script.js.map +1 -0
  323. package/dist/web-editor/client-styles.d.ts +2 -0
  324. package/dist/web-editor/client-styles.d.ts.map +1 -0
  325. package/dist/web-editor/client-styles.generated.d.ts +2 -0
  326. package/dist/web-editor/client-styles.generated.d.ts.map +1 -0
  327. package/dist/web-editor/client-styles.generated.js +3 -0
  328. package/dist/web-editor/client-styles.generated.js.map +1 -0
  329. package/dist/web-editor/client-styles.js +2 -0
  330. package/dist/web-editor/client-styles.js.map +1 -0
  331. package/dist/web-editor/contrib-service.d.ts +41 -0
  332. package/dist/web-editor/contrib-service.d.ts.map +1 -0
  333. package/dist/web-editor/contrib-service.js +174 -0
  334. package/dist/web-editor/contrib-service.js.map +1 -0
  335. package/dist/web-editor/index.d.ts +3 -0
  336. package/dist/web-editor/index.d.ts.map +1 -0
  337. package/dist/web-editor/index.js +2 -0
  338. package/dist/web-editor/index.js.map +1 -0
  339. package/dist/web-editor/line-diff.d.ts +28 -0
  340. package/dist/web-editor/line-diff.d.ts.map +1 -0
  341. package/dist/web-editor/line-diff.js +216 -0
  342. package/dist/web-editor/line-diff.js.map +1 -0
  343. package/dist/web-editor/page.d.ts +4 -0
  344. package/dist/web-editor/page.d.ts.map +1 -0
  345. package/dist/web-editor/page.js +26 -0
  346. package/dist/web-editor/page.js.map +1 -0
  347. package/dist/web-editor/schema-form.d.ts +63 -0
  348. package/dist/web-editor/schema-form.d.ts.map +1 -0
  349. package/dist/web-editor/schema-form.js +213 -0
  350. package/dist/web-editor/schema-form.js.map +1 -0
  351. package/dist/web-editor/server.d.ts +4 -0
  352. package/dist/web-editor/server.d.ts.map +1 -0
  353. package/dist/web-editor/server.js +637 -0
  354. package/dist/web-editor/server.js.map +1 -0
  355. package/dist/web-editor/styles.d.ts +2 -0
  356. package/dist/web-editor/styles.d.ts.map +1 -0
  357. package/dist/web-editor/styles.js +1744 -0
  358. package/dist/web-editor/styles.js.map +1 -0
  359. package/dist/web-editor/types.d.ts +304 -0
  360. package/dist/web-editor/types.d.ts.map +1 -0
  361. package/dist/web-editor/types.js +2 -0
  362. package/dist/web-editor/types.js.map +1 -0
  363. package/dist/web-host.d.ts +56 -0
  364. package/dist/web-host.d.ts.map +1 -0
  365. package/dist/web-host.js +750 -0
  366. package/dist/web-host.js.map +1 -0
  367. package/dist/workspace.d.ts +85 -0
  368. package/dist/workspace.d.ts.map +1 -0
  369. package/dist/workspace.js +409 -0
  370. package/dist/workspace.js.map +1 -0
  371. package/docs/README.md +49 -0
  372. package/docs/concepts/agent-profiles.md +62 -0
  373. package/docs/concepts/prompt-stacks.md +99 -0
  374. package/docs/design/0.5.1-plan.md +237 -0
  375. package/docs/design/README.md +37 -0
  376. package/docs/design/architecture-0.5.md +254 -0
  377. package/docs/design/archive/0.5-full-proposal/0.5-consumer-audit.md +62 -0
  378. package/docs/design/archive/0.5-full-proposal/0.5-inventory.md +208 -0
  379. package/docs/design/archive/0.5-full-proposal/0.5-phase0-decision-drafts.md +237 -0
  380. package/docs/design/archive/0.5-full-proposal/README.md +12 -0
  381. package/docs/design/archive/0.5-full-proposal/architecture-0.5.md +423 -0
  382. package/docs/design/archive/0.5-full-proposal/host-discovery-spike.md +103 -0
  383. package/docs/design/archive/0.5-full-proposal/template-language-spike.md +156 -0
  384. package/docs/design/archive/2026-09-12-system-update-design.md +229 -0
  385. package/docs/design/context-diff-plan.md +78 -0
  386. package/docs/design/decision-template.md +33 -0
  387. package/docs/design/pi-forge-system-update-design-notes.md +157 -0
  388. package/docs/design/roadmap-0.4-archive.md +216 -0
  389. package/docs/design/subagents/design-review.md +220 -0
  390. package/docs/design/subagents/interface-design.md +274 -0
  391. package/docs/design/subagents/sdk-spike-findings.md +117 -0
  392. package/docs/development/architecture-rules.md +148 -0
  393. package/docs/development/complexity-review.md +86 -0
  394. package/docs/development/release.md +59 -0
  395. package/docs/development/roadmap.md +117 -0
  396. package/docs/development/scoped-global-profiles-stacks.md +325 -0
  397. package/docs/development/setup.md +99 -0
  398. package/docs/getting-started.md +96 -0
  399. package/docs/guides/custom-macros-and-slots.md +69 -0
  400. package/docs/guides/debugging.md +39 -0
  401. package/docs/guides/delegation.md +105 -0
  402. package/docs/guides/migrating-to-0.5.md +136 -0
  403. package/docs/guides/pi-1-compatibility.md +160 -0
  404. package/docs/guides/use-cases.md +67 -0
  405. package/docs/guides/web-editor.md +161 -0
  406. package/docs/reference/active-state.md +77 -0
  407. package/docs/reference/capabilities.md +288 -0
  408. package/docs/reference/commands.md +89 -0
  409. package/docs/reference/configuration.md +70 -0
  410. package/docs/reference/features.md +305 -0
  411. package/docs/reference/macros-and-slots.md +103 -0
  412. package/docs/reference/provider-support.md +67 -0
  413. package/docs/reference/public-api.md +133 -0
  414. package/docs/reference/session-cache.md +112 -0
  415. package/docs/reference/stack-schema.md +243 -0
  416. package/docs/reference/subagent-host-port.md +57 -0
  417. package/docs/reference/ui-contribution-port.md +51 -0
  418. package/docs/zh-CN/README.md +41 -0
  419. package/docs/zh-CN/concepts/agent-profiles.md +48 -0
  420. package/docs/zh-CN/concepts/prompt-stacks.md +48 -0
  421. package/docs/zh-CN/getting-started.md +82 -0
  422. package/docs/zh-CN/guides/delegation.md +76 -0
  423. package/docs/zh-CN/guides/migrating-to-0.5.md +121 -0
  424. package/docs/zh-CN/guides/pi-1-compatibility.md +158 -0
  425. package/docs/zh-CN/guides/web-editor.md +134 -0
  426. package/docs/zh-CN/reference/capabilities.md +288 -0
  427. package/docs/zh-CN/reference/commands.md +89 -0
  428. package/docs/zh-CN/reference/provider-support.md +67 -0
  429. package/docs/zh-CN/reference/session-cache.md +112 -0
  430. package/examples/capabilities/review.json +12 -0
  431. package/examples/capabilities/write-tools.json +15 -0
  432. package/examples/custom-system-status-extension/index.ts +144 -0
  433. package/examples/custom-system-status-extension/prompt-stack.json +40 -0
  434. package/examples/default-prompt-stack.json +125 -0
  435. package/examples/fake-assistant-direct-output-prompt-stack.json +48 -0
  436. package/examples/hack-prompt-stack.json +118 -0
  437. package/examples/minimal-prompt-stack.json +40 -0
  438. package/examples/read-first-worker-prompt-stack.json +49 -0
  439. package/examples/validation-issues-prompt-stack.json +38 -0
  440. package/package.json +114 -4
@@ -0,0 +1,85 @@
1
+ import { applyResourcePolicy } from "./policy.js";
2
+ export const promptRenderHelpers = {
3
+ escapeXml,
4
+ formatDate,
5
+ formatTime,
6
+ normalizePath,
7
+ selectedToolNames,
8
+ slotTextFormat,
9
+ plainBullet,
10
+ plainContinuation,
11
+ indentPlainBlock,
12
+ };
13
+ export function createVariableAccess(_runtime, stack) {
14
+ return {
15
+ get: (name) => collectStaticVariables(stack)[name],
16
+ toMacroText: variableValueToMacroText,
17
+ toPromptText: variableValueToPromptText,
18
+ };
19
+ }
20
+ export function selectedToolNames(stack, runtime) {
21
+ // The caller supplies the effective loadout (defaults plus active capabilities),
22
+ // not a catalog. Applying tools.initial again would hide capability-added tools.
23
+ return applyResourcePolicy([...(runtime.options.selectedTools ?? [])], stack.tools);
24
+ }
25
+ /** Selected tools whose declarations reach the model (Pi's declaredTools: selected minus hidden). */
26
+ export function declaredToolNames(stack, runtime) {
27
+ const hidden = new Set(runtime.options.hiddenTools ?? []);
28
+ return selectedToolNames(stack, runtime).filter((name) => !hidden.has(name));
29
+ }
30
+ export function slotTextFormat(item, options = {}) {
31
+ const format = item.options?.format;
32
+ if (format === "plain")
33
+ return "plain";
34
+ if (format === "json" && options.allowJson)
35
+ return "json";
36
+ return "xml";
37
+ }
38
+ export function collectStaticVariables(stack) {
39
+ return { ...(stack.variables ?? {}) };
40
+ }
41
+ export function variableValueToMacroText(value) {
42
+ if (value === undefined)
43
+ return "";
44
+ if (typeof value === "string")
45
+ return value;
46
+ return JSON.stringify(value);
47
+ }
48
+ export function variableValueToPromptText(value) {
49
+ if (typeof value === "string")
50
+ return value;
51
+ return JSON.stringify(value, null, 2);
52
+ }
53
+ export function plainBullet(label, value) {
54
+ return `- ${label}: ${plainContinuation(value, " ")}`;
55
+ }
56
+ export function plainContinuation(value, indent) {
57
+ return value.split("\n").map((line, index) => index === 0 ? line : `${indent}${line}`).join("\n");
58
+ }
59
+ export function indentPlainBlock(value, indent) {
60
+ return value.split("\n").map((line) => `${indent}${line}`).join("\n");
61
+ }
62
+ export function normalizePath(value) {
63
+ return value.replace(/\\/g, "/");
64
+ }
65
+ export function formatDate(now) {
66
+ const year = now.getFullYear();
67
+ const month = String(now.getMonth() + 1).padStart(2, "0");
68
+ const day = String(now.getDate()).padStart(2, "0");
69
+ return `${year}-${month}-${day}`;
70
+ }
71
+ export function formatTime(now) {
72
+ const hours = String(now.getHours()).padStart(2, "0");
73
+ const minutes = String(now.getMinutes()).padStart(2, "0");
74
+ const seconds = String(now.getSeconds()).padStart(2, "0");
75
+ return `${hours}:${minutes}:${seconds}`;
76
+ }
77
+ export function escapeXml(value) {
78
+ return value
79
+ .replace(/&/g, "&")
80
+ .replace(/</g, "&lt;")
81
+ .replace(/>/g, "&gt;")
82
+ .replace(/\"/g, "&quot;")
83
+ .replace(/'/g, "&apos;");
84
+ }
85
+ //# sourceMappingURL=render-helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render-helpers.js","sourceRoot":"","sources":["../src/render-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AA2BlD,MAAM,CAAC,MAAM,mBAAmB,GAAwB;IACvD,SAAS;IACT,UAAU;IACV,UAAU;IACV,aAAa;IACb,iBAAiB;IACjB,cAAc;IACd,WAAW;IACX,iBAAiB;IACjB,gBAAgB;CAChB,CAAC;AAEF,MAAM,UAAU,oBAAoB,CAAC,QAAuB,EAAE,KAAkB;IAC/E,OAAO;QACN,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;QAClD,WAAW,EAAE,wBAAwB;QACrC,YAAY,EAAE,yBAAyB;KACvC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAkB,EAAE,OAAsB;IAC3E,iFAAiF;IACjF,iFAAiF;IACjF,OAAO,mBAAmB,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,IAAI,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;AACrF,CAAC;AAED,qGAAqG;AACrG,MAAM,UAAU,iBAAiB,CAAC,KAAkB,EAAE,OAAsB;IAC3E,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,WAAW,IAAI,EAAE,CAAC,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;IAC1D,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC1D,OAAO,GAAG,KAAK,IAAI,OAAO,IAAI,OAAO,EAAE,CAAC;AACzC,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,KAAa;IACtC,OAAO,KAAK;SACV,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC;SACtB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC;SACxB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AAC3B,CAAC"}
@@ -0,0 +1,36 @@
1
+ import type { AgentProfile } from "../agent-profile.ts";
2
+ import { type LoadedAgentProfile } from "../codecs/agent-profile.ts";
3
+ import type { ResourceScope } from "../resource-identity.ts";
4
+ export type RepositoryScope = Extract<ResourceScope, "project" | "global">;
5
+ export declare function readAgentProfiles(cwd: string): LoadedAgentProfile[];
6
+ export declare function readAgentProfilesScoped(cwd: string, globalDir?: string): LoadedAgentProfile[];
7
+ export declare function readGlobalAgentProfiles(globalDir?: string): LoadedAgentProfile[];
8
+ export declare function readSingleAgentProfileFile(filePath: string, scope?: "global" | "project"): LoadedAgentProfile;
9
+ export type AgentProfileWriteResult = {
10
+ ok: true;
11
+ filePath: string;
12
+ } | {
13
+ ok: false;
14
+ reason: "invalid-path" | "exists" | "io";
15
+ error?: string;
16
+ };
17
+ export type AgentProfileDeleteResult = {
18
+ ok: true;
19
+ filePath: string;
20
+ } | {
21
+ ok: false;
22
+ reason: "invalid-path" | "missing" | "io";
23
+ error?: string;
24
+ };
25
+ export declare function agentProfileTargetPath(cwd: string, scope: RepositoryScope, id: string): string;
26
+ /**
27
+ * Write an agent profile. This is the only write path for agent-profile domain
28
+ * resources. `overwrite: false` refuses to clobber an existing file, matching
29
+ * the current replacement semantics (no expected-fingerprint conflict or atomic
30
+ * replacement is introduced yet).
31
+ */
32
+ export declare function writeAgentProfileFile(cwd: string, scope: RepositoryScope, filePath: string, profile: AgentProfile, options: {
33
+ overwrite: boolean;
34
+ }): AgentProfileWriteResult;
35
+ export declare function deleteAgentProfileFile(cwd: string, scope: RepositoryScope, filePath: string): AgentProfileDeleteResult;
36
+ //# sourceMappingURL=agent-profile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-profile.d.ts","sourceRoot":"","sources":["../../src/repositories/agent-profile.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAKN,KAAK,kBAAkB,EACvB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAU7D,MAAM,MAAM,eAAe,GAAG,OAAO,CAAC,aAAa,EAAE,SAAS,GAAG,QAAQ,CAAC,CAAC;AAM3E,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,kBAAkB,EAAE,CAKnE;AAED,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,GAAE,MAAiC,GAAG,kBAAkB,EAAE,CAQvH;AAED,wBAAgB,uBAAuB,CAAC,SAAS,GAAE,MAAiC,GAAG,kBAAkB,EAAE,CAK1G;AAED,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,GAAE,QAAQ,GAAG,SAAqB,GAAG,kBAAkB,CAYxH;AAsDD,MAAM,MAAM,uBAAuB,GAChC;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,cAAc,GAAG,QAAQ,GAAG,IAAI,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3E,MAAM,MAAM,wBAAwB,GACjC;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,cAAc,GAAG,SAAS,GAAG,IAAI,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE5E,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAE9F;AAYD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACpC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,eAAe,EACtB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,YAAY,EACrB,OAAO,EAAE;IAAE,SAAS,EAAE,OAAO,CAAA;CAAE,GAC7B,uBAAuB,CAuBzB;AAED,wBAAgB,sBAAsB,CACrC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,eAAe,EACtB,QAAQ,EAAE,MAAM,GACd,wBAAwB,CAkB1B"}
@@ -0,0 +1,153 @@
1
+ import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
2
+ import { basename, dirname, join } from "node:path";
3
+ import { createAgentProfileFault, parseAgentProfile, serializeAgentProfile, } from "../codecs/agent-profile.js";
4
+ import { agentProfilePath, agentProfilesDir, globalAgentProfilePath, globalAgentProfilesDir, isSafeAgentProfileMutationPath, isSafeGlobalAgentProfileMutationPath, } from "../storage.js";
5
+ // ---------------------------------------------------------------------------
6
+ // Read (the only scoped read path).
7
+ // ---------------------------------------------------------------------------
8
+ export function readAgentProfiles(cwd) {
9
+ const profiles = loadAgentProfilesFromDir(agentProfilesDir(cwd), "project");
10
+ annotateDuplicateProfileIds(profiles);
11
+ annotateAutoActivateConflicts(profiles);
12
+ return profiles;
13
+ }
14
+ export function readAgentProfilesScoped(cwd, globalDir = globalAgentProfilesDir()) {
15
+ const profiles = [
16
+ ...loadAgentProfilesFromDir(globalDir, "global"),
17
+ ...loadAgentProfilesFromDir(agentProfilesDir(cwd), "project"),
18
+ ];
19
+ annotateDuplicateProfileIds(profiles);
20
+ annotateAutoActivateConflicts(profiles);
21
+ return profiles;
22
+ }
23
+ export function readGlobalAgentProfiles(globalDir = globalAgentProfilesDir()) {
24
+ const profiles = loadAgentProfilesFromDir(globalDir, "global");
25
+ annotateDuplicateProfileIds(profiles);
26
+ annotateAutoActivateConflicts(profiles);
27
+ return profiles;
28
+ }
29
+ export function readSingleAgentProfileFile(filePath, scope = "project") {
30
+ let source;
31
+ try {
32
+ source = readFileSync(filePath, "utf8");
33
+ }
34
+ catch (error) {
35
+ return createAgentProfileFault(filePath, scope, `Failed to read agent profile: ${error instanceof Error ? error.message : String(error)}`);
36
+ }
37
+ return parseAgentProfile(source, filePath, scope);
38
+ }
39
+ function loadAgentProfilesFromDir(dir, scope) {
40
+ if (!existsSync(dir))
41
+ return [];
42
+ let entries;
43
+ try {
44
+ entries = readdirSync(dir).filter((name) => name.endsWith(".json")).sort();
45
+ }
46
+ catch {
47
+ return [];
48
+ }
49
+ return entries.map((name) => readSingleAgentProfileFile(join(dir, name), scope));
50
+ }
51
+ function annotateDuplicateProfileIds(profiles) {
52
+ const byScopeId = new Map();
53
+ for (const loaded of profiles) {
54
+ const key = `${loaded.scope}\0${loaded.profile.id}`;
55
+ const matches = byScopeId.get(key) ?? [];
56
+ matches.push(loaded);
57
+ byScopeId.set(key, matches);
58
+ }
59
+ for (const matches of byScopeId.values()) {
60
+ if (matches.length <= 1)
61
+ continue;
62
+ const files = matches.map((loaded) => basename(loaded.filePath)).join(", ");
63
+ for (const loaded of matches) {
64
+ loaded.diagnostics.push({
65
+ level: "error",
66
+ message: `Duplicate ${loaded.scope} profile id: ${loaded.profile.id} appears in multiple files (${files}).`,
67
+ });
68
+ }
69
+ }
70
+ }
71
+ function annotateAutoActivateConflicts(profiles) {
72
+ const byScope = new Map();
73
+ for (const loaded of profiles) {
74
+ if (loaded.profile.autoActivate !== true)
75
+ continue;
76
+ const matches = byScope.get(loaded.scope) ?? [];
77
+ matches.push(loaded);
78
+ byScope.set(loaded.scope, matches);
79
+ }
80
+ for (const [scope, candidates] of byScope) {
81
+ if (candidates.length <= 1)
82
+ continue;
83
+ const files = candidates.map((loaded) => basename(loaded.filePath)).join(", ");
84
+ for (const loaded of candidates) {
85
+ loaded.diagnostics.push({
86
+ level: "error",
87
+ field: "autoActivate",
88
+ message: `Multiple ${scope} profiles request auto-activation (${files}); exactly one is allowed.`,
89
+ });
90
+ }
91
+ }
92
+ }
93
+ export function agentProfileTargetPath(cwd, scope, id) {
94
+ return scope === "global" ? globalAgentProfilePath(id) : agentProfilePath(cwd, id);
95
+ }
96
+ function isSafeTarget(cwd, scope, filePath) {
97
+ return scope === "project"
98
+ ? isSafeAgentProfileMutationPath(cwd, filePath)
99
+ : isSafeGlobalAgentProfileMutationPath(filePath);
100
+ }
101
+ function targetError(scope, filePath) {
102
+ return `Profile path is outside ${scope} agent-profile storage or traverses a symbolic link: ${filePath}`;
103
+ }
104
+ /**
105
+ * Write an agent profile. This is the only write path for agent-profile domain
106
+ * resources. `overwrite: false` refuses to clobber an existing file, matching
107
+ * the current replacement semantics (no expected-fingerprint conflict or atomic
108
+ * replacement is introduced yet).
109
+ */
110
+ export function writeAgentProfileFile(cwd, scope, filePath, profile, options) {
111
+ if (!isSafeTarget(cwd, scope, filePath)) {
112
+ return { ok: false, reason: "invalid-path", error: targetError(scope, filePath) };
113
+ }
114
+ if (!options.overwrite && existsSync(filePath)) {
115
+ return { ok: false, reason: "exists", error: `Agent profile already exists: ${filePath}` };
116
+ }
117
+ try {
118
+ mkdirSync(dirname(filePath), { recursive: true });
119
+ writeFileSync(filePath, serializeAgentProfile(profile), { encoding: "utf8", flag: options.overwrite ? "w" : "wx" });
120
+ return { ok: true, filePath };
121
+ }
122
+ catch (error) {
123
+ const code = error && typeof error === "object" && "code" in error ? String(error.code) : undefined;
124
+ if (code === "EEXIST") {
125
+ return { ok: false, reason: "exists", error: `Agent profile already exists: ${filePath}` };
126
+ }
127
+ return {
128
+ ok: false,
129
+ reason: "io",
130
+ error: `Failed to write agent profile ${filePath}: ${error instanceof Error ? error.message : String(error)}`,
131
+ };
132
+ }
133
+ }
134
+ export function deleteAgentProfileFile(cwd, scope, filePath) {
135
+ if (!isSafeTarget(cwd, scope, filePath)) {
136
+ return { ok: false, reason: "invalid-path", error: targetError(scope, filePath) };
137
+ }
138
+ if (!existsSync(filePath)) {
139
+ return { ok: false, reason: "missing", error: `Agent profile does not exist: ${filePath}` };
140
+ }
141
+ try {
142
+ unlinkSync(filePath);
143
+ return { ok: true, filePath };
144
+ }
145
+ catch (error) {
146
+ return {
147
+ ok: false,
148
+ reason: "io",
149
+ error: `Failed to delete agent profile ${filePath}: ${error instanceof Error ? error.message : String(error)}`,
150
+ };
151
+ }
152
+ }
153
+ //# sourceMappingURL=agent-profile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-profile.js","sourceRoot":"","sources":["../../src/repositories/agent-profile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACtG,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEpD,OAAO,EACN,uBAAuB,EACvB,iBAAiB,EACjB,qBAAqB,GAGrB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EACN,gBAAgB,EAChB,gBAAgB,EAChB,sBAAsB,EACtB,sBAAsB,EACtB,8BAA8B,EAC9B,oCAAoC,GACpC,MAAM,eAAe,CAAC;AAIvB,8EAA8E;AAC9E,oCAAoC;AACpC,8EAA8E;AAE9E,MAAM,UAAU,iBAAiB,CAAC,GAAW;IAC5C,MAAM,QAAQ,GAAG,wBAAwB,CAAC,gBAAgB,CAAC,GAAG,CAAC,E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AAC;IAED,IAAI,CAAC;QACJ,UAAU,CAAC,QAAQ,CAAC,CAAC;QACrB,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,OAAO;YACN,EAAE,EAAE,KAAK;YACT,MAAM,EAAE,IAAI;YACZ,KAAK,EAAE,kCAAkC,QAAQ,KAAK,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;SAC9G,CAAC;IACH,CAAC;AACF,CAAC"}
@@ -0,0 +1,53 @@
1
+ import { type Capability, type LoadedCapability } from "../codecs/capability.ts";
2
+ import { type ResourceScope } from "../resource-identity.ts";
3
+ export { type Capability, type LoadedCapability };
4
+ export type CapabilityMutationResult = {
5
+ ok: true;
6
+ } | {
7
+ ok: false;
8
+ reason: "invalid-path" | "invalid-capability" | "conflict" | "io-error";
9
+ error: string;
10
+ };
11
+ export type CapabilityWriteResult = CapabilityMutationResult;
12
+ export type CapabilityDeleteResult = CapabilityMutationResult;
13
+ export declare const MAX_CAPABILITY_SOURCE_SIZE: number;
14
+ export declare function globalCapabilitiesDir(): string;
15
+ export declare function capabilitiesDir(cwd: string): string;
16
+ /**
17
+ * Read global capabilities.
18
+ * Returns empty array if directory does not exist (ENOENT).
19
+ * Throws on real directory read errors (e.g. permission denied) to fail closed.
20
+ */
21
+ export declare function readGlobalCapabilities(globalDir?: string): LoadedCapability[];
22
+ /**
23
+ * Read scoped capabilities (global and project).
24
+ * Returns empty array for missing directory (ENOENT); throws on real directory read errors.
25
+ * Preserves faults for shadowed resolution and attaches errors to same-scope duplicate IDs.
26
+ */
27
+ export declare function readCapabilitiesScoped(cwd: string, globalDir?: string): LoadedCapability[];
28
+ /**
29
+ * Read a single capability file.
30
+ * Returns a LoadedCapability fault if reading or parsing fails, or if file exceeds 1MiB.
31
+ */
32
+ export declare function readSingleCapabilityFile(filePath: string, scope?: ResourceScope): LoadedCapability;
33
+ /**
34
+ * Compute the sha256 revision hash of raw capability file bytes.
35
+ * Throws ENOENT if missing or an Error if size exceeds 1MiB limit.
36
+ */
37
+ export declare function capabilityRevision(filePath: string): string;
38
+ /**
39
+ * Write a capability file.
40
+ * Creates exclusively with "wx" when overwrite is false.
41
+ * Updates atomically with expectedSourceRevision check when overwrite is true.
42
+ */
43
+ export declare function writeCapabilityFile(cwd: string, scope: ResourceScope, filePath: string, capability: Capability, options: {
44
+ overwrite: boolean;
45
+ expectedSourceRevision?: string;
46
+ }): CapabilityWriteResult;
47
+ /**
48
+ * Delete a capability file with expectedSourceRevision check.
49
+ */
50
+ export declare function deleteCapabilityFile(cwd: string, scope: ResourceScope, filePath: string, options: {
51
+ expectedSourceRevision: string;
52
+ }): CapabilityDeleteResult;
53
+ //# sourceMappingURL=capability.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../../src/repositories/capability.ts"],"names":[],"mappings":"AAaA,OAAO,EAKN,KAAK,UAAU,EACf,KAAK,gBAAgB,EACrB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAqB,KAAK,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGhF,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,gBAAgB,EAAE,CAAC;AAElD,MAAM,MAAM,wBAAwB,GACjC;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GACZ;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,cAAc,GAAG,oBAAoB,GAAG,UAAU,GAAG,UAAU,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzG,MAAM,MAAM,qBAAqB,GAAG,wBAAwB,CAAC;AAC7D,MAAM,MAAM,sBAAsB,GAAG,wBAAwB,CAAC;AAE9D,eAAO,MAAM,0BAA0B,QAAc,CAAC;AAEtD,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CACrC,SAAS,GAAE,MAAgC,GACzC,gBAAgB,EAAE,CAIpB;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CACrC,GAAG,EAAE,MAAM,EACX,SAAS,GAAE,MAAgC,GACzC,gBAAgB,EAAE,CAOpB;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CACvC,QAAQ,EAAE,MAAM,EAChB,KAAK,GAAE,aAAyB,GAC9B,gBAAgB,CA8BlB;AAyDD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAa3D;AAmFD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,UAAU,EACtB,OAAO,EAAE;IAAE,SAAS,EAAE,OAAO,CAAC;IAAC,sBAAsB,CAAC,EAAE,MAAM,CAAA;CAAE,GAC9D,qBAAqB,CA6CvB;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CACnC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE;IAAE,sBAAsB,EAAE,MAAM,CAAA;CAAE,GACzC,sBAAsB,CAaxB"}
@@ -0,0 +1,294 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync, } from "node:fs";
3
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
4
+ import { createCapabilityFault, isUsableCapability, parseCapability, serializeCapability, } from "../codecs/capability.js";
5
+ import { isValidResourceId } from "../resource-identity.js";
6
+ import { globalForgeDir } from "../storage.js";
7
+ export const MAX_CAPABILITY_SOURCE_SIZE = 1024 * 1024; // 1 MiB
8
+ export function globalCapabilitiesDir() {
9
+ return join(globalForgeDir(), "capabilities");
10
+ }
11
+ export function capabilitiesDir(cwd) {
12
+ return join(cwd, ".pi", "forge", "capabilities");
13
+ }
14
+ /**
15
+ * Read global capabilities.
16
+ * Returns empty array if directory does not exist (ENOENT).
17
+ * Throws on real directory read errors (e.g. permission denied) to fail closed.
18
+ */
19
+ export function readGlobalCapabilities(globalDir = globalCapabilitiesDir()) {
20
+ const capabilities = loadCapabilitiesFromDir(globalDir, "global");
21
+ annotateDuplicateCapabilityIds(capabilities);
22
+ return capabilities;
23
+ }
24
+ /**
25
+ * Read scoped capabilities (global and project).
26
+ * Returns empty array for missing directory (ENOENT); throws on real directory read errors.
27
+ * Preserves faults for shadowed resolution and attaches errors to same-scope duplicate IDs.
28
+ */
29
+ export function readCapabilitiesScoped(cwd, globalDir = globalCapabilitiesDir()) {
30
+ const capabilities = [
31
+ ...loadCapabilitiesFromDir(globalDir, "global"),
32
+ ...loadCapabilitiesFromDir(capabilitiesDir(cwd), "project"),
33
+ ];
34
+ annotateDuplicateCapabilityIds(capabilities);
35
+ return capabilities;
36
+ }
37
+ /**
38
+ * Read a single capability file.
39
+ * Returns a LoadedCapability fault if reading or parsing fails, or if file exceeds 1MiB.
40
+ */
41
+ export function readSingleCapabilityFile(filePath, scope = "project") {
42
+ let source;
43
+ let sourceRevision;
44
+ try {
45
+ const stat = statSync(filePath);
46
+ if (stat.size > MAX_CAPABILITY_SOURCE_SIZE) {
47
+ return createCapabilityFault(filePath, scope, `Capability capability file size (${stat.size} bytes) exceeds 1MiB limit.`);
48
+ }
49
+ const buf = readFileSync(filePath);
50
+ if (buf.byteLength > MAX_CAPABILITY_SOURCE_SIZE) {
51
+ return createCapabilityFault(filePath, scope, `Capability capability file size (${buf.byteLength} bytes) exceeds 1MiB limit.`);
52
+ }
53
+ source = buf.toString("utf8");
54
+ sourceRevision = createHash("sha256").update(buf).digest("hex");
55
+ }
56
+ catch (error) {
57
+ return createCapabilityFault(filePath, scope, `Failed to read capability: ${error instanceof Error ? error.message : String(error)}`);
58
+ }
59
+ return { ...parseCapability(source, filePath, scope), sourceRevision };
60
+ }
61
+ function loadCapabilitiesFromDir(dir, scope) {
62
+ let entries;
63
+ try {
64
+ entries = readdirSync(dir, { withFileTypes: true });
65
+ }
66
+ catch (error) {
67
+ const err = error;
68
+ if (err && (err.code === "ENOENT" || (typeof err.message === "string" && err.message.includes("ENOENT")))) {
69
+ return [];
70
+ }
71
+ throw error instanceof Error ? error : new Error(String(error));
72
+ }
73
+ const fileNames = [];
74
+ for (const entry of entries) {
75
+ if (!entry.name.endsWith(".json") || entry.name.startsWith("."))
76
+ continue;
77
+ if (entry.isDirectory())
78
+ continue;
79
+ if (entry.isSymbolicLink()) {
80
+ try {
81
+ if (!statSync(join(dir, entry.name)).isFile())
82
+ continue;
83
+ }
84
+ catch {
85
+ // Retain broken symlinks so read will record a fault
86
+ }
87
+ }
88
+ else if (!entry.isFile()) {
89
+ continue;
90
+ }
91
+ fileNames.push(entry.name);
92
+ }
93
+ fileNames.sort();
94
+ return fileNames.map((name) => readSingleCapabilityFile(join(dir, name), scope));
95
+ }
96
+ function annotateDuplicateCapabilityIds(capabilities) {
97
+ const byScopeId = new Map();
98
+ for (const loaded of capabilities) {
99
+ const key = `${loaded.scope}\0${loaded.capability.id}`;
100
+ const matches = byScopeId.get(key) ?? [];
101
+ matches.push(loaded);
102
+ byScopeId.set(key, matches);
103
+ }
104
+ for (const matches of byScopeId.values()) {
105
+ if (matches.length <= 1)
106
+ continue;
107
+ const files = matches.map((loaded) => basename(loaded.filePath)).join(", ");
108
+ for (const loaded of matches) {
109
+ loaded.diagnostics.push({
110
+ level: "error",
111
+ message: `Duplicate ${loaded.scope} capability id: "${loaded.capability.id}" appears in multiple files (${files}).`,
112
+ });
113
+ }
114
+ }
115
+ }
116
+ /**
117
+ * Compute the sha256 revision hash of raw capability file bytes.
118
+ * Throws ENOENT if missing or an Error if size exceeds 1MiB limit.
119
+ */
120
+ export function capabilityRevision(filePath) {
121
+ const stat = statSync(filePath);
122
+ if (!stat.isFile()) {
123
+ throw new Error(`Expected a regular file at ${filePath}`);
124
+ }
125
+ if (stat.size > MAX_CAPABILITY_SOURCE_SIZE) {
126
+ throw new Error(`Capability capability file size (${stat.size} bytes) exceeds 1MiB limit.`);
127
+ }
128
+ const buf = readFileSync(filePath);
129
+ if (buf.byteLength > MAX_CAPABILITY_SOURCE_SIZE) {
130
+ throw new Error(`Capability capability file size (${buf.byteLength} bytes) exceeds 1MiB limit.`);
131
+ }
132
+ return createHash("sha256").update(buf).digest("hex");
133
+ }
134
+ function pathTraversesNoSymlink(anchor, target) {
135
+ const resolvedAnchor = resolve(anchor);
136
+ const resolvedTarget = resolve(target);
137
+ const rel = relative(resolvedAnchor, resolvedTarget);
138
+ if (rel.startsWith("..") || isAbsolute(rel))
139
+ return false;
140
+ try {
141
+ if (lstatSync(resolvedAnchor).isSymbolicLink())
142
+ return false;
143
+ }
144
+ catch (error) {
145
+ if (error.code !== "ENOENT")
146
+ return false;
147
+ // anchor does not exist yet
148
+ }
149
+ let current = resolvedAnchor;
150
+ for (const segment of rel.split(sep)) {
151
+ if (!segment || segment === ".")
152
+ continue;
153
+ current = join(current, segment);
154
+ try {
155
+ if (lstatSync(current).isSymbolicLink())
156
+ return false;
157
+ }
158
+ catch (error) {
159
+ if (error.code !== "ENOENT")
160
+ return false;
161
+ // segment does not exist yet
162
+ }
163
+ }
164
+ return true;
165
+ }
166
+ function verifyCapabilityPath(cwd, scope, filePath, expectedId) {
167
+ if (scope !== "project" && scope !== "global") {
168
+ return { ok: false, reason: "invalid-path", error: `Invalid scope: ${String(scope)}` };
169
+ }
170
+ if (!filePath || typeof filePath !== "string") {
171
+ return { ok: false, reason: "invalid-path", error: "File path is required." };
172
+ }
173
+ const rootDir = resolve(scope === "global" ? globalCapabilitiesDir() : capabilitiesDir(cwd));
174
+ const anchor = resolve(scope === "global" ? globalForgeDir() : cwd);
175
+ const resolvedTarget = resolve(filePath);
176
+ if (dirname(resolvedTarget) !== rootDir) {
177
+ return { ok: false, reason: "invalid-path", error: `Path is outside ${scope} root (${rootDir}): ${filePath}` };
178
+ }
179
+ const fileName = basename(resolvedTarget);
180
+ if (!fileName.endsWith(".json") || fileName.startsWith(".")) {
181
+ return { ok: false, reason: "invalid-path", error: `File must be a non-hidden .json file: ${fileName}` };
182
+ }
183
+ const id = basename(fileName, ".json");
184
+ if (!isValidResourceId(id) || (expectedId !== undefined && expectedId !== id)) {
185
+ return { ok: false, reason: "invalid-path", error: `Invalid ID or rename mismatch: ${fileName}` };
186
+ }
187
+ if (!pathTraversesNoSymlink(anchor, resolvedTarget)) {
188
+ return { ok: false, reason: "invalid-path", error: `Path traverses or targets a symlink: ${filePath}` };
189
+ }
190
+ try {
191
+ if (lstatSync(resolvedTarget).isDirectory()) {
192
+ return { ok: false, reason: "invalid-path", error: `Path is a directory: ${filePath}` };
193
+ }
194
+ }
195
+ catch {
196
+ // Target does not exist yet
197
+ }
198
+ return { ok: true, rootDir, fileName };
199
+ }
200
+ function checkExpectedRevision(filePath, expected) {
201
+ if (!expected)
202
+ return { ok: false, reason: "conflict", error: "expectedSourceRevision is required." };
203
+ if (!existsSync(filePath))
204
+ return { ok: false, reason: "conflict", error: `File does not exist: ${filePath}` };
205
+ try {
206
+ const current = capabilityRevision(filePath);
207
+ if (current !== expected) {
208
+ return { ok: false, reason: "conflict", error: `Revision conflict: expected ${expected}, got ${current}.` };
209
+ }
210
+ }
211
+ catch (err) {
212
+ if (err?.code === "ENOENT")
213
+ return { ok: false, reason: "conflict", error: `File does not exist: ${filePath}` };
214
+ return { ok: false, reason: "io-error", error: `Failed to read revision: ${err instanceof Error ? err.message : String(err)}` };
215
+ }
216
+ return null;
217
+ }
218
+ /**
219
+ * Write a capability file.
220
+ * Creates exclusively with "wx" when overwrite is false.
221
+ * Updates atomically with expectedSourceRevision check when overwrite is true.
222
+ */
223
+ export function writeCapabilityFile(cwd, scope, filePath, capability, options) {
224
+ const pathCheck = verifyCapabilityPath(cwd, scope, filePath, capability?.id);
225
+ if (!pathCheck.ok)
226
+ return pathCheck;
227
+ let serialized;
228
+ try {
229
+ serialized = serializeCapability(capability);
230
+ }
231
+ catch (error) {
232
+ return { ok: false, reason: "invalid-capability", error: `Failed to serialize capability: ${error instanceof Error ? error.message : String(error)}` };
233
+ }
234
+ if (Buffer.byteLength(serialized, "utf8") > MAX_CAPABILITY_SOURCE_SIZE) {
235
+ return { ok: false, reason: "invalid-capability", error: "Capability capability file size exceeds 1MiB limit." };
236
+ }
237
+ const parsed = parseCapability(serialized, filePath, scope);
238
+ if (!isUsableCapability(parsed)) {
239
+ const msg = parsed.diagnostics.filter((d) => d.level === "error").map((d) => d.message).join("; ");
240
+ return { ok: false, reason: "invalid-capability", error: msg || "Invalid capability." };
241
+ }
242
+ const { rootDir, fileName } = pathCheck;
243
+ if (!options.overwrite) {
244
+ if (existsSync(filePath))
245
+ return { ok: false, reason: "conflict", error: `Capability capability already exists: ${filePath}` };
246
+ try {
247
+ mkdirSync(rootDir, { recursive: true });
248
+ writeFileSync(filePath, serialized, { encoding: "utf8", flag: "wx" });
249
+ return { ok: true };
250
+ }
251
+ catch (error) {
252
+ if (error?.code === "EEXIST")
253
+ return { ok: false, reason: "conflict", error: `Capability capability already exists: ${filePath}` };
254
+ return { ok: false, reason: "io-error", error: `Failed to write capability: ${error instanceof Error ? error.message : String(error)}` };
255
+ }
256
+ }
257
+ const revConflict = checkExpectedRevision(filePath, options.expectedSourceRevision);
258
+ if (revConflict)
259
+ return revConflict;
260
+ const tempPath = join(rootDir, `.${fileName}.${randomUUID()}.tmp`);
261
+ try {
262
+ mkdirSync(rootDir, { recursive: true });
263
+ writeFileSync(tempPath, serialized, "utf8");
264
+ renameSync(tempPath, filePath);
265
+ return { ok: true };
266
+ }
267
+ catch (error) {
268
+ try {
269
+ if (existsSync(tempPath))
270
+ unlinkSync(tempPath);
271
+ }
272
+ catch { }
273
+ return { ok: false, reason: "io-error", error: `Failed to write capability: ${error instanceof Error ? error.message : String(error)}` };
274
+ }
275
+ }
276
+ /**
277
+ * Delete a capability file with expectedSourceRevision check.
278
+ */
279
+ export function deleteCapabilityFile(cwd, scope, filePath, options) {
280
+ const pathCheck = verifyCapabilityPath(cwd, scope, filePath);
281
+ if (!pathCheck.ok)
282
+ return pathCheck;
283
+ const revConflict = checkExpectedRevision(filePath, options?.expectedSourceRevision);
284
+ if (revConflict)
285
+ return revConflict;
286
+ try {
287
+ unlinkSync(filePath);
288
+ return { ok: true };
289
+ }
290
+ catch (error) {
291
+ return { ok: false, reason: "io-error", error: `Failed to delete capability: ${error instanceof Error ? error.message : String(error)}` };
292
+ }
293
+ }
294
+ //# sourceMappingURL=capability.js.map