@aglyn/plugins-ai 1.0.0-beta.229 → 1.0.0-beta.231

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 (340) hide show
  1. package/README.md +3 -3
  2. package/package.json +17 -17
  3. package/src/lib/activity/ai-activity-actions.js +7 -1
  4. package/src/lib/activity/ai-activity-actions.js.map +1 -1
  5. package/src/lib/components/ai-assist-provider-on-host.component.d.ts +66 -0
  6. package/src/lib/components/ai-assist-provider-on-host.component.js +78 -0
  7. package/src/lib/components/ai-assist-provider-on-host.component.js.map +1 -0
  8. package/src/lib/components/ai-assist-provider.component.js +9 -10
  9. package/src/lib/components/ai-assist-provider.component.js.map +1 -1
  10. package/src/lib/components/ai-automation-zones.d.ts +38 -0
  11. package/src/lib/components/ai-automation-zones.js +5 -1
  12. package/src/lib/components/ai-automation-zones.js.map +1 -1
  13. package/src/lib/components/ai-brief-dialog.component.d.ts +20 -2
  14. package/src/lib/components/ai-brief-dialog.component.js +87 -8
  15. package/src/lib/components/ai-brief-dialog.component.js.map +1 -1
  16. package/src/lib/components/ai-campaign-create.component.d.ts +54 -0
  17. package/src/lib/components/ai-campaign-create.component.js +270 -0
  18. package/src/lib/components/ai-campaign-create.component.js.map +1 -0
  19. package/src/lib/components/ai-describe-automation.component.d.ts +8 -3
  20. package/src/lib/components/ai-describe-automation.component.js +15 -6
  21. package/src/lib/components/ai-describe-automation.component.js.map +1 -1
  22. package/src/lib/components/ai-describe-button.component.d.ts +20 -8
  23. package/src/lib/components/ai-describe-button.component.js +19 -36
  24. package/src/lib/components/ai-describe-button.component.js.map +1 -1
  25. package/src/lib/components/ai-describe-email.component.d.ts +31 -0
  26. package/src/lib/components/ai-describe-email.component.js +22 -0
  27. package/src/lib/components/ai-describe-email.component.js.map +1 -0
  28. package/src/lib/components/ai-describe-org-automation.component.d.ts +36 -0
  29. package/src/lib/components/ai-describe-org-automation.component.js +238 -0
  30. package/src/lib/components/ai-describe-org-automation.component.js.map +1 -0
  31. package/src/lib/components/ai-experiment-cards.component.d.ts +9 -1
  32. package/src/lib/components/ai-experiment-cards.component.js +104 -18
  33. package/src/lib/components/ai-experiment-cards.component.js.map +1 -1
  34. package/src/lib/components/ai-explain-automation.component.js +1 -5
  35. package/src/lib/components/ai-explain-automation.component.js.map +1 -1
  36. package/src/lib/components/ai-funnel-zones.component.d.ts +61 -0
  37. package/src/lib/components/ai-funnel-zones.component.js +178 -0
  38. package/src/lib/components/ai-funnel-zones.component.js.map +1 -0
  39. package/src/lib/components/ai-insight-card.component.d.ts +42 -0
  40. package/src/lib/components/ai-insight-card.component.js +137 -0
  41. package/src/lib/components/ai-insight-card.component.js.map +1 -0
  42. package/src/lib/components/ai-insight-dialog.component.d.ts +6 -0
  43. package/src/lib/components/ai-insight-dialog.component.js +9 -3
  44. package/src/lib/components/ai-insight-dialog.component.js.map +1 -1
  45. package/src/lib/components/ai-job-plan.component.d.ts +7 -2
  46. package/src/lib/components/ai-job-plan.component.js +38 -6
  47. package/src/lib/components/ai-job-plan.component.js.map +1 -1
  48. package/src/lib/components/ai-job-requests.d.ts +17 -1
  49. package/src/lib/components/ai-job-requests.js +39 -3
  50. package/src/lib/components/ai-job-requests.js.map +1 -1
  51. package/src/lib/components/ai-logic-zones.d.ts +64 -0
  52. package/src/lib/components/ai-logic-zones.js +18 -0
  53. package/src/lib/components/ai-logic-zones.js.map +1 -0
  54. package/src/lib/components/ai-logic.component.d.ts +77 -0
  55. package/src/lib/components/ai-logic.component.js +553 -0
  56. package/src/lib/components/ai-logic.component.js.map +1 -0
  57. package/src/lib/components/ai-marketing-insight.component.d.ts +30 -0
  58. package/src/lib/components/ai-marketing-insight.component.js +80 -0
  59. package/src/lib/components/ai-marketing-insight.component.js.map +1 -0
  60. package/src/lib/components/ai-media-create.component.d.ts +55 -0
  61. package/src/lib/components/ai-media-create.component.js +433 -0
  62. package/src/lib/components/ai-media-create.component.js.map +1 -0
  63. package/src/lib/components/ai-overlay-cards.component.d.ts +83 -0
  64. package/src/lib/components/ai-overlay-cards.component.js +505 -0
  65. package/src/lib/components/ai-overlay-cards.component.js.map +1 -0
  66. package/src/lib/components/ai-permissions-on-host.component.d.ts +3 -46
  67. package/src/lib/components/ai-permissions-on-host.component.js +5 -53
  68. package/src/lib/components/ai-permissions-on-host.component.js.map +1 -1
  69. package/src/lib/components/ai-product-zones.d.ts +5 -0
  70. package/src/lib/components/ai-product-zones.js +1 -1
  71. package/src/lib/components/ai-product-zones.js.map +1 -1
  72. package/src/lib/components/ai-products-create-button.component.d.ts +42 -0
  73. package/src/lib/components/ai-products-create-button.component.js +67 -0
  74. package/src/lib/components/ai-products-create-button.component.js.map +1 -0
  75. package/src/lib/components/ai-products-hub-card.component.js +100 -81
  76. package/src/lib/components/ai-products-hub-card.component.js.map +1 -1
  77. package/src/lib/components/ai-revise-automation.component.d.ts +16 -0
  78. package/src/lib/components/ai-revise-automation.component.js +207 -0
  79. package/src/lib/components/ai-revise-automation.component.js.map +1 -0
  80. package/src/lib/components/ai-site-build-page.component.js +160 -24
  81. package/src/lib/components/ai-site-build-page.component.js.map +1 -1
  82. package/src/lib/components/ai-upsell-dialog.component.d.ts +53 -0
  83. package/src/lib/components/ai-upsell-dialog.component.js +177 -0
  84. package/src/lib/components/ai-upsell-dialog.component.js.map +1 -0
  85. package/src/lib/components/assist-build-card.component.d.ts +42 -0
  86. package/src/lib/components/assist-build-card.component.js +183 -0
  87. package/src/lib/components/assist-build-card.component.js.map +1 -0
  88. package/src/lib/components/assist-edit-canvas.d.ts +2 -0
  89. package/src/lib/components/assist-edit-canvas.js +56 -1
  90. package/src/lib/components/assist-edit-canvas.js.map +1 -1
  91. package/src/lib/components/assist-jobs-drawer.component.js +48 -3
  92. package/src/lib/components/assist-jobs-drawer.component.js.map +1 -1
  93. package/src/lib/components/assist-panel.component.js +51 -2
  94. package/src/lib/components/assist-panel.component.js.map +1 -1
  95. package/src/lib/components/use-ai-job-run.d.ts +3 -14
  96. package/src/lib/components/use-ai-job-run.js +0 -50
  97. package/src/lib/components/use-ai-job-run.js.map +1 -1
  98. package/src/lib/jobs/ai-build-capabilities.d.ts +58 -0
  99. package/src/lib/jobs/ai-build-capabilities.js +256 -0
  100. package/src/lib/jobs/ai-build-capabilities.js.map +1 -0
  101. package/src/lib/jobs/ai-build-unit-outcome.d.ts +52 -0
  102. package/src/lib/jobs/ai-build-unit-outcome.js +140 -0
  103. package/src/lib/jobs/ai-build-unit-outcome.js.map +1 -0
  104. package/src/lib/jobs/ai-job-auto-confirm.d.ts +8 -0
  105. package/src/lib/jobs/ai-job-auto-confirm.js +30 -6
  106. package/src/lib/jobs/ai-job-auto-confirm.js.map +1 -1
  107. package/src/lib/jobs/ai-job-build-step.d.ts +144 -0
  108. package/src/lib/jobs/ai-job-build-step.js +668 -0
  109. package/src/lib/jobs/ai-job-build-step.js.map +1 -0
  110. package/src/lib/jobs/ai-job-draft-ids.js +9 -1
  111. package/src/lib/jobs/ai-job-draft-ids.js.map +1 -1
  112. package/src/lib/jobs/ai-job-drafts.d.ts +14 -4
  113. package/src/lib/jobs/ai-job-drafts.js +25 -4
  114. package/src/lib/jobs/ai-job-drafts.js.map +1 -1
  115. package/src/lib/jobs/ai-job-generation.d.ts +8 -0
  116. package/src/lib/jobs/ai-job-generation.js +13 -2
  117. package/src/lib/jobs/ai-job-generation.js.map +1 -1
  118. package/src/lib/jobs/ai-job-insight-step.js +8 -1
  119. package/src/lib/jobs/ai-job-insight-step.js.map +1 -1
  120. package/src/lib/jobs/ai-job-layout-language.d.ts +73 -0
  121. package/src/lib/jobs/ai-job-layout-language.js +194 -0
  122. package/src/lib/jobs/ai-job-layout-language.js.map +1 -0
  123. package/src/lib/jobs/ai-job-layout-site-pages.d.ts +13 -2
  124. package/src/lib/jobs/ai-job-layout-site-pages.js +18 -2
  125. package/src/lib/jobs/ai-job-layout-site-pages.js.map +1 -1
  126. package/src/lib/jobs/ai-job-layout-step.d.ts +5 -0
  127. package/src/lib/jobs/ai-job-layout-step.js +105 -5
  128. package/src/lib/jobs/ai-job-layout-step.js.map +1 -1
  129. package/src/lib/jobs/ai-job-logic-step.d.ts +107 -0
  130. package/src/lib/jobs/ai-job-logic-step.js +395 -0
  131. package/src/lib/jobs/ai-job-logic-step.js.map +1 -0
  132. package/src/lib/jobs/ai-job-overlay-copy.d.ts +70 -0
  133. package/src/lib/jobs/ai-job-overlay-copy.js +178 -0
  134. package/src/lib/jobs/ai-job-overlay-copy.js.map +1 -0
  135. package/src/lib/jobs/ai-job-page-language.d.ts +149 -0
  136. package/src/lib/jobs/ai-job-page-language.js +355 -0
  137. package/src/lib/jobs/ai-job-page-language.js.map +1 -0
  138. package/src/lib/jobs/ai-job-page-sections.d.ts +37 -1
  139. package/src/lib/jobs/ai-job-page-sections.js +93 -11
  140. package/src/lib/jobs/ai-job-page-sections.js.map +1 -1
  141. package/src/lib/jobs/ai-job-page-step.d.ts +4 -4
  142. package/src/lib/jobs/ai-job-page-step.js +111 -20
  143. package/src/lib/jobs/ai-job-page-step.js.map +1 -1
  144. package/src/lib/jobs/ai-job-plan-step.d.ts +27 -2
  145. package/src/lib/jobs/ai-job-plan-step.js +87 -13
  146. package/src/lib/jobs/ai-job-plan-step.js.map +1 -1
  147. package/src/lib/jobs/ai-job-site-step.d.ts +19 -1
  148. package/src/lib/jobs/ai-job-site-step.js +220 -61
  149. package/src/lib/jobs/ai-job-site-step.js.map +1 -1
  150. package/src/lib/jobs/ai-job-text-step.d.ts +22 -1
  151. package/src/lib/jobs/ai-job-text-step.js +12 -0
  152. package/src/lib/jobs/ai-job-text-step.js.map +1 -1
  153. package/src/lib/jobs/ai-job-workflow-step.d.ts +54 -4
  154. package/src/lib/jobs/ai-job-workflow-step.js +320 -15
  155. package/src/lib/jobs/ai-job-workflow-step.js.map +1 -1
  156. package/src/lib/jobs/ai-jobs.d.ts +80 -2
  157. package/src/lib/jobs/ai-jobs.js +391 -18
  158. package/src/lib/jobs/ai-jobs.js.map +1 -1
  159. package/src/lib/jobs/ai-layout-full-height.d.ts +40 -0
  160. package/src/lib/jobs/ai-layout-full-height.js +82 -0
  161. package/src/lib/jobs/ai-layout-full-height.js.map +1 -0
  162. package/src/lib/jobs/ai-layout-site-facts.d.ts +54 -0
  163. package/src/lib/jobs/ai-layout-site-facts.js +153 -0
  164. package/src/lib/jobs/ai-layout-site-facts.js.map +1 -0
  165. package/src/lib/jobs/ai-logic-records.d.ts +40 -0
  166. package/src/lib/jobs/ai-logic-records.js +55 -0
  167. package/src/lib/jobs/ai-logic-records.js.map +1 -0
  168. package/src/lib/jobs/ai-site-publish.d.ts +7 -0
  169. package/src/lib/jobs/ai-site-publish.js +9 -0
  170. package/src/lib/jobs/ai-site-publish.js.map +1 -1
  171. package/src/lib/jobs/ai-workflow-records.d.ts +4 -0
  172. package/src/lib/jobs/ai-workflow-records.js +40 -1
  173. package/src/lib/jobs/ai-workflow-records.js.map +1 -1
  174. package/src/lib/layout-language/ai-layout-compiler.d.ts +145 -0
  175. package/src/lib/layout-language/ai-layout-compiler.js +1148 -0
  176. package/src/lib/layout-language/ai-layout-compiler.js.map +1 -0
  177. package/src/lib/layout-language/ai-layout-copy.d.ts +73 -0
  178. package/src/lib/layout-language/ai-layout-copy.js +137 -0
  179. package/src/lib/layout-language/ai-layout-copy.js.map +1 -0
  180. package/src/lib/layout-language/ai-layout-frame.d.ts +52 -0
  181. package/src/lib/layout-language/ai-layout-frame.js +457 -0
  182. package/src/lib/layout-language/ai-layout-frame.js.map +1 -0
  183. package/src/lib/layout-language/ai-layout-gaps.d.ts +63 -0
  184. package/src/lib/layout-language/ai-layout-gaps.js +345 -0
  185. package/src/lib/layout-language/ai-layout-gaps.js.map +1 -0
  186. package/src/lib/layout-language/ai-layout-language.d.ts +161 -0
  187. package/src/lib/layout-language/ai-layout-language.js +446 -0
  188. package/src/lib/layout-language/ai-layout-language.js.map +1 -0
  189. package/src/lib/layout-language/ai-layout-links.d.ts +80 -0
  190. package/src/lib/layout-language/ai-layout-links.js +109 -0
  191. package/src/lib/layout-language/ai-layout-links.js.map +1 -0
  192. package/src/lib/layout-language/ai-layout-store.d.ts +39 -0
  193. package/src/lib/layout-language/ai-layout-store.js +69 -0
  194. package/src/lib/layout-language/ai-layout-store.js.map +1 -0
  195. package/src/lib/layout-language/ai-layout-tree.d.ts +59 -0
  196. package/src/lib/layout-language/ai-layout-tree.js +84 -0
  197. package/src/lib/layout-language/ai-layout-tree.js.map +1 -0
  198. package/src/lib/lazy-widget.d.ts +45 -0
  199. package/src/lib/lazy-widget.js +53 -0
  200. package/src/lib/lazy-widget.js.map +1 -0
  201. package/src/lib/model/ai-automation-draft.d.ts +18 -0
  202. package/src/lib/model/ai-automation-draft.js +86 -0
  203. package/src/lib/model/ai-automation-draft.js.map +1 -1
  204. package/src/lib/model/ai-build-job.d.ts +201 -0
  205. package/src/lib/model/ai-build-job.js +472 -0
  206. package/src/lib/model/ai-build-job.js.map +1 -0
  207. package/src/lib/model/ai-build-plan.d.ts +61 -0
  208. package/src/lib/model/ai-build-plan.js +154 -9
  209. package/src/lib/model/ai-build-plan.js.map +1 -1
  210. package/src/lib/model/ai-build-progress.d.ts +73 -0
  211. package/src/lib/model/ai-build-progress.js +170 -0
  212. package/src/lib/model/ai-build-progress.js.map +1 -0
  213. package/src/lib/model/ai-host-publish-context.d.ts +51 -0
  214. package/src/lib/model/ai-host-publish-context.js +104 -0
  215. package/src/lib/model/ai-host-publish-context.js.map +1 -0
  216. package/src/lib/model/ai-insight.d.ts +20 -10
  217. package/src/lib/model/ai-insight.js +72 -9
  218. package/src/lib/model/ai-insight.js.map +1 -1
  219. package/src/lib/model/ai-job-activity.d.ts +22 -0
  220. package/src/lib/model/ai-job-activity.js +25 -2
  221. package/src/lib/model/ai-job-activity.js.map +1 -1
  222. package/src/lib/model/ai-job-failure-copy.d.ts +38 -1
  223. package/src/lib/model/ai-job-failure-copy.js +56 -1
  224. package/src/lib/model/ai-job-failure-copy.js.map +1 -1
  225. package/src/lib/model/ai-job-notice.d.ts +3 -1
  226. package/src/lib/model/ai-job-notice.js +31 -9
  227. package/src/lib/model/ai-job-notice.js.map +1 -1
  228. package/src/lib/model/ai-jobs.types.d.ts +99 -2
  229. package/src/lib/model/ai-jobs.types.js +3 -1
  230. package/src/lib/model/ai-jobs.types.js.map +1 -1
  231. package/src/lib/model/ai-logic-job.d.ts +108 -0
  232. package/src/lib/model/ai-logic-job.js +327 -0
  233. package/src/lib/model/ai-logic-job.js.map +1 -0
  234. package/src/lib/model/ai-media-credits.d.ts +52 -0
  235. package/src/lib/model/ai-media-credits.js +61 -0
  236. package/src/lib/model/ai-media-credits.js.map +1 -0
  237. package/src/lib/model/ai-overlay-copy.d.ts +114 -0
  238. package/src/lib/model/ai-overlay-copy.js +175 -0
  239. package/src/lib/model/ai-overlay-copy.js.map +1 -0
  240. package/src/lib/model/ai-site-build-progress.d.ts +25 -4
  241. package/src/lib/model/ai-site-build-progress.js +194 -11
  242. package/src/lib/model/ai-site-build-progress.js.map +1 -1
  243. package/src/lib/model/ai-site-job.d.ts +8 -0
  244. package/src/lib/model/ai-site-job.js +9 -0
  245. package/src/lib/model/ai-site-job.js.map +1 -1
  246. package/src/lib/model/ai-site-start-seo.d.ts +1 -1
  247. package/src/lib/model/ai-site-start.d.ts +5 -2
  248. package/src/lib/model/ai-site-start.js +5 -2
  249. package/src/lib/model/ai-site-start.js.map +1 -1
  250. package/src/lib/model/ai-svg.d.ts +69 -0
  251. package/src/lib/model/ai-svg.js +265 -0
  252. package/src/lib/model/ai-svg.js.map +1 -0
  253. package/src/lib/model/ai-usage-by-user.d.ts +3 -2
  254. package/src/lib/model/ai-usage-by-user.js.map +1 -1
  255. package/src/lib/model/ai-workflow-job.d.ts +92 -4
  256. package/src/lib/model/ai-workflow-job.js +124 -7
  257. package/src/lib/model/ai-workflow-job.js.map +1 -1
  258. package/src/lib/model/assist-build.d.ts +58 -0
  259. package/src/lib/model/assist-build.js +52 -0
  260. package/src/lib/model/assist-build.js.map +1 -0
  261. package/src/lib/model/assist-edit.d.ts +48 -3
  262. package/src/lib/model/assist-edit.js +48 -4
  263. package/src/lib/model/assist-edit.js.map +1 -1
  264. package/src/lib/plugin-config.js +2 -0
  265. package/src/lib/plugin-config.js.map +1 -1
  266. package/src/lib/plugin.js +308 -47
  267. package/src/lib/plugin.js.map +1 -1
  268. package/src/lib/providers/catalog.d.ts +82 -5
  269. package/src/lib/providers/catalog.js +83 -12
  270. package/src/lib/providers/catalog.js.map +1 -1
  271. package/src/lib/providers/image-contract.d.ts +116 -0
  272. package/src/lib/providers/image-contract.js +74 -0
  273. package/src/lib/providers/image-contract.js.map +1 -0
  274. package/src/lib/providers/model-choice.js +11 -0
  275. package/src/lib/providers/model-choice.js.map +1 -1
  276. package/src/lib/providers/routing.js +18 -0
  277. package/src/lib/providers/routing.js.map +1 -1
  278. package/src/lib/providers/vertex-image.d.ts +121 -0
  279. package/src/lib/providers/vertex-image.js +312 -0
  280. package/src/lib/providers/vertex-image.js.map +1 -0
  281. package/src/lib/runtime/ai-doctrine-validators.d.ts +101 -0
  282. package/src/lib/runtime/ai-doctrine-validators.js +525 -4
  283. package/src/lib/runtime/ai-doctrine-validators.js.map +1 -1
  284. package/src/lib/runtime/ai-doctrine.js +21 -6
  285. package/src/lib/runtime/ai-doctrine.js.map +1 -1
  286. package/src/lib/runtime/ai-eval-live.js +4 -0
  287. package/src/lib/runtime/ai-eval-live.js.map +1 -1
  288. package/src/lib/runtime/ai-eval.d.ts +15 -1
  289. package/src/lib/runtime/ai-eval.js +52 -1
  290. package/src/lib/runtime/ai-eval.js.map +1 -1
  291. package/src/lib/runtime/ai-icon-library.d.ts +257 -0
  292. package/src/lib/runtime/ai-icon-library.js +254 -0
  293. package/src/lib/runtime/ai-icon-library.js.map +1 -0
  294. package/src/lib/runtime/ai-node-tree.d.ts +9 -0
  295. package/src/lib/runtime/ai-node-tree.js +30 -4
  296. package/src/lib/runtime/ai-node-tree.js.map +1 -1
  297. package/src/lib/runtime/ai-palette.d.ts +6 -0
  298. package/src/lib/runtime/ai-palette.generated.js +70 -0
  299. package/src/lib/runtime/ai-palette.generated.js.map +1 -1
  300. package/src/lib/runtime/ai-palette.js.map +1 -1
  301. package/src/lib/runtime/experiment-variant-copy.d.ts +34 -0
  302. package/src/lib/runtime/experiment-variant-copy.js +102 -0
  303. package/src/lib/runtime/experiment-variant-copy.js.map +1 -0
  304. package/src/lib/server/ai-experiment-versions.d.ts +51 -0
  305. package/src/lib/server/ai-experiment-versions.js +270 -0
  306. package/src/lib/server/ai-experiment-versions.js.map +1 -0
  307. package/src/lib/server/ai-jobs-resume.js +74 -4
  308. package/src/lib/server/ai-jobs-resume.js.map +1 -1
  309. package/src/lib/server/ai-media-image.d.ts +66 -0
  310. package/src/lib/server/ai-media-image.js +521 -0
  311. package/src/lib/server/ai-media-image.js.map +1 -0
  312. package/src/lib/server/ai-media-svg.d.ts +91 -0
  313. package/src/lib/server/ai-media-svg.js +186 -0
  314. package/src/lib/server/ai-media-svg.js.map +1 -0
  315. package/src/lib/server/assist-build.d.ts +54 -0
  316. package/src/lib/server/assist-build.js +121 -0
  317. package/src/lib/server/assist-build.js.map +1 -0
  318. package/src/lib/server/assist-chat.js +108 -12
  319. package/src/lib/server/assist-chat.js.map +1 -1
  320. package/src/lib/server/assist-edit.js +213 -3
  321. package/src/lib/server/assist-edit.js.map +1 -1
  322. package/src/lib/server/assist-view-context.js +242 -4
  323. package/src/lib/server/assist-view-context.js.map +1 -1
  324. package/src/lib/server.js +26 -0
  325. package/src/lib/server.js.map +1 -1
  326. package/src/lib/tools/ai-logic-tool.d.ts +27 -0
  327. package/src/lib/tools/ai-logic-tool.js +207 -0
  328. package/src/lib/tools/ai-logic-tool.js.map +1 -0
  329. package/src/lib/tools/ai-overlay-tool.d.ts +35 -0
  330. package/src/lib/tools/ai-overlay-tool.js +107 -0
  331. package/src/lib/tools/ai-overlay-tool.js.map +1 -0
  332. package/src/lib/tools/ai-workflow-tool.d.ts +17 -2
  333. package/src/lib/tools/ai-workflow-tool.js +47 -14
  334. package/src/lib/tools/ai-workflow-tool.js.map +1 -1
  335. package/src/lib/usage/assist-job-refund.d.ts +28 -1
  336. package/src/lib/usage/assist-job-refund.js +44 -9
  337. package/src/lib/usage/assist-job-refund.js.map +1 -1
  338. package/src/lib/usage/assist-usage.d.ts +5 -0
  339. package/src/lib/usage/assist-usage.js +2 -0
  340. package/src/lib/usage/assist-usage.js.map +1 -1
