Skip to content

Commit a411ab8

Browse files
committed
feat(chat): send attachments to gemini and move package to peerDeps
1 parent af1a508 commit a411ab8

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

package-lock.json

Lines changed: 25 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
},
5252
"dependencies": {
5353
"@floating-ui/dom": "^1.7.0",
54-
"@google/genai": "^1.3.0",
5554
"@lit-labs/virtualizer": "^2.1.0",
5655
"@lit/context": "^1.1.5",
5756
"lit": "^3.3.0"
@@ -104,7 +103,8 @@
104103
},
105104
"peerDependencies": {
106105
"@supabase/supabase-js": "^2.49.4",
107-
"marked": "^12.0.0"
106+
"marked": "^12.0.0",
107+
"@google/genai": "^1.3.0"
108108
},
109109
"browserslist": [
110110
"defaults"

stories/chat.stories.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,23 @@ function generateAIResponse(message: string): string {
437437
return 'How can I help? Possible commands: hello, help, feature, weather, thank, code, image, list, heading.';
438438
}
439439

440+
function fileToGenerativePart(buffer, mimeType) {
441+
// Convert ArrayBuffer to base64 string in the browser
442+
let binary = '';
443+
const bytes = new Uint8Array(buffer);
444+
for (let i = 0; i < bytes.byteLength; i++) {
445+
binary += String.fromCharCode(bytes[i]);
446+
}
447+
const base64String = btoa(binary);
448+
449+
return {
450+
inlineData: {
451+
data: base64String,
452+
mimeType,
453+
},
454+
};
455+
}
456+
440457
async function handleAIMessageSend(e: CustomEvent) {
441458
const newMessage: IgcMessage = e.detail;
442459
const chat = document.querySelector('igc-chat');
@@ -456,6 +473,18 @@ async function handleAIMessageSend(e: CustomEvent) {
456473

457474
userMessages.push({ role: 'user', parts: [{ text: newMessage.text }] });
458475

476+
if (newMessage.attachments && newMessage.attachments.length > 0) {
477+
for (const attachment of newMessage.attachments) {
478+
if (attachment.file) {
479+
const filePart = fileToGenerativePart(
480+
await attachment.file.arrayBuffer(),
481+
attachment.file.type
482+
);
483+
userMessages.push({ role: 'user', parts: [filePart] });
484+
}
485+
}
486+
}
487+
459488
if (newMessage.text.includes('image')) {
460489
response = await ai.models.generateContent({
461490
model: 'gemini-2.0-flash-preview-image-generation',

0 commit comments

Comments
 (0)