From de086b6201a6f21e447649b15e2008e5694e9007 Mon Sep 17 00:00:00 2001 From: Abdurrehman Subhani Date: Sat, 21 Dec 2024 21:03:59 +0500 Subject: [PATCH 1/2] listen to conversation agent additions in chat --- .../classes/conversation-object.ts | 2 ++ .../components/util/default-components.tsx | 35 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/usdk/packages/upstreet-agent/packages/react-agents/classes/conversation-object.ts b/packages/usdk/packages/upstreet-agent/packages/react-agents/classes/conversation-object.ts index 240749580..d137abd09 100644 --- a/packages/usdk/packages/upstreet-agent/packages/react-agents/classes/conversation-object.ts +++ b/packages/usdk/packages/upstreet-agent/packages/react-agents/classes/conversation-object.ts @@ -111,9 +111,11 @@ export class ConversationObject extends EventTarget { } addAgent(agentId: string, player: Player) { this.agentsMap.set(agentId, player); + this.dispatchEvent(new Event('addAgent')); } removeAgent(agentId: string) { this.agentsMap.delete(agentId); + this.dispatchEvent(new Event('removeAgent')); } getKey() { diff --git a/packages/usdk/packages/upstreet-agent/packages/react-agents/components/util/default-components.tsx b/packages/usdk/packages/upstreet-agent/packages/react-agents/components/util/default-components.tsx index 364df23f2..a09c6746a 100644 --- a/packages/usdk/packages/upstreet-agent/packages/react-agents/components/util/default-components.tsx +++ b/packages/usdk/packages/upstreet-agent/packages/react-agents/components/util/default-components.tsx @@ -120,21 +120,24 @@ const CharactersPrompt = () => { const agent = useAgent(); const name = useName(); const bio = usePersonality(); + const [agents, setAgents] = useState([]); if (conversation) { - const agents = conversation.getAgents(); const currentAgentSpec = { id: agent.id, name, bio, }; - const agentSpecs = agents.map((agent) => { - const agentSpec = agent.getPlayerSpec() as any; - return { - name: agentSpec?.name, - id: agent.playerId, - bio: agentSpec?.bio, - }; - }); + + const agentSpecs = agents + .filter(mapAgent => mapAgent.playerId !== agent.id) + .map((agent) => { + const agentSpec = agent.getPlayerSpec() as any; + return { + name: agentSpec?.name, + id: agent.playerId, + bio: agentSpec?.bio, + }; + }); const formatAgent = (agent: any) => { return [ @@ -144,6 +147,20 @@ const CharactersPrompt = () => { ].join('\n'); }; + useEffect(() => { + const updateAgents = () => { + const agents = conversation.getAgents(); + setAgents(agents); + }; + conversation.addEventListener('addAgent', updateAgents); + conversation.addEventListener('removeAgent', updateAgents); + + return () => { + conversation.removeEventListener('addAgent', updateAgents); + conversation.removeEventListener('removeAgent', updateAgents); + }; + }, [conversation]); + return ( {dedent` From 72368760fec69d55125f6dcd650cb2412f04261a Mon Sep 17 00:00:00 2001 From: Abdurrehman Subhani Date: Mon, 23 Dec 2024 10:03:53 +0500 Subject: [PATCH 2/2] update agent add/remove event name --- .../packages/react-agents/classes/conversation-object.ts | 4 ++-- .../react-agents/components/util/default-components.tsx | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/usdk/packages/upstreet-agent/packages/react-agents/classes/conversation-object.ts b/packages/usdk/packages/upstreet-agent/packages/react-agents/classes/conversation-object.ts index d137abd09..8d9552465 100644 --- a/packages/usdk/packages/upstreet-agent/packages/react-agents/classes/conversation-object.ts +++ b/packages/usdk/packages/upstreet-agent/packages/react-agents/classes/conversation-object.ts @@ -111,11 +111,11 @@ export class ConversationObject extends EventTarget { } addAgent(agentId: string, player: Player) { this.agentsMap.set(agentId, player); - this.dispatchEvent(new Event('addAgent')); + this.dispatchEvent(new Event('agentschange')); } removeAgent(agentId: string) { this.agentsMap.delete(agentId); - this.dispatchEvent(new Event('removeAgent')); + this.dispatchEvent(new Event('agentschange')); } getKey() { diff --git a/packages/usdk/packages/upstreet-agent/packages/react-agents/components/util/default-components.tsx b/packages/usdk/packages/upstreet-agent/packages/react-agents/components/util/default-components.tsx index a09c6746a..ac24f6cce 100644 --- a/packages/usdk/packages/upstreet-agent/packages/react-agents/components/util/default-components.tsx +++ b/packages/usdk/packages/upstreet-agent/packages/react-agents/components/util/default-components.tsx @@ -152,12 +152,10 @@ const CharactersPrompt = () => { const agents = conversation.getAgents(); setAgents(agents); }; - conversation.addEventListener('addAgent', updateAgents); - conversation.addEventListener('removeAgent', updateAgents); + conversation.addEventListener('agentschange', updateAgents); return () => { - conversation.removeEventListener('addAgent', updateAgents); - conversation.removeEventListener('removeAgent', updateAgents); + conversation.removeEventListener('agentschange', updateAgents); }; }, [conversation]);