@tiledesk/tiledesk-server 3.0.1 → 3.2.1

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 (140) hide show
  1. package/.circleci/config.yml +1 -1
  2. package/CHANGELOG.md +47 -0
  3. package/Dockerfile +3 -2
  4. package/Dockerfile-en +3 -2
  5. package/Dockerfile-jobs +3 -2
  6. package/Dockerfile-profiler +3 -2
  7. package/README.md +15 -0
  8. package/app.js +12 -1
  9. package/bin/quota-reset +68 -0
  10. package/bin/quota-restore +86 -0
  11. package/channels/chat21/chat21Handler.js +50 -34
  12. package/channels/chat21/chat21JoinTracker.js +43 -0
  13. package/config/database.js +5 -5
  14. package/config/native_mcp/native_mcp_servers.json +1 -0
  15. package/controllers/chatbot.controller.js +80 -27
  16. package/controllers/intents.controller.js +42 -0
  17. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-chat21-ionic.md +468 -0
  18. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-design-studio.md +1349 -0
  19. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-server.md +1694 -0
  20. package/docs/superpowers/plans/2026-09-15-webhook-chatbot-hitl-tybot.md +789 -0
  21. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-server.md +83 -0
  22. package/docs/superpowers/plans/2026-09-29-hitl-known-limits-tybot.md +76 -0
  23. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-chat21-ionic.md +97 -0
  24. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-design-studio.md +72 -0
  25. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-server.md +223 -0
  26. package/docs/superpowers/plans/2026-09-29-hitl-participant-removal-tybot.md +121 -0
  27. package/docs/superpowers/plans/2026-09-29-hitl-pre-branch-ports.md +65 -0
  28. package/docs/superpowers/plans/2026-09-30-flow-start-points-design-studio.md +88 -0
  29. package/docs/superpowers/plans/2026-09-30-flow-start-points-server.md +103 -0
  30. package/docs/superpowers/plans/2026-09-30-flow-start-points-tybot.md +43 -0
  31. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-design-studio.md +80 -0
  32. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-runtime.md +39 -0
  33. package/docs/superpowers/plans/2026-10-01-scheduled-start-point-server.md +106 -0
  34. package/docs/superpowers/specs/2026-09-15-webhook-chatbot-hitl-design.md +715 -0
  35. package/docs/superpowers/specs/2026-09-30-flow-start-points-design.md +156 -0
  36. package/docs/superpowers/specs/2026-10-01-scheduled-start-point-design.md +303 -0
  37. package/docs/superpowers/todo/2026-09-14-webhook-chatbot-shared-code-fixes.md +191 -0
  38. package/event/botEvent.js +12 -1
  39. package/event/emailEvent.js +3 -0
  40. package/event/messageEvent.js +44 -0
  41. package/jobs.js +10 -0
  42. package/jobsManager.js +19 -0
  43. package/middleware/errorHandler.js +6 -2
  44. package/models/analyticResult.js +1 -1
  45. package/models/channelConstants.js +3 -2
  46. package/models/dataTable.js +16 -0
  47. package/models/location.js +6 -12
  48. package/models/quotaUsage.js +71 -0
  49. package/models/request.js +71 -61
  50. package/models/requestSnapshot.js +12 -0
  51. package/models/routerLogger.js +1 -1
  52. package/models/webhook.js +44 -0
  53. package/package.json +9 -7
  54. package/pubmodules/emailNotification/requestNotification.js +14 -22
  55. package/pubmodules/events/test/eventRoute.js +7 -6
  56. package/pubmodules/rules/conciergeBot.js +9 -4
  57. package/routes/auth.js +1 -12
  58. package/routes/campaigns.js +0 -13
  59. package/routes/dataTable.js +23 -3
  60. package/routes/faq_kb.js +12 -1
  61. package/routes/messagesRoot.js +0 -1
  62. package/routes/quotes.js +38 -1
  63. package/routes/request.js +81 -5
  64. package/routes/users.js +0 -1
  65. package/routes/webhook.js +160 -8
  66. package/routes/webhooks.js +276 -6
  67. package/routes/widgetLoader.js +6 -3
  68. package/services/QuoteManager.js +77 -16
  69. package/services/aiService.js +1 -1
  70. package/services/chatbotService.js +55 -1
  71. package/services/dataTableService.js +436 -42
  72. package/services/departmentService.js +60 -43
  73. package/services/dolphinSchedulerClient.js +116 -0
  74. package/services/emailService.js +6 -1
  75. package/services/hitlParticipantListener.js +260 -0
  76. package/services/participantInviteService.js +302 -0
  77. package/services/participantRemoveService.js +79 -0
  78. package/services/quotaSnapshotService.js +1167 -0
  79. package/services/requestService.js +102 -144
  80. package/services/scheduledStart/dolphinBackend.js +246 -0
  81. package/services/scheduledStart/schedulerBackend.js +101 -0
  82. package/services/scheduledStart/schedulerServiceClient.js +41 -0
  83. package/services/scheduledStartPublishListener.js +186 -0
  84. package/services/scheduledStartRules.js +173 -0
  85. package/services/scheduledStartService.js +130 -0
  86. package/services/startPointCleanupListener.js +30 -0
  87. package/services/startPointService.js +158 -0
  88. package/services/testWsService.js +1 -4
  89. package/services/updateRequestSnapshotQueued.js +8 -9
  90. package/services/webhookConversationService.js +185 -0
  91. package/services/webhookSettingsValidator.js +61 -0
  92. package/template/email/checkpointReachedEmail.html +5 -0
  93. package/test/agentPromptsRoute.test.js +3 -2
  94. package/test/agentTokensQuota.js +237 -0
  95. package/test/botEventHitlFallback.js +19 -0
  96. package/test/chat21HandlerJoinWiring.js +59 -0
  97. package/test/dataTablesRoute.test.js +649 -2
  98. package/test/dateUtils.test.js +0 -3
  99. package/test/departmentService.js +8 -12
  100. package/test/{fileRoute.js → deprecated/_deprecated_fileRoute} +2 -2
  101. package/test/{kbsettingsRoute.js → deprecated/_deprecated_kbsettingsRoute} +3 -3
  102. package/test/faqRoute.js +6 -1
  103. package/test/faqkbRoute.js +14 -10
  104. package/test/faqkbV3Route.js +1 -1
  105. package/test/filepRoute.js +4 -50
  106. package/test/hitlParticipantListener.js +446 -0
  107. package/test/hitlQueueWiring.js +191 -0
  108. package/test/hitlWaitingReset.js +124 -0
  109. package/test/inviteParticipantsRoute.js +492 -0
  110. package/test/kbRoute.js +1 -1
  111. package/test/messageRoute.js +33 -21
  112. package/test/nativemcp.test.js +7 -1
  113. package/test/openaiRoute.js +2 -8
  114. package/test/publishFailLoud.test.js +414 -0
  115. package/test/quotaSnapshots.js +1044 -0
  116. package/test/removeParticipantsRoute.js +186 -0
  117. package/test/requestService.js +24 -16
  118. package/test/requestServiceRemoveParticipant.js +144 -0
  119. package/test/requestVisibility.test.js +186 -0
  120. package/test/scheduledStartFacade.js +184 -0
  121. package/test/scheduledStartHandover.js +95 -0
  122. package/test/scheduledStartPublish.js +320 -0
  123. package/test/scheduledStartRoute.js +163 -0
  124. package/test/scheduledStartRules.js +177 -0
  125. package/test/scheduledStartRun.js +299 -0
  126. package/test/scheduledStartSchedulerBackend.js +116 -0
  127. package/test/scheduledStartService.js +290 -0
  128. package/test/startPointsCleanup.js +69 -0
  129. package/test/startPointsRegression.js +169 -0
  130. package/test/startPointsRoute.js +158 -0
  131. package/test/webhookConversationRoute.js +347 -0
  132. package/test/webhookConversationService.js +261 -0
  133. package/test/webhookConversationTestUtils.js +55 -0
  134. package/utils/aiUtils.js +95 -1
  135. package/utils/dataTableUtils.js +99 -9
  136. package/utils/intentFieldNames.js +192 -0
  137. package/utils/quotaCliUtil.js +43 -0
  138. package/utils/requestVisibility.js +355 -0
  139. package/websocket/webSocketServer.js +36 -31
  140. /package/test/{imageRoute.js → deprecated/_deprecated_imageRoute} +0 -0
