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

Add support for message transformation in reload function(resolves #167) #989

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
43 changes: 25 additions & 18 deletions packages/core/react/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ export type UseChatHelpers = {
* Reload the last AI chat response for the given chat history. If the last
* message isn't from the assistant, it will request the API to generate a
* new response.
*
* If the `transform` option is provided, it will be used to transform the
* messages before sending the request to the API.
*
*/
reload: (
chatRequestOptions?: ChatRequestOptions,
chatRequestOptions?: ChatRequestOptions & {
transform?: (messages: Message[]) => Message[];
},
) => Promise<string | null | undefined>;
/**
* Abort the current request immediately, keep the generated tokens if any.
Expand Down Expand Up @@ -377,31 +383,32 @@ export function useChat({

const reload = useCallback(
async ({
transform,
options,
functions,
function_call,
tools,
tool_choice,
}: ChatRequestOptions = {}) => {
if (messagesRef.current.length === 0) return null;

// Remove last assistant message and retry last user message.
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
if (lastMessage.role === 'assistant') {
const chatRequest: ChatRequest = {
messages: messagesRef.current.slice(0, -1),
options,
...(functions !== undefined && { functions }),
...(function_call !== undefined && { function_call }),
...(tools !== undefined && { tools }),
...(tool_choice !== undefined && { tool_choice }),
};

return triggerRequest(chatRequest);
}: ChatRequestOptions & {
transform?: (messages: Message[]) => Message[];
} = {}) => {
let finalMessages = messagesRef.current;

// If transform is provided, use it directly on the current messages
if (transform) {
finalMessages = transform(messagesRef.current);
} else {
// If transform is not provided, continue with the existing logic
// Check if the last message is from the assistant and remove it if so
const lastMessage = messagesRef.current[messagesRef.current.length - 1];
if (lastMessage && lastMessage.role === 'assistant') {
finalMessages = messagesRef.current.slice(0, -1);
}
}

// Construct the chat request with the final set of messages
const chatRequest: ChatRequest = {
messages: messagesRef.current,
messages: finalMessages,
options,
...(functions !== undefined && { functions }),
...(function_call !== undefined && { function_call }),
Expand Down