king
2025-02-14 32431a7ce3f9a642ec988cf21bcda8935070ddd2
src/views/mkai/index.jsx
@@ -1,8 +1,9 @@
import React, { Component, useState } from 'react'
import { fromJS } from 'immutable'
import { Spin, notification, Tooltip, Input } from 'antd'
import Api from '@/api'
// import Utils from '@/utils/utils.js'
import Utils from '@/utils/utils.js'
import avatar from '@/assets/img/avatar.jpg'
import './index.scss'
@@ -78,6 +79,7 @@
    LoginUID: '',
    textInput: '',
    type: 'deepseek-chat', // deepseek-chat deepseek-reasoner
    currentChat: {uuid: Utils.getuuid(), list: [], title: ''}
  }
  UNSAFE_componentWillMount() {
@@ -135,13 +137,15 @@
          duration: 10
        })
      } else {
        this.setState({
          chats: result.data || []
        })
      }
    })
  }
  newContent = () => {
    this.setState({textInput: ''})
    this.setState({textInput: '', currentChat: {uuid: Utils.getuuid(), list: [], title: ''}})
    let node = document.getElementById('mk-input')
    node && node.focus()
  }
@@ -157,35 +161,101 @@
  }
  submit = () => {
    const { textInput } = this.state
    const { textInput, currentChat, UserID, LoginUID } = this.state
    let val = textInput.replace(/\t+|\v+/g, '').replace(/^\s+|\s+$/g, '')
    if (!val) return
    if (!val || currentChat.loading) return
    let node = document.getElementById('mk-input')
    node && node.blur()
    this.setState({textInput: ''})
    let chat = fromJS(currentChat).toJS()
    chat.list.push({ role: 'user', content: val })
    chat.list.push({ role: 'assistant', content: '', loading: true })
    // Api.directRequest({
    //   url: burl + '/chat/completions',
    //   method: 'post',
    //   headers: { 'Content-Type': 'application/json' },
    //   data: {
    //     model: this.state.type,
    //     messages: [
    //       { role: 'user', content: val }
    //     ],
    //     stream: false
    //   }
    // }).then(result => {
    if (!chat.title) {
      chat.title = val.substr(0, 32)
    }
    chat.loading = true
    // })
    this.setState({textInput: '', currentChat: chat})
    Api.directRequest({
      url: burl + '/chat/completions',
      method: 'post',
      headers: { 'Content-Type': 'application/json' },
      data: {
        model: this.state.type,
        messages: chat.list,
        stream: false
      }
    }).then(res => {
      let _chat = fromJS(chat).toJS()
      delete _chat.loading
      _chat.list.shift()
      let _val = '服务器繁忙,请稍后再试。'
      if (res.success && res.choices && res.choices[0]) {
        _val = res.choices[0].message.content
      }
      _chat.list = _chat.list.map(item => {
        delete item.step
        return item
      })
      _chat.list.push({ role: 'assistant', content: _val, step: true })
      this.setState({currentChat: _chat})
      Api.genericInterface({
        func: 's_deepseek_ai',
        rduri: burl + '/webapi/dostars',
        file_url: '',
        userid: UserID,
        LoginUID: LoginUID,
        u_id: sessionStorage.getItem('UserID'),
        content: window.btoa(window.encodeURIComponent(_val)),
        title: window.btoa(window.encodeURIComponent(chat.title)),
        data_type: 'reply',
        ID: chat.uuid
      }).then(r => {
        if (!r.status) {
          notification.error({
            top: 92,
            message: r.message,
            duration: 10
          })
        }
      })
    })
    Api.genericInterface({
      func: 's_deepseek_ai',
      rduri: burl + '/webapi/dostars',
      file_url: '',
      userid: UserID,
      LoginUID: LoginUID,
      u_id: sessionStorage.getItem('UserID'),
      content: window.btoa(window.encodeURIComponent(val)),
      title: window.btoa(window.encodeURIComponent(chat.title)),
      data_type: 'request',
      ID: chat.uuid
    }).then(result => {
      if (!result.status) {
        notification.error({
          top: 92,
          message: result.message,
          duration: 10
        })
      }
    })
  }
  render () {
    const { loading, textInput, type } = this.state
    const { loading, textInput, type, currentChat } = this.state
    let able = textInput && !/^\s+$/.test(textInput)
@@ -201,7 +271,7 @@
          <div className="mk-right">
            <div className="mk-faad">
              <div className="title">
                <span>标题</span>
                <span>{currentChat.title}</span>
              </div>
            </div>
            <div className="mk-ksje">