@desplega.ai/agent-swarm 2.13.0 → 2.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/acp-adapter-x1qqgzwx.js +17 -0
- package/dist/{acp-targets-86f0sh6g.js → acp-targets-mz8c5dnt.js} +10 -10
- package/dist/{actions-zv8vcxvr.js → actions-5ft3qp5a.js} +27 -27
- package/dist/{agent-fs-provision-jqshb33y.js → agent-fs-provision-6fgz7wvz.js} +4 -4
- package/dist/{amp-adapter-gt503ywc.js → amp-adapter-nthn8ezm.js} +3 -3
- package/dist/{app-6t4k5rh1.js → app-535m78qw.js} +10 -10
- package/dist/{approval-notifications-fcfw063h.js → approval-notifications-dewbk403.js} +11 -11
- package/dist/{assistant-9dzybh12.js → assistant-39zyabv2.js} +28 -28
- package/dist/{boot-args-schema-xzfp2feg.js → boot-args-schema-bqv5bf77.js} +1 -1
- package/dist/{boot-reembed-3jz0p2jk.js → boot-reembed-157c0gcz.js} +1 -1
- package/dist/{boot-reembed-r88wngfg.js → boot-reembed-fas7z3zn.js} +1 -1
- package/dist/{boot-scrub-sweep-scrqhw48.js → boot-scrub-sweep-tmec0be2.js} +2 -2
- package/dist/{boot-seeds-vb2dcrrb.js → boot-seeds-32zm50x2.js} +4 -4
- package/dist/{boxd-68n75y1s.js → boxd-hr8rsxzq.js} +1 -1
- package/dist/{claude-adapter-h3mp9hgs.js → claude-adapter-gwq7x2nj.js} +9 -9
- package/dist/{claude-managed-adapter-kpbyj2vb.js → claude-managed-adapter-rkvw6fvq.js} +4 -4
- package/dist/{claude-managed-setup-0efr3wkg.js → claude-managed-setup-0vgz4d1j.js} +6 -6
- package/dist/{claude-sdk-session-03dw669q.js → claude-sdk-session-5wbwxy8c.js} +20967 -19390
- package/dist/{cli-z64t9pe6.js → cli-0f7n3td3.js} +1 -1
- package/dist/{cli-j53hpbsm.js → cli-0xwb7zpt.js} +2 -2
- package/dist/{cli-wxwv3t11.js → cli-1f48qdnb.js} +55 -13
- package/dist/{cli-gfcv679k.js → cli-1gevjrc1.js} +1 -1
- package/dist/{cli-0jkenasr.js → cli-2v4xxnx2.js} +1 -1
- package/dist/{cli-hc6e4tac.js → cli-2wde56gv.js} +6 -6
- package/dist/{cli-mh7pdxe0.js → cli-31vt7baf.js} +17 -17
- package/dist/{cli-ercwc8xn.js → cli-3mf04tkh.js} +1 -1
- package/dist/{cli-s6zk31gr.js → cli-43dsqhcp.js} +4 -4
- package/dist/{cli-1dbv9mj9.js → cli-4djg31jh.js} +1 -1
- package/dist/{cli-tn91wc6e.js → cli-50whymxq.js} +1 -1
- package/dist/{cli-js10kdk7.js → cli-5basv0e7.js} +19 -5
- package/dist/{cli-b2jh3dyb.js → cli-5k1hvycs.js} +4 -4
- package/dist/{cli-dkejt425.js → cli-62fjschk.js} +7 -7
- package/dist/{cli-zqmmy073.js → cli-69eq6gdk.js} +18 -18
- package/dist/{cli-qm3ntjqs.js → cli-6rmryz03.js} +13 -13
- package/dist/{cli-mz34pxbk.js → cli-7m6bbtab.js} +11 -11
- package/dist/{cli-wv20kbr3.js → cli-8a4qg44m.js} +5 -5
- package/dist/{cli-2m6yaq6b.js → cli-8fj8yfmk.js} +1 -1
- package/dist/{cli-9m9kyw77.js → cli-8qvte9hn.js} +2 -2
- package/dist/{cli-x05p2d05.js → cli-9tevdxyf.js} +1 -1
- package/dist/{cli-qec9bynj.js → cli-a6rzw649.js} +3 -3
- package/dist/{cli-ex1y88j3.js → cli-at67kvnp.js} +1 -1
- package/dist/{cli-yn4kt85g.js → cli-b0kqw55g.js} +1 -1
- package/dist/{cli-n3qe7tyr.js → cli-b0m3bg0f.js} +134 -3
- package/dist/{cli-xfjn2kyq.js → cli-bp2rt28p.js} +9 -9
- package/dist/{cli-2jk4s7z1.js → cli-c9bwawf9.js} +3 -3
- package/dist/{cli-wndegmns.js → cli-cmgdptmx.js} +3 -3
- package/dist/{cli-3p288654.js → cli-cpkxh68d.js} +2 -2
- package/dist/{cli-117g15t4.js → cli-cs26ed89.js} +8 -8
- package/dist/{cli-prf7kz78.js → cli-d0jb2qnt.js} +603 -78
- package/dist/{cli-8hp56wat.js → cli-d2epqm0y.js} +1 -1
- package/dist/{cli-wrhbym01.js → cli-d528acsw.js} +6 -6
- package/dist/{cli-tstj6e8e.js → cli-dnvmwsvc.js} +310 -88
- package/dist/{cli-9fsagra4.js → cli-e87fg3en.js} +1 -1
- package/dist/{cli-jj3c5nyb.js → cli-erh1rgh4.js} +116 -56
- package/dist/{cli-f1952kjy.js → cli-ewb028rs.js} +4 -4
- package/dist/{cli-ct8sz3np.js → cli-f1sdh8ca.js} +6 -6
- package/dist/{cli-0b655smz.js → cli-fcwrd7bh.js} +5 -5
- package/dist/{cli-eb279wn3.js → cli-fgm2mad9.js} +2 -2
- package/dist/{cli-bpje8n65.js → cli-fp65jdp2.js} +31 -6
- package/dist/{cli-dnh64e8e.js → cli-g0b3ma7r.js} +7 -7
- package/dist/{cli-avxh26pd.js → cli-gc7jkf4a.js} +4 -4
- package/dist/{cli-2skf1mdf.js → cli-gedf48yw.js} +4 -4
- package/dist/{cli-ajjvqhf3.js → cli-getrvmtx.js} +27 -6
- package/dist/{cli-c8v2yvej.js → cli-gq6qhmrr.js} +2 -2
- package/dist/{cli-mbwmqb5w.js → cli-h0ewec3d.js} +1 -1
- package/dist/{cli-tdk7jtka.js → cli-hv3820ez.js} +1 -1
- package/dist/{cli-avqkr20b.js → cli-j2ybkdk2.js} +2 -2
- package/dist/{cli-pqfecppn.js → cli-je587f74.js} +1 -1
- package/dist/{cli-hgsxnjyg.js → cli-jkkcm72j.js} +13 -13
- package/dist/{cli-g6j6ac3h.js → cli-ka13xx6b.js} +2 -2
- package/dist/{cli-t35hx078.js → cli-m1wddksc.js} +2 -2
- package/dist/{cli-50w82h6m.js → cli-mwxez0pz.js} +1 -0
- package/dist/{cli-nc6a8b5b.js → cli-n14r8h4n.js} +2 -2
- package/dist/{cli-y4a4458j.js → cli-n46d87mj.js} +3 -3
- package/dist/{cli-0dg7ytxm.js → cli-nkfv398a.js} +1096 -75
- package/dist/{cli-fhvk2nvp.js → cli-nvqq3232.js} +9 -9
- package/dist/{cli-bhgrhedp.js → cli-q8aq48v1.js} +1 -1
- package/dist/{cli-6a40p4kv.js → cli-qn79645p.js} +1 -1
- package/dist/{cli-hbq0kwxz.js → cli-r4fphq38.js} +4 -4
- package/dist/{cli-47hz21se.js → cli-r8ddn0hf.js} +4 -5
- package/dist/{cli-hpb9x72z.js → cli-rvhaahfv.js} +9 -9
- package/dist/{cli-qhhb30e8.js → cli-s08zsqva.js} +9 -9
- package/dist/{cli-sdh61g52.js → cli-s6fhqbz1.js} +2 -2
- package/dist/{cli-y518qkjg.js → cli-sjk85d7v.js} +13 -12
- package/dist/{cli-ytc7fpza.js → cli-t25rq8mg.js} +4 -4
- package/dist/{cli-sq913bh1.js → cli-t9nxq30h.js} +8 -8
- package/dist/{cli-49f5jyhh.js → cli-tbdw3f92.js} +3 -3
- package/dist/{cli-1v82d2w6.js → cli-tceqvv6h.js} +4 -4
- package/dist/{cli-fz9pcjsw.js → cli-tgmate57.js} +4 -4
- package/dist/{cli-rtmqzat7.js → cli-vdfngx2g.js} +2 -2
- package/dist/{cli-9w8712tq.js → cli-vr4eyq6p.js} +43 -14
- package/dist/{cli-z4f346g5.js → cli-vx9acp12.js} +1 -1
- package/dist/{cli-7dbhnsj9.js → cli-wdtqkqnq.js} +4 -4
- package/dist/{cli-kde4ky90.js → cli-x5yb8zc8.js} +2 -2
- package/dist/{cli-w14j2z4k.js → cli-x7aqhxj5.js} +2 -2
- package/dist/{cli-kspxjftk.js → cli-xerdrnqs.js} +15 -6
- package/dist/{cli-48nvk6sb.js → cli-xhkek467.js} +2 -2
- package/dist/{cli-dka73bak.js → cli-xysy1g8x.js} +152 -12
- package/dist/{cli-7wy8gbwb.js → cli-ya1cj689.js} +1 -1
- package/dist/{cli-n85m2wkq.js → cli-zkbz8m2q.js} +3 -2
- package/dist/{cli-0pazkmd4.js → cli-zm6bkx3e.js} +4 -4
- package/dist/{cli-6h88mn9q.js → cli-zqy28rwx.js} +4 -4
- package/dist/cli.js +30 -29
- package/dist/{codex-adapter-f3yp0kaj.js → codex-adapter-x25xfbnh.js} +8 -8
- package/dist/{codex-hook-262cekfp.js → codex-hook-c0nvm30r.js} +2 -2
- package/dist/{codex-login-1ven7yje.js → codex-login-ven5cz5j.js} +1 -1
- package/dist/{codex-session-runner-ns4mv70p.js → codex-session-runner-kn5xnrvp.js} +9 -9
- package/dist/{commands-5bzf2tfw.js → commands-8n59gxmy.js} +3 -3
- package/dist/{controller-5xt7kvfm.js → controller-wn3vd217.js} +3 -3
- package/dist/{controllers-connect-0gg13mzp.js → controllers-connect-09gvkrhc.js} +28 -28
- package/dist/core-2b8bv6ny.js +42 -0
- package/dist/{cursor-adapter-p0c5zv6r.js → cursor-adapter-w1681kp1.js} +2 -2
- package/dist/{db-2s68ycq7.js → db-e71keqxk.js} +3 -3
- package/dist/{db-q7q639k5.js → db-spg81qtr.js} +2 -2
- package/dist/{devin-adapter-t8d86q9x.js → devin-adapter-fs90ab4v.js} +1 -1
- package/dist/{dispatcher-j2y1q7ae.js → dispatcher-fr4vawwb.js} +25 -25
- package/dist/{docker-1b41eysj.js → docker-1h33jvh9.js} +2 -2
- package/dist/{dsh-adapter-pgnkf4st.js → dsh-adapter-g9yqv09y.js} +2 -2
- package/dist/{e2b-xm60c8ad.js → e2b-jnk0yeva.js} +2 -2
- package/dist/{e2b-ew8c5j5v.js → e2b-y7e5kmzf.js} +2 -2
- package/dist/{easter-egg-3d-0kea97jf.js → easter-egg-3d-ac9tv9a6.js} +1 -1
- package/dist/{embedding-space-3n6mhhmy.js → embedding-space-an77f45k.js} +6 -6
- package/dist/{embeddings-jfmj6yme.js → embeddings-jkrehejr.js} +1 -1
- package/dist/{event-bus-v4ywhc2p.js → event-bus-08t2b4y2.js} +1 -1
- package/dist/{execute-tjrej979.js → execute-3mys4nta.js} +1 -1
- package/dist/{external-channel-jy8w2gg3.js → external-channel-s6zxpts9.js} +2 -2
- package/dist/fx-adapter-4yet1c4f.js +20 -0
- package/dist/grok-adapter-8ge6tw7m.js +20 -0
- package/dist/handlers-0r9evrdj.js +41 -0
- package/dist/heartbeat-jjpb44e5.js +50 -0
- package/dist/{hook-fv1jwrv7.js → hook-68gbs87k.js} +6 -6
- package/dist/{hook-bpnncsec.js → hook-y6n23zzj.js} +8 -8
- package/dist/{http-bpa26czn.js → http-8c6y008s.js} +404 -146
- package/dist/{index-w216g9y9.js → index-0vthtbc2.js} +38 -22
- package/dist/index-1v23363p.js +39 -0
- package/dist/index-30qw6vyw.js +44 -0
- package/dist/index-6bm3w6cn.js +40 -0
- package/dist/{index-s8re7mvq.js → index-6h3r0vpe.js} +1 -1
- package/dist/{index-620y7jzq.js → index-7dgxveck.js} +3 -3
- package/dist/{index-4k7m5dgj.js → index-maddg9ys.js} +143 -103
- package/dist/{index-0h6qd4p1.js → index-mbpxhc4w.js} +3 -4
- package/dist/index-rz8ve510.js +569 -0
- package/dist/{index-mpj0z9e0.js → index-we3nd2t0.js} +1 -1
- package/dist/{index-pe3d8jfy.js → index-whysn1wf.js} +1 -1
- package/dist/{index-52tgbhrw.js → index-xcw7hjt5.js} +28 -28
- package/dist/{index-8p0j4h4j.js → index-ym9fgxad.js} +94 -89
- package/dist/{k8s-fy8hbhkg.js → k8s-8326q0rq.js} +2 -2
- package/dist/{keepalive-d6z12s04.js → keepalive-7e9r5dvp.js} +5 -4
- package/dist/lead-3jpc7kcr.js +79 -0
- package/dist/{maintenance-7kac6g2g.js → maintenance-d3erjvcg.js} +1 -1
- package/dist/{oauth-refresh-sweep-t1xjetbh.js → oauth-refresh-sweep-0qh70988.js} +4 -3
- package/dist/{object-webhooks-6rdagg0r.js → object-webhooks-ac7v2q5p.js} +7 -7
- package/dist/{onboard-32y1cfxy.js → onboard-5yphtken.js} +4 -4
- package/dist/{opencode-adapter-enp3v31n.js → opencode-adapter-0xvyba4m.js} +8 -8
- package/dist/{otel-impl-k0tm5ptj.js → otel-impl-svqp691q.js} +2 -2
- package/dist/{pending-24zebgvs.js → pending-bn4xe5br.js} +26 -26
- package/dist/{pi-mono-adapter-yj72xxxe.js → pi-mono-adapter-763s8a3w.js} +20 -18
- package/dist/{pricing-refresh-cf25x38j.js → pricing-refresh-kfc215mf.js} +2 -2
- package/dist/{rbac-roles-0wydd22m.js → rbac-roles-3bznvkpt.js} +3 -3
- package/dist/{rbac-roles-at6crb3r.js → rbac-roles-6r4h2ghj.js} +2 -2
- package/dist/{render-v2-nvcw0jd7.js → render-v2-f3qedbpy.js} +10 -10
- package/dist/{resume-y4w0k5v3.js → resume-vcag4ann.js} +13 -13
- package/dist/{runtime-vpe43hx0.js → runtime-x0s55z2t.js} +1 -1
- package/dist/{session-access-gkyvnfye.js → session-access-nwzve8q2.js} +5 -5
- package/dist/{session-source-backfill-3w2st4m2.js → session-source-backfill-jpqfzmgz.js} +2 -2
- package/dist/{setup-axxvqtys.js → setup-d2smwaem.js} +1 -1
- package/dist/thread-buffer-8fc703jc.js +42 -0
- package/dist/{tree-visibility-y4fqvab5.js → tree-visibility-dwzc8en0.js} +2 -2
- package/dist/{user-merge-jobs-kcmwvrc6.js → user-merge-jobs-4ce7kma2.js} +3 -3
- package/dist/{virtual-modules-7d92dct1.js → virtual-modules-78a6yw71.js} +3 -3
- package/dist/{work-objects-f708e069.js → work-objects-m0w1e7q8.js} +28 -28
- package/dist/worker-3p2vq107.js +79 -0
- package/dist/{worker-follow-up-0by8wztc.js → worker-follow-up-e3t6f895.js} +25 -25
- package/dist/{x-3dtaf2r9.js → x-asbrnafg.js} +2 -2
- package/openapi.json +1795 -1623
- package/package.json +13 -12
- package/src/apps/catalog.generated.json +726 -0
- package/src/apps/definition.ts +24 -7
- package/src/apps/page-validator.ts +225 -3
- package/src/be/db.ts +38 -10
- package/src/be/mcp-proxy.ts +5 -4
- package/src/be/migrations/340_api_key_spend_cap.sql +5 -0
- package/src/be/migrations/341_user_token_expiry.sql +4 -0
- package/src/be/schedules/validate.ts +26 -0
- package/src/be/scripts/run-credential.ts +55 -1
- package/src/be/scripts/typecheck.ts +4 -4
- package/src/be/scripts/user-code-mode.ts +160 -0
- package/src/be/swarm-config-guard.ts +4 -0
- package/src/be/users.ts +48 -7
- package/src/commands/credential-outcome-report.ts +5 -2
- package/src/commands/onboard/agent-fs-version.ts +1 -1
- package/src/commands/provider-credentials.ts +19 -0
- package/src/commands/provider-error-class.ts +4 -1
- package/src/commands/rate-limit-outcome.ts +48 -3
- package/src/commands/runner.ts +113 -55
- package/src/commands/worktree-reaper.ts +600 -0
- package/src/extensions/contract-types.generated.ts +1 -1
- package/src/http/agents.ts +4 -0
- package/src/http/api-keys.ts +27 -3
- package/src/http/pages-public.ts +40 -4
- package/src/http/pool-keys.ts +21 -2
- package/src/http/schedules.ts +28 -3
- package/src/http/script-connection-proxy.ts +18 -6
- package/src/http/script-connections.ts +12 -4
- package/src/http/script-runs.ts +12 -3
- package/src/http/scripts.ts +2 -11
- package/src/http/tasks.ts +6 -10
- package/src/http/users.ts +1 -0
- package/src/integrations/chat-sdk/adapters.ts +29 -0
- package/src/integrations/chat-sdk/limits.ts +3 -0
- package/src/integrations/chat-sdk/rocketchat/README.md +8 -0
- package/src/integrations/chat-sdk/rocketchat/index.ts +790 -0
- package/src/integrations/chat-sdk/rocketchat/markdown.ts +210 -0
- package/src/integrations/chat-sdk/rocketchat/types.ts +249 -0
- package/src/integrations/chat-sdk/runtime.ts +11 -5
- package/src/integrations/chat-sdk/schema.ts +37 -3
- package/src/integrations/chat-sdk/status.ts +9 -0
- package/src/mcp-client/http-client.ts +3 -1
- package/src/oauth/identity-capture.ts +6 -1
- package/src/oauth/mcp-wrapper.ts +10 -3
- package/src/oauth/wrapper.ts +15 -3
- package/src/prompts/session-templates.ts +1 -0
- package/src/providers/acp-targets.ts +2 -2
- package/src/providers/amp-adapter.ts +1 -1
- package/src/providers/dsh-adapter.ts +1 -1
- package/src/providers/pi-mono-adapter.ts +4 -2
- package/src/providers/runtime-dependencies.ts +1 -1
- package/src/rbac/legacy-policy.ts +3 -0
- package/src/rbac/permissions.ts +9 -0
- package/src/scripts-runtime/executors/native.ts +225 -67
- package/src/scripts-runtime/mcp-client.ts +2 -1
- package/src/scripts-runtime/swarm-sdk.ts +2 -1
- package/src/scripts-runtime/types/stdlib.d.ts +4 -4
- package/src/scripts-runtime/types/swarm-extension.d.ts +7 -7
- package/src/scripts-runtime/types/swarm-sdk.d.ts +4 -4
- package/src/server-user.ts +98 -0
- package/src/slack/enrich.ts +14 -11
- package/src/tests/agents-list-model-display.test.ts +23 -0
- package/src/tests/apps-spike2.test.ts +35 -0
- package/src/tests/base-prompt.test.ts +1 -0
- package/src/tests/chat-sdk-rocketchat-vendored.test.ts +67 -0
- package/src/tests/chat-sdk-status-route.test.ts +13 -1
- package/src/tests/codex-app-server.test.ts +4 -6
- package/src/tests/deploy-guard.test.ts +24 -0
- package/src/tests/dsh-adapter.test.ts +1 -1
- package/src/tests/ensure-token.test.ts +2 -2
- package/src/tests/entrypoint-repo-autoclone-args.test.ts +202 -0
- package/src/tests/fake-dns.ts +47 -0
- package/src/tests/fixtures/base-prompt-baseline.json +12 -12
- package/src/tests/latest-model-report.test.ts +138 -0
- package/src/tests/mcp-oauth-dcr-reuse.test.ts +5 -2
- package/src/tests/mcp-oauth-ensure-token.test.ts +3 -2
- package/src/tests/mcp-oauth-manual-client.test.ts +6 -1
- package/src/tests/mcp-oauth-wrapper.test.ts +20 -4
- package/src/tests/mcp-outbound-fetch-guard.test.ts +169 -0
- package/src/tests/mcp-server-ssrf.test.ts +2 -0
- package/src/tests/mcp-user-route.test.ts +10 -1
- package/src/tests/memory-search-stall.test.ts +5 -0
- package/src/tests/oauth-connection-fetch-guard.test.ts +162 -0
- package/src/tests/oauth-token-endpoint-fetch-guard.test.ts +121 -0
- package/src/tests/oauth-wrapper.test.ts +1 -2
- package/src/tests/pages-head-v2.test.ts +2 -1
- package/src/tests/script-connections-http.test.ts +8 -3
- package/src/tests/script-connections-mcp.test.ts +14 -10
- package/src/tests/script-connections-sql.test.ts +6 -2
- package/src/tests/script-connections.test.ts +4 -0
- package/src/tests/slack-thread-participants.test.ts +14 -3
- package/src/tests/spend-cap-credentials.test.ts +34 -0
- package/src/tests/spend-cap-outcome.test.ts +67 -0
- package/src/tests/subscription-first.test.ts +119 -0
- package/src/tests/worktree-reaper.test.ts +424 -0
- package/src/tools/get-task-details.ts +247 -5
- package/src/tools/schedules/patch-schedule.ts +26 -1
- package/src/tools/schedules/update-schedule.ts +28 -2
- package/src/tools/script-common.ts +37 -22
- package/src/tools/script-query-types.ts +1 -1
- package/src/tools/script-run.ts +1 -1
- package/src/tools/script-search.ts +12 -10
- package/src/tools/utils.ts +7 -1
- package/src/utils/credentials.ts +36 -1
- package/src/utils/egress-guard.ts +40 -10
- package/src/utils/error-tracker.ts +17 -0
- package/templates/skills/apps/SKILL.md +191 -2
- package/templates/skills/apps/content.md +191 -2
- package/templates/skills/boards-and-sheets/content.md +11 -0
- package/templates/skills/chat-sdk/content.md +2 -2
- package/templates/skills/pages/content.md +11 -0
- package/templates/skills/swarm-docs/content.md +4 -0
- package/templates/skills/swarm-scripts/SKILL.md +1 -0
- package/templates/skills/swarm-scripts/content.md +1 -0
- package/templates/workflows/ralph-loop/content.md +6 -6
- package/dist/acp-adapter-cn53011z.js +0 -17
- package/dist/cli-pr0eze3x.js +0 -149
- package/dist/core-0fqtbrxa.js +0 -42
- package/dist/fx-adapter-k6w3jvz9.js +0 -20
- package/dist/grok-adapter-4q5tssw0.js +0 -20
- package/dist/handlers-qxgfvvsf.js +0 -41
- package/dist/heartbeat-6tr2w2da.js +0 -50
- package/dist/index-0mcxjdsa.js +0 -44
- package/dist/index-dz4q23ag.js +0 -40
- package/dist/index-s175jckx.js +0 -39
- package/dist/lead-p3hx7h35.js +0 -79
- package/dist/thread-buffer-rwa7d4dx.js +0 -42
- package/dist/worker-8rg9gz74.js +0 -79
- /package/dist/{cli-m4ejz4nj.js → cli-cah298e5.js} +0 -0
|
@@ -0,0 +1,790 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
AdapterRateLimitError,
|
|
4
|
+
AuthenticationError,
|
|
5
|
+
NetworkError,
|
|
6
|
+
PermissionError,
|
|
7
|
+
ResourceNotFoundError,
|
|
8
|
+
ValidationError,
|
|
9
|
+
} from "@chat-adapter/shared";
|
|
10
|
+
import type {
|
|
11
|
+
Adapter,
|
|
12
|
+
AdapterPostableMessage,
|
|
13
|
+
Author,
|
|
14
|
+
ChannelInfo,
|
|
15
|
+
ChatInstance,
|
|
16
|
+
EmojiValue,
|
|
17
|
+
FetchOptions,
|
|
18
|
+
FetchResult,
|
|
19
|
+
FormattedContent,
|
|
20
|
+
Logger,
|
|
21
|
+
RawMessage,
|
|
22
|
+
ThreadInfo,
|
|
23
|
+
UserInfo,
|
|
24
|
+
WebhookOptions,
|
|
25
|
+
} from "chat";
|
|
26
|
+
import { ConsoleLogger, defaultEmojiResolver, Message } from "chat";
|
|
27
|
+
import { RocketChatFormatConverter } from "./markdown";
|
|
28
|
+
import type {
|
|
29
|
+
RocketChatAdapterConfig,
|
|
30
|
+
RocketChatDeleteMessageResponse,
|
|
31
|
+
RocketChatErrorResponse,
|
|
32
|
+
RocketChatGetMessageResponse,
|
|
33
|
+
RocketChatGetThreadMessagesResponse,
|
|
34
|
+
RocketChatMessage,
|
|
35
|
+
RocketChatMessageUser,
|
|
36
|
+
RocketChatPostMessageResponse,
|
|
37
|
+
RocketChatRawMessage,
|
|
38
|
+
RocketChatRoomInfoResponse,
|
|
39
|
+
RocketChatRoomsHistoryResponse,
|
|
40
|
+
RocketChatThreadId,
|
|
41
|
+
RocketChatUpdateMessageResponse,
|
|
42
|
+
RocketChatUserInfoResponse,
|
|
43
|
+
RocketChatWebhookPayload,
|
|
44
|
+
} from "./types";
|
|
45
|
+
|
|
46
|
+
export { RocketChatFormatConverter } from "./markdown";
|
|
47
|
+
export type {
|
|
48
|
+
RocketChatAdapterConfig,
|
|
49
|
+
RocketChatMessage,
|
|
50
|
+
RocketChatMessageUser,
|
|
51
|
+
RocketChatRawMessage,
|
|
52
|
+
RocketChatReactions,
|
|
53
|
+
RocketChatRoom,
|
|
54
|
+
RocketChatThreadId,
|
|
55
|
+
RocketChatUser,
|
|
56
|
+
RocketChatWebhookPayload,
|
|
57
|
+
} from "./types";
|
|
58
|
+
|
|
59
|
+
const DEFAULT_USER_NAME = "rocketchat-bot";
|
|
60
|
+
const THREAD_ID_PATTERN = /^rocketchat:([^:]+)(?::([^:]+))?$/;
|
|
61
|
+
const CHANNEL_ID_PATTERN = /^rocketchat:([^:]+)$/;
|
|
62
|
+
const REGEX_SPECIAL_PATTERN = /[.*+?^${}()|[\]\\]/g;
|
|
63
|
+
const EMOJI_PLACEHOLDER_REGEX = /\{\{emoji:([a-z0-9_]+)\}\}/gi;
|
|
64
|
+
|
|
65
|
+
interface RequestOptions {
|
|
66
|
+
body?: unknown;
|
|
67
|
+
query?: Record<string, string | number | undefined>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Rocket.Chat adapter for Chat SDK.
|
|
72
|
+
*
|
|
73
|
+
* Handles messages posted in channels, private groups, and direct messages
|
|
74
|
+
* via a Rocket.Chat Outgoing Webhook integration, and posts replies through
|
|
75
|
+
* the REST API.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* import { Chat } from "chat";
|
|
80
|
+
* import { createRocketChatAdapter } from "@/integrations/chat-sdk/rocketchat";
|
|
81
|
+
* import { createMemoryState } from "@chat-adapter/state-memory";
|
|
82
|
+
*
|
|
83
|
+
* const bot = new Chat({
|
|
84
|
+
* userName: "my-bot",
|
|
85
|
+
* adapters: { rocketchat: createRocketChatAdapter() },
|
|
86
|
+
* state: createMemoryState(),
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export class RocketChatAdapter implements Adapter<RocketChatThreadId, RocketChatRawMessage> {
|
|
91
|
+
readonly name = "rocketchat";
|
|
92
|
+
|
|
93
|
+
protected readonly baseUrl: string;
|
|
94
|
+
protected readonly token: string;
|
|
95
|
+
protected readonly webhookToken: string;
|
|
96
|
+
protected readonly logger: Logger;
|
|
97
|
+
protected readonly formatConverter = new RocketChatFormatConverter();
|
|
98
|
+
protected chat: ChatInstance | null = null;
|
|
99
|
+
protected _botUserId: string;
|
|
100
|
+
protected _userName: string;
|
|
101
|
+
|
|
102
|
+
/** Bot username used for @-mention detection. */
|
|
103
|
+
get userName(): string {
|
|
104
|
+
return this._userName;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Bot user ID used for self-message detection and the `X-User-Id` header. */
|
|
108
|
+
get botUserId(): string | undefined {
|
|
109
|
+
return this._botUserId;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
constructor(config: RocketChatAdapterConfig = {}) {
|
|
113
|
+
const baseUrl = config.baseUrl ?? process.env.ROCKETCHAT_URL;
|
|
114
|
+
if (!baseUrl) {
|
|
115
|
+
throw new ValidationError(
|
|
116
|
+
"rocketchat",
|
|
117
|
+
"Server URL is required. Set ROCKETCHAT_URL or provide baseUrl in config.",
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const token = config.token ?? process.env.ROCKETCHAT_TOKEN;
|
|
121
|
+
if (!token) {
|
|
122
|
+
throw new ValidationError(
|
|
123
|
+
"rocketchat",
|
|
124
|
+
"Authentication is required. Set ROCKETCHAT_TOKEN or provide token in config.",
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const botUserId = config.botUserId ?? process.env.ROCKETCHAT_USER_ID;
|
|
128
|
+
if (!botUserId) {
|
|
129
|
+
throw new ValidationError(
|
|
130
|
+
"rocketchat",
|
|
131
|
+
"Bot user ID is required. Set ROCKETCHAT_USER_ID or provide botUserId in config.",
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
const webhookToken = config.webhookToken ?? process.env.ROCKETCHAT_WEBHOOK_TOKEN;
|
|
135
|
+
if (!webhookToken) {
|
|
136
|
+
throw new ValidationError(
|
|
137
|
+
"rocketchat",
|
|
138
|
+
"Webhook verification is required. Set ROCKETCHAT_WEBHOOK_TOKEN or provide webhookToken in config.",
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.baseUrl = trimTrailingSlashes(baseUrl);
|
|
143
|
+
this.token = token;
|
|
144
|
+
this._botUserId = botUserId;
|
|
145
|
+
this.webhookToken = webhookToken;
|
|
146
|
+
this._userName = config.userName ?? process.env.ROCKETCHAT_BOT_USERNAME ?? DEFAULT_USER_NAME;
|
|
147
|
+
this.logger = config.logger ?? new ConsoleLogger("info").child("rocketchat");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async initialize(chat: ChatInstance): Promise<void> {
|
|
151
|
+
this.chat = chat;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Whether a user is the bot, by comparing user IDs.
|
|
156
|
+
*/
|
|
157
|
+
protected isSelf(userId: string): boolean {
|
|
158
|
+
return userId === this._botUserId;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ===========================================================================
|
|
162
|
+
// REST API
|
|
163
|
+
// ===========================================================================
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Call the Rocket.Chat REST API with the adapter's credentials.
|
|
167
|
+
*
|
|
168
|
+
* `path` is relative to `/api/v1`, for example `/channels.info`. Use this
|
|
169
|
+
* for any Rocket.Chat API call that isn't covered by the Chat SDK surface.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* const rocketchat = bot.getAdapter("rocketchat");
|
|
174
|
+
* const info = await rocketchat.request("GET", "/channels.info", {
|
|
175
|
+
* query: { roomId: "GENERAL" },
|
|
176
|
+
* });
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
async request<T>(method: string, path: string, options: RequestOptions = {}): Promise<T> {
|
|
180
|
+
const url = new URL(`${this.baseUrl}/api/v1${path}`);
|
|
181
|
+
for (const [key, value] of Object.entries(options.query ?? {})) {
|
|
182
|
+
if (value !== undefined) {
|
|
183
|
+
url.searchParams.set(key, String(value));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const headers: Record<string, string> = {
|
|
188
|
+
"x-auth-token": this.token,
|
|
189
|
+
"x-user-id": this._botUserId,
|
|
190
|
+
accept: "application/json",
|
|
191
|
+
};
|
|
192
|
+
let body: string | undefined;
|
|
193
|
+
if (options.body !== undefined) {
|
|
194
|
+
headers["content-type"] = "application/json";
|
|
195
|
+
body = JSON.stringify(options.body);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let response: Response;
|
|
199
|
+
try {
|
|
200
|
+
// Refuse redirects. `fetch` drops only `Authorization` on a
|
|
201
|
+
// cross-origin redirect, so a followed redirect would carry
|
|
202
|
+
// `x-auth-token` to whatever host the instance points at.
|
|
203
|
+
response = await fetch(url, { method, headers, body, redirect: "error" });
|
|
204
|
+
} catch (error) {
|
|
205
|
+
throw new NetworkError(
|
|
206
|
+
"rocketchat",
|
|
207
|
+
`Rocket.Chat API request failed: ${method} ${path}`,
|
|
208
|
+
error instanceof Error ? error : undefined,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (!response.ok) {
|
|
213
|
+
await this.throwApiError(response, method, path);
|
|
214
|
+
}
|
|
215
|
+
return (await response.json()) as T;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
protected async throwApiError(response: Response, method: string, path: string): Promise<never> {
|
|
219
|
+
const detail = await response.text().catch(() => "");
|
|
220
|
+
this.logger.debug("Rocket.Chat API error", {
|
|
221
|
+
method,
|
|
222
|
+
path,
|
|
223
|
+
status: response.status,
|
|
224
|
+
detail,
|
|
225
|
+
});
|
|
226
|
+
const parsed = parseRocketChatErrorMessage(detail);
|
|
227
|
+
switch (response.status) {
|
|
228
|
+
case 401:
|
|
229
|
+
throw new AuthenticationError(
|
|
230
|
+
"rocketchat",
|
|
231
|
+
"Rocket.Chat rejected the credentials. Check that ROCKETCHAT_TOKEN and ROCKETCHAT_USER_ID are valid.",
|
|
232
|
+
);
|
|
233
|
+
case 403:
|
|
234
|
+
throw new PermissionError("rocketchat", `${method} ${path}`, "api");
|
|
235
|
+
case 404:
|
|
236
|
+
throw new ResourceNotFoundError("rocketchat", "resource", path);
|
|
237
|
+
case 429: {
|
|
238
|
+
const retryAfter = Number.parseInt(response.headers.get("retry-after") ?? "", 10);
|
|
239
|
+
throw new AdapterRateLimitError(
|
|
240
|
+
"rocketchat",
|
|
241
|
+
Number.isNaN(retryAfter) ? undefined : retryAfter,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
case 400:
|
|
245
|
+
throw new ValidationError("rocketchat", parsed ?? `Invalid request: ${method} ${path}`);
|
|
246
|
+
default:
|
|
247
|
+
throw new NetworkError(
|
|
248
|
+
"rocketchat",
|
|
249
|
+
`Rocket.Chat API error ${response.status}: ${parsed ?? detail}`,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ===========================================================================
|
|
255
|
+
// Webhooks
|
|
256
|
+
// ===========================================================================
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Handle an incoming request from a Rocket.Chat Outgoing Webhook
|
|
260
|
+
* integration.
|
|
261
|
+
*/
|
|
262
|
+
async handleWebhook(request: Request, options?: WebhookOptions): Promise<Response> {
|
|
263
|
+
const body = await request.text();
|
|
264
|
+
|
|
265
|
+
let payload: RocketChatWebhookPayload | null;
|
|
266
|
+
try {
|
|
267
|
+
payload = JSON.parse(body);
|
|
268
|
+
} catch {
|
|
269
|
+
this.logger.error("Rocket.Chat webhook invalid JSON");
|
|
270
|
+
return new Response("Invalid JSON", { status: 400 });
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (typeof payload !== "object" || payload === null) {
|
|
274
|
+
return new Response("ok", { status: 200 });
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (!this.verifyToken(payload.token)) {
|
|
278
|
+
this.logger.debug("Rocket.Chat webhook token verification failed");
|
|
279
|
+
return new Response("Invalid token", { status: 401 });
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!this.chat) {
|
|
283
|
+
this.logger.warn("Chat instance not initialized, ignoring message");
|
|
284
|
+
return new Response("ok", { status: 200 });
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (payload.bot || this.isSelf(payload.user_id)) {
|
|
288
|
+
this.logger.debug("Ignoring message from self", {
|
|
289
|
+
messageId: payload.message_id,
|
|
290
|
+
});
|
|
291
|
+
return new Response("ok", { status: 200 });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const message = this.buildMessageFromWebhook(payload);
|
|
295
|
+
// `void` marks the fire-and-forget. processMessage catches
|
|
296
|
+
// its own errors and registers the work with `options.waitUntil`.
|
|
297
|
+
void this.chat.processMessage(this, message.threadId, message, options);
|
|
298
|
+
|
|
299
|
+
return new Response("ok", { status: 200 });
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Verify the webhook's `token` field with a timing-safe comparison.
|
|
304
|
+
*/
|
|
305
|
+
protected verifyToken(token: string | undefined): boolean {
|
|
306
|
+
if (!token) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
// Compare fixed-length digests, so a length mismatch does
|
|
310
|
+
// not return early and leak the secret's length.
|
|
311
|
+
const expected = createHash("sha256").update(this.webhookToken).digest();
|
|
312
|
+
const received = createHash("sha256").update(token).digest();
|
|
313
|
+
return timingSafeEqual(expected, received);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
protected buildMessageFromWebhook(
|
|
317
|
+
payload: RocketChatWebhookPayload,
|
|
318
|
+
): Message<RocketChatRawMessage> {
|
|
319
|
+
const message: RocketChatMessage = {
|
|
320
|
+
_id: payload.message_id,
|
|
321
|
+
msg: payload.text,
|
|
322
|
+
rid: payload.channel_id,
|
|
323
|
+
ts: payload.timestamp,
|
|
324
|
+
u: {
|
|
325
|
+
_id: payload.user_id,
|
|
326
|
+
username: payload.user_name,
|
|
327
|
+
},
|
|
328
|
+
...(payload.tmid ? { tmid: payload.tmid } : {}),
|
|
329
|
+
};
|
|
330
|
+
return this.buildMessage({ message, roomId: payload.channel_id });
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// ===========================================================================
|
|
334
|
+
// Parsing
|
|
335
|
+
// ===========================================================================
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Parse a raw Rocket.Chat message into a normalized Message.
|
|
339
|
+
*/
|
|
340
|
+
parseMessage(raw: RocketChatRawMessage): Message<RocketChatRawMessage> {
|
|
341
|
+
return this.buildMessage(raw);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
protected buildMessage(raw: RocketChatRawMessage): Message<RocketChatRawMessage> {
|
|
345
|
+
const { message } = raw;
|
|
346
|
+
const threadId = this.encodeThreadId({
|
|
347
|
+
roomId: raw.roomId,
|
|
348
|
+
...(message.tmid ? { tmid: message.tmid } : {}),
|
|
349
|
+
});
|
|
350
|
+
const { formatted, text } = this.formatConverter.parseIncoming(message.msg);
|
|
351
|
+
|
|
352
|
+
return new Message({
|
|
353
|
+
id: message._id,
|
|
354
|
+
threadId,
|
|
355
|
+
text,
|
|
356
|
+
formatted,
|
|
357
|
+
raw,
|
|
358
|
+
author: this.parseAuthor(message.u),
|
|
359
|
+
isMention: mentionsUser(text, this._userName),
|
|
360
|
+
metadata: {
|
|
361
|
+
dateSent: parseRocketChatDate(message.ts),
|
|
362
|
+
edited: Boolean(message.editedAt),
|
|
363
|
+
...(message.editedAt ? { editedAt: parseRocketChatDate(message.editedAt) } : {}),
|
|
364
|
+
},
|
|
365
|
+
attachments: [],
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Parse a Rocket.Chat message author into an Author.
|
|
371
|
+
*/
|
|
372
|
+
protected parseAuthor(user: RocketChatMessageUser): Author {
|
|
373
|
+
return {
|
|
374
|
+
userId: user._id,
|
|
375
|
+
userName: user.username,
|
|
376
|
+
fullName: user.name || user.username,
|
|
377
|
+
isBot: "unknown",
|
|
378
|
+
isMe: this.isSelf(user._id),
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// ===========================================================================
|
|
383
|
+
// Messages
|
|
384
|
+
// ===========================================================================
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Render a postable message to a Rocket.Chat-compatible string.
|
|
388
|
+
*
|
|
389
|
+
* Cards, buttons, and modals aren't supported in this version: callers
|
|
390
|
+
* posting a `CardElement` get its fallback text (or a `[Unsupported
|
|
391
|
+
* content]` placeholder) rendered as plain text.
|
|
392
|
+
*/
|
|
393
|
+
protected renderBody(message: AdapterPostableMessage): string {
|
|
394
|
+
const body = this.formatConverter.renderPostable(message);
|
|
395
|
+
return convertRocketChatEmojiPlaceholders(body);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Post a message to a room, or reply inside a thread when the thread ID
|
|
400
|
+
* carries a `tmid`.
|
|
401
|
+
*/
|
|
402
|
+
async postMessage(
|
|
403
|
+
threadId: string,
|
|
404
|
+
message: AdapterPostableMessage,
|
|
405
|
+
): Promise<RawMessage<RocketChatRawMessage>> {
|
|
406
|
+
const thread = this.decodeThreadId(threadId);
|
|
407
|
+
const text = this.renderBody(message);
|
|
408
|
+
|
|
409
|
+
const response = await this.request<RocketChatPostMessageResponse>(
|
|
410
|
+
"POST",
|
|
411
|
+
"/chat.postMessage",
|
|
412
|
+
{
|
|
413
|
+
body: {
|
|
414
|
+
roomId: thread.roomId,
|
|
415
|
+
text,
|
|
416
|
+
...(thread.tmid ? { tmid: thread.tmid } : {}),
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
return {
|
|
422
|
+
id: response.message._id,
|
|
423
|
+
threadId,
|
|
424
|
+
raw: { message: response.message, roomId: thread.roomId },
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Edit an existing message.
|
|
430
|
+
*/
|
|
431
|
+
async editMessage(
|
|
432
|
+
threadId: string,
|
|
433
|
+
messageId: string,
|
|
434
|
+
message: AdapterPostableMessage,
|
|
435
|
+
): Promise<RawMessage<RocketChatRawMessage>> {
|
|
436
|
+
const thread = this.decodeThreadId(threadId);
|
|
437
|
+
const response = await this.request<RocketChatUpdateMessageResponse>("POST", "/chat.update", {
|
|
438
|
+
body: {
|
|
439
|
+
roomId: thread.roomId,
|
|
440
|
+
msgId: messageId,
|
|
441
|
+
text: this.renderBody(message),
|
|
442
|
+
},
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
return {
|
|
446
|
+
id: response.message._id,
|
|
447
|
+
threadId,
|
|
448
|
+
raw: { message: response.message, roomId: thread.roomId },
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Delete a message.
|
|
454
|
+
*/
|
|
455
|
+
async deleteMessage(threadId: string, messageId: string): Promise<void> {
|
|
456
|
+
const thread = this.decodeThreadId(threadId);
|
|
457
|
+
await this.request<RocketChatDeleteMessageResponse>("POST", "/chat.delete", {
|
|
458
|
+
body: { roomId: thread.roomId, msgId: messageId },
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Add an emoji reaction to a message.
|
|
464
|
+
*/
|
|
465
|
+
async addReaction(
|
|
466
|
+
_threadId: string,
|
|
467
|
+
messageId: string,
|
|
468
|
+
emoji: EmojiValue | string,
|
|
469
|
+
): Promise<void> {
|
|
470
|
+
await this.request<{ success: boolean }>("POST", "/chat.react", {
|
|
471
|
+
body: {
|
|
472
|
+
messageId,
|
|
473
|
+
emoji: this.toRocketChatEmoji(emoji),
|
|
474
|
+
shouldReact: true,
|
|
475
|
+
},
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Remove the bot's emoji reaction from a message.
|
|
481
|
+
*/
|
|
482
|
+
async removeReaction(
|
|
483
|
+
_threadId: string,
|
|
484
|
+
messageId: string,
|
|
485
|
+
emoji: EmojiValue | string,
|
|
486
|
+
): Promise<void> {
|
|
487
|
+
await this.request<{ success: boolean }>("POST", "/chat.react", {
|
|
488
|
+
body: {
|
|
489
|
+
messageId,
|
|
490
|
+
emoji: this.toRocketChatEmoji(emoji),
|
|
491
|
+
shouldReact: false,
|
|
492
|
+
},
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Convert an SDK emoji to a Rocket.Chat shortcode (no surrounding colons).
|
|
498
|
+
*/
|
|
499
|
+
protected toRocketChatEmoji(emoji: EmojiValue | string): string {
|
|
500
|
+
const name = typeof emoji === "string" ? emoji : emoji.name;
|
|
501
|
+
return defaultEmojiResolver.toSlack(name);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Show typing indicator (no-op). Rocket.Chat's REST API has no endpoint for
|
|
506
|
+
* sending a typing indicator; that's only available over the realtime
|
|
507
|
+
* DDP/WebSocket API, which this adapter doesn't use.
|
|
508
|
+
*/
|
|
509
|
+
async startTyping(_threadId: string, _status?: string): Promise<void> {
|
|
510
|
+
// No REST endpoint exists for typing indicators.
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// ===========================================================================
|
|
514
|
+
// Fetching
|
|
515
|
+
// ===========================================================================
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Fetch messages in a thread, oldest first.
|
|
519
|
+
*
|
|
520
|
+
* Room-level threads (no `tmid`) use `rooms.history`. Thread-reply threads
|
|
521
|
+
* use `chat.getThreadMessages`.
|
|
522
|
+
*/
|
|
523
|
+
async fetchMessages(
|
|
524
|
+
threadId: string,
|
|
525
|
+
options?: FetchOptions,
|
|
526
|
+
): Promise<FetchResult<RocketChatRawMessage>> {
|
|
527
|
+
const thread = this.decodeThreadId(threadId);
|
|
528
|
+
const limit = options?.limit ?? 50;
|
|
529
|
+
const direction = options?.direction ?? "backward";
|
|
530
|
+
|
|
531
|
+
if (thread.tmid) {
|
|
532
|
+
const response = await this.request<RocketChatGetThreadMessagesResponse>(
|
|
533
|
+
"GET",
|
|
534
|
+
"/chat.getThreadMessages",
|
|
535
|
+
{
|
|
536
|
+
query: {
|
|
537
|
+
tmid: thread.tmid,
|
|
538
|
+
count: limit,
|
|
539
|
+
offset:
|
|
540
|
+
options?.cursor && /^\d+$/.test(options.cursor)
|
|
541
|
+
? Number.parseInt(options.cursor, 10)
|
|
542
|
+
: undefined,
|
|
543
|
+
sort: JSON.stringify({ ts: direction === "backward" ? -1 : 1 }),
|
|
544
|
+
},
|
|
545
|
+
},
|
|
546
|
+
);
|
|
547
|
+
const messages = response.messages
|
|
548
|
+
.map((message) => this.buildMessage({ message, roomId: thread.roomId }))
|
|
549
|
+
.sort((a, b) => a.metadata.dateSent.getTime() - b.metadata.dateSent.getTime());
|
|
550
|
+
const nextOffset = response.offset + response.count;
|
|
551
|
+
return {
|
|
552
|
+
messages,
|
|
553
|
+
nextCursor: nextOffset < response.total ? String(nextOffset) : undefined,
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const response = await this.request<RocketChatRoomsHistoryResponse>("GET", "/rooms.history", {
|
|
558
|
+
query: {
|
|
559
|
+
roomId: thread.roomId,
|
|
560
|
+
count: limit,
|
|
561
|
+
...(direction === "backward" ? { previous: options?.cursor } : { next: options?.cursor }),
|
|
562
|
+
},
|
|
563
|
+
});
|
|
564
|
+
// rooms.history returns messages newest first; the SDK expects
|
|
565
|
+
// chronological (oldest first) order within the page.
|
|
566
|
+
const messages = [...response.messages]
|
|
567
|
+
.reverse()
|
|
568
|
+
.map((message) => this.buildMessage({ message, roomId: thread.roomId }));
|
|
569
|
+
const nextCursor =
|
|
570
|
+
direction === "backward"
|
|
571
|
+
? (response.cursor?.previous ?? undefined)
|
|
572
|
+
: (response.cursor?.next ?? undefined);
|
|
573
|
+
return { messages, nextCursor };
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Fetch a single message by ID.
|
|
578
|
+
*/
|
|
579
|
+
async fetchMessage(
|
|
580
|
+
threadId: string,
|
|
581
|
+
messageId: string,
|
|
582
|
+
): Promise<Message<RocketChatRawMessage> | null> {
|
|
583
|
+
const thread = this.decodeThreadId(threadId);
|
|
584
|
+
try {
|
|
585
|
+
const response = await this.request<RocketChatGetMessageResponse>("GET", "/chat.getMessage", {
|
|
586
|
+
query: { msgId: messageId },
|
|
587
|
+
});
|
|
588
|
+
return this.buildMessage({
|
|
589
|
+
message: response.message,
|
|
590
|
+
roomId: thread.roomId,
|
|
591
|
+
});
|
|
592
|
+
} catch (error) {
|
|
593
|
+
if (error instanceof ResourceNotFoundError) {
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
throw error;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Fetch thread (room) metadata.
|
|
602
|
+
*/
|
|
603
|
+
async fetchThread(threadId: string): Promise<ThreadInfo> {
|
|
604
|
+
const thread = this.decodeThreadId(threadId);
|
|
605
|
+
const response = await this.request<RocketChatRoomInfoResponse>("GET", "/rooms.info", {
|
|
606
|
+
query: { roomId: thread.roomId },
|
|
607
|
+
});
|
|
608
|
+
const { room } = response;
|
|
609
|
+
|
|
610
|
+
return {
|
|
611
|
+
id: threadId,
|
|
612
|
+
channelId: `rocketchat:${thread.roomId}`,
|
|
613
|
+
channelName: room.fname ?? room.name,
|
|
614
|
+
isDM: room.t === "d",
|
|
615
|
+
metadata: { roomId: thread.roomId, type: room.t, topic: room.topic },
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Fetch Rocket.Chat room info as channel metadata.
|
|
621
|
+
*/
|
|
622
|
+
async fetchChannelInfo(channelId: string): Promise<ChannelInfo> {
|
|
623
|
+
const roomId = this.decodeChannelId(channelId);
|
|
624
|
+
const response = await this.request<RocketChatRoomInfoResponse>("GET", "/rooms.info", {
|
|
625
|
+
query: { roomId },
|
|
626
|
+
});
|
|
627
|
+
const { room } = response;
|
|
628
|
+
|
|
629
|
+
return {
|
|
630
|
+
id: channelId,
|
|
631
|
+
name: room.fname ?? room.name ?? roomId,
|
|
632
|
+
isDM: room.t === "d",
|
|
633
|
+
metadata: { roomId, type: room.t, topic: room.topic },
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Render formatted content to Rocket.Chat markdown.
|
|
639
|
+
*/
|
|
640
|
+
renderFormatted(content: FormattedContent): string {
|
|
641
|
+
return this.formatConverter.fromAst(content);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Look up a Rocket.Chat user by ID.
|
|
646
|
+
*/
|
|
647
|
+
async getUser(userId: string): Promise<UserInfo | null> {
|
|
648
|
+
try {
|
|
649
|
+
const response = await this.request<RocketChatUserInfoResponse>("GET", "/users.info", {
|
|
650
|
+
query: { userId },
|
|
651
|
+
});
|
|
652
|
+
const { user } = response;
|
|
653
|
+
return {
|
|
654
|
+
fullName: user.name || user.username,
|
|
655
|
+
isBot: user.type === "bot" || user.type === "app",
|
|
656
|
+
userId: user._id,
|
|
657
|
+
userName: user.username,
|
|
658
|
+
...(user.emails?.[0]?.address ? { email: user.emails[0].address } : {}),
|
|
659
|
+
};
|
|
660
|
+
} catch (error) {
|
|
661
|
+
this.logger.debug("Failed to fetch user", { userId, error });
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// ===========================================================================
|
|
667
|
+
// Thread IDs
|
|
668
|
+
// ===========================================================================
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Encode platform data into a thread ID string.
|
|
672
|
+
*
|
|
673
|
+
* Thread ID formats:
|
|
674
|
+
* - Room-level: `rocketchat:{roomId}`
|
|
675
|
+
* - Thread reply: `rocketchat:{roomId}:{tmid}`
|
|
676
|
+
*/
|
|
677
|
+
encodeThreadId(platformData: RocketChatThreadId): string {
|
|
678
|
+
const { roomId, tmid } = platformData;
|
|
679
|
+
return tmid ? `rocketchat:${roomId}:${tmid}` : `rocketchat:${roomId}`;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Decode a thread ID string back to platform data.
|
|
684
|
+
*/
|
|
685
|
+
decodeThreadId(threadId: string): RocketChatThreadId {
|
|
686
|
+
const match = threadId.match(THREAD_ID_PATTERN);
|
|
687
|
+
const roomId = match?.[1];
|
|
688
|
+
if (!match || !roomId) {
|
|
689
|
+
throw new ValidationError("rocketchat", `Invalid Rocket.Chat thread ID: ${threadId}`);
|
|
690
|
+
}
|
|
691
|
+
return { roomId, ...(match[2] ? { tmid: match[2] } : {}) };
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* Derive the channel (room) ID from a thread ID.
|
|
696
|
+
* `rocketchat:{roomId}:{tmid}` -> `rocketchat:{roomId}`
|
|
697
|
+
*/
|
|
698
|
+
channelIdFromThreadId(threadId: string): string {
|
|
699
|
+
return `rocketchat:${this.decodeThreadId(threadId).roomId}`;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
protected decodeChannelId(channelId: string): string {
|
|
703
|
+
const roomId = channelId.match(CHANNEL_ID_PATTERN)?.[1];
|
|
704
|
+
if (!roomId) {
|
|
705
|
+
throw new ValidationError("rocketchat", `Invalid Rocket.Chat channel ID: ${channelId}`);
|
|
706
|
+
}
|
|
707
|
+
return roomId;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Pull a readable message out of a Rocket.Chat error body
|
|
713
|
+
* (`{ success: false, error, errorType? }`).
|
|
714
|
+
*/
|
|
715
|
+
function parseRocketChatErrorMessage(detail: string): string | undefined {
|
|
716
|
+
let parsed: unknown;
|
|
717
|
+
try {
|
|
718
|
+
parsed = JSON.parse(detail);
|
|
719
|
+
} catch {
|
|
720
|
+
return undefined;
|
|
721
|
+
}
|
|
722
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
723
|
+
return undefined;
|
|
724
|
+
}
|
|
725
|
+
const { error } = parsed as RocketChatErrorResponse;
|
|
726
|
+
return typeof error === "string" ? error : undefined;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* Remove trailing slashes from a URL. A loop rather than a `/\/+$/` regex,
|
|
731
|
+
* which backtracks quadratically on long runs of slashes.
|
|
732
|
+
*/
|
|
733
|
+
function trimTrailingSlashes(url: string): string {
|
|
734
|
+
let end = url.length;
|
|
735
|
+
while (end > 0 && url[end - 1] === "/") {
|
|
736
|
+
end -= 1;
|
|
737
|
+
}
|
|
738
|
+
return url.slice(0, end);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Parse a Rocket.Chat timestamp. The REST API returns ISO 8601; outgoing
|
|
743
|
+
* webhooks send milliseconds since epoch as a string.
|
|
744
|
+
*/
|
|
745
|
+
function parseRocketChatDate(value: string): Date {
|
|
746
|
+
return /^\d+$/.test(value) ? new Date(Number.parseInt(value, 10)) : new Date(value);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Whether plain text mentions `@userName`.
|
|
751
|
+
*/
|
|
752
|
+
function mentionsUser(text: string, userName: string): boolean {
|
|
753
|
+
const pattern = new RegExp(
|
|
754
|
+
`(?<!\\w)@${userName.replace(REGEX_SPECIAL_PATTERN, "\\$&")}(?![\\w-])`,
|
|
755
|
+
"i",
|
|
756
|
+
);
|
|
757
|
+
return pattern.test(text);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Convert `{{emoji:name}}` placeholders to Rocket.Chat's `:shortcode:` syntax.
|
|
762
|
+
*/
|
|
763
|
+
function convertRocketChatEmojiPlaceholders(text: string): string {
|
|
764
|
+
return text.replace(EMOJI_PLACEHOLDER_REGEX, (_, emojiName: string) => {
|
|
765
|
+
return `:${defaultEmojiResolver.toSlack(emojiName)}:`;
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* Create a new Rocket.Chat adapter instance.
|
|
771
|
+
*
|
|
772
|
+
* @example
|
|
773
|
+
* ```typescript
|
|
774
|
+
* const bot = new Chat({
|
|
775
|
+
* userName: "my-bot",
|
|
776
|
+
* adapters: {
|
|
777
|
+
* rocketchat: createRocketChatAdapter({
|
|
778
|
+
* baseUrl: process.env.ROCKETCHAT_URL!,
|
|
779
|
+
* token: process.env.ROCKETCHAT_TOKEN!,
|
|
780
|
+
* botUserId: process.env.ROCKETCHAT_USER_ID!,
|
|
781
|
+
* webhookToken: process.env.ROCKETCHAT_WEBHOOK_TOKEN!,
|
|
782
|
+
* }),
|
|
783
|
+
* },
|
|
784
|
+
* state: createMemoryState(),
|
|
785
|
+
* });
|
|
786
|
+
* ```
|
|
787
|
+
*/
|
|
788
|
+
export function createRocketChatAdapter(config?: RocketChatAdapterConfig): RocketChatAdapter {
|
|
789
|
+
return new RocketChatAdapter(config);
|
|
790
|
+
}
|