# Flow start points (web + webhook) — Design

Date: 2026-09-30 · Status: approved in conversation, pending written review
Repos: tiledesk-server, tiledesk-chatbot (tybot), design-studio. chat21-ionic unchanged.
Branch: continue on `feature/webhook-chatbot-hitl` (this changes the unreleased webhook-conversation feature,
spec `2026-09-15-webhook-chatbot-hitl-design.md`); `-PRE` ports afterwards as for the rest of that feature.

## 1. Goal

A conversational chatbot flow can have several **start points**, each a dedicated box on the canvas with its
own arrow into the flow. First delivery: **web** (the widget interaction, as today) and **webhook**.
Later sub-projects, each with its own spec: **scheduled** and **integration triggers** (e.g. a new Gmail email
via the connector triggers).

## 2. Decisions (user, 2026-09-30)

1. Web start point = the widget interaction as it works today for conversational chatbots.
2. Every start point is a dedicated box in design-studio; under the hood they all go through the webhook interface.
3. At most one start point per type for now; leave room to allow several per type later.
4. First spec: model + web + webhook. Scheduled and integration follow as separate specs.
5. The webhook stays unique per chatbot: it is coupled to the chatbot definition.
6. The start type is informative: it does not change how the flow runs. Type-specific input formats are
   handled by a **mapping** attached to the start type.
7. The department is defined while modelling the chatbot, not per start type.
8. Start points are picked from a dedicated **palette section**; future external triggers go there too.
9. Backward compatibility is mandatory: no regression for existing chatbots.

## 3. Current state (verified 2026-09-30)

- The start block is identified only by the name `start` (design-studio `RESERVED_INTENT_NAMES`,
  `cds-intent.component.ts:276`); `faq` has a unique index on `{id_project, id_faq_kb, intent_display_name}`.
- The widget and channels start a bot with the system message `/start` (`pubmodules/trigger/rulesTrigger.js`);
  `event/botEvent.js` forwards `/start` / `\start` system messages; tybot resolves `/start` as the intent
  named `start` and lets `attributes.action` override the text (`engine/TiledeskChatbot.js:135-147`);
  `#<intent_id>` resolves by id (`engine/MongodbBotsDataSource.js:93-99`).
- `models/webhook.js`: one webhook per chatbot (unique `{id_project, chatbot_id}`); `mode: 'conversation'`
  selects the conversation path (`services/webhookConversationService.js`), no mode = automation path
  (`services/webhookService.run` → tybot `/block`, sync or async). Conversation mode ignores `block_id` and
  always sends `/start`.
- `attributes.start_type` is written only as `'webhook'` and read nowhere.
- Publish/fork keeps `intent_id` (`routes/faq_kb.js:811, 925`), so a `block_id` stays valid on the published copy.
- `Change department` (tybot `department`) re-routes the conversation (`requestService.route` → `routeInternal`)
  and replaces the participants: in a webhook flow it removes the bot.

## 4. Data model and API (tiledesk-server)

### 4.1 Webhook record
One per chatbot, unchanged identity (`webhook_id`, URL). Unchanged fields: `webhook_id`, `id_project`, `name`,
`chatbot_id`, `enabled` (master switch), `async`, `copilot`, `mode`, and `block_id` (automation path only).

New field (only when `mode === 'conversation'`):
```
start_points: [{
  type: 'webhook',            // enum; 'web' is implicit (see 4.3); later 'scheduled', 'integration'
  block_id: <intent_id>,      // the start box
  enabled: Boolean (default true),
  mapping: { source_name: String }   // requester name; home of future type-specific mappings
}]
```
- At most one entry per `type`, enforced by the service (400), not by an index — allowing several per type later
  is a rule change only.
- Removed from the conversation webhook: `department_id`, `source_name` (unreleased branch fields; no migration).

### 4.2 Admin API (project-scoped, admin role)
- `GET /:projectid/webhooks/:chatbot_id` — includes `start_points`.
- `PUT /:projectid/webhooks/:chatbot_id/start_points/:type` body `{block_id, enabled?, mapping?, confirm?}` —
  create/update one start point; creates the webhook (`mode: 'conversation'`) if missing.
  Errors: 400 unknown type / bot not subtype `chatbot`; 404 `block_id` not a block of this chatbot;
  409 existing webhook without mode (automation) unless `confirm: true`, which converts it.
- `DELETE /:projectid/webhooks/:chatbot_id/start_points/:type` — removes it (404 if absent).
- Deleting a block that is a start point's `block_id` removes that start point (faq delete hook).

### 4.3 Web start point
The existing `start` block, implicit: not stored in `start_points`. The widget/channel path is unchanged — still
the `/start` system message — so no existing bot is touched. Design-studio shows it as the "Web start" box.

