forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
270 lines (239 loc) · 8.58 KB
/
index.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { elizaLogger } from "@ai16z/eliza";
import {
Action,
HandlerCallback,
IAgentRuntime,
Memory,
Plugin,
State,
} from "@ai16z/eliza";
import { generateImage } from "@ai16z/eliza";
import fs from "fs";
import path from "path";
import { validateImageGenConfig } from "./enviroment";
export function saveBase64Image(base64Data: string, filename: string): string {
// Create generatedImages directory if it doesn't exist
const imageDir = path.join(process.cwd(), "generatedImages");
if (!fs.existsSync(imageDir)) {
fs.mkdirSync(imageDir, { recursive: true });
}
// Remove the data:image/png;base64 prefix if it exists
const base64Image = base64Data.replace(/^data:image\/\w+;base64,/, "");
// Create a buffer from the base64 string
const imageBuffer = Buffer.from(base64Image, "base64");
// Create full file path
const filepath = path.join(imageDir, `${filename}.png`);
// Save the file
fs.writeFileSync(filepath, imageBuffer);
return filepath;
}
export async function saveHeuristImage(
imageUrl: string,
filename: string
): Promise<string> {
const imageDir = path.join(process.cwd(), "generatedImages");
if (!fs.existsSync(imageDir)) {
fs.mkdirSync(imageDir, { recursive: true });
}
// Fetch image from URL
const response = await fetch(imageUrl);
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
const imageBuffer = Buffer.from(arrayBuffer);
// Create full file path
const filepath = path.join(imageDir, `${filename}.png`);
// Save the file
fs.writeFileSync(filepath, imageBuffer);
return filepath;
}
const imageGeneration: Action = {
name: "GENERATE_IMAGE",
similes: [
"IMAGE_GENERATION",
"IMAGE_GEN",
"CREATE_IMAGE",
"MAKE_PICTURE",
"GENERATE_IMAGE",
"GENERATE_A",
"DRAW",
"DRAW_A",
"MAKE_A",
],
description: "Generate an image to go along with the message.",
validate: async (runtime: IAgentRuntime, _message: Memory) => {
await validateImageGenConfig(runtime);
const anthropicApiKeyOk = !!runtime.getSetting("ANTHROPIC_API_KEY");
const togetherApiKeyOk = !!runtime.getSetting("TOGETHER_API_KEY");
const heuristApiKeyOk = !!runtime.getSetting("HEURIST_API_KEY");
const falApiKeyOk = !!runtime.getSetting("FAL_API_KEY");
const openAiApiKeyOk = !!runtime.getSetting("OPENAI_API_KEY");
return anthropicApiKeyOk || togetherApiKeyOk || heuristApiKeyOk || falApiKeyOk || openAiApiKeyOk;
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
options: any,
callback: HandlerCallback
) => {
elizaLogger.log("Composing state for message:", message);
state = (await runtime.composeState(message)) as State;
const userId = runtime.agentId;
elizaLogger.log("User ID:", userId);
const imagePrompt = message.content.text;
elizaLogger.log("Image prompt received:", imagePrompt);
// TODO: Generate a prompt for the image
const res: { image: string; caption: string }[] = [];
elizaLogger.log("Generating image with prompt:", imagePrompt);
const images = await generateImage(
{
prompt: imagePrompt,
width: 1024,
height: 1024,
count: 1,
},
runtime
);
if (images.success && images.data && images.data.length > 0) {
elizaLogger.log(
"Image generation successful, number of images:",
images.data.length
);
for (let i = 0; i < images.data.length; i++) {
const image = images.data[i];
// Save the image and get filepath
const filename = `generated_${Date.now()}_${i}`;
// Choose save function based on image data format
const filepath = image.startsWith("http")
? await saveHeuristImage(image, filename)
: saveBase64Image(image, filename);
elizaLogger.log(`Processing image ${i + 1}:`, filename);
//just dont even add a caption or a description just have it generate & send
/*
try {
const imageService = runtime.getService(ServiceType.IMAGE_DESCRIPTION);
if (imageService && typeof imageService.describeImage === 'function') {
const caption = await imageService.describeImage({ imageUrl: filepath });
captionText = caption.description;
captionTitle = caption.title;
}
} catch (error) {
elizaLogger.error("Caption generation failed, using default caption:", error);
}*/
const _caption = "...";
/*= await generateCaption(
{
imageUrl: image,
},
runtime
);*/
res.push({ image: filepath, caption: "..." }); //caption.title });
elizaLogger.log(
`Generated caption for image ${i + 1}:`,
"..." //caption.title
);
//res.push({ image: image, caption: caption.title });
callback(
{
text: "...", //caption.description,
attachments: [
{
id: crypto.randomUUID(),
url: filepath,
title: "Generated image",
source: "imageGeneration",
description: "...", //caption.title,
text: "...", //caption.description,
},
],
},
[
{
attachment: filepath,
name: `${filename}.png`,
},
]
);
}
} else {
elizaLogger.error("Image generation failed or returned no data.");
}
},
examples: [
// TODO: We want to generate images in more abstract ways, not just when asked to generate an image
[
{
user: "{{user1}}",
content: { text: "Generate an image of a cat" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a cat",
action: "GENERATE_IMAGE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Generate an image of a dog" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a dog",
action: "GENERATE_IMAGE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Create an image of a cat with a hat" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a cat with a hat",
action: "GENERATE_IMAGE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Make an image of a dog with a hat" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a dog with a hat",
action: "GENERATE_IMAGE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Paint an image of a cat with a hat" },
},
{
user: "{{agentName}}",
content: {
text: "Here's an image of a cat with a hat",
action: "GENERATE_IMAGE",
},
},
],
],
} as Action;
export const imageGenerationPlugin: Plugin = {
name: "imageGeneration",
description: "Generate images",
actions: [imageGeneration],
evaluators: [],
providers: [],
};