ai icon indicating copy to clipboard operation
ai copied to clipboard

SvelteKit "handleSubmit" omits data

Open zicho opened this issue 11 months ago • 1 comments

Description

I cannot get "data" to get passed along to the server when using sveltekit. I am trying this:

I set the "useChat" up on my +page.svelte

const { input, handleSubmit, messages } = useChat();

My submit handler:

function submitHandler(event: any) {
  event.preventDefault();
  handleSubmit(event, {
    data: {
      test: 'data'
    }   
  });
}

And my server:

export const POST = (async ({ request }) => {
  try {
    const jsonData = await request.json();

    console.dir(jsonData); // "data" object is missing from here. i only have "messages"

Based on this React example I also tried:

<form
    on:submit={(e) => {
    handleSubmit(e, {
      data: {
        test: 'data'
      }
    });
  }}
>

but "data" is still not available in +server.ts endpoint

Code example

No response

Additional context

No response

zicho avatar Apr 03 '24 13:04 zicho

I also have this problem.

hauselin avatar May 06 '24 01:05 hauselin

Same problem for me, looks like in the append function, 'data' is not destructured from the ChatRequestOptions and passed through.

    message: Message | CreateMessage,
    {
      options,
      functions,
      function_call,
      tools,
      tool_choice,
    }: ChatRequestOptions = {},
  ) => {
    if (!message.id) {
      message.id = generateId();
    }

    const chatRequest: ChatRequest = {
      messages: get(messages).concat(message as Message),
      options,
      ...(functions !== undefined && { functions }),
      ...(function_call !== undefined && { function_call }),
      ...(tools !== undefined && { tools }),
      ...(tool_choice !== undefined && { tool_choice }),
    };
    return triggerRequest(chatRequest);
  };

andrewrisse avatar May 29 '24 19:05 andrewrisse

https://github.com/vercel/ai/pull/1748

lgrammel avatar May 30 '24 02:05 lgrammel

I am still having this issue as well. Any fix ?

Sometimes I see handleSubmit being called twice. The first time, data is received by the server while the second time, it comes through as empty / undefined

ghoshsanjoy78 avatar Aug 21 '24 07:08 ghoshsanjoy78