@@ -16,7 +16,7 @@ jobs:
16
16
  # You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
17
17
  # A list of available CircleCI Docker Convenience Images are available here: https://circleci.com/developer/images/image/cimg/node
18
18
  docker:
19
- - image: cimg/node:16.17.1
19
+ - image: cimg/node:24.22.1
20
20
  # Then run your tests!
21
21
  # CircleCI will report the results back to your VCS provider.
22
22
  steps:
package/CHANGELOG.md CHANGED
@@ -5,6 +5,53 @@
5
5
  🚀 IN PRODUCTION 🚀
6
6
  (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
7
7
 
8
+ # 3.2.1
9
+ - Bump version `tiledesk-chatbot-template` to 0.1.7
10
+ - Bump verion `tiledesk-vxml-connector` to 0.2.7
11
+ - Data Tables: new `ttl` column type. The column holds the number of seconds a row stays alive and the service mirrors it into the top level `expireAt` date carried by a MongoDB TTL index, so expired rows are removed by the database itself. Only one ttl column per table; the value must be a positive integer not above 31536000 (one year), anything else answers 400
12
+ - `expireAt` is always `updatedAt` plus the ttl, so it measures idle time and not total lifetime: every change to a row renews its deadline, including an update that leaves the ttl column alone. A row carrying no ttl value has no `expireAt` and never expires
13
+ - Changing the schema realigns the rows: turning a column into `ttl` backfills `expireAt` from the numbers already stored (values that are not numeric are left without a deadline), while changing the type away from `ttl` or deleting the column removes `expireAt` from every row
14
+ - The TTL index on `tablerows.expireAt` is created at boot even when `MONGOOSE_AUTOINDEX` is off; without it the rows keep an `expireAt` that nothing honours and never expire. MongoDB sweeps expired documents about once a minute, so a row can outlive its deadline by up to a minute
15
+ - New `POST /:projectid/tables/:id/rows/insert` inserts several rows in one request, body `{ data: [ {row}, … ] }`. The whole batch is validated before anything is written, so an invalid row answers 400 instead of leaving a partial insert behind
16
+ - New `PUT /:projectid/tables/:id/rows/upsert` updates or inserts several rows in one request. Same body as `row/upsert` with `data` holding an array: the `conditions` name the columns that identify a row and the value to match is taken from each row of the batch, so a `value` written in the conditions is ignored. A row that carries no value for a match column answers 400 before any write, and conditions matching more than one row answer 409
17
+ - Both bulk endpoints accept at most `MAX_BULK_ROWS` rows per request (default 100)
18
+ - Fixed `PUT /:projectid/tables/:id/row/update` by conditions answering `[]` even though the rows had been written: the response was read back with the same filter used to select them, which no longer matched when the update rewrote the very field the conditions select on
19
+ - `stats.rows` and `stats.sizeBytes` of a table are now accurate. `GET /tables` and `GET /tables/:id` recompute them for the tables owning a ttl column, which the database empties without telling the service; renaming or deleting a column recomputes them as well, since both rewrite every row; and the version key is counted when a row is inserted, which before made the counters drift below zero on a table with enough churn
20
+
21
+ # 3.1.0
22
+ - Request visibility no longer embeds the project's agents in every request. `snapshot.agents` (full `project_user` documents) is no longer written; new requests store `snapshot.visibility`, `snapshot.agentIds` and `snapshot.visibilitySince`. Legacy requests keep working through `snapshot.agents`, which is removed on the next reroute
23
+ - Department with a group (`id_group`, or several groups in `department.groups`): visible to the active members of all configured groups, not only the group chosen for assignment. Department without groups: visible to the teammates who had joined the project when the request was routed (`visibilitySince`)
24
+ - Inbox (`GET /:projectid/requests`, `/csv`, `/me` unchanged), websocket `/requests` topics and `GET /:projectid/requests/:requestid` apply the same visibility rules. The single-request endpoint now answers 403 when the caller cannot see the request; owner, admin, bot and subscription tokens are not affected
25
+ - Websocket `snapshot.agents` is always `[{ id_user }]`; created/updated events carry `snapshot.visibility`, `snapshot.visibilitySince` and `snapshot.agentIds`. Clients that checked `snapshot.agents` to find the current agent must also check `agentIds` and `visibility`
26
+ - Pooled-request email notifications resolve recipients from the visibility rules instead of the embedded array
27
+ - Bot departments with a group now assign and show only the group's members (before: every project agent)
28
+ - New indexes on `snapshot.agentIds` and `snapshot.visibility`/`snapshot.visibilitySince` in `models/request.js`; they must be created in production before deploying (Atlas does not create them automatically). Existing indexes on `snapshot.agents.id_user` are kept until retention removes the legacy documents
29
+ - Updated node version to 24.x to improved vulnerability
30
+
31
+ # 3.0.2
32
+ - Release of 3.0.2-rc2, tested on stage on 2026-10-26; no code change.
33
+
34
+ # 3.0.2-rc2
35
+ - Publish no longer drops blocks silently. Before, a block MongoDB could not insert (a stored field name containing '.' or starting with '$', e.g. `attributes.commands`) was left out of the published copy while publish answered 200. Now publish checks the agent's attributes and every block before writing anything and answers 422 with a readable `message`, e.g. `The flow was not published: 1 block could not be published — "Invite On Call": field name "attributes.commands" in action 1 (reply) contains '.', which is not allowed. Fix the block and publish again.`, and `errors: [{ intent_id, intent_display_name, field, reason }]` (`{ agent, field, reason }` for the agent's attributes); the draft keeps pointing at its previous published copy. Any other failure while copying removes the half-made copy and answers with a readable message (`The flow was not published: <reason>. The published version was not changed.`). `PUT /publish/multi` reports the same message per agent (`results[].error`, `results[].errors`). The check reads only the project's own source (`restore_from`)
36
+ - Import, fork and restore (`importjson`, `fork`, `/restore/:publish_id`), which insert blocks, check the agent's attributes and every block before any write and answer 422 with a readable message (`The flow could not be imported: …`) instead of a partial write or "Error creating new chatbot"; a failed import in create mode removes the bot it created
37
+ - Saving a block (`POST`/`PUT`/`PATCH /faq`, `ops_update`) is unchanged: such field names are still stored, so existing blocks that hold one stay editable; they are reported when the flow is published, imported or restored
38
+ - Behaviour change: `IntentsController.createMany`/`upsertMany` reject when a block cannot be saved instead of dropping it. An overwrite import whose JSON repeats a block name or id (previously the duplicate was dropped silently) now fails with an error naming the block; duplicates skipped by a create/replace import are still skipped
39
+
40
+ # 3.0.2-rc1
41
+ - vibe coder candidate on top of 3.0.1: agent_tokens quota + quota snapshots, start points (web/webhook/scheduled via scheduler service), HITL (invite/remove human)
42
+ - New quota type `agent_tokens`: a monthly budget for the AI agent runtime (vibe coder), separate from the chatbot `tokens` quota. Plan limits equal to `tokens`, per-project override `profile.quotes.agent_tokens`, threshold emails (50/75/95/100%) like the other quotas
43
+ - `POST /:projectid/quotes/incr/agent_tokens` counts `ceil(cost.amount * AGENT_TOKENS_PER_USD)` when a USD cost is sent, otherwise `ceil(tokens * AGENT_TOKENS_FALLBACK_MULTIPLIER)` (defaults 1000000 and 1); invalid bodies, tokens > 1e9 and cost.amount > 1e4 USD answer 400. `POST /quotes/incr/tokens` is unchanged, any other type answers 400
44
+ - `GET /:projectid/quotes/agent_tokens` returns `isAvailable`; `POST /:projectid/quotes` includes `agent_tokens`
45
+ - Deploy this server before enabling the quota in the AI agent runtime: an older server answers `isAvailable: false` for `agent_tokens` and counts `incr/agent_tokens` into `tokens`
46
+ - Quota counters survive a Redis wipe: every `QUOTA_SNAPSHOT_INTERVAL_MINUTES` (default 60, 0 disables, first run after min(5 min, interval)) one replica (Redis lock `quotes:snapshot:lock`) copies the counters of the current and previous period changed since the last cycle into the new mongo collection `quota_usages`, with `$max` on the value so a wiped Redis never lowers the backup. Usage after the last snapshot (at most one interval) can be lost
47
+ - Changed counters are tracked in the Redis set `quotes:dirty` (`MULTI` increment + `SADD` + `EXEC` when a mark is needed, the plain increment otherwise: a per-process memo, set only after a confirmed `SADD`, skips it until the next snapshot generation and only while the watcher's generation read is younger than 1.5 x `QUOTA_SENTINEL_CHECK_SECONDS`, which all replicas must share; ~90-110 bytes per counter changed per interval, drained every cycle; nothing is marked when the snapshot is disabled). The cycle renames it to `quotes:dirty:saving` (recovered after a crash), with no keyspace `SCAN` and no read back of the backup, yielding to the event loop between batches; `updatedAt` changes only with the value or a flag (update pipeline, MongoDB 4.2+), so unchanged counters cost no write. A full `SCAN` reconciliation runs every `QUOTA_FULL_SCAN_INTERVAL_HOURS` (default 168, 0 disables) and catches missed marks (failed `MULTI`, `allkeys-*` eviction, old pods during a rolling deploy); with it disabled a missed mark waits for the counter's next marked increment. Not Redis Cluster ready (no hash tag)
48
+ - `quota-reset` waits for a running snapshot up to its lock TTL, printing progress
49
+ - A counter missing in Redis is restored from `quota_usages` (`SET NX`) before it is read or incremented; checked once per key per process every `QUOTA_RESTORE_MEMO_TTL_SECONDS` (default 600). If mongo is unreachable the counter starts from zero as before (no lookup for 60 s). chatbots/kbs are not counted in Redis and are skipped
50
+ - Wipe detection: a timer checks the sentinel `quotes:sentinel` at boot and every `QUOTA_SENTINEL_CHECK_SECONDS` (default 15, 0 disables). When it is missing one replica (lock `quotes:restore:lock`) restores the current period (a counter lower than its snapshot becomes snapshot + the usage counted after the wipe) and sets the sentinel again. It assumes a clean wipe: a Redis restored from an older dump with a lost sentinel, or a counter lowered by hand, can be double counted; a rollback that keeps the sentinel is not detected
51
+ - The threshold e-mail flags (`:notify:<pct>`) are backed up with their expiry and restored with their remaining TTL, so the e-mails are not sent again after a wipe; `invalidateCheckpointKeys` clears them in the backup too
52
+ - New command `npm run quota-restore -- [--project <id>] [--period current|all] [--dry-run] [--force | --merge]` to restore the counters after a known wipe
53
+ - Deleting or lowering a quota key in Redis no longer resets the counter: use the new command `npm run quota-reset -- --project <id> --type <type> [--period current|M/D/YYYY] [--value N] [--dry-run]`, which sets Redis and the backup and clears the notify flags
54
+
8
55
  # 3.0.1
9
56
  - Introduced the agent prompts service (`/:projectid/agent-prompts`), restricted to project admins. This service stores prompts used to create AI agents, including metadata such as the agent, the author (from the token), the source and context within the dashboard or external pages, and attachment links. It also provides an endpoint to list all prompts for a project, with optional filtering by agent, ordered from newest to oldest.
10
57
 
package/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM node:18-bookworm
1
+ FROM node:24-bookworm
2
2
 
3
3
  RUN apt-get update
4
4
 
@@ -18,7 +18,8 @@ RUN if [ "$NPM_TOKEN" ]; \
18
18
  # where available (npm@5+)
19
19
  COPY package*.json ./
20
20
 
21
- RUN npm install --production
21
+ #RUN npm install --production
22
+ RUN npm ci --omit=dev
22
23
 
23
24
  RUN rm -f .npmrc
24
25
 
package/Dockerfile-en CHANGED
@@ -1,4 +1,4 @@
1
- FROM node:18-bookworm
1
+ FROM node:24-bookworm
2
2
 
3
3
  RUN apt-get update
4
4
 
@@ -21,7 +21,8 @@ ENV VOICE_TWILIO_TOKEN=${VOICE_TWILIO_TOKEN}
21
21
  # where available (npm@5+)
22
22
  COPY package*.json ./
23
23
 
24
- RUN npm install --production
24
+ #RUN npm install --production
25
+ RUN npm ci --omit=dev
25
26
 
26
27
  RUN rm -f .npmrc
27
28
 
package/Dockerfile-jobs CHANGED
@@ -1,4 +1,4 @@
1
- FROM node:18-bookworm
1
+ FROM node:24-bookworm
2
2
 
3
3
  RUN apt-get update
4
4
 
@@ -18,7 +18,8 @@ RUN if [ "$NPM_TOKEN" ]; \
18
18
  # where available (npm@5+)
19
19
  COPY package*.json ./
20
20
 
21
- RUN npm install --production
21
+ #RUN npm install --production
22
+ RUN npm ci --omit=dev
22
23
 
23
24
  RUN rm -f .npmrc
24
25
 
@@ -1,4 +1,4 @@
1
- FROM node:18-bookworm
1
+ FROM node:24-bookworm
2
2
 
3
3
  RUN apt-get update
4
4
 
@@ -18,7 +18,8 @@ RUN if [ "$NPM_TOKEN" ]; \
18
18
  # where available (npm@5+)
19
19
  COPY package*.json ./
20
20
 
21
- RUN npm install --production
21
+ #RUN npm install --production
22
+ RUN npm ci --omit=dev
22
23
 
23
24
  RUN rm -f .npmrc
24
25
 
package/README.md CHANGED
@@ -102,6 +102,21 @@ See the Tiledesk REST API [here](https://developer.tiledesk.com/apis/rest-api/in
102
102
  # Upgrading
103
103
  To see how to upgrade tiledesk-server see [here](./docs/upgrading.md)
104
104
 
105
+ # Quota usage backup
106
+ Quota usage counters (requests, messages, email, tokens, agent_tokens, voice_duration) live in Redis (`quotes:<type>:<id_project>:<period>`). To survive a Redis wipe they are copied into the MongoDB collection `quota_usages`:
107
+
108
+ - **Dirty set**: an increment that must mark its counter sends `MULTI`, the `INCR`/`INCRBY`/`INCRBYFLOAT`, `SADD quotes:dirty <key>`, `EXEC` in one round trip; setting a threshold e-mail flag marks its counter too. While the watcher runs (see wipe detection), each process remembers the counters it marked (only after `EXEC` confirmed the `SADD`) and then sends just the plain increment, until it sees a new snapshot generation (`quotes:dirty:gen`). The memo is trusted only while the watcher's last generation read is younger than 1.5 x `QUOTA_SENTINEL_CHECK_SECONDS` (it is cleared on a read error): if the watcher stalls, every increment marks again. The set holds the distinct counters changed in one interval (about 90-110 bytes each in Redis) and is drained by every snapshot. With `QUOTA_SNAPSHOT_INTERVAL_MINUTES=0` or `QUOTES_ENABLED=false` nothing is marked and `start()` deletes `quotes:dirty` and `quotes:dirty:saving`.
109
+ - **Snapshot**: every `QUOTA_SNAPSHOT_INTERVAL_MINUTES` (default `60`, `0` disables; the first run is after min(5 minutes, interval) from the start) one server replica, chosen by the Redis lock `quotes:snapshot:lock`, renames `quotes:dirty` to `quotes:dirty:saving` (a saving set left by a crashed cycle is kept and the new changes are merged into it), waits until every process saw the new generation (2 x `QUOTA_SENTINEL_CHECK_SECONDS` + 1 s; 31 s when it is `0` on this replica; all replicas must use the same value), reads only those counters and their notify flags, and upserts them with `$max` on `value`: a lower Redis value (e.g. a wiped Redis not yet restored) never lowers the backup. The upsert is an update pipeline (MongoDB 4.2+): `updatedAt` changes only when the value or a flag expiry grows, so an unchanged counter is a no-op in MongoDB (no write, no oplog entry). The log shows the settle time and the work time separately. Changes made during the cycle go to the new `quotes:dirty`. Only the recent periods are saved (those started in the last 70 days: the current and the previous monthly period). The threshold e-mail flags (`<counter>:notify:<pct>`) are saved too, with their expiry, so the e-mails are not sent again after a restore. No keyspace `SCAN`, no read back of the backup; batches of 500 with a yield to the event loop in between. A failing batch is logged, the next ones are written and `quotes:dirty:saving` is kept for the next cycle. Skipped when `QUOTES_ENABLED=false`.
110
+ - **Full scan**: every `QUOTA_FULL_SCAN_INTERVAL_HOURS` (default `168`, `0` disables; also on the first cycle ever, `quotes:fullscan:last`) the cycle also `SCAN`s the keyspace and saves every counter, to catch the ones changed outside the normal path (manual Redis edits) or whose dirty mark was lost.
111
+ - **Wipe detection**: the sentinel key `quotes:sentinel` exists while Redis holds the counters. In each server process a timer checks it at boot and then every `QUOTA_SENTINEL_CHECK_SECONDS` (default `15`, `0` disables); the request path never checks it. If it is missing Redis was wiped: the process forgets which keys it had verified, one replica (lock `quotes:restore:lock`, 60 s, renewed after every batch) restores the counters of the current period and their notify flags, then sets the sentinel again (warn logs). A counter lower than its snapshot was recreated by usage counted after the wipe, so it becomes snapshot + that usage; a counter equal or higher is left as is.
112
+ - **Restore on miss**: when a counter is read or incremented and its Redis key does not exist, it is seeded from `quota_usages` (`SET NX`; while wipe detection runs, only if the sentinel exists) before the operation, so increments land on top of the restored value. Each key is checked at most once per process every `QUOTA_RESTORE_MEMO_TTL_SECONDS` (default `600`). If MongoDB is unreachable the counter starts from zero as before and no lookup is tried for 60 s.
113
+ - **Restore command**: `npm run quota-restore -- [--project <id>] [--period current|all] [--dry-run] [--force | --merge]`. By default only missing keys are created; `--force` sets each counter to max(Redis value, snapshot); `--merge` applies the wipe rule above. Non expired notify flags are restored too.
114
+ - **Reset command**: deleting or lowering a counter in Redis no longer resets it (the restore on miss and `$max` bring the old value back). Use `npm run quota-reset -- --project <id> --type <type> [--period current|M/D/YYYY] [--value N] [--dry-run]`: it sets the counter in Redis and in `quota_usages` (default `0`, bypassing `$max`) and clears its notify flags in both. If a snapshot is running it waits for it (up to the lock TTL, printing progress).
115
+
116
+ Both commands use the server env (`DATABASE_URI`/`MONGODB_URI`, `CACHE_REDIS_HOST`/`CACHE_REDIS_PORT`/`CACHE_REDIS_PASSWORD`).
117
+
118
+ Limits: usage counted after the last snapshot (at most one interval) is lost on a wipe, unless the counter's dirty mark was missed (a failed `MULTI`, eviction of `quotes:dirty` under an `allkeys-*` policy, pods of an older version during a rolling deploy, which do not mark, or a stalled watcher on a mismatched config): then the change is saved at the counter's next marked increment or by the next full scan, and never if the full scan is disabled. `noeviction` and `volatile-*` policies are safe (the set has no TTL). During a rolling deploy the full scan on the first cycle of the new version (and the weekly one) reconciles what old pods counted. The snapshot uses multi-key commands and Lua scripts on `quotes:*` keys that do not share a hash tag: on a Redis Cluster the keys would need a common hash tag. The wipe restore assumes a clean wipe (empty Redis): a Redis restored from an older RDB/AOF together with a lost sentinel, or a counter lowered by hand, can be double counted; a rollback to an older RDB/AOF that keeps the sentinel is not detected (the usage since that dump is under counted).
119
+
105
120
  # Testing
106
121
  Run unit test with `npm test` and integration test with `npm run test:int`
107
122
 
package/app.js CHANGED
@@ -230,6 +230,11 @@ var IPFilter = require('./middleware/ipFilter');
230
230
  // job_here
231
231
  var BanUserNotifier = require('./services/banUserNotifier');
232
232
  BanUserNotifier.listen();
233
+ // HITL: in queue mode with a job worker it runs in jobs.js, where the *.queue events are consumed
234
+ jobsManager.listenHitlParticipants(require('./services/hitlParticipantListener'));
235
+ var startPointCleanupListener = require('./services/startPointCleanupListener');
236
+ startPointCleanupListener.listen();
237
+ require('./services/scheduledStartPublishListener').listen();
233
238
  const { ChatbotService } = require('./services/chatbotService');
234
239
  const { QuoteManager } = require('./services/QuoteManager');
235
240
  const RateManager = require('./services/RateManager');
@@ -237,6 +242,12 @@ const RateManager = require('./services/RateManager');
237
242
  let qm = new QuoteManager({ tdCache: tdCache });
238
243
  qm.start();
239
244
 
245
+ // Periodic backup of the Redis quota counters into mongo (quota_usages), see services/quotaSnapshotService.js
246
+ if (process.env.NODE_ENV !== 'test') {
247
+ const quotaSnapshotService = require('./services/quotaSnapshotService');
248
+ quotaSnapshotService.start({ tdCache: tdCache });
249
+ }
250
+
240
251
  let rm = new RateManager({ tdCache: tdCache });
241
252
 
242
253
 
@@ -385,7 +396,7 @@ if (process.env.DISABLE_SESSION_STRATEGY==true || process.env.DISABLE_SESSION_S
385
396
 
386
397
 
387
398
  } else {
388
- app.use(session({ secret: sessionSecret}));
399
+ app.use(session({ secret: sessionSecret, resave: false, saveUninitialized: false }));
389
400
  winston.info("Express Session enabled with Secret: " + sessionSecret);
390
401
 
391
402
  }
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Sets a quota counter (default to 0) in Redis AND in the quota_usages backup, and clears its notify flags
5
+ * in both. Deleting or lowering the Redis key by hand does not reset a counter any more: the restore on
6
+ * miss and the snapshot ($max) would bring the old value back.
7
+ *
8
+ * Usage: npm run quota-reset -- --project <id> --type <type> [--period current|M/D/YYYY] [--value N] [--dry-run]
9
+ *
10
+ * Uses the server configuration (see utils/quotaCliUtil.js).
11
+ */
12
+
13
+ const { connect } = require('../utils/quotaCliUtil');
14
+
15
+ function usage(message) {
16
+ if (message) {
17
+ console.error(message);
18
+ }
19
+ console.error("Usage: quota-reset --project <id> --type <type> [--period current|M/D/YYYY] [--value N] [--dry-run]");
20
+ process.exit(message ? 1 : 0);
21
+ }
22
+
23
+ function parseArgs(argv) {
24
+ const options = { period: 'current', value: 0, dryRun: false };
25
+ for (let i = 0; i < argv.length; i++) {
26
+ const arg = argv[i];
27
+ if (arg === '--project') {
28
+ options.project = argv[++i];
29
+ } else if (arg === '--type') {
30
+ options.type = argv[++i];
31
+ } else if (arg === '--period') {
32
+ options.period = argv[++i];
33
+ } else if (arg === '--value') {
34
+ options.value = Number(argv[++i]);
35
+ if (!Number.isFinite(options.value) || options.value < 0) usage('--value must be a number >= 0');
36
+ } else if (arg === '--dry-run') {
37
+ options.dryRun = true;
38
+ } else if (arg === '--help' || arg === '-h') {
39
+ usage();
40
+ } else {
41
+ usage('Unknown argument: ' + arg);
42
+ }
43
+ }
44
+ if (!options.project || !options.type || !options.period) usage('--project and --type are mandatory');
45
+ return options;
46
+ }
47
+
48
+ async function main() {
49
+ const options = parseArgs(process.argv.slice(2));
50
+ const { tdCache, close } = await connect();
51
+ // required after the env is loaded
52
+ const quotaSnapshotService = require('../services/quotaSnapshotService');
53
+
54
+ console.log("quota-reset " + JSON.stringify(options));
55
+ try {
56
+ const r = await quotaSnapshotService.resetCounter(Object.assign({ tdCache: tdCache, onWait: (message) => console.log(message) }, options));
57
+ console.log((r.dryRun ? "would reset " : "reset ") + r.key + " to " + r.value +
58
+ " (redis was " + (r.redis === null ? '(missing)' : r.redis) + ", snapshot was " + (r.snapshot === null ? '(none)' : r.snapshot) +
59
+ ", notify flags cleared: " + r.notifyFlags + ")");
60
+ } finally {
61
+ await close();
62
+ }
63
+ }
64
+
65
+ main().catch((err) => {
66
+ console.error("quota-reset failed:", err.message || err);
67
+ process.exit(1);
68
+ });
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Restores the Redis quota counters from the quota_usages snapshots (e.g. after a Redis wipe).
5
+ *
6
+ * Usage: npm run quota-restore -- [--project <id>] [--period current|all] [--dry-run] [--force | --merge]
7
+ *
8
+ * --project <id> only this project
9
+ * --period current only the period that contains now, per project (default)
10
+ * --period all every snapshotted period
11
+ * --dry-run print what would be done, change nothing
12
+ * --force set each counter to max(redis value, snapshot) instead of only creating missing keys (SET NX)
13
+ * --merge after a wipe the server already counted on: missing -> snapshot, lower than the snapshot ->
14
+ * snapshot + redis value (what the server does by itself when it detects the wipe)
15
+ * Non expired ":notify:<pct>" flags are restored too (SET NX with their remaining TTL).
16
+ *
17
+ * Uses the server configuration: DATABASE_URI / MONGODB_URI (config/database.js, NODE_ENV=test -> test db),
18
+ * CACHE_REDIS_HOST / CACHE_REDIS_PORT / CACHE_REDIS_PASSWORD, DOTENV_PATH / LOAD_DOTENV_SUBFOLDER.
19
+ */
20
+
21
+ const { connect } = require('../utils/quotaCliUtil');
22
+
23
+ function usage(message) {
24
+ if (message) {
25
+ console.error(message);
26
+ }
27
+ console.error("Usage: quota-restore [--project <id>] [--period current|all] [--dry-run] [--force | --merge]");
28
+ process.exit(message ? 1 : 0);
29
+ }
30
+
31
+ function parseArgs(argv) {
32
+ const options = { period: 'current', dryRun: false, force: false };
33
+ for (let i = 0; i < argv.length; i++) {
34
+ const arg = argv[i];
35
+ if (arg === '--project') {
36
+ options.project = argv[++i];
37
+ if (!options.project) usage('--project needs a value');
38
+ } else if (arg === '--period') {
39
+ options.period = argv[++i];
40
+ if (options.period !== 'current' && options.period !== 'all') usage('--period must be current or all');
41
+ } else if (arg === '--dry-run') {
42
+ options.dryRun = true;
43
+ } else if (arg === '--force') {
44
+ options.force = true;
45
+ } else if (arg === '--merge') {
46
+ options.merge = true;
47
+ } else if (arg === '--help' || arg === '-h') {
48
+ usage();
49
+ } else {
50
+ usage('Unknown argument: ' + arg);
51
+ }
52
+ }
53
+ if (options.force && options.merge) usage('--force and --merge are alternatives');
54
+ return options;
55
+ }
56
+
57
+ async function main() {
58
+ const options = parseArgs(process.argv.slice(2));
59
+
60
+ const { tdCache, close } = await connect();
61
+
62
+ // required after the env is loaded
63
+ const quotaSnapshotService = require('../services/quotaSnapshotService');
64
+
65
+ console.log("quota-restore " + JSON.stringify(options));
66
+ try {
67
+ const { results, summary } = await quotaSnapshotService.restoreFromSnapshots({
68
+ tdCache: tdCache,
69
+ project: options.project,
70
+ period: options.period,
71
+ dryRun: options.dryRun,
72
+ mode: options.merge ? 'merge' : (options.force ? 'max' : 'nx')
73
+ });
74
+ results.forEach((r) => {
75
+ console.log(r.action + " " + r.key + " snapshot=" + r.snapshot + " redis=" + (r.redis === null || r.redis === undefined ? '(missing)' : r.redis));
76
+ });
77
+ console.log("summary " + JSON.stringify(summary) + (options.dryRun ? " (dry run, nothing changed)" : ""));
78
+ } finally {
79
+ await close();
80
+ }
81
+ }
82
+
83
+ main().catch((err) => {
84
+ console.error("quota-restore failed:", err);
85
+ process.exit(1);
86
+ });
@@ -15,6 +15,7 @@ const Request = require("../../models/request");
15
15
  const chat21Config = require('./chat21Config');
16
16
  const chat21 = require('./chat21Client');
17
17
  const chat21SyncTracer = require('../../services/chat21SyncTracer');
18
+ const chat21JoinTracker = require('./chat21JoinTracker');
18
19
 
19
20
  const MaskData = require("maskdata");
20
21
 
@@ -176,6 +177,54 @@ class Chat21Handler {
176
177
  }
177
178
 
178
179
 
180
+ /**
181
+ * Joins the new member to the chat21 group of the request. The join promise is registered in
182
+ * chat21JoinTracker (synchronously, while request.participants.join is being emitted) so a caller
183
+ * that added the participant can wait for the member to be in the group (see participantInviteService).
184
+ */
185
+ onParticipantsJoin(data) {
186
+ const traceEvent = 'request.participants.join';
187
+ winston.debug("[Chat21Handler] - request.participants.join - Request participants join: ", data);
188
+
189
+ let request = data.request;
190
+ let member = data.member;
191
+ chat21SyncTracer.trace(traceEvent, request, 'handler.received', 'info', { member: member, ...chat21SyncTracer.describeRequest(request) });
192
+
193
+ if (!request.channelOutbound || request.channelOutbound.name !== ChannelConstants.CHAT21) {
194
+ winston.error("[Chat21Handler] - request.participants.join - Request is not a chat21 request: ", request);
195
+ chat21SyncTracer.trace(traceEvent, request, 'handler.skipped', 'skipped', { channelOutbound: request.channelOutbound?.name });
196
+ return;
197
+ }
198
+
199
+ const groupId = request.request_id;
200
+
201
+ // deferred like before (the tracker runs the join on setImmediate); the returned promise never rejects
202
+ return chat21JoinTracker.track(groupId, member, function () {
203
+ try {
204
+ chat21.auth.setAdminToken(adminToken);
205
+
206
+ winston.verbose("[Chat21Handler] - request.participants.join - Joining member " + member + " for chat21 group with request : " + groupId);
207
+
208
+ return enqueueGroupSync(groupId, () => chat21SyncTracer.traceCall(traceEvent, request, 'group.join', { member: member }, () => {
209
+ chat21.auth.setAdminToken(adminToken);
210
+ return chat21.groups.join(member, groupId);
211
+ }, chat21Outcome)).then(function (data) {
212
+ winston.verbose("[Chat21Handler] - request.participants.join - Chat21 group joined: " + JSON.stringify(data));
213
+ chat21Event.emit('group.join', data);
214
+ return data;
215
+ }).catch(function (err) {
216
+ winston.error("[Chat21Handler] - request.participants.join - Error joining chat21 group: ", err);
217
+ chat21Event.emit('group.join.error', err);
218
+ throw err;
219
+ });
220
+ } catch (err) {
221
+ chat21SyncTracer.trace(traceEvent, request, 'handler.exception', 'error', { error: chat21SyncTracer.describeError(err) });
222
+ throw err;
223
+ }
224
+ });
225
+ }
226
+
227
+
179
228
  listen() {
180
229
 
181
230
  winston.debug("Chat21Handler listener start ");
@@ -615,40 +664,7 @@ class Chat21Handler {
615
664
  }));
616
665
  });
