vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
8.82k stars 1.26k forks source link

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

Open leotrieu opened 5 months ago

leotrieu commented 5 months ago

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

robertoshimizu commented 4 months ago

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

ErikDale commented 2 months ago

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

atihar commented 2 weeks ago

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

zakinadhif commented 2 weeks ago

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.

jbergs-dsit commented 1 week ago

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