@agent-native/core 0.137.7 → 0.138.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (446) hide show
  1. package/agent-native.eject.json +2 -0
  2. package/corpus/README.md +1 -1
  3. package/corpus/templates/analytics/.agents/skills/agent-native-docs/SKILL.md +23 -3
  4. package/corpus/templates/analytics/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  5. package/corpus/templates/analytics/.agents/skills/data-querying/SKILL.md +15 -0
  6. package/corpus/templates/analytics/.agents/skills/frontend-design/SKILL.md +23 -2
  7. package/corpus/templates/analytics/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  8. package/corpus/templates/analytics/AGENTS.md +2 -2
  9. package/corpus/templates/analytics/actions/get-first-party-analytics-health.ts +65 -0
  10. package/corpus/templates/analytics/actions/provider-api-catalog.ts +3 -6
  11. package/corpus/templates/analytics/actions/provider-api-docs.ts +3 -6
  12. package/corpus/templates/analytics/actions/provider-api-request.ts +3 -6
  13. package/corpus/templates/analytics/actions/query-agent-native-analytics.ts +3 -3
  14. package/corpus/templates/analytics/agent-native.json +12 -0
  15. package/corpus/templates/analytics/app/i18n-data.ts +162 -0
  16. package/corpus/templates/analytics/app/pages/DataSources.tsx +126 -1
  17. package/corpus/templates/analytics/changelog/2026-08-05-analytics-now-flags-neon-pressure-and-guides-high-volume-tra.md +6 -0
  18. package/corpus/templates/analytics/changelog/2026-08-05-first-party-analytics-uses-compact-rollups-for-faster-usage-.md +6 -0
  19. package/corpus/templates/analytics/server/db/schema.ts +52 -0
  20. package/corpus/templates/analytics/server/lib/credential-keys.ts +3 -0
  21. package/corpus/templates/analytics/server/lib/first-party-analytics-health.ts +397 -0
  22. package/corpus/templates/analytics/server/lib/first-party-analytics-rollups.ts +182 -0
  23. package/corpus/templates/analytics/server/lib/first-party-analytics.ts +86 -15
  24. package/corpus/templates/analytics/server/lib/provider-api.ts +20 -1
  25. package/corpus/templates/analytics/server/lib/replay-storage.ts +1 -1
  26. package/corpus/templates/analytics/server/plugins/agent-chat.ts +1 -0
  27. package/corpus/templates/analytics/server/plugins/db.ts +60 -0
  28. package/corpus/templates/assets/.agents/skills/agent-native-docs/SKILL.md +23 -3
  29. package/corpus/templates/assets/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  30. package/corpus/templates/assets/.agents/skills/frontend-design/SKILL.md +23 -2
  31. package/corpus/templates/assets/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  32. package/corpus/templates/assets/agent-native.json +12 -0
  33. package/corpus/templates/assets/server/lib/generation.ts +8 -8
  34. package/corpus/templates/assets/server/lib/storage.ts +1 -1
  35. package/corpus/templates/assets/server/plugins/onboarding.ts +1 -1
  36. package/corpus/templates/brain/.agents/skills/agent-native-docs/SKILL.md +23 -3
  37. package/corpus/templates/brain/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  38. package/corpus/templates/brain/.agents/skills/frontend-design/SKILL.md +23 -2
  39. package/corpus/templates/brain/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  40. package/corpus/templates/brain/agent-native.json +12 -0
  41. package/corpus/templates/calendar/.agents/skills/agent-native-docs/SKILL.md +23 -3
  42. package/corpus/templates/calendar/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  43. package/corpus/templates/calendar/.agents/skills/frontend-design/SKILL.md +23 -2
  44. package/corpus/templates/calendar/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  45. package/corpus/templates/calendar/agent-native.json +12 -0
  46. package/corpus/templates/chat/.agents/skills/agent-native-docs/SKILL.md +23 -3
  47. package/corpus/templates/chat/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  48. package/corpus/templates/chat/.agents/skills/frontend-design/SKILL.md +23 -2
  49. package/corpus/templates/chat/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  50. package/corpus/templates/chat/AGENTS.md +3 -0
  51. package/corpus/templates/chat/DESIGN.md +28 -0
  52. package/corpus/templates/chat/agent-native.json +12 -0
  53. package/corpus/templates/clips/.agents/skills/agent-native-docs/SKILL.md +23 -3
  54. package/corpus/templates/clips/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  55. package/corpus/templates/clips/.agents/skills/frontend-design/SKILL.md +23 -2
  56. package/corpus/templates/clips/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  57. package/corpus/templates/clips/actions/cleanup-transcript.ts +2 -2
  58. package/corpus/templates/clips/actions/delete-recording-permanent.ts +3 -0
  59. package/corpus/templates/clips/actions/get-playback-position.ts +68 -0
  60. package/corpus/templates/clips/actions/import-loom-recording.ts +2 -2
  61. package/corpus/templates/clips/actions/save-playback-position.ts +80 -0
  62. package/corpus/templates/clips/actions/set-thumbnail.ts +1 -1
  63. package/corpus/templates/clips/agent-native.json +12 -0
  64. package/corpus/templates/clips/app/components/player/video-player.tsx +54 -5
  65. package/corpus/templates/clips/app/components/recorder/pre-record-panel.tsx +65 -18
  66. package/corpus/templates/clips/app/hooks/use-playback-position.ts +188 -0
  67. package/corpus/templates/clips/app/hooks/use-view-tracking.ts +6 -24
  68. package/corpus/templates/clips/app/i18n/en-US.ts +4 -0
  69. package/corpus/templates/clips/app/lib/playback-position.ts +99 -0
  70. package/corpus/templates/clips/app/lib/viewer-session.ts +29 -0
  71. package/corpus/templates/clips/changelog/2026-08-05-clip-buffering-now-uses-buffering-copy-after-playback.md +6 -0
  72. package/corpus/templates/clips/changelog/2026-08-05-clips-remembers-your-playback-position-when-you-reopen-a-cli.md +6 -0
  73. package/corpus/templates/clips/desktop/package.json +1 -0
  74. package/corpus/templates/clips/desktop/src/app.tsx +70 -6
  75. package/corpus/templates/clips/desktop/src/components/AlertDialog.tsx +131 -0
  76. package/corpus/templates/clips/desktop/src/styles.css +129 -0
  77. package/corpus/templates/clips/learnings.defaults.md +1 -1
  78. package/corpus/templates/clips/server/db/schema.ts +18 -0
  79. package/corpus/templates/clips/server/lib/public-agent-context.ts +1 -1
  80. package/corpus/templates/clips/server/lib/video-storage.ts +1 -1
  81. package/corpus/templates/clips/server/plugins/db.ts +15 -0
  82. package/corpus/templates/clips/server/routes/api/media/index.post.ts +1 -1
  83. package/corpus/templates/content/.agents/skills/agent-native-docs/SKILL.md +23 -3
  84. package/corpus/templates/content/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  85. package/corpus/templates/content/.agents/skills/document-editing/references/databases.md +13 -0
  86. package/corpus/templates/content/.agents/skills/frontend-design/SKILL.md +23 -2
  87. package/corpus/templates/content/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  88. package/corpus/templates/content/AGENTS.md +1 -0
  89. package/corpus/templates/content/actions/_content-database-mutation-lock.ts +41 -0
  90. package/corpus/templates/content/actions/_content-database-row-migration.ts +569 -0
  91. package/corpus/templates/content/actions/_database-source-utils.ts +82 -36
  92. package/corpus/templates/content/actions/_database-utils.ts +5 -0
  93. package/corpus/templates/content/actions/_delete-content-space.ts +25 -11
  94. package/corpus/templates/content/actions/add-database-item.ts +60 -42
  95. package/corpus/templates/content/actions/attach-content-database-source.ts +2 -0
  96. package/corpus/templates/content/actions/configure-document-property.ts +99 -104
  97. package/corpus/templates/content/actions/delete-content-database.ts +19 -7
  98. package/corpus/templates/content/actions/delete-document-property.ts +47 -58
  99. package/corpus/templates/content/actions/delete-document.ts +390 -167
  100. package/corpus/templates/content/actions/disconnect-content-database-source.ts +41 -24
  101. package/corpus/templates/content/actions/duplicate-database-item.ts +63 -31
  102. package/corpus/templates/content/actions/duplicate-database-items.ts +86 -43
  103. package/corpus/templates/content/actions/duplicate-document-property.ts +37 -14
  104. package/corpus/templates/content/actions/migrate-content-database-rows.ts +830 -0
  105. package/corpus/templates/content/actions/move-database-item.ts +14 -0
  106. package/corpus/templates/content/actions/permanently-delete-document.ts +27 -6
  107. package/corpus/templates/content/actions/remove-database-items.ts +13 -0
  108. package/corpus/templates/content/actions/reorder-document-property.ts +45 -46
  109. package/corpus/templates/content/actions/set-document-property.ts +68 -28
  110. package/corpus/templates/content/actions/submit-content-database-form.ts +49 -0
  111. package/corpus/templates/content/actions/transcribe-media.ts +1 -1
  112. package/corpus/templates/content/agent-native.json +12 -0
  113. package/corpus/templates/content/app/components/editor/image-upload.ts +2 -2
  114. package/corpus/templates/content/app/i18n-data.ts +1 -1
  115. package/corpus/templates/content/changelog/2026-08-01-content-can-reorganize-every-row-in-an-existing-database-as-.md +6 -0
  116. package/corpus/templates/content/parity/matrix.md +1 -1
  117. package/corpus/templates/content/parity/matrix.ts +4 -2
  118. package/corpus/templates/content/server/db/schema.ts +30 -0
  119. package/corpus/templates/content/server/plugins/db.ts +24 -0
  120. package/corpus/templates/content/server/plugins/onboarding.ts +2 -2
  121. package/corpus/templates/crm/.agents/skills/agent-native-docs/SKILL.md +23 -3
  122. package/corpus/templates/crm/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  123. package/corpus/templates/crm/agent-native.json +12 -0
  124. package/corpus/templates/design/.agents/skills/agent-native-docs/SKILL.md +23 -3
  125. package/corpus/templates/design/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  126. package/corpus/templates/design/.agents/skills/design-generation/SKILL.md +2 -0
  127. package/corpus/templates/design/.agents/skills/frontend-design/SKILL.md +23 -2
  128. package/corpus/templates/design/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  129. package/corpus/templates/design/CODE_NATIVE_DESIGN_STUDIO_IMPLEMENTATION_PLAN.md +2 -2
  130. package/corpus/templates/design/DESIGN-STUDIO-PLAN.md +2 -2
  131. package/corpus/templates/design/actions/apply-design-token-edit.ts +1 -1
  132. package/corpus/templates/design/actions/capture-design-state.ts +1 -1
  133. package/corpus/templates/design/actions/connect-builder-app.ts +2 -2
  134. package/corpus/templates/design/actions/create-design-branch.ts +1 -1
  135. package/corpus/templates/design/actions/create-fusion-app.ts +3 -3
  136. package/corpus/templates/design/actions/deploy-design-preview.ts +1 -1
  137. package/corpus/templates/design/actions/generate-design.ts +3 -2
  138. package/corpus/templates/design/actions/get-component-details.ts +1 -1
  139. package/corpus/templates/design/actions/get-design-branch-diff.ts +1 -1
  140. package/corpus/templates/design/actions/index-components.ts +3 -3
  141. package/corpus/templates/design/actions/index-design-system-with-builder.ts +1 -1
  142. package/corpus/templates/design/actions/migrate-inline-design-to-app.ts +3 -3
  143. package/corpus/templates/design/actions/open-component-source.ts +1 -1
  144. package/corpus/templates/design/actions/update-design.ts +96 -14
  145. package/corpus/templates/design/agent-native.json +12 -0
  146. package/corpus/templates/design/app/components/design/DesignImportPanel.tsx +7 -2
  147. package/corpus/templates/design/app/components/design/FigmaHydrationDialog.tsx +11 -2
  148. package/corpus/templates/design/app/components/design/edit-panel/component-section.tsx +1 -1
  149. package/corpus/templates/design/app/components/editor/PromptDialog.tsx +8 -1
  150. package/corpus/templates/design/app/i18n-data.ts +53 -22
  151. package/corpus/templates/design/app/lib/design-file-upload.ts +2 -3
  152. package/corpus/templates/design/app/lib/upload-limits.ts +4 -0
  153. package/corpus/templates/design/app/pages/DesignSystemSetup.tsx +1 -1
  154. package/corpus/templates/design/changelog/2026-08-05-chat-attachments-now-warn-before-upload-when-they-exceed-the.md +6 -0
  155. package/corpus/templates/design/changelog/2026-08-05-figma-fig-uploads-now-show-a-clear-4-mb-limit-up-front-inste.md +6 -0
  156. package/corpus/templates/design/scripts/qa-figma-stress-import.ts +1 -1
  157. package/corpus/templates/design/server/handlers/import-design-file.ts +9 -5
  158. package/corpus/templates/design/server/handlers/uploads.ts +13 -9
  159. package/corpus/templates/design/server/lib/fig-file-limits.ts +2 -1
  160. package/corpus/templates/design/server/lib/figma-image-hydration.ts +1 -1
  161. package/corpus/templates/design/server/lib/figma-node-import.ts +1 -1
  162. package/corpus/templates/design/server/lib/request-body-limits.ts +11 -0
  163. package/corpus/templates/design/shared/canvas-frames.ts +81 -0
  164. package/corpus/templates/design/shared/design-source-capabilities.ts +25 -9
  165. package/corpus/templates/dispatch/.agents/skills/agent-native-docs/SKILL.md +23 -3
  166. package/corpus/templates/dispatch/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  167. package/corpus/templates/dispatch/.agents/skills/frontend-design/SKILL.md +23 -2
  168. package/corpus/templates/dispatch/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  169. package/corpus/templates/dispatch/agent-native.json +12 -0
  170. package/corpus/templates/factory/.agents/skills/agent-native-docs/SKILL.md +23 -3
  171. package/corpus/templates/factory/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  172. package/corpus/templates/factory/.agents/skills/frontend-design/SKILL.md +23 -2
  173. package/corpus/templates/factory/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  174. package/corpus/templates/factory/agent-native.json +12 -0
  175. package/corpus/templates/forms/.agents/skills/agent-native-docs/SKILL.md +23 -3
  176. package/corpus/templates/forms/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  177. package/corpus/templates/forms/.agents/skills/frontend-design/SKILL.md +23 -2
  178. package/corpus/templates/forms/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  179. package/corpus/templates/forms/actions/export-responses.ts +1 -1
  180. package/corpus/templates/forms/agent-native.json +12 -0
  181. package/corpus/templates/macros/.agents/skills/agent-native-docs/SKILL.md +23 -3
  182. package/corpus/templates/macros/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  183. package/corpus/templates/macros/.agents/skills/frontend-design/SKILL.md +23 -2
  184. package/corpus/templates/macros/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  185. package/corpus/templates/macros/agent-native.json +12 -0
  186. package/corpus/templates/mail/.agents/skills/agent-native-docs/SKILL.md +23 -3
  187. package/corpus/templates/mail/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  188. package/corpus/templates/mail/.agents/skills/frontend-design/SKILL.md +23 -2
  189. package/corpus/templates/mail/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  190. package/corpus/templates/mail/agent-native.json +12 -0
  191. package/corpus/templates/mail/app/i18n/en-US.ts +1 -1
  192. package/corpus/templates/mail/server/lib/media-upload.ts +1 -1
  193. package/corpus/templates/plan/.agents/skills/agent-native-docs/SKILL.md +23 -3
  194. package/corpus/templates/plan/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  195. package/corpus/templates/plan/.agents/skills/frontend-design/SKILL.md +23 -2
  196. package/corpus/templates/plan/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  197. package/corpus/templates/plan/agent-native.json +12 -0
  198. package/corpus/templates/plan/server/lib/plan-assets.ts +1 -1
  199. package/corpus/templates/slides/.agents/skills/agent-native-docs/SKILL.md +23 -3
  200. package/corpus/templates/slides/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  201. package/corpus/templates/slides/.agents/skills/frontend-design/SKILL.md +23 -2
  202. package/corpus/templates/slides/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  203. package/corpus/templates/slides/actions/generate-image-api.ts +1 -1
  204. package/corpus/templates/slides/actions/generate-slides-ai.ts +1 -1
  205. package/corpus/templates/slides/actions/import-file.ts +2 -2
  206. package/corpus/templates/slides/actions/index-design-system-with-builder.ts +1 -1
  207. package/corpus/templates/slides/agent-native.json +12 -0
  208. package/corpus/templates/slides/app/components/design-system/DesignSystemSetup.tsx +1 -1
  209. package/corpus/templates/slides/app/i18n/en-US.ts +2 -2
  210. package/corpus/templates/slides/app/lib/deck-sorting.ts +19 -0
  211. package/corpus/templates/slides/app/lib/image-drop-to-agent.ts +1 -1
  212. package/corpus/templates/slides/app/pages/Index.tsx +7 -2
  213. package/corpus/templates/slides/changelog/2026-08-05-decks-are-now-sorted-by-most-recently-updated.md +6 -0
  214. package/corpus/templates/slides/server/handlers/assets.ts +1 -1
  215. package/corpus/templates/slides/server/handlers/uploads.ts +1 -1
  216. package/corpus/templates/tasks/.agents/skills/agent-native-docs/SKILL.md +23 -3
  217. package/corpus/templates/tasks/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  218. package/corpus/templates/tasks/.agents/skills/frontend-design/SKILL.md +23 -2
  219. package/corpus/templates/tasks/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  220. package/corpus/templates/tasks/agent-native.json +12 -0
  221. package/dist/action.d.ts +6 -0
  222. package/dist/agent/engine/builder-engine.js +2 -2
  223. package/dist/agent/engine/credential-errors.d.ts +1 -1
  224. package/dist/agent/engine/credential-errors.js +2 -2
  225. package/dist/agent/production-agent.js +3 -2
  226. package/dist/brand-kit/fig/index.js +1 -1
  227. package/dist/cli/agent.js +45 -3
  228. package/dist/cli/code-agent-executor.js +2 -1
  229. package/dist/cli/create.d.ts +9 -1
  230. package/dist/cli/create.js +94 -1
  231. package/dist/cli/doctor.d.ts +8 -11
  232. package/dist/cli/doctor.js +92 -24
  233. package/dist/cli/index.js +6 -5
  234. package/dist/client/AgentAskPopover.d.ts +3 -1
  235. package/dist/client/AgentAskPopover.js +2 -2
  236. package/dist/client/AgentPanel.js +9 -3
  237. package/dist/client/AssistantChat.js +54 -33
  238. package/dist/client/MultiTabAssistantChat.js +1 -1
  239. package/dist/client/RouteTransitionIndicator.d.ts +7 -0
  240. package/dist/client/RouteTransitionIndicator.js +31 -0
  241. package/dist/client/agent-engine-key.js +1 -1
  242. package/dist/client/app-providers.js +11 -2
  243. package/dist/client/chat/message-components.js +1 -1
  244. package/dist/client/chat/run-recovery.js +1 -1
  245. package/dist/client/chat/tool-call-display.d.ts +3 -1
  246. package/dist/client/chat/tool-call-display.js +4 -3
  247. package/dist/client/chat-model-groups.d.ts +6 -0
  248. package/dist/client/chat-model-groups.js +11 -0
  249. package/dist/client/error-format.js +1 -1
  250. package/dist/client/i18n.js +1 -1
  251. package/dist/client/onboarding/FirstRunOnboarding.d.ts +5 -1
  252. package/dist/client/onboarding/FirstRunOnboarding.js +29 -13
  253. package/dist/client/onboarding/first-run-enabled.d.ts +6 -1
  254. package/dist/client/onboarding/first-run-enabled.js +33 -3
  255. package/dist/client/onboarding/use-onboarding.js +16 -11
  256. package/dist/client/require-session.js +13 -4
  257. package/dist/client/resources/mcp-integration-catalog.js +26 -0
  258. package/dist/client/resources/mcp-integration-logos.js +4 -0
  259. package/dist/client/route-warmup.js +56 -10
  260. package/dist/client/settings/AccountSettingsCard.js +78 -3
  261. package/dist/client/settings/SettingsPanel.js +1 -1
  262. package/dist/client/settings/VoiceTranscriptionSection.js +2 -2
  263. package/dist/client/settings/useBuilderStatus.js +1 -1
  264. package/dist/client/setup-connections/useBuilderConnectCardController.js +1 -1
  265. package/dist/client/ui/index.d.ts +1 -0
  266. package/dist/client/ui/index.js +1 -0
  267. package/dist/client/uploads/upload-editor-image.js +1 -1
  268. package/dist/client/use-chat-threads.js +3 -0
  269. package/dist/client/use-session.d.ts +11 -0
  270. package/dist/client/use-session.js +34 -6
  271. package/dist/collab/routes.d.ts +2 -2
  272. package/dist/config.d.ts +48 -0
  273. package/dist/config.js +111 -0
  274. package/dist/db/migrations.js +5 -0
  275. package/dist/eject/provider-api-definitions.d.ts +1 -1
  276. package/dist/eject/provider-api-definitions.js +1 -0
  277. package/dist/eject/remote-mcp-presets.js +1 -0
  278. package/dist/extensions/web-search-tool.js +2 -2
  279. package/dist/file-upload/actions/upload-image.js +1 -1
  280. package/dist/file-upload/registry.js +1 -1
  281. package/dist/guards/index.d.ts +3 -0
  282. package/dist/guards/index.js +2 -0
  283. package/dist/guards/no-empty-migrations.d.ts +14 -0
  284. package/dist/guards/no-empty-migrations.js +126 -0
  285. package/dist/index.browser.d.ts +1 -0
  286. package/dist/index.browser.js +3 -0
  287. package/dist/index.d.ts +1 -0
  288. package/dist/index.js +1 -0
  289. package/dist/localization/default-messages.d.ts +24 -0
  290. package/dist/localization/default-messages.js +26 -2
  291. package/dist/notifications/routes.d.ts +6 -6
  292. package/dist/onboarding/app-profile.js +1 -1
  293. package/dist/onboarding/plugin.d.ts +2 -0
  294. package/dist/onboarding/plugin.js +29 -2
  295. package/dist/progress/routes.d.ts +1 -1
  296. package/dist/provider-api/actions/custom-provider-registration.d.ts +16 -16
  297. package/dist/provider-api/actions/provider-api.d.ts +13 -13
  298. package/dist/provider-api/index.d.ts +4 -4
  299. package/dist/provider-api/index.js +35 -0
  300. package/dist/resources/handlers.d.ts +1 -1
  301. package/dist/resources/handlers.js +1 -1
  302. package/dist/scripts/docs/framework-search.d.ts +9 -0
  303. package/dist/scripts/docs/framework-search.js +239 -0
  304. package/dist/scripts/docs/index.js +1 -0
  305. package/dist/scripts/docs/search.d.ts +10 -0
  306. package/dist/scripts/docs/search.js +1 -1
  307. package/dist/scripts/docs/source-search.d.ts +10 -2
  308. package/dist/scripts/docs/source-search.js +108 -41
  309. package/dist/search-utils/index.d.ts +11 -0
  310. package/dist/search-utils/index.js +72 -0
  311. package/dist/server/action-change.d.ts +6 -0
  312. package/dist/server/action-change.js +28 -14
  313. package/dist/server/action-discovery.js +9 -0
  314. package/dist/server/action-routes.js +1 -0
  315. package/dist/server/agent-chat/browser-team-tools.js +1 -1
  316. package/dist/server/agent-chat/script-entries.js +43 -2
  317. package/dist/server/auth-login-mode.d.ts +8 -0
  318. package/dist/server/auth-login-mode.js +17 -0
  319. package/dist/server/auth.js +100 -15
  320. package/dist/server/better-auth-instance.d.ts +36 -0
  321. package/dist/server/better-auth-instance.js +25 -3
  322. package/dist/server/builder-design-systems.js +1 -1
  323. package/dist/server/core-routes-plugin.js +17 -14
  324. package/dist/server/credential-provider.js +3 -3
  325. package/dist/server/email-templates.d.ts +7 -0
  326. package/dist/server/email-templates.js +24 -0
  327. package/dist/server/email.d.ts +17 -0
  328. package/dist/server/email.js +22 -1
  329. package/dist/server/framework-request-handler.js +6 -0
  330. package/dist/server/onboarding-html.d.ts +2 -0
  331. package/dist/server/onboarding-html.js +169 -8
  332. package/dist/server/realtime-token.d.ts +1 -1
  333. package/dist/server/realtime-voice.js +1 -1
  334. package/dist/server/sentry.d.ts +1 -1
  335. package/dist/server/transcribe-voice.js +3 -3
  336. package/dist/shared/first-run-onboarding.d.ts +1 -0
  337. package/dist/shared/first-run-onboarding.js +1 -0
  338. package/dist/templates/chat/.agents/skills/agent-native-docs/SKILL.md +23 -3
  339. package/dist/templates/chat/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  340. package/dist/templates/chat/.agents/skills/frontend-design/SKILL.md +23 -2
  341. package/dist/templates/chat/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  342. package/dist/templates/chat/AGENTS.md +3 -0
  343. package/dist/templates/chat/DESIGN.md +28 -0
  344. package/dist/templates/chat/agent-native.json +12 -0
  345. package/dist/templates/default/.agents/skills/agent-native-docs/SKILL.md +23 -3
  346. package/dist/templates/default/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  347. package/dist/templates/default/.agents/skills/frontend-design/SKILL.md +23 -2
  348. package/dist/templates/default/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  349. package/dist/templates/default/AGENTS.md +4 -0
  350. package/dist/templates/default/DESIGN.md +30 -0
  351. package/dist/templates/default/agent-native.json +9 -0
  352. package/dist/templates/default/app/i18n/ar-SA.ts +2 -2
  353. package/dist/templates/default/app/i18n/de-DE.ts +2 -2
  354. package/dist/templates/default/app/i18n/en-US.ts +2 -2
  355. package/dist/templates/default/app/i18n/es-ES.ts +2 -2
  356. package/dist/templates/default/app/i18n/fr-FR.ts +2 -2
  357. package/dist/templates/default/app/i18n/hi-IN.ts +2 -2
  358. package/dist/templates/default/app/i18n/ja-JP.ts +2 -2
  359. package/dist/templates/default/app/i18n/ko-KR.ts +2 -2
  360. package/dist/templates/default/app/i18n/pt-BR.ts +2 -2
  361. package/dist/templates/default/app/i18n/zh-CN.ts +2 -2
  362. package/dist/templates/default/app/i18n/zh-TW.ts +1 -1
  363. package/dist/templates/default/package.json +1 -0
  364. package/dist/templates/headless/.agents/skills/agent-native-docs/SKILL.md +23 -3
  365. package/dist/templates/headless/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  366. package/dist/templates/headless/agent-native.json +9 -0
  367. package/dist/templates/headless/package.json +2 -0
  368. package/dist/templates/workspace-core/.agents/skills/agent-native-docs/SKILL.md +23 -3
  369. package/dist/templates/workspace-core/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  370. package/dist/templates/workspace-core/.agents/skills/frontend-design/SKILL.md +23 -2
  371. package/dist/templates/workspace-core/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  372. package/dist/templates/workspace-core/AGENTS.md +3 -0
  373. package/dist/templates/workspace-core/DESIGN.md +35 -0
  374. package/dist/templates/workspace-root/AGENTS.md +3 -0
  375. package/dist/templates/workspace-root/DESIGN.md +35 -0
  376. package/dist/templates/workspace-root/agent-native.json +9 -0
  377. package/dist/templates/workspace-root/package.json +3 -0
  378. package/dist/transcription/builder-transcription.js +1 -1
  379. package/dist/user-profile/actions/change-password.d.ts +8 -0
  380. package/dist/user-profile/actions/change-password.js +24 -0
  381. package/dist/user-profile/actions/get-auth-methods.d.ts +6 -0
  382. package/dist/user-profile/actions/get-auth-methods.js +23 -0
  383. package/dist/user-profile/actions/set-password.d.ts +7 -0
  384. package/dist/user-profile/actions/set-password.js +23 -0
  385. package/dist/vite/client.d.ts +8 -1
  386. package/dist/vite/client.js +63 -12
  387. package/dist/vite/index.d.ts +1 -0
  388. package/dist/vite/index.js +1 -0
  389. package/docs/AGENTS.md +7 -2
  390. package/docs/SKILL.md +7 -5
  391. package/docs/content/agent-native-config.mdx +165 -0
  392. package/docs/content/agent-native-toolkit.mdx +1 -1
  393. package/docs/content/deployment.mdx +7 -2
  394. package/docs/content/doctor.mdx +21 -13
  395. package/docs/content/environment-variables.mdx +158 -0
  396. package/docs/content/file-uploads.mdx +1 -1
  397. package/docs/content/getting-started.mdx +1 -1
  398. package/docs/content/key-concepts.mdx +1 -1
  399. package/docs/content/multi-app-workspace.mdx +1 -1
  400. package/docs/content/template-clips-ai-and-editing.mdx +6 -6
  401. package/docs/content/template-clips-developers.mdx +3 -3
  402. package/docs/content/toolkit-settings.mdx +1 -1
  403. package/docs/content/voice-input.mdx +4 -4
  404. package/package.json +6 -2
  405. package/src/eject/provider-api-definitions.ts +1 -0
  406. package/src/eject/remote-mcp-presets.ts +1 -0
  407. package/src/templates/chat/.agents/skills/agent-native-docs/SKILL.md +23 -3
  408. package/src/templates/chat/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  409. package/src/templates/chat/.agents/skills/frontend-design/SKILL.md +23 -2
  410. package/src/templates/chat/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  411. package/src/templates/chat/AGENTS.md +3 -0
  412. package/src/templates/chat/DESIGN.md +28 -0
  413. package/src/templates/chat/agent-native.json +12 -0
  414. package/src/templates/default/.agents/skills/agent-native-docs/SKILL.md +23 -3
  415. package/src/templates/default/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  416. package/src/templates/default/.agents/skills/frontend-design/SKILL.md +23 -2
  417. package/src/templates/default/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  418. package/src/templates/default/AGENTS.md +4 -0
  419. package/src/templates/default/DESIGN.md +30 -0
  420. package/src/templates/default/agent-native.json +9 -0
  421. package/src/templates/default/app/i18n/ar-SA.ts +2 -2
  422. package/src/templates/default/app/i18n/de-DE.ts +2 -2
  423. package/src/templates/default/app/i18n/en-US.ts +2 -2
  424. package/src/templates/default/app/i18n/es-ES.ts +2 -2
  425. package/src/templates/default/app/i18n/fr-FR.ts +2 -2
  426. package/src/templates/default/app/i18n/hi-IN.ts +2 -2
  427. package/src/templates/default/app/i18n/ja-JP.ts +2 -2
  428. package/src/templates/default/app/i18n/ko-KR.ts +2 -2
  429. package/src/templates/default/app/i18n/pt-BR.ts +2 -2
  430. package/src/templates/default/app/i18n/zh-CN.ts +2 -2
  431. package/src/templates/default/app/i18n/zh-TW.ts +1 -1
  432. package/src/templates/default/package.json +1 -0
  433. package/src/templates/headless/.agents/skills/agent-native-docs/SKILL.md +23 -3
  434. package/src/templates/headless/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  435. package/src/templates/headless/agent-native.json +9 -0
  436. package/src/templates/headless/package.json +2 -0
  437. package/src/templates/workspace-core/.agents/skills/agent-native-docs/SKILL.md +23 -3
  438. package/src/templates/workspace-core/.agents/skills/agent-native-toolkit/SKILL.md +16 -0
  439. package/src/templates/workspace-core/.agents/skills/frontend-design/SKILL.md +23 -2
  440. package/src/templates/workspace-core/.agents/skills/frontend-design/references/visual-direction.md +99 -0
  441. package/src/templates/workspace-core/AGENTS.md +3 -0
  442. package/src/templates/workspace-core/DESIGN.md +35 -0
  443. package/src/templates/workspace-root/AGENTS.md +3 -0
  444. package/src/templates/workspace-root/DESIGN.md +35 -0
  445. package/src/templates/workspace-root/agent-native.json +9 -0
  446. package/src/templates/workspace-root/package.json +3 -0
