@copilotkit/channels-discord 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"await-choice.test.d.ts","sourceRoot":"","sources":["../src/await-choice.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
2
|
+
import { createChannel, FakeAdapter } from "@copilotkit/channels-core";
|
|
3
|
+
import { Actions, Button, Select } from "@copilotkit/channels-ui";
|
|
4
|
+
import { renderDiscordMessage } from "./render/components-v2.js";
|
|
5
|
+
import { decodeInteraction } from "./interaction.js";
|
|
6
|
+
/**
|
|
7
|
+
* `thread.awaitChoice` with Discord's own component encoding: posts are
|
|
8
|
+
* rendered by the real Components V2 renderer and clicks decoded by the real
|
|
9
|
+
* interaction decoder. Only the transport is faked.
|
|
10
|
+
*/
|
|
11
|
+
const PENDING = Symbol("pending");
|
|
12
|
+
const CHANNEL_ID = "1100000000000000001";
|
|
13
|
+
const alice = { id: "2200000000000000001", kind: "human" };
|
|
14
|
+
const activeChannels = [];
|
|
15
|
+
const tick = async () => {
|
|
16
|
+
for (let i = 0; i < 10; i++)
|
|
17
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
18
|
+
};
|
|
19
|
+
/** Every button `custom_id` on the components rendered for post `index`. */
|
|
20
|
+
function buttonCustomIds(fake, index) {
|
|
21
|
+
const json = renderDiscordMessage(fake.posted[index]).components.map((c) => c.toJSON());
|
|
22
|
+
const ids = [];
|
|
23
|
+
const visit = (value) => {
|
|
24
|
+
if (Array.isArray(value)) {
|
|
25
|
+
for (const item of value)
|
|
26
|
+
visit(item);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (typeof value !== "object" || value === null)
|
|
30
|
+
return;
|
|
31
|
+
const el = value;
|
|
32
|
+
// Component type 2 is a button.
|
|
33
|
+
if (el.type === 2 && typeof el.custom_id === "string") {
|
|
34
|
+
ids.push(el.custom_id);
|
|
35
|
+
}
|
|
36
|
+
for (const child of Object.values(value))
|
|
37
|
+
visit(child);
|
|
38
|
+
};
|
|
39
|
+
visit(json);
|
|
40
|
+
return ids;
|
|
41
|
+
}
|
|
42
|
+
/** Click a button the way discord.js delivers it, through the real decoder. */
|
|
43
|
+
function clickButton(fake, customId, messageId) {
|
|
44
|
+
const evt = decodeInteraction({
|
|
45
|
+
isButton: () => true,
|
|
46
|
+
isStringSelectMenu: () => false,
|
|
47
|
+
customId,
|
|
48
|
+
channelId: CHANNEL_ID,
|
|
49
|
+
message: { id: messageId },
|
|
50
|
+
user: { id: alice.id, username: "alice" },
|
|
51
|
+
});
|
|
52
|
+
if (!evt)
|
|
53
|
+
throw new Error("decoder rejected the click");
|
|
54
|
+
fake.emitInteraction({ ...evt, platform: "discord" });
|
|
55
|
+
}
|
|
56
|
+
/** Every string select rendered for post `index`, as Discord would see it. */
|
|
57
|
+
function selectMenus(fake, index) {
|
|
58
|
+
const json = renderDiscordMessage(fake.posted[index]).components.map((c) => c.toJSON());
|
|
59
|
+
const menus = [];
|
|
60
|
+
const visit = (value) => {
|
|
61
|
+
if (Array.isArray(value)) {
|
|
62
|
+
for (const item of value)
|
|
63
|
+
visit(item);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (typeof value !== "object" || value === null)
|
|
67
|
+
return;
|
|
68
|
+
const el = value;
|
|
69
|
+
// Component type 3 is a string select.
|
|
70
|
+
if (el.type === 3 && typeof el.custom_id === "string") {
|
|
71
|
+
menus.push({
|
|
72
|
+
customId: el.custom_id,
|
|
73
|
+
minValues: el.min_values,
|
|
74
|
+
maxValues: el.max_values,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
for (const child of Object.values(value))
|
|
78
|
+
visit(child);
|
|
79
|
+
};
|
|
80
|
+
visit(json);
|
|
81
|
+
return menus;
|
|
82
|
+
}
|
|
83
|
+
/** Pick select option value(s) the way discord.js delivers it, through the real decoder. */
|
|
84
|
+
function pickSelect(fake, menu, values, messageId) {
|
|
85
|
+
const evt = decodeInteraction({
|
|
86
|
+
isButton: () => false,
|
|
87
|
+
isStringSelectMenu: () => true,
|
|
88
|
+
customId: menu.customId,
|
|
89
|
+
values,
|
|
90
|
+
component: { minValues: menu.minValues, maxValues: menu.maxValues },
|
|
91
|
+
channelId: CHANNEL_ID,
|
|
92
|
+
message: { id: messageId },
|
|
93
|
+
user: { id: alice.id, username: "alice" },
|
|
94
|
+
});
|
|
95
|
+
if (!evt)
|
|
96
|
+
throw new Error("decoder rejected the pick");
|
|
97
|
+
fake.emitInteraction({ ...evt, platform: "discord" });
|
|
98
|
+
}
|
|
99
|
+
async function startPicker(picker, before) {
|
|
100
|
+
const fake = new FakeAdapter({ platform: "discord" });
|
|
101
|
+
const channel = createChannel({
|
|
102
|
+
identifyUser: "platform",
|
|
103
|
+
adapters: [fake],
|
|
104
|
+
});
|
|
105
|
+
const state = { chosen: PENDING };
|
|
106
|
+
channel.onMention(async ({ thread }) => {
|
|
107
|
+
if (before)
|
|
108
|
+
await before((ui) => thread.post(ui));
|
|
109
|
+
state.chosen = await thread.awaitChoice(picker());
|
|
110
|
+
});
|
|
111
|
+
await channel.ɵruntime.start();
|
|
112
|
+
activeChannels.push(channel);
|
|
113
|
+
fake.emitTurn({
|
|
114
|
+
userText: "go",
|
|
115
|
+
conversationKey: CHANNEL_ID,
|
|
116
|
+
platform: "discord",
|
|
117
|
+
actor: alice,
|
|
118
|
+
});
|
|
119
|
+
await tick();
|
|
120
|
+
return { fake, state };
|
|
121
|
+
}
|
|
122
|
+
describe("Discord awaitChoice", () => {
|
|
123
|
+
afterEach(async () => {
|
|
124
|
+
vi.restoreAllMocks();
|
|
125
|
+
while (activeChannels.length > 0) {
|
|
126
|
+
await activeChannels.pop().ɵruntime.stop();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
it("is answered only by the picker's own button, not an earlier value-only one", async () => {
|
|
130
|
+
vi.spyOn(console, "debug").mockImplementation(() => { });
|
|
131
|
+
const value = { approved: true };
|
|
132
|
+
const { fake, state } = await startPicker(() => Actions({ children: [Button({ value, children: "Approve" })] }), async (post) => {
|
|
133
|
+
await post(Actions({ children: [Button({ value, children: "Old" })] }));
|
|
134
|
+
});
|
|
135
|
+
const [oldId] = buttonCustomIds(fake, 0);
|
|
136
|
+
const [ownId] = buttonCustomIds(fake, 1);
|
|
137
|
+
expect(oldId).toBe(`v:${JSON.stringify(value)}`);
|
|
138
|
+
expect(ownId).not.toBe(oldId);
|
|
139
|
+
clickButton(fake, oldId, "msg-1");
|
|
140
|
+
await tick();
|
|
141
|
+
expect(state.chosen).toBe(PENDING);
|
|
142
|
+
clickButton(fake, ownId, "msg-2");
|
|
143
|
+
await tick();
|
|
144
|
+
expect(state.chosen).toEqual(value);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
describe("Discord awaitChoice: option values the decoder would parse as JSON", () => {
|
|
148
|
+
afterEach(async () => {
|
|
149
|
+
vi.restoreAllMocks();
|
|
150
|
+
while (activeChannels.length > 0) {
|
|
151
|
+
await activeChannels.pop().ɵruntime.stop();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
const tricky = ["9.90", "null", "1e2", " true "];
|
|
155
|
+
const picker = (offered, multi) => () => Actions({
|
|
156
|
+
children: [
|
|
157
|
+
Select({
|
|
158
|
+
placeholder: "Pick",
|
|
159
|
+
multi,
|
|
160
|
+
options: [
|
|
161
|
+
{ label: "Other", value: "other" },
|
|
162
|
+
{ label: "Tricky", value: offered },
|
|
163
|
+
],
|
|
164
|
+
}),
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
it.each(tricky)("single select answers with the offered option %j", async (offered) => {
|
|
168
|
+
const { fake, state } = await startPicker(picker(offered, false));
|
|
169
|
+
const [menu] = selectMenus(fake, 0);
|
|
170
|
+
pickSelect(fake, menu, [offered], "msg-1");
|
|
171
|
+
await tick();
|
|
172
|
+
expect(state.chosen).toBe(offered);
|
|
173
|
+
});
|
|
174
|
+
it.each(tricky)("multi select answers with the offered option %j", async (offered) => {
|
|
175
|
+
const { fake, state } = await startPicker(picker(offered, true));
|
|
176
|
+
const [menu] = selectMenus(fake, 0);
|
|
177
|
+
pickSelect(fake, menu, ["other", offered], "msg-1");
|
|
178
|
+
await tick();
|
|
179
|
+
expect(state.chosen).toEqual(["other", offered]);
|
|
180
|
+
});
|
|
181
|
+
it("tells apart two options whose values decode to the same JSON", async () => {
|
|
182
|
+
const { fake, state } = await startPicker(() => Actions({
|
|
183
|
+
children: [
|
|
184
|
+
Select({
|
|
185
|
+
placeholder: "Pick",
|
|
186
|
+
options: [
|
|
187
|
+
{ label: "Short", value: "9.9" },
|
|
188
|
+
{ label: "Long", value: "9.90" },
|
|
189
|
+
],
|
|
190
|
+
}),
|
|
191
|
+
],
|
|
192
|
+
}));
|
|
193
|
+
const [menu] = selectMenus(fake, 0);
|
|
194
|
+
pickSelect(fake, menu, ["9.90"], "msg-1");
|
|
195
|
+
await tick();
|
|
196
|
+
expect(state.chosen).toBe("9.90");
|
|
197
|
+
});
|
|
198
|
+
it("still refuses a single-select value the picker does not offer", async () => {
|
|
199
|
+
vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
200
|
+
vi.spyOn(console, "debug").mockImplementation(() => { });
|
|
201
|
+
const { fake, state } = await startPicker(picker("9.90", false));
|
|
202
|
+
const [menu] = selectMenus(fake, 0);
|
|
203
|
+
// "9.9" decodes to the same number as the offered "9.90" but is not offered.
|
|
204
|
+
pickSelect(fake, menu, ["9.9"], "msg-1");
|
|
205
|
+
await tick();
|
|
206
|
+
expect(state.chosen).toBe(PENDING);
|
|
207
|
+
});
|
|
208
|
+
it("still refuses a multi-select value the picker does not offer", async () => {
|
|
209
|
+
vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
210
|
+
vi.spyOn(console, "debug").mockImplementation(() => { });
|
|
211
|
+
const { fake, state } = await startPicker(picker("true", true));
|
|
212
|
+
const [menu] = selectMenus(fake, 0);
|
|
213
|
+
// " true " decodes to the same boolean as the offered "true" but is not offered.
|
|
214
|
+
pickSelect(fake, menu, ["other", " true "], "msg-1");
|
|
215
|
+
await tick();
|
|
216
|
+
expect(state.chosen).toBe(PENDING);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interaction.d.ts","sourceRoot":"","sources":["../src/interaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,EACpB,MAAM,2BAA2B,CAAC;AAsBnC,2FAA2F;AAC3F,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,gBAAgB,GAAG,SAAS,
|
|
1
|
+
{"version":3,"file":"interaction.d.ts","sourceRoot":"","sources":["../src/interaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,EACpB,MAAM,2BAA2B,CAAC;AAsBnC,2FAA2F;AAC3F,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,gBAAgB,GAAG,SAAS,CAiE5E;AA0FD,kFAAkF;AAClF,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,OAAO,GAAG,mBAAmB,CA+B3E;AAMD;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,OAAO,EACjB,IAAI,EAAE,OAAO,EACb,KAAK,EAAE,OAAO,GACb,gBAAgB,GAAG,SAAS,CAmC9B"}
|
package/dist/interaction.js
CHANGED
|
@@ -18,6 +18,10 @@ export function decodeInteraction(raw) {
|
|
|
18
18
|
// (number/boolean/object) round-trips to its original type — mirroring bot-slack.
|
|
19
19
|
let id = customId;
|
|
20
20
|
let value;
|
|
21
|
+
// The chosen select value(s) exactly as Discord sent them. JSON-parsing
|
|
22
|
+
// `value` loses the original string ("9.90" becomes 9.9), so awaitChoice
|
|
23
|
+
// matches the offered option by this raw string instead.
|
|
24
|
+
let rawValue;
|
|
21
25
|
if (isSelect) {
|
|
22
26
|
// Discord sends `values: string[]` for both single and multi selects; the
|
|
23
27
|
// unambiguous signal is the component's value bounds (the renderer sets
|
|
@@ -25,9 +29,9 @@ export function decodeInteraction(raw) {
|
|
|
25
29
|
// values; single → the one value (mirrors bot-slack).
|
|
26
30
|
const c = i.component;
|
|
27
31
|
const multi = (c?.maxValues ?? 1) > 1 || c?.minValues === 0;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
const chosen = (i.values ?? []).filter((v) => typeof v === "string");
|
|
33
|
+
value = multi ? chosen.map(parseSelectValue) : parseSelectValue(chosen[0]);
|
|
34
|
+
rawValue = multi ? chosen : chosen[0];
|
|
31
35
|
}
|
|
32
36
|
else {
|
|
33
37
|
const sep = customId.startsWith("ck:") ? customId.indexOf(";v:") : -1;
|
|
@@ -44,6 +48,7 @@ export function decodeInteraction(raw) {
|
|
|
44
48
|
conversationKey: channelId,
|
|
45
49
|
replyTarget: { channelId, ...(i.guildId ? { guildId: i.guildId } : {}) },
|
|
46
50
|
value,
|
|
51
|
+
...(rawValue !== undefined ? { rawValue } : {}),
|
|
47
52
|
actor: actor ?? { id: "unknown", kind: "unknown" },
|
|
48
53
|
identityContext: identityContext({
|
|
49
54
|
guildId: i.guildId,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@copilotkit/channels-discord",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Discord PlatformAdapter for @copilotkit/channels.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@ag-ui/client": "1.0.2",
|
|
37
37
|
"discord.js": "^14.16.0",
|
|
38
|
-
"@copilotkit/channels-core": "^0.
|
|
39
|
-
"@copilotkit/channels-ui": "^0.
|
|
38
|
+
"@copilotkit/channels-core": "^0.12.0",
|
|
39
|
+
"@copilotkit/channels-ui": "^0.12.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/node": "^22.10.0",
|