617
666
 
618
- requestEvent.on('request.participants.join', function (data) {
619
- const traceEvent = 'request.participants.join';
620
- winston.debug("[Chat21Handler] - request.participants.join - Request participants join: ", data);
621
-
622
- let request = data.request;
623
- let member = data.member;
624
- chat21SyncTracer.trace(traceEvent, request, 'handler.received', 'info', { member: member, ...chat21SyncTracer.describeRequest(request) });
625
-
626
- setImmediate(() => runTraced(traceEvent, request, () => {
627
-
628
- if (request.channelOutbound.name !== ChannelConstants.CHAT21) {
629
- winston.error("[Chat21Handler] - request.participants.join - Request is not a chat21 request: ", request);
630
- chat21SyncTracer.trace(traceEvent, request, 'handler.skipped', 'skipped', { channelOutbound: request.channelOutbound.name });
631
- return;
632
- }
633
-
634
- chat21.auth.setAdminToken(adminToken);
635
-
636
- const groupId = request.request_id;
637
- winston.verbose("[Chat21Handler] - request.participants.join - Joining member " + member + " for chat21 group with request : " + groupId);
638
-
639
- enqueueGroupSync(groupId, () => chat21SyncTracer.traceCall(traceEvent, request, 'group.join', { member: member }, () => {
640
- chat21.auth.setAdminToken(adminToken);
641
- return chat21.groups.join(member, groupId);
642
- }, chat21Outcome)).then(function (data) {
643
- winston.verbose("[Chat21Handler] - request.participants.join - Chat21 group joined: " + JSON.stringify(data));
644
- chat21Event.emit('group.join', data);
645
- }).catch(function (err) {
646
- winston.error("[Chat21Handler] - request.participants.join - Error joining chat21 group: ", err);
647
- chat21Event.emit('group.join.error', err);
648
- });
649
-
650
- }));
651
- });
667
+ requestEvent.on('request.participants.join', (data) => this.onParticipantsJoin(data));
652
668
 
