Skip to content

Commit 7e96753

Browse files
committed
feat: create example folder with example plugin
1 parent 9d1a131 commit 7e96753

14 files changed

+503
-39
lines changed

packages/_examples/plugin/.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*
2+
3+
!dist/**
4+
!package.json
5+
!readme.md
6+
!tsup.config.ts

packages/_examples/plugin/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Sample Plugin for Eliza
2+
3+
The Sample Plugin for Eliza extends the functionality of the Eliza platform by providing additional actions, providers, evaluators, and more. This plugin is designed to be easily extendable and customizable to fit various use cases.
4+
5+
## Description
6+
The Sample Plugin offers a set of features that can be integrated into the Eliza platform to enhance its capabilities. Below is a high-level overview of the different components available in this plugin.
7+
8+
## Actions
9+
- **createResourceAction**: This action enables the creation and management of generic resources. It can be customized to handle different types of resources and integrate with various data sources.
10+
11+
## Providers
12+
- **sampleProvider**: This provider offers a mechanism to supply data or services to the plugin. It can be extended to include additional providers as needed.
13+
14+
## Evaluators
15+
- **sampleEvaluator**: This evaluator provides a way to assess or analyze data within the plugin. It can be extended to include additional evaluators as needed.
16+
17+
## Services
18+
- **[ServiceName]**: Description of the service and its functionality. This can be extended to include additional services as needed.
19+
20+
## Clients
21+
- **[ClientName]**: Description of the client and its functionality. This can be extended to include additional clients as needed.
22+
23+
## How to Extend
24+
To extend the Sample Plugin, you can add new actions, providers, evaluators, services, and clients by following the structure provided in the plugin. Each component can be customized to fit your specific requirements.
25+
26+
1. **Actions**: Add new actions by defining them in the `actions` array.
27+
2. **Providers**: Add new providers by defining them in the `providers` array.
28+
3. **Evaluators**: Add new evaluators by defining them in the `evaluators` array.
29+
4. **Services**: Add new services by defining them in the `services` array.
30+
5. **Clients**: Add new clients by defining them in the `clients` array.
31+
32+
For more detailed information on how to extend the plugin, refer to the documentation provided in the Eliza platform.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import eslintGlobalConfig from "../../eslint.config.mjs";
2+
3+
export default [...eslintGlobalConfig];
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@ai16z/plugin-sample",
3+
"version": "0.1.5-alpha.5",
4+
"main": "dist/index.js",
5+
"type": "module",
6+
"types": "dist/index.d.ts",
7+
"dependencies": {
8+
"@ai16z/eliza": "workspace:*"
9+
},
10+
"devDependencies": {
11+
"tsup": "8.3.5",
12+
"@types/node": "^20.0.0"
13+
},
14+
"scripts": {
15+
"build": "tsup --format esm --dts",
16+
"dev": "tsup --format esm --dts --watch",
17+
"lint": "eslint . --fix"
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import {
2+
Action,
3+
IAgentRuntime,
4+
Memory,
5+
HandlerCallback,
6+
State,
7+
composeContext,
8+
generateObjectV2,
9+
ModelClass,
10+
elizaLogger,
11+
} from "@ai16z/eliza";
12+
13+
import {
14+
CreateResourceSchema,
15+
isCreateResourceContent,
16+
} from "../types";
17+
18+
import { createResourceTemplate } from "../templates";
19+
20+
export const createResourceAction: Action = {
21+
name: "CREATE_RESOURCE",
22+
description: "Create a new resource with the specified details",
23+
validate: async (runtime: IAgentRuntime, _message: Memory) => {
24+
return !!(runtime.character.settings.secrets?.API_KEY);
25+
},
26+
handler: async (
27+
runtime: IAgentRuntime,
28+
_message: Memory,
29+
state: State,
30+
_options: any,
31+
callback: HandlerCallback
32+
) => {
33+
try {
34+
const context = composeContext({
35+
state,
36+
template: createResourceTemplate,
37+
});
38+
39+
const resourceDetails = await generateObjectV2({
40+
runtime,
41+
context,
42+
modelClass: ModelClass.SMALL,
43+
schema: CreateResourceSchema,
44+
});
45+
46+
if (!isCreateResourceContent(resourceDetails.object)) {
47+
callback(
48+
{ text: "Invalid resource details provided." },
49+
[]
50+
);
51+
return;
52+
}
53+
54+
// persist relevant data if needed to memory/knowledge
55+
// const memory = {
56+
// type: "resource",
57+
// content: resourceDetails.object,
58+
// timestamp: new Date().toISOString()
59+
// };
60+
61+
// await runtime.storeMemory(memory);
62+
63+
callback(
64+
{
65+
text: `Resource created successfully:
66+
- Name: ${resourceDetails.object.name}
67+
- Type: ${resourceDetails.object.type}
68+
- Description: ${resourceDetails.object.description}
69+
- Tags: ${resourceDetails.object.tags.join(", ")}
70+
71+
Resource has been stored in memory.`
72+
},
73+
[]
74+
);
75+
} catch (error) {
76+
elizaLogger.error("Error creating resource:", error);
77+
callback(
78+
{ text: "Failed to create resource. Please check the logs." },
79+
[]
80+
);
81+
}
82+
},
83+
examples: [
84+
[
85+
{
86+
user: "{{user1}}",
87+
content: {
88+
text: "Create a new resource with the name 'Resource1' and type 'TypeA'",
89+
},
90+
},
91+
{
92+
user: "{{agentName}}",
93+
content: {
94+
text: `Resource created successfully:
95+
- Name: Resource1
96+
- Type: TypeA`,
97+
},
98+
},
99+
],
100+
[
101+
{
102+
user: "{{user1}}",
103+
content: {
104+
text: "Create a new resource with the name 'Resource2' and type 'TypeB'",
105+
},
106+
},
107+
{
108+
user: "{{agentName}}",
109+
content: {
110+
text: `Resource created successfully:
111+
- Name: Resource2
112+
- Type: TypeB`,
113+
},
114+
},
115+
],
116+
],
117+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Evaluator, IAgentRuntime, Memory, State, elizaLogger } from "@ai16z/eliza";
2+
3+
export const sampleEvaluator: Evaluator = {
4+
alwaysRun: false,
5+
description: "Sample evaluator for checking important content in memory",
6+
similes: ["content checker", "memory evaluator"],
7+
examples: [
8+
{
9+
context: "Checking if memory contains important content",
10+
messages: [
11+
{
12+
action: "evaluate",
13+
input: "This is an important message",
14+
output: {
15+
score: 1,
16+
reason: "Memory contains important content."
17+
}
18+
}
19+
],
20+
outcome: "Memory should be evaluated as important"
21+
}
22+
],
23+
handler: async (runtime: IAgentRuntime, memory: Memory, state: State) => {
24+
// Evaluation logic for the evaluator
25+
elizaLogger.log("Evaluating data in sampleEvaluator...");
26+
27+
// Example evaluation logic
28+
if (memory.content && memory.content.includes("important")) {
29+
elizaLogger.log("Important content found in memory.");
30+
return {
31+
score: 1,
32+
reason: "Memory contains important content."
33+
};
34+
} else {
35+
elizaLogger.log("No important content found in memory.");
36+
return {
37+
score: 0,
38+
reason: "Memory does not contain important content."
39+
};
40+
}
41+
},
42+
name: "sampleEvaluator",
43+
validate: async (runtime: IAgentRuntime, memory: Memory, state: State) => {
44+
// Validation logic for the evaluator
45+
return true;
46+
}
47+
};
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { samplePlugin } from './plugins/samplePlugin';
2+
3+
4+
5+
export * from './plugins/samplePlugin';
6+
7+
8+
export default samplePlugin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {
2+
Plugin,
3+
} from "@ai16z/eliza";
4+
import { createResourceAction } from "../actions/sampleAction";
5+
import { sampleProvider } from "../providers/sampleProvider";
6+
import { sampleEvaluator } from "../evaluators/sampleEvalutor";
7+
8+
export const samplePlugin: Plugin = {
9+
name: "sample",
10+
description: "Enables creation and management of generic resources",
11+
actions: [createResourceAction],
12+
providers: [sampleProvider],
13+
evaluators: [sampleEvaluator],
14+
// separate examples will be added for services and clients
15+
services: [],
16+
clients: [],
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {
2+
Provider,
3+
IAgentRuntime,
4+
Memory,
5+
State,
6+
elizaLogger
7+
} from "@ai16z/eliza";
8+
9+
export const sampleProvider: Provider = {
10+
get: async (runtime: IAgentRuntime, message: Memory, state: State) => {
11+
// Data retrieval logic for the provider
12+
elizaLogger.log("Retrieving data in sampleProvider...");
13+
},
14+
};
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export const createResourceTemplate = `
2+
Extract the following details to create a new resource:
3+
- **name** (string): Name of the resource
4+
- **type** (string): Type of resource (document, image, video)
5+
- **description** (string): Description of the resource
6+
- **tags** (array): Array of tags to categorize the resource
7+
8+
Provide the values in the following JSON format:
9+
10+
\`\`\`json
11+
{
12+
"name": "<resource_name>",
13+
"type": "<resource_type>",
14+
"description": "<resource_description>",
15+
"tags": ["<tag1>", "<tag2>"]
16+
}
17+
\`\`\`
18+
19+
Here are the recent user messages for context:
20+
{{recentMessages}}
21+
`;
22+
23+
export const readResourceTemplate = `
24+
Extract the following details to read a resource:
25+
- **id** (string): Unique identifier of the resource
26+
- **fields** (array): Specific fields to retrieve (optional)
27+
28+
Provide the values in the following JSON format:
29+
30+
\`\`\`json
31+
{
32+
"id": "<resource_id>",
33+
"fields": ["<field1>", "<field2>"]
34+
}
35+
\`\`\`
36+
37+
Here are the recent user messages for context:
38+
{{recentMessages}}
39+
`;
40+
41+
export const updateResourceTemplate = `
42+
Extract the following details to update a resource:
43+
- **id** (string): Unique identifier of the resource
44+
- **updates** (object): Key-value pairs of fields to update
45+
46+
Provide the values in the following JSON format:
47+
48+
\`\`\`json
49+
{
50+
"id": "<resource_id>",
51+
"updates": {
52+
"<field1>": "<new_value1>",
53+
"<field2>": "<new_value2>"
54+
}
55+
}
56+
\`\`\`
57+
58+
Here are the recent user messages for context:
59+
{{recentMessages}}
60+
`;
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { z } from "zod";
2+
3+
// Base resource schema
4+
export const ResourceSchema = z.object({
5+
id: z.string().optional(),
6+
name: z.string().min(1),
7+
type: z.enum(["document", "image", "video"]),
8+
description: z.string(),
9+
tags: z.array(z.string())
10+
});
11+
12+
// Create resource schema
13+
export const CreateResourceSchema = ResourceSchema.omit({ id: true });
14+
15+
// Read resource schema
16+
export const ReadResourceSchema = z.object({
17+
id: z.string(),
18+
fields: z.array(z.string()).optional()
19+
});
20+
21+
// Update resource schema
22+
export const UpdateResourceSchema = z.object({
23+
id: z.string(),
24+
updates: z.record(z.string(), z.any())
25+
});
26+
27+
// Type definitions
28+
export type Resource = z.infer<typeof ResourceSchema>;
29+
export type CreateResourceContent = z.infer<typeof CreateResourceSchema>;
30+
export type ReadResourceContent = z.infer<typeof ReadResourceSchema>;
31+
export type UpdateResourceContent = z.infer<typeof UpdateResourceSchema>;
32+
33+
// Type guards
34+
export const isCreateResourceContent = (obj: any): obj is CreateResourceContent => {
35+
return CreateResourceSchema.safeParse(obj).success;
36+
};
37+
38+
export const isReadResourceContent = (obj: any): obj is ReadResourceContent => {
39+
return ReadResourceSchema.safeParse(obj).success;
40+
};
41+
42+
export const isUpdateResourceContent = (obj: any): obj is UpdateResourceContent => {
43+
return UpdateResourceSchema.safeParse(obj).success;
44+
};
45+
46+
// Plugin configuration type
47+
export interface ExamplePluginConfig {
48+
apiKey: string;
49+
apiSecret: string;
50+
endpoint?: string;
51+
}

0 commit comments

Comments
 (0)