create-ailk 0.39.0 → 0.40.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 (187) hide show
  1. package/dist/add-route.js +15 -0
  2. package/dist/body-heading.d.ts +2 -1
  3. package/dist/body-heading.js +31 -3
  4. package/dist/module-architecture.js +0 -3
  5. package/dist/remove-module.js +13 -4
  6. package/dist/routes.d.ts +7 -0
  7. package/dist/routes.js +1 -1
  8. package/dist/site-roster.js +7 -1
  9. package/dist/surfaces.d.ts +2 -0
  10. package/dist/surfaces.js +59 -172
  11. package/package.json +3 -3
  12. package/templates/apps/web/CLAUDE.md +7 -1
  13. package/templates/apps/web/__tests__/__mocks__/next-intl-server.ts +3 -2
  14. package/templates/apps/web/__tests__/__mocks__/next-intl.ts +3 -2
  15. package/templates/apps/web/app/[locale]/layout.tsx +1 -1
  16. package/templates/apps/web/i18n/request.ts +5 -1
  17. package/templates/apps/web/jest.config.cjs +6 -0
  18. package/templates/apps/web/lib/__tests__/deck-page.test.ts +9 -0
  19. package/templates/apps/web/lib/__tests__/in-page-magnet-self-request.test.ts +1 -1
  20. package/templates/apps/web/lib/__tests__/in-page-magnet.test.ts +2 -2
  21. package/templates/apps/web/lib/__tests__/pricing-page.test.ts +1 -1
  22. package/templates/apps/web/lib/__tests__/structured-data.test.tsx +17 -4
  23. package/templates/apps/web/lib/in-page-magnet.ts +2 -2
  24. package/templates/apps/web/lib/pricing-page.ts +3 -3
  25. package/templates/apps/web/{app/[locale]/contact → lib}/site-contact-email.ts +1 -1
  26. package/templates/apps/web/{app/[locale]/contact → lib}/site-contact-lines.ts +1 -1
  27. package/templates/apps/web/lib/site-header.ts +1 -1
  28. package/templates/apps/web/messages/en.json +0 -119
  29. package/templates/apps/web/next.config.mjs +7 -0
  30. package/templates/apps/web/package.json +1 -0
  31. package/templates/apps/web/site-pages.config.tsx +85 -0
  32. package/templates/tools/body-heading.ts +30 -3
  33. package/versions.json +50 -49
  34. package/templates/apps/web/app/[locale]/(authed)/leads/__tests__/page.test.tsx +0 -205
  35. package/templates/apps/web/app/[locale]/(authed)/leads/components/LeadsTable.tsx +0 -137
  36. package/templates/apps/web/app/[locale]/(authed)/leads/page.tsx +0 -122
  37. package/templates/apps/web/app/[locale]/(authed)/waitlist/components/ForbiddenState.tsx +0 -24
  38. package/templates/apps/web/app/[locale]/(authed)/waitlist/format.ts +0 -111
  39. package/templates/apps/web/app/[locale]/[slug]/[detail]/__tests__/page.test.tsx +0 -402
  40. package/templates/apps/web/app/[locale]/[slug]/[detail]/page.tsx +0 -143
  41. package/templates/apps/web/app/[locale]/[slug]/__tests__/page.test.tsx +0 -148
  42. package/templates/apps/web/app/[locale]/[slug]/page.tsx +0 -109
  43. package/templates/apps/web/app/[locale]/about/[slug]/page.tsx +0 -46
  44. package/templates/apps/web/app/[locale]/about/__tests__/about-routes.test.tsx +0 -353
  45. package/templates/apps/web/app/[locale]/about/page.tsx +0 -122
  46. package/templates/apps/web/app/[locale]/alternatives/__tests__/page.test.tsx +0 -161
  47. package/templates/apps/web/app/[locale]/alternatives/page.tsx +0 -112
  48. package/templates/apps/web/app/[locale]/careers/[slug]/[action]/page.tsx +0 -244
  49. package/templates/apps/web/app/[locale]/careers/[slug]/apply.actions.ts +0 -325
  50. package/templates/apps/web/app/[locale]/careers/[slug]/job-application-form.ts +0 -120
  51. package/templates/apps/web/app/[locale]/careers/[slug]/page.tsx +0 -73
  52. package/templates/apps/web/app/[locale]/careers/__tests__/apply.actions.test.ts +0 -497
  53. package/templates/apps/web/app/[locale]/careers/__tests__/careers-hub.test.tsx +0 -172
  54. package/templates/apps/web/app/[locale]/careers/__tests__/careers-set.test.tsx +0 -153
  55. package/templates/apps/web/app/[locale]/careers/__tests__/job-application-form.test.ts +0 -141
  56. package/templates/apps/web/app/[locale]/careers/__tests__/job-apply-page.test.tsx +0 -546
  57. package/templates/apps/web/app/[locale]/careers/__tests__/page-renderer-migration.test.tsx +0 -189
  58. package/templates/apps/web/app/[locale]/careers/page.tsx +0 -165
  59. package/templates/apps/web/app/[locale]/case-studies/[slug]/page.tsx +0 -49
  60. package/templates/apps/web/app/[locale]/case-studies/__tests__/hub-children.test.tsx +0 -65
  61. package/templates/apps/web/app/[locale]/case-studies/__tests__/page-renderer-migration.test.tsx +0 -125
  62. package/templates/apps/web/app/[locale]/case-studies/page.tsx +0 -49
  63. package/templates/apps/web/app/[locale]/clients/__tests__/page-renderer-migration.test.tsx +0 -85
  64. package/templates/apps/web/app/[locale]/clients/page.tsx +0 -47
  65. package/templates/apps/web/app/[locale]/contact/ConversationalIntakeWrapper.tsx +0 -249
  66. package/templates/apps/web/app/[locale]/contact/__tests__/ConversationalIntakeWrapper.test.tsx +0 -400
  67. package/templates/apps/web/app/[locale]/contact/__tests__/contact-form-magnet.test.ts +0 -138
  68. package/templates/apps/web/app/[locale]/contact/__tests__/contact-form-text.test.ts +0 -49
  69. package/templates/apps/web/app/[locale]/contact/__tests__/contact-magnet-page.test.tsx +0 -494
  70. package/templates/apps/web/app/[locale]/contact/__tests__/contact.actions.test.ts +0 -1122
  71. package/templates/apps/web/app/[locale]/contact/__tests__/intake-analytics.test.ts +0 -51
  72. package/templates/apps/web/app/[locale]/contact/__tests__/page-renderer-migration.test.tsx +0 -319
  73. package/templates/apps/web/app/[locale]/contact/contact-form-magnet.ts +0 -77
  74. package/templates/apps/web/app/[locale]/contact/contact-form-text.ts +0 -52
  75. package/templates/apps/web/app/[locale]/contact/contact.actions.ts +0 -403
  76. package/templates/apps/web/app/[locale]/contact/intake-analytics.ts +0 -49
  77. package/templates/apps/web/app/[locale]/contact/intake-script.ts +0 -92
  78. package/templates/apps/web/app/[locale]/contact/page.tsx +0 -267
  79. package/templates/apps/web/app/[locale]/courses/[slug]/page.tsx +0 -50
  80. package/templates/apps/web/app/[locale]/courses/__tests__/hub-children.test.tsx +0 -32
  81. package/templates/apps/web/app/[locale]/courses/__tests__/page-renderer-migration.test.tsx +0 -123
  82. package/templates/apps/web/app/[locale]/courses/page.tsx +0 -49
  83. package/templates/apps/web/app/[locale]/events/[slug]/page.tsx +0 -57
  84. package/templates/apps/web/app/[locale]/events/__tests__/hub-children.test.tsx +0 -32
  85. package/templates/apps/web/app/[locale]/events/__tests__/page-renderer-migration.test.tsx +0 -164
  86. package/templates/apps/web/app/[locale]/events/__tests__/program-event.test.tsx +0 -120
  87. package/templates/apps/web/app/[locale]/events/page.tsx +0 -60
  88. package/templates/apps/web/app/[locale]/faq/page.tsx +0 -54
  89. package/templates/apps/web/app/[locale]/features/[slug]/page.tsx +0 -54
  90. package/templates/apps/web/app/[locale]/features/__tests__/hub-children.test.tsx +0 -32
  91. package/templates/apps/web/app/[locale]/features/page.tsx +0 -50
  92. package/templates/apps/web/app/[locale]/for/[slug]/page.tsx +0 -54
  93. package/templates/apps/web/app/[locale]/for/__tests__/hub-children.test.tsx +0 -32
  94. package/templates/apps/web/app/[locale]/for/__tests__/page-renderer-migration.test.tsx +0 -90
  95. package/templates/apps/web/app/[locale]/for/page.tsx +0 -51
  96. package/templates/apps/web/app/[locale]/glossary/page.tsx +0 -53
  97. package/templates/apps/web/app/[locale]/how-to/[slug]/page.tsx +0 -50
  98. package/templates/apps/web/app/[locale]/how-to/__tests__/page-renderer-migration.test.tsx +0 -78
  99. package/templates/apps/web/app/[locale]/how-to/page.tsx +0 -50
  100. package/templates/apps/web/app/[locale]/integrations/[slug]/page.tsx +0 -54
  101. package/templates/apps/web/app/[locale]/integrations/__tests__/hub-children.test.tsx +0 -38
  102. package/templates/apps/web/app/[locale]/integrations/__tests__/page-renderer-migration.test.tsx +0 -141
  103. package/templates/apps/web/app/[locale]/integrations/page.tsx +0 -49
  104. package/templates/apps/web/app/[locale]/launch-service/page.tsx +0 -44
  105. package/templates/apps/web/app/[locale]/newsletter/__tests__/page.test.tsx +0 -236
  106. package/templates/apps/web/app/[locale]/newsletter/page.tsx +0 -179
  107. package/templates/apps/web/app/[locale]/page.tsx +0 -147
  108. package/templates/apps/web/app/[locale]/partners/[slug]/page.tsx +0 -45
  109. package/templates/apps/web/app/[locale]/partners/__tests__/hub-children.test.tsx +0 -32
  110. package/templates/apps/web/app/[locale]/partners/__tests__/page-renderer-migration.test.tsx +0 -125
  111. package/templates/apps/web/app/[locale]/partners/page.tsx +0 -49
  112. package/templates/apps/web/app/[locale]/portfolio/[slug]/page.tsx +0 -46
  113. package/templates/apps/web/app/[locale]/portfolio/__tests__/hub-children.test.tsx +0 -38
  114. package/templates/apps/web/app/[locale]/portfolio/page.tsx +0 -50
  115. package/templates/apps/web/app/[locale]/pricing/[slug]/__tests__/page.test.tsx +0 -324
  116. package/templates/apps/web/app/[locale]/pricing/[slug]/page.tsx +0 -87
  117. package/templates/apps/web/app/[locale]/pricing/__tests__/__snapshots__/unconnected.test.tsx.snap +0 -7
  118. package/templates/apps/web/app/[locale]/pricing/__tests__/offerings.test.tsx +0 -605
  119. package/templates/apps/web/app/[locale]/pricing/__tests__/page.test.tsx +0 -219
  120. package/templates/apps/web/app/[locale]/pricing/__tests__/pricing-pages.test.tsx +0 -215
  121. package/templates/apps/web/app/[locale]/pricing/__tests__/unconnected.test.tsx +0 -282
  122. package/templates/apps/web/app/[locale]/pricing/page.tsx +0 -120
  123. package/templates/apps/web/app/[locale]/privacy/page.tsx +0 -40
  124. package/templates/apps/web/app/[locale]/products/[slug]/page.tsx +0 -50
  125. package/templates/apps/web/app/[locale]/products/__tests__/hub-children.test.tsx +0 -32
  126. package/templates/apps/web/app/[locale]/products/__tests__/page-renderer-migration.test.tsx +0 -141
  127. package/templates/apps/web/app/[locale]/products/page.tsx +0 -49
  128. package/templates/apps/web/app/[locale]/projects/[slug]/page.tsx +0 -37
  129. package/templates/apps/web/app/[locale]/projects/__tests__/hub-children.test.tsx +0 -32
  130. package/templates/apps/web/app/[locale]/projects/__tests__/page-renderer-migration.test.tsx +0 -157
  131. package/templates/apps/web/app/[locale]/projects/page.tsx +0 -49
  132. package/templates/apps/web/app/[locale]/purchase-policy/page.tsx +0 -49
  133. package/templates/apps/web/app/[locale]/resources/[slug]/page.tsx +0 -54
  134. package/templates/apps/web/app/[locale]/resources/__tests__/hub-children.test.tsx +0 -38
  135. package/templates/apps/web/app/[locale]/resources/__tests__/page-renderer-migration.test.tsx +0 -151
  136. package/templates/apps/web/app/[locale]/resources/page.tsx +0 -49
  137. package/templates/apps/web/app/[locale]/services/[slug]/[child]/page.tsx +0 -68
  138. package/templates/apps/web/app/[locale]/services/[slug]/page.tsx +0 -88
  139. package/templates/apps/web/app/[locale]/services/__tests__/hub-children.test.tsx +0 -158
  140. package/templates/apps/web/app/[locale]/services/__tests__/page-renderer-migration.test.tsx +0 -187
  141. package/templates/apps/web/app/[locale]/services/__tests__/service-detail-sections.test.tsx +0 -224
  142. package/templates/apps/web/app/[locale]/services/__tests__/service-families.test.tsx +0 -368
  143. package/templates/apps/web/app/[locale]/services/page.tsx +0 -71
  144. package/templates/apps/web/app/[locale]/software/[slug]/page.tsx +0 -49
  145. package/templates/apps/web/app/[locale]/software/__tests__/page-renderer-migration.test.tsx +0 -81
  146. package/templates/apps/web/app/[locale]/software/page.tsx +0 -49
  147. package/templates/apps/web/app/[locale]/team/[slug]/page.tsx +0 -45
  148. package/templates/apps/web/app/[locale]/team/__tests__/hub-children.test.tsx +0 -32
  149. package/templates/apps/web/app/[locale]/team/__tests__/team-routes.test.tsx +0 -161
  150. package/templates/apps/web/app/[locale]/team/page.tsx +0 -50
  151. package/templates/apps/web/app/[locale]/tech-articles/[slug]/page.tsx +0 -54
  152. package/templates/apps/web/app/[locale]/tech-articles/__tests__/page-renderer-migration.test.tsx +0 -82
  153. package/templates/apps/web/app/[locale]/tech-articles/page.tsx +0 -49
  154. package/templates/apps/web/app/[locale]/templates/[slug]/__tests__/page.test.tsx +0 -203
  155. package/templates/apps/web/app/[locale]/templates/[slug]/page.tsx +0 -81
  156. package/templates/apps/web/app/[locale]/templates/__tests__/page.test.tsx +0 -237
  157. package/templates/apps/web/app/[locale]/templates/page.tsx +0 -114
  158. package/templates/apps/web/app/[locale]/terms/page.tsx +0 -40
  159. package/templates/apps/web/app/[locale]/testimonials/__tests__/page-renderer-migration.test.tsx +0 -85
  160. package/templates/apps/web/app/[locale]/testimonials/page.tsx +0 -51
  161. package/templates/apps/web/app/[locale]/thanks/[slug]/__tests__/page.test.tsx +0 -136
  162. package/templates/apps/web/app/[locale]/thanks/[slug]/page.tsx +0 -39
  163. package/templates/apps/web/app/[locale]/unsubscribe/__tests__/unsubscribe-rsc-boundary.test.tsx +0 -226
  164. package/templates/apps/web/app/[locale]/unsubscribe/__tests__/unsubscribe.test.tsx +0 -217
  165. package/templates/apps/web/app/[locale]/unsubscribe/page.tsx +0 -106
  166. package/templates/apps/web/app/[locale]/unsubscribe/unsubscribe-button.tsx +0 -94
  167. package/templates/apps/web/app/[locale]/unsubscribe/unsubscribe.actions.ts +0 -55
  168. package/templates/apps/web/app/[locale]/vs/[slug]/__tests__/page.test.tsx +0 -100
  169. package/templates/apps/web/app/[locale]/vs/[slug]/page.tsx +0 -60
  170. package/templates/apps/web/app/[locale]/whitepapers/[slug]/page.tsx +0 -54
  171. package/templates/apps/web/app/[locale]/whitepapers/__tests__/page-renderer-migration.test.tsx +0 -82
  172. package/templates/apps/web/app/[locale]/whitepapers/page.tsx +0 -49
  173. package/templates/apps/web/app/[locale]/working-agreement/__tests__/page.test.tsx +0 -63
  174. package/templates/apps/web/app/[locale]/working-agreement/page.tsx +0 -41
  175. package/templates/apps/web/lib/__tests__/hub-children.fixtures.ts +0 -279
  176. package/templates/apps/web/lib/__tests__/route-family.test.ts +0 -57
  177. package/templates/apps/web/lib/__tests__/service-listing.test.ts +0 -111
  178. package/templates/apps/web/lib/__tests__/template-listing.test.ts +0 -158
  179. package/templates/apps/web/lib/hub-children.tsx +0 -351
  180. package/templates/apps/web/lib/meet-placeholder.ts +0 -5
  181. package/templates/apps/web/lib/posting-title.ts +0 -39
  182. package/templates/apps/web/lib/route-family.ts +0 -69
  183. package/templates/apps/web/lib/service-listing.ts +0 -89
  184. package/templates/apps/web/lib/template-listing.ts +0 -169
  185. package/templates/apps/web/src/lib/__tests__/lead-write.test.ts +0 -187
  186. package/templates/apps/web/src/lib/lead-write.ts +0 -90
  187. /package/templates/apps/web/{app/[locale]/contact → lib}/__tests__/site-contact-lines.test.ts +0 -0
