@0xmaxma/claude-gateway 2.0.20 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/config.template.json +3 -2
- package/dist/agent/builtin-commands.d.ts +4 -0
- package/dist/agent/builtin-commands.d.ts.map +1 -1
- package/dist/agent/builtin-commands.js +11 -4
- package/dist/agent/builtin-commands.js.map +1 -1
- package/dist/agent/command-help.d.ts +11 -2
- package/dist/agent/command-help.d.ts.map +1 -1
- package/dist/agent/command-help.js +29 -8
- package/dist/agent/command-help.js.map +1 -1
- package/dist/agent/model-registry.d.ts +48 -0
- package/dist/agent/model-registry.d.ts.map +1 -0
- package/dist/agent/model-registry.js +144 -0
- package/dist/agent/model-registry.js.map +1 -0
- package/dist/agent/runner.d.ts +73 -43
- package/dist/agent/runner.d.ts.map +1 -1
- package/dist/agent/runner.js +361 -120
- package/dist/agent/runner.js.map +1 -1
- package/dist/agent/session-confirmations.d.ts +1 -1
- package/dist/agent/session-confirmations.d.ts.map +1 -1
- package/dist/agent/session-confirmations.js +5 -3
- package/dist/agent/session-confirmations.js.map +1 -1
- package/dist/agent/skill-learning/reviewer.d.ts.map +1 -1
- package/dist/agent/skill-learning/reviewer.js +2 -1
- package/dist/agent/skill-learning/reviewer.js.map +1 -1
- package/dist/agent/turn-stream.d.ts +2 -1
- package/dist/agent/turn-stream.d.ts.map +1 -1
- package/dist/agent/turn-stream.js.map +1 -1
- package/dist/api/gateway-router.d.ts.map +1 -1
- package/dist/api/gateway-router.js +17 -1
- package/dist/api/gateway-router.js.map +1 -1
- package/dist/api/manifest-sources.d.ts.map +1 -1
- package/dist/api/manifest-sources.js +3 -0
- package/dist/api/manifest-sources.js.map +1 -1
- package/dist/api/pstack-router.d.ts +14 -0
- package/dist/api/pstack-router.d.ts.map +1 -0
- package/dist/api/pstack-router.js +93 -0
- package/dist/api/pstack-router.js.map +1 -0
- package/dist/api/router.d.ts.map +1 -1
- package/dist/api/router.js +32 -83
- package/dist/api/router.js.map +1 -1
- package/dist/api/voice-call-router.d.ts +21 -0
- package/dist/api/voice-call-router.d.ts.map +1 -0
- package/dist/api/voice-call-router.js +119 -0
- package/dist/api/voice-call-router.js.map +1 -0
- package/dist/api/voice-router.d.ts +27 -0
- package/dist/api/voice-router.d.ts.map +1 -1
- package/dist/api/voice-router.js +126 -63
- package/dist/api/voice-router.js.map +1 -1
- package/dist/api/voice-settings-router.d.ts.map +1 -1
- package/dist/api/voice-settings-router.js +3 -1
- package/dist/api/voice-settings-router.js.map +1 -1
- package/dist/automation/computer-use.d.ts.map +1 -1
- package/dist/automation/computer-use.js +8 -1
- package/dist/automation/computer-use.js.map +1 -1
- package/dist/build-provenance.json +4 -4
- package/dist/cli/commands.generated.d.ts.map +1 -1
- package/dist/cli/commands.generated.js +36 -1
- package/dist/cli/commands.generated.js.map +1 -1
- package/dist/cli-viewer/pairing-store.d.ts +4 -0
- package/dist/cli-viewer/pairing-store.d.ts.map +1 -1
- package/dist/cli-viewer/pairing-store.js +6 -6
- package/dist/cli-viewer/pairing-store.js.map +1 -1
- package/dist/cli-viewer/telegram-initdata.d.ts +1 -1
- package/dist/cli-viewer/telegram-initdata.d.ts.map +1 -1
- package/dist/cli-viewer/telegram-initdata.js +2 -2
- package/dist/cli-viewer/telegram-initdata.js.map +1 -1
- package/dist/cli-viewer/url.d.ts +2 -0
- package/dist/cli-viewer/url.d.ts.map +1 -1
- package/dist/cli-viewer/url.js +6 -0
- package/dist/cli-viewer/url.js.map +1 -1
- package/dist/config/loader.d.ts.map +1 -1
- package/dist/config/loader.js +7 -0
- package/dist/config/loader.js.map +1 -1
- package/dist/config/reload-policy.js +1 -1
- package/dist/config/reload-policy.js.map +1 -1
- package/dist/config/watcher.d.ts.map +1 -1
- package/dist/config/watcher.js +2 -0
- package/dist/config/watcher.js.map +1 -1
- package/dist/orchestration/agent-tool-schemas.js +1 -1
- package/dist/orchestration/agent-tool-schemas.js.map +1 -1
- package/dist/orchestration/bridge.d.ts +6 -2
- package/dist/orchestration/bridge.d.ts.map +1 -1
- package/dist/orchestration/bridge.js +122 -6
- package/dist/orchestration/bridge.js.map +1 -1
- package/dist/orchestration/capabilities.d.ts.map +1 -1
- package/dist/orchestration/capabilities.js +10 -3
- package/dist/orchestration/capabilities.js.map +1 -1
- package/dist/orchestration/capacity.d.ts +9 -2
- package/dist/orchestration/capacity.d.ts.map +1 -1
- package/dist/orchestration/capacity.js +18 -7
- package/dist/orchestration/capacity.js.map +1 -1
- package/dist/orchestration/cli-skills.d.ts.map +1 -1
- package/dist/orchestration/cli-skills.js +4 -2
- package/dist/orchestration/cli-skills.js.map +1 -1
- package/dist/orchestration/config.d.ts +47 -0
- package/dist/orchestration/config.d.ts.map +1 -1
- package/dist/orchestration/config.js +25 -4
- package/dist/orchestration/config.js.map +1 -1
- package/dist/orchestration/conversation-intake.d.ts.map +1 -1
- package/dist/orchestration/conversation-intake.js +3 -1
- package/dist/orchestration/conversation-intake.js.map +1 -1
- package/dist/orchestration/dashboard-reader-worker.js +5 -3
- package/dist/orchestration/dashboard-reader-worker.js.map +1 -1
- package/dist/orchestration/decisions.d.ts.map +1 -1
- package/dist/orchestration/decisions.js +4 -2
- package/dist/orchestration/decisions.js.map +1 -1
- package/dist/orchestration/delivery.d.ts.map +1 -1
- package/dist/orchestration/delivery.js +1 -0
- package/dist/orchestration/delivery.js.map +1 -1
- package/dist/orchestration/display-stream.d.ts +6 -0
- package/dist/orchestration/display-stream.d.ts.map +1 -1
- package/dist/orchestration/display-stream.js +13 -8
- package/dist/orchestration/display-stream.js.map +1 -1
- package/dist/orchestration/gateway-tasks/computer.d.ts +2 -0
- package/dist/orchestration/gateway-tasks/computer.d.ts.map +1 -1
- package/dist/orchestration/gateway-tasks/computer.js +68 -28
- package/dist/orchestration/gateway-tasks/computer.js.map +1 -1
- package/dist/orchestration/gateway-tasks/controller.d.ts +6 -6
- package/dist/orchestration/gateway-tasks/controller.d.ts.map +1 -1
- package/dist/orchestration/identity.d.ts +5 -1
- package/dist/orchestration/identity.d.ts.map +1 -1
- package/dist/orchestration/identity.js +6 -0
- package/dist/orchestration/identity.js.map +1 -1
- package/dist/orchestration/input-batch.d.ts +8 -2
- package/dist/orchestration/input-batch.d.ts.map +1 -1
- package/dist/orchestration/input-batch.js +28 -8
- package/dist/orchestration/input-batch.js.map +1 -1
- package/dist/orchestration/live-voice-transcript.d.ts +13 -0
- package/dist/orchestration/live-voice-transcript.d.ts.map +1 -0
- package/dist/orchestration/live-voice-transcript.js +36 -0
- package/dist/orchestration/live-voice-transcript.js.map +1 -0
- package/dist/orchestration/mutation-recovery.js +1 -1
- package/dist/orchestration/mutation-recovery.js.map +1 -1
- package/dist/orchestration/notification-mailbox.d.ts.map +1 -1
- package/dist/orchestration/notification-mailbox.js +4 -2
- package/dist/orchestration/notification-mailbox.js.map +1 -1
- package/dist/orchestration/process-turn.d.ts +6 -1
- package/dist/orchestration/process-turn.d.ts.map +1 -1
- package/dist/orchestration/process-turn.js +9 -5
- package/dist/orchestration/process-turn.js.map +1 -1
- package/dist/orchestration/recovery.d.ts.map +1 -1
- package/dist/orchestration/recovery.js +21 -0
- package/dist/orchestration/recovery.js.map +1 -1
- package/dist/orchestration/response-errors.d.ts +5 -0
- package/dist/orchestration/response-errors.d.ts.map +1 -1
- package/dist/orchestration/response-errors.js +25 -0
- package/dist/orchestration/response-errors.js.map +1 -1
- package/dist/orchestration/runtime.d.ts +26 -2
- package/dist/orchestration/runtime.d.ts.map +1 -1
- package/dist/orchestration/runtime.js +221 -48
- package/dist/orchestration/runtime.js.map +1 -1
- package/dist/orchestration/speech.d.ts +3 -0
- package/dist/orchestration/speech.d.ts.map +1 -1
- package/dist/orchestration/speech.js +13 -5
- package/dist/orchestration/speech.js.map +1 -1
- package/dist/orchestration/store.d.ts +5 -0
- package/dist/orchestration/store.d.ts.map +1 -1
- package/dist/orchestration/store.js +13 -5
- package/dist/orchestration/store.js.map +1 -1
- package/dist/orchestration/subagent-tool-schemas.d.ts +12 -0
- package/dist/orchestration/subagent-tool-schemas.d.ts.map +1 -0
- package/dist/orchestration/subagent-tool-schemas.js +23 -0
- package/dist/orchestration/subagent-tool-schemas.js.map +1 -0
- package/dist/orchestration/task-controls.d.ts.map +1 -1
- package/dist/orchestration/task-controls.js +2 -1
- package/dist/orchestration/task-controls.js.map +1 -1
- package/dist/orchestration/task-questions.d.ts.map +1 -1
- package/dist/orchestration/task-questions.js +3 -1
- package/dist/orchestration/task-questions.js.map +1 -1
- package/dist/orchestration/tasks/automation-session.d.ts +12 -0
- package/dist/orchestration/tasks/automation-session.d.ts.map +1 -1
- package/dist/orchestration/tasks/automation-session.js +26 -1
- package/dist/orchestration/tasks/automation-session.js.map +1 -1
- package/dist/orchestration/tasks/driver.d.ts.map +1 -1
- package/dist/orchestration/tasks/driver.js +54 -14
- package/dist/orchestration/tasks/driver.js.map +1 -1
- package/dist/orchestration/tasks/scheduler.d.ts.map +1 -1
- package/dist/orchestration/tasks/scheduler.js +17 -3
- package/dist/orchestration/tasks/scheduler.js.map +1 -1
- package/dist/orchestration/tasks/service.d.ts +110 -34
- package/dist/orchestration/tasks/service.d.ts.map +1 -1
- package/dist/orchestration/tasks/service.js +234 -19
- package/dist/orchestration/tasks/service.js.map +1 -1
- package/dist/orchestration/tasks/task-directive.d.ts.map +1 -1
- package/dist/orchestration/tasks/task-directive.js +5 -1
- package/dist/orchestration/tasks/task-directive.js.map +1 -1
- package/dist/orchestration/telegram-tool-status.d.ts +8 -1
- package/dist/orchestration/telegram-tool-status.d.ts.map +1 -1
- package/dist/orchestration/telegram-tool-status.js +57 -6
- package/dist/orchestration/telegram-tool-status.js.map +1 -1
- package/dist/orchestration/telegram-voices.d.ts +4 -0
- package/dist/orchestration/telegram-voices.d.ts.map +1 -1
- package/dist/orchestration/telegram-voices.js +6 -2
- package/dist/orchestration/telegram-voices.js.map +1 -1
- package/dist/orchestration/token-ledger.d.ts +14 -0
- package/dist/orchestration/token-ledger.d.ts.map +1 -1
- package/dist/orchestration/token-ledger.js +28 -2
- package/dist/orchestration/token-ledger.js.map +1 -1
- package/dist/orchestration/token-usage.d.ts +14 -0
- package/dist/orchestration/token-usage.d.ts.map +1 -1
- package/dist/orchestration/token-usage.js +16 -2
- package/dist/orchestration/token-usage.js.map +1 -1
- package/dist/orchestration/tool-name.d.ts.map +1 -1
- package/dist/orchestration/tool-name.js +5 -1
- package/dist/orchestration/tool-name.js.map +1 -1
- package/dist/orchestration/types.d.ts +29 -0
- package/dist/orchestration/types.d.ts.map +1 -1
- package/dist/orchestration/types.js +11 -0
- package/dist/orchestration/types.js.map +1 -1
- package/dist/orchestration/worker-harness.d.ts.map +1 -1
- package/dist/orchestration/worker-harness.js +5 -2
- package/dist/orchestration/worker-harness.js.map +1 -1
- package/dist/pstack/config.d.ts +48 -0
- package/dist/pstack/config.d.ts.map +1 -0
- package/dist/pstack/config.js +147 -0
- package/dist/pstack/config.js.map +1 -0
- package/dist/pstack/menu.d.ts +71 -0
- package/dist/pstack/menu.d.ts.map +1 -0
- package/dist/pstack/menu.js +286 -0
- package/dist/pstack/menu.js.map +1 -0
- package/dist/pstack/overlay.d.ts +30 -0
- package/dist/pstack/overlay.d.ts.map +1 -0
- package/dist/pstack/overlay.js +121 -0
- package/dist/pstack/overlay.js.map +1 -0
- package/dist/pstack/plugin-info.d.ts +36 -0
- package/dist/pstack/plugin-info.d.ts.map +1 -0
- package/dist/pstack/plugin-info.js +51 -0
- package/dist/pstack/plugin-info.js.map +1 -0
- package/dist/pstack/reflect.d.ts +14 -0
- package/dist/pstack/reflect.d.ts.map +1 -0
- package/dist/pstack/reflect.js +49 -0
- package/dist/pstack/reflect.js.map +1 -0
- package/dist/pstack/setup.d.ts +17 -0
- package/dist/pstack/setup.d.ts.map +1 -0
- package/dist/pstack/setup.js +47 -0
- package/dist/pstack/setup.js.map +1 -0
- package/dist/pstack/sheet.d.ts +19 -0
- package/dist/pstack/sheet.d.ts.map +1 -0
- package/dist/pstack/sheet.js +44 -0
- package/dist/pstack/sheet.js.map +1 -0
- package/dist/pstack/templates.d.ts +14 -0
- package/dist/pstack/templates.d.ts.map +1 -0
- package/dist/pstack/templates.js +41 -0
- package/dist/pstack/templates.js.map +1 -0
- package/dist/session/codex-process.d.ts +0 -3
- package/dist/session/codex-process.d.ts.map +1 -1
- package/dist/session/codex-process.js +8 -3
- package/dist/session/codex-process.js.map +1 -1
- package/dist/session/compactor.d.ts.map +1 -1
- package/dist/session/compactor.js +2 -1
- package/dist/session/compactor.js.map +1 -1
- package/dist/session/process-diagnostics.d.ts +3 -0
- package/dist/session/process-diagnostics.d.ts.map +1 -1
- package/dist/session/process-diagnostics.js +10 -0
- package/dist/session/process-diagnostics.js.map +1 -1
- package/dist/session/process.d.ts.map +1 -1
- package/dist/session/process.js +9 -4
- package/dist/session/process.js.map +1 -1
- package/dist/session/provider-refusal.d.ts +36 -0
- package/dist/session/provider-refusal.d.ts.map +1 -0
- package/dist/session/provider-refusal.js +109 -0
- package/dist/session/provider-refusal.js.map +1 -0
- package/dist/session/runtime-profile.d.ts +13 -2
- package/dist/session/runtime-profile.d.ts.map +1 -1
- package/dist/session/runtime-profile.js +35 -2
- package/dist/session/runtime-profile.js.map +1 -1
- package/dist/session/worker-extensions.d.ts +5 -2
- package/dist/session/worker-extensions.d.ts.map +1 -1
- package/dist/session/worker-extensions.js +10 -5
- package/dist/session/worker-extensions.js.map +1 -1
- package/dist/telegram/access.d.ts +23 -0
- package/dist/telegram/access.d.ts.map +1 -0
- package/dist/telegram/access.js +105 -0
- package/dist/telegram/access.js.map +1 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/ui/cli-viewer-ui.d.ts +5 -0
- package/dist/ui/cli-viewer-ui.d.ts.map +1 -1
- package/dist/ui/cli-viewer-ui.js +9 -6
- package/dist/ui/cli-viewer-ui.js.map +1 -1
- package/dist/ui/dashboard-client.d.ts.map +1 -1
- package/dist/ui/dashboard-client.js +14 -4
- package/dist/ui/dashboard-client.js.map +1 -1
- package/dist/ui/dashboard-presentation.d.ts +29 -0
- package/dist/ui/dashboard-presentation.d.ts.map +1 -1
- package/dist/ui/dashboard-presentation.js +26 -1
- package/dist/ui/dashboard-presentation.js.map +1 -1
- package/dist/ui/token-report.d.ts +3 -0
- package/dist/ui/token-report.d.ts.map +1 -1
- package/dist/ui/token-report.js +1 -1
- package/dist/ui/token-report.js.map +1 -1
- package/dist/ui/voice-call-client.d.ts +142 -0
- package/dist/ui/voice-call-client.d.ts.map +1 -0
- package/dist/ui/voice-call-client.js +328 -0
- package/dist/ui/voice-call-client.js.map +1 -0
- package/dist/ui/voice-call-ui.d.ts +21 -0
- package/dist/ui/voice-call-ui.d.ts.map +1 -0
- package/dist/ui/voice-call-ui.js +488 -0
- package/dist/ui/voice-call-ui.js.map +1 -0
- package/dist/voice/audio-tags.d.ts +7 -0
- package/dist/voice/audio-tags.d.ts.map +1 -0
- package/dist/voice/audio-tags.js +66 -0
- package/dist/voice/audio-tags.js.map +1 -0
- package/dist/voice/call-pairing.d.ts +76 -0
- package/dist/voice/call-pairing.d.ts.map +1 -0
- package/dist/voice/call-pairing.js +126 -0
- package/dist/voice/call-pairing.js.map +1 -0
- package/dist/voice/diagnostics.d.ts +12 -9
- package/dist/voice/diagnostics.d.ts.map +1 -1
- package/dist/voice/diagnostics.js +2 -1
- package/dist/voice/diagnostics.js.map +1 -1
- package/dist/voice/errors.d.ts +6 -1
- package/dist/voice/errors.d.ts.map +1 -1
- package/dist/voice/errors.js +12 -1
- package/dist/voice/errors.js.map +1 -1
- package/dist/voice/fast-ack.d.ts +27 -0
- package/dist/voice/fast-ack.d.ts.map +1 -0
- package/dist/voice/fast-ack.js +52 -0
- package/dist/voice/fast-ack.js.map +1 -0
- package/dist/voice/managed-quota.d.ts +29 -3
- package/dist/voice/managed-quota.d.ts.map +1 -1
- package/dist/voice/managed-quota.js +68 -23
- package/dist/voice/managed-quota.js.map +1 -1
- package/dist/voice/notes.d.ts +27 -3
- package/dist/voice/notes.d.ts.map +1 -1
- package/dist/voice/notes.js +59 -10
- package/dist/voice/notes.js.map +1 -1
- package/dist/voice/providers/elevenlabs-tts.d.ts +1 -0
- package/dist/voice/providers/elevenlabs-tts.d.ts.map +1 -1
- package/dist/voice/providers/elevenlabs-tts.js +62 -12
- package/dist/voice/providers/elevenlabs-tts.js.map +1 -1
- package/dist/voice/providers/gemini.d.ts +4 -0
- package/dist/voice/providers/gemini.d.ts.map +1 -1
- package/dist/voice/providers/gemini.js +7 -1
- package/dist/voice/providers/gemini.js.map +1 -1
- package/dist/voice/providers/registry.d.ts.map +1 -1
- package/dist/voice/providers/registry.js +34 -2
- package/dist/voice/providers/registry.js.map +1 -1
- package/dist/voice/scope.d.ts +13 -0
- package/dist/voice/scope.d.ts.map +1 -0
- package/dist/voice/scope.js +3 -0
- package/dist/voice/scope.js.map +1 -0
- package/dist/voice/session.d.ts +16 -1
- package/dist/voice/session.d.ts.map +1 -1
- package/dist/voice/session.js +90 -16
- package/dist/voice/session.js.map +1 -1
- package/dist/voice/telegram-avatar.d.ts +38 -0
- package/dist/voice/telegram-avatar.d.ts.map +1 -0
- package/dist/voice/telegram-avatar.js +95 -0
- package/dist/voice/telegram-avatar.js.map +1 -0
- package/mcp/lazy-tools.ts +4 -2
- package/mcp/server.ts +6 -2
- package/mcp/tools/tasks/module.ts +3 -1
- package/mcp/tools/telegram/pstack-menu.ts +22 -0
- package/mcp/tools/telegram/pure.ts +40 -0
- package/mcp/tools/telegram/receiver-server.ts +86 -7
- package/package.json +1 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TelegramBotAvatars = exports.BOT_AVATAR_MAX_BYTES = void 0;
|
|
4
|
+
exports.BOT_AVATAR_MAX_BYTES = 1024 * 1024;
|
|
5
|
+
/** The page shows the avatar at 112 CSS px; 320 px stays sharp on a 3x screen. */
|
|
6
|
+
const BOT_AVATAR_MIN_SIDE = 320;
|
|
7
|
+
/** How long a call page waits for the lookup before it renders with what is already known. */
|
|
8
|
+
const PAGE_LOOKUP_WAIT_MS = 1500;
|
|
9
|
+
const IMAGE_TYPES = [
|
|
10
|
+
['image/jpeg', bytes => bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff],
|
|
11
|
+
['image/png', bytes => bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))],
|
|
12
|
+
['image/webp', bytes => bytes.subarray(0, 4).toString('latin1') === 'RIFF' && bytes.subarray(8, 12).toString('latin1') === 'WEBP'],
|
|
13
|
+
];
|
|
14
|
+
class TelegramBotAvatars {
|
|
15
|
+
constructor(request = fetch, pageWaitMs = PAGE_LOOKUP_WAIT_MS) {
|
|
16
|
+
this.request = request;
|
|
17
|
+
this.pageWaitMs = pageWaitMs;
|
|
18
|
+
this.profiles = new Map();
|
|
19
|
+
this.lookups = new Map();
|
|
20
|
+
}
|
|
21
|
+
/** The profile a call page renders with: the fresh lookup when it answers within
|
|
22
|
+
* `pageWaitMs`, otherwise the last known one while the lookup finishes for the next call. */
|
|
23
|
+
async forPage(agentId, botToken) {
|
|
24
|
+
let timer;
|
|
25
|
+
const known = new Promise(resolve => { timer = setTimeout(() => resolve(this.known(agentId, botToken) ?? { avatar: null }), this.pageWaitMs); });
|
|
26
|
+
try {
|
|
27
|
+
return await Promise.race([this.refresh(agentId, botToken), known]);
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Look the bot up again, as each call page opens: a renamed bot or a new photo
|
|
34
|
+
* shows on the next call. The photo is downloaded only when it changed. When
|
|
35
|
+
* Telegram cannot be reached the last known profile stands. */
|
|
36
|
+
refresh(agentId, botToken) {
|
|
37
|
+
const running = this.lookups.get(agentId);
|
|
38
|
+
if (running)
|
|
39
|
+
return running;
|
|
40
|
+
const lookup = this.lookUp(agentId, botToken).finally(() => this.lookups.delete(agentId));
|
|
41
|
+
this.lookups.set(agentId, lookup);
|
|
42
|
+
return lookup;
|
|
43
|
+
}
|
|
44
|
+
/** The photo the page was rendered with; looked up once if this process has none yet. */
|
|
45
|
+
async avatar(agentId, botToken) {
|
|
46
|
+
return (this.known(agentId, botToken) ?? await this.refresh(agentId, botToken)).avatar;
|
|
47
|
+
}
|
|
48
|
+
known(agentId, botToken) {
|
|
49
|
+
const known = this.profiles.get(agentId);
|
|
50
|
+
return known?.token === botToken ? known : undefined;
|
|
51
|
+
}
|
|
52
|
+
async lookUp(agentId, token) {
|
|
53
|
+
const previous = this.known(agentId, token);
|
|
54
|
+
try {
|
|
55
|
+
const me = await this.call(token, 'getMe', {});
|
|
56
|
+
const photos = await this.call(token, 'getUserProfilePhotos', { user_id: me.id, limit: 1 });
|
|
57
|
+
const sizes = [...(photos.photos[0] ?? [])].sort((a, b) => a.width - b.width);
|
|
58
|
+
const size = sizes.find(candidate => Math.min(candidate.width, candidate.height) >= BOT_AVATAR_MIN_SIDE) ?? sizes.at(-1);
|
|
59
|
+
const avatar = !size ? null : previous?.photoId === size.file_unique_id ? previous.avatar : await this.download(token, size);
|
|
60
|
+
const profile = { token, name: me.first_name?.trim() || undefined, photoId: avatar ? size?.file_unique_id : undefined, avatar };
|
|
61
|
+
this.profiles.set(agentId, profile);
|
|
62
|
+
return profile;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return previous ?? { avatar: null };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async download(token, size) {
|
|
69
|
+
const file = await this.call(token, 'getFile', { file_id: size.file_id });
|
|
70
|
+
const path = file.file_path;
|
|
71
|
+
if (!path || path.split('/').includes('..') || !/^[A-Za-z0-9_./-]+$/.test(path) || (file.file_size ?? 0) > exports.BOT_AVATAR_MAX_BYTES)
|
|
72
|
+
return null;
|
|
73
|
+
const response = await this.request(`https://api.telegram.org/file/bot${token}/${path}`, { redirect: 'error', signal: AbortSignal.timeout(10000) });
|
|
74
|
+
if (!response.ok)
|
|
75
|
+
throw new Error('BOT_AVATAR_UNAVAILABLE');
|
|
76
|
+
if (Number(response.headers.get('content-length')) > exports.BOT_AVATAR_MAX_BYTES)
|
|
77
|
+
return null;
|
|
78
|
+
const bytes = Buffer.from(await response.arrayBuffer());
|
|
79
|
+
if (bytes.length > exports.BOT_AVATAR_MAX_BYTES)
|
|
80
|
+
return null;
|
|
81
|
+
const type = IMAGE_TYPES.find(([, matches]) => matches(bytes));
|
|
82
|
+
return type ? { bytes, contentType: type[0] } : null;
|
|
83
|
+
}
|
|
84
|
+
async call(token, method, body) {
|
|
85
|
+
const response = await this.request(`https://api.telegram.org/bot${token}/${method}`, {
|
|
86
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), signal: AbortSignal.timeout(10000),
|
|
87
|
+
});
|
|
88
|
+
const parsed = await response.json().catch(() => undefined);
|
|
89
|
+
if (!response.ok || !parsed?.ok || parsed.result === undefined)
|
|
90
|
+
throw new Error('BOT_AVATAR_UNAVAILABLE');
|
|
91
|
+
return parsed.result;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.TelegramBotAvatars = TelegramBotAvatars;
|
|
95
|
+
//# sourceMappingURL=telegram-avatar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telegram-avatar.js","sourceRoot":"","sources":["../../src/voice/telegram-avatar.ts"],"names":[],"mappings":";;;AAUa,QAAA,oBAAoB,GAAG,IAAI,GAAG,IAAI,CAAC;AAChD,kFAAkF;AAClF,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,8FAA8F;AAC9F,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAKjC,MAAM,WAAW,GAAgD;IAC/D,CAAC,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,CAAC,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,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;QAC/D,OAAO,IAAI,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACvD,CAAC;IAEO,KAAK,CAAC,IAAI,CAAI,KAAa,EAAE,MAAc,EAAE,IAAY;QAC/D,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,+BAA+B,KAAK,IAAI,MAAM,EAAE,EAAE;YACpF,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC;SAChI,CAAC,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAgC,CAAC;QAC3F,IAAI,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,EAAE,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC1G,OAAO,MAAM,CAAC,MAAM,CAAC;IACvB,CAAC;CACF;AAvED,gDAuEC"}
|
package/mcp/lazy-tools.ts
CHANGED
|
@@ -33,9 +33,11 @@ export function createLazyToolCatalog(tools: readonly McpToolDefinition[], exclu
|
|
|
33
33
|
tools: matches.slice(offset, offset + limit).map(tool => schemas ? tool : { name: tool.name, description: tool.description }) });
|
|
34
34
|
},
|
|
35
35
|
async call(args: Record<string, unknown>, dispatch: (name: string, input: Record<string, unknown>) => Promise<McpToolResult>): Promise<McpToolResult> {
|
|
36
|
-
|
|
36
|
+
// A model knows these tools by their MCP names (`mcp__gateway__x`); the catalog holds the bare `x`.
|
|
37
|
+
const name = typeof args.name === 'string' ? args.name.replace(/^mcp__gateway__/, '') : undefined;
|
|
38
|
+
if (!name || !names.has(name)) return fail('Unknown or unauthorized gateway tool');
|
|
37
39
|
if (!args.arguments || typeof args.arguments !== 'object' || Array.isArray(args.arguments)) return fail('Tool arguments must be an object');
|
|
38
|
-
return dispatch(
|
|
40
|
+
return dispatch(name, args.arguments as Record<string, unknown>);
|
|
39
41
|
},
|
|
40
42
|
};
|
|
41
43
|
}
|
package/mcp/server.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
CallToolRequestSchema,
|
|
14
14
|
} from '@modelcontextprotocol/sdk/types.js';
|
|
15
15
|
import { gatewayModules } from './modules';
|
|
16
|
-
import { AGENT_TASK_TOOLS, WORKER_REPORT_TOOLS, callTaskBridge } from './tools/tasks/module';
|
|
16
|
+
import { AGENT_TASK_TOOLS, SUBAGENT_TOOLS, WORKER_REPORT_TOOLS, callTaskBridge, readonlyDenied } from './tools/tasks/module';
|
|
17
17
|
import { createLazyToolCatalog, LAZY_TOOL_DEFINITIONS } from './lazy-tools';
|
|
18
18
|
import { buildChannelInstructions } from './instructions';
|
|
19
19
|
import type { ChannelModule, ToolModule, McpToolDefinition } from './types';
|
|
@@ -61,8 +61,12 @@ for (const mod of modules) {
|
|
|
61
61
|
// mid-session and invalidated the whole cache — the same defect ee800a7 fixed for the response
|
|
62
62
|
// schema. The turn's INTAKE_OVERLAY prompt text, below the cache breakpoint, is what asks for
|
|
63
63
|
// the tool; the bridge refuses the call outright when the feature is not active.
|
|
64
|
-
const taskTools = ORCHESTRATION_ROLE === 'agent' ? AGENT_TASK_TOOLS.filter(tool => (tool.name !== 'jev_evaluate' || process.env.GATEWAY_JEV_ENABLED === 'true')).filter(tool => tool.name !== 'capabilities_list' || process.env.GATEWAY_CAPABILITY_CATALOG === 'true') : ORCHESTRATION_ROLE === 'worker' ? WORKER_REPORT_TOOLS.filter(tool => (tool.name !== 'jev_evaluate' || process.env.GATEWAY_JEV_ENABLED === 'true')).filter(tool => tool.name !== 'task_memory_append' || process.env.GATEWAY_ORCHESTRATION_WRITE_MEMORY === 'true') : [];
|
|
64
|
+
const taskTools = ORCHESTRATION_ROLE === 'agent' ? AGENT_TASK_TOOLS.filter(tool => (tool.name !== 'jev_evaluate' || process.env.GATEWAY_JEV_ENABLED === 'true')).filter(tool => tool.name !== 'capabilities_list' || process.env.GATEWAY_CAPABILITY_CATALOG === 'true') : ORCHESTRATION_ROLE === 'worker' ? WORKER_REPORT_TOOLS.filter(tool => (tool.name !== 'jev_evaluate' || process.env.GATEWAY_JEV_ENABLED === 'true')).filter(tool => tool.name !== 'task_memory_append' || process.env.GATEWAY_ORCHESTRATION_WRITE_MEMORY === 'true').concat(process.env.GATEWAY_SUBAGENTS === 'true' ? SUBAGENT_TOOLS : []) : [];
|
|
65
65
|
if (ORCHESTRATION_ROLE && process.env.GATEWAY_ORCHESTRATION_TICKET_FILE) visibleTools.push(...taskTools);
|
|
66
|
+
// A read-only subagent sees only tools that change nothing; the bridge refuses the rest.
|
|
67
|
+
if (ORCHESTRATION_ROLE === 'worker' && process.env.GATEWAY_READONLY_TASK === 'true') {
|
|
68
|
+
for (let i = visibleTools.length - 1; i >= 0; i--) if (readonlyDenied(visibleTools[i].name)) { toolMap.delete(visibleTools[i].name); visibleTools.splice(i, 1); }
|
|
69
|
+
}
|
|
66
70
|
|
|
67
71
|
// Preserve the full policy-filtered inventory internally; only schema exposure is lazy.
|
|
68
72
|
const lazyEnabled = ORCHESTRATION_ROLE === 'worker' && process.env.GATEWAY_LAZY_TOOLS === 'true';
|
|
@@ -7,6 +7,7 @@ import type { McpToolDefinition, McpToolResult } from '../../types';
|
|
|
7
7
|
const schema = (properties: Record<string, unknown>, required: string[]) => ({ type: 'object', properties, required, additionalProperties: false });
|
|
8
8
|
const text = { type: 'string' };
|
|
9
9
|
export { AGENT_TASK_TOOLS } from '../../../dist/orchestration/agent-tool-schemas.js';
|
|
10
|
+
export { SUBAGENT_TOOLS, readonlyDenied } from '../../../dist/orchestration/subagent-tool-schemas.js';
|
|
10
11
|
export const WORKER_REPORT_TOOLS: McpToolDefinition[] = [
|
|
11
12
|
JEV_TOOL,
|
|
12
13
|
{ name: 'task_memory_append', description: 'Append an explicitly requested memory note for a channel task. API tasks cannot write memory. Files are restricted to MEMORY.md, USER.md, or memory/<name>.md. Retains existing content and deduplicates retries.', inputSchema: schema({ note: text, path: text }, ['note']) },
|
|
@@ -15,7 +16,8 @@ export const WORKER_REPORT_TOOLS: McpToolDefinition[] = [
|
|
|
15
16
|
{ name: 'task_request_input', description: 'Ask the user for information required by your task, then end this turn immediately.', inputSchema: schema({ question: text }, ['question']) },
|
|
16
17
|
];
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
// subagent_wait (and a foreground subagent_spawn) answers when the subagent finishes, as native `Agent` does.
|
|
20
|
+
const LONG_RUNNING_TOOLS = new Set(['narrate', 'subagent_wait', 'subagent_spawn']);
|
|
19
21
|
export const LONG_RUNNING_MAX_MS = 2 * 60 * 60 * 1000;
|
|
20
22
|
export async function callTaskBridge(tool: string, args: Record<string, unknown>, requestId: string, signal: AbortSignal): Promise<McpToolResult> {
|
|
21
23
|
// Rotated by the gateway at each decision/attempt boundary. A loaded token is
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { InlineKeyboard } from 'grammy'
|
|
2
|
+
|
|
3
|
+
/** One /pstack-setup screen as the gateway renders it (`telegram_pstack`). `done`: a final result, shown without buttons. */
|
|
4
|
+
export type PstackMenu = { text: string; keyboard: Array<Array<{ text: string; data: string }>>; closed?: boolean; done?: boolean; toast?: string }
|
|
5
|
+
|
|
6
|
+
/** Telegram caps callback_data at 64 UTF-8 bytes and text at 4096 characters. */
|
|
7
|
+
const MAX_DATA_BYTES = 64
|
|
8
|
+
const MAX_TEXT = 4096
|
|
9
|
+
|
|
10
|
+
/** Validate the gateway's screen and build its keyboard; a malformed button is dropped, not sent. */
|
|
11
|
+
export function pstackMenuMarkup(value: unknown): { text: string; reply_markup: InlineKeyboard; closed: boolean; done: boolean; toast?: string } {
|
|
12
|
+
const menu = value as Partial<PstackMenu>
|
|
13
|
+
if (typeof menu?.text !== 'string' || !Array.isArray(menu.keyboard)) throw Error('INVALID_PSTACK_MENU')
|
|
14
|
+
const rows = menu.keyboard.filter(Array.isArray).map(row => row
|
|
15
|
+
.filter(button => typeof button?.text === 'string' && typeof button?.data === 'string' && button.data.startsWith('ps:') && Buffer.byteLength(button.data, 'utf8') <= MAX_DATA_BYTES)
|
|
16
|
+
.map(button => InlineKeyboard.text(button.text.slice(0, 64), button.data))).filter(row => row.length)
|
|
17
|
+
const keyboard = new InlineKeyboard(rows)
|
|
18
|
+
return { text: menu.text.slice(0, MAX_TEXT), reply_markup: keyboard, closed: menu.closed === true, done: menu.done === true, ...(typeof menu.toast === 'string' ? { toast: menu.toast.slice(0, 180) } : {}) }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** `/pstack-setup` or `/pstack_setup` with nothing after it opens the menu; with arguments it is the text command. */
|
|
22
|
+
export const PSTACK_MENU_COMMAND = /^\/pstack[-_]setup(?:@\w+)?\s*$/i
|
|
@@ -339,10 +339,50 @@ export function normalizeTelegramLineBreaks(text: string): string {
|
|
|
339
339
|
return text.replace(/(?:<|<)br\s*\/?(?:>|>)/gi, '\n')
|
|
340
340
|
}
|
|
341
341
|
|
|
342
|
+
/** Commands the receiver answers itself, in a direct message only (and `cli` where the terminal viewer is on). */
|
|
343
|
+
const DM_ONLY_COMMANDS = ['start', 'help', 'status', 'model', 'models', 'clear', 'compact', 'restart', 'session', 'sessions', 'tasks', 'voice', 'voices', 'call']
|
|
344
|
+
/** Commands forwarded to the gateway, which answers them in a group as well. */
|
|
345
|
+
const GROUP_COMMANDS = ['stop', 'new', 'rename', 'pstack_setup', 'pstack-setup']
|
|
346
|
+
|
|
347
|
+
/** The leading `/name` or `/name@<this bot>` of a message, lowercased; undefined when there is none or it names another bot. */
|
|
348
|
+
export function leadingCommand(text: string, botUsername: string): string | undefined {
|
|
349
|
+
const match = /^\/([a-z0-9_-]+)(?:@(\w+))?(?=\s|$)/i.exec(text.trim())
|
|
350
|
+
if (!match || (match[2] && match[2].toLowerCase() !== botUsername.toLowerCase())) return undefined
|
|
351
|
+
return match[1]!.toLowerCase()
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** A gateway command typed in a group is addressed to this bot, the same as a mention. */
|
|
355
|
+
export function isGroupCommand(text: string, botUsername: string): boolean {
|
|
356
|
+
return GROUP_COMMANDS.includes(leadingCommand(text, botUsername) ?? '')
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** `/name@<this bot> …` → `/name …`, so the gateway matches the command it was sent. */
|
|
360
|
+
export function stripCommandTarget(text: string, botUsername: string): string {
|
|
361
|
+
const match = /^(\s*\/[a-z0-9_-]+)@(\w+)(?=\s|$)/i.exec(text)
|
|
362
|
+
return match && botUsername && match[2]!.toLowerCase() === botUsername.toLowerCase() ? match[1] + text.slice(match[0].length) : text
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* The reply to a direct-message-only command typed in a group, or undefined.
|
|
367
|
+
* Only a group whose messages the gate delivers gets one; others stay silent,
|
|
368
|
+
* as the gate keeps them. The gate decides on a copy with pairing off and the
|
|
369
|
+
* mention rule waived, so no pairing code is minted and nothing is saved.
|
|
370
|
+
*/
|
|
371
|
+
export function groupCommandReply(access: Access, input: { chatType?: string; chatId: string; fromId: string; text: string; botUsername: string; cliEnabled?: boolean }): string | undefined {
|
|
372
|
+
if (input.chatType !== 'group' && input.chatType !== 'supergroup') return undefined
|
|
373
|
+
const name = leadingCommand(input.text, input.botUsername)
|
|
374
|
+
if (!name || !(DM_ONLY_COMMANDS.includes(name) || (name === 'cli' && input.cliEnabled))) return undefined
|
|
375
|
+
const copy: Access = { ...access, pairing: false, requireMention: false, pending: { ...access.pending } }
|
|
376
|
+
const verdict = gateLogic({ fromId: input.fromId, chatType: input.chatType, chatId: input.chatId, botUsername: input.botUsername, messageText: input.text },
|
|
377
|
+
() => copy, () => {}, () => { throw Error('NO_PAIRING_IN_GROUP_COMMAND_REPLY') })
|
|
378
|
+
return verdict.action === 'deliver' ? `/${name} is only available in a direct message with the bot.` : undefined
|
|
379
|
+
}
|
|
380
|
+
|
|
342
381
|
export function isMentionedPure(input: GateInput, extraPatterns?: string[]): boolean {
|
|
343
382
|
const entities = input.messageEntities ?? input.captionEntities ?? []
|
|
344
383
|
const text = input.messageText ?? ''
|
|
345
384
|
const botUsername = input.botUsername ?? ''
|
|
385
|
+
if (isGroupCommand(text, botUsername)) return true
|
|
346
386
|
|
|
347
387
|
for (const e of entities) {
|
|
348
388
|
if (e.type === 'mention') {
|
|
@@ -29,6 +29,7 @@ import type { ReactionTypeEmoji } from 'grammy/types'
|
|
|
29
29
|
import { randomBytes } from 'crypto'
|
|
30
30
|
import { confirmSelection, voiceModeConfirmation } from './selection-confirmation'
|
|
31
31
|
import { LiveTaskBrowser, type TaskBrowserMessage } from './task-browser'
|
|
32
|
+
import { PSTACK_MENU_COMMAND, pstackMenuMarkup } from './pstack-menu'
|
|
32
33
|
import { ReceiverSpool } from '../receiver-spool'
|
|
33
34
|
const orchestrationSpool = process.env.GATEWAY_ORCHESTRATION_INGRESS_DIR && process.env.CLAUDE_CHANNEL_CALLBACK
|
|
34
35
|
? new ReceiverSpool(process.env.GATEWAY_ORCHESTRATION_INGRESS_DIR, process.env.CLAUDE_CHANNEL_CALLBACK, fetch, Math.max(250,Math.min(10000,Number(process.env.GATEWAY_MEDIA_GROUP_WAIT_MS)||2000))) : undefined
|
|
@@ -49,7 +50,7 @@ import { formatTurnIncident, type TurnIncident } from '../../../dist/agent/turn-
|
|
|
49
50
|
import { createIncidentStore } from '../../../dist/agent/incident-store.js'
|
|
50
51
|
import type { RecoveryOutcome } from '../../../dist/agent/incident.js'
|
|
51
52
|
import { initDedupDir, isDuplicate as _isDuplicate, pruneDedup as _pruneDedup } from './dedup'
|
|
52
|
-
import { normalizeTelegramLineBreaks, resolveTelegramReplyFormat, migrateAccess, telegramDisplayName } from './pure'
|
|
53
|
+
import { normalizeTelegramLineBreaks, resolveTelegramReplyFormat, migrateAccess, telegramDisplayName, groupCommandReply, isGroupCommand, stripCommandTarget } from './pure'
|
|
53
54
|
import { MAX_ATTACHMENT_BYTES } from '../shared/limits'
|
|
54
55
|
|
|
55
56
|
// Standalone fallback: default state dir to ~/.claude/channels/telegram
|
|
@@ -526,6 +527,7 @@ function countPending(access: Access, kind: 'dm' | 'group'): number {
|
|
|
526
527
|
function isMentioned(ctx: Context, extraPatterns?: string[]): boolean {
|
|
527
528
|
const entities = ctx.message?.entities ?? ctx.message?.caption_entities ?? []
|
|
528
529
|
const text = ctx.message?.text ?? ctx.message?.caption ?? ''
|
|
530
|
+
if (isGroupCommand(text, botUsername)) return true
|
|
529
531
|
for (const e of entities) {
|
|
530
532
|
if (e.type === 'mention') {
|
|
531
533
|
const mentioned = text.slice(e.offset, e.offset + e.length)
|
|
@@ -874,7 +876,7 @@ const RECEIVER_MODE = process.env.TELEGRAM_RECEIVER_MODE === 'true'
|
|
|
874
876
|
import { parseMenuFileContent } from './menu-parser'
|
|
875
877
|
|
|
876
878
|
const BOT_COMMANDS = [
|
|
877
|
-
...(ORCHESTRATION_ENABLED ? [{ command: 'tasks', description: 'View pending tasks in this chat' }, { command: 'voice', description: 'Turn automatic voice replies on or off' }, { command: 'voices', description: 'Choose the agent voice' }] : []),
|
|
879
|
+
...(ORCHESTRATION_ENABLED ? [{ command: 'tasks', description: 'View pending tasks in this chat' }, { command: 'voice', description: 'Turn automatic voice replies on or off' }, { command: 'voices', description: 'Choose the agent voice' }, { command: 'call', description: 'Start a live voice call with the agent' }] : []),
|
|
878
880
|
{ command: 'session', description: 'Show current session info' },
|
|
879
881
|
{ command: 'sessions', description: 'Manage conversation sessions' },
|
|
880
882
|
{ command: 'new', description: 'Create a new session' },
|
|
@@ -885,6 +887,7 @@ const BOT_COMMANDS = [
|
|
|
885
887
|
{ command: 'restart', description: 'Graceful restart session' },
|
|
886
888
|
{ command: 'model', description: 'Show current AI model' },
|
|
887
889
|
{ command: 'models', description: 'Switch AI model' },
|
|
890
|
+
{ command: 'pstack_setup', description: 'Set up pstack for this agent' },
|
|
888
891
|
...(INTERACTIVE_CLI_ENABLED ? [{ command: 'cli', description: 'Open the live terminal viewer' }] : []),
|
|
889
892
|
{ command: 'start', description: 'Welcome and setup guide' },
|
|
890
893
|
{ command: 'status', description: 'Check your pairing status' },
|
|
@@ -922,7 +925,8 @@ const AVAILABLE_MODELS = [
|
|
|
922
925
|
{ id: 'claude-sonnet-5-5', label: 'Sonnet 5.5', alias: 'sonnet' },
|
|
923
926
|
{ id: 'claude-sonnet-5', label: 'Sonnet 5', alias: 'sonnet5' },
|
|
924
927
|
{ id: 'claude-sonnet-4-6', label: 'Sonnet 4.6', alias: 'sonnet46' },
|
|
925
|
-
{ id: 'claude-haiku-
|
|
928
|
+
{ id: 'claude-haiku-5-5', label: 'Haiku 5.5', alias: 'haiku' },
|
|
929
|
+
{ id: 'claude-haiku-4-5-20251001', label: 'Haiku 4.5', alias: 'haiku45' },
|
|
926
930
|
{ id: 'gpt-6-sol[1m]', label: 'GPT 6 Sol', alias: 'gpt6sol' },
|
|
927
931
|
{ id: 'gpt-6-astra[1m]', label: 'GPT 6 Astra', alias: 'gpt6astra' },
|
|
928
932
|
{ id: 'gpt-6-luna[1m]', label: 'GPT 6 Luna', alias: 'gpt6luna' },
|
|
@@ -993,6 +997,7 @@ async function handleInbound(
|
|
|
993
997
|
const access = result.access
|
|
994
998
|
const from = ctx.from!
|
|
995
999
|
const chat_id = String(ctx.chat!.id)
|
|
1000
|
+
text = stripCommandTarget(text, botUsername)
|
|
996
1001
|
const msgId = ctx.message?.message_id
|
|
997
1002
|
|
|
998
1003
|
// Permission-reply intercept: if this looks like "yes xxxxx" for a
|
|
@@ -1144,10 +1149,16 @@ async function handleInbound(
|
|
|
1144
1149
|
|
|
1145
1150
|
if (!SEND_ONLY) {
|
|
1146
1151
|
|
|
1147
|
-
// Commands are DM-only.
|
|
1148
|
-
// /status to other
|
|
1149
|
-
//
|
|
1150
|
-
//
|
|
1152
|
+
// Commands are DM-only. Answering them in a group would leak pairing codes via
|
|
1153
|
+
// /status to other members and act for the whole agent from a shared chat, so
|
|
1154
|
+
// an allowed group gets a pointer to the direct message instead; a group the
|
|
1155
|
+
// gate does not allow stays silent, so its members learn nothing. /stop, /new,
|
|
1156
|
+
// /rename and /pstack_setup are forwarded and answered by the gateway there.
|
|
1157
|
+
bot.on('message:text', async (ctx, next) => {
|
|
1158
|
+
const reply = groupCommandReply(loadAccess(), { chatType: ctx.chat.type, chatId: String(ctx.chat.id), fromId: String(ctx.from.id), text: ctx.message.text, botUsername, cliEnabled: INTERACTIVE_CLI_ENABLED })
|
|
1159
|
+
if (reply === undefined) return next()
|
|
1160
|
+
await ctx.reply(reply, { reply_parameters: { message_id: ctx.message.message_id } }).catch(() => {})
|
|
1161
|
+
})
|
|
1151
1162
|
|
|
1152
1163
|
bot.command('start', async ctx => {
|
|
1153
1164
|
if (ctx.chat?.type !== 'private') return
|
|
@@ -1447,6 +1458,24 @@ bot.command('tasks',async ctx=>{
|
|
|
1447
1458
|
catch{await ctx.reply('Could not load tasks. Please try /tasks again.')}
|
|
1448
1459
|
})
|
|
1449
1460
|
|
|
1461
|
+
// /pstack-setup (Telegram also takes /pstack_setup: command names cannot contain '-').
|
|
1462
|
+
// Bare, it opens the button menu the gateway renders; with arguments it falls
|
|
1463
|
+
// through to the gateway's text command. Groups fall through too: changes are
|
|
1464
|
+
// direct-message only.
|
|
1465
|
+
async function pstackMenuRequest(chatId:string,userId:string,data:string) {
|
|
1466
|
+
if(!CALLBACK_URL_BASE)throw Error('not_configured')
|
|
1467
|
+
const response=await fetch(CALLBACK_URL_BASE+'/command',{method:'POST',headers:{'Content-Type':'application/json'},signal:AbortSignal.timeout(20000),body:JSON.stringify({command:'telegram_pstack',chat_id:chatId,payload:{user_id:userId,data}})})
|
|
1468
|
+
const result=await response.json() as {success?:boolean}
|
|
1469
|
+
if(!response.ok||!result.success)throw Error('unavailable')
|
|
1470
|
+
return pstackMenuMarkup(result)
|
|
1471
|
+
}
|
|
1472
|
+
bot.hears(PSTACK_MENU_COMMAND,async (ctx,next)=>{
|
|
1473
|
+
if(ctx.chat?.type!=='private'||!CALLBACK_URL_BASE)return next()
|
|
1474
|
+
if(!isCallbackAuthorized(ctx))return
|
|
1475
|
+
try{const menu=await pstackMenuRequest(String(ctx.chat.id),String(ctx.from!.id),'ps:h');await ctx.reply(menu.text,{reply_markup:menu.reply_markup})}
|
|
1476
|
+
catch{await ctx.reply('pstack setup unavailable. Please try /pstack_setup again.')}
|
|
1477
|
+
})
|
|
1478
|
+
|
|
1450
1479
|
async function taskStopRequest(chatId: string, userId: string, menuId?: string, index?: number) {
|
|
1451
1480
|
if (!CALLBACK_URL_BASE) throw new Error('not_configured')
|
|
1452
1481
|
const response=await fetch(CALLBACK_URL_BASE+'/command', {
|
|
@@ -1502,6 +1531,28 @@ bot.command('voice', async ctx => {
|
|
|
1502
1531
|
} catch { await ctx.reply('Voice settings are unavailable. Check the agent TTS configuration.') }
|
|
1503
1532
|
})
|
|
1504
1533
|
|
|
1534
|
+
// /call — open a live voice call as a Telegram Mini App bound to this DM. The
|
|
1535
|
+
// page proves the user again with signed initData before any ticket is issued.
|
|
1536
|
+
bot.command('call', async ctx => {
|
|
1537
|
+
if (ctx.chat?.type !== 'private' || !isCallbackAuthorized(ctx) || loadAccess().dmPolicy === 'disabled') return
|
|
1538
|
+
if (!ORCHESTRATION_ENABLED) { await ctx.reply('Voice calls require orchestration mode.'); return }
|
|
1539
|
+
if (!CALLBACK_URL_BASE) return
|
|
1540
|
+
try {
|
|
1541
|
+
const response = await fetch(CALLBACK_URL_BASE + '/command', {
|
|
1542
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' }, signal: AbortSignal.timeout(10000),
|
|
1543
|
+
body: JSON.stringify({ command: 'voice_call_pair', chat_id: String(ctx.chat.id), payload: { user_id: String(ctx.from!.id), ...(ctx.message?.message_thread_id ? { thread_id: String(ctx.message.message_thread_id) } : {}) } }),
|
|
1544
|
+
})
|
|
1545
|
+
const data = (await response.json()) as { success?: boolean; url?: string; error?: string }
|
|
1546
|
+
if (!data.success || !data.url) {
|
|
1547
|
+
await ctx.reply(data.error === 'not_configured' ? 'Voice calls are not configured. Set an HTTPS gateway.publicUrl in config.json first.'
|
|
1548
|
+
: data.error === 'voice_not_configured' ? 'Voice calls need voice enabled for this agent.' : 'Could not start a voice call right now.')
|
|
1549
|
+
return
|
|
1550
|
+
}
|
|
1551
|
+
// web_app buttons open the Mini App with signed initData; Telegram requires HTTPS.
|
|
1552
|
+
await ctx.reply('Tap to start a voice call. Your words and the replies also appear here.', { ...(ctx.message?.message_thread_id ? { message_thread_id: ctx.message.message_thread_id } : {}), reply_markup: new InlineKeyboard().webApp('\u{1F399} Start voice call', data.url) })
|
|
1553
|
+
} catch { await ctx.reply('Could not start a voice call right now.') }
|
|
1554
|
+
})
|
|
1555
|
+
|
|
1505
1556
|
// Reuse the /tasks live-message lifecycle: serialize reads/replacement, edit only
|
|
1506
1557
|
// changes, persist the last message across restart, and honor Telegram backoff.
|
|
1507
1558
|
const SESSION_BROWSER_FILE = join(STATE_DIR, 'session-browser.json')
|
|
@@ -1650,6 +1701,21 @@ bot.on('callback_query:data', async ctx => {
|
|
|
1650
1701
|
return
|
|
1651
1702
|
}
|
|
1652
1703
|
|
|
1704
|
+
// 🔁 Retry on a voice-note transcription failure: orch:vr:<input id> (44 bytes).
|
|
1705
|
+
const voiceRetry=/^orch:vr:([a-f0-9-]{36})$/.exec(data)
|
|
1706
|
+
if(voiceRetry){
|
|
1707
|
+
if(!ORCHESTRATION_ENABLED||ctx.callbackQuery.message?.chat.type!=='private'||!isCallbackAuthorized(ctx)){await ctx.answerCallbackQuery({text:'Not available or not authorized.'}).catch(()=>{});return}
|
|
1708
|
+
try{
|
|
1709
|
+
if(!CALLBACK_URL_BASE)throw Error('not_configured')
|
|
1710
|
+
const response=await fetch(CALLBACK_URL_BASE+'/command',{method:'POST',headers:{'Content-Type':'application/json'},signal:AbortSignal.timeout(10000),body:JSON.stringify({command:'telegram_voice_retry',chat_id:String(ctx.callbackQuery.message.chat.id),payload:{user_id:String(ctx.from.id),input_id:voiceRetry[1]}})})
|
|
1711
|
+
const result=await response.json() as {success?:boolean;text?:string;error?:string}
|
|
1712
|
+
await ctx.answerCallbackQuery({text:((response.ok&&result.success?result.text:result.error)??'Voice note cannot be retried.').slice(0,180)}).catch(()=>{})
|
|
1713
|
+
// Any answer settles this button: a retry was queued, or the note cannot be retried.
|
|
1714
|
+
if(response.ok&&result.success)await ctx.editMessageReplyMarkup({reply_markup:undefined}).catch(()=>{})
|
|
1715
|
+
}catch{await ctx.answerCallbackQuery({text:'Voice retry unavailable. Please send the voice note again.'}).catch(()=>{})}
|
|
1716
|
+
return
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1653
1719
|
const voicePicker=/^(voicepick|voicepage|voicegroup|voicedismiss):([a-f0-9-]{36})(?::(male|female|neutral|unspecified|back))?(?::(\d+))?$/.exec(data)
|
|
1654
1720
|
if(voicePicker){
|
|
1655
1721
|
if(!ORCHESTRATION_ENABLED||ctx.callbackQuery.message?.chat.type!=='private'||!isCallbackAuthorized(ctx)){await ctx.answerCallbackQuery({text:'Not available or not authorized.'}).catch(()=>{});return}
|
|
@@ -1679,6 +1745,19 @@ bot.on('callback_query:data', async ctx => {
|
|
|
1679
1745
|
return
|
|
1680
1746
|
}
|
|
1681
1747
|
|
|
1748
|
+
if(data.startsWith('ps:')){
|
|
1749
|
+
if(ctx.callbackQuery.message?.chat.type!=='private'||!isCallbackAuthorized(ctx)){await ctx.answerCallbackQuery({text:'Not authorized.'}).catch(()=>{});return}
|
|
1750
|
+
try{
|
|
1751
|
+
const menu=await pstackMenuRequest(String(ctx.callbackQuery.message.chat.id),String(ctx.from.id),data)
|
|
1752
|
+
await ctx.answerCallbackQuery(menu.toast?{text:menu.toast}:undefined).catch(()=>{})
|
|
1753
|
+
if(menu.closed){await ctx.deleteMessage().catch(()=>{});return}
|
|
1754
|
+
// Turn on/off ends the menu with its result and no buttons, as a /voice choice does.
|
|
1755
|
+
if(menu.done){await confirmSelection(ctx,menu.text).catch(()=>{});return}
|
|
1756
|
+
await ctx.editMessageText(menu.text,{reply_markup:menu.reply_markup}).catch(()=>{})
|
|
1757
|
+
}catch{await ctx.answerCallbackQuery({text:'pstack setup unavailable. Use /pstack_setup again.'}).catch(()=>{})}
|
|
1758
|
+
return
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1682
1761
|
const taskStop=/^taskstop:([a-f0-9-]{36}):(\d+)$/.exec(data)
|
|
1683
1762
|
if (taskStop) {
|
|
1684
1763
|
if (!ORCHESTRATION_ENABLED) { await ctx.answerCallbackQuery({text:'Task controls require orchestration mode.'}).catch(()=>{}); return }
|