// ai聊天相关的
import type { Express } from 'express'
import type { OpenAI } from 'openai'

const memoryList: { role: 'user' | 'assistant'; content: string }[] = []

export const registerChatRoutes = (app: Express, client: OpenAI) => {
  // ai聊天
  app.post('/api/chat', async (req, res) => {
    const { content } = req.body

    memoryList.push({
      role: 'user',
      content: content,
    })
    const response = await client.responses.create({
      model: 'gpt-5-mini',
      input: memoryList,
      stream: true,
    })
    for await (const event of response) {
      // 说明是文本
      console.log(event)
      if (event.type === 'response.output_text.delta') {
        res.write(event.delta)
      } else if (event.type === 'response.output_text.done') {
        res.end()
      }
    }
  })
}
