Skip to content

Commit db17a4d

Browse files
committed
fix: toBase64() encoder which only accepts string
1 parent 77b6e7d commit db17a4d

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/additionalContextProvider.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,22 @@ export class AdditionalContextProvider {
600600
continue
601601
}
602602
if ('content' in context && context.content) {
603-
imageBlocks.push({
604-
format: format as ImageFormat,
605-
source: {
606-
bytes: new Uint8Array(Object.values(context.content)),
607-
},
608-
})
609-
continue
603+
// Validate content has actual byte data before creating Uint8Array
604+
// After JSON deserialization, Uint8Array becomes plain object with numeric keys
605+
const values = Object.values(context.content)
606+
if (values.length > 0 && values.every((v): v is number => typeof v === 'number')) {
607+
imageBlocks.push({
608+
format: format as ImageFormat,
609+
source: {
610+
bytes: new Uint8Array(values),
611+
},
612+
})
613+
continue
614+
}
615+
// Invalid content data, fall through to read from file
616+
this.features.logging.warn(
617+
`Invalid image content data for ${imagePath}, attempting to read from file`
618+
)
610619
}
611620
const fileContent = await this.features.workspace.fs.readFile(imagePath, {
612621
encoding: 'binary',

server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/chatDb/util.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,34 @@ export function messageToStreamingMessage(msg: Message): StreamingMessage {
175175
userIntent: msg.userIntent,
176176
origin: msg.origin || 'IDE',
177177
userInputMessageContext: msg.userInputMessageContext,
178-
images: msg.images || [],
178+
images: restoreImageBytes(msg.images || []),
179179
},
180180
}
181181
}
182182

183+
/**
184+
* Restores image bytes to proper Uint8Array after deserialization from JSON.
185+
*
186+
* When images are persisted via LokiJS (JSON serialization), Uint8Array bytes
187+
* are serialized as plain objects with numeric keys (e.g., {"0": 137, "1": 80, ...}).
188+
* On reload, these need to be converted back to Uint8Array for the AWS SDK's
189+
* toBase64() encoder which only accepts string | Uint8Array.
190+
*/
191+
function restoreImageBytes(images: ImageBlock[]): ImageBlock[] {
192+
return images.map(image => {
193+
if (image.source?.bytes && !(image.source.bytes instanceof Uint8Array)) {
194+
return {
195+
...image,
196+
source: {
197+
...image.source,
198+
bytes: new Uint8Array(Object.values(image.source.bytes)),
199+
},
200+
}
201+
}
202+
return image
203+
})
204+
}
205+
183206
/**
184207
* Converts Message to LSP Protocol ChatMessage
185208
*/

0 commit comments

Comments
 (0)