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

feat: Code In Plugin, load characters from blockchain for with PDA #2107

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@elizaos/plugin-tee": "workspace:*",
"@elizaos/plugin-tee-log": "workspace:*",
"@elizaos/plugin-tee-marlin": "workspace:*",
"@elizaos/plugin-iq6900": "workspace:*",
"@elizaos/plugin-multiversx": "workspace:*",
"@elizaos/plugin-near": "workspace:*",
"@elizaos/plugin-zksync-era": "workspace:*",
Expand Down
117 changes: 109 additions & 8 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { TwitterClientInterface } from "@elizaos/client-twitter";
// import { ReclaimAdapter } from "@elizaos/plugin-reclaim";
import { PrimusAdapter } from "@elizaos/plugin-primus";


import {
AgentRuntime,
CacheManager,
Expand Down Expand Up @@ -98,10 +99,10 @@ import { thirdwebPlugin } from "@elizaos/plugin-thirdweb";
import { tonPlugin } from "@elizaos/plugin-ton";
import { squidRouterPlugin } from "@elizaos/plugin-squid-router";
import { webSearchPlugin } from "@elizaos/plugin-web-search";
import { echoChambersPlugin } from "@elizaos/plugin-echochambers";
import { dexScreenerPlugin } from "@elizaos/plugin-dexscreener";

import { zksyncEraPlugin } from "@elizaos/plugin-zksync-era";
import { elizaCodeinPlugin, onchainJson } from "@elizaos/plugin-iq6900";


import Database from "better-sqlite3";
import fs from "fs";
import net from "net";
Expand Down Expand Up @@ -155,6 +156,10 @@ function tryLoadFile(filePath: string): string | null {
return null;
}
}

