import OpenAI from "openai";

export const processWithDeepSeek = async (
  content: string,
  apiKey: string,
  streamFn: (content: string) => void,
): Promise<string> => {

  const openai = new OpenAI({
    baseURL: "https://api.deepseek.com/v1",
    apiKey,
    dangerouslyAllowBrowser: true,
  });
  const stream = await openai.chat.completions.create({
    messages: [
      {
        role: "system",
        content: "请将以下工作内容整理成简洁的报告:\n\n# 工作内容报告\n\n## 已完成任务\n1. 列出具体完成的工作任务\n2. 说明主要修改点\n\n请使用Markdown格式输出，保持简洁明了。",
      },
      {
        role: "user",
        content: content,
      },
    ],
    model: "deepseek-chat",
    stream: true,
  });

  let fullContent = "";
  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content || "";
    streamFn(content);
    fullContent += content;
  }
  
  return fullContent;
};

// export const processWithDeepSeek = async (content: string, apiKey: string): Promise<string> => {
//   console.log(apiKey);
//   try {
//     // 预处理git提交记录
//     const processedContent = content.includes('commit ')
//       ? `请将以下git提交记录整理成结构化的报告格式:\n\n${content}\n\n报告要求:\n1. 按时间顺序列出重要提交\n2. 总结主要变更内容\n3. 分析代码变更趋势`
//       : content;

//     const response = await post(
//       "https://api.deepseek.com/v1/chat/completions",
//       {
//         model: "deepseek-chat",
//         messages: [
//           {
//             role: "system",
//             content: "You are a helpful assistant.",
//           },
//           {
//             role: "user",
//             content: processedContent,
//           },
//         ],
//         temperature: 0.7,
//         max_tokens: 2000,
//         stream: false,
//       },
//       {
//         headers: {
//           Authorization: `Bearer ${apiKey}`,
//         },
//       },
//     );

//     console.log(response)
//     return response.choices[0].message.content || "";
//   } catch (error) {
//     console.error('DeepSeek API请求失败:', error);
//     message.error('DeepSeek API请求失败');
//     throw error;
//   }
// };