653
669
  requestEvent.on('request.participants.leave', function (data) {
654
670
  const traceEvent = 'request.participants.leave';
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Pending chat21 group joins, keyed by group (request_id) and member.
3
+ *
4
+ * chat21Handler registers the join synchronously while request.participants.join is emitted, so a
5
+ * caller that has just added a participant (e.g. the HITL invite route) can wait for the member to
6
+ * actually be in the chat21 group before answering. Entries are removed when the join settles.
7
+ * The existing group.join / group.join.error events are unchanged.
8
+ */
9
+ class Chat21JoinTracker {
10
+
11
+ constructor() {
12
+ this.pending = new Map();
13
+ }
14
+
15
+ key(groupId, member) {
16
+ return String(groupId) + '|' + String(member);
17
+ }
18
+
19
+ /** `join` is a function returning the join promise; it runs later (the handler defers it). */
20
+ track(groupId, member, join) {
21
+ const key = this.key(groupId, member);
22
+ const settled = new Promise((resolve) => setImmediate(resolve))
23
+ .then(() => join())
24
+ .then(
25
+ (data) => ({ member: member, groupId: groupId, ok: true, data: data }),
26
+ (err) => ({ member: member, groupId: groupId, ok: false, error: err })
27
+ );
28
+ this.pending.set(key, settled);
29
+ settled.then(() => {
30
+ if (this.pending.get(key) === settled) {
31
+ this.pending.delete(key);
32
+ }
33
+ });
34
+ return settled;
35
+ }
36
+
37
+ /** The settle promise (never rejects) of the pending join, or undefined when none is pending. */
38
+ get(groupId, member) {
39
+ return this.pending.get(this.key(groupId, member));
40
+ }
41
+ }
42
+
43
+ module.exports = new Chat21JoinTracker();
@@ -1,7 +1,7 @@
1
1
  module.exports = {
2
- secret:'nodeauthsecret',
3
- schemaVersion: 2111,
4
- database: 'mongodb://localhost:27017/tiledesk',
5
- databaselogs: 'mongodb://localhost:27017/tiledesk-logs',
6
- databasetest: 'mongodb://localhost:27017/tiledesk-test'
2
+ secret: 'nodeauthsecret',
3
+ schemaVersion: 2111,
4
+ database: 'mongodb://127.0.0.1:27017/tiledesk',
5
+ databaselogs: 'mongodb://127.0.0.1:27017/tiledesk-logs',
6
+ databasetest: 'mongodb://127.0.0.1:27017/tiledesk-test'
7
7
  };
@@ -19,6 +19,7 @@
19
19
  "id": "tiledesk-docsparse",
20
20
  "name": "Tiledesk Docsparse",
21
21
  "url": "https://stage.eks.tiledesk.com/mcp-tools/docsparse/mcp",
22
+ "native": true,
22
23
  "transport": "streamable_http",
23
24
  "description": "Tiledesk Docsparse is a MCP server that extracts the text of documents (PDF, Word, Excel, PowerPoint, CSV, email) from a URL, with OCR for scanned pages."
24
25
  }