@ray-js/t-agent-plugin-aistream 0.2.6 → 0.2.7-beta.10
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/README-zh_CN.md +60 -2
- package/README.md +60 -2
- package/dist/AIStreamTypes.d.ts +53 -35
- package/dist/AIStreamTypes.js +1 -2
- package/dist/asr/AsrAgent.js +6 -3
- package/dist/asr/createAsrAgent.js +4 -0
- package/dist/buildIn/withBuildIn.js +14 -8
- package/dist/utils/AIStream.d.ts +8 -15
- package/dist/utils/AIStream.js +30 -45
- package/dist/utils/defaultMock.js +21 -17
- package/dist/utils/mock.d.ts +2 -3
- package/dist/utils/mock.js +2 -1
- package/dist/utils/object.d.ts +1 -0
- package/dist/utils/object.js +24 -1
- package/dist/utils/sendMessage.d.ts +4 -4
- package/dist/utils/sendMessage.js +20 -9
- package/dist/utils/track.d.ts +1 -0
- package/dist/utils/track.js +29 -0
- package/dist/utils/ttt.d.ts +3 -1
- package/dist/utils/ttt.js +4 -1
- package/dist/withAIStream.d.ts +11 -5
- package/dist/withAIStream.js +40 -9
- package/package.json +2 -2
package/dist/utils/AIStream.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
2
1
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
2
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
4
|
-
|
|
3
|
+
import "core-js/modules/es.json.stringify.js";
|
|
5
4
|
import "core-js/modules/esnext.iterator.constructor.js";
|
|
6
5
|
import "core-js/modules/esnext.iterator.find.js";
|
|
7
6
|
import "core-js/modules/esnext.iterator.for-each.js";
|
|
8
7
|
import "core-js/modules/esnext.iterator.map.js";
|
|
9
8
|
import "core-js/modules/web.dom-collections.iterator.js";
|
|
10
|
-
import {
|
|
9
|
+
import { AIStreamErrorCode, AIStreamServerErrorCode, BizTag, ConnectClientType, ConnectState, EventType, NetworkType, SessionState } from '../AIStreamTypes';
|
|
11
10
|
import { closeSession, connect, createSession, disconnect, getCurrentHomeInfo, getNetworkType, isConnected, queryAgentToken, sendEventChatBreak, sendEventEnd, sendEventPayloadEnd, sendEventStart, sendImageData, sendTextData, startRecordAndSendAudioData, stopRecordAndSendAudioData } from './ttt';
|
|
12
11
|
import { AIStreamObserver, AIStreamObserverPool } from './observer';
|
|
13
12
|
import { isAbortError, safeParseJSON } from '@ray-js/t-agent';
|
|
@@ -180,27 +179,12 @@ export class AIStreamSession {
|
|
|
180
179
|
(_this$activeEvent2 = this.activeEvent) === null || _this$activeEvent2 === void 0 || _this$activeEvent2.emit('data', entry);
|
|
181
180
|
if (entry.type === 'event' && entry.body.eventId === this.activeEvent.eventId) {
|
|
182
181
|
const {
|
|
183
|
-
eventType
|
|
184
|
-
userData
|
|
182
|
+
eventType
|
|
185
183
|
} = entry.body;
|
|
186
184
|
if (eventType === EventType.EVENT_END || eventType === EventType.CHAT_BREAK || eventType === EventType.ONE_SHOT) {
|
|
187
185
|
var _this$activeEvent3;
|
|
188
186
|
(_this$activeEvent3 = this.activeEvent) === null || _this$activeEvent3 === void 0 || _this$activeEvent3.emit('finish');
|
|
189
187
|
this.cleanupEvent();
|
|
190
|
-
} else if (eventType === EventType.SERVER_ERROR) {
|
|
191
|
-
var _this$activeEvent4;
|
|
192
|
-
let code = '';
|
|
193
|
-
let message = '';
|
|
194
|
-
for (const item of userData) {
|
|
195
|
-
if (item.type === AIStreamAttributeType.ERROR_CODE) {
|
|
196
|
-
code = item.value;
|
|
197
|
-
} else if (item.type === AIStreamAttributeType.ERROR_MESSAGE) {
|
|
198
|
-
message = item.value;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
const error = new AIStreamError(message || 'Event error occurred', code || AIStreamErrorCode.UNKNOWN_ERROR);
|
|
202
|
-
(_this$activeEvent4 = this.activeEvent) === null || _this$activeEvent4 === void 0 || _this$activeEvent4.emit('error', error);
|
|
203
|
-
this.cleanupEvent();
|
|
204
188
|
}
|
|
205
189
|
}
|
|
206
190
|
});
|
|
@@ -274,12 +258,16 @@ export class AIStreamSession {
|
|
|
274
258
|
} else {
|
|
275
259
|
this.tokenExtParamsResolvable.resolve({});
|
|
276
260
|
}
|
|
261
|
+
let userData = {};
|
|
262
|
+
if (options.getSessionUserData) {
|
|
263
|
+
userData = await options.getSessionUserData();
|
|
264
|
+
}
|
|
277
265
|
{
|
|
278
266
|
const [err, res] = await tryCatchTTT(() => createSession({
|
|
279
267
|
bizTag: options.bizTag,
|
|
280
268
|
agentToken,
|
|
281
269
|
bizConfig,
|
|
282
|
-
|
|
270
|
+
userDataJson: JSON.stringify(userData)
|
|
283
271
|
}));
|
|
284
272
|
if (err) {
|
|
285
273
|
this.promise = null;
|
|
@@ -302,18 +290,23 @@ export class AIStreamSession {
|
|
|
302
290
|
throw new AIStreamError('Cannot start a new event while another is active', AIStreamErrorCode.EVENT_EXISTS);
|
|
303
291
|
}
|
|
304
292
|
const {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
293
|
+
signal,
|
|
294
|
+
userData,
|
|
295
|
+
eventIdPrefix,
|
|
296
|
+
userDataJson
|
|
297
|
+
} = options;
|
|
308
298
|
await this.ensureSession();
|
|
309
299
|
if (signal !== null && signal !== void 0 && signal.aborted) {
|
|
310
300
|
const error = new AIStreamError('start event was aborted', AIStreamErrorCode.EVENT_ABORTED);
|
|
311
301
|
error.name = 'AbortError';
|
|
312
302
|
throw error;
|
|
313
303
|
}
|
|
314
|
-
const [error, result] = await tryCatchTTT(() => sendEventStart(
|
|
315
|
-
sessionId: this.sessionId
|
|
316
|
-
|
|
304
|
+
const [error, result] = await tryCatchTTT(() => sendEventStart({
|
|
305
|
+
sessionId: this.sessionId,
|
|
306
|
+
userDataJson,
|
|
307
|
+
userData,
|
|
308
|
+
eventIdPrefix
|
|
309
|
+
}));
|
|
317
310
|
if (error) {
|
|
318
311
|
throw error;
|
|
319
312
|
}
|
|
@@ -337,7 +330,8 @@ export class AIStreamSession {
|
|
|
337
330
|
sessionId: this.sessionId
|
|
338
331
|
};
|
|
339
332
|
this.activeObserver = new AIStreamObserver(this.onDataEntry, this.pool);
|
|
340
|
-
for (
|
|
333
|
+
for (let i = 0; i < this.revDataChannels.length; i++) {
|
|
334
|
+
const revDataChannel = this.revDataChannels[i];
|
|
341
335
|
if (revDataChannel.startsWith('text')) {
|
|
342
336
|
observerOptions.text = true;
|
|
343
337
|
}
|
|
@@ -358,11 +352,11 @@ export class AIStreamSession {
|
|
|
358
352
|
return this.activeEvent;
|
|
359
353
|
}
|
|
360
354
|
cleanupEvent() {
|
|
361
|
-
var _this$activeObserver, _this$
|
|
355
|
+
var _this$activeObserver, _this$activeEvent4;
|
|
362
356
|
logger.debug('AIStreamSession cleanupEvent');
|
|
363
357
|
(_this$activeObserver = this.activeObserver) === null || _this$activeObserver === void 0 || _this$activeObserver.disconnect();
|
|
364
358
|
this.activeObserver = null;
|
|
365
|
-
(_this$
|
|
359
|
+
(_this$activeEvent4 = this.activeEvent) === null || _this$activeEvent4 === void 0 || _this$activeEvent4.emit('close');
|
|
366
360
|
this.activeEvent = null;
|
|
367
361
|
}
|
|
368
362
|
cleanup() {
|
|
@@ -420,8 +414,7 @@ export class AIStreamEvent {
|
|
|
420
414
|
const [error] = await tryCatchTTT(() => sendTextData({
|
|
421
415
|
sessionId: this.sessionId,
|
|
422
416
|
text: chunk.text,
|
|
423
|
-
dataChannel
|
|
424
|
-
userData: chunk.userData
|
|
417
|
+
dataChannel
|
|
425
418
|
}));
|
|
426
419
|
if (error) {
|
|
427
420
|
throw error;
|
|
@@ -433,13 +426,11 @@ export class AIStreamEvent {
|
|
|
433
426
|
// path: chunk.path,
|
|
434
427
|
// format: chunk.format,
|
|
435
428
|
// dataChannel,
|
|
436
|
-
// userData: chunk.userData,
|
|
437
429
|
// });
|
|
438
430
|
} else if (chunk.type === 'image') {
|
|
439
431
|
const [error] = await tryCatchTTT(() => sendImageData({
|
|
440
432
|
sessionId: this.sessionId,
|
|
441
|
-
path: chunk.path
|
|
442
|
-
userData: chunk.userData
|
|
433
|
+
path: chunk.path
|
|
443
434
|
}));
|
|
444
435
|
if (error) {
|
|
445
436
|
throw error;
|
|
@@ -485,7 +476,6 @@ export class AIStreamEvent {
|
|
|
485
476
|
startPromise = startRecordAndSendAudioData({
|
|
486
477
|
sessionId: this.sessionId,
|
|
487
478
|
dataChannel,
|
|
488
|
-
userData: source.userData,
|
|
489
479
|
recordInitParams: source.sampleRate ? {
|
|
490
480
|
sampleRate: source.sampleRate
|
|
491
481
|
} : undefined,
|
|
@@ -497,7 +487,6 @@ export class AIStreamEvent {
|
|
|
497
487
|
// await startRecordAndSendVideoData({
|
|
498
488
|
// dataChannel,
|
|
499
489
|
// cameraType,
|
|
500
|
-
// userData: source.userData,
|
|
501
490
|
// });
|
|
502
491
|
}
|
|
503
492
|
},
|
|
@@ -514,8 +503,7 @@ export class AIStreamEvent {
|
|
|
514
503
|
if (source.type === 'audio') {
|
|
515
504
|
file = await stopRecordAndSendAudioData({
|
|
516
505
|
sessionId: this.sessionId,
|
|
517
|
-
dataChannel
|
|
518
|
-
userData: source.userData
|
|
506
|
+
dataChannel
|
|
519
507
|
});
|
|
520
508
|
} else if (source.type === 'video') {
|
|
521
509
|
logger.warn('Video data sending is not implemented yet');
|
|
@@ -523,7 +511,6 @@ export class AIStreamEvent {
|
|
|
523
511
|
// await stopRecordAndSendVideoData({
|
|
524
512
|
// dataChannel,
|
|
525
513
|
// cameraType,
|
|
526
|
-
// userData: source.userData,
|
|
527
514
|
// });
|
|
528
515
|
}
|
|
529
516
|
sendEventPayloadEnd({
|
|
@@ -539,18 +526,17 @@ export class AIStreamEvent {
|
|
|
539
526
|
this.streams[dataChannel] = stream;
|
|
540
527
|
return stream;
|
|
541
528
|
}
|
|
542
|
-
async end(
|
|
529
|
+
async end() {
|
|
543
530
|
if (this.closed) {
|
|
544
531
|
return;
|
|
545
532
|
}
|
|
546
533
|
await Promise.all([...Object.values(this.chains), ...Object.values(this.streams).map(s => s.stop())]);
|
|
547
534
|
await sendEventEnd({
|
|
548
535
|
eventId: this.eventId,
|
|
549
|
-
sessionId: this.sessionId
|
|
550
|
-
userData: options === null || options === void 0 ? void 0 : options.userData
|
|
536
|
+
sessionId: this.sessionId
|
|
551
537
|
});
|
|
552
538
|
}
|
|
553
|
-
abort(
|
|
539
|
+
abort() {
|
|
554
540
|
if (this.closed) {
|
|
555
541
|
return;
|
|
556
542
|
}
|
|
@@ -558,8 +544,7 @@ export class AIStreamEvent {
|
|
|
558
544
|
// 故意不等,直接发送中断事件
|
|
559
545
|
sendEventChatBreak({
|
|
560
546
|
eventId: this.eventId,
|
|
561
|
-
sessionId: this.sessionId
|
|
562
|
-
userData: options === null || options === void 0 ? void 0 : options.userData
|
|
547
|
+
sessionId: this.sessionId
|
|
563
548
|
});
|
|
564
549
|
}
|
|
565
550
|
const error = new AIStreamError('This operation was aborted', AIStreamErrorCode.EVENT_ABORTED);
|
|
@@ -95,17 +95,13 @@ mock.hooks.hook('disconnect', context => {
|
|
|
95
95
|
mock.data.set('sessionMap', new Map());
|
|
96
96
|
});
|
|
97
97
|
mock.hooks.hook('queryAgentToken', context => {
|
|
98
|
-
var _context$options$extP, _context$options$
|
|
98
|
+
var _context$options$extP, _context$options$extP3, _context$options$extP4;
|
|
99
99
|
const agentToken = generateId();
|
|
100
100
|
const map = mock.data.get('tokenMap');
|
|
101
|
-
map.set(agentToken, {
|
|
102
|
-
onlyAsr: ((_context$options$extP = context.options.extParams) === null || _context$options$extP === void 0 ? void 0 : _context$options$extP.onlyAsr) || false,
|
|
103
|
-
needTts: ((_context$options$extP2 = context.options.extParams) === null || _context$options$extP2 === void 0 ? void 0 : _context$options$extP2.needTts) || false
|
|
104
|
-
});
|
|
105
101
|
let chatId;
|
|
106
|
-
if (((_context$options$
|
|
107
|
-
var _context$options$
|
|
108
|
-
if ((_context$options$
|
|
102
|
+
if (((_context$options$extP = context.options.extParams) === null || _context$options$extP === void 0 ? void 0 : _context$options$extP.dialogueMode) === 1) {
|
|
103
|
+
var _context$options$extP2;
|
|
104
|
+
if ((_context$options$extP2 = context.options.extParams) !== null && _context$options$extP2 !== void 0 && _context$options$extP2.chatId) {
|
|
109
105
|
// 如果参数有传吗,则使用传入的 chatId
|
|
110
106
|
chatId = context.options.extParams.chatId;
|
|
111
107
|
} else {
|
|
@@ -113,6 +109,11 @@ mock.hooks.hook('queryAgentToken', context => {
|
|
|
113
109
|
chatId = "chatId-".concat(generateId());
|
|
114
110
|
}
|
|
115
111
|
}
|
|
112
|
+
map.set(agentToken, {
|
|
113
|
+
onlyAsr: ((_context$options$extP3 = context.options.extParams) === null || _context$options$extP3 === void 0 ? void 0 : _context$options$extP3.onlyAsr) || false,
|
|
114
|
+
needTts: ((_context$options$extP4 = context.options.extParams) === null || _context$options$extP4 === void 0 ? void 0 : _context$options$extP4.needTts) || false,
|
|
115
|
+
chatId
|
|
116
|
+
});
|
|
116
117
|
context.result = {
|
|
117
118
|
agentToken,
|
|
118
119
|
bizConfig: {
|
|
@@ -144,7 +145,8 @@ mock.hooks.hook('createSession', context => {
|
|
|
144
145
|
sendDataChannels: ['audio', 'video', 'text', 'image'],
|
|
145
146
|
revDataChannels: ['text', 'audio'],
|
|
146
147
|
currentEvent: null,
|
|
147
|
-
tokenConfig
|
|
148
|
+
tokenConfig,
|
|
149
|
+
chatId: tokenConfig.chatId
|
|
148
150
|
};
|
|
149
151
|
map.set(session.sessionId, session);
|
|
150
152
|
context.result = {
|
|
@@ -273,7 +275,8 @@ mock.hooks.hook('sendEventEnd', async context => {
|
|
|
273
275
|
const ctx = {
|
|
274
276
|
data: event.data,
|
|
275
277
|
responseText: '',
|
|
276
|
-
wordDelayMs: 100
|
|
278
|
+
wordDelayMs: 100,
|
|
279
|
+
chatId: session.chatId
|
|
277
280
|
};
|
|
278
281
|
const [error] = await tryCatch(() => mock.hooks.callHook('sendToAIStream', ctx));
|
|
279
282
|
if (error) {
|
|
@@ -310,7 +313,8 @@ mock.hooks.hook('sendEventEnd', async context => {
|
|
|
310
313
|
}
|
|
311
314
|
const bizId = generateId();
|
|
312
315
|
event.replyText(StreamFlag.START);
|
|
313
|
-
for (
|
|
316
|
+
for (let i = 0; i < words.length; i++) {
|
|
317
|
+
const word = words[i];
|
|
314
318
|
await mock.sleep(wordDelayMs);
|
|
315
319
|
if (event.controller.signal.aborted || session.closed) {
|
|
316
320
|
return;
|
|
@@ -345,7 +349,8 @@ mock.hooks.hook('sendEventEnd', async context => {
|
|
|
345
349
|
return;
|
|
346
350
|
}
|
|
347
351
|
if ((_ctx$responseSkills = ctx.responseSkills) !== null && _ctx$responseSkills !== void 0 && _ctx$responseSkills.length) {
|
|
348
|
-
for (
|
|
352
|
+
for (let i = 0; i < ctx.responseSkills.length; i++) {
|
|
353
|
+
const skill = ctx.responseSkills[i];
|
|
349
354
|
await mock.sleep(100);
|
|
350
355
|
if (event.controller.signal.aborted || session.closed) {
|
|
351
356
|
return;
|
|
@@ -462,7 +467,8 @@ mock.hooks.hook('startRecordAndSendAudioData', async context => {
|
|
|
462
467
|
text
|
|
463
468
|
}
|
|
464
469
|
});
|
|
465
|
-
for (
|
|
470
|
+
for (let i = 0; i < parts.length; i++) {
|
|
471
|
+
const part = parts[i];
|
|
466
472
|
if (finishController.signal.aborted) {
|
|
467
473
|
break;
|
|
468
474
|
}
|
|
@@ -504,8 +510,7 @@ mock.hooks.hook('sendImageData', context => {
|
|
|
504
510
|
}
|
|
505
511
|
session.currentEvent.data.push({
|
|
506
512
|
type: 'image',
|
|
507
|
-
path: context.options.path
|
|
508
|
-
userData: context.options.userData
|
|
513
|
+
path: context.options.path
|
|
509
514
|
});
|
|
510
515
|
context.result = true;
|
|
511
516
|
});
|
|
@@ -517,8 +522,7 @@ mock.hooks.hook('sendTextData', context => {
|
|
|
517
522
|
}
|
|
518
523
|
session.currentEvent.data.push({
|
|
519
524
|
type: 'text',
|
|
520
|
-
text: context.options.text
|
|
521
|
-
userData: context.options.userData
|
|
525
|
+
text: context.options.text
|
|
522
526
|
});
|
|
523
527
|
});
|
|
524
528
|
const filterRecords = query => {
|
package/dist/utils/mock.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Emitter } from '@ray-js/t-agent';
|
|
2
|
-
import {
|
|
2
|
+
import { ReceivedTextSkillPacketBody } from '../AIStreamTypes';
|
|
3
3
|
interface TTTCallContext {
|
|
4
4
|
options: any;
|
|
5
5
|
result: any;
|
|
@@ -8,12 +8,11 @@ export interface SendToAIStreamContext {
|
|
|
8
8
|
data: Array<{
|
|
9
9
|
type: 'text';
|
|
10
10
|
text: string;
|
|
11
|
-
userData?: Attribute[];
|
|
12
11
|
} | {
|
|
13
12
|
type: 'image';
|
|
14
13
|
path: string;
|
|
15
|
-
userData?: Attribute[];
|
|
16
14
|
}>;
|
|
15
|
+
chatId: string;
|
|
17
16
|
responseText: string | undefined;
|
|
18
17
|
wordDelayMs: number;
|
|
19
18
|
responseSkills?: ReceivedTextSkillPacketBody[];
|
package/dist/utils/mock.js
CHANGED
package/dist/utils/object.d.ts
CHANGED
package/dist/utils/object.js
CHANGED
|
@@ -22,4 +22,27 @@ export const createResolvable = () => {
|
|
|
22
22
|
};
|
|
23
23
|
});
|
|
24
24
|
return ret;
|
|
25
|
-
};
|
|
25
|
+
};
|
|
26
|
+
export function deepMerge(target, source) {
|
|
27
|
+
if (typeof target !== 'object' || target === null) {
|
|
28
|
+
return source; // 如果目标不是对象,直接返回来源
|
|
29
|
+
}
|
|
30
|
+
if (typeof source !== 'object' || source === null) {
|
|
31
|
+
return target; // 如果来源不是对象,返回目标
|
|
32
|
+
}
|
|
33
|
+
for (const key in source) {
|
|
34
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
35
|
+
const sourceValue = source[key];
|
|
36
|
+
const targetValue = target[key];
|
|
37
|
+
|
|
38
|
+
// 如果是对象,递归合并
|
|
39
|
+
if (typeof sourceValue === 'object' && sourceValue !== null) {
|
|
40
|
+
target[key] = deepMerge(Array.isArray(targetValue) ? [] : targetValue || {}, sourceValue);
|
|
41
|
+
} else {
|
|
42
|
+
// 否则直接赋值
|
|
43
|
+
target[key] = sourceValue;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return target;
|
|
48
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import '../polyfill';
|
|
2
2
|
import { AIStreamSession } from './AIStream';
|
|
3
|
-
import {
|
|
4
|
-
import { InputBlock, StreamResponse } from '@ray-js/t-agent';
|
|
3
|
+
import { AIStreamUserData } from '../AIStreamTypes';
|
|
4
|
+
import { AbortSignalObject, InputBlock, StreamResponse } from '@ray-js/t-agent';
|
|
5
5
|
export interface SendBlocksToAIStreamParams {
|
|
6
6
|
blocks: InputBlock[];
|
|
7
7
|
session: AIStreamSession;
|
|
8
|
-
|
|
9
|
-
signal?: AbortSignal;
|
|
8
|
+
signal?: AbortSignalObject;
|
|
10
9
|
enableTts?: boolean;
|
|
11
10
|
eventIdPrefix?: string;
|
|
11
|
+
getUserData: () => Promise<AIStreamUserData>;
|
|
12
12
|
}
|
|
13
13
|
export declare function sendBlocksToAIStream(params: SendBlocksToAIStreamParams): {
|
|
14
14
|
response: StreamResponse;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
1
|
import "core-js/modules/es.json.stringify.js";
|
|
3
2
|
import "core-js/modules/web.dom-collections.iterator.js";
|
|
4
3
|
import '../polyfill';
|
|
@@ -8,6 +7,7 @@ import { EmitterEvent, generateId, safeParseJSON, StreamResponse } from '@ray-js
|
|
|
8
7
|
import { tryCatch } from './misc';
|
|
9
8
|
import { AIStreamError, transformErrorCode } from './errors';
|
|
10
9
|
import logger from './logger';
|
|
10
|
+
import { deepMerge } from './object';
|
|
11
11
|
const mimeTypeToFormatMap = {
|
|
12
12
|
'video/mp4': FileFormat.MP4,
|
|
13
13
|
'text/json': FileFormat.JSON,
|
|
@@ -20,10 +20,12 @@ export function sendBlocksToAIStream(params) {
|
|
|
20
20
|
session,
|
|
21
21
|
blocks,
|
|
22
22
|
signal,
|
|
23
|
-
eventIdPrefix
|
|
23
|
+
eventIdPrefix,
|
|
24
|
+
getUserData
|
|
24
25
|
} = params;
|
|
25
26
|
let audioEmitter = null;
|
|
26
|
-
for (
|
|
27
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
28
|
+
const block = blocks[i];
|
|
27
29
|
if (block.type === 'audio') {
|
|
28
30
|
if (audioEmitter) {
|
|
29
31
|
throw new Error('only one audio emitter is allowed');
|
|
@@ -31,10 +33,6 @@ export function sendBlocksToAIStream(params) {
|
|
|
31
33
|
audioEmitter = block.audio_emitter;
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
|
-
const attribute = _objectSpread({
|
|
35
|
-
'processing.interrupt': 'false',
|
|
36
|
-
'asr.enableVad': 'false'
|
|
37
|
-
}, params.attribute);
|
|
38
36
|
let canceled = false;
|
|
39
37
|
let closed = false;
|
|
40
38
|
let event = null;
|
|
@@ -103,14 +101,26 @@ export function sendBlocksToAIStream(params) {
|
|
|
103
101
|
}
|
|
104
102
|
});
|
|
105
103
|
}
|
|
104
|
+
const chatAttributes = {
|
|
105
|
+
'processing.interrupt': 'false',
|
|
106
|
+
'asr.enableVad': 'false'
|
|
107
|
+
};
|
|
108
|
+
let eventUserData = {};
|
|
109
|
+
if (getUserData) {
|
|
110
|
+
eventUserData = await getUserData();
|
|
111
|
+
}
|
|
112
|
+
const userData = deepMerge({
|
|
113
|
+
chatAttributes
|
|
114
|
+
}, eventUserData);
|
|
106
115
|
let error;
|
|
107
116
|
[error, event] = await tryCatch(() => session.startEvent({
|
|
108
117
|
signal,
|
|
109
118
|
eventIdPrefix,
|
|
119
|
+
userDataJson: JSON.stringify(userData),
|
|
110
120
|
userData: [{
|
|
111
121
|
type: AIStreamAttributeType.AI_CHAT,
|
|
112
122
|
payloadType: AIStreamAttributePayloadType.STRING,
|
|
113
|
-
value: JSON.stringify(
|
|
123
|
+
value: JSON.stringify(chatAttributes)
|
|
114
124
|
}]
|
|
115
125
|
}));
|
|
116
126
|
if (error) {
|
|
@@ -262,7 +272,8 @@ export function sendBlocksToAIStream(params) {
|
|
|
262
272
|
}
|
|
263
273
|
});
|
|
264
274
|
event.on('error', emitError);
|
|
265
|
-
for (
|
|
275
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
276
|
+
const block = blocks[i];
|
|
266
277
|
if (block.type === 'text') {
|
|
267
278
|
event.write({
|
|
268
279
|
type: 'text',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function trackEvent(agent_code: string, type: string, params?: Record<string, any>): Promise<void>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import "core-js/modules/es.json.stringify.js";
|
|
2
|
+
import { getAccountInfo, sendEvent } from './ttt';
|
|
3
|
+
const eventId = 'thing_jKaquNvaV2qMDvTpKcNQvb1ZppBZnlNq';
|
|
4
|
+
let accountInfoPromise = null;
|
|
5
|
+
export async function trackEvent(agent_code, type) {
|
|
6
|
+
let params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
7
|
+
if (!accountInfoPromise) {
|
|
8
|
+
accountInfoPromise = getAccountInfo();
|
|
9
|
+
}
|
|
10
|
+
const {
|
|
11
|
+
miniProgram
|
|
12
|
+
} = await accountInfoPromise;
|
|
13
|
+
try {
|
|
14
|
+
await sendEvent({
|
|
15
|
+
eventId,
|
|
16
|
+
event: {
|
|
17
|
+
type,
|
|
18
|
+
agent_code,
|
|
19
|
+
miniapp_code: miniProgram.appId,
|
|
20
|
+
miniapp_version: miniProgram.version,
|
|
21
|
+
miniapp_name: miniProgram.appName,
|
|
22
|
+
miniapp_env: miniProgram.envVersion,
|
|
23
|
+
params: JSON.stringify(params)
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error('track event error', e);
|
|
28
|
+
}
|
|
29
|
+
}
|
package/dist/utils/ttt.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApiRequestByAtopParams, ApiRequestByHighwayParams, AudioBody, AuthorizeParams, AuthorizePolicyStatusParams, CanIUseRouterParams, CloseSessionParams, ConnectParams, ConnectStateBody, CreateSessionParams, DeleteRecordListParams, DisconnectParams, EventBody, EventChannelMessageParams, GetAppInfoParams, GetCurrentHomeInfoParams, GetMiniAppConfigParams, GetAccountInfoParams, ImageBody, InsertRecordParams, NavigateToMiniProgramParams, OpenInnerH5Params, OpenMiniWidgetParams, QueryAgentTokenParams, QueryRecordListParams, RecordAmplitudesBody, RegisterChannelParams, RouterParams, SendEventChatBreakParams, SendEventEndParams, SendEventPayloadEndParams, SendEventStartParams, SendImageDataParams, SendTextDataParams, SessionStateBody, StartRecordAndSendAudioDataParams, StopRecordAndSendAudioDataParams, TextBody, UpdateRecordParams, IsConnectedParams, GetNetworkTypeParams, StartPlayAudioParams, InitAudioRecorderParams, StopPlayAudioParams, AudioPlayChangedBody } from '../AIStreamTypes';
|
|
1
|
+
import { ApiRequestByAtopParams, ApiRequestByHighwayParams, AudioBody, AuthorizeParams, AuthorizePolicyStatusParams, CanIUseRouterParams, CloseSessionParams, ConnectParams, ConnectStateBody, CreateSessionParams, DeleteRecordListParams, DisconnectParams, EventBody, EventChannelMessageParams, GetAppInfoParams, GetCurrentHomeInfoParams, GetMiniAppConfigParams, GetAccountInfoParams, ImageBody, InsertRecordParams, NavigateToMiniProgramParams, OpenInnerH5Params, OpenMiniWidgetParams, QueryAgentTokenParams, QueryRecordListParams, RecordAmplitudesBody, RegisterChannelParams, RouterParams, SendEventChatBreakParams, SendEventEndParams, SendEventPayloadEndParams, SendEventStartParams, SendImageDataParams, SendTextDataParams, SessionStateBody, StartRecordAndSendAudioDataParams, StopRecordAndSendAudioDataParams, TextBody, UpdateRecordParams, IsConnectedParams, GetNetworkTypeParams, StartPlayAudioParams, InitAudioRecorderParams, StopPlayAudioParams, AudioPlayChangedBody, EventParams } from '../AIStreamTypes';
|
|
2
2
|
export declare const getMiniAppConfig: (options?: Omit<GetMiniAppConfigParams, "success" | "fail"> | undefined) => Promise<{
|
|
3
3
|
config: any;
|
|
4
4
|
}>;
|
|
@@ -77,6 +77,7 @@ export declare const closeSession: (options?: Omit<CloseSessionParams, "success"
|
|
|
77
77
|
export declare const sendEventStart: (options?: Omit<SendEventStartParams, "success" | "fail"> | undefined) => Promise<{
|
|
78
78
|
eventId: string;
|
|
79
79
|
}>;
|
|
80
|
+
/** @deprecated */
|
|
80
81
|
export declare const sendEventPayloadEnd: (options?: Omit<SendEventPayloadEndParams, "success" | "fail"> | undefined) => Promise<null>;
|
|
81
82
|
export declare const sendEventEnd: (options?: Omit<SendEventEndParams, "success" | "fail"> | undefined) => Promise<null>;
|
|
82
83
|
export declare const sendEventChatBreak: (options?: Omit<SendEventChatBreakParams, "success" | "fail"> | undefined) => Promise<null>;
|
|
@@ -112,3 +113,4 @@ export declare const getNetworkType: (options?: Omit<GetNetworkTypeParams, "succ
|
|
|
112
113
|
networkType: import("../AIStreamTypes").NetworkType;
|
|
113
114
|
signalStrength: number;
|
|
114
115
|
}>;
|
|
116
|
+
export declare const sendEvent: (options?: Omit<EventParams, "success" | "fail"> | undefined) => Promise<null>;
|
package/dist/utils/ttt.js
CHANGED
|
@@ -37,6 +37,8 @@ export const queryAgentToken = promisify(ty.aistream.queryAgentToken, true);
|
|
|
37
37
|
export const createSession = promisify(ty.aistream.createSession, true);
|
|
38
38
|
export const closeSession = promisify(ty.aistream.closeSession, true);
|
|
39
39
|
export const sendEventStart = promisify(ty.aistream.sendEventStart, true);
|
|
40
|
+
|
|
41
|
+
/** @deprecated */
|
|
40
42
|
export const sendEventPayloadEnd = promisify(ty.aistream.sendEventPayloadEnd, true);
|
|
41
43
|
export const sendEventEnd = promisify(ty.aistream.sendEventEnd, true);
|
|
42
44
|
export const sendEventChatBreak = promisify(ty.aistream.sendEventChatBreak, true);
|
|
@@ -89,4 +91,5 @@ export const queryRecordList = promisify(ty.aistream.queryRecordList, true);
|
|
|
89
91
|
export const deleteRecordList = promisify(ty.aistream.deleteRecordList, true);
|
|
90
92
|
export const updateRecord = promisify(ty.aistream.updateRecord, true);
|
|
91
93
|
export const insertRecord = promisify(ty.aistream.insertRecord, true);
|
|
92
|
-
export const getNetworkType = promisify(ty.getNetworkType, true);
|
|
94
|
+
export const getNetworkType = promisify(ty.getNetworkType, true);
|
|
95
|
+
export const sendEvent = promisify(ty.event);
|
package/dist/withAIStream.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ChatAgent, ChatCardObject, ChatMessage, ChatMessageStatus, ChatTile, GetChatPluginHandler, InputBlock } from '@ray-js/t-agent';
|
|
1
|
+
import { AbortSignalObject, ChatAgent, ChatCardObject, ChatMessage, ChatMessageStatus, ChatTile, GetChatPluginHandler, InputBlock } from '@ray-js/t-agent';
|
|
2
2
|
import { TTTAction } from './utils';
|
|
3
|
-
import { ConnectClientType, ReceivedTextSkillPacketBody } from './AIStreamTypes';
|
|
3
|
+
import { AIStreamUserData, ConnectClientType, ReceivedTextSkillPacketBody } from './AIStreamTypes';
|
|
4
4
|
import { ChatHistoryStore, StoredMessageObject } from './ChatHistoryStore';
|
|
5
5
|
export interface AIStreamOptions {
|
|
6
6
|
/** client 类型: 1-作为设备代理, 2-作为 App,3-作为开发者(行业 App) */
|
|
@@ -58,18 +58,23 @@ export interface AIStreamHooks {
|
|
|
58
58
|
onCardsReceived: (skills: ReceivedTextSkillPacketBody[], result: {
|
|
59
59
|
cards: ChatCardObject[];
|
|
60
60
|
}) => void;
|
|
61
|
+
onUserDataRead: (type: 'create-session' | 'start-event', data: {
|
|
62
|
+
blocks?: InputBlock[];
|
|
63
|
+
}, result: {
|
|
64
|
+
userData: AIStreamUserData;
|
|
65
|
+
}) => void;
|
|
61
66
|
}
|
|
62
67
|
export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAgent) => {
|
|
63
68
|
hooks: import("hookable").Hookable<AIStreamHooks, import("hookable").HookKeys<AIStreamHooks>>;
|
|
64
69
|
aiStream: {
|
|
65
|
-
send: (blocks: InputBlock[], signal?:
|
|
70
|
+
send: (blocks: InputBlock[], signal?: AbortSignalObject, eventUserData?: AIStreamUserData) => {
|
|
66
71
|
response: import("@ray-js/t-agent").StreamResponse;
|
|
67
72
|
metaPromise: Promise<Record<string, any>>;
|
|
68
73
|
};
|
|
69
|
-
chat: (blocks: InputBlock[], signal?:
|
|
74
|
+
chat: (blocks: InputBlock[], signal?: AbortSignalObject, options?: {
|
|
70
75
|
sendBy?: string | undefined;
|
|
71
76
|
responseBy?: string | undefined;
|
|
72
|
-
|
|
77
|
+
userData?: AIStreamUserData | undefined;
|
|
73
78
|
} | undefined) => Promise<ChatMessage[]>;
|
|
74
79
|
options: AIStreamOptions;
|
|
75
80
|
removeMessage: (message: ChatMessage) => Promise<void>;
|
|
@@ -86,6 +91,7 @@ export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAge
|
|
|
86
91
|
onTextCompose: (fn: AIStreamHooks['onTextCompose']) => () => void;
|
|
87
92
|
onSkillsEnd: (fn: AIStreamHooks['onSkillsEnd']) => () => void;
|
|
88
93
|
onCardsReceived: (fn: AIStreamHooks['onCardsReceived']) => () => void;
|
|
94
|
+
onUserDataRead: (fn: AIStreamHooks['onUserDataRead']) => () => void;
|
|
89
95
|
onTTTAction: (fn: AIStreamHooks['onTTTAction']) => () => void;
|
|
90
96
|
getChatId: () => Promise<string>;
|
|
91
97
|
};
|