@@ -0,0 +1,397 @@
1
+ import { sql } from "@agent-native/core/db/schema";
2
+ import { buildDeepLink } from "@agent-native/core/server";
3
+ import { and, gte, inArray, lte } from "drizzle-orm";
4
+
5
+ import { getDb, schema } from "../db/index.js";
6
+ import { hasCredential } from "./credentials.js";
7
+ import type { AnalyticsScope } from "./first-party-analytics.js";
8
+
9
+ export const FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS = {
10
+ slowQueryMs: 5_000,
11
+ recommendEventCount: 1_000_000,
12
+ recommendSlowQueries24h: 3,
13
+ recommendMaxQueryMs: 30_000,
14
+ } as const;
15
+
16
+ const BIGQUERY_SETUP_LINK = buildDeepLink({
17
+ app: "analytics",
18
+ view: "data-sources",
19
+ to: "/data-sources?source=bigquery",
20
+ });
21
+
22
+ export type FirstPartyAnalyticsQueryClass =
23
+ | "raw-events"
24
+ | "rollups"
25
+ | "session-replay"
26
+ | "mixed"
27
+ | "other";
28
+
29
+ export type FirstPartyAnalyticsQueryOutcome = "success" | "timeout" | "error";
30
+
31
+ export type FirstPartyAnalyticsHealthStatus =
32
+ | "healthy"
33
+ | "monitor"
34
+ | "recommend_bigquery"
35
+ | "unavailable";
36
+
37
+ export type FirstPartyAnalyticsRecommendation =
38
+ | "none"
39
+ | "connect_bigquery"
40
+ | "use_bigquery";
41
+
42
+ export interface FirstPartyAnalyticsHealth {
43
+ status: FirstPartyAnalyticsHealthStatus;
44
+ recommendation: FirstPartyAnalyticsRecommendation;
45
+ reasons: Array<"event_volume" | "slow_queries" | "query_timeout">;
46
+ observedAt: string;
47
+ metrics: {
48
+ eventCount: number;
49
+ dailyRollupRows: number;
50
+ firstEventDate: string | null;
51
+ lastEventDate: string | null;
52
+ spanDays: number;
53
+ slowQueryCount24h: number;
54
+ timeoutCount24h: number;
55
+ errorCount24h: number;
56
+ maxQueryDurationMs24h: number;
57
+ };
58
+ thresholds: typeof FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS;
59
+ bigQuery: {
60
+ configured: boolean | null;
61
+ missingRequiredKeys: string[];
62
+ setupLink: string;
63
+ };
64
+ }
65
+
66
+ interface QueryPressureEvent {
67
+ durationMs: number;
68
+ outcome: FirstPartyAnalyticsQueryOutcome;
69
+ queryClass: FirstPartyAnalyticsQueryClass;
70
+ }
71
+
72
+ function nowIso(): string {
73
+ return new Date().toISOString();
74
+ }
75
+
76
+ function todayIsoDate(): string {
77
+ return nowIso().slice(0, 10);
78
+ }
79
+
80
+ function tenantKey(scope: AnalyticsScope): string {
81
+ return scope.orgId ? `org:${scope.orgId}` : `user:${scope.userEmail}`;
82
+ }
83
+
84
+ function stableId(parts: readonly string[]): string {
85
+ return `aqpd_${parts.map((part) => encodeURIComponent(part)).join("|")}`;
86
+ }
87
+
88
+ function finiteNumber(value: unknown): number {
89
+ if (typeof value === "number" && Number.isFinite(value)) return value;
90
+ if (typeof value === "bigint") return Number(value);
91
+ if (typeof value === "string" && value.trim()) {
92
+ const parsed = Number(value);
93
+ if (Number.isFinite(parsed)) return parsed;
94
+ }
95
+ return 0;
96
+ }
97
+
98
+ function normalizedDurationMs(value: number): number {
99
+ return Number.isFinite(value) ? Math.max(0, Math.round(value)) : 0;
100
+ }
101
+
102
+ function isTimeoutError(error: unknown): boolean {
103
+ const message = error instanceof Error ? error.message : String(error);
104
+ return /timeout|timed out|statement deadline|cancelled by deadline/i.test(
105
+ message,
106
+ );
107
+ }
108
+
109
+ export function queryOutcomeFromError(
110
+ error: unknown,
111
+ ): FirstPartyAnalyticsQueryOutcome {
112
+ return isTimeoutError(error) ? "timeout" : "error";
113
+ }
114
+
115
+ export function classifyFirstPartyAnalyticsQuery(
116
+ query: string,
117
+ ): FirstPartyAnalyticsQueryClass {
118
+ const normalized = query.toLowerCase();
119
+ const usesRawEvents = /\banalytics_events\b/.test(normalized);
120
+ const usesRollups =
121
+ /\banalytics_event_daily_rollups\b|\banalytics_user_days\b/.test(
122
+ normalized,
123
+ );
124
+ const usesSessionReplay = /\bsession_recordings\b/.test(normalized);
125
+ const sourceCount =
126
+ Number(usesRawEvents) + Number(usesRollups) + Number(usesSessionReplay);
127
+
128
+ if (sourceCount > 1) return "mixed";
129
+ if (usesRawEvents) return "raw-events";
130
+ if (usesRollups) return "rollups";
131
+ if (usesSessionReplay) return "session-replay";
132
+ return "other";
133
+ }
134
+
135
+ function shouldRecordPressure(event: QueryPressureEvent): boolean {
136
+ return (
137
+ event.outcome !== "success" ||
138
+ normalizedDurationMs(event.durationMs) >=
139
+ FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS.slowQueryMs
140
+ );
141
+ }
142
+
143
+ /**
144
+ * Persist only slow/failing query aggregates. This intentionally sits outside
145
+ * the raw event stream so diagnosing database pressure cannot recursively add
146
+ * more analytics events to the same database.
147
+ */
148
+ export async function recordFirstPartyAnalyticsQueryPressure(
149
+ scope: AnalyticsScope,
150
+ event: QueryPressureEvent,
151
+ ): Promise<void> {
152
+ if (!shouldRecordPressure(event)) return;
153
+
154
+ const durationMs = normalizedDurationMs(event.durationMs);
155
+ const eventDate = todayIsoDate();
156
+ const key = tenantKey(scope);
157
+ const table = schema.analyticsQueryPressureDaily;
158
+ const db = getDb() as any;
159
+ const isSlow =
160
+ event.outcome !== "success" ||
161
+ durationMs >= FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS.slowQueryMs;
162
+
163
+ await db
164
+ .insert(table)
165
+ .values({
166
+ id: stableId([key, eventDate, event.queryClass]),
167
+ tenantKey: key,
168
+ ownerEmail: scope.userEmail,
169
+ orgId: scope.orgId,
170
+ eventDate,
171
+ queryClass: event.queryClass,
172
+ slowQueryCount: isSlow ? 1 : 0,
173
+ timeoutCount: event.outcome === "timeout" ? 1 : 0,
174
+ errorCount: event.outcome === "error" ? 1 : 0,
175
+ totalDurationMs: durationMs,
176
+ maxDurationMs: durationMs,
177
+ lastSeenAt: nowIso(),
178
+ })
179
+ .onConflictDoUpdate({
180
+ target: [table.tenantKey, table.eventDate, table.queryClass],
181
+ set: {
182
+ slowQueryCount: sql`${table.slowQueryCount} + ${isSlow ? 1 : 0}`,
183
+ timeoutCount: sql`${table.timeoutCount} + ${event.outcome === "timeout" ? 1 : 0}`,
184
+ errorCount: sql`${table.errorCount} + ${event.outcome === "error" ? 1 : 0}`,
185
+ totalDurationMs: sql`${table.totalDurationMs} + ${durationMs}`,
186
+ maxDurationMs: sql`CASE WHEN ${table.maxDurationMs} > ${durationMs} THEN ${table.maxDurationMs} ELSE ${durationMs} END`,
187
+ lastSeenAt: nowIso(),
188
+ },
189
+ });
190
+ }
191
+
192
+ function dateSpanDays(
193
+ firstEventDate: string | null,
194
+ lastEventDate: string | null,
195
+ ): number {
196
+ if (!firstEventDate || !lastEventDate) return 0;
197
+ const first = Date.parse(`${firstEventDate}T00:00:00.000Z`);
198
+ const last = Date.parse(`${lastEventDate}T00:00:00.000Z`);
199
+ if (!Number.isFinite(first) || !Number.isFinite(last) || last < first) {
200
+ return 0;
201
+ }
202
+ return Math.floor((last - first) / 86_400_000) + 1;
203
+ }
204
+
205
+ async function bigQueryStatus(scope: AnalyticsScope): Promise<{
206
+ configured: boolean | null;
207
+ missingRequiredKeys: string[];
208
+ setupLink: string;
209
+ }> {
210
+ const requiredKeys = [
211
+ "GOOGLE_APPLICATION_CREDENTIALS_JSON",
212
+ "BIGQUERY_PROJECT_ID",
213
+ ];
214
+ try {
215
+ const configured = await Promise.all(
216
+ requiredKeys.map((key) =>
217
+ hasCredential(key, {
218
+ userEmail: scope.userEmail,
219
+ orgId: scope.orgId,
220
+ }),
221
+ ),
222
+ );
223
+ return {
224
+ configured: configured.every(Boolean),
225
+ missingRequiredKeys: requiredKeys.filter(
226
+ (_, index) => !configured[index],
227
+ ),
228
+ setupLink: BIGQUERY_SETUP_LINK,
229
+ };
230
+ } catch (error) {
231
+ console.warn("[first-party-analytics] BigQuery status unavailable:", error);
232
+ return {
233
+ configured: null,
234
+ missingRequiredKeys: [],
235
+ setupLink: BIGQUERY_SETUP_LINK,
236
+ };
237
+ }
238
+ }
239
+
240
+ function recommendationFor(
241
+ status: FirstPartyAnalyticsHealthStatus,
242
+ configured: boolean | null,
243
+ ): FirstPartyAnalyticsRecommendation {
244
+ if (status !== "recommend_bigquery") return "none";
245
+ return configured === true ? "use_bigquery" : "connect_bigquery";
246
+ }
247
+
248
+ export async function getFirstPartyAnalyticsHealth(
249
+ scope: AnalyticsScope,
250
+ ): Promise<FirstPartyAnalyticsHealth> {
251
+ const now = new Date();
252
+ const today = now.toISOString().slice(0, 10);
253
+ const since24h = new Date(now.getTime() - 86_400_000)
254
+ .toISOString()
255
+ .slice(0, 10);
256
+ const keys = scope.orgId
257
+ ? [`org:${scope.orgId}`, `user:${scope.userEmail}`]
258
+ : [`user:${scope.userEmail}`];
259
+ const rollups = schema.analyticsEventDailyRollups;
260
+ const pressure = schema.analyticsQueryPressureDaily;
261
+ const db = getDb() as any;
262
+
263
+ const [rollupRows, pressureRows, bigQuery] = await Promise.all([
264
+ db
265
+ .select({
266
+ eventCount: sql<number>`coalesce(sum(${rollups.eventCount}), 0)`,
267
+ dailyRollupRows: sql<number>`count(*)`,
268
+ firstEventDate: sql<string | null>`min(${rollups.eventDate})`,
269
+ lastEventDate: sql<string | null>`max(${rollups.eventDate})`,
270
+ })
271
+ .from(rollups)
272
+ .where(
273
+ and(inArray(rollups.tenantKey, keys), lte(rollups.eventDate, today)),
274
+ ),
275
+ db
276
+ .select({
277
+ queryClass: pressure.queryClass,
278
+ slowQueryCount: pressure.slowQueryCount,
279
+ timeoutCount: pressure.timeoutCount,
280
+ errorCount: pressure.errorCount,
281
+ maxDurationMs: pressure.maxDurationMs,
282
+ })
283
+ .from(pressure)
284
+ .where(
285
+ and(
286
+ inArray(pressure.tenantKey, keys),
287
+ gte(pressure.eventDate, since24h),
288
+ lte(pressure.eventDate, today),
289
+ ),
290
+ ),
291
+ bigQueryStatus(scope),
292
+ ]);
293
+
294
+ const rollup = (rollupRows[0] ?? {}) as Record<string, unknown>;
295
+ const eventCount = finiteNumber(rollup.eventCount);
296
+ const dailyRollupRows = finiteNumber(rollup.dailyRollupRows);
297
+ const firstEventDate =
298
+ typeof rollup.firstEventDate === "string" ? rollup.firstEventDate : null;
299
+ const lastEventDate =
300
+ typeof rollup.lastEventDate === "string" ? rollup.lastEventDate : null;
301
+ const slowQueryCount24h = pressureRows.reduce(
302
+ (sum: number, row: Record<string, unknown>) =>
303
+ sum + finiteNumber(row.slowQueryCount),
304
+ 0,
305
+ );
306
+ const timeoutCount24h = pressureRows.reduce(
307
+ (sum: number, row: Record<string, unknown>) =>
308
+ sum + finiteNumber(row.timeoutCount),
309
+ 0,
310
+ );
311
+ const errorCount24h = pressureRows.reduce(
312
+ (sum: number, row: Record<string, unknown>) =>
313
+ sum + finiteNumber(row.errorCount),
314
+ 0,
315
+ );
316
+ const maxQueryDurationMs24h = pressureRows.reduce(
317
+ (max: number, row: Record<string, unknown>) =>
318
+ Math.max(max, finiteNumber(row.maxDurationMs)),
319
+ 0,
320
+ );
321
+ const reasons: FirstPartyAnalyticsHealth["reasons"] = [];
322
+ if (
323
+ eventCount >= FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS.recommendEventCount
324
+ ) {
325
+ reasons.push("event_volume");
326
+ }
327
+ if (
328
+ slowQueryCount24h >=
329
+ FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS.recommendSlowQueries24h
330
+ ) {
331
+ reasons.push("slow_queries");
332
+ }
333
+ if (
334
+ timeoutCount24h > 0 ||
335
+ maxQueryDurationMs24h >=
336
+ FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS.recommendMaxQueryMs
337
+ ) {
338
+ reasons.push("query_timeout");
339
+ }
340
+
341
+ const hasMonitorSignal =
342
+ eventCount >=
343
+ FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS.recommendEventCount / 4 ||
344
+ slowQueryCount24h > 0;
345
+ const status: FirstPartyAnalyticsHealthStatus =
346
+ reasons.length > 0
347
+ ? "recommend_bigquery"
348
+ : hasMonitorSignal
349
+ ? "monitor"
350
+ : "healthy";
351
+
352
+ return {
353
+ status,
354
+ recommendation: recommendationFor(status, bigQuery.configured),
355
+ reasons,
356
+ observedAt: nowIso(),
357
+ metrics: {
358
+ eventCount,
359
+ dailyRollupRows,
360
+ firstEventDate,
361
+ lastEventDate,
362
+ spanDays: dateSpanDays(firstEventDate, lastEventDate),
363
+ slowQueryCount24h,
364
+ timeoutCount24h,
365
+ errorCount24h,
366
+ maxQueryDurationMs24h,
367
+ },
368
+ thresholds: FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS,
369
+ bigQuery,
370
+ };
371
+ }
372
+
373
+ export function unavailableFirstPartyAnalyticsHealth(): FirstPartyAnalyticsHealth {
374
+ return {
375
+ status: "unavailable",
376
+ recommendation: "none",
377
+ reasons: [],
378
+ observedAt: nowIso(),
379
+ metrics: {
380
+ eventCount: 0,
381
+ dailyRollupRows: 0,
382
+ firstEventDate: null,
383
+ lastEventDate: null,
384
+ spanDays: 0,
385
+ slowQueryCount24h: 0,
386
+ timeoutCount24h: 0,
387
+ errorCount24h: 0,
388
+ maxQueryDurationMs24h: 0,
389
+ },
390
+ thresholds: FIRST_PARTY_ANALYTICS_PRESSURE_THRESHOLDS,
391
+ bigQuery: {
392
+ configured: null,
393
+ missingRequiredKeys: [],
394
+ setupLink: BIGQUERY_SETUP_LINK,
395
+ },
396
+ };
397
+ }
@@ -0,0 +1,182 @@
1
+ import { sql } from "@agent-native/core/db/schema";
2
+
3
+ import { getDb, schema } from "../db/index.js";
4
+
5
+ /**
6
+ * The normalized subset emitted by first-party analytics ingest. Keeping this
7
+ * contract smaller than the raw event row makes it clear that rollups never
8
+ * need to parse properties or context JSON.
9
+ */
10
+ export interface NormalizedFirstPartyAnalyticsEventRow {
11
+ eventName: string;
12
+ eventDate: string;
13
+ ownerEmail: string;
14
+ orgId?: string | null;
15
+ userKey?: string | null;
16
+ app?: string | null;
17
+ template?: string | null;
18
+ }
19
+
20
+ export interface FirstPartyAnalyticsRollupResult {
21
+ eventCount: number;
22
+ dailyRollupCount: number;
23
+ userDayCount: number;
24
+ }
25
+
26
+ interface DailyRollupRow {
27
+ id: string;
28
+ tenantKey: string;
29
+ ownerEmail: string;
30
+ orgId: string | null;
31
+ eventDate: string;
32
+ eventName: string;
33
+ app: string;
34
+ template: string;
35
+ eventCount: number;
36
+ }
37
+
38
+ interface UserDayRow {
39
+ id: string;
40
+ tenantKey: string;
41
+ ownerEmail: string;
42
+ orgId: string | null;
43
+ eventDate: string;
44
+ userKey: string;
45
+ }
46
+
47
+ function requiredString(value: unknown, field: string): string {
48
+ if (typeof value !== "string" || !value.trim()) {
49
+ throw new Error(`Analytics rollup row requires a non-empty ${field}`);
50
+ }
51
+ return value.trim();
52
+ }
53
+
54
+ function nullableString(value: unknown, field: string): string | null {
55
+ if (value == null) return null;
56
+ if (typeof value !== "string") {
57
+ throw new Error(`Analytics rollup row ${field} must be a string or null`);
58
+ }
59
+ const trimmed = value.trim();
60
+ return trimmed || null;
61
+ }
62
+
63
+ function tenantKey(ownerEmail: string, orgId: string | null): string {
64
+ return orgId ? `org:${orgId}` : `user:${ownerEmail}`;
65
+ }
66
+
67
+ function compositeKey(parts: readonly string[]): string {
68
+ return JSON.stringify(parts);
69
+ }
70
+
71
+ function stableId(prefix: string, parts: readonly string[]): string {
72
+ return `${prefix}_${parts.map((part) => encodeURIComponent(part)).join("|")}`;
73
+ }
74
+
75
+ /**
76
+ * Upsert compact rollups after the corresponding raw analytics rows commit.
77
+ * The caller owns the raw insert; this function deliberately does not read or
78
+ * write analytics_events, so ingestion can choose its own failure boundary.
79
+ */
80
+ export async function upsertFirstPartyAnalyticsRollups(
81
+ rows: readonly NormalizedFirstPartyAnalyticsEventRow[],
82
+ ): Promise<FirstPartyAnalyticsRollupResult> {
83
+ const dailyRollups = new Map<string, DailyRollupRow>();
84
+ const userDays = new Map<string, UserDayRow>();
85
+
86
+ for (const row of rows) {
87
+ const eventName = requiredString(row.eventName, "eventName");
88
+ const eventDate = requiredString(row.eventDate, "eventDate");
89
+ const ownerEmail = requiredString(row.ownerEmail, "ownerEmail");
90
+ const orgId = nullableString(row.orgId, "orgId");
91
+ const userKey = nullableString(row.userKey, "userKey");
92
+ const app = nullableString(row.app, "app") ?? "";
93
+ const template = nullableString(row.template, "template") ?? "";
94
+ const scopeKey = tenantKey(ownerEmail, orgId);
95
+ const dailyKey = compositeKey([
96
+ scopeKey,
97
+ eventDate,
98
+ eventName,
99
+ app,
100
+ template,
101
+ ]);
102
+ const existingDaily = dailyRollups.get(dailyKey);
103
+
104
+ if (existingDaily) {
105
+ existingDaily.eventCount += 1;
106
+ } else {
107
+ dailyRollups.set(dailyKey, {
108
+ id: stableId("aedr", [scopeKey, eventDate, eventName, app, template]),
109
+ tenantKey: scopeKey,
110
+ ownerEmail,
111
+ orgId,
112
+ eventDate,
113
+ eventName,
114
+ app,
115
+ template,
116
+ eventCount: 1,
117
+ });
118
+ }
119
+
120
+ if (userKey) {
121
+ const userDayKey = compositeKey([scopeKey, eventDate, userKey]);
122
+ if (!userDays.has(userDayKey)) {
123
+ userDays.set(userDayKey, {
124
+ id: stableId("aud", [scopeKey, eventDate, userKey]),
125
+ tenantKey: scopeKey,
126
+ ownerEmail,
127
+ orgId,
128
+ eventDate,
129
+ userKey,
130
+ });
131
+ }
132
+ }
133
+ }
134
+
135
+ if (dailyRollups.size === 0) {
136
+ return {
137
+ eventCount: rows.length,
138
+ dailyRollupCount: 0,
139
+ userDayCount: 0,
140
+ };
141
+ }
142
+
143
+ const db = getDb() as any;
144
+ await db.transaction(async (tx: any) => {
145
+ const dailyRows = [...dailyRollups.values()];
146
+ await tx
147
+ .insert(schema.analyticsEventDailyRollups)
148
+ .values(dailyRows)
149
+ .onConflictDoUpdate({
150
+ target: [
151
+ schema.analyticsEventDailyRollups.tenantKey,
152
+ schema.analyticsEventDailyRollups.eventDate,
153
+ schema.analyticsEventDailyRollups.eventName,
154
+ schema.analyticsEventDailyRollups.app,
155
+ schema.analyticsEventDailyRollups.template,
156
+ ],
157
+ set: {
158
+ eventCount: sql`${schema.analyticsEventDailyRollups.eventCount} + excluded.event_count`,
159
+ },
160
+ });
161
+
162
+ const userDayRows = [...userDays.values()];
163
+ if (userDayRows.length > 0) {
164
+ await tx
165
+ .insert(schema.analyticsUserDays)
166
+ .values(userDayRows)
167
+ .onConflictDoNothing({
168
+ target: [
169
+ schema.analyticsUserDays.tenantKey,
170
+ schema.analyticsUserDays.eventDate,
171
+ schema.analyticsUserDays.userKey,
172
+ ],
173
+ });
174
+ }
175
+ });
176
+
177
+ return {
178
+ eventCount: rows.length,
179
+ dailyRollupCount: dailyRollups.size,
180
+ userDayCount: userDays.size,
181
+ };
182
+ }