feat(*): send images from website
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { v } from 'convex/values';
|
||||
import { mutation, query } from './_generated/server';
|
||||
import type { Id } from './_generated/dataModel';
|
||||
|
||||
export const listByChat = query({
|
||||
args: { chatId: v.id('chats') },
|
||||
@@ -39,6 +40,7 @@ export const create = mutation({
|
||||
imageMediaType: v.optional(v.string()),
|
||||
imagesBase64: v.optional(v.array(v.string())),
|
||||
imagesMediaTypes: v.optional(v.array(v.string())),
|
||||
photoDraftIds: v.optional(v.array(v.id('photoDrafts'))),
|
||||
followUpOptions: v.optional(v.array(v.string())),
|
||||
isStreaming: v.optional(v.boolean())
|
||||
},
|
||||
@@ -58,18 +60,50 @@ export const create = mutation({
|
||||
isStreaming: args.isStreaming
|
||||
});
|
||||
|
||||
const drafts: Array<{ base64: string; mediaType: string; id: Id<'photoDrafts'> }> = [];
|
||||
if (args.photoDraftIds && args.photoDraftIds.length > 0) {
|
||||
for (const draftId of args.photoDraftIds) {
|
||||
const draft = await ctx.db.get(draftId);
|
||||
if (draft) {
|
||||
drafts.push({ base64: draft.base64, mediaType: draft.mediaType, id: draft._id });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < drafts.length; i++) {
|
||||
await ctx.db.insert('messageImages', {
|
||||
messageId,
|
||||
base64: drafts[i].base64,
|
||||
mediaType: drafts[i].mediaType,
|
||||
order: i
|
||||
});
|
||||
}
|
||||
|
||||
if (args.source === 'web' && args.role === 'user') {
|
||||
const chat = await ctx.db.get(args.chatId);
|
||||
if (chat) {
|
||||
await ctx.db.insert('pendingGenerations', {
|
||||
const pendingGenId = await ctx.db.insert('pendingGenerations', {
|
||||
userId: chat.userId,
|
||||
chatId: args.chatId,
|
||||
userMessage: args.content,
|
||||
createdAt: Date.now()
|
||||
});
|
||||
|
||||
for (let i = 0; i < drafts.length; i++) {
|
||||
await ctx.db.insert('pendingGenerationImages', {
|
||||
pendingGenerationId: pendingGenId,
|
||||
base64: drafts[i].base64,
|
||||
mediaType: drafts[i].mediaType,
|
||||
order: i
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const draft of drafts) {
|
||||
await ctx.db.delete(draft.id);
|
||||
}
|
||||
|
||||
return messageId;
|
||||
}
|
||||
});
|
||||
@@ -177,6 +211,15 @@ export const getChatImages = query({
|
||||
const images: Array<{ base64: string; mediaType: string }> = [];
|
||||
|
||||
for (const m of messages) {
|
||||
const msgImages = await ctx.db
|
||||
.query('messageImages')
|
||||
.withIndex('by_message_id', (q) => q.eq('messageId', m._id))
|
||||
.collect();
|
||||
|
||||
for (const img of msgImages.sort((a, b) => a.order - b.order)) {
|
||||
images.push({ base64: img.base64, mediaType: img.mediaType });
|
||||
}
|
||||
|
||||
if (m.imagesBase64 && m.imagesMediaTypes) {
|
||||
for (let i = 0; i < m.imagesBase64.length; i++) {
|
||||
images.push({
|
||||
|
||||
Reference in New Issue
Block a user