@ray-js/t-agent-plugin-aistream 0.2.0-beta-2 → 0.2.0-beta-4
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/ChatHistoryStore.d.ts +31 -29
- package/dist/ChatHistoryStore.js +132 -101
- package/dist/utils/AIStream.d.ts +4 -4
- package/dist/utils/AIStream.js +15 -7
- package/dist/utils/apis.d.ts +2 -2
- package/dist/utils/apis.js +1 -1
- package/dist/utils/createAsrAgent.d.ts +79 -0
- package/dist/utils/createAsrAgent.js +197 -0
- package/dist/utils/defaultMock.js +81 -16
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +3 -1
- package/dist/utils/mock.d.ts +1 -0
- package/dist/utils/mock.js +8 -0
- package/dist/utils/promisify.d.ts +6 -0
- package/dist/utils/promisify.js +24 -4
- package/dist/utils/ttt.js +1 -1
- package/dist/withAIStream.d.ts +17 -17
- package/dist/withAIStream.js +129 -84
- package/package.json +2 -2
package/dist/withAIStream.js
CHANGED
|
@@ -2,19 +2,18 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
|
2
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
3
3
|
const _excluded = ["message"];
|
|
4
4
|
import "core-js/modules/es.array.flat.js";
|
|
5
|
+
import "core-js/modules/es.array.reverse.js";
|
|
5
6
|
import "core-js/modules/es.array.unscopables.flat.js";
|
|
6
7
|
import "core-js/modules/esnext.iterator.constructor.js";
|
|
7
8
|
import "core-js/modules/esnext.iterator.for-each.js";
|
|
8
9
|
import "core-js/modules/esnext.iterator.map.js";
|
|
9
10
|
import "core-js/modules/web.dom-collections.iterator.js";
|
|
10
11
|
import { BubbleTileStatus, ChatMessageStatus, createHooks } from '@ray-js/t-agent';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import { ChatHistoryLocalStore, ChatHistoryStore } from './ChatHistoryStore';
|
|
12
|
+
import { messageAppraise } from './utils/apis';
|
|
13
|
+
import { AIStreamObserver, getAccountInfo, getCurrentHomeInfo, runTTTAction, sendBlocksToAIStream } from './utils';
|
|
14
|
+
import { BizCode, ConnectClientType, ConnectState } from './AIStreamTypes';
|
|
15
|
+
import { ChatHistoryLocalStore } from './ChatHistoryStore';
|
|
16
16
|
import { DEFAULT_TOKEN_API, DEFAULT_TOKEN_API_VERSION, globalAIStreamClient } from './global';
|
|
17
|
-
import { runTTTAction } from '@ray-js/t-agent-plugin-assistant';
|
|
18
17
|
import logger from './utils/logger';
|
|
19
18
|
export function withAIStream() {
|
|
20
19
|
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
@@ -32,6 +31,7 @@ export function withAIStream() {
|
|
|
32
31
|
onAgentDispose,
|
|
33
32
|
onTileEvent
|
|
34
33
|
} = agent;
|
|
34
|
+
agent.session.initValue('multiModal', options.multiModal);
|
|
35
35
|
const ui = agent.plugins.ui;
|
|
36
36
|
onAgentStart(async () => {
|
|
37
37
|
/**
|
|
@@ -40,8 +40,10 @@ export function withAIStream() {
|
|
|
40
40
|
const {
|
|
41
41
|
deviceId,
|
|
42
42
|
agentId,
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
tokenOptions = {
|
|
44
|
+
api: DEFAULT_TOKEN_API,
|
|
45
|
+
version: DEFAULT_TOKEN_API_VERSION
|
|
46
|
+
}
|
|
45
47
|
} = options;
|
|
46
48
|
let homeId = options.homeId;
|
|
47
49
|
if (!homeId) {
|
|
@@ -59,32 +61,30 @@ export function withAIStream() {
|
|
|
59
61
|
let historyStore = null;
|
|
60
62
|
if (typeof options.createChatHistoryStore === 'function') {
|
|
61
63
|
historyStore = options.createChatHistoryStore(agent);
|
|
62
|
-
if (!(historyStore instanceof ChatHistoryStore)) {
|
|
63
|
-
throw new Error('createChatHistoryStore must return an instance of ChatHistoryStore');
|
|
64
|
-
}
|
|
65
64
|
} else {
|
|
66
65
|
historyStore = new ChatHistoryLocalStore({
|
|
66
|
+
bizCode: BizCode.CHAT,
|
|
67
67
|
agentId,
|
|
68
68
|
deviceId,
|
|
69
69
|
homeId,
|
|
70
70
|
indexId
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
73
|
+
if (historyStore) {
|
|
74
|
+
await agent.session.set('AIStream.historyStore', historyStore);
|
|
75
|
+
const {
|
|
76
|
+
records
|
|
77
|
+
} = await historyStore.queryAll({
|
|
78
|
+
sort: 'desc',
|
|
79
|
+
// id 倒序去获取最新的消息list,然后再reverse,保证最新的消息在最前面
|
|
80
|
+
limit: options.historySize || 1000,
|
|
81
|
+
offset: 0
|
|
82
|
+
});
|
|
83
|
+
await session.set('AIStream.rawMessages', records.reverse());
|
|
84
|
+
session.isNewChat = records.length === 0;
|
|
85
|
+
} else {
|
|
86
|
+
session.isNewChat = true;
|
|
87
|
+
}
|
|
88
88
|
session.sessionId = indexId;
|
|
89
89
|
|
|
90
90
|
// 创建 streamSession
|
|
@@ -92,12 +92,23 @@ export function withAIStream() {
|
|
|
92
92
|
clientType: options.clientType || ConnectClientType.APP,
|
|
93
93
|
deviceId: options.deviceId
|
|
94
94
|
});
|
|
95
|
+
if (options.clientType === ConnectClientType.DEVICE && !options.deviceId) {
|
|
96
|
+
throw new Error('deviceId is required when clientType is device');
|
|
97
|
+
}
|
|
95
98
|
const streamSession = connection.createSession({
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
ownerId: options.clientType === ConnectClientType.DEVICE ? options.deviceId : "".concat(homeId),
|
|
100
|
+
api: tokenOptions.api,
|
|
101
|
+
apiVersion: tokenOptions.version,
|
|
102
|
+
solutionCode: agentId,
|
|
103
|
+
extParams: tokenOptions.extParams
|
|
99
104
|
});
|
|
100
105
|
await session.set('AIStream.streamSession', streamSession);
|
|
106
|
+
if (options.earlyStart) {
|
|
107
|
+
// 故意异步,不阻塞消息列表加载
|
|
108
|
+
streamSession.ensureSession().catch(error => {
|
|
109
|
+
logger.error('earlyStart failed', error);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
101
112
|
const observer = new AIStreamObserver(data => {
|
|
102
113
|
if (data.type === 'amplitudes') {
|
|
103
114
|
ui.emitEvent('amplitudes', data);
|
|
@@ -113,22 +124,16 @@ export function withAIStream() {
|
|
|
113
124
|
});
|
|
114
125
|
await session.set('AIStream.observer', observer);
|
|
115
126
|
});
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
const streamSession = session.get('AIStream.streamSession');
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* 关闭session
|
|
125
|
-
*/
|
|
126
|
-
await streamSession.close();
|
|
127
|
-
});
|
|
127
|
+
const getHistoryStore = () => {
|
|
128
|
+
const historyStore = session.get('AIStream.historyStore');
|
|
129
|
+
return historyStore || null;
|
|
130
|
+
};
|
|
128
131
|
const persist = async message => {
|
|
129
|
-
console.log('persist', message);
|
|
130
132
|
// 对消息进行持久化,已存在的数据进行更新
|
|
131
|
-
const historyStore =
|
|
133
|
+
const historyStore = getHistoryStore();
|
|
134
|
+
if (!historyStore) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
132
137
|
const storeId = message.meta.id;
|
|
133
138
|
const messageObj = message.toObject();
|
|
134
139
|
if (storeId) {
|
|
@@ -177,7 +182,10 @@ export function withAIStream() {
|
|
|
177
182
|
if (!storeId) {
|
|
178
183
|
return;
|
|
179
184
|
}
|
|
180
|
-
const historyStore =
|
|
185
|
+
const historyStore = getHistoryStore();
|
|
186
|
+
if (!historyStore) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
181
189
|
await historyStore.remove(storeId);
|
|
182
190
|
};
|
|
183
191
|
onMessageChange(async (type, message) => {
|
|
@@ -186,13 +194,14 @@ export function withAIStream() {
|
|
|
186
194
|
}
|
|
187
195
|
});
|
|
188
196
|
const composeHandler = {
|
|
189
|
-
attachmentCompose: async () => {
|
|
190
|
-
|
|
191
|
-
// await hooks.callHook('onExtensionCompose', part.attachment, respMsg, result, 'stream');
|
|
192
|
-
// }
|
|
193
|
-
return {
|
|
197
|
+
attachmentCompose: async (respMsg, part) => {
|
|
198
|
+
const result = {
|
|
194
199
|
messages: []
|
|
195
|
-
}
|
|
200
|
+
};
|
|
201
|
+
if (part.attachmentType === 'skill') {
|
|
202
|
+
await hooks.callHook('onSkillCompose', part.attachment, respMsg, result, 'stream');
|
|
203
|
+
}
|
|
204
|
+
return result.messages;
|
|
196
205
|
}
|
|
197
206
|
};
|
|
198
207
|
const send = (blocks, signal, extraOptions) => {
|
|
@@ -303,7 +312,9 @@ export function withAIStream() {
|
|
|
303
312
|
});
|
|
304
313
|
const messages = await agent.flushStreamToShow(message, response, composeHandler);
|
|
305
314
|
if (message.bubble.status === BubbleTileStatus.ABORTED) {
|
|
306
|
-
if (
|
|
315
|
+
if (message.bubble.text) {
|
|
316
|
+
await message.persist();
|
|
317
|
+
} else {
|
|
307
318
|
await message.remove();
|
|
308
319
|
}
|
|
309
320
|
} else {
|
|
@@ -312,6 +323,67 @@ export function withAIStream() {
|
|
|
312
323
|
return [userMsg, ...messages];
|
|
313
324
|
};
|
|
314
325
|
onInputBlocksPush(chat);
|
|
326
|
+
const feedback = async _ref => {
|
|
327
|
+
var _accountInfo$miniProg;
|
|
328
|
+
let {
|
|
329
|
+
requestId,
|
|
330
|
+
type
|
|
331
|
+
} = _ref;
|
|
332
|
+
const agentId = session.get('AIStream.agentId');
|
|
333
|
+
if (!agentId) {
|
|
334
|
+
throw new Error('agentId is not found');
|
|
335
|
+
}
|
|
336
|
+
const accountInfo = await getAccountInfo();
|
|
337
|
+
const appId = accountInfo === null || accountInfo === void 0 || (_accountInfo$miniProg = accountInfo.miniProgram) === null || _accountInfo$miniProg === void 0 ? void 0 : _accountInfo$miniProg.appId;
|
|
338
|
+
if (!appId) {
|
|
339
|
+
throw new Error('appId is not found');
|
|
340
|
+
}
|
|
341
|
+
return messageAppraise({
|
|
342
|
+
aiPtChannel: agentId,
|
|
343
|
+
miniProgramId: appId,
|
|
344
|
+
requestId,
|
|
345
|
+
/** 评价(1-有帮助,2-无帮助) */
|
|
346
|
+
approveStatus: type === 'like' ? 1 : 2
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
ui.hook('onMessageFeedback', async context => {
|
|
350
|
+
const {
|
|
351
|
+
payload
|
|
352
|
+
} = context;
|
|
353
|
+
const message = session.messages.get(payload.messageId);
|
|
354
|
+
if (!message) {
|
|
355
|
+
throw new Error('message not found');
|
|
356
|
+
}
|
|
357
|
+
const {
|
|
358
|
+
eventId,
|
|
359
|
+
sessionId
|
|
360
|
+
} = message.meta;
|
|
361
|
+
if (!eventId || !sessionId) {
|
|
362
|
+
context.result = {
|
|
363
|
+
success: true
|
|
364
|
+
};
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
await feedback({
|
|
368
|
+
requestId: [eventId, sessionId].join('#'),
|
|
369
|
+
type: payload.rate
|
|
370
|
+
});
|
|
371
|
+
context.result = {
|
|
372
|
+
success: true
|
|
373
|
+
};
|
|
374
|
+
});
|
|
375
|
+
onAgentDispose(async () => {
|
|
376
|
+
const observer = session.get('AIStream.observer');
|
|
377
|
+
if (observer) {
|
|
378
|
+
observer.disconnect();
|
|
379
|
+
}
|
|
380
|
+
const streamSession = session.get('AIStream.streamSession');
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* 关闭session
|
|
384
|
+
*/
|
|
385
|
+
await streamSession.close();
|
|
386
|
+
});
|
|
315
387
|
const handleTTTAction = async (tile, tttAction) => {
|
|
316
388
|
const result = {
|
|
317
389
|
action: tttAction
|
|
@@ -368,7 +440,7 @@ export function withAIStream() {
|
|
|
368
440
|
});
|
|
369
441
|
return {
|
|
370
442
|
hooks,
|
|
371
|
-
|
|
443
|
+
aiStream: {
|
|
372
444
|
send,
|
|
373
445
|
chat,
|
|
374
446
|
composeHandler,
|
|
@@ -380,40 +452,13 @@ export function withAIStream() {
|
|
|
380
452
|
Array.from(messages).forEach(message => {
|
|
381
453
|
message.remove();
|
|
382
454
|
});
|
|
383
|
-
if (options !== null && options !== void 0 && options.indexId) {
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
455
|
// 清空缓存的数据
|
|
387
|
-
const historyStore =
|
|
388
|
-
historyStore
|
|
389
|
-
|
|
390
|
-
});
|
|
391
|
-
},
|
|
392
|
-
feedback: async _ref => {
|
|
393
|
-
var _accountInfo$miniProg;
|
|
394
|
-
let {
|
|
395
|
-
requestId,
|
|
396
|
-
type
|
|
397
|
-
} = _ref;
|
|
398
|
-
const aiPtChannel = session.get('AIStream.aiPtChannel');
|
|
399
|
-
if (!aiPtChannel) {
|
|
400
|
-
logger.error('aiPtChannel is not found');
|
|
401
|
-
return null;
|
|
402
|
-
}
|
|
403
|
-
const accountInfo = await getAccountInfo();
|
|
404
|
-
const appId = accountInfo === null || accountInfo === void 0 || (_accountInfo$miniProg = accountInfo.miniProgram) === null || _accountInfo$miniProg === void 0 ? void 0 : _accountInfo$miniProg.appId;
|
|
405
|
-
if (!appId) {
|
|
406
|
-
logger.error('appId is not found');
|
|
407
|
-
return null;
|
|
456
|
+
const historyStore = getHistoryStore();
|
|
457
|
+
if (historyStore) {
|
|
458
|
+
await historyStore.removeAll();
|
|
408
459
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
miniProgramId: appId,
|
|
412
|
-
requestId,
|
|
413
|
-
/** 评价(1-有帮助,2-无帮助) */
|
|
414
|
-
approveStatus: type === 'like' ? 1 : 2
|
|
415
|
-
});
|
|
416
|
-
}
|
|
460
|
+
},
|
|
461
|
+
feedback
|
|
417
462
|
}
|
|
418
463
|
};
|
|
419
464
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/t-agent-plugin-aistream",
|
|
3
|
-
"version": "0.2.0-beta-
|
|
3
|
+
"version": "0.2.0-beta-4",
|
|
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": "1728f8a1cac6b2ca53e506e06171808fbf38efdc"
|
|
39
39
|
}
|