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 26 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 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
df634eb
Merge remote-tracking branch 'upstream/develop' into chore/dynamic-pl…
ChristopherTrimboli Dec 27, 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
201 changes: 111 additions & 90 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,14 @@ import {
stringToUuid,
validateCharacterConfig,
CacheStore,
Plugin,
} 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 { 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 { TEEMode } from "@elizaos/plugin-tee";
ChristopherTrimboli marked this conversation as resolved.
Show resolved Hide resolved
import Database from "better-sqlite3";
import fs from "fs";
import path from "path";
Expand Down Expand Up @@ -495,6 +473,18 @@ export async function createAgent(
);
}
ChristopherTrimboli marked this conversation as resolved.
Show resolved Hide resolved

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

return new AgentRuntime({
databaseAdapter: db,
token,
Expand All @@ -504,73 +494,104 @@ 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, "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, "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,
getSecret(character, "ZKSYNC_PRIVATE_KEY") ? zksyncEraPlugin : null,
getSecret(character, "TON_PRIVATE_KEY") ? tonPlugin : null,
getSecret(character, "SUI_PRIVATE_KEY") ? suiPlugin : null,
getSecret(character, "STORY_PRIVATE_KEY") ? storyPlugin : null,
await loadPlugin(["COINBASE_API_KEY", "COINBASE_PRIVATE_KEY"], () =>
import("@elizaos/plugin-coinbase").then(
(m) => m.coinbaseMassPaymentsPlugin
)
),
await loadPlugin(["COINBASE_API_KEY", "COINBASE_PRIVATE_KEY"], () =>
import("@elizaos/plugin-coinbase").then((m) => m.tradePlugin)
),
await loadPlugin(["COINBASE_API_KEY", "COINBASE_PRIVATE_KEY"], () =>
import("@elizaos/plugin-coinbase").then(
(m) => m.tokenContractPlugin
)
),
await loadPlugin(["COINBASE_API_KEY", "COINBASE_PRIVATE_KEY"], () =>
import("@elizaos/plugin-coinbase").then(
(m) => m.advancedTradePlugin
)
),
await loadPlugin("CONFLUX_CORE_PRIVATE_KEY", () =>
import("@elizaos/plugin-conflux").then((m) => m.confluxPlugin)
),
await loadPlugin("SOLANA_PUBLIC_KEY", () =>
import("@elizaos/plugin-solana").then((m) => m.solanaPlugin)
),
await loadPlugin(
[
"NEAR_ADDRESS",
"NEAR_WALLET_PUBLIC_KEY",
"NEAR_WALLET_SECRET_KEY",
],
() => import("@elizaos/plugin-near").then((m) => m.nearPlugin)
),
await loadPlugin("EVM_PUBLIC_KEY", () =>
import("@ai16z/plugin-evm").then((m) => m.evmPlugin)
),
await loadPlugin(
[
"SOLANA_PUBLIC_KEY",
"SOLANA_ADMIN_PUBLIC_KEY",
"SOLANA_PRIVATE_KEY",
"SOLANA_ADMIN_PRIVATE_KEY",
],
() =>
import("@elizaos/plugin-nft-generation").then(
(m) => m.nftGenerationPlugin
)
),
await loadPlugin("ZEROG_PRIVATE_KEY", () =>
import("@elizaos/plugin-0g").then((m) => m.zgPlugin)
),
await loadPlugin("COINBASE_COMMERCE_KEY", () =>
import("@elizaos/plugin-coinbase").then(
(m) => m.coinbaseCommercePlugin
)
),
await loadPlugin(
[
"FAL_API_KEY",
"OPENAI_API_KEY",
"VENICE_API_KEY",
"HEURIST_API_KEY",
],
() =>
import("@elizaos/plugin-image-generation").then(
(m) => m.imageGenerationPlugin
)
),
await loadPlugin(["ALCHEMY_API_KEY"], () =>
import("@elizaos/plugin-goat").then((m) =>
m.default((secret: string) => getSecret(character, secret))
)
),
await loadPlugin("FLOW_ADDRESS", () =>
import("@elizaos/plugin-flow").then((m) => m.flowPlugin)
),
await loadPlugin("APTOS_PRIVATE_KEY", () =>
import("@elizaos/plugin-aptos").then((m) => m.aptosPlugin)
),
await loadPlugin("MVX_PRIVATE_KEY", () =>
import("@elizaos/plugin-multiversx").then(
(m) => m.multiversxPlugin
)
),
await loadPlugin("ZKSYNC_PRIVATE_KEY", () =>
import("@elizaos/plugin-zksync-era").then(
(m) => m.zksyncEraPlugin
)
),
await loadPlugin("TON_PRIVATE_KEY", () =>
import("@elizaos/plugin-ton").then((m) => m.tonPlugin)
),
await loadPlugin("SUI_PRIVATE_KEY", () =>
import("@elizaos/plugin-sui").then((m) => m.suiPlugin)
),
await loadPlugin("STORY_PRIVATE_KEY", () =>
import("@elizaos/plugin-story").then((m) => m.storyPlugin)
),
].filter(Boolean),
providers: [],
actions: [],
Expand Down
24 changes: 11 additions & 13 deletions scripts/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,17 @@ cat << "EOF"
* *
* "@elizaos/your-plugin-name": "workspace:*" *
* *
* 5. Edit the 'index.ts' file in 'agent/src': *
* *
* a. Import your plugin: *
* *
* import yourPlugin from '@elizaos/your-plugin-name'; *
* *
* b. Add your plugin to the `plugins` array: *
* *
* const plugins = [ *
* existingPlugin, *
* yourPlugin, *
* ]; *
* *
* 5. Edit the 'index.ts' file in 'agent/src': *
* a. Use the `await loadPlugin` function to dynamically import your plugin: *
* *
* await loadPlugin( *
* "YOUR_PLUGIN_SECRET_KEY", *
* () => import("path/to/your-plugin"), *
* "yourPluginName" *
* ) *
* *
* b. Add this call in the `plugins` array. *
* *
* This will ensure that your plugin's development server runs *
* alongside others when you execute this script. *
***********************************************************************
Expand Down
Loading