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

chore: dynamic plugin imports #1383

Open
wants to merge 25 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9d967c8
dynamic import plugins
ChristopherTrimboli Dec 22, 2024
cb945d2
Merge branch 'develop' into chore/dynamic-plugins
ChristopherTrimboli Dec 22, 2024
8e86d66
loadPlugin abstracted function
ChristopherTrimboli Dec 23, 2024
0747012
Merge remote-tracking branch 'upstream/develop' into chore/dynamic-pl…
ChristopherTrimboli Dec 23, 2024
e510db6
Merge remote-tracking branch 'upstream/develop' into chore/dynamic-pl…
ChristopherTrimboli Dec 23, 2024
2efee64
change to callback import param
ChristopherTrimboli Dec 23, 2024
c5d4dc4
edit dev.sh plugin instructions
ChristopherTrimboli Dec 23, 2024
af11d77
format lint
ChristopherTrimboli Dec 23, 2024
feba542
strict Plugin type imports
ChristopherTrimboli Dec 23, 2024
70c207a
fix default plugin goat usage
ChristopherTrimboli Dec 23, 2024
23cb080
loadPlugins, DynamicPlugin type, core/plugins helpers
ChristopherTrimboli Dec 23, 2024
12d8537
nuke Goat plugin
ChristopherTrimboli Dec 23, 2024
7b2a150
tune up forgetten plugins
ChristopherTrimboli Dec 23, 2024
2199949
remove JS docs
ChristopherTrimboli Dec 23, 2024
0726718
promise all loadPlugins
ChristopherTrimboli Dec 23, 2024
b243fb4
Merge remote-tracking branch 'upstream/develop' into chore/dynamic-pl…
ChristopherTrimboli Dec 24, 2024
98c6a89
remove TEEMode import
ChristopherTrimboli Dec 24, 2024
32c7e5a
import cache to solve multiple imports
ChristopherTrimboli Dec 26, 2024
fb8dda4
Merge remote-tracking branch 'upstream/develop' into chore/dynamic-pl…
ChristopherTrimboli Dec 26, 2024
217754b
remove junk function
ChristopherTrimboli Dec 26, 2024
5896700
add abstract plugin back, regen lockfile
ChristopherTrimboli Dec 26, 2024
430dab1
consolidate same secret coinbase plugins
ChristopherTrimboli Dec 26, 2024
4f90663
Merge remote-tracking branch 'upstream/develop' into chore/dynamic-pl…
ChristopherTrimboli Dec 26, 2024
d22a710
Merge remote-tracking branch 'upstream/develop' into chore/dynamic-pl…
ChristopherTrimboli Dec 26, 2024
d77f058
add chronos plugin back
ChristopherTrimboli Dec 26, 2024
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
264 changes: 148 additions & 116 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,13 @@ import {
stringToUuid,
validateCharacterConfig,
CacheStore,
getSecret,
loadPlugins,
} from "@elizaos/core";
import { RedisClient } from "@elizaos/adapter-redis";
import { zgPlugin } from "@elizaos/plugin-0g";
import { bootstrapPlugin } from "@elizaos/plugin-bootstrap";
import createGoatPlugin from "@elizaos/plugin-goat";
// import { intifacePlugin } from "@elizaos/plugin-intiface";
import { DirectClient } from "@elizaos/client-direct";
import { aptosPlugin } from "@elizaos/plugin-aptos";
import {
advancedTradePlugin,
coinbaseCommercePlugin,
coinbaseMassPaymentsPlugin,
tokenContractPlugin,
tradePlugin,
webhookPlugin,
} from "@elizaos/plugin-coinbase";
import { confluxPlugin } from "@elizaos/plugin-conflux";
import { evmPlugin } from "@elizaos/plugin-evm";
import { storyPlugin } from "@elizaos/plugin-story";
import { flowPlugin } from "@elizaos/plugin-flow";
import { imageGenerationPlugin } from "@elizaos/plugin-image-generation";
import { ThreeDGenerationPlugin } from "@elizaos/plugin-3d-generation";
import { multiversxPlugin } from "@elizaos/plugin-multiversx";
import { nearPlugin } from "@elizaos/plugin-near";
import { nftGenerationPlugin } from "@elizaos/plugin-nft-generation";
import { createNodePlugin } from "@elizaos/plugin-node";
import { solanaPlugin } from "@elizaos/plugin-solana";
import { suiPlugin } from "@elizaos/plugin-sui";
import { TEEMode, teePlugin } from "@elizaos/plugin-tee";
import { tonPlugin } from "@elizaos/plugin-ton";
import { zksyncEraPlugin } from "@elizaos/plugin-zksync-era";
import { cronosZkEVMPlugin } from "@elizaos/plugin-cronoszkevm";
import { abstractPlugin } from "@elizaos/plugin-abstract";
import Database from "better-sqlite3";
import fs from "fs";
import path from "path";
Expand Down Expand Up @@ -451,10 +425,6 @@ export async function initializeClients(
return clients;
}

