Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated message reactions #829

Draft
wants to merge 60 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
067b421
receive reaction events from bot
AbdurrehmanSubhani Dec 25, 2024
c0f6e5d
delegate reaction handling to DiscordOutput, bind incoming message re…
AbdurrehmanSubhani Dec 25, 2024
6fe58fe
init process message reactions in runtime
AbdurrehmanSubhani Dec 25, 2024
5db96a7
add "messageReaction" perception, add reaction as localmessage
AbdurrehmanSubhani Dec 25, 2024
41b3a4e
update "sendTyping" as optional for perceptions
AbdurrehmanSubhani Dec 25, 2024
63ab1a6
fix agent object id and name being formatted
AbdurrehmanSubhani Dec 25, 2024
6296aa0
add reaction context to message args
AbdurrehmanSubhani Dec 25, 2024
039c23a
default sendtyping to true
AbdurrehmanSubhani Dec 25, 2024
cb91177
comment out conversation processMessageReactions for now
AbdurrehmanSubhani Dec 25, 2024
b289013
init agent message reaction support
AbdurrehmanSubhani Dec 25, 2024
dbbd104
add agent -> discord bot react back support
AbdurrehmanSubhani Dec 25, 2024
fd1c38a
Merge branch 'dev' of https://github.com/UpstreetAI/monorepo into dis…
AbdurrehmanSubhani Dec 26, 2024
6148e04
remove commented code
AbdurrehmanSubhani Dec 26, 2024
1042e59
add discord appid to discord component props and args
AbdurrehmanSubhani Dec 26, 2024
99a5aef
add missing token and appId missing exceptions
AbdurrehmanSubhani Dec 26, 2024
2965ca6
add appId attribute in discord manager and client
AbdurrehmanSubhani Dec 26, 2024
a000641
read and pass appId prop in features renderer
AbdurrehmanSubhani Dec 26, 2024
6b08665
add agentSpecs attribute to the conversation
AbdurrehmanSubhani Dec 26, 2024
83dbed6
remove discord appId integration updates from branch
AbdurrehmanSubhani Dec 26, 2024
6c88a77
remove appId from discord.tsx
AbdurrehmanSubhani Dec 26, 2024
573d8b6
rename agentSpec to currentAgentSpec to add clarity
AbdurrehmanSubhani Dec 26, 2024
2c86880
add appId to discord component and types
AbdurrehmanSubhani Dec 26, 2024
20c8cc2
add appId to feat renderer and discord client
AbdurrehmanSubhani Dec 26, 2024
72511ae
add mentionId to current agent's specs
AbdurrehmanSubhani Dec 26, 2024
0200cc0
add missing agentJson to active agent json
AbdurrehmanSubhani Dec 26, 2024
615cc8c
Merge branch 'fix-undefined-agentJson-in-active-agent' of https://git…
AbdurrehmanSubhani Dec 26, 2024
a0ee2c2
use Player for agent in conversation to maintain standardized logic
AbdurrehmanSubhani Dec 26, 2024
e1966a3
Merge branch 'create-agent-specs-in-conversation' of https://github.c…
AbdurrehmanSubhani Dec 26, 2024
b8e93fc
improve the prompt
AbdurrehmanSubhani Dec 26, 2024
9180283
Merge branch 'add-app-id-discord-client' of https://github.com/Upstre…
AbdurrehmanSubhani Dec 26, 2024
454ccf1
remove userId from the schema and get that from the message
AbdurrehmanSubhani Dec 26, 2024
bcd9f16
remove userId from reaction schema
AbdurrehmanSubhani Dec 26, 2024
9a1fba8
remove agentJson, use config in getAllAgents
AbdurrehmanSubhani Dec 27, 2024
2a1f860
Merge branch 'fix-undefined-agentJson-in-active-agent' of https://git…
AbdurrehmanSubhani Dec 27, 2024
7b5337e
revert back default typing message for evalutation
AbdurrehmanSubhani Dec 27, 2024
9e9967a
update conversation object to remove agentJson usage
AbdurrehmanSubhani Dec 27, 2024
9d0679d
remove activeAgentObject from within the ConversationObject
AbdurrehmanSubhani Dec 27, 2024
0e4be57
remove unused imports
AbdurrehmanSubhani Dec 27, 2024
473a4f4
update discord manager to latest conversation object
AbdurrehmanSubhani Dec 27, 2024
e100853
update telnyx manager to latest conversation object
AbdurrehmanSubhani Dec 27, 2024
0d49b94
update twitter manager to latest converastion object
AbdurrehmanSubhani Dec 27, 2024
cf43af1
update twitter spaces to latest conversation manager
AbdurrehmanSubhani Dec 27, 2024
f08b0af
update action loop conversation object
AbdurrehmanSubhani Dec 27, 2024
8cdb850
Merge branch 'dev' of https://github.com/UpstreetAI/monorepo into rem…
AbdurrehmanSubhani Dec 30, 2024
67c7658
add agentPlayer as arg to conv object, add agentPlayer to agentsMap
AbdurrehmanSubhani Dec 30, 2024
47047e9
update clients to set agentPlayer
AbdurrehmanSubhani Dec 30, 2024
a314c52
update CharactersPrompt component
AbdurrehmanSubhani Dec 30, 2024
1c3b747
Merge branch 'remove-active-agent-from-conversation' of https://githu…
AbdurrehmanSubhani Dec 30, 2024
c75aed4
remove MessageReactionEvent import
AbdurrehmanSubhani Dec 30, 2024
e4c0827
update discord agentPlayer to use mentionId in playerSpec
AbdurrehmanSubhani Dec 30, 2024
706bba8
update getDiscordIdForUserId
AbdurrehmanSubhani Dec 30, 2024
6250b65
update handleReaction to use guildId for determining reaction source
AbdurrehmanSubhani Dec 30, 2024
948146c
remove dead MessageReactionEventData import
AbdurrehmanSubhani Dec 30, 2024
72c6964
remove unnecessary attributes from the rawMessageReaction and replace…
AbdurrehmanSubhani Dec 30, 2024
9465fa0
add id to action message, add discord message id to metadata
AbdurrehmanSubhani Dec 30, 2024
5c3c748
add id to action message
AbdurrehmanSubhani Dec 30, 2024
37dc44b
update chat message reaction action to use "id" to sync with message …
AbdurrehmanSubhani Dec 30, 2024
1d181c3
add metadata to ActionMessage, use metadata for messageReaction
AbdurrehmanSubhani Dec 30, 2024
84c0448
update multiplayer sendMessage
AbdurrehmanSubhani Dec 30, 2024
d9303e6
save and load metadata to and from db
AbdurrehmanSubhani Dec 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/chat/components/ui/multiplayer-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export function MultiplayerActionsProvider({ children }: MultiplayerActionsProvi

const timestamp = new Date();
const message: ActionMessage = {
id: crypto.randomUUID(),
method,
userId,
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import {
bindConversationToAgent,
} from '../runtime';
// import { Player } from 'react-agents-client/util/player.mjs';
import { Player } from 'react-agents-client/util/player.mjs';
import { ReactAgentsMultiplayerConnection } from 'react-agents-client/react-agents-client.mjs';
import {
ExtendableMessageEvent,
Expand All @@ -35,7 +35,7 @@ import {
import {
TranscribedVoiceInput,
} from 'react-agents/devices/audio-transcriber.mjs';
import { formatConversationMessage } from '../util/message-utils';
import { createMessageCache, formatConversationMessage } from '../util/message-utils';

//

Expand Down Expand Up @@ -91,14 +91,23 @@ export class ChatsManager {
agent,
} = this;

const agentPlayer = new Player(agent.id, {
name: agent.name,
bio: agent.bio,
});
const conversation = new ConversationObject({
agent,
agentPlayer,
getHash: () => {
return getChatKey({
room,
endpointUrl,
});
},
messageCache: createMessageCache({
agent,
conversationId: key,
agentId: agent.id,
}),
});
this.agent.conversationManager.addConversation(conversation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import {
import { SceneObject } from '../classes/scene-object';
import { Player } from 'react-agents-client/util/player.mjs';
import { ExtendableMessageEvent } from '../util/extendable-message-event';
import { MessageCache as MessageCacheConstructor, CACHED_MESSAGES_LIMIT } from './message-cache';
import { loadMessagesFromDatabase } from '../util/loadMessagesFromDatabase';

//

export class ConversationObject extends EventTarget {
agent: ActiveAgentObject; // the current agent
agentsMap: Map<string, Player>; // note: agents does not include the current agent
scene: SceneObject | null;
getHash: GetHashFn; // XXX this can be a string, since conversation hashes do not change (?)
Expand All @@ -27,37 +24,30 @@ export class ConversationObject extends EventTarget {
mentionsRegex: RegExp | null = null;

constructor({
agent,
agentsMap = new Map(),
agentPlayer,
scene = null,
getHash = () => '',
mentionsRegex = null,
messageCache,
}: {
agent: ActiveAgentObject | null;
agentsMap?: Map<string, Player>;
agentPlayer: Player;
scene?: SceneObject | null;
getHash?: GetHashFn;
mentionsRegex?: RegExp | null;
messageCache: MessageCache;
}) {
super();

this.agent = agent;
this.agentsMap = agentsMap;
this.scene = scene;
this.getHash = getHash;
this.mentionsRegex = mentionsRegex;
this.messageCache = new MessageCacheConstructor({
loader: async () => {
const supabase = this.agent.appContextValue.useSupabase();
const messages = await loadMessagesFromDatabase({
supabase,
conversationId: this.getKey(),
agentId: this.agent.id,
limit: CACHED_MESSAGES_LIMIT,
});
return messages;
},
});
this.messageCache = messageCache;

// add the agent player to the agents map
this.agentsMap.set(agentPlayer.playerId, agentPlayer);
}

//
Expand Down Expand Up @@ -94,9 +84,6 @@ export class ConversationObject extends EventTarget {
this.scene = scene;
}

getAgent() {
return this.agent;
}
// setAgent(agent: ActiveAgentObject) {
// this.agent = agent;
// }
Expand Down Expand Up @@ -127,7 +114,6 @@ export class ConversationObject extends EventTarget {
const allAgents: object[] = [
...Array.from(this.agentsMap.values()).map(player => player.playerSpec),
];
this.agent && allAgents.push(this.agent.agentJson);
return allAgents;
}
getEmbeddingString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from './conversation-object';
import { Player } from 'react-agents-client/util/player.mjs';
import { DiscordBotClient } from '../lib/discord/discord-client';
import { formatConversationMessage } from '../util/message-utils';
import { createMessageCache, formatConversationMessage } from '../util/message-utils';
import {
bindConversationToAgent,
} from '../runtime';
Expand Down Expand Up @@ -97,6 +97,45 @@ const bindOutgoing = ({
channelId,
userId,
});
} else if (method === 'messageReaction') {
const {
reaction,
id,
} = args as {
reaction: string,
id: string,
};


// get message from conversation by messageId
const message = conversation.getCachedMessages().find(m => m.args.id === id);

const getDiscordIdForUserId = (userId: string) => {
const agents = conversation.getAgents();
const agent = agents.find(
agent => agent.playerId === userId
);

const discordId = agent?.playerSpec?.mentionId;
return discordId;
};

// get userId from message
const userId = message?.userId;
const discordMessageId = message?.metadata?.discordMessageId;

const discordId = getDiscordIdForUserId(userId);
console.log('discord manager message reaction', {
reaction,
discordMessageId,
userId,
channelId,
discordId,
});
discordBotClient.input.reactToMessage(reaction, discordMessageId, {
channelId,
userId: discordId,
});
} else {
// ignore
}
Expand Down Expand Up @@ -126,6 +165,7 @@ const bindOutgoing = ({

export class DiscordBot extends EventTarget {
token: string;
appId: string;
channels: DiscordRoomSpec[];
dms: DiscordRoomSpec[];
userWhitelist: string[];
Expand All @@ -139,6 +179,7 @@ export class DiscordBot extends EventTarget {
// arguments
const {
token,
appId,
channels,
dms,
userWhitelist,
Expand All @@ -147,6 +188,7 @@ export class DiscordBot extends EventTarget {
jwt,
} = args;
this.token = token;
this.appId = appId;
this.channels = channels;
this.dms = dms;
this.userWhitelist = userWhitelist;
Expand All @@ -164,6 +206,7 @@ export class DiscordBot extends EventTarget {
// initialize discord bot client
const discordBotClient = new DiscordBotClient({
token,
appId,
codecs,
jwt,
name,
Expand Down Expand Up @@ -247,12 +290,23 @@ export class DiscordBot extends EventTarget {
return
}

const conversationId = `discord:channel:${channelId}`;
const agentPlayer = new Player(agent.id, {
name: agent.name,
bio: agent.bio,
mentionId: appId,
});
const conversation = new ConversationObject({
agent,
agentPlayer,
getHash: () => {
return `discord:channel:${channelId}`;
return conversationId;
},
mentionsRegex: discordMentionRegex,
messageCache: createMessageCache({
agent,
conversationId,
agentId: agent.id,
}),
});

this.agent.conversationManager.addConversation(conversation);
Expand Down Expand Up @@ -293,15 +347,26 @@ export class DiscordBot extends EventTarget {
console.log('dm conversation already exists for this user, skipping', userId);
return
}


const conversationId = `discord:dm:${userId}`;
const agentPlayer = new Player(agent.id, {
name: agent.name,
bio: agent.bio,
mentionId: appId,
});
const conversation = new ConversationObject({
agent,
agentPlayer,
getHash: () => {
return `discord:dm:${userId}`;
return conversationId;
},
mentionsRegex: discordMentionRegex,
messageCache: createMessageCache({
agent,
conversationId,
agentId: agent.id,
}),
});

this.agent.conversationManager.addConversation(conversation);
this.dmConversations.set(userId, conversation);

Expand Down Expand Up @@ -370,6 +435,7 @@ export class DiscordBot extends EventTarget {
text,
channelId, // if there is no channelId, it's a DM
// XXX discord channel/dm distinction can be made more explicit with a type: string field...
messageId,
} = e.data;

// look up conversation
Expand All @@ -391,6 +457,9 @@ export class DiscordBot extends EventTarget {
args: {
text: formattedMessage,
},
metadata: {
discordMessageId: messageId,
},
};
const id = getIdFromUserId(userId);
const agent = {
Expand All @@ -411,11 +480,71 @@ export class DiscordBot extends EventTarget {
});
};

// message reactions
const _bindIncomingMessageReactions = () => {
const handleReaction = (e: MessageEvent, eventType: string) => {
const {
userId,
messageId,
emoji,
channelId,
userDisplayName,
guildId,
} = e.data;

console.log(eventType, {
userId,
userDisplayName,
messageId,
emoji,
channelId,
guildId,
});

// look up conversation
const conversation = guildId
? this.channelConversations.get(channelId) ?? null
: this.dmConversations.get(userId) ?? null;

if (!conversation) return;

const rawMessageReaction = {
userId,
name: userDisplayName,
method: 'messageReaction',
args: {
text: `${eventType === 'messagereactionadd' ? 'Added' : 'Removed'} reaction '${emoji}' ${eventType === 'messagereactionadd' ? 'to' : 'from'} message ${messageId}`,
},
metadata: {
discordMessageId: messageId,
},
};

const newMessageReaction = formatConversationMessage(rawMessageReaction, {
agent: {
id: getIdFromUserId(userId),
name: userDisplayName,
},
});

conversation.addLocalMessage(newMessageReaction);
};

discordBotClient.output.addEventListener('messagereactionadd',
(e) => handleReaction(e, 'messagereactionadd')
);

discordBotClient.output.addEventListener('messagereactionremove',
(e) => handleReaction(e, 'messagereactionremove')
);
};

(async () => {
_bindChannels();
_bindGuildMemberAdd();
_bindGuildMemberRemove();
_bindIncoming();
_bindIncomingMessageReactions();
await _connect();
})().catch(err => {
console.warn('discord bot error', err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
ActionStep,
Evaluator,
DebugOptions,
EvaluateOpts,
} from '../types';
import {
ConversationObject,
Expand Down Expand Up @@ -110,7 +111,7 @@ export class GenerativeAgentObject {
});
// });
}
async evaluate(evaluator: Evaluator) {
async evaluate(evaluator: Evaluator, opts?: EvaluateOpts) {
return await this.conversation.typing(async () => {
const step = await evaluator.evaluate({
generativeAgent: this,
Expand Down
Loading