Skip to content

Commit 71e58c1

Browse files
author
remote-swe-app[bot]
committed
fix: Use imageCache to store both image data and local path
1 parent 7559249 commit 71e58c1

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

worker/src/agent/common/messages.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -236,29 +236,37 @@ const saveImageToLocalFs = async (imageBuffer: Buffer): Promise<string> => {
236236

237237
const postProcessMessageContent = async (content: string) => {
238238
const contentArray = JSON.parse(content);
239-
const resultArray = [];
239+
const flattenedArray = [];
240240

241241
for (const c of contentArray) {
242242
if (!('image' in c)) {
243-
resultArray.push(c);
243+
flattenedArray.push(c);
244244
continue;
245245
}
246246

247247
// Process image
248248
const s3Key = c.image.source.s3Key;
249249
let imageBuffer: Buffer;
250+
let localPath: string;
250251

251252
if (s3Key in imageCache) {
252-
imageBuffer = imageCache[s3Key];
253+
// Use cached image data and path
254+
imageBuffer = imageCache[s3Key].data;
255+
localPath = imageCache[s3Key].localPath;
253256
} else {
254257
const file = await getBytesFromKey(s3Key);
255258
// Convert file to webp
256259
imageBuffer = await sharp(file).webp({ lossless: false, quality: 80 }).toBuffer();
257-
imageCache[s3Key] = imageBuffer;
260+
261+
// Save image to local filesystem
262+
localPath = await saveImageToLocalFs(imageBuffer);
263+
264+
// Cache both the image buffer and local path
265+
imageCache[s3Key] = { data: imageBuffer, localPath };
258266
}
259267

260268
// Add image to result
261-
resultArray.push({
269+
flattenedArray.push({
262270
image: {
263271
format: 'webp',
264272
source: {
@@ -267,14 +275,11 @@ const postProcessMessageContent = async (content: string) => {
267275
},
268276
});
269277

270-
// Save image to local filesystem
271-
const localPath = await saveImageToLocalFs(imageBuffer);
272-
273278
// Add a text block after the image with the path information
274-
resultArray.push({
279+
flattenedArray.push({
275280
text: `the image is stored locally on ${localPath}`,
276281
});
277282
}
278283

279-
return resultArray;
284+
return flattenedArray;
280285
};

0 commit comments

Comments
 (0)