Skip to content

Commit 50493c6

Browse files
committed
readme
1 parent 5ef3392 commit 50493c6

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

examples/chat-basic/README.md

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@ You have a function that allows paginating over messages.
1212
```ts
1313
import { paginationOptsValidator } from "convex/server";
1414

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+
});
2834
```
2935

3036
### Client setup
@@ -33,15 +39,24 @@ See [ChatBasic.tsx](./src/ChatBasic.tsx) for the client-side code.
3339

3440
The crux is to use the `useThreadMessages` hook:
3541

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";
3845

39-
// in the component
46+
function MyComponent({ threadId }: { threadId: string }) {
4047
const messages = useThreadMessages(
4148
api.chatBasic.listThreadMessages,
4249
{ threadId },
4350
{ initialNumItems: 10 },
4451
);
52+
return (
53+
<div>
54+
{toUIMessages(messages.results ?? []).map((message) => (
55+
<div key={message.key}>{message.content}</div>
56+
))}
57+
</div>
58+
);
59+
}
4560
```
4661

4762
### 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
5469
ephemeral message at the top of the list.
5570

5671
```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+
);
5977
```
6078

6179
If your arguments don't include `{ threadId, prompt }` then you can use it as a

0 commit comments

Comments
 (0)