Skip to content

Commit

Permalink
Merge pull request #2045 from asterai-io/main
Browse files Browse the repository at this point in the history
feat: Implement asterai plugin
  • Loading branch information
ag-wnl authored Jan 11, 2025
2 parents eedf278 + 75e8e5a commit bbaf626
Show file tree
Hide file tree
Showing 11 changed files with 449 additions and 12 deletions.
6 changes: 6 additions & 0 deletions packages/plugin-asterai/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
80 changes: 80 additions & 0 deletions packages/plugin-asterai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# @elizaos/plugin-asterai

A plugin for interacting with [asterai](https://asterai.io) plugins and agents.

## Description

This plugin provides functionality to allow Eliza agents to interact with
asterai plugins and agents.

This will expand your Eliza character's utility by giving it access to all
the functionality of asterai's ecosystem of marketplace and private plugins
and agents.

## Installation

```bash
pnpm install @elizaos/plugin-asterai
```

## Configuration

The plugin requires the following environment variables to be set:

```typescript
ASTERAI_AGENT_ID=
ASTERAI_PUBLIC_QUERY_KEY=
```

## Usage

### Basic Integration

```typescript
import { asteraiPlugin } from '@elizaos/plugin-asterai';
```

### Example Usage

The plugin supports natural language for interacting with the asterai agent
through your Eliza character.

For example, if your asterai agent can fetch weather data:

```typescript
"Hey Eliza, how's the weather in LA?"
```

Eliza will then query the asterai agent to fetch the information.

## Development Guide

### Setting Up Development Environment

1. Clone the repository
2. Install dependencies:

```bash
pnpm install
```

3. Build the plugin:

```bash
pnpm run build
```

4. Run tests:

```bash
pnpm run test
```

## Contributing

Contributions are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information.

## License

This plugin is part of the Eliza project. See the main project repository for license information.

3 changes: 3 additions & 0 deletions packages/plugin-asterai/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];
46 changes: 46 additions & 0 deletions packages/plugin-asterai/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@elizaos/plugin-asterai",
"version": "0.1.0",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"@elizaos/source": "./src/index.ts",
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"files": [
"dist"
],
"dependencies": {
"@asterai/client": "0.1.6",
"@elizaos/core": "workspace:*",
"bignumber.js": "9.1.2",
"bs58": "6.0.0",
"elliptic": "6.6.1",
"node-cache": "5.1.2",
"sha3": "2.1.4",
"uuid": "11.0.3",
"zod": "3.23.8"
},
"devDependencies": {
"@types/elliptic": "6.4.18",
"@types/uuid": "10.0.0",
"tsup": "8.3.5"
},
"scripts": {
"lines": "find . \\( -name '*.cdc' -o -name '*.ts' \\) -not -path '*/node_modules/*' -not -path '*/tests/*' -not -path '*/deps/*' -not -path '*/dist/*' -not -path '*/imports*' | xargs wc -l",
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
},
"peerDependencies": {
"whatwg-url": "7.1.0"
}
}
72 changes: 72 additions & 0 deletions packages/plugin-asterai/src/actions/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
elizaLogger,
type Action,
type ActionExample,
type HandlerCallback,
type IAgentRuntime,
type Memory,
type State,
} from "@elizaos/core";
import { validateAsteraiConfig } from "../environment";
import {getInitAsteraiClient} from "../index.ts";

export const queryAction = {
name: "QUERY_ASTERAI_AGENT",
similes: [
"MESSAGE_ASTERAI_AGENT",
"TALK_TO_ASTERAI_AGENT",
"SEND_MESSAGE_TO_ASTERAI_AGENT",
"COMMUNICATE_WITH_ASTERAI_AGENT",
],
description:
"Call this action to send a message to the asterai agent which " +
"has access to external plugins and functionality to answer " +
"the user you are assisting, to help perform a workflow task, etc.",
validate: async (runtime: IAgentRuntime, _message: Memory) => {
const config = await validateAsteraiConfig(runtime);
getInitAsteraiClient(
config.ASTERAI_AGENT_ID,
config.ASTERAI_PUBLIC_QUERY_KEY
);
return true;
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
_state: State,
_options: { [key: string]: unknown },
callback?: HandlerCallback
): Promise<boolean> => {
const config = await validateAsteraiConfig(runtime);
const asteraiClient = getInitAsteraiClient(
config.ASTERAI_AGENT_ID,
config.ASTERAI_PUBLIC_QUERY_KEY
);
elizaLogger.debug("called QUERY_ASTERAI_AGENT action with message:", message.content);
const response = await asteraiClient.query({
query: message.content.text
});
const textResponse = await response.text();
callback({
text: textResponse
});
return true;
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "How's the weather in LA?",
},
},
{
user: "{{user2}}",
content: {
text: "Let me check that for you, just a moment.",
action: "QUERY_ASTERAI_AGENT",
},
},
],
] as ActionExample[][],
} as Action;
39 changes: 39 additions & 0 deletions packages/plugin-asterai/src/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IAgentRuntime } from "@elizaos/core";
import { z } from "zod";

