ADR-0031: Forms in the chat and leads
- Status: Accepted on 2026-11-27 (task z1), as built by tasks p3, c1, c2, c3 and c4. Decisions 1, 3 and 4 are amended below ("As built in 0.7").
- Date: 2026-11-26
- Related: ADR-0030 (a handoff collects contact details with a form, and falls back to one when nobody is available). Built by tasks 0.7-p3 (fields and validation), c1 (protocol), c2 (chat state), c3 (bot and handler) and c4 (UI).
Context
Sales and support bots ask for structured data: a lead's name and email, a callback number, a topic, consent to be contacted. Asking in free text works badly: the model has to parse "my email is anna at example dot com", the value passes through the model and the conversation whether or not it needs to, and nothing validates it. Hosted products (Intercom Fin, Chatbase) show a small form in the chat instead and send its values to a CRM or a webhook.
What askdare has to build on:
- Reply options (ADR-0015) and tool cards (ADR-0017) set the path for UI from the server: the server's code builds a small, typed, size-capped payload, sends it as its own protocol event with its own limits, and the client renders it with known components, as plain text.
- Tool cards render only with the site's
toolRenderers. The script-tag widget draws nothing for them (ADR-0017), so a form built ontool.uialone would not reach most installs. - ADR-0004: the client chooses nothing the server must decide, and every endpoint has same-origin, token and rate checks.
- ADR-0021: Zod is always there on the server; the browser bundles can't take it (ADR-0028's 34 KB budget for the React entry, 14 KB for the iframe).
- ADR-0019: consent and the privacy link are the operator's choice; askdare provides the means.
- ADR-0022: what the server decided is recorded in the conversation as fixed developer notes without content.
- ADR-0014: the chat can switch language in place, from the bot's public configuration.
Decision
1. Forms are defined in server code only, from a closed set of fields
const contact = defineForm({
name: 'contact', // [a-z0-9_]{1,32}, unique in the bot
description: 'Contact details for a sales follow-up. Show it when the visitor wants an offer or a call.',
title: { en: 'Get an offer', pl: 'Zapytaj o ofertę' },
fields: [
{ name: 'name', type: 'text', label: { en: 'Name', pl: 'Imię' }, required: true },
{ name: 'email', type: 'email', label: 'Email', required: true },
{ name: 'phone', type: 'phone', label: 'Phone' },
{ name: 'topic', type: 'select', label: 'Topic', options: [{ value: 'pricing', label: 'Pricing' }, { value: 'demo', label: 'A demo' }], toModel: true },
{ name: 'message', type: 'textarea', label: 'Message' },
{ name: 'consent', type: 'consent', label: 'I agree to be contacted about my request.', required: true },
],
submit: 'Send',
done: "Thanks! We'll be in touch.",
});
defineBot({ forms: [contact], /* … */ });- Field types are a closed set:
text(at most 200 characters),email(at most 254),phone(digits, spaces,+,-,(,), 6 to 20 characters; no country rules in 0.7),select(2 to 10 options, values from the definition),textarea(at most 2,000) andconsent(a checkbox;requiredmeans it must be ticked). Each hasname,label, optionalrequired,placeholder(text,email,phone,textarea) andtoModel(point 5). Atextortextareamay set a lowermaxLength. - Limits, checked by
defineFormat startup: at most 8 fields and 10 forms per bot; labels and titles at most 120 characters,doneat most 300; names unique. An invalid form fails startup with the reason, like an invalid bot. - Texts per language: every label, title,
submitanddoneis a string or a map by language tag, as the bot'stextsare. The server resolves them for the visitor's language, falling back to English, then to the first one given. descriptionis for the model only and never reaches the client. A form withmodel: falseis not offered to the model at all; it serves a handoff (ADR-0030) only.- No custom field types, no validation functions, no conditional fields in 0.7. The closed set is what the server can validate and the widget can draw without the site's code; the rest is an
embedPathpage or a site's own form.
2. The model picks a form by name and never invents fields
- A reserved tool
show_formwith the strict schema{ form }, whereformis an enum of the bot's forms offered to the model. Its description lists each form's name anddescription.defineBotrejects a bot tool with that name. The tool is offered throughallowed_tools, never on the turn's last step (ADR-0015), and once per turn at most. - No prefilled values in 0.7. The model can't pass values it heard in the conversation. A value copied by the model can be wrong, and one placed by a prompt injection (a different email address, a link) would look to the visitor like the site's own suggestion. The visitor types every value.
- The server answers the call itself with a fixed output, as it does
suggest_replies:{"status":"shown"}and a sentence telling the model the visitor sees the form, that it shouldn't ask for the same fields in text, and that it will be told when the form is sent. It runs no bot code.
3. How it is shown: its own form event and a built-in form, not tool.ui
- The public description travels in the bot's public configuration.
GET /config/<bot>and the chat document getforms: for each form its name, title, fields (name, type, label, required, placeholder, options with value and label, max length),submitanddone, in the visitor's language. Neverdescription,toModelormodel. A language switch (ADR-0014) refetches the configuration, so an open form changes language with the rest of the chat. - A new event
form { messageId, callId, name, handle, purpose }tells the chat to show a form under that answer.namepicks the description from the configuration;handleis the submission handle (point 4);purposeislead,handofformessage(a handoff form while nobody is available, ADR-0030), which picks the texts around it. It comes once the step's calls are done, in call order, liketool.ui. - The chat renders it with built-in components: a headless primitive
Formwith the partsform,form-field,form-errorandform-done, and the default look in the skin. It works in the script-tag widget, inline, and in anembedPathpage. Labels and options render as plain text; aconsentfield links the bot'sprivacyUrl(ADR-0019) through theLinkprimitive when one is set. Every field has a visible label, errors are tied to their field witharia-describedby, and the first invalid field gets focus (WCAG 2.2 AA, ADR-0019). - A form stays open under its answer until it is sent or its handle expires, even after the visitor writes more: a visitor often asks a question before filling it in. Once sent, it is replaced by
done. - Custom look: the parts and theme variables; a headless chat builds its own form from
useChat(), which exposes the forms of each message andactions.submitForm(handle, values). Forms don't go throughtoolRenderers. - History: a message gets an optional
forms: [{ callId, name, handle?, state }]withstateopen,sentorexpired, rebuilt from the storedshow_formcalls and the developer notes of point 5. A reload shows an open form open again, with a fresh handle.
4. How it is submitted: a new endpoint
POST /forms { botId, resumeToken, form, values }with the access token, whereformis the handle andvaluesa JSON object of strings and booleans.- The handle is an encrypted token like the feedback handle (ADR-0012): bound to the bot and to the visitor or signed-in user, holding the conversation, the
callId, the form's name, its purpose and, for a handoff, the handoff's reason and trigger. It is valid for 24 hours. The client never names the form or the conversation itself. - Checks, in order: same origin and the access token, as every endpoint; the handle belongs to this visitor and bot, and the resume token to the same conversation; the per-visitor limits (5 submissions a minute, 20 a day, and the same per IP); the form hasn't been sent already (the conversation has no submitted note for this
callId; the note is appended with the version check of the store, so two concurrent submissions can't both pass); then validation. - Validation with Zod on the server, built from the form's definition: unknown keys are refused, strings are trimmed and normalized (NFC), control and invisible characters other than line breaks in a
textareaare refused, emails lower-cased in the domain part,selectvalues must be one of the options, a requiredconsentmust betrue. The widget checks the same rules by hand first, for quick errors, without Zod. - Answers:
200 { state: 'sent' }, or for a handoff form{ state: 'sent', handoff }with ADR-0030's final state;400withinvalid_inputandfields: { <name>: <error key> }(error.form.required,error.form.email,error.form.phone,error.form.tooLong,error.form.option), never echoing a value;409when the form was already sent;404for a handle of someone else, another bot or a forged one;429over a limit.
- A submission is not a model turn. No model call, no tokens, no stream: the chat shows
done(or the handoff's state). What the model learns comes with the next turn (point 5). A form that triggers an answer from the bot can come later as an option, if asked for.
5. What the model sees after a submission: "sent" by default, chosen fields on opt-in
- Always: a fixed developer note,
[askdare:form] <name> sent, with a fixed sentence saying the visitor sent the form and that the model doesn't see its values. No content, as in ADR-0022. It tells the model not to ask again, and history uses it for the form's state. - Values only for fields marked
toModel: true. Their values follow the note as a user message,[askdare:form-values]and onelabel: valueline per field, so text the visitor typed never carries developer authority. History shows it as part of the form, not as a visitor message, and only right after its note; a visitor who types the marker gets a plain message. The default isfalsefor every field. - The recommendation for the docs: mark fields the bot needs to go on helping (a topic, a product), not contact details. The model doesn't need an email address to say "thanks, we'll write to you".
6. What onLead gets
createChatHandler({ onLead }), onLead: (report: ChatLeadReport) => void | Promise<void>, called after the response like onFeedback (waitUntil), so a slow CRM doesn't delay the visitor's "done". A throw goes to onError as internal_error with the error's class name only, as for onTranscript, since a CRM's error message can quote the values. The report:
id: random, for the receiver to drop duplicates;bot,form(its name),locale,createdAt;values: the validated, normalized values, every field of the form,toModelor not;visitor: the pseudonym;identity: the signed-in user, if any;conversationId: raw, never sent to the client;source:{ kind: 'model' }, or{ kind: 'handoff', reason, trigger, delivered: false, why: 'unavailable' | 'failed' }for a handoff saved as a message (ADR-0030).
A bot whose forms are offered to the model needs onLead: without it, the handler refuses to start and askdare doctor says why. Values are never logged, by askdare or its transports, and the report is listed in docs/privacy.md with onTranscript as a hook that carries personal data.
7. Forms and handoff (agreed with ADR-0030)
- A handoff's
formis a form of the bot, usually withmodel: false. Its values go toonHandoffascontactwhen the handoff is delivered. - When nobody is available, the same form is shown with
purpose: 'message', and its values go toonLeadwithsource.kind: 'handoff'andwhy: 'unavailable'. When the delivery fails after the form was sent, they go toonLeadwithwhy: 'failed'. The visitor sees ADR-0030'ssavedeither way. - Without
onLead, a message left while nobody was available goes toonHandoffwithavailable: false; a failed delivery ends asfailed. - A handoff form doesn't count against the lead limits twice: one submission, one count.
8. Consent, and what askdare keeps
- Consent is a field,
type: 'consent', with the site's own wording. When the bot'swidget.disclosurehas aprivacyUrl, the field links it. askdare requires no consent field: whether one is needed, and its wording, is the operator's decision, which the compliance guide (docs/compliance.md) helps with. - askdare stores no values. They pass from the request to
onLeadoronHandoffand are gone. In the conversation there is only the note, plus thetoModelvalues the operator chose, which then live wherever the conversation does (OpenAI or the app's own store) and are deleted with it. - The turn log (ADR-0027) holds the
formevent (name and handle), never values, since values don't travel in a turn.
As built in 0.7
Amended on 2026-11-27 (task z1), where the code differs from the decision above:
- Decision 1, names and texts: field names are
[a-z][a-z0-9_]{0,31}(a letter first), form names[a-z0-9_]{1,32}.submitanddoneare optional: a form without them uses the chat's ownform.submit("Send") andform.donetexts. The chat has three more texts of its own:form.optional,form.chooseandform.expired. - Decision 3, the language of a shown form: a shown form keeps the visitor's language, in the
show_formorhandoffcall's output, or in the note of a form the button showed. A handle issued again after a reload reports the same locale toonLeadandonHandoff, andtoModellabels resolve in it. - Decision 3, the parts:
form(withdata-purpose),form-title,form-field(withdata-type),form-optional,form-error,form-submitandform-done(withdata-state). In the chat state a form is alsosubmittingwhile it is sent. A focused field scrolls itself into view, so a tall field isn't left under the composer. - Decision 3, history:
forms[]items also carrypurpose. An open form gets a fresh handle on every history read, so its 24 hours aren't shown;expiredmeans the bot no longer defines the form. - Decision 4, the answers: besides the five error keys there are
error.form.consent(a required box not ticked) anderror.form.invalid(a value of the wrong type, or with control or invisible characters). An already-sent form answers409with the codeinvalid_inputand the keyerror.form.sent, soERROR_CODESdoesn't grow. A400putsfieldsundererror. - Decision 5: a
show_formcall after the answer ends the turn, assuggest_repliesdoes. The chat emits no browser event for a sent form (see ADR-0030, "As built").
Consequences
- New public API (ADR-0006, listed by task z1):
defineFormand its field types;formsindefineBot;onLeadandChatLeadReport; the reserved tool nameshow_form; theformevent, theformsfield of the public configuration and of history,POST /formsand its error keys; theFormprimitive and its parts,actions.submitForm; theform.*anderror.form.*texts. - Forms work in every install, the script-tag widget included, without the site writing components. The price is bytes: the
Formprimitive and its skin go into the React entry and the iframe app, which is why task b1 makes room in the React entry first. Task c4 measures them. - Personal data reaches the app, not the model, by default. A bot that needs a value to go on helping marks that field, and the operator's privacy notice covers it.
- A conversation holds a note per sent form, a few tokens in later turns.
- The
show_formtool costs input tokens on every step of a bot with forms offered to the model, inside the cached prefix, assuggest_repliesdoes. - The closed set of fields will be asked to grow (dates, numbers, file uploads, several pages). Each addition is a minor release with its validation rule, not an escape hatch.
Settled with the maintainer (2026-11-26)
The questions left open in the first draft, all settled as recommended:
- Values to the model: per field, with
toModel, off by default. - A submission runs no model turn in 0.7.
- No prefilled values from the model in 0.7.
onLeadis required when a bot offers a form to the model: the handler refuses to start without it.
Alternatives considered
- A form as a
tool.uicard, the developer's own tool returning fields throughui. It reaches only sites withtoolRenderers(not the script-tag widget), puts validation in every site's code and lets the tool's output decide fields at runtime, so the server couldn't validate a submission against a fixed definition. - The model defines the fields (a
show_formwith a list of fields as arguments). Flexible, and exactly what a prompt injection would use to ask for a password or a card number in the site's own UI. - Submitting as a visitor message (the values written into
POST /messages, as a reply option is). Every value would reach the model and the conversation, and validation would run in the middle of a turn. - The form's description in the
formevent instead of the public configuration. Self-contained, but it repeats the fields in every event and history page, and an open form couldn't follow a language switch. - Values to the model by default. Simpler for bots that want to use them, but sends contact details to OpenAI and keeps them in the conversation for every bot, which most don't need.
- Values in the developer note. One item instead of two, but text the visitor typed would carry developer authority: a prompt injection through a form field.
- A submission that runs a model turn. Lets the bot answer the form in its own words, at the cost of a model call per submission and the values in the prompt. Left as a possible option.
- askdare stores leads (in the rate-limit store, or the admin store of ADR-0032). The core stays without a database; the admin add-on can record leads from
onLeadlike it records ratings, if it chooses to. - Validation in the browser only, or with a shared Zod schema in the browser. The client is untrusted, and Zod doesn't fit the browser budgets; the server is the authority and the client's hand-written checks only speed up the errors.