vercel / ai

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

useChat not sending header options in request #2707

Closed apat183 closed 3 weeks ago

apat183 commented 3 weeks ago

Description

I can't get the headers option to work in VueJS. It doesn't get passed (specifically "authorization". The "Content-Type" appears to be changing. I'm calling api on nodejs backend with express. but see screenshot below I don't think that matters as the request is not including the header.

const { messages, input, handleSubmit } = useChat({
  api: '/api/ai/chat', headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${getToken()}`
  }
});

I've tried creating new headers object:

const { messages, input, handleSubmit } = useChat({
  api: '/api/ai/chat', headers: new Headers({
    'Content-Type': 'application/json',
    'Authorization': getToken()
  })
});

As you can see in dev tools, nothing is added to request headers. On express side it's not received as well.

Screenshot 2024-08-17 at 10 44 02 AM

Code example

import { useChat } from '@ai-sdk/vue';
import { getToken } from '@/lib/supabase'

console.log(getToken())
const { messages, input, handleSubmit } = useChat({
  api: '/api/ai/chat', headers: new Headers({
    'Content-Type': 'application/json',
    'Authorization': getToken()
  })
});

Additional context

Just add, this works with own code and using fetch. So can't see issue on backend or through other code as I'm just changing the request/tool

const response = await fetch('/api/ai/chat', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: getToken()
    },
    body: JSON.stringify({
      prompt: prompt
    })
  })
apat183 commented 3 weeks ago

Update in some investigation.

Looks like useCompletion works.

I noticed in the use-chat.ts. The headers and body are never used?

Screenshot 2024-08-17 at 11 54 09 AM
lgrammel commented 3 weeks ago

Fix: https://github.com/vercel/ai/pull/2718

lgrammel commented 3 weeks ago

Fixed in @ai-sdk/vue@0.0.38