SparkDirective.js 1.34 KB
Newer Older
xuchaozou's avatar
xuchaozou committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import BaseDirective from "./BaseDirective"
import PandaSparkWebSocket from '../core/sockets/PandaSparkWebSocket'

class SparkDirective extends BaseDirective {
    constructor(options) {
        super(options)
        this.pandaSparkWebSocket = new PandaSparkWebSocket({
            config : this.params.sparkModelConfig
        })
        this.pandaSparkWebSocket.start()
    }
    match () {
        const { text } = this;
        return true
    }

    async excute({
        sendMsg = function () {}
    }) {
        return new Promise(async (resolve, reject) => {
邹绪超's avatar
邹绪超 committed
21
            this.reject = reject
xuchaozou's avatar
xuchaozou committed
22 23
            this.resolve = resolve
            this.sendMsg = sendMsg
xuchaozou's avatar
xuchaozou committed
24 25 26 27
            const data = await this.pandaSparkWebSocket.sendMessag({
                content : this.text,
                isLinkContext : false,
                frame ({resultTextTemp, resultText}) {
xuchaozou's avatar
xuchaozou committed
28
                    sendMsg?.(resultTextTemp)
xuchaozou's avatar
xuchaozou committed
29 30 31 32 33 34
                }
            })
            resolve(data.resultText)
        })
    }

xuchaozou's avatar
xuchaozou committed
35 36 37 38 39
    closeResponse() {
        if(this.pandaSparkWebSocket) {
            this.pandaSparkWebSocket.stop()
        }
        this.sendMsg = null
邹绪超's avatar
邹绪超 committed
40
        this.reject("")
xuchaozou's avatar
xuchaozou committed
41 42
    }

xuchaozou's avatar
xuchaozou committed
43 44 45 46 47 48 49 50 51
    destroy(){
        if(this.pandaSparkWebSocket) {
            this.pandaSparkWebSocket.destroy()
            this.pandaSparkWebSocket = null
        }
    }
} 

export default SparkDirective