Skip to content

Commit ff9eb67

Browse files
authored
Merge branch 'develop' into fix_discord_ci
2 parents bffd2f7 + 747b5c2 commit ff9eb67

File tree

7 files changed

+122
-40
lines changed

7 files changed

+122
-40
lines changed

agent/src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ const startAgents = async () => {
578578
} catch (error) {
579579
elizaLogger.error("Error starting agents:", error);
580580
}
581+
// upload some agent functionality into directClient
582+
directClient.startAgent = async character => {
583+
// wrap it so we don't have to inject directClient later
584+
return startAgent(character, directClient)
585+
};
581586

582587
function chat() {
583588
const agentId = characters[0].name ?? "Agent";

packages/client-auto/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client, IAgentRuntime } from "@ai16z/eliza";
1+
import { Client, IAgentRuntime, elizaLogger } from "@ai16z/eliza";
22

33
export class AutoClient {
44
interval: NodeJS.Timeout;
@@ -10,7 +10,7 @@ export class AutoClient {
1010
// start a loop that runs every x seconds
1111
this.interval = setInterval(
1212
async () => {
13-
console.log("running auto client...");
13+
elizaLogger.log("running auto client...");
1414
},
1515
60 * 60 * 1000
1616
); // 1 hour in milliseconds

packages/client-direct/src/api.ts

+48-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ import express from "express";
22
import bodyParser from "body-parser";
33
import cors from "cors";
44

5-
import { AgentRuntime } from "@ai16z/eliza";
5+
import {
6+
AgentRuntime,
7+
elizaLogger,
8+
validateCharacterConfig,
9+
} from "@ai16z/eliza";
610

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

9-
export function createApiRouter(agents: Map<string, AgentRuntime>) {
13+
export function createApiRouter(agents: Map<string, AgentRuntime>, directClient) {
1014
const router = express.Router();
1115

1216
router.use(cors());
1317
router.use(bodyParser.json());
1418
router.use(bodyParser.urlencoded({ extended: true }));
1519

20+
router.get("/", (req, res) => {
21+
res.send("Welcome, this is the REST API!");
22+
});
23+
1624
router.get("/hello", (req, res) => {
1725
res.json({ message: "Hello World!" });
1826
});
@@ -21,6 +29,7 @@ export function createApiRouter(agents: Map<string, AgentRuntime>) {
2129
const agentsList = Array.from(agents.values()).map((agent) => ({
2230
id: agent.agentId,
2331
name: agent.character.name,
32+
clients: Object.keys(agent.clients),
2433
}));
2534
res.json({ agents: agentsList });
2635
});
@@ -40,6 +49,43 @@ export function createApiRouter(agents: Map<string, AgentRuntime>) {
4049
});
4150
});
4251

