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

V2 #1061

Closed
wants to merge 7 commits into from
Closed

V2 #1061

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
17 changes: 1 addition & 16 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,15 @@
"dependencies": {
"@ai16z/adapter-postgres": "workspace:*",
"@ai16z/adapter-sqlite": "workspace:*",
"@ai16z/client-auto": "workspace:*",
"@ai16z/client-direct": "workspace:*",
"@ai16z/client-discord": "workspace:*",
"@ai16z/client-farcaster": "workspace:*",
"@ai16z/client-telegram": "workspace:*",
"@ai16z/client-twitter": "workspace:*",
"@ai16z/client-direct": "workspace:*",
"@ai16z/eliza": "workspace:*",
"@ai16z/plugin-0g": "workspace:*",
"@ai16z/plugin-aptos": "workspace:*",
"@ai16z/plugin-bootstrap": "workspace:*",
"@ai16z/plugin-intiface": "workspace:*",
"@ai16z/plugin-coinbase": "workspace:*",
"@ai16z/plugin-conflux": "workspace:*",
"@ai16z/plugin-evm": "workspace:*",
"@ai16z/plugin-flow": "workspace:*",
"@ai16z/plugin-goat": "workspace:*",
"@ai16z/plugin-icp": "workspace:*",
"@ai16z/plugin-image-generation": "workspace:*",
"@ai16z/plugin-node": "workspace:*",
"@ai16z/plugin-solana": "workspace:*",
"@ai16z/plugin-starknet": "workspace:*",
"@ai16z/plugin-tee": "workspace:*",
"@ai16z/plugin-multiversx": "workspace:*",
"@ai16z/plugin-near": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
Expand Down
131 changes: 16 additions & 115 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { PostgresDatabaseAdapter } from "@ai16z/adapter-postgres";
import { SqliteDatabaseAdapter } from "@ai16z/adapter-sqlite";
import { AutoClientInterface } from "@ai16z/client-auto";
import { DirectClientInterface } from "@ai16z/client-direct";
import { DiscordClientInterface } from "@ai16z/client-discord";
import { TelegramClientInterface } from "@ai16z/client-telegram";
import { TwitterClientInterface } from "@ai16z/client-twitter";
import { FarcasterAgentClient } from "@ai16z/client-farcaster";
import { createNodePlugin } from "@ai16z/plugin-node";
import { evmPlugin } from "@ai16z/plugin-evm";
import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
import { imageGenerationPlugin } from "@ai16z/plugin-image-generation";
import {
AgentRuntime,
CacheManager,
Character,
Clients,
DbCacheAdapter,
FsCacheAdapter,
IAgentRuntime,
Expand All @@ -24,34 +23,14 @@ import {
stringToUuid,
validateCharacterConfig,
} from "@ai16z/eliza";
import { zgPlugin } from "@ai16z/plugin-0g";
import createGoatPlugin from "@ai16z/plugin-goat";
import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
// import { intifacePlugin } from "@ai16z/plugin-intiface";
import {
coinbaseCommercePlugin,
coinbaseMassPaymentsPlugin,
tradePlugin,
tokenContractPlugin,
webhookPlugin,
advancedTradePlugin,
} from "@ai16z/plugin-coinbase";
import { confluxPlugin } from "@ai16z/plugin-conflux";
import { imageGenerationPlugin } from "@ai16z/plugin-image-generation";
import { evmPlugin } from "@ai16z/plugin-evm";
import { createNodePlugin } from "@ai16z/plugin-node";
import { solanaPlugin } from "@ai16z/plugin-solana";
import { teePlugin, TEEMode } from "@ai16z/plugin-tee";
import { aptosPlugin, TransferAptosToken } from "@ai16z/plugin-aptos";
import { flowPlugin } from "@ai16z/plugin-flow";
import { multiversxPlugin } from "@ai16z/plugin-multiversx";
import { nearPlugin } from "@ai16z/plugin-near";
import Database from "better-sqlite3";
import fs from "fs";
import path from "path";
import readline from "readline";
import { fileURLToPath } from "url";
import yargs from "yargs";
import { DirectClient } from "@ai16z/client-direct";

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 Down Expand Up @@ -200,6 +179,7 @@ export async function loadCharacters(
return loadedCharacters;
}

// v2 TODO: should be a registered pattern to get the registered token from the plugin being used
export function getTokenForProvider(
provider: ModelProviderName,
character: Character
Expand Down Expand Up @@ -278,24 +258,10 @@ export function getTokenForProvider(
character.settings?.secrets?.VOLENGINE_API_KEY ||
settings.VOLENGINE_API_KEY
);
case ModelProviderName.NANOGPT:
return (
character.settings?.secrets?.NANOGPT_API_KEY ||
settings.NANOGPT_API_KEY
);
case ModelProviderName.HYPERBOLIC:
return (
character.settings?.secrets?.HYPERBOLIC_API_KEY ||
settings.HYPERBOLIC_API_KEY
);
case ModelProviderName.VENICE:
return (
character.settings?.secrets?.VENICE_API_KEY ||
settings.VENICE_API_KEY
);
}
}

// v2 TODO: should be a registered pattern to get the database adapter being used
function initializeDatabase(dataDir: string) {
if (process.env.POSTGRES_URL) {
elizaLogger.info("Initializing PostgreSQL connection...");
Expand Down Expand Up @@ -337,21 +303,13 @@ export async function initializeClients(
character.clients?.map((str) => str.toLowerCase()) || [];
elizaLogger.log("initializeClients", clientTypes, "for", character.name);

if (clientTypes.includes("auto")) {
const autoClient = await AutoClientInterface.start(runtime);
if (autoClient) clients.auto = autoClient;
}

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

if (clientTypes.includes("telegram")) {
const telegramClient = await TelegramClientInterface.start(runtime);
if (telegramClient) clients.telegram = telegramClient;
}

if (clientTypes.includes("twitter")) {
const twitterClient = await TwitterClientInterface.start(runtime);
// TODO: This might be incorrect, please test if you are concerned about this functionality
Expand All @@ -362,15 +320,6 @@ export async function initializeClients(
if (twitterClient) clients.twitter = twitterClient;
}

if (clientTypes.includes("farcaster")) {
// why is this one different :(
const farcasterClient = new FarcasterAgentClient(runtime);
if (farcasterClient) {
farcasterClient.start();
clients.farcaster = farcasterClient;
}
}

elizaLogger.log("client keys", Object.keys(clients));

if (character.plugins?.length > 0) {
Expand Down Expand Up @@ -432,17 +381,6 @@ export async function createAgent(

nodePlugin ??= createNodePlugin();

const teeMode = getSecret(character, "TEE_MODE") || "OFF";
const walletSecretSalt = getSecret(character, "WALLET_SECRET_SALT");

// Validate TEE configuration
if (teeMode !== TEEMode.OFF && !walletSecretSalt) {
elizaLogger.error(
"WALLET_SECRET_SALT required when TEE_MODE is enabled"
);
throw new Error("Invalid TEE configuration");
}

let goatPlugin: any | undefined;
if (getSecret(character, "ALCHEMY_API_KEY")) {
goatPlugin = await createGoatPlugin((secret) =>
Expand All @@ -459,59 +397,19 @@ export async function createAgent(
// character.plugins are handled when clients are added
plugins: [
bootstrapPlugin,
getSecret(character, "CONFLUX_CORE_PRIVATE_KEY")
? confluxPlugin
: null,
nodePlugin,
getSecret(character, "SOLANA_PUBLIC_KEY") ||
(getSecret(character, "WALLET_PUBLIC_KEY") &&
!getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
? solanaPlugin
: null,
(getSecret(character, "NEAR_ADDRESS") ||
getSecret(character, "NEAR_WALLET_PUBLIC_KEY")) &&
getSecret(character, "NEAR_WALLET_SECRET_KEY")
? nearPlugin
: null,
getSecret(character, "EVM_PUBLIC_KEY") ||
getSecret(character, "EVM_PRIVATE_KEY") ||
(getSecret(character, "WALLET_PUBLIC_KEY") &&
getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
? evmPlugin
: null,
getSecret(character, "ZEROG_PRIVATE_KEY") ? zgPlugin : null,
getSecret(character, "COINBASE_COMMERCE_KEY")
? coinbaseCommercePlugin
: null,
getSecret(character, "FAL_API_KEY") ||
getSecret(character, "OPENAI_API_KEY") ||
getSecret(character, "VENICE_API_KEY") ||
getSecret(character, "HEURIST_API_KEY")
? imageGenerationPlugin
: null,
...(getSecret(character, "COINBASE_API_KEY") &&
getSecret(character, "COINBASE_PRIVATE_KEY")
? [
coinbaseMassPaymentsPlugin,
tradePlugin,
tokenContractPlugin,
advancedTradePlugin,
]
: []),
...(teeMode !== TEEMode.OFF && walletSecretSalt
? [teePlugin, solanaPlugin]
: []),
getSecret(character, "COINBASE_API_KEY") &&
getSecret(character, "COINBASE_PRIVATE_KEY") &&
getSecret(character, "COINBASE_NOTIFICATION_URI")
? webhookPlugin
: null,
getSecret(character, "ALCHEMY_API_KEY") ? goatPlugin : null,
getSecret(character, "FLOW_ADDRESS") &&
getSecret(character, "FLOW_PRIVATE_KEY")
? flowPlugin
: null,
getSecret(character, "APTOS_PRIVATE_KEY") ? aptosPlugin : null,
getSecret(character, "MVX_PRIVATE_KEY") ? multiversxPlugin : null,
].filter(Boolean),
providers: [],
actions: [],
Expand Down Expand Up @@ -569,8 +467,8 @@ async function startAgent(
// start assigned clients
runtime.clients = await initializeClients(character, runtime);

// add to container
directClient.registerAgent(runtime);
console.log("registered agent");

// report to console
elizaLogger.debug(`Started ${character.name} as ${runtime.agentId}`);
Expand All @@ -590,7 +488,8 @@ async function startAgent(
}

const startAgents = async () => {
const directClient = await DirectClientInterface.start(null);
const directClient = new DirectClient();
await directClient.start(parseInt(settings.SERVER_PORT || "3000"));
const args = parseArguments();

let charactersArg = args.characters || args.character;
Expand All @@ -612,6 +511,7 @@ const startAgents = async () => {
function chat() {
const agentId = characters[0].name ?? "Agent";
rl.question("You: ", async (input) => {
console.log("input", input);
await handleUserInput(input, agentId);
if (input.toLowerCase() !== "exit") {
chat(); // Loop back to ask another question
Expand Down Expand Up @@ -641,8 +541,9 @@ async function handleUserInput(input, agentId) {
}

try {
console.log("input", input);
const serverPort = parseInt(settings.SERVER_PORT || "3000");

console.log("send message");
const response = await fetch(
`http://localhost:${serverPort}/${agentId}/message`,
{
Expand Down
Loading
Loading