in src/tiny_cats/index.ts [42:103]
async function generate(message: string) {
userInput.disabled = true;
chat.history.length = 0;
modelOutput.innerHTML = '';
slideshow.innerHTML = '';
error.innerHTML = '';
error.toggleAttribute('hidden', true);
try {
const userTurn = document.createElement('div') as HTMLDivElement;
userTurn.innerHTML = await marked.parse(message);
userTurn.className = 'user-turn';
modelOutput.append(userTurn);
userInput.value = '';
const result = await chat.sendMessageStream({
message: message + additionalInstructions,
});
let text = '';
let img = null;
for await (const chunk of result) {
for (const candidate of chunk.candidates) {
for (const part of candidate.content.parts ?? []) {
if (part.text) {
text += part.text;
} else {
try {
const data = part.inlineData;
if (data) {
img = document.createElement('img');
img.src = `data:image/png;base64,` + data.data;
} else {
console.log('no data', chunk);
}
} catch (e) {
console.log('no data', chunk);
}
}
if (text && img) {
await addSlide(text, img);
slideshow.removeAttribute('hidden');
text = '';
img = null;
}
}
}
}
if (img) {
await addSlide(text, img);
slideshow.removeAttribute('hidden');
text = '';
}
} catch (e) {
error.innerHTML = 'Something went wrong.';
error.removeAttribute('hidden');
}
userInput.disabled = false;
userInput.focus();
}