Skip to content

Commit 0fa8432

Browse files
committed
fix image handler
1 parent aaf162b commit 0fa8432

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

packages/worker/src/worker.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ export default {
124124
case 'message': {
125125
await bot.sendTyping();
126126
const prompt = bot.update.message?.text?.toString() ?? '';
127-
const { results } = await env.DB.prepare('SELECT * FROM Messages WHERE userId=?').bind(bot.update.message?.from.id).all();
127+
128+
const { results } = await env.DB.prepare('SELECT * FROM Messages WHERE userId=?')
129+
.bind(bot.update.message?.from.id)
130+
.all();
128131
const messageHistory = results.map((col) => ({ role: 'system', content: col.content as string }));
129132

130133
const messages = [
@@ -134,6 +137,7 @@ export default {
134137
];
135138

136139
try {
140+
console.log('Processing text message:', prompt);
137141
// @ts-expect-error broken bindings
138142
const response = await env.AI.run(AI_MODELS.LLAMA, { messages });
139143

@@ -162,6 +166,59 @@ export default {
162166
break;
163167
}
164168

169+
case 'photo': {
170+
await bot.sendTyping();
171+
const photo = bot.update.message?.photo;
172+
const fileId: string = photo ? photo[photo.length - 1]?.file_id ?? '' : '';
173+
const prompt = bot.update.message?.caption ?? 'Please describe this image';
174+
175+
console.log('Processing photo:', { fileId, prompt });
176+
177+
const { results } = await env.DB.prepare('SELECT * FROM Messages WHERE userId=?')
178+
.bind(bot.update.message?.from.id)
179+
.all();
180+
const messageHistory = results.map((col) => ({ role: 'system', content: col.content as string }));
181+
182+
const messages = [
183+
{ role: 'system', content: SYSTEM_PROMPTS.TUX_ROBOT },
184+
...messageHistory,
185+
{ role: 'user', content: prompt },
186+
];
187+
188+
try {
189+
const fileResponse = await bot.getFile(fileId);
190+
const blob = await fileResponse.arrayBuffer();
191+
// @ts-expect-error broken bindings
192+
const response = await env.AI.run(AI_MODELS.LLAMA, {
193+
messages,
194+
image: [...new Uint8Array(blob)]
195+
});
196+
197+
if ('response' in response && response.response) {
198+
await bot.reply(
199+
await markdownToHtml(
200+
typeof response.response === 'string'
201+
? response.response
202+
: JSON.stringify(response.response)
203+
),
204+
'HTML'
205+
);
206+
207+
await env.DB.prepare('INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)')
208+
.bind(
209+
crypto.randomUUID(),
210+
bot.update.message?.from.id,
211+
`'[INST] ${prompt} [/INST] \n ${typeof response.response === 'string' ? response.response : JSON.stringify(response.response)}'`
212+
)
213+
.run();
214+
}
215+
} catch (e) {
216+
console.error('Error in photo handler:', e);
217+
await bot.reply(`Error processing image: ${e as string}`);
218+
}
219+
break;
220+
}
221+
165222
case 'inline': {
166223
const messages = [
167224
{ role: 'system', content: SYSTEM_PROMPTS.TUX_ROBOT },

0 commit comments

Comments
 (0)