Skip to content

Commit f89a390

Browse files
committed
feat: asterai plugin
1 parent 30421fe commit f89a390

File tree

4 files changed

+96
-21
lines changed

4 files changed

+96
-21
lines changed

packages/plugin-asterai/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@
3232
"devDependencies": {
3333
"@types/elliptic": "6.4.18",
3434
"@types/uuid": "10.0.0",
35-
"tsup": "8.3.5",
36-
"vitest": "2.1.4"
35+
"tsup": "8.3.5"
3736
},
3837
"scripts": {
3938
"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",
4039
"build": "tsup --format esm --dts",
4140
"dev": "tsup --format esm --dts --watch",
42-
"lint": "eslint --fix --cache .",
43-
"test": "vitest run"
41+
"lint": "eslint --fix --cache ."
4442
},
4543
"peerDependencies": {
4644
"whatwg-url": "7.1.0"

packages/plugin-asterai/src/actions/query.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import {
88
type State,
99
} from "@elizaos/core";
1010
import { validateAsteraiConfig } from "../environment";
11-
import {AsteraiClient} from "@asterai/client";
12-
13-
let asteraiClient: AsteraiClient | null = null;
11+
import {getInitAsteraiClient} from "../index.ts";
1412

1513
export const queryAction = {
1614
name: "QUERY_ASTERAI_AGENT",
@@ -26,25 +24,24 @@ export const queryAction = {
2624
"the user you are assisting, to help perform a workflow task, etc.",
2725
validate: async (runtime: IAgentRuntime, _message: Memory) => {
2826
const config = await validateAsteraiConfig(runtime);
29-
if (!asteraiClient) {
30-
asteraiClient = new AsteraiClient({
31-
appId: config.ASTERAI_AGENT_ID,
32-
queryKey: config.ASTERAI_PUBLIC_QUERY_KEY,
33-
})
34-
}
27+
getInitAsteraiClient(
28+
config.ASTERAI_AGENT_ID,
29+
config.ASTERAI_PUBLIC_QUERY_KEY
30+
);
3531
return true;
3632
},
3733
handler: async (
3834
runtime: IAgentRuntime,
3935
message: Memory,
40-
state: State,
36+
_state: State,
4137
_options: { [key: string]: unknown },
4238
callback?: HandlerCallback
4339
): Promise<boolean> => {
44-
if (asteraiClient === null) {
45-
elizaLogger.warn("asterai client is not initialised; ignoring");
46-
return null;
47-
}
40+
const config = await validateAsteraiConfig(runtime);
41+
const asteraiClient = getInitAsteraiClient(
42+
config.ASTERAI_AGENT_ID,
43+
config.ASTERAI_PUBLIC_QUERY_KEY
44+
);
4845
elizaLogger.debug("called QUERY_ASTERAI_AGENT action with message:", message.content);
4946
const response = await asteraiClient.query({
5047
query: message.content.text

packages/plugin-asterai/src/index.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1+
import {asteraiProvider} from "./providers/asterai.provider.ts";
2+
import type { Plugin } from "@elizaos/core";
3+
import { queryAction } from "./actions/query";
4+
import { AsteraiClient } from "@asterai/client";
5+
16
export * from "./environment";
27
export * from "./providers/asterai.provider";
38

4-
import type { Plugin } from "@elizaos/core";
5-
import { queryAction } from "./actions/query";
9+
let asteraiClient: AsteraiClient | null = null;
10+
11+
export const getInitAsteraiClient = (
12+
agentId: string,
13+
publicQueryKey: string
14+
): AsteraiClient => {
15+
if (!asteraiClient) {
16+
asteraiClient = new AsteraiClient({
17+
appId: agentId,
18+
queryKey: publicQueryKey,
19+
})
20+
}
21+
return asteraiClient;
22+
};
623

724
export const asteraiPlugin: Plugin = {
825
name: "asterai",
926
description: "asterai Plugin for Eliza",
10-
providers: [/*todo*/],
27+
providers: [asteraiProvider],
1128
actions: [queryAction],
1229
evaluators: [],
1330
services: [],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
elizaLogger,
3+
IAgentRuntime,
4+
Memory,
5+
Provider,
6+
State, UUID,
7+
} from "@elizaos/core";
8+
import {validateAsteraiConfig} from "../environment.ts";
9+
import {getInitAsteraiClient} from "../index.ts";
10+
11+
const asteraiProvider: Provider = {
12+
get: async (
13+
runtime: IAgentRuntime,
14+
message: Memory,
15+
_state?: State
16+
): Promise<string | null> => {
17+
const hasConfiguredEnv =
18+
!!runtime.getSetting("ASTERAI_AGENT_ID") &&
19+
!!runtime.getSetting("ASTERAI_PUBLIC_QUERY_KEY");
20+
if (!hasConfiguredEnv) {
21+
elizaLogger.error(
22+
"ASTERAI_AGENT_ID or ASTERAI_PUBLIC_QUERY_KEY " +
23+
"not configured, skipping provider"
24+
);
25+
return null;
26+
}
27+
const config = await validateAsteraiConfig(runtime);
28+
const asteraiClient = getInitAsteraiClient(
29+
config.ASTERAI_AGENT_ID,
30+
config.ASTERAI_PUBLIC_QUERY_KEY
31+
);
32+
if (!asteraiClient) {
33+
elizaLogger.error("asteraiClient is not initialised");
34+
return null;
35+
}
36+
const agentId = runtime.getSetting("ASTERAI_AGENT_ID") as UUID;
37+
let agentSummaryMemory = await runtime.knowledgeManager.getMemoryById(agentId);
38+
if (!agentSummaryMemory) {
39+
// Fetch & set summary memory.
40+
const summary = await asteraiClient.fetchSummary();
41+
elizaLogger.debug("asterai agent summary fetched:", summary);
42+
await runtime.knowledgeManager.createMemory({
43+
id: agentId,
44+
userId: message.userId,
45+
agentId: message.agentId,
46+
roomId: message.roomId,
47+
createdAt: Date.now(),
48+
content: {
49+
text: summary
50+
}
51+
});
52+
agentSummaryMemory = await runtime.knowledgeManager.getMemoryById(agentId);
53+
}
54+
if (!agentSummaryMemory) {
55+
elizaLogger.error("failed to initialise agent's summary memory");
56+
return null;
57+
}
58+
return agentSummaryMemory.content.text;
59+
},
60+
};
61+
62+
// Module exports
63+
export { asteraiProvider };

0 commit comments

Comments
 (0)