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

listen to conversation agent additions in chat #805

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ export class ConversationObject extends EventTarget {
}
addAgent(agentId: string, player: Player) {
this.agentsMap.set(agentId, player);
this.dispatchEvent(new Event('addAgent'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend rename to 'agentschange'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

}
removeAgent(agentId: string) {
this.agentsMap.delete(agentId);
this.dispatchEvent(new Event('removeAgent'));
}

getKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want the current agent in this list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current agent is being added via the currentAgentSpec + formatAgent inside the Prompt component, agentSpecs mapped out for "Other Characters" hence need to exclude the current agent

.map((agent) => {
const agentSpec = agent.getPlayerSpec() as any;
return {
name: agentSpec?.name,
id: agent.playerId,
bio: agentSpec?.bio,
};
});

const formatAgent = (agent: any) => {
return [
Expand All @@ -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 (
<Prompt>
{dedent`
Expand Down