import fs from 'fs'
import { getOpenAIClient } from './openai'
const openai = getOpenAIClient()

export async function createFile(filePath: string) {
  let result
  if (filePath.startsWith('http://') || filePath.startsWith('https://')) {
    // Download the file content from the URL
    const res = await fetch(filePath)
    console.log(res, 22)
    const buffer = await res.arrayBuffer()
    const urlParts = filePath.split('/')
    const fileName = urlParts[urlParts.length - 1]
    const file = new File([buffer], fileName)
    result = await openai.files.create({
      file: file,
      purpose: 'assistants',
    })
  } else {
    // Handle local file path
    const fileContent = fs.createReadStream(filePath)
    result = await openai.files.create({
      file: fileContent,
      purpose: 'assistants',
    })
  }
  return result.id
}

// Replace with your own file path or URL
const fileId = await createFile('https://cdn.openai.com/API/docs/deep_research_blog.pdf')

console.log(fileId, 11)
