Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -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 @@ -247,12 +247,22 @@ export class DiscordBot extends EventTarget {
return
}

const conversationId = `discord:channel:${channelId}`;
const agentPlayer = new Player(agent.id, {
name: agent.name,
bio: agent.bio,
});
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,13 +303,23 @@ 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,
});
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
TelnyxMessageArgs,
TelnyxVoiceArgs,
} from '../lib/telnyx/telnyx-client';
import { formatConversationMessage } from '../util/message-utils';
import { createMessageCache, formatConversationMessage } from '../util/message-utils';
import {
bindConversationToAgent,
} from '../runtime';
Expand Down Expand Up @@ -175,9 +175,19 @@ export class TelnyxBot extends EventTarget {
});
let conversation = this.conversations.get(hash);
if (!conversation) {

const agentPlayer = new Player(agent.id, {
name: agent.name,
bio: agent.bio,
});
conversation = new ConversationObject({
agent,
agentPlayer,
getHash: () => hash,
messageCache: createMessageCache({
agent,
conversationId: hash,
agentId: agent.id,
}),
});
const player = makePlayerFromPhoneNumber(toPhoneNumber);
conversation.addAgent(player.playerId, player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ConversationObject,
} from './conversation-object';
// import { Player } from 'react-agents-client/util/player.mjs';
import { formatConversationMessage } from '../util/message-utils';
import { createMessageCache, formatConversationMessage } from '../util/message-utils';
import {
bindConversationToAgent,
} from '../runtime';
Expand Down Expand Up @@ -197,9 +197,19 @@ class TwitterBot {
// Create or get conversation
let conversation = this.conversations.get(conversation_id);
if (!conversation) {
const agentPlayer = new Player(this.agent.id, {
name: this.agent.name,
bio: this.agent.bio,
});

conversation = new ConversationObject({
agent: this.agent,
agentPlayer,
getHash: () => `twitter:conversation:${conversation_id}`,
messageCache: createMessageCache({
agent: this.agent,
conversationId: `twitter:conversation:${conversation_id}`,
agentId: this.agent.id,
}),
});

this.agent.conversationManager.addConversation(conversation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
bindConversationToAgent,
} from '../runtime';
import { AudioDecodeStream } from 'codecs/audio-decode.mjs';
import { formatConversationMessage } from '../util/message-utils';
import { createMessageCache, formatConversationMessage } from '../util/message-utils';
import {
QueueManager,
} from 'queue-manager';
Expand All @@ -24,6 +24,7 @@ import {
import {
TranscribedVoiceInput,
} from '../devices/audio-transcriber.mjs';
import { Player } from 'react-agents-client/util/player.mjs';

//

Expand Down Expand Up @@ -80,11 +81,20 @@ class TwitterSpacesBot {
live = false;
});

const agentPlayer = new Player(this.agent.id, {
name: this.agent.name,
bio: this.agent.bio,
});
const conversation = new ConversationObject({
agent,
agentPlayer,
getHash: () => {
return `twitterSpaces:channel:${url}`;
},
messageCache: createMessageCache({
agent,
conversationId: `twitterSpaces:channel:${url}`,
agentId: agent.id,
}),
});

this.agent.conversationManager.addConversation(conversation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
AgentObject,
Attachment,
FormattedAttachment,
Player,
} from '../../types';
import {
useAgent,
Expand Down Expand Up @@ -117,59 +118,47 @@ const ScenePrompt = () => {
};
const CharactersPrompt = () => {
const conversation = useConversation();
const agent = useAgent();
const name = useName();
const bio = usePersonality();
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 activeAgent = useAgent();

const formatAgent = (agent: any) => {
return [
`Name: ${agent.name}`,
`UserId: ${agent.id}`,
`Bio: ${agent.bio}`,
].join('\n');
};
if (!conversation) return null;

return (
<Prompt>
{dedent`
# Your Character
` +
'\n\n' +
formatAgent(currentAgentSpec) +
(agents.length > 0
? (
'\n\n' +
dedent`
# Other Characters
` +
'\n\n' +
agentSpecs
.map(formatAgent)
.join('\n\n')
)
: ''
const agents = conversation.getAgents();
const agentCharacter = agents.find(agent => agent.playerId === activeAgent.id);
const otherAgents = agents
.filter(agent => agent.playerId !== activeAgent.id);

const formatAgent = (agent: Player) => {
const agentSpecs = agent.getPlayerSpec() as any;
return [
`Name: ${agentSpecs?.name}`,
`UserId: ${agent.playerId}`,
`Bio: ${agentSpecs?.bio}`,
].join('\n');
};

return (
<Prompt>
{dedent`
# Your Character
` +
'\n\n' +
formatAgent(agentCharacter) +
(agents.length > 0
? (
'\n\n' +
dedent`
# Other Characters
` +
'\n\n' +
otherAgents
.map(formatAgent)
.join('\n\n')
)
}
</Prompt>
);
} else {
return null;
}
: ''
)
}
</Prompt>
);
};
const ActionsPromptInternal = () => {
const actions = useActions();
Expand Down
Loading