function getSecret(character: Character, secret: string) {
return character.settings?.secrets?.[secret] || process.env[secret];
}

let nodePlugin: any | undefined;

export async function createAgent(
Expand All @@ -475,19 +445,155 @@ export async function createAgent(
const walletSecretSalt = getSecret(character, "WALLET_SECRET_SALT");

// Validate TEE configuration
if (teeMode !== TEEMode.OFF && !walletSecretSalt) {
if (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, "EVM_PROVIDER_URL")) {
goatPlugin = await createGoatPlugin((secret) =>
getSecret(character, secret)
);
}
const dynamicPlugins = await loadPlugins(character, [
{
secrets: ["COINBASE_API_KEY", "COINBASE_PRIVATE_KEY"],
importFn: () =>
import("@elizaos/plugin-coinbase").then((m) => [
m.coinbaseMassPaymentsPlugin,
m.tradePlugin,
m.tokenContractPlugin,
m.advancedTradePlugin,
]),
},
{
secrets: "COINBASE_COMMERCE_KEY",
importFn: () =>
import("@elizaos/plugin-coinbase").then(
(m) => m.coinbaseCommercePlugin
),
},
{
secrets: [
"COINBASE_API_KEY",
"COINBASE_PRIVATE_KEY",
"COINBASE_NOTIFICATION_URI",
],
importFn: () =>
import("@elizaos/plugin-coinbase").then((m) => m.webhookPlugin),
},
{
secrets: "CONFLUX_CORE_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-conflux").then((m) => m.confluxPlugin),
},
{
secrets: "SOLANA_PUBLIC_KEY",
importFn: () =>
import("@elizaos/plugin-solana").then((m) => m.solanaPlugin),
},
{
secrets: [
"NEAR_ADDRESS",
"NEAR_WALLET_PUBLIC_KEY",
"NEAR_WALLET_SECRET_KEY",
],
importFn: () =>
import("@elizaos/plugin-near").then((m) => m.nearPlugin),
},
{
secrets: "EVM_PUBLIC_KEY",
importFn: () =>
import("@ai16z/plugin-evm").then((m) => m.evmPlugin),
},
{
secrets: [
"SOLANA_PUBLIC_KEY",
"SOLANA_ADMIN_PUBLIC_KEY",
"SOLANA_PRIVATE_KEY",
"SOLANA_ADMIN_PRIVATE_KEY",
],
importFn: () =>
import("@elizaos/plugin-nft-generation").then(
(m) => m.nftGenerationPlugin
),
},
{
secrets: "ZEROG_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-0g").then((m) => m.zgPlugin),
},
{
secrets: [
"FAL_API_KEY",
"OPENAI_API_KEY",
"VENICE_API_KEY",
"HEURIST_API_KEY",
],
importFn: () =>
import("@elizaos/plugin-image-generation").then(
(m) => m.imageGenerationPlugin
),
},
{
secrets: ["ABSTRACT_PRIVATE_KEY"],
importFn: () =>
import("@elizaos/plugin-abstract").then(
(m) => m.abstractPlugin
),
},
{
secrets: ["FLOW_ADDRESS", "FLOW_PRIVATE_KEY"],
importFn: () =>
import("@elizaos/plugin-flow").then((m) => m.flowPlugin),
},
{
secrets: "APTOS_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-aptos").then((m) => m.aptosPlugin),
},
{
secrets: "MVX_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-multiversx").then(
(m) => m.multiversxPlugin
),
},
{
secrets: "ZKSYNC_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-zksync-era").then(
(m) => m.zksyncEraPlugin
),
},
{
secrets: "TON_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-ton").then((m) => m.tonPlugin),
},
{
secrets: "SUI_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-sui").then((m) => m.suiPlugin),
},
{
secrets: "STORY_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-story").then((m) => m.storyPlugin),
},
{
secrets: "CRONOSZKEVM_PRIVATE_KEY",
importFn: () =>
import("@elizaos/plugin-cronoszkevm").then(
(m) => m.cronosZkEVMPlugin
),
},
...(teeMode === "OFF" &&
walletSecretSalt && [
{
secrets: [],
importFn: () =>
import("@elizaos/plugin-tee").then((m) => m.teePlugin),
},
]),
]);