@@ -1,403 +0,0 @@
1
- "use server";
2
-
3
- /**
4
- * captureLeadAction — Server Action paired with ailkCaptureLead MCP tool.
5
- *
6
- * Validates contact-form input with its own `contactFormSchema` (the form's
7
- * field set is the page's, not a wire contract — the `capture_lead` MCP tool
8
- * takes the route body since #7171), calls POST /v1/leads/submit via
9
- * @working-theory/api-client, and returns
10
- * the shared `LeadCaptureForm` block's `LeadCaptureFormState`.
11
- *
12
- * Canonical filename: contact.actions.ts (*.actions.ts per ADR 0013 / U0 spec
13
- * parity-test regex `ACTION_FILE_RE = /\.actions\.ts$/`).
14
- *
15
- * The honeypot is checked here, and so is a per-visitor limit (#8747): 30
16
- * calls per 10 minutes per visitor address, before the write. From a server
17
- * action the hub sees this site's egress address, so one visitor could
18
- * otherwise drain the hub's per-IP budget for every visitor of the site. With
19
- * no readable address the post is counted in one shared bucket at ten times
20
- * the limit, never skipped (#9967). The hub keeps its own per-IP limit on the
21
- * route.
22
- *
23
- * The write (#8747): every lead goes through `writeLead`, the site's lead
24
- * writer (`src/lib/lead-write.ts`), which marks it `payload.site =
25
- * AILK_LEAD_SITE` and `payload.page = "contact"` (spread last, so no form field
26
- * can override them) and sends no `siteId` — the API names the workspace
27
- * itself, from the spoke key on a hub or its own site alias otherwise.
28
- *
29
- * Outcome (#6730 D8): the API's 503 `lead_capture_not_configured` — this
30
- * deployment has nowhere to put a lead — maps to `{ kind: "unavailable" }`,
31
- * which the shared `LeadCaptureForm` renders as the site's contact email. So
32
- * does an API that cannot be reached (#8747): a thrown submit, a 502 or a 504
33
- * — the message reached nobody, so the visitor is shown the email rather than
34
- * a "try again". Every other non-2xx is the generic `error`; only a 2xx is
35
- * ever `success`.
36
- *
37
- * Extra fields (#6730 D8a): a `contact-form` section can declare fields
38
- * beyond name/email/message/phone (a `select`, say). Every other string
39
- * field that is not reserved and not a Next internal (`$ACTION_…`) is carried
40
- * into `payload` — at most `MAX_EXTRA_FIELDS` keys, each at most
41
- * `MAX_EXTRA_FIELD_LENGTH` characters, else a form-level error. A reserved
42
- * name can never override `name`/`email`/`source`.
43
- *
44
- * `source` (#4534, epic #4521's integration leg): an optional hidden
45
- * FormData field so `ConversationalIntakeWrapper` can tag leads it submits
46
- * as `"conversational-intake"` rather than the plain form's `"web"` —
47
- * reusing this SAME action (built imperatively from the conversation's
48
- * answers via `new FormData()`, not a real `<form>` submit) instead of a
49
- * second `*Action`-suffixed export, which would trip
50
- * `apps/mcp/__tests__/parity.test.ts`'s Server-Action↔MCP-tool 1:1 count for
51
- * a capability (lead capture) this repo already has an MCP twin for
52
- * (`ailkCaptureLead`). Absent (the real `<form>` never sets it) → the
53
- * built-in form's one standard source, `"contact-form"` (#8747; it was
54
- * `"web"`, and the sites disagreed).
55
- *
56
- * Source allow-list and terms (#8661 D8, #8730): `/contact` stamps a
57
- * submission it drew from the Contact form magnet with a hidden `source` of
58
- * `"flow:contact"`, so it lands as an Inbox submission of that magnet (it
59
- * reaches the Needs-you queue through the submissions read, not the message
60
- * list — `flow:<slug>` is never a message source). The field is
61
- * visitor-controlled, so only the contact sources (`CONTACT_LEAD_SOURCES`:
62
- * `"contact-form"`, `"conversational-intake"`, `"flow:contact"`) are kept;
63
- * anything else is sent as `"contact-form"`, a message source, so no
64
- * visitor-set value drops a real message out of Needs-you. The source picks
65
- * the stamp and never the gate (#8730 D2): EVERY post must carry
66
- * `consent=true` — the form's terms box, or the intake's own terms turn — or
67
- * it answers a `consent` field error, merged with the schema's, and nothing
68
- * is sent. An accepted post tells the API so (`consent: true`, which the API
69
- * requires of a contact lead and records on it); an API `400
70
- * consent_required` is shown as the same field error.
71
- *
72
- * The bot check (#9255 D4): after the honeypot, consent, schema and
73
- * per-visitor refusals and immediately before the write, `runBotCheck`
74
- * (lib/bot-check.ts) asks Vercel BotID for a verdict. A bot gets the generic
75
- * error and a check that cannot decide `unavailable`; neither is sent.
76
- *
77
- * Visitor forwarding (#7394): on a spoke whose API is a hub, the call
78
- * carries the visitor's IP and the spoke's `AILK_VISITOR_FORWARD_KEY`
79
- * (server-only, never `NEXT_PUBLIC_`) via `visitorForwardHeaders`, so the
80
- * hub counts this lead against the visitor's own rate-limit budget instead
81
- * of the spoke host's. No key, no client address, or headers that cannot be
82
- * read → the call is made with one argument, exactly as before; a failure to
83
- * read headers never blocks a submission.
84
- *
85
- * The account link (#8757 D8): after a successful write, the API's 201 may
86
- * carry `next` (D6). `{ completion: "invite" }` — the magnet invites the
87
- * submitter to their account — answers `{ kind: "success", next: { href,
88
- * label } }`, the href from `accountInviteHref` (the request's locale, the
89
- * submitter's email, this site's sign-in provider) and the label the
90
- * `contact.trackInAccount` message. Anything else — no `next`, a site with no
91
- * sign-in, an email the hint schema refuses, a locale or message that cannot
92
- * be read — answers exactly `{ kind: "success" }`: a written lead is never
93
- * reported as a failure.
94
- *
95
- * Continue to checkout (#8757 D11, #9171): `{ completion: "checkout",
96
- * checkoutOffering }` — the magnet sends the submitter on to the offering's
97
- * checkout — answers `{ kind: "success", redirect:
98
- * "/api/offerings/checkout/<lookupKey>" }`, the Offerings buy route every
99
- * `/pricing` card links; the legacy `/api/checkout/<offering>` is retired
100
- * (#9142). The form hard-navigates there (`LeadCaptureForm`'s `redirect`),
101
- * because the route starts a session only for a top-level navigation and a
102
- * Server Action `redirect()` is fetched as RSC, which it sends to `/pricing`.
103
- * A stored value `parseLookupKey` accepts (`growth-ops-growth_monthly`,
104
- * #10823) is the key itself. Otherwise it is a `{plan}`, and the key is the
105
- * one that plan's `/pricing` card links first: the first of `<plan>_monthly`,
106
- * `<plan>_yearly` and `<plan>_once` the shown set shows. Either way the key
107
- * must be in the shown set, read through
108
- * `offerings.shown()` within 5 seconds and shared per instance for 15, as
109
- * the buy route reads it. It runs only after the write resolved `ok`. The
110
- * value is re-checked with `flowCheckoutOfferingSchema` first; one that
111
- * fails, a key or plan the shown set does not show, or a read that fails or times
112
- * out answers `{ kind: "success" }`, as `lead` does.
113
- * A failed write never answers the buy route.
114
- */
115
-
116
- import { offerings, visitorForwardHeaders } from "@working-theory/api-client";
117
- import { LEAD_CAPTURE_HONEYPOT, type LeadCaptureFormState } from "@working-theory/ui";
118
- import {
119
- CONTACT_LEAD_SOURCES,
120
- flowCheckoutOfferingSchema,
121
- leadSubmitNextSchema,
122
- LOOKUP_KEY_PERIODS,
123
- parseLookupKey,
124
- } from "@working-theory/validation";
125
- import { cookies, headers } from "next/headers";
126
- import { getLocale, getTranslations } from "next-intl/server";
127
- import { z } from "zod";
128
-
129
- import { accountInviteHref } from "../../../lib/account-invite";
130
- import { runBotCheck } from "../../../lib/bot-check";
131
- import { consentVisitorHeader, readConsentVisitor } from "../../../lib/consent-visitor";
132
- import { isShownLookupKey, offeringsCheckoutHref } from "../../../lib/offerings-pricing";
133
- import { writeLead } from "../../../src/lib/lead-write";
134
- import { takeVisitorLimit } from "../../../src/lib/rate-limit";
135
-
136
- const GENERIC_ERROR = "Something went wrong, please try again.";
137
- const RATE_LIMITED = "Too many messages from here. Please try again in a few minutes.";
138
-
139
- /** The per-visitor budget (#8747): the consumer sites' own values. */
140
- const RATE_LIMIT_MAX_CALLS = 30;
141
- const RATE_LIMIT_WINDOW_MS = 10 * 60 * 1000;
142
- /** Outside a request scope there is no address: the shared bucket. */
143
- const NO_HEADERS = { get: () => null };
144
-
145
- /** Field names the action reads itself — never copied into `payload`. */
146
- const RESERVED_FIELDS = new Set([
147
- "name",
148
- "email",
149
- "message",
150
- "phone",
151
- "consent",
152
- LEAD_CAPTURE_HONEYPOT,
153
- "source",
154
- ]);
155
- const MAX_EXTRA_FIELDS = 20;
156
-
157
- /** The built-in form's source (#8747), and the one an unlisted value becomes. */
158
- const DEFAULT_SOURCE = "contact-form";
159
- /** The server's terms-error text. The form block shows its own copy for this key. */
160
- const CONSENT_REQUIRED = "Please accept the terms to send your message.";
161
- const MAX_EXTRA_FIELD_LENGTH = 2000;
162
-
163
- /** The form's four fields and bounds; each field error is zod's own message. */
164
- const contactFormSchema = z.object({
165
- name: z.string().min(1).max(200),
166
- email: z.string().email(),
167
- message: z.string().max(2000).optional(),
168
- phone: z.string().max(50).optional(),
169
- });
170
-
171
- /**
172
- * The declared extra fields, or `null` when they exceed the bounds. Non-string
173
- * entries (files) and Next's own `$ACTION_…` keys are skipped.
174
- */
175
- function extraFields(formData: FormData): Record<string, string> | null {
176
- const extra: Record<string, string> = {};
177
- let count = 0;
178
- for (const [key, value] of formData.entries()) {
179
- if (RESERVED_FIELDS.has(key) || key.startsWith("$ACTION")) continue;
180
- if (typeof value !== "string") continue;
181
- if (value.length > MAX_EXTRA_FIELD_LENGTH) return null;
182
- if (!Object.hasOwn(extra, key)) count += 1;
183
- if (count > MAX_EXTRA_FIELDS) return null;
184
- extra[key] = value;
185
- }
186
- return extra;
187
- }
188
-
189
- /** A 400 whose body names `consent_required` (#8730). */
190
- function isConsentRequired(res: { status: number; body: unknown }): boolean {
191
- return (
192
- res.status === 400 &&
193
- typeof res.body === "object" &&
194
- res.body !== null &&
195
- (res.body as { error?: unknown }).error === "consent_required"
196
- );
197
- }
198
-
199
- /** A 503 whose body names `lead_capture_not_configured`. */
200
- function isNotConfigured(res: { status: number; body: unknown }): boolean {
201
- return (
202
- res.status === 503 &&
203
- typeof res.body === "object" &&
204
- res.body !== null &&
205
- (res.body as { error?: unknown }).error === "lead_capture_not_configured"
206
- );
207
- }
208
-
209
- /**
210
- * The API could not be reached (#8747): `writeLead`'s `status: 0` (a thrown
211
- * submit) or a gateway failure. A 503 that is not `lead_capture_not_configured`
212
- * stays the generic error — the API answered.
213
- */
214
- function isApiUnreachable(res: { status: number }): boolean {
215
- return res.status === 0 || res.status === 502 || res.status === 504;
216
- }
217
-
218
- /** The buy route's own bounds on its shown-set read. */
219
- const SHOWN_READ_TIMEOUT_MS = 5_000;
220
- const SHOWN_READ_TTL_MS = 15_000;
221
-
222
- /** The shown set `/pricing` renders from, or `null`: any other answer, a throw, or none in time. */
223
- async function readShownSet(): Promise<unknown> {
224
- let timer: ReturnType<typeof setTimeout> | undefined;
225
- const late = new Promise<null>((resolve) => {
226
- timer = setTimeout(() => resolve(null), SHOWN_READ_TIMEOUT_MS);
227
- });
228
- try {
229
- const res = await Promise.race([offerings.shown(), late]);
230
- return res?.ok && res.body.status === "shown" ? res.body.shownSet : null;
231
- } catch {
232
- return null;
233
- } finally {
234
- clearTimeout(timer);
235
- }
236
- }
237
-
238
- let shownRead: { at: number; read: Promise<unknown> } | undefined;
239
-
240
- /**
241
- * One read per instance per SHOWN_READ_TTL_MS: `offerings.shown()` forwards
242
- * no visitor header, so a read per submit would count every visitor against
243
- * this server's one per-IP limit at the API. Failed reads are shared too.
244
- */
245
- function sharedShownSet(): Promise<unknown> {
246
- const now = Date.now();
247
- if (shownRead && now - shownRead.at < SHOWN_READ_TTL_MS) return shownRead.read;
248
- shownRead = { at: now, read: readShownSet() };
249
- return shownRead.read;
250
- }
251
-
252
- /**
253
- * #8757 D11, #9171 — the Offerings buy route for the offering a `checkout`
254
- * answer names: the stored lookup key when it is one and shown (#10823), else
255
- * the plan's first shown key in `/pricing`'s card order; else `null`.
256
- */
257
- async function checkoutHrefOf(next: unknown): Promise<string | null> {
258
- const parsed = leadSubmitNextSchema.safeParse(next);
259
- if (!parsed.success || parsed.data.completion !== "checkout") return null;
260
- const plan = flowCheckoutOfferingSchema.safeParse(parsed.data.checkoutOffering);
261
- if (!plan.success) return null;
262
- const shownSet = await sharedShownSet();
263
- const candidates =
264
- parseLookupKey(plan.data) === null
265
- ? LOOKUP_KEY_PERIODS.map((period) => `${plan.data}_${period}`)
266
- : [plan.data];
267
- const key = candidates.find((k) => isShownLookupKey(shownSet, k));
268
- return key === undefined ? null : offeringsCheckoutHref(key);
269
- }
270
-
271
- /**
272
- * #8757 D8 — the success state for a written lead: with the account link when
273
- * the API answered `next: { completion: "invite" }` and this site can build
274
- * one, else exactly `{ kind: "success" }`. Never throws.
275
- */
276
- async function successState(
277
- next: unknown,
278
- email: string,
279
- ): Promise<Extract<LeadCaptureFormState, { kind: "success" }>> {
280
- const parsed = leadSubmitNextSchema.safeParse(next);
281
- if (!parsed.success || parsed.data.completion !== "invite") return { kind: "success" };
282
- let locale = "en";
283
- try {
284
- locale = await getLocale();
285
- } catch {
286
- // No request-scoped locale — the default locale's link.
287
- }
288
- const href = accountInviteHref(locale, email);
289
- if (href === null) return { kind: "success" };
290
- try {
291
- const t = await getTranslations("contact");
292
- return { kind: "success", next: { href, label: t("trackInAccount") } };
293
- } catch {
294
- return { kind: "success" };
295
- }
296
- }
297
-
298
- export async function captureLeadAction(
299
- _prevState: LeadCaptureFormState,
300
- formData: FormData,
301
- ): Promise<LeadCaptureFormState> {
302
- // Honeypot: people never see `LEAD_CAPTURE_HONEYPOT`; a bot that fills it
303
- // gets the same answer a person would, and nothing is sent. The API does
304
- // not check it.
305
- const honeypot = formData.get(LEAD_CAPTURE_HONEYPOT);
306
- if (typeof honeypot === "string" && honeypot !== "") return { kind: "success" };
307
-
308
- const raw = {
309
- name: formData.get("name"),
310
- email: formData.get("email"),
311
- message: formData.get("message") ?? undefined,
312
- phone: formData.get("phone") ?? undefined,
313
- };
314
-
315
- const posted = formData.get("source");
316
- const source =
317
- typeof posted === "string" && CONTACT_LEAD_SOURCES.has(posted) ? posted : DEFAULT_SOURCE;
318
-
319
- // #8730 D2 — every source, the intake's included: it asks for the terms
320
- // itself and posts consent=true like the form does.
321
- const fieldErrors: Record<string, string> = {};
322
- if (formData.get("consent") !== "true") {
323
- fieldErrors.consent = CONSENT_REQUIRED;
324
- }
325
-
326
- const parsed = contactFormSchema.safeParse(raw);
327
- if (!parsed.success) {
328
- for (const issue of parsed.error.issues) {
329
- const field = String(issue.path[0] ?? "form");
330
- if (!fieldErrors[field]) {
331
- fieldErrors[field] = issue.message;
332
- }
333
- }
334
- }
335
- if (!parsed.success || Object.keys(fieldErrors).length > 0) {
336
- return { kind: "error", fieldErrors };
337
- }
338
-
339
- const extra = extraFields(formData);
340
- if (extra === null) return { kind: "error", message: GENERIC_ERROR };
341
-
342
- const { name, email, message, phone } = parsed.data;
343
- const payload = {
344
- ...extra,
345
- name,
346
- ...(message !== undefined && { message }),
347
- ...(phone !== undefined && { phone }),
348
- };
349
-
350
- let requestHeaders: Awaited<ReturnType<typeof headers>> | undefined;
351
- try {
352
- requestHeaders = await headers();
353
- } catch {
354
- // No request scope to read — no forwarding, and the visitor is "unknown".
355
- }
356
-
357
- // No readable address: the shared, wider bucket, never a skip (#9967 D1).
358
- // The hub's own per-IP limit still applies.
359
- if (
360
- !takeVisitorLimit("contact", requestHeaders ?? NO_HEADERS, RATE_LIMIT_MAX_CALLS, RATE_LIMIT_WINDOW_MS)
361
- .allowed
362
- ) {
363
- return { kind: "error", message: RATE_LIMITED };
364
- }
365
-
366
- let init: { headers: Record<string, string> } | undefined;
367
- if (requestHeaders) {
368
- const forwarded = visitorForwardHeaders(requestHeaders, process.env.AILK_VISITOR_FORWARD_KEY);
369
- if (forwarded) init = { headers: forwarded };
370
- }
371
-
372
- // #8438 D12 — the banner's visitor id, so a later form submit links its
373
- // cookie choices to the client record this submit creates or resolves
374
- // (D11). Absent or invalid → no header, exactly as before this unit.
375
- try {
376
- const visitorId = readConsentVisitor(await cookies());
377
- if (visitorId) init = { headers: { ...(init?.headers ?? {}), ...consentVisitorHeader(visitorId) } };
378
- } catch {
379
- // No request scope to read — submit with no visitor-link header.
380
- }
381
-
382
- // #9255 D4 — the spoke edge's bot check, after every local refusal and
383
- // immediately before the write.
384
- const botCheck = await runBotCheck({ source: "bot-check", target: "captureLeadAction", scope: "write" });
385
- if (botCheck === "bot") return { kind: "error", message: GENERIC_ERROR };
386
- if (botCheck === "unavailable") return { kind: "unavailable" };
387
-
388
- // writeLead never throws: a thrown submit is logged there by error name
389
- // only, with no field values (#4042), and resolves as `status: 0`.
390
- const res = await writeLead({ source, email, page: "contact", consent: true, payload }, init);
391
- if (!res.ok) {
392
- if (isConsentRequired(res)) {
393
- return { kind: "error", fieldErrors: { consent: CONSENT_REQUIRED } };
394
- }
395
- if (isNotConfigured(res) || isApiUnreachable(res)) return { kind: "unavailable" };
396
- return { kind: "error", message: GENERIC_ERROR };
397
- }
398
- // #9171 — the form navigates: the buy route refuses a Server Action redirect.
399
- const checkoutHref = await checkoutHrefOf(res.body.next);
400
- if (checkoutHref !== null) return { kind: "success", name, redirect: checkoutHref };
401
- // #8847 D7 — the parsed name, which the magnet's ending fills `{name}` with.
402
- return { ...(await successState(res.body.next, email)), name };
403
- }
@@ -1,49 +0,0 @@
1
- "use server";
2
-
3
- /**
4
- * intake-analytics.ts — per-turn abandonment analytics for
5
- * ConversationalIntakeWrapper (#4534, epic #4521's integration leg).
6
- *
7
- * `recordIntakeTurn` fires `track()` (`@working-theory/analytics/server`,
8
- * the same fire-and-forget helper `packages/api/src/routes/leads/index.ts`
9
- * already uses for "lead-captured") once per conversation turn, tagged with
10
- * the turn index and the engine status as of that turn. A session's last
11
- * "intake-turn-abandonment" event with `status: "in_progress"` marks the
12
- * turn it stopped progressing past — the standard step-funnel shape for
13
- * computing per-turn abandonment, without needing a bespoke "abandoned"
14
- * signal (tab-close is indistinguishable from "still reading" client-side;
15
- * the downstream analytics consumer infers abandonment from the absence of
16
- * a later "completed"/"skipped" event for the session). PII-free by
17
- * construction: only the turn index, status, and locale ever leave the
18
- * client — never the answer text (honesty-by-design applies to what we
19
- * track, too).
20
- *
21
- * Deliberately NOT named `recordIntakeTurnAction` / kept OUT of a
22
- * `*.actions.ts` file: this is telemetry, not a business-capability Server
23
- * Action, and `apps/mcp/__tests__/parity.test.ts` counts every
24
- * `*.actions.ts` export matching `^[a-z][a-zA-Z0-9]*Action$` as one half of
25
- * a Server-Action↔MCP-tool pair it expects to find a twin for — there is no
26
- * (and should be no) `ailkRecordIntakeTurn` MCP tool, since this reports a
27
- * pageview-shaped web-analytics event, not a capability an agent invokes.
28
- */
29
-
30
- import { track } from "@working-theory/analytics/server";
31
-
32
- /**
33
- * The conversation's status as of the turn being reported. `"in_progress"`
34
- * fires once per turn while the conversation is ongoing (including turn 1,
35
- * on mount); `"completed"`/`"skipped"` fire once, at the terminal turn.
36
- */
37
- export type IntakeTurnStatus = "in_progress" | "completed" | "skipped";
38
-
39
- export async function recordIntakeTurn(input: {
40
- turnIndex: number;
41
- status: IntakeTurnStatus;
42
- locale?: string;
43
- }): Promise<void> {
44
- await track("intake-turn-abandonment", {
45
- turnIndex: input.turnIndex,
46
- status: input.status,
47
- locale: input.locale,
48
- });
49
- }
@@ -1,92 +0,0 @@
1
- /**
2
- * buildContactIntakeScript — the authored `IntakeScript` for the contact
3
- * page's `ConversationalIntakeWrapper` (#4534, epic #4521's integration
4
- * leg).
5
- *
6
- * Three open questions, deliberately keyed `name` / `message` / `email` —
7
- * the SAME field names `contactFormSchema` (`./contact.actions.ts`)
8
- * expects. `ConversationalIntakeWrapper` reads `IntakeEngineState.answers`
9
- * by these exact ids to build the `FormData` it hands to the existing
10
- * `captureLeadAction` (`./contact.actions.ts`), so the script's question
11
- * ids ARE the submission adapter's field-mapping contract; renaming a
12
- * question id here requires updating that mapping too.
13
- *
14
- * A fourth, closed question (`consent`, #8730 D2) asks for the terms before
15
- * anything is sent. The engine records a tapped pill as its LABEL, so the
16
- * wrapper treats the answer as acceptance only when it equals
17
- * `copy.consentAccept` exactly; any other answer — the decline pill or typed
18
- * text — sends nothing and shows the form, whose own terms box decides.
19
- *
20
- * Copy is translated (next-intl "contact" namespace, "intake.*" keys —
21
- * see apps/web/messages/en.json) rather than hardcoded, consistent with
22
- * ContactForm's own `useTranslations("contact")` usage. The wrapper resolves
23
- * the strings via its own scoped translator and passes them in as plain
24
- * data (`ContactIntakeCopy`) rather than passing the translator function
25
- * itself — next-intl's `t` is typed to a specific literal key union per
26
- * call site, which doesn't assign cleanly to a generic `(key: string) =>
27
- * string` parameter; plain strings sidestep that entirely and keep this
28
- * file decoupled from next-intl's types.
29
- */
30
- import { intakeScriptSchema } from "@working-theory/validation";
31
- import type { IntakeScript } from "@working-theory/validation";
32
-
33
- export interface ContactIntakeCopy {
34
- nameAck: string;
35
- namePrompt: string;
36
- messageAck: string;
37
- messagePrompt: string;
38
- emailAck: string;
39
- emailPrompt: string;
40
- consentAck: string;
41
- consentPrompt: string;
42
- /** The accept pill's label — the one answer that is terms acceptance. */
43
- consentAccept: string;
44
- consentDecline: string;
45
- skipMessage: string;
46
- completionMessage: string;
47
- }
48
-
49
- /** The accept option's value — its pill's test id is `…-pill-accept`. */
50
- const INTAKE_CONSENT_ACCEPT = "accept";
51
-
52
- export function buildContactIntakeScript(
53
- copy: ContactIntakeCopy,
54
- ): IntakeScript {
55
- return intakeScriptSchema.parse({
56
- slug: "contact-intake",
57
- questions: [
58
- {
59
- id: "name",
60
- kind: "open",
61
- acknowledgement: copy.nameAck,
62
- prompt: copy.namePrompt,
63
- },
64
- {
65
- id: "message",
66
- kind: "open",
67
- acknowledgement: copy.messageAck,
68
- prompt: copy.messagePrompt,
69
- },
70
- {
71
- id: "email",
72
- kind: "open",
73
- acknowledgement: copy.emailAck,
74
- prompt: copy.emailPrompt,
75
- },
76
- // #8730 D2 — the intake asks for the terms itself. Only the accept
77
- // pill's label is acceptance; the wrapper reads `consent` by that id.
78
- {
79
- id: "consent",
80
- kind: "closed",
81
- acknowledgement: copy.consentAck,
82
- prompt: copy.consentPrompt,
83
- options: [
84
- { value: INTAKE_CONSENT_ACCEPT, label: copy.consentAccept },
85
- { value: "decline", label: copy.consentDecline },
86
- ],
87
- },
88
- ],
89
- skip: { message: copy.skipMessage },
90
- completion: { message: copy.completionMessage },
91
- });
92
- }