Skip to content

Commit aa88d2b

Browse files
committed
fix the issues and simplify so agent is working
1 parent 9e6460d commit aa88d2b

15 files changed

+723
-5799
lines changed

agent/package.json

+1-14
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,15 @@
1919
"dependencies": {
2020
"@ai16z/adapter-postgres": "workspace:*",
2121
"@ai16z/adapter-sqlite": "workspace:*",
22-
"@ai16z/client-auto": "workspace:*",
23-
"@ai16z/client-direct": "workspace:*",
2422
"@ai16z/client-discord": "workspace:*",
25-
"@ai16z/client-farcaster": "workspace:*",
26-
"@ai16z/client-telegram": "workspace:*",
2723
"@ai16z/client-twitter": "workspace:*",
24+
"@ai16z/client-direct": "workspace:*",
2825
"@ai16z/eliza": "workspace:*",
29-
"@ai16z/plugin-0g": "workspace:*",
30-
"@ai16z/plugin-aptos": "workspace:*",
3126
"@ai16z/plugin-bootstrap": "workspace:*",
32-
"@ai16z/plugin-intiface": "workspace:*",
33-
"@ai16z/plugin-coinbase": "workspace:*",
34-
"@ai16z/plugin-conflux": "workspace:*",
3527
"@ai16z/plugin-evm": "workspace:*",
36-
"@ai16z/plugin-flow": "workspace:*",
3728
"@ai16z/plugin-goat": "workspace:*",
38-
"@ai16z/plugin-icp": "workspace:*",
3929
"@ai16z/plugin-image-generation": "workspace:*",
4030
"@ai16z/plugin-node": "workspace:*",
41-
"@ai16z/plugin-solana": "workspace:*",
42-
"@ai16z/plugin-starknet": "workspace:*",
43-
"@ai16z/plugin-tee": "workspace:*",
4431
"readline": "1.3.0",
4532
"ws": "8.18.0",
4633
"yargs": "17.7.2"

agent/src/index.ts

+31-136
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { PostgresDatabaseAdapter } from "@ai16z/adapter-postgres";
22
import { SqliteDatabaseAdapter } from "@ai16z/adapter-sqlite";
3-
import { AutoClientInterface } from "@ai16z/client-auto";
4-
import { DirectClientInterface } from "@ai16z/client-direct";
53
import { DiscordClientInterface } from "@ai16z/client-discord";
6-
import { TelegramClientInterface } from "@ai16z/client-telegram";
74
import { TwitterClientInterface } from "@ai16z/client-twitter";
8-
import { FarcasterAgentClient } from "@ai16z/client-farcaster";
5+
import { createNodePlugin } from "@ai16z/plugin-node";
6+
import { evmPlugin } from "@ai16z/plugin-evm";
7+
import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
8+
import { imageGenerationPlugin } from "@ai16z/plugin-image-generation";
99
import {
1010
AgentRuntime,
1111
CacheManager,
1212
Character,
13-
Clients,
1413
DbCacheAdapter,
1514
FsCacheAdapter,
1615
IAgentRuntime,
@@ -24,32 +23,14 @@ import {
2423
stringToUuid,
2524
validateCharacterConfig,
2625
} from "@ai16z/eliza";
27-
import { zgPlugin } from "@ai16z/plugin-0g";
2826
import createGoatPlugin from "@ai16z/plugin-goat";
29-
import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
30-
// import { intifacePlugin } from "@ai16z/plugin-intiface";
31-
import {
32-
coinbaseCommercePlugin,
33-
coinbaseMassPaymentsPlugin,
34-
tradePlugin,
35-
tokenContractPlugin,
36-
webhookPlugin,
37-
advancedTradePlugin,
38-
} from "@ai16z/plugin-coinbase";
39-
import { confluxPlugin } from "@ai16z/plugin-conflux";
40-
import { imageGenerationPlugin } from "@ai16z/plugin-image-generation";
41-
import { evmPlugin } from "@ai16z/plugin-evm";
42-
import { createNodePlugin } from "@ai16z/plugin-node";
43-
import { solanaPlugin } from "@ai16z/plugin-solana";
44-
import { teePlugin, TEEMode } from "@ai16z/plugin-tee";
45-
import { aptosPlugin, TransferAptosToken } from "@ai16z/plugin-aptos";
46-
import { flowPlugin } from "@ai16z/plugin-flow";
4727
import Database from "better-sqlite3";
4828
import fs from "fs";
4929
import path from "path";
5030
import readline from "readline";
5131
import { fileURLToPath } from "url";
5232
import yargs from "yargs";
33+
import { DirectClient } from "@ai16z/client-direct";
5334

5435
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
5536
const __dirname = path.dirname(__filename); // get the name of the directory
@@ -198,6 +179,7 @@ export async function loadCharacters(
198179
return loadedCharacters;
199180
}
200181

182+
// v2 TODO: should be a registered pattern to get the registered token from the plugin being used
201183
export function getTokenForProvider(
202184
provider: ModelProviderName,
203185
character: Character
@@ -276,24 +258,10 @@ export function getTokenForProvider(
276258
character.settings?.secrets?.VOLENGINE_API_KEY ||
277259
settings.VOLENGINE_API_KEY
278260
);
279-
case ModelProviderName.NANOGPT:
280-
return (
281-
character.settings?.secrets?.NANOGPT_API_KEY ||
282-
settings.NANOGPT_API_KEY
283-
);
284-
case ModelProviderName.HYPERBOLIC:
285-
return (
286-
character.settings?.secrets?.HYPERBOLIC_API_KEY ||
287-
settings.HYPERBOLIC_API_KEY
288-
);
289-
case ModelProviderName.VENICE:
290-
return (
291-
character.settings?.secrets?.VENICE_API_KEY ||
292-
settings.VENICE_API_KEY
293-
);
294261
}
295262
}
296263

264+
// v2 TODO: should be a registered pattern to get the database adapter being used
297265
function initializeDatabase(dataDir: string) {
298266
if (process.env.POSTGRES_URL) {
299267
elizaLogger.info("Initializing PostgreSQL connection...");
@@ -331,41 +299,23 @@ export async function initializeClients(
331299
// each client can only register once
332300
// and if we want two we can explicitly support it
333301
const clients: Record<string, any> = {};
334-
const clientTypes:string[] =
302+
const clientTypes: string[] =
335303
character.clients?.map((str) => str.toLowerCase()) || [];
336-
elizaLogger.log('initializeClients', clientTypes, 'for', character.name)
337-
338-
if (clientTypes.includes("auto")) {
339-
const autoClient = await AutoClientInterface.start(runtime);
340-
if (autoClient) clients.auto = autoClient;
341-
}
304+
elizaLogger.log("initializeClients", clientTypes, "for", character.name);
342305

306+
// v2 TODO: Instead of checking if client type includes "discord" or "twitter", should just be a loop and hook
307+
// So we don't need to check per platform
343308
if (clientTypes.includes("discord")) {
344309
const discordClient = await DiscordClientInterface.start(runtime);
345310
if (discordClient) clients.discord = discordClient;
346311
}
347312

348-
if (clientTypes.includes("telegram")) {
349-
const telegramClient = await TelegramClientInterface.start(runtime);
350-
if (telegramClient) clients.telegram = telegramClient;
351-
}
352-
353313
if (clientTypes.includes("twitter")) {
354-
TwitterClientInterface.enableSearch = !isFalsish(getSecret(character, "TWITTER_SEARCH_ENABLE"));
355314
const twitterClient = await TwitterClientInterface.start(runtime);
356315
if (twitterClient) clients.twitter = twitterClient;
357316
}
358317

359-
if (clientTypes.includes("farcaster")) {
360-
// why is this one different :(
361-
const farcasterClient = new FarcasterAgentClient(runtime);
362-
if (farcasterClient) {
363-
farcasterClient.start();
364-
clients.farcaster = farcasterClient;
365-
}
366-
}
367-
368-
elizaLogger.log('client keys', Object.keys(clients));
318+
elizaLogger.log("client keys", Object.keys(clients));
369319

370320
if (character.plugins?.length > 0) {
371321
for (const plugin of character.plugins) {
@@ -381,22 +331,6 @@ export async function initializeClients(
381331
return clients;
382332
}
383333

384-
function isFalsish(input: any): boolean {
385-
// If the input is exactly NaN, return true
386-
if (Number.isNaN(input)) {
387-
return true;
388-
}
389-
390-
// Convert input to a string if it's not null or undefined
391-
const value = input == null ? '' : String(input);
392-
393-
// List of common falsish string representations
394-
const falsishValues = ['false', '0', 'no', 'n', 'off', 'null', 'undefined', ''];
395-
396-
// Check if the value (trimmed and lowercased) is in the falsish list
397-
return falsishValues.includes(value.trim().toLowerCase());
398-
}
399-
400334
function getSecret(character: Character, secret: string) {
401335
return character.settings?.secrets?.[secret] || process.env[secret];
402336
}
@@ -408,7 +342,7 @@ export async function createAgent(
408342
db: IDatabaseAdapter,
409343
cache: ICacheManager,
410344
token: string
411-
):AgentRuntime {
345+
): Promise<AgentRuntime> {
412346
elizaLogger.success(
413347
elizaLogger.successesTitle,
414348
"Creating runtime for character",
@@ -417,17 +351,6 @@ export async function createAgent(
417351

418352
nodePlugin ??= createNodePlugin();
419353

420-
const teeMode = getSecret(character, "TEE_MODE") || "OFF";
421-
const walletSecretSalt = getSecret(character, "WALLET_SECRET_SALT");
422-
423-
// Validate TEE configuration
424-
if (teeMode !== TEEMode.OFF && !walletSecretSalt) {
425-
elizaLogger.error(
426-
"WALLET_SECRET_SALT required when TEE_MODE is enabled"
427-
);
428-
throw new Error("Invalid TEE configuration");
429-
}
430-
431354
let goatPlugin: any | undefined;
432355
if (getSecret(character, "ALCHEMY_API_KEY")) {
433356
goatPlugin = await createGoatPlugin((secret) =>
@@ -444,53 +367,19 @@ export async function createAgent(
444367
// character.plugins are handled when clients are added
445368
plugins: [
446369
bootstrapPlugin,
447-
getSecret(character, "CONFLUX_CORE_PRIVATE_KEY")
448-
? confluxPlugin
449-
: null,
450370
nodePlugin,
451-
getSecret(character, "SOLANA_PUBLIC_KEY") ||
452-
(getSecret(character, "WALLET_PUBLIC_KEY") &&
453-
!getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
454-
? solanaPlugin
455-
: null,
456371
getSecret(character, "EVM_PRIVATE_KEY") ||
457372
(getSecret(character, "WALLET_PUBLIC_KEY") &&
458373
getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
459374
? evmPlugin
460375
: null,
461-
getSecret(character, "ZEROG_PRIVATE_KEY") ? zgPlugin : null,
462-
getSecret(character, "COINBASE_COMMERCE_KEY")
463-
? coinbaseCommercePlugin
464-
: null,
465376
getSecret(character, "FAL_API_KEY") ||
466377
getSecret(character, "OPENAI_API_KEY") ||
467378
getSecret(character, "VENICE_API_KEY") ||
468379
getSecret(character, "HEURIST_API_KEY")
469380
? imageGenerationPlugin
470381
: null,
471-
...(getSecret(character, "COINBASE_API_KEY") &&
472-
getSecret(character, "COINBASE_PRIVATE_KEY")
473-
? [
474-
coinbaseMassPaymentsPlugin,
475-
tradePlugin,
476-
tokenContractPlugin,
477-
advancedTradePlugin,
478-
]
479-
: []),
480-
...(teeMode !== TEEMode.OFF && walletSecretSalt
481-
? [teePlugin, solanaPlugin]
482-
: []),
483-
getSecret(character, "COINBASE_API_KEY") &&
484-
getSecret(character, "COINBASE_PRIVATE_KEY") &&
485-
getSecret(character, "COINBASE_NOTIFICATION_URI")
486-
? webhookPlugin
487-
: null,
488382
getSecret(character, "ALCHEMY_API_KEY") ? goatPlugin : null,
489-
getSecret(character, "FLOW_ADDRESS") &&
490-
getSecret(character, "FLOW_PRIVATE_KEY")
491-
? flowPlugin
492-
: null,
493-
getSecret(character, "APTOS_PRIVATE_KEY") ? aptosPlugin : null,
494383
].filter(Boolean),
495384
providers: [],
496385
actions: [],
@@ -513,7 +402,10 @@ function initializeDbCache(character: Character, db: IDatabaseCacheAdapter) {
513402
return cache;
514403
}
515404

516-
async function startAgent(character: Character, directClient):AgentRuntime {
405+
async function startAgent(
406+
character: Character,
407+
directClient
408+
): Promise<AgentRuntime> {
517409
let db: IDatabaseAdapter & IDatabaseCacheAdapter;
518410
try {
519411
character.id ??= stringToUuid(character.name);
@@ -532,19 +424,24 @@ async function startAgent(character: Character, directClient):AgentRuntime {
532424
await db.init();
533425

534426
const cache = initializeDbCache(character, db);
535-
const runtime:AgentRuntime = await createAgent(character, db, cache, token);
427+
const runtime: AgentRuntime = await createAgent(
428+
character,
429+
db,
430+
cache,
431+
token
432+
);
536433

537434
// start services/plugins/process knowledge
538435
await runtime.initialize();
539436

540437
// start assigned clients
541438
runtime.clients = await initializeClients(character, runtime);
542439

543-
// add to container
544440
directClient.registerAgent(runtime);
441+
console.log("registered agent");
545442

546443
// report to console
547-
elizaLogger.debug(`Started ${character.name} as ${runtime.agentId}`)
444+
elizaLogger.debug(`Started ${character.name} as ${runtime.agentId}`);
548445

549446
return runtime;
550447
} catch (error) {
@@ -561,7 +458,8 @@ async function startAgent(character: Character, directClient):AgentRuntime {
561458
}
562459

563460
const startAgents = async () => {
564-
const directClient = await DirectClientInterface.start();
461+
const dc = new DirectClient();
462+
await dc.start(parseInt(settings.SERVER_PORT || "3000"));
565463
const args = parseArguments();
566464

567465
let charactersArg = args.characters || args.character;
@@ -574,20 +472,16 @@ const startAgents = async () => {
574472

575473
try {
576474
for (const character of characters) {
577-
await startAgent(character, directClient);
475+
await startAgent(character, dc);
578476
}
579477
} catch (error) {
580478
elizaLogger.error("Error starting agents:", error);
581479
}
582-
// upload some agent functionality into directClient
583-
directClient.startAgent = async character => {
584-
// wrap it so we don't have to inject directClient later
585-
return startAgent(character, directClient)
586-
};
587480

588481
function chat() {
589482
const agentId = characters[0].name ?? "Agent";
590483
rl.question("You: ", async (input) => {
484+
console.log("input", input);
591485
await handleUserInput(input, agentId);
592486
if (input.toLowerCase() !== "exit") {
593487
chat(); // Loop back to ask another question
@@ -617,8 +511,9 @@ async function handleUserInput(input, agentId) {
617511
}
618512

619513
try {
514+
console.log("input", input);
620515
const serverPort = parseInt(settings.SERVER_PORT || "3000");
621-
516+
console.log("send message");
622517
const response = await fetch(
623518
`http://localhost:${serverPort}/${agentId}/message`,
624519
{

packages/client-direct/.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*
2+
3+
!dist/**
4+
!package.json
5+
!readme.md
6+
!tsup.config.ts
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import eslintGlobalConfig from "../../eslint.config.mjs";
2+
3+
export default [...eslintGlobalConfig];

packages/client-direct/package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@ai16z/client-direct",
3+
"version": "0.1.5-alpha.5",
4+
"main": "dist/index.js",
5+
"type": "module",
6+
"types": "dist/index.d.ts",
7+
"dependencies": {
8+
"@ai16z/eliza": "workspace:*",
9+
"@ai16z/plugin-image-generation": "workspace:*",
10+
"@types/body-parser": "1.19.5",
11+
"@types/cors": "2.8.17",
12+
"@types/express": "5.0.0",
13+
"body-parser": "1.20.3",
14+
"cors": "2.8.5",
15+
"discord.js": "14.16.3",
16+
"express": "4.21.1",
17+
"multer": "1.4.5-lts.1"
18+
},
19+
"devDependencies": {
20+
"tsup": "8.3.5"
21+
},
22+
"scripts": {
23+
"build": "tsup --format esm --dts",
24+
"dev": "tsup --format esm --dts --watch",
25+
"lint": "eslint . --fix"
26+
},
27+
"peerDependencies": {
28+
"whatwg-url": "7.1.0"
29+
}
30+
}

0 commit comments

Comments
 (0)