@@ -9,9 +9,34 @@ import {
9
9
} from "@ai16z/eliza/src/types.ts" ;
10
10
import { generateCaption , generateImage } from "@ai16z/eliza/src/generation.ts" ;
11
11
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 ( / ^ d a t a : i m a g e \/ \w + ; b a s e 6 4 , / , '' ) ;
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
+
12
37
const imageGeneration : Action = {
13
38
name : "GENERATE_IMAGE" ,
14
- similes : [ "IMAGE_GENERATION" , "IMAGE_GEN" , "CREATE_IMAGE" , "MAKE_PICTURE" ] ,
39
+ similes : [ "IMAGE_GENERATION" , "IMAGE_GEN" , "CREATE_IMAGE" , "MAKE_PICTURE" , ] ,
15
40
description : "Generate an image to go along with the message." ,
16
41
validate : async ( runtime : IAgentRuntime , message : Memory ) => {
17
42
const anthropicApiKeyOk = ! ! runtime . getSetting ( "ANTHROPIC_API_KEY" ) ;
@@ -58,36 +83,62 @@ const imageGeneration: Action = {
58
83
) ;
59
84
for ( let i = 0 ; i < images . data . length ; i ++ ) {
60
85
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 ) ;
62
92
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(
64
108
{
65
109
imageUrl: image,
66
110
},
67
111
runtime
68
- ) ;
112
+ );*/
113
+
114
+ res . push ( { image : filepath , caption : "..." } ) ; //caption.title });
69
115
70
116
elizaLogger . log (
71
117
`Generated caption for image ${ i + 1 } :` ,
72
- caption . title
118
+ "..." // caption.title
73
119
) ;
74
- res . push ( { image : image , caption : caption . title } ) ;
120
+ // res.push({ image: image, caption: caption.title });
75
121
76
122
callback (
77
123
{
78
- text : caption . description ,
124
+ text : "..." , // caption.description,
79
125
attachments : [
80
126
{
81
127
id : crypto . randomUUID ( ) ,
82
- url : image ,
128
+ url : filepath ,
83
129
title : "Generated image" ,
84
130
source : "imageGeneration" ,
85
- description : caption . title ,
86
- text : caption . description ,
131
+ description : "..." , // caption.title,
132
+ text : "..." , // caption.description,
87
133
} ,
88
134
] ,
89
135
} ,
90
- [ ]
136
+ [
137
+ {
138
+ attachment : filepath ,
139
+ name : `${ filename } .png`
140
+ }
141
+ ]
91
142
) ;
92
143
}
93
144
} else {
0 commit comments