@intent-systems/nexus 2026.1.5-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +222 -0
- package/LICENSE +21 -0
- package/README-header.png +0 -0
- package/README.md +462 -0
- package/dist/agents/agent-paths.js +16 -0
- package/dist/agents/agent-scope.js +44 -0
- package/dist/agents/auth-profiles.js +626 -0
- package/dist/agents/bash-process-registry.js +126 -0
- package/dist/agents/bash-tools.js +838 -0
- package/dist/agents/chutes-oauth.js +47 -0
- package/dist/agents/clawdbot-tools.js +62 -0
- package/dist/agents/context.js +34 -0
- package/dist/agents/defaults.js +6 -0
- package/dist/agents/memory-search.js +80 -0
- package/dist/agents/model-auth.js +115 -0
- package/dist/agents/model-catalog.js +55 -0
- package/dist/agents/model-fallback.js +210 -0
- package/dist/agents/model-scan.js +263 -0
- package/dist/agents/model-selection.js +152 -0
- package/dist/agents/models-config.js +171 -0
- package/dist/agents/nexus-tools.js +46 -0
- package/dist/agents/pi-embedded-block-chunker.js +188 -0
- package/dist/agents/pi-embedded-helpers.js +139 -0
- package/dist/agents/pi-embedded-runner.js +932 -0
- package/dist/agents/pi-embedded-subscribe.js +541 -0
- package/dist/agents/pi-embedded-utils.js +20 -0
- package/dist/agents/pi-embedded.js +1 -0
- package/dist/agents/pi-extensions/compaction-safeguard.js +140 -0
- package/dist/agents/pi-tool-definition-adapter.js +17 -0
- package/dist/agents/pi-tools.js +510 -0
- package/dist/agents/pi-tools.schema.js +358 -0
- package/dist/agents/sandbox-paths.js +68 -0
- package/dist/agents/sandbox.js +667 -0
- package/dist/agents/shell-utils.js +53 -0
- package/dist/agents/skill-runner.js +224 -0
- package/dist/agents/skill-state.js +164 -0
- package/dist/agents/skill-tools.js +191 -0
- package/dist/agents/skill-usage.js +43 -0
- package/dist/agents/skills-install.js +244 -0
- package/dist/agents/skills-status.js +157 -0
- package/dist/agents/skills.js +472 -0
- package/dist/agents/subagent-registry.js +321 -0
- package/dist/agents/subagent-registry.store.js +47 -0
- package/dist/agents/system-prompt.js +179 -0
- package/dist/agents/timeout.js +26 -0
- package/dist/agents/tool-display.js +155 -0
- package/dist/agents/tool-display.json +236 -0
- package/dist/agents/tool-images.js +138 -0
- package/dist/agents/tool-policy.js +87 -0
- package/dist/agents/tools/agent-step.js +41 -0
- package/dist/agents/tools/browser-tool.js +295 -0
- package/dist/agents/tools/canvas-tool.js +193 -0
- package/dist/agents/tools/common.js +88 -0
- package/dist/agents/tools/cron-tool.js +141 -0
- package/dist/agents/tools/discord-actions-guild.js +186 -0
- package/dist/agents/tools/discord-actions-messaging.js +313 -0
- package/dist/agents/tools/discord-actions-moderation.js +70 -0
- package/dist/agents/tools/discord-actions.js +56 -0
- package/dist/agents/tools/discord-schema.js +199 -0
- package/dist/agents/tools/discord-tool.js +16 -0
- package/dist/agents/tools/gateway-tool.js +46 -0
- package/dist/agents/tools/gateway.js +28 -0
- package/dist/agents/tools/image-tool.js +140 -0
- package/dist/agents/tools/memory-tool.js +92 -0
- package/dist/agents/tools/nodes-tool.js +413 -0
- package/dist/agents/tools/nodes-utils.js +92 -0
- package/dist/agents/tools/sessions-announce-target.js +35 -0
- package/dist/agents/tools/sessions-helpers.js +88 -0
- package/dist/agents/tools/sessions-history-tool.js +137 -0
- package/dist/agents/tools/sessions-list-tool.js +196 -0
- package/dist/agents/tools/sessions-send-helpers.js +103 -0
- package/dist/agents/tools/sessions-send-tool.js +371 -0
- package/dist/agents/tools/sessions-spawn-tool.js +319 -0
- package/dist/agents/tools/slack-actions.js +129 -0
- package/dist/agents/tools/slack-schema.js +59 -0
- package/dist/agents/tools/slack-tool.js +16 -0
- package/dist/agents/tools/telegram-actions.js +159 -0
- package/dist/agents/tools/telegram-schema.js +28 -0
- package/dist/agents/tools/telegram-tool.js +16 -0
- package/dist/agents/tools/whatsapp-login-tool.js +63 -0
- package/dist/agents/usage.js +58 -0
- package/dist/agents/workspace.js +264 -0
- package/dist/auto-reply/chunk.js +177 -0
- package/dist/auto-reply/command-auth.js +44 -0
- package/dist/auto-reply/command-detection.js +22 -0
- package/dist/auto-reply/envelope.js +30 -0
- package/dist/auto-reply/group-activation.js +20 -0
- package/dist/auto-reply/heartbeat.js +58 -0
- package/dist/auto-reply/model.js +22 -0
- package/dist/auto-reply/reply/abort.js +14 -0
- package/dist/auto-reply/reply/agent-runner.js +426 -0
- package/dist/auto-reply/reply/bash-command.js +314 -0
- package/dist/auto-reply/reply/block-streaming.js +34 -0
- package/dist/auto-reply/reply/body.js +29 -0
- package/dist/auto-reply/reply/commands.js +332 -0
- package/dist/auto-reply/reply/directive-handling.js +626 -0
- package/dist/auto-reply/reply/directives.js +59 -0
- package/dist/auto-reply/reply/dispatch-from-config.js +23 -0
- package/dist/auto-reply/reply/followup-runner.js +181 -0
- package/dist/auto-reply/reply/groups.js +152 -0
- package/dist/auto-reply/reply/mentions.js +64 -0
- package/dist/auto-reply/reply/model-selection.js +209 -0
- package/dist/auto-reply/reply/queue.js +399 -0
- package/dist/auto-reply/reply/reply-dispatcher.js +68 -0
- package/dist/auto-reply/reply/reply-tags.js +26 -0
- package/dist/auto-reply/reply/session-updates.js +103 -0
- package/dist/auto-reply/reply/session.js +169 -0
- package/dist/auto-reply/reply/typing.js +125 -0
- package/dist/auto-reply/reply.js +655 -0
- package/dist/auto-reply/send-policy.js +28 -0
- package/dist/auto-reply/status.js +197 -0
- package/dist/auto-reply/templating.js +9 -0
- package/dist/auto-reply/thinking.js +49 -0
- package/dist/auto-reply/tokens.js +2 -0
- package/dist/auto-reply/tool-meta.js +74 -0
- package/dist/auto-reply/transcription.js +57 -0
- package/dist/auto-reply/types.js +1 -0
- package/dist/browser/bridge-server.js +37 -0
- package/dist/browser/cdp.js +382 -0
- package/dist/browser/chrome.js +432 -0
- package/dist/browser/client-actions-core.js +67 -0
- package/dist/browser/client-actions-observe.js +24 -0
- package/dist/browser/client-actions-types.js +1 -0
- package/dist/browser/client-actions.js +3 -0
- package/dist/browser/client-fetch.js +43 -0
- package/dist/browser/client.js +105 -0
- package/dist/browser/config.js +155 -0
- package/dist/browser/constants.js +5 -0
- package/dist/browser/profiles-service.js +124 -0
- package/dist/browser/profiles.js +96 -0
- package/dist/browser/pw-ai.js +2 -0
- package/dist/browser/pw-session.js +144 -0
- package/dist/browser/pw-tools-core.js +363 -0
- package/dist/browser/routes/agent.js +535 -0
- package/dist/browser/routes/basic.js +155 -0
- package/dist/browser/routes/index.js +8 -0
- package/dist/browser/routes/tabs.js +105 -0
- package/dist/browser/routes/utils.js +62 -0
- package/dist/browser/screenshot.js +40 -0
- package/dist/browser/server-context.js +377 -0
- package/dist/browser/server.js +81 -0
- package/dist/browser/target-id.js +18 -0
- package/dist/browser/trash.js +21 -0
- package/dist/canvas-host/a2ui/a2ui.bundle.js +17768 -0
- package/dist/canvas-host/a2ui/index.html +246 -0
- package/dist/canvas-host/a2ui.js +187 -0
- package/dist/canvas-host/server.js +382 -0
- package/dist/channel-web.js +8 -0
- package/dist/cli/browser-cli-actions-input.js +459 -0
- package/dist/cli/browser-cli-actions-observe.js +56 -0
- package/dist/cli/browser-cli-examples.js +31 -0
- package/dist/cli/browser-cli-inspect.js +97 -0
- package/dist/cli/browser-cli-manage.js +286 -0
- package/dist/cli/browser-cli-shared.js +1 -0
- package/dist/cli/browser-cli.js +26 -0
- package/dist/cli/canvas-cli.js +416 -0
- package/dist/cli/cloud-cli.js +336 -0
- package/dist/cli/credential-cli.js +227 -0
- package/dist/cli/cron-cli.js +454 -0
- package/dist/cli/deps.js +17 -0
- package/dist/cli/dns-cli.js +180 -0
- package/dist/cli/gateway-cli.js +665 -0
- package/dist/cli/gateway-rpc.js +20 -0
- package/dist/cli/hooks-cli.js +135 -0
- package/dist/cli/memory-cli.js +101 -0
- package/dist/cli/models-cli.js +248 -0
- package/dist/cli/nodes-camera.js +57 -0
- package/dist/cli/nodes-canvas.js +26 -0
- package/dist/cli/nodes-cli.js +946 -0
- package/dist/cli/nodes-screen.js +37 -0
- package/dist/cli/pairing-cli.js +100 -0
- package/dist/cli/parse-duration.js +20 -0
- package/dist/cli/plugins-cli.js +158 -0
- package/dist/cli/ports.js +97 -0
- package/dist/cli/profile.js +81 -0
- package/dist/cli/program.js +162 -0
- package/dist/cli/prompt.js +19 -0
- package/dist/cli/run-main.js +48 -0
- package/dist/cli/skills-cli.js +132 -0
- package/dist/cli/skills-hub-cli.js +1093 -0
- package/dist/cli/telegram-cli.js +56 -0
- package/dist/cli/tool-connector-cli.js +118 -0
- package/dist/cli/tui-cli.js +35 -0
- package/dist/cli/upstream-sync-cli.js +2833 -0
- package/dist/cli/usage-cli.js +24 -0
- package/dist/cli/wait.js +8 -0
- package/dist/commands/agent-via-gateway.js +115 -0
- package/dist/commands/agent.js +665 -0
- package/dist/commands/antigravity-oauth.js +327 -0
- package/dist/commands/auth-choice-options.js +80 -0
- package/dist/commands/auth-choice.js +134 -0
- package/dist/commands/auth-token.js +31 -0
- package/dist/commands/bootstrap-preset.js +214 -0
- package/dist/commands/capabilities.js +36 -0
- package/dist/commands/chutes-oauth.js +144 -0
- package/dist/commands/claude-md.js +137 -0
- package/dist/commands/config-view.js +11 -0
- package/dist/commands/configure.js +648 -0
- package/dist/commands/credential.js +236 -0
- package/dist/commands/cursor-rules.js +230 -0
- package/dist/commands/doctor-state-migrations.js +358 -0
- package/dist/commands/doctor-ui.js +113 -0
- package/dist/commands/doctor.js +673 -0
- package/dist/commands/health.js +112 -0
- package/dist/commands/identity.js +54 -0
- package/dist/commands/init.js +167 -0
- package/dist/commands/models/aliases.js +85 -0
- package/dist/commands/models/fallbacks.js +123 -0
- package/dist/commands/models/image-fallbacks.js +123 -0
- package/dist/commands/models/list.js +347 -0
- package/dist/commands/models/scan.js +271 -0
- package/dist/commands/models/set-image.js +27 -0
- package/dist/commands/models/set.js +27 -0
- package/dist/commands/models/shared.js +73 -0
- package/dist/commands/models.js +7 -0
- package/dist/commands/onboard-auth.js +101 -0
- package/dist/commands/onboard-channels.js +814 -0
- package/dist/commands/onboard-eve-identity.js +98 -0
- package/dist/commands/onboard-github.js +153 -0
- package/dist/commands/onboard-helpers.js +303 -0
- package/dist/commands/onboard-interactive.js +17 -0
- package/dist/commands/onboard-non-interactive.js +228 -0
- package/dist/commands/onboard-providers.js +829 -0
- package/dist/commands/onboard-quickstart.js +185 -0
- package/dist/commands/onboard-remote.js +120 -0
- package/dist/commands/onboard-skills.js +148 -0
- package/dist/commands/onboard-types.js +1 -0
- package/dist/commands/onboard.js +19 -0
- package/dist/commands/openai-codex-model-default.js +38 -0
- package/dist/commands/poll.js +64 -0
- package/dist/commands/quest.js +27 -0
- package/dist/commands/reset.js +61 -0
- package/dist/commands/send.js +124 -0
- package/dist/commands/sessions-ingest.js +359 -0
- package/dist/commands/sessions.js +212 -0
- package/dist/commands/setup.js +59 -0
- package/dist/commands/signal-install.js +135 -0
- package/dist/commands/skills-manifest.js +235 -0
- package/dist/commands/status.js +139 -0
- package/dist/commands/suggestions.js +54 -0
- package/dist/commands/systemd-linger.js +71 -0
- package/dist/commands/update.js +16 -0
- package/dist/commands/usage-upload.js +27 -0
- package/dist/config/config.js +6 -0
- package/dist/config/defaults.js +140 -0
- package/dist/config/group-policy.js +49 -0
- package/dist/config/includes.js +183 -0
- package/dist/config/io.js +188 -0
- package/dist/config/legacy-migrate.js +13 -0
- package/dist/config/legacy.js +425 -0
- package/dist/config/paths.js +82 -0
- package/dist/config/port-defaults.js +32 -0
- package/dist/config/schema.js +173 -0
- package/dist/config/sessions.js +611 -0
- package/dist/config/talk.js +31 -0
- package/dist/config/types.js +1 -0
- package/dist/config/validation.js +29 -0
- package/dist/config/zod-schema.js +1110 -0
- package/dist/control-ui/assets/index-D8Q5AI4D.js +2393 -0
- package/dist/control-ui/assets/index-D8Q5AI4D.js.map +1 -0
- package/dist/control-ui/assets/index-g06q5Xc3.css +1 -0
- package/dist/control-ui/favicon.ico +0 -0
- package/dist/control-ui/index.html +16 -0
- package/dist/cron/isolated-agent.js +529 -0
- package/dist/cron/normalize.js +73 -0
- package/dist/cron/parse.js +24 -0
- package/dist/cron/run-log.js +72 -0
- package/dist/cron/schedule.js +24 -0
- package/dist/cron/service.js +471 -0
- package/dist/cron/store.js +43 -0
- package/dist/cron/types.js +1 -0
- package/dist/daemon/constants.js +10 -0
- package/dist/daemon/launchd.js +295 -0
- package/dist/daemon/legacy.js +63 -0
- package/dist/daemon/program-args.js +141 -0
- package/dist/daemon/schtasks.js +269 -0
- package/dist/daemon/service.js +69 -0
- package/dist/daemon/systemd.js +332 -0
- package/dist/discord/index.js +2 -0
- package/dist/discord/monitor.js +1089 -0
- package/dist/discord/probe.js +54 -0
- package/dist/discord/send.js +652 -0
- package/dist/discord/token.js +8 -0
- package/dist/entry.js +16 -0
- package/dist/gateway/auth.js +121 -0
- package/dist/gateway/call.js +103 -0
- package/dist/gateway/chat-attachments.js +41 -0
- package/dist/gateway/client.js +180 -0
- package/dist/gateway/config-reload.js +274 -0
- package/dist/gateway/control-ui.js +184 -0
- package/dist/gateway/hooks-mapping.js +282 -0
- package/dist/gateway/hooks.js +168 -0
- package/dist/gateway/net.js +29 -0
- package/dist/gateway/protocol/index.js +62 -0
- package/dist/gateway/protocol/schema.js +577 -0
- package/dist/gateway/server-bridge-subscriptions.js +93 -0
- package/dist/gateway/server-bridge.js +1066 -0
- package/dist/gateway/server-browser.js +11 -0
- package/dist/gateway/server-channels.js +680 -0
- package/dist/gateway/server-chat.js +159 -0
- package/dist/gateway/server-constants.js +8 -0
- package/dist/gateway/server-discovery.js +62 -0
- package/dist/gateway/server-http.js +165 -0
- package/dist/gateway/server-methods/agent-job.js +114 -0
- package/dist/gateway/server-methods/agent.js +254 -0
- package/dist/gateway/server-methods/channels.js +239 -0
- package/dist/gateway/server-methods/chat.js +207 -0
- package/dist/gateway/server-methods/config.js +50 -0
- package/dist/gateway/server-methods/connect.js +6 -0
- package/dist/gateway/server-methods/cron.js +89 -0
- package/dist/gateway/server-methods/health.js +28 -0
- package/dist/gateway/server-methods/models.js +16 -0
- package/dist/gateway/server-methods/nodes.js +294 -0
- package/dist/gateway/server-methods/providers.js +257 -0
- package/dist/gateway/server-methods/send.js +254 -0
- package/dist/gateway/server-methods/sessions.js +382 -0
- package/dist/gateway/server-methods/skills.js +83 -0
- package/dist/gateway/server-methods/system.js +118 -0
- package/dist/gateway/server-methods/talk.js +22 -0
- package/dist/gateway/server-methods/types.js +1 -0
- package/dist/gateway/server-methods/voicewake.js +30 -0
- package/dist/gateway/server-methods/web.js +81 -0
- package/dist/gateway/server-methods/wizard.js +100 -0
- package/dist/gateway/server-methods.js +53 -0
- package/dist/gateway/server-providers.js +687 -0
- package/dist/gateway/server-shared.js +1 -0
- package/dist/gateway/server-utils.js +35 -0
- package/dist/gateway/server.js +1478 -0
- package/dist/gateway/session-utils.js +355 -0
- package/dist/gateway/ws-log.js +343 -0
- package/dist/gateway/ws-logging.js +8 -0
- package/dist/globals.js +41 -0
- package/dist/hooks/gmail-ops.js +236 -0
- package/dist/hooks/gmail-setup-utils.js +278 -0
- package/dist/hooks/gmail-watcher.js +190 -0
- package/dist/hooks/gmail.js +177 -0
- package/dist/imessage/client.js +165 -0
- package/dist/imessage/index.js +3 -0
- package/dist/imessage/monitor.js +365 -0
- package/dist/imessage/probe.js +26 -0
- package/dist/imessage/send.js +83 -0
- package/dist/imessage/targets.js +176 -0
- package/dist/index.js +55 -0
- package/dist/infra/agent-events.js +46 -0
- package/dist/infra/binaries.js +9 -0
- package/dist/infra/bonjour-discovery.js +163 -0
- package/dist/infra/bonjour.js +200 -0
- package/dist/infra/bridge/server.js +564 -0
- package/dist/infra/canvas-host-url.js +54 -0
- package/dist/infra/channel-summary.js +78 -0
- package/dist/infra/control-ui-assets.js +112 -0
- package/dist/infra/dotenv.js +15 -0
- package/dist/infra/env.js +8 -0
- package/dist/infra/errors.js +28 -0
- package/dist/infra/event-log.js +251 -0
- package/dist/infra/gateway-lock.js +8 -0
- package/dist/infra/git-commit.js +91 -0
- package/dist/infra/heartbeat-events.js +21 -0
- package/dist/infra/heartbeat-runner.js +458 -0
- package/dist/infra/heartbeat-wake.js +61 -0
- package/dist/infra/is-main.js +37 -0
- package/dist/infra/json-file.js +21 -0
- package/dist/infra/machine-name.js +40 -0
- package/dist/infra/nexus-root.js +56 -0
- package/dist/infra/node-pairing.js +212 -0
- package/dist/infra/path-env.js +93 -0
- package/dist/infra/ports.js +87 -0
- package/dist/infra/provider-summary.js +80 -0
- package/dist/infra/provider-usage.auth.js +189 -0
- package/dist/infra/provider-usage.fetch.claude.js +129 -0
- package/dist/infra/provider-usage.fetch.codex.js +62 -0
- package/dist/infra/provider-usage.fetch.copilot.js +42 -0
- package/dist/infra/provider-usage.fetch.gemini.js +57 -0
- package/dist/infra/provider-usage.fetch.js +6 -0
- package/dist/infra/provider-usage.fetch.minimax.js +214 -0
- package/dist/infra/provider-usage.fetch.shared.js +11 -0
- package/dist/infra/provider-usage.fetch.zai.js +62 -0
- package/dist/infra/provider-usage.format.js +77 -0
- package/dist/infra/provider-usage.js +145 -0
- package/dist/infra/provider-usage.load.js +54 -0
- package/dist/infra/provider-usage.shared.js +19 -0
- package/dist/infra/provider-usage.types.js +1 -0
- package/dist/infra/restart.js +29 -0
- package/dist/infra/retry.js +16 -0
- package/dist/infra/runtime-guard.js +59 -0
- package/dist/infra/shell-env.js +88 -0
- package/dist/infra/system-events.js +71 -0
- package/dist/infra/system-presence.js +217 -0
- package/dist/infra/tailnet.js +46 -0
- package/dist/infra/tailscale.js +149 -0
- package/dist/infra/unhandled-rejections.js +19 -0
- package/dist/infra/usage-suggestions.js +241 -0
- package/dist/infra/usage-upload.js +290 -0
- package/dist/infra/voicewake.js +78 -0
- package/dist/infra/widearea-dns.js +123 -0
- package/dist/infra/ws.js +13 -0
- package/dist/logger.js +52 -0
- package/dist/logging.js +506 -0
- package/dist/macos/gateway-daemon.js +145 -0
- package/dist/macos/relay.js +49 -0
- package/dist/media/constants.js +33 -0
- package/dist/media/host.js +42 -0
- package/dist/media/image-ops.js +119 -0
- package/dist/media/mime.js +123 -0
- package/dist/media/parse.js +83 -0
- package/dist/media/server.js +64 -0
- package/dist/media/store.js +139 -0
- package/dist/polls.js +43 -0
- package/dist/process/command-queue.js +97 -0
- package/dist/process/exec.js +75 -0
- package/dist/provider-web.js +8 -0
- package/dist/providers/github-copilot-auth.js +123 -0
- package/dist/providers/github-copilot-models.js +35 -0
- package/dist/providers/github-copilot-token.js +11 -0
- package/dist/providers/location.js +48 -0
- package/dist/providers/web/index.js +2 -0
- package/dist/runtime.js +8 -0
- package/dist/sessions/level-overrides.js +9 -0
- package/dist/sessions/send-policy.js +68 -0
- package/dist/signal/client.js +134 -0
- package/dist/signal/daemon.js +69 -0
- package/dist/signal/index.js +3 -0
- package/dist/signal/monitor.js +411 -0
- package/dist/signal/probe.js +46 -0
- package/dist/signal/send.js +91 -0
- package/dist/slack/actions.js +97 -0
- package/dist/slack/index.js +5 -0
- package/dist/slack/monitor.js +1270 -0
- package/dist/slack/probe.js +47 -0
- package/dist/slack/send.js +131 -0
- package/dist/slack/token.js +10 -0
- package/dist/telegram/allowed-updates.js +8 -0
- package/dist/telegram/bot.js +724 -0
- package/dist/telegram/download.js +34 -0
- package/dist/telegram/index.js +4 -0
- package/dist/telegram/monitor.js +47 -0
- package/dist/telegram/pairing-store.js +77 -0
- package/dist/telegram/probe.js +63 -0
- package/dist/telegram/proxy.js +9 -0
- package/dist/telegram/reaction-level.js +45 -0
- package/dist/telegram/send.js +151 -0
- package/dist/telegram/sent-message-cache.js +65 -0
- package/dist/telegram/token.js +30 -0
- package/dist/telegram/update-offset-store.js +61 -0
- package/dist/telegram/webhook-set.js +12 -0
- package/dist/telegram/webhook.js +56 -0
- package/dist/tui/commands.js +87 -0
- package/dist/tui/components/assistant-message.js +16 -0
- package/dist/tui/components/chat-log.js +92 -0
- package/dist/tui/components/custom-editor.js +55 -0
- package/dist/tui/components/selectors.js +8 -0
- package/dist/tui/components/tool-execution.js +111 -0
- package/dist/tui/components/user-message.js +17 -0
- package/dist/tui/gateway-chat.js +140 -0
- package/dist/tui/theme/theme.js +80 -0
- package/dist/tui/tui.js +708 -0
- package/dist/utils.js +153 -0
- package/dist/version.js +18 -0
- package/dist/web/accounts.js +86 -0
- package/dist/web/active-listener.js +25 -0
- package/dist/web/auto-reply.js +1256 -0
- package/dist/web/inbound.js +649 -0
- package/dist/web/login-qr.js +230 -0
- package/dist/web/login.js +71 -0
- package/dist/web/media.js +175 -0
- package/dist/web/outbound.js +102 -0
- package/dist/web/qr-image.js +97 -0
- package/dist/web/reconnect.js +60 -0
- package/dist/web/session.js +370 -0
- package/dist/wizard/clack-prompter.js +56 -0
- package/dist/wizard/onboarding.js +620 -0
- package/dist/wizard/prompts.js +6 -0
- package/dist/wizard/session.js +203 -0
- package/docs/AGENTS.default.md +116 -0
- package/docs/CAPABILITIES.md +444 -0
- package/docs/CNAME +1 -0
- package/docs/NEXUS_CORE_REWRITE_SPEC.md +226 -0
- package/docs/RELEASING.md +69 -0
- package/docs/_config.yml +53 -0
- package/docs/_layouts/default.html +145 -0
- package/docs/agent-assisted-install.md +95 -0
- package/docs/agent-loop.md +61 -0
- package/docs/agent-send.md +21 -0
- package/docs/agent.md +108 -0
- package/docs/android.md +133 -0
- package/docs/architecture.md +114 -0
- package/docs/assets/markdown.css +133 -0
- package/docs/assets/pixel-lobster.svg +60 -0
- package/docs/assets/terminal.css +470 -0
- package/docs/assets/theme.js +55 -0
- package/docs/audio.md +48 -0
- package/docs/automation/nexus-sync.md +371 -0
- package/docs/background-process.md +74 -0
- package/docs/bash.md +32 -0
- package/docs/bedrock.md +71 -0
- package/docs/bonjour.md +159 -0
- package/docs/browser-linux-troubleshooting.md +114 -0
- package/docs/browser.md +293 -0
- package/docs/bun.md +56 -0
- package/docs/camera.md +152 -0
- package/docs/clawd.md +212 -0
- package/docs/concepts/usage-tracking.md +29 -0
- package/docs/configuration.md +1666 -0
- package/docs/control-ui.md +83 -0
- package/docs/cron.md +385 -0
- package/docs/dashboard.md +17 -0
- package/docs/device-models.md +46 -0
- package/docs/discord.md +308 -0
- package/docs/discovery.md +112 -0
- package/docs/docker.md +258 -0
- package/docs/docs.json +105 -0
- package/docs/doctor.md +68 -0
- package/docs/elevated.md +31 -0
- package/docs/faq.md +736 -0
- package/docs/feature-inventory/overview.md +141 -0
- package/docs/feature-inventory/rollout-checklist.md +53 -0
- package/docs/feature-inventory/test-matrix.md +87 -0
- package/docs/feature-inventory.md +9 -0
- package/docs/gateway/configuration-examples.md +221 -0
- package/docs/gateway/configuration.md +172 -0
- package/docs/gateway/cron.md +61 -0
- package/docs/gateway/heartbeat.md +207 -0
- package/docs/gateway/pairing.md +109 -0
- package/docs/gateway-lock.md +28 -0
- package/docs/gateway.md +227 -0
- package/docs/gmail-pubsub.md +191 -0
- package/docs/grammy.md +27 -0
- package/docs/group-messages.md +73 -0
- package/docs/groups.md +130 -0
- package/docs/health.md +28 -0
- package/docs/heartbeat.md +73 -0
- package/docs/home-userspace.md +277 -0
- package/docs/hubs.md +148 -0
- package/docs/images.md +51 -0
- package/docs/imessage.md +94 -0
- package/docs/index.md +196 -0
- package/docs/ios.md +372 -0
- package/docs/linux.md +11 -0
- package/docs/location-command.md +95 -0
- package/docs/location.md +46 -0
- package/docs/logging.md +110 -0
- package/docs/lore.md +131 -0
- package/docs/mac/bun.md +133 -0
- package/docs/mac/canvas.md +161 -0
- package/docs/mac/child-process.md +72 -0
- package/docs/mac/dev-setup.md +81 -0
- package/docs/mac/health.md +28 -0
- package/docs/mac/icon.md +26 -0
- package/docs/mac/logging.md +51 -0
- package/docs/mac/menu-bar.md +69 -0
- package/docs/mac/peekaboo.md +170 -0
- package/docs/mac/permissions.md +40 -0
- package/docs/mac/release.md +76 -0
- package/docs/mac/remote.md +57 -0
- package/docs/mac/signing.md +41 -0
- package/docs/mac/skills.md +27 -0
- package/docs/mac/voice-overlay.md +52 -0
- package/docs/mac/voicewake.md +56 -0
- package/docs/mac/webchat.md +27 -0
- package/docs/mac/xpc.md +40 -0
- package/docs/macos.md +104 -0
- package/docs/model-failover.md +75 -0
- package/docs/models.md +91 -0
- package/docs/multi-agent.md +74 -0
- package/docs/nix.md +95 -0
- package/docs/nodes.md +157 -0
- package/docs/onboarding-config-protocol.md +34 -0
- package/docs/onboarding.md +189 -0
- package/docs/pairing.md +85 -0
- package/docs/plans/cron-add-hardening.md +72 -0
- package/docs/plans/group-policy-hardening.md +121 -0
- package/docs/poll.md +52 -0
- package/docs/prereqs.md +67 -0
- package/docs/presence.md +133 -0
- package/docs/proposals/model-config.md +147 -0
- package/docs/provider-routing.md +25 -0
- package/docs/queue.md +78 -0
- package/docs/reference/templates/AGENTS.md +164 -0
- package/docs/remote-gateway-readme.md +153 -0
- package/docs/remote.md +61 -0
- package/docs/research/memory.md +227 -0
- package/docs/rpc.md +35 -0
- package/docs/security.md +200 -0
- package/docs/session-ingestion.md +119 -0
- package/docs/session-tool.md +154 -0
- package/docs/session.md +85 -0
- package/docs/sessions.md +8 -0
- package/docs/setup.md +131 -0
- package/docs/showcase.md +37 -0
- package/docs/signal.md +122 -0
- package/docs/skills-config.md +58 -0
- package/docs/skills.md +153 -0
- package/docs/slack.md +221 -0
- package/docs/subagents.md +72 -0
- package/docs/tailscale.md +71 -0
- package/docs/talk.md +79 -0
- package/docs/telegram.md +96 -0
- package/docs/templates/AGENTS.md +286 -0
- package/docs/templates/BOOTSTRAP.md +35 -0
- package/docs/templates/IDENTITY.md +17 -0
- package/docs/templates/PROFILE.md +14 -0
- package/docs/templates/SOUL.md +41 -0
- package/docs/templates/TOOLS.md +41 -0
- package/docs/templates/USER.md +8 -0
- package/docs/test.md +43 -0
- package/docs/testing-onboarding-quickstart.md +76 -0
- package/docs/testing-philosophy.md +211 -0
- package/docs/thinking.md +46 -0
- package/docs/timezone.md +40 -0
- package/docs/tools.md +346 -0
- package/docs/troubleshooting.md +257 -0
- package/docs/tui.md +71 -0
- package/docs/typebox.md +42 -0
- package/docs/updating.md +138 -0
- package/docs/usage-cloud-aggregation-spec.md +133 -0
- package/docs/usage-suggestions-pipeline.md +126 -0
- package/docs/voicewake.md +61 -0
- package/docs/web.md +115 -0
- package/docs/webchat.md +34 -0
- package/docs/webhook.md +132 -0
- package/docs/whatsapp-clawd.jpg +0 -0
- package/docs/whatsapp.md +170 -0
- package/docs/windows.md +11 -0
- package/docs/wizard.md +167 -0
- package/package.json +209 -0
- package/skills/1password/SKILL.md +54 -0
- package/skills/1password/docs/setup.md +85 -0
- package/skills/1password/docs/troubleshooting.md +63 -0
- package/skills/1password/references/cli-examples.md +29 -0
- package/skills/1password/references/get-started.md +17 -0
- package/skills/agent-browser/SKILL.md +450 -0
- package/skills/agent-browser/docs/browser-use-eval.md +95 -0
- package/skills/agent-browser/docs/first-tests.md +261 -0
- package/skills/agent-browser/docs/wordle-nyt-eval.js +32 -0
- package/skills/aix/SKILL.md +93 -0
- package/skills/aix/docs/embeddings.md +40 -0
- package/skills/aix/docs/setup.md +58 -0
- package/skills/aix/docs/troubleshooting.md +41 -0
- package/skills/aix/references/sql.md +48 -0
- package/skills/apple-notes/SKILL.md +50 -0
- package/skills/apple-reminders/SKILL.md +67 -0
- package/skills/bear-notes/SKILL.md +79 -0
- package/skills/bird/SKILL.md +32 -0
- package/skills/bird/docs/auth.md +31 -0
- package/skills/bird/docs/troubleshooting.md +31 -0
- package/skills/blogwatcher/SKILL.md +46 -0
- package/skills/blucli/SKILL.md +27 -0
- package/skills/brave-search/SKILL.md +36 -0
- package/skills/brave-search/docs/setup.md +40 -0
- package/skills/brave-search/docs/troubleshooting.md +37 -0
- package/skills/brave-search/docs/usage.md +28 -0
- package/skills/brave-search/scripts/content.mjs +53 -0
- package/skills/brave-search/scripts/search.mjs +79 -0
- package/skills/browser-use-agent-sdk/SKILL.md +90 -0
- package/skills/camsnap/SKILL.md +25 -0
- package/skills/clawdhub/SKILL.md +53 -0
- package/skills/coding-agent/SKILL.md +274 -0
- package/skills/comms/SKILL.md +249 -0
- package/skills/comms/docs/adapters.md +54 -0
- package/skills/comms/docs/setup.md +56 -0
- package/skills/comms/docs/troubleshooting.md +44 -0
- package/skills/comms/references/schema.md +49 -0
- package/skills/computer-use/SKILL.md +204 -0
- package/skills/computer-use/docs/open-interpreter.md +26 -0
- package/skills/computer-use/docs/peekaboo.md +26 -0
- package/skills/computer-use/docs/setup.md +47 -0
- package/skills/computer-use/docs/troubleshooting.md +33 -0
- package/skills/discord/SKILL.md +370 -0
- package/skills/eightctl/SKILL.md +29 -0
- package/skills/eve/SKILL.md +215 -0
- package/skills/eve/docs/dual-account.md +84 -0
- package/skills/eve/docs/intelligence.md +58 -0
- package/skills/eve/docs/setup.md +60 -0
- package/skills/eve/docs/troubleshooting.md +54 -0
- package/skills/eve/scripts/setup-dual-account.sh +125 -0
- package/skills/filesystem/SKILL.md +217 -0
- package/skills/food-order/SKILL.md +41 -0
- package/skills/gemini/SKILL.md +23 -0
- package/skills/gh/SKILL.md +22 -0
- package/skills/gh/docs/usage.md +41 -0
- package/skills/gifgrep/SKILL.md +47 -0
- package/skills/github/SKILL.md +26 -0
- package/skills/github/docs/setup.md +21 -0
- package/skills/github/docs/troubleshooting.md +24 -0
- package/skills/gog/SKILL.md +104 -0
- package/skills/gog/docs/portability.md +94 -0
- package/skills/gog/docs/setup.md +76 -0
- package/skills/gog/docs/troubleshooting.md +94 -0
- package/skills/gog/scripts/cdp/README.md +90 -0
- package/skills/gog/scripts/cdp/add_test_users.py +69 -0
- package/skills/gog/scripts/cdp/auth_add_accounts.py +209 -0
- package/skills/gog/scripts/cdp/auth_add_accounts_manual.py +206 -0
- package/skills/gog/scripts/cdp/create_oauth_client.py +165 -0
- package/skills/gog/scripts/cdp/launch_cdp_chrome.sh +58 -0
- package/skills/google-oauth/SKILL.md +94 -0
- package/skills/goplaces/SKILL.md +30 -0
- package/skills/imsg/SKILL.md +25 -0
- package/skills/json-render/SKILL.md +154 -0
- package/skills/json-render/assets/components/README.md +21 -0
- package/skills/json-render/assets/components/catalog.ts +78 -0
- package/skills/json-render/assets/components/registry.tsx +172 -0
- package/skills/json-render/assets/demo/App.css +397 -0
- package/skills/json-render/assets/demo/App.tsx +897 -0
- package/skills/json-render/assets/demo/README.md +22 -0
- package/skills/json-render/assets/demo/catalog.ts +78 -0
- package/skills/json-render/assets/demo/data/nexus-core.json +31 -0
- package/skills/json-render/assets/demo/index.css +27 -0
- package/skills/json-render/assets/demo/registry.tsx +150 -0
- package/skills/json-render/docs/nexus-state-demo.md +84 -0
- package/skills/json-render/docs/shadcn-preset.md +33 -0
- package/skills/json-render/scripts/create-vite-demo.sh +45 -0
- package/skills/json-render/scripts/llm-server/README.md +33 -0
- package/skills/json-render/scripts/llm-server/catalog.ts +78 -0
- package/skills/json-render/scripts/llm-server/package-lock.json +702 -0
- package/skills/json-render/scripts/llm-server/package.json +18 -0
- package/skills/json-render/scripts/llm-server/server.ts +285 -0
- package/skills/local-places/SERVER_README.md +101 -0
- package/skills/local-places/SKILL.md +91 -0
- package/skills/local-places/pyproject.toml +27 -0
- package/skills/local-places/src/local_places/__init__.py +2 -0
- package/skills/local-places/src/local_places/__pycache__/__init__.cpython-314.pyc +0 -0
- package/skills/local-places/src/local_places/__pycache__/google_places.cpython-314.pyc +0 -0
- package/skills/local-places/src/local_places/__pycache__/main.cpython-314.pyc +0 -0
- package/skills/local-places/src/local_places/__pycache__/schemas.cpython-314.pyc +0 -0
- package/skills/local-places/src/local_places/google_places.py +314 -0
- package/skills/local-places/src/local_places/main.py +65 -0
- package/skills/local-places/src/local_places/schemas.py +107 -0
- package/skills/mcporter/SKILL.md +38 -0
- package/skills/model-usage/SKILL.md +45 -0
- package/skills/model-usage/references/codexbar-cli.md +28 -0
- package/skills/model-usage/scripts/model_usage.py +310 -0
- package/skills/nano-banana-pro/SKILL.md +30 -0
- package/skills/nano-banana-pro/scripts/generate_image.py +169 -0
- package/skills/nano-pdf/SKILL.md +20 -0
- package/skills/nexus-cloud/SKILL.md +53 -0
- package/skills/nexus-cloud/docs/security.md +24 -0
- package/skills/nexus-cloud/docs/setup.md +51 -0
- package/skills/nexus-cloud/docs/troubleshooting.md +28 -0
- package/skills/notion/SKILL.md +156 -0
- package/skills/obsidian/SKILL.md +55 -0
- package/skills/onboarding/SKILL.md +515 -0
- package/skills/onboarding/docs/CAPABILITIES.md +444 -0
- package/skills/onboarding/docs/CAPABILITY_TAXONOMY.md +608 -0
- package/skills/onboarding/docs/CLI_GRAMMAR.md +797 -0
- package/skills/onboarding/docs/CLI_GRAMMAR_CREDENTIALS.md +632 -0
- package/skills/onboarding/docs/CLI_GRAMMAR_ONBOARDING.md +815 -0
- package/skills/onboarding/docs/CLI_GRAMMAR_SKILLS.md +449 -0
- package/skills/onboarding/docs/DOCUMENTATION_OVERVIEW.md +290 -0
- package/skills/onboarding/docs/ENTITY_MODEL.md +582 -0
- package/skills/onboarding/docs/GOAL_STATE_ARCHITECTURE.md +395 -0
- package/skills/onboarding/docs/NEXUS_SYSTEM_OVERVIEW.md +476 -0
- package/skills/onboarding/docs/SKILLS_HUB_SPEC.md +477 -0
- package/skills/onboarding/docs/SKILLS_SPECIFICATION.md +947 -0
- package/skills/onboarding/docs/SKILL_GATEWAY_DESIGN.md +702 -0
- package/skills/onboarding/docs/SKILL_GATEWAY_PRD.md +278 -0
- package/skills/onboarding/docs/SKILL_INVENTORY.md +266 -0
- package/skills/onboarding/docs/STATE_ARCHITECTURE.md +547 -0
- package/skills/onboarding/docs/TROUBLESHOOTING.md +363 -0
- package/skills/onboarding/docs/USER_JOURNEY.md +797 -0
- package/skills/onboarding/docs/WOW_MOMENTS.md +232 -0
- package/skills/onboarding/docs/agent-apple-id.md +289 -0
- package/skills/onboarding/docs/skill-deep-dives/1password.md +367 -0
- package/skills/onboarding/docs/skill-deep-dives/TEMPLATE.md +197 -0
- package/skills/onboarding/docs/skill-deep-dives/aix.md +498 -0
- package/skills/onboarding/docs/skill-deep-dives/bird.md +357 -0
- package/skills/onboarding/docs/skill-deep-dives/brave-search.md +601 -0
- package/skills/onboarding/docs/skill-deep-dives/comms.md +607 -0
- package/skills/onboarding/docs/skill-deep-dives/computer-use.md +599 -0
- package/skills/onboarding/docs/skill-deep-dives/cron-and-heartbeat.md +576 -0
- package/skills/onboarding/docs/skill-deep-dives/eve.md +711 -0
- package/skills/onboarding/docs/skill-deep-dives/github.md +333 -0
- package/skills/onboarding/docs/skill-deep-dives/gog.md +640 -0
- package/skills/onboarding/docs/skill-deep-dives/homebrew-prereqs.md +785 -0
- package/skills/onboarding/docs/skill-deep-dives/nexus-cloud.md +689 -0
- package/skills/onboarding/docs/skill-deep-dives/qmd.md +742 -0
- package/skills/onboarding/docs/skill-deep-dives/telegram.md +379 -0
- package/skills/onboarding/docs/skill-deep-dives/wacli.md +399 -0
- package/skills/onboarding/docs/skill-deep-dives/weather.md +513 -0
- package/skills/onboarding/scripts/ralph/prd.json +215 -0
- package/skills/onboarding/scripts/ralph/progress.txt +99 -0
- package/skills/onboarding/scripts/ralph/prompt.md +87 -0
- package/skills/onboarding/scripts/ralph/ralph.log +84 -0
- package/skills/onboarding/scripts/ralph/ralph.sh +45 -0
- package/skills/onboarding/scripts/setup-cursor-skills.sh +40 -0
- package/skills/openai-image-gen/SKILL.md +31 -0
- package/skills/openai-image-gen/scripts/gen.py +173 -0
- package/skills/openai-whisper/SKILL.md +19 -0
- package/skills/openai-whisper-api/SKILL.md +43 -0
- package/skills/openai-whisper-api/scripts/transcribe.sh +85 -0
- package/skills/openhue/SKILL.md +30 -0
- package/skills/oracle/SKILL.md +105 -0
- package/skills/ordercli/SKILL.md +47 -0
- package/skills/peekaboo/SKILL.md +153 -0
- package/skills/qmd/SKILL.md +32 -0
- package/skills/qmd/docs/mcp.md +30 -0
- package/skills/qmd/docs/ollama.md +42 -0
- package/skills/qmd/docs/setup.md +44 -0
- package/skills/sag/SKILL.md +62 -0
- package/skills/skill-cli-template/SKILL.md +109 -0
- package/skills/skill-creator/scripts/__pycache__/quick_validate.cpython-311.pyc +0 -0
- package/skills/slack/SKILL.md +144 -0
- package/skills/songsee/SKILL.md +29 -0
- package/skills/sonoscli/SKILL.md +26 -0
- package/skills/spotify-player/SKILL.md +34 -0
- package/skills/summarize/SKILL.md +49 -0
- package/skills/telegram/SKILL.md +20 -0
- package/skills/telegram/docs/pairing.md +30 -0
- package/skills/telegram/docs/setup.md +41 -0
- package/skills/telegram/docs/webhook.md +17 -0
- package/skills/things-mac/SKILL.md +61 -0
- package/skills/tmux/SKILL.md +121 -0
- package/skills/tmux/scripts/find-sessions.sh +112 -0
- package/skills/tmux/scripts/wait-for-text.sh +83 -0
- package/skills/trello/SKILL.md +84 -0
- package/skills/upstream-sync/SKILL.md +151 -0
- package/skills/upstream-sync/scripts/auto-port.sh +227 -0
- package/skills/upstream-sync/scripts/check-all.sh +88 -0
- package/skills/upstream-sync/scripts/check-nexus.sh +146 -0
- package/skills/upstream-sync/scripts/check-pi-ai.sh +129 -0
- package/skills/video-frames/SKILL.md +29 -0
- package/skills/video-frames/scripts/frame.sh +81 -0
- package/skills/wacli/SKILL.md +48 -0
- package/skills/wacli/docs/auth.md +21 -0
- package/skills/wacli/docs/backup.md +9 -0
- package/skills/wacli/docs/troubleshooting.md +21 -0
- package/skills/weather/SKILL.md +53 -0
- package/skills/weather/docs/usage.md +40 -0
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import { apiThrottler } from "@grammyjs/transformer-throttler";
|
|
4
|
+
import { Bot, InputFile, webhookCallback } from "grammy";
|
|
5
|
+
import { chunkMarkdownText, resolveTextChunkLimit, } from "../auto-reply/chunk.js";
|
|
6
|
+
import { hasControlCommand } from "../auto-reply/command-detection.js";
|
|
7
|
+
import { formatAgentEnvelope } from "../auto-reply/envelope.js";
|
|
8
|
+
import { dispatchReplyFromConfig } from "../auto-reply/reply/dispatch-from-config.js";
|
|
9
|
+
import { buildMentionRegexes, matchesMentionPatterns, } from "../auto-reply/reply/mentions.js";
|
|
10
|
+
import { createReplyDispatcher } from "../auto-reply/reply/reply-dispatcher.js";
|
|
11
|
+
import { loadConfig } from "../config/config.js";
|
|
12
|
+
import { resolveProviderGroupPolicy, resolveProviderGroupRequireMention, } from "../config/group-policy.js";
|
|
13
|
+
import { resolveStorePath, updateLastRoute } from "../config/sessions.js";
|
|
14
|
+
import { danger, logVerbose, shouldLogVerbose } from "../globals.js";
|
|
15
|
+
import { formatErrorMessage } from "../infra/errors.js";
|
|
16
|
+
import { getChildLogger } from "../logging.js";
|
|
17
|
+
import { mediaKindFromMime } from "../media/constants.js";
|
|
18
|
+
import { detectMime, isGifMedia } from "../media/mime.js";
|
|
19
|
+
import { saveMediaBuffer } from "../media/store.js";
|
|
20
|
+
import { formatLocationText, toLocationContext, } from "../providers/location.js";
|
|
21
|
+
import { resolveAgentRoute } from "../routing/resolve-route.js";
|
|
22
|
+
import { loadWebMedia } from "../web/media.js";
|
|
23
|
+
import { readTelegramAllowFromStore, upsertTelegramPairingRequest, } from "./pairing-store.js";
|
|
24
|
+
const PARSE_ERR_RE = /can't parse entities|parse entities|find end of the entity/i;
|
|
25
|
+
// Media group aggregation - Telegram sends multi-image messages as separate updates
|
|
26
|
+
// with a shared media_group_id. We buffer them and process as a single message after a short delay.
|
|
27
|
+
const MEDIA_GROUP_TIMEOUT_MS = 500;
|
|
28
|
+
export function createTelegramBot(opts) {
|
|
29
|
+
const runtime = opts.runtime ?? {
|
|
30
|
+
log: console.log,
|
|
31
|
+
error: console.error,
|
|
32
|
+
exit: (code) => {
|
|
33
|
+
throw new Error(`exit ${code}`);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
const client = opts.proxyFetch
|
|
37
|
+
? { fetch: opts.proxyFetch }
|
|
38
|
+
: undefined;
|
|
39
|
+
const bot = new Bot(opts.token, { client });
|
|
40
|
+
bot.api.config.use(apiThrottler());
|
|
41
|
+
const mediaGroupBuffer = new Map();
|
|
42
|
+
const cfg = loadConfig();
|
|
43
|
+
const textLimit = resolveTextChunkLimit(cfg, "telegram");
|
|
44
|
+
const dmPolicy = cfg.telegram?.dmPolicy ?? "pairing";
|
|
45
|
+
const allowFrom = opts.allowFrom ?? cfg.telegram?.allowFrom;
|
|
46
|
+
const groupAllowFrom = opts.groupAllowFrom ??
|
|
47
|
+
cfg.telegram?.groupAllowFrom ??
|
|
48
|
+
(cfg.telegram?.allowFrom && cfg.telegram.allowFrom.length > 0
|
|
49
|
+
? cfg.telegram.allowFrom
|
|
50
|
+
: undefined) ??
|
|
51
|
+
(opts.allowFrom && opts.allowFrom.length > 0 ? opts.allowFrom : undefined);
|
|
52
|
+
const normalizeAllowFrom = (list) => {
|
|
53
|
+
const entries = (list ?? [])
|
|
54
|
+
.map((value) => String(value).trim())
|
|
55
|
+
.filter(Boolean);
|
|
56
|
+
const hasWildcard = entries.includes("*");
|
|
57
|
+
const normalized = entries
|
|
58
|
+
.filter((value) => value !== "*")
|
|
59
|
+
.map((value) => value.replace(/^(telegram|tg):/i, ""));
|
|
60
|
+
const normalizedLower = normalized.map((value) => value.toLowerCase());
|
|
61
|
+
return {
|
|
62
|
+
entries: normalized,
|
|
63
|
+
entriesLower: normalizedLower,
|
|
64
|
+
hasWildcard,
|
|
65
|
+
hasEntries: entries.length > 0,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
const isSenderAllowed = (params) => {
|
|
69
|
+
const { allow, senderId, senderUsername } = params;
|
|
70
|
+
if (!allow.hasEntries)
|
|
71
|
+
return true;
|
|
72
|
+
if (allow.hasWildcard)
|
|
73
|
+
return true;
|
|
74
|
+
if (senderId && allow.entries.includes(senderId))
|
|
75
|
+
return true;
|
|
76
|
+
const username = senderUsername?.toLowerCase();
|
|
77
|
+
if (!username)
|
|
78
|
+
return false;
|
|
79
|
+
return allow.entriesLower.some((entry) => entry === username || entry === `@${username}`);
|
|
80
|
+
};
|
|
81
|
+
const replyToMode = opts.replyToMode ?? cfg.telegram?.replyToMode ?? "off";
|
|
82
|
+
const ackReaction = (cfg.messages?.ackReaction ?? "").trim();
|
|
83
|
+
const ackReactionScope = cfg.messages?.ackReactionScope ?? "group-mentions";
|
|
84
|
+
const mediaMaxBytes = (opts.mediaMaxMb ?? cfg.telegram?.mediaMaxMb ?? 5) * 1024 * 1024;
|
|
85
|
+
const logger = getChildLogger({ module: "telegram-auto-reply" });
|
|
86
|
+
const mentionRegexes = buildMentionRegexes(cfg);
|
|
87
|
+
const resolveGroupPolicy = (chatId) => resolveProviderGroupPolicy({
|
|
88
|
+
cfg,
|
|
89
|
+
provider: "telegram",
|
|
90
|
+
groupId: String(chatId),
|
|
91
|
+
});
|
|
92
|
+
const resolveGroupRequireMention = (chatId) => resolveProviderGroupRequireMention({
|
|
93
|
+
cfg,
|
|
94
|
+
provider: "telegram",
|
|
95
|
+
groupId: String(chatId),
|
|
96
|
+
requireMentionOverride: opts.requireMention,
|
|
97
|
+
overrideOrder: "after-config",
|
|
98
|
+
});
|
|
99
|
+
const processMessage = async (primaryCtx, allMedia, storeAllowFrom) => {
|
|
100
|
+
const msg = primaryCtx.message;
|
|
101
|
+
const chatId = msg.chat.id;
|
|
102
|
+
const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup";
|
|
103
|
+
const effectiveDmAllow = normalizeAllowFrom([
|
|
104
|
+
...(allowFrom ?? []),
|
|
105
|
+
...storeAllowFrom,
|
|
106
|
+
]);
|
|
107
|
+
const effectiveGroupAllow = normalizeAllowFrom([
|
|
108
|
+
...(groupAllowFrom ?? []),
|
|
109
|
+
...storeAllowFrom,
|
|
110
|
+
]);
|
|
111
|
+
const sendTyping = async () => {
|
|
112
|
+
try {
|
|
113
|
+
await bot.api.sendChatAction(chatId, "typing");
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
logVerbose(`telegram typing cue failed for chat ${chatId}: ${String(err)}`);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
// DM access control (secure defaults): "pairing" (default) / "allowlist" / "open" / "disabled"
|
|
120
|
+
if (!isGroup) {
|
|
121
|
+
if (dmPolicy === "disabled")
|
|
122
|
+
return;
|
|
123
|
+
if (dmPolicy !== "open") {
|
|
124
|
+
const candidate = String(chatId);
|
|
125
|
+
const senderUsername = msg.from?.username ?? "";
|
|
126
|
+
const allowed = effectiveDmAllow.hasWildcard ||
|
|
127
|
+
(effectiveDmAllow.hasEntries &&
|
|
128
|
+
isSenderAllowed({
|
|
129
|
+
allow: effectiveDmAllow,
|
|
130
|
+
senderId: candidate,
|
|
131
|
+
senderUsername,
|
|
132
|
+
}));
|
|
133
|
+
if (!allowed) {
|
|
134
|
+
if (dmPolicy === "pairing") {
|
|
135
|
+
try {
|
|
136
|
+
const from = msg.from;
|
|
137
|
+
const { code } = await upsertTelegramPairingRequest({
|
|
138
|
+
chatId: candidate,
|
|
139
|
+
username: from?.username,
|
|
140
|
+
firstName: from?.first_name,
|
|
141
|
+
lastName: from?.last_name,
|
|
142
|
+
});
|
|
143
|
+
logger.info({
|
|
144
|
+
chatId: candidate,
|
|
145
|
+
username: from?.username,
|
|
146
|
+
firstName: from?.first_name,
|
|
147
|
+
lastName: from?.last_name,
|
|
148
|
+
code,
|
|
149
|
+
}, "telegram pairing request");
|
|
150
|
+
await bot.api.sendMessage(chatId, [
|
|
151
|
+
"Nexus: access not configured.",
|
|
152
|
+
"",
|
|
153
|
+
`Pairing code: ${code}`,
|
|
154
|
+
"",
|
|
155
|
+
"Ask the bot owner to approve with:",
|
|
156
|
+
"nexus telegram pairing approve <code>",
|
|
157
|
+
].join("\n"));
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
logVerbose(`telegram pairing reply failed for chat ${chatId}: ${String(err)}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
logVerbose(`Blocked unauthorized telegram sender ${candidate} (dmPolicy=${dmPolicy})`);
|
|
165
|
+
}
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const botUsername = primaryCtx.me?.username?.toLowerCase();
|
|
171
|
+
const senderId = msg.from?.id ? String(msg.from.id) : "";
|
|
172
|
+
const senderUsername = msg.from?.username ?? "";
|
|
173
|
+
const commandAuthorized = isSenderAllowed({
|
|
174
|
+
allow: isGroup ? effectiveGroupAllow : effectiveDmAllow,
|
|
175
|
+
senderId,
|
|
176
|
+
senderUsername,
|
|
177
|
+
});
|
|
178
|
+
const wasMentioned = (Boolean(botUsername) && hasBotMention(msg, botUsername)) ||
|
|
179
|
+
matchesMentionPatterns(msg.text ?? msg.caption ?? "", mentionRegexes);
|
|
180
|
+
const hasAnyMention = (msg.entities ?? msg.caption_entities ?? []).some((ent) => ent.type === "mention");
|
|
181
|
+
const requireMention = resolveGroupRequireMention(chatId);
|
|
182
|
+
const shouldBypassMention = isGroup &&
|
|
183
|
+
requireMention &&
|
|
184
|
+
!wasMentioned &&
|
|
185
|
+
!hasAnyMention &&
|
|
186
|
+
commandAuthorized &&
|
|
187
|
+
hasControlCommand(msg.text ?? msg.caption ?? "");
|
|
188
|
+
const canDetectMention = Boolean(botUsername) || mentionRegexes.length > 0;
|
|
189
|
+
if (isGroup && requireMention && canDetectMention) {
|
|
190
|
+
if (!wasMentioned && !shouldBypassMention) {
|
|
191
|
+
logger.info({ chatId, reason: "no-mention" }, "skipping group message");
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// ACK reactions
|
|
196
|
+
const shouldAckReaction = () => {
|
|
197
|
+
if (!ackReaction)
|
|
198
|
+
return false;
|
|
199
|
+
if (ackReactionScope === "all")
|
|
200
|
+
return true;
|
|
201
|
+
if (ackReactionScope === "direct")
|
|
202
|
+
return !isGroup;
|
|
203
|
+
if (ackReactionScope === "group-all")
|
|
204
|
+
return isGroup;
|
|
205
|
+
if (ackReactionScope === "group-mentions") {
|
|
206
|
+
if (!isGroup)
|
|
207
|
+
return false;
|
|
208
|
+
if (!requireMention)
|
|
209
|
+
return false;
|
|
210
|
+
if (!canDetectMention)
|
|
211
|
+
return false;
|
|
212
|
+
return wasMentioned || shouldBypassMention;
|
|
213
|
+
}
|
|
214
|
+
return false;
|
|
215
|
+
};
|
|
216
|
+
if (shouldAckReaction() && msg.message_id) {
|
|
217
|
+
const api = bot.api;
|
|
218
|
+
if (typeof api.setMessageReaction === "function") {
|
|
219
|
+
api
|
|
220
|
+
.setMessageReaction(chatId, msg.message_id, [
|
|
221
|
+
{ type: "emoji", emoji: ackReaction },
|
|
222
|
+
])
|
|
223
|
+
.catch((err) => {
|
|
224
|
+
logVerbose(`telegram react failed for chat ${chatId}: ${String(err)}`);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
let placeholder = "";
|
|
229
|
+
if (msg.photo)
|
|
230
|
+
placeholder = "<media:image>";
|
|
231
|
+
else if (msg.video)
|
|
232
|
+
placeholder = "<media:video>";
|
|
233
|
+
else if (msg.audio || msg.voice)
|
|
234
|
+
placeholder = "<media:audio>";
|
|
235
|
+
else if (msg.document)
|
|
236
|
+
placeholder = "<media:document>";
|
|
237
|
+
const replyTarget = describeReplyTarget(msg);
|
|
238
|
+
const locationData = extractTelegramLocation(msg);
|
|
239
|
+
const locationText = locationData
|
|
240
|
+
? formatLocationText(locationData)
|
|
241
|
+
: undefined;
|
|
242
|
+
const rawText = (msg.text ?? msg.caption ?? "").trim();
|
|
243
|
+
let rawBody = [rawText, locationText].filter(Boolean).join("\n").trim();
|
|
244
|
+
if (!rawBody)
|
|
245
|
+
rawBody = placeholder;
|
|
246
|
+
if (!rawBody && allMedia.length === 0)
|
|
247
|
+
return;
|
|
248
|
+
let bodyText = rawBody;
|
|
249
|
+
if (!bodyText && allMedia.length > 0) {
|
|
250
|
+
bodyText = `<media:image>${allMedia.length > 1 ? ` (${allMedia.length} images)` : ""}`;
|
|
251
|
+
}
|
|
252
|
+
const replySuffix = replyTarget
|
|
253
|
+
? `\n\n[Replying to ${replyTarget.sender}${replyTarget.id ? ` id:${replyTarget.id}` : ""}]\n${replyTarget.body}\n[/Replying]`
|
|
254
|
+
: "";
|
|
255
|
+
const body = formatAgentEnvelope({
|
|
256
|
+
provider: "Telegram",
|
|
257
|
+
from: isGroup
|
|
258
|
+
? buildGroupLabel(msg, chatId)
|
|
259
|
+
: buildSenderLabel(msg, chatId),
|
|
260
|
+
timestamp: msg.date ? msg.date * 1000 : undefined,
|
|
261
|
+
body: `${bodyText}${replySuffix}`,
|
|
262
|
+
});
|
|
263
|
+
const route = resolveAgentRoute({
|
|
264
|
+
cfg,
|
|
265
|
+
provider: "telegram",
|
|
266
|
+
peer: {
|
|
267
|
+
kind: isGroup ? "group" : "dm",
|
|
268
|
+
id: String(chatId),
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
const ctxPayload = {
|
|
272
|
+
Body: body,
|
|
273
|
+
From: isGroup ? `group:${chatId}` : `telegram:${chatId}`,
|
|
274
|
+
To: `telegram:${chatId}`,
|
|
275
|
+
SessionKey: route.sessionKey,
|
|
276
|
+
AccountId: route.accountId,
|
|
277
|
+
ChatType: isGroup ? "group" : "direct",
|
|
278
|
+
GroupSubject: isGroup ? (msg.chat.title ?? undefined) : undefined,
|
|
279
|
+
SenderName: buildSenderName(msg),
|
|
280
|
+
SenderId: senderId || undefined,
|
|
281
|
+
SenderUsername: senderUsername || undefined,
|
|
282
|
+
Provider: "telegram",
|
|
283
|
+
MessageSid: String(msg.message_id),
|
|
284
|
+
ReplyToId: replyTarget?.id,
|
|
285
|
+
ReplyToBody: replyTarget?.body,
|
|
286
|
+
ReplyToSender: replyTarget?.sender,
|
|
287
|
+
Timestamp: msg.date ? msg.date * 1000 : undefined,
|
|
288
|
+
WasMentioned: isGroup ? wasMentioned : undefined,
|
|
289
|
+
MediaPath: allMedia[0]?.path,
|
|
290
|
+
MediaType: allMedia[0]?.contentType,
|
|
291
|
+
MediaUrl: allMedia[0]?.path,
|
|
292
|
+
MediaPaths: allMedia.length > 0 ? allMedia.map((m) => m.path) : undefined,
|
|
293
|
+
MediaUrls: allMedia.length > 0 ? allMedia.map((m) => m.path) : undefined,
|
|
294
|
+
MediaTypes: allMedia.length > 0
|
|
295
|
+
? allMedia.map((m) => m.contentType).filter(Boolean)
|
|
296
|
+
: undefined,
|
|
297
|
+
...(locationData ? toLocationContext(locationData) : undefined),
|
|
298
|
+
CommandAuthorized: commandAuthorized,
|
|
299
|
+
};
|
|
300
|
+
if (replyTarget && shouldLogVerbose()) {
|
|
301
|
+
const preview = replyTarget.body.replace(/\s+/g, " ").slice(0, 120);
|
|
302
|
+
logVerbose(`telegram reply-context: replyToId=${replyTarget.id} replyToSender=${replyTarget.sender} replyToBody="${preview}"`);
|
|
303
|
+
}
|
|
304
|
+
if (!isGroup) {
|
|
305
|
+
const sessionCfg = cfg.session;
|
|
306
|
+
const storePath = resolveStorePath(sessionCfg?.store, {
|
|
307
|
+
agentId: route.agentId,
|
|
308
|
+
});
|
|
309
|
+
await updateLastRoute({
|
|
310
|
+
storePath,
|
|
311
|
+
sessionKey: route.mainSessionKey,
|
|
312
|
+
provider: "telegram",
|
|
313
|
+
to: String(chatId),
|
|
314
|
+
accountId: route.accountId,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
if (shouldLogVerbose()) {
|
|
318
|
+
const preview = body.slice(0, 200).replace(/\n/g, "\\n");
|
|
319
|
+
const mediaInfo = allMedia.length > 1 ? ` mediaCount=${allMedia.length}` : "";
|
|
320
|
+
logVerbose(`telegram inbound: chatId=${chatId} from=${ctxPayload.From} len=${body.length}${mediaInfo} preview="${preview}"`);
|
|
321
|
+
}
|
|
322
|
+
let typingController;
|
|
323
|
+
const dispatcher = createReplyDispatcher({
|
|
324
|
+
responsePrefix: cfg.messages?.responsePrefix,
|
|
325
|
+
deliver: async (payload) => {
|
|
326
|
+
await deliverReplies({
|
|
327
|
+
replies: [payload],
|
|
328
|
+
chatId: String(chatId),
|
|
329
|
+
token: opts.token,
|
|
330
|
+
runtime,
|
|
331
|
+
bot,
|
|
332
|
+
replyToMode,
|
|
333
|
+
textLimit,
|
|
334
|
+
});
|
|
335
|
+
},
|
|
336
|
+
onIdle: () => {
|
|
337
|
+
typingController?.markDispatchIdle();
|
|
338
|
+
},
|
|
339
|
+
onError: (err, info) => {
|
|
340
|
+
runtime.error?.(danger(`telegram ${info.kind} reply failed: ${String(err)}`));
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
const { queuedFinal } = await dispatchReplyFromConfig({
|
|
344
|
+
ctx: ctxPayload,
|
|
345
|
+
cfg,
|
|
346
|
+
dispatcher,
|
|
347
|
+
replyOptions: {
|
|
348
|
+
onReplyStart: sendTyping,
|
|
349
|
+
onTypingController: (typing) => {
|
|
350
|
+
typingController = typing;
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
typingController?.markDispatchIdle();
|
|
355
|
+
if (!queuedFinal)
|
|
356
|
+
return;
|
|
357
|
+
};
|
|
358
|
+
bot.on("message", async (ctx) => {
|
|
359
|
+
try {
|
|
360
|
+
const msg = ctx.message;
|
|
361
|
+
if (!msg)
|
|
362
|
+
return;
|
|
363
|
+
const chatId = msg.chat.id;
|
|
364
|
+
const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup";
|
|
365
|
+
const storeAllowFrom = await readTelegramAllowFromStore().catch(() => []);
|
|
366
|
+
if (isGroup) {
|
|
367
|
+
// Group policy filtering: controls how group messages are handled
|
|
368
|
+
// - "open" (default): groups bypass allowFrom, only mention-gating applies
|
|
369
|
+
// - "disabled": block all group messages entirely
|
|
370
|
+
// - "allowlist": only allow group messages from senders in groupAllowFrom/allowFrom
|
|
371
|
+
const groupPolicy = cfg.telegram?.groupPolicy ?? "open";
|
|
372
|
+
if (groupPolicy === "disabled") {
|
|
373
|
+
logVerbose(`Blocked telegram group message (groupPolicy: disabled)`);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (groupPolicy === "allowlist") {
|
|
377
|
+
const effectiveGroupAllow = normalizeAllowFrom([
|
|
378
|
+
...(groupAllowFrom ?? []),
|
|
379
|
+
...storeAllowFrom,
|
|
380
|
+
]);
|
|
381
|
+
// For allowlist mode, the sender (msg.from.id) must be in allowFrom
|
|
382
|
+
const senderId = msg.from?.id;
|
|
383
|
+
if (senderId == null) {
|
|
384
|
+
logVerbose(`Blocked telegram group message (no sender ID, groupPolicy: allowlist)`);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (!effectiveGroupAllow.hasEntries) {
|
|
388
|
+
logVerbose("Blocked telegram group message (groupPolicy: allowlist, no groupAllowFrom)");
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const senderUsername = msg.from?.username ?? "";
|
|
392
|
+
if (!isSenderAllowed({
|
|
393
|
+
allow: effectiveGroupAllow,
|
|
394
|
+
senderId: String(senderId),
|
|
395
|
+
senderUsername,
|
|
396
|
+
})) {
|
|
397
|
+
logVerbose(`Blocked telegram group message from ${senderId} (groupPolicy: allowlist)`);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
// Group allowlist based on configured group IDs.
|
|
402
|
+
const groupAllowlist = resolveGroupPolicy(chatId);
|
|
403
|
+
if (groupAllowlist.allowlistEnabled && !groupAllowlist.allowed) {
|
|
404
|
+
logger.info({ chatId, title: msg.chat.title, reason: "not-allowed" }, "skipping group message");
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
// Media group handling - buffer multi-image messages
|
|
409
|
+
const mediaGroupId = msg.media_group_id;
|
|
410
|
+
if (mediaGroupId) {
|
|
411
|
+
const existing = mediaGroupBuffer.get(mediaGroupId);
|
|
412
|
+
if (existing) {
|
|
413
|
+
clearTimeout(existing.timer);
|
|
414
|
+
existing.messages.push({ msg, ctx });
|
|
415
|
+
existing.timer = setTimeout(async () => {
|
|
416
|
+
mediaGroupBuffer.delete(mediaGroupId);
|
|
417
|
+
await processMediaGroup(existing);
|
|
418
|
+
}, MEDIA_GROUP_TIMEOUT_MS);
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
const entry = {
|
|
422
|
+
messages: [{ msg, ctx }],
|
|
423
|
+
timer: setTimeout(async () => {
|
|
424
|
+
mediaGroupBuffer.delete(mediaGroupId);
|
|
425
|
+
await processMediaGroup(entry);
|
|
426
|
+
}, MEDIA_GROUP_TIMEOUT_MS),
|
|
427
|
+
};
|
|
428
|
+
mediaGroupBuffer.set(mediaGroupId, entry);
|
|
429
|
+
}
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
let media = null;
|
|
433
|
+
try {
|
|
434
|
+
media = await resolveMedia(ctx, mediaMaxBytes, opts.token, opts.proxyFetch);
|
|
435
|
+
}
|
|
436
|
+
catch (mediaErr) {
|
|
437
|
+
const errMsg = String(mediaErr);
|
|
438
|
+
if (errMsg.includes("exceeds") && errMsg.includes("MB limit")) {
|
|
439
|
+
const limitMb = Math.round(mediaMaxBytes / (1024 * 1024));
|
|
440
|
+
await bot.api
|
|
441
|
+
.sendMessage(chatId, `⚠️ File too large. Maximum size is ${limitMb}MB.`, { reply_to_message_id: msg.message_id })
|
|
442
|
+
.catch(() => { });
|
|
443
|
+
logger.warn({ chatId, error: errMsg }, "media exceeds size limit");
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
throw mediaErr;
|
|
447
|
+
}
|
|
448
|
+
const allMedia = media
|
|
449
|
+
? [{ path: media.path, contentType: media.contentType }]
|
|
450
|
+
: [];
|
|
451
|
+
await processMessage(ctx, allMedia, storeAllowFrom);
|
|
452
|
+
}
|
|
453
|
+
catch (err) {
|
|
454
|
+
runtime.error?.(danger(`handler failed: ${String(err)}`));
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
const processMediaGroup = async (entry) => {
|
|
458
|
+
try {
|
|
459
|
+
entry.messages.sort((a, b) => a.msg.message_id - b.msg.message_id);
|
|
460
|
+
const captionMsg = entry.messages.find((m) => m.msg.caption || m.msg.text);
|
|
461
|
+
const primaryEntry = captionMsg ?? entry.messages[0];
|
|
462
|
+
const allMedia = [];
|
|
463
|
+
for (const { ctx } of entry.messages) {
|
|
464
|
+
const media = await resolveMedia(ctx, mediaMaxBytes, opts.token, opts.proxyFetch);
|
|
465
|
+
if (media) {
|
|
466
|
+
allMedia.push({ path: media.path, contentType: media.contentType });
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const storeAllowFrom = await readTelegramAllowFromStore().catch(() => []);
|
|
470
|
+
await processMessage(primaryEntry.ctx, allMedia, storeAllowFrom);
|
|
471
|
+
}
|
|
472
|
+
catch (err) {
|
|
473
|
+
runtime.error?.(danger(`media group handler failed: ${String(err)}`));
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
return bot;
|
|
477
|
+
}
|
|
478
|
+
export function createTelegramWebhookCallback(bot, path = "/telegram-webhook") {
|
|
479
|
+
return { path, handler: webhookCallback(bot, "http") };
|
|
480
|
+
}
|
|
481
|
+
async function deliverReplies(params) {
|
|
482
|
+
const { replies, chatId, runtime, bot, replyToMode, textLimit } = params;
|
|
483
|
+
let hasReplied = false;
|
|
484
|
+
for (const reply of replies) {
|
|
485
|
+
if (!reply?.text && !reply?.mediaUrl && !(reply?.mediaUrls?.length ?? 0)) {
|
|
486
|
+
runtime.error?.(danger("reply missing text/media"));
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
const replyToId = replyToMode === "off"
|
|
490
|
+
? undefined
|
|
491
|
+
: resolveTelegramReplyId(reply.replyToId);
|
|
492
|
+
const mediaList = reply.mediaUrls?.length
|
|
493
|
+
? reply.mediaUrls
|
|
494
|
+
: reply.mediaUrl
|
|
495
|
+
? [reply.mediaUrl]
|
|
496
|
+
: [];
|
|
497
|
+
if (mediaList.length === 0) {
|
|
498
|
+
for (const chunk of chunkMarkdownText(reply.text || "", textLimit)) {
|
|
499
|
+
await sendTelegramText(bot, chatId, chunk, runtime, {
|
|
500
|
+
replyToMessageId: replyToId && (replyToMode === "all" || !hasReplied)
|
|
501
|
+
? replyToId
|
|
502
|
+
: undefined,
|
|
503
|
+
});
|
|
504
|
+
if (replyToId && !hasReplied) {
|
|
505
|
+
hasReplied = true;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
// media with optional caption on first item
|
|
511
|
+
let first = true;
|
|
512
|
+
for (const mediaUrl of mediaList) {
|
|
513
|
+
const media = await loadWebMedia(mediaUrl);
|
|
514
|
+
const kind = mediaKindFromMime(media.contentType ?? undefined);
|
|
515
|
+
const isGif = isGifMedia({
|
|
516
|
+
contentType: media.contentType,
|
|
517
|
+
fileName: media.fileName,
|
|
518
|
+
});
|
|
519
|
+
const fileName = media.fileName ?? (isGif ? "animation.gif" : "file");
|
|
520
|
+
const file = new InputFile(media.buffer, fileName);
|
|
521
|
+
const caption = first ? (reply.text ?? undefined) : undefined;
|
|
522
|
+
first = false;
|
|
523
|
+
const replyToMessageId = replyToId && (replyToMode === "all" || !hasReplied)
|
|
524
|
+
? replyToId
|
|
525
|
+
: undefined;
|
|
526
|
+
if (isGif) {
|
|
527
|
+
await bot.api.sendAnimation(chatId, file, {
|
|
528
|
+
caption,
|
|
529
|
+
reply_to_message_id: replyToMessageId,
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
else if (kind === "image") {
|
|
533
|
+
await bot.api.sendPhoto(chatId, file, {
|
|
534
|
+
caption,
|
|
535
|
+
reply_to_message_id: replyToMessageId,
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
else if (kind === "video") {
|
|
539
|
+
await bot.api.sendVideo(chatId, file, {
|
|
540
|
+
caption,
|
|
541
|
+
reply_to_message_id: replyToMessageId,
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
else if (kind === "audio") {
|
|
545
|
+
await bot.api.sendAudio(chatId, file, {
|
|
546
|
+
caption,
|
|
547
|
+
reply_to_message_id: replyToMessageId,
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
else {
|
|
551
|
+
await bot.api.sendDocument(chatId, file, {
|
|
552
|
+
caption,
|
|
553
|
+
reply_to_message_id: replyToMessageId,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
if (replyToId && !hasReplied) {
|
|
557
|
+
hasReplied = true;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
function buildSenderName(msg) {
|
|
563
|
+
const name = [msg.from?.first_name, msg.from?.last_name]
|
|
564
|
+
.filter(Boolean)
|
|
565
|
+
.join(" ")
|
|
566
|
+
.trim() || msg.from?.username;
|
|
567
|
+
return name || undefined;
|
|
568
|
+
}
|
|
569
|
+
function buildSenderLabel(msg, chatId) {
|
|
570
|
+
const name = buildSenderName(msg);
|
|
571
|
+
const username = msg.from?.username ? `@${msg.from.username}` : undefined;
|
|
572
|
+
let label = name;
|
|
573
|
+
if (name && username) {
|
|
574
|
+
label = `${name} (${username})`;
|
|
575
|
+
}
|
|
576
|
+
else if (!name && username) {
|
|
577
|
+
label = username;
|
|
578
|
+
}
|
|
579
|
+
const idPart = `id:${chatId}`;
|
|
580
|
+
return label ? `${label} ${idPart}` : idPart;
|
|
581
|
+
}
|
|
582
|
+
function buildGroupLabel(msg, chatId) {
|
|
583
|
+
const title = msg.chat?.title;
|
|
584
|
+
if (title)
|
|
585
|
+
return `${title} id:${chatId}`;
|
|
586
|
+
return `group:${chatId}`;
|
|
587
|
+
}
|
|
588
|
+
function hasBotMention(msg, botUsername) {
|
|
589
|
+
const text = (msg.text ?? msg.caption ?? "").toLowerCase();
|
|
590
|
+
if (text.includes(`@${botUsername}`))
|
|
591
|
+
return true;
|
|
592
|
+
const entities = msg.entities ?? msg.caption_entities ?? [];
|
|
593
|
+
for (const ent of entities) {
|
|
594
|
+
if (ent.type !== "mention")
|
|
595
|
+
continue;
|
|
596
|
+
const slice = (msg.text ?? msg.caption ?? "").slice(ent.offset, ent.offset + ent.length);
|
|
597
|
+
if (slice.toLowerCase() === `@${botUsername}`)
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
function resolveTelegramReplyId(raw) {
|
|
603
|
+
if (!raw)
|
|
604
|
+
return undefined;
|
|
605
|
+
const parsed = Number(raw);
|
|
606
|
+
if (!Number.isFinite(parsed))
|
|
607
|
+
return undefined;
|
|
608
|
+
return parsed;
|
|
609
|
+
}
|
|
610
|
+
async function resolveMedia(ctx, maxBytes, token, proxyFetch) {
|
|
611
|
+
const msg = ctx.message;
|
|
612
|
+
const m = msg.photo?.[msg.photo.length - 1] ??
|
|
613
|
+
msg.video ??
|
|
614
|
+
msg.document ??
|
|
615
|
+
msg.audio ??
|
|
616
|
+
msg.voice;
|
|
617
|
+
if (!m?.file_id)
|
|
618
|
+
return null;
|
|
619
|
+
const file = await ctx.getFile();
|
|
620
|
+
if (!file.file_path) {
|
|
621
|
+
throw new Error("Telegram getFile returned no file_path");
|
|
622
|
+
}
|
|
623
|
+
const fetchImpl = proxyFetch ?? globalThis.fetch;
|
|
624
|
+
if (!fetchImpl) {
|
|
625
|
+
throw new Error("fetch is not available; set telegram.proxy in config");
|
|
626
|
+
}
|
|
627
|
+
const url = `https://api.telegram.org/file/bot${token}/${file.file_path}`;
|
|
628
|
+
const res = await fetchImpl(url);
|
|
629
|
+
if (!res.ok) {
|
|
630
|
+
throw new Error(`Failed to download telegram file: HTTP ${res.status} ${res.statusText}`);
|
|
631
|
+
}
|
|
632
|
+
const data = Buffer.from(await res.arrayBuffer());
|
|
633
|
+
const mime = await detectMime({
|
|
634
|
+
buffer: data,
|
|
635
|
+
headerMime: res.headers.get("content-type"),
|
|
636
|
+
filePath: file.file_path,
|
|
637
|
+
});
|
|
638
|
+
const saved = await saveMediaBuffer(data, mime, "inbound", maxBytes);
|
|
639
|
+
let placeholder = "<media:document>";
|
|
640
|
+
if (msg.photo)
|
|
641
|
+
placeholder = "<media:image>";
|
|
642
|
+
else if (msg.video)
|
|
643
|
+
placeholder = "<media:video>";
|
|
644
|
+
else if (msg.audio || msg.voice)
|
|
645
|
+
placeholder = "<media:audio>";
|
|
646
|
+
return { path: saved.path, contentType: saved.contentType, placeholder };
|
|
647
|
+
}
|
|
648
|
+
async function sendTelegramText(bot, chatId, text, runtime, opts) {
|
|
649
|
+
try {
|
|
650
|
+
const res = await bot.api.sendMessage(chatId, text, {
|
|
651
|
+
parse_mode: "Markdown",
|
|
652
|
+
reply_to_message_id: opts?.replyToMessageId,
|
|
653
|
+
});
|
|
654
|
+
return res.message_id;
|
|
655
|
+
}
|
|
656
|
+
catch (err) {
|
|
657
|
+
const errText = formatErrorMessage(err);
|
|
658
|
+
if (PARSE_ERR_RE.test(errText)) {
|
|
659
|
+
runtime.log?.(`telegram markdown parse failed; retrying without formatting: ${errText}`);
|
|
660
|
+
const res = await bot.api.sendMessage(chatId, text, {
|
|
661
|
+
reply_to_message_id: opts?.replyToMessageId,
|
|
662
|
+
});
|
|
663
|
+
return res.message_id;
|
|
664
|
+
}
|
|
665
|
+
throw err;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
function describeReplyTarget(msg) {
|
|
669
|
+
const reply = msg.reply_to_message;
|
|
670
|
+
if (!reply)
|
|
671
|
+
return null;
|
|
672
|
+
const replyBody = (reply.text ?? reply.caption ?? "").trim();
|
|
673
|
+
let body = replyBody;
|
|
674
|
+
if (!body) {
|
|
675
|
+
if (reply.photo)
|
|
676
|
+
body = "<media:image>";
|
|
677
|
+
else if (reply.video)
|
|
678
|
+
body = "<media:video>";
|
|
679
|
+
else if (reply.audio || reply.voice)
|
|
680
|
+
body = "<media:audio>";
|
|
681
|
+
else if (reply.document)
|
|
682
|
+
body = "<media:document>";
|
|
683
|
+
else {
|
|
684
|
+
const locationData = extractTelegramLocation(reply);
|
|
685
|
+
if (locationData)
|
|
686
|
+
body = formatLocationText(locationData);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
if (!body)
|
|
690
|
+
return null;
|
|
691
|
+
const sender = buildSenderName(reply);
|
|
692
|
+
const senderLabel = sender ? `${sender}` : "unknown sender";
|
|
693
|
+
return {
|
|
694
|
+
id: reply.message_id ? String(reply.message_id) : undefined,
|
|
695
|
+
sender: senderLabel,
|
|
696
|
+
body,
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
function extractTelegramLocation(msg) {
|
|
700
|
+
const msgWithLocation = msg;
|
|
701
|
+
const { venue, location } = msgWithLocation;
|
|
702
|
+
if (venue) {
|
|
703
|
+
return {
|
|
704
|
+
latitude: venue.location.latitude,
|
|
705
|
+
longitude: venue.location.longitude,
|
|
706
|
+
accuracy: venue.location.horizontal_accuracy,
|
|
707
|
+
name: venue.title,
|
|
708
|
+
address: venue.address,
|
|
709
|
+
source: "place",
|
|
710
|
+
isLive: false,
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
if (location) {
|
|
714
|
+
const isLive = typeof location.live_period === "number" && location.live_period > 0;
|
|
715
|
+
return {
|
|
716
|
+
latitude: location.latitude,
|
|
717
|
+
longitude: location.longitude,
|
|
718
|
+
accuracy: location.horizontal_accuracy,
|
|
719
|
+
source: isLive ? "live" : "pin",
|
|
720
|
+
isLive,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
return null;
|
|
724
|
+
}
|