chatwoot-router 0.1.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-10-02
11
+
12
+ ### Added
13
+
14
+ - First release: the TypeSafe Jev routing of chatwoot-discord-relay 0.28.0 as its own Cloudflare Worker. A new ticket
15
+ gets its owner, its topic label, and its kind (with a canned response and a status, if the kind has them), once per
16
+ version of its first three customer messages; people own the ticket after that. See "How it works" in the README.
17
+ - Signed per-account Chatwoot webhooks and a five-minute sweep; per-account `startAfterConversationId` for a cutover.
18
+ - Coordination with chatwoot-discord-relay through the conversation attributes `routing_seen`, `routing_handled`, and
19
+ `routing_kind`.
20
+ - The `chatwoot-router-store-config` command and `chatwoot-router/stored-config`, for a configuration in KV.
21
+
22
+ [Unreleased]: https://github.com/Phala-Network/chatwoot-workers/compare/chatwoot-router@0.1.0...HEAD
23
+ [0.1.0]: https://github.com/Phala-Network/chatwoot-workers/releases/tag/chatwoot-router@0.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Phala
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,296 @@
1
+ # chatwoot-router
2
+
3
+ Route Chatwoot support tickets with [TypeSafe Jev](https://docs.typesafe.ai) on Cloudflare Workers:
4
+ assign owners, add topic and kind labels, and optionally send a canned response or resolve/snooze tickets.
5
+ Runs independently of [chatwoot-discord-relay](https://github.com/Phala-Network/chatwoot-workers/tree/main/packages/chatwoot-discord-relay).
6
+ Neither Worker calls the other: Chatwoot is their only coordination surface.
7
+
8
+ ## How it works
9
+
10
+ The router is a level-triggered reconciler running one-time automation per input version.
11
+ People own assignment, labels, and status after those effects, just as with a Chatwoot automation rule.
12
+
13
+ - Webhooks and the all-status, newest-activity-first sweep enqueue the same deduplicated
14
+ `route:<account>:<conversation>` job. Every run reads Chatwoot; event payloads are not routing inputs.
15
+ The SQLite queue retries failed runs with backoff. Sweep pages resume within a window anchored to
16
+ the previous pass's start, using the same pass model as the relay.
17
+ - Inputs are the first three customer messages with usable, redacted text, capped at 1,600 characters.
18
+ Their message ids form the memoization key. An unchanged key reuses Jev's answer; a new message
19
+ entering this window gets a new answer. There are no pending/waiting/done decision states.
20
+ Internal messages are filtered out; public replies are scanned within a bounded three-page read.
21
+ If that read cannot fill or finish the window, no automatic status action is allowed.
22
+ - Immediately before effects, a fresh read recomputes the input key. If it changed during Jev,
23
+ nothing from the old plan is applied or acknowledged; the existing queue reruns the job.
24
+ The answer and the fresh conversation determine applicable labels, owner, reply, and status.
25
+ Non-open, blocked, pre-cutover, or differently assigned conversations receive no routing actions.
26
+ An assignment to this decision's owner is eligible on retry. Empty inputs never call Jev or snooze.
27
+ - Every action has an effects-ledger key: `<effect>:<account>:<conversation>:<input key>` for
28
+ `assign`, `labels` (topic and kind together), and `status`; `reply:<account>:<conversation>` remains
29
+ once per ticket. Recorded effects never run again for that version, even if a person unassigns or
30
+ clears labels. Assignment and labels are recorded after confirmed success, or without a request when
31
+ already satisfied. Failed idempotent actions retry. Reply and status attempts are recorded before
32
+ sending; failed or uncertain attempts are not repeated. Confirmed kind actions carry their handled
33
+ input id in that ledger entry. There is no dispatcher.
34
+ - `routing_seen` projects a durable, monotonic per-conversation checkpoint: the highest customer
35
+ message id observed after applicable effects complete. Persist it before synchronizing attributes,
36
+ retaining greater existing values. Later bounded reads cannot lower it. `routing_handled` and
37
+ `routing_kind` derive from the effects ledger, not current labels or the bounded message view.
38
+ Each run writes only differences; this repairs lost attributes for any status without a repair job.
39
+ Memo lookups use exact keys; ledger history uses indexed key ranges, never a whole-cache scan.
40
+ - A run completes after applicable actions and attribute synchronization succeed, or after acknowledging
41
+ an out-of-scope conversation. Errors retry; deleted conversations are dropped. Events during a run
42
+ keep its job queued for another read. The sweep provides the same reconciliation if a webhook is lost.
43
+
44
+ ## Deploy
45
+
46
+ Use Node 24 (24.15 or newer), npm 12.1.0, and the Cloudflare CLI `cf`. From this repository, run inside Docker:
47
+
48
+ ```sh
49
+ npm ci
50
+ cd packages/chatwoot-router
51
+ npm run typecheck
52
+ npm test
53
+ npm run build
54
+ npx cf deploy --prebuilt --dry-run
55
+ ```
56
+
57
+ Edit `cloudflare.config.ts` with your non-secret settings. Keep required secrets out of source control;
58
+ `.dev.vars.example` lists placeholders. For local development copy it to `.dev.vars` and use `npm run dev`.
59
+ A maintainer deploys with `npx cf deploy --secrets-file <private-file>`.
60
+ The default Worker name is `chatwoot-router`; it exports one SQLite Durable Object, `Router`, bound as `ROUTER`.
61
+ Keep the `*/5 * * * *` cron trigger for reconciliation. `GET /healthz` returns 200 for a valid, readable
62
+ configuration and 503 otherwise; it does not test upstream credentials or connectivity.
63
+
64
+ For a private deployment repository, depend on the public `chatwoot-router` npm package plus `cf`, Vite, and
65
+ `@cloudflare/vite-plugin`. Its Worker entry is:
66
+
67
+ ```ts
68
+ export { default, Router } from "chatwoot-router";
69
+ ```
70
+
71
+ Use the same `cloudflare.config.ts` and `vite.config.ts` structure as this package. Pin compatible tooling versions
72
+ from the workspace and root manifests. Declare `Router` with `exports.durableObject({ storage: "sqlite" })`, bind it as
73
+ `ROUTER`, declare required secrets with `bindings.secret()`, and put `CONFIG` in `bindings.json(...)`.
74
+
75
+ For a configuration larger than a Worker JSON binding, use immutable KV configuration instead:
76
+
77
+ ```sh
78
+ npx cf kv namespaces create --title chatwoot-router-config
79
+ npx chatwoot-router-store-config config.jsonc --namespace-id <namespace-id>
80
+ ```
81
+
82
+ ```ts
83
+ import { storedConfig } from "chatwoot-router/stored-config";
84
+ ```
85
+
86
+ Set `CONFIG_STORE: bindings.kv({ id: "<namespace-id>" })` and
87
+ `CONFIG_KEY: bindings.text(storedConfig("config.jsonc").key)` in the Worker's `env`, removing `CONFIG`.
88
+ The CLI validates JSONC, uploads through the deployment project's `cf`, and prints the content-addressed key.
89
+ Keep older keys for rollbacks. The JavaScript and type declarations in each npm tarball are self-contained;
90
+ no unpublished shared package needs installing.
91
+
92
+ ## Chatwoot setup
93
+
94
+ - `CHATWOOT_TOKEN` belongs to an agent in every routed inbox or an administrator.
95
+ - In each routed account, add a signed webhook pointing to `POST /chatwoot/webhook`. Subscribe to
96
+ `message_created`, `conversation_created`, `conversation_updated`, and `conversation_status_changed`.
97
+ - Put that webhook's secret in `CHATWOOT_WEBHOOK_SECRETS`, keyed by account id. Webhooks use HMAC-SHA256 of
98
+ `<timestamp>.<raw body>`, a ±5-minute timestamp window, and account matching. Only public incoming messages
99
+ whose sender is a contact, and the relevant conversation events, queue routing; the API is re-read before acting.
100
+ - Create the topic and kind labels in Chatwoot. Create the following **conversation** custom attribute definitions:
101
+
102
+ | Fixed name | Type | Meaning |
103
+ | --- | --- | --- |
104
+ | `routing_seen` | Number | Latest customer message id observed by a completed run, including skipped tickets, written after applicable actions. The relay's sole completion signal. |
105
+ | `routing_handled` | Number | Latest customer message id handled by a kind's successful reply or set-aside action. |
106
+ | `routing_kind` | Text | Kind label added by the router, preserved by the relay's Manage card. |
107
+
108
+ Every public incoming contact `message_created` in a routed account queues reconciliation, even for an
109
+ assigned, closed, pre-cutover, or already-routed ticket. Skipped messages and no-owner decisions also advance
110
+ `routing_seen`; only eligible tickets ask Jev. When a kind handles messages, `routing_handled`
111
+ and `routing_kind` are included in the same update as `routing_seen`, after its reply/status/label actions.
112
+
113
+ Attributes are updated through Chatwoot's `custom_attributes` API with `merge: true`. Chatwoot v4.18 implements
114
+ this as read-merge-save, not an atomic merge: concurrent relay/router writes can still lose keys. Every run
115
+ projects its durable `routing_seen` checkpoint and effects ledger again. This repairs assigned, resolved, and
116
+ snoozed tickets too, without a separate repair job. The checkpoint retains the greatest observed customer id
117
+ and existing `routing_seen` value, even after that message leaves the bounded read window. The handled id and
118
+ kind label come from ledger history, independently of current labels or visible inputs. Greater existing
119
+ watermarks are retained in Chatwoot; unchanged attributes are not written again.
120
+ The relay similarly repairs its missing or different post URL on sync. Recovery is eventual, not a cross-Worker
121
+ transaction. Failed reads and writes retry without reclassifying unchanged inputs or repeating irreversible actions.
122
+ With the relay, set `router.accounts` there; its default wait is 30 seconds from each customer's message time.
123
+ Assignment and status changes never release that wait; only `routing_seen` or the timeout does.
124
+ The three attribute names are fixed. The relay's link attribute must not use any of them.
125
+ Watermarks accept non-negative safe integers stored as numbers or numeric strings; other values count as absent.
126
+
127
+ ## Configuration reference
128
+
129
+ `CONFIG` accepts a JSON object or JSON string, or use `CONFIG_STORE` + `CONFIG_KEY` instead. Do not set both.
130
+
131
+ ```json
132
+ {
133
+ "chatwoot": { "baseUrl": "https://chatwoot.example.com" },
134
+ "routing": {
135
+ "accounts": {
136
+ "1": {
137
+ "support": { "assignee": 6, "covers": "Product support, billing, and account access." },
138
+ "sales": { "assignee": 7, "covers": "Sales questions and purchase enquiries." }
139
+ }
140
+ },
141
+ "topics": { "billing": "Invoices and payments.", "technical": "Product troubleshooting." },
142
+ "minConfidence": 0.7,
143
+ "snoozeUnclear": true,
144
+ "kinds": { "1": { "spam": { "covers": "Unsolicited advertising.", "status": "resolved" } } }
145
+ },
146
+ "startAfterConversationId": { "1": 0 }
147
+ }
148
+ ```
149
+
150
+ | Setting | Type | Default | Meaning |
151
+ | --- | --- | --- | --- |
152
+ | `chatwoot.baseUrl` | HTTP(S) URL | required | Final Chatwoot API URL; redirects are refused. |
153
+ | `startAfterConversationId` | object: routed account id → integer ≥ 0 | `{}` | Each account's conversation display id at cutover; omitted accounts default to `0`. At or below it, routing actions and Jev are skipped, but customer messages still advance `routing_seen`. |
154
+ | `reconcile.lookbackSeconds` | integer ≥ 60 | `3600` | Minimum activity window for the five-minute sweep. |
155
+ | `reconcile.maxCatchUpSeconds` | integer ≥ 60 | `604800` | Maximum activity window after downtime. |
156
+ | `subrequestBudget` | integer 20–1000 | `45` | Per-alarm outbound budget; keep under your Workers plan's limit. |
157
+ | `routing` | object | required | TypeSafe Jev owner, topic, and kind configuration. |
158
+ | `routing.model` | non-empty string | `jev-1.13.0` | TypeSafe model. |
159
+ | `routing.minConfidence` | number 0.5–1 | `0.7` | Probability an answer needs before it is applied. |
160
+ | `routing.snoozeUnclear` | boolean | `false` | Snooze a ticket with no clear owner in which the customer asked for nothing yet (a greeting, a test) until their next message. |
161
+ | `routing.accounts` | object: account id → (owner name → owner) | required | Routed accounts and the owners Jev chooses from. Owner names are 1–40 lower-case letters, digits, or `_`; `unclear` is reserved. |
162
+ | `routing.accounts.<id>.<name>.assignee` | integer > 0 | required | Chatwoot user id to assign. |
163
+ | `routing.accounts.<id>.<name>.covers` | 1–1000 characters | required | What the owner handles: Jev's criterion for choosing them. |
164
+ | `routing.topics` | object: label → what it covers | unset | Topic labels (Chatwoot label names, lower case) Jev chooses from; one is added when a ticket has no label other than its kinds (an automation rule's label is kept alone). The separate relay can display them using its `forumTags` configuration. Unset: no topic. |
165
+ | `routing.kinds` | object: account id → (kind name → kind) | unset | Kinds of ticket Jev recognizes in routed accounts, added as labels beside the topic, and what is done once when it does ([Routing behavior](#routing-behavior)). Kind names are the account's label names (1–40 lower-case letters, digits, `_`, or `-`); `none` is reserved. Unset: none. |
166
+ | `routing.kinds.<id>.<name>.covers` | 1–1000 characters | required | What the kind is: Jev's criterion for recognizing it. |
167
+ | `routing.kinds.<id>.<name>.cannedResponse` | short code | unset | The account's Chatwoot canned response sent to the customer once, by the account's agent bot (`CHATWOOT_BOT_TOKENS`); none while it does not exist. |
168
+ | `routing.kinds.<id>.<name>.status` | `resolved` or `snoozed` | unset | Set instead of routing the ticket (`snoozed`: until the customer's next message), after the reply of a `cannedResponse`; a new customer message reopens it. |
169
+
170
+ ### Secrets
171
+
172
+ | Secret | Required | Meaning |
173
+ | --- | --- | --- |
174
+ | `CHATWOOT_TOKEN` | Yes | Chatwoot agent/admin access token for reading and applying decisions. |
175
+ | `CHATWOOT_WEBHOOK_SECRETS` | Yes | JSON object with a webhook secret for every routed account id. Missing entries fail configuration validation and `/healthz` returns 503. |
176
+ | `TYPESAFE_API_KEY` | Yes | TypeSafe Jev API key. |
177
+ | `CHATWOOT_BOT_TOKENS` | For kinds that reply | JSON object of Chatwoot agent bot tokens by account id; default `{}`. |
178
+
179
+ Required secrets are declared with `bindings.secret()`. Optional bot tokens are declared only in development
180
+ so deployment without canned replies does not require one. Do not connect a reply-only agent bot to an inbox.
181
+
182
+ ## Routing behavior
183
+
184
+ The [reconciliation model](#how-it-works) defines the flow. Jev answers multiple-choice questions about the
185
+ account's owner (or `unclear`), topic (with `topics`), kind (with `kinds`), and whether the customer asks for
186
+ anything yet (with `snoozeUnclear`). Inputs include email subjects and the first three customer messages with
187
+ usable text. Before they leave the Worker, emails, URLs, hex and base58 addresses, long tokens, phone numbers,
188
+ IP addresses (and four-part version numbers, which read as one), @handles, and the contact's name (each word
189
+ of two characters or more) are replaced with `[REDACTED]`. Identifier patterns run before name replacement
190
+ so a name cannot split an identifier. This is best-effort redaction, not anonymization: other personal details
191
+ can still reach TypeSafe. Attachment-only and redacted-only messages are acknowledged but do not consume
192
+ the text window, call Jev, or cause snoozing.
193
+
194
+ An owner at `minConfidence` or above is assigned unless the kind sets the ticket aside. A confident topic is
195
+ added when there are no labels other than kinds; labels set by an automation rule or a person are kept.
196
+ New messages entering the three-text-message window get a new memoized answer, even after an earlier
197
+ assignment; later messages outside that window do not change it. When no owner fits, the ticket is left for
198
+ a person. With `snoozeUnclear`, a confident `no request` answer can snooze an unassigned ticket until a
199
+ customer writes again, but not once all three inputs have been used. Reopening with the same inputs never
200
+ repeats that status action. A new window can apply a new status action. Before effects, the run re-reads the
201
+ input window and compares its key. If a new message entered, the existing queue job is deferred immediately,
202
+ without actions or acknowledgment of the old plan. The next run decides the new inputs. Messages outside a
203
+ full window do not change the key. Chatwoot provides no atomic snapshot across message reads and actions.
204
+
205
+ Router message reads set `filter_internal_messages=true`: Chatwoot v4.18's controller passes it to
206
+ `MessageFinder`, which filters private notes and activity lines before paging. The generated OpenAPI schema
207
+ omits it, so the shared typed client extends the query type; relay reads are unchanged. The forward scan is
208
+ bounded to 300 public messages, including agent replies. If it cannot fill or finish the input window, labels,
209
+ assignment, and a kind reply can still apply, but no status action does; the ticket stays for a person.
210
+
211
+ Immediately before actions a fresh read must show an open ticket, with an unblocked contact, unassigned or
212
+ assigned to this answer's owner. Otherwise only coordination attributes are synchronized. Routing resumes
213
+ after reopening using memoized inputs and the effects ledger, not a saved decision state. Recorded assignment,
214
+ label, and status effects are never repeated for that input version: a person unassigning or clearing labels
215
+ via Manage has the final say. A new input version permits new effects. Actions run in order: assignment/labels,
216
+ reply, status, then the checkpoint and attributes. Lost assignment/label responses are recovered by observing
217
+ Chatwoot and recording an already-satisfied effect without another request. Routing uses `CHATWOOT_TOKEN`,
218
+ whose user must be an agent in the routed inboxes; Chatwoot records the assignment as made by that user.
219
+
220
+
221
+ With `kinds`, Jev is also asked which of the account's kinds the ticket is (or `none`). Kinds are
222
+ labels of a second family: a ticket has one topic label, the category, and a kind Jev is confident
223
+ about is added beside it (create each kind as a label in its account; it needs no forum tag, and
224
+ the card shows it). It also acts with the decision: a kind with a `status` (spam, for example) sets the
225
+ ticket aside, resolved or snoozed until the customer's next message, instead of routing it; the
226
+ contact is not blocked, so a new message reopens the ticket as usual. A kind with a `cannedResponse` sends that
227
+ Chatwoot canned response (Settings → Canned Responses, by its short code) to the customer, for
228
+ example to acknowledge an application or point a security report to its process; a kind with both
229
+ replies, then sets the ticket aside (a templated security report: acknowledged, then resolved). It is read when
230
+ it is sent, so it is edited in Chatwoot, can use Chatwoot's variables such as `{{contact.name}}`,
231
+ and a missing response leaves the job retrying without recording a send attempt. It is sent as the account's
232
+ Chatwoot agent bot (`CHATWOOT_BOT_TOKENS`): customers see the bot's name, such as "Acme Support"; a
233
+ bot's message assigns nobody and is no human first reply, and Chatwoot then counts the customer as
234
+ answered (no longer waiting) until they write again. Create the bot in the account (Settings →
235
+ Bots), without connecting it to an inbox. A reply goes out at most once per ticket: it is recorded
236
+ before it is sent, so a failed send is not retried. Once a kind replied or set the ticket aside, the
237
+ customer messages it handled (those Jev was given) are relayed without calling the triage bot, with a
238
+ note, through the coordination attributes below. The relay waits up to its configured deadline;
239
+ if routing is unavailable or slower than that, the message proceeds with its usual mention. Rules that need no judgement of the text (by inbox, sender, or
240
+ subject) are Chatwoot's automation rules.
241
+
242
+ ```jsonc
243
+ "routing": {
244
+ "accounts": {
245
+ "1": {
246
+ "cloud": { "assignee": 6, "covers": "Cloud support and billing: deployments, invoices, account access." },
247
+ "sales": { "assignee": 7, "covers": "Sales and partnerships: pricing, capacity, volume deals." }
248
+ }
249
+ },
250
+ "topics": { "technical-support": "Something does not work.", "billing": "Payments, invoices, refunds." },
251
+ "kinds": {
252
+ "1": {
253
+ "spam": { "covers": "Unsolicited promotion or scams.", "status": "resolved" },
254
+ "startup-program": { "covers": "A Startup Program application.", "cannedResponse": "startup-program" }
255
+ }
256
+ }
257
+ }
258
+ ```
259
+
260
+
261
+ ## Reliability and cutover
262
+
263
+ The Worker acknowledges after a SQLite job is queued. Alarms serialize decisions, preserve work across failures,
264
+ and retry with exponential backoff capped at 30 minutes. The subrequest budget reserves 15 requests for the
265
+ worst-case route and yields to a fresh invocation before starting work it cannot finish.
266
+ Chatwoot's conversation-not-found response (JSON HTTP 404) is logged and the job is dropped, including a deletion
267
+ during a message read or write. Other failures, including proxy errors and account-level API failures, keep their backoff.
268
+ A sweep every five minutes pages **all statuses**, newest activity first, using the same shared Chatwoot client
269
+ and pass-window/cursor logic as the relay. It queues the same route job for every listed conversation,
270
+ regardless of status or assignment; reconciliation includes attribute repair.
271
+ Closing a ticket does not remove it from this list. Activity can move it to the front, so it may be seen again;
272
+ the next window starts from the previous pass's **start**, with a 60-second overlap, bounded by
273
+ `reconcile.lookbackSeconds` and `reconcile.maxCatchUpSeconds`.
274
+ The persisted page cursor survives budget yields and failed-page backoff. Route jobs run between
275
+ pages; a failed sweep does not hold due jobs in its own account or any other account.
276
+ Memoized answers, effects-ledger entries, and checkpoints remain in the Router's own Durable Object without expiry.
277
+
278
+ When splitting an existing relay deployment, **deploy the new relay first**, removing its old `routing` setting and
279
+ routing secrets and adding `router.accounts`. Keep its existing Worker `name` and `Hub` export to preserve state.
280
+ Then find the newest Chatwoot conversation display id **in each routed account** at cutover and record the map in
281
+ `startAfterConversationId`, for example `{ "1": 1200, "2": 85 }`. Deploy the router with the old routing configuration and its secrets, then add its
282
+ Chatwoot webhook. Do not start the router while the old embedded routing is active. The cutover watermark
283
+ intentionally leaves pre-cutover conversations to humans; old relay routing decisions are not imported. New
284
+ customer messages on those tickets are still acknowledged with `routing_seen`. Set the relay's `router.keepLabels`
285
+ (for example `["spam", "security", "beg-bounty"]`) to keep legacy kind labels even without `routing_kind`.
286
+
287
+ Chatwoot conversation display ids are account-local: never use one account's id as another account's cutoff.
288
+ The map accepts only accounts in `routing.accounts`, with non-negative integer ids. An omitted account defaults
289
+ to `0`, so include every account whose pre-cutover history must be left alone. In the example, account 1 starts
290
+ at conversation 1201 and account 2 at 86, independently of the other account's counter.
291
+
292
+ Redaction is best effort, not anonymization: other personal information can still reach TypeSafe. Review its
293
+ policy before enabling routing. Logs contain ids and outcomes, never message bodies or credentials.
294
+ See [SECURITY.md](https://github.com/Phala-Network/chatwoot-workers/blob/main/SECURITY.md) to report vulnerabilities,
295
+ [CONTRIBUTING.md](https://github.com/Phala-Network/chatwoot-workers/blob/main/CONTRIBUTING.md) for development,
296
+ and [CHANGELOG.md](https://github.com/Phala-Network/chatwoot-workers/blob/main/packages/chatwoot-router/CHANGELOG.md) for releases. Licensed under [MIT](https://github.com/Phala-Network/chatwoot-workers/blob/main/packages/chatwoot-router/LICENSE).
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../../shared/store-config.ts
4
+ import { spawnSync } from "node:child_process";
5
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
6
+ import { tmpdir } from "node:os";
7
+ import { join } from "node:path";
8
+ import { parseArgs } from "node:util";
9
+ function storeConfig(storedConfig2, command) {
10
+ const { values, positionals } = parseArgs({
11
+ options: { "namespace-id": { type: "string" } },
12
+ allowPositionals: true
13
+ });
14
+ const [file] = positionals;
15
+ const namespaceId = values["namespace-id"];
16
+ if (!file || !namespaceId) {
17
+ console.error(`Usage: ${command} <config.jsonc> --namespace-id <id>`);
18
+ process.exit(2);
19
+ }
20
+ let stored;
21
+ try {
22
+ stored = storedConfig2(file);
23
+ } catch (error) {
24
+ console.error(error instanceof Error ? error.message : String(error));
25
+ process.exit(1);
26
+ }
27
+ const dir = mkdtempSync(join(tmpdir(), "store-config-"));
28
+ try {
29
+ const path = join(dir, "config.json");
30
+ writeFileSync(path, stored.value);
31
+ const put = spawnSync(
32
+ "npx",
33
+ ["cf", "kv", "keys", "put", stored.key, "--namespace-id", namespaceId, "--file", path],
34
+ {
35
+ stdio: ["ignore", process.stderr, process.stderr]
36
+ }
37
+ );
38
+ if (put.status === 0) {
39
+ console.log(stored.key);
40
+ } else {
41
+ console.error(`Storing ${stored.key} failed: cf exited with ${put.status ?? put.signal}`);
42
+ process.exitCode = 1;
43
+ }
44
+ } finally {
45
+ rmSync(dir, { recursive: true, force: true });
46
+ }
47
+ }
48
+
49
+ // ../../shared/stored-config.ts
50
+ import { createHash } from "node:crypto";
51
+ import { readFileSync } from "node:fs";
52
+ import { parse, printParseErrorCode } from "jsonc-parser";
53
+ function readStoredConfig(file, configSchema2) {
54
+ const errors = [];
55
+ const config = parse(readFileSync(file, "utf8"), errors, { allowTrailingComma: true });
56
+ if (errors.length > 0) {
57
+ throw new Error(
58
+ `${file} is not JSON: ${errors.map((e) => `${printParseErrorCode(e.error)} at ${e.offset}`).join("; ")}`
59
+ );
60
+ }
61
+ const result = configSchema2.safeParse(config);
62
+ if (!result.success) {
63
+ throw new Error(
64
+ `Invalid CONFIG: ${result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`
65
+ );
66
+ }
67
+ const value = JSON.stringify(config);
68
+ return { key: `config-${createHash("sha256").update(value).digest("hex")}`, value };
69
+ }
70
+
71
+ // src/config.ts
72
+ import { z as z2 } from "zod";
73
+
74
+ // ../../shared/config.ts
75
+ import { z } from "zod";
76
+ var jsonRecord = z.string().transform((value, ctx) => {
77
+ try {
78
+ return JSON.parse(value);
79
+ } catch {
80
+ ctx.addIssue({ code: "custom", message: "must be a JSON object" });
81
+ return z.NEVER;
82
+ }
83
+ }).pipe(z.record(z.string(), z.string().min(1)));
84
+ var reconcileSchema = z.strictObject({
85
+ /** The sweep looks at conversations with activity within at least this window. */
86
+ lookbackSeconds: z.number().int().min(60).default(3600),
87
+ /** After downtime, the sweep catches up at most this far back. */
88
+ maxCatchUpSeconds: z.number().int().min(60).default(7 * 24 * 3600)
89
+ }).prefault({});
90
+
91
+ // src/config.ts
92
+ var accountId = z2.string().regex(/^[1-9]\d*$/, "must be a Chatwoot account id").refine((value) => Number.isSafeInteger(Number(value)), "must be a safe integer");
93
+ var configSchema = z2.strictObject({
94
+ chatwoot: z2.strictObject({ baseUrl: z2.url({ protocol: /^https?$/ }) }),
95
+ reconcile: reconcileSchema,
96
+ startAfterConversationId: z2.record(accountId, z2.number().int().min(0)).default({}),
97
+ subrequestBudget: z2.number().int().min(20).max(1e3).default(45),
98
+ routing: z2.strictObject({
99
+ model: z2.string().min(1).default("jev-1.13.0"),
100
+ /** Jev's probability an answer needs before it is applied. */
101
+ minConfidence: z2.number().min(0.5).max(1).default(0.7),
102
+ /**
103
+ * Snooze a ticket Jev cannot assign yet, and in which the customer asked for nothing yet, until
104
+ * their next message (see src/routing.ts).
105
+ */
106
+ snoozeUnclear: z2.boolean().default(false),
107
+ /** Per Chatwoot account id: the owners Jev chooses from, by a short name. */
108
+ accounts: z2.record(
109
+ accountId,
110
+ z2.record(
111
+ z2.string().regex(/^[a-z0-9_]{1,40}$/, "must be a short lower-case name").refine((name) => name !== "unclear", "unclear is reserved"),
112
+ z2.strictObject({
113
+ /** Chatwoot user id to assign. */
114
+ assignee: z2.number().int().positive(),
115
+ /** What the owner handles, as Jev's criterion for choosing them. */
116
+ covers: z2.string().min(1).max(1e3)
117
+ })
118
+ ).refine((owners) => Object.keys(owners).length > 0, "needs at least one owner")
119
+ ),
120
+ /** Topic labels (Chatwoot label names, lower case) and what each covers. Unset: no topic. */
121
+ topics: z2.record(
122
+ z2.string().regex(/^[a-z0-9_-]{1,255}$/, "must be a Chatwoot label name (lower case)"),
123
+ z2.string().min(1).max(1e3)
124
+ ).optional(),
125
+ /**
126
+ * Per Chatwoot account id: kinds of ticket Jev recognizes, by a short name, and what is done,
127
+ * once, when it does (see src/routing.ts). Unset: none.
128
+ */
129
+ kinds: z2.record(
130
+ accountId,
131
+ z2.record(
132
+ z2.string().regex(/^[a-z0-9_-]{1,40}$/, "must be a short lower-case name").refine((name) => name !== "none", "none is reserved"),
133
+ z2.strictObject({
134
+ /** What the kind is, as Jev's criterion for recognizing it. */
135
+ covers: z2.string().min(1).max(1e3),
136
+ /**
137
+ * Short code of the account's Chatwoot canned response sent to the customer once, as
138
+ * the account's agent bot (CHATWOOT_BOT_TOKENS). None is sent while it does not exist.
139
+ */
140
+ cannedResponse: z2.string().trim().min(1).max(255).optional(),
141
+ /**
142
+ * Set instead of routing the ticket (after the reply, with cannedResponse): `resolved`,
143
+ * or `snoozed` until the customer's next message. A new message from the customer
144
+ * reopens either.
145
+ */
146
+ status: z2.enum(["resolved", "snoozed"]).optional()
147
+ })
148
+ )
149
+ ).optional()
150
+ })
151
+ }).refine((config) => Object.keys(config.startAfterConversationId).every((id) => config.routing.accounts[id]), {
152
+ path: ["startAfterConversationId"],
153
+ message: "must only name routed accounts"
154
+ }).refine((config) => Object.keys(config.routing?.kinds ?? {}).every((id) => config.routing?.accounts[id]), {
155
+ path: ["routing", "kinds"],
156
+ message: "must only name routed accounts"
157
+ }).refine(
158
+ (config) => Object.values(config.routing?.kinds ?? {}).every(
159
+ (kinds) => Object.keys(kinds).every((kind) => !Object.hasOwn(config.routing?.topics ?? {}, kind))
160
+ ),
161
+ { path: ["routing", "kinds"], message: "a kind cannot be named as a topic: they are labels of two families" }
162
+ );
163
+ var secretsSchema = z2.object({
164
+ CHATWOOT_TOKEN: z2.string().min(1),
165
+ CHATWOOT_WEBHOOK_SECRETS: jsonRecord,
166
+ TYPESAFE_API_KEY: z2.string().min(1),
167
+ CHATWOOT_BOT_TOKENS: jsonRecord.default({})
168
+ });
169
+
170
+ // scripts/stored-config.ts
171
+ function storedConfig(file) {
172
+ return readStoredConfig(file, configSchema);
173
+ }
174
+
175
+ // scripts/store-config.ts
176
+ storeConfig(storedConfig, "chatwoot-router-store-config");
@@ -0,0 +1,4 @@
1
+ export declare function storedConfig(file: string | URL): {
2
+ key: string;
3
+ value: string;
4
+ };
@@ -0,0 +1,128 @@
1
+ // ../../shared/stored-config.ts
2
+ import { createHash } from "node:crypto";
3
+ import { readFileSync } from "node:fs";
4
+ import { parse, printParseErrorCode } from "jsonc-parser";
5
+ function readStoredConfig(file, configSchema2) {
6
+ const errors = [];
7
+ const config = parse(readFileSync(file, "utf8"), errors, { allowTrailingComma: true });
8
+ if (errors.length > 0) {
9
+ throw new Error(
10
+ `${file} is not JSON: ${errors.map((e) => `${printParseErrorCode(e.error)} at ${e.offset}`).join("; ")}`
11
+ );
12
+ }
13
+ const result = configSchema2.safeParse(config);
14
+ if (!result.success) {
15
+ throw new Error(
16
+ `Invalid CONFIG: ${result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`
17
+ );
18
+ }
19
+ const value = JSON.stringify(config);
20
+ return { key: `config-${createHash("sha256").update(value).digest("hex")}`, value };
21
+ }
22
+
23
+ // src/config.ts
24
+ import { z as z2 } from "zod";
25
+
26
+ // ../../shared/config.ts
27
+ import { z } from "zod";
28
+ var jsonRecord = z.string().transform((value, ctx) => {
29
+ try {
30
+ return JSON.parse(value);
31
+ } catch {
32
+ ctx.addIssue({ code: "custom", message: "must be a JSON object" });
33
+ return z.NEVER;
34
+ }
35
+ }).pipe(z.record(z.string(), z.string().min(1)));
36
+ var reconcileSchema = z.strictObject({
37
+ /** The sweep looks at conversations with activity within at least this window. */
38
+ lookbackSeconds: z.number().int().min(60).default(3600),
39
+ /** After downtime, the sweep catches up at most this far back. */
40
+ maxCatchUpSeconds: z.number().int().min(60).default(7 * 24 * 3600)
41
+ }).prefault({});
42
+
43
+ // src/config.ts
44
+ var accountId = z2.string().regex(/^[1-9]\d*$/, "must be a Chatwoot account id").refine((value) => Number.isSafeInteger(Number(value)), "must be a safe integer");
45
+ var configSchema = z2.strictObject({
46
+ chatwoot: z2.strictObject({ baseUrl: z2.url({ protocol: /^https?$/ }) }),
47
+ reconcile: reconcileSchema,
48
+ startAfterConversationId: z2.record(accountId, z2.number().int().min(0)).default({}),
49
+ subrequestBudget: z2.number().int().min(20).max(1e3).default(45),
50
+ routing: z2.strictObject({
51
+ model: z2.string().min(1).default("jev-1.13.0"),
52
+ /** Jev's probability an answer needs before it is applied. */
53
+ minConfidence: z2.number().min(0.5).max(1).default(0.7),
54
+ /**
55
+ * Snooze a ticket Jev cannot assign yet, and in which the customer asked for nothing yet, until
56
+ * their next message (see src/routing.ts).
57
+ */
58
+ snoozeUnclear: z2.boolean().default(false),
59
+ /** Per Chatwoot account id: the owners Jev chooses from, by a short name. */
60
+ accounts: z2.record(
61
+ accountId,
62
+ z2.record(
63
+ z2.string().regex(/^[a-z0-9_]{1,40}$/, "must be a short lower-case name").refine((name) => name !== "unclear", "unclear is reserved"),
64
+ z2.strictObject({
65
+ /** Chatwoot user id to assign. */
66
+ assignee: z2.number().int().positive(),
67
+ /** What the owner handles, as Jev's criterion for choosing them. */
68
+ covers: z2.string().min(1).max(1e3)
69
+ })
70
+ ).refine((owners) => Object.keys(owners).length > 0, "needs at least one owner")
71
+ ),
72
+ /** Topic labels (Chatwoot label names, lower case) and what each covers. Unset: no topic. */
73
+ topics: z2.record(
74
+ z2.string().regex(/^[a-z0-9_-]{1,255}$/, "must be a Chatwoot label name (lower case)"),
75
+ z2.string().min(1).max(1e3)
76
+ ).optional(),
77
+ /**
78
+ * Per Chatwoot account id: kinds of ticket Jev recognizes, by a short name, and what is done,
79
+ * once, when it does (see src/routing.ts). Unset: none.
80
+ */
81
+ kinds: z2.record(
82
+ accountId,
83
+ z2.record(
84
+ z2.string().regex(/^[a-z0-9_-]{1,40}$/, "must be a short lower-case name").refine((name) => name !== "none", "none is reserved"),
85
+ z2.strictObject({
86
+ /** What the kind is, as Jev's criterion for recognizing it. */
87
+ covers: z2.string().min(1).max(1e3),
88
+ /**
89
+ * Short code of the account's Chatwoot canned response sent to the customer once, as
90
+ * the account's agent bot (CHATWOOT_BOT_TOKENS). None is sent while it does not exist.
91
+ */
92
+ cannedResponse: z2.string().trim().min(1).max(255).optional(),
93
+ /**
94
+ * Set instead of routing the ticket (after the reply, with cannedResponse): `resolved`,
95
+ * or `snoozed` until the customer's next message. A new message from the customer
96
+ * reopens either.
97
+ */
98
+ status: z2.enum(["resolved", "snoozed"]).optional()
99
+ })
100
+ )
101
+ ).optional()
102
+ })
103
+ }).refine((config) => Object.keys(config.startAfterConversationId).every((id) => config.routing.accounts[id]), {
104
+ path: ["startAfterConversationId"],
105
+ message: "must only name routed accounts"
106
+ }).refine((config) => Object.keys(config.routing?.kinds ?? {}).every((id) => config.routing?.accounts[id]), {
107
+ path: ["routing", "kinds"],
108
+ message: "must only name routed accounts"
109
+ }).refine(
110
+ (config) => Object.values(config.routing?.kinds ?? {}).every(
111
+ (kinds) => Object.keys(kinds).every((kind) => !Object.hasOwn(config.routing?.topics ?? {}, kind))
112
+ ),
113
+ { path: ["routing", "kinds"], message: "a kind cannot be named as a topic: they are labels of two families" }
114
+ );
115
+ var secretsSchema = z2.object({
116
+ CHATWOOT_TOKEN: z2.string().min(1),
117
+ CHATWOOT_WEBHOOK_SECRETS: jsonRecord,
118
+ TYPESAFE_API_KEY: z2.string().min(1),
119
+ CHATWOOT_BOT_TOKENS: jsonRecord.default({})
120
+ });
121
+
122
+ // scripts/stored-config.ts
123
+ function storedConfig(file) {
124
+ return readStoredConfig(file, configSchema);
125
+ }
126
+ export {
127
+ storedConfig
128
+ };