@@ -0,0 +1,668 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Aglyn LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */ import { _ as _extends } from "@swc/helpers/_/_extends";
17
+ import { resolveEffectivePlan } from "@aglyn/aglyn/app-utils/plan-entitlements";
18
+ import { resolveOrgIdForHost } from "@aglyn/tenant-data-admin/server/organizations";
19
+ import { AI_BUILD_LIMITS, AI_BUILD_PUBLISH_INPUT, aiArticle, aiBuildCreditEstimate, aiBuildDegradation, aiBuildInitialLedger, aiBuildItemDelivered, aiBuildItemOpen, aiBuildNextUnit, aiBuildOrder, aiBuildPlacedItemLines, aiBuildPlanShapeRefusal, aiBuildUnits } from "../model/ai-build-job.js";
20
+ import { isAiPlanNewRef } from "../model/ai-build-plan.js";
21
+ import { AI_SITE_MAX_SECTIONS } from "../model/ai-site-job.js";
22
+ import { FREE_AI_TASTE_CREDITS_PER_MONTH } from "../plan-entitlements.js";
23
+ import { aiModelForStep } from "../providers/routing.js";
24
+ import { AI_BUILD_UNIT_EMPTY_COPY, aiBuildBuiltRefs, aiUnitErrorRetryable, aiUnitFailure, aiUnitSpend } from "./ai-build-unit-outcome.js";
25
+ import { assistCreditsFromUsd } from "../usage/assist-credits.js";
26
+ import { ASSIST_RETURNED_USD_FIELD, assistSpendAfterReturnsUsd } from "../usage/assist-credit-returns.js";
27
+ import { freeAccountUsageRef, freeAssistAccount } from "../usage/assist-free-taste.js";
28
+ import { aiBuildOps } from "./ai-build-capabilities.js";
29
+ import { aiJobAdmissionRefusal, registerAiJobAdmission } from "./ai-job-admission.js";
30
+ import { aiOriginJobId } from "./ai-job-draft-ids.js";
31
+ import { readAiDraftNodes } from "./ai-job-drafts.js";
32
+ import { aiConfirmedPlan, aiUnspentOutcome } from "./ai-job-generation.js";
33
+ import { AI_JOB_PAGE_STEP_MINIMUM_MS } from "./ai-job-page-budget.js";
34
+ import { aiPluginDraftAdmissionRefusal, aiPluginDraftWriter } from "./ai-job-plugin-drafts.js";
35
+ import { aiSitePageWritten, aiSiteResolvedRef } from "./ai-job-site-step.js";
36
+ import { AI_LAYOUT_SITE_PAGES_INPUT, aiLayoutSitePagesOfPlan } from "./ai-job-layout-site-pages.js";
37
+ import { AI_LAYOUT_FORM_PAGE_INPUT, AI_LAYOUT_LANGUAGE_INPUT, aiLayoutFormPageOfPlan } from "./ai-job-page-language.js";
38
+ import { AI_JOB_BRIEF_MAX_CHARS } from "./ai-job-text-step.js";
39
+ import { aiJobPublishesBuild, aiPublishGuidedSite } from "./ai-site-publish.js";
40
+ import { AI_PLANNED_JOB_KINDS, aiJobStepRunMinimumMs, aiJobStepRunnerFor, registerAiJobStep, registerAiJobStepPasses } from "./ai-jobs.js";
41
+ /**
42
+ * The build step (AGL-3616): the generation step of a `build` job, run once
43
+ * the member confirmed the plan their request produced.
44
+ *
45
+ * Like the site scaffold it generalizes, it builds nothing itself and asks no
46
+ * model of its own. Each pass takes the next unit in dependency order and
47
+ * hands it to what builds that kind of thing — a page, layout, form,
48
+ * component, email design or template to this plugin's own step, under a job
49
+ * derived for the unit; another plugin's resource to that plugin's writer on
50
+ * the resource-draft seam, with the content the owner's capability derives
51
+ * from the item's arguments. One unit, one pass, one recorded spend.
52
+ *
53
+ * ── Settled item by item ────────────────────────────────────────────────
54
+ *
55
+ * Where a unit stands is read from the job's ITEM LEDGER, never from counting
56
+ * outputs: the machine writes each pass's row in the transaction that
57
+ * records its spend (`recordStep`). A unit that fails is a FAILED ROW, not a
58
+ * failed job: the pass reports it, the machine gives back that item's spend
59
+ * where the failure was ours, and the next pass builds the next unit. What
60
+ * depended on it is built without it, or skipped where it cannot stand
61
+ * without it (`aiBuildDegradation`). The machine ends the job when nothing is
62
+ * left: `done` when anything was delivered, `failed` — its planning given
63
+ * back too — when nothing was.
64
+ *
65
+ * ── Drafts, unless asked and confirmed ───────────────────────────────────
66
+ *
67
+ * Every unit writes the draft its own kind writes, which nothing publishes.
68
+ * Only a build whose request asked to publish AND whose member ticked the
69
+ * plan card's box puts its built pages live, once, on its last pass, through
70
+ * the guided start's publish.
71
+ */ /** What a build answers with no confirmed plan to build. */ export const AI_BUILD_NO_PLAN_COPY = 'This build has no confirmed plan to build.';
72
+ /** What a unit says when nothing on this site builds its operation any more. */ export const aiBuildUnavailableCopy = (noun)=>`Not built: ${aiArticle(noun)} cannot be made on this site right now.`;
73
+ /** What a page that was reported built but holds none of its plan says. */ export const AI_BUILD_PAGE_NOT_WRITTEN_COPY = 'The page was not written from its plan.';
74
+ /**
75
+ * The most passes a build's step may take: every unit at a page's worst — its
76
+ * sections and its listing — so a runner that never finishes is still
77
+ * bounded while the largest plan is not. Try again starts the count over.
78
+ */ export const AI_BUILD_MAX_PASSES = AI_BUILD_LIMITS.units * (AI_SITE_MAX_SECTIONS + 1);
79
+ /** The job kind a creation of the build is built by. */ const CREATION_JOB_KINDS = {
80
+ layout: 'layout',
81
+ form: 'form',
82
+ component: 'component',
83
+ email: 'email'
84
+ };
85
+ /** The job kind a unit is built by, where an AI runner builds it. */ export function aiBuildUnitJobKind(unit, ops) {
86
+ var _CREATION_JOB_KINDS_unit_creation_kind;
87
+ var _ops_get;
88
+ if (unit.screen) return 'page';
89
+ if (unit.creation) return (_CREATION_JOB_KINDS_unit_creation_kind = CREATION_JOB_KINDS[unit.creation.kind]) != null ? _CREATION_JOB_KINDS_unit_creation_kind : null;
90
+ const runnerKind = (_ops_get = ops.get(unit.op)) == null ? void 0 : _ops_get.runnerKind;
91
+ return runnerKind ? runnerKind : null;
92
+ }
93
+ /** The id a unit's job, and so its draft, is named by. */ export function aiBuildUnitJobId(job, unit) {
94
+ var _ref, _ref1, _ref2;
95
+ var _unit_creation, _unit_screen, _unit_item;
96
+ return (_ref = (_ref1 = (_ref2 = (_unit_creation = unit.creation) == null ? void 0 : _unit_creation.id) != null ? _ref2 : (_unit_screen = unit.screen) == null ? void 0 : _unit_screen.id) != null ? _ref1 : (_unit_item = unit.item) == null ? void 0 : _unit_item.id) != null ? _ref : `${job.$id}-${unit.slot}`;
97
+ }
98
+ /**
99
+ * The job a unit is built under (generalizing `aiSiteUnitJob`): this job's
100
+ * org, site, creator and model; the kind that builds the unit; a plan
101
+ * narrowed to the unit, its `new:` references resolved to what was built and
102
+ * the failed ones left out; and a brief that says what the unit is, what it
103
+ * is built without, and which blocks its sections place.
104
+ */ export function aiBuildUnitJob(job, unit, context) {
105
+ var _job_outputs, _plan_labels, _context_briefLines, _ref;
106
+ const plan = job.plan;
107
+ const built = aiBuildBuiltRefs(context.units, context.ledger, (_job_outputs = job.outputs) != null ? _job_outputs : []);
108
+ const labels = _extends({}, (_plan_labels = plan.labels) != null ? _plan_labels : {});
109
+ for (const entry of built.values())labels[entry.id] = entry.label;
110
+ const brief = [
111
+ job.brief.trim()
112
+ ];
113
+ const unitPlan = _extends({}, plan, {
114
+ labels,
115
+ reuse: [
116
+ ...plan.reuse
117
+ ],
118
+ create: [],
119
+ screens: []
120
+ });
121
+ delete unitPlan.items;
122
+ const placedByScreens = new Set(plan.screens.flatMap((screen)=>screen.sections.flatMap((section)=>section.uses)));
123
+ if (unit.creation) {
124
+ unitPlan.reuse = plan.reuse.filter((entry)=>!placedByScreens.has(entry.id));
125
+ unitPlan.create = [
126
+ _extends({}, unit.creation, {
127
+ duplicateOf: aiSiteResolvedRef(unit.creation.duplicateOf, built)
128
+ })
129
+ ];
130
+ brief.push(`Build the ${unit.creation.kind} “${unit.creation.name}”: ${unit.creation.why}`);
131
+ }
132
+ if (unit.screen) {
133
+ var _aiSiteResolvedRef, _aiSiteResolvedRef1;
134
+ const screen = unit.screen;
135
+ for (const entry of built.values()){
136
+ unitPlan.reuse.push({
137
+ kind: entry.kind,
138
+ id: entry.id,
139
+ purpose: `the ${entry.kind} this request built`
140
+ });
141
+ }
142
+ let builtLayout = null;
143
+ for (const entry of built.values())if (entry.kind === 'layout') builtLayout = builtLayout != null ? builtLayout : entry.id;
144
+ unitPlan.screens = [
145
+ _extends({}, screen, {
146
+ // A page whose layout failed renders inside the site's own: the page
147
+ // step falls back to it when the plan names none.
148
+ layout: (_aiSiteResolvedRef = aiSiteResolvedRef(screen.layout, built)) != null ? _aiSiteResolvedRef : isAiPlanNewRef(screen.layout) ? builtLayout : null,
149
+ template: aiSiteResolvedRef(screen.template, built),
150
+ duplicateOf: aiSiteResolvedRef(screen.duplicateOf, built),
151
+ record: screen.record ? _extends({}, screen.record, {
152
+ dataset: (_aiSiteResolvedRef1 = aiSiteResolvedRef(screen.record.dataset, built)) != null ? _aiSiteResolvedRef1 : screen.record.dataset
153
+ }) : null,
154
+ sections: screen.sections.map((section)=>_extends({}, section, {
155
+ uses: section.uses.map((ref)=>aiSiteResolvedRef(ref, built)).filter((ref)=>!!ref)
156
+ }))
157
+ })
158
+ ];
159
+ brief.push(`Build the page “${screen.title}”, at ${screen.slug}.`);
160
+ brief.push(...aiBuildPlacedItemLines(unit, {
161
+ units: context.units,
162
+ ledger: context.ledger,
163
+ ops: context.ops
164
+ }));
165
+ }
166
+ const item = unit.item;
167
+ if (item) {
168
+ var _ref1;
169
+ var _context_ops_get;
170
+ const noun = (_ref1 = (_context_ops_get = context.ops.get(item.op)) == null ? void 0 : _context_ops_get.noun) != null ? _ref1 : item.op;
171
+ brief.push(`Build the ${noun} “${item.name}”${item.why ? `: ${item.why}` : '.'}`);
172
+ // A planned kind's step builds the plan's first creation of its kind.
173
+ unitPlan.create = AI_PLANNED_JOB_KINDS.includes(context.kind) ? [
174
+ _extends({
175
+ kind: context.kind,
176
+ name: item.name,
177
+ why: item.why,
178
+ duplicateOf: null,
179
+ fields: []
180
+ }, item.id ? {
181
+ id: item.id
182
+ } : {})
183
+ ] : [];
184
+ }
185
+ brief.push(...(_context_briefLines = context.briefLines) != null ? _context_briefLines : []);
186
+ const inputs = _extends({}, job.inputs, (_ref = item == null ? void 0 : item.args) != null ? _ref : {}, {
187
+ originJobId: aiOriginJobId(job)
188
+ });
189
+ delete inputs[AI_BUILD_PUBLISH_INPUT];
190
+ // A build's pages and layouts are designed in the layout language and
191
+ // compiled (AGL-3660), told the pages the build plans and where its form is.
192
+ if (context.kind === 'page' || context.kind === 'layout') {
193
+ inputs[AI_LAYOUT_LANGUAGE_INPUT] = true;
194
+ const pages = aiLayoutSitePagesOfPlan(plan.screens);
195
+ if (pages.length) inputs[AI_LAYOUT_SITE_PAGES_INPUT] = pages;
196
+ const formPage = aiLayoutFormPageOfPlan(plan);
197
+ if (formPage) inputs[AI_LAYOUT_FORM_PAGE_INPUT] = formPage;
198
+ }
199
+ return _extends({}, job, {
200
+ $id: aiBuildUnitJobId(job, unit),
201
+ kind: context.kind,
202
+ steps: [],
203
+ outputs: [],
204
+ items: null,
205
+ plan: item && !AI_PLANNED_JOB_KINDS.includes(context.kind) ? null : unitPlan,
206
+ inputs,
207
+ brief: brief.join('\n').slice(0, AI_JOB_BRIEF_MAX_CHARS)
208
+ });
209
+ }
210
+ /** The unit's failure, for its row: ours or not, why, in customer-safe words. */ function failed(slot, failure, extra = {}) {
211
+ return _extends({
212
+ slot,
213
+ status: 'failed',
214
+ failure
215
+ }, extra);
216
+ }
217
+ /** Whether a build's workspace spends the Free taste. */ export function aiBuildFreeTaste(org) {
218
+ return Boolean(org) && resolveEffectivePlan(org) === 'free';
219
+ }
220
+ /** The site document a gate reads, or `null`. */ async function readHost(firestore, hostId) {
221
+ var _data;
222
+ if (!hostId) return null;
223
+ return (_data = (await firestore.collection('hosts').doc(hostId).get()).data()) != null ? _data : null;
224
+ }
225
+ export function createAiJobBuildStep(deps = {}) {
226
+ var _deps_runnerFor, _deps_writerFor, _deps_opsFor, _deps_admissionFor, _deps_pluginAdmission, _deps_readNodes, _deps_publish;
227
+ const runnerFor = (_deps_runnerFor = deps.runnerFor) != null ? _deps_runnerFor : aiJobStepRunnerFor;
228
+ const writerFor = (_deps_writerFor = deps.writerFor) != null ? _deps_writerFor : aiPluginDraftWriter;
229
+ const opsFor = (_deps_opsFor = deps.opsFor) != null ? _deps_opsFor : (context)=>aiBuildOps(context);
230
+ const admissionFor = (_deps_admissionFor = deps.admissionFor) != null ? _deps_admissionFor : aiJobAdmissionRefusal;
231
+ const pluginAdmission = (_deps_pluginAdmission = deps.pluginAdmission) != null ? _deps_pluginAdmission : aiPluginDraftAdmissionRefusal;
232
+ const readNodes = (_deps_readNodes = deps.readNodes) != null ? _deps_readNodes : readAiDraftNodes;
233
+ const publish = (_deps_publish = deps.publish) != null ? _deps_publish : aiPublishGuidedSite;
234
+ return async (context)=>{
235
+ var _ref, _context_org, _aiBuildOrder, _job_hostId, _job_hostId1, _ref1;
236
+ var _job_items, _rows_get;
237
+ const { job, firestore } = context;
238
+ const model = (_ref = context.modelFor == null ? void 0 : context.modelFor.call(context, 'job.plan')) != null ? _ref : aiModelForStep('job.plan');
239
+ const plan = aiConfirmedPlan(job);
240
+ if (!plan) return _extends({}, aiUnspentOutcome(model), {
241
+ failure: AI_BUILD_NO_PLAN_COPY
242
+ });
243
+ const org = (_context_org = context.org) != null ? _context_org : null;
244
+ const units = aiBuildUnits(plan);
245
+ const ordered = (_aiBuildOrder = aiBuildOrder(units)) != null ? _aiBuildOrder : units;
246
+ const starting = !((_job_items = job.items) == null ? void 0 : _job_items.length);
247
+ const ledger = starting ? aiBuildInitialLedger(units) : job.items;
248
+ const init = starting ? {
249
+ items: ledger
250
+ } : {};
251
+ const unit = aiBuildNextUnit(ordered, ledger);
252
+ const rows = new Map(ledger.map((row)=>[
253
+ row.slot,
254
+ row
255
+ ]));
256
+ const host = await readHost(firestore, (_job_hostId = job.hostId) != null ? _job_hostId : null);
257
+ const ops = await opsFor({
258
+ orgId: job.orgId,
259
+ hostId: (_job_hostId1 = job.hostId) != null ? _job_hostId1 : null,
260
+ org,
261
+ host,
262
+ freeTaste: aiBuildFreeTaste(org)
263
+ });
264
+ /** Whether units other than this one are still to build. */ const othersOpen = (slot)=>ordered.some((one)=>{
265
+ var _rows_get;
266
+ return one.slot !== slot && aiBuildItemOpen((_rows_get = rows.get(one.slot)) != null ? _rows_get : {
267
+ status: 'pending'
268
+ });
269
+ });
270
+ /** The build's last pass puts its pages live, where it was asked and confirmed. */ const finish = async (outcome, extraPages = [])=>{
271
+ var _job_outputs;
272
+ if (!aiJobPublishesBuild(job) || job.sitePublish || !job.hostId) return outcome;
273
+ const pageIds = new Set(units.filter((one)=>{
274
+ var _rows_get;
275
+ return one.screen && aiBuildItemDelivered((_rows_get = rows.get(one.slot)) != null ? _rows_get : {
276
+ status: 'pending'
277
+ });
278
+ }).flatMap((one)=>{
279
+ var _ref;
280
+ var _rows_get;
281
+ return (_ref = (_rows_get = rows.get(one.slot)) == null ? void 0 : _rows_get.outputs) != null ? _ref : [];
282
+ }));
283
+ const pages = [
284
+ ...((_job_outputs = job.outputs) != null ? _job_outputs : []).filter((output)=>output.resource === 'screen' && pageIds.has(output.id)),
285
+ ...extraPages
286
+ ];
287
+ if (!pages.length) return outcome;
288
+ const sitePublish = await publish(firestore, {
289
+ job,
290
+ outputs: pages,
291
+ now: context.now
292
+ }).catch((error)=>{
293
+ console.error('ai build publish threw', {
294
+ orgId: job.orgId,
295
+ jobId: job.$id,
296
+ error
297
+ });
298
+ return null;
299
+ });
300
+ return sitePublish ? _extends({}, outcome, {
301
+ sitePublish
302
+ }) : outcome;
303
+ };
304
+ if (!unit) return finish(_extends({}, aiUnspentOutcome(model), init));
305
+ const settle = (item, spent = aiUnspentOutcome(model))=>_extends({}, spent, init, {
306
+ item
307
+ }, item.status === 'running' || othersOpen(unit.slot) ? {
308
+ continue: true
309
+ } : {});
310
+ // What its dependencies came to.
311
+ const degradation = aiBuildDegradation(unit, {
312
+ units,
313
+ ledger,
314
+ ops
315
+ });
316
+ if (degradation.skip) {
317
+ return settle({
318
+ slot: unit.slot,
319
+ status: 'skipped',
320
+ note: degradation.skip,
321
+ degradedBy: degradation.degradedBy
322
+ });
323
+ }
324
+ const capability = ops.get(unit.op);
325
+ const noun = (_ref1 = capability == null ? void 0 : capability.noun) != null ? _ref1 : unit.op;
326
+ if (!capability) return settle({
327
+ slot: unit.slot,
328
+ status: 'skipped',
329
+ note: aiBuildUnavailableCopy(noun)
330
+ });
331
+ const degraded = degradation.degradedBy.length > 0;
332
+ const note = degradation.notes.length ? degradation.notes.join(' ') : null;
333
+ // Another plugin's resource: its writer, with the content its capability derives.
334
+ if (capability.draftResource) {
335
+ var _ref2;
336
+ const item = unit.item;
337
+ const writer = writerFor(capability.draftResource);
338
+ if (!item || !writer || !job.hostId) {
339
+ return settle({
340
+ slot: unit.slot,
341
+ status: 'skipped',
342
+ note: aiBuildUnavailableCopy(noun)
343
+ });
344
+ }
345
+ const admission = {
346
+ firestore,
347
+ orgId: job.orgId,
348
+ hostId: job.hostId,
349
+ inputs: {},
350
+ org,
351
+ uid: job.createdBy
352
+ };
353
+ const refusal = await pluginAdmission(admission, {
354
+ kind: 'build',
355
+ drafts: [
356
+ {
357
+ resource: capability.draftResource,
358
+ label: capability.noun
359
+ }
360
+ ],
361
+ writerFor
362
+ });
363
+ if (refusal) return settle({
364
+ slot: unit.slot,
365
+ status: 'skipped',
366
+ note: `Not built: ${refusal.error}`
367
+ });
368
+ const dependencies = {};
369
+ for (const slot of unit.deps){
370
+ const dep = units.find((one)=>one.slot === slot);
371
+ const row = rows.get(slot);
372
+ if (!dep || !row || !aiBuildItemDelivered(row) || !row.outputs.length) continue;
373
+ dependencies[`new:${dep.label}`] = {
374
+ op: dep.op,
375
+ id: row.outputs[0]
376
+ };
377
+ }
378
+ const content = (_ref2 = capability.draftContent == null ? void 0 : capability.draftContent.call(capability, {
379
+ name: item.name,
380
+ args: item.args
381
+ }, {
382
+ hostId: job.hostId,
383
+ dependencies
384
+ })) != null ? _ref2 : item.args;
385
+ const checked = writer.check(content, {
386
+ hostId: job.hostId
387
+ });
388
+ if (checked.ok === false) {
389
+ var _checked_problems_;
390
+ return settle(failed(unit.slot, {
391
+ ours: true,
392
+ reason: 'step-failure',
393
+ message: (_checked_problems_ = checked.problems[0]) != null ? _checked_problems_ : AI_BUILD_UNIT_EMPTY_COPY
394
+ }));
395
+ }
396
+ const id = aiBuildUnitJobId(job, unit);
397
+ const written = await writer.write({
398
+ orgId: job.orgId,
399
+ hostId: job.hostId,
400
+ uid: job.createdBy,
401
+ org,
402
+ now: context.now,
403
+ id,
404
+ name: item.name,
405
+ content
406
+ });
407
+ if (written.ok === false) {
408
+ return settle(failed(unit.slot, {
409
+ ours: false,
410
+ reason: 'review',
411
+ message: written.error
412
+ }));
413
+ }
414
+ const output = {
415
+ resource: 'draft',
416
+ id: written.id,
417
+ versionId: written.versionId,
418
+ hostId: job.hostId,
419
+ label: written.name,
420
+ draftResource: capability.draftResource,
421
+ note: `A draft, in ${capability.where}.`
422
+ };
423
+ return settle(_extends({
424
+ slot: unit.slot,
425
+ status: degraded ? 'degraded' : 'succeeded',
426
+ outputs: [
427
+ written.id
428
+ ],
429
+ note
430
+ }, degraded ? {
431
+ degradedBy: degradation.degradedBy
432
+ } : {}), _extends({}, aiUnspentOutcome(model), {
433
+ outputs: [
434
+ output
435
+ ]
436
+ }));
437
+ }
438
+ // This plugin's own runner, under a job derived for the unit.
439
+ const kind = aiBuildUnitJobKind(unit, ops);
440
+ const runner = kind ? runnerFor(kind) : null;
441
+ if (!kind || !runner) return settle({
442
+ slot: unit.slot,
443
+ status: 'skipped',
444
+ note: aiBuildUnavailableCopy(noun)
445
+ });
446
+ const derived = aiBuildUnitJob(job, unit, {
447
+ kind,
448
+ units,
449
+ ledger,
450
+ ops,
451
+ briefLines: degradation.briefLines
452
+ });
453
+ // An item's kind is asked its own admission first (a campaign's plan,
454
+ // a template's subject), once, before its first pass.
455
+ if (unit.item && ((_rows_get = rows.get(unit.slot)) == null ? void 0 : _rows_get.status) !== 'running') {
456
+ var _job_hostId2;
457
+ const refusal = await admissionFor(kind, {
458
+ firestore,
459
+ orgId: job.orgId,
460
+ hostId: (_job_hostId2 = job.hostId) != null ? _job_hostId2 : null,
461
+ inputs: derived.inputs,
462
+ org,
463
+ uid: job.createdBy
464
+ }).catch((error)=>{
465
+ console.error('ai build item admission failed', {
466
+ orgId: job.orgId,
467
+ jobId: job.$id,
468
+ slot: unit.slot,
469
+ error
470
+ });
471
+ return {
472
+ status: 403,
473
+ error: AI_BUILD_UNIT_EMPTY_COPY
474
+ };
475
+ });
476
+ if (refusal) return settle({
477
+ slot: unit.slot,
478
+ status: 'skipped',
479
+ note: `Not built: ${refusal.error}`
480
+ });
481
+ }
482
+ let outcome;
483
+ try {
484
+ outcome = await runner(_extends({}, context, {
485
+ job: derived
486
+ }));
487
+ } catch (error) {
488
+ // A provider that will answer later is the machine's to retry; anything
489
+ // else is this item's failure, and the build goes on.
490
+ if (aiUnitErrorRetryable(error)) throw error;
491
+ console.error('ai build unit threw', {
492
+ orgId: job.orgId,
493
+ jobId: job.$id,
494
+ slot: unit.slot,
495
+ error
496
+ });
497
+ return settle(failed(unit.slot, {
498
+ ours: true,
499
+ reason: 'provider',
500
+ message: AI_BUILD_UNIT_EMPTY_COPY
501
+ }));
502
+ }
503
+ const spent = aiUnitSpend(outcome);
504
+ const ids = outcome.outputs.map((output)=>output.id);
505
+ const stopped = aiUnitFailure(unit.slot, outcome);
506
+ if (stopped) return settle(stopped, spent);
507
+ // A page counts as built only when its plan's sections are in it (AGL-3596).
508
+ if (unit.screen && job.hostId) {
509
+ const page = outcome.outputs.find((output)=>output.resource === 'screen');
510
+ const written = page !== undefined && await aiSitePageWritten(firestore, {
511
+ hostId: job.hostId,
512
+ unitJobId: derived.$id,
513
+ screen: unit.screen,
514
+ draftId: page.id
515
+ }, readNodes);
516
+ if (!written) {
517
+ return settle(failed(unit.slot, {
518
+ ours: true,
519
+ reason: 'step-failure',
520
+ message: AI_BUILD_PAGE_NOT_WRITTEN_COPY
521
+ }), _extends({}, spent, {
522
+ outputs: outcome.outputs.filter((output)=>output.resource !== 'screen')
523
+ }));
524
+ }
525
+ }
526
+ const done = settle(_extends({
527
+ slot: unit.slot,
528
+ status: degraded ? 'degraded' : 'succeeded',
529
+ outputs: ids,
530
+ note
531
+ }, degraded ? {
532
+ degradedBy: degradation.degradedBy
533
+ } : {}), spent);
534
+ if (done.continue) return done;
535
+ // The last unit: what was built goes live where it was asked and confirmed.
536
+ rows.set(unit.slot, _extends({}, rows.get(unit.slot), {
537
+ status: degraded ? 'degraded' : 'succeeded',
538
+ outputs: ids
539
+ }));
540
+ return finish(done, unit.screen ? outcome.outputs.filter((output)=>output.resource === 'screen') : []);
541
+ };
542
+ }
543
+ export const runAiJobBuildStep = createAiJobBuildStep();
544
+ /**
545
+ * The least time a build's next pass needs: what the step that builds its
546
+ * next unit registers for the job derived for it; a page pass's when the
547
+ * next unit is another plugin's draft, which asks no model.
548
+ */ export function aiBuildJobRunMinimumMs(job) {
549
+ var _aiBuildOrder, _CREATION_JOB_KINDS_unit_creation_kind, _AI_BUILD_ITEM_RUNNER_KINDS_unit_op;
550
+ var _job_items;
551
+ const plan = aiConfirmedPlan(job);
552
+ if (!plan) return AI_JOB_PAGE_STEP_MINIMUM_MS;
553
+ const units = aiBuildUnits(plan);
554
+ const ordered = (_aiBuildOrder = aiBuildOrder(units)) != null ? _aiBuildOrder : units;
555
+ const ledger = ((_job_items = job.items) == null ? void 0 : _job_items.length) ? job.items : aiBuildInitialLedger(units);
556
+ const unit = aiBuildNextUnit(ordered, ledger);
557
+ if (!unit) return AI_JOB_PAGE_STEP_MINIMUM_MS;
558
+ const kind = unit.screen ? 'page' : unit.creation ? (_CREATION_JOB_KINDS_unit_creation_kind = CREATION_JOB_KINDS[unit.creation.kind]) != null ? _CREATION_JOB_KINDS_unit_creation_kind : null : (_AI_BUILD_ITEM_RUNNER_KINDS_unit_op = AI_BUILD_ITEM_RUNNER_KINDS[unit.op]) != null ? _AI_BUILD_ITEM_RUNNER_KINDS_unit_op : null;
559
+ if (!kind) return AI_JOB_PAGE_STEP_MINIMUM_MS;
560
+ return aiJobStepRunMinimumMs(aiBuildUnitJob(job, unit, {
561
+ kind,
562
+ units,
563
+ ledger,
564
+ ops: new Map()
565
+ }));
566
+ }
567
+ /** The runner kinds this plugin's own item operations use, for the time a pass needs without the registry. */ const AI_BUILD_ITEM_RUNNER_KINDS = {
568
+ template: 'template',
569
+ campaign: 'campaign',
570
+ workflow: 'workflow'
571
+ };
572
+ /**
573
+ * Why a Free build cannot be confirmed: its estimate is more than the owner's
574
+ * Free allowance has left this month. Read off the account month the meter
575
+ * writes, net of give-backs; a read that fails admits, since the reservation
576
+ * still refuses at the wall.
577
+ */ export async function aiBuildFreeBalanceRefusal(input) {
578
+ const account = freeAssistAccount(input.org);
579
+ if (!(account == null ? void 0 : account.accountUid)) return null;
580
+ try {
581
+ const month = input.now.toISOString().slice(0, 7);
582
+ const snapshot = await freeAccountUsageRef(input.firestore, account.accountUid, month).get();
583
+ const spent = assistCreditsFromUsd(assistSpendAfterReturnsUsd(snapshot.get('estCostUsd'), snapshot.get(ASSIST_RETURNED_USD_FIELD)));
584
+ const left = Math.max(0, FREE_AI_TASTE_CREDITS_PER_MONTH - spent);
585
+ return input.estimate > left ? `This plan is estimated at ${input.estimate} credits, and your Free AI credits have ${left} left this month. Ask for less, or upgrade for more.` : null;
586
+ } catch (error) {
587
+ console.error('ai build free balance read failed', {
588
+ error
589
+ });
590
+ return null;
591
+ }
592
+ }
593
+ /**
594
+ * A build is admitted for a site of the job's own org where a page can be
595
+ * built; and confirmed only for a plan it can build here — its shape, its
596
+ * operations as this site has them, and on the Free taste an estimate the
597
+ * month's Free credits still cover.
598
+ */ export function createAiBuildJobAdmission(deps = {}) {
599
+ var _deps_opsFor, _deps_freeBalanceRefusal, _deps_ownerOf;
600
+ const opsFor = (_deps_opsFor = deps.opsFor) != null ? _deps_opsFor : (context)=>aiBuildOps(context);
601
+ const freeBalanceRefusal = (_deps_freeBalanceRefusal = deps.freeBalanceRefusal) != null ? _deps_freeBalanceRefusal : aiBuildFreeBalanceRefusal;
602
+ const ownerOf = (_deps_ownerOf = deps.ownerOf) != null ? _deps_ownerOf : resolveOrgIdForHost;
603
+ return async (context)=>{
604
+ if (!context.hostId) return {
605
+ status: 400,
606
+ error: 'Open the site to build on before asking Assist to build'
607
+ };
608
+ const owner = await ownerOf(context.hostId);
609
+ if (!owner || owner !== context.orgId) return {
610
+ status: 404,
611
+ error: 'Unknown site'
612
+ };
613
+ if (!aiJobStepRunnerFor('page')) return {
614
+ status: 400,
615
+ error: 'Building from a request is not available yet.'
616
+ };
617
+ const plan = context.plan;
618
+ if (!plan) return null;
619
+ const org = context.org;
620
+ const freeTaste = aiBuildFreeTaste(org);
621
+ const host = await readHost(context.firestore, context.hostId);
622
+ const ops = await opsFor({
623
+ orgId: context.orgId,
624
+ hostId: context.hostId,
625
+ org,
626
+ host,
627
+ freeTaste
628
+ });
629
+ const shape = aiBuildPlanShapeRefusal(plan, {
630
+ freeTaste,
631
+ ops
632
+ });
633
+ if (shape) return {
634
+ status: 400,
635
+ error: shape
636
+ };
637
+ if (freeTaste) {
638
+ var _ref;
639
+ const refusal = await freeBalanceRefusal({
640
+ firestore: context.firestore,
641
+ org,
642
+ estimate: aiBuildCreditEstimate(plan, {
643
+ ops
644
+ }),
645
+ now: (_ref = deps.now == null ? void 0 : deps.now.call(deps)) != null ? _ref : new Date()
646
+ });
647
+ if (refusal) return {
648
+ status: 403,
649
+ error: refusal
650
+ };
651
+ }
652
+ return null;
653
+ };
654
+ }
655
+ export const aiBuildJobAdmission = createAiBuildJobAdmission();
656
+ /**
657
+ * Registers the build with the least time a pass needs, the time its next
658
+ * pass needs, the passes its largest plan may take, and its admission.
659
+ */ export function registerAiBuildJob() {
660
+ registerAiJobStep('build', runAiJobBuildStep, {
661
+ minimumMs: AI_JOB_PAGE_STEP_MINIMUM_MS,
662
+ minimumMsFor: aiBuildJobRunMinimumMs
663
+ });
664
+ registerAiJobStepPasses('build', AI_BUILD_MAX_PASSES);
665
+ registerAiJobAdmission('build', aiBuildJobAdmission);
666
+ }
667
+
668
+ //# sourceMappingURL=ai-job-build-step.js.map