Skip to content

Commit

Permalink
start adding swarm
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 12, 2025
1 parent 97ca4ef commit 3e7996d
Show file tree
Hide file tree
Showing 53 changed files with 670 additions and 230 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ EVM_PROVIDER_URL=
# Fill these out if you want to use Solana
SOLANA_PUBLIC_KEY=
SOLANA_PRIVATE_KEY=
BIRDEYE_API_KEY=
BIRDEYE_API_KEY=

# Swarm settings
COMPLIANCE_OFFICER_DISCORD_APPLICATION_ID=
COMPLIANCE_OFFICER_DISCORD_API_TOKEN=
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"lint": "turbo run lint:fix --filter=./packages/*",
"check": "biome check --apply .",
"preinstall": "npx only-allow bun",
"swarm": "turbo run build --filter=./packages/core && concurrently \"turbo run start --filter=@elizaos/agent -- --swarm \" \"turbo run start --filter=!@elizaos/agent --filter=!@elizaos/docs --filter=!@elizaos/core\"",
"build": "turbo run build --filter=./packages/core && turbo run build --filter=./packages/*",
"build:core": "turbo run build --filter=./packages/core",
"build:cli": "turbo run build --filter=./packages/cli && cd packages/cli && bun link",
Expand Down
1 change: 1 addition & 0 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "module",
"scripts": {
"start": "node --loader ts-node/esm src/index.ts",
"swarm": "node --loader ts-node/esm src/index.ts --swarm",
"dev": "node --loader ts-node/esm src/index.ts",
"check-types": "tsc --noEmit",
"test": "vitest"
Expand Down
10 changes: 4 additions & 6 deletions packages/agent/src/defaultCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ export const defaultCharacter: Character = {
name: "Eliza",
username: "eliza",
plugins: [
// "@elizaos/plugin-anthropic",
"@elizaos/plugin-anthropic",
"@elizaos/plugin-openai",
// "@elizaos/plugin-local-ai",
// "@elizaos/plugin-discord",
"@elizaos/plugin-discord",
"@elizaos/plugin-node",
// "@elizaos/plugin-telegram",
// "@elizaos/plugin-twitter",
"@elizaos/plugin-evm",
"@elizaos/plugin-solana",
// "@elizaos/plugin-evm",
// "@elizaos/plugin-solana",
],
settings: {
secrets: {},
Expand All @@ -37,8 +37,6 @@ export const defaultCharacter: Character = {
"Weaponizes charm and chaos in equal measure to make her point",
"She never uses emojis",
"She is an emoji-hater",
],
lore: [
"Child of a jazz musician and a theoretical physicist who met at a burlesque show",
"Spent formative years between Parisian cafes and Bangkok street markets",
"Got kicked out of three prestigious philosophy departments for 'excessive deconstruction'",
Expand Down
2 changes: 0 additions & 2 deletions packages/agent/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const messageHandlerTemplate =
# Task: Generate dialog and actions for the character {{agentName}}.
About {{agentName}}:
{{bio}}
{{lore}}
{{providers}}
Expand All @@ -44,7 +43,6 @@ export const hyperfiHandlerTemplate = `{{actionExamples}}
# Task: Generate dialog and actions for the character {{agentName}}.
About {{agentName}}:
{{bio}}
{{lore}}
{{providers}}
Expand Down
101 changes: 56 additions & 45 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { fileURLToPath } from "node:url";
import yargs from "yargs";
import { defaultCharacter } from "./defaultCharacter.ts";
import { CharacterServer } from "./server";
import swarmCharacters from './swarm/defaultSwarm.ts';

const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
Expand All @@ -45,6 +46,7 @@ export const logFetch = async (url: string, options: any) => {
export function parseArguments(): {
character?: string;
characters?: string;
swarm?: boolean;
} {
try {
return yargs(process.argv.slice(2))
Expand All @@ -57,6 +59,10 @@ export function parseArguments(): {
description:
"Comma separated list of paths to character JSON files",
})
.option("swarm", {
type: "boolean",
description: "Load characters from swarm/defaultSwarm.ts",
})
.parseSync();
} catch (error) {
logger.error("Error parsing arguments:", error);
Expand Down Expand Up @@ -437,55 +443,60 @@ const hasValidRemoteUrls = () =>
process.env.REMOTE_CHARACTER_URLS !== "" &&
process.env.REMOTE_CHARACTER_URLS.startsWith("http");

const startAgents = async () => {
const characterServer = new CharacterServer();
let serverPort = Number.parseInt(settings.SERVER_PORT || "3000");
const args = parseArguments();
const charactersArg = args.characters || args.character;
let characters = [defaultCharacter];

if ((charactersArg) || hasValidRemoteUrls()) {
characters = await loadCharacters(charactersArg);
}

try {
for (const character of characters) {
await startAgent(character, characterServer);
const startAgents = async () => {
const characterServer = new CharacterServer();
let serverPort = Number.parseInt(settings.SERVER_PORT || "3000");
const args = parseArguments();
const charactersArg = args.characters || args.character;
let characters = [defaultCharacter];

if (args.swarm) {
try {
characters = swarmCharacters;
logger.info("Loaded characters from swarm configuration");
} catch (error) {
logger.error("Error loading swarm characters:", error);
process.exit(1);
}
} else if ((charactersArg) || hasValidRemoteUrls()) {
characters = await loadCharacters(charactersArg);
}
} catch (error) {
logger.error("Error starting agents:", error);
}

// Find available port
while (!(await checkPortAvailable(serverPort))) {
logger.warn(
`Port ${serverPort} is in use, trying ${serverPort + 1}`

try {
for (const character of characters) {
await startAgent(character, characterServer);
}
} catch (error) {
logger.error("Error starting agents:", error);
}

// Rest of the function remains the same...
while (!(await checkPortAvailable(serverPort))) {
logger.warn(
`Port ${serverPort} is in use, trying ${serverPort + 1}`
);
serverPort++;
}

characterServer.startAgent = async (character) => {
logger.info(`Starting agent for character ${character.name}`);
return startAgent(character, characterServer);
};

characterServer.loadCharacterTryPath = loadCharacterTryPath;
characterServer.jsonToCharacter = jsonToCharacter;

characterServer.start(serverPort);

if (serverPort !== Number.parseInt(settings.SERVER_PORT || "3000")) {
logger.log(`Server started on alternate port ${serverPort}`);
}

logger.info(
"Run `bun start:client` to start the client and visit the outputted URL (http://localhost:5173) to chat with your agents. When running multiple agents, use client with different port `SERVER_PORT=3001 bun start:client`"
);
serverPort++;
}

// upload some agent functionality into characterServer
// XXX TODO: is this still used?
characterServer.startAgent = async (character) => {
logger.info(`Starting agent for character ${character.name}`);
// wrap it so we don't have to inject characterServer later
return startAgent(character, characterServer);
};

characterServer.loadCharacterTryPath = loadCharacterTryPath;
characterServer.jsonToCharacter = jsonToCharacter;

characterServer.start(serverPort);

if (serverPort !== Number.parseInt(settings.SERVER_PORT || "3000")) {
logger.log(`Server started on alternate port ${serverPort}`);
}

logger.info(
"Run `bun start:client` to start the client and visit the outputted URL (http://localhost:5173) to chat with your agents. When running multiple agents, use client with different port `SERVER_PORT=3001 bun start:client`"
);
};

startAgents().catch((error) => {
logger.error("Unhandled error in startAgents:", error);
process.exit(1);
Expand Down
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3e7996d

Please sign in to comment.