@jadelclemens/pi-forge 0.0.0-stage → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,637 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { createServer } from "node:http";
3
+ import { AGENT_PROFILE_THINKING_LEVELS, AGENT_PROFILE_TYPE, } from "../agent-profile.js";
4
+ import { isCapabilityStateMutation, isCapabilityEnableRequest } from "../capability-state.js";
5
+ import { ContributionService } from "./contrib-service.js";
6
+ import { renderEditorHtml } from "./page.js";
7
+ // Port 0 asks Node to bind any available localhost port.
8
+ export const DEFAULT_WEB_EDITOR_PORT = 0;
9
+ class RequestBodyError extends Error {
10
+ status;
11
+ constructor(status, message) {
12
+ super(message);
13
+ this.name = "RequestBodyError";
14
+ this.status = status;
15
+ }
16
+ }
17
+ export async function startWebEditorServer(host, options = {}) {
18
+ let currentHost = host;
19
+ const token = randomBytes(24).toString("base64url");
20
+ const sockets = new Set();
21
+ const contributionService = options.contributionTransport
22
+ ? new ContributionService(options.contributionTransport, {
23
+ discoverTimeoutMs: options.contributionDiscoverTimeoutMs,
24
+ })
25
+ : undefined;
26
+ const server = createServer((req, res) => {
27
+ void handleRequest(currentHost, token, contributionService, req, res, () => currentHost).catch((error) => {
28
+ const status = error instanceof RequestBodyError ? error.status : 500;
29
+ sendJson(res, status, { error: error instanceof Error ? error.message : String(error) });
30
+ });
31
+ });
32
+ server.on("connection", (socket) => {
33
+ sockets.add(socket);
34
+ socket.on("close", () => sockets.delete(socket));
35
+ });
36
+ await new Promise((resolve, reject) => {
37
+ server.once("error", reject);
38
+ server.listen(options.port ?? DEFAULT_WEB_EDITOR_PORT, "127.0.0.1", () => {
39
+ server.off("error", reject);
40
+ resolve();
41
+ });
42
+ });
43
+ const address = server.address();
44
+ if (!address || typeof address === "string") {
45
+ await closeServer(server, sockets);
46
+ throw new Error("Failed to start pi-forge editor server.");
47
+ }
48
+ const url = `http://127.0.0.1:${address.port}/?token=${encodeURIComponent(token)}`;
49
+ contributionService?.start();
50
+ return {
51
+ url,
52
+ port: address.port,
53
+ updateHost: (nextHost) => {
54
+ currentHost = nextHost;
55
+ },
56
+ close: async () => {
57
+ await contributionService?.stop();
58
+ await closeServer(server, sockets);
59
+ },
60
+ };
61
+ }
62
+ async function handleRequest(host, token, contributionService, req, res, getCurrentHost) {
63
+ const url = new URL(req.url ?? "/", "http://127.0.0.1");
64
+ if (url.pathname === "/" && req.method === "GET") {
65
+ if (url.searchParams.get("token") !== token) {
66
+ sendText(res, 403, "Invalid pi-forge editor token.");
67
+ return;
68
+ }
69
+ sendHtml(res, renderEditorHtml(resolvePageLang(host, req)));
70
+ return;
71
+ }
72
+ if (!url.pathname.startsWith("/api/")) {
73
+ sendText(res, 404, "Not found.");
74
+ return;
75
+ }
76
+ if (!hasValidToken(req, url, token)) {
77
+ sendJson(res, 403, { error: "Invalid pi-forge editor token." });
78
+ return;
79
+ }
80
+ const parts = url.pathname.split("/").filter(Boolean).map((part) => decodeURIComponent(part));
81
+ if (req.method === "GET" && parts[1] === "editor-config" && parts.length === 2) {
82
+ sendJson(res, 200, host.getEditorConfig());
83
+ return;
84
+ }
85
+ if (req.method === "PUT" && parts[1] === "editor-config" && parts.length === 2) {
86
+ const body = await readJsonBody(req);
87
+ host = getCurrentHost ? getCurrentHost() : host;
88
+ const locale = isPlainObject(body) ? body.locale : undefined;
89
+ if (locale !== "en" && locale !== "zh-CN" && locale !== "auto") {
90
+ sendJson(res, 400, { error: 'locale must be "en", "zh-CN", or "auto".' });
91
+ return;
92
+ }
93
+ const result = host.setEditorLocale(locale);
94
+ if (!result.ok) {
95
+ sendJson(res, result.status ?? 500, { error: result.error });
96
+ return;
97
+ }
98
+ sendJson(res, 200, { locale: result.locale });
99
+ return;
100
+ }
101
+ if (req.method === "GET" && parts[1] === "stacks" && parts.length === 2) {
102
+ sendJson(res, 200, { stacks: host.listStacks(), cwd: host.cwd });
103
+ return;
104
+ }
105
+ if (req.method === "GET" && parts[1] === "profiles" && parts.length === 2) {
106
+ sendJson(res, 200, host.listProfiles());
107
+ return;
108
+ }
109
+ if (req.method === "GET" && parts[1] === "contrib" && parts.length === 2) {
110
+ if (!contributionService) {
111
+ sendJson(res, 200, { tabs: [], providerKey: null });
112
+ return;
113
+ }
114
+ const tabs = await contributionService.listTabs();
115
+ sendJson(res, 200, { tabs, providerKey: contributionService.providerKey });
116
+ return;
117
+ }
118
+ if (req.method === "PUT" && parts[1] === "contrib" && parts.length === 3) {
119
+ if (!contributionService) {
120
+ sendJson(res, 503, { error: "No UI contribution provider is available." });
121
+ return;
122
+ }
123
+ const body = await readJsonBody(req);
124
+ host = getCurrentHost ? getCurrentHost() : host;
125
+ const patch = isPlainObject(body) && isPlainObject(body.patch)
126
+ ? body.patch
127
+ : isPlainObject(body)
128
+ ? body
129
+ : undefined;
130
+ if (!patch) {
131
+ sendJson(res, 400, { error: "Contribution patch must be a JSON object." });
132
+ return;
133
+ }
134
+ const result = await contributionService.writeValues(parts[2], patch);
135
+ if (!result.ok) {
136
+ sendJson(res, result.status, {
137
+ error: result.error,
138
+ ...(result.errors ? { errors: result.errors } : {}),
139
+ });
140
+ return;
141
+ }
142
+ sendJson(res, 200, { ok: true, ...(result.values ? { values: result.values } : {}) });
143
+ return;
144
+ }
145
+ if (req.method === "POST" && parts[1] === "profiles" && parts[2] === "reload" && parts.length === 3) {
146
+ sendOperation(res, await host.reloadProfiles());
147
+ return;
148
+ }
149
+ if (req.method === "POST" && parts[1] === "profiles" && parts[2] === "validate" && parts.length === 3) {
150
+ const body = await readJsonBody(req);
151
+ host = getCurrentHost ? getCurrentHost() : host;
152
+ const parsed = readProfilePayload(body);
153
+ if (!parsed.ok) {
154
+ sendJson(res, 400, { error: parsed.error });
155
+ return;
156
+ }
157
+ const existingId = isPlainObject(body) && typeof body.existingId === "string" ? body.existingId : undefined;
158
+ const scopeResult = isPlainObject(body) ? parseScope(body.scope) : { ok: true, scope: undefined };
159
+ if (!scopeResult.ok) {
160
+ sendJson(res, 400, { error: scopeResult.error });
161
+ return;
162
+ }
163
+ sendJson(res, 200, host.validateProfile(parsed.profile, existingId, scopeResult.scope ?? "project"));
164
+ return;
165
+ }
166
+ if (req.method === "POST" && parts[1] === "profiles" && parts.length === 2) {
167
+ const body = await readJsonBody(req);
168
+ host = getCurrentHost ? getCurrentHost() : host;
169
+ const parsed = readProfilePayload(body);
170
+ if (!parsed.ok) {
171
+ sendJson(res, 400, { error: parsed.error });
172
+ return;
173
+ }
174
+ const scopeResult = isPlainObject(body) ? parseScope(body.scope) : { ok: true, scope: undefined };
175
+ if (!scopeResult.ok) {
176
+ sendJson(res, 400, { error: scopeResult.error });
177
+ return;
178
+ }
179
+ sendOperation(res, await host.createProfile(parsed.profile, scopeResult.scope ?? "project"));
180
+ return;
181
+ }
182
+ if (req.method === "PUT" && parts[1] === "profiles" && parts.length === 3) {
183
+ const body = await readJsonBody(req);
184
+ host = getCurrentHost ? getCurrentHost() : host;
185
+ const parsed = readProfilePayload(body);
186
+ if (!parsed.ok) {
187
+ sendJson(res, 400, { error: parsed.error });
188
+ return;
189
+ }
190
+ sendOperation(res, await host.saveProfile(parts[2], parsed.profile));
191
+ return;
192
+ }
193
+ if (req.method === "POST" && parts[1] === "profiles" && parts[3] === "apply" && parts.length === 4) {
194
+ sendOperation(res, await host.applyProfile(parts[2]));
195
+ return;
196
+ }
197
+ if (req.method === "DELETE" && parts[1] === "profiles" && parts.length === 3) {
198
+ sendOperation(res, await host.deleteProfile(parts[2]));
199
+ return;
200
+ }
201
+ if (req.method === "GET" && parts[1] === "resources" && parts.length === 2) {
202
+ sendJson(res, 200, host.listResources());
203
+ return;
204
+ }
205
+ if (req.method === "POST" && parts[1] === "stacks" && parts.length === 2) {
206
+ const body = await readJsonBody(req);
207
+ host = getCurrentHost ? getCurrentHost() : host;
208
+ const parsed = readStackPayload(body);
209
+ if (!parsed.ok) {
210
+ sendJson(res, 400, { error: parsed.error });
211
+ return;
212
+ }
213
+ const scopeResult = isPlainObject(body) ? parseScope(body.scope) : { ok: true, scope: undefined };
214
+ if (!scopeResult.ok) {
215
+ sendJson(res, 400, { error: scopeResult.error });
216
+ return;
217
+ }
218
+ const options = isPlainObject(body)
219
+ ? {
220
+ activate: body.activate === true,
221
+ overwrite: body.overwrite === true,
222
+ scope: scopeResult.scope,
223
+ }
224
+ : {};
225
+ sendOperation(res, await host.createStack(parsed.stack, options));
226
+ return;
227
+ }
228
+ if (req.method === "GET" && parts[1] === "stacks" && parts.length === 3) {
229
+ const loaded = host.getStack(parts[2]);
230
+ if (!loaded) {
231
+ sendJson(res, 404, { error: `Unknown preset: ${parts[2]}` });
232
+ return;
233
+ }
234
+ sendJson(res, 200, loaded);
235
+ return;
236
+ }
237
+ if (req.method === "PUT" && parts[1] === "stacks" && parts.length === 3) {
238
+ const body = await readJsonBody(req);
239
+ host = getCurrentHost ? getCurrentHost() : host;
240
+ const parsed = readStackPayload(body);
241
+ if (!parsed.ok) {
242
+ sendJson(res, 400, { error: parsed.error });
243
+ return;
244
+ }
245
+ const expectedSourceRevision = isPlainObject(body) && body.expectedSourceRevision !== undefined
246
+ ? body.expectedSourceRevision
247
+ : undefined;
248
+ if (expectedSourceRevision !== undefined && typeof expectedSourceRevision !== "string") {
249
+ sendJson(res, 400, { error: "expectedSourceRevision must be a string when provided." });
250
+ return;
251
+ }
252
+ const hostNow = getCurrentHost ? getCurrentHost() : host;
253
+ sendOperation(res, await hostNow.saveStack(parts[2], parsed.stack, expectedSourceRevision));
254
+ return;
255
+ }
256
+ if (req.method === "DELETE" && parts[1] === "stacks" && parts.length === 3) {
257
+ sendOperation(res, await host.deleteStack(parts[2]));
258
+ return;
259
+ }
260
+ if (req.method === "POST" && parts[1] === "stacks" && parts.length === 4 && parts[3] === "validate") {
261
+ const body = await readJsonBody(req);
262
+ host = getCurrentHost ? getCurrentHost() : host;
263
+ const parsed = readStackPayload(body);
264
+ if (!parsed.ok) {
265
+ sendJson(res, 400, { error: parsed.error });
266
+ return;
267
+ }
268
+ sendJson(res, 200, { diagnostics: host.validateStack(parsed.stack) });
269
+ return;
270
+ }
271
+ if (req.method === "POST" && parts[1] === "stacks" && parts.length === 4 && parts[3] === "preview") {
272
+ const body = await readJsonBody(req);
273
+ host = getCurrentHost ? getCurrentHost() : host;
274
+ const parsed = readStackPayload(body);
275
+ if (!parsed.ok) {
276
+ sendJson(res, 400, { error: parsed.error });
277
+ return;
278
+ }
279
+ sendOperation(res, host.previewStack(parts[2], parsed.stack));
280
+ return;
281
+ }
282
+ if (req.method === "GET" && parts[1] === "payload" && parts.length === 2) {
283
+ sendOperation(res, host.getPayload());
284
+ return;
285
+ }
286
+ if (req.method === "POST" && parts[1] === "payload" && parts.length === 3 && parts[2] === "arm") {
287
+ const body = await readJsonBody(req);
288
+ host = getCurrentHost ? getCurrentHost() : host;
289
+ const savePath = isPlainObject(body) && typeof body.savePath === "string" && body.savePath.trim() ? body.savePath.trim() : undefined;
290
+ sendOperation(res, host.armPayload(savePath));
291
+ return;
292
+ }
293
+ if (req.method === "DELETE" && parts[1] === "payload" && parts.length === 2) {
294
+ sendOperation(res, host.clearPayload());
295
+ return;
296
+ }
297
+ if (req.method === "GET" && parts[1] === "context-diff" && parts.length === 2) {
298
+ sendOperation(res, host.getContextDiff());
299
+ return;
300
+ }
301
+ if (parts[1] === "capabilities" && (parts.length === 2 || parts.length === 3)) {
302
+ const action = req.method === "GET" ? (parts.length === 2 ? "list" : "get")
303
+ : req.method === "POST" && parts.length === 2 ? "create"
304
+ : req.method === "POST" && parts[2] === "effective" ? "effective"
305
+ : req.method === "PUT" && parts.length === 3 ? "save"
306
+ : req.method === "DELETE" && parts.length === 3 ? "delete" : undefined;
307
+ if (!action) {
308
+ sendJson(res, 405, { ok: false, error: "Unsupported capability operation." });
309
+ return;
310
+ }
311
+ const body = req.method === "GET" ? undefined : await readJsonBody(req);
312
+ const hostNow = getCurrentHost ? getCurrentHost() : host;
313
+ if (!hostNow.capabilityOperation) {
314
+ sendJson(res, 503, { ok: false, error: "Capability resources unavailable." });
315
+ return;
316
+ }
317
+ if (action === "create" || action === "save" || action === "delete") {
318
+ try {
319
+ if (hostNow.isProjectTrusted?.() !== true) {
320
+ sendJson(res, 403, { ok: false, error: "Project is not trusted." });
321
+ return;
322
+ }
323
+ }
324
+ catch {
325
+ sendJson(res, 503, { ok: false, error: "Session replaced. Refresh before writing." });
326
+ return;
327
+ }
328
+ }
329
+ sendOperation(res, hostNow.capabilityOperation(action, parts[2], body));
330
+ return;
331
+ }
332
+ if (req.method === "GET" && parts[1] === "capability-state" && parts[2] === "available" && parts.length === 3) {
333
+ const hostNow = getCurrentHost ? getCurrentHost() : host;
334
+ if (!hostNow.readCapabilityChoices) {
335
+ sendJson(res, 503, { ok: false, error: "Capability runtime is unavailable." });
336
+ return;
337
+ }
338
+ const result = hostNow.readCapabilityChoices();
339
+ if (!result.ok) {
340
+ sendJson(res, result.status, { ok: false, error: result.error });
341
+ return;
342
+ }
343
+ sendJson(res, 200, { ok: true, state: result.state, choices: result.choices });
344
+ return;
345
+ }
346
+ if (req.method === "GET" && parts[1] === "capability-state" && parts[2] === "preview" && parts.length === 3) {
347
+ const hostNow = getCurrentHost ? getCurrentHost() : host;
348
+ sendOperation(res, hostNow.previewCapabilities
349
+ ? hostNow.previewCapabilities()
350
+ : { ok: false, status: 503, error: "Capability runtime is unavailable." });
351
+ return;
352
+ }
353
+ if (req.method === "POST" && parts[1] === "capability-state" && parts[2] === "enable" && parts.length === 3) {
354
+ const body = await readJsonBody(req);
355
+ host = getCurrentHost ? getCurrentHost() : host;
356
+ // Resolve the host only after consuming the body: session replacement during
357
+ // request parsing must never send activation to the retired host.
358
+ const hostNow = getCurrentHost ? getCurrentHost() : host;
359
+ try {
360
+ if (hostNow.isProjectTrusted?.() !== true) {
361
+ sendJson(res, 403, { ok: false, error: "Project is not trusted; refusing to activate capabilities." });
362
+ return;
363
+ }
364
+ }
365
+ catch {
366
+ sendJson(res, 503, { ok: false, error: "Capability session is unavailable. Refresh after session replacement." });
367
+ return;
368
+ }
369
+ if (!isCapabilityEnableRequest(body)) {
370
+ sendJson(res, 400, { ok: false, error: "Invalid capability activation payload." });
371
+ return;
372
+ }
373
+ if (!hostNow.enableCapability) {
374
+ sendJson(res, 503, { ok: false, error: "Capability runtime is unavailable." });
375
+ return;
376
+ }
377
+ const result = hostNow.enableCapability(body);
378
+ if (!result.ok) {
379
+ sendJson(res, result.status, { ok: false, error: result.error });
380
+ return;
381
+ }
382
+ sendJson(res, 200, { ok: true, state: result.state });
383
+ return;
384
+ }
385
+ if (req.method === "GET" && parts[1] === "capability-state" && parts.length === 2) {
386
+ const hostNow = getCurrentHost ? getCurrentHost() : host;
387
+ if (!hostNow.readCapabilityState) {
388
+ sendJson(res, 503, { ok: false, error: "Capability runtime is unavailable." });
389
+ return;
390
+ }
391
+ const result = hostNow.readCapabilityState();
392
+ if (!result.ok) {
393
+ sendJson(res, result.status, { ok: false, error: result.error });
394
+ return;
395
+ }
396
+ sendJson(res, 200, { ok: true, state: result.state });
397
+ return;
398
+ }
399
+ if (req.method === "POST" && parts[1] === "capability-state" && parts.length === 2) {
400
+ const body = await readJsonBody(req);
401
+ const hostNow = getCurrentHost ? getCurrentHost() : host;
402
+ try {
403
+ if (hostNow.isProjectTrusted?.() !== true) {
404
+ sendJson(res, 403, { ok: false, error: "Project is not trusted; refusing to mutate capabilities." });
405
+ return;
406
+ }
407
+ }
408
+ catch {
409
+ // Pi invalidates captured contexts during replacement/reload.
410
+ sendJson(res, 503, { ok: false, error: "Capability session is unavailable. Refresh after session replacement." });
411
+ return;
412
+ }
413
+ if (!isCapabilityStateMutation(body)) {
414
+ sendJson(res, 400, { ok: false, error: "Invalid capability state mutation payload." });
415
+ return;
416
+ }
417
+ if (!hostNow.mutateCapabilityState) {
418
+ sendJson(res, 503, { ok: false, error: "Capability runtime is unavailable." });
419
+ return;
420
+ }
421
+ const result = hostNow.mutateCapabilityState(body);
422
+ if (!result.ok) {
423
+ sendJson(res, result.status, { ok: false, error: result.error });
424
+ return;
425
+ }
426
+ sendJson(res, 200, { ok: true, state: result.state });
427
+ return;
428
+ }
429
+ if (req.method === "POST" && parts[1] === "stacks" && parts.length === 4 && parts[3] === "activate") {
430
+ sendOperation(res, host.activateStack(parts[2]));
431
+ return;
432
+ }
433
+ if (req.method === "POST" && parts[1] === "disable" && parts.length === 2) {
434
+ sendOperation(res, host.disableStacks());
435
+ return;
436
+ }
437
+ if (req.method === "POST" && parts[1] === "reload" && parts.length === 2) {
438
+ sendOperation(res, await host.reloadStacks());
439
+ return;
440
+ }
441
+ sendJson(res, 404, { error: "Unknown pi-forge editor API route." });
442
+ }
443
+ function hasValidToken(req, url, token) {
444
+ const header = req.headers["x-pi-forge-token"];
445
+ return header === token || url.searchParams.get("token") === token;
446
+ }
447
+ async function readJsonBody(req) {
448
+ const chunks = [];
449
+ let size = 0;
450
+ const maxBytes = 2_000_000;
451
+ for await (const chunk of req) {
452
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
453
+ size += buffer.length;
454
+ if (size > maxBytes)
455
+ throw new RequestBodyError(413, "Request body is too large.");
456
+ chunks.push(buffer);
457
+ }
458
+ const text = Buffer.concat(chunks).toString("utf8");
459
+ if (!text.trim())
460
+ return {};
461
+ try {
462
+ return JSON.parse(text);
463
+ }
464
+ catch {
465
+ throw new RequestBodyError(400, "Request body must contain valid JSON.");
466
+ }
467
+ }
468
+ function readStackPayload(body) {
469
+ const rawStack = isPlainObject(body) && "stack" in body ? body.stack : body;
470
+ if (!isPlainObject(rawStack))
471
+ return { ok: false, error: "Preset payload must be a JSON object." };
472
+ if (typeof rawStack.id !== "string" || !rawStack.id.trim())
473
+ return { ok: false, error: "Preset id must be a non-empty string." };
474
+ if (!Array.isArray(rawStack.items))
475
+ return { ok: false, error: "Preset must contain a Stack items array." };
476
+ for (const [index, item] of rawStack.items.entries()) {
477
+ if (!isPlainObject(item))
478
+ return { ok: false, error: `Item ${index + 1} must be an object.` };
479
+ if (item.kind !== "block" && item.kind !== "slot")
480
+ return { ok: false, error: `Item ${index + 1} kind must be block or slot.` };
481
+ if (typeof item.id !== "string" || !item.id.trim())
482
+ return { ok: false, error: `Item ${index + 1} id must be a non-empty string.` };
483
+ if (item.kind === "block" && typeof item.content !== "string")
484
+ return { ok: false, error: `Block item ${item.id} content must be a string.` };
485
+ if (item.kind === "slot" && typeof item.slot !== "string")
486
+ return { ok: false, error: `Slot item ${item.id} slot must be a string.` };
487
+ }
488
+ return { ok: true, stack: rawStack };
489
+ }
490
+ function readProfilePayload(body) {
491
+ const raw = isPlainObject(body) && "profile" in body ? body.profile : body;
492
+ if (!isPlainObject(raw))
493
+ return { ok: false, error: "Profile payload must be a JSON object." };
494
+ const allowedFields = new Set([
495
+ "schemaVersion",
496
+ "type",
497
+ "id",
498
+ "name",
499
+ "description",
500
+ "autoActivate",
501
+ "model",
502
+ "thinkingLevel",
503
+ "promptStack",
504
+ ]);
505
+ const unsupported = Object.keys(raw).find((field) => !allowedFields.has(field));
506
+ if (unsupported)
507
+ return { ok: false, error: `Unsupported profile field: ${unsupported}` };
508
+ if (raw.schemaVersion !== 1)
509
+ return { ok: false, error: "Profile schemaVersion must be 1." };
510
+ if (raw.type !== AGENT_PROFILE_TYPE)
511
+ return { ok: false, error: `Profile type must be "${AGENT_PROFILE_TYPE}".` };
512
+ if (typeof raw.id !== "string")
513
+ return { ok: false, error: "Profile id must be a string." };
514
+ if (raw.name !== undefined && typeof raw.name !== "string")
515
+ return { ok: false, error: "Profile name must be a string when provided." };
516
+ if (raw.description !== undefined && typeof raw.description !== "string") {
517
+ return { ok: false, error: "Profile description must be a string when provided." };
518
+ }
519
+ if (raw.autoActivate !== undefined && typeof raw.autoActivate !== "boolean") {
520
+ return { ok: false, error: "Profile autoActivate must be a boolean when provided." };
521
+ }
522
+ if (!isPlainObject(raw.model))
523
+ return { ok: false, error: "Profile model must be an object." };
524
+ const unsupportedModelField = Object.keys(raw.model).find((field) => field !== "provider" && field !== "id");
525
+ if (unsupportedModelField)
526
+ return { ok: false, error: `Unsupported profile model field: ${unsupportedModelField}` };
527
+ if (typeof raw.model.provider !== "string" || typeof raw.model.id !== "string") {
528
+ return { ok: false, error: "Profile model provider and id must be strings." };
529
+ }
530
+ if (typeof raw.thinkingLevel !== "string"
531
+ || !AGENT_PROFILE_THINKING_LEVELS.includes(raw.thinkingLevel)) {
532
+ return { ok: false, error: `Unsupported profile thinkingLevel: ${String(raw.thinkingLevel)}` };
533
+ }
534
+ if (raw.promptStack !== null && typeof raw.promptStack !== "string") {
535
+ return { ok: false, error: "Profile promptStack must be a string or null." };
536
+ }
537
+ return {
538
+ ok: true,
539
+ profile: {
540
+ schemaVersion: 1,
541
+ type: AGENT_PROFILE_TYPE,
542
+ id: raw.id,
543
+ name: raw.name,
544
+ description: raw.description,
545
+ autoActivate: raw.autoActivate,
546
+ model: {
547
+ provider: raw.model.provider,
548
+ id: raw.model.id,
549
+ },
550
+ thinkingLevel: raw.thinkingLevel,
551
+ promptStack: raw.promptStack,
552
+ },
553
+ };
554
+ }
555
+ function sendOperation(res, result) {
556
+ if (!result.ok) {
557
+ sendJson(res, result.status ?? 400, { error: result.error });
558
+ return;
559
+ }
560
+ sendJson(res, 200, result);
561
+ }
562
+ function sendHtml(res, html) {
563
+ res.writeHead(200, {
564
+ "content-type": "text/html; charset=utf-8",
565
+ "cache-control": "no-store",
566
+ "connection": "close",
567
+ });
568
+ res.end(html);
569
+ }
570
+ function sendText(res, status, text) {
571
+ res.writeHead(status, {
572
+ "content-type": "text/plain; charset=utf-8",
573
+ "cache-control": "no-store",
574
+ "connection": "close",
575
+ });
576
+ res.end(text);
577
+ }
578
+ /**
579
+ * Resolve the initial page language: an explicit configured locale wins;
580
+ * "auto" follows the browser's Accept-Language header. The client keeps
581
+ * document.documentElement.lang in sync when the user switches locale.
582
+ */
583
+ function resolvePageLang(host, req) {
584
+ const configured = host.getEditorConfig().locale;
585
+ if (configured === "en" || configured === "zh-CN")
586
+ return configured;
587
+ const accepted = req.headers["accept-language"] ?? "";
588
+ const ranges = accepted
589
+ .split(",")
590
+ .map((entry, index) => {
591
+ const [rawRange = "", ...parameters] = entry.trim().split(";");
592
+ let quality = 1;
593
+ for (const parameter of parameters) {
594
+ if (!/^q\s*=/iu.test(parameter.trim()))
595
+ continue;
596
+ const match = parameter.trim().match(/^q\s*=\s*(0(?:\.\d{0,3})?|\.\d{1,3}|1(?:\.0{0,3})?)$/iu);
597
+ quality = match ? Number(match[1]) : 0;
598
+ break;
599
+ }
600
+ return { range: rawRange.toLowerCase(), quality, index };
601
+ })
602
+ .filter(({ range, quality }) => range.length > 0 && quality > 0)
603
+ .sort((a, b) => b.quality - a.quality || a.index - b.index);
604
+ for (const { range } of ranges) {
605
+ if (range === "zh" || range.startsWith("zh-"))
606
+ return "zh-CN";
607
+ if (range === "en" || range.startsWith("en-") || range === "*")
608
+ return "en";
609
+ }
610
+ return "en";
611
+ }
612
+ function sendJson(res, status, value) {
613
+ res.writeHead(status, {
614
+ "content-type": "application/json; charset=utf-8",
615
+ "cache-control": "no-store",
616
+ "connection": "close",
617
+ });
618
+ res.end(JSON.stringify(value));
619
+ }
620
+ function closeServer(server, sockets) {
621
+ return new Promise((resolve, reject) => {
622
+ server.close((error) => error ? reject(error) : resolve());
623
+ for (const socket of sockets)
624
+ socket.destroy();
625
+ });
626
+ }
627
+ function parseScope(value) {
628
+ if (value === undefined)
629
+ return { ok: true };
630
+ if (value === "project" || value === "global")
631
+ return { ok: true, scope: value };
632
+ return { ok: false, error: 'scope must be "project" or "global".' };
633
+ }
634
+ function isPlainObject(value) {
635
+ return !!value && typeof value === "object" && !Array.isArray(value);
636
+ }
637
+ //# sourceMappingURL=server.js.map