Commit fe0d5ad2 authored by ali's avatar ali

fix: 格式化

parent 229f45cf
......@@ -45,11 +45,7 @@ export default class IPCs {
// open new window
ipcMain.on(
'openWindow',
async (
event,
url: string,
options: BrowserWindowConstructorOptions
) => {
async (event, url: string, options: BrowserWindowConstructorOptions) => {
const ops = Object.assign(
{},
{
......
......@@ -64,7 +64,7 @@ const asrItems = ref([
// 'Whisper Api'
])
const liveHosts = ref([
'laihua',
'laihua'
// 'http://111.229.216.162:9000',
// 'http://124.221.182.173:9000',
// 'http://110.42.214.59:9000',
......
......@@ -65,7 +65,7 @@ export class HwWebRTC extends EventEmitter {
constructor(id: string, log: 'none' | 'error' | 'warn' | 'info' | 'debug' = 'none') {
super()
this.elementId = id
window.HWLLSPlayer.setLogLevel(log);
window.HWLLSPlayer.setLogLevel(log)
}
/**
......
/* eslint-disable camelcase */
import http, { axiosInstance } from '@/renderer/utils/http'
import { JWT } from '@/renderer/utils/jwt';
import { JWT } from '@/renderer/utils/jwt'
import { HwWebRTC } from './HwWebRTC'
import { guid } from '@/renderer/utils/index'
import EventEmitter from 'EventEmitter'
......@@ -21,10 +21,9 @@ type LiveOptions = {
}
type SdkConfigDataParams = {
baseUrl: string;
baseSocketUrl: string;
};
baseUrl: string
baseSocketUrl: string
}
export class PhotoAnswer {
question = ''
......@@ -462,7 +461,7 @@ export class PhotoRole extends EventEmitter {
this._bindEvents()
if (this.host === 'laihua') {
this.host = await this.auth();
this.host = await this.auth()
}
await this.initLive()
......@@ -473,19 +472,19 @@ export class PhotoRole extends EventEmitter {
const token = JWT.encode(
JSON.stringify({ appId: '88ca56ac88257f14', iat: new Date().getTime() + 1000 * 30 }),
'92b39dd13b952bcd4ce1995473cc3302'
);
axiosInstance.defaults.headers.common.Authorization = `Bearer ${token}`;
)
axiosInstance.defaults.headers.common.Authorization = `Bearer ${token}`
const res = await http({ url: 'https://openapi.laihua.com/sdk/getConfig' });
const res = await http({ url: 'https://openapi.laihua.com/sdk/getConfig' })
const { code, data } = res as {
code: number | string;
data: SdkConfigDataParams | string;
};
code: number | string
data: SdkConfigDataParams | string
}
if (code !== 200) {
throw new Error(`${code}: ${ JSON.stringify(res) }`);
throw new Error(`${code}: ${JSON.stringify(res)}`)
}
return (data as SdkConfigDataParams).baseUrl;
return (data as SdkConfigDataParams).baseUrl
}
destroy() {
......
......@@ -61,11 +61,7 @@ async function init() {
return
}
if (
microphoneState.value === 'reply' &&
ans.playState === 'pause' &&
await checkSteps()
) {
if (microphoneState.value === 'reply' && ans.playState === 'pause' && (await checkSteps())) {
microphoneState.value = 'input'
}
})
......@@ -360,7 +356,6 @@ function initLLMSocket(): Promise<WebSocket> {
})
}
function endAudioInput() {
microphoneState.value = 'waitInput'
inputContext.mediaStream?.getTracks().forEach((track) => track.stop())
......@@ -375,32 +370,34 @@ function endAudioInput() {
}
const answerArray: { text: string; isLast: boolean }[] = []
const steps: Promise<string>[] = [];
const steps: Promise<string>[] = []
const checkSteps = async () => {
let count = 0;
let count = 0
for (let i = 0; i < steps.length; i++) {
try {
const res = await Promise.race([steps[i], new Promise((resolve) => setTimeout(() => resolve(false), 10))])
if (res === false) continue;
const res = await Promise.race([
steps[i],
new Promise((resolve) => setTimeout(() => resolve(false), 10))
])
if (res === false) continue
} catch (e) {
console.error(e)
}
count ++;
count++
if (count >= 2) {
return true
}
}
return false;
return false
}
const createStep = () => {
let stepResolve: (string) => void = () => {};
let stepReject: (string) => void = () => {};
let stepResolve: (string) => void = () => {}
let stepReject: (string) => void = () => {}
const pose = new Promise<string>((resolve, reject) => {
stepResolve = resolve;
stepReject = reject;
stepResolve = resolve
stepReject = reject
})
return { pose, stepResolve, stepReject }
......@@ -418,9 +415,9 @@ async function onQ(question: string) {
let isTime = true
let sliceAnswerLength = 10
answerArray.length = 0
steps.length = 0;
const { pose, stepResolve, stepReject } = createStep();
steps.push(pose);
steps.length = 0
const { pose, stepResolve, stepReject } = createStep()
steps.push(pose)
photoRole!.answerArgs = new PhotoAnswer()
ws.onmessage = (message) => {
......@@ -437,7 +434,7 @@ async function onQ(question: string) {
runTTSTask(answerArray)
inputContext.ws?.close()
console.log('----------------> answer: ', answer)
stepResolve('chat');
stepResolve('chat')
return
}
......@@ -474,8 +471,8 @@ async function runTTSTask(tasks: { text: string; isLast: boolean }[]) {
if (isTTSRunning) return
isTTSRunning = true
const { pose, stepResolve, stepReject } = createStep();
steps.push(pose);
const { pose, stepResolve, stepReject } = createStep()
steps.push(pose)
try {
while (tasks.length) {
......
......@@ -263,11 +263,11 @@ function endAudioInput() {
inputContext.voskWs.send('{"eof" : 1}')
inputContext.voskWs.close()
}
ttsAudios.length = 0;
inputContext.playingAudio?.pause();
videos[1].value?.pause();
videos[0].value?.pause();
isPlayRunning = false;
ttsAudios.length = 0
inputContext.playingAudio?.pause()
videos[1].value?.pause()
videos[0].value?.pause()
isPlayRunning = false
}
const canplay = () => {
......@@ -286,7 +286,8 @@ function loadVideo(url: string) {
}
async function qNLP(question: string) {
const resp = await (await fetch(`${settings.nlpHost}/api/v1/generate`, {
const resp = (await (
await fetch(`${settings.nlpHost}/api/v1/generate`, {
headers: {
accept: 'application/json, text/plain, */*',
'content-type': 'application/json'
......@@ -296,15 +297,16 @@ async function qNLP(question: string) {
}),
method: 'POST',
mode: 'cors'
})).json() as { results: {text: null | string}[] };
})
).json()) as { results: { text: null | string }[] }
if (resp.results[0].text === null) return '';
if (resp.results[0].text === null) return ''
for (let i = 0; i < role!.qa.length; i++) {
const { q, url } = role!.qa[i]
if (q.includes(resp.results[0].text)) {
return url;
};
return url
}
}
}
......@@ -338,7 +340,7 @@ async function onQ(question: string) {
ws.onmessage = (message) => {
if (microphoneState.value === 'input') {
return;
return
}
try {
......@@ -412,10 +414,9 @@ async function runTTSTask(tasks: string[]) {
// @ts-ignore
if (microphoneState.value === 'input') {
break;
break
}
const audio = new Audio(`file://${res[0].text}`)
audio.load()
ttsAudios.push(audio)
......@@ -447,7 +448,7 @@ async function runAudioPlay() {
runAudioPlay()
}
await audio.play()
inputContext.playingAudio = audio;
inputContext.playingAudio = audio
loadVideo(role!.playUrl)
videos[1].value!.loop = true
videos[1].value!.muted = true
......@@ -471,10 +472,10 @@ async function xfTTS(text: string) {
function down() {
if (microphoneState.value === 'reply') {
endAudioInput();
endAudioInput()
}
startVoskWsAudioInput();
startVoskWsAudioInput()
}
</script>
......@@ -494,10 +495,7 @@ function down() {
color="#fff"
variant="elevated"
size="x-large"
:disabled="
microphoneState === 'loading' ||
microphoneState === 'disabled'
"
:disabled="microphoneState === 'loading' || microphoneState === 'disabled'"
@pointerdown="down"
>
<v-icon v-if="microphoneState === 'waitInput'" icon="mdi-microphone"></v-icon>
......
import CryptoJS from 'crypto-js';
import CryptoJS from 'crypto-js'
export class JWT {
static sign(content: string, secret: string) {
const r = CryptoJS.HmacSHA256(content, secret);
const b = CryptoJS.enc.Base64.stringify(r);
return this.base64urlEscape(b);
const r = CryptoJS.HmacSHA256(content, secret)
const b = CryptoJS.enc.Base64.stringify(r)
return this.base64urlEscape(b)
}
static base64urlEscape(str: string) {
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
}
static toBase64(content: string) {
return btoa(content);
return btoa(content)
}
static encode(username: string, secret: string) {
const header = this.toBase64(JSON.stringify({ typ: 'JWT', alg: 'HS256' }));
const content = this.toBase64(username);
const sign = this.sign([header, content].join('.'), secret);
return [header, content, sign].join('.');
const header = this.toBase64(JSON.stringify({ typ: 'JWT', alg: 'HS256' }))
const content = this.toBase64(username)
const sign = this.sign([header, content].join('.'), secret)
return [header, content, sign].join('.')
}
static decode(token: string, secret: string) {
const [header, content, sign] = token.split('.');
const newSign = this.sign([header, content].join('.'), secret);
const [header, content, sign] = token.split('.')
const newSign = this.sign([header, content].join('.'), secret)
if (sign === newSign) {
return atob(content);
return atob(content)
} else {
throw new Error('被篡改');
throw new Error('被篡改')
}
}
}
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