Skip to content

Commit 1b0d5fe

Browse files
committed
image gen saves file & returns format for clients; discord image poasting just works
1 parent 83152cc commit 1b0d5fe

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ X_SERVER_URL=
2929
XAI_API_KEY=
3030
XAI_MODEL=
3131

32+
#USE IMAGE GEN
33+
IMAGE_GEN= #TRUE
34+
3235
#Leave blank to use local embeddings
3336
USE_OPENAI_EMBEDDING= #TRUE
3437

packages/client-discord/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class DiscordClient extends EventEmitter {
7373
this.runtime.registerAction(transcribe_media);
7474
this.runtime.registerAction(download_media);
7575

76+
7677
this.runtime.providers.push(channelStateProvider);
7778
this.runtime.providers.push(voiceStateProvider);
7879
}

packages/plugin-image-generation/src/index.ts

+62-11
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,34 @@ import {
99
} from "@ai16z/eliza/src/types.ts";
1010
import { generateCaption, generateImage } from "@ai16z/eliza/src/generation.ts";
1111

12+
import fs from 'fs';
13+
import path from 'path';
14+
15+
export function saveBase64Image(base64Data: string, filename: string): string {
16+
// Create generatedImages directory if it doesn't exist
17+
const imageDir = path.join(process.cwd(), 'generatedImages');
18+
if (!fs.existsSync(imageDir)) {
19+
fs.mkdirSync(imageDir, { recursive: true });
20+
}
21+
22+
// Remove the data:image/png;base64 prefix if it exists
23+
const base64Image = base64Data.replace(/^data:image\/\w+;base64,/, '');
24+
25+
// Create a buffer from the base64 string
26+
const imageBuffer = Buffer.from(base64Image, 'base64');
27+
28+
// Create full file path
29+
const filepath = path.join(imageDir, `${filename}.png`);
30+
31+
// Save the file
32+
fs.writeFileSync(filepath, imageBuffer);
33+
34+
return filepath;
35+
}
36+
1237
const imageGeneration: Action = {
1338
name: "GENERATE_IMAGE",
14-
similes: ["IMAGE_GENERATION", "IMAGE_GEN", "CREATE_IMAGE", "MAKE_PICTURE"],
39+
similes: ["IMAGE_GENERATION", "IMAGE_GEN", "CREATE_IMAGE", "MAKE_PICTURE",],
1540
description: "Generate an image to go along with the message.",
1641
validate: async (runtime: IAgentRuntime, message: Memory) => {
1742
const anthropicApiKeyOk = !!runtime.getSetting("ANTHROPIC_API_KEY");
@@ -58,36 +83,62 @@ const imageGeneration: Action = {
5883
);
5984
for (let i = 0; i < images.data.length; i++) {
6085
const image = images.data[i];
61-
elizaLogger.log(`Processing image ${i + 1}:`, image);
86+
87+
const base64Image = images.data[i];
88+
// Save the image and get filepath
89+
const filename = `generated_${Date.now()}_${i}`;
90+
const filepath = saveBase64Image(base64Image, filename);
91+
elizaLogger.log(`Processing image ${i + 1}:`, filename);
6292

63-
const caption = await generateCaption(
93+
//just dont even add a caption or a description just have it generate & send
94+
/*
95+
try {
96+
const imageService = runtime.getService(ServiceType.IMAGE_DESCRIPTION);
97+
if (imageService && typeof imageService.describeImage === 'function') {
98+
const caption = await imageService.describeImage({ imageUrl: filepath });
99+
captionText = caption.description;
100+
captionTitle = caption.title;
101+
}
102+
} catch (error) {
103+
elizaLogger.error("Caption generation failed, using default caption:", error);
104+
}*/
105+
106+
const caption = "...";
107+
/*= await generateCaption(
64108
{
65109
imageUrl: image,
66110
},
67111
runtime
68-
);
112+
);*/
113+
114+
res.push({ image: filepath, caption: "..."});//caption.title });
69115

70116
elizaLogger.log(
71117
`Generated caption for image ${i + 1}:`,
72-
caption.title
118+
"..."//caption.title
73119
);
74-
res.push({ image: image, caption: caption.title });
120+
//res.push({ image: image, caption: caption.title });
75121

76122
callback(
77123
{
78-
text: caption.description,
124+
text: "...",//caption.description,
79125
attachments: [
80126
{
81127
id: crypto.randomUUID(),
82-
url: image,
128+
url: filepath,
83129
title: "Generated image",
84130
source: "imageGeneration",
85-
description: caption.title,
86-
text: caption.description,
131+
description: "...",//caption.title,
132+
text: "...",//caption.description,
87133
},
88134
],
89135
},
90-
[]
136+
[
137+
{
138+
attachment: filepath,
139+
name: `${filename}.png`
140+
}
141+
]
91142
);
92143
}
93144
} else {

0 commit comments

Comments
 (0)