pi-roundtable 0.47.3 → 0.48.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +5 -0
- package/docs/plugins.md +57 -6
- package/package.json +6 -1
- package/src/core/builtin/discord.ts +41 -0
- package/src/core/config/adapters.ts +10 -0
- package/src/core/contract/surface.ts +5 -4
- package/src/core/discord/discord-surface.ts +4 -0
- package/src/core/discord/voice/voice-clock.ts +51 -0
- package/src/core/discord/voice/voice-discord.ts +185 -0
- package/src/core/discord/voice/voice-hearing.ts +357 -0
- package/src/core/discord/voice/voice-link.ts +418 -0
- package/src/core/discord/voice/voice-service.ts +336 -0
- package/src/core/discord/voice/voice-session.ts +230 -0
- package/src/core/discord/voice/voice.ts +144 -0
- package/src/core/domain/block-support.ts +15 -9
- package/src/core/host/define-roundtable.ts +1 -0
- package/src/core/testing/fake-discord.ts +7 -2
- package/src/core/testing/fake-voice.ts +229 -0
- package/src/core/testing/plugin-stand-ins.ts +27 -0
- package/src/core/testing/test-host.ts +5 -1
- package/src/discord/index.ts +14 -0
- package/src/testing.ts +7 -19
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Discord voice as plugins use it: joining a guild's voice channel to hear who speaks. It names
|
|
2
|
+
// no discord.js type, so a test stands in for it with `fakeDiscordVoice()`.
|
|
3
|
+
|
|
4
|
+
/** Why a voice channel was not joined. */
|
|
5
|
+
export type VoiceJoinFailure =
|
|
6
|
+
| "forbidden" // the bot may not see the channel or connect to it
|
|
7
|
+
| "channel_gone" // the guild has no such channel (any more)
|
|
8
|
+
| "not_voice" // the channel is neither a voice nor a stage channel
|
|
9
|
+
| "full" // the channel is at its user limit, and the bot may not move members
|
|
10
|
+
| "unavailable" // Discord is not connected, the host is stopping, or the voice connection did not come up in time; asking again may work
|
|
11
|
+
| "already_joined"; // the bot has a session in this guild, or is joining one: Discord allows one per guild
|
|
12
|
+
|
|
13
|
+
/** Why a session ended. */
|
|
14
|
+
export type VoiceDisconnectReason =
|
|
15
|
+
| "left" // the plugin called `leave()`
|
|
16
|
+
| "removed" // the bot was disconnected from the channel: someone disconnected it, or the channel was deleted
|
|
17
|
+
| "moved" // someone moved the bot to another channel; a session never follows
|
|
18
|
+
| "connection_lost" // the voice connection dropped and did not come back within the resume time, or it heard nothing while someone spoke and neither setting it up again nor rejoining the channel brought the audio back
|
|
19
|
+
| "stopped"; // the host is stopping
|
|
20
|
+
|
|
21
|
+
/** Someone in a voice channel. */
|
|
22
|
+
export interface VoiceMember {
|
|
23
|
+
/** Their Discord user id. */
|
|
24
|
+
userId: string;
|
|
25
|
+
bot: boolean;
|
|
26
|
+
/** What the guild shows for them: the server nickname, else the account's display name. */
|
|
27
|
+
displayName: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Someone entered or left the session's channel. */
|
|
31
|
+
export interface VoiceMemberEvent {
|
|
32
|
+
kind: "joined" | "left";
|
|
33
|
+
member: VoiceMember;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Someone started or stopped speaking, as the session's utterances are cut. */
|
|
37
|
+
export interface VoiceSpeakingEvent {
|
|
38
|
+
/** The speaker's Discord user id. */
|
|
39
|
+
userId: string;
|
|
40
|
+
speaking: boolean;
|
|
41
|
+
/** When, by the host's clock, in milliseconds since the epoch. */
|
|
42
|
+
at: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The format of every utterance's `pcm`. */
|
|
46
|
+
export const VOICE_PCM = Object.freeze({
|
|
47
|
+
sampleRate: 48_000,
|
|
48
|
+
/** Discord sends stereo; the two channels are mixed down to one. */
|
|
49
|
+
channels: 1,
|
|
50
|
+
/** Signed 16-bit, little-endian. */
|
|
51
|
+
bytesPerSample: 2,
|
|
52
|
+
} as const);
|
|
53
|
+
|
|
54
|
+
/** What one person said from when they started until they went quiet. */
|
|
55
|
+
export interface VoiceUtterance {
|
|
56
|
+
/** The speaker's Discord user id. */
|
|
57
|
+
userId: string;
|
|
58
|
+
/**
|
|
59
|
+
* The decoded audio, in the format `VOICE_PCM` names: 48 kHz, signed 16-bit little-endian,
|
|
60
|
+
* mono. It holds the audio Discord sent and nothing for the pauses inside it, so its length
|
|
61
|
+
* may be shorter than `endedAt - startedAt`. The session keeps no copy.
|
|
62
|
+
*/
|
|
63
|
+
pcm: Uint8Array;
|
|
64
|
+
/** When its first audio arrived, by the host's clock, in milliseconds since the epoch. */
|
|
65
|
+
startedAt: number;
|
|
66
|
+
/** When its last audio arrived. */
|
|
67
|
+
endedAt: number;
|
|
68
|
+
/**
|
|
69
|
+
* True when it holds `maxUtteranceMs` of audio and the speaker went on: the rest is their next
|
|
70
|
+
* utterance, which always follows, so a run of `continues` utterances ends with one that says
|
|
71
|
+
* false. One that is full just as its speaker stops says false.
|
|
72
|
+
*/
|
|
73
|
+
continues: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** How a session cuts what it hears into utterances. */
|
|
77
|
+
export interface VoiceJoinOptions {
|
|
78
|
+
/** How long a speaker is quiet before their utterance ends, in milliseconds: 200 to 5,000. Default 800. */
|
|
79
|
+
silenceMs?: number;
|
|
80
|
+
/** The most audio one utterance holds, in milliseconds: 1,000 to 300,000. Default 30,000, about 2.9 MB. */
|
|
81
|
+
maxUtteranceMs?: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The session ended; it reports nothing after this. */
|
|
85
|
+
export interface VoiceDisconnectEvent {
|
|
86
|
+
reason: VoiceDisconnectReason;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** What `join` answers: the session, or why there is none. */
|
|
90
|
+
export type VoiceJoinResult =
|
|
91
|
+
| { ok: true; session: VoiceSession }
|
|
92
|
+
| {
|
|
93
|
+
ok: false;
|
|
94
|
+
failure: VoiceJoinFailure;
|
|
95
|
+
/** The failure in words, for a log or the person who asked; never audio. */
|
|
96
|
+
message: string;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The bot in one voice channel, muted and listening. Every `on…` returns a function that removes
|
|
101
|
+
* the listener; a listener that throws is logged and never stops the next.
|
|
102
|
+
*/
|
|
103
|
+
export interface VoiceSession {
|
|
104
|
+
readonly guildId: string;
|
|
105
|
+
readonly channelId: string;
|
|
106
|
+
/** Who is in the channel now, without the bot itself; empty once the session ended. */
|
|
107
|
+
members(): VoiceMember[];
|
|
108
|
+
/** Someone entered or left the channel. */
|
|
109
|
+
onMember(listener: (event: VoiceMemberEvent) => void): () => void;
|
|
110
|
+
/**
|
|
111
|
+
* Someone started or stopped speaking: `speaking: true` with the first audio of an utterance,
|
|
112
|
+
* `speaking: false` once they were quiet for `silenceMs`, just before the utterance is delivered.
|
|
113
|
+
*/
|
|
114
|
+
onSpeaking(listener: (event: VoiceSpeakingEvent) => void): () => void;
|
|
115
|
+
/**
|
|
116
|
+
* A finished utterance of one speaker. One arrives when the speaker went quiet, when their
|
|
117
|
+
* utterance is full at `maxUtteranceMs` and they go on (`continues: true`), when they left the
|
|
118
|
+
* channel, and when the session ends, so no audio heard is dropped.
|
|
119
|
+
*/
|
|
120
|
+
onUtterance(listener: (utterance: VoiceUtterance) => void): () => void;
|
|
121
|
+
/** The session ended, once; a listener added after the end is called at once. */
|
|
122
|
+
onDisconnect(listener: (event: VoiceDisconnectEvent) => void): () => void;
|
|
123
|
+
/** Why the session ended; undefined while it is open. */
|
|
124
|
+
ended(): VoiceDisconnectReason | undefined;
|
|
125
|
+
/** Leaves the channel and ends the session with `left`; does nothing once it ended. */
|
|
126
|
+
leave(): Promise<void>;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Discord voice for plugins: `DISCORD.voice`, there only when the host's `discord.voice` is on.
|
|
131
|
+
* The bot joins muted and not deafened, hears each speaker apart, and never speaks.
|
|
132
|
+
*/
|
|
133
|
+
export interface DiscordVoice {
|
|
134
|
+
/**
|
|
135
|
+
* Joins a voice or stage channel of a guild. Never rejects for something Discord refuses: the
|
|
136
|
+
* answer names the failure. Throws a RangeError for an option out of its range.
|
|
137
|
+
*/
|
|
138
|
+
join(
|
|
139
|
+
target: { guildId: string; channelId: string },
|
|
140
|
+
options?: VoiceJoinOptions,
|
|
141
|
+
): Promise<VoiceJoinResult>;
|
|
142
|
+
/** The open session in a guild, if there is one. */
|
|
143
|
+
session(guildId: string): VoiceSession | undefined;
|
|
144
|
+
}
|
|
@@ -19,9 +19,9 @@ export interface BlockSupport {
|
|
|
19
19
|
*/
|
|
20
20
|
readonly lang?: TextLang;
|
|
21
21
|
/**
|
|
22
|
-
* `false` = the surface's own guidance already says what
|
|
23
|
-
*
|
|
24
|
-
* surface does not draw every interactive kind.
|
|
22
|
+
* `false` = the surface's own guidance already says what becomes of a form, option-button or
|
|
23
|
+
* checklist block it does not draw, so core adds no line about them. Absent = core adds one
|
|
24
|
+
* whenever the surface does not draw every interactive kind.
|
|
25
25
|
*/
|
|
26
26
|
readonly blockNote?: false;
|
|
27
27
|
}
|
|
@@ -37,7 +37,9 @@ export interface ChannelSupport extends BlockSupport {
|
|
|
37
37
|
* channel's `support` lays what differs (a host's room on an app, which draws no blocks). A
|
|
38
38
|
* surface that does not draw every interactive kind (`options`, `suggest`, `form`, `checklist`,
|
|
39
39
|
* `review`) gets a closing line in its `formatting` that says so, so the model does not expect
|
|
40
|
-
* its reader to fill one in.
|
|
40
|
+
* its reader to fill one in. The line speaks of blocks written in a reply and of nothing else:
|
|
41
|
+
* a tool that asks the user on the surface's own cards, as `ask_user` does, is not a block.
|
|
42
|
+
* `lang` is the host's language for the text form of a block.
|
|
41
43
|
*/
|
|
42
44
|
export function supportOf(
|
|
43
45
|
surface:
|
|
@@ -73,16 +75,20 @@ export function supportOf(
|
|
|
73
75
|
|
|
74
76
|
/**
|
|
75
77
|
* What the model is told about the interactive kinds a surface does not draw: none when it draws
|
|
76
|
-
* them all. Such a block reaches the reader as a question to answer by typing.
|
|
78
|
+
* them all. Such a block reaches the reader as a question to answer by typing. It names blocks
|
|
79
|
+
* written in a reply only, and says so: read as "this chat has no buttons", it kept a model from
|
|
80
|
+
* `ask_user`, whose card with buttons the same chat shows.
|
|
77
81
|
*/
|
|
78
82
|
function interactiveNote(
|
|
79
83
|
blocks: readonly BlockKind[] | undefined,
|
|
80
84
|
): string | undefined {
|
|
81
85
|
const missing = INTERACTIVE_KINDS.filter((kind) => !blocks?.includes(kind));
|
|
82
86
|
if (missing.length === 0) return undefined;
|
|
83
|
-
const
|
|
87
|
+
const notDrawn =
|
|
84
88
|
missing.length === INTERACTIVE_KINDS.length
|
|
85
|
-
? "
|
|
86
|
-
:
|
|
87
|
-
|
|
89
|
+
? "Display blocks that ask for input (a form, option buttons or a checklist written as a fenced block in a reply) are"
|
|
90
|
+
: missing.length === 1
|
|
91
|
+
? `The display block \`${missing[0]}\` is`
|
|
92
|
+
: `The display blocks ${missing.map((kind) => `\`${kind}\``).join(", ")} are`;
|
|
93
|
+
return `${notDrawn} not drawn in this chat: the user sees one as text and answers by typing, so put the question in plain words instead of writing one. This is about blocks written in a reply only: a tool that asks the user, such as ask_user, works as its description says.`;
|
|
88
94
|
}
|
|
@@ -170,6 +170,7 @@ function discordAssembly(
|
|
|
170
170
|
? {}
|
|
171
171
|
: { formatting: discord.formatting }),
|
|
172
172
|
...(discord.memberEvents ? { memberEvents: true } : {}),
|
|
173
|
+
...(discord.voice ? { voice: true } : {}),
|
|
173
174
|
cardGraceMs: discord.cardGraceSeconds * 1_000,
|
|
174
175
|
cardCopies: discord.cardCopies,
|
|
175
176
|
}),
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
} from "../discord/interaction-module.ts";
|
|
9
9
|
import { commandGuard, ownerRootCommand } from "../discord/owner-command.ts";
|
|
10
10
|
import type { DiscordPoster } from "../discord/poster/poster.ts";
|
|
11
|
+
import type { DiscordVoice } from "../discord/voice/voice.ts";
|
|
11
12
|
import { silentLogger } from "../kernel/log.ts";
|
|
12
13
|
import { fakeDiscordPoster } from "./fake-poster.ts";
|
|
13
14
|
import { type ServicePair, servicePair } from "./service-pair.ts";
|
|
@@ -32,8 +33,10 @@ export interface FakeDiscord {
|
|
|
32
33
|
* A `DISCORD` service for a plugin that adds slash commands, without a Discord connection: the
|
|
33
34
|
* plugin's `commands.add` calls are recorded, and `compose()` shows the tree Discord would get.
|
|
34
35
|
* `rootCommand` is the root command's name, `roundtable` by default. `channelContext` is what
|
|
35
|
-
* `DISCORD.channelContext` answers, by default no context, as on a host with it off.
|
|
36
|
-
*
|
|
36
|
+
* `DISCORD.channelContext` answers, by default no context, as on a host with it off. `voice` is
|
|
37
|
+
* what `DISCORD.voice` is, such as a `fakeDiscordVoice()`; without it the plugin reads none, as on
|
|
38
|
+
* a host with `discord.voice` off. Only `commands`, `guard`, `poster`, `channelContext`, and
|
|
39
|
+
* `voice` are given; reading another member throws.
|
|
37
40
|
*/
|
|
38
41
|
export function fakeDiscord(
|
|
39
42
|
options: {
|
|
@@ -41,6 +44,7 @@ export function fakeDiscord(
|
|
|
41
44
|
rootCommand?: string;
|
|
42
45
|
channelContext?: DiscordServices["channelContext"];
|
|
43
46
|
poster?: DiscordPoster;
|
|
47
|
+
voice?: DiscordVoice;
|
|
44
48
|
} = {},
|
|
45
49
|
): FakeDiscord {
|
|
46
50
|
const collection = new CommandCollection();
|
|
@@ -57,6 +61,7 @@ export function fakeDiscord(
|
|
|
57
61
|
guard,
|
|
58
62
|
poster,
|
|
59
63
|
channelContext: options.channelContext ?? (async () => undefined),
|
|
64
|
+
voice: options.voice,
|
|
60
65
|
}),
|
|
61
66
|
poster,
|
|
62
67
|
commands: collection.registrar,
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DiscordVoice,
|
|
3
|
+
VoiceDisconnectReason,
|
|
4
|
+
VoiceMember,
|
|
5
|
+
} from "../discord/voice/voice.ts";
|
|
6
|
+
import { manualVoiceClock } from "../discord/voice/voice-clock.ts";
|
|
7
|
+
import {
|
|
8
|
+
type VoiceLinkHandlers,
|
|
9
|
+
type VoiceLinkStatus,
|
|
10
|
+
type VoiceStateChange,
|
|
11
|
+
voiceService,
|
|
12
|
+
} from "../discord/voice/voice-service.ts";
|
|
13
|
+
import { silentLogger } from "../kernel/log.ts";
|
|
14
|
+
|
|
15
|
+
/** A channel of the fake's Discord. */
|
|
16
|
+
export interface FakeVoiceChannel {
|
|
17
|
+
guildId: string;
|
|
18
|
+
/** Default `voice`; `text` stands for every channel that is not joined by voice. */
|
|
19
|
+
kind?: "voice" | "stage" | "text";
|
|
20
|
+
/** Who is in it; `enter` and `exit` change it. Default nobody. */
|
|
21
|
+
members?: VoiceMember[];
|
|
22
|
+
/** The bot may not see it or connect to it. */
|
|
23
|
+
forbidden?: boolean;
|
|
24
|
+
/** It is at its user limit. */
|
|
25
|
+
full?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What a test reads and drives on the fake voice, besides the `DiscordVoice` calls. */
|
|
29
|
+
export interface FakeDiscordVoice extends DiscordVoice {
|
|
30
|
+
/** The channels that exist, by id; a join of any other answers `channel_gone`. */
|
|
31
|
+
readonly channels: Map<string, FakeVoiceChannel>;
|
|
32
|
+
/** Whether Discord is connected; `false` makes a join answer `unavailable`. */
|
|
33
|
+
available: boolean;
|
|
34
|
+
/** Every join asked for, in order, whatever it answered. */
|
|
35
|
+
readonly joins: { guildId: string; channelId: string }[];
|
|
36
|
+
/** Every session that ended, in order, with why: `left` for the plugin's own `leave()`. */
|
|
37
|
+
readonly ended: {
|
|
38
|
+
guildId: string;
|
|
39
|
+
channelId: string;
|
|
40
|
+
reason: VoiceDisconnectReason;
|
|
41
|
+
}[];
|
|
42
|
+
/** The bot's user id, which is never among a session's members. */
|
|
43
|
+
readonly botId: string;
|
|
44
|
+
/** Someone enters a channel, leaving the one they were in. */
|
|
45
|
+
enter(channelId: string, member: VoiceMember): void;
|
|
46
|
+
/** Someone leaves a channel. */
|
|
47
|
+
exit(channelId: string, userId: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* One frame of someone speaking in a channel, as 48 kHz 16-bit mono PCM; they must be in it.
|
|
50
|
+
* A session in that channel hears it, and its clock moves on by the frame's length. Their
|
|
51
|
+
* utterance stays open until `quiet()`.
|
|
52
|
+
*/
|
|
53
|
+
audio(channelId: string, userId: string, pcm: Uint8Array): void;
|
|
54
|
+
/** Everyone stops speaking: five seconds pass, so every open utterance ends. */
|
|
55
|
+
quiet(): void;
|
|
56
|
+
/** `audio` for each frame, then `quiet()`: one whole utterance. */
|
|
57
|
+
say(channelId: string, userId: string, ...frames: Uint8Array[]): void;
|
|
58
|
+
/** Ends the guild's session from outside: the bot is disconnected, moved elsewhere, or loses its connection for good. */
|
|
59
|
+
drop(guildId: string, how: "removed" | "moved" | "connection_lost"): void;
|
|
60
|
+
/** The host stops: every session ends with `stopped`, and joins answer `unavailable`. */
|
|
61
|
+
stop(): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const BOT: VoiceMember = {
|
|
65
|
+
userId: "900000000000000001",
|
|
66
|
+
bot: true,
|
|
67
|
+
displayName: "bot",
|
|
68
|
+
};
|
|
69
|
+
/** Bytes of a millisecond of 48 kHz 16-bit mono. */
|
|
70
|
+
const BYTES_PER_MS = 96;
|
|
71
|
+
/** Longer than any `silenceMs` a join may ask for. */
|
|
72
|
+
const QUIET_MS = 5_000;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* `DISCORD.voice` in memory, for a plugin's tests: the real session logic (members, utterances,
|
|
76
|
+
* one session per guild, the join failures) over a Discord the test drives. Give it to
|
|
77
|
+
* `fakeDiscord({ voice })` or `testHost({ discord: { voice } })`. `now` is where its clock starts.
|
|
78
|
+
*/
|
|
79
|
+
export function fakeDiscordVoice(
|
|
80
|
+
options: { now?: number } = {},
|
|
81
|
+
): FakeDiscordVoice {
|
|
82
|
+
const clock = manualVoiceClock(options.now);
|
|
83
|
+
const channels = new Map<string, FakeVoiceChannel>();
|
|
84
|
+
const joins: FakeDiscordVoice["joins"] = [];
|
|
85
|
+
const ended: FakeDiscordVoice["ended"] = [];
|
|
86
|
+
/** The open connection of each guild: where the bot is, and who listens. */
|
|
87
|
+
const links = new Map<
|
|
88
|
+
string,
|
|
89
|
+
{ channelId: string; handlers: VoiceLinkHandlers | undefined }
|
|
90
|
+
>();
|
|
91
|
+
let report: (change: VoiceStateChange) => void = () => undefined;
|
|
92
|
+
let available = true;
|
|
93
|
+
|
|
94
|
+
const channelOf = (channelId: string, call: string): FakeVoiceChannel => {
|
|
95
|
+
const channel = channels.get(channelId);
|
|
96
|
+
if (!channel)
|
|
97
|
+
throw new Error(
|
|
98
|
+
`fakeDiscordVoice ${call}: there is no channel ${channelId}. Add it first: voice.channels.set("${channelId}", { guildId: "..." }).`,
|
|
99
|
+
);
|
|
100
|
+
return channel;
|
|
101
|
+
};
|
|
102
|
+
const without = (channel: FakeVoiceChannel, userId: string) =>
|
|
103
|
+
(channel.members ?? []).filter((member) => member.userId !== userId);
|
|
104
|
+
|
|
105
|
+
const service = voiceService({
|
|
106
|
+
clock,
|
|
107
|
+
logger: silentLogger(),
|
|
108
|
+
gateway: {
|
|
109
|
+
ready: () => available,
|
|
110
|
+
botId: () => BOT.userId,
|
|
111
|
+
channel: async (channelId) => {
|
|
112
|
+
const channel = channels.get(channelId);
|
|
113
|
+
if (!channel) return "gone";
|
|
114
|
+
return {
|
|
115
|
+
guildId: channel.guildId,
|
|
116
|
+
kind: channel.kind === "text" ? "other" : (channel.kind ?? "voice"),
|
|
117
|
+
connectable: !channel.forbidden,
|
|
118
|
+
full: channel.full ?? false,
|
|
119
|
+
members: without(channel, BOT.userId),
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
onVoiceState: (listener) => {
|
|
123
|
+
report = listener;
|
|
124
|
+
},
|
|
125
|
+
onReady: () => undefined,
|
|
126
|
+
prepare: async () => undefined,
|
|
127
|
+
connect: async ({ guildId, channelId }) => {
|
|
128
|
+
let status: VoiceLinkStatus = "ready";
|
|
129
|
+
const link = {
|
|
130
|
+
channelId,
|
|
131
|
+
handlers: undefined as VoiceLinkHandlers | undefined,
|
|
132
|
+
};
|
|
133
|
+
links.set(guildId, link);
|
|
134
|
+
return {
|
|
135
|
+
status: () => status,
|
|
136
|
+
listen: (handlers) => {
|
|
137
|
+
link.handlers = handlers;
|
|
138
|
+
},
|
|
139
|
+
forget: () => undefined,
|
|
140
|
+
leftItself: () => false,
|
|
141
|
+
close: () => {
|
|
142
|
+
status = "closed";
|
|
143
|
+
if (links.get(guildId) === link) links.delete(guildId);
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const fake: FakeDiscordVoice = {
|
|
151
|
+
channels,
|
|
152
|
+
get available() {
|
|
153
|
+
return available;
|
|
154
|
+
},
|
|
155
|
+
set available(value: boolean) {
|
|
156
|
+
available = value;
|
|
157
|
+
},
|
|
158
|
+
joins,
|
|
159
|
+
ended,
|
|
160
|
+
botId: BOT.userId,
|
|
161
|
+
join: async (target, given) => {
|
|
162
|
+
joins.push({ guildId: target.guildId, channelId: target.channelId });
|
|
163
|
+
const answer = await service.join(target, given);
|
|
164
|
+
if (answer.ok)
|
|
165
|
+
answer.session.onDisconnect(({ reason }) =>
|
|
166
|
+
ended.push({ ...target, reason }),
|
|
167
|
+
);
|
|
168
|
+
return answer;
|
|
169
|
+
},
|
|
170
|
+
session: (guildId) => service.session(guildId),
|
|
171
|
+
enter(channelId, member) {
|
|
172
|
+
const channel = channelOf(channelId, "enter");
|
|
173
|
+
for (const [id, other] of channels)
|
|
174
|
+
if (id !== channelId && other.guildId === channel.guildId)
|
|
175
|
+
other.members = without(other, member.userId);
|
|
176
|
+
channel.members = [...without(channel, member.userId), member];
|
|
177
|
+
report({
|
|
178
|
+
guildId: channel.guildId,
|
|
179
|
+
userId: member.userId,
|
|
180
|
+
channelId,
|
|
181
|
+
member,
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
exit(channelId, userId) {
|
|
185
|
+
const channel = channelOf(channelId, "exit");
|
|
186
|
+
const member = channel.members?.find((one) => one.userId === userId);
|
|
187
|
+
if (!member) return;
|
|
188
|
+
channel.members = without(channel, userId);
|
|
189
|
+
report({
|
|
190
|
+
guildId: channel.guildId,
|
|
191
|
+
userId,
|
|
192
|
+
channelId: undefined,
|
|
193
|
+
member,
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
audio(channelId, userId, pcm) {
|
|
197
|
+
const channel = channelOf(channelId, "audio");
|
|
198
|
+
if (!channel.members?.some((member) => member.userId === userId))
|
|
199
|
+
throw new Error(
|
|
200
|
+
`fakeDiscordVoice audio: ${userId} is not in the channel ${channelId}. Call enter("${channelId}", { userId: "${userId}", ... }) first.`,
|
|
201
|
+
);
|
|
202
|
+
const link = links.get(channel.guildId);
|
|
203
|
+
// Copied, as a decoder hands the session a frame of its own.
|
|
204
|
+
if (link?.channelId === channelId)
|
|
205
|
+
link.handlers?.frame(userId, pcm.slice());
|
|
206
|
+
clock.advance(pcm.byteLength / BYTES_PER_MS);
|
|
207
|
+
},
|
|
208
|
+
quiet() {
|
|
209
|
+
clock.advance(QUIET_MS);
|
|
210
|
+
},
|
|
211
|
+
say(channelId, userId, ...frames) {
|
|
212
|
+
for (const frame of frames) fake.audio(channelId, userId, frame);
|
|
213
|
+
fake.quiet();
|
|
214
|
+
},
|
|
215
|
+
drop(guildId, how) {
|
|
216
|
+
const link = links.get(guildId);
|
|
217
|
+
if (!link) return;
|
|
218
|
+
if (how === "connection_lost") return link.handlers?.status("closed");
|
|
219
|
+
report({
|
|
220
|
+
guildId,
|
|
221
|
+
userId: BOT.userId,
|
|
222
|
+
channelId: how === "moved" ? `${link.channelId}-elsewhere` : undefined,
|
|
223
|
+
member: BOT,
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
stop: () => service.stop(),
|
|
227
|
+
};
|
|
228
|
+
return fake;
|
|
229
|
+
}
|
|
@@ -4,7 +4,14 @@ import type { ServiceKey } from "../contract/services.ts";
|
|
|
4
4
|
import type { PendingConfirmation } from "../domain/conversation.ts";
|
|
5
5
|
import type { ChannelKey } from "../domain/sessions.ts";
|
|
6
6
|
import { NotLinkedError, PluginError } from "../kernel/errors.ts";
|
|
7
|
+
import type {
|
|
8
|
+
EventSink,
|
|
9
|
+
TurnEndEvent,
|
|
10
|
+
TurnEvent,
|
|
11
|
+
TurnProgressEvent,
|
|
12
|
+
} from "../plugin-api/plugin.ts";
|
|
7
13
|
import type { ChannelRouter } from "../routing/channel-router.ts";
|
|
14
|
+
import type { RecordedEvent } from "./test-plugin-types.ts";
|
|
8
15
|
|
|
9
16
|
/** The same pre-link failures and wording the host exposes during setup. */
|
|
10
17
|
export const NOT_LINKED = {
|
|
@@ -45,6 +52,26 @@ export function partialService<T extends object>(
|
|
|
45
52
|
});
|
|
46
53
|
}
|
|
47
54
|
|
|
55
|
+
/** What `testPlugin` reports the plugin's own events to: each is kept in `events` and delivered to the plugin's handlers. */
|
|
56
|
+
export function recordedSink(events: RecordedEvent[], sink: EventSink) {
|
|
57
|
+
return {
|
|
58
|
+
turnStarted: (turn: TurnEvent) => {
|
|
59
|
+
events.push({ name: "turnStarted", turn });
|
|
60
|
+
sink.turnStarted(turn);
|
|
61
|
+
},
|
|
62
|
+
turnEnded: (turn: TurnEndEvent) => {
|
|
63
|
+
events.push({ name: "turnEnded", turn });
|
|
64
|
+
sink.turnEnded(turn);
|
|
65
|
+
},
|
|
66
|
+
// Delivered to the plugin's handlers, not recorded: a turn reports many.
|
|
67
|
+
turnProgress: (event: TurnProgressEvent) => sink.turnProgress?.(event),
|
|
68
|
+
changed: () => {
|
|
69
|
+
events.push({ name: "changed" });
|
|
70
|
+
sink.changed();
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
48
75
|
/** Held actions kept in memory, for the runtime a plugin's provider builds under test. */
|
|
49
76
|
export function memoryHeldActions(): HeldActionStore {
|
|
50
77
|
const held = new Map<ChannelKey, PendingConfirmation>();
|
|
@@ -22,6 +22,7 @@ import type {
|
|
|
22
22
|
} from "../discord/interaction-module.ts";
|
|
23
23
|
import { commandGuard, ownerRootCommand } from "../discord/owner-command.ts";
|
|
24
24
|
import type { DiscordPoster } from "../discord/poster/poster.ts";
|
|
25
|
+
import type { DiscordVoice } from "../discord/voice/voice.ts";
|
|
25
26
|
import type {
|
|
26
27
|
AgentTurnScope,
|
|
27
28
|
ChannelKey,
|
|
@@ -72,6 +73,8 @@ export interface TestHostOptions {
|
|
|
72
73
|
> & {
|
|
73
74
|
/** What `DISCORD.poster` is; by default a `fakeDiscordPoster()`. */
|
|
74
75
|
poster?: DiscordPoster;
|
|
76
|
+
/** What `DISCORD.voice` is, such as a `fakeDiscordVoice()`; by default none, as with `discord.voice` off. */
|
|
77
|
+
voice?: DiscordVoice;
|
|
75
78
|
});
|
|
76
79
|
}
|
|
77
80
|
|
|
@@ -202,7 +205,7 @@ export function standInDiscord(
|
|
|
202
205
|
replaces: [DISCORD],
|
|
203
206
|
setup: ({ services, dashboard }) => {
|
|
204
207
|
const identity = services.find(IDENTITY);
|
|
205
|
-
const { poster, ...overrides } = given ?? {};
|
|
208
|
+
const { poster, voice, ...overrides } = given ?? {};
|
|
206
209
|
const ownerChannel =
|
|
207
210
|
overrides.ownerChannel ?? (async () => "discord:owner-dm" as const);
|
|
208
211
|
// Each person's direct messages, as the Discord plugin's provider reaches them; the owner's is the owner channel.
|
|
@@ -229,6 +232,7 @@ export function standInDiscord(
|
|
|
229
232
|
threads: { open: async () => undefined, sweep: async () => undefined },
|
|
230
233
|
poster: poster ?? fakeDiscordPoster(),
|
|
231
234
|
channelContext,
|
|
235
|
+
...(voice ? { voice } : {}),
|
|
232
236
|
} as unknown as DiscordServices);
|
|
233
237
|
// Discord's side of the agent guild, as the built-in plugin contributes it with one, over a board that shows nothing.
|
|
234
238
|
const guild = agentGuild
|
package/src/discord/index.ts
CHANGED
|
@@ -118,5 +118,19 @@ export {
|
|
|
118
118
|
DiscordPosterError,
|
|
119
119
|
ROLE_LIMITS,
|
|
120
120
|
} from "../core/discord/poster/poster.ts";
|
|
121
|
+
export type {
|
|
122
|
+
DiscordVoice,
|
|
123
|
+
VoiceDisconnectEvent,
|
|
124
|
+
VoiceDisconnectReason,
|
|
125
|
+
VoiceJoinFailure,
|
|
126
|
+
VoiceJoinOptions,
|
|
127
|
+
VoiceJoinResult,
|
|
128
|
+
VoiceMember,
|
|
129
|
+
VoiceMemberEvent,
|
|
130
|
+
VoiceSession,
|
|
131
|
+
VoiceSpeakingEvent,
|
|
132
|
+
VoiceUtterance,
|
|
133
|
+
} from "../core/discord/voice/voice.ts";
|
|
134
|
+
export { VOICE_PCM } from "../core/discord/voice/voice.ts";
|
|
121
135
|
export type { OwnerOperations } from "../core/modules/discord-admin/discord-admin.ts";
|
|
122
136
|
export { DISCORD_ADMIN_TOOLS } from "../core/modules/discord-admin/discord-admin.ts";
|
package/src/testing.ts
CHANGED
|
@@ -26,9 +26,6 @@ import type {
|
|
|
26
26
|
HostEnv,
|
|
27
27
|
LinkedSessions,
|
|
28
28
|
RoundtablePlugin,
|
|
29
|
-
TurnEndEvent,
|
|
30
|
-
TurnEvent,
|
|
31
|
-
TurnProgressEvent,
|
|
32
29
|
} from "./core/plugin-api/plugin.ts";
|
|
33
30
|
import {
|
|
34
31
|
AGENTS,
|
|
@@ -61,6 +58,7 @@ import { memberOf } from "./core/testing/partial.ts";
|
|
|
61
58
|
import {
|
|
62
59
|
memoryHeldActions,
|
|
63
60
|
partialService,
|
|
61
|
+
recordedSink,
|
|
64
62
|
routedConversations,
|
|
65
63
|
unlinked,
|
|
66
64
|
} from "./core/testing/plugin-stand-ins.ts";
|
|
@@ -97,6 +95,11 @@ export type {
|
|
|
97
95
|
FakeRoleChange,
|
|
98
96
|
} from "./core/testing/fake-poster.ts";
|
|
99
97
|
export { fakeDiscordPoster } from "./core/testing/fake-poster.ts";
|
|
98
|
+
export type {
|
|
99
|
+
FakeDiscordVoice,
|
|
100
|
+
FakeVoiceChannel,
|
|
101
|
+
} from "./core/testing/fake-voice.ts";
|
|
102
|
+
export { fakeDiscordVoice } from "./core/testing/fake-voice.ts";
|
|
100
103
|
export type { TestLocale } from "./core/testing/locale.ts";
|
|
101
104
|
export { useTestLocale } from "./core/testing/locale.ts";
|
|
102
105
|
export { OWNER_SPEAKER } from "./core/testing/owner.ts";
|
|
@@ -190,22 +193,7 @@ export async function testPlugin(
|
|
|
190
193
|
: unlinked("surfaces"),
|
|
191
194
|
);
|
|
192
195
|
const observers = new MessageObservers(logger, () => surfaces.roomSurfaces());
|
|
193
|
-
const sink =
|
|
194
|
-
turnStarted: (turn: TurnEvent) => {
|
|
195
|
-
events.push({ name: "turnStarted", turn });
|
|
196
|
-
bus.sink.turnStarted(turn);
|
|
197
|
-
},
|
|
198
|
-
turnEnded: (turn: TurnEndEvent) => {
|
|
199
|
-
events.push({ name: "turnEnded", turn });
|
|
200
|
-
bus.sink.turnEnded(turn);
|
|
201
|
-
},
|
|
202
|
-
// Delivered to the plugin's handlers, not recorded: a turn reports many.
|
|
203
|
-
turnProgress: (event: TurnProgressEvent) => bus.sink.turnProgress?.(event),
|
|
204
|
-
changed: () => {
|
|
205
|
-
events.push({ name: "changed" });
|
|
206
|
-
bus.sink.changed();
|
|
207
|
-
},
|
|
208
|
-
};
|
|
196
|
+
const sink = recordedSink(events, bus.sink);
|
|
209
197
|
const turns: ConversationTurns =
|
|
210
198
|
options.turns ??
|
|
211
199
|
conversationTurns({
|