Skip to content

Commit 02bca2f

Browse files
author
weilei
committed
feat: 添加流处理和语音识别相关的 mixins,支持 SSE 数据解析和中断功能
1 parent 62d1313 commit 02bca2f

File tree

5 files changed

+981
-7
lines changed

5 files changed

+981
-7
lines changed

packages/element-ui-x/src/index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import ElXTypewriter from './components/Typewriter/index.js';
1+
import ElXAttachments from './components/Attachments/index.js';
22
import ElXBubble from './components/Bubble/index.js';
33
import ElXBubbleList from './components/BubbleList/index.js';
4-
import ElXWelcome from './components/Welcome/index.js';
5-
import ElXPrompts from './components/Prompts/index.js';
64
import ElXConversations from './components/Conversations/index.js';
7-
import ElXThinking from './components/Thinking/index.js';
5+
import ElXFilesCard from './components/FilesCard/index.js';
6+
import ElXPrompts from './components/Prompts/index.js';
7+
import ElXSender from './components/Sender/index.js';
88
import ElXThink from './components/Think/index.js';
9+
import ElXThinking from './components/Thinking/index.js';
910
import ElXThoughtChain from './components/ThoughtChain/index.js';
10-
import ElXSender from './components/Sender/index.js';
11-
import ElXFilesCard from './components/FilesCard/index.js';
12-
import ElXAttachments from './components/Attachments/index.js';
11+
import ElXTypewriter from './components/Typewriter/index.js';
12+
import ElXWelcome from './components/Welcome/index.js';
13+
14+
// 导入所有 mixins
15+
import * as mixins from './mixins';
1316

1417
const components = [
1518
ElXTypewriter,
@@ -35,5 +38,8 @@ const install = function (Vue) {
3538
export default {
3639
version: '1.0.0',
3740
install,
41+
mixins,
3842
...components,
3943
};
44+
// 单独导出 mixins 以便按需引入
45+
export { mixins };
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Mixins 入口文件
3+
*/
4+
5+
import recordMixin from './recordMixin';
6+
import { createSendUtils, sendMixin, XRequest } from './sendMixin';
7+
import {
8+
createStreamUtils,
9+
DEFAULT_KV_SEPARATOR,
10+
DEFAULT_PART_SEPARATOR,
11+
DEFAULT_STREAM_SEPARATOR,
12+
isValidString,
13+
splitPart,
14+
splitStream,
15+
streamMixin,
16+
XStream,
17+
} from './streamMixin';
18+
19+
export {
20+
createSendUtils,
21+
createStreamUtils,
22+
DEFAULT_KV_SEPARATOR,
23+
DEFAULT_PART_SEPARATOR,
24+
DEFAULT_STREAM_SEPARATOR,
25+
isValidString,
26+
recordMixin,
27+
sendMixin,
28+
splitPart,
29+
splitStream,
30+
streamMixin,
31+
XRequest,
32+
XStream,
33+
};
34+
35+
export default {
36+
recordMixin,
37+
sendMixin,
38+
XRequest,
39+
createSendUtils,
40+
streamMixin,
41+
createStreamUtils,
42+
XStream,
43+
splitStream,
44+
splitPart,
45+
isValidString,
46+
DEFAULT_STREAM_SEPARATOR,
47+
DEFAULT_PART_SEPARATOR,
48+
DEFAULT_KV_SEPARATOR,
49+
};
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @description 提供语音识别的混入,允许语音输入并处理开始、结束、结果及错误等各种事件。
3+
*
4+
* @mixin recordMixin
5+
*/
6+
7+
export default {
8+
data() {
9+
return {
10+
// 语音识别状态
11+
recordLoading: false,
12+
// 识别结果
13+
recordValue: '',
14+
// 识别实例
15+
recordRecognition: null,
16+
// 配置选项
17+
recordOptions: {
18+
onError: null,
19+
onStart: null,
20+
onEnd: null,
21+
onResult: null,
22+
},
23+
};
24+
},
25+
26+
methods: {
27+
/**
28+
* 初始化语音识别配置
29+
* @param {Object} options - 配置选项
30+
* @param {Function} [options.onError] - 错误回调
31+
* @param {Function} [options.onStart] - 开始回调
32+
* @param {Function} [options.onEnd] - 结束回调
33+
* @param {Function} [options.onResult] - 结果回调
34+
*/
35+
initRecord(options = {}) {
36+
this.recordOptions = {
37+
...this.recordOptions,
38+
...options,
39+
};
40+
},
41+
42+
/**
43+
* 开始语音识别
44+
*/
45+
startRecord() {
46+
if ('webkitSpeechRecognition' in window) {
47+
this.recordRecognition = new window.webkitSpeechRecognition();
48+
this.recordRecognition.continuous = true;
49+
this.recordRecognition.interimResults = true;
50+
this.recordRecognition.lang = 'zh-CN';
51+
52+
this.recordRecognition.onstart = () => {
53+
this.recordLoading = true;
54+
this.recordValue = '';
55+
if (this.recordOptions.onStart) {
56+
this.recordOptions.onStart();
57+
}
58+
};
59+
60+
this.recordRecognition.onend = () => {
61+
this.recordLoading = false;
62+
if (this.recordOptions.onEnd) {
63+
this.recordOptions.onEnd(this.recordValue);
64+
}
65+
};
66+
67+
this.recordRecognition.onerror = e => {
68+
this.recordLoading = false;
69+
if (this.recordOptions.onError) {
70+
this.recordOptions.onError(e);
71+
}
72+
};
73+
74+
this.recordRecognition.onresult = e => {
75+
let results = '';
76+
for (let i = 0; i <= e.resultIndex; i++) {
77+
results += e.results[i][0].transcript;
78+
}
79+
this.recordValue = results;
80+
if (this.recordOptions.onResult) {
81+
this.recordOptions.onResult(results);
82+
}
83+
};
84+
85+
this.recordRecognition.start();
86+
} else {
87+
const error = {
88+
code: -1,
89+
message: 'The current browser does not support voice recognition',
90+
};
91+
if (this.recordOptions.onError) {
92+
this.recordOptions.onError(error);
93+
}
94+
}
95+
},
96+
97+
/**
98+
* 停止语音识别
99+
*/
100+
stopRecord() {
101+
if (this.recordRecognition) {
102+
this.recordRecognition.stop();
103+
}
104+
},
105+
106+
/**
107+
* 清理语音识别资源
108+
*/
109+
cleanupRecord() {
110+
this.stopRecord();
111+
this.recordRecognition = null;
112+
},
113+
},
114+
115+
beforeDestroy() {
116+
this.cleanupRecord();
117+
},
118+
};

0 commit comments

Comments
 (0)