Skip to content

Commit

Permalink
Merge branch 'develop' into odi-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
odilitime authored Jan 11, 2025
2 parents 1f8b51e + 94daffc commit 4a1a5b8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 43 deletions.
82 changes: 56 additions & 26 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,61 @@ function tryLoadFile(filePath: string): string | null {
return null;
}
}
function mergeCharacters(base: Character, child: Character): Character {
const mergeObjects = (baseObj: any, childObj: any) => {
const result: any = {};
const keys = new Set([...Object.keys(baseObj || {}), ...Object.keys(childObj || {})]);
keys.forEach(key => {
if (typeof baseObj[key] === 'object' && typeof childObj[key] === 'object' && !Array.isArray(baseObj[key]) && !Array.isArray(childObj[key])) {
result[key] = mergeObjects(baseObj[key], childObj[key]);
} else if (Array.isArray(baseObj[key]) || Array.isArray(childObj[key])) {
result[key] = [...(baseObj[key] || []), ...(childObj[key] || [])];
} else {
result[key] = childObj[key] !== undefined ? childObj[key] : baseObj[key];
}
});
return result;
};
return mergeObjects(base, child);
}
async function loadCharacter(filePath: string): Promise<Character> {
const content = tryLoadFile(filePath);
if (!content) {
throw new Error(`Character file not found: ${filePath}`);
}
let character = JSON.parse(content);
validateCharacterConfig(character);

// .id isn't really valid
const characterId = character.id || character.name;
const characterPrefix = `CHARACTER.${characterId.toUpperCase().replace(/ /g, "_")}.`;
const characterSettings = Object.entries(process.env)
.filter(([key]) => key.startsWith(characterPrefix))
.reduce((settings, [key, value]) => {
const settingKey = key.slice(characterPrefix.length);
return { ...settings, [settingKey]: value };
}, {});
if (Object.keys(characterSettings).length > 0) {
character.settings = character.settings || {};
character.settings.secrets = {
...characterSettings,
...character.settings.secrets,
};
}
// Handle plugins
character.plugins = await handlePluginImporting(
character.plugins
);
if (character.extends) {
elizaLogger.info(`Merging ${character.name} character with parent characters`);
for (const extendPath of character.extends) {
const baseCharacter = await loadCharacter(path.resolve(path.dirname(filePath), extendPath));
character = mergeCharacters(baseCharacter, character);
elizaLogger.info(`Merged ${character.name} with ${baseCharacter.name}`);
}
}
return character;
}

export async function loadCharacters(
charactersArg: string
Expand Down Expand Up @@ -211,32 +266,7 @@ export async function loadCharacters(
}

try {
const character = JSON.parse(content);
validateCharacterConfig(character);

// .id isn't really valid
const characterId = character.id || character.name;
const characterPrefix = `CHARACTER.${characterId.toUpperCase().replace(/ /g, "_")}.`;

const characterSettings = Object.entries(process.env)
.filter(([key]) => key.startsWith(characterPrefix))
.reduce((settings, [key, value]) => {
const settingKey = key.slice(characterPrefix.length);
return { ...settings, [settingKey]: value };
}, {});

if (Object.keys(characterSettings).length > 0) {
character.settings = character.settings || {};
character.settings.secrets = {
...characterSettings,
...character.settings.secrets,
};
}

// Handle plugins
character.plugins = await handlePluginImporting(
character.plugins
);
const character: Character = await loadCharacter(resolvedPath);

loadedCharacters.push(character);
elizaLogger.info(
Expand Down
32 changes: 16 additions & 16 deletions packages/client-discord/src/actions/joinvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
IAgentRuntime,
Memory,
State,
generateText,
ModelClass,
} from "@elizaos/core";
import {
Channel,
Expand All @@ -17,6 +19,7 @@ import {
Guild,
GuildMember,
} from "discord.js";
import { joinVoiceChannel } from "@discordjs/voice";

export default {
name: "JOIN_VOICE",
Expand Down Expand Up @@ -66,12 +69,7 @@ export default {
return false;
}

const client = state.discordClient as Client;

// Check if the client is connected to any voice channel
const isConnectedToVoice = client.voice.adapters.size === 0;

return isConnectedToVoice;
return true;
},
description: "Join a voice channel to participate in voice chat.",
handler: async (
Expand Down Expand Up @@ -115,31 +113,30 @@ export default {
);
});

if (!state.voiceManager) {
state.voiceManager = new VoiceManager({
client: state.discordClient,
runtime: runtime,
});
}

if (targetChannel) {
state.voiceManager.joinVoiceChannel({
joinVoiceChannel({
channelId: targetChannel.id,
guildId: (discordMessage as DiscordMessage).guild?.id as string,
adapterCreator: (client.guilds.cache.get(id) as Guild)
.voiceAdapterCreator,
selfDeaf: false,
selfMute: false,
group: client.user.id,
});
return true;
} else {
const member = (discordMessage as DiscordMessage)
.member as GuildMember;
if (member?.voice?.channel) {
state.voiceManager.joinVoiceChannel({
joinVoiceChannel({
channelId: member.voice.channel.id,
guildId: (discordMessage as DiscordMessage).guild
?.id as string,
adapterCreator: (client.guilds.cache.get(id) as Guild)
.voiceAdapterCreator,
selfDeaf: false,
selfMute: false,
group: client.user.id,
});
return true;
}
Expand Down Expand Up @@ -204,12 +201,15 @@ You should only respond with the name of the voice channel or none, no commentar
});

if (targetChannel) {
state.voiceManager.joinVoiceChannel({
joinVoiceChannel({
channelId: targetChannel.id,
guildId: (discordMessage as DiscordMessage).guild
?.id as string,
adapterCreator: (client.guilds.cache.get(id) as Guild)
.voiceAdapterCreator,
selfDeaf: false,
selfMute: false,
group: client.user.id,
});
return true;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/defaultCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,5 @@ export const defaultCharacter: Character = {
"meticulous",
"provocative",
],
extends: [],
};
1 change: 1 addition & 0 deletions packages/core/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const CharacterSchema = z.object({
prompt: z.string().optional(),
})
.optional(),
extends: z.array(z.string()).optional(),
});

// Type inference
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ export enum Clients {
LENS = "lens",
AUTO = "auto",
SLACK = "slack",
GITHUB = "github",
}

export interface IAgentConfig {
Expand Down Expand Up @@ -871,6 +872,8 @@ export type Character = {
nft?: {
prompt: string;
};
/**Optinal Parent characters to inherit information from */
extends?: string[];
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-sui/src/providers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class WalletProvider {
}
);
const prices: Prices = {
sui: { usd: suiPriceData.pair.priceUsd },
sui: { usd: (1 / suiPriceData.pair.priceNative).toString() },
};
this.setCachedData(cacheKey, prices);
return prices;
Expand Down

0 comments on commit 4a1a5b8

Please sign in to comment.