@pixelbyte-software/pixcode 1.51.2 → 1.51.4
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/CODE_OF_CONDUCT.md +41 -41
- package/CONTRIBUTING.md +155 -155
- package/LICENSE +718 -718
- package/README.de.md +169 -169
- package/README.ja.md +167 -167
- package/README.ko.md +167 -167
- package/README.md +419 -419
- package/README.ru.md +169 -169
- package/README.tr.md +298 -298
- package/README.zh-CN.md +167 -167
- package/SECURITY.md +46 -46
- package/dist/api-automation.html +110 -110
- package/dist/api-docs.html +548 -548
- package/dist/assets/index-B9N-gfOQ.css +32 -0
- package/dist/assets/{index-EN9ngyxf.js → index-HfGHXhD6.js} +175 -175
- package/dist/clear-cache.html +85 -85
- package/dist/convert-icons.md +52 -52
- package/dist/docs.html +308 -308
- package/dist/favicon.svg +8 -8
- package/dist/features.html +133 -133
- package/dist/generate-icons.js +48 -48
- package/dist/humans.txt +15 -15
- package/dist/icons/codex-white.svg +3 -3
- package/dist/icons/codex.svg +3 -3
- package/dist/icons/cursor-white.svg +11 -11
- package/dist/icons/icon-128x128.svg +9 -9
- package/dist/icons/icon-144x144.svg +9 -9
- package/dist/icons/icon-152x152.svg +9 -9
- package/dist/icons/icon-192x192.svg +9 -9
- package/dist/icons/icon-384x384.svg +9 -9
- package/dist/icons/icon-512x512.svg +9 -9
- package/dist/icons/icon-72x72.svg +9 -9
- package/dist/icons/icon-96x96.svg +9 -9
- package/dist/icons/icon-template.svg +9 -9
- package/dist/icons/qwen-logo.svg +14 -14
- package/dist/index.html +59 -59
- package/dist/landing.html +268 -268
- package/dist/llms-full.txt +119 -119
- package/dist/llms.txt +53 -53
- package/dist/logo.svg +12 -12
- package/dist/manifest.json +60 -60
- package/dist/openapi.yaml +1696 -1696
- package/dist/orchestration.html +125 -125
- package/dist/robots.txt +4 -4
- package/dist/site.css +692 -692
- package/dist/sitemap.xml +51 -51
- package/dist/sw.js +132 -132
- package/dist-server/server/cli.js +96 -96
- package/dist-server/server/daemon/manager.js +33 -33
- package/dist-server/server/daemon-manager.js +64 -64
- package/dist-server/server/database/db.js +14 -2
- package/dist-server/server/database/db.js.map +1 -1
- package/dist-server/server/index.js +191 -31
- package/dist-server/server/index.js.map +1 -1
- package/dist-server/server/middleware/auth.js +16 -5
- package/dist-server/server/middleware/auth.js.map +1 -1
- package/dist-server/server/modules/orchestration/a2a/adapters/json-event.adapter.js +84 -0
- package/dist-server/server/modules/orchestration/a2a/adapters/json-event.adapter.js.map +1 -0
- package/dist-server/server/modules/orchestration/a2a/adapters/json-event.adapter.test.js +43 -0
- package/dist-server/server/modules/orchestration/a2a/adapters/json-event.adapter.test.js.map +1 -0
- package/dist-server/server/modules/orchestration/hermes/hermes.routes.js +55 -1
- package/dist-server/server/modules/orchestration/hermes/hermes.routes.js.map +1 -1
- package/dist-server/server/modules/orchestration/index.js +1 -0
- package/dist-server/server/modules/orchestration/index.js.map +1 -1
- package/dist-server/server/routes/auth.js +12 -5
- package/dist-server/server/routes/auth.js.map +1 -1
- package/dist-server/server/routes/commands.js +25 -25
- package/dist-server/server/routes/git.js +29 -17
- package/dist-server/server/routes/git.js.map +1 -1
- package/dist-server/server/routes/live-view.js +46 -46
- package/dist-server/server/routes/platformization.js +7 -6
- package/dist-server/server/routes/platformization.js.map +1 -1
- package/dist-server/server/services/hermes-gateway.js +310 -0
- package/dist-server/server/services/hermes-gateway.js.map +1 -1
- package/dist-server/server/services/platformization.js +58 -2
- package/dist-server/server/services/platformization.js.map +1 -1
- package/dist-server/server/services/public-api-manifest.js +59 -51
- package/dist-server/server/services/public-api-manifest.js.map +1 -1
- package/package.json +222 -222
- package/scripts/fix-node-pty.js +67 -67
- package/scripts/github/create-v1.38-issues.mjs +351 -351
- package/scripts/github/create-vscode-workbench-issues.mjs +121 -121
- package/scripts/hermes/configure-pixcode-mcp.mjs +165 -163
- package/scripts/hermes/pixcode-mcp-server.mjs +1009 -958
- package/scripts/smoke/changes-panel-layout.mjs +48 -48
- package/scripts/smoke/chat-composer-fixed-layout.mjs +55 -55
- package/scripts/smoke/chat-message-timeline-order.mjs +41 -41
- package/scripts/smoke/chat-realtime-hydration.mjs +44 -44
- package/scripts/smoke/chat-session-provider-pools.mjs +35 -35
- package/scripts/smoke/chat-session-state.mjs +19 -19
- package/scripts/smoke/code-editor-theme.mjs +55 -55
- package/scripts/smoke/code-editor-vscode-engine.mjs +91 -91
- package/scripts/smoke/command-center-agent-writes.mjs +79 -79
- package/scripts/smoke/command-center-non-git.mjs +46 -46
- package/scripts/smoke/context-packet.mjs +43 -43
- package/scripts/smoke/control-room-ux-redesign.mjs +91 -91
- package/scripts/smoke/daemon-entrypoint.mjs +20 -20
- package/scripts/smoke/default-landing-routing.mjs +33 -33
- package/scripts/smoke/desktop-native-notifications.mjs +30 -30
- package/scripts/smoke/desktop-tray-icon.mjs +33 -33
- package/scripts/smoke/discord-release-workflow.mjs +24 -24
- package/scripts/smoke/git-install-update.mjs +255 -255
- package/scripts/smoke/handoff-artifact-protocol.mjs +50 -50
- package/scripts/smoke/hermes-api-install.mjs +56 -56
- package/scripts/smoke/hermes-gateway-persistence.mjs +104 -104
- package/scripts/smoke/hermes-mcp-pixcode-roundtrip.mjs +426 -367
- package/scripts/smoke/hermes-rest-chat-api.mjs +162 -162
- package/scripts/smoke/hermes-rest-chat-live.mjs +45 -45
- package/scripts/smoke/hermes-rest-codex-launch.mjs +209 -209
- package/scripts/smoke/hermes-rest-gateway.mjs +79 -70
- package/scripts/smoke/hermes-rest-live.mjs +42 -42
- package/scripts/smoke/hermes-roundtrip.mjs +167 -167
- package/scripts/smoke/hermes-settings-commands.mjs +349 -346
- package/scripts/smoke/hermes-smoke-launcher-guard.mjs +34 -34
- package/scripts/smoke/live-view-diagnostics.mjs +53 -53
- package/scripts/smoke/live-view-environment.mjs +92 -92
- package/scripts/smoke/live-view-integration.mjs +450 -450
- package/scripts/smoke/mac-desktop-runtime.mjs +37 -37
- package/scripts/smoke/mobile-tunnel-guidance.mjs +29 -29
- package/scripts/smoke/model-registry.mjs +36 -36
- package/scripts/smoke/multi-project-ui.mjs +45 -45
- package/scripts/smoke/multi-worker-slots.mjs +42 -42
- package/scripts/smoke/notification-center.mjs +87 -87
- package/scripts/smoke/notification-inapp-preference.mjs +23 -23
- package/scripts/smoke/notification-taxonomy.mjs +58 -58
- package/scripts/smoke/orchestration-api.mjs +172 -172
- package/scripts/smoke/orchestration-execution-dashboard.mjs +33 -33
- package/scripts/smoke/orchestration-live-run.mjs +176 -176
- package/scripts/smoke/orchestration-mobile-scroll.mjs +29 -29
- package/scripts/smoke/orchestration-model-sync.mjs +30 -30
- package/scripts/smoke/orchestration-permission-fallback.mjs +34 -34
- package/scripts/smoke/orchestration-runtime-guards.mjs +48 -48
- package/scripts/smoke/orchestration-user-facing-output.mjs +25 -25
- package/scripts/smoke/permission-policy.mjs +50 -50
- package/scripts/smoke/pixcode-workbench-1-48.mjs +167 -167
- package/scripts/smoke/provider-models-opencode-live.mjs +66 -66
- package/scripts/smoke/provider-rest-api.mjs +124 -124
- package/scripts/smoke/provider-selection-status.mjs +52 -52
- package/scripts/smoke/run-state-refresh.mjs +52 -52
- package/scripts/smoke/runtime-manager.mjs +99 -99
- package/scripts/smoke/shell-manual-disconnect.mjs +30 -30
- package/scripts/smoke/side-panel-editor-layout.mjs +34 -34
- package/scripts/smoke/static-root-routing.mjs +21 -21
- package/scripts/smoke/strict-handoff-compact.mjs +60 -60
- package/scripts/smoke/taskmaster-config.mjs +24 -24
- package/scripts/smoke/taskmaster-execution-telegram.mjs +3 -3
- package/scripts/smoke/taskmaster-onboarding.mjs +3 -3
- package/scripts/smoke/taskmaster-run-graph.mjs +3 -3
- package/scripts/smoke/telegram-control.mjs +242 -242
- package/scripts/smoke/tunnel-persistence.mjs +56 -56
- package/scripts/smoke/update-issue-progress.mjs +69 -69
- package/scripts/smoke/update-ux.mjs +55 -55
- package/scripts/smoke/v138-completion.mjs +132 -132
- package/scripts/smoke/v138-desktop-release-hardening.mjs +69 -69
- package/scripts/smoke/v138-diagnostics.mjs +63 -63
- package/scripts/smoke/v138-issue-planner.mjs +33 -33
- package/scripts/smoke/v143-remote-control.mjs +76 -76
- package/scripts/smoke/v144-production-loop.mjs +47 -47
- package/scripts/smoke/v145-platformization.mjs +46 -46
- package/scripts/smoke/v146-control-room-ui.mjs +150 -150
- package/scripts/smoke/version-modal-autoshow.mjs +29 -29
- package/scripts/smoke/vscode-workbench-layout.mjs +63 -63
- package/scripts/smoke/vscode-workbench-polish.mjs +461 -436
- package/scripts/smoke/workflow-fallback-replay.mjs +56 -56
- package/scripts/smoke/workflow-templates.mjs +43 -43
- package/scripts/smoke/workflow-trace-timeline.mjs +46 -46
- package/scripts/update-git-install.mjs +293 -293
- package/server/claude-sdk.js +920 -920
- package/server/cli.js +1039 -1039
- package/server/constants/config.js +4 -4
- package/server/cursor-cli.js +344 -344
- package/server/daemon/manager.js +563 -563
- package/server/daemon-manager.js +964 -964
- package/server/database/db.js +908 -895
- package/server/database/json-store.js +197 -197
- package/server/gemini-cli.js +550 -550
- package/server/gemini-response-handler.js +79 -79
- package/server/index.js +201 -30
- package/server/load-env.js +35 -35
- package/server/middleware/auth.js +171 -156
- package/server/modules/orchestration/a2a/adapter-registry.ts +108 -108
- package/server/modules/orchestration/a2a/adapters/abstract-a2a.adapter.ts +63 -63
- package/server/modules/orchestration/a2a/adapters/claude-code.adapter.ts +286 -286
- package/server/modules/orchestration/a2a/adapters/codex.adapter.ts +244 -244
- package/server/modules/orchestration/a2a/adapters/cursor.adapter.ts +249 -249
- package/server/modules/orchestration/a2a/adapters/gemini.adapter.ts +248 -248
- package/server/modules/orchestration/a2a/adapters/json-event.adapter.test.ts +60 -0
- package/server/modules/orchestration/a2a/adapters/json-event.adapter.ts +101 -0
- package/server/modules/orchestration/a2a/adapters/opencode.adapter.ts +248 -248
- package/server/modules/orchestration/a2a/adapters/qwen.adapter.ts +248 -248
- package/server/modules/orchestration/a2a/agent-card.ts +55 -55
- package/server/modules/orchestration/a2a/routes.ts +590 -590
- package/server/modules/orchestration/a2a/task-store.ts +178 -178
- package/server/modules/orchestration/a2a/types.ts +126 -126
- package/server/modules/orchestration/a2a/validator.ts +113 -113
- package/server/modules/orchestration/hermes/hermes.routes.ts +642 -583
- package/server/modules/orchestration/index.ts +101 -100
- package/server/modules/orchestration/preview/port-watcher.ts +112 -112
- package/server/modules/orchestration/preview/preview-proxy.ts +60 -60
- package/server/modules/orchestration/preview/types.ts +19 -19
- package/server/modules/orchestration/security/permission-policy.ts +401 -401
- package/server/modules/orchestration/tasks/orchestration-task-store.ts +41 -41
- package/server/modules/orchestration/tasks/orchestration-task.routes.ts +64 -64
- package/server/modules/orchestration/tasks/orchestration-task.service.ts +209 -209
- package/server/modules/orchestration/tasks/orchestration-task.types.ts +40 -40
- package/server/modules/orchestration/tasks/task-run-graph.ts +155 -155
- package/server/modules/orchestration/workflows/approval-queue.ts +106 -106
- package/server/modules/orchestration/workflows/built-in-workflows.ts +127 -127
- package/server/modules/orchestration/workflows/context-packet.ts +186 -186
- package/server/modules/orchestration/workflows/handoff-artifact.ts +175 -175
- package/server/modules/orchestration/workflows/workflow-fallback-policy.ts +161 -161
- package/server/modules/orchestration/workflows/workflow-replay.ts +254 -254
- package/server/modules/orchestration/workflows/workflow-runner.ts +2070 -2070
- package/server/modules/orchestration/workflows/workflow-store.ts +97 -97
- package/server/modules/orchestration/workflows/workflow-templates.ts +272 -272
- package/server/modules/orchestration/workflows/workflow-trace.ts +424 -424
- package/server/modules/orchestration/workflows/workflow.routes.ts +586 -586
- package/server/modules/orchestration/workflows/workflow.types.ts +111 -111
- package/server/modules/orchestration/workflows/workspace-target.ts +122 -122
- package/server/modules/orchestration/workspace/docker-workspace.ts +136 -136
- package/server/modules/orchestration/workspace/path-safety.ts +55 -55
- package/server/modules/orchestration/workspace/types.ts +52 -52
- package/server/modules/orchestration/workspace/workspace-manager.ts +102 -102
- package/server/modules/orchestration/workspace/worktree-workspace.ts +126 -126
- package/server/modules/providers/index.ts +2 -2
- package/server/modules/providers/list/claude/claude-auth.provider.ts +146 -146
- package/server/modules/providers/list/claude/claude-mcp.provider.ts +135 -135
- package/server/modules/providers/list/claude/claude-sessions.provider.ts +306 -306
- package/server/modules/providers/list/claude/claude.provider.ts +15 -15
- package/server/modules/providers/list/codex/codex-auth.provider.ts +117 -117
- package/server/modules/providers/list/codex/codex-mcp.provider.ts +135 -135
- package/server/modules/providers/list/codex/codex-sessions.provider.ts +319 -319
- package/server/modules/providers/list/codex/codex.provider.ts +15 -15
- package/server/modules/providers/list/cursor/cursor-auth.provider.ts +147 -147
- package/server/modules/providers/list/cursor/cursor-mcp.provider.ts +108 -108
- package/server/modules/providers/list/cursor/cursor-sessions.provider.ts +421 -421
- package/server/modules/providers/list/cursor/cursor.provider.ts +15 -15
- package/server/modules/providers/list/gemini/gemini-auth.provider.ts +173 -173
- package/server/modules/providers/list/gemini/gemini-mcp.provider.ts +110 -110
- package/server/modules/providers/list/gemini/gemini-sessions.provider.ts +227 -227
- package/server/modules/providers/list/gemini/gemini.provider.ts +15 -15
- package/server/modules/providers/list/opencode/opencode-auth.provider.ts +131 -131
- package/server/modules/providers/list/opencode/opencode-mcp.provider.ts +126 -126
- package/server/modules/providers/list/opencode/opencode-sessions.provider.ts +286 -286
- package/server/modules/providers/list/opencode/opencode.provider.ts +29 -29
- package/server/modules/providers/list/qwen/qwen-auth.provider.ts +146 -146
- package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -114
- package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +265 -265
- package/server/modules/providers/list/qwen/qwen.provider.ts +21 -21
- package/server/modules/providers/provider.registry.ts +40 -40
- package/server/modules/providers/provider.routes.ts +944 -944
- package/server/modules/providers/services/mcp.service.ts +86 -86
- package/server/modules/providers/services/provider-auth.service.ts +26 -26
- package/server/modules/providers/services/sessions.service.ts +45 -45
- package/server/modules/providers/shared/base/abstract.provider.ts +20 -20
- package/server/modules/providers/shared/mcp/mcp.provider.ts +151 -151
- package/server/modules/providers/shared/provider-configs.ts +142 -142
- package/server/modules/providers/tests/mcp.test.ts +293 -293
- package/server/openai-codex.js +462 -462
- package/server/opencode-cli.js +491 -491
- package/server/opencode-response-handler.js +111 -111
- package/server/projects.js +3008 -3008
- package/server/qwen-code-cli.js +410 -410
- package/server/qwen-response-handler.js +73 -73
- package/server/routes/agent.js +1435 -1435
- package/server/routes/auth.js +154 -146
- package/server/routes/codex.js +20 -20
- package/server/routes/commands.js +570 -570
- package/server/routes/cursor.js +61 -61
- package/server/routes/diagnostics.js +41 -41
- package/server/routes/gemini.js +25 -25
- package/server/routes/git.js +1650 -1635
- package/server/routes/live-view.js +411 -411
- package/server/routes/mcp-utils.js +13 -13
- package/server/routes/messages.js +62 -62
- package/server/routes/network.js +125 -125
- package/server/routes/platformization.js +198 -197
- package/server/routes/plugins.js +320 -320
- package/server/routes/production-agent-loop.js +90 -90
- package/server/routes/projects.js +917 -917
- package/server/routes/public-api.js +34 -34
- package/server/routes/qwen.js +27 -27
- package/server/routes/remote.js +55 -55
- package/server/routes/settings.js +321 -321
- package/server/routes/telegram.js +140 -140
- package/server/routes/user.js +125 -125
- package/server/routes/webhooks.js +63 -63
- package/server/services/control-room.js +102 -102
- package/server/services/diagnostics.js +165 -165
- package/server/services/external-access.js +375 -375
- package/server/services/hermes-gateway.js +1562 -1247
- package/server/services/hermes-install-jobs.js +729 -729
- package/server/services/install-jobs.js +715 -715
- package/server/services/live-view.js +956 -956
- package/server/services/managed-runtimes.js +493 -493
- package/server/services/model-registry.js +144 -144
- package/server/services/notification-orchestrator.js +365 -365
- package/server/services/notification-taxonomy.js +204 -204
- package/server/services/platformization.js +844 -779
- package/server/services/production-agent-loop.js +248 -248
- package/server/services/provider-cli-versions.js +149 -149
- package/server/services/provider-credentials.js +189 -189
- package/server/services/provider-models.js +396 -396
- package/server/services/public-api-manifest.js +190 -182
- package/server/services/remote-connection.js +127 -127
- package/server/services/runtime-manager.js +323 -323
- package/server/services/startup-update.js +234 -234
- package/server/services/telegram/bot.js +331 -331
- package/server/services/telegram/control-center.js +979 -979
- package/server/services/telegram/telegram-http-client.js +151 -151
- package/server/services/telegram/translations.js +340 -340
- package/server/services/vapid-keys.js +36 -36
- package/server/services/webhooks.js +216 -216
- package/server/sessionManager.js +225 -225
- package/server/shared/interfaces.ts +54 -54
- package/server/shared/types.ts +172 -172
- package/server/shared/utils.ts +193 -193
- package/server/tsconfig.json +36 -36
- package/server/utils/colors.js +21 -21
- package/server/utils/commandParser.js +305 -305
- package/server/utils/frontmatter.js +18 -18
- package/server/utils/gitConfig.js +34 -34
- package/server/utils/plugin-loader.js +457 -457
- package/server/utils/plugin-process-manager.js +185 -185
- package/server/utils/port-access.js +209 -209
- package/server/utils/runtime-paths.js +37 -37
- package/server/utils/url-detection.js +71 -71
- package/server/vite-daemon.js +79 -79
- package/shared/modelConstants.js +161 -161
- package/shared/networkHosts.js +22 -22
- package/dist/assets/index-DMz0zv6T.css +0 -32
|
@@ -1,979 +1,979 @@
|
|
|
1
|
-
import crypto from 'node:crypto';
|
|
2
|
-
|
|
3
|
-
import { apiKeysDb, telegramLinksDb } from '../../database/db.js';
|
|
4
|
-
import { getProjects } from '../../projects.js';
|
|
5
|
-
import { getStaticProviderModels } from '../model-registry.js';
|
|
6
|
-
|
|
7
|
-
import { SUPPORTED_LANGUAGES, t } from './translations.js';
|
|
8
|
-
|
|
9
|
-
const PROVIDERS = ['claude', 'cursor', 'codex', 'gemini', 'qwen', 'opencode'];
|
|
10
|
-
const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
|
|
11
|
-
const CALLBACK_TTL_MS = 10 * 60 * 1000;
|
|
12
|
-
const MAX_TELEGRAM_TEXT = 3600;
|
|
13
|
-
const callbackActions = new Map();
|
|
14
|
-
const runMonitors = new Map();
|
|
15
|
-
|
|
16
|
-
const MODEL_FALLBACKS = Object.fromEntries(
|
|
17
|
-
PROVIDERS.map((provider) => [provider, getStaticProviderModels(provider)]),
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
const AUTH_HELP = {
|
|
21
|
-
claude: '`claude login`',
|
|
22
|
-
cursor: '`cursor-agent login`',
|
|
23
|
-
codex: '`codex login`',
|
|
24
|
-
gemini: '`gemini auth login`',
|
|
25
|
-
qwen: '`qwen auth`',
|
|
26
|
-
opencode: '`opencode auth login`',
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const CONTROL_COMMANDS = new Set([
|
|
30
|
-
'/menu',
|
|
31
|
-
'/start',
|
|
32
|
-
'/help',
|
|
33
|
-
'/projects',
|
|
34
|
-
'/provider',
|
|
35
|
-
'/model',
|
|
36
|
-
'/models',
|
|
37
|
-
'/workflows',
|
|
38
|
-
'/orchestration',
|
|
39
|
-
'/runs',
|
|
40
|
-
'/approvals',
|
|
41
|
-
'/controlroom',
|
|
42
|
-
'/control-room',
|
|
43
|
-
'/webhooks',
|
|
44
|
-
'/sessions',
|
|
45
|
-
'/newchat',
|
|
46
|
-
'/settings',
|
|
47
|
-
'/install',
|
|
48
|
-
'/auth',
|
|
49
|
-
'/control',
|
|
50
|
-
'/progress',
|
|
51
|
-
'/chat',
|
|
52
|
-
'/workflow',
|
|
53
|
-
'/orchestrate',
|
|
54
|
-
'/cancel',
|
|
55
|
-
'menu',
|
|
56
|
-
]);
|
|
57
|
-
|
|
58
|
-
function compact(text, max = 80) {
|
|
59
|
-
const value = String(text || '').replace(/\s+/g, ' ').trim();
|
|
60
|
-
return value.length > max ? `${value.slice(0, max - 1)}…` : value;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function truncate(text, max = MAX_TELEGRAM_TEXT) {
|
|
64
|
-
const value = String(text || '').trim();
|
|
65
|
-
return value.length > max ? `${value.slice(0, max - 20)}\n\n…truncated…` : value;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function languageFor(link) {
|
|
69
|
-
return SUPPORTED_LANGUAGES.includes(link?.language) ? link.language : 'en';
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function normalizeCommandToken(commandRaw = '') {
|
|
73
|
-
return String(commandRaw || '')
|
|
74
|
-
.trim()
|
|
75
|
-
.toLowerCase()
|
|
76
|
-
.replace(/@[^@\s]+$/, '');
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function getTelegramControlCommand(text = '') {
|
|
80
|
-
const [commandRaw = ''] = String(text || '').trim().split(/\s+/);
|
|
81
|
-
const command = normalizeCommandToken(commandRaw);
|
|
82
|
-
return CONTROL_COMMANDS.has(command) ? command : null;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function isTelegramControlCommand(text = '') {
|
|
86
|
-
return Boolean(getTelegramControlCommand(text));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function getState(userId) {
|
|
90
|
-
return telegramLinksDb.getControlState(userId);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function updateTelegramControlState(userId, patch) {
|
|
94
|
-
return telegramLinksDb.updateControlState(userId, patch);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function registerAction(action, payload = {}) {
|
|
98
|
-
const id = crypto.randomBytes(8).toString('hex');
|
|
99
|
-
callbackActions.set(id, {
|
|
100
|
-
action,
|
|
101
|
-
payload,
|
|
102
|
-
expiresAt: Date.now() + CALLBACK_TTL_MS,
|
|
103
|
-
});
|
|
104
|
-
return `tc:${id}`;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function readAction(data) {
|
|
108
|
-
if (typeof data !== 'string' || !data.startsWith('tc:')) return null;
|
|
109
|
-
const id = data.slice(3);
|
|
110
|
-
const entry = callbackActions.get(id);
|
|
111
|
-
if (!entry) return null;
|
|
112
|
-
if (entry.expiresAt < Date.now()) {
|
|
113
|
-
callbackActions.delete(id);
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
return entry;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function button(text, action, payload = {}) {
|
|
120
|
-
return { text, callback_data: registerAction(action, payload) };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function rows(buttons, width = 2) {
|
|
124
|
-
const out = [];
|
|
125
|
-
for (let i = 0; i < buttons.length; i += width) out.push(buttons.slice(i, i + width));
|
|
126
|
-
return out;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async function send(bot, chatId, text, options = {}) {
|
|
130
|
-
const { editMessageId, ...telegramOptions } = options;
|
|
131
|
-
const extra = {
|
|
132
|
-
parse_mode: 'Markdown',
|
|
133
|
-
disable_web_page_preview: true,
|
|
134
|
-
...telegramOptions,
|
|
135
|
-
};
|
|
136
|
-
const messageText = truncate(text);
|
|
137
|
-
if (editMessageId && typeof bot.editMessageText === 'function') {
|
|
138
|
-
try {
|
|
139
|
-
return await bot.editMessageText(messageText, {
|
|
140
|
-
chat_id: chatId,
|
|
141
|
-
message_id: editMessageId,
|
|
142
|
-
...extra,
|
|
143
|
-
});
|
|
144
|
-
} catch (err) {
|
|
145
|
-
const description = err?.response?.body?.description || err?.message || '';
|
|
146
|
-
if (/message is not modified/i.test(description)) return null;
|
|
147
|
-
console.warn('[telegram-control] editMessageText failed:', description || err);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
try {
|
|
151
|
-
return await bot.sendMessage(chatId, messageText, extra);
|
|
152
|
-
} catch {
|
|
153
|
-
const fallback = { ...extra };
|
|
154
|
-
delete fallback.parse_mode;
|
|
155
|
-
return bot.sendMessage(chatId, messageText, fallback);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function localApiBase() {
|
|
160
|
-
const port = process.env.SERVER_PORT || process.env.PORT || '3001';
|
|
161
|
-
return `http://127.0.0.1:${port}`;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function getOrCreateTelegramApiKey(userId) {
|
|
165
|
-
const existing = apiKeysDb
|
|
166
|
-
.getApiKeys(userId)
|
|
167
|
-
.find((key) => key.key_name === 'Telegram Control' && Boolean(key.is_active));
|
|
168
|
-
if (existing?.api_key) return existing.api_key;
|
|
169
|
-
return apiKeysDb.createApiKey(userId, 'Telegram Control').apiKey;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async function localApi(userId, path, { method = 'GET', body } = {}) {
|
|
173
|
-
const apiKey = getOrCreateTelegramApiKey(userId);
|
|
174
|
-
const response = await fetch(`${localApiBase()}${path}`, {
|
|
175
|
-
method,
|
|
176
|
-
headers: {
|
|
177
|
-
'Content-Type': 'application/json',
|
|
178
|
-
'X-API-Key': apiKey,
|
|
179
|
-
},
|
|
180
|
-
body: body === undefined ? undefined : JSON.stringify(body),
|
|
181
|
-
});
|
|
182
|
-
const data = await response.json().catch(() => ({}));
|
|
183
|
-
if (!response.ok) {
|
|
184
|
-
const error = data?.error?.message || data?.error || `HTTP ${response.status}`;
|
|
185
|
-
throw new Error(typeof error === 'string' ? error : JSON.stringify(error));
|
|
186
|
-
}
|
|
187
|
-
return data?.data ?? data;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function checked(label) {
|
|
191
|
-
return `${label} ✓`;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function stateSummary(lang, state) {
|
|
195
|
-
return [
|
|
196
|
-
`${t(lang, 'control.summary.project')}: ${state.selectedProjectName || t(lang, 'control.notSelected')}`,
|
|
197
|
-
`${t(lang, 'control.summary.provider')}: ${state.selectedProvider}${state.selectedModel ? ` / ${state.selectedModel}` : ''}`,
|
|
198
|
-
`${t(lang, 'control.summary.workflow')}: ${state.selectedWorkflowId || t(lang, 'control.notSelected')}`,
|
|
199
|
-
`${t(lang, 'control.summary.progress')}: ${state.progressMode}`,
|
|
200
|
-
].join('\n');
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function mainMenuKeyboard(lang) {
|
|
204
|
-
return [
|
|
205
|
-
[button(t(lang, 'control.button.projects'), 'projects'), button(t(lang, 'control.button.provider'), 'providers')],
|
|
206
|
-
[button(t(lang, 'control.button.models'), 'models'), button(t(lang, 'control.button.workflows'), 'workflows')],
|
|
207
|
-
[button(t(lang, 'control.button.runs'), 'runs'), button(t(lang, 'control.button.approvals'), 'approvals')],
|
|
208
|
-
[button(t(lang, 'control.button.controlRoom'), 'control_room'), button(t(lang, 'control.button.webhooks'), 'webhooks')],
|
|
209
|
-
[button(t(lang, 'control.button.sessions'), 'sessions'), button(t(lang, 'control.button.newChat'), 'new_chat')],
|
|
210
|
-
[button(t(lang, 'control.button.install'), 'install_menu'), button(t(lang, 'control.button.auth'), 'auth_menu')],
|
|
211
|
-
[button(t(lang, 'control.button.settings'), 'settings')],
|
|
212
|
-
];
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export async function showMainMenu({ bot, chatId, link, editMessageId, notice }) {
|
|
216
|
-
const lang = languageFor(link);
|
|
217
|
-
const state = getState(link.user_id);
|
|
218
|
-
const prefix = notice ? `${notice}\n\n` : '';
|
|
219
|
-
await send(bot, chatId, `${prefix}${t(lang, 'control.menu')}\n\n${stateSummary(lang, state)}`, {
|
|
220
|
-
editMessageId,
|
|
221
|
-
reply_markup: { inline_keyboard: mainMenuKeyboard(lang) },
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async function showHelp({ bot, chatId, link }) {
|
|
226
|
-
const lang = languageFor(link);
|
|
227
|
-
await send(bot, chatId, t(lang, 'control.help'));
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
async function showCommandPalette({ bot, chatId, link, editMessageId, unknown = false }) {
|
|
231
|
-
const lang = languageFor(link);
|
|
232
|
-
const prefix = unknown ? `${t(lang, 'control.unknownCommand')}\n\n` : '';
|
|
233
|
-
await send(bot, chatId, `${prefix}${t(lang, 'control.help')}\n\n${t(lang, 'control.examples')}`, {
|
|
234
|
-
editMessageId,
|
|
235
|
-
reply_markup: { inline_keyboard: mainMenuKeyboard(lang) },
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
async function listProjects() {
|
|
240
|
-
const projects = await getProjects();
|
|
241
|
-
return projects.slice(0, 20);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
async function showProjectMenu({ bot, chatId, link, editMessageId }) {
|
|
245
|
-
const lang = languageFor(link);
|
|
246
|
-
const projects = await listProjects();
|
|
247
|
-
if (projects.length === 0) {
|
|
248
|
-
await send(bot, chatId, t(lang, 'control.noProjects'), { editMessageId });
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
const buttons = projects.map((project, index) => button(
|
|
253
|
-
`${index + 1}. ${compact(project.displayName || project.name, 34)}`,
|
|
254
|
-
'project_select',
|
|
255
|
-
{
|
|
256
|
-
name: project.name,
|
|
257
|
-
path: project.fullPath || project.path,
|
|
258
|
-
displayName: project.displayName || project.name,
|
|
259
|
-
},
|
|
260
|
-
));
|
|
261
|
-
await send(bot, chatId, t(lang, 'control.pickProject'), {
|
|
262
|
-
editMessageId,
|
|
263
|
-
reply_markup: { inline_keyboard: rows(buttons, 1) },
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export async function showProviderMenu({ bot, chatId, link, editMessageId }) {
|
|
268
|
-
const lang = languageFor(link);
|
|
269
|
-
const state = getState(link.user_id);
|
|
270
|
-
const buttons = PROVIDERS.map((provider) => button(
|
|
271
|
-
provider === state.selectedProvider ? checked(provider) : provider,
|
|
272
|
-
'provider_select',
|
|
273
|
-
{ provider },
|
|
274
|
-
));
|
|
275
|
-
await send(bot, chatId, t(lang, 'control.pickProvider'), {
|
|
276
|
-
editMessageId,
|
|
277
|
-
reply_markup: { inline_keyboard: rows(buttons, 2) },
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
async function readProviderModels(userId, provider, refresh = false) {
|
|
282
|
-
try {
|
|
283
|
-
const data = await localApi(userId, `/api/providers/${provider}/models${refresh ? '?refresh=1' : ''}`);
|
|
284
|
-
const models = Array.isArray(data?.models) ? data.models : [];
|
|
285
|
-
return models.length > 0 ? models : MODEL_FALLBACKS[provider] || [];
|
|
286
|
-
} catch {
|
|
287
|
-
return MODEL_FALLBACKS[provider] || [];
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
export async function showModelMenu({ bot, chatId, link, refresh = false, editMessageId }) {
|
|
292
|
-
const lang = languageFor(link);
|
|
293
|
-
const state = getState(link.user_id);
|
|
294
|
-
const models = await readProviderModels(link.user_id, state.selectedProvider, refresh);
|
|
295
|
-
const modelButtons = models.slice(0, 18).map((model) => {
|
|
296
|
-
const value = model.value || model.id || model.label;
|
|
297
|
-
const label = compact(model.label || value, 38);
|
|
298
|
-
return button(value === state.selectedModel ? checked(label) : label, 'model_select', { model: value });
|
|
299
|
-
});
|
|
300
|
-
modelButtons.push(button(t(lang, 'control.button.refreshModels'), 'models_refresh'));
|
|
301
|
-
modelButtons.push(button(t(lang, 'control.button.clearModel'), 'model_select', { model: null }));
|
|
302
|
-
await send(bot, chatId, t(lang, 'control.modelsFor', { provider: state.selectedProvider }), {
|
|
303
|
-
editMessageId,
|
|
304
|
-
reply_markup: { inline_keyboard: rows(modelButtons, 1) },
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
export async function showWorkflowMenu({ bot, chatId, link, editMessageId }) {
|
|
309
|
-
const lang = languageFor(link);
|
|
310
|
-
const workflows = await localApi(link.user_id, '/api/orchestration/workflows');
|
|
311
|
-
const list = Array.isArray(workflows?.workflows) ? workflows.workflows : [];
|
|
312
|
-
const state = getState(link.user_id);
|
|
313
|
-
const workflowButtons = list.map((workflow) => button(
|
|
314
|
-
workflow.id === state.selectedWorkflowId ? checked(compact(workflow.name || workflow.id, 34)) : compact(workflow.name || workflow.id, 34),
|
|
315
|
-
'workflow_select',
|
|
316
|
-
{ workflowId: workflow.id, name: workflow.name || workflow.id },
|
|
317
|
-
));
|
|
318
|
-
workflowButtons.push(button(t(lang, 'control.button.runWorkflow'), 'workflow_prompt'));
|
|
319
|
-
await send(bot, chatId, t(lang, 'control.pickWorkflow'), {
|
|
320
|
-
editMessageId,
|
|
321
|
-
reply_markup: { inline_keyboard: rows(workflowButtons, 1) },
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
async function showRuns({ bot, chatId, link, editMessageId }) {
|
|
326
|
-
const lang = languageFor(link);
|
|
327
|
-
const data = await localApi(link.user_id, '/api/orchestration/workflows/runs?limit=10');
|
|
328
|
-
const runs = Array.isArray(data?.runs) ? data.runs : [];
|
|
329
|
-
if (runs.length === 0) {
|
|
330
|
-
await send(bot, chatId, t(lang, 'control.noRuns'), { editMessageId });
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
const buttons = runs.map((run) => button(
|
|
334
|
-
`${run.status} ${compact(run.workflowId || run.id, 28)}`,
|
|
335
|
-
'run_detail',
|
|
336
|
-
{ runId: run.id },
|
|
337
|
-
));
|
|
338
|
-
await send(bot, chatId, t(lang, 'control.recentRuns'), {
|
|
339
|
-
editMessageId,
|
|
340
|
-
reply_markup: { inline_keyboard: rows(buttons, 1) },
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
async function showApprovalQueue({ bot, chatId, link, editMessageId }) {
|
|
345
|
-
const lang = languageFor(link);
|
|
346
|
-
const data = await localApi(link.user_id, '/api/orchestration/workflows/approvals');
|
|
347
|
-
const approvals = Array.isArray(data?.pendingApprovals) ? data.pendingApprovals : [];
|
|
348
|
-
if (approvals.length === 0) {
|
|
349
|
-
await send(bot, chatId, t(lang, 'control.noApprovals'), { editMessageId });
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
const keyboard = [];
|
|
354
|
-
const lines = approvals.slice(0, 8).map((approval, index) => {
|
|
355
|
-
const label = compact(approval.summary || approval.reason || approval.id, 70);
|
|
356
|
-
keyboard.push([
|
|
357
|
-
button(t(lang, 'control.button.approve'), 'approval_decide', { approvalId: approval.id, allow: true }),
|
|
358
|
-
button(t(lang, 'control.button.deny'), 'approval_decide', { approvalId: approval.id, allow: false }),
|
|
359
|
-
]);
|
|
360
|
-
return `${index + 1}. ${label}\nRun: ${approval.runId}`;
|
|
361
|
-
});
|
|
362
|
-
keyboard.push([button(t(lang, 'control.button.refresh'), 'approvals'), button(t(lang, 'control.button.mainMenu'), 'menu')]);
|
|
363
|
-
await send(bot, chatId, `${t(lang, 'control.approvalQueue')}\n\n${lines.join('\n\n')}`, {
|
|
364
|
-
editMessageId,
|
|
365
|
-
reply_markup: { inline_keyboard: keyboard },
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
async function showControlRoom({ bot, chatId, link, editMessageId }) {
|
|
370
|
-
const lang = languageFor(link);
|
|
371
|
-
const data = await localApi(link.user_id, '/api/remote/control-room');
|
|
372
|
-
const snapshot = data?.controlRoom || data;
|
|
373
|
-
const projects = Array.isArray(snapshot?.projects) ? snapshot.projects : [];
|
|
374
|
-
const totals = snapshot?.totals || {};
|
|
375
|
-
const lines = projects.map((project, index) => [
|
|
376
|
-
`${index + 1}. ${compact(project.name || project.id, 44)}`,
|
|
377
|
-
`Runs: ${project.activeRunCount || 0} active / ${project.failedRunCount || 0} failed`,
|
|
378
|
-
`Approvals: ${project.pendingApprovalCount || 0}`,
|
|
379
|
-
].join('\n'));
|
|
380
|
-
await send(bot, chatId, [
|
|
381
|
-
t(lang, 'control.controlRoomTitle'),
|
|
382
|
-
'',
|
|
383
|
-
`Projects: ${totals.projects || 0}`,
|
|
384
|
-
`Active runs: ${totals.activeRuns || 0}`,
|
|
385
|
-
`Pending approvals: ${totals.pendingApprovals || 0}`,
|
|
386
|
-
'',
|
|
387
|
-
lines.join('\n\n') || t(lang, 'control.noProjects'),
|
|
388
|
-
].join('\n'), {
|
|
389
|
-
editMessageId,
|
|
390
|
-
reply_markup: {
|
|
391
|
-
inline_keyboard: [
|
|
392
|
-
[button(t(lang, 'control.button.approvals'), 'approvals'), button(t(lang, 'control.button.runs'), 'runs')],
|
|
393
|
-
[button(t(lang, 'control.button.mainMenu'), 'menu')],
|
|
394
|
-
],
|
|
395
|
-
},
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
async function showWebhookMenu({ bot, chatId, link, editMessageId }) {
|
|
400
|
-
const lang = languageFor(link);
|
|
401
|
-
const data = await localApi(link.user_id, '/api/webhooks');
|
|
402
|
-
const webhooks = Array.isArray(data?.webhooks) ? data.webhooks : [];
|
|
403
|
-
const lines = webhooks.slice(0, 10).map((webhook, index) => (
|
|
404
|
-
`${index + 1}. ${webhook.enabled ? 'on' : 'off'} ${compact(webhook.name || webhook.url, 50)}\n${compact(webhook.events?.join(', ') || webhook.url, 90)}`
|
|
405
|
-
));
|
|
406
|
-
await send(bot, chatId, [
|
|
407
|
-
t(lang, 'control.webhookTitle'),
|
|
408
|
-
'',
|
|
409
|
-
lines.join('\n\n') || t(lang, 'control.noWebhooks'),
|
|
410
|
-
].join('\n'), {
|
|
411
|
-
editMessageId,
|
|
412
|
-
reply_markup: {
|
|
413
|
-
inline_keyboard: [
|
|
414
|
-
[button(t(lang, 'control.button.refresh'), 'webhooks'), button(t(lang, 'control.button.mainMenu'), 'menu')],
|
|
415
|
-
],
|
|
416
|
-
},
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
async function showSessions({ bot, chatId, link, editMessageId }) {
|
|
421
|
-
const lang = languageFor(link);
|
|
422
|
-
const state = getState(link.user_id);
|
|
423
|
-
if (!state.selectedProjectName) {
|
|
424
|
-
await send(bot, chatId, t(lang, 'control.selectProjectFirst'), { editMessageId });
|
|
425
|
-
return;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
const data = await localApi(link.user_id, `/api/projects/${encodeURIComponent(state.selectedProjectName)}/sessions?limit=10&offset=0`);
|
|
429
|
-
const sessions = Array.isArray(data?.sessions) ? data.sessions : [];
|
|
430
|
-
if (sessions.length === 0) {
|
|
431
|
-
await send(bot, chatId, t(lang, 'control.noSessions'), { editMessageId });
|
|
432
|
-
return;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
const lines = sessions.slice(0, 10).map((session, index) =>
|
|
436
|
-
`${index + 1}. ${compact(session.summary || session.id || session.sessionId, 70)}`
|
|
437
|
-
);
|
|
438
|
-
await send(bot, chatId, `${t(lang, 'control.recentSessions')}\n\n${lines.join('\n')}`, {
|
|
439
|
-
editMessageId,
|
|
440
|
-
reply_markup: {
|
|
441
|
-
inline_keyboard: [
|
|
442
|
-
[button(t(lang, 'control.button.newChat'), 'new_chat')],
|
|
443
|
-
[button(t(lang, 'control.button.mainMenu'), 'menu')],
|
|
444
|
-
],
|
|
445
|
-
},
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
async function startNewChat({ bot, chatId, link, editMessageId }) {
|
|
450
|
-
const lang = languageFor(link);
|
|
451
|
-
updateTelegramControlState(link.user_id, { awaiting: { type: 'agent_prompt' } });
|
|
452
|
-
await send(bot, chatId, t(lang, 'control.newChatReady'), {
|
|
453
|
-
editMessageId,
|
|
454
|
-
reply_markup: { inline_keyboard: [[button(t(lang, 'control.button.mainMenu'), 'menu')]] },
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
function extractAssistantText(response) {
|
|
459
|
-
const messages = Array.isArray(response?.messages) ? response.messages : [];
|
|
460
|
-
const chunks = [];
|
|
461
|
-
for (const message of messages) {
|
|
462
|
-
if (typeof message?.content === 'string') chunks.push(message.content);
|
|
463
|
-
const content = message?.message?.content;
|
|
464
|
-
if (Array.isArray(content)) {
|
|
465
|
-
for (const part of content) {
|
|
466
|
-
if (part?.type === 'text' && typeof part.text === 'string') chunks.push(part.text);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
return chunks.join('\n\n').trim();
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
async function runAgent({ bot, chatId, link, prompt }) {
|
|
474
|
-
const lang = languageFor(link);
|
|
475
|
-
const state = getState(link.user_id);
|
|
476
|
-
if (!state.remoteControlEnabled) {
|
|
477
|
-
await send(bot, chatId, t(lang, 'control.disabled'));
|
|
478
|
-
return;
|
|
479
|
-
}
|
|
480
|
-
if (!state.selectedProjectPath) {
|
|
481
|
-
await send(bot, chatId, t(lang, 'control.selectProjectFirst'));
|
|
482
|
-
await showProjectMenu({ bot, chatId, link });
|
|
483
|
-
return;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
if (state.progressMode !== 'final') {
|
|
487
|
-
await send(bot, chatId, t(lang, 'control.agentStarted', {
|
|
488
|
-
provider: state.selectedProvider,
|
|
489
|
-
project: state.selectedProjectName || state.selectedProjectPath,
|
|
490
|
-
}));
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
const response = await localApi(link.user_id, '/api/agent', {
|
|
494
|
-
method: 'POST',
|
|
495
|
-
body: {
|
|
496
|
-
projectPath: state.selectedProjectPath,
|
|
497
|
-
provider: state.selectedProvider,
|
|
498
|
-
model: state.selectedModel || undefined,
|
|
499
|
-
message: prompt,
|
|
500
|
-
cleanup: false,
|
|
501
|
-
stream: false,
|
|
502
|
-
},
|
|
503
|
-
});
|
|
504
|
-
const assistantText = extractAssistantText(response);
|
|
505
|
-
const statusLine = response.success === false
|
|
506
|
-
? t(lang, 'control.agentFailed', { error: response.error || 'provider returned no answer' })
|
|
507
|
-
: t(lang, 'control.agentDone', { provider: state.selectedProvider, session: response.sessionId ? ` (${response.sessionId})` : '' });
|
|
508
|
-
await send(bot, chatId, `${statusLine}\n\n${assistantText || response.error || t(lang, 'control.noAssistantText')}`);
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
export async function runWorkflow({ bot, chatId, link, input }) {
|
|
512
|
-
const lang = languageFor(link);
|
|
513
|
-
const state = getState(link.user_id);
|
|
514
|
-
const workflowId = state.selectedWorkflowId;
|
|
515
|
-
if (!workflowId) {
|
|
516
|
-
await send(bot, chatId, t(lang, 'control.selectWorkflowFirst'));
|
|
517
|
-
await showWorkflowMenu({ bot, chatId, link });
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
if (!state.selectedProjectPath) {
|
|
521
|
-
await send(bot, chatId, t(lang, 'control.selectProjectFirst'));
|
|
522
|
-
await showProjectMenu({ bot, chatId, link });
|
|
523
|
-
return;
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
const run = await localApi(link.user_id, `/api/orchestration/workflows/${workflowId}/runs`, {
|
|
527
|
-
method: 'POST',
|
|
528
|
-
body: {
|
|
529
|
-
input,
|
|
530
|
-
metadata: {
|
|
531
|
-
projectId: state.selectedProjectName,
|
|
532
|
-
projectName: state.selectedProjectName,
|
|
533
|
-
projectPath: state.selectedProjectPath,
|
|
534
|
-
workspaceTarget: 'selected_project',
|
|
535
|
-
telegram: true,
|
|
536
|
-
preferredProvider: state.selectedProvider,
|
|
537
|
-
preferredModel: state.selectedModel,
|
|
538
|
-
},
|
|
539
|
-
},
|
|
540
|
-
});
|
|
541
|
-
await send(bot, chatId, t(lang, 'control.workflowStarted', { runId: run.id, workflowId }));
|
|
542
|
-
monitorWorkflowRun({ bot, chatId, link, runId: run.id }).catch((error) => {
|
|
543
|
-
console.warn('[telegram-control] workflow monitor failed:', error?.message || error);
|
|
544
|
-
});
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
async function fetchRun(userId, runId) {
|
|
548
|
-
return localApi(userId, `/api/orchestration/workflows/runs/${runId}`);
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
function summarizeRun(run, mode) {
|
|
552
|
-
const lines = [
|
|
553
|
-
`Run ${run.id}`,
|
|
554
|
-
`Workflow: ${run.workflowId}`,
|
|
555
|
-
`Status: ${run.status}`,
|
|
556
|
-
];
|
|
557
|
-
const nodeRuns = Array.isArray(run.nodeRuns) ? run.nodeRuns : [];
|
|
558
|
-
if (mode !== 'final') {
|
|
559
|
-
const visibleNodes = mode === 'errors'
|
|
560
|
-
? nodeRuns.filter((node) => node.error || node.status === 'failed')
|
|
561
|
-
: nodeRuns;
|
|
562
|
-
for (const node of visibleNodes) {
|
|
563
|
-
lines.push(`- ${node.status}: ${node.agentLabel || node.nodeId}${node.error ? ` — ${node.error}` : ''}`);
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
const outputs = nodeRuns
|
|
567
|
-
.filter((node) => node.outputText)
|
|
568
|
-
.slice(-3)
|
|
569
|
-
.map((node) => `\n${node.agentLabel || node.nodeId}:\n${node.outputText}`);
|
|
570
|
-
return truncate(`${lines.join('\n')}${outputs.join('\n')}`);
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
async function monitorWorkflowRun({ bot, chatId, link, runId }) {
|
|
574
|
-
if (runMonitors.has(runId)) return;
|
|
575
|
-
runMonitors.set(runId, true);
|
|
576
|
-
const state = getState(link.user_id);
|
|
577
|
-
const seenNodeStatus = new Map();
|
|
578
|
-
try {
|
|
579
|
-
for (;;) {
|
|
580
|
-
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
581
|
-
const run = await fetchRun(link.user_id, runId);
|
|
582
|
-
const nodeRuns = Array.isArray(run.nodeRuns) ? run.nodeRuns : [];
|
|
583
|
-
if (state.progressMode === 'all') {
|
|
584
|
-
for (const node of nodeRuns) {
|
|
585
|
-
const key = `${node.nodeId}:${node.status}`;
|
|
586
|
-
if (!seenNodeStatus.has(key)) {
|
|
587
|
-
seenNodeStatus.set(key, true);
|
|
588
|
-
await send(bot, chatId, `${node.agentLabel || node.nodeId}: ${node.status}${node.error ? `\n${node.error}` : ''}`);
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
if (state.progressMode === 'errors') {
|
|
593
|
-
for (const node of nodeRuns.filter((candidate) => candidate.error || candidate.status === 'failed')) {
|
|
594
|
-
const key = `${node.nodeId}:${node.status}:${node.error || ''}`;
|
|
595
|
-
if (!seenNodeStatus.has(key)) {
|
|
596
|
-
seenNodeStatus.set(key, true);
|
|
597
|
-
await send(bot, chatId, `${node.agentLabel || node.nodeId}: ${node.status}\n${node.error || ''}`);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
if (TERMINAL_RUN_STATES.has(run.status)) {
|
|
602
|
-
await send(bot, chatId, summarizeRun(run, state.progressMode));
|
|
603
|
-
return;
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
} finally {
|
|
607
|
-
runMonitors.delete(runId);
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
export async function startCliInstall({ bot, chatId, link, provider }) {
|
|
612
|
-
const lang = languageFor(link);
|
|
613
|
-
const data = await localApi(link.user_id, `/api/providers/${provider}/install`, { method: 'POST' });
|
|
614
|
-
if (data?.manual) {
|
|
615
|
-
await send(bot, chatId, t(lang, 'control.manualInstall', { provider, manual: data.manual }));
|
|
616
|
-
return;
|
|
617
|
-
}
|
|
618
|
-
await send(bot, chatId, t(lang, 'control.installStarted', {
|
|
619
|
-
provider,
|
|
620
|
-
jobId: data.jobId,
|
|
621
|
-
command: data.installCmd || 'internal installer',
|
|
622
|
-
}));
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
async function showInstallMenu({ bot, chatId, link, editMessageId }) {
|
|
626
|
-
const lang = languageFor(link);
|
|
627
|
-
const buttons = PROVIDERS.map((provider) => button(provider, 'install_provider', { provider }));
|
|
628
|
-
await send(bot, chatId, t(lang, 'control.installMenu'), {
|
|
629
|
-
editMessageId,
|
|
630
|
-
reply_markup: { inline_keyboard: rows(buttons, 2) },
|
|
631
|
-
});
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
async function showAuthMenu({ bot, chatId, link, editMessageId }) {
|
|
635
|
-
const lang = languageFor(link);
|
|
636
|
-
const buttons = PROVIDERS.map((provider) => button(provider, 'auth_provider', { provider }));
|
|
637
|
-
await send(bot, chatId, t(lang, 'control.authMenu'), {
|
|
638
|
-
editMessageId,
|
|
639
|
-
reply_markup: { inline_keyboard: rows(buttons, 2) },
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
async function showSettings({ bot, chatId, link, editMessageId }) {
|
|
644
|
-
const lang = languageFor(link);
|
|
645
|
-
const state = getState(link.user_id);
|
|
646
|
-
const keyboard = [
|
|
647
|
-
[
|
|
648
|
-
button(
|
|
649
|
-
state.remoteControlEnabled
|
|
650
|
-
? checked(t(lang, 'control.button.remoteOn'))
|
|
651
|
-
: t(lang, 'control.button.remoteOff'),
|
|
652
|
-
'toggle_control',
|
|
653
|
-
),
|
|
654
|
-
],
|
|
655
|
-
[
|
|
656
|
-
button(state.progressMode === 'final' ? checked(t(lang, 'control.button.progressFinal')) : t(lang, 'control.button.progressFinal'), 'progress_mode', { progressMode: 'final' }),
|
|
657
|
-
button(state.progressMode === 'steps' ? checked(t(lang, 'control.button.progressSteps')) : t(lang, 'control.button.progressSteps'), 'progress_mode', { progressMode: 'steps' }),
|
|
658
|
-
button(state.progressMode === 'errors' ? checked(t(lang, 'control.button.progressErrors')) : t(lang, 'control.button.progressErrors'), 'progress_mode', { progressMode: 'errors' }),
|
|
659
|
-
button(state.progressMode === 'all' ? checked(t(lang, 'control.button.progressAll')) : t(lang, 'control.button.progressAll'), 'progress_mode', { progressMode: 'all' }),
|
|
660
|
-
],
|
|
661
|
-
[
|
|
662
|
-
button(t(lang, 'control.button.language'), 'language_menu'),
|
|
663
|
-
button(t(lang, 'control.button.mainMenu'), 'menu'),
|
|
664
|
-
],
|
|
665
|
-
];
|
|
666
|
-
await send(bot, chatId, `${t(lang, 'control.settingsTitle')}\n\n${stateSummary(lang, state)}`, {
|
|
667
|
-
editMessageId,
|
|
668
|
-
reply_markup: { inline_keyboard: keyboard },
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
async function showLanguageMenu({ bot, chatId, link, editMessageId }) {
|
|
673
|
-
const lang = languageFor(link);
|
|
674
|
-
const buttons = SUPPORTED_LANGUAGES.map((language) => button(language, 'language_select', { language }));
|
|
675
|
-
await send(bot, chatId, t(lang, 'control.pickLanguage'), {
|
|
676
|
-
editMessageId,
|
|
677
|
-
reply_markup: { inline_keyboard: rows(buttons, 3) },
|
|
678
|
-
});
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
async function handleAwaitingInput({ bot, chatId, link, text }) {
|
|
682
|
-
const state = getState(link.user_id);
|
|
683
|
-
const awaiting = state.awaiting;
|
|
684
|
-
if (!awaiting?.type) return false;
|
|
685
|
-
updateTelegramControlState(link.user_id, { awaiting: null });
|
|
686
|
-
|
|
687
|
-
if (awaiting.type === 'agent_prompt') {
|
|
688
|
-
await runAgent({ bot, chatId, link, prompt: text });
|
|
689
|
-
return true;
|
|
690
|
-
}
|
|
691
|
-
if (awaiting.type === 'workflow_prompt') {
|
|
692
|
-
await runWorkflow({ bot, chatId, link, input: text });
|
|
693
|
-
return true;
|
|
694
|
-
}
|
|
695
|
-
return false;
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
async function handleCommand({ bot, chatId, link, text }) {
|
|
699
|
-
const [commandRaw, ...rest] = text.split(/\s+/);
|
|
700
|
-
const command = normalizeCommandToken(commandRaw);
|
|
701
|
-
const argText = rest.join(' ').trim();
|
|
702
|
-
const lang = languageFor(link);
|
|
703
|
-
|
|
704
|
-
if (command === '/menu' || command === '/start' || command === 'menu') {
|
|
705
|
-
await showMainMenu({ bot, chatId, link });
|
|
706
|
-
return true;
|
|
707
|
-
}
|
|
708
|
-
if (command === '/help') {
|
|
709
|
-
await showHelp({ bot, chatId, link });
|
|
710
|
-
return true;
|
|
711
|
-
}
|
|
712
|
-
if (command === '/projects') {
|
|
713
|
-
await showProjectMenu({ bot, chatId, link });
|
|
714
|
-
return true;
|
|
715
|
-
}
|
|
716
|
-
if (command === '/provider') {
|
|
717
|
-
await showProviderMenu({ bot, chatId, link });
|
|
718
|
-
return true;
|
|
719
|
-
}
|
|
720
|
-
if (command === '/model' || command === '/models') {
|
|
721
|
-
await showModelMenu({ bot, chatId, link, refresh: command === '/models' && argText === 'refresh' });
|
|
722
|
-
return true;
|
|
723
|
-
}
|
|
724
|
-
if (command === '/workflows' || command === '/orchestration') {
|
|
725
|
-
await showWorkflowMenu({ bot, chatId, link });
|
|
726
|
-
return true;
|
|
727
|
-
}
|
|
728
|
-
if (command === '/runs') {
|
|
729
|
-
await showRuns({ bot, chatId, link });
|
|
730
|
-
return true;
|
|
731
|
-
}
|
|
732
|
-
if (command === '/approvals') {
|
|
733
|
-
await showApprovalQueue({ bot, chatId, link });
|
|
734
|
-
return true;
|
|
735
|
-
}
|
|
736
|
-
if (command === '/controlroom' || command === '/control-room') {
|
|
737
|
-
await showControlRoom({ bot, chatId, link });
|
|
738
|
-
return true;
|
|
739
|
-
}
|
|
740
|
-
if (command === '/webhooks') {
|
|
741
|
-
await showWebhookMenu({ bot, chatId, link });
|
|
742
|
-
return true;
|
|
743
|
-
}
|
|
744
|
-
if (command === '/sessions') {
|
|
745
|
-
await showSessions({ bot, chatId, link });
|
|
746
|
-
return true;
|
|
747
|
-
}
|
|
748
|
-
if (command === '/newchat') {
|
|
749
|
-
await startNewChat({ bot, chatId, link });
|
|
750
|
-
return true;
|
|
751
|
-
}
|
|
752
|
-
if (command === '/settings') {
|
|
753
|
-
await showSettings({ bot, chatId, link });
|
|
754
|
-
return true;
|
|
755
|
-
}
|
|
756
|
-
if (command === '/install') {
|
|
757
|
-
if (PROVIDERS.includes(argText)) await startCliInstall({ bot, chatId, link, provider: argText });
|
|
758
|
-
else await showInstallMenu({ bot, chatId, link });
|
|
759
|
-
return true;
|
|
760
|
-
}
|
|
761
|
-
if (command === '/auth') {
|
|
762
|
-
if (PROVIDERS.includes(argText)) await send(bot, chatId, `${argText} login:\n${AUTH_HELP[argText]}`);
|
|
763
|
-
else await showAuthMenu({ bot, chatId, link });
|
|
764
|
-
return true;
|
|
765
|
-
}
|
|
766
|
-
if (command === '/control') {
|
|
767
|
-
const enabled = argText !== 'off';
|
|
768
|
-
updateTelegramControlState(link.user_id, { remoteControlEnabled: enabled });
|
|
769
|
-
await send(bot, chatId, enabled ? t(lang, 'control.remoteEnabled') : t(lang, 'control.remoteDisabled'));
|
|
770
|
-
return true;
|
|
771
|
-
}
|
|
772
|
-
if (command === '/progress') {
|
|
773
|
-
if (!['final', 'steps', 'all', 'errors'].includes(argText)) {
|
|
774
|
-
await send(bot, chatId, t(lang, 'control.progressUsage'));
|
|
775
|
-
return true;
|
|
776
|
-
}
|
|
777
|
-
updateTelegramControlState(link.user_id, { progressMode: argText });
|
|
778
|
-
await send(bot, chatId, t(lang, 'control.progressSet', { mode: argText }));
|
|
779
|
-
return true;
|
|
780
|
-
}
|
|
781
|
-
if (command === '/chat') {
|
|
782
|
-
if (!argText) {
|
|
783
|
-
updateTelegramControlState(link.user_id, { awaiting: { type: 'agent_prompt' } });
|
|
784
|
-
await send(bot, chatId, t(lang, 'control.sendAgentPrompt'));
|
|
785
|
-
return true;
|
|
786
|
-
}
|
|
787
|
-
await runAgent({ bot, chatId, link, prompt: argText });
|
|
788
|
-
return true;
|
|
789
|
-
}
|
|
790
|
-
if (command === '/workflow' || command === '/orchestrate') {
|
|
791
|
-
if (!argText) {
|
|
792
|
-
updateTelegramControlState(link.user_id, { awaiting: { type: 'workflow_prompt' } });
|
|
793
|
-
await send(bot, chatId, t(lang, 'control.sendWorkflowPrompt'));
|
|
794
|
-
return true;
|
|
795
|
-
}
|
|
796
|
-
await runWorkflow({ bot, chatId, link, input: argText });
|
|
797
|
-
return true;
|
|
798
|
-
}
|
|
799
|
-
if (command === '/cancel') {
|
|
800
|
-
if (!argText) {
|
|
801
|
-
await send(bot, chatId, t(lang, 'control.cancelUsage'));
|
|
802
|
-
return true;
|
|
803
|
-
}
|
|
804
|
-
const run = await localApi(link.user_id, `/api/orchestration/workflows/runs/${encodeURIComponent(argText)}/cancel`, {
|
|
805
|
-
method: 'POST',
|
|
806
|
-
});
|
|
807
|
-
await send(bot, chatId, t(lang, 'control.runStatus', { runId: run.id, status: run.status }));
|
|
808
|
-
return true;
|
|
809
|
-
}
|
|
810
|
-
return false;
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
export async function handleTelegramControlMessage({ bot, msg, link }) {
|
|
814
|
-
const chatId = msg.chat.id;
|
|
815
|
-
const text = String(msg.text || '').trim();
|
|
816
|
-
if (!text) return false;
|
|
817
|
-
|
|
818
|
-
if (text === '/') {
|
|
819
|
-
await showCommandPalette({ bot, chatId, link });
|
|
820
|
-
return true;
|
|
821
|
-
}
|
|
822
|
-
if (text.startsWith('/') && !getTelegramControlCommand(text)) {
|
|
823
|
-
await showCommandPalette({ bot, chatId, link, unknown: true });
|
|
824
|
-
return true;
|
|
825
|
-
}
|
|
826
|
-
|
|
827
|
-
if (await handleAwaitingInput({ bot, chatId, link, text })) return true;
|
|
828
|
-
if (await handleCommand({ bot, chatId, link, text })) return true;
|
|
829
|
-
|
|
830
|
-
const state = getState(link.user_id);
|
|
831
|
-
if (!state.remoteControlEnabled) {
|
|
832
|
-
await send(bot, chatId, t(languageFor(link), 'control.disabled'));
|
|
833
|
-
return true;
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
await runAgent({ bot, chatId, link, prompt: text });
|
|
837
|
-
return true;
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
async function showRunDetail({ bot, chatId, link, runId, editMessageId }) {
|
|
841
|
-
const lang = languageFor(link);
|
|
842
|
-
const run = await fetchRun(link.user_id, runId);
|
|
843
|
-
await send(bot, chatId, summarizeRun(run, 'all'), {
|
|
844
|
-
editMessageId,
|
|
845
|
-
reply_markup: {
|
|
846
|
-
inline_keyboard: [
|
|
847
|
-
[button(t(lang, 'control.button.cancelRun'), 'run_cancel', { runId }), button(t(lang, 'control.button.refresh'), 'run_detail', { runId })],
|
|
848
|
-
[button(t(lang, 'control.button.runs'), 'runs'), button(t(lang, 'control.button.mainMenu'), 'menu')],
|
|
849
|
-
],
|
|
850
|
-
},
|
|
851
|
-
});
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
export async function handleTelegramControlCallback({ bot, query, link }) {
|
|
855
|
-
const chatId = query.message?.chat?.id;
|
|
856
|
-
if (!chatId) return;
|
|
857
|
-
const editMessageId = query.message?.message_id;
|
|
858
|
-
const entry = readAction(query.data);
|
|
859
|
-
if (!entry) {
|
|
860
|
-
const lang = languageFor(link);
|
|
861
|
-
await bot.answerCallbackQuery(query.id, { text: t(lang, 'control.menuExpired') }).catch(() => {});
|
|
862
|
-
await send(bot, chatId, t(lang, 'control.menuExpiredLong'));
|
|
863
|
-
return;
|
|
864
|
-
}
|
|
865
|
-
await bot.answerCallbackQuery(query.id).catch(() => {});
|
|
866
|
-
|
|
867
|
-
const { action, payload } = entry;
|
|
868
|
-
if (action === 'menu') return showMainMenu({ bot, chatId, link, editMessageId });
|
|
869
|
-
if (action === 'projects') return showProjectMenu({ bot, chatId, link, editMessageId });
|
|
870
|
-
if (action === 'providers') return showProviderMenu({ bot, chatId, link, editMessageId });
|
|
871
|
-
if (action === 'models') return showModelMenu({ bot, chatId, link, editMessageId });
|
|
872
|
-
if (action === 'models_refresh') return showModelMenu({ bot, chatId, link, refresh: true, editMessageId });
|
|
873
|
-
if (action === 'workflows') return showWorkflowMenu({ bot, chatId, link, editMessageId });
|
|
874
|
-
if (action === 'runs') return showRuns({ bot, chatId, link, editMessageId });
|
|
875
|
-
if (action === 'approvals') return showApprovalQueue({ bot, chatId, link, editMessageId });
|
|
876
|
-
if (action === 'control_room') return showControlRoom({ bot, chatId, link, editMessageId });
|
|
877
|
-
if (action === 'webhooks') return showWebhookMenu({ bot, chatId, link, editMessageId });
|
|
878
|
-
if (action === 'sessions') return showSessions({ bot, chatId, link, editMessageId });
|
|
879
|
-
if (action === 'new_chat') return startNewChat({ bot, chatId, link, editMessageId });
|
|
880
|
-
if (action === 'install_menu') return showInstallMenu({ bot, chatId, link, editMessageId });
|
|
881
|
-
if (action === 'auth_menu') return showAuthMenu({ bot, chatId, link, editMessageId });
|
|
882
|
-
if (action === 'settings') return showSettings({ bot, chatId, link, editMessageId });
|
|
883
|
-
if (action === 'language_menu') return showLanguageMenu({ bot, chatId, link, editMessageId });
|
|
884
|
-
if (action === 'project_select') {
|
|
885
|
-
updateTelegramControlState(link.user_id, {
|
|
886
|
-
selectedProjectName: payload.name,
|
|
887
|
-
selectedProjectPath: payload.path,
|
|
888
|
-
});
|
|
889
|
-
const lang = languageFor(link);
|
|
890
|
-
return showMainMenu({
|
|
891
|
-
bot,
|
|
892
|
-
chatId,
|
|
893
|
-
link,
|
|
894
|
-
editMessageId,
|
|
895
|
-
notice: t(lang, 'control.projectSelected', { project: payload.displayName || payload.name, path: payload.path }),
|
|
896
|
-
});
|
|
897
|
-
}
|
|
898
|
-
if (action === 'provider_select') {
|
|
899
|
-
updateTelegramControlState(link.user_id, { selectedProvider: payload.provider, selectedModel: null });
|
|
900
|
-
return showModelMenu({ bot, chatId, link, editMessageId });
|
|
901
|
-
}
|
|
902
|
-
if (action === 'model_select') {
|
|
903
|
-
updateTelegramControlState(link.user_id, { selectedModel: payload.model || null });
|
|
904
|
-
const lang = languageFor(link);
|
|
905
|
-
return showMainMenu({
|
|
906
|
-
bot,
|
|
907
|
-
chatId,
|
|
908
|
-
link,
|
|
909
|
-
editMessageId,
|
|
910
|
-
notice: payload.model
|
|
911
|
-
? t(lang, 'control.modelSelected', { model: payload.model })
|
|
912
|
-
: t(lang, 'control.modelCleared'),
|
|
913
|
-
});
|
|
914
|
-
}
|
|
915
|
-
if (action === 'workflow_select') {
|
|
916
|
-
updateTelegramControlState(link.user_id, { selectedWorkflowId: payload.workflowId });
|
|
917
|
-
const lang = languageFor(link);
|
|
918
|
-
return showMainMenu({
|
|
919
|
-
bot,
|
|
920
|
-
chatId,
|
|
921
|
-
link,
|
|
922
|
-
editMessageId,
|
|
923
|
-
notice: t(lang, 'control.workflowSelected', { workflow: payload.name || payload.workflowId }),
|
|
924
|
-
});
|
|
925
|
-
}
|
|
926
|
-
if (action === 'workflow_prompt') {
|
|
927
|
-
updateTelegramControlState(link.user_id, { awaiting: { type: 'workflow_prompt' } });
|
|
928
|
-
await send(bot, chatId, t(languageFor(link), 'control.sendWorkflowPrompt'), { editMessageId });
|
|
929
|
-
return;
|
|
930
|
-
}
|
|
931
|
-
if (action === 'run_detail') return showRunDetail({ bot, chatId, link, runId: payload.runId, editMessageId });
|
|
932
|
-
if (action === 'run_cancel') {
|
|
933
|
-
const run = await localApi(link.user_id, `/api/orchestration/workflows/runs/${encodeURIComponent(payload.runId)}/cancel`, {
|
|
934
|
-
method: 'POST',
|
|
935
|
-
});
|
|
936
|
-
await send(bot, chatId, t(languageFor(link), 'control.runStatus', { runId: run.id, status: run.status }), { editMessageId });
|
|
937
|
-
return;
|
|
938
|
-
}
|
|
939
|
-
if (action === 'approval_decide') {
|
|
940
|
-
const result = await localApi(link.user_id, `/api/orchestration/workflows/approvals/${encodeURIComponent(payload.approvalId)}`, {
|
|
941
|
-
method: 'POST',
|
|
942
|
-
body: {
|
|
943
|
-
allow: payload.allow === true,
|
|
944
|
-
source: 'telegram',
|
|
945
|
-
},
|
|
946
|
-
});
|
|
947
|
-
const lang = languageFor(link);
|
|
948
|
-
await send(bot, chatId, t(lang, 'control.approvalDecided', {
|
|
949
|
-
approvalId: payload.approvalId,
|
|
950
|
-
status: payload.allow === true ? 'approved' : 'denied',
|
|
951
|
-
runId: result?.runId || '',
|
|
952
|
-
}), { editMessageId });
|
|
953
|
-
return showApprovalQueue({ bot, chatId, link });
|
|
954
|
-
}
|
|
955
|
-
if (action === 'install_provider') return startCliInstall({ bot, chatId, link, provider: payload.provider });
|
|
956
|
-
if (action === 'auth_provider') {
|
|
957
|
-
await send(bot, chatId, `${payload.provider} login:\n${AUTH_HELP[payload.provider] || t(languageFor(link), 'control.providerAuthFallback')}`, { editMessageId });
|
|
958
|
-
return;
|
|
959
|
-
}
|
|
960
|
-
if (action === 'toggle_control') {
|
|
961
|
-
const state = getState(link.user_id);
|
|
962
|
-
updateTelegramControlState(link.user_id, { remoteControlEnabled: !state.remoteControlEnabled });
|
|
963
|
-
return showSettings({ bot, chatId, link, editMessageId });
|
|
964
|
-
}
|
|
965
|
-
if (action === 'progress_mode') {
|
|
966
|
-
updateTelegramControlState(link.user_id, { progressMode: payload.progressMode });
|
|
967
|
-
return showSettings({ bot, chatId, link, editMessageId });
|
|
968
|
-
}
|
|
969
|
-
if (action === 'language_select') {
|
|
970
|
-
telegramLinksDb.updatePreferences(link.user_id, { language: payload.language });
|
|
971
|
-
return showMainMenu({
|
|
972
|
-
bot,
|
|
973
|
-
chatId,
|
|
974
|
-
link: { ...link, language: payload.language },
|
|
975
|
-
editMessageId,
|
|
976
|
-
notice: t(payload.language, 'control.languageSet', { language: payload.language }),
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
}
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
import { apiKeysDb, telegramLinksDb } from '../../database/db.js';
|
|
4
|
+
import { getProjects } from '../../projects.js';
|
|
5
|
+
import { getStaticProviderModels } from '../model-registry.js';
|
|
6
|
+
|
|
7
|
+
import { SUPPORTED_LANGUAGES, t } from './translations.js';
|
|
8
|
+
|
|
9
|
+
const PROVIDERS = ['claude', 'cursor', 'codex', 'gemini', 'qwen', 'opencode'];
|
|
10
|
+
const TERMINAL_RUN_STATES = new Set(['completed', 'failed', 'canceled']);
|
|
11
|
+
const CALLBACK_TTL_MS = 10 * 60 * 1000;
|
|
12
|
+
const MAX_TELEGRAM_TEXT = 3600;
|
|
13
|
+
const callbackActions = new Map();
|
|
14
|
+
const runMonitors = new Map();
|
|
15
|
+
|
|
16
|
+
const MODEL_FALLBACKS = Object.fromEntries(
|
|
17
|
+
PROVIDERS.map((provider) => [provider, getStaticProviderModels(provider)]),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const AUTH_HELP = {
|
|
21
|
+
claude: '`claude login`',
|
|
22
|
+
cursor: '`cursor-agent login`',
|
|
23
|
+
codex: '`codex login`',
|
|
24
|
+
gemini: '`gemini auth login`',
|
|
25
|
+
qwen: '`qwen auth`',
|
|
26
|
+
opencode: '`opencode auth login`',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const CONTROL_COMMANDS = new Set([
|
|
30
|
+
'/menu',
|
|
31
|
+
'/start',
|
|
32
|
+
'/help',
|
|
33
|
+
'/projects',
|
|
34
|
+
'/provider',
|
|
35
|
+
'/model',
|
|
36
|
+
'/models',
|
|
37
|
+
'/workflows',
|
|
38
|
+
'/orchestration',
|
|
39
|
+
'/runs',
|
|
40
|
+
'/approvals',
|
|
41
|
+
'/controlroom',
|
|
42
|
+
'/control-room',
|
|
43
|
+
'/webhooks',
|
|
44
|
+
'/sessions',
|
|
45
|
+
'/newchat',
|
|
46
|
+
'/settings',
|
|
47
|
+
'/install',
|
|
48
|
+
'/auth',
|
|
49
|
+
'/control',
|
|
50
|
+
'/progress',
|
|
51
|
+
'/chat',
|
|
52
|
+
'/workflow',
|
|
53
|
+
'/orchestrate',
|
|
54
|
+
'/cancel',
|
|
55
|
+
'menu',
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
function compact(text, max = 80) {
|
|
59
|
+
const value = String(text || '').replace(/\s+/g, ' ').trim();
|
|
60
|
+
return value.length > max ? `${value.slice(0, max - 1)}…` : value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function truncate(text, max = MAX_TELEGRAM_TEXT) {
|
|
64
|
+
const value = String(text || '').trim();
|
|
65
|
+
return value.length > max ? `${value.slice(0, max - 20)}\n\n…truncated…` : value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function languageFor(link) {
|
|
69
|
+
return SUPPORTED_LANGUAGES.includes(link?.language) ? link.language : 'en';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function normalizeCommandToken(commandRaw = '') {
|
|
73
|
+
return String(commandRaw || '')
|
|
74
|
+
.trim()
|
|
75
|
+
.toLowerCase()
|
|
76
|
+
.replace(/@[^@\s]+$/, '');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function getTelegramControlCommand(text = '') {
|
|
80
|
+
const [commandRaw = ''] = String(text || '').trim().split(/\s+/);
|
|
81
|
+
const command = normalizeCommandToken(commandRaw);
|
|
82
|
+
return CONTROL_COMMANDS.has(command) ? command : null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function isTelegramControlCommand(text = '') {
|
|
86
|
+
return Boolean(getTelegramControlCommand(text));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function getState(userId) {
|
|
90
|
+
return telegramLinksDb.getControlState(userId);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function updateTelegramControlState(userId, patch) {
|
|
94
|
+
return telegramLinksDb.updateControlState(userId, patch);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function registerAction(action, payload = {}) {
|
|
98
|
+
const id = crypto.randomBytes(8).toString('hex');
|
|
99
|
+
callbackActions.set(id, {
|
|
100
|
+
action,
|
|
101
|
+
payload,
|
|
102
|
+
expiresAt: Date.now() + CALLBACK_TTL_MS,
|
|
103
|
+
});
|
|
104
|
+
return `tc:${id}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function readAction(data) {
|
|
108
|
+
if (typeof data !== 'string' || !data.startsWith('tc:')) return null;
|
|
109
|
+
const id = data.slice(3);
|
|
110
|
+
const entry = callbackActions.get(id);
|
|
111
|
+
if (!entry) return null;
|
|
112
|
+
if (entry.expiresAt < Date.now()) {
|
|
113
|
+
callbackActions.delete(id);
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
return entry;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function button(text, action, payload = {}) {
|
|
120
|
+
return { text, callback_data: registerAction(action, payload) };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function rows(buttons, width = 2) {
|
|
124
|
+
const out = [];
|
|
125
|
+
for (let i = 0; i < buttons.length; i += width) out.push(buttons.slice(i, i + width));
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function send(bot, chatId, text, options = {}) {
|
|
130
|
+
const { editMessageId, ...telegramOptions } = options;
|
|
131
|
+
const extra = {
|
|
132
|
+
parse_mode: 'Markdown',
|
|
133
|
+
disable_web_page_preview: true,
|
|
134
|
+
...telegramOptions,
|
|
135
|
+
};
|
|
136
|
+
const messageText = truncate(text);
|
|
137
|
+
if (editMessageId && typeof bot.editMessageText === 'function') {
|
|
138
|
+
try {
|
|
139
|
+
return await bot.editMessageText(messageText, {
|
|
140
|
+
chat_id: chatId,
|
|
141
|
+
message_id: editMessageId,
|
|
142
|
+
...extra,
|
|
143
|
+
});
|
|
144
|
+
} catch (err) {
|
|
145
|
+
const description = err?.response?.body?.description || err?.message || '';
|
|
146
|
+
if (/message is not modified/i.test(description)) return null;
|
|
147
|
+
console.warn('[telegram-control] editMessageText failed:', description || err);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
return await bot.sendMessage(chatId, messageText, extra);
|
|
152
|
+
} catch {
|
|
153
|
+
const fallback = { ...extra };
|
|
154
|
+
delete fallback.parse_mode;
|
|
155
|
+
return bot.sendMessage(chatId, messageText, fallback);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function localApiBase() {
|
|
160
|
+
const port = process.env.SERVER_PORT || process.env.PORT || '3001';
|
|
161
|
+
return `http://127.0.0.1:${port}`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function getOrCreateTelegramApiKey(userId) {
|
|
165
|
+
const existing = apiKeysDb
|
|
166
|
+
.getApiKeys(userId)
|
|
167
|
+
.find((key) => key.key_name === 'Telegram Control' && Boolean(key.is_active));
|
|
168
|
+
if (existing?.api_key) return existing.api_key;
|
|
169
|
+
return apiKeysDb.createApiKey(userId, 'Telegram Control').apiKey;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function localApi(userId, path, { method = 'GET', body } = {}) {
|
|
173
|
+
const apiKey = getOrCreateTelegramApiKey(userId);
|
|
174
|
+
const response = await fetch(`${localApiBase()}${path}`, {
|
|
175
|
+
method,
|
|
176
|
+
headers: {
|
|
177
|
+
'Content-Type': 'application/json',
|
|
178
|
+
'X-API-Key': apiKey,
|
|
179
|
+
},
|
|
180
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
181
|
+
});
|
|
182
|
+
const data = await response.json().catch(() => ({}));
|
|
183
|
+
if (!response.ok) {
|
|
184
|
+
const error = data?.error?.message || data?.error || `HTTP ${response.status}`;
|
|
185
|
+
throw new Error(typeof error === 'string' ? error : JSON.stringify(error));
|
|
186
|
+
}
|
|
187
|
+
return data?.data ?? data;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function checked(label) {
|
|
191
|
+
return `${label} ✓`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function stateSummary(lang, state) {
|
|
195
|
+
return [
|
|
196
|
+
`${t(lang, 'control.summary.project')}: ${state.selectedProjectName || t(lang, 'control.notSelected')}`,
|
|
197
|
+
`${t(lang, 'control.summary.provider')}: ${state.selectedProvider}${state.selectedModel ? ` / ${state.selectedModel}` : ''}`,
|
|
198
|
+
`${t(lang, 'control.summary.workflow')}: ${state.selectedWorkflowId || t(lang, 'control.notSelected')}`,
|
|
199
|
+
`${t(lang, 'control.summary.progress')}: ${state.progressMode}`,
|
|
200
|
+
].join('\n');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function mainMenuKeyboard(lang) {
|
|
204
|
+
return [
|
|
205
|
+
[button(t(lang, 'control.button.projects'), 'projects'), button(t(lang, 'control.button.provider'), 'providers')],
|
|
206
|
+
[button(t(lang, 'control.button.models'), 'models'), button(t(lang, 'control.button.workflows'), 'workflows')],
|
|
207
|
+
[button(t(lang, 'control.button.runs'), 'runs'), button(t(lang, 'control.button.approvals'), 'approvals')],
|
|
208
|
+
[button(t(lang, 'control.button.controlRoom'), 'control_room'), button(t(lang, 'control.button.webhooks'), 'webhooks')],
|
|
209
|
+
[button(t(lang, 'control.button.sessions'), 'sessions'), button(t(lang, 'control.button.newChat'), 'new_chat')],
|
|
210
|
+
[button(t(lang, 'control.button.install'), 'install_menu'), button(t(lang, 'control.button.auth'), 'auth_menu')],
|
|
211
|
+
[button(t(lang, 'control.button.settings'), 'settings')],
|
|
212
|
+
];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export async function showMainMenu({ bot, chatId, link, editMessageId, notice }) {
|
|
216
|
+
const lang = languageFor(link);
|
|
217
|
+
const state = getState(link.user_id);
|
|
218
|
+
const prefix = notice ? `${notice}\n\n` : '';
|
|
219
|
+
await send(bot, chatId, `${prefix}${t(lang, 'control.menu')}\n\n${stateSummary(lang, state)}`, {
|
|
220
|
+
editMessageId,
|
|
221
|
+
reply_markup: { inline_keyboard: mainMenuKeyboard(lang) },
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function showHelp({ bot, chatId, link }) {
|
|
226
|
+
const lang = languageFor(link);
|
|
227
|
+
await send(bot, chatId, t(lang, 'control.help'));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function showCommandPalette({ bot, chatId, link, editMessageId, unknown = false }) {
|
|
231
|
+
const lang = languageFor(link);
|
|
232
|
+
const prefix = unknown ? `${t(lang, 'control.unknownCommand')}\n\n` : '';
|
|
233
|
+
await send(bot, chatId, `${prefix}${t(lang, 'control.help')}\n\n${t(lang, 'control.examples')}`, {
|
|
234
|
+
editMessageId,
|
|
235
|
+
reply_markup: { inline_keyboard: mainMenuKeyboard(lang) },
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async function listProjects() {
|
|
240
|
+
const projects = await getProjects();
|
|
241
|
+
return projects.slice(0, 20);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function showProjectMenu({ bot, chatId, link, editMessageId }) {
|
|
245
|
+
const lang = languageFor(link);
|
|
246
|
+
const projects = await listProjects();
|
|
247
|
+
if (projects.length === 0) {
|
|
248
|
+
await send(bot, chatId, t(lang, 'control.noProjects'), { editMessageId });
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const buttons = projects.map((project, index) => button(
|
|
253
|
+
`${index + 1}. ${compact(project.displayName || project.name, 34)}`,
|
|
254
|
+
'project_select',
|
|
255
|
+
{
|
|
256
|
+
name: project.name,
|
|
257
|
+
path: project.fullPath || project.path,
|
|
258
|
+
displayName: project.displayName || project.name,
|
|
259
|
+
},
|
|
260
|
+
));
|
|
261
|
+
await send(bot, chatId, t(lang, 'control.pickProject'), {
|
|
262
|
+
editMessageId,
|
|
263
|
+
reply_markup: { inline_keyboard: rows(buttons, 1) },
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export async function showProviderMenu({ bot, chatId, link, editMessageId }) {
|
|
268
|
+
const lang = languageFor(link);
|
|
269
|
+
const state = getState(link.user_id);
|
|
270
|
+
const buttons = PROVIDERS.map((provider) => button(
|
|
271
|
+
provider === state.selectedProvider ? checked(provider) : provider,
|
|
272
|
+
'provider_select',
|
|
273
|
+
{ provider },
|
|
274
|
+
));
|
|
275
|
+
await send(bot, chatId, t(lang, 'control.pickProvider'), {
|
|
276
|
+
editMessageId,
|
|
277
|
+
reply_markup: { inline_keyboard: rows(buttons, 2) },
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async function readProviderModels(userId, provider, refresh = false) {
|
|
282
|
+
try {
|
|
283
|
+
const data = await localApi(userId, `/api/providers/${provider}/models${refresh ? '?refresh=1' : ''}`);
|
|
284
|
+
const models = Array.isArray(data?.models) ? data.models : [];
|
|
285
|
+
return models.length > 0 ? models : MODEL_FALLBACKS[provider] || [];
|
|
286
|
+
} catch {
|
|
287
|
+
return MODEL_FALLBACKS[provider] || [];
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export async function showModelMenu({ bot, chatId, link, refresh = false, editMessageId }) {
|
|
292
|
+
const lang = languageFor(link);
|
|
293
|
+
const state = getState(link.user_id);
|
|
294
|
+
const models = await readProviderModels(link.user_id, state.selectedProvider, refresh);
|
|
295
|
+
const modelButtons = models.slice(0, 18).map((model) => {
|
|
296
|
+
const value = model.value || model.id || model.label;
|
|
297
|
+
const label = compact(model.label || value, 38);
|
|
298
|
+
return button(value === state.selectedModel ? checked(label) : label, 'model_select', { model: value });
|
|
299
|
+
});
|
|
300
|
+
modelButtons.push(button(t(lang, 'control.button.refreshModels'), 'models_refresh'));
|
|
301
|
+
modelButtons.push(button(t(lang, 'control.button.clearModel'), 'model_select', { model: null }));
|
|
302
|
+
await send(bot, chatId, t(lang, 'control.modelsFor', { provider: state.selectedProvider }), {
|
|
303
|
+
editMessageId,
|
|
304
|
+
reply_markup: { inline_keyboard: rows(modelButtons, 1) },
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export async function showWorkflowMenu({ bot, chatId, link, editMessageId }) {
|
|
309
|
+
const lang = languageFor(link);
|
|
310
|
+
const workflows = await localApi(link.user_id, '/api/orchestration/workflows');
|
|
311
|
+
const list = Array.isArray(workflows?.workflows) ? workflows.workflows : [];
|
|
312
|
+
const state = getState(link.user_id);
|
|
313
|
+
const workflowButtons = list.map((workflow) => button(
|
|
314
|
+
workflow.id === state.selectedWorkflowId ? checked(compact(workflow.name || workflow.id, 34)) : compact(workflow.name || workflow.id, 34),
|
|
315
|
+
'workflow_select',
|
|
316
|
+
{ workflowId: workflow.id, name: workflow.name || workflow.id },
|
|
317
|
+
));
|
|
318
|
+
workflowButtons.push(button(t(lang, 'control.button.runWorkflow'), 'workflow_prompt'));
|
|
319
|
+
await send(bot, chatId, t(lang, 'control.pickWorkflow'), {
|
|
320
|
+
editMessageId,
|
|
321
|
+
reply_markup: { inline_keyboard: rows(workflowButtons, 1) },
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async function showRuns({ bot, chatId, link, editMessageId }) {
|
|
326
|
+
const lang = languageFor(link);
|
|
327
|
+
const data = await localApi(link.user_id, '/api/orchestration/workflows/runs?limit=10');
|
|
328
|
+
const runs = Array.isArray(data?.runs) ? data.runs : [];
|
|
329
|
+
if (runs.length === 0) {
|
|
330
|
+
await send(bot, chatId, t(lang, 'control.noRuns'), { editMessageId });
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const buttons = runs.map((run) => button(
|
|
334
|
+
`${run.status} ${compact(run.workflowId || run.id, 28)}`,
|
|
335
|
+
'run_detail',
|
|
336
|
+
{ runId: run.id },
|
|
337
|
+
));
|
|
338
|
+
await send(bot, chatId, t(lang, 'control.recentRuns'), {
|
|
339
|
+
editMessageId,
|
|
340
|
+
reply_markup: { inline_keyboard: rows(buttons, 1) },
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function showApprovalQueue({ bot, chatId, link, editMessageId }) {
|
|
345
|
+
const lang = languageFor(link);
|
|
346
|
+
const data = await localApi(link.user_id, '/api/orchestration/workflows/approvals');
|
|
347
|
+
const approvals = Array.isArray(data?.pendingApprovals) ? data.pendingApprovals : [];
|
|
348
|
+
if (approvals.length === 0) {
|
|
349
|
+
await send(bot, chatId, t(lang, 'control.noApprovals'), { editMessageId });
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const keyboard = [];
|
|
354
|
+
const lines = approvals.slice(0, 8).map((approval, index) => {
|
|
355
|
+
const label = compact(approval.summary || approval.reason || approval.id, 70);
|
|
356
|
+
keyboard.push([
|
|
357
|
+
button(t(lang, 'control.button.approve'), 'approval_decide', { approvalId: approval.id, allow: true }),
|
|
358
|
+
button(t(lang, 'control.button.deny'), 'approval_decide', { approvalId: approval.id, allow: false }),
|
|
359
|
+
]);
|
|
360
|
+
return `${index + 1}. ${label}\nRun: ${approval.runId}`;
|
|
361
|
+
});
|
|
362
|
+
keyboard.push([button(t(lang, 'control.button.refresh'), 'approvals'), button(t(lang, 'control.button.mainMenu'), 'menu')]);
|
|
363
|
+
await send(bot, chatId, `${t(lang, 'control.approvalQueue')}\n\n${lines.join('\n\n')}`, {
|
|
364
|
+
editMessageId,
|
|
365
|
+
reply_markup: { inline_keyboard: keyboard },
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async function showControlRoom({ bot, chatId, link, editMessageId }) {
|
|
370
|
+
const lang = languageFor(link);
|
|
371
|
+
const data = await localApi(link.user_id, '/api/remote/control-room');
|
|
372
|
+
const snapshot = data?.controlRoom || data;
|
|
373
|
+
const projects = Array.isArray(snapshot?.projects) ? snapshot.projects : [];
|
|
374
|
+
const totals = snapshot?.totals || {};
|
|
375
|
+
const lines = projects.map((project, index) => [
|
|
376
|
+
`${index + 1}. ${compact(project.name || project.id, 44)}`,
|
|
377
|
+
`Runs: ${project.activeRunCount || 0} active / ${project.failedRunCount || 0} failed`,
|
|
378
|
+
`Approvals: ${project.pendingApprovalCount || 0}`,
|
|
379
|
+
].join('\n'));
|
|
380
|
+
await send(bot, chatId, [
|
|
381
|
+
t(lang, 'control.controlRoomTitle'),
|
|
382
|
+
'',
|
|
383
|
+
`Projects: ${totals.projects || 0}`,
|
|
384
|
+
`Active runs: ${totals.activeRuns || 0}`,
|
|
385
|
+
`Pending approvals: ${totals.pendingApprovals || 0}`,
|
|
386
|
+
'',
|
|
387
|
+
lines.join('\n\n') || t(lang, 'control.noProjects'),
|
|
388
|
+
].join('\n'), {
|
|
389
|
+
editMessageId,
|
|
390
|
+
reply_markup: {
|
|
391
|
+
inline_keyboard: [
|
|
392
|
+
[button(t(lang, 'control.button.approvals'), 'approvals'), button(t(lang, 'control.button.runs'), 'runs')],
|
|
393
|
+
[button(t(lang, 'control.button.mainMenu'), 'menu')],
|
|
394
|
+
],
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async function showWebhookMenu({ bot, chatId, link, editMessageId }) {
|
|
400
|
+
const lang = languageFor(link);
|
|
401
|
+
const data = await localApi(link.user_id, '/api/webhooks');
|
|
402
|
+
const webhooks = Array.isArray(data?.webhooks) ? data.webhooks : [];
|
|
403
|
+
const lines = webhooks.slice(0, 10).map((webhook, index) => (
|
|
404
|
+
`${index + 1}. ${webhook.enabled ? 'on' : 'off'} ${compact(webhook.name || webhook.url, 50)}\n${compact(webhook.events?.join(', ') || webhook.url, 90)}`
|
|
405
|
+
));
|
|
406
|
+
await send(bot, chatId, [
|
|
407
|
+
t(lang, 'control.webhookTitle'),
|
|
408
|
+
'',
|
|
409
|
+
lines.join('\n\n') || t(lang, 'control.noWebhooks'),
|
|
410
|
+
].join('\n'), {
|
|
411
|
+
editMessageId,
|
|
412
|
+
reply_markup: {
|
|
413
|
+
inline_keyboard: [
|
|
414
|
+
[button(t(lang, 'control.button.refresh'), 'webhooks'), button(t(lang, 'control.button.mainMenu'), 'menu')],
|
|
415
|
+
],
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
async function showSessions({ bot, chatId, link, editMessageId }) {
|
|
421
|
+
const lang = languageFor(link);
|
|
422
|
+
const state = getState(link.user_id);
|
|
423
|
+
if (!state.selectedProjectName) {
|
|
424
|
+
await send(bot, chatId, t(lang, 'control.selectProjectFirst'), { editMessageId });
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const data = await localApi(link.user_id, `/api/projects/${encodeURIComponent(state.selectedProjectName)}/sessions?limit=10&offset=0`);
|
|
429
|
+
const sessions = Array.isArray(data?.sessions) ? data.sessions : [];
|
|
430
|
+
if (sessions.length === 0) {
|
|
431
|
+
await send(bot, chatId, t(lang, 'control.noSessions'), { editMessageId });
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const lines = sessions.slice(0, 10).map((session, index) =>
|
|
436
|
+
`${index + 1}. ${compact(session.summary || session.id || session.sessionId, 70)}`
|
|
437
|
+
);
|
|
438
|
+
await send(bot, chatId, `${t(lang, 'control.recentSessions')}\n\n${lines.join('\n')}`, {
|
|
439
|
+
editMessageId,
|
|
440
|
+
reply_markup: {
|
|
441
|
+
inline_keyboard: [
|
|
442
|
+
[button(t(lang, 'control.button.newChat'), 'new_chat')],
|
|
443
|
+
[button(t(lang, 'control.button.mainMenu'), 'menu')],
|
|
444
|
+
],
|
|
445
|
+
},
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
async function startNewChat({ bot, chatId, link, editMessageId }) {
|
|
450
|
+
const lang = languageFor(link);
|
|
451
|
+
updateTelegramControlState(link.user_id, { awaiting: { type: 'agent_prompt' } });
|
|
452
|
+
await send(bot, chatId, t(lang, 'control.newChatReady'), {
|
|
453
|
+
editMessageId,
|
|
454
|
+
reply_markup: { inline_keyboard: [[button(t(lang, 'control.button.mainMenu'), 'menu')]] },
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function extractAssistantText(response) {
|
|
459
|
+
const messages = Array.isArray(response?.messages) ? response.messages : [];
|
|
460
|
+
const chunks = [];
|
|
461
|
+
for (const message of messages) {
|
|
462
|
+
if (typeof message?.content === 'string') chunks.push(message.content);
|
|
463
|
+
const content = message?.message?.content;
|
|
464
|
+
if (Array.isArray(content)) {
|
|
465
|
+
for (const part of content) {
|
|
466
|
+
if (part?.type === 'text' && typeof part.text === 'string') chunks.push(part.text);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return chunks.join('\n\n').trim();
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async function runAgent({ bot, chatId, link, prompt }) {
|
|
474
|
+
const lang = languageFor(link);
|
|
475
|
+
const state = getState(link.user_id);
|
|
476
|
+
if (!state.remoteControlEnabled) {
|
|
477
|
+
await send(bot, chatId, t(lang, 'control.disabled'));
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (!state.selectedProjectPath) {
|
|
481
|
+
await send(bot, chatId, t(lang, 'control.selectProjectFirst'));
|
|
482
|
+
await showProjectMenu({ bot, chatId, link });
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (state.progressMode !== 'final') {
|
|
487
|
+
await send(bot, chatId, t(lang, 'control.agentStarted', {
|
|
488
|
+
provider: state.selectedProvider,
|
|
489
|
+
project: state.selectedProjectName || state.selectedProjectPath,
|
|
490
|
+
}));
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const response = await localApi(link.user_id, '/api/agent', {
|
|
494
|
+
method: 'POST',
|
|
495
|
+
body: {
|
|
496
|
+
projectPath: state.selectedProjectPath,
|
|
497
|
+
provider: state.selectedProvider,
|
|
498
|
+
model: state.selectedModel || undefined,
|
|
499
|
+
message: prompt,
|
|
500
|
+
cleanup: false,
|
|
501
|
+
stream: false,
|
|
502
|
+
},
|
|
503
|
+
});
|
|
504
|
+
const assistantText = extractAssistantText(response);
|
|
505
|
+
const statusLine = response.success === false
|
|
506
|
+
? t(lang, 'control.agentFailed', { error: response.error || 'provider returned no answer' })
|
|
507
|
+
: t(lang, 'control.agentDone', { provider: state.selectedProvider, session: response.sessionId ? ` (${response.sessionId})` : '' });
|
|
508
|
+
await send(bot, chatId, `${statusLine}\n\n${assistantText || response.error || t(lang, 'control.noAssistantText')}`);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export async function runWorkflow({ bot, chatId, link, input }) {
|
|
512
|
+
const lang = languageFor(link);
|
|
513
|
+
const state = getState(link.user_id);
|
|
514
|
+
const workflowId = state.selectedWorkflowId;
|
|
515
|
+
if (!workflowId) {
|
|
516
|
+
await send(bot, chatId, t(lang, 'control.selectWorkflowFirst'));
|
|
517
|
+
await showWorkflowMenu({ bot, chatId, link });
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
if (!state.selectedProjectPath) {
|
|
521
|
+
await send(bot, chatId, t(lang, 'control.selectProjectFirst'));
|
|
522
|
+
await showProjectMenu({ bot, chatId, link });
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const run = await localApi(link.user_id, `/api/orchestration/workflows/${workflowId}/runs`, {
|
|
527
|
+
method: 'POST',
|
|
528
|
+
body: {
|
|
529
|
+
input,
|
|
530
|
+
metadata: {
|
|
531
|
+
projectId: state.selectedProjectName,
|
|
532
|
+
projectName: state.selectedProjectName,
|
|
533
|
+
projectPath: state.selectedProjectPath,
|
|
534
|
+
workspaceTarget: 'selected_project',
|
|
535
|
+
telegram: true,
|
|
536
|
+
preferredProvider: state.selectedProvider,
|
|
537
|
+
preferredModel: state.selectedModel,
|
|
538
|
+
},
|
|
539
|
+
},
|
|
540
|
+
});
|
|
541
|
+
await send(bot, chatId, t(lang, 'control.workflowStarted', { runId: run.id, workflowId }));
|
|
542
|
+
monitorWorkflowRun({ bot, chatId, link, runId: run.id }).catch((error) => {
|
|
543
|
+
console.warn('[telegram-control] workflow monitor failed:', error?.message || error);
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
async function fetchRun(userId, runId) {
|
|
548
|
+
return localApi(userId, `/api/orchestration/workflows/runs/${runId}`);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
function summarizeRun(run, mode) {
|
|
552
|
+
const lines = [
|
|
553
|
+
`Run ${run.id}`,
|
|
554
|
+
`Workflow: ${run.workflowId}`,
|
|
555
|
+
`Status: ${run.status}`,
|
|
556
|
+
];
|
|
557
|
+
const nodeRuns = Array.isArray(run.nodeRuns) ? run.nodeRuns : [];
|
|
558
|
+
if (mode !== 'final') {
|
|
559
|
+
const visibleNodes = mode === 'errors'
|
|
560
|
+
? nodeRuns.filter((node) => node.error || node.status === 'failed')
|
|
561
|
+
: nodeRuns;
|
|
562
|
+
for (const node of visibleNodes) {
|
|
563
|
+
lines.push(`- ${node.status}: ${node.agentLabel || node.nodeId}${node.error ? ` — ${node.error}` : ''}`);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
const outputs = nodeRuns
|
|
567
|
+
.filter((node) => node.outputText)
|
|
568
|
+
.slice(-3)
|
|
569
|
+
.map((node) => `\n${node.agentLabel || node.nodeId}:\n${node.outputText}`);
|
|
570
|
+
return truncate(`${lines.join('\n')}${outputs.join('\n')}`);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
async function monitorWorkflowRun({ bot, chatId, link, runId }) {
|
|
574
|
+
if (runMonitors.has(runId)) return;
|
|
575
|
+
runMonitors.set(runId, true);
|
|
576
|
+
const state = getState(link.user_id);
|
|
577
|
+
const seenNodeStatus = new Map();
|
|
578
|
+
try {
|
|
579
|
+
for (;;) {
|
|
580
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
581
|
+
const run = await fetchRun(link.user_id, runId);
|
|
582
|
+
const nodeRuns = Array.isArray(run.nodeRuns) ? run.nodeRuns : [];
|
|
583
|
+
if (state.progressMode === 'all') {
|
|
584
|
+
for (const node of nodeRuns) {
|
|
585
|
+
const key = `${node.nodeId}:${node.status}`;
|
|
586
|
+
if (!seenNodeStatus.has(key)) {
|
|
587
|
+
seenNodeStatus.set(key, true);
|
|
588
|
+
await send(bot, chatId, `${node.agentLabel || node.nodeId}: ${node.status}${node.error ? `\n${node.error}` : ''}`);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (state.progressMode === 'errors') {
|
|
593
|
+
for (const node of nodeRuns.filter((candidate) => candidate.error || candidate.status === 'failed')) {
|
|
594
|
+
const key = `${node.nodeId}:${node.status}:${node.error || ''}`;
|
|
595
|
+
if (!seenNodeStatus.has(key)) {
|
|
596
|
+
seenNodeStatus.set(key, true);
|
|
597
|
+
await send(bot, chatId, `${node.agentLabel || node.nodeId}: ${node.status}\n${node.error || ''}`);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
if (TERMINAL_RUN_STATES.has(run.status)) {
|
|
602
|
+
await send(bot, chatId, summarizeRun(run, state.progressMode));
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
} finally {
|
|
607
|
+
runMonitors.delete(runId);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
export async function startCliInstall({ bot, chatId, link, provider }) {
|
|
612
|
+
const lang = languageFor(link);
|
|
613
|
+
const data = await localApi(link.user_id, `/api/providers/${provider}/install`, { method: 'POST' });
|
|
614
|
+
if (data?.manual) {
|
|
615
|
+
await send(bot, chatId, t(lang, 'control.manualInstall', { provider, manual: data.manual }));
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
await send(bot, chatId, t(lang, 'control.installStarted', {
|
|
619
|
+
provider,
|
|
620
|
+
jobId: data.jobId,
|
|
621
|
+
command: data.installCmd || 'internal installer',
|
|
622
|
+
}));
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
async function showInstallMenu({ bot, chatId, link, editMessageId }) {
|
|
626
|
+
const lang = languageFor(link);
|
|
627
|
+
const buttons = PROVIDERS.map((provider) => button(provider, 'install_provider', { provider }));
|
|
628
|
+
await send(bot, chatId, t(lang, 'control.installMenu'), {
|
|
629
|
+
editMessageId,
|
|
630
|
+
reply_markup: { inline_keyboard: rows(buttons, 2) },
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
async function showAuthMenu({ bot, chatId, link, editMessageId }) {
|
|
635
|
+
const lang = languageFor(link);
|
|
636
|
+
const buttons = PROVIDERS.map((provider) => button(provider, 'auth_provider', { provider }));
|
|
637
|
+
await send(bot, chatId, t(lang, 'control.authMenu'), {
|
|
638
|
+
editMessageId,
|
|
639
|
+
reply_markup: { inline_keyboard: rows(buttons, 2) },
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
async function showSettings({ bot, chatId, link, editMessageId }) {
|
|
644
|
+
const lang = languageFor(link);
|
|
645
|
+
const state = getState(link.user_id);
|
|
646
|
+
const keyboard = [
|
|
647
|
+
[
|
|
648
|
+
button(
|
|
649
|
+
state.remoteControlEnabled
|
|
650
|
+
? checked(t(lang, 'control.button.remoteOn'))
|
|
651
|
+
: t(lang, 'control.button.remoteOff'),
|
|
652
|
+
'toggle_control',
|
|
653
|
+
),
|
|
654
|
+
],
|
|
655
|
+
[
|
|
656
|
+
button(state.progressMode === 'final' ? checked(t(lang, 'control.button.progressFinal')) : t(lang, 'control.button.progressFinal'), 'progress_mode', { progressMode: 'final' }),
|
|
657
|
+
button(state.progressMode === 'steps' ? checked(t(lang, 'control.button.progressSteps')) : t(lang, 'control.button.progressSteps'), 'progress_mode', { progressMode: 'steps' }),
|
|
658
|
+
button(state.progressMode === 'errors' ? checked(t(lang, 'control.button.progressErrors')) : t(lang, 'control.button.progressErrors'), 'progress_mode', { progressMode: 'errors' }),
|
|
659
|
+
button(state.progressMode === 'all' ? checked(t(lang, 'control.button.progressAll')) : t(lang, 'control.button.progressAll'), 'progress_mode', { progressMode: 'all' }),
|
|
660
|
+
],
|
|
661
|
+
[
|
|
662
|
+
button(t(lang, 'control.button.language'), 'language_menu'),
|
|
663
|
+
button(t(lang, 'control.button.mainMenu'), 'menu'),
|
|
664
|
+
],
|
|
665
|
+
];
|
|
666
|
+
await send(bot, chatId, `${t(lang, 'control.settingsTitle')}\n\n${stateSummary(lang, state)}`, {
|
|
667
|
+
editMessageId,
|
|
668
|
+
reply_markup: { inline_keyboard: keyboard },
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
async function showLanguageMenu({ bot, chatId, link, editMessageId }) {
|
|
673
|
+
const lang = languageFor(link);
|
|
674
|
+
const buttons = SUPPORTED_LANGUAGES.map((language) => button(language, 'language_select', { language }));
|
|
675
|
+
await send(bot, chatId, t(lang, 'control.pickLanguage'), {
|
|
676
|
+
editMessageId,
|
|
677
|
+
reply_markup: { inline_keyboard: rows(buttons, 3) },
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
async function handleAwaitingInput({ bot, chatId, link, text }) {
|
|
682
|
+
const state = getState(link.user_id);
|
|
683
|
+
const awaiting = state.awaiting;
|
|
684
|
+
if (!awaiting?.type) return false;
|
|
685
|
+
updateTelegramControlState(link.user_id, { awaiting: null });
|
|
686
|
+
|
|
687
|
+
if (awaiting.type === 'agent_prompt') {
|
|
688
|
+
await runAgent({ bot, chatId, link, prompt: text });
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
if (awaiting.type === 'workflow_prompt') {
|
|
692
|
+
await runWorkflow({ bot, chatId, link, input: text });
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
async function handleCommand({ bot, chatId, link, text }) {
|
|
699
|
+
const [commandRaw, ...rest] = text.split(/\s+/);
|
|
700
|
+
const command = normalizeCommandToken(commandRaw);
|
|
701
|
+
const argText = rest.join(' ').trim();
|
|
702
|
+
const lang = languageFor(link);
|
|
703
|
+
|
|
704
|
+
if (command === '/menu' || command === '/start' || command === 'menu') {
|
|
705
|
+
await showMainMenu({ bot, chatId, link });
|
|
706
|
+
return true;
|
|
707
|
+
}
|
|
708
|
+
if (command === '/help') {
|
|
709
|
+
await showHelp({ bot, chatId, link });
|
|
710
|
+
return true;
|
|
711
|
+
}
|
|
712
|
+
if (command === '/projects') {
|
|
713
|
+
await showProjectMenu({ bot, chatId, link });
|
|
714
|
+
return true;
|
|
715
|
+
}
|
|
716
|
+
if (command === '/provider') {
|
|
717
|
+
await showProviderMenu({ bot, chatId, link });
|
|
718
|
+
return true;
|
|
719
|
+
}
|
|
720
|
+
if (command === '/model' || command === '/models') {
|
|
721
|
+
await showModelMenu({ bot, chatId, link, refresh: command === '/models' && argText === 'refresh' });
|
|
722
|
+
return true;
|
|
723
|
+
}
|
|
724
|
+
if (command === '/workflows' || command === '/orchestration') {
|
|
725
|
+
await showWorkflowMenu({ bot, chatId, link });
|
|
726
|
+
return true;
|
|
727
|
+
}
|
|
728
|
+
if (command === '/runs') {
|
|
729
|
+
await showRuns({ bot, chatId, link });
|
|
730
|
+
return true;
|
|
731
|
+
}
|
|
732
|
+
if (command === '/approvals') {
|
|
733
|
+
await showApprovalQueue({ bot, chatId, link });
|
|
734
|
+
return true;
|
|
735
|
+
}
|
|
736
|
+
if (command === '/controlroom' || command === '/control-room') {
|
|
737
|
+
await showControlRoom({ bot, chatId, link });
|
|
738
|
+
return true;
|
|
739
|
+
}
|
|
740
|
+
if (command === '/webhooks') {
|
|
741
|
+
await showWebhookMenu({ bot, chatId, link });
|
|
742
|
+
return true;
|
|
743
|
+
}
|
|
744
|
+
if (command === '/sessions') {
|
|
745
|
+
await showSessions({ bot, chatId, link });
|
|
746
|
+
return true;
|
|
747
|
+
}
|
|
748
|
+
if (command === '/newchat') {
|
|
749
|
+
await startNewChat({ bot, chatId, link });
|
|
750
|
+
return true;
|
|
751
|
+
}
|
|
752
|
+
if (command === '/settings') {
|
|
753
|
+
await showSettings({ bot, chatId, link });
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
if (command === '/install') {
|
|
757
|
+
if (PROVIDERS.includes(argText)) await startCliInstall({ bot, chatId, link, provider: argText });
|
|
758
|
+
else await showInstallMenu({ bot, chatId, link });
|
|
759
|
+
return true;
|
|
760
|
+
}
|
|
761
|
+
if (command === '/auth') {
|
|
762
|
+
if (PROVIDERS.includes(argText)) await send(bot, chatId, `${argText} login:\n${AUTH_HELP[argText]}`);
|
|
763
|
+
else await showAuthMenu({ bot, chatId, link });
|
|
764
|
+
return true;
|
|
765
|
+
}
|
|
766
|
+
if (command === '/control') {
|
|
767
|
+
const enabled = argText !== 'off';
|
|
768
|
+
updateTelegramControlState(link.user_id, { remoteControlEnabled: enabled });
|
|
769
|
+
await send(bot, chatId, enabled ? t(lang, 'control.remoteEnabled') : t(lang, 'control.remoteDisabled'));
|
|
770
|
+
return true;
|
|
771
|
+
}
|
|
772
|
+
if (command === '/progress') {
|
|
773
|
+
if (!['final', 'steps', 'all', 'errors'].includes(argText)) {
|
|
774
|
+
await send(bot, chatId, t(lang, 'control.progressUsage'));
|
|
775
|
+
return true;
|
|
776
|
+
}
|
|
777
|
+
updateTelegramControlState(link.user_id, { progressMode: argText });
|
|
778
|
+
await send(bot, chatId, t(lang, 'control.progressSet', { mode: argText }));
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
if (command === '/chat') {
|
|
782
|
+
if (!argText) {
|
|
783
|
+
updateTelegramControlState(link.user_id, { awaiting: { type: 'agent_prompt' } });
|
|
784
|
+
await send(bot, chatId, t(lang, 'control.sendAgentPrompt'));
|
|
785
|
+
return true;
|
|
786
|
+
}
|
|
787
|
+
await runAgent({ bot, chatId, link, prompt: argText });
|
|
788
|
+
return true;
|
|
789
|
+
}
|
|
790
|
+
if (command === '/workflow' || command === '/orchestrate') {
|
|
791
|
+
if (!argText) {
|
|
792
|
+
updateTelegramControlState(link.user_id, { awaiting: { type: 'workflow_prompt' } });
|
|
793
|
+
await send(bot, chatId, t(lang, 'control.sendWorkflowPrompt'));
|
|
794
|
+
return true;
|
|
795
|
+
}
|
|
796
|
+
await runWorkflow({ bot, chatId, link, input: argText });
|
|
797
|
+
return true;
|
|
798
|
+
}
|
|
799
|
+
if (command === '/cancel') {
|
|
800
|
+
if (!argText) {
|
|
801
|
+
await send(bot, chatId, t(lang, 'control.cancelUsage'));
|
|
802
|
+
return true;
|
|
803
|
+
}
|
|
804
|
+
const run = await localApi(link.user_id, `/api/orchestration/workflows/runs/${encodeURIComponent(argText)}/cancel`, {
|
|
805
|
+
method: 'POST',
|
|
806
|
+
});
|
|
807
|
+
await send(bot, chatId, t(lang, 'control.runStatus', { runId: run.id, status: run.status }));
|
|
808
|
+
return true;
|
|
809
|
+
}
|
|
810
|
+
return false;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
export async function handleTelegramControlMessage({ bot, msg, link }) {
|
|
814
|
+
const chatId = msg.chat.id;
|
|
815
|
+
const text = String(msg.text || '').trim();
|
|
816
|
+
if (!text) return false;
|
|
817
|
+
|
|
818
|
+
if (text === '/') {
|
|
819
|
+
await showCommandPalette({ bot, chatId, link });
|
|
820
|
+
return true;
|
|
821
|
+
}
|
|
822
|
+
if (text.startsWith('/') && !getTelegramControlCommand(text)) {
|
|
823
|
+
await showCommandPalette({ bot, chatId, link, unknown: true });
|
|
824
|
+
return true;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
if (await handleAwaitingInput({ bot, chatId, link, text })) return true;
|
|
828
|
+
if (await handleCommand({ bot, chatId, link, text })) return true;
|
|
829
|
+
|
|
830
|
+
const state = getState(link.user_id);
|
|
831
|
+
if (!state.remoteControlEnabled) {
|
|
832
|
+
await send(bot, chatId, t(languageFor(link), 'control.disabled'));
|
|
833
|
+
return true;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
await runAgent({ bot, chatId, link, prompt: text });
|
|
837
|
+
return true;
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
async function showRunDetail({ bot, chatId, link, runId, editMessageId }) {
|
|
841
|
+
const lang = languageFor(link);
|
|
842
|
+
const run = await fetchRun(link.user_id, runId);
|
|
843
|
+
await send(bot, chatId, summarizeRun(run, 'all'), {
|
|
844
|
+
editMessageId,
|
|
845
|
+
reply_markup: {
|
|
846
|
+
inline_keyboard: [
|
|
847
|
+
[button(t(lang, 'control.button.cancelRun'), 'run_cancel', { runId }), button(t(lang, 'control.button.refresh'), 'run_detail', { runId })],
|
|
848
|
+
[button(t(lang, 'control.button.runs'), 'runs'), button(t(lang, 'control.button.mainMenu'), 'menu')],
|
|
849
|
+
],
|
|
850
|
+
},
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
export async function handleTelegramControlCallback({ bot, query, link }) {
|
|
855
|
+
const chatId = query.message?.chat?.id;
|
|
856
|
+
if (!chatId) return;
|
|
857
|
+
const editMessageId = query.message?.message_id;
|
|
858
|
+
const entry = readAction(query.data);
|
|
859
|
+
if (!entry) {
|
|
860
|
+
const lang = languageFor(link);
|
|
861
|
+
await bot.answerCallbackQuery(query.id, { text: t(lang, 'control.menuExpired') }).catch(() => {});
|
|
862
|
+
await send(bot, chatId, t(lang, 'control.menuExpiredLong'));
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
await bot.answerCallbackQuery(query.id).catch(() => {});
|
|
866
|
+
|
|
867
|
+
const { action, payload } = entry;
|
|
868
|
+
if (action === 'menu') return showMainMenu({ bot, chatId, link, editMessageId });
|
|
869
|
+
if (action === 'projects') return showProjectMenu({ bot, chatId, link, editMessageId });
|
|
870
|
+
if (action === 'providers') return showProviderMenu({ bot, chatId, link, editMessageId });
|
|
871
|
+
if (action === 'models') return showModelMenu({ bot, chatId, link, editMessageId });
|
|
872
|
+
if (action === 'models_refresh') return showModelMenu({ bot, chatId, link, refresh: true, editMessageId });
|
|
873
|
+
if (action === 'workflows') return showWorkflowMenu({ bot, chatId, link, editMessageId });
|
|
874
|
+
if (action === 'runs') return showRuns({ bot, chatId, link, editMessageId });
|
|
875
|
+
if (action === 'approvals') return showApprovalQueue({ bot, chatId, link, editMessageId });
|
|
876
|
+
if (action === 'control_room') return showControlRoom({ bot, chatId, link, editMessageId });
|
|
877
|
+
if (action === 'webhooks') return showWebhookMenu({ bot, chatId, link, editMessageId });
|
|
878
|
+
if (action === 'sessions') return showSessions({ bot, chatId, link, editMessageId });
|
|
879
|
+
if (action === 'new_chat') return startNewChat({ bot, chatId, link, editMessageId });
|
|
880
|
+
if (action === 'install_menu') return showInstallMenu({ bot, chatId, link, editMessageId });
|
|
881
|
+
if (action === 'auth_menu') return showAuthMenu({ bot, chatId, link, editMessageId });
|
|
882
|
+
if (action === 'settings') return showSettings({ bot, chatId, link, editMessageId });
|
|
883
|
+
if (action === 'language_menu') return showLanguageMenu({ bot, chatId, link, editMessageId });
|
|
884
|
+
if (action === 'project_select') {
|
|
885
|
+
updateTelegramControlState(link.user_id, {
|
|
886
|
+
selectedProjectName: payload.name,
|
|
887
|
+
selectedProjectPath: payload.path,
|
|
888
|
+
});
|
|
889
|
+
const lang = languageFor(link);
|
|
890
|
+
return showMainMenu({
|
|
891
|
+
bot,
|
|
892
|
+
chatId,
|
|
893
|
+
link,
|
|
894
|
+
editMessageId,
|
|
895
|
+
notice: t(lang, 'control.projectSelected', { project: payload.displayName || payload.name, path: payload.path }),
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
if (action === 'provider_select') {
|
|
899
|
+
updateTelegramControlState(link.user_id, { selectedProvider: payload.provider, selectedModel: null });
|
|
900
|
+
return showModelMenu({ bot, chatId, link, editMessageId });
|
|
901
|
+
}
|
|
902
|
+
if (action === 'model_select') {
|
|
903
|
+
updateTelegramControlState(link.user_id, { selectedModel: payload.model || null });
|
|
904
|
+
const lang = languageFor(link);
|
|
905
|
+
return showMainMenu({
|
|
906
|
+
bot,
|
|
907
|
+
chatId,
|
|
908
|
+
link,
|
|
909
|
+
editMessageId,
|
|
910
|
+
notice: payload.model
|
|
911
|
+
? t(lang, 'control.modelSelected', { model: payload.model })
|
|
912
|
+
: t(lang, 'control.modelCleared'),
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
if (action === 'workflow_select') {
|
|
916
|
+
updateTelegramControlState(link.user_id, { selectedWorkflowId: payload.workflowId });
|
|
917
|
+
const lang = languageFor(link);
|
|
918
|
+
return showMainMenu({
|
|
919
|
+
bot,
|
|
920
|
+
chatId,
|
|
921
|
+
link,
|
|
922
|
+
editMessageId,
|
|
923
|
+
notice: t(lang, 'control.workflowSelected', { workflow: payload.name || payload.workflowId }),
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
if (action === 'workflow_prompt') {
|
|
927
|
+
updateTelegramControlState(link.user_id, { awaiting: { type: 'workflow_prompt' } });
|
|
928
|
+
await send(bot, chatId, t(languageFor(link), 'control.sendWorkflowPrompt'), { editMessageId });
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
if (action === 'run_detail') return showRunDetail({ bot, chatId, link, runId: payload.runId, editMessageId });
|
|
932
|
+
if (action === 'run_cancel') {
|
|
933
|
+
const run = await localApi(link.user_id, `/api/orchestration/workflows/runs/${encodeURIComponent(payload.runId)}/cancel`, {
|
|
934
|
+
method: 'POST',
|
|
935
|
+
});
|
|
936
|
+
await send(bot, chatId, t(languageFor(link), 'control.runStatus', { runId: run.id, status: run.status }), { editMessageId });
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
if (action === 'approval_decide') {
|
|
940
|
+
const result = await localApi(link.user_id, `/api/orchestration/workflows/approvals/${encodeURIComponent(payload.approvalId)}`, {
|
|
941
|
+
method: 'POST',
|
|
942
|
+
body: {
|
|
943
|
+
allow: payload.allow === true,
|
|
944
|
+
source: 'telegram',
|
|
945
|
+
},
|
|
946
|
+
});
|
|
947
|
+
const lang = languageFor(link);
|
|
948
|
+
await send(bot, chatId, t(lang, 'control.approvalDecided', {
|
|
949
|
+
approvalId: payload.approvalId,
|
|
950
|
+
status: payload.allow === true ? 'approved' : 'denied',
|
|
951
|
+
runId: result?.runId || '',
|
|
952
|
+
}), { editMessageId });
|
|
953
|
+
return showApprovalQueue({ bot, chatId, link });
|
|
954
|
+
}
|
|
955
|
+
if (action === 'install_provider') return startCliInstall({ bot, chatId, link, provider: payload.provider });
|
|
956
|
+
if (action === 'auth_provider') {
|
|
957
|
+
await send(bot, chatId, `${payload.provider} login:\n${AUTH_HELP[payload.provider] || t(languageFor(link), 'control.providerAuthFallback')}`, { editMessageId });
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
if (action === 'toggle_control') {
|
|
961
|
+
const state = getState(link.user_id);
|
|
962
|
+
updateTelegramControlState(link.user_id, { remoteControlEnabled: !state.remoteControlEnabled });
|
|
963
|
+
return showSettings({ bot, chatId, link, editMessageId });
|
|
964
|
+
}
|
|
965
|
+
if (action === 'progress_mode') {
|
|
966
|
+
updateTelegramControlState(link.user_id, { progressMode: payload.progressMode });
|
|
967
|
+
return showSettings({ bot, chatId, link, editMessageId });
|
|
968
|
+
}
|
|
969
|
+
if (action === 'language_select') {
|
|
970
|
+
telegramLinksDb.updatePreferences(link.user_id, { language: payload.language });
|
|
971
|
+
return showMainMenu({
|
|
972
|
+
bot,
|
|
973
|
+
chatId,
|
|
974
|
+
link: { ...link, language: payload.language },
|
|
975
|
+
editMessageId,
|
|
976
|
+
notice: t(payload.language, 'control.languageSet', { language: payload.language }),
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
}
|