ai icon indicating copy to clipboard operation
ai copied to clipboard

Passing parameter to useChat > reload() function with Server Action is not working

Open leotrieu opened this issue 1 year ago • 5 comments

Description

  1. Create a useChat with the api as a server action (for experimental_StreamingReactResponse),
  2. The api as a server action worked fine.
  3. pass a parameter to reload (for re-generating)
  4. this parameter is not passed as a parameter to the handler

Code example

### Front-end

const { messages, input, reload, handleInputChange, handleSubmit } = useChat({
    api: handler,
  });

  const handleRegenerate = () => {
    reload({
      options: {
        body: {
          isRegenerate: true,
        },
      },
    });
  };

### Server Action

"use server";

export async function handler(req: any) {
  console.log("Request", req);
}

### Output Request { messages: [ { role: 'user', content: 'Hi' } ] }

(There is no way to get the isRegenerate parameter passed from client)

Additional context

No response

leotrieu avatar Feb 23 '24 12:02 leotrieu

I am also reporting attempts to pass any ChatRequestOptions to reload(), for example:

options = {
          data: {
            key1: value1;
            key2: value2
          }
        }

reload(options)

The route.ts of useChat does not receive the options, just the messages.

Hope you can prioritize this requirement. Cheers RS

robertoshimizu avatar Mar 18 '24 23:03 robertoshimizu

@leotrieu @robertoshimizu have any of you found a fix for this yet?

ErikDale avatar May 02 '24 11:05 ErikDale

I have solved this problem by this const options = { api: "/api/chat?id=hello", }; const { messages, input, handleInputChange, handleSubmit } = useChat(options);

atihar avatar Jul 07 '24 23:07 atihar

I can't find anywhere in the doc where it says you can put a server action into useChat()'s api parameter. Instead, it says the api parameter takes a string value.

Although it'd be nice if we could 😁

https://github.com/vercel/ai/blob/5063b8cb2b481d95b00c022566c16e63489c68c9/packages/ui-utils/src/types.ts#L283 https://github.com/vercel/ai/blob/5063b8cb2b481d95b00c022566c16e63489c68c9/packages/ui-utils/src/call-chat-api.ts

Yep, confirmed. All type definitions of api expects a string.

zakinadhif avatar Jul 09 '24 14:07 zakinadhif

Are there any updates on useChat's reload and why it's dropping passed ChatRequestOptions?

jbergs-dsit avatar Jul 19 '24 15:07 jbergs-dsit