const envSchema = z.object({
ASTERAI_AGENT_ID: z
.string()
.min(1, "ASTERAI_AGENT_ID is required"),
ASTERAI_PUBLIC_QUERY_KEY: z
.string()
.min(1, "ASTERAI_PUBLIC_QUERY_KEY is required"),
});

export type AsteraiConfig = z.infer<typeof envSchema>;

export async function validateAsteraiConfig(
runtime: IAgentRuntime
): Promise<AsteraiConfig> {
try {
const config = {
ASTERAI_AGENT_ID:
runtime.getSetting("ASTERAI_AGENT_ID") ||
process.env.ASTERAI_AGENT_ID,
ASTERAI_PUBLIC_QUERY_KEY:
runtime.getSetting("ASTERAI_PUBLIC_QUERY_KEY") || process.env.ASTERAI_PUBLIC_QUERY_KEY,
};

return envSchema.parse(config);
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessages = error.errors
.map((err) => `${err.path.join(".")}: ${err.message}`)
.join("\n");
throw new Error(
`Asterai plugin configuration validation failed:\n${errorMessages}`
);
}
throw error;
}
}
33 changes: 33 additions & 0 deletions packages/plugin-asterai/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {asteraiProvider} from "./providers/asterai.provider.ts";
import type { Plugin } from "@elizaos/core";
import { queryAction } from "./actions/query";
import { AsteraiClient } from "@asterai/client";

export * from "./environment";
export * from "./providers/asterai.provider";

let asteraiClient: AsteraiClient | null = null;

export const getInitAsteraiClient = (
agentId: string,
publicQueryKey: string
): AsteraiClient => {
if (!asteraiClient) {
asteraiClient = new AsteraiClient({
appId: agentId,
queryKey: publicQueryKey,
})
}
return asteraiClient;
};

export const asteraiPlugin: Plugin = {
name: "asterai",
description: "asterai Plugin for Eliza",
providers: [asteraiProvider],
actions: [queryAction],
evaluators: [],
services: [],
};

export default asteraiPlugin;
63 changes: 63 additions & 0 deletions packages/plugin-asterai/src/providers/asterai.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
elizaLogger,
IAgentRuntime,
Memory,
Provider,
State, UUID,
} from "@elizaos/core";
import {validateAsteraiConfig} from "../environment.ts";
import {getInitAsteraiClient} from "../index.ts";

const asteraiProvider: Provider = {
get: async (
runtime: IAgentRuntime,
message: Memory,
_state?: State
): Promise<string | null> => {
const hasConfiguredEnv =
!!runtime.getSetting("ASTERAI_AGENT_ID") &&
!!runtime.getSetting("ASTERAI_PUBLIC_QUERY_KEY");
if (!hasConfiguredEnv) {
elizaLogger.error(
"ASTERAI_AGENT_ID or ASTERAI_PUBLIC_QUERY_KEY " +
"not configured, skipping provider"
);
return null;
}
const config = await validateAsteraiConfig(runtime);
const asteraiClient = getInitAsteraiClient(
config.ASTERAI_AGENT_ID,
config.ASTERAI_PUBLIC_QUERY_KEY
);
if (!asteraiClient) {
elizaLogger.error("asteraiClient is not initialised");
return null;
}
const agentId = runtime.getSetting("ASTERAI_AGENT_ID") as UUID;
let agentSummaryMemory = await runtime.knowledgeManager.getMemoryById(agentId);
if (!agentSummaryMemory) {
// Fetch & set summary memory.
const summary = await asteraiClient.fetchSummary();
elizaLogger.debug("asterai agent summary fetched:", summary);
await runtime.knowledgeManager.createMemory({
id: agentId,
userId: message.userId,
agentId: message.agentId,
roomId: message.roomId,
createdAt: Date.now(),
content: {
text: summary
}
});
agentSummaryMemory = await runtime.knowledgeManager.getMemoryById(agentId);
}
if (!agentSummaryMemory) {
elizaLogger.error("failed to initialise agent's summary memory");
return null;
}
return agentSummaryMemory.content.text;
},
};

// Module exports
export { asteraiProvider };
10 changes: 10 additions & 0 deletions packages/plugin-asterai/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../core/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": [
"src/**/*.ts"
]
}
35 changes: 35 additions & 0 deletions packages/plugin-asterai/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
outDir: "dist",
sourcemap: true,
clean: true,
format: ["esm"], // Ensure you're targeting CommonJS
loader: {
".cdc": "text",
},
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",
"safe-buffer",
"base-x",
"bs58",
"borsh",
"stream",
"buffer",
"querystring",
"amqplib",
// Add other modules you want to externalize
"@onflow/fcl",
"@onflow/types",
"sha3",
"elliptic",
],
});
Loading

0 comments on commit bbaf626

Please sign in to comment.