Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (ai/ui): add mutator function support to useChat / setMessages. #2347

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/tough-spies-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@ai-sdk/svelte': patch
'@ai-sdk/react': patch
'@ai-sdk/solid': patch
'ai': patch
'@ai-sdk/vue': patch
---

feat (ai/ui): add mutator function support to useChat / setMessages
2 changes: 1 addition & 1 deletion content/docs/07-reference/ai-sdk-ui/01-use-chat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ Allows you to easily create a conversational user interface for your chatbot app
},
{
name: 'setMessages',
type: '(messages: Message[]) => void',
type: '(messages: Message[] | ((messages: Message[]) => Message[]) => void',
description:
'Function to update the `messages` state locally without triggering an API call.',
},
Expand Down
15 changes: 12 additions & 3 deletions packages/core/svelte/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export type UseChatHelpers = {
* edit the messages on the client, and then trigger the `reload` method
* manually to regenerate the AI response.
*/
setMessages: (messages: Message[]) => void;
setMessages: (
messages: Message[] | ((messages: Message[]) => Message[]),
) => void;

/** The current value of the input */
input: Writable<string>;
/** Form submission handler to automatically reset input and append a user message */
Expand Down Expand Up @@ -359,8 +362,14 @@ export function useChat({
}
};

const setMessages = (messages: Message[]) => {
mutate(messages);
const setMessages = (
messagesArg: Message[] | ((messages: Message[]) => Message[]),
) => {
if (typeof messagesArg === 'function') {
messagesArg = messagesArg(get(messages));
}

mutate(messagesArg);
};

const input = writable(initialInput);
Expand Down
10 changes: 8 additions & 2 deletions packages/react/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export type UseChatHelpers = {
* edit the messages on the client, and then trigger the `reload` method
* manually to regenerate the AI response.
*/
setMessages: (messages: Message[]) => void;
setMessages: (
messages: Message[] | ((messages: Message[]) => Message[]),
) => void;
/** The current value of the input */
input: string;
/** setState-powered method to update the input value */
Expand Down Expand Up @@ -507,7 +509,11 @@ By default, it's set to 0, which will disable the feature.
}, []);

const setMessages = useCallback(
(messages: Message[]) => {
(messages: Message[] | ((messages: Message[]) => Message[])) => {
if (typeof messages === 'function') {
messages = messages(messagesRef.current);
}

mutate(messages, false);
messagesRef.current = messages;
},
Expand Down
16 changes: 12 additions & 4 deletions packages/solid/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export type UseChatHelpers = {
* edit the messages on the client, and then trigger the `reload` method
* manually to regenerate the AI response.
*/
setMessages: (messages: Message[]) => void;
setMessages: (
messages: Message[] | ((messages: Message[]) => Message[]),
) => void;
/** The current value of the input */
input: Accessor<string>;
/** Signal setter to update the input value */
Expand Down Expand Up @@ -362,9 +364,15 @@ export function useChat(
}
};

const setMessages = (messages: Message[]) => {
mutate(messages);
messagesRef = messages;
const setMessages = (
messagesArg: Message[] | ((messages: Message[]) => Message[]),
) => {
if (typeof messagesArg === 'function') {
messagesArg = messagesArg(messagesRef);
}

mutate(messagesArg);
messagesRef = messagesArg;
};

const [input, setInput] = createSignal(
Expand Down
15 changes: 12 additions & 3 deletions packages/svelte/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export type UseChatHelpers = {
* edit the messages on the client, and then trigger the `reload` method
* manually to regenerate the AI response.
*/
setMessages: (messages: Message[]) => void;
setMessages: (
messages: Message[] | ((messages: Message[]) => Message[]),
) => void;

/** The current value of the input */
input: Writable<string>;
/** Form submission handler to automatically reset input and append a user message */
Expand Down Expand Up @@ -356,8 +359,14 @@ export function useChat({
}
};

const setMessages = (messages: Message[]) => {
mutate(messages);
const setMessages = (
messagesArg: Message[] | ((messages: Message[]) => Message[]),
) => {
if (typeof messagesArg === 'function') {
messagesArg = messagesArg(get(messages));
}

mutate(messagesArg);
};

const input = writable(initialInput);
Expand Down
14 changes: 11 additions & 3 deletions packages/vue/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export type UseChatHelpers = {
* edit the messages on the client, and then trigger the `reload` method
* manually to regenerate the AI response.
*/
setMessages: (messages: Message[]) => void;
setMessages: (
messages: Message[] | ((messages: Message[]) => Message[]),
) => void;
/** The current value of the input */
input: Ref<string>;
/** Form submission handler to automatically reset input and append a user message */
Expand Down Expand Up @@ -264,8 +266,14 @@ export function useChat({
}
};

const setMessages = (messages: Message[]) => {
mutate(messages);
const setMessages = (
messagesArg: Message[] | ((messages: Message[]) => Message[]),
) => {
if (typeof messagesArg === 'function') {
messagesArg = messagesArg(messages.value);
}

mutate(messagesArg);
};

const input = ref(initialInput);
Expand Down
Loading