52+
router.post("/agents/:agentId/set", async (req, res) => {
53+
const agentId = req.params.agentId;
54+
console.log('agentId', agentId)
55+
let agent:AgentRuntime = agents.get(agentId);
56+
57+
// update character
58+
if (agent) {
59+
// stop agent
60+
agent.stop()
61+
directClient.unregisterAgent(agent)
62+
// if it has a different name, the agentId will change
63+
}
64+
65+
// load character from body
66+
const character = req.body
67+
try {
68+
validateCharacterConfig(character)
69+
} catch(e) {
70+
elizaLogger.error(`Error parsing character: ${e}`);
71+
res.status(400).json({
72+
success: false,
73+
message: e.message,
74+
});
75+
return;
76+
}
77+
78+
// start it up (and register it)
79+
agent = await directClient.startAgent(character)
80+
elizaLogger.log(`${character.name} started`)
81+
82+
res.json({
83+
id: character.id,
84+
character: character,
85+
});
86+
});
87+
88+
4389
router.get("/agents/:agentId/channels", async (req, res) => {
4490
const agentId = req.params.agentId;
4591
const runtime = agents.get(agentId);

packages/client-direct/src/index.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,11 @@ Note that {{agentName}} is capable of reading/seeing/hearing various forms of me
5151
# Instructions: Write the next message for {{agentName}}.
5252
` + messageCompletionFooter;
5353

54-
export interface SimliClientConfig {
55-
apiKey: string;
56-
faceID: string;
57-
handleSilence: boolean;
58-
videoRef: any;
59-
audioRef: any;
60-
}
6154
export class DirectClient {
6255
public app: express.Application;
63-
private agents: Map<string, AgentRuntime>;
56+
private agents: Map<string, AgentRuntime>; // container management
6457
private server: any; // Store server instance
58+
public startAgent: Function; // Store startAgent functor
6559

6660
constructor() {
6761
elizaLogger.log("DirectClient constructor");
@@ -72,7 +66,7 @@ export class DirectClient {
7266
this.app.use(bodyParser.json());
7367
this.app.use(bodyParser.urlencoded({ extended: true }));
7468

75-
const apiRouter = createApiRouter(this.agents);
69+
const apiRouter = createApiRouter(this.agents, this);
7670
this.app.use(apiRouter);
7771

7872
// Define an interface that extends the Express Request interface
@@ -338,7 +332,7 @@ export class DirectClient {
338332
fileResponse.headers
339333
.get("content-disposition")
340334
?.split("filename=")[1]
341-
?.replace(/"/g, "") || "default_name.txt";
335+
?.replace(/"/g, /* " */ "") || "default_name.txt";
342336

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

@@ -378,6 +372,7 @@ export class DirectClient {
378372
);
379373
}
380374

375+
// agent/src/index.ts:startAgent calls this
381376
public registerAgent(runtime: AgentRuntime) {
382377
this.agents.set(runtime.agentId, runtime);
383378
}
@@ -388,7 +383,7 @@ export class DirectClient {
388383

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

394389
// Handle graceful shutdown
@@ -430,7 +425,7 @@ export const DirectClientInterface: Client = {
430425
client.start(serverPort);
431426
return client;
432427
},
433-
stop: async (_runtime: IAgentRuntime, client?: any) => {
428+
stop: async (_runtime: IAgentRuntime, client?: Client) => {
434429
if (client instanceof DirectClient) {
435430
client.stop();
436431
}

packages/core/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ export enum Clients {
611611
TWITTER = "twitter",
612612
TELEGRAM = "telegram",
613613
FARCASTER = "farcaster",
614+
AUTO = "auto",
614615
}
615616
/**
616617
* Configuration for an agent character

pnpm-lock.yaml

+30-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/smokeTests.sh

+29-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
# Strict mode, exit on error, undefined variables, and pipe failures
4+
set -euo pipefail
5+
36
# Print some information about the environment to aid in case of troubleshooting
47

58
echo "node version:"
@@ -27,60 +30,63 @@ if (( CURRENT_NODE_VERSION < REQUIRED_NODE_VERSION )); then
2730
fi
2831

2932
# Autodetect project directory relative to this script's path
30-
PROJECT_DIR="$0"
31-
while [ -h "$PROJECT_DIR" ]; do
32-
ls=$(ls -ld "$PROJECT_DIR")
33-
link=$(expr "$ls" : '.*-> \(.*\)$')
34-
if expr "$link" : '/.*' > /dev/null; then
35-
PROJECT_DIR="$link"
36-
else
37-
PROJECT_DIR="$(dirname "$PROJECT_DIR")/$link"
38-
fi
39-
done
40-
PROJECT_DIR="$(dirname "$PROJECT_DIR")/.."
41-
PROJECT_DIR="$(cd "$PROJECT_DIR"; pwd)"
33+
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
4234

43-
cd $PROJECT_DIR
35+
cd "$PROJECT_DIR"
4436

4537
cp .env.example .env
4638

4739
pnpm install -r
4840

4941
pnpm build
5042

43+
# Create temp file and ensure cleanup
5144
OUTFILE="$(mktemp)"
52-
echo $OUTFILE
45+
trap 'rm -f "$OUTFILE"' EXIT
46+
echo "Using temporary output file: $OUTFILE"
47+
48+
# Add timeout configuration
49+
TIMEOUT=30
50+
INTERVAL=0.5
51+
TIMER=0
52+
5353
(
54-
# Wait for the ready message
54+
# Wait for the ready message with timeout
5555
while true; do
56+
if [[ $TIMER -ge $TIMEOUT ]]; then
57+
echo "Error: Timeout waiting for application to start after $TIMEOUT seconds"
58+
kill $$
59+
exit 1
60+
fi
61+
5662
if grep -q "Chat started" "$OUTFILE"; then
5763
echo "exit"; sleep 2
5864
break
5965
fi
60-
sleep 0.5
66+
67+
sleep $INTERVAL
68+
TIMER=$(echo "$TIMER + $INTERVAL" | bc)
6169
done
6270
) | pnpm start --character=characters/trump.character.json > "$OUTFILE" &
6371

6472
# Wait for process to finish
6573
wait $!
6674
RESULT=$?
75+
6776
echo "----- OUTPUT START -----"
6877
cat "$OUTFILE"
6978
echo "----- OUTPUT END -----"
7079

7180
# Check the exit code of the last command
7281
if [[ $RESULT -ne 0 ]]; then
73-
echo "Error: 'start' command exited with an error."
82+
echo "Error: 'start' command exited with an error (code: $RESULT)"
7483
exit 1
7584
fi
7685

77-
# Check if output.txt contains "Terminating and cleaning up resources..."
86+
# Check if output contains expected termination message
7887
if grep -q "Terminating and cleaning up resources..." "$OUTFILE"; then
7988
echo "Script completed successfully."
8089
else
81-
echo "Error: The output does not contain the expected string."
90+
echo "Error: The output does not contain the expected termination message."
8291
exit 1
83-
fi
84-
85-
# Clean up
86-
rm "$OUTFILE"
92+
fi

0 commit comments

Comments
 (0)