Skip to content

Commit

Permalink
created test actions for testing integration of two plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislawkurzypBD committed Jan 15, 2025
1 parent ec367bf commit 557ac89
Show file tree
Hide file tree
Showing 15 changed files with 6,210 additions and 2,189 deletions.
1 change: 1 addition & 0 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"@elizaos/plugin-akash": "workspace:*",
"@elizaos/plugin-quai": "workspace:*",
"@elizaos/plugin-nft-collections": "workspace:*",
"@elizaos/test-linkedin-plugin": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
Expand Down
3 changes: 2 additions & 1 deletion agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import {
import { coinmarketcapPlugin } from "@elizaos/plugin-coinmarketcap";
import { coingeckoPlugin } from "@elizaos/plugin-coingecko";
import { confluxPlugin } from "@elizaos/plugin-conflux";
import { createCosmosPlugin } from "@elizaos/plugin-cosmos";
import { cronosZkEVMPlugin } from "@elizaos/plugin-cronoszkevm";
import { echoChambersPlugin } from "@elizaos/plugin-echochambers";
import { evmPlugin } from "@elizaos/plugin-evm";
Expand Down Expand Up @@ -90,6 +89,7 @@ import { letzAIPlugin } from "@elizaos/plugin-letzai";
import { thirdwebPlugin } from "@elizaos/plugin-thirdweb";
import { hyperliquidPlugin } from "@elizaos/plugin-hyperliquid";
import { zksyncEraPlugin } from "@elizaos/plugin-zksync-era";
import { linkedinPost } from "@elizaos/test-linkedin-plugin";

import { OpacityAdapter } from "@elizaos/plugin-opacity";
import { openWeatherPlugin } from "@elizaos/plugin-open-weather";
Expand Down Expand Up @@ -886,6 +886,7 @@ export async function createAgent(
getSecret(character, "RESERVOIR_API_KEY")
? createNFTCollectionsPlugin()
: null,
getSecret(character, "FAL_API_KEY") ? linkedinPost : null,
].filter(Boolean),
providers: [],
actions: [],
Expand Down
1 change: 1 addition & 0 deletions characters/cosmosHelper.character.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "CosmosHelper",
"clients": [],
"modelProvider": "groq",
"imageModelProvider": "falai",
"settings": {
"voice": {
"model": "en_US-male-medium"
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-image-generation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ Ensure that your prompt is detailed, vivid, and incorporates all the elements me
);
//res.push({ image: image, caption: caption.title });

callback(
await callback(
{
text: "...", //caption.description,
text: `${filepath}`, //caption.description,
attachments: [
{
id: crypto.randomUUID(),
Expand Down
3 changes: 3 additions & 0 deletions packages/test-linkedin-plugin/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];
20 changes: 20 additions & 0 deletions packages/test-linkedin-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@elizaos/test-linkedin-plugin",
"version": "0.1.8+build.1",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"axios": "^1.7.9",
"tsup": "8.3.5",
"zod": "3.23.8",
"interchain": "^1.10.4",
"@elizaos/core": "workspace:*"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache .",
"test": "vitest run"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


export class createPostAction {
constructor() {

}

async execute(
postText: string,
imagePath: string,
): Promise<boolean> {
console.log(`Here shold be LinkedIn posting handled: ${postText}, with imagePath: ${imagePath}`);
return true;
}
}
114 changes: 114 additions & 0 deletions packages/test-linkedin-plugin/src/actions/create-post/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { createPostTemplate } from "../../template/template.ts";
import { createPostAction } from "./create-post-action.ts";
import {extractedContent} from "../../types/types.ts";
import {
composeContext,
generateObjectDeprecated,
HandlerCallback,
IAgentRuntime,
Memory,
ModelClass,
State
} from "@elizaos/core";

export const createPost = ({
name: "LINKEDIN_POST",
description: "Swaps tokens on cosmos chains",
handler: async (
_runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: { [key: string]: unknown },
_callback?: HandlerCallback
) => {
const postContext = composeContext({
state: state,
template: createPostTemplate, // to be aligned with logic
templatingEngine: "handlebars",
});

const createPostContent: extractedContent = await generateObjectDeprecated({
runtime: _runtime,
context: postContext,
modelClass: ModelClass.SMALL,
});

try {
const action = new createPostAction();

await action.execute(createPostContent.text, createPostContent.imagePath);

if (_callback) {
await _callback({
text: `Generation completed. Post has been published.`,
content: {
success:true,
},
});
}
return true;
} catch (error) {
console.error("Unhandled error:", error);

if (_callback) {
await _callback({
text: `Error generating image: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},
validate: async (runtime: IAgentRuntime) => {
return true;
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Generate post on LinkedIn about cats and dogs. It should contain appropriate image.",
action: "GENERATE_IMAGE",
},
},
{
user: "{{user2}}",
content: {
text: "Generated image for your post",
action: "GENERATE_IMAGE",
}
},
{
user: "{{user2}}",
content: {
text: "Generated text for your post and published it.",
action: "LINKEDIN_POST",
}
}
],
[
{
user: "{{user1}}",
content: {
text: "Create a post with image on LinkedIn about cats and dogs.",
action: "GENERATE_IMAGE",
},
},
{
user: "{{user2}}",
content: {
text: "Generated image for your post",
action: "GENERATE_IMAGE",
}
},
{
user: "{{user2}}",
content: {
text: "Generated text for your post and published it.",
action: "LINKEDIN_POST",
}
}
],
],
similes: ["CREATE_LINKEDIN_POST", "LINKEDIN_POST"],
});
82 changes: 82 additions & 0 deletions packages/test-linkedin-plugin/src/actions/test-action/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

import {
HandlerCallback,
IAgentRuntime,
Memory,
State
} from "@elizaos/core";

export const testAction = ({
name: "TEST_ACTION",
description: "Runs test action",
handler: async (
_runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: { [key: string]: unknown },
_callback?: HandlerCallback
) => {
try {
if (_callback) {
const filepath = `../agent/generatedImages/generated_1736938600821_0.png`;
const filename = `generated_1736938600821_0`;
await _callback(
{
text: `${filepath}`, //caption.description,
attachments: [
{
id: crypto.randomUUID(),
url: filepath,
title: "Generated image",
source: "imageGeneration",
description: "...", //caption.title,
text: "...", //caption.description,
contentType: "image/png",
},
],
},
[
{
attachment: filepath,
name: `${filename}.png`,
},
]
);
}
return true;
} catch (error) {
console.error("Unhandled error:", error);

if (_callback) {
await _callback({
text: `Error generating image: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},
validate: async (runtime: IAgentRuntime) => {
return true;
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Show me test action.",
action: "TEST_ACTION",
},
},
{
user: "{{user2}}",
content: {
text: "Here it is.",
action: "TEST_ACTION",
}
},

],
],
similes: ["TEST-ACTION"],
});
14 changes: 14 additions & 0 deletions packages/test-linkedin-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Plugin } from "@elizaos/core";
import { createPost } from "./actions/create-post";
import {testAction} from "./actions/test-action";

export const linkedinPost: Plugin = {
name: "Linkedin Plugin",
description: "Linkedin integration plugin",
providers: [],
evaluators: [],
services: [],
actions: [createPost, testAction],
};

export default linkedinPost;
15 changes: 15 additions & 0 deletions packages/test-linkedin-plugin/src/template/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const createPostTemplate = `
Given the recent messages, especially generated image, its path, and connected LinkedIn account:
{{recentMessages}}
{{linkedinAccount}}
Prepare text for the requested LinkedIn post. Be engaging, relevant, and professional.
Respond with a JSON markdown block containing only the extracted values:
\`\`\`json
{
"text": "Write your crafted LinkedIn post text here.", // example string
"imagePath": "https://image.tmdb.org/t/p/original/original.png", //example string with url to image
}
\`\`\`
`;

4 changes: 4 additions & 0 deletions packages/test-linkedin-plugin/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface extractedContent {
text: string;
imagePath: string;
}
15 changes: 15 additions & 0 deletions packages/test-linkedin-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../core/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "./src",
"typeRoots": [
"./node_modules/@types",
"./src/types"
],
"declaration": true
},
"include": [
"src"
]
}
20 changes: 20 additions & 0 deletions packages/test-linkedin-plugin/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
outDir: "dist",
sourcemap: true,
clean: true,
format: ["esm"], // Ensure you're targeting CommonJS
external: [
"dotenv", // Externalize dotenv to prevent bundling
"fs", // Externalize fs to use Node.js built-in module
"path", // Externalize other built-ins if necessary
"@reflink/reflink",
"@node-llama-cpp",
"https",
"http",
"agentkeepalive",
"zod",
],
});
Loading

0 comments on commit 557ac89

Please sign in to comment.