fix(*): images do work

This commit is contained in:
h
2026-01-21 02:33:01 +01:00
parent 592aa5bc6b
commit ae9013536b
6 changed files with 85 additions and 113 deletions

View File

@@ -9,7 +9,6 @@
*/
import type * as chats from "../chats.js";
import type * as http from "../http.js";
import type * as messages from "../messages.js";
import type * as pendingGenerations from "../pendingGenerations.js";
import type * as users from "../users.js";
@@ -22,7 +21,6 @@ import type {
declare const fullApi: ApiFromModules<{
chats: typeof chats;
http: typeof http;
messages: typeof messages;
pendingGenerations: typeof pendingGenerations;
users: typeof users;

View File

@@ -43,7 +43,7 @@ export const clear = mutation({
.collect();
for (const message of messages) {
if (args.preserveImages && message.imageStorageId) {
if (args.preserveImages && message.imageBase64) {
continue;
}
await ctx.db.delete(message._id);

View File

@@ -1,40 +0,0 @@
import { httpRouter } from 'convex/server';
import { httpAction } from './_generated/server';
import { internal } from './_generated/api';
import type { Id } from './_generated/dataModel';
const http = httpRouter();
http.route({
path: '/upload-image',
method: 'POST',
handler: httpAction(async (ctx, req) => {
const chatId = req.headers.get('X-Chat-Id');
const mediaType = req.headers.get('Content-Type') || 'image/jpeg';
const caption = req.headers.get('X-Caption') || '';
if (!chatId) {
return new Response(JSON.stringify({ error: 'Missing X-Chat-Id header' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
const blob = await req.blob();
const storageId = await ctx.storage.store(blob);
await ctx.runMutation(internal.messages.createWithImage, {
chatId: chatId as Id<'chats'>,
content: caption,
imageStorageId: storageId,
imageMediaType: mediaType
});
return new Response(JSON.stringify({ storageId }), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
})
});
export default http;

View File

@@ -1,5 +1,5 @@
import { v } from 'convex/values';
import { internalMutation, mutation, query } from './_generated/server';
import { mutation, query } from './_generated/server';
export const listByChat = query({
args: { chatId: v.id('chats') },
@@ -10,7 +10,7 @@ export const listByChat = query({
chatId: v.id('chats'),
role: v.union(v.literal('user'), v.literal('assistant')),
content: v.string(),
imageStorageId: v.optional(v.id('_storage')),
imageBase64: v.optional(v.string()),
imageMediaType: v.optional(v.string()),
followUpOptions: v.optional(v.array(v.string())),
source: v.union(v.literal('telegram'), v.literal('web')),
@@ -33,7 +33,7 @@ export const create = mutation({
role: v.union(v.literal('user'), v.literal('assistant')),
content: v.string(),
source: v.union(v.literal('telegram'), v.literal('web')),
imageStorageId: v.optional(v.id('_storage')),
imageBase64: v.optional(v.string()),
imageMediaType: v.optional(v.string()),
followUpOptions: v.optional(v.array(v.string())),
isStreaming: v.optional(v.boolean())
@@ -45,7 +45,7 @@ export const create = mutation({
role: args.role,
content: args.content,
source: args.source,
imageStorageId: args.imageStorageId,
imageBase64: args.imageBase64,
imageMediaType: args.imageMediaType,
followUpOptions: args.followUpOptions,
createdAt: Date.now(),
@@ -132,7 +132,7 @@ export const getLastAssistantMessage = query({
chatId: v.id('chats'),
role: v.union(v.literal('user'), v.literal('assistant')),
content: v.string(),
imageStorageId: v.optional(v.id('_storage')),
imageBase64: v.optional(v.string()),
imageMediaType: v.optional(v.string()),
followUpOptions: v.optional(v.array(v.string())),
source: v.union(v.literal('telegram'), v.literal('web')),
@@ -152,21 +152,12 @@ export const getLastAssistantMessage = query({
}
});
export const generateUploadUrl = mutation({
args: {},
returns: v.string(),
handler: async (ctx) => {
return await ctx.storage.generateUploadUrl();
}
});
export const getImageUrls = query({
export const getChatImages = query({
args: { chatId: v.id('chats') },
returns: v.array(
v.object({
storageId: v.id('_storage'),
mediaType: v.string(),
url: v.union(v.string(), v.null())
base64: v.string(),
mediaType: v.string()
})
),
handler: async (ctx, args) => {
@@ -175,41 +166,11 @@ export const getImageUrls = query({
.withIndex('by_chat_id', (q) => q.eq('chatId', args.chatId))
.collect();
const imageMessages = messages.filter((m) => m.imageStorageId && m.imageMediaType);
const results = [];
for (const msg of imageMessages) {
if (msg.imageStorageId && msg.imageMediaType) {
const url = await ctx.storage.getUrl(msg.imageStorageId);
results.push({
storageId: msg.imageStorageId,
mediaType: msg.imageMediaType,
url
});
}
}
return results;
}
});
export const createWithImage = internalMutation({
args: {
chatId: v.id('chats'),
content: v.string(),
imageStorageId: v.id('_storage'),
imageMediaType: v.string()
},
returns: v.id('messages'),
handler: async (ctx, args) => {
return await ctx.db.insert('messages', {
chatId: args.chatId,
role: 'user' as const,
content: args.content,
source: 'telegram' as const,
imageStorageId: args.imageStorageId,
imageMediaType: args.imageMediaType,
createdAt: Date.now()
});
return messages
.filter((m) => m.imageBase64 && m.imageMediaType)
.map((m) => ({
base64: m.imageBase64!,
mediaType: m.imageMediaType!
}));
}
});

View File

@@ -23,8 +23,9 @@ export default defineSchema({
chatId: v.id('chats'),
role: v.union(v.literal('user'), v.literal('assistant')),
content: v.string(),
imageStorageId: v.optional(v.id('_storage')),
imageBase64: v.optional(v.string()),
imageMediaType: v.optional(v.string()),
imageStorageId: v.optional(v.id('_storage')),
followUpOptions: v.optional(v.array(v.string())),
source: v.union(v.literal('telegram'), v.literal('web')),
createdAt: v.number(),