@ray-js/t-agent-plugin-aistream 0.2.0-beta-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README-zh_CN.md +12 -0
- package/README.md +12 -0
- package/dist/AIStreamTypes.d.ts +1413 -0
- package/dist/AIStreamTypes.js +216 -0
- package/dist/ChatHistoryStore.d.ts +66 -0
- package/dist/ChatHistoryStore.js +160 -0
- package/dist/global.d.ts +4 -0
- package/dist/global.js +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/polyfill.d.ts +1 -0
- package/dist/polyfill.js +8 -0
- package/dist/utils/AIStream.d.ts +137 -0
- package/dist/utils/AIStream.js +486 -0
- package/dist/utils/abort.d.ts +38 -0
- package/dist/utils/abort.js +177 -0
- package/dist/utils/actions.d.ts +48 -0
- package/dist/utils/actions.js +76 -0
- package/dist/utils/apis.d.ts +15 -0
- package/dist/utils/apis.js +10 -0
- package/dist/utils/defaultMock.d.ts +1 -0
- package/dist/utils/defaultMock.js +429 -0
- package/dist/utils/index.d.ts +11 -0
- package/dist/utils/index.js +11 -0
- package/dist/utils/logger.d.ts +2 -0
- package/dist/utils/logger.js +3 -0
- package/dist/utils/mock.d.ts +48 -0
- package/dist/utils/mock.js +72 -0
- package/dist/utils/observer.d.ts +61 -0
- package/dist/utils/observer.js +152 -0
- package/dist/utils/parsers.d.ts +10 -0
- package/dist/utils/parsers.js +13 -0
- package/dist/utils/promisify.d.ts +18 -0
- package/dist/utils/promisify.js +82 -0
- package/dist/utils/sendMessage.d.ts +21 -0
- package/dist/utils/sendMessage.js +241 -0
- package/dist/utils/ttt.d.ts +99 -0
- package/dist/utils/ttt.js +97 -0
- package/dist/utils/url.d.ts +11 -0
- package/dist/utils/url.js +31 -0
- package/dist/utils/version.d.ts +1 -0
- package/dist/utils/version.js +63 -0
- package/dist/withAIStream.d.ts +64 -0
- package/dist/withAIStream.js +420 -0
- package/package.json +39 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { InputBlock } from '../AIStreamTypes';
|
|
2
|
+
export interface TTTActionOpenRoute {
|
|
3
|
+
type: 'openRoute';
|
|
4
|
+
url: string;
|
|
5
|
+
}
|
|
6
|
+
export interface TTTActionOpenMiniApp {
|
|
7
|
+
type: 'openMiniApp';
|
|
8
|
+
appId?: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
extraData?: Record<string, any>;
|
|
11
|
+
shortLink?: string;
|
|
12
|
+
position?: string;
|
|
13
|
+
aiPtChannel?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface TTTActionOpenH5 {
|
|
16
|
+
type: 'openH5';
|
|
17
|
+
url: string;
|
|
18
|
+
title?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface TTTActionSendMessage {
|
|
21
|
+
type: 'sendMessage';
|
|
22
|
+
blocks: InputBlock[];
|
|
23
|
+
sendImmediately?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface TTTActionSendSkill {
|
|
26
|
+
type: 'sendSkill';
|
|
27
|
+
block?: string;
|
|
28
|
+
options: {
|
|
29
|
+
domain: string;
|
|
30
|
+
intent: string;
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export interface TTTActionBuildIn {
|
|
35
|
+
type: 'buildIn';
|
|
36
|
+
name: 'questionnaire' | 'homeLocation' | string;
|
|
37
|
+
data?: any;
|
|
38
|
+
}
|
|
39
|
+
export type TTTAction = TTTActionOpenRoute | TTTActionOpenMiniApp | TTTActionOpenH5 | TTTActionSendMessage | TTTActionSendSkill | TTTActionBuildIn;
|
|
40
|
+
export declare function runTTTAction(action: TTTAction, options: {
|
|
41
|
+
onSendSkills?: (block: string, options: {
|
|
42
|
+
domain: string;
|
|
43
|
+
intent: string;
|
|
44
|
+
[key: string]: string;
|
|
45
|
+
}) => Promise<void> | void;
|
|
46
|
+
onSendMessages?: (blocks: InputBlock[], sendImmediately: boolean) => Promise<void> | void;
|
|
47
|
+
onBuildIn?: (name: string, data: any) => Promise<void> | void;
|
|
48
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import "core-js/modules/es.regexp.exec.js";
|
|
2
|
+
import { canIUseRouter, navigateToMiniProgram, openInnerH5, router } from './ttt';
|
|
3
|
+
import logger from './logger';
|
|
4
|
+
import { parseMiniAppURL } from './url';
|
|
5
|
+
export async function runTTTAction(action, options) {
|
|
6
|
+
switch (action.type) {
|
|
7
|
+
case 'openRoute':
|
|
8
|
+
{
|
|
9
|
+
if (!action.url) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (/^http(s)?:\/\//.test(action.url)) {
|
|
13
|
+
await openInnerH5({
|
|
14
|
+
url: action.url
|
|
15
|
+
});
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const mini = parseMiniAppURL(action.url);
|
|
19
|
+
if (mini) {
|
|
20
|
+
await navigateToMiniProgram(mini);
|
|
21
|
+
} else {
|
|
22
|
+
const {
|
|
23
|
+
result
|
|
24
|
+
} = await canIUseRouter({
|
|
25
|
+
url: action.url
|
|
26
|
+
});
|
|
27
|
+
if (result) {
|
|
28
|
+
await router({
|
|
29
|
+
url: action.url
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
throw new Error('openRoute not allowed');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
case 'openMiniApp':
|
|
38
|
+
await navigateToMiniProgram({
|
|
39
|
+
appId: action.appId,
|
|
40
|
+
path: action.path,
|
|
41
|
+
shortLink: action.shortLink,
|
|
42
|
+
extraData: action.extraData,
|
|
43
|
+
position: action.position,
|
|
44
|
+
aiPtChannel: action.aiPtChannel
|
|
45
|
+
});
|
|
46
|
+
break;
|
|
47
|
+
case 'openH5':
|
|
48
|
+
await openInnerH5({
|
|
49
|
+
url: action.url,
|
|
50
|
+
title: action.title
|
|
51
|
+
});
|
|
52
|
+
break;
|
|
53
|
+
case 'sendMessage':
|
|
54
|
+
if (options.onSendMessages) {
|
|
55
|
+
var _action$sendImmediate;
|
|
56
|
+
await options.onSendMessages(action.blocks, (_action$sendImmediate = action.sendImmediately) !== null && _action$sendImmediate !== void 0 ? _action$sendImmediate : false);
|
|
57
|
+
} else {
|
|
58
|
+
logger.error('onSendMessages not provided');
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
case 'sendSkill':
|
|
62
|
+
if (options.onSendSkills) {
|
|
63
|
+
await options.onSendSkills(action.block, action.options);
|
|
64
|
+
} else {
|
|
65
|
+
logger.error('onSendSkills not provided');
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
case 'buildIn':
|
|
69
|
+
if (options.onBuildIn) {
|
|
70
|
+
await options.onBuildIn(action.name, action.data);
|
|
71
|
+
} else {
|
|
72
|
+
logger.error('onBuildIn not provided');
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface ItAgentMessageAppraise {
|
|
2
|
+
aiPtChannel: string;
|
|
3
|
+
/** 小程序 ID */
|
|
4
|
+
miniProgramId: string;
|
|
5
|
+
/** 请求 ID, 消息id */
|
|
6
|
+
requestId: string;
|
|
7
|
+
/** 评价(1-有帮助,2-无帮助) */
|
|
8
|
+
approveStatus: 1 | 2;
|
|
9
|
+
}
|
|
10
|
+
/** 聊天智能体消息的反馈评价 */
|
|
11
|
+
export declare const tAgentMessageAppraise: (params: ItAgentMessageAppraise) => Promise<{
|
|
12
|
+
thingjson?: any;
|
|
13
|
+
data: string;
|
|
14
|
+
}>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { apiRequestByHighway } from './ttt';
|
|
2
|
+
import { HighwayMethod } from '../AIStreamTypes';
|
|
3
|
+
/** 聊天智能体消息的反馈评价 */
|
|
4
|
+
export const tAgentMessageAppraise = params => {
|
|
5
|
+
return apiRequestByHighway({
|
|
6
|
+
api: '/v1.0/m/life/ai/agent/chat-panel-mini/mini/history/appraise',
|
|
7
|
+
data: params,
|
|
8
|
+
method: HighwayMethod.POST
|
|
9
|
+
});
|
|
10
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import "core-js/modules/es.array.sort.js";
|
|
2
|
+
import "core-js/modules/es.json.stringify.js";
|
|
3
|
+
import "core-js/modules/es.regexp.exec.js";
|
|
4
|
+
import "core-js/modules/esnext.iterator.constructor.js";
|
|
5
|
+
import "core-js/modules/esnext.iterator.filter.js";
|
|
6
|
+
import "core-js/modules/esnext.iterator.map.js";
|
|
7
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
8
|
+
import { mock } from './mock';
|
|
9
|
+
import { EmitterEvent, generateId } from '@ray-js/t-agent';
|
|
10
|
+
import { BizCode, EventType, ReceivedTextPacketEof, ReceivedTextPacketType, StreamFlag } from '../AIStreamTypes';
|
|
11
|
+
import AbortController from './abort';
|
|
12
|
+
function splitString(input) {
|
|
13
|
+
return input.match(/[a-zA-Z0-9]+\s*|[\u4e00-\u9fff]+\s*|[^\w\s\u4e00-\u9fff]+\s*|[\s]+/g) || [];
|
|
14
|
+
}
|
|
15
|
+
mock.data.set('sessionMap', new Map());
|
|
16
|
+
const getSession = (sessionId, eventId) => {
|
|
17
|
+
const map = mock.data.get('sessionMap');
|
|
18
|
+
const session = map.get(sessionId);
|
|
19
|
+
if (!session) {
|
|
20
|
+
throw new Error('session not exists');
|
|
21
|
+
}
|
|
22
|
+
if (eventId) {
|
|
23
|
+
if (!session.currentEvent) {
|
|
24
|
+
throw new Error('event not exists');
|
|
25
|
+
}
|
|
26
|
+
if (session.currentEvent.eventId !== eventId) {
|
|
27
|
+
throw new Error('eventId mismatch');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return session;
|
|
31
|
+
};
|
|
32
|
+
mock.hooks.hook('getMiniAppConfig', context => {
|
|
33
|
+
context.result = {
|
|
34
|
+
config: {}
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
mock.hooks.hook('connect', context => {
|
|
38
|
+
const {
|
|
39
|
+
options
|
|
40
|
+
} = context;
|
|
41
|
+
const key = "connection-".concat(options.clientType, "-").concat(options.deviceId || '');
|
|
42
|
+
if (!mock.data.has(key)) {
|
|
43
|
+
const connectionId = generateId();
|
|
44
|
+
mock.data.set(key, {
|
|
45
|
+
connectionId
|
|
46
|
+
});
|
|
47
|
+
mock.data.set("connection-".concat(connectionId), key);
|
|
48
|
+
}
|
|
49
|
+
context.result = mock.data.get(key);
|
|
50
|
+
});
|
|
51
|
+
mock.hooks.hook('disconnect', context => {
|
|
52
|
+
const {
|
|
53
|
+
options: {
|
|
54
|
+
connectionId
|
|
55
|
+
}
|
|
56
|
+
} = context;
|
|
57
|
+
const key = mock.data.get("connection-".concat(connectionId));
|
|
58
|
+
mock.data.delete(key);
|
|
59
|
+
mock.data.delete("connection-".concat(connectionId));
|
|
60
|
+
});
|
|
61
|
+
mock.hooks.hook('queryAgentToken', context => {
|
|
62
|
+
context.result = {
|
|
63
|
+
agentToken: generateId(),
|
|
64
|
+
bizConfig: {
|
|
65
|
+
bizCode: BizCode.CHAT,
|
|
66
|
+
sendData: ['audio', 'video', 'text', 'image'],
|
|
67
|
+
revData: ['text', 'audio']
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
const dispatch = (type, detail) => {
|
|
72
|
+
mock.emitter.dispatchEvent(new EmitterEvent(type, {
|
|
73
|
+
detail
|
|
74
|
+
}));
|
|
75
|
+
};
|
|
76
|
+
mock.hooks.hook('createSession', context => {
|
|
77
|
+
const map = mock.data.get('sessionMap');
|
|
78
|
+
const session = {
|
|
79
|
+
sessionId: generateId(),
|
|
80
|
+
sendDataChannels: ['audio', 'video', 'text', 'image'],
|
|
81
|
+
revDataChannels: ['text', 'audio'],
|
|
82
|
+
currentEvent: null,
|
|
83
|
+
replyText: function (streamFlag) {
|
|
84
|
+
let data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
85
|
+
dispatch('onTextReceived', {
|
|
86
|
+
dataChannel: 'text',
|
|
87
|
+
sessionIdList: [session.sessionId],
|
|
88
|
+
streamFlag,
|
|
89
|
+
text: data ? JSON.stringify(data) : ''
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
replyEvent: eventType => {
|
|
93
|
+
if (!session.currentEvent) {
|
|
94
|
+
throw new Error('replyEvent event not exists');
|
|
95
|
+
}
|
|
96
|
+
dispatch('onEventReceived', {
|
|
97
|
+
eventId: session.currentEvent.eventId,
|
|
98
|
+
sessionId: session.sessionId,
|
|
99
|
+
eventType
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
map.set(session.sessionId, session);
|
|
104
|
+
context.result = {
|
|
105
|
+
sessionId: session.sessionId,
|
|
106
|
+
sendDataChannels: session.sendDataChannels,
|
|
107
|
+
revDataChannels: session.revDataChannels
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
mock.hooks.hook('closeSession', context => {
|
|
111
|
+
const map = mock.data.get('sessionMap');
|
|
112
|
+
const sessionId = context.options.sessionId;
|
|
113
|
+
map.delete(sessionId);
|
|
114
|
+
context.result = true;
|
|
115
|
+
});
|
|
116
|
+
mock.hooks.hook('sendEventStart', context => {
|
|
117
|
+
const {
|
|
118
|
+
options
|
|
119
|
+
} = context;
|
|
120
|
+
const {
|
|
121
|
+
sessionId
|
|
122
|
+
} = options;
|
|
123
|
+
const session = getSession(sessionId);
|
|
124
|
+
if (session.currentEvent) {
|
|
125
|
+
throw new Error('sendEventStart already in event');
|
|
126
|
+
}
|
|
127
|
+
const eventId = generateId();
|
|
128
|
+
session.currentEvent = {
|
|
129
|
+
asrStatus: {
|
|
130
|
+
controller: new AbortController(),
|
|
131
|
+
promise: Promise.resolve()
|
|
132
|
+
},
|
|
133
|
+
eventId,
|
|
134
|
+
data: [],
|
|
135
|
+
controller: new AbortController()
|
|
136
|
+
};
|
|
137
|
+
context.result = {
|
|
138
|
+
eventId
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
mock.hooks.hook('registerRecordAmplitudes', context => {
|
|
142
|
+
mock.data.set('recordAmplitudesCount', context.options.count);
|
|
143
|
+
context.result = true;
|
|
144
|
+
});
|
|
145
|
+
mock.hooks.hook('unregisterVoiceAmplitudes', context => {
|
|
146
|
+
mock.data.delete('recordAmplitudesCount');
|
|
147
|
+
context.result = true;
|
|
148
|
+
});
|
|
149
|
+
mock.hooks.hook('sendEventEnd', context => {
|
|
150
|
+
const session = getSession(context.options.sessionId, context.options.eventId);
|
|
151
|
+
context.result = true;
|
|
152
|
+
(async () => {
|
|
153
|
+
const ctx = {
|
|
154
|
+
data: session.currentEvent.data,
|
|
155
|
+
responseText: ''
|
|
156
|
+
};
|
|
157
|
+
await session.currentEvent.asrStatus.promise;
|
|
158
|
+
await mock.hooks.callHook('sendToAIStream', ctx);
|
|
159
|
+
const text = ctx.responseText || '⚠️ No mock text response matched!';
|
|
160
|
+
const words = splitString(text);
|
|
161
|
+
session.replyEvent(EventType.EVENT_START);
|
|
162
|
+
await mock.sleep(500);
|
|
163
|
+
const bizId = generateId();
|
|
164
|
+
session.replyText(StreamFlag.START);
|
|
165
|
+
for (const word of words) {
|
|
166
|
+
await mock.sleep(100);
|
|
167
|
+
session.replyText(StreamFlag.IN_PROGRESS, {
|
|
168
|
+
bizType: ReceivedTextPacketType.NLG,
|
|
169
|
+
eof: ReceivedTextPacketEof.CONTINUE,
|
|
170
|
+
bizId,
|
|
171
|
+
data: {
|
|
172
|
+
content: word,
|
|
173
|
+
appendMode: 'append',
|
|
174
|
+
finish: false
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
session.replyText(StreamFlag.IN_PROGRESS, {
|
|
179
|
+
bizType: ReceivedTextPacketType.NLG,
|
|
180
|
+
eof: ReceivedTextPacketEof.END,
|
|
181
|
+
bizId,
|
|
182
|
+
data: {
|
|
183
|
+
content: '',
|
|
184
|
+
appendMode: 'append',
|
|
185
|
+
finish: true
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
await mock.sleep(10);
|
|
189
|
+
session.replyText(StreamFlag.END);
|
|
190
|
+
await mock.sleep(500);
|
|
191
|
+
session.replyEvent(EventType.EVENT_END);
|
|
192
|
+
session.currentEvent = null;
|
|
193
|
+
})();
|
|
194
|
+
});
|
|
195
|
+
mock.hooks.hook('sendEventPayloadEnd', context => {
|
|
196
|
+
getSession(context.options.sessionId, context.options.eventId);
|
|
197
|
+
context.result = true;
|
|
198
|
+
});
|
|
199
|
+
mock.hooks.hook('sendEventChatBreak', context => {
|
|
200
|
+
const session = getSession(context.options.sessionId, context.options.eventId);
|
|
201
|
+
session.currentEvent.controller.abort(new Error('sendEventChatBreak'));
|
|
202
|
+
session.currentEvent = null;
|
|
203
|
+
context.result = true;
|
|
204
|
+
});
|
|
205
|
+
mock.hooks.hook('startRecordAndSendAudioData', context => {
|
|
206
|
+
const session = getSession(context.options.sessionId);
|
|
207
|
+
if (!session.currentEvent) {
|
|
208
|
+
throw new Error('startRecordAndSendAudioData event not exists');
|
|
209
|
+
}
|
|
210
|
+
context.result = true;
|
|
211
|
+
(async () => {
|
|
212
|
+
const {
|
|
213
|
+
controller
|
|
214
|
+
} = session.currentEvent.asrStatus;
|
|
215
|
+
const count = mock.data.get('recordAmplitudesCount');
|
|
216
|
+
if (count) {
|
|
217
|
+
const id = setInterval(() => {
|
|
218
|
+
dispatch('onRecordAmplitudes', {
|
|
219
|
+
amplitudes: new Array(count).fill(0).map(() => Math.random())
|
|
220
|
+
});
|
|
221
|
+
}, 100);
|
|
222
|
+
controller.signal.addEventListener('abort', () => {
|
|
223
|
+
clearInterval(id);
|
|
224
|
+
mock.data.delete('recordAmplitudesCountId');
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
const result = {
|
|
228
|
+
responseText: ''
|
|
229
|
+
};
|
|
230
|
+
await mock.hooks.callHook('asrDetection', result);
|
|
231
|
+
const responseText = result.responseText || '⚠️ No mock asr response matched!';
|
|
232
|
+
const bizId = generateId();
|
|
233
|
+
let finishResolve;
|
|
234
|
+
session.currentEvent.asrStatus.promise = new Promise(resolve => {
|
|
235
|
+
finishResolve = resolve;
|
|
236
|
+
});
|
|
237
|
+
const parts = splitString(responseText);
|
|
238
|
+
let text = '';
|
|
239
|
+
controller.signal.addEventListener('abort', async () => {
|
|
240
|
+
var _finishResolve;
|
|
241
|
+
await mock.sleep(300);
|
|
242
|
+
session.replyText(StreamFlag.IN_PROGRESS, {
|
|
243
|
+
bizType: ReceivedTextPacketType.ASR,
|
|
244
|
+
eof: ReceivedTextPacketEof.END,
|
|
245
|
+
bizId,
|
|
246
|
+
data: {
|
|
247
|
+
text
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
await mock.sleep(100);
|
|
251
|
+
session.replyText(StreamFlag.END);
|
|
252
|
+
(_finishResolve = finishResolve) === null || _finishResolve === void 0 || _finishResolve();
|
|
253
|
+
});
|
|
254
|
+
await mock.sleep(100);
|
|
255
|
+
if (controller.signal.aborted) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
session.replyText(StreamFlag.START, {
|
|
259
|
+
bizType: ReceivedTextPacketType.ASR,
|
|
260
|
+
eof: ReceivedTextPacketEof.CONTINUE,
|
|
261
|
+
bizId,
|
|
262
|
+
data: {
|
|
263
|
+
text
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
for (const part of parts) {
|
|
267
|
+
if (controller.signal.aborted) {
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
await mock.sleep(300);
|
|
271
|
+
if (controller.signal.aborted) {
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
text += part;
|
|
275
|
+
session.replyText(StreamFlag.IN_PROGRESS, {
|
|
276
|
+
bizType: ReceivedTextPacketType.ASR,
|
|
277
|
+
eof: ReceivedTextPacketEof.CONTINUE,
|
|
278
|
+
bizId,
|
|
279
|
+
data: {
|
|
280
|
+
text
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
})();
|
|
285
|
+
});
|
|
286
|
+
mock.hooks.hook('stopRecordAndSendAudioData', context => {
|
|
287
|
+
const session = getSession(context.options.sessionId);
|
|
288
|
+
if (!session.currentEvent) {
|
|
289
|
+
throw new Error('stopRecordAndSendAudioData event not exists');
|
|
290
|
+
}
|
|
291
|
+
session.currentEvent.asrStatus.controller.abort(new Error('stopRecordAndSendAudioData'));
|
|
292
|
+
context.result = true;
|
|
293
|
+
});
|
|
294
|
+
mock.hooks.hook('startRecordAndSendVideoData', context => {
|
|
295
|
+
context.result = true;
|
|
296
|
+
});
|
|
297
|
+
mock.hooks.hook('stopRecordAndSendVideoData', context => {
|
|
298
|
+
context.result = true;
|
|
299
|
+
});
|
|
300
|
+
mock.hooks.hook('sendImageData', context => {
|
|
301
|
+
const session = getSession(context.options.sessionId);
|
|
302
|
+
if (!session.currentEvent) {
|
|
303
|
+
throw new Error('sendImageData event not exists');
|
|
304
|
+
}
|
|
305
|
+
session.currentEvent.data.push({
|
|
306
|
+
type: 'image',
|
|
307
|
+
path: context.options.path,
|
|
308
|
+
userData: context.options.userData
|
|
309
|
+
});
|
|
310
|
+
context.result = true;
|
|
311
|
+
});
|
|
312
|
+
mock.hooks.hook('sendTextData', context => {
|
|
313
|
+
context.result = true;
|
|
314
|
+
const session = getSession(context.options.sessionId);
|
|
315
|
+
if (!session.currentEvent) {
|
|
316
|
+
throw new Error('sendTextData event not exists');
|
|
317
|
+
}
|
|
318
|
+
session.currentEvent.data.push({
|
|
319
|
+
type: 'text',
|
|
320
|
+
text: context.options.text,
|
|
321
|
+
userData: context.options.userData
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
const filterRecords = query => {
|
|
325
|
+
var _query$sortType;
|
|
326
|
+
const rows = mock.getRecords().filter(row => {
|
|
327
|
+
var _query$id, _query$bizCode, _query$solutionCode, _query$devId, _query$homeId, _query$type, _query$index, _query$index2, _query$index3;
|
|
328
|
+
const item = row.record;
|
|
329
|
+
if ((_query$id = query.id) !== null && _query$id !== void 0 && _query$id.length && !query.id.includes(item.id)) {
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
if ((_query$bizCode = query.bizCode) !== null && _query$bizCode !== void 0 && _query$bizCode.length && (!item.bizCode || !query.bizCode.includes(item.bizCode))) {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
if ((_query$solutionCode = query.solutionCode) !== null && _query$solutionCode !== void 0 && _query$solutionCode.length && (!item.solutionCode || !query.solutionCode.includes(item.solutionCode))) {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
if ((_query$devId = query.devId) !== null && _query$devId !== void 0 && _query$devId.length && (!item.devId || !query.devId.includes(item.devId))) {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
if ((_query$homeId = query.homeId) !== null && _query$homeId !== void 0 && _query$homeId.length && (item.homeId === undefined || !query.homeId.includes(item.homeId))) {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
if ((_query$type = query.type) !== null && _query$type !== void 0 && _query$type.length && (!item.type || !query.type.includes(item.type))) {
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
if ((_query$index = query.index) !== null && _query$index !== void 0 && _query$index.length && (!item.index || !query.index.includes(item.index))) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
if ((_query$index2 = query.index1) !== null && _query$index2 !== void 0 && _query$index2.length && (!item.index1 || !query.index1.includes(item.index1))) {
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
if ((_query$index3 = query.index2) !== null && _query$index3 !== void 0 && _query$index3.length && (!item.index2 || !query.index2.includes(item.index2))) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
return true;
|
|
357
|
+
});
|
|
358
|
+
const sortType = (_query$sortType = query.sortType) !== null && _query$sortType !== void 0 ? _query$sortType : 0;
|
|
359
|
+
const result = rows.map(row => row.record).sort((a, b) => {
|
|
360
|
+
return sortType === 0 ? b.id - a.id : a.id - b.id;
|
|
361
|
+
});
|
|
362
|
+
if (query.offset) {
|
|
363
|
+
return result.slice(query.offset, query.limit ? query.offset + query.limit : undefined);
|
|
364
|
+
}
|
|
365
|
+
if (query.limit && query.limit > 0) {
|
|
366
|
+
return result.slice(0, query.limit);
|
|
367
|
+
}
|
|
368
|
+
return result;
|
|
369
|
+
};
|
|
370
|
+
mock.hooks.hook('queryRecordList', context => {
|
|
371
|
+
const options = context.options;
|
|
372
|
+
|
|
373
|
+
// 默认倒序
|
|
374
|
+
const records = filterRecords(options);
|
|
375
|
+
context.result = {
|
|
376
|
+
records,
|
|
377
|
+
total: records.length
|
|
378
|
+
};
|
|
379
|
+
});
|
|
380
|
+
mock.hooks.hook('deleteRecordList', context => {
|
|
381
|
+
const options = context.options;
|
|
382
|
+
const records = filterRecords(options);
|
|
383
|
+
records.map(_ref => {
|
|
384
|
+
let {
|
|
385
|
+
id
|
|
386
|
+
} = _ref;
|
|
387
|
+
return mock.deleteRecord(id);
|
|
388
|
+
});
|
|
389
|
+
context.result = true;
|
|
390
|
+
});
|
|
391
|
+
mock.hooks.hook('insertRecord', context => {
|
|
392
|
+
const options = context.options;
|
|
393
|
+
const {
|
|
394
|
+
bizCode,
|
|
395
|
+
solutionCode,
|
|
396
|
+
devId,
|
|
397
|
+
homeId,
|
|
398
|
+
type,
|
|
399
|
+
index,
|
|
400
|
+
index1,
|
|
401
|
+
index2,
|
|
402
|
+
data,
|
|
403
|
+
value,
|
|
404
|
+
value1,
|
|
405
|
+
value2
|
|
406
|
+
} = options;
|
|
407
|
+
const id = mock.getId();
|
|
408
|
+
mock.setRecord({
|
|
409
|
+
id,
|
|
410
|
+
record: {
|
|
411
|
+
id,
|
|
412
|
+
bizCode,
|
|
413
|
+
solutionCode,
|
|
414
|
+
devId,
|
|
415
|
+
homeId,
|
|
416
|
+
type,
|
|
417
|
+
index,
|
|
418
|
+
index1,
|
|
419
|
+
index2,
|
|
420
|
+
data,
|
|
421
|
+
value,
|
|
422
|
+
value1,
|
|
423
|
+
value2
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
context.result = {
|
|
427
|
+
id
|
|
428
|
+
};
|
|
429
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import './defaultMock';
|
|
2
|
+
export * from './abort';
|
|
3
|
+
export * from './logger';
|
|
4
|
+
export * from './mock';
|
|
5
|
+
export * from './promisify';
|
|
6
|
+
export * from './ttt';
|
|
7
|
+
export * from './url';
|
|
8
|
+
export * from './AIStream';
|
|
9
|
+
export * from './observer';
|
|
10
|
+
export * from './sendMessage';
|
|
11
|
+
export * from './version';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import './defaultMock';
|
|
2
|
+
export * from './abort';
|
|
3
|
+
export * from './logger';
|
|
4
|
+
export * from './mock';
|
|
5
|
+
export * from './promisify';
|
|
6
|
+
export * from './ttt';
|
|
7
|
+
export * from './url';
|
|
8
|
+
export * from './AIStream';
|
|
9
|
+
export * from './observer';
|
|
10
|
+
export * from './sendMessage';
|
|
11
|
+
export * from './version';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Emitter } from '@ray-js/t-agent';
|
|
2
|
+
import { Attribute } from '../AIStreamTypes';
|
|
3
|
+
interface TTTCallContext {
|
|
4
|
+
options: any;
|
|
5
|
+
result: any;
|
|
6
|
+
}
|
|
7
|
+
interface SendToAIStreamContext {
|
|
8
|
+
data: Array<{
|
|
9
|
+
type: 'text';
|
|
10
|
+
text: string;
|
|
11
|
+
userData?: Attribute[];
|
|
12
|
+
} | {
|
|
13
|
+
type: 'image';
|
|
14
|
+
path: string;
|
|
15
|
+
userData?: Attribute[];
|
|
16
|
+
}>;
|
|
17
|
+
responseText: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
interface AsrDetectionContext {
|
|
20
|
+
responseText: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
type MockHooks = {
|
|
23
|
+
sendToAIStream: (context: SendToAIStreamContext) => any;
|
|
24
|
+
asrDetection: (context: AsrDetectionContext) => any;
|
|
25
|
+
} & {
|
|
26
|
+
[fn: string]: (context: TTTCallContext) => any;
|
|
27
|
+
};
|
|
28
|
+
interface Row<T = any> {
|
|
29
|
+
id: number;
|
|
30
|
+
record: T;
|
|
31
|
+
}
|
|
32
|
+
declare const mock: {
|
|
33
|
+
loaded: boolean;
|
|
34
|
+
lastId: number;
|
|
35
|
+
database: Map<number, Row<any>>;
|
|
36
|
+
data: Map<string, any>;
|
|
37
|
+
sleep: (ms: number) => Promise<void>;
|
|
38
|
+
generateInt: (min: number, max: number) => number;
|
|
39
|
+
emitter: Emitter;
|
|
40
|
+
hooks: import("hookable").Hookable<MockHooks, string>;
|
|
41
|
+
getRecords: <T = any>() => Row<T>[];
|
|
42
|
+
getRecord: <T_1 = any>(id: number) => Row<T_1> | undefined;
|
|
43
|
+
setRecord: <T_2 = any>(entry: Row<T_2>) => void;
|
|
44
|
+
deleteRecord: (id: number) => void;
|
|
45
|
+
clearRecords: () => void;
|
|
46
|
+
getId: () => number;
|
|
47
|
+
};
|
|
48
|
+
export { mock };
|