### 4.4 Start flow — `POST /webhook/:webhook_id` (and `/dev`), conversation mode
1. Load the webhook; 404 unknown; 422 if `enabled` false or the `webhook` start point is missing/disabled
   ("Webhook start is not enabled"); rate limit (429) as today.
2. Upsert lead `webhook_<webhook_id>` named `mapping.source_name` (default: the webhook name).
3. Create the request as today (`support-group-…`, bot participant, channel `webhook`), department = the
   department whose bot is this chatbot, else the project default. Attributes
   `{start_type: 'webhook', webhook_id, payload}`; dev keeps `sourcePage … &td_draft=true`.
4. Send system message `/start` with `attributes.action = '#<block_id>'` (forwarded by botEvent as today;
   tybot runs the box block, the text stays `/start`).
5. 202 `{success: true, request_id}`.

Payload handling unchanged: non-object bodies stored as `payload.body`; tybot merges payload keys + `payload`.

### 4.5 Automation webhooks
Untouched: no `start_points`, `block_id` + `/block` sync/async as today.

## 5. tybot

- `start_type` becomes a flow variable (from request attributes, like `payload`); empty for web/channel starts.
- A `/start` whose `attributes.action` names a missing block falls back to the `start` block (warn log), so a
  conversation never stalls.
- No other change.

## 6. design-studio

### 6.1 Palette section "Start points"
- New palette category next to Most used / AI / Flow / Integrations / Special, own icon, shown only for chatbot
  subtype `chatbot`.
- Items: "Web" (shown as placed, not draggable; click focuses the `start` box) and "Webhook". Future: Scheduled,
  integration triggers grouped by app.
- Dragging "Webhook" onto the canvas creates the start box at the drop point: a protected block (default name
  "Webhook start", `attributes.start_point: 'webhook'`, one connector action like `start`), then
  `PUT …/start_points/webhook {block_id, enabled: true}`; on 409 show the existing confirmation
  ("This chatbot already has a webhook that runs a block without a conversation…") and retry with `confirm: true`.
- A type already in the flow is disabled ("Already in this flow"); clicking it focuses the box.

### 6.2 Start boxes
- Identified by `attributes.start_point` + the start point's `block_id`, never by name (no new reserved names).
- Rendered like `start` (pill + type icon); no actions besides the outgoing connector; no rename as a normal
  block; no "Start test from here". The `start` box is labelled "Web start".
- Webhook box panel: enabled switch, production + development URLs (copy, regenerate), Source name (mapping),
  Test webhook start, hint that flows can read `start_type` and the payload variables. The "Start from webhook"
  section of the `start` panel is removed.
- Delete (webhook box only): confirmation → `DELETE …/start_points/webhook` → delete the block.
- Test webhook start: from the webhook box; highlights that box.
- Help text of Change department: note that in conversations started by a webhook it re-routes and removes
  the bot.

## 7. chat21-ionic
No change: conversations still carry `request_channel: 'webhook'`; the requester shown is the mapping's source name.

## 8. Backward compatibility

| Existing case | Guarantee |
|---|---|
| Conversational bots started by widget/channels | Path unchanged (`/start`); no data written to existing bots |
| Automation webhooks (webhook/copilot subtype or no mode) | Record, `block_id`, `/block` sync/async unchanged; converting needs explicit confirmation |
| Block names | Start boxes use an attribute marker; `start`, `webhook`, `defaultFallback` keep their meaning |
| Publish / fork / import / templates | `intent_id` preserved → `block_id` valid on published copies; webhooks not exported (as today) |
| Deleting bot | Deletes its webhook as today |
| Unreleased webhook-conversation fields | `department_id`, `source_name`, top-level conversation `block_id` dropped without migration |

## 9. Testing

- tiledesk-server (mocha): start_points CRUD + validation (400/404/409, one per type), webhook call runs the box
  (`attributes.action`) with `start_type`, 422 cases, start point removed on block delete, department rule.
  Regression: automation webhook sync + async, widget `/start` launch, published bot, imported bot.
- tybot: `/start` + `action: '#box'` runs the box with `start_type` variable; missing block → `start`;
  existing conversation tests unchanged.
- design-studio: palette category (one-per-type disabling, drop creates box + start point, 409 confirmation),
  webhook box panel, delete with confirmation; `ng build` with no new warnings.
- End-to-end (local stack, see memory `hitl-local-e2e-stack`): drag Webhook start → curl runs the webhook box with
  invite/buttons/Leave; widget still runs `start`; disable box → 422; delete box → 422; publish → works on the
  published copy.

## 10. Out of scope / later
- Scheduled start points (needs a cluster-safe scheduler; `node-schedule` is in-process only).
- Integration start points (Tiledesk-side subscription to connector `POST /triggers`, per-trigger mapping).
- Several start points of the same type.
- Moving the widget/channel start onto the start service.
