-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathdiscord-with-chatAgent.ts
More file actions
71 lines (60 loc) · 2.3 KB
/
Copy pathdiscord-with-chatAgent.ts
File metadata and controls
71 lines (60 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { GameAgent } from "@virtuals-protocol/game";
import DiscordPlugin from "../../plugins/discordPlugin/src/discordPlugin";
import { ChatAgent } from "../../src/chatAgent";
const discordPlugin = new DiscordPlugin({
credentials: {
botToken: process.env.DISCORD_BOT_TOKEN || ""
},
});
const chatAgent = new ChatAgent(process.env.GAME_API_KEY || "", "You are a helpful assistant");
(async () => {
// Track active chats using a Map
const activeChats = new Map();
discordPlugin.onMessage(async (msg) => {
// Skip messages from bots
if (msg.author.bot) {
console.log('This message is from a bot.');
return;
}
// Log guild information if available
if (msg.guild) {
console.log(`Guild Name: ${msg.guild.name}, Guild ID: ${msg.guild.id}`);
} else {
console.log('This message is not from a guild (e.g., DM).');
}
// Get or create chat for this user
let chat = activeChats.get(msg.author.id);
if (!chat) {
chat = await chatAgent.createChat({
partnerId: msg.author.id,
partnerName: msg.author.username,
actionSpace: [
discordPlugin.sendMessageFunction,
discordPlugin.addReactionFunction,
discordPlugin.pinMessageFunction,
discordPlugin.deleteMessageFunction,
// Add other Discord-specific functions here
],
});
activeChats.set(msg.author.id, chat);
}
const userMessage = msg.content;
const response = await chat.next(userMessage);
if (response.functionCall) {
console.log(`Function call: ${response.functionCall.fn_name}`);
// The function will be automatically executed by the agent
}
if (response.message) {
await discordPlugin.sendMessageFunction.executable({
channel_id: msg.channelId,
content: response.message
}, console.log);
}
if (response.isFinished) {
await discordPlugin.sendMessageFunction.executable({
channel_id: msg.channelId,
content: "Chat ended"
}, console.log);
}
});
})();