|
1 | | -import { query, mutation, internalQuery } from "./_generated/server"; |
2 | | -import { StreamId } from "@convex-dev/persistent-text-streaming"; |
3 | | -import { v } from "convex/values"; |
4 | | -import { streamingComponent } from "./streaming"; |
5 | | - |
6 | | -export const listMessages = query({ |
7 | | - args: {}, |
8 | | - handler: async (ctx) => { |
9 | | - return await ctx.db.query("userMessages").collect(); |
10 | | - }, |
11 | | -}); |
12 | | - |
13 | | -export const clearMessages = mutation({ |
14 | | - args: {}, |
15 | | - handler: async (ctx) => { |
16 | | - const chats = await ctx.db.query("userMessages").collect(); |
17 | | - await Promise.all(chats.map((chat) => ctx.db.delete(chat._id))); |
18 | | - }, |
19 | | -}); |
20 | | - |
21 | | -export const sendMessage = mutation({ |
22 | | - args: { |
23 | | - prompt: v.string(), |
24 | | - }, |
25 | | - handler: async (ctx, args) => { |
26 | | - const responseStreamId = await streamingComponent.createStream(ctx); |
27 | | - const chatId = await ctx.db.insert("userMessages", { |
28 | | - prompt: args.prompt, |
29 | | - responseStreamId, |
30 | | - }); |
31 | | - return chatId; |
32 | | - }, |
33 | | -}); |
34 | | - |
35 | | -export const getHistory = internalQuery({ |
36 | | - args: {}, |
37 | | - handler: async (ctx) => { |
38 | | - // Grab all the user messages |
39 | | - const allMessages = await ctx.db.query("userMessages").collect(); |
40 | | - |
41 | | - // Lets join the user messages with the assistant messages |
42 | | - const joinedResponses = await Promise.all( |
43 | | - allMessages.map(async (userMessage) => { |
44 | | - return { |
45 | | - userMessage, |
46 | | - responseMessage: await streamingComponent.getStreamBody( |
47 | | - ctx, |
48 | | - userMessage.responseStreamId as StreamId |
49 | | - ), |
50 | | - }; |
51 | | - }) |
52 | | - ); |
53 | | - |
54 | | - return joinedResponses.flatMap((joined) => { |
55 | | - const user = { |
56 | | - role: "user" as const, |
57 | | - content: joined.userMessage.prompt, |
58 | | - }; |
59 | | - |
60 | | - const assistant = { |
61 | | - role: "assistant" as const, |
62 | | - content: joined.responseMessage.text, |
63 | | - }; |
64 | | - |
65 | | - // If the assistant message is empty, its probably because we have not |
66 | | - // started streaming yet so lets not include it in the history |
67 | | - if (!assistant.content) return [user]; |
68 | | - |
69 | | - return [user, assistant]; |
70 | | - }); |
71 | | - }, |
72 | | -}); |
| 1 | +import { query, mutation, internalQuery } from "./_generated/server"; |
| 2 | +import { StreamId } from "@convex-dev/persistent-text-streaming"; |
| 3 | +import { v } from "convex/values"; |
| 4 | +import { streamingComponent } from "./streaming"; |
| 5 | + |
| 6 | +export const listMessages = query({ |
| 7 | + args: {}, |
| 8 | + handler: async (ctx) => { |
| 9 | + return await ctx.db.query("userMessages").collect(); |
| 10 | + }, |
| 11 | +}); |
| 12 | + |
| 13 | +export const clearMessages = mutation({ |
| 14 | + args: {}, |
| 15 | + handler: async (ctx) => { |
| 16 | + const chats = await ctx.db.query("userMessages").collect(); |
| 17 | + await Promise.all(chats.map((chat) => ctx.db.delete(chat._id))); |
| 18 | + }, |
| 19 | +}); |
| 20 | + |
| 21 | +export const sendMessage = mutation({ |
| 22 | + args: { |
| 23 | + prompt: v.string(), |
| 24 | + }, |
| 25 | + handler: async (ctx, args) => { |
| 26 | + const responseStreamId = await streamingComponent.createStream(ctx); |
| 27 | + const chatId = await ctx.db.insert("userMessages", { |
| 28 | + prompt: args.prompt, |
| 29 | + responseStreamId, |
| 30 | + }); |
| 31 | + return chatId; |
| 32 | + }, |
| 33 | +}); |
| 34 | + |
| 35 | +export const getHistory = internalQuery({ |
| 36 | + args: {}, |
| 37 | + handler: async (ctx) => { |
| 38 | + // Grab all the user messages |
| 39 | + const allMessages = await ctx.db.query("userMessages").collect(); |
| 40 | + |
| 41 | + // Lets join the user messages with the assistant messages |
| 42 | + const joinedResponses = await Promise.all( |
| 43 | + allMessages.map(async (userMessage) => { |
| 44 | + return { |
| 45 | + userMessage, |
| 46 | + responseMessage: await streamingComponent.getStreamBody( |
| 47 | + ctx, |
| 48 | + userMessage.responseStreamId as StreamId |
| 49 | + ), |
| 50 | + }; |
| 51 | + }) |
| 52 | + ); |
| 53 | + |
| 54 | + return joinedResponses.flatMap((joined) => { |
| 55 | + const user = { |
| 56 | + role: "user" as const, |
| 57 | + content: joined.userMessage.prompt, |
| 58 | + }; |
| 59 | + |
| 60 | + const assistant = { |
| 61 | + role: "assistant" as const, |
| 62 | + content: joined.responseMessage.text, |
| 63 | + }; |
| 64 | + |
| 65 | + // If the assistant message is empty, its probably because we have not |
| 66 | + // started streaming yet so lets not include it in the history |
| 67 | + if (!assistant.content) return [user]; |
| 68 | + |
| 69 | + return [user, assistant]; |
| 70 | + }); |
| 71 | + }, |
| 72 | +}); |
0 commit comments