@ray-js/t-agent-plugin-aistream 0.2.8-beta.1 → 0.2.8-beta.3
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 +70 -11
- package/README.md +916 -706
- package/dist/AIStreamTypes.d.ts +36 -17
- package/dist/asr/AsrAgent.d.ts +4 -4
- package/dist/asr/AsrAgent.js +8 -5
- package/dist/buildIn/withBuildIn.d.ts +1 -0
- package/dist/buildIn/withBuildIn.js +162 -25
- package/dist/polyfill.js +1 -0
- package/dist/utils/AIStream.d.ts +16 -3
- package/dist/utils/AIStream.js +84 -41
- package/dist/utils/defaultMock.js +90 -3
- package/dist/utils/errors.js +3 -1
- package/dist/utils/mock.d.ts +18 -3
- package/dist/utils/observer.d.ts +6 -0
- package/dist/utils/observer.js +100 -32
- package/dist/utils/promisify.d.ts +1 -1
- package/dist/utils/sendMessage.d.ts +0 -1
- package/dist/utils/sendMessage.js +26 -13
- package/dist/utils/ttt.d.ts +5 -4
- package/dist/utils/ttt.js +2 -0
- package/dist/withAIStream.d.ts +22 -14
- package/dist/withAIStream.js +94 -58
- package/package.json +5 -4
package/dist/withAIStream.js
CHANGED
|
@@ -93,65 +93,79 @@ export function withAIStream() {
|
|
|
93
93
|
clientType,
|
|
94
94
|
deviceId
|
|
95
95
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
hooks.callHook('onSessionEventReceived', event);
|
|
115
|
-
});
|
|
116
|
-
await session.set('AIStream.streamSession', streamSession);
|
|
117
|
-
if (options.earlyStart) {
|
|
118
|
-
// 故意异步,不阻塞消息列表加载
|
|
119
|
-
streamSession.ensureSession().catch(error => {
|
|
120
|
-
logger.error('earlyStart failed', error);
|
|
96
|
+
let streamSession = null;
|
|
97
|
+
try {
|
|
98
|
+
streamSession = connection.createSession({
|
|
99
|
+
ownerId: clientType === ConnectClientType.DEVICE ? deviceId : "".concat(homeId),
|
|
100
|
+
api: tokenOptions.api,
|
|
101
|
+
apiVersion: tokenOptions.version,
|
|
102
|
+
solutionCode: agentId,
|
|
103
|
+
extParams: _objectSpread({
|
|
104
|
+
needTts: !!options.enableTts,
|
|
105
|
+
deviceId
|
|
106
|
+
}, tokenOptions.extParams),
|
|
107
|
+
getSessionUserData: async () => {
|
|
108
|
+
const result = {
|
|
109
|
+
userData: {}
|
|
110
|
+
};
|
|
111
|
+
await hooks.callHook('onUserDataRead', 'create-session', {}, result);
|
|
112
|
+
return result.userData;
|
|
113
|
+
}
|
|
121
114
|
});
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
/*
|
|
125
|
-
* 2. 获取历史消息
|
|
126
|
-
*/
|
|
127
|
-
let historyStore;
|
|
128
|
-
if (typeof options.createChatHistoryStore === 'function') {
|
|
129
|
-
historyStore = options.createChatHistoryStore(agent);
|
|
130
|
-
} else {
|
|
131
|
-
historyStore = new ChatHistoryLocalStore({
|
|
132
|
-
bizCode: BizCode.CHAT,
|
|
133
|
-
agentId,
|
|
134
|
-
deviceId,
|
|
135
|
-
homeId,
|
|
136
|
-
indexId
|
|
115
|
+
streamSession.on('sessionEvent', event => {
|
|
116
|
+
hooks.callHook('onSessionEventReceived', event);
|
|
137
117
|
});
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
118
|
+
await session.set('AIStream.connection', connection);
|
|
119
|
+
await session.set('AIStream.streamSession', streamSession);
|
|
120
|
+
if (options.earlyStart) {
|
|
121
|
+
// 故意异步,不阻塞消息列表加载
|
|
122
|
+
streamSession.ensureSession().catch(error => {
|
|
123
|
+
logger.error('earlyStart failed', error);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/*
|
|
128
|
+
* 2. 获取历史消息
|
|
129
|
+
*/
|
|
130
|
+
let historyStore;
|
|
131
|
+
if (typeof options.createChatHistoryStore === 'function') {
|
|
132
|
+
historyStore = options.createChatHistoryStore(agent);
|
|
133
|
+
} else {
|
|
134
|
+
historyStore = new ChatHistoryLocalStore({
|
|
135
|
+
bizCode: BizCode.CHAT,
|
|
136
|
+
agentId,
|
|
137
|
+
deviceId,
|
|
138
|
+
homeId,
|
|
139
|
+
indexId
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (historyStore) {
|
|
143
|
+
await agent.session.set('AIStream.historyStore', historyStore);
|
|
144
|
+
const {
|
|
145
|
+
records
|
|
146
|
+
} = await historyStore.queryAll({
|
|
147
|
+
sort: 'desc',
|
|
148
|
+
// id 倒序去获取最新的消息list,然后再reverse,保证最新的消息在最前面
|
|
149
|
+
limit: options.historySize || 1000,
|
|
150
|
+
offset: 0
|
|
151
|
+
});
|
|
152
|
+
await session.set('AIStream.rawMessages', records.reverse());
|
|
153
|
+
session.isNewChat = records.length === 0;
|
|
154
|
+
} else {
|
|
155
|
+
session.isNewChat = true;
|
|
156
|
+
}
|
|
157
|
+
session.sessionId = indexId;
|
|
158
|
+
} catch (error) {
|
|
159
|
+
if (streamSession) {
|
|
160
|
+
await streamSession.close().catch(closeError => {
|
|
161
|
+
logger.error('withAIStream cleanup streamSession failed', closeError);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
await connection.close().catch(closeError => {
|
|
165
|
+
logger.error('withAIStream cleanup connection failed', closeError);
|
|
148
166
|
});
|
|
149
|
-
|
|
150
|
-
session.isNewChat = records.length === 0;
|
|
151
|
-
} else {
|
|
152
|
-
session.isNewChat = true;
|
|
167
|
+
throw error;
|
|
153
168
|
}
|
|
154
|
-
session.sessionId = indexId;
|
|
155
169
|
});
|
|
156
170
|
const getHistoryStore = () => {
|
|
157
171
|
const historyStore = session.get('AIStream.historyStore');
|
|
@@ -431,6 +445,7 @@ export function withAIStream() {
|
|
|
431
445
|
});
|
|
432
446
|
await hooks.callHook('onChatMessageSent', userMsg, message);
|
|
433
447
|
const skills = [];
|
|
448
|
+
const attachments = [];
|
|
434
449
|
logger.debug('withAIStream chat agent.flushStreamToShow');
|
|
435
450
|
const result = {
|
|
436
451
|
messages: await agent.flushStreamToShow(message, response, {
|
|
@@ -445,17 +460,28 @@ export function withAIStream() {
|
|
|
445
460
|
const result = {
|
|
446
461
|
messages: []
|
|
447
462
|
};
|
|
463
|
+
await hooks.callHook('onAttachmentCompose', part, respMsg, result);
|
|
464
|
+
attachments.push(part);
|
|
448
465
|
if (part.attachmentType === 'skill') {
|
|
449
|
-
|
|
450
|
-
|
|
466
|
+
const skill = part.attachment;
|
|
467
|
+
skills.push(skill);
|
|
468
|
+
await hooks.callHook('onSkillCompose', skill, respMsg, result);
|
|
451
469
|
}
|
|
452
470
|
return result.messages;
|
|
453
471
|
}
|
|
454
472
|
})
|
|
455
473
|
};
|
|
456
474
|
logger.debug('withAIStream chat agent.flushStreamToShow end');
|
|
475
|
+
let changed = false;
|
|
457
476
|
if (skills.length) {
|
|
477
|
+
changed = true;
|
|
458
478
|
await hooks.callHook('onSkillsEnd', skills, message, result);
|
|
479
|
+
}
|
|
480
|
+
if (attachments.length) {
|
|
481
|
+
changed = true;
|
|
482
|
+
await hooks.callHook('onAttachmentsEnd', attachments, message, result);
|
|
483
|
+
}
|
|
484
|
+
if (changed) {
|
|
459
485
|
await message.update();
|
|
460
486
|
}
|
|
461
487
|
let valid = false;
|
|
@@ -533,6 +559,7 @@ export function withAIStream() {
|
|
|
533
559
|
});
|
|
534
560
|
onAgentDispose(async () => {
|
|
535
561
|
const streamSession = session.get('AIStream.streamSession');
|
|
562
|
+
const connection = session.get('AIStream.connection');
|
|
536
563
|
|
|
537
564
|
// 比较低的概率 onAgentStart 还没跑完(卡在获取获取家庭信息)就调用了 dispose
|
|
538
565
|
if (streamSession) {
|
|
@@ -541,6 +568,9 @@ export function withAIStream() {
|
|
|
541
568
|
*/
|
|
542
569
|
await streamSession.close();
|
|
543
570
|
}
|
|
571
|
+
if (connection) {
|
|
572
|
+
await connection.close();
|
|
573
|
+
}
|
|
544
574
|
});
|
|
545
575
|
const handleTTTAction = async (tile, tttAction) => {
|
|
546
576
|
const result = {
|
|
@@ -611,7 +641,7 @@ export function withAIStream() {
|
|
|
611
641
|
}
|
|
612
642
|
}
|
|
613
643
|
if ((_skill$general = skill.general) !== null && _skill$general !== void 0 && (_skill$general = _skill$general.data) !== null && _skill$general !== void 0 && _skill$general.aiCards) {
|
|
614
|
-
for (const card of skill.
|
|
644
|
+
for (const card of skill.general.data.aiCards) {
|
|
615
645
|
cards.push(card);
|
|
616
646
|
}
|
|
617
647
|
}
|
|
@@ -664,6 +694,12 @@ export function withAIStream() {
|
|
|
664
694
|
onSessionEventReceived: fn => {
|
|
665
695
|
return hooks.hook('onSessionEventReceived', fn);
|
|
666
696
|
},
|
|
697
|
+
onAttachmentCompose: fn => {
|
|
698
|
+
return hooks.hook('onAttachmentCompose', fn);
|
|
699
|
+
},
|
|
700
|
+
onAttachmentsEnd: fn => {
|
|
701
|
+
return hooks.hook('onAttachmentsEnd', fn);
|
|
702
|
+
},
|
|
667
703
|
sendEvent: params => sendEventToAIStream(params),
|
|
668
704
|
onTTTAction: fn => {
|
|
669
705
|
return hooks.hook('onTTTAction', fn);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/t-agent-plugin-aistream",
|
|
3
|
-
"version": "0.2.8-beta.
|
|
3
|
+
"version": "0.2.8-beta.3",
|
|
4
4
|
"author": "Tuya.inc",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"private": false,
|
|
@@ -21,10 +21,11 @@
|
|
|
21
21
|
"scripts": {
|
|
22
22
|
"dev": "ray start --type=component --output dist --emit-declaration-dev",
|
|
23
23
|
"build": "ray build --type=component --output dist",
|
|
24
|
-
"clean": "rimraf ./dist"
|
|
24
|
+
"clean": "rimraf ./dist",
|
|
25
|
+
"test": "jest --runInBand",
|
|
26
|
+
"test:coverage": "jest --runInBand --coverage"
|
|
25
27
|
},
|
|
26
28
|
"dependencies": {
|
|
27
|
-
"dayjs": "^1.10.4",
|
|
28
29
|
"url-parse": "^1.5.10",
|
|
29
30
|
"web-streams-polyfill": "^4.0.0"
|
|
30
31
|
},
|
|
@@ -35,5 +36,5 @@
|
|
|
35
36
|
"devDependencies": {
|
|
36
37
|
"@types/url-parse": "^1.4.11"
|
|
37
38
|
},
|
|
38
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "938bc0c7da7d23dbbad8c3c14c97e5f4007f06f7"
|
|
39
40
|
}
|