forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleAction.ts
117 lines (108 loc) · 2.87 KB
/
sampleAction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {
Action,
IAgentRuntime,
Memory,
HandlerCallback,
State,
composeContext,
generateObjectV2,
ModelClass,
elizaLogger,
} from "@ai16z/eliza";
import {
CreateResourceSchema,
isCreateResourceContent,
} from "../types";
import { createResourceTemplate } from "../templates";
export const createResourceAction: Action = {
name: "CREATE_RESOURCE",
description: "Create a new resource with the specified details",
validate: async (runtime: IAgentRuntime, _message: Memory) => {
return !!(runtime.character.settings.secrets?.API_KEY);
},
handler: async (
runtime: IAgentRuntime,
_message: Memory,
state: State,
_options: any,
callback: HandlerCallback
) => {
try {
const context = composeContext({
state,
template: createResourceTemplate,
});
const resourceDetails = await generateObjectV2({
runtime,
context,
modelClass: ModelClass.SMALL,
schema: CreateResourceSchema,
});
if (!isCreateResourceContent(resourceDetails.object)) {
callback(
{ text: "Invalid resource details provided." },
[]
);
return;
}
// persist relevant data if needed to memory/knowledge
// const memory = {
// type: "resource",
// content: resourceDetails.object,
// timestamp: new Date().toISOString()
// };
// await runtime.storeMemory(memory);
callback(
{
text: `Resource created successfully:
- Name: ${resourceDetails.object.name}
- Type: ${resourceDetails.object.type}
- Description: ${resourceDetails.object.description}
- Tags: ${resourceDetails.object.tags.join(", ")}
Resource has been stored in memory.`
},
[]
);
} catch (error) {
elizaLogger.error("Error creating resource:", error);
callback(
{ text: "Failed to create resource. Please check the logs." },
[]
);
}
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Create a new resource with the name 'Resource1' and type 'TypeA'",
},
},
{
user: "{{agentName}}",
content: {
text: `Resource created successfully:
- Name: Resource1
- Type: TypeA`,
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Create a new resource with the name 'Resource2' and type 'TypeB'",
},
},
{
user: "{{agentName}}",
content: {
text: `Resource created successfully:
- Name: Resource2
- Type: TypeB`,
},
},
],
],
};