Commit 4effee3c authored by xuchaozou's avatar xuchaozou

fix: 修复语音识别失败

parent 274212ae
Pipeline #85919 passed with stages
import { request, event } from '@wisdom-utils/utils';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
import { message } from 'antd';
import { init, cut, extract, ready } from '@congcongcai/jieba.js';
import { PandaRecordWebSocket } from './core';
import PandaTip from './Ui/PandaTip';
import context from './config'
const PandaUi = props => {
const pandaRecordWebSocketRef = useRef(null);
const [isStartRecorder, setIsStartRecorder] = useState(false);
const { keyRef } = props;
const {globalConfig , setGlobalConfig} = useContext(context)
useEffect(async () => {
try {
......@@ -61,7 +62,7 @@ const PandaUi = props => {
const createPandaRecordWebSocket = async () => {
destroyPandaRecorderWebSocket();
pandaRecordWebSocketRef.current = new PandaRecordWebSocket({
config: keyRef.current.shortSound,
config : globalConfig.get('key').shortSound,
events: {
frame(data) {
event.emit('aiSound:frame', data);
......
import style from './style.less'
import { withRouter } from '@wisdom-utils/runtime'
import { Tooltip, Typography } from 'antd'
import { Fragment, useCallback, useEffect, useRef, useState } from 'react'
import { Fragment, useCallback, useContext, useEffect, useRef, useState } from 'react'
import { event } from '@wisdom-utils/utils'
import moment from 'moment'
import { Directive , AllDirectiveTypes} from '../../core'
import config from '../../config'
import context from '../../config'
import SparkDirective from '../../core/directive/SparkDirective'
const Dialog = props => {
const {globalConfig , setGlobalConfig} = useContext(context)
const { pandaRecordWebSocketRef , close} = props
const ulRef = useRef(null)
const directiveRef = useRef(null)
......@@ -29,8 +30,12 @@ const Dialog = props => {
useEffect(() => {
directiveRef.current = new Directive({
params: {
sparkModelConfig : globalConfig.getIn(['key']).shortSound
}
})
if(globalConfig.get('key').isOpenSparkModel) {
directiveRef.current.directiveTypes.push(SparkDirective)
}
event.on("aiSound:finish", finish)
event.on("aiSound:frame", frame)
startTime()
......@@ -52,9 +57,14 @@ const Dialog = props => {
role: "system",
content: "系统正在思考中,请稍后..."
})
const msg = await directive.excute()
updateLastData({
content: msg
let resultMsg = ""
await directive.excute({
sendMsg : msg => {
resultMsg += msg
updateLastData({
content : resultMsg
})
}
})
pandaRecordWebSocketRef.current.setStatus("playing")
return
......@@ -124,7 +134,7 @@ const Dialog = props => {
destroyTime()
timeRef.current = setTimeout(() => {
close && close()
}, config.closePandaAiTime)
}, globalConfig.getIn(['closePandaAiTime']))
}
const destroyTime = () => {
......
import { useCallback, useEffect, useRef, useState } from 'react'
import { useCallback, useContext, useEffect, useRef, useState } from 'react'
import style from './style.less'
import image from '@/assets/images/soundAi/眨眼.gif'
import { event } from '@wisdom-utils/utils'
import { AwakenDirective, ByeDirective, Directive } from '../../core'
import Dialog from '../Dialog'
import config from '../../config'
import context from '../../config'
const PandaTip = props => {
const { pandaRecordWebSocketRef } = props
const {globalConfig , setGlobalConfig} = useContext(context)
const [status, setStatus] = useState("close")
const directiveRef = useRef(null)
const [initOpen, setInitOpen] = useState(false)
......@@ -54,7 +54,7 @@ const PandaTip = props => {
destroyTime()
timeRef.current = setTimeout(() => {
setShowTip(true)
}, config.showOpenAiTipTime)
}, globalConfig.getIn(['showOpenAiTipTime']))
}
const destroyTime = () => {
......
const config = {
closePandaAiTime : 1000 * 60,
showOpenAiTipTime : 1000 * 60
}
import { fromJS } from "immutable"
import { createContext } from "react"
export default createContext(null)
export default config
\ No newline at end of file
export const contextData = data => fromJS({
closePandaAiTime : 1000 * 60,
showOpenAiTipTime : 1000 * 60,
key : null,
...data ?? {}
})
\ No newline at end of file
......@@ -29,7 +29,11 @@ class PandaRecordWebSocket extends PandaWebSocket {
}
async start() {
const data = await super.start();
let data = super.start();
if (!data.isSuccess) {
return data;
}
data = await this.checkRecorder()
if (!data.isSuccess) {
return data;
}
......@@ -41,10 +45,8 @@ class PandaRecordWebSocket extends PandaWebSocket {
}
async checkParams() {
let data = super.checkParams();
if (!data.isSuccess) return data;
data = await Utils.isCanableRecorder(this.config.checkDelayDeciveTime);
async checkRecorder() {
const data = await Utils.isCanableRecorder(this.config.checkDelayDeciveTime);
if (!data.isSuccess) return data;
return data;
}
......
......@@ -22,6 +22,7 @@ class PandaSparkWebSocket extends PandaWebSocket {
this.recordData = null;
this.resolve = null;
this.reject = null;
this.frame = null
}
initConfig() {
......@@ -115,7 +116,7 @@ class PandaSparkWebSocket extends PandaWebSocket {
}
}
sendMessag({ content, role, isLinkContext = true }) {
sendMessag({ content, role, isLinkContext = true , frame = function() {} }) {
return new Promise((resolve, reject) => {
if (this.resolve) {
return reject({
......@@ -127,6 +128,7 @@ class PandaSparkWebSocket extends PandaWebSocket {
this.resultTextTemp = '';
this.resolve = resolve;
this.reject = reject;
this.frame = frame
this.recordData = {
content,
role,
......@@ -172,6 +174,10 @@ class PandaSparkWebSocket extends PandaWebSocket {
resultTextTemp: content,
resultText: this.resultText,
});
this.frame && this.frame({
resultTextTemp: content,
resultText: this.resultText,
})
} else if (status == 2) {
this.data.push({
role: 'assistant',
......
......@@ -18,20 +18,20 @@ class PandaWebSocket {
this.events = Object.assign(
{},
{
wsOpen: function () {},
wsOnmessage: function () {},
wsOnerror: function () {},
wsOnclose: function () {},
fail: function () {},
frame: function () {},
finish: function () {},
wsOpen: function () { },
wsOnmessage: function () { },
wsOnerror: function () { },
wsOnclose: function () { },
fail: function () { },
frame: function () { },
finish: function () { },
},
options.events || {},
);
}
async start() {
const data = await this.checkParams();
start() {
const data = this.checkParams();
if (!data.isSuccess) {
return data;
}
......
......@@ -10,7 +10,7 @@ class AwakenDirective extends BaseDirective{
return false
}
async excute () {
async excute ({sendMsg = function() {}}) {
}
}
......
......@@ -10,7 +10,9 @@ class BaseDirective {
match () {
}
async excute () {
async excute ({
sendMsg = function () {}
}) {
}
}
......
......@@ -44,7 +44,9 @@ class CloseMenuDirective extends BaseDirective {
return true;
}
async excute() {
async excute({
sendMsg = function () {}
}) {
let widgets = null;
if (typeof this.matchResult === 'number') {
const widgets = this.getOpenedMenus();
......@@ -52,33 +54,33 @@ class CloseMenuDirective extends BaseDirective {
this.data = {};
this.data.type = 'widgets';
this.data.widgets = widgets;
return `索引超出了界限,请重新语音选择关闭第几个菜单`;
return sendMsg(`索引超出了界限,请重新语音选择关闭第几个菜单`);
}
const menu = widgets[this.matchResult];
event.emit('event:closeMenuByName', menu.name);
return `关闭${menu.name}菜单成功`;
return sendMsg(`关闭${menu.name}菜单成功`);
}
if (this.matchResult == '所有') {
return this.closeAllMenu();
return sendMsg(this.closeAllMenu());
}
if (this.matchResult == '当前') {
return this.closeCurrentMenu();
return sendMsg(this.closeCurrentMenu());
}
widgets = this.getIsOneWdiget();
if (!widgets) {
widgets = this.getCompleteMenus();
}
if (!widgets) {
return '已打开菜单中没有查找到此菜单,请检查语音是否正确';
return sendMsg('已打开菜单中没有查找到此菜单,请检查语音是否正确');
}
if (widgets.length == 1) {
event.emit('event:closeMenuByName', widgets[0].name);
return `关闭${widgets[0].name}菜单成功`;
return sendMsg(`关闭${widgets[0].name}菜单成功`);
}
this.data = {};
this.data.type = 'widgets';
this.data.widgets = widgets;
return this.widgetsToMsg(widgets);
return sendMsg(this.widgetsToMsg(widgets));
}
widgetsToMsg(widgets) {
......
......@@ -10,12 +10,14 @@ class HomePageDirective extends BaseDirective {
}
return false
}
async excute () {
async excute ({
sendMsg = function() {}
}) {
if(!window.globalConfig.homepage) {
return "没有配置首页"
return sendMsg("没有配置首页")
}
Utils.openMenu({url : window.globalConfig.homepage})
return "返回首页成功"
return sendMsg("返回首页成功")
}
}
......
......@@ -39,7 +39,9 @@ class OpenMenuDirective extends BaseDirective {
return true
}
async excute() {
async excute({
sendMsg = function () {}
}) {
let widgets = null
if(typeof this.matchResult == "number") {
const lastMenu = this.directives.at(-2)
......@@ -48,11 +50,11 @@ class OpenMenuDirective extends BaseDirective {
this.data = {}
this.data.type = "widgets"
this.data.widgets = widgets
return `索引超出了界限,请重新语音选择第几个菜单`
return sendMsg(`索引超出了界限,请重新语音选择第几个菜单`)
} else {
const currentMenu = widgets[this.matchResult - 1]
this.openSingleMenu(currentMenu)
return `成功打开${currentMenu.label}菜单`
return sendMsg(`成功打开${currentMenu.label}菜单`)
}
}
else {
......@@ -61,15 +63,15 @@ class OpenMenuDirective extends BaseDirective {
widgets = this.getPatternMenus()
}
if (!widgets) {
return "没有查找到此菜单,请检查语音是否正确"
return sendMsg("没有查找到此菜单,请检查语音是否正确")
}
if (widgets.length == 1) {
return this.handleOneSingleMenu(widgets[0])
return sendMsg(this.handleOneSingleMenu(widgets[0]))
} else {
this.data = {}
this.data.type = "widgets"
this.data.widgets = widgets
return `${this.widgetsToMsg(widgets)}`
sendMsg(`${this.widgetsToMsg(widgets)}`)
}
}
}
......@@ -81,7 +83,7 @@ class OpenMenuDirective extends BaseDirective {
} else {
this.data = {}
this.data.type = "widgets"
this.data.widgets = widgets
this.data.widgets = widget.widgets
return `你打开的${widget.label}是一个菜单组,${this.widgetsToMsg(widget.widgets)}`
}
}
......
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) => {
const data = await this.pandaSparkWebSocket.sendMessag({
content : this.text,
isLinkContext : false,
frame ({resultTextTemp, resultText}) {
sendMsg(resultTextTemp)
}
})
resolve(data.resultText)
})
}
destroy(){
if(this.pandaSparkWebSocket) {
this.pandaSparkWebSocket.destroy()
this.pandaSparkWebSocket = null
}
}
}
export default SparkDirective
\ No newline at end of file
import { request, event } from '@wisdom-utils/utils'
import { Suspense, lazy, useCallback, useEffect, useRef, useState } from 'react'
import Context , { contextData } from './config'
const PandaUi = lazy(() => import("./PandaUi"))
const SoundAi = props => {
const keyRef = useRef(null)
const [visible, setVisible] = useState(false)
const [globalConfig , setGlobalConfig] = useState(contextData())
useEffect(async () => {
try {
await getIsOpenConfig()
await getKey()
await getIsOpenConfig()
setVisible(true)
} catch (error) {
console.warn(error)
......@@ -25,7 +27,7 @@ const SoundAi = props => {
request("/PandaOMS/OMS/DataManger/GetDicConfigs", {
params: {
ParentName: "智能语音",
ChilName: "是否开启"
ChilName: "是否开启,是否开启人机交互"
}
}).then(res => {
if (res.code != 0) {
......@@ -42,6 +44,18 @@ const SoundAi = props => {
reject("字段开启字段值为空")
return
}
const spark = data.find(item => item.nodeName == "是否开启人机交互")
if (spark && spark.nodeValue == "开") {
setGlobalConfig(globalConfig => globalConfig.set("key", {
...keyRef.current,
isOpenSparkModel : true
}))
} else {
setGlobalConfig(globalConfig => globalConfig.set("key", {
...keyRef.current,
isOpenSparkModel : false
}))
}
resolve()
}).catch(error => {
reject(error)
......@@ -63,9 +77,11 @@ const SoundAi = props => {
return (<>
{
visible ? <Suspense fallback={<></>}>
<PandaUi keyRef={keyRef} />
</Suspense> : null
visible && globalConfig.get("key")? <Context.Provider value={{globalConfig , setGlobalConfig}}>
<Suspense fallback={<></>}>
<PandaUi />
</Suspense>
</Context.Provider> : null
}
</>)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment