Skip to content

Commit 44c6d72

Browse files
committed
fix(cli): use atomic setState for pending image updates
- Fix race condition in addPendingImageFromFile by using functional setState - Improve validateAndAddImage to return actual error messages
1 parent 07af04b commit 44c6d72

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

cli/src/utils/add-pending-image.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,30 @@ export async function addPendingImageFromFile(
2626
const result = await processImageFile(imagePath, cwd)
2727

2828
// Update the pending image with processed data
29-
const store = useChatStore.getState()
30-
const pendingImages = store.pendingImages
31-
const updatedImages = pendingImages.map((img) => {
32-
if (img.path !== imagePath) return img
29+
useChatStore.setState((state) => ({
30+
pendingImages: state.pendingImages.map((img) => {
31+
if (img.path !== imagePath) return img
3332

34-
if (result.success && result.imagePart) {
35-
return {
36-
...img,
37-
size: result.imagePart.size,
38-
width: result.imagePart.width,
39-
height: result.imagePart.height,
40-
note: result.wasCompressed ? 'compressed' : undefined,
41-
processedImage: {
42-
base64: result.imagePart.image,
43-
mediaType: result.imagePart.mediaType,
44-
},
33+
if (result.success && result.imagePart) {
34+
return {
35+
...img,
36+
size: result.imagePart.size,
37+
width: result.imagePart.width,
38+
height: result.imagePart.height,
39+
note: result.wasCompressed ? 'compressed' : undefined,
40+
processedImage: {
41+
base64: result.imagePart.image,
42+
mediaType: result.imagePart.mediaType,
43+
},
44+
}
4545
}
46-
} else {
46+
4747
return {
4848
...img,
4949
note: result.error || 'failed',
5050
}
51-
}
52-
})
53-
54-
useChatStore.setState({ pendingImages: updatedImages })
51+
}),
52+
}))
5553
}
5654

5755
/**
@@ -107,8 +105,8 @@ export function addPendingImageWithError(
107105

108106
/**
109107
* Validate and add an image from a file path.
110-
* Returns { success: true } if the image was added (for processing or with an error),
111-
* or { success: false, error } if the file doesn't exist.
108+
* Returns { success: true } if the image was added for processing,
109+
* or { success: false, error } if the file doesn't exist or isn't supported.
112110
*/
113111
export async function validateAndAddImage(
114112
imagePath: string,
@@ -118,15 +116,17 @@ export async function validateAndAddImage(
118116

119117
// Check if file exists
120118
if (!existsSync(resolvedPath)) {
121-
addPendingImageWithError(imagePath, '❌ file not found')
122-
return { success: true }
119+
const error = 'file not found'
120+
addPendingImageWithError(imagePath, `❌ ${error}`)
121+
return { success: false, error }
123122
}
124123

125124
// Check if it's a supported format
126125
if (!isImageFile(resolvedPath)) {
127126
const ext = path.extname(imagePath).toLowerCase()
128-
addPendingImageWithError(resolvedPath, `❌ unsupported format ${ext}`)
129-
return { success: true }
127+
const error = ext ? `unsupported format ${ext}` : 'unsupported format'
128+
addPendingImageWithError(resolvedPath, `❌ ${error}`)
129+
return { success: false, error }
130130
}
131131

132132
// Process and add the image

0 commit comments

Comments
 (0)