@ray-js/t-agent-plugin-aistream 0.2.6-beta-6 → 0.2.6-beta-8
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/dist/withAIStream.d.ts +2 -0
- package/dist/withAIStream.js +63 -47
- package/package.json +2 -2
package/dist/withAIStream.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ export interface AIStreamHooks {
|
|
|
42
42
|
onMessageParse: (msgItem: StoredMessageObject, result: {
|
|
43
43
|
messages: ChatMessage[];
|
|
44
44
|
}) => void;
|
|
45
|
+
onChatMessageSent: (userMessage: ChatMessage, respMessage: ChatMessage) => void;
|
|
45
46
|
onTextCompose: (respMsg: ChatMessage, status: ChatMessageStatus, result: {
|
|
46
47
|
text: string;
|
|
47
48
|
}) => void;
|
|
@@ -81,6 +82,7 @@ export declare function withAIStream(options?: AIStreamOptions): (agent: ChatAge
|
|
|
81
82
|
data: string;
|
|
82
83
|
}>;
|
|
83
84
|
onSkillCompose: (fn: AIStreamHooks['onSkillCompose']) => () => void;
|
|
85
|
+
onChatMessageSent: (fn: AIStreamHooks['onChatMessageSent']) => () => void;
|
|
84
86
|
onTextCompose: (fn: AIStreamHooks['onTextCompose']) => () => void;
|
|
85
87
|
onSkillsEnd: (fn: AIStreamHooks['onSkillsEnd']) => () => void;
|
|
86
88
|
onCardsReceived: (fn: AIStreamHooks['onCardsReceived']) => () => void;
|
package/dist/withAIStream.js
CHANGED
|
@@ -73,6 +73,31 @@ export function withAIStream() {
|
|
|
73
73
|
await agent.session.set('AIStream.agentId', agentId);
|
|
74
74
|
await agent.session.set('AIStream.deviceId', deviceId);
|
|
75
75
|
|
|
76
|
+
// 消息列表可能耗时久,优先创建 streamSession 再去加载消息列表
|
|
77
|
+
|
|
78
|
+
// 创建 streamSession
|
|
79
|
+
const connection = globalAIStreamClient.getConnection({
|
|
80
|
+
clientType,
|
|
81
|
+
deviceId
|
|
82
|
+
});
|
|
83
|
+
const streamSession = connection.createSession({
|
|
84
|
+
ownerId: clientType === ConnectClientType.DEVICE ? deviceId : "".concat(homeId),
|
|
85
|
+
api: tokenOptions.api,
|
|
86
|
+
apiVersion: tokenOptions.version,
|
|
87
|
+
solutionCode: agentId,
|
|
88
|
+
extParams: _objectSpread({
|
|
89
|
+
needTts: !!options.enableTts,
|
|
90
|
+
deviceId
|
|
91
|
+
}, tokenOptions.extParams)
|
|
92
|
+
});
|
|
93
|
+
await session.set('AIStream.streamSession', streamSession);
|
|
94
|
+
if (options.earlyStart) {
|
|
95
|
+
// 故意异步,不阻塞消息列表加载
|
|
96
|
+
streamSession.ensureSession().catch(error => {
|
|
97
|
+
logger.error('earlyStart failed', error);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
76
101
|
/*
|
|
77
102
|
* 2. 获取历史消息
|
|
78
103
|
*/
|
|
@@ -104,29 +129,6 @@ export function withAIStream() {
|
|
|
104
129
|
session.isNewChat = true;
|
|
105
130
|
}
|
|
106
131
|
session.sessionId = indexId;
|
|
107
|
-
|
|
108
|
-
// 创建 streamSession
|
|
109
|
-
const connection = globalAIStreamClient.getConnection({
|
|
110
|
-
clientType,
|
|
111
|
-
deviceId
|
|
112
|
-
});
|
|
113
|
-
const streamSession = connection.createSession({
|
|
114
|
-
ownerId: clientType === ConnectClientType.DEVICE ? deviceId : "".concat(homeId),
|
|
115
|
-
api: tokenOptions.api,
|
|
116
|
-
apiVersion: tokenOptions.version,
|
|
117
|
-
solutionCode: agentId,
|
|
118
|
-
extParams: _objectSpread({
|
|
119
|
-
needTts: !!options.enableTts,
|
|
120
|
-
deviceId
|
|
121
|
-
}, tokenOptions.extParams)
|
|
122
|
-
});
|
|
123
|
-
await session.set('AIStream.streamSession', streamSession);
|
|
124
|
-
if (options.earlyStart) {
|
|
125
|
-
// 故意异步,不阻塞消息列表加载
|
|
126
|
-
streamSession.ensureSession().catch(error => {
|
|
127
|
-
logger.error('earlyStart failed', error);
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
132
|
});
|
|
131
133
|
const getHistoryStore = () => {
|
|
132
134
|
const historyStore = session.get('AIStream.historyStore');
|
|
@@ -229,6 +231,7 @@ export function withAIStream() {
|
|
|
229
231
|
attribute: _objectSpread({}, extraOptions)
|
|
230
232
|
});
|
|
231
233
|
signal === null || signal === void 0 || signal.addEventListener('abort', event => {
|
|
234
|
+
logger.debug('withAIStream signal aborted, response.started:', result.response.started);
|
|
232
235
|
if (result.response.started) {
|
|
233
236
|
result.response.cancel(event.reason);
|
|
234
237
|
}
|
|
@@ -265,7 +268,8 @@ export function withAIStream() {
|
|
|
265
268
|
audioPromise = new Promise((resolve, reject) => {
|
|
266
269
|
// 当确认发送时,展示 loading
|
|
267
270
|
let userMsgShow = false;
|
|
268
|
-
|
|
271
|
+
audioEmitter.addEventListener('confirm', async () => {
|
|
272
|
+
logger.debug('withAIStream chat agent.chat audioEmitter onConfirm');
|
|
269
273
|
if (end) {
|
|
270
274
|
return;
|
|
271
275
|
}
|
|
@@ -275,15 +279,18 @@ export function withAIStream() {
|
|
|
275
279
|
userMsg.bubble.setStatus(BubbleTileStatus.NORMAL);
|
|
276
280
|
userMsgShow = true; // show 会延迟变更 isShow 状态,所以使用 userMsgShow
|
|
277
281
|
await userMsg.show();
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
282
|
+
}, {
|
|
283
|
+
once: true
|
|
284
|
+
});
|
|
281
285
|
|
|
282
286
|
// 当音频识别完毕时,展示文本
|
|
283
|
-
|
|
287
|
+
|
|
288
|
+
audioEmitter.addEventListener('finish', async event => {
|
|
289
|
+
logger.debug('withAIStream chat agent.chat audioEmitter onFinished');
|
|
284
290
|
if (end) {
|
|
285
291
|
return;
|
|
286
292
|
}
|
|
293
|
+
end = true;
|
|
287
294
|
if (!event.detail.text && userMsgShow) {
|
|
288
295
|
await userMsg.remove();
|
|
289
296
|
reject(new Error('No text found in audio event'));
|
|
@@ -296,33 +303,35 @@ export function withAIStream() {
|
|
|
296
303
|
});
|
|
297
304
|
await userMsg.update();
|
|
298
305
|
await userMsg.persist();
|
|
299
|
-
end = true;
|
|
300
|
-
audioEmitter.removeEventListener('finish', onFinished);
|
|
301
306
|
resolve();
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
307
|
+
}, {
|
|
308
|
+
once: true
|
|
309
|
+
});
|
|
310
|
+
audioEmitter.addEventListener('cancel', async () => {
|
|
311
|
+
logger.debug('withAIStream chat agent.chat audioEmitter onCancel');
|
|
305
312
|
end = true;
|
|
313
|
+
// 取消时,有可能 userMsg.persist 还在执行,所以这里不 await,先 reject
|
|
314
|
+
reject(new Error('User cancel'));
|
|
306
315
|
if (!response.started && userMsgShow) {
|
|
307
316
|
await userMsg.remove();
|
|
308
317
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
};
|
|
312
|
-
audioEmitter.addEventListener('cancel', onCancel);
|
|
318
|
+
}, {
|
|
319
|
+
once: true
|
|
320
|
+
});
|
|
313
321
|
signal.addEventListener('abort', () => {
|
|
314
|
-
logger.
|
|
322
|
+
logger.debug('withAIStream chat agent.chat audioEmitter onAbort');
|
|
315
323
|
audioEmitter.dispatchEvent(new EmitterEvent('cancel'));
|
|
316
324
|
});
|
|
317
|
-
|
|
325
|
+
audioEmitter.addEventListener('error', async () => {
|
|
326
|
+
logger.debug('withAIStream chat agent.chat audioEmitter onError');
|
|
318
327
|
end = true;
|
|
328
|
+
reject(new Error('Audio emitter error'));
|
|
319
329
|
if (userMsgShow && userMsg.status !== ChatMessageStatus.FINISH) {
|
|
320
330
|
await userMsg.remove();
|
|
321
331
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
};
|
|
325
|
-
audioEmitter.addEventListener('error', onError);
|
|
332
|
+
}, {
|
|
333
|
+
once: true
|
|
334
|
+
});
|
|
326
335
|
});
|
|
327
336
|
} else {
|
|
328
337
|
userMsg.set({
|
|
@@ -350,6 +359,7 @@ export function withAIStream() {
|
|
|
350
359
|
const message = createMessage({
|
|
351
360
|
role: responseBy
|
|
352
361
|
});
|
|
362
|
+
await hooks.callHook('onChatMessageSent', userMsg, message);
|
|
353
363
|
const skills = [];
|
|
354
364
|
logger.debug('withAIStream chat agent.flushStreamToShow');
|
|
355
365
|
const result = {
|
|
@@ -453,10 +463,13 @@ export function withAIStream() {
|
|
|
453
463
|
onAgentDispose(async () => {
|
|
454
464
|
const streamSession = session.get('AIStream.streamSession');
|
|
455
465
|
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
466
|
+
// 比较低的概率 onAgentStart 还没跑完(卡在获取获取家庭信息)就调用了 dispose
|
|
467
|
+
if (streamSession) {
|
|
468
|
+
/**
|
|
469
|
+
* 关闭session
|
|
470
|
+
*/
|
|
471
|
+
await streamSession.close();
|
|
472
|
+
}
|
|
460
473
|
});
|
|
461
474
|
const handleTTTAction = async (tile, tttAction) => {
|
|
462
475
|
const result = {
|
|
@@ -562,6 +575,9 @@ export function withAIStream() {
|
|
|
562
575
|
onSkillCompose: fn => {
|
|
563
576
|
return hooks.hook('onSkillCompose', fn);
|
|
564
577
|
},
|
|
578
|
+
onChatMessageSent: fn => {
|
|
579
|
+
return hooks.hook('onChatMessageSent', fn);
|
|
580
|
+
},
|
|
565
581
|
onTextCompose: fn => {
|
|
566
582
|
return hooks.hook('onTextCompose', fn);
|
|
567
583
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/t-agent-plugin-aistream",
|
|
3
|
-
"version": "0.2.6-beta-
|
|
3
|
+
"version": "0.2.6-beta-8",
|
|
4
4
|
"author": "Tuya.inc",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"private": false,
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/url-parse": "^1.4.11"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "6fc9a8195706d72c2bab10479e8d65a26854bdde"
|
|
39
39
|
}
|