function isAllStrings(arr: unknown[]): boolean {
return Array.isArray(arr) && arr.every((item) => typeof item === "string");
zo-eth marked this conversation as resolved.
Show resolved Hide resolved
}
function mergeCharacters(base: Character, child: Character): Character {
const mergeObjects = (baseObj: any, childObj: any) => {
const result: any = {};
Expand Down Expand Up @@ -241,7 +246,95 @@ async function loadCharacter(filePath: string): Promise<Character> {
throw new Error(`Character file not found: ${filePath}`);
}
let character = JSON.parse(content);
return jsonToCharacter(filePath, character);
validateCharacterConfig(character);
// .id isn't really valid
const characterId = character.id || character.name;
const characterPrefix = `CHARACTER.${characterId.toUpperCase().replace(/ /g, "_")}.`;
const characterSettings = Object.entries(process.env)
.filter(([key]) => key.startsWith(characterPrefix))
.reduce((settings, [key, value]) => {
const settingKey = key.slice(characterPrefix.length);
return { ...settings, [settingKey]: value };
}, {});
if (Object.keys(characterSettings).length > 0) {
character.settings = character.settings || {};
character.settings.secrets = {
...characterSettings,
...character.settings.secrets,
};
}
// Handle plugins
character.plugins = await handlePluginImporting(character.plugins);
if (character.extends) {
elizaLogger.info(
`Merging ${character.name} character with parent characters`
);
for (const extendPath of character.extends) {
const baseCharacter = await loadCharacter(
path.resolve(path.dirname(filePath), extendPath)
);
character = mergeCharacters(baseCharacter, character);
elizaLogger.info(
`Merged ${character.name} with ${baseCharacter.name}`
);
}
}
return character;
}

export async function loadCharacterFromOnchain(): Promise<Character[]> {
const jsonText = onchainJson;

console.log('JSON:', jsonText);
if (jsonText == "null") return;
const loadedCharacters = [];
try {

const character = JSON.parse(jsonText);
validateCharacterConfig(character);

// .id isn't really valid
const characterId = character.id || character.name;
const characterPrefix = `CHARACTER.${characterId.toUpperCase().replace(/ /g, "_")}.`;

const characterSettings = Object.entries(process.env)
.filter(([key]) => key.startsWith(characterPrefix))
.reduce((settings, [key, value]) => {
const settingKey = key.slice(characterPrefix.length);
return { ...settings, [settingKey]: value };
}, {});

if (Object.keys(characterSettings).length > 0) {
character.settings = character.settings || {};
character.settings.secrets = {
...characterSettings,
...character.settings.secrets,
};
}

// Handle plugins
if (isAllStrings(character.plugins)) {
elizaLogger.info("Plugins are: ", character.plugins);
const importedPlugins = await Promise.all(
character.plugins.map(async (plugin) => {
const importedPlugin = await import(plugin);
return importedPlugin.default;
})
);
character.plugins = importedPlugins;
}

loadedCharacters.push(character);
elizaLogger.info(
`Successfully loaded character from: ${process.env.IQ_WALLET_ADDRESS}`
);
return loadedCharacters;
} catch (e) {
elizaLogger.error(
`Error parsing character from ${process.env.IQ_WALLET_ADDRESS}: ${e}`
);
process.exit(1);
}
}

export async function loadCharacters(
Expand Down Expand Up @@ -763,6 +856,10 @@ export async function createAgent(
character,
// character.plugins are handled when clients are added
plugins: [
getSecret(character, "IQ_WALLET_ADDRESS")&&
getSecret(character, "IQSOlRPC")
? elizaCodeinPlugin
: null,
bootstrapPlugin,
getSecret(character, "DEXSCREENER_API_KEY")
? dexScreenerPlugin
Expand Down Expand Up @@ -794,6 +891,7 @@ export async function createAgent(
getSecret(character, "COSMOS_RECOVERY_PHRASE") &&
getSecret(character, "COSMOS_AVAILABLE_CHAINS") &&
createCosmosPlugin(),

(getSecret(character, "SOLANA_PUBLIC_KEY") ||
(getSecret(character, "WALLET_PUBLIC_KEY") &&
!getSecret(character, "WALLET_PUBLIC_KEY")?.startsWith(
Expand Down Expand Up @@ -892,10 +990,9 @@ export async function createAgent(
: null,
getSecret(character, "BIRDEYE_API_KEY") ? birdeyePlugin : null,
getSecret(character, "ECHOCHAMBERS_API_URL") &&
getSecret(character, "ECHOCHAMBERS_API_KEY")
? echoChambersPlugin
getSecret(character, "ECHOCHAMBERS_API_KEY")? echoChambersPlugin
: null,
getSecret(character, "LETZAI_API_KEY") ? letzAIPlugin : null,
getSecret(character, "LETZAI_API_KEY") ? letzAIPlugin : null,
getSecret(character, "STARGAZE_ENDPOINT") ? stargazePlugin : null,
getSecret(character, "GIPHY_API_KEY") ? giphyPlugin : null,
getSecret(character, "PASSPORT_API_KEY")
Expand Down Expand Up @@ -930,6 +1027,7 @@ export async function createAgent(
getSecret(character, "RESERVOIR_API_KEY")
? createNFTCollectionsPlugin()
: null,

].filter(Boolean),
providers: [],
actions: [],
Expand Down Expand Up @@ -1098,7 +1196,10 @@ const startAgents = async () => {
let charactersArg = args.characters || args.character;
let characters = [defaultCharacter];

if (charactersArg) {
if (process.env.IQ_WALLET_ADDRESS) {
characters = await loadCharacterFromOnchain();
}
if (onchainJson && charactersArg) {
characters = await loadCharacters(charactersArg);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-iq6900/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
28 changes: 28 additions & 0 deletions packages/plugin-iq6900/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Code In Plugin For Eliza

## Description
Through IQ6900's new inscription standard "Code-In", powerful inscription functionality is provided to Eliza.
Engrave Eliza on the blockchain forever.
All your Character JSON files are input onto the blockchain without compression.
Everyone can read your agent from blocks forever.

## inscription
- **Code-in your eliza**: Go to the site and engrave your character file on-chain. https://elizacodein.com/

## Onchain git
- **Commit your update**:
Update your files anytime.
On our site, your files are managed in a format similar to GitHub,
and our plugin automatically loads your latest agent file.

## Let's get started
- **Edit your .env**: write down IQ_WALLET_ADDRESS to your wallet address that you used on website.
To be sure, right after inscription, wait about 5 minutes and just type pmpn start. You are now all set.


You have engraved an eternal record.
Imagine, someone could develop your agent 200 years from now.

Many things will be updated.

Learn more: https://linktr.ee/IQ6900Docs
3 changes: 3 additions & 0 deletions packages/plugin-iq6900/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
21 changes: 21 additions & 0 deletions packages/plugin-iq6900/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@elizaos/plugin-iq6900",
"version": "0.1.5-alpha.5",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@elizaos/core": "workspace:*",
"@solana/web3.js": "^1.98.0"
},

"devDependencies": {
"tsup": "8.3.5",
"@types/node": "^20.0.0"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
}
}
Loading
Loading