primitive-admin 1.2.0-alpha.14 → 1.2.0-alpha.15

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 (318) hide show
  1. package/README.md +102 -78
  2. package/assets/skill/skills/primitive-platform/SKILL.md +87 -23
  3. package/dist/bin/primitive.js +11 -82
  4. package/dist/bin/primitive.js.map +1 -1
  5. package/dist/src/commands/admins.js +79 -17
  6. package/dist/src/commands/admins.js.map +1 -1
  7. package/dist/src/commands/agent-sessions.js +2 -0
  8. package/dist/src/commands/agent-sessions.js.map +1 -1
  9. package/dist/src/commands/analytics.js +183 -12
  10. package/dist/src/commands/analytics.js.map +1 -1
  11. package/dist/src/commands/apps-children.js +2 -0
  12. package/dist/src/commands/apps-children.js.map +1 -1
  13. package/dist/src/commands/apps.js +19 -2
  14. package/dist/src/commands/apps.js.map +1 -1
  15. package/dist/src/commands/auth.js +11 -1
  16. package/dist/src/commands/auth.js.map +1 -1
  17. package/dist/src/commands/collections.js +13 -0
  18. package/dist/src/commands/collections.js.map +1 -1
  19. package/dist/src/commands/config.d.ts +1 -1
  20. package/dist/src/commands/config.js +40 -9
  21. package/dist/src/commands/config.js.map +1 -1
  22. package/dist/src/commands/connections.js +2 -0
  23. package/dist/src/commands/connections.js.map +1 -1
  24. package/dist/src/commands/database-type-configs.js +5 -3
  25. package/dist/src/commands/database-type-configs.js.map +1 -1
  26. package/dist/src/commands/databases.js +6 -4
  27. package/dist/src/commands/databases.js.map +1 -1
  28. package/dist/src/commands/db3-types.js +2 -0
  29. package/dist/src/commands/db3-types.js.map +1 -1
  30. package/dist/src/commands/db3.d.ts +12 -0
  31. package/dist/src/commands/db3.js +343 -24
  32. package/dist/src/commands/db3.js.map +1 -1
  33. package/dist/src/commands/documents.d.ts +52 -12
  34. package/dist/src/commands/documents.js +120 -29
  35. package/dist/src/commands/documents.js.map +1 -1
  36. package/dist/src/commands/email-templates.js +72 -0
  37. package/dist/src/commands/email-templates.js.map +1 -1
  38. package/dist/src/commands/functions.js +47 -10
  39. package/dist/src/commands/functions.js.map +1 -1
  40. package/dist/src/commands/groups.js +18 -3
  41. package/dist/src/commands/groups.js.map +1 -1
  42. package/dist/src/commands/init.js +7 -1
  43. package/dist/src/commands/init.js.map +1 -1
  44. package/dist/src/commands/integrations.js +80 -47
  45. package/dist/src/commands/integrations.js.map +1 -1
  46. package/dist/src/commands/metadata-category-configs.js +2 -0
  47. package/dist/src/commands/metadata-category-configs.js.map +1 -1
  48. package/dist/src/commands/metadata.js +41 -0
  49. package/dist/src/commands/metadata.js.map +1 -1
  50. package/dist/src/commands/prompts.js +73 -47
  51. package/dist/src/commands/prompts.js.map +1 -1
  52. package/dist/src/commands/rule-sets.js +2 -0
  53. package/dist/src/commands/rule-sets.js.map +1 -1
  54. package/dist/src/commands/schemas.d.ts +15 -0
  55. package/dist/src/commands/schemas.js +122 -0
  56. package/dist/src/commands/schemas.js.map +1 -0
  57. package/dist/src/commands/secrets.js +45 -3
  58. package/dist/src/commands/secrets.js.map +1 -1
  59. package/dist/src/commands/sessions.js +2 -0
  60. package/dist/src/commands/sessions.js.map +1 -1
  61. package/dist/src/commands/sync-schemas.d.ts +202 -0
  62. package/dist/src/commands/sync-schemas.js +629 -0
  63. package/dist/src/commands/sync-schemas.js.map +1 -0
  64. package/dist/src/commands/sync-user-properties.d.ts +118 -0
  65. package/dist/src/commands/sync-user-properties.js +276 -0
  66. package/dist/src/commands/sync-user-properties.js.map +1 -0
  67. package/dist/src/commands/sync.d.ts +243 -12
  68. package/dist/src/commands/sync.js +1474 -314
  69. package/dist/src/commands/sync.js.map +1 -1
  70. package/dist/src/commands/user-properties.d.ts +14 -0
  71. package/dist/src/commands/user-properties.js +79 -0
  72. package/dist/src/commands/user-properties.js.map +1 -0
  73. package/dist/src/commands/users.js +879 -16
  74. package/dist/src/commands/users.js.map +1 -1
  75. package/dist/src/commands/waitlist.js +39 -4
  76. package/dist/src/commands/waitlist.js.map +1 -1
  77. package/dist/src/lib/analytics-query.d.ts +58 -0
  78. package/dist/src/lib/analytics-query.js +134 -0
  79. package/dist/src/lib/analytics-query.js.map +1 -0
  80. package/dist/src/lib/api-client.d.ts +748 -95
  81. package/dist/src/lib/api-client.js +356 -158
  82. package/dist/src/lib/api-client.js.map +1 -1
  83. package/dist/src/lib/block-selector.d.ts +9 -15
  84. package/dist/src/lib/block-selector.js +9 -20
  85. package/dist/src/lib/block-selector.js.map +1 -1
  86. package/dist/src/lib/cli-manifest.d.ts +8 -0
  87. package/dist/src/lib/cli-manifest.js +10 -1
  88. package/dist/src/lib/cli-manifest.js.map +1 -1
  89. package/dist/src/lib/cli-version.d.ts +14 -0
  90. package/dist/src/lib/cli-version.js +38 -0
  91. package/dist/src/lib/cli-version.js.map +1 -0
  92. package/dist/src/lib/codegen-shared/prettierStable.d.ts +41 -0
  93. package/dist/src/lib/codegen-shared/prettierStable.js +146 -24
  94. package/dist/src/lib/codegen-shared/prettierStable.js.map +1 -1
  95. package/dist/src/lib/command-status.d.ts +110 -0
  96. package/dist/src/lib/command-status.js +184 -0
  97. package/dist/src/lib/command-status.js.map +1 -0
  98. package/dist/src/lib/config-object-descriptor.d.ts +6 -0
  99. package/dist/src/lib/config-object-descriptor.js +71 -0
  100. package/dist/src/lib/config-object-descriptor.js.map +1 -1
  101. package/dist/src/lib/config-toml.d.ts +11 -2
  102. package/dist/src/lib/config-toml.js +25 -2
  103. package/dist/src/lib/config-toml.js.map +1 -1
  104. package/dist/src/lib/data-input.d.ts +1 -1
  105. package/dist/src/lib/data-input.js +1 -1
  106. package/dist/src/lib/db-codegen/dbTsTypes.d.ts +1 -1
  107. package/dist/src/lib/db-codegen/dbTsTypes.js +3 -0
  108. package/dist/src/lib/db-codegen/dbTsTypes.js.map +1 -1
  109. package/dist/src/lib/db-codegen/dbTypeIR.d.ts +11 -1
  110. package/dist/src/lib/db-codegen/dbTypeIR.js +11 -5
  111. package/dist/src/lib/db-codegen/dbTypeIR.js.map +1 -1
  112. package/dist/src/lib/db-type-records.d.ts +95 -0
  113. package/dist/src/lib/db-type-records.js +219 -0
  114. package/dist/src/lib/db-type-records.js.map +1 -0
  115. package/dist/src/lib/db3-export.d.ts +51 -14
  116. package/dist/src/lib/db3-export.js +135 -27
  117. package/dist/src/lib/db3-export.js.map +1 -1
  118. package/dist/src/lib/db3-index-output.d.ts +68 -3
  119. package/dist/src/lib/db3-index-output.js +139 -3
  120. package/dist/src/lib/db3-index-output.js.map +1 -1
  121. package/dist/src/lib/db3-type-models.d.ts +39 -7
  122. package/dist/src/lib/db3-type-models.js +138 -27
  123. package/dist/src/lib/db3-type-models.js.map +1 -1
  124. package/dist/src/lib/deprecation.d.ts +2 -2
  125. package/dist/src/lib/deprecation.js +2 -2
  126. package/dist/src/lib/experimental-apis.d.ts +36 -0
  127. package/dist/src/lib/experimental-apis.js +46 -0
  128. package/dist/src/lib/experimental-apis.js.map +1 -0
  129. package/dist/src/lib/file-attachment.d.ts +24 -0
  130. package/dist/src/lib/file-attachment.js +31 -0
  131. package/dist/src/lib/file-attachment.js.map +1 -0
  132. package/dist/src/lib/function-authorize-check.d.ts +1 -1
  133. package/dist/src/lib/function-authorize-check.js +2 -1
  134. package/dist/src/lib/function-authorize-check.js.map +1 -1
  135. package/dist/src/lib/function-authorize-view.js +4 -0
  136. package/dist/src/lib/function-authorize-view.js.map +1 -1
  137. package/dist/src/lib/function-bundle.d.ts +27 -5
  138. package/dist/src/lib/function-bundle.js +106 -7
  139. package/dist/src/lib/function-bundle.js.map +1 -1
  140. package/dist/src/lib/function-collect.d.ts +2 -0
  141. package/dist/src/lib/function-collect.js +36 -7
  142. package/dist/src/lib/function-collect.js.map +1 -1
  143. package/dist/src/lib/function-db-types.d.ts +57 -40
  144. package/dist/src/lib/function-db-types.js +246 -285
  145. package/dist/src/lib/function-db-types.js.map +1 -1
  146. package/dist/src/lib/function-document-types.d.ts +19 -7
  147. package/dist/src/lib/function-document-types.js +24 -12
  148. package/dist/src/lib/function-document-types.js.map +1 -1
  149. package/dist/src/lib/function-run.d.ts +1 -1
  150. package/dist/src/lib/function-run.js +1 -1
  151. package/dist/src/lib/function-run.js.map +1 -1
  152. package/dist/src/lib/function-schema-codegen.d.ts +58 -18
  153. package/dist/src/lib/function-schema-codegen.js +85 -74
  154. package/dist/src/lib/function-schema-codegen.js.map +1 -1
  155. package/dist/src/lib/function-schema-refs.d.ts +98 -0
  156. package/dist/src/lib/function-schema-refs.js +177 -0
  157. package/dist/src/lib/function-schema-refs.js.map +1 -0
  158. package/dist/src/lib/function-sync.d.ts +38 -4
  159. package/dist/src/lib/function-sync.js +83 -14
  160. package/dist/src/lib/function-sync.js.map +1 -1
  161. package/dist/src/lib/function-typecheck.d.ts +52 -0
  162. package/dist/src/lib/function-typecheck.js +171 -54
  163. package/dist/src/lib/function-typecheck.js.map +1 -1
  164. package/dist/src/lib/generated-build-flags.d.ts +13 -0
  165. package/dist/src/lib/generated-build-flags.js +14 -0
  166. package/dist/src/lib/generated-build-flags.js.map +1 -0
  167. package/dist/src/lib/generated-config-surfaces.d.ts +211 -11
  168. package/dist/src/lib/generated-config-surfaces.js +579 -69
  169. package/dist/src/lib/generated-config-surfaces.js.map +1 -1
  170. package/dist/src/lib/generated-sdk-types.d.ts +1 -1
  171. package/dist/src/lib/generated-sdk-types.js +1 -1
  172. package/dist/src/lib/generated-sdk-types.js.map +1 -1
  173. package/dist/src/lib/integration-test-request.d.ts +54 -0
  174. package/dist/src/lib/integration-test-request.js +207 -0
  175. package/dist/src/lib/integration-test-request.js.map +1 -0
  176. package/dist/src/lib/metadata-report.d.ts +85 -0
  177. package/dist/src/lib/metadata-report.js +81 -0
  178. package/dist/src/lib/metadata-report.js.map +1 -0
  179. package/dist/src/lib/output.d.ts +1 -1
  180. package/dist/src/lib/output.js +1 -1
  181. package/dist/src/lib/platform-owned.js +5 -0
  182. package/dist/src/lib/platform-owned.js.map +1 -1
  183. package/dist/src/lib/primitive-declaration-names.d.ts +21 -0
  184. package/dist/src/lib/primitive-declaration-names.js +22 -0
  185. package/dist/src/lib/primitive-declaration-names.js.map +1 -0
  186. package/dist/src/lib/primitive-declaration.d.ts +143 -0
  187. package/dist/src/lib/primitive-declaration.js +658 -0
  188. package/dist/src/lib/primitive-declaration.js.map +1 -0
  189. package/dist/src/lib/prompt-schema-codegen.d.ts +36 -12
  190. package/dist/src/lib/prompt-schema-codegen.js +82 -98
  191. package/dist/src/lib/prompt-schema-codegen.js.map +1 -1
  192. package/dist/src/lib/prompt-schema-document.d.ts +35 -0
  193. package/dist/src/lib/prompt-schema-document.js +92 -0
  194. package/dist/src/lib/prompt-schema-document.js.map +1 -0
  195. package/dist/src/lib/prompt-schema-refs.d.ts +164 -0
  196. package/dist/src/lib/prompt-schema-refs.js +452 -0
  197. package/dist/src/lib/prompt-schema-refs.js.map +1 -0
  198. package/dist/src/lib/records-verbs.d.ts +12 -2
  199. package/dist/src/lib/records-verbs.js +7 -2
  200. package/dist/src/lib/records-verbs.js.map +1 -1
  201. package/dist/src/lib/register-commands.d.ts +28 -0
  202. package/dist/src/lib/register-commands.js +101 -0
  203. package/dist/src/lib/register-commands.js.map +1 -0
  204. package/dist/src/lib/retired-imports.d.ts +53 -0
  205. package/dist/src/lib/retired-imports.js +120 -0
  206. package/dist/src/lib/retired-imports.js.map +1 -0
  207. package/dist/src/lib/schema-codegen/compiledSpace.d.ts +62 -0
  208. package/dist/src/lib/schema-codegen/compiledSpace.js +103 -0
  209. package/dist/src/lib/schema-codegen/compiledSpace.js.map +1 -0
  210. package/dist/src/lib/schema-codegen/generated-db3-bridge.d.ts +86 -0
  211. package/dist/src/lib/schema-codegen/generated-db3-bridge.js +221 -0
  212. package/dist/src/lib/schema-codegen/generated-db3-bridge.js.map +1 -0
  213. package/dist/src/lib/schema-codegen/generated-db3-query-capabilities.d.ts +85 -0
  214. package/dist/src/lib/schema-codegen/generated-db3-query-capabilities.js +86 -0
  215. package/dist/src/lib/schema-codegen/generated-db3-query-capabilities.js.map +1 -0
  216. package/dist/src/lib/schema-codegen/generated-field-grammar.d.ts +209 -0
  217. package/dist/src/lib/schema-codegen/generated-field-grammar.js +379 -0
  218. package/dist/src/lib/schema-codegen/generated-field-grammar.js.map +1 -0
  219. package/dist/src/lib/schema-codegen/generated-field-table.d.ts +80 -0
  220. package/dist/src/lib/schema-codegen/generated-field-table.js +542 -0
  221. package/dist/src/lib/schema-codegen/generated-field-table.js.map +1 -0
  222. package/dist/src/lib/schema-codegen/generated-prompt-schemas.d.ts +195 -0
  223. package/dist/src/lib/schema-codegen/generated-prompt-schemas.js +503 -0
  224. package/dist/src/lib/schema-codegen/generated-prompt-schemas.js.map +1 -0
  225. package/dist/src/lib/schema-codegen/generated-schema-descriptor.d.ts +129 -0
  226. package/dist/src/lib/schema-codegen/generated-schema-descriptor.js +154 -1
  227. package/dist/src/lib/schema-codegen/generated-schema-descriptor.js.map +1 -1
  228. package/dist/src/lib/schema-codegen/generated-schema-file.d.ts +129 -0
  229. package/dist/src/lib/schema-codegen/generated-schema-file.js +475 -0
  230. package/dist/src/lib/schema-codegen/generated-schema-file.js.map +1 -0
  231. package/dist/src/lib/schema-codegen/generated-schema-provenance.d.ts +115 -0
  232. package/dist/src/lib/schema-codegen/generated-schema-provenance.js +274 -0
  233. package/dist/src/lib/schema-codegen/generated-schema-provenance.js.map +1 -0
  234. package/dist/src/lib/schema-codegen/generated-schema-refs.d.ts +366 -0
  235. package/dist/src/lib/schema-codegen/generated-schema-refs.js +1789 -0
  236. package/dist/src/lib/schema-codegen/generated-schema-refs.js.map +1 -0
  237. package/dist/src/lib/schema-codegen/namedRefs.d.ts +56 -0
  238. package/dist/src/lib/schema-codegen/namedRefs.js +143 -0
  239. package/dist/src/lib/schema-codegen/namedRefs.js.map +1 -0
  240. package/dist/src/lib/schema-codegen/schemaChanges.d.ts +74 -0
  241. package/dist/src/lib/schema-codegen/schemaChanges.js +277 -0
  242. package/dist/src/lib/schema-codegen/schemaChanges.js.map +1 -0
  243. package/dist/src/lib/schema-codegen/schemaDocs.d.ts +69 -0
  244. package/dist/src/lib/schema-codegen/schemaDocs.js +219 -0
  245. package/dist/src/lib/schema-codegen/schemaDocs.js.map +1 -0
  246. package/dist/src/lib/schema-codegen/schemaIR.d.ts +158 -0
  247. package/dist/src/lib/schema-codegen/schemaIR.js +279 -0
  248. package/dist/src/lib/schema-codegen/schemaIR.js.map +1 -0
  249. package/dist/src/lib/schema-codegen/schemaToTs.d.ts +40 -13
  250. package/dist/src/lib/schema-codegen/schemaToTs.js +124 -160
  251. package/dist/src/lib/schema-codegen/schemaToTs.js.map +1 -1
  252. package/dist/src/lib/schema-codegen/valuesLists.d.ts +35 -0
  253. package/dist/src/lib/schema-codegen/valuesLists.js +128 -0
  254. package/dist/src/lib/schema-codegen/valuesLists.js.map +1 -0
  255. package/dist/src/lib/schema-family-index.d.ts +228 -0
  256. package/dist/src/lib/schema-family-index.js +813 -0
  257. package/dist/src/lib/schema-family-index.js.map +1 -0
  258. package/dist/src/lib/schema-pins.d.ts +82 -0
  259. package/dist/src/lib/schema-pins.js +198 -0
  260. package/dist/src/lib/schema-pins.js.map +1 -0
  261. package/dist/src/lib/schema-preview.d.ts +244 -0
  262. package/dist/src/lib/schema-preview.js +498 -0
  263. package/dist/src/lib/schema-preview.js.map +1 -0
  264. package/dist/src/lib/skill-installer.d.ts +2 -2
  265. package/dist/src/lib/skill-installer.js +2 -21
  266. package/dist/src/lib/skill-installer.js.map +1 -1
  267. package/dist/src/lib/step-run-table.d.ts +1 -2
  268. package/dist/src/lib/step-run-table.js +1 -2
  269. package/dist/src/lib/step-run-table.js.map +1 -1
  270. package/dist/src/lib/swift-codegen/agentGenerator.d.ts +2 -1
  271. package/dist/src/lib/swift-codegen/agentGenerator.js +24 -22
  272. package/dist/src/lib/swift-codegen/agentGenerator.js.map +1 -1
  273. package/dist/src/lib/swift-codegen/banners.d.ts +12 -0
  274. package/dist/src/lib/swift-codegen/banners.js +12 -0
  275. package/dist/src/lib/swift-codegen/banners.js.map +1 -1
  276. package/dist/src/lib/swift-codegen/functionGenerator.d.ts +36 -1
  277. package/dist/src/lib/swift-codegen/functionGenerator.js +167 -50
  278. package/dist/src/lib/swift-codegen/functionGenerator.js.map +1 -1
  279. package/dist/src/lib/swift-codegen/schemaToSwift.d.ts +31 -7
  280. package/dist/src/lib/swift-codegen/schemaToSwift.js +213 -177
  281. package/dist/src/lib/swift-codegen/schemaToSwift.js.map +1 -1
  282. package/dist/src/lib/swift-codegen/sharedGenerator.d.ts +45 -0
  283. package/dist/src/lib/swift-codegen/sharedGenerator.js +120 -0
  284. package/dist/src/lib/swift-codegen/sharedGenerator.js.map +1 -0
  285. package/dist/src/lib/swift-codegen/userPropertiesGenerator.d.ts +30 -0
  286. package/dist/src/lib/swift-codegen/userPropertiesGenerator.js +69 -0
  287. package/dist/src/lib/swift-codegen/userPropertiesGenerator.js.map +1 -0
  288. package/dist/src/lib/sync-resource-types.d.ts +37 -5
  289. package/dist/src/lib/sync-resource-types.js +53 -5
  290. package/dist/src/lib/sync-resource-types.js.map +1 -1
  291. package/dist/src/lib/sync-selectors.d.ts +7 -0
  292. package/dist/src/lib/sync-selectors.js +22 -2
  293. package/dist/src/lib/sync-selectors.js.map +1 -1
  294. package/dist/src/lib/test-runs.d.ts +45 -0
  295. package/dist/src/lib/test-runs.js +150 -0
  296. package/dist/src/lib/test-runs.js.map +1 -0
  297. package/dist/src/lib/toml-database-config.d.ts +2 -1
  298. package/dist/src/lib/toml-database-config.js +4 -3
  299. package/dist/src/lib/toml-database-config.js.map +1 -1
  300. package/dist/src/lib/toml-scalar-edit.js +6 -4
  301. package/dist/src/lib/toml-scalar-edit.js.map +1 -1
  302. package/dist/src/lib/user-index.d.ts +80 -0
  303. package/dist/src/lib/user-index.js +166 -0
  304. package/dist/src/lib/user-index.js.map +1 -0
  305. package/dist/src/lib/user-properties/generated-properties-schema.d.ts +159 -0
  306. package/dist/src/lib/user-properties/generated-properties-schema.js +471 -0
  307. package/dist/src/lib/user-properties/generated-properties-schema.js.map +1 -0
  308. package/dist/src/lib/user-properties/user-properties-codegen.d.ts +55 -0
  309. package/dist/src/lib/user-properties/user-properties-codegen.js +129 -0
  310. package/dist/src/lib/user-properties/user-properties-codegen.js.map +1 -0
  311. package/dist/src/lib/user-walk.d.ts +31 -0
  312. package/dist/src/lib/user-walk.js +102 -0
  313. package/dist/src/lib/user-walk.js.map +1 -0
  314. package/dist/src/lib/worker-perf-format.d.ts +22 -0
  315. package/dist/src/lib/worker-perf-format.js +27 -0
  316. package/dist/src/lib/worker-perf-format.js.map +1 -0
  317. package/dist/src/types/index.d.ts +13 -34
  318. package/package.json +5 -3
