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

feat: add/change change through REST api (client-direct) #1052

Merged
merged 3 commits into from
Dec 13, 2024
Merged
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
5 changes: 5 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,11 @@ const startAgents = async () => {
} catch (error) {
elizaLogger.error("Error starting agents:", error);
}
// upload some agent functionality into directClient
directClient.startAgent = async character => {
// wrap it so we don't have to inject directClient later
return startAgent(character, directClient)
};

function chat() {
const agentId = characters[0].name ?? "Agent";
Expand Down
50 changes: 48 additions & 2 deletions packages/client-direct/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
import bodyParser from "body-parser";
import cors from "cors";

import { AgentRuntime } from "@ai16z/eliza";
import {
AgentRuntime,
elizaLogger,
validateCharacterConfig,
} from "@ai16z/eliza";

import { REST, Routes } from "discord.js";

export function createApiRouter(agents: Map<string, AgentRuntime>) {
export function createApiRouter(agents: Map<string, AgentRuntime>, directClient) {
const router = express.Router();

router.use(cors());
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));

router.get("/", (req, res) => {
res.send("Welcome, this is the REST API!");
});

router.get("/hello", (req, res) => {
res.json({ message: "Hello World!" });
});
Expand All @@ -21,6 +29,7 @@
const agentsList = Array.from(agents.values()).map((agent) => ({
id: agent.agentId,
name: agent.character.name,
clients: Object.keys(agent.clients),
}));
res.json({ agents: agentsList });
});
Expand All @@ -40,6 +49,43 @@
});
});

router.post("/agents/:agentId/set", async (req, res) => {
const agentId = req.params.agentId;
console.log('agentId', agentId)
let agent:AgentRuntime = agents.get(agentId);

// update character
if (agent) {
// stop agent
agent.stop()
directClient.unregisterAgent(agent)
// if it has a different name, the agentId will change
}

// load character from body
const character = req.body
try {
validateCharacterConfig(character)
} catch(e) {
elizaLogger.error(`Error parsing character: ${e}`);
res.status(400).json({
success: false,
message: e.message,
});
return;
}

// start it up (and register it)
agent = await directClient.startAgent(character)
elizaLogger.log(`${character.name} started`)

res.json({
id: character.id,
character: character,
});
});


router.get("/agents/:agentId/channels", async (req, res) => {
const agentId = req.params.agentId;
const runtime = agents.get(agentId);
Expand All @@ -55,7 +101,7 @@
try {
const guilds = (await rest.get(Routes.userGuilds())) as Array<any>;

res.json({

Check warning on line 104 in packages/client-direct/src/api.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
id: runtime.agentId,
guilds: guilds,
serverCount: guilds.length,
Expand Down
19 changes: 7 additions & 12 deletions packages/client-direct/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,11 @@
# Instructions: Write the next message for {{agentName}}.
` + messageCompletionFooter;

export interface SimliClientConfig {
apiKey: string;
faceID: string;
handleSilence: boolean;
videoRef: any;
audioRef: any;
}
export class DirectClient {
public app: express.Application;
private agents: Map<string, AgentRuntime>;
private agents: Map<string, AgentRuntime>; // container management
private server: any; // Store server instance

Check warning on line 57 in packages/client-direct/src/index.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
public startAgent: Function; // Store startAgent functor

constructor() {
elizaLogger.log("DirectClient constructor");
Expand All @@ -72,7 +66,7 @@
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: true }));

const apiRouter = createApiRouter(this.agents);
const apiRouter = createApiRouter(this.agents, this);
this.app.use(apiRouter);

// Define an interface that extends the Express Request interface
Expand Down Expand Up @@ -338,7 +332,7 @@
fileResponse.headers
.get("content-disposition")
?.split("filename=")[1]
?.replace(/"/g, "") || "default_name.txt";
?.replace(/"/g, /* " */ "") || "default_name.txt";

console.log("Saving as:", fileName);

Expand Down Expand Up @@ -378,6 +372,7 @@
);
}

// agent/src/index.ts:startAgent calls this
public registerAgent(runtime: AgentRuntime) {
this.agents.set(runtime.agentId, runtime);
}
Expand All @@ -388,7 +383,7 @@

public start(port: number) {
this.server = this.app.listen(port, () => {
elizaLogger.success(`Server running at http://localhost:${port}/`);
elizaLogger.success(`REST API bound to 0.0.0.0:${port}. If running locally, access it at http://localhost:${port}.`);
});

// Handle graceful shutdown
Expand Down Expand Up @@ -430,7 +425,7 @@
client.start(serverPort);
return client;
},
stop: async (_runtime: IAgentRuntime, client?: any) => {
stop: async (_runtime: IAgentRuntime, client?: Client) => {
if (client instanceof DirectClient) {
client.stop();
}
Expand Down
Loading