import {
	http,
	Method
} from '@/utils/request.js';
  // 使用storage模块的方法设置登录状态为false
import storage from "@/utils/storage.js";
import api from "@/config/api.js";
const request = http.request;

/**
 * 上传用户头像和昵称
 * @param params
 */
export function chartStream(params) {
  return http.request({
    url: "dify/chart/stream",
    method: Method.POST,
    // needToken: true,
    data:params,
  });
}

// 发送流式AI聊天请求(返回fetch Promise)
export function postChatMessageData(data) {
	console.log("JSON.stringify(data)",JSON.stringify(data));
  const baseUrl =api.serve || '';
  const url = `${baseUrl}/uniapp/dify/chat/stream`;
  
  return fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'text/event-stream',
      'Authorization': `Bearer ${storage.getAccessToken()}`
    },
    body: JSON.stringify(data),
    credentials: 'include', // 确保发送凭证（cookie等）
    mode: 'cors'           // 允许跨域请求
  });
}

// 终止流式请求
export function stopChatStream(data) {
  return request({
    url: '/uniapp/dify/chat/stop',
    method: 'post',
    data: data
  })
} 
// 获取下一轮建议问题列表
export function chatStreamSuggested(data) {
  return http.request({
    url: '/uniapp/dify/chat/suggested',
    method: 'get',
    params: data
  })
} 