@@ -0,0 +1,658 @@
1
+ /**
2
+ * `functions/primitive.d.ts` — the one generated declaration (#5544, project
3
+ * `unified-schemas` phase 10).
4
+ *
5
+ * It replaces four per-family files (`primitive-function-types.d.ts`,
6
+ * `primitive-prompt-types.d.ts`, `primitive-db3-types.d.ts`,
7
+ * `primitive-user-properties.d.ts`), which repeated a shape once per place it
8
+ * was inlined. One module-style file now holds, in order:
9
+ *
10
+ * - five namespaces, each always present: `Schemas` (every def of every
11
+ * `schemas/*.toml`), `Functions` (`<Key>Input`/`<Key>Output`), `Prompts`
12
+ * (`<Key>PromptOutput` and the agent types), `Db3` (each model the
13
+ * `ctx.db3` handle types, and each db3 projection some schema in the tree
14
+ * references) and `UserProperties`;
15
+ * - the top-level glue interfaces with the names the four files used
16
+ * (`PrimitiveFunctionSchemas`, …), maps of references into the namespaces;
17
+ * - one `declare module "primitive-functions"` block with every
18
+ * augmentation. The augmentations extend the top-level glue names, so no
19
+ * name inside the block can shadow a namespace.
20
+ *
21
+ * ── Each shape once ──────────────────────────────────────────────────────
22
+ *
23
+ * Every schema is rendered COMPILED, and a node the compiler inlined a ref at
24
+ * renders as a reference to the target's one declaration (`Schemas.Money`)
25
+ * when the reference is exact (`namedRefs.ts`): a sibling that overrides the
26
+ * target renders inline, so the type says what the validator enforces.
27
+ *
28
+ * ── Names ────────────────────────────────────────────────────────────────
29
+ *
30
+ * Every name comes from the compiled space (`compiledSpace.ts`), computed
31
+ * once for both languages: short when one party produces it, renamed
32
+ * `<Namespace>_<PascalKey>_<Name>` for every party to a collision, refused
33
+ * when a collision survives. The shared Swift file reads the same tree
34
+ * (`describeTreeTypes`), so a reader types the same name in either language.
35
+ *
36
+ * A db3 model's `<Model>Filter` and `<Model>Sort` (#5546) are not parties:
37
+ * they are named after the model's emitted name once the space is computed,
38
+ * so a renamed model's helpers follow it and no other name moves in either
39
+ * language. A helper named like another `Db3` declaration is refused. They
40
+ * reference the SDK's `Db3Filter` and `Db3Sort` through import types, which
41
+ * no declaration in the namespace can shadow, so the file imports nothing.
42
+ *
43
+ * The bytes depend on the tree only: every list is sorted by key and def name.
44
+ */
45
+ import { describeAll as describeFunctionSet, describeFunctionSchemaSet, FunctionSchemaCodegenError, functionSchemaSources, renderDeclaredType, renderTypeDecl, } from "./function-schema-codegen.js";
46
+ import { describeAll as describePromptSet, promptSchemaSources, renderAgentType, } from "./prompt-schema-codegen.js";
47
+ import { dbTypeRecords, db3TypeSources } from "./db-type-records.js";
48
+ import { atom, PRINT_WIDTH, printedAtLevel, renderMember, tsPropertyKey, tsStringLiteral, union } from "./codegen-shared/prettierStable.js";
49
+ import { loadSchemaFamilyIndex } from "./schema-family-index.js";
50
+ import { compiledSpace } from "./schema-codegen/compiledSpace.js";
51
+ import { namedReferences } from "./schema-codegen/namedRefs.js";
52
+ import { canonicalJsonText, pointerSegment } from "./schema-codegen/schemaIR.js";
53
+ import { workflowTypeBaseName } from "./schema-codegen/naming.js";
54
+ import { SWIFT_KEYWORDS, structTypeName } from "./swift-codegen/swiftNaming.js";
55
+ import { workflowSwiftTypeBaseName } from "./swift-codegen/swiftKeys.js";
56
+ import { renderUserPropertiesDeclarations, USER_PROPERTIES_FILE, userPropertiesSource, } from "./user-properties/user-properties-codegen.js";
57
+ export { PRIMITIVE_DECLARATION_FILE, PRIMITIVE_DECLARATION_SPECIFIER } from "./primitive-declaration-names.js";
58
+ /** Every input a tree has. */
59
+ export function primitiveDeclarationInputs(configDir) {
60
+ return {
61
+ functions: functionSchemaSources(configDir),
62
+ prompts: promptSchemaSources(configDir),
63
+ db3: db3TypeSources(configDir),
64
+ userProperties: userPropertiesSource(configDir)?.schema ?? null,
65
+ configDir,
66
+ };
67
+ }
68
+ export const functionTypeId = (key, part) => `fn:${key}#/$defs/${part}`;
69
+ export const promptTypeId = (key, target) => `prompt:${key}#/$defs/${target}`;
70
+ export const defTypeId = (file, name) => `schemas:${file}#/$defs/${pointerSegment(name)}`;
71
+ const modelTypeId = (type, model) => `db3model:${type}#${model}`;
72
+ const USER_PROPERTIES_KEY = "UserProperties";
73
+ export const USER_PROPERTIES_IDS = {
74
+ userProperties: "user-properties#UserProperties",
75
+ clientUserProperties: "user-properties#ClientUserProperties",
76
+ uniqueUserProperties: "user-properties#UniqueUserProperties",
77
+ };
78
+ /** The reserved party standing for TypeScript's `Record` in the compiled space; it declares nothing. */
79
+ const TS_RECORD_ID = "typescript#Record";
80
+ /** A def's name in Swift: the def name, a keyword lifted out with `_`. */
81
+ const swiftDefName = (name) => (SWIFT_KEYWORDS.has(name) ? `${name}_` : name);
82
+ /** The tree's `schemas/` defs, each compiled; a file the push would refuse declares none (its preflight names it). */
83
+ function treeDefs(configDir) {
84
+ if (!configDir)
85
+ return [];
86
+ const defs = [];
87
+ for (const local of loadSchemaFamilyIndex(configDir).locals) {
88
+ const compile = local.compile;
89
+ if (local.error || !compile || "refusal" in compile)
90
+ continue;
91
+ for (const name of [...compile.names].sort()) {
92
+ defs.push({
93
+ id: defTypeId(local.file, name),
94
+ file: local.file,
95
+ name,
96
+ schema: compile.defs[name],
97
+ resolved: (compile.resolved?.[name] ?? []),
98
+ });
99
+ }
100
+ }
101
+ return defs;
102
+ }
103
+ /** A projection's identity: its schema without the root annotations naming its type. */
104
+ function projectionIdentity(schema) {
105
+ if (!schema || typeof schema !== "object" || Array.isArray(schema))
106
+ return canonicalJsonText(schema);
107
+ const { description: _d, $comment: _c, ...rest } = schema;
108
+ return canonicalJsonText(rest);
109
+ }
110
+ /** Every db3 projection a chain step of `resolved` names, with the definition it inlined. */
111
+ function collectProjections(resolved, into) {
112
+ for (const entry of resolved) {
113
+ entry.chain.forEach((step, index) => {
114
+ if (!step.startsWith("db3:") || into.has(step))
115
+ return;
116
+ const match = /^db3:([^#]+)#\/\$defs\/(.+)\/(row|declared)$/.exec(step);
117
+ const schema = entry.definitions?.[index];
118
+ if (!match || schema === undefined)
119
+ return;
120
+ const model = match[2].replace(/~1/g, "/").replace(/~0/g, "~");
121
+ into.set(step, { id: step, type: match[1], model, projection: match[3], schema });
122
+ });
123
+ }
124
+ }
125
+ /**
126
+ * Describe a tree's generated types and name them; see the module note.
127
+ * `language` picks the per-family refusals that language's generator makes
128
+ * (the TypeScript base-name check), and which collision refuses.
129
+ */
130
+ export function describeTreeTypes(inputs, language) {
131
+ const functionSources = inputs.functions ?? [];
132
+ const functions = (language === "ts" ? describeFunctionSet(functionSources) : describeFunctionSchemaSet(functionSources)).map((entry) => entry.fn);
133
+ const prompts = describePromptSet(inputs.prompts ?? []).map((entry) => entry.prompt);
134
+ const defs = treeDefs(inputs.configDir);
135
+ const userProperties = inputs.userProperties ?? null;
136
+ const names = [];
137
+ const namespaces = new Map();
138
+ const add = (name, referable = true) => {
139
+ names.push(name);
140
+ if (referable)
141
+ namespaces.set(name.id, name.namespace);
142
+ };
143
+ for (const def of defs) {
144
+ add({
145
+ id: def.id,
146
+ namespace: "Schemas",
147
+ key: def.file,
148
+ source: `schemas/${def.file}.toml`,
149
+ short: { ts: def.name, swift: swiftDefName(def.name) },
150
+ suffix: def.name,
151
+ });
152
+ }
153
+ for (const fn of functions) {
154
+ const swiftBase = workflowSwiftTypeBaseName(fn.key);
155
+ for (const [part, suffix] of [
156
+ ["input", "Input"],
157
+ ["output", "Output"],
158
+ ]) {
159
+ add({
160
+ id: functionTypeId(fn.key, part),
161
+ namespace: "Functions",
162
+ key: fn.key,
163
+ source: fn.fileLabel,
164
+ short: { ts: `${fn.typeBaseName}${suffix}`, swift: `${swiftBase}${suffix}` },
165
+ suffix,
166
+ });
167
+ }
168
+ }
169
+ for (const prompt of prompts) {
170
+ const base = prompt.typeBaseName;
171
+ const promptName = (target, typeName) => add({
172
+ id: promptTypeId(prompt.key, target),
173
+ namespace: "Prompts",
174
+ key: prompt.key,
175
+ source: prompt.fileLabel,
176
+ short: { ts: typeName, swift: typeName },
177
+ suffix: typeName.slice(base.length),
178
+ });
179
+ if (!prompt.agent) {
180
+ if (prompt.outputSchema !== undefined)
181
+ promptName("output", prompt.outputTypeName);
182
+ continue;
183
+ }
184
+ const agent = prompt.agent;
185
+ promptName("input", agent.variablesTypeName);
186
+ for (const tool of agent.clientTools) {
187
+ promptName(`tools/${tool.name}/input`, tool.input.typeName);
188
+ promptName(`tools/${tool.name}/output`, tool.output.typeName);
189
+ }
190
+ for (const presentation of agent.presentations)
191
+ promptName(`tools/${presentation.name}/input`, presentation.typeName);
192
+ for (const event of agent.events)
193
+ promptName(`events/${event.name}`, event.typeName);
194
+ }
195
+ // db3: each model the handle types, and each projection a rendered schema references.
196
+ const db3Types = [...(inputs.db3 ?? [])]
197
+ .sort((a, b) => (a.type < b.type ? -1 : a.type > b.type ? 1 : 0))
198
+ .map((source) => ({
199
+ type: source.type,
200
+ models: dbTypeRecords({
201
+ key: source.type,
202
+ tomlPath: source.tomlPath,
203
+ tomlContent: source.tomlContent,
204
+ table: source.directory === "db3-types" ? "db3" : "database",
205
+ }, "required").map((record) => ({ id: modelTypeId(source.type, record.modelName), type: source.type, ...record })),
206
+ }));
207
+ for (const { type, models } of db3Types) {
208
+ for (const model of models) {
209
+ const short = workflowTypeBaseName(model.modelName);
210
+ add({
211
+ id: model.id,
212
+ namespace: "Db3",
213
+ key: type,
214
+ source: `db3 type ${type}`,
215
+ short: { ts: short, swift: structTypeName(model.modelName) },
216
+ suffix: short,
217
+ // Two types declaring one model identically are one party: its
218
+ // members (fields and the stamps the type applies) in any order, as
219
+ // the audit compares the model table's canonical JSON. A member's
220
+ // query kind (#5546) is left out, so the row's name is what it was
221
+ // before filters existed; `db3KindEntries` opens a member whose
222
+ // kind the parties sharing a name disagree on.
223
+ identity: canonicalJsonText(JSON.parse(JSON.stringify([...model.members]
224
+ .sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
225
+ .map(({ kind: _kind, ...member }) => member)))),
226
+ }, false);
227
+ }
228
+ }
229
+ const byProjection = new Map();
230
+ for (const fn of functions) {
231
+ collectProjections(fn.resolved.inputSchema, byProjection);
232
+ collectProjections(fn.resolved.outputSchema, byProjection);
233
+ }
234
+ for (const prompt of prompts) {
235
+ collectProjections(prompt.resolved.inputSchema, byProjection);
236
+ collectProjections(prompt.resolved.outputSchema, byProjection);
237
+ collectProjections(prompt.resolved.agent, byProjection);
238
+ }
239
+ for (const def of defs)
240
+ collectProjections(def.resolved, byProjection);
241
+ const projections = [...byProjection.values()].sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
242
+ for (const projection of projections) {
243
+ const suffix = `${workflowTypeBaseName(projection.model)}${projection.projection === "row" ? "Row" : "Declared"}`;
244
+ add({
245
+ id: projection.id,
246
+ namespace: "Db3",
247
+ key: projection.type,
248
+ source: `db3 type ${projection.type}`,
249
+ short: { ts: suffix, swift: `${structTypeName(projection.model)}${projection.projection === "row" ? "Row" : "Declared"}` },
250
+ suffix,
251
+ identity: `${projection.projection}\u0000${projectionIdentity(projection.schema)}`,
252
+ });
253
+ }
254
+ for (const [field, name] of [
255
+ ["userProperties", "UserProperties"],
256
+ ["clientUserProperties", "ClientUserProperties"],
257
+ ["uniqueUserProperties", "UniqueUserProperties"],
258
+ ]) {
259
+ add({
260
+ id: USER_PROPERTIES_IDS[field],
261
+ namespace: "UserProperties",
262
+ key: USER_PROPERTIES_KEY,
263
+ source: USER_PROPERTIES_FILE,
264
+ short: { ts: name, swift: name },
265
+ suffix: name,
266
+ }, false);
267
+ }
268
+ // TypeScript's `Record`, which the declarations write unqualified (an empty
269
+ // model, an open map): a def or a model named so would shadow it inside its
270
+ // namespace, so it is renamed as though another party produced the name.
271
+ add({
272
+ id: TS_RECORD_ID,
273
+ namespace: "Schemas",
274
+ key: "TypeScript",
275
+ source: "TypeScript's built-in `Record`",
276
+ short: { ts: "Record", swift: TS_RECORD_ID },
277
+ suffix: "Record",
278
+ }, false);
279
+ const space = compiledSpace(names);
280
+ const collision = space.collisions[language][0];
281
+ if (collision && language === "ts") {
282
+ throw new FunctionSchemaCodegenError(`${collision.sources[0]} and ${collision.sources[1]} both produce the generated type \`${collision.name}\` even ` +
283
+ "after the collision rename — rename one so the two differ");
284
+ }
285
+ if (language === "ts")
286
+ refuseDb3HelperCollisions(db3Types, projections, space);
287
+ return { functions, prompts, defs, db3Types, projections, userProperties, space, namespaces };
288
+ }
289
+ /** The helpers each db3 model declares beside its interface (#5546), by the suffix on its emitted name. */
290
+ const DB3_HELPERS = [
291
+ ["Filter", "filter"],
292
+ ["Sort", "sort"],
293
+ ];
294
+ /**
295
+ * Refuse a db3 model's filter or sort named like another declaration of the
296
+ * `Db3` namespace (a model or a projection), whatever their fields: the
297
+ * helpers are not parties of the compiled space, so nothing renames them.
298
+ */
299
+ function refuseDb3HelperCollisions(db3Types, projections, space) {
300
+ const declared = new Map();
301
+ for (const { type, models } of db3Types) {
302
+ for (const model of models) {
303
+ const emitted = space.ts.get(model.id);
304
+ if (!declared.has(emitted))
305
+ declared.set(emitted, `db3 type ${type}`);
306
+ }
307
+ }
308
+ for (const projection of projections) {
309
+ const emitted = space.ts.get(projection.id);
310
+ if (!declared.has(emitted))
311
+ declared.set(emitted, `db3 type ${projection.type}`);
312
+ }
313
+ for (const { type, models } of db3Types) {
314
+ for (const model of models) {
315
+ for (const [suffix, role] of DB3_HELPERS) {
316
+ const helper = `${space.ts.get(model.id)}${suffix}`;
317
+ const other = declared.get(helper);
318
+ if (other === undefined)
319
+ continue;
320
+ throw new FunctionSchemaCodegenError(`${other} and db3 type ${type} (the ${role} of ${model.modelName}) both produce the generated type \`${helper}\` ` +
321
+ "— rename one so the two differ");
322
+ }
323
+ }
324
+ }
325
+ }
326
+ /**
327
+ * The name a reference to `ref` (a chain step: an explicit ref) renders in
328
+ * `language`, or null when the target has no generated declaration and
329
+ * renders inline: `Schemas.Money` in TypeScript, `Money` in Swift.
330
+ */
331
+ export function treeNameOf(tree, language) {
332
+ return (ref) => {
333
+ const namespace = tree.namespaces.get(ref);
334
+ if (!namespace)
335
+ return null;
336
+ const emitted = (language === "ts" ? tree.space.ts : tree.space.swift).get(ref);
337
+ if (emitted === undefined)
338
+ return null;
339
+ return language === "ts" ? `${namespace}.${emitted}` : emitted;
340
+ };
341
+ }
342
+ /**
343
+ * The schema each referable declaration is rendered from, by id: what a
344
+ * reference to it must match to be exact (`namedRefs.ts`).
345
+ */
346
+ export function treeTargetOf(tree) {
347
+ const known = targetsByTree.get(tree);
348
+ if (known)
349
+ return known;
350
+ const targets = new Map();
351
+ for (const def of tree.defs)
352
+ targets.set(def.id, def.schema);
353
+ for (const fn of tree.functions) {
354
+ targets.set(functionTypeId(fn.key, "input"), fn.inputSchema);
355
+ targets.set(functionTypeId(fn.key, "output"), fn.outputSchema);
356
+ }
357
+ for (const prompt of tree.prompts) {
358
+ const agent = prompt.agent;
359
+ if (!agent) {
360
+ targets.set(promptTypeId(prompt.key, "output"), prompt.outputSchema);
361
+ continue;
362
+ }
363
+ targets.set(promptTypeId(prompt.key, "input"), agent.inputSchema);
364
+ for (const tool of agent.clientTools) {
365
+ targets.set(promptTypeId(prompt.key, `tools/${tool.name}/input`), tool.input.schema);
366
+ targets.set(promptTypeId(prompt.key, `tools/${tool.name}/output`), tool.output.schema);
367
+ }
368
+ for (const presentation of agent.presentations)
369
+ targets.set(promptTypeId(prompt.key, `tools/${presentation.name}/input`), presentation.schema);
370
+ for (const event of agent.events)
371
+ targets.set(promptTypeId(prompt.key, `events/${event.name}`), event.schema);
372
+ }
373
+ for (const projection of tree.projections)
374
+ targets.set(projection.id, projection.schema);
375
+ const targetOf = (ref) => (targets.has(ref) ? { schema: targets.get(ref) } : undefined);
376
+ targetsByTree.set(tree, targetOf);
377
+ return targetOf;
378
+ }
379
+ const targetsByTree = new WeakMap();
380
+ /** The emitted (unqualified) name of an id. */
381
+ export function treeName(tree, language, id) {
382
+ return (language === "ts" ? tree.space.ts : tree.space.swift).get(id);
383
+ }
384
+ /**
385
+ * The names `config diff`'s declaration preview reads (#5544): the tree's
386
+ * compiled space, as `primitive.d.ts` names and references; null when the
387
+ * tree's declaration cannot be described (the push reports why).
388
+ */
389
+ export function previewNamesOf(configDir) {
390
+ let tree;
391
+ try {
392
+ tree = describeTreeTypes(primitiveDeclarationInputs(configDir), "ts");
393
+ }
394
+ catch {
395
+ return undefined;
396
+ }
397
+ const reference = treeNameOf(tree, "ts");
398
+ return {
399
+ reference,
400
+ target: treeTargetOf(tree),
401
+ declaration: (id, namespace, name) => `${namespace}.${tree.space.ts.get(id) ?? name}`,
402
+ };
403
+ }
404
+ /** The references a slot's compiled schema renders, by pointer under the slot. */
405
+ export function treeReferences(tree, language, schema, resolved, prefix = "") {
406
+ return namedReferences(schema, resolved, treeNameOf(tree, language), prefix, treeTargetOf(tree)).named;
407
+ }
408
+ const HEADER = `// GENERATED by \`primitive config push\` from this tree's functions/, prompts/,
409
+ // schemas/, db3-types/, database-type-configs/ and user-properties.toml.
410
+ // DO NOT EDIT — your changes are overwritten on the next push.
411
+ //
412
+ // Import a namespace and name its types through it:
413
+ //
414
+ // import type { Functions, Schemas } from "./primitive";
415
+ // const price: Schemas.Money = input.price;
416
+ //
417
+ // \`Schemas\` holds every def of schemas/*.toml, \`Functions\` each function's
418
+ // \`<Key>Input\` and \`<Key>Output\`, \`Prompts\` each prompt's types, \`Db3\` the
419
+ // models \`ctx.db3\` types (each with a \`<Model>Filter\` and a \`<Model>Sort\`)
420
+ // and the projections a schema references, and \`UserProperties\` the user
421
+ // property types. A shape a ref reaches is declared once and referenced by
422
+ // name. The augmentation at the end types \`defineFunction\`,
423
+ // \`ctx.prompts.run\`, \`ctx.db3\` and \`ctx.users\`.
424
+ //
425
+ // A db3 type's timestamps are required on a read row, epoch milliseconds unless
426
+ // the model declares the field: the room stamps every write. A model outside the
427
+ // type's \`timestamps.models\` carries none.
428
+ // A row written before the db3 was typed may lack them.`;
429
+ /**
430
+ * `export namespace <name> { … }`, each declaration printed one level deep as
431
+ * Prettier prints it there, a blank line apart; `{}` with none.
432
+ */
433
+ function namespaceBlock(name, decls) {
434
+ if (decls.length === 0)
435
+ return `export namespace ${name} {}`;
436
+ return [`export namespace ${name} {`, decls.map((render) => printedAtLevel(1, render)).join("\n\n"), "}"].join("\n");
437
+ }
438
+ /**
439
+ * A glue interface: its entries always expanded, which Prettier keeps; `{}`
440
+ * with none.
441
+ */
442
+ function glue(name, entries) {
443
+ if (entries.length === 0)
444
+ return `export interface ${name} {}`;
445
+ return [`export interface ${name} {`, ...entries, "}"].join("\n");
446
+ }
447
+ /** `export interface <name> extends <base> {}`, the `extends` clause on its own line when the whole does not fit. */
448
+ function extendsGlue(name, base) {
449
+ const line = `export interface ${name} extends ${base} {}`;
450
+ return line.length <= 80 ? line : `export interface ${name}\n extends ${base} {}`;
451
+ }
452
+ /** An object type always expanded at `level`: one `key: value;` line per entry, `{}` with none. */
453
+ function expanded(level, entries) {
454
+ if (entries.length === 0)
455
+ return "{}";
456
+ const pad = " ".repeat(level + 1);
457
+ return ["{", ...entries.map(([key, value]) => `${pad}${tsPropertyKey(key)}: ${value};`), `${" ".repeat(level)}}`].join("\n");
458
+ }
459
+ /** A db3 model member, as Prettier prints it one level inside its interface. */
460
+ function db3MemberLines(member) {
461
+ const type = member.enum ? union(member.enum.map((value) => atom(tsStringLiteral(value)))) : atom(member.type);
462
+ return [...(member.comment ? [` /** ${member.comment} */`] : []), renderMember(1, tsPropertyKey(member.name), member.optional, type)];
463
+ }
464
+ /** The SDK helpers a model's filter and sort instantiate, as import types (#5546). */
465
+ const DB3_FILTER_HELPER = `import("primitive-functions").Db3Filter`;
466
+ const DB3_SORT_HELPER = `import("primitive-functions").Db3Sort`;
467
+ /**
468
+ * A model's filter's kinds map, member by member in the interface's order:
469
+ * `id` first when the model declares none, each member with a kind, nothing
470
+ * for a model with no members. `shared` is every model emitted under this
471
+ * name (one party, so the same members by name); a member whose kind they do
472
+ * not all agree on (`date` in one type, `string` in another) is left out, and
473
+ * so open, rather than typed by one of them.
474
+ */
475
+ function db3KindEntries(members, shared) {
476
+ if (members.length === 0)
477
+ return [];
478
+ const agreed = (member) => shared.every((other) => other.find((candidate) => candidate.name === member.name)?.kind === member.kind);
479
+ const entries = [];
480
+ if (!members.some((member) => member.name === "id"))
481
+ entries.push(["id", "id"]);
482
+ for (const member of members)
483
+ if (member.kind && agreed(member))
484
+ entries.push([member.name, member.kind]);
485
+ return entries.map(([name, kind]) => [tsPropertyKey(name), tsStringLiteral(kind)]);
486
+ }
487
+ /**
488
+ * `export type <name> = <helper><<arg>>;` as Prettier prints it one level
489
+ * inside a namespace: on one line when it fits; else a lone object argument
490
+ * hugs its brackets on the `=` line when that line reaches its `{`; else the
491
+ * right-hand side moves below the `=`, on one line when it fits there, its
492
+ * object broken open otherwise. `members` is the object argument's entries,
493
+ * or null for a plain type argument (which never breaks).
494
+ */
495
+ function renderHelperAlias(name, helper, members, arg = "") {
496
+ const width = PRINT_WIDTH - 2;
497
+ const head = `export type ${name} =`;
498
+ const inlineArg = members === null ? arg : members.length === 0 ? "{}" : `{ ${members.map(([k, v]) => `${k}: ${v}`).join("; ")} }`;
499
+ const rhs = `${helper}<${inlineArg}>;`;
500
+ if (`${head} ${rhs}`.length <= width)
501
+ return `${head} ${rhs}`;
502
+ const broken = (level) => [`${helper}<{`, ...members.map(([k, v]) => `${" ".repeat(level + 1)}${k}: ${v};`), `${" ".repeat(level)}}>;`].join("\n");
503
+ const hugs = members !== null && members.length > 0;
504
+ if (hugs && `${head} ${helper}<{`.length <= width)
505
+ return `${head} ${broken(0)}`;
506
+ if (!hugs || ` ${rhs}`.length <= width)
507
+ return `${head}\n ${rhs}`;
508
+ return `${head}\n ${broken(1)}`;
509
+ }
510
+ /** A db3 model's `<Model>Filter` and `<Model>Sort` (#5546), after its interface. */
511
+ function db3HelperDecls(emitted, members, shared) {
512
+ return [
513
+ () => renderHelperAlias(`${emitted}Filter`, DB3_FILTER_HELPER, db3KindEntries(members, shared)),
514
+ () => renderHelperAlias(`${emitted}Sort`, DB3_SORT_HELPER, null, emitted),
515
+ ];
516
+ }
517
+ /** Render a declaration, naming the source and the slot when it cannot be. */
518
+ function renderNamed(where, render) {
519
+ try {
520
+ return render();
521
+ }
522
+ catch (error) {
523
+ if (error instanceof FunctionSchemaCodegenError)
524
+ throw error;
525
+ throw new FunctionSchemaCodegenError(`${where} cannot be rendered as a TypeScript type (${error?.message ?? error})`);
526
+ }
527
+ }
528
+ /** The whole `functions/primitive.d.ts`; see the module note. A fixed point of Prettier's default config. */
529
+ export function renderPrimitiveDeclaration(inputs) {
530
+ const tree = describeTreeTypes(inputs, "ts");
531
+ const name = (id) => treeName(tree, "ts", id);
532
+ const refs = (schema, resolved, prefix = "") => ({
533
+ named: treeReferences(tree, "ts", schema, resolved, prefix),
534
+ });
535
+ const schemas = tree.defs.map((def) => () => renderNamed(`schemas/${def.file}.toml: def \`${def.name}\``, () => renderTypeDecl(name(def.id), def.schema, refs(def.schema, def.resolved))));
536
+ const functions = tree.functions.flatMap((fn) => [
537
+ () => renderDeclaredType(fn, "inputSchema", name(functionTypeId(fn.key, "input")), refs(fn.inputSchema, fn.resolved.inputSchema)),
538
+ () => renderDeclaredType(fn, "outputSchema", name(functionTypeId(fn.key, "output")), refs(fn.outputSchema, fn.resolved.outputSchema)),
539
+ ]);
540
+ const prompts = [];
541
+ for (const prompt of tree.prompts) {
542
+ const agent = prompt.agent;
543
+ if (!agent) {
544
+ if (prompt.outputSchema === undefined)
545
+ continue;
546
+ prompts.push(() => renderAgentType(prompt, name(promptTypeId(prompt.key, "output")), prompt.outputSchema, "outputSchema", refs(prompt.outputSchema, prompt.resolved.outputSchema)));
547
+ continue;
548
+ }
549
+ const slot = (target, schema, path, pointer) => () => renderAgentType(prompt, name(promptTypeId(prompt.key, target)), schema, path, refs(schema, prompt.resolved.agent, pointer));
550
+ prompts.push(() => renderAgentType(prompt, name(promptTypeId(prompt.key, "input")), agent.inputSchema, "inputSchema", refs(agent.inputSchema, prompt.resolved.inputSchema)));
551
+ for (const tool of agent.clientTools) {
552
+ prompts.push(slot(`tools/${tool.name}/input`, tool.input.schema, `tools.${tool.name}.inputSchema`, tool.input.pointer));
553
+ prompts.push(slot(`tools/${tool.name}/output`, tool.output.schema, `tools.${tool.name}.outputSchema`, tool.output.pointer));
554
+ }
555
+ for (const presentation of agent.presentations) {
556
+ prompts.push(slot(`tools/${presentation.name}/input`, presentation.schema, `tools.${presentation.name}.inputSchema`, presentation.pointer));
557
+ }
558
+ for (const event of agent.events) {
559
+ prompts.push(slot(`events/${event.name}`, event.schema, `events.${event.name}.schema`, event.pointer));
560
+ }
561
+ }
562
+ // Db3: one interface per model party (two types declaring a model
563
+ // identically share one), then each referenced projection.
564
+ const db3 = [];
565
+ const declaredDb3 = new Set();
566
+ for (const { models } of tree.db3Types) {
567
+ for (const model of models) {
568
+ const emitted = name(model.id);
569
+ if (declaredDb3.has(emitted))
570
+ continue;
571
+ declaredDb3.add(emitted);
572
+ db3.push(() => model.members.length === 0
573
+ ? `export type ${emitted} = Record<string, unknown>;`
574
+ : [`export interface ${emitted} {`, ...model.members.flatMap(db3MemberLines), "}"].join("\n"));
575
+ const shared = tree.db3Types.flatMap(({ models: others }) => others.filter((other) => name(other.id) === emitted).map((other) => other.members));
576
+ db3.push(...db3HelperDecls(emitted, model.members, shared));
577
+ }
578
+ }
579
+ for (const projection of tree.projections) {
580
+ const emitted = name(projection.id);
581
+ if (declaredDb3.has(emitted))
582
+ continue;
583
+ declaredDb3.add(emitted);
584
+ db3.push(() => renderNamed(`db3 type ${projection.type}: the ${projection.projection} projection of \`${projection.model}\``, () => renderTypeDecl(emitted, projection.schema)));
585
+ }
586
+ const userPropertyNames = {
587
+ userProperties: name(USER_PROPERTIES_IDS.userProperties),
588
+ clientUserProperties: name(USER_PROPERTIES_IDS.clientUserProperties),
589
+ uniqueUserProperties: name(USER_PROPERTIES_IDS.uniqueUserProperties),
590
+ };
591
+ const functionEntries = tree.functions.map((fn) => ` ${tsPropertyKey(fn.key)}: ${expanded(1, [
592
+ ["input", `Functions.${name(functionTypeId(fn.key, "input"))}`],
593
+ ["output", `Functions.${name(functionTypeId(fn.key, "output"))}`],
594
+ ])};`);
595
+ const promptEntries = tree.prompts.map((prompt) => {
596
+ const key = tsPropertyKey(prompt.key);
597
+ const ref = (target) => `Prompts.${name(promptTypeId(prompt.key, target))}`;
598
+ const agent = prompt.agent;
599
+ if (!agent) {
600
+ if (prompt.outputSchema === undefined)
601
+ return ` ${key}: {};`;
602
+ const inline = ` ${key}: { output: ${ref("output")} };`;
603
+ return inline.length <= 80 ? inline : ` ${key}: ${expanded(1, [["output", ref("output")]])};`;
604
+ }
605
+ const tools = expanded(3, agent.clientTools.map((tool) => [
606
+ tool.name,
607
+ expanded(4, [
608
+ ["input", ref(`tools/${tool.name}/input`)],
609
+ ["output", ref(`tools/${tool.name}/output`)],
610
+ ]),
611
+ ]));
612
+ const events = expanded(3, agent.events.map((event) => [event.name, ref(`events/${event.name}`)]));
613
+ const presentations = expanded(3, agent.presentations.map((p) => [p.name, ref(`tools/${p.name}/input`)]));
614
+ const body = expanded(2, [
615
+ ["variables", ref("input")],
616
+ ["clientTools", tools],
617
+ ["events", events],
618
+ ["presentations", presentations],
619
+ ]);
620
+ return ` ${key}: ${expanded(1, [["agent", body]])};`;
621
+ });
622
+ const db3Entries = tree.db3Types.map(({ type, models }) => ` ${tsPropertyKey(type)}: ${expanded(1, models.map((model) => [model.modelName, `Db3.${name(model.id)}`]))};`);
623
+ const db3QueryEntries = tree.db3Types.map(({ type, models }) => ` ${tsPropertyKey(type)}: ${expanded(1, models.map((model) => [
624
+ model.modelName,
625
+ expanded(2, [
626
+ ["filter", `Db3.${name(model.id)}Filter`],
627
+ ["sort", `Db3.${name(model.id)}Sort`],
628
+ ]),
629
+ ]))};`);
630
+ const blocks = [
631
+ HEADER,
632
+ namespaceBlock("Schemas", schemas),
633
+ namespaceBlock("Functions", functions),
634
+ namespaceBlock("Prompts", prompts),
635
+ namespaceBlock("Db3", db3),
636
+ namespaceBlock("UserProperties", [0, 1, 2].map((index) => () => renderUserPropertiesDeclarations(tree.userProperties, userPropertyNames)[index])),
637
+ glue("PrimitiveFunctionSchemas", functionEntries),
638
+ glue("PrimitivePromptSchemas", promptEntries),
639
+ glue("PrimitiveDb3Schemas", db3Entries),
640
+ glue("PrimitiveDb3Queries", db3QueryEntries),
641
+ extendsGlue("PrimitiveUserProperties", `UserProperties.${userPropertyNames.userProperties}`),
642
+ extendsGlue("PrimitiveClientUserProperties", `UserProperties.${userPropertyNames.clientUserProperties}`),
643
+ extendsGlue("PrimitiveUserUniqueProperties", `UserProperties.${userPropertyNames.uniqueUserProperties}`),
644
+ [
645
+ `declare module "primitive-functions" {`,
646
+ ` interface FunctionSchemas extends PrimitiveFunctionSchemas {}`,
647
+ ` interface PromptSchemas extends PrimitivePromptSchemas {}`,
648
+ ` interface Db3Schemas extends PrimitiveDb3Schemas {}`,
649
+ ` interface Db3Queries extends PrimitiveDb3Queries {}`,
650
+ ` interface UserProperties extends PrimitiveUserProperties {}`,
651
+ ` interface ClientUserProperties extends PrimitiveClientUserProperties {}`,
652
+ ` interface UserUniqueProperties extends PrimitiveUserUniqueProperties {}`,
653
+ `}`,
654
+ ].join("\n"),
655
+ ];
656
+ return blocks.join("\n\n") + "\n";
657
+ }
658
+ //# sourceMappingURL=primitive-declaration.js.map