fix(*): images do work
This commit is contained in:
@@ -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!
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user