From 280abe01a9e9d74e5d85dd7c1c288cb396583715 Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:03:08 +0000 Subject: [PATCH 01/16] support bot: build and run from source in Docker --- apps/simplex-support-bot/package-lock.json | 149 ++++---------------- apps/simplex-support-bot/package.json | 7 +- scripts/support-bot/.gitignore | 2 + scripts/support-bot/Dockerfile | 122 ++++++++++++++++ scripts/support-bot/Dockerfile.dockerignore | 22 +++ scripts/support-bot/compose.yml | 33 +++++ scripts/support-bot/config/.gitkeep | 0 7 files changed, 207 insertions(+), 128 deletions(-) create mode 100644 scripts/support-bot/.gitignore create mode 100644 scripts/support-bot/Dockerfile create mode 100644 scripts/support-bot/Dockerfile.dockerignore create mode 100644 scripts/support-bot/compose.yml create mode 100644 scripts/support-bot/config/.gitkeep diff --git a/apps/simplex-support-bot/package-lock.json b/apps/simplex-support-bot/package-lock.json index eddbcb2dff..1dc31adaa2 100644 --- a/apps/simplex-support-bot/package-lock.json +++ b/apps/simplex-support-bot/package-lock.json @@ -9,10 +9,10 @@ "version": "0.1.0", "license": "AGPL-3.0", "dependencies": { - "@simplex-chat/types": "^0.6.0", + "@simplex-chat/types": "^7.1.0-beta.9.1", "async-mutex": "^0.5.0", "commander": "^14.0.3", - "simplex-chat": "^6.5.1", + "simplex-chat": "^7.1.0-beta.10.2", "yaml": "^2.8.4" }, "devDependencies": { @@ -783,9 +783,9 @@ ] }, "node_modules/@simplex-chat/types": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/@simplex-chat/types/-/types-0.6.0.tgz", - "integrity": "sha512-QVYvaRsS6TnS+IROjNkekZYvhvy8QoA8vKCuGe9E6lplXDVutJo9tdDOSWS9NDdtwxT1wRZ29zN4xEZEEG/NHw==", + "version": "7.1.0-beta.9.1", + "resolved": "https://registry.npmjs.org/@simplex-chat/types/-/types-7.1.0-beta.9.1.tgz", + "integrity": "sha512-bK/x3D1mX9xDH8i2bl6mCBSBUTim52IYvq54FSLMjJ6ttLnSyrWskJKGF7xRTOCotiFI6+Gz6JmV7ekFaEi/fw==", "license": "AGPL-3.0", "dependencies": { "typescript": "^5.9.2" @@ -809,22 +809,12 @@ "version": "22.19.17", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.17.tgz", "integrity": "sha512-wGdMcf+vPYM6jikpS/qhg6WiqSV/OhG+jeeHT/KlVqxYfD40iYJf9/AE1uQxVWFvU7MipKRkRv8NSHiCGgPr8Q==", - "devOptional": true, + "dev": true, "license": "MIT", "dependencies": { "undici-types": "~6.21.0" } }, - "node_modules/@types/yauzl": { - "version": "2.10.3", - "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.10.3.tgz", - "integrity": "sha512-oJoftv0LSuaDZE3Le4DbKX+KS9G36NzOeSap90UIK0yMA/NhKJhqlSGtNDORNRaIbQfzjXDrQa0ytJ6mNRGz/Q==", - "license": "MIT", - "optional": true, - "dependencies": { - "@types/node": "*" - } - }, "node_modules/@vitest/expect": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-1.6.1.tgz", @@ -957,15 +947,6 @@ "tslib": "^2.4.0" } }, - "node_modules/buffer-crc32": { - "version": "0.2.13", - "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", - "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", - "license": "MIT", - "engines": { - "node": "*" - } - }, "node_modules/cac": { "version": "6.7.14", "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz", @@ -1043,6 +1024,7 @@ "version": "4.4.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "dev": true, "license": "MIT", "dependencies": { "ms": "^2.1.3" @@ -1079,15 +1061,6 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, - "node_modules/end-of-stream": { - "version": "1.4.5", - "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz", - "integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==", - "license": "MIT", - "dependencies": { - "once": "^1.4.0" - } - }, "node_modules/esbuild": { "version": "0.21.5", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.21.5.tgz", @@ -1174,41 +1147,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/extract-zip": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/extract-zip/-/extract-zip-2.0.1.tgz", - "integrity": "sha512-GDhU9ntwuKyGXdZBUgTIe+vXnWj0fppUEtMDL0+idd5Sta8TGpHssn/eusA9mrPr9qNDym6SxAYZjNvCn/9RBg==", - "license": "BSD-2-Clause", - "dependencies": { - "debug": "^4.1.1", - "get-stream": "^5.1.0", - "yauzl": "^2.10.0" - }, - "bin": { - "extract-zip": "cli.js" - }, - "engines": { - "node": ">= 10.17.0" - }, - "optionalDependencies": { - "@types/yauzl": "^2.9.1" - } - }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", "license": "MIT" }, - "node_modules/fd-slicer": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", - "integrity": "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==", - "license": "MIT", - "dependencies": { - "pend": "~1.2.0" - } - }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -1234,21 +1178,6 @@ "node": "*" } }, - "node_modules/get-stream": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.2.0.tgz", - "integrity": "sha512-nBF+F1rAZVCu/p7rjzgA+Yb4lfYXrpl7a6VmJrU8wF9I1CKvP/QwPNZHnOlwbTkY6dvtFIzFMSyQXbLoTQPRpA==", - "license": "MIT", - "dependencies": { - "pump": "^3.0.0" - }, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/human-signals": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-5.0.0.tgz", @@ -1367,6 +1296,7 @@ "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true, "license": "MIT" }, "node_modules/nanoid": { @@ -1388,15 +1318,6 @@ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" } }, - "node_modules/node-addon-api": { - "version": "8.7.0", - "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-8.7.0.tgz", - "integrity": "sha512-9MdFxmkKaOYVTV+XVRG8ArDwwQ77XIgIPyKASB1k3JPq3M8fGQQQE3YpMOrKm6g//Ktx8ivZr8xo1Qmtqub+GA==", - "license": "MIT", - "engines": { - "node": "^18 || ^20 || >= 21" - } - }, "node_modules/npm-run-path": { "version": "5.3.0", "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-5.3.0.tgz", @@ -1426,15 +1347,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/once": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", - "license": "ISC", - "dependencies": { - "wrappy": "1" - } - }, "node_modules/onetime": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/onetime/-/onetime-6.0.0.tgz", @@ -1570,16 +1482,6 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, - "node_modules/pump": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.4.tgz", - "integrity": "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==", - "license": "MIT", - "dependencies": { - "end-of-stream": "^1.1.0", - "once": "^1.3.1" - } - }, "node_modules/react-is": { "version": "18.3.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz", @@ -1676,16 +1578,17 @@ } }, "node_modules/simplex-chat": { - "version": "6.5.1", - "resolved": "https://registry.npmjs.org/simplex-chat/-/simplex-chat-6.5.1.tgz", - "integrity": "sha512-1cv91iMCqtP+9R1PQM3NKazocoUInKT2/06pIOuzORD5/VzulR6cMZnEQmaT02Jz+8FVkEKTsaAIJfVP6/tJmw==", - "hasInstallScript": true, + "version": "7.1.0-beta.10.2", + "resolved": "https://registry.npmjs.org/simplex-chat/-/simplex-chat-7.1.0-beta.10.2.tgz", + "integrity": "sha512-P3ONgfa7msuLC0/eK/Id0AUhnB7v7jzhOXgawgWDYHHqg/ZvYQPOrT0Y7wT3c2JNReg6kAdsn7YoioEk24SY3A==", "license": "AGPL-3.0", "dependencies": { - "@simplex-chat/types": "^0.6.0", - "extract-zip": "^2.0.1", + "@simplex-chat/types": "^7.1.0-beta.9.1", "fast-deep-equal": "^3.1.3", - "node-addon-api": "^8.5.0" + "yauzl": "^3.4.0" + }, + "bin": { + "simplex-chat": "dist/cli.js" } }, "node_modules/source-map-js": { @@ -1805,7 +1708,7 @@ "version": "6.21.0", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", - "devOptional": true, + "dev": true, "license": "MIT" }, "node_modules/vite": { @@ -1990,12 +1893,6 @@ "node": ">=8" } }, - "node_modules/wrappy": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", - "license": "ISC" - }, "node_modules/yaml": { "version": "2.8.4", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.4.tgz", @@ -2012,13 +1909,15 @@ } }, "node_modules/yauzl": { - "version": "2.10.0", - "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", - "integrity": "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==", + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-3.4.0.tgz", + "integrity": "sha512-jIH9yLR9wqr0wOS0TpBvo/g/2UgZH5qePVbjgRliiF0BYvOZyaBknKsF+x9Iht0O6sqgnB93rCICdOZFecJuDw==", "license": "MIT", "dependencies": { - "buffer-crc32": "~0.2.3", - "fd-slicer": "~1.1.0" + "pend": "~1.2.0" + }, + "engines": { + "node": ">=12" } }, "node_modules/yocto-queue": { diff --git a/apps/simplex-support-bot/package.json b/apps/simplex-support-bot/package.json index 97caee2278..790a756327 100644 --- a/apps/simplex-support-bot/package.json +++ b/apps/simplex-support-bot/package.json @@ -5,13 +5,14 @@ "main": "dist/index.js", "scripts": { "build": "tsc", - "start": "node dist/index.js" + "start": "node dist/index.js", + "test": "vitest run" }, "dependencies": { - "@simplex-chat/types": "^0.7.0", + "@simplex-chat/types": "^7.1.0-beta.9.1", "async-mutex": "^0.5.0", "commander": "^14.0.3", - "simplex-chat": "^6.5.2", + "simplex-chat": "^7.1.0-beta.10.2", "yaml": "^2.8.4" }, "devDependencies": { diff --git a/scripts/support-bot/.gitignore b/scripts/support-bot/.gitignore new file mode 100644 index 0000000000..0c54f604fc --- /dev/null +++ b/scripts/support-bot/.gitignore @@ -0,0 +1,2 @@ +data/ +.env diff --git a/scripts/support-bot/Dockerfile b/scripts/support-bot/Dockerfile new file mode 100644 index 0000000000..544a8be05e --- /dev/null +++ b/scripts/support-bot/Dockerfile @@ -0,0 +1,122 @@ +# syntax=docker/dockerfile:1 + +# The runtime glibc must be the same or newer. +ARG UBUNTU_LIBS=22.04 +ARG GHC=9.6.3 +ARG CABAL=3.10.2.0 +ARG NODE=24 + +# --------------------------------------------------------------------------- # +# libsimplex +# --------------------------------------------------------------------------- # +FROM ubuntu:${UBUNTU_LIBS} AS libsimplex + +ARG GHC +ARG CABAL +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential ca-certificates curl git libgmp3-dev libnuma-dev \ + libpq-dev libsqlite3-dev libssl-dev llvm patchelf pkg-config zlib1g-dev && \ + rm -rf /var/lib/apt/lists/* + +ENV BOOTSTRAP_HASKELL_NONINTERACTIVE=1 \ + BOOTSTRAP_HASKELL_GHC_VERSION=${GHC} \ + BOOTSTRAP_HASKELL_CABAL_VERSION=${CABAL} \ + BOOTSTRAP_HASKELL_INSTALL_NO_STACK=true \ + BOOTSTRAP_HASKELL_INSTALL_NO_STACK_HOOK=true +RUN curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh +ENV PATH="/root/.ghcup/bin:/root/.cabal/bin:$PATH" +ENV CABAL_DIR=/root/.cabal + +WORKDIR /src +COPY cabal.project simplex-chat.cabal README.md PRIVACY.md ./ +COPY scripts/cabal.project.local.linux ./cabal.project.local +# The build script checks the export lists in these. +COPY libsimplex.dll.def flake.nix ./ +COPY scripts/desktop ./scripts/desktop +COPY src ./src + +# Other versions' build trees are removed: the script's simplex-chat-* glob +# fails with "cd: too many arguments" when the cache holds more than one. +RUN --mount=type=cache,target=/root/.cabal \ + --mount=type=cache,target=/src/dist-newstyle \ + set -eu; \ + ver=$(sed -n 's/^version: *//p' simplex-chat.cabal | tr -d '[:space:]'); \ + build_dir=dist-newstyle/build/$(uname -m)-linux/ghc-${GHC}/simplex-chat-$ver/build; \ + find dist-newstyle/build -maxdepth 3 -type d -name 'simplex-chat-*' \ + ! -path "*/simplex-chat-$ver" -exec rm -rf {} + 2>/dev/null || true; \ + cabal update; \ + SKIP_VLC=1 scripts/desktop/build-lib-linux.sh postgres; \ + mkdir -p /libs; \ + cp "$build_dir"/libsimplex.so "$build_dir"/deps/* /libs/ + +# --------------------------------------------------------------------------- # +# build +# --------------------------------------------------------------------------- # +FROM node:${NODE}-bookworm-slim AS build + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential python3 && \ + rm -rf /var/lib/apt/lists/* + +COPY --from=libsimplex /libs /opt/simplex/libs +# The library loads libsimplex and the add-on built below instead of downloading the released ones. +ENV SIMPLEX_LIBS_DIR=/opt/simplex/libs \ + SIMPLEX_ADDON_PATH=/src/packages/simplex-chat-nodejs/build/Release/simplex.node + +WORKDIR /src +COPY packages/simplex-chat-client/types/typescript packages/simplex-chat-client/types/typescript +RUN cd packages/simplex-chat-client/types/typescript && \ + npm install --no-audit --no-fund && \ + npx tsc + +COPY packages/simplex-chat-nodejs packages/simplex-chat-nodejs +RUN cd packages/simplex-chat-nodejs && \ + npm install --no-audit --no-fund && \ + npm install --no-save --no-audit --no-fund ../simplex-chat-client/types/typescript && \ + npx node-gyp rebuild && \ + npx tsc && \ + cp src/simplex.* dist/ + +WORKDIR /src/apps/simplex-support-bot +COPY apps/simplex-support-bot/package.json apps/simplex-support-bot/package-lock.json ./ +RUN npm ci --no-audit --no-fund +RUN npm install --no-save --no-audit --no-fund \ + /src/packages/simplex-chat-nodejs \ + /src/packages/simplex-chat-client/types/typescript + +COPY apps/simplex-support-bot ./ +RUN npm run build && npm test + +# --------------------------------------------------------------------------- # +# runtime +# --------------------------------------------------------------------------- # +FROM node:${NODE}-bookworm-slim AS runtime + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install -y --no-install-recommends \ + ca-certificates dumb-init libffi8 libgmp10 libnuma1 libpq5 libssl3 zlib1g && \ + rm -rf /var/lib/apt/lists/* + +# A named volume takes this ownership; a bind mount must be owned by uid 1000. +RUN mkdir -p /data && chown node:node /data && chmod 0700 /data + +COPY --from=build /opt/simplex/libs /opt/simplex/libs +COPY --from=build --chown=node:node /src /src +ENV SIMPLEX_LIBS_DIR=/opt/simplex/libs \ + SIMPLEX_ADDON_PATH=/src/packages/simplex-chat-nodejs/build/Release/simplex.node + +USER node +WORKDIR /src/apps/simplex-support-bot + +ENV NODE_ENV=production +# Single API responses grow with the number of chats. +ENV NODE_OPTIONS=--max-old-space-size=8192 + +# node as PID 1 ignores SIGTERM, and the bot deletes the team group invite link on SIGTERM. +ENTRYPOINT ["dumb-init", "--", "node", "dist/index.js"] +CMD ["--team-group", "Support Team", "--state-file", "/data/state.json"] diff --git a/scripts/support-bot/Dockerfile.dockerignore b/scripts/support-bot/Dockerfile.dockerignore new file mode 100644 index 0000000000..f38eabc36f --- /dev/null +++ b/scripts/support-bot/Dockerfile.dockerignore @@ -0,0 +1,22 @@ +* + +!cabal.project +!simplex-chat.cabal +!README.md +!PRIVACY.md +!libsimplex.dll.def +!flake.nix +!src +!scripts/cabal.project.local.linux +!scripts/desktop +!packages/simplex-chat-client +!packages/simplex-chat-nodejs +!apps/simplex-support-bot + +**/node_modules +**/dist +**/build +**/libs +**/data +**/.env +**/.npmrc diff --git a/scripts/support-bot/compose.yml b/scripts/support-bot/compose.yml new file mode 100644 index 0000000000..274b5c10eb --- /dev/null +++ b/scripts/support-bot/compose.yml @@ -0,0 +1,33 @@ +# network_mode: host makes 127.0.0.1 reach PostgreSQL on the host. +# +# PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' \ +# docker compose -f scripts/support-bot/compose.yml up --build + +services: + support-bot: + image: simplex-support-bot:latest + build: + context: ../.. + dockerfile: scripts/support-bot/Dockerfile + network_mode: host + restart: on-failure:5 + environment: + GROK_API_KEY: ${GROK_API_KEY:-} + volumes: + - support-bot-data:/data + - ./config:/etc/support-bot:ro + command: + - "--team-group" + - "Support Team" + - "--state-file" + - "/data/state.json" + - "--db" + - "postgres" + - "--pg-conn" + - "${PG_CONN:?set PG_CONN, e.g. postgres://simplex@127.0.0.1/simplex_support_bot}" + - "--context-file" + - "/etc/support-bot/grok-context.txt" + stop_grace_period: 30s + +volumes: + support-bot-data: diff --git a/scripts/support-bot/config/.gitkeep b/scripts/support-bot/config/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 From a0a8e03ff618536416d7278609819ddc7f21fcc8 Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:03:10 +0000 Subject: [PATCH 02/16] support bot: check config and database with --dry-run --- apps/simplex-support-bot/README.md | 43 +++++++++ apps/simplex-support-bot/bot.test.ts | 98 ++++++++++++++++++++ apps/simplex-support-bot/src/config.ts | 10 +++ apps/simplex-support-bot/src/dryRun.ts | 120 +++++++++++++++++++++++++ apps/simplex-support-bot/src/index.ts | 73 ++++++++++++++- 5 files changed, 342 insertions(+), 2 deletions(-) create mode 100644 apps/simplex-support-bot/src/dryRun.ts diff --git a/apps/simplex-support-bot/README.md b/apps/simplex-support-bot/README.md index d95aa5b59b..2483fb1fb4 100644 --- a/apps/simplex-support-bot/README.md +++ b/apps/simplex-support-bot/README.md @@ -56,6 +56,8 @@ Run `npm start -- --help` for the auto-generated reference. Summary: | `--complete-hours` | both | no | `3` | auto-complete chats after N hours idle (`0` disables) | | `--card-flush-seconds` | both | no | `300` | debounce card state writes | | `--context-file` | both | required with `GROK_API_KEY` | | text file with Grok system context | +| `--dry-run` | both | no | | check config, database and state, then exit | +| `--allow-migrations` | both | no | | with `--dry-run`: apply pending migrations instead of reporting them | | `-h` / `--help` | both | no | | show usage and exit | ## Environment variables @@ -65,6 +67,46 @@ Run `npm start -- --help` for the auto-generated reference. Summary: | `GROK_API_KEY` | xAI API key; enables Grok replies | | `SIMPLEX_LIBS_DIR` | directory with a local libsimplex build for the `--db` backend, instead of the downloaded one | | `SIMPLEX_ADDON_PATH` | locally built Node.js add-on (`simplex.node`), instead of the downloaded one | +| `NODE_OPTIONS` | `--max-old-space-size=8192` on a bot with many chats: single API responses outgrow the default heap | + +## Docker + +`scripts/support-bot/Dockerfile` builds libsimplex (PostgreSQL backend), the Node.js library and the bot from this checkout. The first build compiles the core and takes hours on a cold cabal cache. + +```bash +PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' \ + docker compose -f scripts/support-bot/compose.yml up --build +``` + +The Grok context file is read from `scripts/support-bot/config/grok-context.txt`, and `GROK_API_KEY` from the environment. + +## Dry run + +`--dry-run` opens the database, checks what the next start would do, and exits. It does not start chat, so nothing is sent to the network, and pending migrations are reported, not applied. + +```bash +npm start -- --dry-run --team-group "Support Team" --db postgres --pg-conn "postgres://user:pass@host/db" \ + -a 3:alice --context-file ./data/grok-context.yaml +``` + +``` +ok database postgres opened, schema up to date +ok active user: 1:Ask SimpleX Team +ok team group 1: Support Team +FAIL team member 3:alice has display name "bob", the bot would exit +``` + +A pending migration stops the checks: + +``` +FAIL database: 2 migration(s) pending, a real start would apply them: 20261019_chat_layout, 20261021_badge_subscriptions +``` + +With `--allow-migrations`, the migrations are applied first and the checks run after them. Run it against a restored copy of the database unless the migration is intended, and with the bot stopped. + +A line is `ok`, `FAIL` or `?`. `?` marks an id absent from the chat previews: with chat stopped, only chats that were used are listed, so the bot verifies such ids at startup. + +The exit code is 0 when no line is `FAIL`, 1 otherwise. The checks cover the database connection and migrations, the persisted team group, Grok user and Grok contact, the `--auto-add-team-members` ids and names, and the `--context-file`. ## Local development against unreleased lib changes @@ -87,3 +129,4 @@ export SIMPLEX_ADDON_PATH=../../packages/simplex-chat-nodejs/build/Release/simpl - **`--pg-conn is required with --db postgres`** — pass a connection string, or use `--db sqlite`. - **`libpq5` errors at startup** — install `libpq5` on the host (`apt install libpq5` on Debian/Ubuntu). - **`ENOENT: no such file or directory, open './data/state.json'`** — the parent directory of `--state-file` must exist; `mkdir -p data` before starting. +- **`JavaScript heap out of memory`** — a response outgrew the default heap; restart with `NODE_OPTIONS=--max-old-space-size=8192`. diff --git a/apps/simplex-support-bot/bot.test.ts b/apps/simplex-support-bot/bot.test.ts index a9785de1e3..3bb341e23d 100644 --- a/apps/simplex-support-bot/bot.test.ts +++ b/apps/simplex-support-bot/bot.test.ts @@ -8,6 +8,7 @@ import {CardManager} from "./src/cards.js" import {parseConfig} from "./src/config.js" import {GrokApiClient} from "./src/grok.js" import {loadGrokContext} from "./src/context.js" +import {dryRun} from "./src/dryRun.js" import {welcomeMessage, queueMessage, grokActivatedMessage, teamLockedMessage, teamAlreadyInvitedMessage} from "./src/messages.js" // Silence console output during tests @@ -2452,6 +2453,19 @@ describe("parseConfig Validation", () => { expect(cfg.db).toEqual({type: "sqlite", filePrefix: "./data/simplex", encryptionKey: "secret"}) }) + test("--dry-run and --allow-migrations default to false and parse when given", () => { + expect(parseConfig(baseArgs).dryRun).toBe(false) + expect(parseConfig(baseArgs).allowMigrations).toBe(false) + const cfg = parseConfig([...baseArgs, "--dry-run", "--allow-migrations"]) + expect(cfg.dryRun).toBe(true) + expect(cfg.allowMigrations).toBe(true) + }) + + test("--allow-migrations without --dry-run → throws", () => { + expect(() => parseConfig([...baseArgs, "--allow-migrations"])) + .toThrow(/--allow-migrations requires --dry-run/) + }) + test("unknown flag → parseArgs throws", () => { expect(() => parseConfig([...baseArgs, "--team-gropu", "typo"])) .toThrow() @@ -2688,3 +2702,87 @@ describe("loadGrokContext", () => { expect(() => loadGrokContext(join(dir, "does-not-exist.yaml"))).toThrow() }) }) + +describe("Dry Run", () => { + const directChat = (contactId: number, displayName: string) => ({ + chatInfo: {type: "direct", contact: {contactId, profile: {displayName}}}, + }) + const groupChat = (groupId: number, displayName: string) => ({ + chatInfo: {type: "group", groupInfo: {groupId, groupProfile: {displayName}}}, + }) + + function mkChat(chats: unknown[], users: number[] = [1]) { + return { + apiGetActiveUser: async () => ({userId: 1, profile: {displayName: "Ask SimpleX Team"}}), + apiListUsers: async () => users.map(userId => ({user: {userId, profile: {displayName: `user${userId}`}}})), + apiGetChats: async (_userId: number, _pagination: unknown, query?: {type: string; search?: string}) => + query?.type === "search" + ? chats.filter((c: any) => + (c.chatInfo.contact?.profile.displayName ?? c.chatInfo.groupInfo?.groupProfile.displayName) === query.search) + : chats, + } as any + } + + const config = (over: object = {}) => + ({ + teamGroup: {id: 0, name: "Support Team"}, + teamMembers: [], + contextFile: null, + ...over, + }) as any + + test("all ids resolve → passes", async () => { + const chat = mkChat([groupChat(1, "Support Team"), directChat(3, "alice")]) + const ok = await dryRun(chat, config({teamMembers: [{id: 3, name: "alice"}]}), {teamGroupId: 1}) + expect(ok).toBe(true) + }) + + test("no team group in state → reports it would be created, passes", async () => { + const chat = mkChat([]) + expect(await dryRun(chat, config(), {})).toBe(true) + }) + + test("team group id not in chat previews → reported, passes", async () => { + const chat = mkChat([groupChat(7, "Support Team")]) + expect(await dryRun(chat, config(), {teamGroupId: 1})).toBe(true) + }) + + test("team member id not in chat previews → reported, passes", async () => { + const chat = mkChat([groupChat(1, "Support Team")]) + expect(await dryRun(chat, config({teamMembers: [{id: 3, name: "alice"}]}), {teamGroupId: 1})).toBe(true) + }) + + test("team member found by id after a rename → fails", async () => { + const chat = mkChat([groupChat(1, "Support Team"), directChat(3, "alice")]) + expect(await dryRun(chat, config({teamMembers: [{id: 3, name: "bob"}]}), {teamGroupId: 1})).toBe(false) + }) + + test("persisted Grok user missing → fails", async () => { + const chat = mkChat([groupChat(1, "Support Team")], [1]) + expect(await dryRun(chat, config(), {teamGroupId: 1, grokUserId: 2})).toBe(false) + }) + + test("persisted Grok user present → passes", async () => { + const chat = mkChat([groupChat(1, "Support Team")], [1, 2]) + expect(await dryRun(chat, config(), {teamGroupId: 1, grokUserId: 2})).toBe(true) + }) + + test("no active user → fails", async () => { + const chat = {...mkChat([]), apiGetActiveUser: async () => undefined} + expect(await dryRun(chat, config(), {})).toBe(false) + }) + + test("malformed context file → fails", async () => { + const dir = mkdtempSync(join(tmpdir(), "support-bot-dryrun-")) + const path = join(dir, "ctx.yaml") + writeFileSync(path, "- role: user\n message: [unclosed\n") + const chat = mkChat([groupChat(1, "Support Team")]) + expect(await dryRun(chat, config({contextFile: path}), {teamGroupId: 1})).toBe(false) + }) + + test("missing context file → reported, passes", async () => { + const chat = mkChat([groupChat(1, "Support Team")]) + const cfg = config({contextFile: join(tmpdir(), "no-such-context.txt")}) + expect(await dryRun(chat, cfg, {teamGroupId: 1})).toBe(true) + }) +}) diff --git a/apps/simplex-support-bot/src/config.ts b/apps/simplex-support-bot/src/config.ts index be33db0c38..a1c6dca395 100644 --- a/apps/simplex-support-bot/src/config.ts +++ b/apps/simplex-support-bot/src/config.ts @@ -16,6 +16,8 @@ export interface Config { completeHours: number cardFlushSeconds: number contextFile: string | null + dryRun: boolean + allowMigrations: boolean grokApiKey: string | null } @@ -53,6 +55,8 @@ function buildCommand(): Command { .option("--complete-hours ", "auto-complete chats after N hours idle (0 disables)", parseNonNegativeInt("--complete-hours"), 3) .option("--card-flush-seconds ", "debounce card state writes", parseNonNegativeInt("--card-flush-seconds"), 300) .option("--context-file ", "text file with Grok system context (required if GROK_API_KEY set)") + .option("--dry-run", "check config, database and state, then exit without starting chat") + .option("--allow-migrations", "with --dry-run: apply pending migrations instead of reporting them") .addHelpText("after", "\nEnvironment:\n GROK_API_KEY xAI API key — enables Grok replies\n SIMPLEX_LIBS_DIR local libsimplex build for the --db backend\n SIMPLEX_ADDON_PATH locally built Node.js add-on\n") } @@ -69,6 +73,8 @@ interface RawOpts { completeHours: number cardFlushSeconds: number contextFile?: string + dryRun?: boolean + allowMigrations?: boolean } export function parseConfig(args: string[]): Config { @@ -99,6 +105,8 @@ export function parseConfig(args: string[]): Config { : {type: "postgres", connectionString: opts.pgConn} } + if (opts.allowMigrations && !opts.dryRun) throw new Error("--allow-migrations requires --dry-run") + const teamGroup: IdName = {id: 0, name: opts.teamGroup} const teamMembersRaw = opts.autoAddTeamMembers ?? "" @@ -127,6 +135,8 @@ export function parseConfig(args: string[]): Config { completeHours: opts.completeHours, cardFlushSeconds: opts.cardFlushSeconds, contextFile, + dryRun: opts.dryRun ?? false, + allowMigrations: opts.allowMigrations ?? false, grokApiKey, } } diff --git a/apps/simplex-support-bot/src/dryRun.ts b/apps/simplex-support-bot/src/dryRun.ts new file mode 100644 index 0000000000..a04128cbc5 --- /dev/null +++ b/apps/simplex-support-bot/src/dryRun.ts @@ -0,0 +1,120 @@ +import {api} from "simplex-chat" +import {T} from "@simplex-chat/types" +import {Config, IdName} from "./config.js" +import {loadGrokContext} from "./context.js" + +export interface DryRunState { + teamGroupId?: number + grokContactId?: number + grokUserId?: number +} + +const SEARCH_LIMIT = 100 +const LISTING_LIMIT = 1000 +const GROK_PROFILE_NAME = "Grok" + +type Check = (ok: boolean, msg: string) => void +type Unverified = (msg: string) => void + +export async function dryRun(chat: api.ChatApi, config: Config, state: DryRunState): Promise { + const results: boolean[] = [] + const check: Check = (ok, msg) => { + results.push(ok) + console.log(`${ok ? "ok " : "FAIL"} ${msg}`) + } + const unverified: Unverified = msg => console.log(`? ${msg}`) + + if (config.contextFile) { + try { + const context = loadGrokContext(config.contextFile) + check(true, `Grok context: ${context.length} message(s) from ${config.contextFile}`) + } catch (err) { + const e = err as NodeJS.ErrnoException + const missing = e.code === "ENOENT" + check(missing, `Grok context ${config.contextFile}: ${missing ? "not found, would start without it" : e.message}`) + } + } + + const user = await chat.apiGetActiveUser() + if (!user) { + check(false, "no active user in database, the bot would create one") + return false + } + check(true, `active user: ${user.userId}:${user.profile.displayName}`) + + if (state.grokUserId !== undefined) { + const users = await chat.apiListUsers() + const found = users.find(u => u.user.userId === state.grokUserId) + check(!!found, `Grok user ${state.grokUserId}: ${found ? found.user.profile.displayName : "missing, the bot would exit"}`) + } + + if (state.teamGroupId === undefined) { + check(true, `no team group in state, "${config.teamGroup.name}" would be created`) + } else { + const group = await findGroup(chat, user.userId, config.teamGroup.name, state.teamGroupId) + if (!group) { + unverified(`team group ${state.teamGroupId} not in chat previews, the bot verifies it at startup`) + } else { + check(true, `team group ${state.teamGroupId}: ${group.groupProfile.displayName}`) + if (group.groupProfile.displayName !== config.teamGroup.name) { + check(true, `team group name differs from --team-group, profile would be updated to "${config.teamGroup.name}"`) + } + } + } + + if (state.grokContactId !== undefined) { + const contact = await findContact(chat, user.userId, GROK_PROFILE_NAME, state.grokContactId) + if (contact) check(true, `Grok contact ${state.grokContactId}: ${contact.profile.displayName}`) + else unverified(`Grok contact ${state.grokContactId} not in chat previews, the bot verifies it at startup`) + } + + await checkTeamMembers(chat, user.userId, config.teamMembers, check, unverified) + + return results.every(Boolean) +} + +async function checkTeamMembers( + chat: api.ChatApi, + userId: number, + members: IdName[], + check: Check, + unverified: Unverified +): Promise { + for (const {id, name} of members) { + const contact = await findContact(chat, userId, name, id) + if (!contact) { + unverified(`team member ${id}:${name} not in chat previews, the bot verifies it at startup`) + } else { + const match = contact.profile.displayName === name + check(match, `team member ${id}:${name}${match ? "" : ` has display name "${contact.profile.displayName}", the bot would exit`}`) + } + } +} + +async function findGroup(chat: api.ChatApi, userId: number, search: string, groupId: number): Promise { + const group = (chats: T.AChat[]): T.GroupInfo | undefined => { + for (const {chatInfo} of chats) { + if (chatInfo.type === "group" && chatInfo.groupInfo.groupId === groupId) return chatInfo.groupInfo + } + return undefined + } + return group(await searchChats(chat, userId, search)) ?? group(await recentChats(chat, userId)) +} + +async function findContact(chat: api.ChatApi, userId: number, search: string, contactId: number): Promise { + const contact = (chats: T.AChat[]): T.Contact | undefined => { + for (const {chatInfo} of chats) { + if (chatInfo.type === "direct" && chatInfo.contact.contactId === contactId) return chatInfo.contact + } + return undefined + } + return contact(await searchChats(chat, userId, search)) ?? contact(await recentChats(chat, userId)) +} + +function searchChats(chat: api.ChatApi, userId: number, search: string): Promise { + return chat.apiGetChats(userId, {type: "last", count: SEARCH_LIMIT}, {type: "search", search}) +} + +function recentChats(chat: api.ChatApi, userId: number): Promise { + return chat.apiGetChats(userId, {type: "last", count: LISTING_LIMIT}) +} diff --git a/apps/simplex-support-bot/src/index.ts b/apps/simplex-support-bot/src/index.ts index c99b1f5842..653d839593 100644 --- a/apps/simplex-support-bot/src/index.ts +++ b/apps/simplex-support-bot/src/index.ts @@ -1,8 +1,9 @@ import {readFileSync, writeFileSync, existsSync} from "fs" -import {api, bot, util} from "simplex-chat" +import {api, bot, core, util} from "simplex-chat" import {T} from "@simplex-chat/types" -import {parseConfig} from "./config.js" +import {Config, parseConfig} from "./config.js" import {SupportBot} from "./bot.js" +import {dryRun} from "./dryRun.js" import {GrokApiClient, GrokMessage} from "./grok.js" import {loadGrokContext} from "./context.js" import {welcomeMessage} from "./messages.js" @@ -40,6 +41,10 @@ async function main(): Promise { const stateFilePath = config.stateFile const state = readState(stateFilePath) + if (config.dryRun) { + process.exit((await runDryRun(config, state)) ? 0 : 1) + } + // Forward-reference for event handlers during init let supportBot: SupportBot | undefined @@ -370,6 +375,70 @@ async function main(): Promise { process.on("SIGTERM", () => shutdown("SIGTERM")) } +async function runDryRun(config: Config, state: BotState): Promise { + const probe = await probeDatabase(config.db) + if (probe.type === "failed") { + console.log(`FAIL database: ${probe.text}`) + return false + } + const pending = probe.type === "pending" ? probe.names : [] + if (pending.length > 0 && !config.allowMigrations) { + console.log(`FAIL database: ${pending.length} migration(s) pending, a real start would apply them: ${pending.join(", ")}`) + return false + } + const migrating = pending.length > 0 + let chat: api.ChatApi + try { + chat = await api.ChatApi.init( + config.db, + migrating ? core.MigrationConfirmation.YesUp : core.MigrationConfirmation.Error + ) + } catch (err) { + console.log(`FAIL database: ${migrationErrorText(err)}`) + return false + } + const schemaState = migrating ? `applied ${pending.length} migration(s): ${pending.join(", ")}` : "schema up to date" + console.log(`ok database ${config.db.type} opened, ${schemaState}`) + try { + return await dryRun(chat, config, state) + } finally { + await chat.close() + } +} + +type DbProbe = {type: "current"} | {type: "pending"; names: string[]} | {type: "failed"; text: string} + +async function probeDatabase(db: api.DbConfig): Promise { + try { + const chat = await api.ChatApi.init(db, core.MigrationConfirmation.Error) + await chat.close() + return {type: "current"} + } catch (err) { + const dbErr = (err as core.ChatInitError).dbMigrationError + if (dbErr?.type === "errorMigration" && dbErr.migrationError.type === "upgrade") { + return {type: "pending", names: dbErr.migrationError.upMigrations.map(m => m.upName)} + } + return {type: "failed", text: migrationErrorText(err)} + } +} + +function migrationErrorText(err: unknown): string { + const e = err as core.ChatInitError + const dbErr = e.dbMigrationError + if (dbErr?.type === "errorMigration") { + const me = dbErr.migrationError + switch (me.type) { + case "upgrade": + return `${me.upMigrations.length} migration(s) pending: ${me.upMigrations.map(m => m.upName).join(", ")}` + case "downgrade": + return `database is newer than this build, it would refuse to start; migrations to roll back: ${me.downMigrations.join(", ")}` + default: + return `migration state error: ${JSON.stringify(me)}` + } + } + return e.message +} + main().catch(err => { logError("Fatal error", err) process.exit(1) From f92d0fc34e5c6c0aaa3487c5b4cb245f68b82c49 Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:03:11 +0000 Subject: [PATCH 03/16] ci: test the support bot --- .github/workflows/build.yml | 1 + scripts/ci/README.md | 8 +++++++- scripts/ci/packages_release.py | 1 + scripts/ci/scope-tests.sh | 5 ++++- scripts/ci/test_packages_release.py | 4 ++++ 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 459d5b7e51..c90accfbf2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -42,6 +42,7 @@ on: - "website/test/**" - "website/package.json" - "packages/simplex-chat-mcp/**" + - "apps/simplex-support-bot/**" types: [opened, synchronize, reopened, ready_for_review] workflow_dispatch: inputs: diff --git a/scripts/ci/README.md b/scripts/ci/README.md index c0986dbe33..0693d4e90d 100644 --- a/scripts/ci/README.md +++ b/scripts/ci/README.md @@ -146,6 +146,13 @@ The reduced build runs one Linux job, build-linux (ubuntu-22.04 x86_64), for eve - before a publication: the package with its published dependencies, type-checked and unit tested in job `publish` of build.yml (docs/contributing/RELEASES.md, "MCP server release"). - Local: `scripts/ci/scope-tests.sh mcp`. +#### support-bot + +- Globs: apps/simplex-support-bot/**. +- CI, in build-linux: `scope-tests.sh support-bot` (step "Test changed scopes"): `npm ci`, `npm run build` and `npm test`, with the published `simplex-chat` and `@simplex-chat/types` of package-lock.json. +- The Docker image (scripts/support-bot/Dockerfile) is not built in CI. +- Local: `scripts/ci/scope-tests.sh support-bot`. + #### Scopes lacking CI tests | Scope | Paths | CI | @@ -155,7 +162,6 @@ The reduced build runs one Linux job, build-linux (ubuntu-22.04 x86_64), for eve | ts-client | packages/simplex-chat-client/typescript/** | - | | webrtc | packages/simplex-chat-webrtc/** | - | | calculator-bot | apps/simplex-calculator-bot/** | - | -| support-bot | apps/simplex-support-bot/** | - | | support-bot-light | apps/simplex-support-bot-light/** | - | | prices | scripts/prices/** | - | | nft | eth/nft/** | - | diff --git a/scripts/ci/packages_release.py b/scripts/ci/packages_release.py index 842432daab..15c00501f7 100644 --- a/scripts/ci/packages_release.py +++ b/scripts/ci/packages_release.py @@ -90,6 +90,7 @@ SCOPE_CHANGES = { "packages/simplex-chat-nodejs/**", "packages/simplex-chat-client/types/typescript/**", ], + "support-bot": ["apps/simplex-support-bot/**"], } CABAL_FILE = "simplex-chat.cabal" CABAL_VERSION = re.compile(r"^(version:\s*)(\S+)", re.M) diff --git a/scripts/ci/scope-tests.sh b/scripts/ci/scope-tests.sh index 7d9108372d..74c04c2842 100755 --- a/scripts/ci/scope-tests.sh +++ b/scripts/ci/scope-tests.sh @@ -25,8 +25,11 @@ case "${1:-}" in npm test npm run test:e2e ;; + support-bot) + (cd apps/simplex-support-bot && npm ci --no-audit --no-fund && npm run build && npm test) + ;; *) - echo "usage: $0 badge-web | ci-scripts | website | mcp" >&2 + echo "usage: $0 badge-web | ci-scripts | website | mcp | support-bot" >&2 exit 1 ;; esac diff --git a/scripts/ci/test_packages_release.py b/scripts/ci/test_packages_release.py index 8c8a5c054a..e5bfac4823 100644 --- a/scripts/ci/test_packages_release.py +++ b/scripts/ci/test_packages_release.py @@ -527,6 +527,10 @@ class ChangesTest(GitRepoTest): self.assertEqual(self.builds("packages/simplex-chat-nodejs/src/api.ts"), {"packages"}) self.assertEqual(self.values["scopes"], "mcp") + def test_support_bot_change_tests_its_scope(self): + self.assertEqual(self.builds("apps/simplex-support-bot/src/bot.ts"), set()) + self.assertEqual(self.values["scopes"], "support-bot") + class PackageStateTest(GitRepoTest): def setUp(self): From e0406c287f1532fcf7d1dc559d036041466a7522 Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:43:53 +0000 Subject: [PATCH 04/16] support bot: options and messages in a TOML config file --- apps/simplex-support-bot/README.md | 41 +++++++- apps/simplex-support-bot/bot.test.ts | 115 ++++++++++++++++++--- apps/simplex-support-bot/package-lock.json | 13 +++ apps/simplex-support-bot/package.json | 1 + apps/simplex-support-bot/src/bot.ts | 40 ++++--- apps/simplex-support-bot/src/config.ts | 49 ++++++++- apps/simplex-support-bot/src/index.ts | 3 +- apps/simplex-support-bot/src/messages.ts | 79 ++++++++------ apps/simplex-support-bot/tsconfig.json | 4 +- 9 files changed, 270 insertions(+), 75 deletions(-) diff --git a/apps/simplex-support-bot/README.md b/apps/simplex-support-bot/README.md index 2483fb1fb4..95a16df0e2 100644 --- a/apps/simplex-support-bot/README.md +++ b/apps/simplex-support-bot/README.md @@ -44,6 +44,7 @@ Run `npm start -- --help` for the auto-generated reference. Summary: | Flag | Backend | Required | Default | Description | |---|---|---|---|---| +| `--config` | both | no | | TOML file with option values and message texts, see [Config file](#config-file) | | `--team-group` | both | yes | — | team group display name | | `--state-file` | both | no | `./data/state.json` | path to bot state JSON | | `--db` | both | no | `sqlite` | `sqlite` or `postgres` | @@ -60,6 +61,44 @@ Run `npm start -- --help` for the auto-generated reference. Summary: | `--allow-migrations` | both | no | | with `--dry-run`: apply pending migrations instead of reporting them | | `-h` / `--help` | both | no | | show usage and exit | +## Config file + +`--config bot.toml` reads option values and message texts from a TOML file. Keys are the flag names without `--`; a list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run` and `--allow-migrations` are command-line only. + +```toml +team-group = "SimpleX Support Team" +auto-add-team-members = ["142681:Alice"] +context-file = "/etc/support-bot/grok-context.yaml" +timezone = "UTC" + +[messages] +welcome = """ +Hello! This is a *SimpleX team* support bot - not an AI. +Discover public groups: [simplex.chat/directory](https://simplex.chat/directory) + +Please ask any questions about SimpleX Chat.""" +queue = "The team will reply to your message within {hours} hours." +``` + +A message left out keeps its built-in text. `{hours}` is replaced with 24 on weekdays and 48 at weekends in `--timezone`. The welcome message is updated in the bot address on the next start. + +| Message | Sent when | +|---|---| +| `welcome` | a customer connects to the business address | +| `queue` | a customer's first message is queued for the team, and after `grokUnavailable` when `/grok` was the first message | +| `queueGrok` | appended to `queue` after an empty line when Grok is enabled | +| `grokActivated` | Grok joins after `/grok` | +| `teamAdded` | team members are added after `/team` | +| `teamAddedGrok` | appended to `teamAdded` on a new line when Grok is in the chat | +| `teamAlreadyInvited` | `/team` is sent again while a team member is in the chat | +| `teamLocked` | `/grok` is sent after a team member wrote in the chat | +| `noTeamMembers` | `/team` is sent with no team members configured | +| `noTeamMembersGrok` | the same, when Grok is enabled | +| `grokInviting` | Grok is being invited | +| `grokUnavailable` | Grok cannot be invited, does not join within 2 minutes, or its first answer fails | +| `grokError` | Grok's answer to a later message fails | +| `grokNoHistory` | Grok joined but could not read earlier messages | + ## Environment variables | Var | Purpose | @@ -78,7 +117,7 @@ PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' \ docker compose -f scripts/support-bot/compose.yml up --build ``` -The Grok context file is read from `scripts/support-bot/config/grok-context.txt`, and `GROK_API_KEY` from the environment. +The Grok context file is read from `scripts/support-bot/config/grok-context.txt`, and `GROK_API_KEY` from the environment. `scripts/support-bot/config` is mounted at `/etc/support-bot`, so a config file placed there is passed as `--config /etc/support-bot/bot.toml`. ## Dry run diff --git a/apps/simplex-support-bot/bot.test.ts b/apps/simplex-support-bot/bot.test.ts index 3bb341e23d..83d96c3a1a 100644 --- a/apps/simplex-support-bot/bot.test.ts +++ b/apps/simplex-support-bot/bot.test.ts @@ -9,7 +9,7 @@ import {parseConfig} from "./src/config.js" import {GrokApiClient} from "./src/grok.js" import {loadGrokContext} from "./src/context.js" import {dryRun} from "./src/dryRun.js" -import {welcomeMessage, queueMessage, grokActivatedMessage, teamLockedMessage, teamAlreadyInvitedMessage} from "./src/messages.js" +import {defaultMessages, queueMessage, teamAddedMessage, noTeamMembersMessage} from "./src/messages.js" // Silence console output during tests vi.spyOn(console, "log").mockImplementation(() => {}) @@ -273,6 +273,7 @@ function makeConfig(overrides: Partial = {}) { cardFlushSeconds: 300, grokApiKey: "test-key", grokContactId: GROK_CONTACT_ID as number | null, + messages: defaultMessages, ...overrides, } } @@ -670,7 +671,7 @@ describe("/grok Activation", () => { await joinPromise await bot.flush() expectMemberAdded(CUSTOMER_GROUP_ID, GROK_CONTACT_ID) - expectSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) + expectSentToGroup(CUSTOMER_GROUP_ID, defaultMessages.grokActivated) }) test("/grok as first message → WELCOME→GROK directly, no queue message", async () => { @@ -678,7 +679,7 @@ describe("/grok Activation", () => { await bot.onNewChatItems(customerMessage("/grok")) await joinPromise await bot.flush() - expectSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) + expectSentToGroup(CUSTOMER_GROUP_ID, defaultMessages.grokActivated) expectNotSentToGroup(CUSTOMER_GROUP_ID, "The team will reply to your message") expect(chat.sentTo(TEAM_GROUP_ID).length).toBeGreaterThan(0) }) @@ -686,7 +687,7 @@ describe("/grok Activation", () => { test("/grok in TEAM → rejected with teamLockedMessage", async () => { await reachTeam() await bot.onNewChatItems(customerMessage("/grok")) - expectSentToGroup(CUSTOMER_GROUP_ID, teamLockedMessage) + expectSentToGroup(CUSTOMER_GROUP_ID, defaultMessages.teamLocked) }) test("/grok when grokContactId is null → grokUnavailableMessage", async () => { @@ -964,7 +965,7 @@ describe("/team Activation", () => { addBotMessage("We will reply within 24 hours.") chat.members.set(CUSTOMER_GROUP_ID, [makeTeamMember(TEAM_MEMBER_1_ID, "Alice")]) await bot.onNewChatItems(customerMessage("/team")) - expectSentToGroup(CUSTOMER_GROUP_ID, teamAlreadyInvitedMessage) + expectSentToGroup(CUSTOMER_GROUP_ID, defaultMessages.teamAlreadyInvited) }) test("/team with no team members → noTeamMembersMessage", async () => { @@ -998,7 +999,7 @@ describe("One-Way Gate", () => { test("/grok after gate → teamLockedMessage", async () => { await reachTeam() await bot.onNewChatItems(customerMessage("/grok")) - expectSentToGroup(CUSTOMER_GROUP_ID, teamLockedMessage) + expectSentToGroup(CUSTOMER_GROUP_ID, defaultMessages.teamLocked) }) test("customer text in TEAM → card update scheduled, no bot reply", async () => { @@ -1569,7 +1570,7 @@ describe("Error Handling", () => { // Only the "Inviting Grok" message is sent — no activated/unavailable result expect(chat.sent.length).toBe(sentBefore + 1) expectSentToGroup(CUSTOMER_GROUP_ID, "Inviting Grok") - expectNotSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) + expectNotSentToGroup(CUSTOMER_GROUP_ID, defaultMessages.grokActivated) expectNotSentToGroup(CUSTOMER_GROUP_ID, "temporarily unavailable") }) @@ -1759,7 +1760,7 @@ describe("Grok Join Flow", () => { await bot.flush() expect(chat.joined).toContain(GROK_LOCAL_GROUP_ID) - expectSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) + expectSentToGroup(CUSTOMER_GROUP_ID, defaultMessages.grokActivated) }) test("per-message responses suppressed during activateGrok initial response", async () => { @@ -1907,7 +1908,7 @@ describe("End-to-End Flows", () => { await joinPromise await bot.flush() - expectSentToGroup(CUSTOMER_GROUP_ID, grokActivatedMessage) + expectSentToGroup(CUSTOMER_GROUP_ID, defaultMessages.grokActivated) expectNotSentToGroup(CUSTOMER_GROUP_ID, "The team will reply to your message") expect(chat.sentTo(TEAM_GROUP_ID).length).toBeGreaterThan(0) }) @@ -1939,22 +1940,39 @@ describe("End-to-End Flows", () => { describe("Message Templates", () => { test("welcomeMessage is a non-empty string", () => { - expect(typeof welcomeMessage).toBe("string") - expect(welcomeMessage.length).toBeGreaterThan(0) + expect(typeof defaultMessages.welcome).toBe("string") + expect(defaultMessages.welcome.length).toBeGreaterThan(0) }) test("grokActivatedMessage mentions chatting with Grok", () => { - expect(grokActivatedMessage).toContain("chatting with Grok") + expect(defaultMessages.grokActivated).toContain("chatting with Grok") }) test("teamLockedMessage tells customer the team will handle the conversation", () => { - expect(teamLockedMessage).toContain("team") + expect(defaultMessages.teamLocked).toContain("team") }) test("queueMessage mentions hours", () => { - const msg = queueMessage("UTC", true) + const msg = queueMessage(defaultMessages, "UTC", true) expect(msg).toContain("hours") }) + + test("queueMessage replaces every {hours} with 24 or 48 and appends the Grok part only with Grok", () => { + const messages = {...defaultMessages, queue: "Reply in {hours}h, not later than {hours}h.", queueGrok: "Try /grok."} + expect(queueMessage(messages, "UTC", false)).toMatch(/^Reply in (24|48)h, not later than (24|48)h\.$/) + expect(queueMessage(messages, "UTC", true)).toMatch(/^Reply in (24|48)h, not later than (24|48)h\.\n\nTry \/grok\.$/) + }) + + test("teamAddedMessage appends the Grok line on a new line only when Grok is present", () => { + const messages = {...defaultMessages, teamAdded: "Within {hours}h.", teamAddedGrok: "Grok helps."} + expect(teamAddedMessage(messages, "UTC", false)).toMatch(/^Within (24|48)h\.$/) + expect(teamAddedMessage(messages, "UTC", true)).toMatch(/^Within (24|48)h\.\nGrok helps\.$/) + }) + + test("noTeamMembersMessage selects the text by Grok availability", () => { + expect(noTeamMembersMessage(defaultMessages, true)).toBe(defaultMessages.noTeamMembersGrok) + expect(noTeamMembersMessage(defaultMessages, false)).toBe(defaultMessages.noTeamMembers) + }) }) describe("State persistence in customData", () => { @@ -2466,6 +2484,75 @@ describe("parseConfig Validation", () => { .toThrow(/--allow-migrations requires --dry-run/) }) + describe("--config TOML file", () => { + const configFile = (toml: string): string => { + const path = join(mkdtempSync(join(tmpdir(), "support-bot-config-")), "bot.toml") + writeFileSync(path, toml) + return path + } + + test("option values and messages are read; missing messages keep the defaults", () => { + const path = configFile([ + `team-group = "SimpleX Support Team"`, + `auto-add-team-members = ["1:Alice", "2:Bob"]`, + `complete-hours = 0`, + `db = "postgres"`, + `pg-conn = "postgres://localhost/db"`, + `[messages]`, + `welcome = """`, + `Hello!`, + ``, + `Ask anything."""`, + ].join("\n")) + const cfg = parseConfig(["--config", path]) + expect(cfg.teamGroup).toEqual({id: 0, name: "SimpleX Support Team"}) + expect(cfg.teamMembers).toEqual([{id: 1, name: "Alice"}, {id: 2, name: "Bob"}]) + expect(cfg.completeHours).toBe(0) + expect(cfg.db).toEqual({type: "postgres", connectionString: "postgres://localhost/db"}) + expect(cfg.messages.welcome).toBe("Hello!\n\nAsk anything.") + expect(cfg.messages.queue).toBe(defaultMessages.queue) + }) + + test("command-line options take precedence over the file, given before or after --config", () => { + const path = configFile(`team-group = "From file"\ntimezone = "Europe/London"`) + expect(parseConfig(["--team-group", "From CLI", "--config", path]).teamGroup.name).toBe("From CLI") + expect(parseConfig([`--config=${path}`, "--team-group", "From CLI"]).timezone).toBe("Europe/London") + }) + + test("without --config → default messages", () => { + expect(parseConfig(baseArgs).messages).toEqual(defaultMessages) + }) + + test("values are validated as on the command line", () => { + expect(() => parseConfig(["--config", configFile(`team-group = "T"\ncomplete-hours = -1`)])) + .toThrow(/--complete-hours must be a non-negative integer, got "-1"/) + expect(() => parseConfig(["--config", configFile(`team-group = "T"\ndb = "mysql"`)])) + .toThrow(/Allowed choices are sqlite, postgres/) + expect(() => parseConfig(["--config", configFile(`team-group = "T"\nteam_group = "U"`)])) + .toThrow(/unknown option '--team_group=U'/) + }) + + test("flags, nested config and non-scalar values → throw", () => { + expect(() => parseConfig(["--config", configFile(`team-group = "T"\ndry-run = true`)])) + .toThrow(/dry-run must be a string, a number or a list of strings/) + expect(() => parseConfig(["--config", configFile(`team-group = "T"\nconfig = "other.toml"`)])) + .toThrow(/config cannot be set in a config file/) + }) + + test("unknown or non-string messages → throw", () => { + expect(() => parseConfig(["--config", configFile(`team-group = "T"\n[messages]\nwelcom = "Hi"`)])) + .toThrow(/unknown message "welcom", known messages: welcome, queue,/) + expect(() => parseConfig(["--config", configFile(`team-group = "T"\n[messages]\nwelcome = 1`)])) + .toThrow(/message "welcome" must be a string/) + expect(() => parseConfig(["--config", configFile(`team-group = "T"\nmessages = "Hi"`)])) + .toThrow(/messages must be a table/) + }) + + test("malformed TOML → throws", () => { + expect(() => parseConfig(["--config", configFile(`team-group = "unclosed`)])).toThrow() + }) + }) + test("unknown flag → parseArgs throws", () => { expect(() => parseConfig([...baseArgs, "--team-gropu", "typo"])) .toThrow() diff --git a/apps/simplex-support-bot/package-lock.json b/apps/simplex-support-bot/package-lock.json index 1dc31adaa2..8e361689f1 100644 --- a/apps/simplex-support-bot/package-lock.json +++ b/apps/simplex-support-bot/package-lock.json @@ -13,6 +13,7 @@ "async-mutex": "^0.5.0", "commander": "^14.0.3", "simplex-chat": "^7.1.0-beta.10.2", + "smol-toml": "^1.9.1", "yaml": "^2.8.4" }, "devDependencies": { @@ -1591,6 +1592,18 @@ "simplex-chat": "dist/cli.js" } }, + "node_modules/smol-toml": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.9.1.tgz", + "integrity": "sha512-j7WyPdf0CJoutMzMoPLF/bVXHtKZIT9Rb4eGeEbWw8X2t5oirjPgpL+OpaENZrILYaXqYwnVH+qgUwVbl+pUJQ==", + "license": "BSD-3-Clause", + "engines": { + "node": ">= 18" + }, + "funding": { + "url": "https://github.com/sponsors/cyyynthia" + } + }, "node_modules/source-map-js": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", diff --git a/apps/simplex-support-bot/package.json b/apps/simplex-support-bot/package.json index 790a756327..4666224fd5 100644 --- a/apps/simplex-support-bot/package.json +++ b/apps/simplex-support-bot/package.json @@ -13,6 +13,7 @@ "async-mutex": "^0.5.0", "commander": "^14.0.3", "simplex-chat": "^7.1.0-beta.10.2", + "smol-toml": "^1.9.1", "yaml": "^2.8.4" }, "devDependencies": { diff --git a/apps/simplex-support-bot/src/bot.ts b/apps/simplex-support-bot/src/bot.ts index 28b2080793..6fb4b96599 100644 --- a/apps/simplex-support-bot/src/bot.ts +++ b/apps/simplex-support-bot/src/bot.ts @@ -3,11 +3,7 @@ import {T, CEvt} from "@simplex-chat/types" import {Config} from "./config.js" import {GrokMessage, GrokApiClient} from "./grok.js" import {CardManager, ConversationState} from "./cards.js" -import { - queueMessage, grokInvitingMessage, grokActivatedMessage, teamAddedMessage, - teamAlreadyInvitedMessage, teamLockedMessage, noTeamMembersMessage, - grokUnavailableMessage, grokErrorMessage, grokNoHistoryMessage, -} from "./messages.js" +import {queueMessage, teamAddedMessage, noTeamMembersMessage} from "./messages.js" import {profileMutex, log, logError, getGroupInfo} from "./util.js" // Collects the keyword of every "command" entry in the bot's registered @@ -496,7 +492,7 @@ export class SupportBot { // First regular message → QUEUE if (text) { await this.cards.mergeCustomData(groupId, {state: "QUEUE"}) - await this.sendToGroup(groupId, queueMessage(this.config.timezone, this.grokEnabled)) + await this.sendToGroup(groupId, queueMessage(this.config.messages, this.config.timezone, this.grokEnabled)) await this.cards.createCard(groupId, groupInfo) } break @@ -542,7 +538,7 @@ export class SupportBot { case "TEAM": if (cmd?.keyword === "grok") { - await this.sendToGroup(groupId, teamLockedMessage) + await this.sendToGroup(groupId, this.config.messages.teamLocked) } else if (cmd?.keyword === "team") { // Team still present → "already invited"; team all left → silent re-add await this.activateTeam(groupId) @@ -619,7 +615,7 @@ export class SupportBot { logError(`Grok per-message error for grokGroup ${grokGroupId}`, err) try { await this.withGrokProfile(() => - this.chat.apiSendTextMessage([T.ChatType.Group, grokGroupId], grokErrorMessage) + this.chat.apiSendTextMessage([T.ChatType.Group, grokGroupId], this.config.messages.grokError) ) } catch {} } @@ -643,8 +639,8 @@ export class SupportBot { } if (this.config.grokContactId === null) { await revertStateOnFail() - await this.sendToGroup(groupId, grokUnavailableMessage) - if (opts.sendQueueOnFail) await this.sendToGroup(groupId, queueMessage(this.config.timezone, this.grokEnabled)) + await this.sendToGroup(groupId, this.config.messages.grokUnavailable) + if (opts.sendQueueOnFail) await this.sendToGroup(groupId, queueMessage(this.config.messages, this.config.timezone, this.grokEnabled)) this.cards.scheduleUpdate(groupId) return } @@ -663,7 +659,7 @@ export class SupportBot { // any later and onGrokNewChatItems can fire a duplicate per-message reply. this.grokInitialResponsePending.add(groupId) try { - await this.sendToGroup(groupId, grokInvitingMessage) + await this.sendToGroup(groupId, this.config.messages.grokInviting) let member: T.GroupMember try { @@ -679,8 +675,8 @@ export class SupportBot { } logError(`Failed to invite Grok to group ${groupId}`, err) await revertStateOnFail() - await this.sendToGroup(groupId, grokUnavailableMessage) - if (opts.sendQueueOnFail) await this.sendToGroup(groupId, queueMessage(this.config.timezone, this.grokEnabled)) + await this.sendToGroup(groupId, this.config.messages.grokUnavailable) + if (opts.sendQueueOnFail) await this.sendToGroup(groupId, queueMessage(this.config.messages, this.config.timezone, this.grokEnabled)) this.cards.scheduleUpdate(groupId) return } @@ -705,19 +701,19 @@ export class SupportBot { } catch {} this.cleanupGrokMaps(groupId) await revertStateOnFail() - await this.sendToGroup(groupId, grokUnavailableMessage) - if (opts.sendQueueOnFail) await this.sendToGroup(groupId, queueMessage(this.config.timezone, this.grokEnabled)) + await this.sendToGroup(groupId, this.config.messages.grokUnavailable) + if (opts.sendQueueOnFail) await this.sendToGroup(groupId, queueMessage(this.config.messages, this.config.timezone, this.grokEnabled)) this.cards.scheduleUpdate(groupId) return } - await this.sendToGroup(groupId, grokActivatedMessage) + await this.sendToGroup(groupId, this.config.messages.grokActivated) // Grok joined — send initial response based on customer's accumulated messages try { const grokLocalGId = this.grokGroupMap.get(groupId) if (grokLocalGId === undefined) { - await this.sendToGroup(groupId, grokUnavailableMessage) + await this.sendToGroup(groupId, this.config.messages.grokUnavailable) return } @@ -739,7 +735,7 @@ export class SupportBot { if (customerMessages.length === 0) { await this.withGrokProfile(() => - this.chat.apiSendTextMessage([T.ChatType.Group, grokLocalGId], grokNoHistoryMessage) + this.chat.apiSendTextMessage([T.ChatType.Group, grokLocalGId], this.config.messages.grokNoHistory) ) return } @@ -755,7 +751,7 @@ export class SupportBot { if (response.includes("/team")) await this.activateTeam(groupId) } catch (err) { logError(`Grok initial response failed for group ${groupId}`, err) - await this.sendToGroup(groupId, grokUnavailableMessage) + await this.sendToGroup(groupId, this.config.messages.grokUnavailable) } } finally { this.grokInitialResponsePending.delete(groupId) @@ -766,7 +762,7 @@ export class SupportBot { private async activateTeam(groupId: number): Promise { if (this.config.teamMembers.length === 0) { - await this.sendToGroup(groupId, noTeamMembersMessage(this.grokEnabled)) + await this.sendToGroup(groupId, noTeamMembersMessage(this.config.messages, this.grokEnabled)) return } @@ -775,7 +771,7 @@ export class SupportBot { if (alreadyActivated) { const {teamMembers} = await this.cards.getGroupComposition(groupId) if (teamMembers.length > 0) { - await this.sendToGroup(groupId, teamAlreadyInvitedMessage) + await this.sendToGroup(groupId, this.config.messages.teamAlreadyInvited) return } // Team previously activated but all team members have since left — @@ -803,7 +799,7 @@ export class SupportBot { } const {grokMember} = await this.cards.getGroupComposition(groupId) - await this.sendToGroup(groupId, teamAddedMessage(this.config.timezone, !!grokMember)) + await this.sendToGroup(groupId, teamAddedMessage(this.config.messages, this.config.timezone, !!grokMember)) } // --- Team group commands --- diff --git a/apps/simplex-support-bot/src/config.ts b/apps/simplex-support-bot/src/config.ts index a1c6dca395..196963f971 100644 --- a/apps/simplex-support-bot/src/config.ts +++ b/apps/simplex-support-bot/src/config.ts @@ -1,5 +1,8 @@ +import {readFileSync} from "fs" import {Command, Option} from "commander" +import {parse as parseToml} from "smol-toml" import {api} from "simplex-chat" +import {Messages, defaultMessages} from "./messages.js" export interface IdName { id: number @@ -19,6 +22,7 @@ export interface Config { dryRun: boolean allowMigrations: boolean grokApiKey: string | null + messages: Messages } export function parseIdName(s: string): IdName { @@ -43,6 +47,7 @@ function buildCommand(): Command { return new Command() .name("simplex-chat-support-bot") .description("business-address triage bot") + .option("--config ", "TOML file with option values and message texts; command-line options take precedence") .requiredOption("--team-group ", "team group display name") .option("--state-file ", "state JSON path", "./data/state.json") .addOption(new Option("--db ", "database backend").choices(["sqlite", "postgres"]).default("sqlite")) @@ -77,10 +82,51 @@ interface RawOpts { allowMigrations?: boolean } +interface ConfigFile { + args: string[] + messages: Messages +} + +function configFilePath(args: string[]): string | undefined { + const i = args.indexOf("--config") + if (i >= 0) return args[i + 1] + return args.find(arg => arg.startsWith("--config="))?.slice("--config=".length) +} + +function readConfigFile(path: string): ConfigFile { + const {messages = {}, ...options} = parseToml(readFileSync(path, "utf-8")) + return { + args: Object.entries(options).map(([key, value]) => configOptionArg(path, key, value)), + messages: configMessages(path, messages), + } +} + +function configOptionArg(path: string, key: string, value: unknown): string { + if (key === "config") throw new Error(`${path}: config cannot be set in a config file`) + if (typeof value === "string" || typeof value === "number") return `--${key}=${value}` + if (Array.isArray(value) && value.every(v => typeof v === "string")) return `--${key}=${value.join(",")}` + throw new Error(`${path}: ${key} must be a string, a number or a list of strings`) +} + +function configMessages(path: string, table: unknown): Messages { + if (typeof table !== "object" || table === null || Array.isArray(table)) { + throw new Error(`${path}: messages must be a table`) + } + for (const [key, value] of Object.entries(table)) { + if (!(key in defaultMessages)) { + throw new Error(`${path}: unknown message "${key}", known messages: ${Object.keys(defaultMessages).join(", ")}`) + } + if (typeof value !== "string") throw new Error(`${path}: message "${key}" must be a string`) + } + return {...defaultMessages, ...table} +} + export function parseConfig(args: string[]): Config { const cmd = buildCommand().exitOverride() + const configFile = configFilePath(args) + const file: ConfigFile = configFile ? readConfigFile(configFile) : {args: [], messages: defaultMessages} try { - cmd.parse(args, {from: "user"}) + cmd.parse([...file.args, ...args], {from: "user"}) } catch (err) { const code = (err as {code?: string}).code if (code === "commander.helpDisplayed" || code === "commander.version") process.exit(0) @@ -138,5 +184,6 @@ export function parseConfig(args: string[]): Config { dryRun: opts.dryRun ?? false, allowMigrations: opts.allowMigrations ?? false, grokApiKey, + messages: file.messages, } } diff --git a/apps/simplex-support-bot/src/index.ts b/apps/simplex-support-bot/src/index.ts index 653d839593..373f819115 100644 --- a/apps/simplex-support-bot/src/index.ts +++ b/apps/simplex-support-bot/src/index.ts @@ -6,7 +6,6 @@ import {SupportBot} from "./bot.js" import {dryRun} from "./dryRun.js" import {GrokApiClient, GrokMessage} from "./grok.js" import {loadGrokContext} from "./context.js" -import {welcomeMessage} from "./messages.js" import {profileMutex, log, logError, getGroupInfo, getContact} from "./util.js" interface BotState { @@ -101,7 +100,7 @@ async function main(): Promise { addressSettings: { businessAddress: true, autoAccept: true, - welcomeMessage, + welcomeMessage: config.messages.welcome, }, commands: desiredCommands, useBotProfile: true, diff --git a/apps/simplex-support-bot/src/messages.ts b/apps/simplex-support-bot/src/messages.ts index 3c2dfd844f..3cad7fd844 100644 --- a/apps/simplex-support-bot/src/messages.ts +++ b/apps/simplex-support-bot/src/messages.ts @@ -1,46 +1,59 @@ import {isWeekend} from "./util.js" -export const welcomeMessage = `Hello! This is a **SimpleX team** support bot - not an AI. +export interface Messages { + welcome: string + queue: string + queueGrok: string + grokActivated: string + teamAdded: string + teamAddedGrok: string + teamAlreadyInvited: string + teamLocked: string + noTeamMembers: string + noTeamMembersGrok: string + grokInviting: string + grokUnavailable: string + grokError: string + grokNoHistory: string +} + +export const defaultMessages: Messages = { + welcome: `Hello! This is a **SimpleX team** support bot - not an AI. Discover public groups: [simplex.chat/directory](https://simplex.chat/directory) -Join the livestream about SimpleX roadmap and equity crowdfunding: [simplex.chat/livestream](https://simplex.chat/livestream) (September 15, at 17:00 UTC)` +Join the livestream about SimpleX roadmap and equity crowdfunding: [simplex.chat/livestream](https://simplex.chat/livestream) (September 15, at 17:00 UTC)`, + queue: "The team will reply to your message within {hours} hours.", + queueGrok: `If your question is about SimpleX, click /grok for an **instant Grok answer**. -export function queueMessage(timezone: string, grokEnabled: boolean): string { - const hours = isWeekend(timezone) ? "48" : "24" - const base = `The team will reply to your message within ${hours} hours.` - if (!grokEnabled) return base - return `${base} - -If your question is about SimpleX, click /grok for an **instant Grok answer**. - -Send /team to switch back.` +Send /team to switch back.`, + grokActivated: "**You are now chatting with Grok** - use any language.", + teamAdded: "We will reply within {hours} hours.", + teamAddedGrok: "Grok will be answering your questions until then.", + teamAlreadyInvited: "A team member was invited to this conversation and will reply when available.", + teamLocked: "Only the team will now receive your messages.", + noTeamMembers: "No team members are available yet. Please try again later.", + noTeamMembersGrok: "No team members are available yet. Please try again later or click /grok.", + grokInviting: "Inviting Grok, please wait...", + grokUnavailable: "Grok is temporarily unavailable. Please try again later or send /team for a human team member.", + grokError: "Sorry, I couldn't process that. Please try again or send /team for a human team member.", + grokNoHistory: "I just joined but couldn't see your earlier messages. Could you repeat your question?", } -export const grokActivatedMessage = `**You are now chatting with Grok** - use any language.` - -export function teamAddedMessage(timezone: string, grokPresent: boolean): string { - const hours = isWeekend(timezone) ? "48" : "24" - const base = `We will reply within ${hours} hours.` - if (!grokPresent) return base - return `${base} -Grok will be answering your questions until then.` +function withReplyHours(text: string, timezone: string): string { + return text.replaceAll("{hours}", isWeekend(timezone) ? "48" : "24") } -export const teamAlreadyInvitedMessage = "A team member was invited to this conversation and will reply when available." - -export const teamLockedMessage = "Only the team will now receive your messages." - -export function noTeamMembersMessage(grokEnabled: boolean): string { - return grokEnabled - ? "No team members are available yet. Please try again later or click /grok." - : "No team members are available yet. Please try again later." +export function queueMessage(messages: Messages, timezone: string, grokEnabled: boolean): string { + const base = withReplyHours(messages.queue, timezone) + return grokEnabled ? `${base}\n\n${messages.queueGrok}` : base } -export const grokInvitingMessage = "Inviting Grok, please wait..." +export function teamAddedMessage(messages: Messages, timezone: string, grokPresent: boolean): string { + const base = withReplyHours(messages.teamAdded, timezone) + return grokPresent ? `${base}\n${messages.teamAddedGrok}` : base +} -export const grokUnavailableMessage = "Grok is temporarily unavailable. Please try again later or send /team for a human team member." - -export const grokErrorMessage = "Sorry, I couldn't process that. Please try again or send /team for a human team member." - -export const grokNoHistoryMessage = "I just joined but couldn't see your earlier messages. Could you repeat your question?" +export function noTeamMembersMessage(messages: Messages, grokEnabled: boolean): string { + return grokEnabled ? messages.noTeamMembersGrok : messages.noTeamMembers +} diff --git a/apps/simplex-support-bot/tsconfig.json b/apps/simplex-support-bot/tsconfig.json index 821fa663e3..fd2abb9d51 100644 --- a/apps/simplex-support-bot/tsconfig.json +++ b/apps/simplex-support-bot/tsconfig.json @@ -4,8 +4,8 @@ "declaration": true, "forceConsistentCasingInFileNames": true, "lib": ["ES2022"], - "module": "Node16", - "moduleResolution": "Node16", + "module": "NodeNext", + "moduleResolution": "NodeNext", "noFallthroughCasesInSwitch": true, "noImplicitAny": true, "noImplicitReturns": true, From ad7c2ef745d0b1d6291b95943c0acdf775b2f235 Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:50:26 +0000 Subject: [PATCH 05/16] support bot: published or source libraries in Docker --- apps/simplex-support-bot/README.md | 60 +++++++--------- apps/simplex-support-bot/bot.test.ts | 42 ++++++----- apps/simplex-support-bot/src/config.ts | 21 ++++-- scripts/support-bot/.gitignore | 2 + scripts/support-bot/Dockerfile | 71 +++++++++++++------ scripts/support-bot/compose.yml | 17 ++--- scripts/support-bot/config/.gitkeep | 0 .../support-bot/config/config.toml.example | 18 +++++ 8 files changed, 141 insertions(+), 90 deletions(-) delete mode 100644 scripts/support-bot/config/.gitkeep create mode 100644 scripts/support-bot/config/config.toml.example diff --git a/apps/simplex-support-bot/README.md b/apps/simplex-support-bot/README.md index 95a16df0e2..f67e6bb739 100644 --- a/apps/simplex-support-bot/README.md +++ b/apps/simplex-support-bot/README.md @@ -63,41 +63,26 @@ Run `npm start -- --help` for the auto-generated reference. Summary: ## Config file -`--config bot.toml` reads option values and message texts from a TOML file. Keys are the flag names without `--`; a list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run` and `--allow-migrations` are command-line only. +`--config config.toml` reads option values and message texts from a TOML file, as [config.toml.example](../../scripts/support-bot/config/config.toml.example) shows. Keys are the command-line options in snake_case (`team_group` for `--team-group`); a list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run` and `--allow-migrations` are command-line only. -```toml -team-group = "SimpleX Support Team" -auto-add-team-members = ["142681:Alice"] -context-file = "/etc/support-bot/grok-context.yaml" -timezone = "UTC" - -[messages] -welcome = """ -Hello! This is a *SimpleX team* support bot - not an AI. -Discover public groups: [simplex.chat/directory](https://simplex.chat/directory) - -Please ask any questions about SimpleX Chat.""" -queue = "The team will reply to your message within {hours} hours." -``` - -A message left out keeps its built-in text. `{hours}` is replaced with 24 on weekdays and 48 at weekends in `--timezone`. The welcome message is updated in the bot address on the next start. +A message left out of `[messages]` keeps its built-in text. `{hours}` is replaced with 24 on weekdays and 48 at weekends in `timezone`. The welcome message is updated in the bot address on the next start. | Message | Sent when | |---|---| | `welcome` | a customer connects to the business address | -| `queue` | a customer's first message is queued for the team, and after `grokUnavailable` when `/grok` was the first message | -| `queueGrok` | appended to `queue` after an empty line when Grok is enabled | -| `grokActivated` | Grok joins after `/grok` | -| `teamAdded` | team members are added after `/team` | -| `teamAddedGrok` | appended to `teamAdded` on a new line when Grok is in the chat | -| `teamAlreadyInvited` | `/team` is sent again while a team member is in the chat | -| `teamLocked` | `/grok` is sent after a team member wrote in the chat | -| `noTeamMembers` | `/team` is sent with no team members configured | -| `noTeamMembersGrok` | the same, when Grok is enabled | -| `grokInviting` | Grok is being invited | -| `grokUnavailable` | Grok cannot be invited, does not join within 2 minutes, or its first answer fails | -| `grokError` | Grok's answer to a later message fails | -| `grokNoHistory` | Grok joined but could not read earlier messages | +| `queue` | a customer's first message is queued for the team, and after `grok_unavailable` when `/grok` was the first message | +| `queue_grok` | appended to `queue` after an empty line when Grok is enabled | +| `grok_activated` | Grok joins after `/grok` | +| `team_added` | team members are added after `/team` | +| `team_added_grok` | appended to `team_added` on a new line when Grok is in the chat | +| `team_already_invited` | `/team` is sent again while a team member is in the chat | +| `team_locked` | `/grok` is sent after a team member wrote in the chat | +| `no_team_members` | `/team` is sent with no team members configured | +| `no_team_members_grok` | the same, when Grok is enabled | +| `grok_inviting` | Grok is being invited | +| `grok_unavailable` | Grok cannot be invited, does not join within 2 minutes, or its first answer fails | +| `grok_error` | Grok's answer to a later message fails | +| `grok_no_history` | Grok joined but could not read earlier messages | ## Environment variables @@ -110,14 +95,21 @@ A message left out keeps its built-in text. `{hours}` is replaced with 24 on wee ## Docker -`scripts/support-bot/Dockerfile` builds libsimplex (PostgreSQL backend), the Node.js library and the bot from this checkout. The first build compiles the core and takes hours on a cold cabal cache. +`scripts/support-bot/Dockerfile` builds the bot on the PostgreSQL backend with one of two library sources, selected by `LIBS`: + +| `LIBS` | libsimplex, Node.js add-on and npm packages | Build time | +|---|---|---| +| `release` (default) | the versions pinned by package-lock.json, downloaded and checked by SHA-256 at build time | minutes | +| `source` | built from this checkout | hours on a cold cabal cache | ```bash -PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' \ - docker compose -f scripts/support-bot/compose.yml up --build +cd scripts/support-bot +cp config/config.toml.example config/config.toml +PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' docker compose up --build +LIBS=source docker compose build ``` -The Grok context file is read from `scripts/support-bot/config/grok-context.txt`, and `GROK_API_KEY` from the environment. `scripts/support-bot/config` is mounted at `/etc/support-bot`, so a config file placed there is passed as `--config /etc/support-bot/bot.toml`. +`scripts/support-bot/config` is mounted at `/etc/support-bot` and holds `config.toml` and the Grok context file. `GROK_API_KEY` is read from the environment. ## Dry run diff --git a/apps/simplex-support-bot/bot.test.ts b/apps/simplex-support-bot/bot.test.ts index 83d96c3a1a..25a91fce90 100644 --- a/apps/simplex-support-bot/bot.test.ts +++ b/apps/simplex-support-bot/bot.test.ts @@ -2493,16 +2493,17 @@ describe("parseConfig Validation", () => { test("option values and messages are read; missing messages keep the defaults", () => { const path = configFile([ - `team-group = "SimpleX Support Team"`, - `auto-add-team-members = ["1:Alice", "2:Bob"]`, - `complete-hours = 0`, + `team_group = "SimpleX Support Team"`, + `auto_add_team_members = ["1:Alice", "2:Bob"]`, + `complete_hours = 0`, `db = "postgres"`, - `pg-conn = "postgres://localhost/db"`, + `pg_conn = "postgres://localhost/db"`, `[messages]`, `welcome = """`, `Hello!`, ``, `Ask anything."""`, + `team_added_grok = "Grok helps."`, ].join("\n")) const cfg = parseConfig(["--config", path]) expect(cfg.teamGroup).toEqual({id: 0, name: "SimpleX Support Team"}) @@ -2510,11 +2511,12 @@ describe("parseConfig Validation", () => { expect(cfg.completeHours).toBe(0) expect(cfg.db).toEqual({type: "postgres", connectionString: "postgres://localhost/db"}) expect(cfg.messages.welcome).toBe("Hello!\n\nAsk anything.") + expect(cfg.messages.teamAddedGrok).toBe("Grok helps.") expect(cfg.messages.queue).toBe(defaultMessages.queue) }) test("command-line options take precedence over the file, given before or after --config", () => { - const path = configFile(`team-group = "From file"\ntimezone = "Europe/London"`) + const path = configFile(`team_group = "From file"\ntimezone = "Europe/London"`) expect(parseConfig(["--team-group", "From CLI", "--config", path]).teamGroup.name).toBe("From CLI") expect(parseConfig([`--config=${path}`, "--team-group", "From CLI"]).timezone).toBe("Europe/London") }) @@ -2524,32 +2526,34 @@ describe("parseConfig Validation", () => { }) test("values are validated as on the command line", () => { - expect(() => parseConfig(["--config", configFile(`team-group = "T"\ncomplete-hours = -1`)])) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\ncomplete_hours = -1`)])) .toThrow(/--complete-hours must be a non-negative integer, got "-1"/) - expect(() => parseConfig(["--config", configFile(`team-group = "T"\ndb = "mysql"`)])) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\ndb = "mysql"`)])) .toThrow(/Allowed choices are sqlite, postgres/) - expect(() => parseConfig(["--config", configFile(`team-group = "T"\nteam_group = "U"`)])) - .toThrow(/unknown option '--team_group=U'/) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\nteam_grup = "U"`)])) + .toThrow(/unknown option '--team-grup=U'/) }) - test("flags, nested config and non-scalar values → throw", () => { - expect(() => parseConfig(["--config", configFile(`team-group = "T"\ndry-run = true`)])) - .toThrow(/dry-run must be a string, a number or a list of strings/) - expect(() => parseConfig(["--config", configFile(`team-group = "T"\nconfig = "other.toml"`)])) + test("dashed keys, flags, nested config and non-scalar values → throw", () => { + expect(() => parseConfig(["--config", configFile(`team-group = "T"`)])) + .toThrow(/key team-group must be lowercase with underscores, as team_group/) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\ndry_run = true`)])) + .toThrow(/dry_run must be a string, a number or a list of strings/) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\nconfig = "other.toml"`)])) .toThrow(/config cannot be set in a config file/) }) test("unknown or non-string messages → throw", () => { - expect(() => parseConfig(["--config", configFile(`team-group = "T"\n[messages]\nwelcom = "Hi"`)])) - .toThrow(/unknown message "welcom", known messages: welcome, queue,/) - expect(() => parseConfig(["--config", configFile(`team-group = "T"\n[messages]\nwelcome = 1`)])) - .toThrow(/message "welcome" must be a string/) - expect(() => parseConfig(["--config", configFile(`team-group = "T"\nmessages = "Hi"`)])) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\n[messages]\nteamAdded = "Hi"`)])) + .toThrow(/unknown message teamAdded, known messages: welcome, queue, queue_grok, grok_activated, team_added,/) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\n[messages]\nwelcome = 1`)])) + .toThrow(/message welcome must be a string/) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\nmessages = "Hi"`)])) .toThrow(/messages must be a table/) }) test("malformed TOML → throws", () => { - expect(() => parseConfig(["--config", configFile(`team-group = "unclosed`)])).toThrow() + expect(() => parseConfig(["--config", configFile(`team_group = "unclosed`)])).toThrow() }) }) diff --git a/apps/simplex-support-bot/src/config.ts b/apps/simplex-support-bot/src/config.ts index 196963f971..1823875e06 100644 --- a/apps/simplex-support-bot/src/config.ts +++ b/apps/simplex-support-bot/src/config.ts @@ -102,23 +102,30 @@ function readConfigFile(path: string): ConfigFile { } function configOptionArg(path: string, key: string, value: unknown): string { + if (!/^[a-z][a-z_]*$/.test(key)) throw new Error(`${path}: key ${key} must be lowercase with underscores, as team_group`) if (key === "config") throw new Error(`${path}: config cannot be set in a config file`) - if (typeof value === "string" || typeof value === "number") return `--${key}=${value}` - if (Array.isArray(value) && value.every(v => typeof v === "string")) return `--${key}=${value.join(",")}` + const flag = `--${key.replaceAll("_", "-")}` + if (typeof value === "string" || typeof value === "number") return `${flag}=${value}` + if (Array.isArray(value) && value.every(v => typeof v === "string")) return `${flag}=${value.join(",")}` throw new Error(`${path}: ${key} must be a string, a number or a list of strings`) } +const messageKeys = new Map( + (Object.keys(defaultMessages) as (keyof Messages)[]).map(field => [field.replace(/[A-Z]/g, c => `_${c.toLowerCase()}`), field]) +) + function configMessages(path: string, table: unknown): Messages { if (typeof table !== "object" || table === null || Array.isArray(table)) { throw new Error(`${path}: messages must be a table`) } + const messages = {...defaultMessages} for (const [key, value] of Object.entries(table)) { - if (!(key in defaultMessages)) { - throw new Error(`${path}: unknown message "${key}", known messages: ${Object.keys(defaultMessages).join(", ")}`) - } - if (typeof value !== "string") throw new Error(`${path}: message "${key}" must be a string`) + const field = messageKeys.get(key) + if (!field) throw new Error(`${path}: unknown message ${key}, known messages: ${[...messageKeys.keys()].join(", ")}`) + if (typeof value !== "string") throw new Error(`${path}: message ${key} must be a string`) + messages[field] = value } - return {...defaultMessages, ...table} + return messages } export function parseConfig(args: string[]): Config { diff --git a/scripts/support-bot/.gitignore b/scripts/support-bot/.gitignore index 0c54f604fc..65aa47b8fb 100644 --- a/scripts/support-bot/.gitignore +++ b/scripts/support-bot/.gitignore @@ -1,2 +1,4 @@ data/ .env +config/* +!config/config.toml.example diff --git a/scripts/support-bot/Dockerfile b/scripts/support-bot/Dockerfile index 544a8be05e..a95cf9386e 100644 --- a/scripts/support-bot/Dockerfile +++ b/scripts/support-bot/Dockerfile @@ -1,5 +1,9 @@ # syntax=docker/dockerfile:1 +# release: libsimplex, the add-on and the npm packages pinned by package-lock.json +# source: all of them built from this checkout +ARG LIBS=release +ARG BACKEND=postgres # The runtime glibc must be the same or newer. ARG UBUNTU_LIBS=22.04 ARG GHC=9.6.3 @@ -7,12 +11,13 @@ ARG CABAL=3.10.2.0 ARG NODE=24 # --------------------------------------------------------------------------- # -# libsimplex +# libsimplex from source # --------------------------------------------------------------------------- # -FROM ubuntu:${UBUNTU_LIBS} AS libsimplex +FROM ubuntu:${UBUNTU_LIBS} AS libsimplex-source ARG GHC ARG CABAL +ARG BACKEND ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get install -y --no-install-recommends \ @@ -47,14 +52,14 @@ RUN --mount=type=cache,target=/root/.cabal \ find dist-newstyle/build -maxdepth 3 -type d -name 'simplex-chat-*' \ ! -path "*/simplex-chat-$ver" -exec rm -rf {} + 2>/dev/null || true; \ cabal update; \ - SKIP_VLC=1 scripts/desktop/build-lib-linux.sh postgres; \ + SKIP_VLC=1 scripts/desktop/build-lib-linux.sh "${BACKEND}"; \ mkdir -p /libs; \ cp "$build_dir"/libsimplex.so "$build_dir"/deps/* /libs/ # --------------------------------------------------------------------------- # -# build +# libs-source: libsimplex, the add-on and the npm packages from this checkout # --------------------------------------------------------------------------- # -FROM node:${NODE}-bookworm-slim AS build +FROM node:${NODE}-bookworm-slim AS libs-source ENV DEBIAN_FRONTEND=noninteractive @@ -62,31 +67,55 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential python3 && \ rm -rf /var/lib/apt/lists/* -COPY --from=libsimplex /libs /opt/simplex/libs -# The library loads libsimplex and the add-on built below instead of downloading the released ones. -ENV SIMPLEX_LIBS_DIR=/opt/simplex/libs \ - SIMPLEX_ADDON_PATH=/src/packages/simplex-chat-nodejs/build/Release/simplex.node +COPY --from=libsimplex-source /libs /opt/simplex/libs WORKDIR /src COPY packages/simplex-chat-client/types/typescript packages/simplex-chat-client/types/typescript RUN cd packages/simplex-chat-client/types/typescript && \ npm install --no-audit --no-fund && \ - npx tsc + npx tsc && \ + mkdir -p /opt/simplex/packages && \ + npm pack --pack-destination /opt/simplex/packages COPY packages/simplex-chat-nodejs packages/simplex-chat-nodejs RUN cd packages/simplex-chat-nodejs && \ npm install --no-audit --no-fund && \ - npm install --no-save --no-audit --no-fund ../simplex-chat-client/types/typescript && \ + npm install --no-save --no-audit --no-fund /opt/simplex/packages/*.tgz && \ npx node-gyp rebuild && \ npx tsc && \ - cp src/simplex.* dist/ + cp src/simplex.* dist/ && \ + cp build/Release/simplex.node /opt/simplex/simplex.node && \ + npm pack --pack-destination /opt/simplex/packages + +# --------------------------------------------------------------------------- # +# libs-release: libsimplex and the add-on pinned by the published simplex-chat +# --------------------------------------------------------------------------- # +FROM node:${NODE}-bookworm-slim AS libs-release + +ARG BACKEND WORKDIR /src/apps/simplex-support-bot COPY apps/simplex-support-bot/package.json apps/simplex-support-bot/package-lock.json ./ -RUN npm ci --no-audit --no-fund -RUN npm install --no-save --no-audit --no-fund \ - /src/packages/simplex-chat-nodejs \ - /src/packages/simplex-chat-client/types/typescript +RUN npm ci --no-audit --no-fund && \ + XDG_CACHE_HOME=/tmp/cache npx simplex-chat install --backend "${BACKEND}" && \ + mkdir -p /opt/simplex/packages && \ + cp -r "$(dirname "$(find /tmp/cache -name libsimplex.so)")" /opt/simplex/libs && \ + cp "$(find /tmp/cache -name '*.node')" /opt/simplex/simplex.node + +FROM libs-${LIBS} AS libs + +# --------------------------------------------------------------------------- # +# build +# --------------------------------------------------------------------------- # +FROM node:${NODE}-bookworm-slim AS build + +COPY --from=libs /opt/simplex /opt/simplex + +WORKDIR /src/apps/simplex-support-bot +COPY apps/simplex-support-bot/package.json apps/simplex-support-bot/package-lock.json ./ +RUN npm ci --no-audit --no-fund && \ + find /opt/simplex/packages -name '*.tgz' -print0 | \ + xargs -0 -r npm install --no-save --no-audit --no-fund COPY apps/simplex-support-bot ./ RUN npm run build && npm test @@ -105,10 +134,12 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ # A named volume takes this ownership; a bind mount must be owned by uid 1000. RUN mkdir -p /data && chown node:node /data && chmod 0700 /data -COPY --from=build /opt/simplex/libs /opt/simplex/libs -COPY --from=build --chown=node:node /src /src +COPY --from=libs /opt/simplex/libs /opt/simplex/libs +COPY --from=libs /opt/simplex/simplex.node /opt/simplex/simplex.node +COPY --from=build --chown=node:node /src/apps/simplex-support-bot /src/apps/simplex-support-bot +# The library loads these instead of downloading libsimplex and the add-on at startup. ENV SIMPLEX_LIBS_DIR=/opt/simplex/libs \ - SIMPLEX_ADDON_PATH=/src/packages/simplex-chat-nodejs/build/Release/simplex.node + SIMPLEX_ADDON_PATH=/opt/simplex/simplex.node USER node WORKDIR /src/apps/simplex-support-bot @@ -119,4 +150,4 @@ ENV NODE_OPTIONS=--max-old-space-size=8192 # node as PID 1 ignores SIGTERM, and the bot deletes the team group invite link on SIGTERM. ENTRYPOINT ["dumb-init", "--", "node", "dist/index.js"] -CMD ["--team-group", "Support Team", "--state-file", "/data/state.json"] +CMD ["--config", "/etc/support-bot/config.toml"] diff --git a/scripts/support-bot/compose.yml b/scripts/support-bot/compose.yml index 274b5c10eb..8a8238c3c9 100644 --- a/scripts/support-bot/compose.yml +++ b/scripts/support-bot/compose.yml @@ -1,7 +1,8 @@ # network_mode: host makes 127.0.0.1 reach PostgreSQL on the host. # -# PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' \ -# docker compose -f scripts/support-bot/compose.yml up --build +# cp config/config.toml.example config/config.toml +# PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' docker compose up --build +# LIBS=source docker compose build services: support-bot: @@ -9,6 +10,8 @@ services: build: context: ../.. dockerfile: scripts/support-bot/Dockerfile + args: + LIBS: ${LIBS:-release} network_mode: host restart: on-failure:5 environment: @@ -17,16 +20,10 @@ services: - support-bot-data:/data - ./config:/etc/support-bot:ro command: - - "--team-group" - - "Support Team" - - "--state-file" - - "/data/state.json" - - "--db" - - "postgres" + - "--config" + - "/etc/support-bot/config.toml" - "--pg-conn" - "${PG_CONN:?set PG_CONN, e.g. postgres://simplex@127.0.0.1/simplex_support_bot}" - - "--context-file" - - "/etc/support-bot/grok-context.txt" stop_grace_period: 30s volumes: diff --git a/scripts/support-bot/config/.gitkeep b/scripts/support-bot/config/.gitkeep deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/scripts/support-bot/config/config.toml.example b/scripts/support-bot/config/config.toml.example new file mode 100644 index 0000000000..4d08b0dd44 --- /dev/null +++ b/scripts/support-bot/config/config.toml.example @@ -0,0 +1,18 @@ +# Copy to config.toml. Keys are the command-line options in snake_case; +# an option given on the command line takes precedence. +team_group = "SimpleX Support Team" +auto_add_team_members = ["1:Alice"] +context_file = "/etc/support-bot/grok-context.yaml" +state_file = "/data/state.json" +db = "postgres" +timezone = "UTC" + +# A message left out keeps its built-in text. +# {hours} is 24 on weekdays and 48 at weekends in the timezone above. +[messages] +welcome = """ +Hello! This is a *SimpleX team* support bot - not an AI. +Discover public groups: [simplex.chat/directory](https://simplex.chat/directory) + +Please ask any questions about SimpleX Chat.""" +queue = "The team will reply to your message within {hours} hours." From 66b22f6869de08694753a0d73f49b59316fbaf7e Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:28:03 +0000 Subject: [PATCH 06/16] ui: keep default contact preferences when allowing a feature (#8105) * ui: keep default contact preferences when allowing a feature * ui: take the time to disappear only from the contact's preferences --------- Co-authored-by: spaced4ndy-agents <342056592+spaced4ndy-agents@users.noreply.github.com> --- .../Chat/ChatItem/CIFeaturePreferenceView.swift | 2 +- apps/ios/SimpleXChat/ChatTypes.swift | 12 ------------ .../kotlin/chat/simplex/common/model/SimpleXAPI.kt | 12 +----------- 3 files changed, 2 insertions(+), 24 deletions(-) diff --git a/apps/ios/Shared/Views/Chat/ChatItem/CIFeaturePreferenceView.swift b/apps/ios/Shared/Views/Chat/ChatItem/CIFeaturePreferenceView.swift index a334dc45df..58baec1d2e 100644 --- a/apps/ios/Shared/Views/Chat/ChatItem/CIFeaturePreferenceView.swift +++ b/apps/ios/Shared/Views/Chat/ChatItem/CIFeaturePreferenceView.swift @@ -65,7 +65,7 @@ struct CIFeaturePreferenceView: View { func allowFeatureToContact(_ contact: Contact, _ feature: ChatFeature, param: Int? = nil) { Task { do { - let prefs = contactUserPreferencesToPreferences(contact.mergedPreferences).setAllowed(feature, param: param) + let prefs = contact.userPreferences.setAllowed(feature, param: param) if let toContact = try await apiSetContactPrefs(contactId: contact.contactId, preferences: prefs) { await MainActor.run { ChatModel.shared.updateContact(toContact) diff --git a/apps/ios/SimpleXChat/ChatTypes.swift b/apps/ios/SimpleXChat/ChatTypes.swift index e43d1950d0..fc5e3bad94 100644 --- a/apps/ios/SimpleXChat/ChatTypes.swift +++ b/apps/ios/SimpleXChat/ChatTypes.swift @@ -960,18 +960,6 @@ public func fullPreferencesToPreferences(_ fullPreferences: FullPreferences) -> ) } -public func contactUserPreferencesToPreferences(_ contactUserPreferences: ContactUserPreferences) -> Preferences { - Preferences( - timedMessages: contactUserPreferences.timedMessages.userPreference.preference, - fullDelete: contactUserPreferences.fullDelete.userPreference.preference, - reactions: contactUserPreferences.reactions.userPreference.preference, - voice: contactUserPreferences.voice.userPreference.preference, - files: contactUserPreferences.files.userPreference.preference, - calls: contactUserPreferences.calls.userPreference.preference, - commands: contactUserPreferences.commands - ) -} - public protocol Preference: Codable, Equatable, Hashable { var allow: FeatureAllowed { get set } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index 58d85f784b..7d63a69faa 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -2681,7 +2681,7 @@ object ChatController { } suspend fun allowFeatureToContact(rh: Long?, contact: Contact, feature: ChatFeature, param: Int? = null) { - val prefs = contact.mergedPreferences.toPreferences().setAllowed(feature, param = param) + val prefs = contact.userPreferences.setAllowed(feature, param = param) val toContact = apiSetContactPrefs(rh, contact.contactId, prefs) if (toContact != null) { withContext(Dispatchers.Main) { @@ -5939,16 +5939,6 @@ data class ContactUserPreferences( val calls: ContactUserPreference, val commands: List?, ) { - fun toPreferences(): ChatPreferences = ChatPreferences( - timedMessages = timedMessages.userPreference.pref, - fullDelete = fullDelete.userPreference.pref, - reactions = reactions.userPreference.pref, - voice = voice.userPreference.pref, - files = files.userPreference.pref, - calls = calls.userPreference.pref, - commands = commands, - ) - companion object { val sampleData = ContactUserPreferences( timedMessages = ContactUserPreferenceTimed( From 57cee6ca5c0c8e571dcd308c22ed1029c0602e11 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:40:12 +0000 Subject: [PATCH 07/16] android, desktop: show "Enter password in search" after hiding a profile (#8050) Co-authored-by: spaced4ndy-agents <342056592+spaced4ndy-agents@users.noreply.github.com> --- .../kotlin/chat/simplex/common/views/chatlist/UserPicker.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/UserPicker.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/UserPicker.kt index e3f6b92be8..10243338d2 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/UserPicker.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/UserPicker.kt @@ -295,7 +295,7 @@ fun UserPicker( { ModalManager.start.showCustomModal(keyboardCoversBar = false) { close -> val search = rememberSaveable { mutableStateOf("") } - val profileHidden = rememberSaveable { mutableStateOf(false) } + val profileHidden = remember { stateGetOrPut("profileHidden") { false } } val authorized = remember { stateGetOrPut("authorized") { false } } ModalView( { close() }, From 1a931cd8f13967d309fd452a1a18828ff4befa27 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:40:49 +0000 Subject: [PATCH 08/16] ios: remove "LIVE" from a finished live message at once (#8109) Co-authored-by: spaced4ndy-agents <342056592+spaced4ndy-agents@users.noreply.github.com> --- apps/ios/Shared/Views/Chat/ChatItemsMerger.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ios/Shared/Views/Chat/ChatItemsMerger.swift b/apps/ios/Shared/Views/Chat/ChatItemsMerger.swift index 02ea691887..b1d1c1d5cf 100644 --- a/apps/ios/Shared/Views/Chat/ChatItemsMerger.swift +++ b/apps/ios/Shared/Views/Chat/ChatItemsMerger.swift @@ -282,7 +282,7 @@ struct ListItem: Hashable { // using meta.hashValue instead of parts takes much more time so better to use partial meta here func genHash(_ prevRevealed: Bool, _ nextRevealed: Bool) -> String { - "\(item.meta.itemId) \(item.meta.updatedAt.hashValue) \(item.meta.itemEdited) \(item.meta.itemDeleted?.hashValue ?? 0) \(item.meta.itemTimed?.hashValue ?? 0) \(item.meta.itemStatus.hashValue) \(item.meta.sentViaProxy ?? false) \(item.mergeCategory?.hashValue ?? 0) \(chatDirHash(item.chatDir)) \(item.reactions.hashValue) \(item.meta.isRcvNew) \(item.text.hash) \(item.file?.hashValue ?? 0) \(item.quotedItem?.itemId ?? 0) \(unreadBefore) \(prevItem?.id ?? 0) \(chatDirHash(prevItem?.chatDir)) \(prevItem?.mergeCategory?.hashValue ?? 0) \(prevRevealed) \(nextItem?.id ?? 0) \(chatDirHash(nextItem?.chatDir)) \(nextItem?.mergeCategory?.hashValue ?? 0) \(nextItem?.reactions.isEmpty ?? true) \(nextItem?.isOneLine ?? false) \(nextRevealed)" + "\(item.meta.itemId) \(item.meta.updatedAt.hashValue) \(item.meta.itemEdited) \(item.meta.isLive) \(item.meta.itemDeleted?.hashValue ?? 0) \(item.meta.itemTimed?.hashValue ?? 0) \(item.meta.itemStatus.hashValue) \(item.meta.sentViaProxy ?? false) \(item.mergeCategory?.hashValue ?? 0) \(chatDirHash(item.chatDir)) \(item.reactions.hashValue) \(item.meta.isRcvNew) \(item.text.hash) \(item.file?.hashValue ?? 0) \(item.quotedItem?.itemId ?? 0) \(unreadBefore) \(prevItem?.id ?? 0) \(chatDirHash(prevItem?.chatDir)) \(prevItem?.mergeCategory?.hashValue ?? 0) \(prevRevealed) \(nextItem?.id ?? 0) \(chatDirHash(nextItem?.chatDir)) \(nextItem?.mergeCategory?.hashValue ?? 0) \(nextItem?.reactions.isEmpty ?? true) \(nextItem?.isOneLine ?? false) \(nextRevealed)" } } From f7c91cbeb99974ffe56de7b1203c29be56aaf534 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:55:05 +0000 Subject: [PATCH 09/16] ui: fix chat rows showing the date 01/01/70 (#8055) * core: fix chat rows showing the date 01/01/70 * ios: show favourite, mute and unread icons on chat rows without items * core: remove an outdated comment from a chat list test * ios: add a preview of a chat row without items marked unread * ios: place the icons of a chat row without items at the row's end * ios: references of the chat row without items * core, ios: undo leaving the chat banner out of chat list previews * ui: fix chat rows showing the date 01/01/70 --------- Co-authored-by: spaced4ndy-agents <342056592+spaced4ndy-agents@users.noreply.github.com> --- apps/ios/Shared/Views/ChatList/ChatPreviewView.swift | 2 +- .../chat/simplex/common/views/chatlist/ChatPreviewView.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift b/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift index 70cacf5bc3..cfb88c773b 100644 --- a/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift +++ b/apps/ios/Shared/Views/ChatList/ChatPreviewView.swift @@ -37,7 +37,7 @@ struct ChatPreviewView: View { } .padding(.leading, 4) - let chatTs = if let cItem { + let chatTs = if let cItem, cItem.content != .chatBanner { cItem.meta.itemTs } else { chat.chatInfo.chatTs diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt index 9392889552..48e09cb67a 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatPreviewView.kt @@ -415,7 +415,7 @@ fun ChatPreviewView( chatPreviewTitle() } Spacer(Modifier.width(8.sp.toDp())) - val ts = getTimestampText(chat.chatItems.lastOrNull()?.meta?.itemTs ?: chat.chatInfo.chatTs) + val ts = getTimestampText(chat.chatItems.lastOrNull()?.takeIf { it.content !is CIContent.ChatBanner }?.meta?.itemTs ?: chat.chatInfo.chatTs) ChatListTimestampView(ts) } Row(Modifier.heightIn(min = 46.sp.toDp()).fillMaxWidth()) { From 3469bc75144db8fe506b1b9a41e30b64e237d780 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:57:32 +0000 Subject: [PATCH 10/16] ui: update list filters when a chat is added (#8107) * ui: update list filters when a chat is added * ui: count list filters on the main thread and after chat list reloads * android, desktop: check and add a known chat on the main thread * docs: list filter counts in the apps' specs * docs: correct the list filter gaps in the apps' specs * ui: revert the recounts, replaceChat change and main-thread moves * docs: revert the list filter notes in the apps' specs --------- Co-authored-by: spaced4ndy-agents <342056592+spaced4ndy-agents@users.noreply.github.com> --- apps/ios/Shared/Model/ChatModel.swift | 2 +- .../commonMain/kotlin/chat/simplex/common/model/ChatModel.kt | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/ios/Shared/Model/ChatModel.swift b/apps/ios/Shared/Model/ChatModel.swift index 1e9205473d..c2004bc10b 100644 --- a/apps/ios/Shared/Model/ChatModel.swift +++ b/apps/ios/Shared/Model/ChatModel.swift @@ -612,6 +612,7 @@ final class ChatModel: ObservableObject { } else { addChat_(chat, at: 0) } + ChatTagsModel.shared.addPresetChatTags(chat.chatInfo, chat.chatStats) popChatCollector.throttlePopChat(chat.chatInfo.id, currentPosition: 0) } @@ -656,7 +657,6 @@ final class ChatModel: ObservableObject { updateChatInfo(cInfo) } else if addMissing { addChat(Chat(chatInfo: cInfo, chatItems: [])) - ChatTagsModel.shared.addPresetChatTags(cInfo, ChatStats()) } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt index b7655a2d27..0d5e97caaf 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/ChatModel.kt @@ -458,6 +458,9 @@ object ChatModel { suspend fun addChat(chat: Chat) { chats.add(index = 0, chat) + if (secondaryContextFilter == null) { + addPresetChatTags(chat.chatInfo, chat.chatStats) + } popChatCollector.throttlePopChat(chat.remoteHostId, chat.id, currentPosition = 0) } @@ -519,7 +522,6 @@ object ChatModel { updateChatInfo(rhId, cInfo) } else if (addMissing) { addChat(Chat(remoteHostId = rhId, chatInfo = cInfo, chatItems = arrayListOf())) - addPresetChatTags(cInfo, Chat.ChatStats()) } } From d94981224a366a433f2d42388effff720419f9fd Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:38:56 +0000 Subject: [PATCH 11/16] docs: names conditions of use (#8135) * docs: names conditions of use * docs: capitalize SimpleX Network * docs: state transfer rule after registration ends --- docs/NAMES-CONDITIONS.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 docs/NAMES-CONDITIONS.md diff --git a/docs/NAMES-CONDITIONS.md b/docs/NAMES-CONDITIONS.md new file mode 100644 index 0000000000..ad244fe752 --- /dev/null +++ b/docs/NAMES-CONDITIONS.md @@ -0,0 +1,25 @@ +# SimpleX Names Conditions of Use + +You accept these Conditions of Use of SimpleX Names ("Names Conditions") by registering, receiving or extending a name in the .simplex namespace. + +**SimpleX names**. A name is a record controlled by your key. The record can contain links to addresses in the SimpleX Network. "Community names" are marked in the registry as referring to specific communities, usually cities or territories. + +**Registry**. The .simplex names registry is a set of contracts on the Ethereum blockchain. Name registrations, records, subnames and controlling addresses are stored in immutable contracts. Other contracts may be updated during a stabilization period, expected to last 12 to 24 months from the first registration. + +**What the registry allows**. + +- Changing the record of the name, creating subnames and transferring control of the name require your key. +- A name is registered for the period stored in the registry and can be extended until 90 days after that period ends, at the price set at the time of extension. Once the period ends, control of the name cannot be transferred. + +**You agree**: + +- not to use a name you control for fraud, to impersonate people or organizations, to store links to illegal content, or to knowingly infringe the rights of others, including trademarks; +- to use a community name you control to benefit that community, not as the brand of one person or business. + +You are responsible for the names you control, their records and subnames. A name gives you no rights to the word itself, or to any word marks or other intellectual property. + +**Your key**. Keep your key safe. If you lose your key, nobody can recover the key or control of the name, and nobody can reverse a transfer made with the key. + +**Public information**. Everything in the registry is public with its history, including controlling addresses and acceptances of these Names Conditions. + +**Changes to the conditions**. New versions of these Names Conditions are recorded in the registry. Each version applies to registrations, transfers and extensions made after it is recorded. A version that does not reduce your rights applies at once. From 9764699e0fca7c6d226331f3043aadf80ba3a945 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Wed, 11 Nov 2026 21:00:26 +0000 Subject: [PATCH 12/16] core: 7.1.3.0, types: 7.1.3 (#8133) * core: 7.1.3.0 * types: 7.1.3 --- packages/simplex-chat-client/types/typescript/README.md | 2 +- packages/simplex-chat-client/types/typescript/package.json | 4 ++-- simplex-chat.cabal | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/simplex-chat-client/types/typescript/README.md b/packages/simplex-chat-client/types/typescript/README.md index 314629f8b5..bd38d9eec9 100644 --- a/packages/simplex-chat-client/types/typescript/README.md +++ b/packages/simplex-chat-client/types/typescript/README.md @@ -4,7 +4,7 @@ This TypeScript library provides auto-generated types for bots API: commands and It is used in [simplex-chat](https://www.npmjs.com/package/simplex-chat) Node.js library. -SimpleX Chat core: 7.1.0.18 +SimpleX Chat core: 7.1.3.0 [API reference](https://github.com/simplex-chat/simplex-chat/tree/stable/bots). diff --git a/packages/simplex-chat-client/types/typescript/package.json b/packages/simplex-chat-client/types/typescript/package.json index 1f7da2e61f..15e1ec87d8 100644 --- a/packages/simplex-chat-client/types/typescript/package.json +++ b/packages/simplex-chat-client/types/typescript/package.json @@ -1,7 +1,7 @@ { "name": "@simplex-chat/types", - "version": "7.1.0-beta.9.1", - "simplexChatCore": "7.1.0.18", + "version": "7.1.3", + "simplexChatCore": "7.1.3.0", "description": "TypeScript types for SimpleX Chat bot libraries", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/simplex-chat.cabal b/simplex-chat.cabal index 7f20efe840..6a1cbe056b 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: simplex-chat -version: 7.1.2.0 +version: 7.1.3.0 category: Web, System, Services, Cryptography homepage: https://github.com/simplex-chat/simplex-chat#readme author: simplex.chat From 43e8426732bbfa932479aebefff5f518ad680155 Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Thu, 12 Nov 2026 06:46:01 +0000 Subject: [PATCH 13/16] support bot: roll back migrations with --dry-run, connection in config --- apps/simplex-support-bot/README.md | 17 +++++-- apps/simplex-support-bot/bot.test.ts | 46 ++++++++++++++++++- apps/simplex-support-bot/src/config.ts | 5 ++ apps/simplex-support-bot/src/dryRun.ts | 33 ++++++++++++- apps/simplex-support-bot/src/index.ts | 30 ++++-------- .../test/__mocks__/simplex-chat.js | 2 +- scripts/support-bot/compose.yml | 7 +-- .../support-bot/config/config.toml.example | 1 + 8 files changed, 108 insertions(+), 33 deletions(-) diff --git a/apps/simplex-support-bot/README.md b/apps/simplex-support-bot/README.md index f67e6bb739..031d9f0cd5 100644 --- a/apps/simplex-support-bot/README.md +++ b/apps/simplex-support-bot/README.md @@ -59,11 +59,12 @@ Run `npm start -- --help` for the auto-generated reference. Summary: | `--context-file` | both | required with `GROK_API_KEY` | | text file with Grok system context | | `--dry-run` | both | no | | check config, database and state, then exit | | `--allow-migrations` | both | no | | with `--dry-run`: apply pending migrations instead of reporting them | +| `--allow-down-migrations` | both | no | | with `--dry-run`: roll back migrations that this build does not have | | `-h` / `--help` | both | no | | show usage and exit | ## Config file -`--config config.toml` reads option values and message texts from a TOML file, as [config.toml.example](../../scripts/support-bot/config/config.toml.example) shows. Keys are the command-line options in snake_case (`team_group` for `--team-group`); a list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run` and `--allow-migrations` are command-line only. +`--config config.toml` reads option values and message texts from a TOML file, as [config.toml.example](../../scripts/support-bot/config/config.toml.example) shows. Keys are the command-line options in snake_case (`team_group` for `--team-group`); a list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run`, `--allow-migrations` and `--allow-down-migrations` are command-line only. The file holds the `pg_conn` password, so keep it readable only by the user that runs the bot. A message left out of `[messages]` keeps its built-in text. `{hours}` is replaced with 24 on weekdays and 48 at weekends in `timezone`. The welcome message is updated in the bot address on the next start. @@ -105,11 +106,11 @@ A message left out of `[messages]` keeps its built-in text. `{hours}` is replace ```bash cd scripts/support-bot cp config/config.toml.example config/config.toml -PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' docker compose up --build +docker compose up --build LIBS=source docker compose build ``` -`scripts/support-bot/config` is mounted at `/etc/support-bot` and holds `config.toml` and the Grok context file. `GROK_API_KEY` is read from the environment. +`scripts/support-bot/config` is mounted at `/etc/support-bot` and holds `config.toml`, with `pg_conn`, and the Grok context file; the bot runs as uid 1000, which must be able to read them. `GROK_API_KEY` is read from the environment. Other arguments are passed after the service name, for example `docker compose run --rm support-bot --config /etc/support-bot/config.toml --dry-run`. ## Dry run @@ -130,11 +131,19 @@ FAIL team member 3:alice has display name "bob", the bot would exit A pending migration stops the checks: ``` -FAIL database: 2 migration(s) pending, a real start would apply them: 20261019_chat_layout, 20261021_badge_subscriptions +FAIL database: pending 2 migration(s): 20261019_chat_layout, 20261021_badge_subscriptions; a real start would apply them, --allow-migrations applies them now ``` With `--allow-migrations`, the migrations are applied first and the checks run after them. Run it against a restored copy of the database unless the migration is intended, and with the bot stopped. +A database migrated by a newer build also stops the checks, since the bot refuses to start on it: + +``` +FAIL database: database is newer than this build, to roll back 1 migration(s): 20260905_feeds; a real start would refuse, --allow-down-migrations rolls them back +``` + +With `--allow-down-migrations`, those migrations are rolled back with the down SQL that the newer build stored in the database, and the checks run after it. The core rolls back only when this build's migrations are a prefix of the database's; a database whose migrations diverge from this build's is reported as a migration state error and is not changed. Rolling back deletes the data of the removed migrations, so take a backup first. + A line is `ok`, `FAIL` or `?`. `?` marks an id absent from the chat previews: with chat stopped, only chats that were used are listed, so the bot verifies such ids at startup. The exit code is 0 when no line is `FAIL`, 1 otherwise. The checks cover the database connection and migrations, the persisted team group, Grok user and Grok contact, the `--auto-add-team-members` ids and names, and the `--context-file`. diff --git a/apps/simplex-support-bot/bot.test.ts b/apps/simplex-support-bot/bot.test.ts index 25a91fce90..640b10069f 100644 --- a/apps/simplex-support-bot/bot.test.ts +++ b/apps/simplex-support-bot/bot.test.ts @@ -8,7 +8,7 @@ import {CardManager} from "./src/cards.js" import {parseConfig} from "./src/config.js" import {GrokApiClient} from "./src/grok.js" import {loadGrokContext} from "./src/context.js" -import {dryRun} from "./src/dryRun.js" +import {dryRun, migrationPlan} from "./src/dryRun.js" import {defaultMessages, queueMessage, teamAddedMessage, noTeamMembersMessage} from "./src/messages.js" // Silence console output during tests @@ -2484,6 +2484,13 @@ describe("parseConfig Validation", () => { .toThrow(/--allow-migrations requires --dry-run/) }) + test("--allow-down-migrations defaults to false, parses with --dry-run, throws without it", () => { + expect(parseConfig(baseArgs).allowDownMigrations).toBe(false) + expect(parseConfig([...baseArgs, "--dry-run", "--allow-down-migrations"]).allowDownMigrations).toBe(true) + expect(() => parseConfig([...baseArgs, "--allow-down-migrations"])) + .toThrow(/--allow-down-migrations requires --dry-run/) + }) + describe("--config TOML file", () => { const configFile = (toml: string): string => { const path = join(mkdtempSync(join(tmpdir(), "support-bot-config-")), "bot.toml") @@ -2871,6 +2878,43 @@ describe("Dry Run", () => { expect(await dryRun(chat, config({contextFile: path}), {teamGroupId: 1})).toBe(false) }) + describe("migrationPlan", () => { + const allow = (allowMigrations: boolean, allowDownMigrations: boolean) => ({allowMigrations, allowDownMigrations}) + const upgrade = {type: "upgrade" as const, names: ["20261019_chat_layout", "20261021_badge_subscriptions"]} + const downgrade = {type: "downgrade" as const, names: ["20260905_feeds"]} + + test("current schema → opens without migrating", () => { + expect(migrationPlan({type: "current"}, allow(false, false))) + .toEqual({ok: true, confirm: "error", result: "schema up to date"}) + }) + + test("pending up migrations → fail without --allow-migrations, YesUp with it", () => { + expect(migrationPlan(upgrade, allow(false, true))).toEqual({ + ok: false, + reason: "pending 2 migration(s): 20261019_chat_layout, 20261021_badge_subscriptions; a real start would apply them, --allow-migrations applies them now", + }) + expect(migrationPlan(upgrade, allow(true, false))).toEqual({ + ok: true, + confirm: "yesUp", + result: "applied 2 migration(s): 20261019_chat_layout, 20261021_badge_subscriptions", + }) + }) + + test("down migrations → fail without --allow-down-migrations, YesUpDown with it", () => { + expect(migrationPlan(downgrade, allow(true, false))).toEqual({ + ok: false, + reason: "database is newer than this build, to roll back 1 migration(s): 20260905_feeds; a real start would refuse, --allow-down-migrations rolls them back", + }) + expect(migrationPlan(downgrade, allow(false, true))) + .toEqual({ok: true, confirm: "yesUpDown", result: "rolled back 1 migration(s): 20260905_feeds"}) + }) + + test("probe failure → fails with its text whatever is allowed", () => { + expect(migrationPlan({type: "failed", text: "connection refused"}, allow(true, true))) + .toEqual({ok: false, reason: "connection refused"}) + }) + }) + test("missing context file → reported, passes", async () => { const chat = mkChat([groupChat(1, "Support Team")]) const cfg = config({contextFile: join(tmpdir(), "no-such-context.txt")}) diff --git a/apps/simplex-support-bot/src/config.ts b/apps/simplex-support-bot/src/config.ts index 1823875e06..e9da119290 100644 --- a/apps/simplex-support-bot/src/config.ts +++ b/apps/simplex-support-bot/src/config.ts @@ -21,6 +21,7 @@ export interface Config { contextFile: string | null dryRun: boolean allowMigrations: boolean + allowDownMigrations: boolean grokApiKey: string | null messages: Messages } @@ -62,6 +63,7 @@ function buildCommand(): Command { .option("--context-file ", "text file with Grok system context (required if GROK_API_KEY set)") .option("--dry-run", "check config, database and state, then exit without starting chat") .option("--allow-migrations", "with --dry-run: apply pending migrations instead of reporting them") + .option("--allow-down-migrations", "with --dry-run: roll back migrations that this build does not have") .addHelpText("after", "\nEnvironment:\n GROK_API_KEY xAI API key — enables Grok replies\n SIMPLEX_LIBS_DIR local libsimplex build for the --db backend\n SIMPLEX_ADDON_PATH locally built Node.js add-on\n") } @@ -80,6 +82,7 @@ interface RawOpts { contextFile?: string dryRun?: boolean allowMigrations?: boolean + allowDownMigrations?: boolean } interface ConfigFile { @@ -159,6 +162,7 @@ export function parseConfig(args: string[]): Config { } if (opts.allowMigrations && !opts.dryRun) throw new Error("--allow-migrations requires --dry-run") + if (opts.allowDownMigrations && !opts.dryRun) throw new Error("--allow-down-migrations requires --dry-run") const teamGroup: IdName = {id: 0, name: opts.teamGroup} @@ -190,6 +194,7 @@ export function parseConfig(args: string[]): Config { contextFile, dryRun: opts.dryRun ?? false, allowMigrations: opts.allowMigrations ?? false, + allowDownMigrations: opts.allowDownMigrations ?? false, grokApiKey, messages: file.messages, } diff --git a/apps/simplex-support-bot/src/dryRun.ts b/apps/simplex-support-bot/src/dryRun.ts index a04128cbc5..b0fe4b0a87 100644 --- a/apps/simplex-support-bot/src/dryRun.ts +++ b/apps/simplex-support-bot/src/dryRun.ts @@ -1,4 +1,4 @@ -import {api} from "simplex-chat" +import {api, core} from "simplex-chat" import {T} from "@simplex-chat/types" import {Config, IdName} from "./config.js" import {loadGrokContext} from "./context.js" @@ -9,6 +9,37 @@ export interface DryRunState { grokUserId?: number } +export type DbProbe = + | {type: "current"} + | {type: "upgrade"; names: string[]} + | {type: "downgrade"; names: string[]} + | {type: "failed"; text: string} + +export type MigrationPlan = + | {ok: true; confirm: core.MigrationConfirmation; result: string} + | {ok: false; reason: string} + +export function migrationPlan(probe: DbProbe, config: Pick): MigrationPlan { + switch (probe.type) { + case "current": + return {ok: true, confirm: core.MigrationConfirmation.Error, result: "schema up to date"} + case "upgrade": { + const list = `${probe.names.length} migration(s): ${probe.names.join(", ")}` + return config.allowMigrations + ? {ok: true, confirm: core.MigrationConfirmation.YesUp, result: `applied ${list}`} + : {ok: false, reason: `pending ${list}; a real start would apply them, --allow-migrations applies them now`} + } + case "downgrade": { + const list = `${probe.names.length} migration(s): ${probe.names.join(", ")}` + return config.allowDownMigrations + ? {ok: true, confirm: core.MigrationConfirmation.YesUpDown, result: `rolled back ${list}`} + : {ok: false, reason: `database is newer than this build, to roll back ${list}; a real start would refuse, --allow-down-migrations rolls them back`} + } + case "failed": + return {ok: false, reason: probe.text} + } +} + const SEARCH_LIMIT = 100 const LISTING_LIMIT = 1000 const GROK_PROFILE_NAME = "Grok" diff --git a/apps/simplex-support-bot/src/index.ts b/apps/simplex-support-bot/src/index.ts index 373f819115..ef289ecc65 100644 --- a/apps/simplex-support-bot/src/index.ts +++ b/apps/simplex-support-bot/src/index.ts @@ -3,7 +3,7 @@ import {api, bot, core, util} from "simplex-chat" import {T} from "@simplex-chat/types" import {Config, parseConfig} from "./config.js" import {SupportBot} from "./bot.js" -import {dryRun} from "./dryRun.js" +import {DbProbe, dryRun, migrationPlan} from "./dryRun.js" import {GrokApiClient, GrokMessage} from "./grok.js" import {loadGrokContext} from "./context.js" import {profileMutex, log, logError, getGroupInfo, getContact} from "./util.js" @@ -375,29 +375,19 @@ async function main(): Promise { } async function runDryRun(config: Config, state: BotState): Promise { - const probe = await probeDatabase(config.db) - if (probe.type === "failed") { - console.log(`FAIL database: ${probe.text}`) + const plan = migrationPlan(await probeDatabase(config.db), config) + if (!plan.ok) { + console.log(`FAIL database: ${plan.reason}`) return false } - const pending = probe.type === "pending" ? probe.names : [] - if (pending.length > 0 && !config.allowMigrations) { - console.log(`FAIL database: ${pending.length} migration(s) pending, a real start would apply them: ${pending.join(", ")}`) - return false - } - const migrating = pending.length > 0 let chat: api.ChatApi try { - chat = await api.ChatApi.init( - config.db, - migrating ? core.MigrationConfirmation.YesUp : core.MigrationConfirmation.Error - ) + chat = await api.ChatApi.init(config.db, plan.confirm) } catch (err) { console.log(`FAIL database: ${migrationErrorText(err)}`) return false } - const schemaState = migrating ? `applied ${pending.length} migration(s): ${pending.join(", ")}` : "schema up to date" - console.log(`ok database ${config.db.type} opened, ${schemaState}`) + console.log(`ok database ${config.db.type} opened, ${plan.result}`) try { return await dryRun(chat, config, state) } finally { @@ -405,8 +395,6 @@ async function runDryRun(config: Config, state: BotState): Promise { } } -type DbProbe = {type: "current"} | {type: "pending"; names: string[]} | {type: "failed"; text: string} - async function probeDatabase(db: api.DbConfig): Promise { try { const chat = await api.ChatApi.init(db, core.MigrationConfirmation.Error) @@ -414,8 +402,10 @@ async function probeDatabase(db: api.DbConfig): Promise { return {type: "current"} } catch (err) { const dbErr = (err as core.ChatInitError).dbMigrationError - if (dbErr?.type === "errorMigration" && dbErr.migrationError.type === "upgrade") { - return {type: "pending", names: dbErr.migrationError.upMigrations.map(m => m.upName)} + if (dbErr?.type === "errorMigration") { + const me = dbErr.migrationError + if (me.type === "upgrade") return {type: "upgrade", names: me.upMigrations.map(m => m.upName)} + if (me.type === "downgrade") return {type: "downgrade", names: me.downMigrations} } return {type: "failed", text: migrationErrorText(err)} } diff --git a/apps/simplex-support-bot/test/__mocks__/simplex-chat.js b/apps/simplex-support-bot/test/__mocks__/simplex-chat.js index 92e05a4178..974f0deb38 100644 --- a/apps/simplex-support-bot/test/__mocks__/simplex-chat.js +++ b/apps/simplex-support-bot/test/__mocks__/simplex-chat.js @@ -31,6 +31,6 @@ class ChatAPIError extends Error { module.exports = { api: {ChatApi: {}}, bot: {}, - core: {ChatAPIError}, + core: {ChatAPIError, MigrationConfirmation: {YesUp: "yesUp", YesUpDown: "yesUpDown", Console: "console", Error: "error"}}, util: {ciContentText, ciBotCommand, contactAddressStr}, } diff --git a/scripts/support-bot/compose.yml b/scripts/support-bot/compose.yml index 8a8238c3c9..70f8114aaa 100644 --- a/scripts/support-bot/compose.yml +++ b/scripts/support-bot/compose.yml @@ -1,7 +1,7 @@ # network_mode: host makes 127.0.0.1 reach PostgreSQL on the host. # # cp config/config.toml.example config/config.toml -# PG_CONN='postgres://simplex@127.0.0.1/simplex_support_bot' docker compose up --build +# docker compose up --build # LIBS=source docker compose build services: @@ -19,11 +19,6 @@ services: volumes: - support-bot-data:/data - ./config:/etc/support-bot:ro - command: - - "--config" - - "/etc/support-bot/config.toml" - - "--pg-conn" - - "${PG_CONN:?set PG_CONN, e.g. postgres://simplex@127.0.0.1/simplex_support_bot}" stop_grace_period: 30s volumes: diff --git a/scripts/support-bot/config/config.toml.example b/scripts/support-bot/config/config.toml.example index 4d08b0dd44..89f3c2d01f 100644 --- a/scripts/support-bot/config/config.toml.example +++ b/scripts/support-bot/config/config.toml.example @@ -5,6 +5,7 @@ auto_add_team_members = ["1:Alice"] context_file = "/etc/support-bot/grok-context.yaml" state_file = "/data/state.json" db = "postgres" +pg_conn = "postgres://simplex@127.0.0.1/simplex_support_bot" timezone = "UTC" # A message left out keeps its built-in text. From 1eb80416440bd46298094ae68bb4dbe9e866ae02 Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Thu, 12 Nov 2026 06:50:14 +0000 Subject: [PATCH 14/16] support bot: Grok API key in config --- apps/simplex-support-bot/README.md | 10 ++--- apps/simplex-support-bot/bot.test.ts | 39 ++++++++++++++++++- apps/simplex-support-bot/src/bot.ts | 2 +- apps/simplex-support-bot/src/config.ts | 17 +++++--- apps/simplex-support-bot/src/index.ts | 2 +- .../support-bot/config/config.toml.example | 2 + 6 files changed, 58 insertions(+), 14 deletions(-) diff --git a/apps/simplex-support-bot/README.md b/apps/simplex-support-bot/README.md index 031d9f0cd5..a105aa5718 100644 --- a/apps/simplex-support-bot/README.md +++ b/apps/simplex-support-bot/README.md @@ -5,7 +5,7 @@ A business-address bot that triages incoming support chats, optionally runs them ## Prerequisites - Node.js v18 or newer (v24 tested) -- `GROK_API_KEY` env var (xAI) — optional; the bot runs without it +- an xAI API key in `GROK_API_KEY` or in `grok_api_key` of the config file — optional; the bot runs without it - For the PostgreSQL backend: Linux x86_64, `libpq5` installed on the host, and a reachable PostgreSQL server ## Install & build @@ -56,7 +56,7 @@ Run `npm start -- --help` for the auto-generated reference. Summary: | `--timezone` | both | no | `UTC` | IANA zone for weekend detection | | `--complete-hours` | both | no | `3` | auto-complete chats after N hours idle (`0` disables) | | `--card-flush-seconds` | both | no | `300` | debounce card state writes | -| `--context-file` | both | required with `GROK_API_KEY` | | text file with Grok system context | +| `--context-file` | both | required with a Grok API key | | text file with Grok system context | | `--dry-run` | both | no | | check config, database and state, then exit | | `--allow-migrations` | both | no | | with `--dry-run`: apply pending migrations instead of reporting them | | `--allow-down-migrations` | both | no | | with `--dry-run`: roll back migrations that this build does not have | @@ -64,7 +64,7 @@ Run `npm start -- --help` for the auto-generated reference. Summary: ## Config file -`--config config.toml` reads option values and message texts from a TOML file, as [config.toml.example](../../scripts/support-bot/config/config.toml.example) shows. Keys are the command-line options in snake_case (`team_group` for `--team-group`); a list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run`, `--allow-migrations` and `--allow-down-migrations` are command-line only. The file holds the `pg_conn` password, so keep it readable only by the user that runs the bot. +`--config config.toml` reads option values and message texts from a TOML file, as [config.toml.example](../../scripts/support-bot/config/config.toml.example) shows. Keys are the command-line options in snake_case (`team_group` for `--team-group`); a list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run`, `--allow-migrations` and `--allow-down-migrations` are command-line only. `grok_api_key` is file-only, so that the key is not visible in the process list; `GROK_API_KEY` takes precedence over it. The file holds the `pg_conn` password and the Grok key, so keep it readable only by the user that runs the bot. A message left out of `[messages]` keeps its built-in text. `{hours}` is replaced with 24 on weekdays and 48 at weekends in `timezone`. The welcome message is updated in the bot address on the next start. @@ -89,7 +89,7 @@ A message left out of `[messages]` keeps its built-in text. `{hours}` is replace | Var | Purpose | |---|---| -| `GROK_API_KEY` | xAI API key; enables Grok replies | +| `GROK_API_KEY` | xAI API key; enables Grok replies; takes precedence over `grok_api_key` of the config file | | `SIMPLEX_LIBS_DIR` | directory with a local libsimplex build for the `--db` backend, instead of the downloaded one | | `SIMPLEX_ADDON_PATH` | locally built Node.js add-on (`simplex.node`), instead of the downloaded one | | `NODE_OPTIONS` | `--max-old-space-size=8192` on a bot with many chats: single API responses outgrow the default heap | @@ -110,7 +110,7 @@ docker compose up --build LIBS=source docker compose build ``` -`scripts/support-bot/config` is mounted at `/etc/support-bot` and holds `config.toml`, with `pg_conn`, and the Grok context file; the bot runs as uid 1000, which must be able to read them. `GROK_API_KEY` is read from the environment. Other arguments are passed after the service name, for example `docker compose run --rm support-bot --config /etc/support-bot/config.toml --dry-run`. +`scripts/support-bot/config` is mounted at `/etc/support-bot` and holds `config.toml`, with `pg_conn` and `grok_api_key`, and the Grok context file; the bot runs as uid 1000, which must be able to read them. Other arguments are passed after the service name, for example `docker compose run --rm support-bot --config /etc/support-bot/config.toml --dry-run`. ## Dry run diff --git a/apps/simplex-support-bot/bot.test.ts b/apps/simplex-support-bot/bot.test.ts index 640b10069f..c24f0e0790 100644 --- a/apps/simplex-support-bot/bot.test.ts +++ b/apps/simplex-support-bot/bot.test.ts @@ -1,4 +1,4 @@ -import {describe, test, expect, beforeEach, vi} from "vitest" +import {describe, test, expect, beforeEach, afterEach, vi} from "vitest" import {mkdtempSync, writeFileSync} from "fs" import {tmpdir} from "os" import {join} from "path" @@ -2559,6 +2559,43 @@ describe("parseConfig Validation", () => { .toThrow(/messages must be a table/) }) + describe("grok_api_key", () => { + const savedKey = process.env.GROK_API_KEY + beforeEach(() => { delete process.env.GROK_API_KEY }) + afterEach(() => { + if (savedKey === undefined) delete process.env.GROK_API_KEY + else process.env.GROK_API_KEY = savedKey + }) + const withKey = (extra = "") => configFile(`team_group = "T"\ncontext_file = "ctx.yaml"\ngrok_api_key = "file-key"${extra}`) + + test("is read from the file", () => { + expect(parseConfig(["--config", withKey()]).grokApiKey).toBe("file-key") + }) + + test("GROK_API_KEY takes precedence; an empty GROK_API_KEY does not", () => { + process.env.GROK_API_KEY = "env-key" + expect(parseConfig(["--config", withKey()]).grokApiKey).toBe("env-key") + process.env.GROK_API_KEY = "" + expect(parseConfig(["--config", withKey()]).grokApiKey).toBe("file-key") + }) + + test("requires a context file", () => { + expect(() => parseConfig(["--config", configFile(`team_group = "T"\ngrok_api_key = "file-key"`)])) + .toThrow(/A Grok API key is set but --context-file is not provided/) + }) + + test("must be a non-empty string", () => { + expect(() => parseConfig(["--config", configFile(`team_group = "T"\ngrok_api_key = ""`)])) + .toThrow(/grok_api_key must be a non-empty string/) + expect(() => parseConfig(["--config", configFile(`team_group = "T"\ngrok_api_key = 1`)])) + .toThrow(/grok_api_key must be a non-empty string/) + }) + + test("absent from file and environment → Grok disabled", () => { + expect(parseConfig(["--config", configFile(`team_group = "T"`)]).grokApiKey).toBeNull() + }) + }) + test("malformed TOML → throws", () => { expect(() => parseConfig(["--config", configFile(`team_group = "unclosed`)])).toThrow() }) diff --git a/apps/simplex-support-bot/src/bot.ts b/apps/simplex-support-bot/src/bot.ts index 6fb4b96599..f3e7bea481 100644 --- a/apps/simplex-support-bot/src/bot.ts +++ b/apps/simplex-support-bot/src/bot.ts @@ -152,7 +152,7 @@ export class SupportBot { } private async withGrokProfile(fn: () => Promise): Promise { - if (this.grokUserId === null) throw new Error("Grok is disabled (no GROK_API_KEY)") + if (this.grokUserId === null) throw new Error("Grok is disabled (no Grok API key)") const grokUserId = this.grokUserId return profileMutex.runExclusive(async () => { await this.chat.apiSetActiveUser(grokUserId) diff --git a/apps/simplex-support-bot/src/config.ts b/apps/simplex-support-bot/src/config.ts index e9da119290..d268b71c54 100644 --- a/apps/simplex-support-bot/src/config.ts +++ b/apps/simplex-support-bot/src/config.ts @@ -60,11 +60,11 @@ function buildCommand(): Command { .option("--timezone ", "IANA timezone for weekend detection", "UTC") .option("--complete-hours ", "auto-complete chats after N hours idle (0 disables)", parseNonNegativeInt("--complete-hours"), 3) .option("--card-flush-seconds ", "debounce card state writes", parseNonNegativeInt("--card-flush-seconds"), 300) - .option("--context-file ", "text file with Grok system context (required if GROK_API_KEY set)") + .option("--context-file ", "text file with Grok system context (required with a Grok API key)") .option("--dry-run", "check config, database and state, then exit without starting chat") .option("--allow-migrations", "with --dry-run: apply pending migrations instead of reporting them") .option("--allow-down-migrations", "with --dry-run: roll back migrations that this build does not have") - .addHelpText("after", "\nEnvironment:\n GROK_API_KEY xAI API key — enables Grok replies\n SIMPLEX_LIBS_DIR local libsimplex build for the --db backend\n SIMPLEX_ADDON_PATH locally built Node.js add-on\n") + .addHelpText("after", "\nEnvironment:\n GROK_API_KEY xAI API key — enables Grok replies, takes precedence over grok_api_key in --config\n SIMPLEX_LIBS_DIR local libsimplex build for the --db backend\n SIMPLEX_ADDON_PATH locally built Node.js add-on\n") } interface RawOpts { @@ -88,6 +88,7 @@ interface RawOpts { interface ConfigFile { args: string[] messages: Messages + grokApiKey: string | null } function configFilePath(args: string[]): string | undefined { @@ -97,10 +98,14 @@ function configFilePath(args: string[]): string | undefined { } function readConfigFile(path: string): ConfigFile { - const {messages = {}, ...options} = parseToml(readFileSync(path, "utf-8")) + const {messages = {}, grok_api_key = null, ...options} = parseToml(readFileSync(path, "utf-8")) + if (grok_api_key !== null && (typeof grok_api_key !== "string" || grok_api_key === "")) { + throw new Error(`${path}: grok_api_key must be a non-empty string`) + } return { args: Object.entries(options).map(([key, value]) => configOptionArg(path, key, value)), messages: configMessages(path, messages), + grokApiKey: grok_api_key, } } @@ -134,7 +139,7 @@ function configMessages(path: string, table: unknown): Messages { export function parseConfig(args: string[]): Config { const cmd = buildCommand().exitOverride() const configFile = configFilePath(args) - const file: ConfigFile = configFile ? readConfigFile(configFile) : {args: [], messages: defaultMessages} + const file: ConfigFile = configFile ? readConfigFile(configFile) : {args: [], messages: defaultMessages, grokApiKey: null} try { cmd.parse([...file.args, ...args], {from: "user"}) } catch (err) { @@ -145,7 +150,7 @@ export function parseConfig(args: string[]): Config { const opts = cmd.opts() const given = (option: keyof RawOpts) => cmd.getOptionValueSource(option) === "cli" - const grokApiKey = process.env.GROK_API_KEY || null + const grokApiKey = process.env.GROK_API_KEY || file.grokApiKey let db: api.DbConfig if (opts.db === "sqlite") { @@ -179,7 +184,7 @@ export function parseConfig(args: string[]): Config { const contextFile = opts.contextFile ?? null if (grokApiKey && !contextFile) { - throw new Error("GROK_API_KEY is set but --context-file is not provided. Grok requires a context file.") + throw new Error("A Grok API key is set but --context-file is not provided. Grok requires a context file.") } return { diff --git a/apps/simplex-support-bot/src/index.ts b/apps/simplex-support-bot/src/index.ts index ef289ecc65..aab7f5f604 100644 --- a/apps/simplex-support-bot/src/index.ts +++ b/apps/simplex-support-bot/src/index.ts @@ -35,7 +35,7 @@ async function main(): Promise { completeHours: config.completeHours, }) const grokEnabled = config.grokApiKey !== null - if (!grokEnabled) log("No GROK_API_KEY provided, disabling Grok support") + if (!grokEnabled) log("No Grok API key in GROK_API_KEY or grok_api_key, disabling Grok support") const stateFilePath = config.stateFile const state = readState(stateFilePath) diff --git a/scripts/support-bot/config/config.toml.example b/scripts/support-bot/config/config.toml.example index 89f3c2d01f..fe05f6d7ad 100644 --- a/scripts/support-bot/config/config.toml.example +++ b/scripts/support-bot/config/config.toml.example @@ -6,6 +6,8 @@ context_file = "/etc/support-bot/grok-context.yaml" state_file = "/data/state.json" db = "postgres" pg_conn = "postgres://simplex@127.0.0.1/simplex_support_bot" +# Enables Grok replies; GROK_API_KEY takes precedence. +# grok_api_key = "xai-..." timezone = "UTC" # A message left out keeps its built-in text. From 8b73d46585e82c4e080bf44239b4646fb540ac2a Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Thu, 12 Nov 2026 07:02:56 +0000 Subject: [PATCH 15/16] support bot: set the core queue size --- apps/simplex-support-bot/README.md | 1 + apps/simplex-support-bot/bot.test.ts | 17 +++++++++++++++++ apps/simplex-support-bot/src/config.ts | 14 ++++++++++++++ apps/simplex-support-bot/src/index.ts | 2 +- scripts/support-bot/config/config.toml.example | 2 ++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/apps/simplex-support-bot/README.md b/apps/simplex-support-bot/README.md index a105aa5718..6274590c69 100644 --- a/apps/simplex-support-bot/README.md +++ b/apps/simplex-support-bot/README.md @@ -56,6 +56,7 @@ Run `npm start -- --help` for the auto-generated reference. Summary: | `--timezone` | both | no | `UTC` | IANA zone for weekend detection | | `--complete-hours` | both | no | `3` | auto-complete chats after N hours idle (`0` disables) | | `--card-flush-seconds` | both | no | `300` | debounce card state writes | +| `--queue-size` | both | no | core default, `1024` | internal queue size of the chat core, as the CLI's `-q`/`--queue-size` | | `--context-file` | both | required with a Grok API key | | text file with Grok system context | | `--dry-run` | both | no | | check config, database and state, then exit | | `--allow-migrations` | both | no | | with `--dry-run`: apply pending migrations instead of reporting them | diff --git a/apps/simplex-support-bot/bot.test.ts b/apps/simplex-support-bot/bot.test.ts index c24f0e0790..6cfe2fa75a 100644 --- a/apps/simplex-support-bot/bot.test.ts +++ b/apps/simplex-support-bot/bot.test.ts @@ -2484,6 +2484,21 @@ describe("parseConfig Validation", () => { .toThrow(/--allow-migrations requires --dry-run/) }) + test("--queue-size is unset by default, so the core default applies", () => { + expect(parseConfig(baseArgs).queueSize).toBeUndefined() + }) + + test("--queue-size parses a positive integer", () => { + expect(parseConfig([...baseArgs, "--queue-size", "65536"]).queueSize).toBe(65536) + }) + + test("--queue-size 0 or non-numeric → throws, as the core rejects 0", () => { + expect(() => parseConfig([...baseArgs, "--queue-size", "0"])) + .toThrow(/--queue-size must be a positive integer, got "0"/) + expect(() => parseConfig([...baseArgs, "--queue-size", "big"])) + .toThrow(/--queue-size must be a positive integer, got "big"/) + }) + test("--allow-down-migrations defaults to false, parses with --dry-run, throws without it", () => { expect(parseConfig(baseArgs).allowDownMigrations).toBe(false) expect(parseConfig([...baseArgs, "--dry-run", "--allow-down-migrations"]).allowDownMigrations).toBe(true) @@ -2503,6 +2518,7 @@ describe("parseConfig Validation", () => { `team_group = "SimpleX Support Team"`, `auto_add_team_members = ["1:Alice", "2:Bob"]`, `complete_hours = 0`, + `queue_size = 65536`, `db = "postgres"`, `pg_conn = "postgres://localhost/db"`, `[messages]`, @@ -2516,6 +2532,7 @@ describe("parseConfig Validation", () => { expect(cfg.teamGroup).toEqual({id: 0, name: "SimpleX Support Team"}) expect(cfg.teamMembers).toEqual([{id: 1, name: "Alice"}, {id: 2, name: "Bob"}]) expect(cfg.completeHours).toBe(0) + expect(cfg.queueSize).toBe(65536) expect(cfg.db).toEqual({type: "postgres", connectionString: "postgres://localhost/db"}) expect(cfg.messages.welcome).toBe("Hello!\n\nAsk anything.") expect(cfg.messages.teamAddedGrok).toBe("Grok helps.") diff --git a/apps/simplex-support-bot/src/config.ts b/apps/simplex-support-bot/src/config.ts index d268b71c54..899113f822 100644 --- a/apps/simplex-support-bot/src/config.ts +++ b/apps/simplex-support-bot/src/config.ts @@ -18,6 +18,7 @@ export interface Config { timezone: string completeHours: number cardFlushSeconds: number + queueSize: number | undefined contextFile: string | null dryRun: boolean allowMigrations: boolean @@ -44,6 +45,16 @@ function parseNonNegativeInt(flag: string) { } } +function parsePositiveInt(flag: string) { + return (raw: string): number => { + const n = parseInt(raw, 10) + if (!Number.isFinite(n) || n < 1) { + throw new Error(`${flag} must be a positive integer, got "${raw}"`) + } + return n + } +} + function buildCommand(): Command { return new Command() .name("simplex-chat-support-bot") @@ -60,6 +71,7 @@ function buildCommand(): Command { .option("--timezone ", "IANA timezone for weekend detection", "UTC") .option("--complete-hours ", "auto-complete chats after N hours idle (0 disables)", parseNonNegativeInt("--complete-hours"), 3) .option("--card-flush-seconds ", "debounce card state writes", parseNonNegativeInt("--card-flush-seconds"), 300) + .option("--queue-size ", "internal queue size of the chat core, as the CLI's --queue-size (core default: 1024)", parsePositiveInt("--queue-size")) .option("--context-file ", "text file with Grok system context (required with a Grok API key)") .option("--dry-run", "check config, database and state, then exit without starting chat") .option("--allow-migrations", "with --dry-run: apply pending migrations instead of reporting them") @@ -79,6 +91,7 @@ interface RawOpts { timezone: string completeHours: number cardFlushSeconds: number + queueSize?: number contextFile?: string dryRun?: boolean allowMigrations?: boolean @@ -196,6 +209,7 @@ export function parseConfig(args: string[]): Config { timezone: opts.timezone, completeHours: opts.completeHours, cardFlushSeconds: opts.cardFlushSeconds, + queueSize: opts.queueSize, contextFile, dryRun: opts.dryRun ?? false, allowMigrations: opts.allowMigrations ?? false, diff --git a/apps/simplex-support-bot/src/index.ts b/apps/simplex-support-bot/src/index.ts index aab7f5f604..2bb8ccec9f 100644 --- a/apps/simplex-support-bot/src/index.ts +++ b/apps/simplex-support-bot/src/index.ts @@ -95,7 +95,7 @@ async function main(): Promise { log("Initializing main bot...") const [chat, mainUser, mainAddress] = await bot.run({ profile: {displayName: "Ask SimpleX Team", fullName: "", image: supportImage}, - dbOpts: config.db, + dbOpts: {...config.db, queueSize: config.queueSize}, options: { addressSettings: { businessAddress: true, diff --git a/scripts/support-bot/config/config.toml.example b/scripts/support-bot/config/config.toml.example index fe05f6d7ad..a78d9c93b6 100644 --- a/scripts/support-bot/config/config.toml.example +++ b/scripts/support-bot/config/config.toml.example @@ -9,6 +9,8 @@ pg_conn = "postgres://simplex@127.0.0.1/simplex_support_bot" # Enables Grok replies; GROK_API_KEY takes precedence. # grok_api_key = "xai-..." timezone = "UTC" +# Internal queue size of the chat core, as the CLI's --queue-size; default 1024. +# queue_size = 65536 # A message left out keeps its built-in text. # {hours} is 24 on weekdays and 48 at weekends in the timezone above. From b3732dd6d5c609aeec8b47a0572c640d5b84123a Mon Sep 17 00:00:00 2001 From: sh-agents <350651734+sh-agents@users.noreply.github.com> Date: Thu, 12 Nov 2026 07:07:14 +0000 Subject: [PATCH 16/16] support bot: config file sections --- apps/simplex-support-bot/README.md | 27 +++++-- apps/simplex-support-bot/bot.test.ts | 60 ++++++++------- apps/simplex-support-bot/src/config.ts | 75 ++++++++++++++----- apps/simplex-support-bot/src/index.ts | 2 +- .../support-bot/config/config.toml.example | 32 +++++--- 5 files changed, 135 insertions(+), 61 deletions(-) diff --git a/apps/simplex-support-bot/README.md b/apps/simplex-support-bot/README.md index 6274590c69..35870dca04 100644 --- a/apps/simplex-support-bot/README.md +++ b/apps/simplex-support-bot/README.md @@ -5,7 +5,7 @@ A business-address bot that triages incoming support chats, optionally runs them ## Prerequisites - Node.js v18 or newer (v24 tested) -- an xAI API key in `GROK_API_KEY` or in `grok_api_key` of the config file — optional; the bot runs without it +- an xAI API key in `GROK_API_KEY` or in `grok.api_key` of the config file — optional; the bot runs without it - For the PostgreSQL backend: Linux x86_64, `libpq5` installed on the host, and a reachable PostgreSQL server ## Install & build @@ -65,9 +65,26 @@ Run `npm start -- --help` for the auto-generated reference. Summary: ## Config file -`--config config.toml` reads option values and message texts from a TOML file, as [config.toml.example](../../scripts/support-bot/config/config.toml.example) shows. Keys are the command-line options in snake_case (`team_group` for `--team-group`); a list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run`, `--allow-migrations` and `--allow-down-migrations` are command-line only. `grok_api_key` is file-only, so that the key is not visible in the process list; `GROK_API_KEY` takes precedence over it. The file holds the `pg_conn` password and the Grok key, so keep it readable only by the user that runs the bot. +`--config config.toml` reads option values and message texts from a TOML file, as [config.toml.example](../../scripts/support-bot/config/config.toml.example) shows. A list is joined with commas. An option given on the command line takes precedence over the file. `--dry-run`, `--allow-migrations` and `--allow-down-migrations` are command-line only. `grok.api_key` is file-only, so that the key is not visible in the process list; `GROK_API_KEY` takes precedence over it. The file holds the `pg_conn` password and the Grok key, so keep it readable only by the user that runs the bot. -A message left out of `[messages]` keeps its built-in text. `{hours}` is replaced with 24 on weekdays and 48 at weekends in `timezone`. The welcome message is updated in the bot address on the next start. +| Key | Option | +|---|---| +| `bot.team_group` | `--team-group` | +| `bot.auto_add_team_members` | `--auto-add-team-members` | +| `bot.state_file` | `--state-file` | +| `bot.timezone` | `--timezone` | +| `bot.complete_hours` | `--complete-hours` | +| `bot.card_flush_seconds` | `--card-flush-seconds` | +| `bot.queue_size` | `--queue-size` | +| `database.type` | `--db` | +| `database.pg_conn` | `--pg-conn` | +| `database.pg_schema` | `--pg-schema` | +| `database.sqlite_file_prefix` | `--sqlite-file-prefix` | +| `database.sqlite_key` | `--sqlite-key` | +| `grok.context_file` | `--context-file` | +| `grok.api_key` | `GROK_API_KEY` | + +A message left out of `[messages]` keeps its built-in text. `{hours}` is replaced with 24 on weekdays and 48 at weekends in `bot.timezone`. The welcome message is updated in the bot address on the next start. | Message | Sent when | |---|---| @@ -90,7 +107,7 @@ A message left out of `[messages]` keeps its built-in text. `{hours}` is replace | Var | Purpose | |---|---| -| `GROK_API_KEY` | xAI API key; enables Grok replies; takes precedence over `grok_api_key` of the config file | +| `GROK_API_KEY` | xAI API key; enables Grok replies; takes precedence over `grok.api_key` of the config file | | `SIMPLEX_LIBS_DIR` | directory with a local libsimplex build for the `--db` backend, instead of the downloaded one | | `SIMPLEX_ADDON_PATH` | locally built Node.js add-on (`simplex.node`), instead of the downloaded one | | `NODE_OPTIONS` | `--max-old-space-size=8192` on a bot with many chats: single API responses outgrow the default heap | @@ -111,7 +128,7 @@ docker compose up --build LIBS=source docker compose build ``` -`scripts/support-bot/config` is mounted at `/etc/support-bot` and holds `config.toml`, with `pg_conn` and `grok_api_key`, and the Grok context file; the bot runs as uid 1000, which must be able to read them. Other arguments are passed after the service name, for example `docker compose run --rm support-bot --config /etc/support-bot/config.toml --dry-run`. +`scripts/support-bot/config` is mounted at `/etc/support-bot` and holds `config.toml`, with `database.pg_conn` and `grok.api_key`, and the Grok context file; the bot runs as uid 1000, which must be able to read them. Other arguments are passed after the service name, for example `docker compose run --rm support-bot --config /etc/support-bot/config.toml --dry-run`. ## Dry run diff --git a/apps/simplex-support-bot/bot.test.ts b/apps/simplex-support-bot/bot.test.ts index 6cfe2fa75a..f806cf93da 100644 --- a/apps/simplex-support-bot/bot.test.ts +++ b/apps/simplex-support-bot/bot.test.ts @@ -2515,11 +2515,13 @@ describe("parseConfig Validation", () => { test("option values and messages are read; missing messages keep the defaults", () => { const path = configFile([ + `[bot]`, `team_group = "SimpleX Support Team"`, `auto_add_team_members = ["1:Alice", "2:Bob"]`, `complete_hours = 0`, `queue_size = 65536`, - `db = "postgres"`, + `[database]`, + `type = "postgres"`, `pg_conn = "postgres://localhost/db"`, `[messages]`, `welcome = """`, @@ -2540,7 +2542,7 @@ describe("parseConfig Validation", () => { }) test("command-line options take precedence over the file, given before or after --config", () => { - const path = configFile(`team_group = "From file"\ntimezone = "Europe/London"`) + const path = configFile(`[bot]\nteam_group = "From file"\ntimezone = "Europe/London"`) expect(parseConfig(["--team-group", "From CLI", "--config", path]).teamGroup.name).toBe("From CLI") expect(parseConfig([`--config=${path}`, "--team-group", "From CLI"]).timezone).toBe("Europe/London") }) @@ -2550,40 +2552,46 @@ describe("parseConfig Validation", () => { }) test("values are validated as on the command line", () => { - expect(() => parseConfig(["--config", configFile(`team_group = "T"\ncomplete_hours = -1`)])) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "T"\ncomplete_hours = -1`)])) .toThrow(/--complete-hours must be a non-negative integer, got "-1"/) - expect(() => parseConfig(["--config", configFile(`team_group = "T"\ndb = "mysql"`)])) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "T"\n[database]\ntype = "mysql"`)])) .toThrow(/Allowed choices are sqlite, postgres/) - expect(() => parseConfig(["--config", configFile(`team_group = "T"\nteam_grup = "U"`)])) - .toThrow(/unknown option '--team-grup=U'/) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "T"\n[database]\ntype = "sqlite"\npg_conn = "c"`)])) + .toThrow(/--pg-conn and --pg-schema require --db postgres/) }) - test("dashed keys, flags, nested config and non-scalar values → throw", () => { - expect(() => parseConfig(["--config", configFile(`team-group = "T"`)])) - .toThrow(/key team-group must be lowercase with underscores, as team_group/) - expect(() => parseConfig(["--config", configFile(`team_group = "T"\ndry_run = true`)])) - .toThrow(/dry_run must be a string, a number or a list of strings/) - expect(() => parseConfig(["--config", configFile(`team_group = "T"\nconfig = "other.toml"`)])) - .toThrow(/config cannot be set in a config file/) + test("unknown sections and keys, top-level keys and non-scalar values → throw", () => { + expect(() => parseConfig(["--config", configFile(`[server]\nport = 1`)])) + .toThrow(/unknown section \[server\], known sections: bot, database, grok, messages/) + expect(() => parseConfig(["--config", configFile(`team_group = "T"`)])) + .toThrow(/unknown section \[team_group\]/) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_grup = "T"`)])) + .toThrow(/unknown key bot.team_grup, known keys: team_group, auto_add_team_members,/) + expect(() => parseConfig(["--config", configFile(`[bot]\ndry_run = true`)])) + .toThrow(/unknown key bot.dry_run/) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = true`)])) + .toThrow(/bot.team_group must be a string, a number or a list of strings/) + expect(() => parseConfig(["--config", configFile(`bot = "T"`)])) + .toThrow(/\[bot\] must be a section/) }) test("unknown or non-string messages → throw", () => { - expect(() => parseConfig(["--config", configFile(`team_group = "T"\n[messages]\nteamAdded = "Hi"`)])) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "T"\n[messages]\nteamAdded = "Hi"`)])) .toThrow(/unknown message teamAdded, known messages: welcome, queue, queue_grok, grok_activated, team_added,/) - expect(() => parseConfig(["--config", configFile(`team_group = "T"\n[messages]\nwelcome = 1`)])) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "T"\n[messages]\nwelcome = 1`)])) .toThrow(/message welcome must be a string/) - expect(() => parseConfig(["--config", configFile(`team_group = "T"\nmessages = "Hi"`)])) - .toThrow(/messages must be a table/) + expect(() => parseConfig(["--config", configFile(`messages = "Hi"\n[bot]\nteam_group = "T"`)])) + .toThrow(/\[messages\] must be a section/) }) - describe("grok_api_key", () => { + describe("grok.api_key", () => { const savedKey = process.env.GROK_API_KEY beforeEach(() => { delete process.env.GROK_API_KEY }) afterEach(() => { if (savedKey === undefined) delete process.env.GROK_API_KEY else process.env.GROK_API_KEY = savedKey }) - const withKey = (extra = "") => configFile(`team_group = "T"\ncontext_file = "ctx.yaml"\ngrok_api_key = "file-key"${extra}`) + const withKey = () => configFile(`[bot]\nteam_group = "T"\n[grok]\ncontext_file = "ctx.yaml"\napi_key = "file-key"`) test("is read from the file", () => { expect(parseConfig(["--config", withKey()]).grokApiKey).toBe("file-key") @@ -2597,24 +2605,24 @@ describe("parseConfig Validation", () => { }) test("requires a context file", () => { - expect(() => parseConfig(["--config", configFile(`team_group = "T"\ngrok_api_key = "file-key"`)])) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "T"\n[grok]\napi_key = "file-key"`)])) .toThrow(/A Grok API key is set but --context-file is not provided/) }) test("must be a non-empty string", () => { - expect(() => parseConfig(["--config", configFile(`team_group = "T"\ngrok_api_key = ""`)])) - .toThrow(/grok_api_key must be a non-empty string/) - expect(() => parseConfig(["--config", configFile(`team_group = "T"\ngrok_api_key = 1`)])) - .toThrow(/grok_api_key must be a non-empty string/) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "T"\n[grok]\napi_key = ""`)])) + .toThrow(/grok.api_key must be a non-empty string/) + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "T"\n[grok]\napi_key = 1`)])) + .toThrow(/grok.api_key must be a non-empty string/) }) test("absent from file and environment → Grok disabled", () => { - expect(parseConfig(["--config", configFile(`team_group = "T"`)]).grokApiKey).toBeNull() + expect(parseConfig(["--config", configFile(`[bot]\nteam_group = "T"`)]).grokApiKey).toBeNull() }) }) test("malformed TOML → throws", () => { - expect(() => parseConfig(["--config", configFile(`team_group = "unclosed`)])).toThrow() + expect(() => parseConfig(["--config", configFile(`[bot]\nteam_group = "unclosed`)])).toThrow() }) }) diff --git a/apps/simplex-support-bot/src/config.ts b/apps/simplex-support-bot/src/config.ts index 899113f822..978678b36e 100644 --- a/apps/simplex-support-bot/src/config.ts +++ b/apps/simplex-support-bot/src/config.ts @@ -76,7 +76,7 @@ function buildCommand(): Command { .option("--dry-run", "check config, database and state, then exit without starting chat") .option("--allow-migrations", "with --dry-run: apply pending migrations instead of reporting them") .option("--allow-down-migrations", "with --dry-run: roll back migrations that this build does not have") - .addHelpText("after", "\nEnvironment:\n GROK_API_KEY xAI API key — enables Grok replies, takes precedence over grok_api_key in --config\n SIMPLEX_LIBS_DIR local libsimplex build for the --db backend\n SIMPLEX_ADDON_PATH locally built Node.js add-on\n") + .addHelpText("after", "\nEnvironment:\n GROK_API_KEY xAI API key — enables Grok replies, takes precedence over grok.api_key in --config\n SIMPLEX_LIBS_DIR local libsimplex build for the --db backend\n SIMPLEX_ADDON_PATH locally built Node.js add-on\n") } interface RawOpts { @@ -110,35 +110,76 @@ function configFilePath(args: string[]): string | undefined { return args.find(arg => arg.startsWith("--config="))?.slice("--config=".length) } +const configFlags = new Map( + Object.entries({ + bot: { + team_group: "--team-group", + auto_add_team_members: "--auto-add-team-members", + state_file: "--state-file", + timezone: "--timezone", + complete_hours: "--complete-hours", + card_flush_seconds: "--card-flush-seconds", + queue_size: "--queue-size", + }, + database: { + type: "--db", + pg_conn: "--pg-conn", + pg_schema: "--pg-schema", + sqlite_file_prefix: "--sqlite-file-prefix", + sqlite_key: "--sqlite-key", + }, + grok: { + context_file: "--context-file", + }, + }).map(([section, flags]): [string, Map] => [section, new Map(Object.entries(flags))]) +) + function readConfigFile(path: string): ConfigFile { - const {messages = {}, grok_api_key = null, ...options} = parseToml(readFileSync(path, "utf-8")) - if (grok_api_key !== null && (typeof grok_api_key !== "string" || grok_api_key === "")) { - throw new Error(`${path}: grok_api_key must be a non-empty string`) - } + const {messages = {}, grok = {}, ...sections} = parseToml(readFileSync(path, "utf-8")) + const {api_key, ...grokOptions} = configTable(path, "grok", grok) return { - args: Object.entries(options).map(([key, value]) => configOptionArg(path, key, value)), - messages: configMessages(path, messages), - grokApiKey: grok_api_key, + args: Object.entries({...sections, grok: grokOptions}).flatMap(([section, options]) => configSectionArgs(path, section, options)), + messages: configMessages(path, configTable(path, "messages", messages)), + grokApiKey: configGrokApiKey(path, api_key), } } -function configOptionArg(path: string, key: string, value: unknown): string { - if (!/^[a-z][a-z_]*$/.test(key)) throw new Error(`${path}: key ${key} must be lowercase with underscores, as team_group`) - if (key === "config") throw new Error(`${path}: config cannot be set in a config file`) - const flag = `--${key.replaceAll("_", "-")}` +function configGrokApiKey(path: string, value: unknown): string | null { + if (value === undefined) return null + if (typeof value !== "string" || value === "") throw new Error(`${path}: grok.api_key must be a non-empty string`) + return value +} + +function configTable(path: string, section: string, value: unknown): Record { + if (typeof value !== "object" || value === null || Array.isArray(value)) { + throw new Error(`${path}: [${section}] must be a section`) + } + return value as Record +} + +function configSectionArgs(path: string, section: string, options: unknown): string[] { + const flags = configFlags.get(section) + if (!flags) { + throw new Error(`${path}: unknown section [${section}], known sections: ${[...configFlags.keys(), "messages"].join(", ")}`) + } + return Object.entries(configTable(path, section, options)).map(([key, value]) => { + const flag = flags.get(key) + if (!flag) throw new Error(`${path}: unknown key ${section}.${key}, known keys: ${[...flags.keys()].join(", ")}`) + return configOptionArg(path, `${section}.${key}`, flag, value) + }) +} + +function configOptionArg(path: string, name: string, flag: string, value: unknown): string { if (typeof value === "string" || typeof value === "number") return `${flag}=${value}` if (Array.isArray(value) && value.every(v => typeof v === "string")) return `${flag}=${value.join(",")}` - throw new Error(`${path}: ${key} must be a string, a number or a list of strings`) + throw new Error(`${path}: ${name} must be a string, a number or a list of strings`) } const messageKeys = new Map( (Object.keys(defaultMessages) as (keyof Messages)[]).map(field => [field.replace(/[A-Z]/g, c => `_${c.toLowerCase()}`), field]) ) -function configMessages(path: string, table: unknown): Messages { - if (typeof table !== "object" || table === null || Array.isArray(table)) { - throw new Error(`${path}: messages must be a table`) - } +function configMessages(path: string, table: Record): Messages { const messages = {...defaultMessages} for (const [key, value] of Object.entries(table)) { const field = messageKeys.get(key) diff --git a/apps/simplex-support-bot/src/index.ts b/apps/simplex-support-bot/src/index.ts index 2bb8ccec9f..71fc84242d 100644 --- a/apps/simplex-support-bot/src/index.ts +++ b/apps/simplex-support-bot/src/index.ts @@ -35,7 +35,7 @@ async function main(): Promise { completeHours: config.completeHours, }) const grokEnabled = config.grokApiKey !== null - if (!grokEnabled) log("No Grok API key in GROK_API_KEY or grok_api_key, disabling Grok support") + if (!grokEnabled) log("No Grok API key in GROK_API_KEY or grok.api_key, disabling Grok support") const stateFilePath = config.stateFile const state = readState(stateFilePath) diff --git a/scripts/support-bot/config/config.toml.example b/scripts/support-bot/config/config.toml.example index a78d9c93b6..256b77c617 100644 --- a/scripts/support-bot/config/config.toml.example +++ b/scripts/support-bot/config/config.toml.example @@ -1,19 +1,27 @@ -# Copy to config.toml. Keys are the command-line options in snake_case; +# Copy to config.toml. Each key sets the command-line option named next to it; # an option given on the command line takes precedence. -team_group = "SimpleX Support Team" -auto_add_team_members = ["1:Alice"] -context_file = "/etc/support-bot/grok-context.yaml" -state_file = "/data/state.json" -db = "postgres" -pg_conn = "postgres://simplex@127.0.0.1/simplex_support_bot" + +[bot] +team_group = "SimpleX Support Team" # --team-group +auto_add_team_members = ["1:Alice"] # --auto-add-team-members +state_file = "/data/state.json" # --state-file +timezone = "UTC" # --timezone +# complete_hours = 3 # --complete-hours +# card_flush_seconds = 300 # --card-flush-seconds +# queue_size = 65536 # --queue-size, core default 1024 + +[database] +type = "postgres" # --db +pg_conn = "postgres://simplex@127.0.0.1/simplex_support_bot" # --pg-conn +# pg_schema = "simplex_v1" # --pg-schema + +[grok] +context_file = "/etc/support-bot/grok-context.yaml" # --context-file # Enables Grok replies; GROK_API_KEY takes precedence. -# grok_api_key = "xai-..." -timezone = "UTC" -# Internal queue size of the chat core, as the CLI's --queue-size; default 1024. -# queue_size = 65536 +# api_key = "xai-..." # A message left out keeps its built-in text. -# {hours} is 24 on weekdays and 48 at weekends in the timezone above. +# {hours} is 24 on weekdays and 48 at weekends in bot.timezone. [messages] welcome = """ Hello! This is a *SimpleX team* support bot - not an AI.