import { XStream } from '@ant-design/x'
import { sseRequest } from '@/utils/sseRequest'

async function request() {
  const response = await fetch('/api/openai/chat/create', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'ezijing@20250331',
    },
    body: JSON.stringify({
      stream: true,
      model: 'qwen-max-latest',
      messages: [{ role: 'user', content: '你好' }],
    }),
  })

  console.log(response)

  for await (const chunk of XStream({
    readableStream: response.body,
  })) {
    console.log(chunk)
  }
}
const controller = new AbortController()
async function request2() {
  const res = await sseRequest('/api/openai/chat/create', {
    method: 'POST',
    signal: controller.signal,
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'ezijing@20250331',
    },
    body: JSON.stringify({
      stream: true,
      model: 'qwen-max-latest',
      messages: [{ role: 'user', content: '你好' }],
    }),
    onUpdate: (data) => {
      console.log('update', data)
    },
    onSuccess: (data) => {
      console.log('success', data)
    },
    onError: (error) => {
      console.log('error', error)
    },
  })
  console.log(res)
}
export default function App() {
  return (
    <div>
      <button onClick={request2}>请求</button>
      <br />
      <br />
      <button onClick={() => controller.abort()}>取消</button>
    </div>
  )
}
