@@ -12,19 +12,25 @@ You have a function that allows paginating over messages.
12
12
``` ts
13
13
import { paginationOptsValidator } from " convex/server" ;
14
14
15
- export const listThreadMessages = query ({
16
- args: {
17
- threadId: v .string (),
18
- paginationOpts: paginationOptsValidator ,
19
- // ... other arguments you want
20
- },
21
- handler : async (ctx , { threadId , paginationOpts }): PaginationResult <MessageDoc > => {
22
- // await authorizeThreadAccess(ctx, threadId);
23
- const paginated = await agent .listMessages (ctx , { threadId , paginationOpts });
24
- // Here you could filter out / modify the documents
25
- return paginated ;
26
- },
27
- });
15
+ export const listThreadMessages = query ({
16
+ args: {
17
+ threadId: v .string (),
18
+ paginationOpts: paginationOptsValidator ,
19
+ // ... other arguments you want
20
+ },
21
+ handler : async (
22
+ ctx ,
23
+ { threadId , paginationOpts },
24
+ ): PaginationResult <MessageDoc > => {
25
+ // await authorizeThreadAccess(ctx, threadId);
26
+ const paginated = await agent .listMessages (ctx , {
27
+ threadId ,
28
+ paginationOpts ,
29
+ });
30
+ // Here you could filter out / modify the documents
31
+ return paginated ;
32
+ },
33
+ });
28
34
```
29
35
30
36
### Client setup
@@ -33,15 +39,24 @@ See [ChatBasic.tsx](./src/ChatBasic.tsx) for the client-side code.
33
39
34
40
The crux is to use the ` useThreadMessages ` hook:
35
41
36
- ``` ts
37
- import { useThreadMessages } from " @convex-dev/agent/react" ;
42
+ ``` tsx
43
+ import { api } from " ../convex/_generated/api" ;
44
+ import { useThreadMessages , toUIMessages } from " @convex-dev/agent/react" ;
38
45
39
- // in the component
46
+ function MyComponent({ threadId } : { threadId : string }) {
40
47
const messages = useThreadMessages (
41
48
api .chatBasic .listThreadMessages ,
42
49
{ threadId },
43
50
{ initialNumItems: 10 },
44
51
);
52
+ return (
53
+ <div >
54
+ { toUIMessages (messages .results ?? []).map ((message ) => (
55
+ <div key = { message .key } >{ message .content } </div >
56
+ ))}
57
+ </div >
58
+ );
59
+ }
45
60
```
46
61
47
62
### Optimistic updates for sending messages
@@ -54,8 +69,11 @@ Pass in the query that you're using to list messages, and it will insert the
54
69
ephemeral message at the top of the list.
55
70
56
71
``` ts
57
- const sendMessage = useMutation (api .chatBasic .generateResponse )
58
- .withOptimisticUpdate (optimisticallySendMessage (api .chatBasic .listThreadMessages ));
72
+ const sendMessage = useMutation (
73
+ api .chatBasic .generateResponse ,
74
+ ).withOptimisticUpdate (
75
+ optimisticallySendMessage (api .chatBasic .listThreadMessages ),
76
+ );
59
77
```
60
78
61
79
If your arguments don't include ` { threadId, prompt } ` then you can use it as a
0 commit comments