Skip to content

Commit 67fc649

Browse files
authored
Merge pull request elizaOS#1415 from 0xCardinalError/multiple_localhosts
fix: Multiple Agents running at the same time on localhost
2 parents b67b609 + d5c966b commit 67fc649

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

agent/src/index.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import fs from "fs";
6060
import path from "path";
6161
import { fileURLToPath } from "url";
6262
import yargs from "yargs";
63+
import net from "net";
6364

6465
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
6566
const __dirname = path.dirname(__filename); // get the name of the directory
@@ -692,13 +693,30 @@ async function startAgent(
692693
}
693694
}
694695

696+
const checkPortAvailable = (port: number): Promise<boolean> => {
697+
return new Promise((resolve) => {
698+
const server = net.createServer();
699+
700+
server.once("error", (err: NodeJS.ErrnoException) => {
701+
if (err.code === "EADDRINUSE") {
702+
resolve(false);
703+
}
704+
});
705+
706+
server.once("listening", () => {
707+
server.close();
708+
resolve(true);
709+
});
710+
711+
server.listen(port);
712+
});
713+
};
714+
695715
const startAgents = async () => {
696716
const directClient = new DirectClient();
697-
const serverPort = parseInt(settings.SERVER_PORT || "3000");
717+
let serverPort = parseInt(settings.SERVER_PORT || "3000");
698718
const args = parseArguments();
699-
700719
let charactersArg = args.characters || args.character;
701-
702720
let characters = [defaultCharacter];
703721

704722
if (charactersArg) {
@@ -713,19 +731,32 @@ const startAgents = async () => {
713731
elizaLogger.error("Error starting agents:", error);
714732
}
715733

734+
// Find available port
735+
while (!(await checkPortAvailable(serverPort))) {
736+
elizaLogger.warn(
737+
`Port ${serverPort} is in use, trying ${serverPort + 1}`
738+
);
739+
serverPort++;
740+
}
741+
716742
// upload some agent functionality into directClient
717743
directClient.startAgent = async (character: Character) => {
718744
// wrap it so we don't have to inject directClient later
719745
return startAgent(character, directClient);
720746
};
747+
721748
directClient.start(serverPort);
722749

750+
if (serverPort !== parseInt(settings.SERVER_PORT || "3000")) {
751+
elizaLogger.log(`Server started on alternate port ${serverPort}`);
752+
}
753+
723754
elizaLogger.log(
724-
"Run `pnpm start:client` to start the client and visit the outputted URL (http://localhost:5173) to chat with your agents"
755+
"Run `pnpm 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 pnpm start:client`"
725756
);
726757
};
727758

728759
startAgents().catch((error) => {
729760
elizaLogger.error("Unhandled error in startAgents:", error);
730-
process.exit(1); // Exit the process after logging
761+
process.exit(1);
731762
});

0 commit comments

Comments
 (0)