return new AgentRuntime({
databaseAdapter: db,
Expand All @@ -496,83 +602,9 @@ export async function createAgent(
evaluators: [],
character,
// 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, "WALLET_PUBLIC_KEY") &&
getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith("0x"))
? evmPlugin
: null,
(getSecret(character, "SOLANA_PUBLIC_KEY") ||
(getSecret(character, "WALLET_PUBLIC_KEY") &&
!getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith(
"0x"
))) &&
getSecret(character, "SOLANA_ADMIN_PUBLIC_KEY") &&
getSecret(character, "SOLANA_PRIVATE_KEY") &&
getSecret(character, "SOLANA_ADMIN_PRIVATE_KEY")
? nftGenerationPlugin
: 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, "FAL_API_KEY") ? ThreeDGenerationPlugin : 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, "EVM_PROVIDER_URL") ? goatPlugin : null,
getSecret(character, "ABSTRACT_PRIVATE_KEY")
? abstractPlugin
: 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,
getSecret(character, "ZKSYNC_PRIVATE_KEY") ? zksyncEraPlugin : null,
getSecret(character, "CRONOSZKEVM_PRIVATE_KEY")
? cronosZkEVMPlugin
: null,
getSecret(character, "TON_PRIVATE_KEY") ? tonPlugin : null,
getSecret(character, "SUI_PRIVATE_KEY") ? suiPlugin : null,
getSecret(character, "STORY_PRIVATE_KEY") ? storyPlugin : null,
].filter(Boolean),
plugins: [bootstrapPlugin, nodePlugin, ...dynamicPlugins].filter(
Boolean
),
providers: [],
actions: [],
services: [],
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export * from "./parsing.ts";
export * from "./uuid.ts";
export * from "./environment.ts";
export * from "./cache.ts";
export * from "./plugins.ts";
export { default as knowledge } from "./knowledge.ts";
export * from "./utils.ts";
52 changes: 52 additions & 0 deletions packages/core/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Character, DynamicPlugin, Plugin } from "./types";

export function getSecret(
character: Character,
secret: string
): string | undefined {
return character.settings?.secrets?.[secret] ?? process.env[secret];
}

export async function loadPlugin(
keys: string | string[],
character: Character,
importFn: () => Promise<Plugin>
): Promise<Plugin | null> {
const keyArray = Array.isArray(keys) ? keys : [keys];
const hasAllSecrets =
keyArray.every((key) => getSecret(character, key)) ||
keyArray.length === 0;
if (!hasAllSecrets) return null;
return importFn();
}

const importCache = new Map<
() => Promise<Plugin | Plugin[]>,
Promise<Plugin | Plugin[]>
>();

export async function loadPlugins(
character: Character,
definitions: DynamicPlugin[]
): Promise<Plugin[]> {
const plugins: Plugin[] = [];

for (const def of definitions) {
const keyArray = Array.isArray(def.secrets)
? def.secrets
: [def.secrets];
const hasAllSecrets =
keyArray.every((key) => getSecret(character, key)) ||
keyArray.length === 0;
if (!hasAllSecrets) continue;

let importPromise = importCache.get(def.importFn);
if (!importPromise) {
importPromise = def.importFn();
importCache.set(def.importFn, importPromise);
}
const result = await importPromise;
plugins.push(...(Array.isArray(result) ? result : [result]));
}
return plugins;
}
5 changes: 5 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ export type Plugin = {
clients?: Client[];
};

export interface DynamicPlugin {
secrets: string | string[];
importFn: () => Promise<Plugin>;
}

/**
* Available client platforms
*/
Expand Down
Loading
Loading