@stage-labs/metro 0.1.0-beta.2 → 0.1.0-beta.21
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/dist/api.js +103 -0
- package/dist/cli.js +134 -565
- package/dist/control.js +102 -0
- package/dist/prompt.js +10 -0
- package/dist/runtime.js +94 -0
- package/dist/store.js +70 -0
- package/dist/update.js +33 -0
- package/dist/version.js +97 -0
- package/package.json +34 -22
- package/runtime/node_modules/@metro-labs/discord/package.json +28 -0
- package/runtime/node_modules/@metro-labs/discord/src/accounts.ts +119 -0
- package/runtime/node_modules/@metro-labs/discord/src/actions.ts +388 -0
- package/runtime/node_modules/@metro-labs/discord/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord/src/format.ts +210 -0
- package/runtime/node_modules/@metro-labs/discord/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord/src/index.ts +127 -0
- package/runtime/node_modules/@metro-labs/discord/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord/src/station.ts +19 -0
- package/runtime/node_modules/@metro-labs/discord/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice-speak.ts +195 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice-transcribe.ts +292 -0
- package/runtime/node_modules/@metro-labs/discord/src/voice.ts +197 -0
- package/runtime/node_modules/@metro-labs/discord/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/package.json +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/dedupe.ts +24 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/inbound.ts +367 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/media-note.ts +85 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/pending.ts +75 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/account-api.ts +311 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/agent-api.ts +335 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/api-http.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-serve.ts +124 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/attach-session.ts +278 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/boot.ts +322 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair-api.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cli-pair.ts +20 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/collection-api.ts +202 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-api.ts +338 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-json.ts +31 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth-routes.ts +157 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-oauth.ts +127 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-tools.ts +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-url.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/connector-verify.ts +333 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/events.ts +117 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-auth.ts +164 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/google-oauth.ts +208 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/http.ts +357 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/identity.ts +79 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-client.ts +162 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-discovery.ts +120 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/oauth-pending.ts +65 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/pair-codes.ts +58 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/paths.ts +134 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/project-api.ts +214 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/protocol.ts +224 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-api.ts +125 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/run-pair.ts +18 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/runtime-source.ts +105 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/secure-fs.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session-apis.ts +41 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/session.ts +218 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/supervisor.ts +323 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/tunnel.ts +169 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-api.ts +295 -0
- package/runtime/node_modules/@metro-labs/mcp/src/daemon/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/account-attach.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-admin.ts +215 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-map.ts +71 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/agent-scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/client.ts +37 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-collections.ts +218 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connector-config.ts +147 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/connectors.ts +329 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/ids.ts +19 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/key-map.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/materialize.ts +364 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/projects.ts +300 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtime-admin.ts +31 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/runtimes.ts +105 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/schema.ts +131 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/token-carry.ts +45 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/users.ts +57 -0
- package/runtime/node_modules/@metro-labs/mcp/src/db/whatsapp-login.ts +18 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/accounts.ts +145 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/call-tools.ts +203 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/channel-owner.ts +29 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/event-store.ts +91 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/frame-scope.ts +21 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/index.ts +192 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/permission-relay.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/request-identity.ts +73 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-registry.ts +172 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session-route.ts +43 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/session.ts +211 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-dispatch.ts +212 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/tool-schemas.ts +397 -0
- package/runtime/node_modules/@metro-labs/mcp/src/mcp/upload-tool.ts +66 -0
- package/runtime/node_modules/@metro-labs/mcp/src/monitor/api.ts +240 -0
- package/runtime/node_modules/@metro-labs/mcp/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/account-store.ts +80 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-inline.ts +100 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-interactive.ts +161 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-resolve.ts +200 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attach.ts +182 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/lines.ts +82 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/registry.ts +35 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/station-runtime.ts +64 -0
- package/runtime/node_modules/@metro-labs/mcp/src/stations/types.ts +107 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +120 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +295 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +172 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +48 -0
- package/runtime/node_modules/@metro-labs/telegram/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +3 -0
- package/runtime/node_modules/@metro-labs/telegram-user/package.json +28 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/accounts.ts +81 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/actions.ts +254 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/format.ts +141 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/inbound.ts +78 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/index.ts +46 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/types.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram-user/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +29 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +67 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +178 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +318 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +132 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +141 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +98 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +5 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +113 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +299 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-push.ts +87 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +375 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-core.ts +55 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +333 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +78 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +33 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +125 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +157 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/push.ts +349 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +126 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +212 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +90 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +84 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +39 -0
- package/runtime/runtime.json +3 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -78
- package/dist/channels/discord.js +0 -148
- package/dist/channels/telegram.js +0 -137
- package/dist/lib/address.js +0 -21
- package/dist/lib/codex-rc.js +0 -274
- package/dist/lib/dotenv.js +0 -31
- package/dist/log.js +0 -12
- package/dist/paths.js +0 -45
- package/dist/tail.js +0 -161
- package/skills/metro/SKILL.md +0 -122
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/mcp/log';
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { unlink } from 'node:fs/promises';
|
|
4
|
+
import {
|
|
5
|
+
createAudioPlayer,
|
|
6
|
+
createAudioResource,
|
|
7
|
+
getVoiceConnection,
|
|
8
|
+
entersState,
|
|
9
|
+
AudioPlayerStatus,
|
|
10
|
+
StreamType,
|
|
11
|
+
NoSubscriberBehavior,
|
|
12
|
+
type VoiceConnection,
|
|
13
|
+
} from '@discordjs/voice';
|
|
14
|
+
import type { Client } from 'discord.js';
|
|
15
|
+
import { accountFor, accounts, routeOf } from './accounts.js';
|
|
16
|
+
import { respond } from './wire.js';
|
|
17
|
+
|
|
18
|
+
const TTS_VOICE = 'Samantha';
|
|
19
|
+
|
|
20
|
+
function clientFor(account?: string): { accountId: string; client: Client } {
|
|
21
|
+
const accountId = accountFor({ account });
|
|
22
|
+
const acct = accounts.get(accountId);
|
|
23
|
+
if (!acct) throw new Error(`unknown account '${accountId}'`);
|
|
24
|
+
return { accountId, client: acct.client };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function run(cmd: string, args: string[]): Promise<void> {
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
const p = spawn(cmd, args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
|
30
|
+
let err = '';
|
|
31
|
+
p.stderr?.on('data', (d: Buffer) => {
|
|
32
|
+
err += d.toString();
|
|
33
|
+
});
|
|
34
|
+
p.on('error', reject);
|
|
35
|
+
p.on('close', (code) => {
|
|
36
|
+
if (code === 0) resolve();
|
|
37
|
+
else reject(new Error(`${cmd} exited ${code}: ${err.slice(-400)}`));
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function channelName(ch: unknown, fallback: string): string {
|
|
43
|
+
return ch && typeof ch === 'object' && 'name' in ch
|
|
44
|
+
? String((ch as { name?: string }).name)
|
|
45
|
+
: fallback;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function resolveChannelId(args: {
|
|
49
|
+
channelId?: string;
|
|
50
|
+
line?: string;
|
|
51
|
+
account?: string;
|
|
52
|
+
}): string | undefined {
|
|
53
|
+
if (args.channelId) return args.channelId;
|
|
54
|
+
if (!args.line) return undefined;
|
|
55
|
+
try {
|
|
56
|
+
return routeOf(args.line, args.account).channelId;
|
|
57
|
+
} catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function connByChannel(
|
|
63
|
+
client: Client,
|
|
64
|
+
channelId: string | undefined,
|
|
65
|
+
): { conn: VoiceConnection; channelName: string } | null {
|
|
66
|
+
if (!channelId) return null;
|
|
67
|
+
const ch = client.channels.cache.get(channelId);
|
|
68
|
+
const gid =
|
|
69
|
+
ch && 'guildId' in ch ? (ch as { guildId: string }).guildId : undefined;
|
|
70
|
+
if (!gid) return null;
|
|
71
|
+
const conn = getVoiceConnection(gid);
|
|
72
|
+
if (!conn) return null;
|
|
73
|
+
return { conn, channelName: channelName(ch, gid) };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function connByGuildScan(
|
|
77
|
+
client: Client,
|
|
78
|
+
): { conn: VoiceConnection; channelName: string } | null {
|
|
79
|
+
for (const guild of client.guilds.cache.values()) {
|
|
80
|
+
const conn = getVoiceConnection(guild.id);
|
|
81
|
+
if (conn) {
|
|
82
|
+
const chId = (conn.joinConfig as { channelId?: string }).channelId;
|
|
83
|
+
const ch = chId ? client.channels.cache.get(chId) : undefined;
|
|
84
|
+
return { conn, channelName: channelName(ch, guild.name) };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function resolveConnection(
|
|
91
|
+
client: Client,
|
|
92
|
+
args: { channelId?: string; line?: string; account?: string },
|
|
93
|
+
): { conn: VoiceConnection; channelName: string } | { error: string } {
|
|
94
|
+
const channelId = resolveChannelId(args);
|
|
95
|
+
return (
|
|
96
|
+
connByChannel(client, channelId) ??
|
|
97
|
+
connByGuildScan(client) ?? {
|
|
98
|
+
error: 'no active voice connection — run joinVoice first',
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function speak(
|
|
104
|
+
id: string,
|
|
105
|
+
rawArgs: Record<string, unknown>,
|
|
106
|
+
): Promise<void> {
|
|
107
|
+
const args = rawArgs as {
|
|
108
|
+
account?: string;
|
|
109
|
+
text?: string;
|
|
110
|
+
channelId?: string;
|
|
111
|
+
line?: string;
|
|
112
|
+
voice?: string;
|
|
113
|
+
};
|
|
114
|
+
const text = (args.text ?? '').trim();
|
|
115
|
+
if (!text) {
|
|
116
|
+
respond(id, { result: { ok: false, error: 'text is required' } });
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const { accountId, client } = clientFor(args.account);
|
|
121
|
+
if (!client.isReady()) {
|
|
122
|
+
respond(id, {
|
|
123
|
+
result: { ok: false, error: `gateway not ready for '${accountId}'` },
|
|
124
|
+
});
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const resolved = resolveConnection(client, args);
|
|
129
|
+
if ('error' in resolved) {
|
|
130
|
+
respond(id, { result: { ok: false, error: resolved.error } });
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const { conn, channelName } = resolved;
|
|
134
|
+
|
|
135
|
+
const ts = Date.now();
|
|
136
|
+
const aiff = `/tmp/metro-tts-${ts}.aiff`;
|
|
137
|
+
const ogg = `/tmp/metro-tts-${ts}.ogg`;
|
|
138
|
+
const cleanup = async () => {
|
|
139
|
+
await Promise.allSettled([unlink(aiff), unlink(ogg)]);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
await run('say', ['-v', args.voice ?? TTS_VOICE, '-o', aiff, text]);
|
|
144
|
+
await run('ffmpeg', [
|
|
145
|
+
'-hide_banner',
|
|
146
|
+
'-loglevel',
|
|
147
|
+
'error',
|
|
148
|
+
'-y',
|
|
149
|
+
'-i',
|
|
150
|
+
aiff,
|
|
151
|
+
'-ac',
|
|
152
|
+
'2',
|
|
153
|
+
'-ar',
|
|
154
|
+
'48000',
|
|
155
|
+
'-c:a',
|
|
156
|
+
'libopus',
|
|
157
|
+
'-b:a',
|
|
158
|
+
'64k',
|
|
159
|
+
'-f',
|
|
160
|
+
'ogg',
|
|
161
|
+
ogg,
|
|
162
|
+
]);
|
|
163
|
+
|
|
164
|
+
const player = createAudioPlayer({
|
|
165
|
+
behaviors: { noSubscriber: NoSubscriberBehavior.Play },
|
|
166
|
+
});
|
|
167
|
+
const resource = createAudioResource(ogg, {
|
|
168
|
+
inputType: StreamType.OggOpus,
|
|
169
|
+
});
|
|
170
|
+
conn.subscribe(player);
|
|
171
|
+
try {
|
|
172
|
+
(conn as { rejoin: (c: object) => void }).rejoin({ selfMute: false });
|
|
173
|
+
} catch {
|
|
174
|
+
}
|
|
175
|
+
player.play(resource);
|
|
176
|
+
|
|
177
|
+
await entersState(player, AudioPlayerStatus.Playing, 10_000);
|
|
178
|
+
await entersState(player, AudioPlayerStatus.Idle, 120_000);
|
|
179
|
+
player.stop();
|
|
180
|
+
try {
|
|
181
|
+
(conn as { rejoin: (c: object) => void }).rejoin({ selfMute: true });
|
|
182
|
+
} catch {
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
respond(id, {
|
|
186
|
+
result: { ok: true, spoke: text, channelName, account: accountId },
|
|
187
|
+
});
|
|
188
|
+
} catch (err) {
|
|
189
|
+
respond(id, {
|
|
190
|
+
result: { ok: false, error: errMsg(err), account: accountId },
|
|
191
|
+
});
|
|
192
|
+
} finally {
|
|
193
|
+
await cleanup();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/mcp/log';
|
|
2
|
+
import {
|
|
3
|
+
EndBehaviorType,
|
|
4
|
+
type VoiceConnection,
|
|
5
|
+
type VoiceReceiver,
|
|
6
|
+
} from '@discordjs/voice';
|
|
7
|
+
import type { Client } from 'discord.js';
|
|
8
|
+
import { spawn, type ChildProcess } from 'node:child_process';
|
|
9
|
+
import {
|
|
10
|
+
createWriteStream,
|
|
11
|
+
existsSync,
|
|
12
|
+
mkdtempSync,
|
|
13
|
+
readFileSync,
|
|
14
|
+
rmSync,
|
|
15
|
+
} from 'node:fs';
|
|
16
|
+
import { tmpdir } from 'node:os';
|
|
17
|
+
import { join } from 'node:path';
|
|
18
|
+
import { pipeline } from 'node:stream/promises';
|
|
19
|
+
import prism from 'prism-media';
|
|
20
|
+
import { emitInbound } from './format.js';
|
|
21
|
+
import { mintId } from './wire.js';
|
|
22
|
+
|
|
23
|
+
const WHISPER_BIN = process.env.WHISPER_CLI ?? 'whisper-cli';
|
|
24
|
+
const WHISPER_MODEL =
|
|
25
|
+
process.env.WHISPER_MODEL ??
|
|
26
|
+
join(process.env.HOME ?? '', '.whisper-models', 'ggml-base.en.bin');
|
|
27
|
+
const FFMPEG_BIN = process.env.FFMPEG_BIN ?? 'ffmpeg';
|
|
28
|
+
const MIN_PCM_BYTES = 0.5 * 48000 * 2 * 2;
|
|
29
|
+
const TRANSCRIBE_TIMEOUT_MS = 120_000;
|
|
30
|
+
|
|
31
|
+
function killAfter(p: ChildProcess, ms: number): () => void {
|
|
32
|
+
const t = setTimeout(() => {
|
|
33
|
+
p.kill('SIGKILL');
|
|
34
|
+
}, ms);
|
|
35
|
+
return () => {
|
|
36
|
+
clearTimeout(t);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Session {
|
|
41
|
+
receiver: VoiceReceiver;
|
|
42
|
+
accountId: string;
|
|
43
|
+
guildId: string;
|
|
44
|
+
channelId: string;
|
|
45
|
+
client: Client;
|
|
46
|
+
active: Set<string>;
|
|
47
|
+
tmp: string;
|
|
48
|
+
enabled: boolean;
|
|
49
|
+
onStart?: (userId: string) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const sessions = new Map<string, Session>();
|
|
53
|
+
|
|
54
|
+
function run(bin: string, args: string[]): Promise<number> {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const p = spawn(bin, args, { stdio: ['ignore', 'ignore', 'ignore'] });
|
|
57
|
+
const cancel = killAfter(p, TRANSCRIBE_TIMEOUT_MS);
|
|
58
|
+
p.on('error', (e) => {
|
|
59
|
+
cancel();
|
|
60
|
+
reject(e);
|
|
61
|
+
});
|
|
62
|
+
p.on('close', (code) => {
|
|
63
|
+
cancel();
|
|
64
|
+
resolve(code ?? 0);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function captureUtterance(
|
|
70
|
+
s: Session,
|
|
71
|
+
userId: string,
|
|
72
|
+
pcmPath: string,
|
|
73
|
+
): Promise<number> {
|
|
74
|
+
const opus = s.receiver.subscribe(userId, {
|
|
75
|
+
end: { behavior: EndBehaviorType.AfterSilence, duration: 1000 },
|
|
76
|
+
});
|
|
77
|
+
const decoder = new prism.opus.Decoder({
|
|
78
|
+
rate: 48000,
|
|
79
|
+
channels: 2,
|
|
80
|
+
frameSize: 960,
|
|
81
|
+
});
|
|
82
|
+
let bytes = 0;
|
|
83
|
+
decoder.on('data', (c: Buffer) => {
|
|
84
|
+
bytes += c.length;
|
|
85
|
+
});
|
|
86
|
+
const out = createWriteStream(pcmPath);
|
|
87
|
+
await pipeline(opus, decoder, out);
|
|
88
|
+
return bytes;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function pcmToWav(pcmPath: string, wavPath: string): Promise<void> {
|
|
92
|
+
await run(FFMPEG_BIN, [
|
|
93
|
+
'-hide_banner',
|
|
94
|
+
'-loglevel',
|
|
95
|
+
'error',
|
|
96
|
+
'-y',
|
|
97
|
+
'-f',
|
|
98
|
+
's16le',
|
|
99
|
+
'-ar',
|
|
100
|
+
'48000',
|
|
101
|
+
'-ac',
|
|
102
|
+
'2',
|
|
103
|
+
'-i',
|
|
104
|
+
pcmPath,
|
|
105
|
+
'-ar',
|
|
106
|
+
'16000',
|
|
107
|
+
'-ac',
|
|
108
|
+
'1',
|
|
109
|
+
wavPath,
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function transcribe(wavPath: string): Promise<string> {
|
|
114
|
+
const ofBase = wavPath.replace(/\.wav$/, '');
|
|
115
|
+
return new Promise((resolve) => {
|
|
116
|
+
const p = spawn(
|
|
117
|
+
WHISPER_BIN,
|
|
118
|
+
[
|
|
119
|
+
'-m',
|
|
120
|
+
WHISPER_MODEL,
|
|
121
|
+
'-f',
|
|
122
|
+
wavPath,
|
|
123
|
+
'-otxt',
|
|
124
|
+
'-of',
|
|
125
|
+
ofBase,
|
|
126
|
+
'-np',
|
|
127
|
+
'-nt',
|
|
128
|
+
],
|
|
129
|
+
{ stdio: ['ignore', 'pipe', 'ignore'] },
|
|
130
|
+
);
|
|
131
|
+
const cancel = killAfter(p, TRANSCRIBE_TIMEOUT_MS);
|
|
132
|
+
let out = '';
|
|
133
|
+
p.stdout.on('data', (d: Buffer) => {
|
|
134
|
+
out += d.toString();
|
|
135
|
+
});
|
|
136
|
+
p.on('error', () => {
|
|
137
|
+
cancel();
|
|
138
|
+
resolve('');
|
|
139
|
+
});
|
|
140
|
+
p.on('close', () => {
|
|
141
|
+
cancel();
|
|
142
|
+
let text = out.trim();
|
|
143
|
+
if (!text && existsSync(`${ofBase}.txt`)) {
|
|
144
|
+
try {
|
|
145
|
+
text = readFileSync(`${ofBase}.txt`, 'utf8').trim();
|
|
146
|
+
} catch {
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
resolve(text);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const NOISE = new Set([
|
|
155
|
+
'',
|
|
156
|
+
'[blank_audio]',
|
|
157
|
+
'[silence]',
|
|
158
|
+
'[ Silence ]',
|
|
159
|
+
'(silence)',
|
|
160
|
+
'you',
|
|
161
|
+
'Thank you.',
|
|
162
|
+
]);
|
|
163
|
+
|
|
164
|
+
function speakerName(s: Session, userId: string): string {
|
|
165
|
+
const member = s.client.guilds.cache
|
|
166
|
+
.get(s.guildId)
|
|
167
|
+
?.members.cache.get(userId);
|
|
168
|
+
return member?.displayName ?? member?.user.username ?? userId;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function emitTranscript(
|
|
172
|
+
s: Session,
|
|
173
|
+
userId: string,
|
|
174
|
+
stamp: string,
|
|
175
|
+
text: string,
|
|
176
|
+
): void {
|
|
177
|
+
emitInbound(s.accountId, {
|
|
178
|
+
kind: 'inbound',
|
|
179
|
+
id: mintId(),
|
|
180
|
+
ts: new Date().toISOString(),
|
|
181
|
+
station: 'discord',
|
|
182
|
+
line: `metro://discord/${s.accountId}/voice/${s.channelId}`,
|
|
183
|
+
line_name: 'voice',
|
|
184
|
+
from: `metro://discord/${s.accountId}/user/${userId}`,
|
|
185
|
+
from_name: speakerName(s, userId),
|
|
186
|
+
message_id: stamp,
|
|
187
|
+
text,
|
|
188
|
+
is_private: false,
|
|
189
|
+
payload: {
|
|
190
|
+
kind: 'voice_transcript',
|
|
191
|
+
channel_id: s.channelId,
|
|
192
|
+
user_id: userId,
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function cleanupFiles(pcmPath: string, wavPath: string): void {
|
|
198
|
+
for (const f of [pcmPath, wavPath, `${wavPath.replace(/\.wav$/, '')}.txt`]) {
|
|
199
|
+
try {
|
|
200
|
+
rmSync(f, { force: true });
|
|
201
|
+
} catch {
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function transcribeUtterance(
|
|
207
|
+
s: Session,
|
|
208
|
+
userId: string,
|
|
209
|
+
pcmPath: string,
|
|
210
|
+
wavPath: string,
|
|
211
|
+
stamp: string,
|
|
212
|
+
): Promise<void> {
|
|
213
|
+
const bytes = await captureUtterance(s, userId, pcmPath);
|
|
214
|
+
if (bytes < MIN_PCM_BYTES) return;
|
|
215
|
+
await pcmToWav(pcmPath, wavPath);
|
|
216
|
+
const text = await transcribe(wavPath);
|
|
217
|
+
if (NOISE.has(text) || text.length < 2) return;
|
|
218
|
+
emitTranscript(s, userId, stamp, text);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function handleSpeaker(s: Session, userId: string): Promise<void> {
|
|
222
|
+
if (!s.enabled || s.active.has(userId)) return;
|
|
223
|
+
s.active.add(userId);
|
|
224
|
+
const stamp = `${userId}_${Date.now()}`;
|
|
225
|
+
const pcmPath = join(s.tmp, `${stamp}.pcm`);
|
|
226
|
+
const wavPath = join(s.tmp, `${stamp}.wav`);
|
|
227
|
+
try {
|
|
228
|
+
await transcribeUtterance(s, userId, pcmPath, wavPath, stamp);
|
|
229
|
+
} catch (err) {
|
|
230
|
+
process.stderr.write(
|
|
231
|
+
`discord voice transcribe error (${userId}): ${errMsg(err)}\n`,
|
|
232
|
+
);
|
|
233
|
+
} finally {
|
|
234
|
+
s.active.delete(userId);
|
|
235
|
+
cleanupFiles(pcmPath, wavPath);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function startTranscription(
|
|
240
|
+
guildId: string,
|
|
241
|
+
channelId: string,
|
|
242
|
+
accountId: string,
|
|
243
|
+
client: Client,
|
|
244
|
+
conn: VoiceConnection,
|
|
245
|
+
): void {
|
|
246
|
+
stopTranscription(guildId);
|
|
247
|
+
const receiver = conn.receiver;
|
|
248
|
+
const s: Session = {
|
|
249
|
+
receiver,
|
|
250
|
+
accountId,
|
|
251
|
+
guildId,
|
|
252
|
+
channelId,
|
|
253
|
+
client,
|
|
254
|
+
active: new Set(),
|
|
255
|
+
tmp: mkdtempSync(join(tmpdir(), 'metro-voice-')),
|
|
256
|
+
enabled: true,
|
|
257
|
+
};
|
|
258
|
+
s.onStart = (userId: string) => {
|
|
259
|
+
handleSpeaker(s, userId).catch((err: unknown) => {
|
|
260
|
+
process.stderr.write(
|
|
261
|
+
`discord[${accountId}] voice speaker failed: ${errMsg(err)}\n`,
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
receiver.speaking.on('start', s.onStart);
|
|
266
|
+
sessions.set(guildId, s);
|
|
267
|
+
process.stderr.write(
|
|
268
|
+
`discord[${accountId}] voice transcription armed on ${channelId}\n`,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function setTranscription(guildId: string, on: boolean): boolean {
|
|
273
|
+
const s = sessions.get(guildId);
|
|
274
|
+
if (!s) return false;
|
|
275
|
+
s.enabled = on;
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function stopTranscription(guildId: string): void {
|
|
280
|
+
const s = sessions.get(guildId);
|
|
281
|
+
if (!s) return;
|
|
282
|
+
try {
|
|
283
|
+
if (s.onStart) s.receiver.speaking.removeListener('start', s.onStart);
|
|
284
|
+
} catch {
|
|
285
|
+
}
|
|
286
|
+
s.enabled = false;
|
|
287
|
+
try {
|
|
288
|
+
rmSync(s.tmp, { recursive: true, force: true });
|
|
289
|
+
} catch {
|
|
290
|
+
}
|
|
291
|
+
sessions.delete(guildId);
|
|
292
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { errMsg } from '@metro-labs/mcp/log';
|
|
2
|
+
import {
|
|
3
|
+
joinVoiceChannel,
|
|
4
|
+
getVoiceConnection,
|
|
5
|
+
VoiceConnectionStatus,
|
|
6
|
+
entersState,
|
|
7
|
+
type VoiceConnection,
|
|
8
|
+
} from '@discordjs/voice';
|
|
9
|
+
import type { Client, VoiceBasedChannel } from 'discord.js';
|
|
10
|
+
import { accountFor, accounts } from './accounts.js';
|
|
11
|
+
import { respond } from './wire.js';
|
|
12
|
+
import {
|
|
13
|
+
startTranscription,
|
|
14
|
+
stopTranscription,
|
|
15
|
+
setTranscription,
|
|
16
|
+
} from './voice-transcribe.js';
|
|
17
|
+
|
|
18
|
+
const DEFAULT_USERNAME = 'bonustrack_';
|
|
19
|
+
|
|
20
|
+
function clientFor(account?: string): { accountId: string; client: Client } {
|
|
21
|
+
const accountId = accountFor({ account });
|
|
22
|
+
const acct = accounts.get(accountId);
|
|
23
|
+
if (!acct) throw new Error(`unknown account '${accountId}'`);
|
|
24
|
+
return { accountId, client: acct.client };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function voiceStateMatches(
|
|
28
|
+
vs: { id: string; member?: { user: { username?: string } } | null },
|
|
29
|
+
opts: { userId?: string; username?: string },
|
|
30
|
+
): boolean {
|
|
31
|
+
if (opts.userId && vs.id === opts.userId) return true;
|
|
32
|
+
return Boolean(
|
|
33
|
+
opts.username &&
|
|
34
|
+
vs.member?.user.username?.toLowerCase() === opts.username.toLowerCase(),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function findUserVoiceChannel(
|
|
39
|
+
client: Client,
|
|
40
|
+
opts: { userId?: string; username?: string },
|
|
41
|
+
): VoiceBasedChannel | null {
|
|
42
|
+
for (const guild of client.guilds.cache.values()) {
|
|
43
|
+
for (const vs of guild.voiceStates.cache.values()) {
|
|
44
|
+
if (!vs.channelId) continue;
|
|
45
|
+
if (voiceStateMatches(vs, opts)) return vs.channel ?? null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function resolveTarget(
|
|
52
|
+
client: Client,
|
|
53
|
+
args: {
|
|
54
|
+
guildId?: string;
|
|
55
|
+
channelId?: string;
|
|
56
|
+
userId?: string;
|
|
57
|
+
username?: string;
|
|
58
|
+
},
|
|
59
|
+
): Promise<VoiceBasedChannel> {
|
|
60
|
+
if (args.channelId) {
|
|
61
|
+
const ch = await client.channels.fetch(args.channelId);
|
|
62
|
+
if (!ch || !('guild' in ch) || !ch.isVoiceBased()) {
|
|
63
|
+
throw new Error(`channel ${args.channelId} is not a voice channel`);
|
|
64
|
+
}
|
|
65
|
+
return ch;
|
|
66
|
+
}
|
|
67
|
+
const ch = findUserVoiceChannel(client, {
|
|
68
|
+
userId: args.userId,
|
|
69
|
+
username: args.userId ? undefined : (args.username ?? DEFAULT_USERNAME),
|
|
70
|
+
});
|
|
71
|
+
if (!ch) {
|
|
72
|
+
const who = args.userId ?? args.username ?? DEFAULT_USERNAME;
|
|
73
|
+
throw new Error(
|
|
74
|
+
`could not find a voice channel for '${who}' — ` +
|
|
75
|
+
'is the user connected to voice in a guild the bot shares, and is the ' +
|
|
76
|
+
'GuildVoiceStates intent enabled? (full daemon restart needed after intent change)',
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return ch;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function joinVoice(
|
|
83
|
+
id: string,
|
|
84
|
+
rawArgs: Record<string, unknown>,
|
|
85
|
+
): Promise<void> {
|
|
86
|
+
const args = rawArgs as {
|
|
87
|
+
account?: string;
|
|
88
|
+
guildId?: string;
|
|
89
|
+
channelId?: string;
|
|
90
|
+
userId?: string;
|
|
91
|
+
username?: string;
|
|
92
|
+
};
|
|
93
|
+
const { accountId, client } = clientFor(args.account);
|
|
94
|
+
if (!client.isReady()) {
|
|
95
|
+
respond(id, { error: `gateway not ready for '${accountId}'` });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const channel = await resolveTarget(client, args);
|
|
100
|
+
const guild = channel.guild;
|
|
101
|
+
|
|
102
|
+
const connection: VoiceConnection = joinVoiceChannel({
|
|
103
|
+
channelId: channel.id,
|
|
104
|
+
guildId: guild.id,
|
|
105
|
+
adapterCreator: guild.voiceAdapterCreator,
|
|
106
|
+
selfDeaf: false,
|
|
107
|
+
selfMute: true,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
await entersState(connection, VoiceConnectionStatus.Ready, 15_000);
|
|
112
|
+
} catch (err) {
|
|
113
|
+
connection.destroy();
|
|
114
|
+
throw new Error(
|
|
115
|
+
`voice connection failed to become Ready: ${errMsg(err)}`,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
startTranscription(guild.id, channel.id, accountId, client, connection);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
process.stderr.write(
|
|
123
|
+
`voice transcription arm failed: ${errMsg(err)}\n`,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
respond(id, {
|
|
128
|
+
result: {
|
|
129
|
+
ok: true,
|
|
130
|
+
account: accountId,
|
|
131
|
+
guildId: guild.id,
|
|
132
|
+
guildName: guild.name,
|
|
133
|
+
channelId: channel.id,
|
|
134
|
+
channelName: channel.name,
|
|
135
|
+
status: connection.state.status,
|
|
136
|
+
transcribing: true,
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function voiceDebug(id: string, rawArgs: Record<string, unknown>): void {
|
|
142
|
+
const args = rawArgs as { account?: string };
|
|
143
|
+
const { accountId, client } = clientFor(args.account);
|
|
144
|
+
const guilds = [...client.guilds.cache.values()].map((g) => {
|
|
145
|
+
const voiceChannels = [...g.channels.cache.values()]
|
|
146
|
+
.filter((c) => c.isVoiceBased())
|
|
147
|
+
.map((c) => ({ id: c.id, name: c.name }));
|
|
148
|
+
const occupants = [...g.voiceStates.cache.values()]
|
|
149
|
+
.filter((vs) => vs.channelId)
|
|
150
|
+
.map((vs) => ({
|
|
151
|
+
channelId: vs.channelId,
|
|
152
|
+
channelName: vs.channel?.name ?? null,
|
|
153
|
+
userId: vs.id,
|
|
154
|
+
username: vs.member?.user.username ?? null,
|
|
155
|
+
}));
|
|
156
|
+
return { id: g.id, name: g.name, voiceChannels, occupants };
|
|
157
|
+
});
|
|
158
|
+
respond(id, {
|
|
159
|
+
result: { ok: true, account: accountId, ready: client.isReady(), guilds },
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function leaveVoice(id: string, rawArgs: Record<string, unknown>): void {
|
|
164
|
+
const args = rawArgs as { account?: string; guildId?: string };
|
|
165
|
+
const { accountId, client } = clientFor(args.account);
|
|
166
|
+
|
|
167
|
+
const guildIds = args.guildId
|
|
168
|
+
? [args.guildId]
|
|
169
|
+
: [...client.guilds.cache.keys()];
|
|
170
|
+
const left: string[] = [];
|
|
171
|
+
for (const gid of guildIds) {
|
|
172
|
+
const conn = getVoiceConnection(gid);
|
|
173
|
+
if (conn) {
|
|
174
|
+
stopTranscription(gid);
|
|
175
|
+
conn.destroy();
|
|
176
|
+
left.push(gid);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
respond(id, { result: { ok: true, account: accountId, leftGuilds: left } });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function voiceTranscribe(
|
|
183
|
+
id: string,
|
|
184
|
+
rawArgs: Record<string, unknown>,
|
|
185
|
+
): void {
|
|
186
|
+
const args = rawArgs as { account?: string; guildId?: string; on?: boolean };
|
|
187
|
+
const { accountId, client } = clientFor(args.account);
|
|
188
|
+
const on = args.on !== false;
|
|
189
|
+
const guildIds = args.guildId
|
|
190
|
+
? [args.guildId]
|
|
191
|
+
: [...client.guilds.cache.keys()];
|
|
192
|
+
const toggled: string[] = [];
|
|
193
|
+
for (const gid of guildIds) if (setTranscription(gid, on)) toggled.push(gid);
|
|
194
|
+
respond(id, {
|
|
195
|
+
result: { ok: true, account: accountId, on, toggledGuilds: toggled },
|
|
196
|
+
});
|
|
197
|
+
}
|